]> git.sesse.net Git - ffmpeg/blob - libavfilter/f_setpts.c
Merge commit '1647da89dd8ac09a55c111589f7a30d7e6b87d90'
[ffmpeg] / libavfilter / f_setpts.c
1 /*
2  * Copyright (c) 2010 Stefano Sabatini
3  * Copyright (c) 2008 Victor Paesa
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 "libavutil/eval.h"
28 #include "libavutil/internal.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/time.h"
31 #include "avfilter.h"
32 #include "internal.h"
33 #include "audio.h"
34 #include "video.h"
35
36 static const char *const var_names[] = {
37     "FRAME_RATE",  ///< defined only for constant frame-rate video
38     "INTERLACED",  ///< tell if the current frame is interlaced
39     "N",           ///< frame number (starting at zero)
40     "NB_CONSUMED_SAMPLES", ///< number of samples consumed by the filter (only audio)
41     "NB_SAMPLES",  ///< number of samples in the current frame (only audio)
42     "POS",         ///< original position in the file of the frame
43     "PREV_INPTS",  ///< previous  input PTS
44     "PREV_INT",    ///< previous  input time in seconds
45     "PREV_OUTPTS", ///< previous output PTS
46     "PREV_OUTT",   ///< previous output time in seconds
47     "PTS",         ///< original pts in the file of the frame
48     "SAMPLE_RATE", ///< sample rate (only audio)
49     "STARTPTS",    ///< PTS at start of movie
50     "STARTT",      ///< time at start of movie
51     "T",           ///< original time in the file of the frame
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     NULL
56 };
57
58 enum var_name {
59     VAR_FRAME_RATE,
60     VAR_INTERLACED,
61     VAR_N,
62     VAR_NB_CONSUMED_SAMPLES,
63     VAR_NB_SAMPLES,
64     VAR_POS,
65     VAR_PREV_INPTS,
66     VAR_PREV_INT,
67     VAR_PREV_OUTPTS,
68     VAR_PREV_OUTT,
69     VAR_PTS,
70     VAR_SAMPLE_RATE,
71     VAR_STARTPTS,
72     VAR_STARTT,
73     VAR_T,
74     VAR_TB,
75     VAR_RTCTIME,
76     VAR_RTCSTART,
77     VAR_VARS_NB
78 };
79
80 typedef struct {
81     AVExpr *expr;
82     double var_values[VAR_VARS_NB];
83     enum AVMediaType type;
84 } SetPTSContext;
85
86 static av_cold int init(AVFilterContext *ctx, const char *args)
87 {
88     SetPTSContext *setpts = ctx->priv;
89     int ret;
90
91     if ((ret = av_expr_parse(&setpts->expr, args ? args : "PTS",
92                              var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
93         av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
94         return ret;
95     }
96
97     setpts->var_values[VAR_N          ] = 0.0;
98     setpts->var_values[VAR_PREV_INPTS ] = setpts->var_values[VAR_PREV_INT ] = NAN;
99     setpts->var_values[VAR_PREV_OUTPTS] = setpts->var_values[VAR_PREV_OUTT] = NAN;
100     setpts->var_values[VAR_STARTPTS   ] = setpts->var_values[VAR_STARTT   ] = NAN;
101     return 0;
102 }
103
104 static int config_input(AVFilterLink *inlink)
105 {
106     AVFilterContext *ctx = inlink->dst;
107     SetPTSContext *setpts = ctx->priv;
108
109     setpts->type = inlink->type;
110     setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
111     setpts->var_values[VAR_RTCSTART] = av_gettime();
112
113     setpts->var_values[VAR_SAMPLE_RATE] =
114         setpts->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
115
116     setpts->var_values[VAR_FRAME_RATE] = inlink->frame_rate.num && inlink->frame_rate.den ?
117         av_q2d(inlink->frame_rate) : NAN;
118
119     av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f FRAME_RATE:%f SAMPLE_RATE:%f\n",
120            setpts->var_values[VAR_TB],
121            setpts->var_values[VAR_FRAME_RATE],
122            setpts->var_values[VAR_SAMPLE_RATE]);
123     return 0;
124 }
125
126 #define D2TS(d)  (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
127 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
128 #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
129
130 #define BUF_SIZE 64
131
132 static inline char *double2int64str(char *buf, double v)
133 {
134     if (isnan(v)) snprintf(buf, BUF_SIZE, "nan");
135     else          snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)v);
136     return buf;
137 }
138
139 #define d2istr(v) double2int64str((char[BUF_SIZE]){0}, v)
140
141 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
142 {
143     SetPTSContext *setpts = inlink->dst->priv;
144     int64_t in_pts = frame->pts;
145     double d;
146
147     if (isnan(setpts->var_values[VAR_STARTPTS])) {
148         setpts->var_values[VAR_STARTPTS] = TS2D(frame->pts);
149         setpts->var_values[VAR_STARTT  ] = TS2T(frame->pts, inlink->time_base);
150     }
151     setpts->var_values[VAR_PTS       ] = TS2D(frame->pts);
152     setpts->var_values[VAR_T         ] = TS2T(frame->pts, inlink->time_base);
153     setpts->var_values[VAR_POS       ] = frame->pos == -1 ? NAN : frame->pos;
154     setpts->var_values[VAR_RTCTIME   ] = av_gettime();
155
156     switch (inlink->type) {
157     case AVMEDIA_TYPE_VIDEO:
158         setpts->var_values[VAR_INTERLACED] = frame->video->interlaced;
159         break;
160
161     case AVMEDIA_TYPE_AUDIO:
162         setpts->var_values[VAR_NB_SAMPLES] = frame->audio->nb_samples;
163         break;
164     }
165
166     d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
167
168     av_log(inlink->dst, AV_LOG_DEBUG,
169            "N:%"PRId64" PTS:%s T:%f POS:%s",
170            (int64_t)setpts->var_values[VAR_N],
171            d2istr(setpts->var_values[VAR_PTS]),
172            setpts->var_values[VAR_T],
173            d2istr(setpts->var_values[VAR_POS]));
174     switch (inlink->type) {
175     case AVMEDIA_TYPE_VIDEO:
176         av_log(inlink->dst, AV_LOG_DEBUG, " INTERLACED:%"PRId64,
177                (int64_t)setpts->var_values[VAR_INTERLACED]);
178         break;
179     case AVMEDIA_TYPE_AUDIO:
180         av_log(inlink->dst, AV_LOG_DEBUG, " NB_SAMPLES:%"PRId64" NB_CONSUMED_SAMPLES:%"PRId64,
181                (int64_t)setpts->var_values[VAR_NB_SAMPLES],
182                (int64_t)setpts->var_values[VAR_NB_CONSUMED_SAMPLES]);
183         break;
184     }
185     av_log(inlink->dst, AV_LOG_DEBUG, " -> PTS:%s T:%f\n", d2istr(d), TS2T(d, inlink->time_base));
186
187     frame->pts = D2TS(d);
188
189     setpts->var_values[VAR_PREV_INPTS ] = TS2D(in_pts);
190     setpts->var_values[VAR_PREV_INT   ] = TS2T(in_pts, inlink->time_base);
191     setpts->var_values[VAR_PREV_OUTPTS] = TS2D(frame->pts);
192     setpts->var_values[VAR_PREV_OUTT]   = TS2T(frame->pts, inlink->time_base);
193     setpts->var_values[VAR_N] += 1.0;
194     if (setpts->type == AVMEDIA_TYPE_AUDIO) {
195         setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += frame->audio->nb_samples;
196     }
197     return ff_filter_frame(inlink->dst->outputs[0], frame);
198 }
199
200 static av_cold void uninit(AVFilterContext *ctx)
201 {
202     SetPTSContext *setpts = ctx->priv;
203     av_expr_free(setpts->expr);
204     setpts->expr = NULL;
205 }
206
207 #if CONFIG_ASETPTS_FILTER
208 static const AVFilterPad avfilter_af_asetpts_inputs[] = {
209     {
210         .name             = "default",
211         .type             = AVMEDIA_TYPE_AUDIO,
212         .get_audio_buffer = ff_null_get_audio_buffer,
213         .config_props     = config_input,
214         .filter_frame     = filter_frame,
215     },
216     { NULL }
217 };
218
219 static const AVFilterPad avfilter_af_asetpts_outputs[] = {
220     {
221         .name = "default",
222         .type = AVMEDIA_TYPE_AUDIO,
223     },
224     { NULL }
225 };
226
227 AVFilter avfilter_af_asetpts = {
228     .name      = "asetpts",
229     .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
230     .init      = init,
231     .uninit    = uninit,
232     .priv_size = sizeof(SetPTSContext),
233     .inputs    = avfilter_af_asetpts_inputs,
234     .outputs   = avfilter_af_asetpts_outputs,
235 };
236 #endif /* CONFIG_ASETPTS_FILTER */
237
238 #if CONFIG_SETPTS_FILTER
239 static const AVFilterPad avfilter_vf_setpts_inputs[] = {
240     {
241         .name             = "default",
242         .type             = AVMEDIA_TYPE_VIDEO,
243         .get_video_buffer = ff_null_get_video_buffer,
244         .config_props     = config_input,
245         .filter_frame     = filter_frame,
246     },
247     { NULL }
248 };
249
250 static const AVFilterPad avfilter_vf_setpts_outputs[] = {
251     {
252         .name = "default",
253         .type = AVMEDIA_TYPE_VIDEO,
254     },
255     { NULL }
256 };
257
258 AVFilter avfilter_vf_setpts = {
259     .name      = "setpts",
260     .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
261     .init      = init,
262     .uninit    = uninit,
263
264     .priv_size = sizeof(SetPTSContext),
265
266     .inputs    = avfilter_vf_setpts_inputs,
267     .outputs   = avfilter_vf_setpts_outputs,
268 };
269 #endif /* CONFIG_SETPTS_FILTER */