FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/setpts.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 103 121 85.1%
Functions: 8 9 88.9%
Branches: 46 73 63.0%

Line Branch Exec Source
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 #include "config_components.h"
28
29 #include <inttypes.h>
30
31 #include "libavutil/eval.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/time.h"
36 #include "audio.h"
37 #include "avfilter.h"
38 #include "filters.h"
39 #include "video.h"
40
41 static const char *const var_names[] = {
42 "FRAME_RATE", ///< defined only for constant frame-rate video
43 "INTERLACED", ///< tell if the current frame is interlaced
44 "N", ///< frame / sample number (starting at zero)
45 "NB_CONSUMED_SAMPLES", ///< number of samples consumed by the filter (only audio)
46 "NB_SAMPLES", ///< number of samples in the current frame (only audio)
47 "PREV_INPTS", ///< previous input PTS
48 "PREV_INT", ///< previous input time in seconds
49 "PREV_OUTPTS", ///< previous output PTS
50 "PREV_OUTT", ///< previous output time in seconds
51 "PTS", ///< original pts in the file of the frame
52 "SAMPLE_RATE", ///< sample rate (only audio)
53 "STARTPTS", ///< PTS at start of movie
54 "STARTT", ///< time at start of movie
55 "T", ///< original time in the file of the frame
56 "TB", ///< timebase
57 "RTCTIME", ///< wallclock (RTC) time in micro seconds
58 "RTCSTART", ///< wallclock (RTC) time at the start of the movie in micro seconds
59 "S", // Number of samples in the current frame
60 "SR", // Audio sample rate
61 "FR", ///< defined only for constant frame-rate video
62 "T_CHANGE", ///< time of first frame after latest command was applied
63 NULL
64 };
65
66 enum var_name {
67 VAR_FRAME_RATE,
68 VAR_INTERLACED,
69 VAR_N,
70 VAR_NB_CONSUMED_SAMPLES,
71 VAR_NB_SAMPLES,
72 VAR_PREV_INPTS,
73 VAR_PREV_INT,
74 VAR_PREV_OUTPTS,
75 VAR_PREV_OUTT,
76 VAR_PTS,
77 VAR_SAMPLE_RATE,
78 VAR_STARTPTS,
79 VAR_STARTT,
80 VAR_T,
81 VAR_TB,
82 VAR_RTCTIME,
83 VAR_RTCSTART,
84 VAR_S,
85 VAR_SR,
86 VAR_FR,
87 VAR_T_CHANGE,
88 VAR_VARS_NB,
89 };
90
91 typedef struct SetPTSContext {
92 const AVClass *class;
93 char *expr_str;
94 AVExpr *expr;
95 int strip_fps;
96 double var_values[VAR_VARS_NB];
97 enum AVMediaType type;
98 } SetPTSContext;
99
100 #define V(name_) \
101 setpts->var_values[VAR_##name_]
102
103 129 static av_cold int init(AVFilterContext *ctx)
104 {
105 129 SetPTSContext *setpts = ctx->priv;
106 int ret;
107
108
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 129 times.
129 if ((ret = av_expr_parse(&setpts->expr, setpts->expr_str,
109 var_names, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
110 av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", setpts->expr_str);
111 return ret;
112 }
113
114 129 V(N) = 0.0;
115 129 V(S) = 0.0;
116 129 V(PREV_INPTS) = NAN;
117 129 V(PREV_INT) = NAN;
118 129 V(PREV_OUTPTS) = NAN;
119 129 V(PREV_OUTT) = NAN;
120 129 V(STARTPTS) = NAN;
121 129 V(STARTT) = NAN;
122 129 V(T_CHANGE) = NAN;
123 129 return 0;
124 }
125
126 65 static int config_input(AVFilterLink *inlink)
127 {
128 65 FilterLink *l = ff_filter_link(inlink);
129 65 AVFilterContext *ctx = inlink->dst;
130 65 SetPTSContext *setpts = ctx->priv;
131
132 65 setpts->type = inlink->type;
133 65 V(TB) = av_q2d(inlink->time_base);
134 65 V(RTCSTART) = av_gettime();
135
136 65 V(SR) = V(SAMPLE_RATE) =
137
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 11 times.
65 setpts->type == AVMEDIA_TYPE_AUDIO ? inlink->sample_rate : NAN;
138
139 65 V(FRAME_RATE) = V(FR) =
140
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 l->frame_rate.num && l->frame_rate.den ?
141
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 54 times.
76 av_q2d(l->frame_rate) : NAN;
142
143 65 av_log(inlink->src, AV_LOG_VERBOSE, "TB:%f FRAME_RATE:%f SAMPLE_RATE:%f\n",
144 V(TB), V(FRAME_RATE), V(SAMPLE_RATE));
145 65 return 0;
146 }
147
148 11 static int config_output_video(AVFilterLink *outlink)
149 {
150 11 FilterLink *l = ff_filter_link(outlink);
151 11 SetPTSContext *s = outlink->src->priv;
152
153
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
11 if (s->strip_fps)
154 10 l->frame_rate = (AVRational){ 1, 0 };
155
156 11 return 0;
157 }
158
159 #define BUF_SIZE 64
160
161 4742 static inline char *double2int64str(char *buf, double v)
162 {
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4742 times.
4742 if (isnan(v)) snprintf(buf, BUF_SIZE, "nan");
164 4742 else snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)v);
165 4742 return buf;
166 }
167
168 2371 static double eval_pts(SetPTSContext *setpts, AVFilterLink *inlink, AVFrame *frame, int64_t pts)
169 {
170
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 2306 times.
2371 if (isnan(V(STARTPTS))) {
171
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 V(STARTPTS) = TS2D(pts);
172
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 V(STARTT ) = TS2T(pts, inlink->time_base);
173 }
174
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 2306 times.
2371 if (isnan(V(T_CHANGE))) {
175
1/2
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
65 V(T_CHANGE) = TS2T(pts, inlink->time_base);
176 }
177
1/2
✓ Branch 0 taken 2371 times.
✗ Branch 1 not taken.
2371 V(PTS ) = TS2D(pts);
178
1/2
✓ Branch 0 taken 2371 times.
✗ Branch 1 not taken.
2371 V(T ) = TS2T(pts, inlink->time_base);
179 2371 V(RTCTIME ) = av_gettime();
180
181
2/2
✓ Branch 0 taken 2311 times.
✓ Branch 1 taken 60 times.
2371 if (frame) {
182
2/2
✓ Branch 0 taken 502 times.
✓ Branch 1 taken 1809 times.
2311 if (inlink->type == AVMEDIA_TYPE_VIDEO) {
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 502 times.
502 V(INTERLACED) = !!(frame->flags & AV_FRAME_FLAG_INTERLACED);
184
1/2
✓ Branch 0 taken 1809 times.
✗ Branch 1 not taken.
1809 } else if (inlink->type == AVMEDIA_TYPE_AUDIO) {
185 1809 V(S) = frame->nb_samples;
186 1809 V(NB_SAMPLES) = frame->nb_samples;
187 }
188 }
189
190 2371 return av_expr_eval(setpts->expr, setpts->var_values, NULL);
191 }
192 #define d2istr(v) double2int64str((char[BUF_SIZE]){0}, v)
193
194 2311 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
195 {
196 2311 SetPTSContext *setpts = inlink->dst->priv;
197 2311 int64_t in_pts = frame->pts;
198 double d;
199
200 2311 d = eval_pts(setpts, inlink, frame, frame->pts);
201
1/2
✓ Branch 0 taken 2311 times.
✗ Branch 1 not taken.
2311 frame->pts = D2TS(d);
202 2311 frame->duration = 0;
203
204 4622 av_log(inlink->dst, AV_LOG_TRACE,
205 "N:%"PRId64" PTS:%s T:%f",
206 2311 (int64_t)V(N), d2istr(V(PTS)), V(T));
207
2/3
✓ Branch 0 taken 502 times.
✓ Branch 1 taken 1809 times.
✗ Branch 2 not taken.
2311 switch (inlink->type) {
208 502 case AVMEDIA_TYPE_VIDEO:
209 502 av_log(inlink->dst, AV_LOG_TRACE, " INTERLACED:%"PRId64,
210 502 (int64_t)V(INTERLACED));
211 502 break;
212 1809 case AVMEDIA_TYPE_AUDIO:
213 1809 av_log(inlink->dst, AV_LOG_TRACE, " NB_SAMPLES:%"PRId64" NB_CONSUMED_SAMPLES:%"PRId64,
214 1809 (int64_t)V(NB_SAMPLES),
215 1809 (int64_t)V(NB_CONSUMED_SAMPLES));
216 1809 break;
217 }
218
1/2
✓ Branch 0 taken 2311 times.
✗ Branch 1 not taken.
2311 av_log(inlink->dst, AV_LOG_TRACE, " -> PTS:%s T:%f\n", d2istr(d), TS2T(d, inlink->time_base));
219
220
2/2
✓ Branch 0 taken 502 times.
✓ Branch 1 taken 1809 times.
2311 if (inlink->type == AVMEDIA_TYPE_VIDEO) {
221 502 V(N) += 1.0;
222 } else {
223 1809 V(N) += frame->nb_samples;
224 }
225
226
1/2
✓ Branch 0 taken 2311 times.
✗ Branch 1 not taken.
2311 V(PREV_INPTS ) = TS2D(in_pts);
227
1/2
✓ Branch 0 taken 2311 times.
✗ Branch 1 not taken.
2311 V(PREV_INT ) = TS2T(in_pts, inlink->time_base);
228
1/2
✓ Branch 0 taken 2311 times.
✗ Branch 1 not taken.
2311 V(PREV_OUTPTS) = TS2D(frame->pts);
229
1/2
✓ Branch 0 taken 2311 times.
✗ Branch 1 not taken.
2311 V(PREV_OUTT) = TS2T(frame->pts, inlink->time_base);
230
2/2
✓ Branch 0 taken 1809 times.
✓ Branch 1 taken 502 times.
2311 if (setpts->type == AVMEDIA_TYPE_AUDIO) {
231 1809 V(NB_CONSUMED_SAMPLES) += frame->nb_samples;
232 }
233 2311 return ff_filter_frame(inlink->dst->outputs[0], frame);
234 }
235
236 4706 static int activate(AVFilterContext *ctx)
237 {
238 4706 SetPTSContext *setpts = ctx->priv;
239 4706 AVFilterLink *inlink = ctx->inputs[0];
240 4706 AVFilterLink *outlink = ctx->outputs[0];
241 AVFrame *in;
242 int status;
243 int64_t pts;
244 int ret;
245
246
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4702 times.
4706 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
247
248 4702 ret = ff_inlink_consume_frame(inlink, &in);
249
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4702 times.
4702 if (ret < 0)
250 return ret;
251
2/2
✓ Branch 0 taken 2311 times.
✓ Branch 1 taken 2391 times.
4702 if (ret > 0)
252 2311 return filter_frame(inlink, in);
253
254
2/2
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 2331 times.
2391 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
255 60 double d = eval_pts(setpts, inlink, NULL, pts);
256
257
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
180 av_log(ctx, AV_LOG_TRACE, "N:EOF PTS:%s T:%f -> PTS:%s T:%f\n",
258 120 d2istr(V(PTS)), V(T), d2istr(d), TS2T(d, inlink->time_base));
259
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 ff_outlink_set_status(outlink, status, D2TS(d));
260 60 return 0;
261 }
262
263
1/2
✓ Branch 1 taken 2331 times.
✗ Branch 2 not taken.
2331 FF_FILTER_FORWARD_WANTED(outlink, inlink);
264
265 return FFERROR_NOT_READY;
266 }
267
268 129 static av_cold void uninit(AVFilterContext *ctx)
269 {
270 129 SetPTSContext *setpts = ctx->priv;
271 129 av_expr_free(setpts->expr);
272 129 setpts->expr = NULL;
273 129 }
274
275 static int process_command(AVFilterContext *ctx, const char *cmd, const char *arg,
276 char *res, int res_len, int flags)
277 {
278 SetPTSContext *setpts = ctx->priv;
279 AVExpr *new_expr;
280 int ret;
281
282 ret = ff_filter_process_command(ctx, cmd, arg, res, res_len, flags);
283
284 if (ret < 0)
285 return ret;
286
287 if (!strcmp(cmd, "expr")) {
288 ret = av_expr_parse(&new_expr, arg, var_names, NULL, NULL, NULL, NULL, 0, ctx);
289 // Only free and replace previous expression if new one succeeds,
290 // otherwise defensively keep everything intact even if reporting an error.
291 if (ret < 0) {
292 av_log(ctx, AV_LOG_ERROR, "Error while parsing expression '%s'\n", arg);
293 } else {
294 av_expr_free(setpts->expr);
295 setpts->expr = new_expr;
296 V(T_CHANGE) = NAN;
297 }
298 } else {
299 ret = AVERROR(EINVAL);
300 }
301
302 return ret;
303 }
304 #undef V
305
306 #define OFFSET(x) offsetof(SetPTSContext, x)
307 #define V AV_OPT_FLAG_VIDEO_PARAM
308 #define A AV_OPT_FLAG_AUDIO_PARAM
309 #define R AV_OPT_FLAG_RUNTIME_PARAM
310 #define F AV_OPT_FLAG_FILTERING_PARAM
311
312 #if CONFIG_SETPTS_FILTER
313 static const AVOption setpts_options[] = {
314 { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = V|F|R },
315 { "strip_fps", "Unset framerate metadata", OFFSET(strip_fps), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, .flags = V|F },
316 { NULL }
317 };
318 AVFILTER_DEFINE_CLASS(setpts);
319
320 static const AVFilterPad avfilter_vf_setpts_inputs[] = {
321 {
322 .name = "default",
323 .type = AVMEDIA_TYPE_VIDEO,
324 .config_props = config_input,
325 },
326 };
327
328 static const AVFilterPad outputs_video[] = {
329 {
330 .name = "default",
331 .type = AVMEDIA_TYPE_VIDEO,
332 .config_props = config_output_video,
333 },
334 };
335
336 const FFFilter ff_vf_setpts = {
337 .p.name = "setpts",
338 .p.description = NULL_IF_CONFIG_SMALL("Set PTS for the output video frame."),
339 .p.flags = AVFILTER_FLAG_METADATA_ONLY,
340
341 .p.priv_class = &setpts_class,
342
343 .init = init,
344 .activate = activate,
345 .uninit = uninit,
346 .process_command = process_command,
347
348 .priv_size = sizeof(SetPTSContext),
349
350 FILTER_INPUTS(avfilter_vf_setpts_inputs),
351 FILTER_OUTPUTS(outputs_video),
352 };
353 #endif /* CONFIG_SETPTS_FILTER */
354
355 #if CONFIG_ASETPTS_FILTER
356
357 static const AVOption asetpts_options[] = {
358 { "expr", "Expression determining the frame timestamp", OFFSET(expr_str), AV_OPT_TYPE_STRING, { .str = "PTS" }, .flags = A|F|R },
359 { NULL }
360 };
361 AVFILTER_DEFINE_CLASS(asetpts);
362
363 static const AVFilterPad asetpts_inputs[] = {
364 {
365 .name = "default",
366 .type = AVMEDIA_TYPE_AUDIO,
367 .config_props = config_input,
368 },
369 };
370
371 const FFFilter ff_af_asetpts = {
372 .p.name = "asetpts",
373 .p.description = NULL_IF_CONFIG_SMALL("Set PTS for the output audio frame."),
374 .p.priv_class = &asetpts_class,
375 .p.flags = AVFILTER_FLAG_METADATA_ONLY,
376 .init = init,
377 .activate = activate,
378 .uninit = uninit,
379 .process_command = process_command,
380 .priv_size = sizeof(SetPTSContext),
381 FILTER_INPUTS(asetpts_inputs),
382 FILTER_OUTPUTS(ff_audio_default_filterpad),
383 };
384 #endif /* CONFIG_ASETPTS_FILTER */
385