2 * Copyright (c) 2010 Stefano Sabatini
3 * Copyright (c) 2008 Victor Paesa
5 * This file is part of FFmpeg.
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.
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.
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
24 * video presentation timestamp (PTS) modification filter
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"
40 static const char *const var_names[] = {
41 "FRAME_RATE", ///< defined only for constant frame-rate video
42 "INTERLACED", ///< tell if the current frame is interlaced
43 "N", ///< frame / sample number (starting at zero)
44 "NB_CONSUMED_SAMPLES", ///< number of samples consumed by the filter (only audio)
45 "NB_SAMPLES", ///< number of samples in the current frame (only audio)
46 "POS", ///< original position in the file of the frame
47 "PREV_INPTS", ///< previous input PTS
48 "PREV_INT", ///< previous input time in seconds
49 "PREV_OUTPTS", ///< previous output PTS
50 "PREV_OUTT", ///< previous output time in seconds
51 "PTS", ///< original pts in the file of the frame
52 "SAMPLE_RATE", ///< sample rate (only audio)
53 "STARTPTS", ///< PTS at start of movie
54 "STARTT", ///< time at start of movie
55 "T", ///< original time in the file of the frame
57 "RTCTIME", ///< wallclock (RTC) time in micro seconds
58 "RTCSTART", ///< wallclock (RTC) time at the start of the movie in micro seconds
59 "S", // Number of samples in the current frame
60 "SR", // Audio sample rate
61 "FR", ///< defined only for constant frame-rate video
69 VAR_NB_CONSUMED_SAMPLES,
90 typedef struct SetPTSContext {
94 double var_values[VAR_VARS_NB];
95 enum AVMediaType type;
98 static av_cold int init(AVFilterContext *ctx)
100 SetPTSContext *setpts = ctx->priv;
103 if ((ret = av_expr_parse(&setpts->expr, setpts->expr_str,
104 var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
105 av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", setpts->expr_str);
109 setpts->var_values[VAR_N] = 0.0;
110 setpts->var_values[VAR_S] = 0.0;
111 setpts->var_values[VAR_PREV_INPTS] = NAN;
112 setpts->var_values[VAR_PREV_INT] = NAN;
113 setpts->var_values[VAR_PREV_OUTPTS] = NAN;
114 setpts->var_values[VAR_PREV_OUTT] = NAN;
115 setpts->var_values[VAR_STARTPTS] = NAN;
116 setpts->var_values[VAR_STARTT] = NAN;
120 static int config_input(AVFilterLink *inlink)
122 AVFilterContext *ctx = inlink->dst;
123 SetPTSContext *setpts = ctx->priv;
125 setpts->type = inlink->type;
126 setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
127 setpts->var_values[VAR_RTCSTART] = av_gettime();
129 setpts->var_values[VAR_SR] =
130 setpts->var_values[VAR_SAMPLE_RATE] =
131 setpts->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
133 setpts->var_values[VAR_FRAME_RATE] =
134 setpts->var_values[VAR_FR] = inlink->frame_rate.num &&
135 inlink->frame_rate.den ?
136 av_q2d(inlink->frame_rate) : NAN;
138 av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f FRAME_RATE:%f SAMPLE_RATE:%f\n",
139 setpts->var_values[VAR_TB],
140 setpts->var_values[VAR_FRAME_RATE],
141 setpts->var_values[VAR_SAMPLE_RATE]);
147 static inline char *double2int64str(char *buf, double v)
149 if (isnan(v)) snprintf(buf, BUF_SIZE, "nan");
150 else snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)v);
154 static double eval_pts(SetPTSContext *setpts, AVFilterLink *inlink, AVFrame *frame, int64_t pts)
156 if (isnan(setpts->var_values[VAR_STARTPTS])) {
157 setpts->var_values[VAR_STARTPTS] = TS2D(pts);
158 setpts->var_values[VAR_STARTT ] = TS2T(pts, inlink->time_base);
160 setpts->var_values[VAR_PTS ] = TS2D(pts);
161 setpts->var_values[VAR_T ] = TS2T(pts, inlink->time_base);
162 setpts->var_values[VAR_POS ] = !frame || frame->pkt_pos == -1 ? NAN : frame->pkt_pos;
163 setpts->var_values[VAR_RTCTIME ] = av_gettime();
166 if (inlink->type == AVMEDIA_TYPE_VIDEO) {
167 setpts->var_values[VAR_INTERLACED] = frame->interlaced_frame;
168 } else if (inlink->type == AVMEDIA_TYPE_AUDIO) {
169 setpts->var_values[VAR_S] = frame->nb_samples;
170 setpts->var_values[VAR_NB_SAMPLES] = frame->nb_samples;
174 return av_expr_eval(setpts->expr, setpts->var_values, NULL);
176 #define d2istr(v) double2int64str((char[BUF_SIZE]){0}, v)
178 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
180 SetPTSContext *setpts = inlink->dst->priv;
181 int64_t in_pts = frame->pts;
184 d = eval_pts(setpts, inlink, frame, frame->pts);
185 frame->pts = D2TS(d);
187 av_log(inlink->dst, AV_LOG_TRACE,
188 "N:%"PRId64" PTS:%s T:%f POS:%s",
189 (int64_t)setpts->var_values[VAR_N],
190 d2istr(setpts->var_values[VAR_PTS]),
191 setpts->var_values[VAR_T],
192 d2istr(setpts->var_values[VAR_POS]));
193 switch (inlink->type) {
194 case AVMEDIA_TYPE_VIDEO:
195 av_log(inlink->dst, AV_LOG_TRACE, " INTERLACED:%"PRId64,
196 (int64_t)setpts->var_values[VAR_INTERLACED]);
198 case AVMEDIA_TYPE_AUDIO:
199 av_log(inlink->dst, AV_LOG_TRACE, " NB_SAMPLES:%"PRId64" NB_CONSUMED_SAMPLES:%"PRId64,
200 (int64_t)setpts->var_values[VAR_NB_SAMPLES],
201 (int64_t)setpts->var_values[VAR_NB_CONSUMED_SAMPLES]);
204 av_log(inlink->dst, AV_LOG_TRACE, " -> PTS:%s T:%f\n", d2istr(d), TS2T(d, inlink->time_base));
206 if (inlink->type == AVMEDIA_TYPE_VIDEO) {
207 setpts->var_values[VAR_N] += 1.0;
209 setpts->var_values[VAR_N] += frame->nb_samples;
212 setpts->var_values[VAR_PREV_INPTS ] = TS2D(in_pts);
213 setpts->var_values[VAR_PREV_INT ] = TS2T(in_pts, inlink->time_base);
214 setpts->var_values[VAR_PREV_OUTPTS] = TS2D(frame->pts);
215 setpts->var_values[VAR_PREV_OUTT] = TS2T(frame->pts, inlink->time_base);
216 if (setpts->type == AVMEDIA_TYPE_AUDIO) {
217 setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += frame->nb_samples;
219 return ff_filter_frame(inlink->dst->outputs[0], frame);
222 static int activate(AVFilterContext *ctx)
224 SetPTSContext *setpts = ctx->priv;
225 AVFilterLink *inlink = ctx->inputs[0];
226 AVFilterLink *outlink = ctx->outputs[0];
232 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
234 ret = ff_inlink_consume_frame(inlink, &in);
238 return filter_frame(inlink, in);
240 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
241 double d = eval_pts(setpts, inlink, NULL, pts);
243 av_log(ctx, AV_LOG_TRACE, "N:EOF PTS:%s T:%f POS:%s -> PTS:%s T:%f\n",
244 d2istr(setpts->var_values[VAR_PTS]),
245 setpts->var_values[VAR_T],
246 d2istr(setpts->var_values[VAR_POS]),
247 d2istr(d), TS2T(d, inlink->time_base));
248 ff_outlink_set_status(outlink, status, D2TS(d));
252 FF_FILTER_FORWARD_WANTED(outlink, inlink);
254 return FFERROR_NOT_READY;
257 static av_cold void uninit(AVFilterContext *ctx)
259 SetPTSContext *setpts = ctx->priv;
260 av_expr_free(setpts->expr);
264 #define OFFSET(x) offsetof(SetPTSContext, x)
265 #define V AV_OPT_FLAG_VIDEO_PARAM
266 #define A AV_OPT_FLAG_AUDIO_PARAM
267 #define F AV_OPT_FLAG_FILTERING_PARAM
269 #if CONFIG_SETPTS_FILTER
270 static const AVOption setpts_options[] = {
271 { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = V|F },
274 AVFILTER_DEFINE_CLASS(setpts);
276 static const AVFilterPad avfilter_vf_setpts_inputs[] = {
279 .type = AVMEDIA_TYPE_VIDEO,
280 .config_props = config_input,
285 static const AVFilterPad avfilter_vf_setpts_outputs[] = {
288 .type = AVMEDIA_TYPE_VIDEO,
293 AVFilter ff_vf_setpts = {
295 .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
297 .activate = activate,
300 .priv_size = sizeof(SetPTSContext),
301 .priv_class = &setpts_class,
303 .inputs = avfilter_vf_setpts_inputs,
304 .outputs = avfilter_vf_setpts_outputs,
306 #endif /* CONFIG_SETPTS_FILTER */
308 #if CONFIG_ASETPTS_FILTER
310 static const AVOption asetpts_options[] = {
311 { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = A|F },
314 AVFILTER_DEFINE_CLASS(asetpts);
316 static const AVFilterPad asetpts_inputs[] = {
319 .type = AVMEDIA_TYPE_AUDIO,
320 .config_props = config_input,
325 static const AVFilterPad asetpts_outputs[] = {
328 .type = AVMEDIA_TYPE_AUDIO,
333 AVFilter ff_af_asetpts = {
335 .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
337 .activate = activate,
339 .priv_size = sizeof(SetPTSContext),
340 .priv_class = &asetpts_class,
341 .inputs = asetpts_inputs,
342 .outputs = asetpts_outputs,
344 #endif /* CONFIG_ASETPTS_FILTER */