FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_fps.c
Date: 2026-10-20 22:17:59
Exec Total Coverage
Lines: 127 143 88.8%
Functions: 8 8 100.0%
Branches: 72 86 83.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 "video.h"
40
41 enum EOFAction {
42 EOF_ACTION_ROUND,
43 EOF_ACTION_PASS,
44 EOF_ACTION_NB
45 };
46
47 static const char *const var_names[] = {
48 "source_fps",
49 "ntsc",
50 "pal",
51 "film",
52 "ntsc_film",
53 NULL
54 };
55
56 enum var_name {
57 VAR_SOURCE_FPS,
58 VAR_FPS_NTSC,
59 VAR_FPS_PAL,
60 VAR_FPS_FILM,
61 VAR_FPS_NTSC_FILM,
62 VARS_NB
63 };
64
65 static const double ntsc_fps = 30000.0 / 1001.0;
66 static const double pal_fps = 25.0;
67 static const double film_fps = 24.0;
68 static const double ntsc_film_fps = 24000.0 / 1001.0;
69
70 typedef struct FPSContext {
71 const AVClass *class;
72
73 double start_time; ///< pts, in seconds, of the expected first frame
74
75 char *framerate; ///< expression that defines the target framerate
76 int rounding; ///< AVRounding method for timestamps
77 int eof_action; ///< action performed for last frame in FIFO
78
79 /* Set during outlink configuration */
80 int64_t in_pts_off; ///< input frame pts offset for start_time handling
81 int64_t out_pts_off; ///< output frame pts offset for start_time handling
82
83 /* Runtime state */
84 int status; ///< buffered input status
85 int64_t status_pts; ///< buffered input status timestamp
86 int64_t last_in_pts; ///< last frame timestamp in input timebase
87 int last_frame_trimmed; ///< last frame ends at or before start_time
88
89 AVFrame *frames[2]; ///< buffered frames
90 int frames_count; ///< number of buffered frames
91 CCFifo cc_fifo; ///< closed captions
92
93 int64_t next_pts; ///< pts of the next frame to output
94
95 /* statistics */
96 int cur_frame_out; ///< number of times current frame has been output
97 int frames_in; ///< number of frames on input
98 int frames_out; ///< number of frames on output
99 int dup; ///< number of frames duplicated
100 int drop; ///< number of framed dropped
101 } FPSContext;
102
103 #define OFFSET(x) offsetof(FPSContext, x)
104 #define V AV_OPT_FLAG_VIDEO_PARAM
105 #define F AV_OPT_FLAG_FILTERING_PARAM
106 static const AVOption fps_options[] = {
107 { "fps", "A string describing desired output framerate", OFFSET(framerate), AV_OPT_TYPE_STRING, { .str = "25" }, 0, 0, V|F },
108 { "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 },
109 { "round", "set rounding method for timestamps", OFFSET(rounding), AV_OPT_TYPE_INT, { .i64 = AV_ROUND_NEAR_INF }, 0, 5, V|F, .unit = "round" },
110 { "zero", "round towards 0", 0, AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_ZERO }, 0, 0, V|F, .unit = "round" },
111 { "inf", "round away from 0", 0, AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_INF }, 0, 0, V|F, .unit = "round" },
112 { "down", "round towards -infty", 0, AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_DOWN }, 0, 0, V|F, .unit = "round" },
113 { "up", "round towards +infty", 0, AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_UP }, 0, 0, V|F, .unit = "round" },
114 { "near", "round to nearest", 0, AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_NEAR_INF }, 0, 0, V|F, .unit = "round" },
115 { "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" },
116 { "round", "round similar to other frames", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_ROUND }, 0, 0, V|F, .unit = "eof_action" },
117 { "pass", "pass through last frame", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_PASS }, 0, 0, V|F, .unit = "eof_action" },
118 { NULL }
119 };
120
121 AVFILTER_DEFINE_CLASS(fps);
122
123 50 static av_cold int init(AVFilterContext *ctx)
124 {
125 50 FPSContext *s = ctx->priv;
126
127 50 s->status_pts = AV_NOPTS_VALUE;
128 50 s->next_pts = AV_NOPTS_VALUE;
129
130 50 return 0;
131 }
132
133 /* Remove the first frame from the buffer, returning it */
134 554 static AVFrame *shift_frame(AVFilterContext *ctx, FPSContext *s)
135 {
136 AVFrame *frame;
137
138 /* Must only be called when there are frames in the buffer */
139 av_assert1(s->frames_count > 0);
140
141 554 frame = s->frames[0];
142 554 s->frames[0] = s->frames[1];
143 554 s->frames[1] = NULL;
144 554 s->frames_count--;
145
146 /* Update statistics counters */
147 554 s->frames_out += s->cur_frame_out;
148
2/2
✓ Branch 0 taken 323 times.
✓ Branch 1 taken 231 times.
554 if (s->cur_frame_out > 1) {
149 323 av_log(ctx, AV_LOG_DEBUG, "Duplicated frame with pts %"PRId64" %d times\n",
150 323 frame->pts, s->cur_frame_out - 1);
151 323 s->dup += s->cur_frame_out - 1;
152
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 147 times.
231 } else if (s->cur_frame_out == 0) {
153 84 av_log(ctx, AV_LOG_DEBUG, "Dropping frame with pts %"PRId64"\n",
154 frame->pts);
155 84 s->drop++;
156 }
157 554 s->cur_frame_out = 0;
158
159 554 return frame;
160 }
161
162 50 static av_cold void uninit(AVFilterContext *ctx)
163 {
164 50 FPSContext *s = ctx->priv;
165
166 AVFrame *frame;
167
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 while (s->frames_count > 0) {
169 ✗ frame = shift_frame(ctx, s);
170 ✗ av_frame_free(&frame);
171 }
172 50 ff_ccfifo_uninit(&s->cc_fifo);
173
174 50 av_log(ctx, AV_LOG_VERBOSE, "%d frames in, %d frames out; %d frames dropped, "
175 "%d frames duplicated.\n", s->frames_in, s->frames_out, s->drop, s->dup);
176 50 }
177
178 25 static int config_props(AVFilterLink* outlink)
179 {
180 25 AVFilterContext *ctx = outlink->src;
181 25 AVFilterLink *inlink = ctx->inputs[0];
182 25 FilterLink *il = ff_filter_link(inlink);
183 25 FilterLink *ol = ff_filter_link(outlink);
184 25 FPSContext *s = ctx->priv;
185
186 double var_values[VARS_NB], res;
187 int ret;
188
189 25 var_values[VAR_SOURCE_FPS] = av_q2d(il->frame_rate);
190 25 var_values[VAR_FPS_NTSC] = ntsc_fps;
191 25 var_values[VAR_FPS_PAL] = pal_fps;
192 25 var_values[VAR_FPS_FILM] = film_fps;
193 25 var_values[VAR_FPS_NTSC_FILM] = ntsc_film_fps;
194 25 ret = av_expr_parse_and_eval(&res, s->framerate,
195 var_names, var_values,
196 NULL, NULL, NULL, NULL, NULL, 0, ctx);
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (ret < 0)
198 ✗ return ret;
199
200 25 ol->frame_rate = av_d2q(res, INT_MAX);
201 25 outlink->time_base = av_inv_q(ol->frame_rate);
202
203 /* Calculate the input and output pts offsets for start_time */
204
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
25 if (s->start_time != DBL_MAX && s->start_time != AV_NOPTS_VALUE) {
205 6 double first_pts = s->start_time * AV_TIME_BASE;
206
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (first_pts < INT64_MIN || first_pts > INT64_MAX) {
207 ✗ av_log(ctx, AV_LOG_ERROR, "Start time %f cannot be represented in internal time base\n",
208 s->start_time);
209 ✗ return AVERROR(EINVAL);
210 }
211 6 s->in_pts_off = av_rescale_q_rnd(first_pts, AV_TIME_BASE_Q, inlink->time_base,
212 6 s->rounding | AV_ROUND_PASS_MINMAX);
213 6 s->out_pts_off = av_rescale_q_rnd(first_pts, AV_TIME_BASE_Q, outlink->time_base,
214 6 s->rounding | AV_ROUND_PASS_MINMAX);
215 6 s->next_pts = s->out_pts_off;
216 6 av_log(ctx, AV_LOG_VERBOSE, "Set first pts to (in:%"PRId64" out:%"PRId64") from start time %f\n",
217 s->in_pts_off, s->out_pts_off, s->start_time);
218 }
219
220 25 ret = ff_ccfifo_init(&s->cc_fifo, ol->frame_rate, ctx);
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (ret < 0) {
222 ✗ av_log(ctx, AV_LOG_ERROR, "Failure to setup CC FIFO queue\n");
223 ✗ return ret;
224 }
225
226 25 av_log(ctx, AV_LOG_VERBOSE, "fps=%d/%d\n", ol->frame_rate.num, ol->frame_rate.den);
227
228 25 return 0;
229 }
230
231 /* Read a frame from the input and save it in the buffer */
232 554 static int read_frame(AVFilterContext *ctx, FPSContext *s, AVFilterLink *inlink, AVFilterLink *outlink)
233 {
234 AVFrame *frame;
235 int ret;
236 int64_t in_pts;
237
238 /* Must only be called when we have buffer room available */
239 av_assert1(s->frames_count < 2);
240
241 554 ret = ff_inlink_consume_frame(inlink, &frame);
242 /* Caller must have run ff_inlink_check_available_frame first */
243 av_assert1(ret);
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 554 times.
554 if (ret < 0)
245 ✗ return ret;
246
247 /* Convert frame pts to output timebase.
248 * The dance with offsets is required to match the rounding behaviour of the
249 * previous version of the fps filter when using the start_time option. */
250 554 in_pts = s->last_in_pts = frame->pts;
251 554 frame->pts = s->out_pts_off + av_rescale_q_rnd(in_pts - s->in_pts_off,
252 inlink->time_base, outlink->time_base,
253 554 s->rounding | AV_ROUND_PASS_MINMAX);
254
255 554 av_log(ctx, AV_LOG_DEBUG, "Read frame with in pts %"PRId64", out pts %"PRId64"\n",
256 554 in_pts, frame->pts);
257
258 554 ff_ccfifo_extract(&s->cc_fifo, frame);
259 554 s->frames[s->frames_count++] = frame;
260 554 s->frames_in++;
261
262 554 return 1;
263 }
264
265 /* Write a frame to the output */
266 1673 static int write_frame(AVFilterContext *ctx, FPSContext *s, AVFilterLink *outlink, int *again)
267 {
268 AVFrame *frame;
269 int eof_before_or_at_next;
270 int first_output_due;
271
272 av_assert1(s->frames_count == 2 || (s->status && s->frames_count == 1));
273
274 /* We haven't yet determined the pts of the first frame */
275
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1654 times.
1673 if (s->next_pts == AV_NOPTS_VALUE) {
276
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 if (s->frames[0]->pts != AV_NOPTS_VALUE) {
277 19 s->next_pts = s->frames[0]->pts;
278 19 av_log(ctx, AV_LOG_VERBOSE, "Set first pts to %"PRId64"\n", s->next_pts);
279 } else {
280 ✗ av_log(ctx, AV_LOG_WARNING, "Discarding initial frame(s) with no "
281 "timestamp.\n");
282 ✗ frame = shift_frame(ctx, s);
283 ✗ av_frame_free(&frame);
284 ✗ *again = 1;
285 ✗ return 0;
286 }
287 }
288
289
4/4
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 1593 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 52 times.
1673 eof_before_or_at_next = s->status && s->status_pts <= s->next_pts;
290
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 61 times.
102 first_output_due = !s->frames_out && !s->cur_frame_out &&
291
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 2 times.
41 !s->last_frame_trimmed &&
292
4/4
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 1571 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 11 times.
1803 s->frames[0]->pts == s->next_pts &&
293
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 25 times.
28 s->status_pts == s->next_pts;
294
295 /* There are two conditions where we want to drop a frame:
296 * - If we have two buffered frames and the second frame is acceptable
297 * as the next output frame, then drop the first buffered frame.
298 * - If we have status (EOF) set, drop frames when we hit the
299 * status timestamp, unless no frame has been output yet and the
300 * buffered frame is due at the next output timestamp. */
301
6/6
✓ Branch 0 taken 1593 times.
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 1064 times.
✓ Branch 3 taken 529 times.
✓ Branch 4 taken 28 times.
✓ Branch 5 taken 1116 times.
1673 if ((s->frames_count == 2 && s->frames[1]->pts <= s->next_pts) ||
302
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 3 times.
28 (eof_before_or_at_next && !first_output_due)) {
303
304 554 frame = shift_frame(ctx, s);
305 554 av_frame_free(&frame);
306 554 *again = 1;
307 554 return 0;
308
309 /* Output a copy of the first buffered frame */
310 } else {
311 1119 frame = av_frame_clone(s->frames[0]);
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1119 times.
1119 if (!frame)
313 ✗ return AVERROR(ENOMEM);
314 // Make sure Closed Captions will not be duplicated
315 1119 ff_ccfifo_inject(&s->cc_fifo, frame);
316 1119 frame->pts = s->next_pts++;
317 1119 frame->duration = 1;
318
319 1119 av_log(ctx, AV_LOG_DEBUG, "Writing frame with pts %"PRId64" to pts %"PRId64"\n",
320 1119 s->frames[0]->pts, frame->pts);
321 1119 s->cur_frame_out++;
322 1119 *again = 1;
323 1119 return ff_filter_frame(outlink, frame);
324 }
325 }
326
327 /* Convert status_pts to outlink timebase */
328 25 static void update_eof_pts(AVFilterContext *ctx, FPSContext *s, AVFilterLink *inlink, AVFilterLink *outlink, int64_t status_pts)
329 {
330
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 1 times.
25 int eof_rounding = (s->eof_action == EOF_ACTION_PASS) ? AV_ROUND_UP : s->rounding;
331
332 /* Check trimming before rounding can collapse a frame before start_time
333 * and the end of its interval onto the first output timestamp. A frame
334 * starting at start_time is still eligible even with zero duration. */
335 56 s->last_frame_trimmed = s->start_time != DBL_MAX &&
336
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 s->start_time != AV_NOPTS_VALUE &&
337
4/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
34 s->last_in_pts < s->in_pts_off &&
338
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 status_pts <= s->in_pts_off;
339 25 s->status_pts = av_rescale_q_rnd(status_pts, inlink->time_base, outlink->time_base,
340 25 eof_rounding | AV_ROUND_PASS_MINMAX);
341
342 25 av_log(ctx, AV_LOG_DEBUG, "EOF is at pts %"PRId64"\n", s->status_pts);
343 25 }
344
345 2281 static int activate(AVFilterContext *ctx)
346 {
347 2281 FPSContext *s = ctx->priv;
348 2281 AVFilterLink *inlink = ctx->inputs[0];
349 2281 AVFilterLink *outlink = ctx->outputs[0];
350
351 int ret;
352 2281 int again = 0;
353 int64_t status_pts;
354
355
2/2
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 2274 times.
2281 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
356
357 /* No buffered status: normal operation */
358
2/2
✓ Branch 0 taken 2194 times.
✓ Branch 1 taken 80 times.
2274 if (!s->status) {
359
360 /* Read available input frames if we have room */
361
4/4
✓ Branch 0 taken 1155 times.
✓ Branch 1 taken 1593 times.
✓ Branch 3 taken 554 times.
✓ Branch 4 taken 601 times.
2748 while (s->frames_count < 2 && ff_inlink_check_available_frame(inlink)) {
362 554 ret = read_frame(ctx, s, inlink, outlink);
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 554 times.
554 if (ret < 0)
364 ✗ return ret;
365 }
366
367 /* We do not yet have enough frames to produce output */
368
2/2
✓ Branch 0 taken 601 times.
✓ Branch 1 taken 1593 times.
2194 if (s->frames_count < 2) {
369 /* Check if we've hit EOF (or otherwise that an error status is set) */
370 601 ret = ff_inlink_acknowledge_status(inlink, &s->status, &status_pts);
371
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 576 times.
601 if (ret > 0)
372 25 update_eof_pts(ctx, s, inlink, outlink, status_pts);
373
374
2/2
✓ Branch 0 taken 576 times.
✓ Branch 1 taken 25 times.
601 if (!ret) {
375 /* If someone wants us to output, we'd better ask for more input */
376
2/2
✓ Branch 1 taken 573 times.
✓ Branch 2 taken 3 times.
576 FF_FILTER_FORWARD_WANTED(outlink, inlink);
377 3 return 0;
378 }
379 }
380 }
381
382 /* Buffered frames are available, so generate an output frame */
383
2/2
✓ Branch 0 taken 1673 times.
✓ Branch 1 taken 25 times.
1698 if (s->frames_count > 0) {
384 1673 ret = write_frame(ctx, s, outlink, &again);
385 /* Couldn't generate a frame, so schedule us to perform another step */
386
3/4
✓ Branch 0 taken 1673 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 676 times.
✓ Branch 4 taken 997 times.
1673 if (again && ff_inoutlink_check_flow(inlink, outlink))
387 676 ff_filter_set_ready(ctx, 100);
388 1673 return ret;
389 }
390
391 /* No frames left, so forward the status */
392
2/4
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
25 if (s->status && s->frames_count == 0) {
393 25 ff_outlink_set_status(outlink, s->status, s->next_pts);
394 25 return 0;
395 }
396
397 ✗ return FFERROR_NOT_READY;
398 }
399
400 static const AVFilterPad avfilter_vf_fps_outputs[] = {
401 {
402 .name = "default",
403 .type = AVMEDIA_TYPE_VIDEO,
404 .config_props = config_props,
405 },
406 };
407
408 const FFFilter ff_vf_fps = {
409 .p.name = "fps",
410 .p.description = NULL_IF_CONFIG_SMALL("Force constant framerate."),
411 .p.priv_class = &fps_class,
412 .p.flags = AVFILTER_FLAG_METADATA_ONLY,
413 .init = init,
414 .uninit = uninit,
415 .priv_size = sizeof(FPSContext),
416 .activate = activate,
417 FILTER_INPUTS(ff_video_default_filterpad),
418 FILTER_OUTPUTS(avfilter_vf_fps_outputs),
419 .flags_internal = FF_FILTER_FLAG_DYNAMIC_FRAME_PARAMS,
420 };
421