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
27 #include "libavutil/eval.h"
28 #include "libavutil/internal.h"
29 #include "libavutil/mathematics.h"
30 #include "libavutil/time.h"
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
53 "RTCTIME", ///< wallclock (RTC) time in micro seconds
54 "RTCSTART", ///< wallclock (RTC) time at the start of the movie in micro seconds
62 VAR_NB_CONSUMED_SAMPLES,
82 double var_values[VAR_VARS_NB];
83 enum AVMediaType type;
86 static av_cold int init(AVFilterContext *ctx, const char *args)
88 SetPTSContext *setpts = ctx->priv;
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);
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;
104 static int config_input(AVFilterLink *inlink)
106 AVFilterContext *ctx = inlink->dst;
107 SetPTSContext *setpts = ctx->priv;
109 setpts->type = inlink->type;
110 setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
111 setpts->var_values[VAR_RTCSTART] = av_gettime();
113 setpts->var_values[VAR_SAMPLE_RATE] =
114 setpts->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
116 setpts->var_values[VAR_FRAME_RATE] = inlink->frame_rate.num && inlink->frame_rate.den ?
117 av_q2d(inlink->frame_rate) : NAN;
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]);
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))
132 static inline char *double2int64str(char *buf, double v)
134 if (isnan(v)) snprintf(buf, BUF_SIZE, "nan");
135 else snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)v);
139 #define d2istr(v) double2int64str((char[BUF_SIZE]){0}, v)
141 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
143 SetPTSContext *setpts = inlink->dst->priv;
144 int64_t in_pts = frame->pts;
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);
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();
156 switch (inlink->type) {
157 case AVMEDIA_TYPE_VIDEO:
158 setpts->var_values[VAR_INTERLACED] = frame->video->interlaced;
161 case AVMEDIA_TYPE_AUDIO:
162 setpts->var_values[VAR_NB_SAMPLES] = frame->audio->nb_samples;
166 d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
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]);
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]);
185 av_log(inlink->dst, AV_LOG_DEBUG, " -> PTS:%s T:%f\n", d2istr(d), TS2T(d, inlink->time_base));
187 frame->pts = D2TS(d);
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;
197 return ff_filter_frame(inlink->dst->outputs[0], frame);
200 static av_cold void uninit(AVFilterContext *ctx)
202 SetPTSContext *setpts = ctx->priv;
203 av_expr_free(setpts->expr);
207 #if CONFIG_ASETPTS_FILTER
208 static const AVFilterPad avfilter_af_asetpts_inputs[] = {
211 .type = AVMEDIA_TYPE_AUDIO,
212 .get_audio_buffer = ff_null_get_audio_buffer,
213 .config_props = config_input,
214 .filter_frame = filter_frame,
219 static const AVFilterPad avfilter_af_asetpts_outputs[] = {
222 .type = AVMEDIA_TYPE_AUDIO,
227 AVFilter avfilter_af_asetpts = {
229 .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
232 .priv_size = sizeof(SetPTSContext),
233 .inputs = avfilter_af_asetpts_inputs,
234 .outputs = avfilter_af_asetpts_outputs,
236 #endif /* CONFIG_ASETPTS_FILTER */
238 #if CONFIG_SETPTS_FILTER
239 static const AVFilterPad avfilter_vf_setpts_inputs[] = {
242 .type = AVMEDIA_TYPE_VIDEO,
243 .get_video_buffer = ff_null_get_video_buffer,
244 .config_props = config_input,
245 .filter_frame = filter_frame,
250 static const AVFilterPad avfilter_vf_setpts_outputs[] = {
253 .type = AVMEDIA_TYPE_VIDEO,
258 AVFilter avfilter_vf_setpts = {
260 .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
264 .priv_size = sizeof(SetPTSContext),
266 .inputs = avfilter_vf_setpts_inputs,
267 .outputs = avfilter_vf_setpts_outputs,
269 #endif /* CONFIG_SETPTS_FILTER */