]> git.sesse.net Git - ffmpeg/blob - libavfilter/f_setpts.c
Merge remote-tracking branch 'qatar/master'
[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     "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
52     "TB",          ///< timebase
53     NULL
54 };
55
56 enum var_name {
57     VAR_INTERLACED,
58     VAR_N,
59     VAR_NB_CONSUMED_SAMPLES,
60     VAR_NB_SAMPLES,
61     VAR_POS,
62     VAR_PREV_INPTS,
63     VAR_PREV_INT,
64     VAR_PREV_OUTPTS,
65     VAR_PREV_OUTT,
66     VAR_PTS,
67     VAR_SAMPLE_RATE,
68     VAR_STARTPTS,
69     VAR_STARTT,
70     VAR_T,
71     VAR_TB,
72     VAR_VARS_NB
73 };
74
75 typedef struct {
76     AVExpr *expr;
77     double var_values[VAR_VARS_NB];
78     enum AVMediaType type;
79 } SetPTSContext;
80
81 static av_cold int init(AVFilterContext *ctx, const char *args)
82 {
83     SetPTSContext *setpts = ctx->priv;
84     int ret;
85
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);
89         return ret;
90     }
91
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;
96     return 0;
97 }
98
99 static int config_input(AVFilterLink *inlink)
100 {
101     AVFilterContext *ctx = inlink->dst;
102     SetPTSContext *setpts = ctx->priv;
103
104     setpts->type = inlink->type;
105     setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
106
107     if (setpts->type == AVMEDIA_TYPE_AUDIO)
108         setpts->var_values[VAR_SAMPLE_RATE] = inlink->sample_rate;
109
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]);
112     return 0;
113 }
114
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))
118
119 static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
120 {
121     SetPTSContext *setpts = inlink->dst->priv;
122     double d;
123     AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
124
125     if (!outpicref)
126         return AVERROR(ENOMEM);
127
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);
131     }
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;
135
136     switch (inlink->type) {
137     case AVMEDIA_TYPE_VIDEO:
138         setpts->var_values[VAR_INTERLACED] = inpicref->video->interlaced;
139         break;
140
141     case AVMEDIA_TYPE_AUDIO:
142         setpts->var_values[VAR_NB_SAMPLES] = inpicref->audio->nb_samples;
143         break;
144     }
145
146     d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
147     outpicref->pts = D2TS(d);
148
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);
153
154     av_dlog(inlink->dst,
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]);
166
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);
171     } else
172         return ff_start_frame   (inlink->dst->outputs[0], outpicref);
173 }
174
175 static av_cold void uninit(AVFilterContext *ctx)
176 {
177     SetPTSContext *setpts = ctx->priv;
178     av_expr_free(setpts->expr);
179     setpts->expr = NULL;
180 }
181
182 #if CONFIG_ASETPTS_FILTER
183 AVFilter avfilter_af_asetpts = {
184     .name      = "asetpts",
185     .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
186     .init      = init,
187     .uninit    = uninit,
188
189     .priv_size = sizeof(SetPTSContext),
190
191     .inputs = (const AVFilterPad[]) {
192         {
193             .name             = "default",
194             .type             = AVMEDIA_TYPE_AUDIO,
195             .get_audio_buffer = ff_null_get_audio_buffer,
196             .config_props     = config_input,
197             .filter_samples   = filter_frame,
198         },
199         { .name = NULL }
200     },
201     .outputs = (const AVFilterPad[]) {
202         {
203             .name             = "default",
204             .type             = AVMEDIA_TYPE_AUDIO,
205         },
206         { .name = NULL }
207     },
208 };
209 #endif /* CONFIG_ASETPTS_FILTER */
210
211 #if CONFIG_SETPTS_FILTER
212 AVFilter avfilter_vf_setpts = {
213     .name      = "setpts",
214     .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
215     .init      = init,
216     .uninit    = uninit,
217
218     .priv_size = sizeof(SetPTSContext),
219
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, },
225                                         { .name = NULL }},
226     .outputs   = (const AVFilterPad[]) {{ .name             = "default",
227                                           .type             = AVMEDIA_TYPE_VIDEO, },
228                                         { .name = NULL}},
229 };
230 #endif /* CONFIG_SETPTS_FILTER */