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_OUTPTS", ///< previous output PTS
45 "PTS", ///< original pts in the file of the frame
46 "SAMPLE_RATE", ///< sample rate (only audio)
47 "STARTPTS", ///< PTS at start of movie
55 VAR_NB_CONSUMED_SAMPLES,
69 double var_values[VAR_VARS_NB];
70 enum AVMediaType type;
73 static av_cold int init(AVFilterContext *ctx, const char *args)
75 SetPTSContext *setpts = ctx->priv;
78 if ((ret = av_expr_parse(&setpts->expr, args ? args : "PTS",
79 var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
80 av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", args);
84 setpts->var_values[VAR_N ] = 0.0;
85 setpts->var_values[VAR_PREV_INPTS ] = NAN;
86 setpts->var_values[VAR_PREV_OUTPTS] = NAN;
87 setpts->var_values[VAR_STARTPTS ] = NAN;
91 static int config_input(AVFilterLink *inlink)
93 AVFilterContext *ctx = inlink->dst;
94 SetPTSContext *setpts = ctx->priv;
96 setpts->type = inlink->type;
97 setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
99 if (setpts->type == AVMEDIA_TYPE_AUDIO)
100 setpts->var_values[VAR_SAMPLE_RATE] = inlink->sample_rate;
102 av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f SAMPLE_RATE:%f\n",
103 setpts->var_values[VAR_TB], setpts->var_values[VAR_SAMPLE_RATE]);
107 #define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
108 #define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
110 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
112 SetPTSContext *setpts = inlink->dst->priv;
114 AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
117 return AVERROR(ENOMEM);
119 if (isnan(setpts->var_values[VAR_STARTPTS]))
120 setpts->var_values[VAR_STARTPTS] = TS2D(inpicref->pts);
121 setpts->var_values[VAR_PTS ] = TS2D(inpicref->pts);
122 setpts->var_values[VAR_POS ] = inpicref->pos == -1 ? NAN : inpicref->pos;
124 switch (inlink->type) {
125 case AVMEDIA_TYPE_VIDEO:
126 setpts->var_values[VAR_INTERLACED] = inpicref->video->interlaced;
129 case AVMEDIA_TYPE_AUDIO:
130 setpts->var_values[VAR_NB_SAMPLES] = inpicref->audio->nb_samples;
134 d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
135 outpicref->pts = D2TS(d);
138 av_log(inlink->dst, AV_LOG_DEBUG,
139 "n:%"PRId64" interlaced:%d nb_samples:%d nb_consumed_samples:%d "
140 "pos:%"PRId64" pts:%"PRId64" t:%f -> pts:%"PRId64" t:%f\n",
141 (int64_t)setpts->var_values[VAR_N],
142 (int)setpts->var_values[VAR_INTERLACED],
143 (int)setpts->var_values[VAR_NB_SAMPLES],
144 (int)setpts->var_values[VAR_NB_CONSUMED_SAMPLES],
146 inpicref ->pts, inpicref ->pts * av_q2d(inlink->time_base),
147 outpicref->pts, outpicref->pts * av_q2d(inlink->time_base));
151 setpts->var_values[VAR_N] += 1.0;
152 setpts->var_values[VAR_PREV_INPTS ] = TS2D(inpicref ->pts);
153 setpts->var_values[VAR_PREV_OUTPTS] = TS2D(outpicref->pts);
155 if (setpts->type == AVMEDIA_TYPE_AUDIO) {
156 setpts->var_values[VAR_NB_CONSUMED_SAMPLES] += inpicref->audio->nb_samples;
157 return ff_filter_samples(inlink->dst->outputs[0], outpicref);
159 return ff_start_frame (inlink->dst->outputs[0], outpicref);
162 static av_cold void uninit(AVFilterContext *ctx)
164 SetPTSContext *setpts = ctx->priv;
165 av_expr_free(setpts->expr);
169 #if CONFIG_ASETPTS_FILTER
170 AVFilter avfilter_af_asetpts = {
172 .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
176 .priv_size = sizeof(SetPTSContext),
178 .inputs = (const AVFilterPad[]) {
181 .type = AVMEDIA_TYPE_AUDIO,
182 .get_audio_buffer = ff_null_get_audio_buffer,
183 .config_props = config_input,
184 .filter_samples = filter_frame,
188 .outputs = (const AVFilterPad[]) {
191 .type = AVMEDIA_TYPE_AUDIO,
196 #endif /* CONFIG_ASETPTS_FILTER */
198 #if CONFIG_SETPTS_FILTER
199 AVFilter avfilter_vf_setpts = {
201 .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
205 .priv_size = sizeof(SetPTSContext),
207 .inputs = (const AVFilterPad[]) {{ .name = "default",
208 .type = AVMEDIA_TYPE_VIDEO,
209 .get_video_buffer = ff_null_get_video_buffer,
210 .config_props = config_input,
211 .start_frame = filter_frame, },
213 .outputs = (const AVFilterPad[]) {{ .name = "default",
214 .type = AVMEDIA_TYPE_VIDEO, },
217 #endif /* CONFIG_SETPTS_FILTER */