FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/setpts.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 104 122 85.2%
Functions: 8 9 88.9%
Branches: 50 77 64.9%

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