FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_fps.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 116 132 87.9%
Functions: 8 8 100.0%
Branches: 51 64 79.7%

Line Branch Exec Source
1 /*
2 * Copyright 2007 Bobby Bingham
3 * Copyright 2012 Robert Nagy <ronag89 gmail com>
4 * Copyright 2012 Anton Khirnov <anton khirnov net>
5 * Copyright 2018 Calvin Walton <calvin.walton@kepstin.ca>
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 /**
25 * @file
26 * a filter enforcing given constant framerate
27 */
28
29 #include <float.h>
30 #include <stdint.h>
31
32 #include "libavutil/avassert.h"
33 #include "libavutil/eval.h"
34 #include "libavutil/mathematics.h"
35 #include "libavutil/opt.h"
36 #include "avfilter.h"
37 #include "ccfifo.h"
38 #include "filters.h"
39 #include "internal.h"
40 #include "video.h"
41
42 enum EOFAction {
43 EOF_ACTION_ROUND,
44 EOF_ACTION_PASS,
45 EOF_ACTION_NB
46 };
47
48 static const char *const var_names[] = {
49 "source_fps",
50 "ntsc",
51 "pal",
52 "film",
53 "ntsc_film",
54 NULL
55 };
56
57 enum var_name {
58 VAR_SOURCE_FPS,
59 VAR_FPS_NTSC,
60 VAR_FPS_PAL,
61 VAR_FPS_FILM,
62 VAR_FPS_NTSC_FILM,
63 VARS_NB
64 };
65
66 static const double ntsc_fps = 30000.0 / 1001.0;
67 static const double pal_fps = 25.0;
68 static const double film_fps = 24.0;
69 static const double ntsc_film_fps = 24000.0 / 1001.0;
70
71 typedef struct FPSContext {
72 const AVClass *class;
73
74 double start_time; ///< pts, in seconds, of the expected first frame
75
76 char *framerate; ///< expression that defines the target framerate
77 int rounding; ///< AVRounding method for timestamps
78 int eof_action; ///< action performed for last frame in FIFO
79
80 /* Set during outlink configuration */
81 int64_t in_pts_off; ///< input frame pts offset for start_time handling
82 int64_t out_pts_off; ///< output frame pts offset for start_time handling
83
84 /* Runtime state */
85 int status; ///< buffered input status
86 int64_t status_pts; ///< buffered input status timestamp
87
88 AVFrame *frames[2]; ///< buffered frames
89 int frames_count; ///< number of buffered frames
90 CCFifo cc_fifo; ///< closed captions
91
92 int64_t next_pts; ///< pts of the next frame to output
93
94 /* statistics */
95 int cur_frame_out; ///< number of times current frame has been output
96 int frames_in; ///< number of frames on input
97 int frames_out; ///< number of frames on output
98 int dup; ///< number of frames duplicated
99 int drop; ///< number of framed dropped
100 } FPSContext;
101
102 #define OFFSET(x) offsetof(FPSContext, x)
103 #define V AV_OPT_FLAG_VIDEO_PARAM
104 #define F AV_OPT_FLAG_FILTERING_PARAM
105 static const AVOption fps_options[] = {
106 { "fps", "A string describing desired output framerate", OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = "25" }, 0, 0, V|F },
107 { "start_time", "Assume the first PTS should be this value.", OFFSET(start_time), AV_OPT_TYPE_DOUBLE, { .dbl = DBL_MAX}, -DBL_MAX, DBL_MAX, V|F },
108 { "round", "set rounding method for timestamps", OFFSET(rounding), AV_OPT_TYPE_INT, { .i64 = AV_ROUND_NEAR_INF }, 0, 5, V|F, .unit = "round" },
109 { "zero", "round towards 0", 0, AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_ZERO }, 0, 0, V|F, .unit = "round" },
110 { "inf", "round away from 0", 0, AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_INF }, 0, 0, V|F, .unit = "round" },
111 { "down", "round towards -infty", 0, AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_DOWN }, 0, 0, V|F, .unit = "round" },
112 { "up", "round towards +infty", 0, AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_UP }, 0, 0, V|F, .unit = "round" },
113 { "near", "round to nearest", 0, AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_NEAR_INF }, 0, 0, V|F, .unit = "round" },
114 { "eof_action", "action performed for last frame", OFFSET(eof_action), AV_OPT_TYPE_INT, { .i64 = EOF_ACTION_ROUND }, 0, EOF_ACTION_NB-1, V|F, .unit = "eof_action" },
115 { "round", "round similar to other frames", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_ROUND }, 0, 0, V|F, .unit = "eof_action" },
116 { "pass", "pass through last frame", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_PASS }, 0, 0, V|F, .unit = "eof_action" },
117 { NULL }
118 };
119
120 AVFILTER_DEFINE_CLASS(fps);
121
122 28 static av_cold int init(AVFilterContext *ctx)
123 {
124 28 FPSContext *s = ctx->priv;
125
126 28 s->status_pts = AV_NOPTS_VALUE;
127 28 s->next_pts = AV_NOPTS_VALUE;
128
129 28 return 0;
130 }
131
132 /* Remove the first frame from the buffer, returning it */
133 485 static AVFrame *shift_frame(AVFilterContext *ctx, FPSContext *s)
134 {
135 AVFrame *frame;
136
137 /* Must only be called when there are frames in the buffer */
138 av_assert1(s->frames_count > 0);
139
140 485 frame = s->frames[0];
141 485 s->frames[0] = s->frames[1];
142 485 s->frames[1] = NULL;
143 485 s->frames_count--;
144
145 /* Update statistics counters */
146 485 s->frames_out += s->cur_frame_out;
147
2/2
✓ Branch 0 taken 274 times.
✓ Branch 1 taken 211 times.
485 if (s->cur_frame_out > 1) {
148 274 av_log(ctx, AV_LOG_DEBUG, "Duplicated frame with pts %"PRId64" %d times\n",
149 274 frame->pts, s->cur_frame_out - 1);
150 274 s->dup += s->cur_frame_out - 1;
151
2/2
✓ Branch 0 taken 82 times.
✓ Branch 1 taken 129 times.
211 } else if (s->cur_frame_out == 0) {
152 82 av_log(ctx, AV_LOG_DEBUG, "Dropping frame with pts %"PRId64"\n",
153 frame->pts);
154 82 s->drop++;
155 }
156 485 s->cur_frame_out = 0;
157
158 485 return frame;
159 }
160
161 28 static av_cold void uninit(AVFilterContext *ctx)
162 {
163 28 FPSContext *s = ctx->priv;
164
165 AVFrame *frame;
166
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 while (s->frames_count > 0) {
168 frame = shift_frame(ctx, s);
169 av_frame_free(&frame);
170 }
171 28 ff_ccfifo_uninit(&s->cc_fifo);
172
173 28 av_log(ctx, AV_LOG_VERBOSE, "%d frames in, %d frames out; %d frames dropped, "
174 "%d frames duplicated.\n", s->frames_in, s->frames_out, s->drop, s->dup);
175 28 }
176
177 14 static int config_props(AVFilterLink* outlink)
178 {
179 14 AVFilterContext *ctx = outlink->src;
180 14 AVFilterLink *inlink = ctx->inputs[0];
181 14 FPSContext *s = ctx->priv;
182
183 double var_values[VARS_NB], res;
184 int ret;
185
186 14 var_values[VAR_SOURCE_FPS] = av_q2d(inlink->frame_rate);
187 14 var_values[VAR_FPS_NTSC] = ntsc_fps;
188 14 var_values[VAR_FPS_PAL] = pal_fps;
189 14 var_values[VAR_FPS_FILM] = film_fps;
190 14 var_values[VAR_FPS_NTSC_FILM] = ntsc_film_fps;
191 14 ret = av_expr_parse_and_eval(&res, s->framerate,
192 var_names, var_values,
193 NULL, NULL, NULL, NULL, NULL, 0, ctx);
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (ret < 0)
195 return ret;
196
197 14 outlink->frame_rate = av_d2q(res, INT_MAX);
198 14 outlink->time_base = av_inv_q(outlink->frame_rate);
199
200 /* Calculate the input and output pts offsets for start_time */
201
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
14 if (s->start_time != DBL_MAX && s->start_time != AV_NOPTS_VALUE) {
202 2 double first_pts = s->start_time * AV_TIME_BASE;
203
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (first_pts < INT64_MIN || first_pts > INT64_MAX) {
204 av_log(ctx, AV_LOG_ERROR, "Start time %f cannot be represented in internal time base\n",
205 s->start_time);
206 return AVERROR(EINVAL);
207 }
208 2 s->in_pts_off = av_rescale_q_rnd(first_pts, AV_TIME_BASE_Q, inlink->time_base,
209 2 s->rounding | AV_ROUND_PASS_MINMAX);
210 2 s->out_pts_off = av_rescale_q_rnd(first_pts, AV_TIME_BASE_Q, outlink->time_base,
211 2 s->rounding | AV_ROUND_PASS_MINMAX);
212 2 s->next_pts = s->out_pts_off;
213 2 av_log(ctx, AV_LOG_VERBOSE, "Set first pts to (in:%"PRId64" out:%"PRId64") from start time %f\n",
214 s->in_pts_off, s->out_pts_off, s->start_time);
215 }
216
217 14 ret = ff_ccfifo_init(&s->cc_fifo, outlink->frame_rate, ctx);
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (ret < 0) {
219 av_log(ctx, AV_LOG_ERROR, "Failure to setup CC FIFO queue\n");
220 return ret;
221 }
222
223 14 av_log(ctx, AV_LOG_VERBOSE, "fps=%d/%d\n", outlink->frame_rate.num, outlink->frame_rate.den);
224
225 14 return 0;
226 }
227
228 /* Read a frame from the input and save it in the buffer */
229 485 static int read_frame(AVFilterContext *ctx, FPSContext *s, AVFilterLink *inlink, AVFilterLink *outlink)
230 {
231 AVFrame *frame;
232 int ret;
233 int64_t in_pts;
234
235 /* Must only be called when we have buffer room available */
236 av_assert1(s->frames_count < 2);
237
238 485 ret = ff_inlink_consume_frame(inlink, &frame);
239 /* Caller must have run ff_inlink_check_available_frame first */
240 av_assert1(ret);
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 485 times.
485 if (ret < 0)
242 return ret;
243
244 /* Convert frame pts to output timebase.
245 * The dance with offsets is required to match the rounding behaviour of the
246 * previous version of the fps filter when using the start_time option. */
247 485 in_pts = frame->pts;
248 485 frame->pts = s->out_pts_off + av_rescale_q_rnd(in_pts - s->in_pts_off,
249 inlink->time_base, outlink->time_base,
250 485 s->rounding | AV_ROUND_PASS_MINMAX);
251
252 485 av_log(ctx, AV_LOG_DEBUG, "Read frame with in pts %"PRId64", out pts %"PRId64"\n",
253 485 in_pts, frame->pts);
254
255 485 ff_ccfifo_extract(&s->cc_fifo, frame);
256 485 s->frames[s->frames_count++] = frame;
257 485 s->frames_in++;
258
259 485 return 1;
260 }
261
262 /* Write a frame to the output */
263 1366 static int write_frame(AVFilterContext *ctx, FPSContext *s, AVFilterLink *outlink, int *again)
264 {
265 AVFrame *frame;
266
267 av_assert1(s->frames_count == 2 || (s->status && s->frames_count == 1));
268
269 /* We haven't yet determined the pts of the first frame */
270
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1354 times.
1366 if (s->next_pts == AV_NOPTS_VALUE) {
271
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (s->frames[0]->pts != AV_NOPTS_VALUE) {
272 12 s->next_pts = s->frames[0]->pts;
273 12 av_log(ctx, AV_LOG_VERBOSE, "Set first pts to %"PRId64"\n", s->next_pts);
274 } else {
275 av_log(ctx, AV_LOG_WARNING, "Discarding initial frame(s) with no "
276 "timestamp.\n");
277 frame = shift_frame(ctx, s);
278 av_frame_free(&frame);
279 *again = 1;
280 return 0;
281 }
282 }
283
284 /* There are two conditions where we want to drop a frame:
285 * - If we have two buffered frames and the second frame is acceptable
286 * as the next output frame, then drop the first buffered frame.
287 * - If we have status (EOF) set, drop frames when we hit the
288 * status timestamp. */
289
4/4
✓ Branch 0 taken 1331 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 860 times.
✓ Branch 3 taken 471 times.
1366 if ((s->frames_count == 2 && s->frames[1]->pts <= s->next_pts) ||
290
4/4
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 860 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 21 times.
895 (s->status && s->status_pts <= s->next_pts)) {
291
292 485 frame = shift_frame(ctx, s);
293 485 av_frame_free(&frame);
294 485 *again = 1;
295 485 return 0;
296
297 /* Output a copy of the first buffered frame */
298 } else {
299 881 frame = av_frame_clone(s->frames[0]);
300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 881 times.
881 if (!frame)
301 return AVERROR(ENOMEM);
302 // Make sure Closed Captions will not be duplicated
303 881 ff_ccfifo_inject(&s->cc_fifo, frame);
304 881 frame->pts = s->next_pts++;
305 881 frame->duration = 1;
306
307 881 av_log(ctx, AV_LOG_DEBUG, "Writing frame with pts %"PRId64" to pts %"PRId64"\n",
308 881 s->frames[0]->pts, frame->pts);
309 881 s->cur_frame_out++;
310 881 *again = 1;
311 881 return ff_filter_frame(outlink, frame);
312 }
313 }
314
315 /* Convert status_pts to outlink timebase */
316 14 static void update_eof_pts(AVFilterContext *ctx, FPSContext *s, AVFilterLink *inlink, AVFilterLink *outlink, int64_t status_pts)
317 {
318
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 times.
14 int eof_rounding = (s->eof_action == EOF_ACTION_PASS) ? AV_ROUND_UP : s->rounding;
319 14 s->status_pts = av_rescale_q_rnd(status_pts, inlink->time_base, outlink->time_base,
320 14 eof_rounding | AV_ROUND_PASS_MINMAX);
321
322 14 av_log(ctx, AV_LOG_DEBUG, "EOF is at pts %"PRId64"\n", s->status_pts);
323 14 }
324
325 1881 static int activate(AVFilterContext *ctx)
326 {
327 1881 FPSContext *s = ctx->priv;
328 1881 AVFilterLink *inlink = ctx->inputs[0];
329 1881 AVFilterLink *outlink = ctx->outputs[0];
330
331 int ret;
332 1881 int again = 0;
333 int64_t status_pts;
334
335
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1879 times.
1881 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
336
337 /* No buffered status: normal operation */
338
2/2
✓ Branch 0 taken 1844 times.
✓ Branch 1 taken 35 times.
1879 if (!s->status) {
339
340 /* Read available input frames if we have room */
341
4/4
✓ Branch 0 taken 998 times.
✓ Branch 1 taken 1331 times.
✓ Branch 3 taken 485 times.
✓ Branch 4 taken 513 times.
2329 while (s->frames_count < 2 && ff_inlink_check_available_frame(inlink)) {
342 485 ret = read_frame(ctx, s, inlink, outlink);
343
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 485 times.
485 if (ret < 0)
344 return ret;
345 }
346
347 /* We do not yet have enough frames to produce output */
348
2/2
✓ Branch 0 taken 513 times.
✓ Branch 1 taken 1331 times.
1844 if (s->frames_count < 2) {
349 /* Check if we've hit EOF (or otherwise that an error status is set) */
350 513 ret = ff_inlink_acknowledge_status(inlink, &s->status, &status_pts);
351
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 499 times.
513 if (ret > 0)
352 14 update_eof_pts(ctx, s, inlink, outlink, status_pts);
353
354
2/2
✓ Branch 0 taken 499 times.
✓ Branch 1 taken 14 times.
513 if (!ret) {
355 /* If someone wants us to output, we'd better ask for more input */
356
2/2
✓ Branch 1 taken 496 times.
✓ Branch 2 taken 3 times.
499 FF_FILTER_FORWARD_WANTED(outlink, inlink);
357 3 return 0;
358 }
359 }
360 }
361
362 /* Buffered frames are available, so generate an output frame */
363
2/2
✓ Branch 0 taken 1366 times.
✓ Branch 1 taken 14 times.
1380 if (s->frames_count > 0) {
364 1366 ret = write_frame(ctx, s, outlink, &again);
365 /* Couldn't generate a frame, so schedule us to perform another step */
366
3/4
✓ Branch 0 taken 1366 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 506 times.
✓ Branch 4 taken 860 times.
1366 if (again && ff_inoutlink_check_flow(inlink, outlink))
367 506 ff_filter_set_ready(ctx, 100);
368 1366 return ret;
369 }
370
371 /* No frames left, so forward the status */
372
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
14 if (s->status && s->frames_count == 0) {
373 14 ff_outlink_set_status(outlink, s->status, s->next_pts);
374 14 return 0;
375 }
376
377 return FFERROR_NOT_READY;
378 }
379
380 static const AVFilterPad avfilter_vf_fps_outputs[] = {
381 {
382 .name = "default",
383 .type = AVMEDIA_TYPE_VIDEO,
384 .config_props = config_props,
385 },
386 };
387
388 const AVFilter ff_vf_fps = {
389 .name = "fps",
390 .description = NULL_IF_CONFIG_SMALL("Force constant framerate."),
391 .init = init,
392 .uninit = uninit,
393 .priv_size = sizeof(FPSContext),
394 .priv_class = &fps_class,
395 .activate = activate,
396 .flags = AVFILTER_FLAG_METADATA_ONLY,
397 FILTER_INPUTS(ff_video_default_filterpad),
398 FILTER_OUTPUTS(avfilter_vf_fps_outputs),
399 };
400