mp3: Properly use AVCodecContext API
[platform/upstream/libav.git] / libavfilter / setpts.c
1 /*
2  * Copyright (c) 2010 Stefano Sabatini
3  * Copyright (c) 2008 Victor Paesa
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * video presentation timestamp (PTS) modification filter
25  */
26
27 #include <inttypes.h>
28
29 #include "libavutil/eval.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/time.h"
34
35 #include "audio.h"
36 #include "avfilter.h"
37 #include "internal.h"
38 #include "video.h"
39
40 #include "config.h"
41
42 static const char *const var_names[] = {
43     "E",           ///< Euler number
44     "INTERLACED",  ///< tell if the current frame is interlaced
45     "N",           ///< frame / sample number (starting at zero)
46     "PHI",         ///< golden ratio
47     "PI",          ///< greek pi
48     "PREV_INPTS",  ///< previous  input PTS
49     "PREV_OUTPTS", ///< previous output PTS
50     "PTS",         ///< original pts in the file of the frame
51     "STARTPTS",    ///< PTS at start of movie
52     "TB",          ///< timebase
53     "RTCTIME",     ///< wallclock (RTC) time in micro seconds
54     "RTCSTART",    ///< wallclock (RTC) time at the start of the movie in micro seconds
55     "S",           //   Number of samples in the current frame
56     "SR",          //   Audio sample rate
57     NULL
58 };
59
60 enum var_name {
61     VAR_E,
62     VAR_INTERLACED,
63     VAR_N,
64     VAR_PHI,
65     VAR_PI,
66     VAR_PREV_INPTS,
67     VAR_PREV_OUTPTS,
68     VAR_PTS,
69     VAR_STARTPTS,
70     VAR_TB,
71     VAR_RTCTIME,
72     VAR_RTCSTART,
73     VAR_S,
74     VAR_SR,
75     VAR_VARS_NB
76 };
77
78 typedef struct SetPTSContext {
79     const AVClass *class;
80     char *expr_str;
81     AVExpr *expr;
82     double var_values[VAR_VARS_NB];
83 } SetPTSContext;
84
85 static av_cold int init(AVFilterContext *ctx)
86 {
87     SetPTSContext *setpts = ctx->priv;
88     int ret;
89
90     if ((ret = av_expr_parse(&setpts->expr, setpts->expr_str,
91                              var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
92         av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", setpts->expr_str);
93         return ret;
94     }
95
96     setpts->var_values[VAR_E]           = M_E;
97     setpts->var_values[VAR_N]           = 0.0;
98     setpts->var_values[VAR_S]           = 0.0;
99     setpts->var_values[VAR_PHI]         = M_PHI;
100     setpts->var_values[VAR_PI]          = M_PI;
101     setpts->var_values[VAR_PREV_INPTS]  = NAN;
102     setpts->var_values[VAR_PREV_OUTPTS] = NAN;
103     setpts->var_values[VAR_STARTPTS]    = NAN;
104     return 0;
105 }
106
107 static int config_input(AVFilterLink *inlink)
108 {
109     SetPTSContext *setpts = inlink->dst->priv;
110
111     setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
112     setpts->var_values[VAR_RTCSTART] = av_gettime();
113
114     if (inlink->type == AVMEDIA_TYPE_AUDIO) {
115         setpts->var_values[VAR_SR] = inlink->sample_rate;
116     }
117
118     av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f\n", setpts->var_values[VAR_TB]);
119     return 0;
120 }
121
122 #define D2TS(d)  (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
123 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
124
125 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
126 {
127     SetPTSContext *setpts = inlink->dst->priv;
128     int64_t in_pts = frame->pts;
129     double d;
130
131     if (isnan(setpts->var_values[VAR_STARTPTS]))
132         setpts->var_values[VAR_STARTPTS] = TS2D(frame->pts);
133
134     setpts->var_values[VAR_PTS       ] = TS2D(frame->pts);
135     setpts->var_values[VAR_RTCTIME   ] = av_gettime();
136
137     if (inlink->type == AVMEDIA_TYPE_VIDEO) {
138         setpts->var_values[VAR_INTERLACED] = frame->interlaced_frame;
139     } else {
140         setpts->var_values[VAR_S] = frame->nb_samples;
141     }
142
143     d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
144     frame->pts = D2TS(d);
145
146     av_dlog(inlink->dst,
147             "n:%"PRId64" interlaced:%d pts:%"PRId64" t:%f -> pts:%"PRId64" t:%f\n",
148             (int64_t)setpts->var_values[VAR_N],
149             (int)setpts->var_values[VAR_INTERLACED],
150             in_pts, in_pts * av_q2d(inlink->time_base),
151             frame->pts, frame->pts * av_q2d(inlink->time_base));
152
153     if (inlink->type == AVMEDIA_TYPE_VIDEO) {
154         setpts->var_values[VAR_N] += 1.0;
155     } else {
156         setpts->var_values[VAR_N] += frame->nb_samples;
157     }
158
159     setpts->var_values[VAR_PREV_INPTS ] = TS2D(in_pts);
160     setpts->var_values[VAR_PREV_OUTPTS] = TS2D(frame->pts);
161     return ff_filter_frame(inlink->dst->outputs[0], frame);
162 }
163
164 static av_cold void uninit(AVFilterContext *ctx)
165 {
166     SetPTSContext *setpts = ctx->priv;
167     av_expr_free(setpts->expr);
168     setpts->expr = NULL;
169 }
170
171 #define OFFSET(x) offsetof(SetPTSContext, x)
172 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM
173 static const AVOption options[] = {
174     { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = FLAGS },
175     { NULL },
176 };
177
178 #if CONFIG_SETPTS_FILTER
179 static const AVClass setpts_class = {
180     .class_name = "setpts",
181     .item_name  = av_default_item_name,
182     .option     = options,
183     .version    = LIBAVUTIL_VERSION_INT,
184 };
185
186 static const AVFilterPad avfilter_vf_setpts_inputs[] = {
187     {
188         .name             = "default",
189         .type             = AVMEDIA_TYPE_VIDEO,
190         .get_video_buffer = ff_null_get_video_buffer,
191         .config_props     = config_input,
192         .filter_frame     = filter_frame,
193     },
194     { NULL }
195 };
196
197 static const AVFilterPad avfilter_vf_setpts_outputs[] = {
198     {
199         .name = "default",
200         .type = AVMEDIA_TYPE_VIDEO,
201     },
202     { NULL }
203 };
204
205 AVFilter ff_vf_setpts = {
206     .name      = "setpts",
207     .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
208     .init      = init,
209     .uninit    = uninit,
210
211     .priv_size = sizeof(SetPTSContext),
212     .priv_class = &setpts_class,
213
214     .inputs    = avfilter_vf_setpts_inputs,
215     .outputs   = avfilter_vf_setpts_outputs,
216 };
217 #endif
218
219 #if CONFIG_ASETPTS_FILTER
220 static const AVClass asetpts_class = {
221     .class_name = "asetpts",
222     .item_name  = av_default_item_name,
223     .option     = options,
224     .version    = LIBAVUTIL_VERSION_INT,
225 };
226
227 static const AVFilterPad asetpts_inputs[] = {
228     {
229         .name             = "default",
230         .type             = AVMEDIA_TYPE_AUDIO,
231         .get_audio_buffer = ff_null_get_audio_buffer,
232         .config_props     = config_input,
233         .filter_frame     = filter_frame,
234     },
235     { NULL }
236 };
237
238 static const AVFilterPad asetpts_outputs[] = {
239     {
240         .name = "default",
241         .type = AVMEDIA_TYPE_AUDIO,
242     },
243     { NULL }
244 };
245
246 AVFilter ff_af_asetpts = {
247     .name        = "asetpts",
248     .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
249     .init        = init,
250     .uninit      = uninit,
251
252     .priv_size  = sizeof(SetPTSContext),
253     .priv_class = &asetpts_class,
254
255     .inputs    = asetpts_inputs,
256     .outputs   = asetpts_outputs,
257 };
258 #endif