]> git.sesse.net Git - ffmpeg/blob - libavfilter/f_setpts.c
lavfi/blackdetect: switch to new ff_filter_frame() API
[ffmpeg] / libavfilter / f_setpts.c
1 /*
2  * Copyright (c) 2010 Stefano Sabatini
3  * Copyright (c) 2008 Victor Paesa
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * video presentation timestamp (PTS) modification filter
25  */
26
27 /* #define DEBUG */
28
29 #include "libavutil/eval.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/mathematics.h"
32 #include "avfilter.h"
33 #include "internal.h"
34 #include "audio.h"
35 #include "video.h"
36
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
53     "TB",          ///< timebase
54     NULL
55 };
56
57 enum var_name {
58     VAR_FRAME_RATE,
59     VAR_INTERLACED,
60     VAR_N,
61     VAR_NB_CONSUMED_SAMPLES,
62     VAR_NB_SAMPLES,
63     VAR_POS,
64     VAR_PREV_INPTS,
65     VAR_PREV_INT,
66     VAR_PREV_OUTPTS,
67     VAR_PREV_OUTT,
68     VAR_PTS,
69     VAR_SAMPLE_RATE,
70     VAR_STARTPTS,
71     VAR_STARTT,
72     VAR_T,
73     VAR_TB,
74     VAR_VARS_NB
75 };
76
77 typedef struct {
78     AVExpr *expr;
79     double var_values[VAR_VARS_NB];
80     enum AVMediaType type;
81 } SetPTSContext;
82
83 static av_cold int init(AVFilterContext *ctx, const char *args)
84 {
85     SetPTSContext *setpts = ctx->priv;
86     int ret;
87
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);
91         return ret;
92     }
93
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;
98     return 0;
99 }
100
101 static int config_input(AVFilterLink *inlink)
102 {
103     AVFilterContext *ctx = inlink->dst;
104     SetPTSContext *setpts = ctx->priv;
105
106     setpts->type = inlink->type;
107     setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
108
109     setpts->var_values[VAR_SAMPLE_RATE] =
110         setpts->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
111
112     setpts->var_values[VAR_FRAME_RATE] = inlink->frame_rate.num && inlink->frame_rate.den ?
113         av_q2d(inlink->frame_rate) : NAN;
114
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]);
119     return 0;
120 }
121
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))
125
126 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
127 {
128     SetPTSContext *setpts = inlink->dst->priv;
129     int64_t in_pts = frame->pts;
130     double d;
131
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);
135     }
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;
139
140     switch (inlink->type) {
141     case AVMEDIA_TYPE_VIDEO:
142         setpts->var_values[VAR_INTERLACED] = frame->video->interlaced;
143         break;
144
145     case AVMEDIA_TYPE_AUDIO:
146         setpts->var_values[VAR_NB_SAMPLES] = frame->audio->nb_samples;
147         break;
148     }
149
150     d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
151     frame->pts = D2TS(d);
152
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);
157
158     av_dlog(inlink->dst,
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]);
170
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;
174     }
175     return ff_filter_frame(inlink->dst->outputs[0], frame);
176 }
177
178 static av_cold void uninit(AVFilterContext *ctx)
179 {
180     SetPTSContext *setpts = ctx->priv;
181     av_expr_free(setpts->expr);
182     setpts->expr = NULL;
183 }
184
185 #if CONFIG_ASETPTS_FILTER
186 static const AVFilterPad avfilter_af_asetpts_inputs[] = {
187     {
188         .name             = "default",
189         .type             = AVMEDIA_TYPE_AUDIO,
190         .get_audio_buffer = ff_null_get_audio_buffer,
191         .config_props     = config_input,
192         .filter_frame     = filter_frame,
193     },
194     { NULL }
195 };
196
197 static const AVFilterPad avfilter_af_asetpts_outputs[] = {
198     {
199         .name = "default",
200         .type = AVMEDIA_TYPE_AUDIO,
201     },
202     { NULL }
203 };
204
205 AVFilter avfilter_af_asetpts = {
206     .name      = "asetpts",
207     .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
208     .init      = init,
209     .uninit    = uninit,
210     .priv_size = sizeof(SetPTSContext),
211     .inputs    = avfilter_af_asetpts_inputs,
212     .outputs   = avfilter_af_asetpts_outputs,
213 };
214 #endif /* CONFIG_ASETPTS_FILTER */
215
216 #if CONFIG_SETPTS_FILTER
217 static const AVFilterPad avfilter_vf_setpts_inputs[] = {
218     {
219         .name             = "default",
220         .type             = AVMEDIA_TYPE_VIDEO,
221         .get_video_buffer = ff_null_get_video_buffer,
222         .config_props     = config_input,
223         .filter_frame     = filter_frame,
224     },
225     { NULL }
226 };
227
228 static const AVFilterPad avfilter_vf_setpts_outputs[] = {
229     {
230         .name = "default",
231         .type = AVMEDIA_TYPE_VIDEO,
232     },
233     { NULL }
234 };
235
236 AVFilter avfilter_vf_setpts = {
237     .name      = "setpts",
238     .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
239     .init      = init,
240     .uninit    = uninit,
241
242     .priv_size = sizeof(SetPTSContext),
243
244     .inputs    = avfilter_vf_setpts_inputs,
245     .outputs   = avfilter_vf_setpts_outputs,
246 };
247 #endif /* CONFIG_SETPTS_FILTER */