FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/setpts.c
Date: 2024-04-12 08:31:17
Exec Total Coverage
Lines: 100 118 84.7%
Functions: 8 9 88.9%
Branches: 47 75 62.7%

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