h264_metadata: Add option to delete filler data
[libav.git] / libavfilter / setpts.c
CommitLineData
a532bb39
SS
1/*
2 * Copyright (c) 2010 Stefano Sabatini
3 * Copyright (c) 2008 Victor Paesa
4 *
2912e87a 5 * This file is part of Libav.
a532bb39 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
a532bb39
SS
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
a532bb39
SS
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
a532bb39
SS
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
593aaee9
DB
27#include <inttypes.h>
28
a532bb39 29#include "libavutil/eval.h"
1d9c2dc8 30#include "libavutil/internal.h"
0ebcdf5c 31#include "libavutil/mathematics.h"
33b97faa 32#include "libavutil/opt.h"
0b55b16a 33#include "libavutil/time.h"
b4729382
AK
34
35#include "audio.h"
a532bb39 36#include "avfilter.h"
9d0bfc50 37#include "internal.h"
c04c533f 38#include "video.h"
a532bb39 39
b4729382
AK
40#include "config.h"
41
b0f29db5 42static const char *const var_names[] = {
a532bb39 43 "E", ///< Euler number
018bdaed 44 "FRAME_RATE", ///< defined only for constant frame-rate video
a532bb39 45 "INTERLACED", ///< tell if the current frame is interlaced
b4729382 46 "N", ///< frame / sample number (starting at zero)
a532bb39 47 "PHI", ///< golden ratio
41ed7ab4 48 "PI", ///< Greek pi
a532bb39
SS
49 "PREV_INPTS", ///< previous input PTS
50 "PREV_OUTPTS", ///< previous output PTS
51 "PTS", ///< original pts in the file of the frame
b4729382 52 "STARTPTS", ///< PTS at start of movie
a532bb39 53 "TB", ///< timebase
0b55b16a
VP
54 "RTCTIME", ///< wallclock (RTC) time in micro seconds
55 "RTCSTART", ///< wallclock (RTC) time at the start of the movie in micro seconds
b4729382
AK
56 "S", // Number of samples in the current frame
57 "SR", // Audio sample rate
a532bb39
SS
58 NULL
59};
60
61enum var_name {
62 VAR_E,
018bdaed 63 VAR_FRAME_RATE,
a532bb39
SS
64 VAR_INTERLACED,
65 VAR_N,
66 VAR_PHI,
67 VAR_PI,
a532bb39
SS
68 VAR_PREV_INPTS,
69 VAR_PREV_OUTPTS,
70 VAR_PTS,
71 VAR_STARTPTS,
72 VAR_TB,
0b55b16a
VP
73 VAR_RTCTIME,
74 VAR_RTCSTART,
b4729382
AK
75 VAR_S,
76 VAR_SR,
a532bb39
SS
77 VAR_VARS_NB
78};
79
58400ac1 80typedef struct SetPTSContext {
33b97faa
AK
81 const AVClass *class;
82 char *expr_str;
a532bb39
SS
83 AVExpr *expr;
84 double var_values[VAR_VARS_NB];
85} SetPTSContext;
86
c43a7eca 87static av_cold int init(AVFilterContext *ctx)
a532bb39
SS
88{
89 SetPTSContext *setpts = ctx->priv;
90 int ret;
91
33b97faa 92 if ((ret = av_expr_parse(&setpts->expr, setpts->expr_str,
a532bb39 93 var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
33b97faa 94 av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", setpts->expr_str);
a532bb39
SS
95 return ret;
96 }
97
2548834b
AK
98 setpts->var_values[VAR_E] = M_E;
99 setpts->var_values[VAR_N] = 0.0;
b4729382 100 setpts->var_values[VAR_S] = 0.0;
2548834b
AK
101 setpts->var_values[VAR_PHI] = M_PHI;
102 setpts->var_values[VAR_PI] = M_PI;
103 setpts->var_values[VAR_PREV_INPTS] = NAN;
a532bb39 104 setpts->var_values[VAR_PREV_OUTPTS] = NAN;
2548834b 105 setpts->var_values[VAR_STARTPTS] = NAN;
a532bb39
SS
106 return 0;
107}
108
109static int config_input(AVFilterLink *inlink)
110{
111 SetPTSContext *setpts = inlink->dst->priv;
112
113 setpts->var_values[VAR_TB] = av_q2d(inlink->time_base);
0b55b16a 114 setpts->var_values[VAR_RTCSTART] = av_gettime();
a532bb39 115
b4729382
AK
116 if (inlink->type == AVMEDIA_TYPE_AUDIO) {
117 setpts->var_values[VAR_SR] = inlink->sample_rate;
118 }
119
018bdaed
SS
120 setpts->var_values[VAR_FRAME_RATE] = inlink->frame_rate.num &&
121 inlink->frame_rate.den ?
122 av_q2d(inlink->frame_rate) : NAN;
123
124 // Indicate the output can be variable framerate.
125 inlink->frame_rate = (AVRational){1, 0};
126
1a49a169 127 av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f\n", setpts->var_values[VAR_TB]);
a532bb39
SS
128 return 0;
129}
130
131#define D2TS(d) (isnan(d) ? AV_NOPTS_VALUE : (int64_t)(d))
132#define TS2D(ts) ((ts) == AV_NOPTS_VALUE ? NAN : (double)(ts))
133
7e350379 134static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
a532bb39
SS
135{
136 SetPTSContext *setpts = inlink->dst->priv;
565e4993 137 int64_t in_pts = frame->pts;
a532bb39 138 double d;
ebc8d974 139
a532bb39 140 if (isnan(setpts->var_values[VAR_STARTPTS]))
565e4993 141 setpts->var_values[VAR_STARTPTS] = TS2D(frame->pts);
a532bb39 142
565e4993 143 setpts->var_values[VAR_PTS ] = TS2D(frame->pts);
0b55b16a 144 setpts->var_values[VAR_RTCTIME ] = av_gettime();
a532bb39 145
b4729382
AK
146 if (inlink->type == AVMEDIA_TYPE_VIDEO) {
147 setpts->var_values[VAR_INTERLACED] = frame->interlaced_frame;
148 } else {
149 setpts->var_values[VAR_S] = frame->nb_samples;
150 }
151
d2af7205 152 d = av_expr_eval(setpts->expr, setpts->var_values, NULL);
565e4993 153 frame->pts = D2TS(d);
a532bb39 154
1a3eb042 155 av_log(inlink->dst, AV_LOG_TRACE,
593aaee9
DB
156 "n:%"PRId64" interlaced:%d 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 in_pts, in_pts * av_q2d(inlink->time_base),
160 frame->pts, frame->pts * av_q2d(inlink->time_base));
565e4993 161
b4729382
AK
162 if (inlink->type == AVMEDIA_TYPE_VIDEO) {
163 setpts->var_values[VAR_N] += 1.0;
164 } else {
165 setpts->var_values[VAR_N] += frame->nb_samples;
166 }
167
565e4993
AK
168 setpts->var_values[VAR_PREV_INPTS ] = TS2D(in_pts);
169 setpts->var_values[VAR_PREV_OUTPTS] = TS2D(frame->pts);
170 return ff_filter_frame(inlink->dst->outputs[0], frame);
a532bb39
SS
171}
172
173static av_cold void uninit(AVFilterContext *ctx)
174{
175 SetPTSContext *setpts = ctx->priv;
d2af7205 176 av_expr_free(setpts->expr);
a532bb39
SS
177 setpts->expr = NULL;
178}
179
33b97faa 180#define OFFSET(x) offsetof(SetPTSContext, x)
b4729382 181#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_AUDIO_PARAM
33b97faa
AK
182static const AVOption options[] = {
183 { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = FLAGS },
184 { NULL },
185};
186
b4729382 187#if CONFIG_SETPTS_FILTER
33b97faa
AK
188static const AVClass setpts_class = {
189 .class_name = "setpts",
190 .item_name = av_default_item_name,
191 .option = options,
192 .version = LIBAVUTIL_VERSION_INT,
193};
194
568c70e7
MR
195static const AVFilterPad avfilter_vf_setpts_inputs[] = {
196 {
197 .name = "default",
198 .type = AVMEDIA_TYPE_VIDEO,
199 .get_video_buffer = ff_null_get_video_buffer,
200 .config_props = config_input,
565e4993 201 .filter_frame = filter_frame,
568c70e7
MR
202 },
203 { NULL }
204};
205
206static const AVFilterPad avfilter_vf_setpts_outputs[] = {
207 {
208 .name = "default",
209 .type = AVMEDIA_TYPE_VIDEO,
210 },
211 { NULL }
212};
213
cd43ca04 214AVFilter ff_vf_setpts = {
a532bb39
SS
215 .name = "setpts",
216 .description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
217 .init = init,
218 .uninit = uninit,
219
220 .priv_size = sizeof(SetPTSContext),
33b97faa 221 .priv_class = &setpts_class,
a532bb39 222
568c70e7
MR
223 .inputs = avfilter_vf_setpts_inputs,
224 .outputs = avfilter_vf_setpts_outputs,
a532bb39 225};
b4729382
AK
226#endif
227
228#if CONFIG_ASETPTS_FILTER
229static const AVClass asetpts_class = {
230 .class_name = "asetpts",
231 .item_name = av_default_item_name,
232 .option = options,
233 .version = LIBAVUTIL_VERSION_INT,
234};
235
236static const AVFilterPad asetpts_inputs[] = {
237 {
238 .name = "default",
239 .type = AVMEDIA_TYPE_AUDIO,
240 .get_audio_buffer = ff_null_get_audio_buffer,
241 .config_props = config_input,
242 .filter_frame = filter_frame,
243 },
244 { NULL }
245};
246
247static const AVFilterPad asetpts_outputs[] = {
248 {
249 .name = "default",
250 .type = AVMEDIA_TYPE_AUDIO,
251 },
252 { NULL }
253};
254
cd43ca04 255AVFilter ff_af_asetpts = {
b4729382
AK
256 .name = "asetpts",
257 .description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
258 .init = init,
259 .uninit = uninit,
260
261 .priv_size = sizeof(SetPTSContext),
262 .priv_class = &asetpts_class,
263
264 .inputs = asetpts_inputs,
265 .outputs = asetpts_outputs,
266};
267#endif