2 * Copyright (c) 2013 Nicolas George
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21#ifndef AVFILTER_FRAMESYNC_H
22#define AVFILTER_FRAMESYNC_H
34 * Export convenient options.
38 * This API is intended as a helper for filters that have several video
39 * input and need to combine them somehow. If the inputs have different or
40 * variable frame rate, getting the input frames to match requires a rather
41 * complex logic and a few user-tunable options.
43 * In this API, when a set of synchronized input frames is ready to be
44 * processed is called a frame event. Frame event can be generated in
45 * response to input frames on any or all inputs and the handling of
46 * situations where some stream extend beyond the beginning or the end of
47 * others can be configured.
49 * The basic working of this API is the following: set the on_event
50 * callback, then call ff_framesync_activate() from the filter's activate
55 * Stream extrapolation mode
57 * Describe how the frames of a stream are extrapolated before the first one
58 * and after EOF to keep sync with possibly longer other streams.
63 * Completely stop all streams with this one.
68 * Ignore this stream and continue processing the other ones.
73 * Extend the frame to infinity.
79 * Timestamp synchronization mode
81 * Describe how the frames of a stream are synchronized based on timestamp
87 * Sync to frames from secondary input with the nearest, lower or equal
88 * timestamp to the frame event one.
93 * Sync to frames from secondary input with the absolute nearest timestamp
94 * to the frame event one.
100 * Input stream structure
105 * Extrapolation mode for timestamps before the first frame
110 * Extrapolation mode for timestamps after the last frame
115 * Time base for the incoming frames
120 * Current frame, may be NULL before the first one or after EOF
125 * Next frame, for internal use
130 * PTS of the current frame
135 * PTS of the next frame, for internal use
140 * Boolean flagging the next frame, for internal use
145 * State: before first, in stream or after EOF, for internal use
150 * Synchronization level: frames on input at the highest sync level will
151 * generate output frame events.
153 * For example, if inputs #0 and #1 have sync level 2 and input #2 has
154 * sync level 1, then a frame on either input #0 or #1 will generate a
155 * frame event, but not a frame on input #2 until both inputs #0 and #1
158 * If sync is 0, no frame event will be generated.
166 * Frame sync structure.
172 * Parent filter context.
177 * Number of input streams
182 * Time base for the output events
187 * Timestamp of the current event
192 * Callback called when a frame event is ready
197 * Opaque pointer, not used by the API
202 * Index of the input that requires a request
207 * Synchronization level: only inputs with the same sync level are sync
213 * Flag indicating that a frame event is ready
218 * Flag indicating that output has reached EOF.
223 * Pointer to array of inputs.
235 * Pre-initialize a frame sync structure.
237 * It sets the class pointer and inits the options to their default values.
238 * The entire structure is expected to be already set to 0.
239 * This step is optional, but necessary to use the options.
244 * Initialize a frame sync structure.
246 * The entire structure is expected to be already set to 0 or preinited.
248 * @param fs frame sync structure to initialize
249 * @param parent parent AVFilterContext object
250 * @param nb_in number of inputs
251 * @return >= 0 for success or a negative error code
256 * Configure a frame sync structure.
258 * Must be called after all options are set but before all use.
260 * @return >= 0 for success or a negative error code
265 * Free all memory currently allocated.
270 * Get the current frame in an input.
272 * @param fs frame sync structure
273 * @param in index of the input
274 * @param rframe used to return the current frame (or NULL)
275 * @param get if not zero, the calling code needs to get ownership of
276 * the returned frame; the current frame will either be
277 * duplicated or removed from the framesync structure
283 * Examine the frames in the filter's input and try to produce output.
285 * This function can be the complete implementation of the activate
286 * method of a filter using framesync.
291 * Initialize a frame sync structure for dualinput.
293 * Compared to generic framesync, dualinput assumes the first input is the
294 * main one and the filtering is performed on it. The first input will be
295 * the only one with sync set and generic timeline support will just pass it
296 * unchanged when disabled.
298 * Equivalent to ff_framesync_init(fs, parent, 2) then setting the time
299 * base, sync and ext modes on the inputs.
304 * @param f0 used to return the main frame
305 * @param f1 used to return the second frame, or NULL if disabled
306 * @return >=0 for success or AVERROR code
307 * @note The frame returned in f0 belongs to the caller (get = 1 in
308 * ff_framesync_get_frame()) while the frame returned in f1 is still owned
309 * by the framesync structure.
314 * Same as ff_framesync_dualinput_get(), but make sure that f0 is writable.
321 #define FRAMESYNC_DEFINE_PURE_CLASS(name, desc, func_prefix, options) \
322static const AVClass name##_class = { \
323 .class_name = desc, \
324 .item_name = av_default_item_name, \
326 .version = LIBAVUTIL_VERSION_INT, \
327 .category = AV_CLASS_CATEGORY_FILTER, \
328 .child_class_iterate = ff_framesync_child_class_iterate, \
329 .child_next = func_prefix##_child_next, \
332/* A filter that uses the *_child_next-function from this macro
333 * is required to initialize the FFFrameSync structure in AVFilter.preinit
334 * via the *_framesync_preinit function defined alongside it. */
335 #define FRAMESYNC_AUXILIARY_FUNCS(func_prefix, context, field) \
336static int func_prefix##_framesync_preinit(AVFilterContext *ctx) \
338 context *s = ctx->priv; \
339 ff_framesync_preinit(&s->field); \
342static void *func_prefix##_child_next(void *obj, void *prev) \
345 return prev ? NULL : &s->field; \
348 #define FRAMESYNC_DEFINE_CLASS_EXT(name, context, field, options) \
349FRAMESYNC_AUXILIARY_FUNCS(name, context, field) \
350FRAMESYNC_DEFINE_PURE_CLASS(name, #name, name, options)
352 #define FRAMESYNC_DEFINE_CLASS(name, context, field) \
353FRAMESYNC_DEFINE_CLASS_EXT(name, context, field, name##_options)
355#endif /* AVFILTER_FRAMESYNC_H */
#define fs(width, name, subs,...)
const AVClass ff_framesync_class
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
int ff_framesync_dualinput_get(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
int ff_framesync_get_frame(FFFrameSync *fs, unsigned in, AVFrame **rframe, unsigned get)
Get the current frame in an input.
FFFrameTSSyncMode
Timestamp synchronization mode.
int ff_framesync_init_dualinput(FFFrameSync *fs, AVFilterContext *parent)
Initialize a frame sync structure for dualinput.
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
void ff_framesync_preinit(FFFrameSync *fs)
Pre-initialize a frame sync structure.
int ff_framesync_dualinput_get_writable(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
Same as ff_framesync_dualinput_get(), but make sure that f0 is writable.
int ff_framesync_init(FFFrameSync *fs, AVFilterContext *parent, unsigned nb_in)
Initialize a frame sync structure.
const AVClass * ff_framesync_child_class_iterate(void **iter)
FFFrameSyncExtMode
This API is intended as a helper for filters that have several video input and need to combine them s...
@ EXT_INFINITY
Extend the frame to infinity.
@ TS_DEFAULT
Sync to packets from secondary input with the nearest, lower or equal timestamp to the packet event o...
@ TS_NEAREST
Sync to packets from secondary input with the absolute nearest timestamp to the packet event one.
@ EXT_STOP
Completely stop all streams with this one.
@ EXT_NULL
Ignore this stream and continue processing the other ones.
static void get(const uint8_t *pixels, int stride, int16_t *block)
Describe the class of an AVClass context structure.
This structure describes decoded (raw) audio or video data.
Rational number (pair of numerator and denominator).
int64_t pts_next
PTS of the next frame, for internal use.
enum FFFrameSyncExtMode after
Extrapolation mode for timestamps after the last frame.
uint8_t have_next
Boolean flagging the next frame, for internal use.
enum FFFrameTSSyncMode ts_mode
int64_t pts
PTS of the current frame.
uint8_t state
State: before first, in stream or after EOF, for internal use.
AVFrame * frame
Current frame, may be NULL before the first one or after EOF.
enum FFFrameSyncExtMode before
Extrapolation mode for timestamps before the first frame.
AVFrame * frame_next
Next frame, for internal use.
AVRational time_base
Time base for the incoming frames.
unsigned sync
Synchronization level: frames on input at the highest sync level will generate output frame events.
unsigned in_request
Index of the input that requires a request.
FFFrameSyncIn * in
Pointer to array of inputs.
uint8_t eof
Flag indicating that output has reached EOF.
unsigned nb_in
Number of input streams.
AVRational time_base
Time base for the output events.
uint8_t frame_ready
Flag indicating that a frame event is ready.
AVFilterContext * parent
Parent filter context.
int(* on_event)(struct FFFrameSync *fs)
Callback called when a frame event is ready.
unsigned sync_level
Synchronization level: only inputs with the same sync level are sync sources.
void * opaque
Opaque pointer, not used by the API.
int64_t pts
Timestamp of the current event.