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"
37 static const char *const var_names[] = {
38 "FRAME_RATE", ///< defined only for constant frame-rate video
39 "INTERLACED", ///< tell if the current frame is interlaced
40 "N", ///< frame number (starting at zero)
41 "NB_CONSUMED_SAMPLES", ///< number of samples consumed by the filter (only audio)
42 "NB_SAMPLES", ///< number of samples in the current frame (only audio)
43 "POS", ///< original position in the file of the frame
44 "PREV_INPTS", ///< previous input PTS
45 "PREV_INT", ///< previous input time in seconds
46 "PREV_OUTPTS", ///< previous output PTS
47 "PREV_OUTT", ///< previous output time in seconds
48 "PTS", ///< original pts in the file of the frame
49 "SAMPLE_RATE", ///< sample rate (only audio)
50 "STARTPTS", ///< PTS at start of movie
51 "STARTT", ///< time at start of movie
52 "T", ///< original time in the file of the frame
61 VAR_NB_CONSUMED_SAMPLES,
79 double var_values[VAR_VARS_NB];
80 enum AVMediaType type;
83 static av_cold int init(AVFilterContext *ctx, const char *args)
85 SetPTSContext *setpts = ctx->priv;
88 if ((ret = av_expr_parse(&setpts->expr, args ? args : "PTS",
89 var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
90 av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
94 setpts->var_values[VAR_N ] = 0.0;
95 setpts->var_values[VAR_PREV_INPTS ] = setpts->var_values[VAR_PREV_INT ] = NAN;
96 setpts->var_values[VAR_PREV_OUTPTS] = setpts->var_values[VAR_PREV_OUTT] = NAN;
97 setpts->var_values[VAR_STARTPTS ] = setpts->var_values[VAR_STARTT ] = NAN;
101 static int config_input(AVFilterLink *inlink)
103 AVFilterContext *ctx = inlink->dst;
104 SetPTSContext *setpts = ctx->priv;
106 setpts->type = inlink->type;
107 setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
109 setpts->var_values[VAR_SAMPLE_RATE] =
110 setpts->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
112 setpts->var_values[VAR_FRAME_RATE] = inlink->frame_rate.num && inlink->frame_rate.den ?
113 av_q2d(inlink->frame_rate) : NAN;
115 av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f FRAME_RATE:%f SAMPLE_RATE:%f\n",
116 setpts->var_values[VAR_TB],
117 setpts->var_values[VAR_FRAME_RATE],
118 setpts->var_values[VAR_SAMPLE_RATE]);
122 #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
123 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
124 #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
126 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
128 SetPTSContext *setpts = inlink->dst->priv;
129 int64_t in_pts = frame->pts;
132 if (isnan(setpts->var_values[VAR_STARTPTS])) {
133 setpts->var_values[VAR_STARTPTS] = TS2D(frame->pts);
134 setpts->var_values[VAR_STARTT ] = TS2T(frame->pts, inlink->time_base);
136 setpts->var_values[VAR_PTS ] = TS2D(frame->pts);
137 setpts->var_values[VAR_T ] = TS2T(frame->pts, inlink->time_base);
138 setpts->var_values[VAR_POS ] = frame->pos == -1 ? NAN : frame->pos;
140 switch (inlink->type) {
141 case AVMEDIA_TYPE_VIDEO:
142 setpts->var_values[VAR_INTERLACED] = frame->video->interlaced;
145 case AVMEDIA_TYPE_AUDIO:
146 setpts->var_values[VAR_NB_SAMPLES] = frame->audio->nb_samples;
150 d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
151 frame->pts = D2TS(d);
153 setpts->var_values[VAR_PREV_INPTS ] = TS2D(in_pts);
154 setpts->var_values[VAR_PREV_INT ] = TS2T(in_pts, inlink->time_base);
155 setpts->var_values[VAR_PREV_OUTPTS] = TS2D(frame->pts);
156 setpts->var_values[VAR_PREV_OUTT] = TS2T(frame->pts, inlink->time_base);
159 "n:%"PRId64" interlaced:%d nb_samples:%d nb_consumed_samples:%d "
160 "pos:%"PRId64" pts:%"PRId64" t:%f -> pts:%"PRId64" t:%f\n",
161 (int64_t)setpts->var_values[VAR_N],
162 (int)setpts->var_values[VAR_INTERLACED],
163 (int)setpts->var_values[VAR_NB_SAMPLES],
164 (int)setpts->var_values[VAR_NB_CONSUMED_SAMPLES],
165 (int64_t)setpts->var_values[VAR_POS],
166 (int64_t)setpts->var_values[VAR_PREV_INPTS],
167 setpts->var_values[VAR_PREV_INT],
168 (int64_t)setpts->var_values[VAR_PREV_OUTPTS],
169 setpts->var_values[VAR_PREV_OUTT]);
171 setpts->var_values[VAR_N] += 1.0;
172 if (setpts->type == AVMEDIA_TYPE_AUDIO) {
173 setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += frame->audio->nb_samples;
175 return ff_filter_frame(inlink->dst->outputs[0], frame);
178 static av_cold void uninit(AVFilterContext *ctx)
180 SetPTSContext *setpts = ctx->priv;
181 av_expr_free(setpts->expr);
185 #if CONFIG_ASETPTS_FILTER
186 static const AVFilterPad avfilter_af_asetpts_inputs[] = {
189 .type = AVMEDIA_TYPE_AUDIO,
190 .get_audio_buffer = ff_null_get_audio_buffer,
191 .config_props = config_input,
192 .filter_frame = filter_frame,
197 static const AVFilterPad avfilter_af_asetpts_outputs[] = {
200 .type = AVMEDIA_TYPE_AUDIO,
205 AVFilter avfilter_af_asetpts = {
207 .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
210 .priv_size = sizeof(SetPTSContext),
211 .inputs = avfilter_af_asetpts_inputs,
212 .outputs = avfilter_af_asetpts_outputs,
214 #endif /* CONFIG_ASETPTS_FILTER */
216 #if CONFIG_SETPTS_FILTER
217 static const AVFilterPad avfilter_vf_setpts_inputs[] = {
220 .type = AVMEDIA_TYPE_VIDEO,
221 .get_video_buffer = ff_null_get_video_buffer,
222 .config_props = config_input,
223 .filter_frame = filter_frame,
228 static const AVFilterPad avfilter_vf_setpts_outputs[] = {
231 .type = AVMEDIA_TYPE_VIDEO,
236 AVFilter avfilter_vf_setpts = {
238 .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
242 .priv_size = sizeof(SetPTSContext),
244 .inputs = avfilter_vf_setpts_inputs,
245 .outputs = avfilter_vf_setpts_outputs,
247 #endif /* CONFIG_SETPTS_FILTER */