2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19#ifndef AVCODEC_BSF_PACKETSYNC_H
20#define AVCODEC_BSF_PACKETSYNC_H
35 * This API is intended as a helper for filters that have several video
36 * input and need to combine them somehow. If the inputs have different or
37 * variable frame rate, getting the input packets to match requires a rather
38 * complex logic and a few user-tunable options.
40 * In this API, when a set of synchronized input packets is ready to be
41 * processed is called a packet event. packet event can be generated in
42 * response to input packets on any or all inputs and the handling of
43 * situations where some stream extend beyond the beginning or the end of
44 * others can be configured.
46 * The basic working of this API is the following: set the on_event
47 * callback, then call ff_packetsync_activate() from the filter's activate
52 * Stream extrapolation mode
54 * Describe how the packets of a stream are extrapolated before the first one
55 * and after EOF to keep sync with possibly longer other streams.
60 * Completely stop all streams with this one.
65 * Ignore this stream and continue processing the other ones.
71 * Timestamp synchronization mode
73 * Describe how the packets of a stream are synchronized based on timestamp
79 * Sync to packets from secondary input with the nearest, lower or equal
80 * timestamp to the packet event one.
85 * Sync to packets from secondary input with the absolute nearest timestamp
86 * to the packet event one.
92 * Input stream structure
97 * Extrapolation mode for timestamps before the first packet
102 * Extrapolation mode for timestamps after the last packet
107 * Time base for the incoming packets
112 * Current packet, may be NULL before the first one or after EOF
117 * Next packet, for internal use
122 * PTS of the current packet
127 * PTS of the next packet, for internal use
132 * Boolean flagging the next packet, for internal use
137 * State: before first, in stream or after EOF, for internal use
142 * Synchronization level: packets on input at the highest sync level will
143 * generate output packet events.
145 * For example, if inputs #0 and #1 have sync level 2 and input #2 has
146 * sync level 1, then a packet on either input #0 or #1 will generate a
147 * packet event, but not a packet on input #2 until both inputs #0 and #1
150 * If sync is 0, no packet event will be generated.
158 * Packet sync structure.
164 * Parent filter context.
169 * Number of input streams
174 * Time base for the output events
179 * Timestamp of the current event
184 * Callback called when a packet event is ready
189 * Opaque pointer, not used by the API
194 * Index of the input that requires a request
199 * Synchronization level: only inputs with the same sync level are sync
205 * Flag indicating that a packet event is ready
210 * Flag indicating that output has reached EOF.
215 * Pointer to array of inputs.
225 * Pre-initialize a packet sync structure.
227 * It sets the class pointer and inits the options to their default values.
228 * The entire structure is expected to be already set to 0.
229 * This step is optional, but necessary to use the options.
234 * Initialize a packet sync structure.
236 * The entire structure is expected to be already set to 0 or preinited.
238 * @param fs packet sync structure to initialize
239 * @param parent parent AVBitStreamFilterContext object
240 * @param nb_in number of inputs
241 * @return >= 0 for success or a negative error code
246 * Configure a packet sync structure.
248 * Must be called after all options are set but before all use.
250 * @return >= 0 for success or a negative error code
255 * Free all memory currently allocated.
260 * Get the current packet in an input.
262 * @param fs packet sync structure
263 * @param in index of the input
264 * @param rpacket used to return the current packet (or NULL)
265 * @param get if not zero, the calling code needs to get ownership of
266 * the returned packet; the current packet will either be
267 * duplicated or removed from the packetsync structure
273 * Examine the packets in the filter's input and try to produce output.
275 * This function can be the complete implementation of the activate
276 * method of a filter using packetsync.
281 * Initialize a packet sync structure for dualinput.
283 * Compared to generic packetsync, dualinput assumes the first input is the
284 * main one and the filtering is performed on it. The first input will be
285 * the only one with sync set and generic timeline support will just pass it
286 * unchanged when disabled.
288 * Equivalent to ff_packetsync_init(fs, parent, 2) then setting the time
289 * base, sync and ext modes on the inputs.
294 * @param f0 used to return the main packet
295 * @param f1 used to return the second packet, or NULL if disabled
296 * @return >=0 for success or AVERROR code
297 * @note The packet returned in f0 belongs to the caller (get = 1 in
298 * ff_packetsync_get_packet()) while the packet returned in f1 is still owned
299 * by the packetsync structure.
304 * Same as ff_packetsync_dualinput_get(), but make sure that f0 is writable.
311 #define PACKETSYNC_DEFINE_PURE_CLASS(name, desc, func_prefix, options) \
312static const AVClass name##_class = { \
313 .class_name = desc, \
314 .item_name = av_default_item_name, \
316 .version = LIBAVUTIL_VERSION_INT, \
317 .category = AV_CLASS_CATEGORY_BITSTREAM_FILTER, \
318 .child_class_iterate = ff_packetsync_child_class_iterate, \
319 .child_next = func_prefix##_child_next, \
322/* A filter that uses the *_child_next-function from this macro
323 * is required to initialize the FFPacketSync structure in AVBitStreamFilter.preinit
324 * via the *_packetsync_preinit function defined alongside it. */
325 #define PACKETSYNC_AUXILIARY_FUNCS(func_prefix, context, field) \
326static int func_prefix##_packetsync_preinit(AVBitStreamFilterContext *ctx) \
328 context *s = ctx->priv_data; \
329 ff_packetsync_preinit(&s->field); \
332static void *func_prefix##_child_next(void *obj, void *prev) \
335 return prev ? NULL : &s->field; \
338 #define PACKETSYNC_DEFINE_CLASS_EXT(name, context, field, options) \
339PACKETSYNC_AUXILIARY_FUNCS(name, context, field) \
340PACKETSYNC_DEFINE_PURE_CLASS(name, #name, name, options)
342 #define PACKETSYNC_DEFINE_CLASS(name, context, field) \
343PACKETSYNC_DEFINE_CLASS_EXT(name, context, field, name##_options)
345#endif /* AVCODEC_BSF_PACKETSYNC_H */
#define fs(width, name, subs,...)
const AVClass ff_packetsync_class
int ff_packetsync_get_packet(FFPacketSync *fs, unsigned in, AVPacket **rpacket, unsigned get)
Get the current packet in an input.
void ff_packetsync_uninit(FFPacketSync *fs)
Free all memory currently allocated.
int ff_packetsync_init_dualinput(FFPacketSync *fs, AVBitStreamFilterContext *parent)
Initialize a packet sync structure for dualinput.
int ff_packetsync_configure(FFPacketSync *fs)
Configure a packet sync structure.
const AVClass * ff_packetsync_child_class_iterate(void **iter)
int ff_packetsync_activate(FFPacketSync *fs)
Examine the packets in the filter's input and try to produce output.
FFPacketTSSyncMode
Timestamp synchronization mode.
@ 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.
int ff_packetsync_init(FFPacketSync *fs, AVBitStreamFilterContext *parent, unsigned nb_in)
Initialize a packet sync structure.
int ff_packetsync_dualinput_get(FFPacketSync *fs, AVPacket **f0, AVPacket **f1)
FFPacketSyncExtMode
This API is intended as a helper for filters that have several video input and need to combine them s...
@ EXT_STOP
Completely stop all streams with this one.
@ EXT_NULL
Ignore this stream and continue processing the other ones.
int ff_packetsync_dualinput_get_writable(FFPacketSync *fs, AVPacket **f0, AVPacket **f1)
Same as ff_packetsync_dualinput_get(), but make sure that f0 is writable.
void ff_packetsync_preinit(FFPacketSync *fs)
Pre-initialize a packet sync structure.
static void get(const uint8_t *pixels, int stride, int16_t *block)
Describe the class of an AVClass context structure.
This structure stores compressed data.
Rational number (pair of numerator and denominator).
enum FFPacketSyncExtMode after
Extrapolation mode for timestamps after the last packet.
unsigned sync
Synchronization level: packets on input at the highest sync level will generate output packet events.
enum FFPacketTSSyncMode ts_mode
uint8_t have_next
Boolean flagging the next packet, for internal use.
uint8_t state
State: before first, in stream or after EOF, for internal use.
int64_t pts_next
PTS of the next packet, for internal use.
AVPacket * pkt
Current packet, may be NULL before the first one or after EOF.
AVPacket * pkt_next
Next packet, for internal use.
enum FFPacketSyncExtMode before
Extrapolation mode for timestamps before the first packet.
AVRational time_base
Time base for the incoming packets.
int64_t pts
PTS of the current packet.
int64_t pts
Timestamp of the current event.
unsigned in_request
Index of the input that requires a request.
AVBitStreamFilterContext * parent
Parent filter context.
void * opaque
Opaque pointer, not used by the API.
FFPacketSyncIn * in
Pointer to array of inputs.
unsigned nb_in
Number of input streams.
uint8_t pkt_ready
Flag indicating that a packet event is ready.
uint8_t eof
Flag indicating that output has reached EOF.
AVRational time_base
Time base for the output events.
int(* on_event)(struct FFPacketSync *fs)
Callback called when a packet event is ready.
unsigned sync_level
Synchronization level: only inputs with the same sync level are sync sources.