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 "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
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 if (setpts->type == AVMEDIA_TYPE_AUDIO)
108 setpts->var_values[VAR_SAMPLE_RATE] = inlink->sample_rate;
110 av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f SAMPLE_RATE:%f\n",
111 setpts->var_values[VAR_TB], setpts->var_values[VAR_SAMPLE_RATE]);
115 #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
116 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
117 #define TS2T(ts, tb) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts)*av_q2d(tb))
119 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
121 SetPTSContext *setpts = inlink->dst->priv;
123 AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
126 return AVERROR(ENOMEM);
128 if (isnan(setpts->var_values[VAR_STARTPTS])) {
129 setpts->var_values[VAR_STARTPTS] = TS2D(inpicref->pts);
130 setpts->var_values[VAR_STARTT ] = TS2T(inpicref->pts, inlink->time_base);
132 setpts->var_values[VAR_PTS ] = TS2D(inpicref->pts);
133 setpts->var_values[VAR_T ] = TS2T(inpicref->pts, inlink->time_base);
134 setpts->var_values[VAR_POS ] = inpicref->pos == -1 ? NAN : inpicref->pos;
136 switch (inlink->type) {
137 case AVMEDIA_TYPE_VIDEO:
138 setpts->var_values[VAR_INTERLACED] = inpicref->video->interlaced;
141 case AVMEDIA_TYPE_AUDIO:
142 setpts->var_values[VAR_NB_SAMPLES] = inpicref->audio->nb_samples;
146 d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
147 outpicref->pts = D2TS(d);
149 setpts->var_values[VAR_PREV_INPTS ] = TS2D(inpicref ->pts);
150 setpts->var_values[VAR_PREV_INT ] = TS2T(inpicref ->pts, inlink->time_base);
151 setpts->var_values[VAR_PREV_OUTPTS] = TS2D(outpicref->pts);
152 setpts->var_values[VAR_PREV_OUTT] = TS2T(outpicref->pts, inlink->time_base);
155 "n:%"PRId64" interlaced:%d nb_samples:%d nb_consumed_samples:%d "
156 "pos:%"PRId64" pts:%"PRId64" t:%f -> pts:%"PRId64" t:%f\n",
157 (int64_t)setpts->var_values[VAR_N],
158 (int)setpts->var_values[VAR_INTERLACED],
159 (int)setpts->var_values[VAR_NB_SAMPLES],
160 (int)setpts->var_values[VAR_NB_CONSUMED_SAMPLES],
161 (int64_t)setpts->var_values[VAR_POS],
162 (int64_t)setpts->var_values[VAR_PREV_INPTS],
163 setpts->var_values[VAR_PREV_INT],
164 (int64_t)setpts->var_values[VAR_PREV_OUTPTS],
165 setpts->var_values[VAR_PREV_OUTT]);
167 setpts->var_values[VAR_N] += 1.0;
168 if (setpts->type == AVMEDIA_TYPE_AUDIO) {
169 setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += inpicref->audio->nb_samples;
170 return ff_filter_samples(inlink->dst->outputs[0], outpicref);
172 return ff_start_frame (inlink->dst->outputs[0], outpicref);
175 static av_cold void uninit(AVFilterContext *ctx)
177 SetPTSContext *setpts = ctx->priv;
178 av_expr_free(setpts->expr);
182 #if CONFIG_ASETPTS_FILTER
183 AVFilter avfilter_af_asetpts = {
185 .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
189 .priv_size = sizeof(SetPTSContext),
191 .inputs = (const AVFilterPad[]) {
194 .type = AVMEDIA_TYPE_AUDIO,
195 .get_audio_buffer = ff_null_get_audio_buffer,
196 .config_props = config_input,
197 .filter_samples = filter_frame,
201 .outputs = (const AVFilterPad[]) {
204 .type = AVMEDIA_TYPE_AUDIO,
209 #endif /* CONFIG_ASETPTS_FILTER */
211 #if CONFIG_SETPTS_FILTER
212 AVFilter avfilter_vf_setpts = {
214 .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
218 .priv_size = sizeof(SetPTSContext),
220 .inputs = (const AVFilterPad[]) {{ .name = "default",
221 .type = AVMEDIA_TYPE_VIDEO,
222 .get_video_buffer = ff_null_get_video_buffer,
223 .config_props = config_input,
224 .start_frame = filter_frame, },
226 .outputs = (const AVFilterPad[]) {{ .name = "default",
227 .type = AVMEDIA_TYPE_VIDEO, },
230 #endif /* CONFIG_SETPTS_FILTER */