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"
35 static const char *const var_names[] = {
36 "FRAME_RATE", ///< defined only for constant frame-rate video
37 "INTERLACED", ///< tell if the current frame is interlaced
38 "N", ///< frame number (starting at zero)
39 "NB_CONSUMED_SAMPLES", ///< number of samples consumed by the filter (only audio)
40 "NB_SAMPLES", ///< number of samples in the current frame (only audio)
41 "POS", ///< original position in the file of the frame
42 "PREV_INPTS", ///< previous input PTS
43 "PREV_INT", ///< previous input time in seconds
44 "PREV_OUTPTS", ///< previous output PTS
45 "PREV_OUTT", ///< previous output time in seconds
46 "PTS", ///< original pts in the file of the frame
47 "SAMPLE_RATE", ///< sample rate (only audio)
48 "STARTPTS", ///< PTS at start of movie
49 "STARTT", ///< time at start of movie
50 "T", ///< original time in the file of the frame
59 VAR_NB_CONSUMED_SAMPLES,
77 double var_values[VAR_VARS_NB];
78 enum AVMediaType type;
81 static av_cold int init(AVFilterContext *ctx, const char *args)
83 SetPTSContext *setpts = ctx->priv;
86 if ((ret = av_expr_parse(&setpts->expr, args ? args : "PTS",
87 var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
88 av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
92 setpts->var_values[VAR_N ] = 0.0;
93 setpts->var_values[VAR_PREV_INPTS ] = setpts->var_values[VAR_PREV_INT ] = NAN;
94 setpts->var_values[VAR_PREV_OUTPTS] = setpts->var_values[VAR_PREV_OUTT] = NAN;
95 setpts->var_values[VAR_STARTPTS ] = setpts->var_values[VAR_STARTT ] = NAN;
99 static int config_input(AVFilterLink *inlink)
101 AVFilterContext *ctx = inlink->dst;
102 SetPTSContext *setpts = ctx->priv;
104 setpts->type = inlink->type;
105 setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
107 setpts->var_values[VAR_SAMPLE_RATE] =
108 setpts->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
110 setpts->var_values[VAR_FRAME_RATE] = inlink->frame_rate.num && inlink->frame_rate.den ?
111 av_q2d(inlink->frame_rate) : NAN;
113 av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f FRAME_RATE:%f SAMPLE_RATE:%f\n",
114 setpts->var_values[VAR_TB],
115 setpts->var_values[VAR_FRAME_RATE],
116 setpts->var_values[VAR_SAMPLE_RATE]);
120 #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
121 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
122 #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
126 static inline char *double2int64str(char *buf, double v)
128 if (isnan(v)) snprintf(buf, BUF_SIZE, "nan");
129 else snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)v);
133 #define d2istr(v) double2int64str((char[BUF_SIZE]){0}, v)
135 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
137 SetPTSContext *setpts = inlink->dst->priv;
138 int64_t in_pts = frame->pts;
141 if (isnan(setpts->var_values[VAR_STARTPTS])) {
142 setpts->var_values[VAR_STARTPTS] = TS2D(frame->pts);
143 setpts->var_values[VAR_STARTT ] = TS2T(frame->pts, inlink->time_base);
145 setpts->var_values[VAR_PTS ] = TS2D(frame->pts);
146 setpts->var_values[VAR_T ] = TS2T(frame->pts, inlink->time_base);
147 setpts->var_values[VAR_POS ] = frame->pos == -1 ? NAN : frame->pos;
149 switch (inlink->type) {
150 case AVMEDIA_TYPE_VIDEO:
151 setpts->var_values[VAR_INTERLACED] = frame->video->interlaced;
154 case AVMEDIA_TYPE_AUDIO:
155 setpts->var_values[VAR_NB_SAMPLES] = frame->audio->nb_samples;
159 d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
161 av_log(inlink->dst, AV_LOG_DEBUG,
162 "N:%"PRId64" PTS:%s T:%f POS:%s",
163 (int64_t)setpts->var_values[VAR_N],
164 d2istr(setpts->var_values[VAR_PTS]),
165 setpts->var_values[VAR_T],
166 d2istr(setpts->var_values[VAR_POS]));
167 switch (inlink->type) {
168 case AVMEDIA_TYPE_VIDEO:
169 av_log(inlink->dst, AV_LOG_DEBUG, " INTERLACED:%"PRId64,
170 (int64_t)setpts->var_values[VAR_INTERLACED]);
172 case AVMEDIA_TYPE_AUDIO:
173 av_log(inlink->dst, AV_LOG_DEBUG, " NB_SAMPLES:%"PRId64" NB_CONSUMED_SAMPLES:%"PRId64,
174 (int64_t)setpts->var_values[VAR_NB_SAMPLES],
175 (int64_t)setpts->var_values[VAR_NB_CONSUMED_SAMPLES]);
178 av_log(inlink->dst, AV_LOG_DEBUG, " -> PTS:%s T:%f\n", d2istr(d), TS2T(d, inlink->time_base));
180 frame->pts = D2TS(d);
182 setpts->var_values[VAR_PREV_INPTS ] = TS2D(in_pts);
183 setpts->var_values[VAR_PREV_INT ] = TS2T(in_pts, inlink->time_base);
184 setpts->var_values[VAR_PREV_OUTPTS] = TS2D(frame->pts);
185 setpts->var_values[VAR_PREV_OUTT] = TS2T(frame->pts, inlink->time_base);
186 setpts->var_values[VAR_N] += 1.0;
187 if (setpts->type == AVMEDIA_TYPE_AUDIO) {
188 setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += frame->audio->nb_samples;
190 return ff_filter_frame(inlink->dst->outputs[0], frame);
193 static av_cold void uninit(AVFilterContext *ctx)
195 SetPTSContext *setpts = ctx->priv;
196 av_expr_free(setpts->expr);
200 #if CONFIG_ASETPTS_FILTER
201 static const AVFilterPad avfilter_af_asetpts_inputs[] = {
204 .type = AVMEDIA_TYPE_AUDIO,
205 .get_audio_buffer = ff_null_get_audio_buffer,
206 .config_props = config_input,
207 .filter_frame = filter_frame,
212 static const AVFilterPad avfilter_af_asetpts_outputs[] = {
215 .type = AVMEDIA_TYPE_AUDIO,
220 AVFilter avfilter_af_asetpts = {
222 .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
225 .priv_size = sizeof(SetPTSContext),
226 .inputs = avfilter_af_asetpts_inputs,
227 .outputs = avfilter_af_asetpts_outputs,
229 #endif /* CONFIG_ASETPTS_FILTER */
231 #if CONFIG_SETPTS_FILTER
232 static const AVFilterPad avfilter_vf_setpts_inputs[] = {
235 .type = AVMEDIA_TYPE_VIDEO,
236 .get_video_buffer = ff_null_get_video_buffer,
237 .config_props = config_input,
238 .filter_frame = filter_frame,
243 static const AVFilterPad avfilter_vf_setpts_outputs[] = {
246 .type = AVMEDIA_TYPE_VIDEO,
251 AVFilter avfilter_vf_setpts = {
253 .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
257 .priv_size = sizeof(SetPTSContext),
259 .inputs = avfilter_vf_setpts_inputs,
260 .outputs = avfilter_vf_setpts_outputs,
262 #endif /* CONFIG_SETPTS_FILTER */