FFmpeg
vf_fps.c
Go to the documentation of this file.
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/mathematics.h"
34 #include "libavutil/opt.h"
35 #include "avfilter.h"
36 #include "filters.h"
37 #include "internal.h"
38 
39 enum EOFAction {
43 };
44 
45 typedef struct FPSContext {
46  const AVClass *class;
47 
48  double start_time; ///< pts, in seconds, of the expected first frame
49 
50  AVRational framerate; ///< target framerate
51  int rounding; ///< AVRounding method for timestamps
52  int eof_action; ///< action performed for last frame in FIFO
53 
54  /* Set during outlink configuration */
55  int64_t in_pts_off; ///< input frame pts offset for start_time handling
56  int64_t out_pts_off; ///< output frame pts offset for start_time handling
57 
58  /* Runtime state */
59  int status; ///< buffered input status
60  int64_t status_pts; ///< buffered input status timestamp
61 
62  AVFrame *frames[2]; ///< buffered frames
63  int frames_count; ///< number of buffered frames
64 
65  int64_t next_pts; ///< pts of the next frame to output
66 
67  /* statistics */
68  int cur_frame_out; ///< number of times current frame has been output
69  int frames_in; ///< number of frames on input
70  int frames_out; ///< number of frames on output
71  int dup; ///< number of frames duplicated
72  int drop; ///< number of framed dropped
73 } FPSContext;
74 
75 #define OFFSET(x) offsetof(FPSContext, x)
76 #define V AV_OPT_FLAG_VIDEO_PARAM
77 #define F AV_OPT_FLAG_FILTERING_PARAM
78 static const AVOption fps_options[] = {
79  { "fps", "A string describing desired output framerate", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, { .str = "25" }, 0, INT_MAX, V|F },
80  { "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 },
81  { "round", "set rounding method for timestamps", OFFSET(rounding), AV_OPT_TYPE_INT, { .i64 = AV_ROUND_NEAR_INF }, 0, 5, V|F, "round" },
82  { "zero", "round towards 0", 0, AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_ZERO }, 0, 0, V|F, "round" },
83  { "inf", "round away from 0", 0, AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_INF }, 0, 0, V|F, "round" },
84  { "down", "round towards -infty", 0, AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_DOWN }, 0, 0, V|F, "round" },
85  { "up", "round towards +infty", 0, AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_UP }, 0, 0, V|F, "round" },
86  { "near", "round to nearest", 0, AV_OPT_TYPE_CONST, { .i64 = AV_ROUND_NEAR_INF }, 0, 0, V|F, "round" },
87  { "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, "eof_action" },
88  { "round", "round similar to other frames", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_ROUND }, 0, 0, V|F, "eof_action" },
89  { "pass", "pass through last frame", 0, AV_OPT_TYPE_CONST, { .i64 = EOF_ACTION_PASS }, 0, 0, V|F, "eof_action" },
90  { NULL }
91 };
92 
94 
96 {
97  FPSContext *s = ctx->priv;
98 
101 
102  av_log(ctx, AV_LOG_VERBOSE, "fps=%d/%d\n", s->framerate.num, s->framerate.den);
103  return 0;
104 }
105 
106 /* Remove the first frame from the buffer, returning it */
108 {
109  AVFrame *frame;
110 
111  /* Must only be called when there are frames in the buffer */
112  av_assert1(s->frames_count > 0);
113 
114  frame = s->frames[0];
115  s->frames[0] = s->frames[1];
116  s->frames[1] = NULL;
117  s->frames_count--;
118 
119  /* Update statistics counters */
120  s->frames_out += s->cur_frame_out;
121  if (s->cur_frame_out > 1) {
122  av_log(ctx, AV_LOG_DEBUG, "Duplicated frame with pts %"PRId64" %d times\n",
123  frame->pts, s->cur_frame_out - 1);
124  s->dup += s->cur_frame_out - 1;
125  } else if (s->cur_frame_out == 0) {
126  av_log(ctx, AV_LOG_DEBUG, "Dropping frame with pts %"PRId64"\n",
127  frame->pts);
128  s->drop++;
129  }
130  s->cur_frame_out = 0;
131 
132  return frame;
133 }
134 
136 {
137  FPSContext *s = ctx->priv;
138 
139  AVFrame *frame;
140 
141  while (s->frames_count > 0) {
142  frame = shift_frame(ctx, s);
143  av_frame_free(&frame);
144  }
145 
146  av_log(ctx, AV_LOG_VERBOSE, "%d frames in, %d frames out; %d frames dropped, "
147  "%d frames duplicated.\n", s->frames_in, s->frames_out, s->drop, s->dup);
148 }
149 
150 static int config_props(AVFilterLink* outlink)
151 {
152  AVFilterContext *ctx = outlink->src;
153  AVFilterLink *inlink = ctx->inputs[0];
154  FPSContext *s = ctx->priv;
155 
156  outlink->time_base = av_inv_q(s->framerate);
157  outlink->frame_rate = s->framerate;
158 
159  /* Calculate the input and output pts offsets for start_time */
160  if (s->start_time != DBL_MAX && s->start_time != AV_NOPTS_VALUE) {
161  double first_pts = s->start_time * AV_TIME_BASE;
162  if (first_pts < INT64_MIN || first_pts > INT64_MAX) {
163  av_log(ctx, AV_LOG_ERROR, "Start time %f cannot be represented in internal time base\n",
164  s->start_time);
165  return AVERROR(EINVAL);
166  }
167  s->in_pts_off = av_rescale_q_rnd(first_pts, AV_TIME_BASE_Q, inlink->time_base,
169  s->out_pts_off = av_rescale_q_rnd(first_pts, AV_TIME_BASE_Q, outlink->time_base,
171  s->next_pts = s->out_pts_off;
172  av_log(ctx, AV_LOG_VERBOSE, "Set first pts to (in:%"PRId64" out:%"PRId64") from start time %f\n",
173  s->in_pts_off, s->out_pts_off, s->start_time);
174  }
175 
176  return 0;
177 }
178 
179 /* Read a frame from the input and save it in the buffer */
181 {
182  AVFrame *frame;
183  int ret;
184  int64_t in_pts;
185 
186  /* Must only be called when we have buffer room available */
187  av_assert1(s->frames_count < 2);
188 
189  ret = ff_inlink_consume_frame(inlink, &frame);
190  /* Caller must have run ff_inlink_check_available_frame first */
191  av_assert1(ret);
192  if (ret < 0)
193  return ret;
194 
195  /* Convert frame pts to output timebase.
196  * The dance with offsets is required to match the rounding behaviour of the
197  * previous version of the fps filter when using the start_time option. */
198  in_pts = frame->pts;
199  frame->pts = s->out_pts_off + av_rescale_q_rnd(in_pts - s->in_pts_off,
200  inlink->time_base, outlink->time_base,
202 
203  av_log(ctx, AV_LOG_DEBUG, "Read frame with in pts %"PRId64", out pts %"PRId64"\n",
204  in_pts, frame->pts);
205 
206  s->frames[s->frames_count++] = frame;
207  s->frames_in++;
208 
209  return 1;
210 }
211 
212 /* Write a frame to the output */
214 {
215  AVFrame *frame;
216 
217  av_assert1(s->frames_count == 2 || (s->status && s->frames_count == 1));
218 
219  /* We haven't yet determined the pts of the first frame */
220  if (s->next_pts == AV_NOPTS_VALUE) {
221  if (s->frames[0]->pts != AV_NOPTS_VALUE) {
222  s->next_pts = s->frames[0]->pts;
223  av_log(ctx, AV_LOG_VERBOSE, "Set first pts to %"PRId64"\n", s->next_pts);
224  } else {
225  av_log(ctx, AV_LOG_WARNING, "Discarding initial frame(s) with no "
226  "timestamp.\n");
227  frame = shift_frame(ctx, s);
228  av_frame_free(&frame);
229  *again = 1;
230  return 0;
231  }
232  }
233 
234  /* There are two conditions where we want to drop a frame:
235  * - If we have two buffered frames and the second frame is acceptable
236  * as the next output frame, then drop the first buffered frame.
237  * - If we have status (EOF) set, drop frames when we hit the
238  * status timestamp. */
239  if ((s->frames_count == 2 && s->frames[1]->pts <= s->next_pts) ||
240  (s->status && s->status_pts <= s->next_pts)) {
241 
242  frame = shift_frame(ctx, s);
243  av_frame_free(&frame);
244  *again = 1;
245  return 0;
246 
247  /* Output a copy of the first buffered frame */
248  } else {
249  frame = av_frame_clone(s->frames[0]);
250  if (!frame)
251  return AVERROR(ENOMEM);
252  // Make sure Closed Captions will not be duplicated
254  frame->pts = s->next_pts++;
255 
256  av_log(ctx, AV_LOG_DEBUG, "Writing frame with pts %"PRId64" to pts %"PRId64"\n",
257  s->frames[0]->pts, frame->pts);
258  s->cur_frame_out++;
259  *again = 1;
260  return ff_filter_frame(outlink, frame);
261  }
262 }
263 
264 /* Convert status_pts to outlink timebase */
266 {
267  int eof_rounding = (s->eof_action == EOF_ACTION_PASS) ? AV_ROUND_UP : s->rounding;
268  s->status_pts = av_rescale_q_rnd(status_pts, inlink->time_base, outlink->time_base,
269  eof_rounding | AV_ROUND_PASS_MINMAX);
270 
271  av_log(ctx, AV_LOG_DEBUG, "EOF is at pts %"PRId64"\n", s->status_pts);
272 }
273 
275 {
276  FPSContext *s = ctx->priv;
277  AVFilterLink *inlink = ctx->inputs[0];
278  AVFilterLink *outlink = ctx->outputs[0];
279 
280  int ret;
281  int again = 0;
282  int64_t status_pts;
283 
284  FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
285 
286  /* No buffered status: normal operation */
287  if (!s->status) {
288 
289  /* Read available input frames if we have room */
290  while (s->frames_count < 2 && ff_inlink_check_available_frame(inlink)) {
291  ret = read_frame(ctx, s, inlink, outlink);
292  if (ret < 0)
293  return ret;
294  }
295 
296  /* We do not yet have enough frames to produce output */
297  if (s->frames_count < 2) {
298  /* Check if we've hit EOF (or otherwise that an error status is set) */
299  ret = ff_inlink_acknowledge_status(inlink, &s->status, &status_pts);
300  if (ret > 0)
301  update_eof_pts(ctx, s, inlink, outlink, status_pts);
302 
303  if (!ret) {
304  /* If someone wants us to output, we'd better ask for more input */
305  FF_FILTER_FORWARD_WANTED(outlink, inlink);
306  return 0;
307  }
308  }
309  }
310 
311  /* Buffered frames are available, so generate an output frame */
312  if (s->frames_count > 0) {
313  ret = write_frame(ctx, s, outlink, &again);
314  /* Couldn't generate a frame, so schedule us to perform another step */
315  if (again)
316  ff_filter_set_ready(ctx, 100);
317  return ret;
318  }
319 
320  /* No frames left, so forward the status */
321  if (s->status && s->frames_count == 0) {
322  ff_outlink_set_status(outlink, s->status, s->next_pts);
323  return 0;
324  }
325 
326  return FFERROR_NOT_READY;
327 }
328 
330  {
331  .name = "default",
332  .type = AVMEDIA_TYPE_VIDEO,
333  },
334  { NULL }
335 };
336 
338  {
339  .name = "default",
340  .type = AVMEDIA_TYPE_VIDEO,
341  .config_props = config_props,
342  },
343  { NULL }
344 };
345 
347  .name = "fps",
348  .description = NULL_IF_CONFIG_SMALL("Force constant framerate."),
349  .init = init,
350  .uninit = uninit,
351  .priv_size = sizeof(FPSContext),
352  .priv_class = &fps_class,
353  .activate = activate,
354  .inputs = avfilter_vf_fps_inputs,
355  .outputs = avfilter_vf_fps_outputs,
356 };
int frames_out
number of frames on output
Definition: vf_fps.c:70
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link&#39;s FIFO and update the link&#39;s stats.
Definition: avfilter.c:1494
#define NULL
Definition: coverity.c:32
#define F
Definition: vf_fps.c:77
AVFilter ff_vf_fps
Definition: vf_fps.c:346
static const AVOption fps_options[]
Definition: vf_fps.c:78
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
AVOption.
Definition: opt.h:246
static int config_props(AVFilterLink *outlink)
Definition: vf_fps.c:150
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
Main libavfilter public API header.
int num
Numerator.
Definition: rational.h:59
EOFAction
Definition: framesync.h:26
AVFrame * frames[2]
buffered frames
Definition: vf_fps.c:62
int drop
number of framed dropped
Definition: vf_fps.c:72
return FFERROR_NOT_READY
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:189
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1093
int64_t out_pts_off
output frame pts offset for start_time handling
Definition: vf_fps.c:56
Round toward +infinity.
Definition: mathematics.h:83
#define av_cold
Definition: attributes.h:82
int frames_in
number of frames on input
Definition: vf_fps.c:69
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:388
static av_cold int init(AVFilterContext *ctx)
Definition: vf_fps.c:95
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
#define av_log(a,...)
static void update_eof_pts(AVFilterContext *ctx, FPSContext *s, AVFilterLink *inlink, AVFilterLink *outlink, int64_t status_pts)
Definition: vf_fps.c:265
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:199
A filter pad used for either input or output.
Definition: internal.h:54
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition: avfilter.c:1449
int dup
number of frames duplicated
Definition: vf_fps.c:71
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int eof_action
action performed for last frame in FIFO
Definition: vf_fps.c:52
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_fps.c:135
Round away from zero.
Definition: mathematics.h:81
int frames_count
number of buffered frames
Definition: vf_fps.c:63
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
ATSC A53 Part 4 Closed Captions.
Definition: frame.h:58
static const AVFilterPad avfilter_vf_fps_inputs[]
Definition: vf_fps.c:329
Round to nearest and halfway cases away from zero.
Definition: mathematics.h:84
simple assert() macros that are a bit more flexible than ISO C assert().
#define V
Definition: vf_fps.c:76
static const AVFilterPad avfilter_vf_fps_outputs[]
Definition: vf_fps.c:337
static int write_frame(AVFilterContext *ctx, FPSContext *s, AVFilterLink *outlink, int *again)
Definition: vf_fps.c:213
#define OFFSET(x)
Definition: vf_fps.c:75
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
Round toward zero.
Definition: mathematics.h:80
static int read_frame(AVFilterContext *ctx, FPSContext *s, AVFilterLink *inlink, AVFilterLink *outlink)
Definition: vf_fps.c:180
int64_t next_pts
pts of the next frame to output
Definition: vf_fps.c:65
AVFormatContext * ctx
Definition: movenc.c:48
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
#define s(width, name)
Definition: cbs_vp9.c:257
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:540
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining again
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type)
If side data of the supplied type exists in the frame, free it and remove it from the frame...
Definition: frame.c:805
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
Rational number (pair of numerator and denominator).
Definition: rational.h:58
int ff_inlink_check_available_frame(AVFilterLink *link)
Test if a frame is available on the link.
Definition: avfilter.c:1469
offset must point to AVRational
Definition: opt.h:236
const char * name
Filter name.
Definition: avfilter.h:148
static AVFrame * shift_frame(AVFilterContext *ctx, FPSContext *s)
Definition: vf_fps.c:107
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
Round toward -infinity.
Definition: mathematics.h:82
AVFILTER_DEFINE_CLASS(fps)
static int activate(AVFilterContext *ctx)
Definition: vf_fps.c:274
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
AVRational framerate
target framerate
Definition: vf_fps.c:50
int status
buffered input status
Definition: vf_fps.c:59
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition: avfilter.c:193
int den
Denominator.
Definition: rational.h:60
FF_FILTER_FORWARD_WANTED(outlink, inlink)
Flag telling rescaling functions to pass INT64_MIN/MAX through unchanged, avoiding special cases for ...
Definition: mathematics.h:108
int cur_frame_out
number of times current frame has been output
Definition: vf_fps.c:68
int64_t status_pts
buffered input status timestamp
Definition: vf_fps.c:60
int rounding
AVRounding method for timestamps.
Definition: vf_fps.c:51
An instance of a filter.
Definition: avfilter.h:338
int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq, enum AVRounding rnd)
Rescale a 64-bit integer by 2 rational numbers with specified rounding.
Definition: mathematics.c:134
internal API functions
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
int64_t in_pts_off
input frame pts offset for start_time handling
Definition: vf_fps.c:55
double start_time
pts, in seconds, of the expected first frame
Definition: vf_fps.c:48
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248