FFmpeg
Data Structures | Macros | Functions
fifo.c File Reference
#include <stdint.h>
#include <string.h>
#include "avassert.h"
#include "error.h"
#include "fifo.h"
#include "macros.h"
#include "mem.h"

Go to the source code of this file.

Data Structures

struct  AVFifo
 

Macros

#define AUTO_GROW_DEFAULT_BYTES   (1024 * 1024)
 
#define OLD_FIFO_SIZE_MAX   (size_t)FFMIN3(INT_MAX, UINT32_MAX, SIZE_MAX)
 

Functions

AVFifoav_fifo_alloc2 (size_t nb_elems, size_t elem_size, unsigned int flags)
 Allocate and initialize an AVFifo with a given element size. More...
 
void av_fifo_auto_grow_limit (AVFifo *f, size_t max_elems)
 Set the maximum size (in elements) to which the FIFO can be resized automatically. More...
 
size_t av_fifo_elem_size (const AVFifo *f)
 
size_t av_fifo_can_read (const AVFifo *f)
 
size_t av_fifo_can_write (const AVFifo *f)
 
int av_fifo_grow2 (AVFifo *f, size_t inc)
 Enlarge an AVFifo. More...
 
static int fifo_check_space (AVFifo *f, size_t to_write)
 
static int fifo_write_common (AVFifo *f, const uint8_t *buf, size_t *nb_elems, AVFifoCB read_cb, void *opaque)
 
int av_fifo_write (AVFifo *f, const void *buf, size_t nb_elems)
 Write data into a FIFO. More...
 
int av_fifo_write_from_cb (AVFifo *f, AVFifoCB read_cb, void *opaque, size_t *nb_elems)
 Write data from a user-provided callback into a FIFO. More...
 
static int fifo_peek_common (const AVFifo *f, uint8_t *buf, size_t *nb_elems, size_t offset, AVFifoCB write_cb, void *opaque)
 
int av_fifo_read (AVFifo *f, void *buf, size_t nb_elems)
 Read data from a FIFO. More...
 
int av_fifo_read_to_cb (AVFifo *f, AVFifoCB write_cb, void *opaque, size_t *nb_elems)
 Feed data from a FIFO into a user-provided callback. More...
 
int av_fifo_peek (AVFifo *f, void *buf, size_t nb_elems, size_t offset)
 Read data from a FIFO without modifying FIFO state. More...
 
int av_fifo_peek_to_cb (AVFifo *f, AVFifoCB write_cb, void *opaque, size_t *nb_elems, size_t offset)
 Feed data from a FIFO into a user-provided callback. More...
 
void av_fifo_drain2 (AVFifo *f, size_t size)
 Discard the specified amount of data from an AVFifo. More...
 
void av_fifo_reset2 (AVFifo *f)
 
void av_fifo_freep2 (AVFifo **f)
 Free an AVFifo and reset pointer to NULL. More...
 
AVFifoBufferav_fifo_alloc_array (size_t nmemb, size_t size)
 Initialize an AVFifoBuffer. More...
 
AVFifoBufferav_fifo_alloc (unsigned int size)
 Initialize an AVFifoBuffer. More...
 
void av_fifo_free (AVFifoBuffer *f)
 Free an AVFifoBuffer. More...
 
void av_fifo_freep (AVFifoBuffer **f)
 Free an AVFifoBuffer and reset pointer to NULL. More...
 
void av_fifo_reset (AVFifoBuffer *f)
 Reset the AVFifoBuffer to the state right after av_fifo_alloc, in particular it is emptied. More...
 
int av_fifo_size (const AVFifoBuffer *f)
 Return the amount of data in bytes in the AVFifoBuffer, that is the amount of data you can read from it. More...
 
int av_fifo_space (const AVFifoBuffer *f)
 Return the amount of space in bytes in the AVFifoBuffer, that is the amount of data you can write into it. More...
 
int av_fifo_realloc2 (AVFifoBuffer *f, unsigned int new_size)
 Resize an AVFifoBuffer. More...
 
int av_fifo_grow (AVFifoBuffer *f, unsigned int size)
 Enlarge an AVFifoBuffer. More...
 
int av_fifo_generic_write (AVFifoBuffer *f, void *src, int size, int(*func)(void *, void *, int))
 Feed data from a user-supplied callback to an AVFifoBuffer. More...
 
int av_fifo_generic_peek_at (AVFifoBuffer *f, void *dest, int offset, int buf_size, void(*func)(void *, void *, int))
 Feed data at specific position from an AVFifoBuffer to a user-supplied callback. More...
 
int av_fifo_generic_peek (AVFifoBuffer *f, void *dest, int buf_size, void(*func)(void *, void *, int))
 Feed data from an AVFifoBuffer to a user-supplied callback. More...
 
int av_fifo_generic_read (AVFifoBuffer *f, void *dest, int buf_size, void(*func)(void *, void *, int))
 Feed data from an AVFifoBuffer to a user-supplied callback. More...
 
void av_fifo_drain (AVFifoBuffer *f, int size)
 Discard data from the FIFO. More...
 

Macro Definition Documentation

◆ AUTO_GROW_DEFAULT_BYTES

#define AUTO_GROW_DEFAULT_BYTES   (1024 * 1024)

Definition at line 33 of file fifo.c.

◆ OLD_FIFO_SIZE_MAX

#define OLD_FIFO_SIZE_MAX   (size_t)FFMIN3(INT_MAX, UINT32_MAX, SIZE_MAX)

Definition at line 297 of file fifo.c.

Function Documentation

◆ av_fifo_alloc2()

AVFifo* av_fifo_alloc2 ( size_t  elems,
size_t  elem_size,
unsigned int  flags 
)

Allocate and initialize an AVFifo with a given element size.

Parameters
elemsinitial number of elements that can be stored in the FIFO
elem_sizeSize in bytes of a single element. Further operations on the returned FIFO will implicitly use this element size.
flagsa combination of AV_FIFO_FLAG_*
Returns
newly-allocated AVFifo on success, a negative error code on failure

Definition at line 47 of file fifo.c.

Referenced by amf_load_library(), av_audio_fifo_alloc(), av_thread_message_queue_alloc(), config_input(), cuvid_decode_init(), deshake_opencl_init(), dv_init_mux(), ff_decode_preinit(), ff_qsv_enc_init(), ff_qsvvpp_create(), ff_vaapi_encode_init(), init(), init_input_filter(), init_simple_filtergraph(), libvorbis_encode_init(), main(), mpeg_mux_init(), nvenc_setup_surfaces(), of_muxer_init(), packet_queue_init(), qsv_decode_init(), qsv_decode_preinit(), ring_init(), sq_add_stream(), start_jack(), swf_write_header(), tq_alloc(), transcode_subtitles(), udp_open(), and vpx_init().

◆ av_fifo_auto_grow_limit()

void av_fifo_auto_grow_limit ( AVFifo f,
size_t  max_elems 
)

Set the maximum size (in elements) to which the FIFO can be resized automatically.

Has no effect unless AV_FIFO_FLAG_AUTO_GROW is used.

Definition at line 77 of file fifo.c.

◆ av_fifo_elem_size()

size_t av_fifo_elem_size ( const AVFifo f)
Returns
Element size for FIFO operations. This element size is set at FIFO allocation and remains constant during its lifetime

Definition at line 82 of file fifo.c.

Referenced by main().

◆ av_fifo_can_read()

size_t av_fifo_can_read ( const AVFifo f)

◆ av_fifo_can_write()

size_t av_fifo_can_write ( const AVFifo f)

◆ av_fifo_grow2()

int av_fifo_grow2 ( AVFifo f,
size_t  inc 
)

Enlarge an AVFifo.

On success, the FIFO will be large enough to hold exactly inc + av_fifo_can_read() + av_fifo_can_write() elements. In case of failure, the old FIFO is kept unchanged.

Parameters
fAVFifo to resize
incnumber of elements to allocate for, in addition to the current allocated size
Returns
a non-negative number on success, a negative error code on failure

Definition at line 99 of file fifo.c.

Referenced by av_audio_fifo_realloc(), fifo_check_space(), main(), mpeg_mux_write_packet(), and queue_packet().

◆ fifo_check_space()

static int fifo_check_space ( AVFifo f,
size_t  to_write 
)
static

Definition at line 129 of file fifo.c.

Referenced by fifo_write_common().

◆ fifo_write_common()

static int fifo_write_common ( AVFifo f,
const uint8_t *  buf,
size_t *  nb_elems,
AVFifoCB  read_cb,
void *  opaque 
)
static

Definition at line 149 of file fifo.c.

Referenced by av_fifo_write(), and av_fifo_write_from_cb().

◆ av_fifo_write()

int av_fifo_write ( AVFifo f,
const void *  buf,
size_t  nb_elems 
)

Write data into a FIFO.

In case nb_elems > av_fifo_can_write(f), nothing is written and an error is returned.

Parameters
fthe FIFO buffer
bufData to be written. nb_elems * av_fifo_elem_size(f) bytes will be read from buf on success.
nb_elemsnumber of elements to write into FIFO
Returns
a non-negative number on success, a negative error code on failure

Definition at line 188 of file fifo.c.

Referenced by activate(), av_audio_fifo_write(), cuvid_handle_picture_display(), dv_assemble_frame(), encode_frame(), extract_packet_props(), ff_amf_receive_packet(), ff_nvenc_receive_packet(), ff_qsvvpp_filter_frame(), ff_vaapi_encode_receive_packet(), filter_frame(), ifilter_send_frame(), libvorbis_encode_frame(), main(), mpeg_mux_write_packet(), nvenc_alloc_surface(), nvenc_send_frame(), packet_queue_put_private(), process_callback(), qsv_decode(), qsv_decode_frame(), queue_packet(), sq_send(), supply_new_packets(), swf_write_audio(), timestamp_queue_enqueue(), tq_send(), transcode_subtitles(), udp_write(), and vpx_encode().

◆ av_fifo_write_from_cb()

int av_fifo_write_from_cb ( AVFifo f,
AVFifoCB  read_cb,
void *  opaque,
size_t *  nb_elems 
)

Write data from a user-provided callback into a FIFO.

Parameters
fthe FIFO buffer
read_cbCallback supplying the data to the FIFO. May be called multiple times.
opaqueopaque user data to be provided to read_cb
nb_elemsShould point to the maximum number of elements that can be written. Will be updated to contain the number of elements actually written.
Returns
non-negative number on success, a negative error code on failure

Definition at line 193 of file fifo.c.

Referenced by main(), and ring_write().

◆ fifo_peek_common()

static int fifo_peek_common ( const AVFifo f,
uint8_t *  buf,
size_t *  nb_elems,
size_t  offset,
AVFifoCB  write_cb,
void *  opaque 
)
static

Definition at line 199 of file fifo.c.

Referenced by av_fifo_peek(), av_fifo_peek_to_cb(), av_fifo_read(), and av_fifo_read_to_cb().

◆ av_fifo_read()

int av_fifo_read ( AVFifo f,
void *  buf,
size_t  nb_elems 
)

Read data from a FIFO.

In case nb_elems > av_fifo_can_read(f), nothing is read and an error is returned.

Parameters
fthe FIFO buffer
bufBuffer to store the data. nb_elems * av_fifo_elem_size(f) bytes will be written into buf on success.
nb_elemsnumber of elements to read from FIFO
Returns
a non-negative number on success, a negative error code on failure

Definition at line 240 of file fifo.c.

Referenced by activate(), amf_copy_buffer(), audio_read_packet(), av_audio_fifo_read(), avcodec_close(), avcodec_flush_buffers(), configure_filtergraph(), cuvid_output_frame(), decode_receive_frame_internal(), extract_packet_props(), ff_nvenc_receive_packet(), ff_qsv_enc_close(), ff_qsv_encode(), ff_qsvvpp_filter_frame(), ff_vaapi_encode_receive_packet(), ffmpeg_cleanup(), filter_frame(), free_debug_matches(), free_hdr10_plus_fifo(), free_pkt_fifo(), get_free_frame(), libvorbis_encode_frame(), main(), mux_free(), nvenc_send_frame(), packet_queue_flush(), packet_queue_get(), process_callback(), qsv_clear_buffers(), qsv_decode(), qsv_decode_close_qsvcontext(), qsv_decode_frame(), receive_locked(), sq_free(), thread_start(), timestamp_queue_dequeue(), tq_free(), udp_read(), and uninit().

◆ av_fifo_read_to_cb()

int av_fifo_read_to_cb ( AVFifo f,
AVFifoCB  write_cb,
void *  opaque,
size_t *  nb_elems 
)

Feed data from a FIFO into a user-provided callback.

Parameters
fthe FIFO buffer
write_cbCallback the data will be supplied to. May be called multiple times.
opaqueopaque user data to be provided to write_cb
nb_elemsShould point to the maximum number of elements that can be read. Will be updated to contain the total number of elements actually sent to the callback.
Returns
non-negative number on success, a negative error code on failure

Definition at line 247 of file fifo.c.

Referenced by av_thread_message_flush(), flush_packet(), main(), and swf_write_video().

◆ av_fifo_peek()

int av_fifo_peek ( AVFifo f,
void *  buf,
size_t  nb_elems,
size_t  offset 
)

Read data from a FIFO without modifying FIFO state.

Returns an error if an attempt is made to peek to nonexistent elements (i.e. if offset + nb_elems is larger than av_fifo_can_read(f)).

Parameters
fthe FIFO buffer
bufBuffer to store the data. nb_elems * av_fifo_elem_size(f) bytes will be written into buf.
nb_elemsnumber of elements to read from FIFO
offsetnumber of initial elements to skip.
Returns
a non-negative number on success, a negative error code on failure

Definition at line 255 of file fifo.c.

Referenced by amf_copy_buffer(), av_audio_fifo_peek_at(), copy_hdr10_plus_to_pkt(), dv_inject_audio(), filter_frame(), main(), overflow_heartbeat(), receive_for_stream(), ring_read(), and ringbuf_float_at().

◆ av_fifo_peek_to_cb()

int av_fifo_peek_to_cb ( AVFifo f,
AVFifoCB  write_cb,
void *  opaque,
size_t *  nb_elems,
size_t  offset 
)

Feed data from a FIFO into a user-provided callback.

Parameters
fthe FIFO buffer
write_cbCallback the data will be supplied to. May be called multiple times.
opaqueopaque user data to be provided to write_cb
nb_elemsShould point to the maximum number of elements that can be read. Will be updated to contain the total number of elements actually sent to the callback.
offsetnumber of initial elements to skip; offset + *nb_elems must not be larger than av_fifo_can_read(f).
Returns
a non-negative number on success, a negative error code on failure

Definition at line 260 of file fifo.c.

Referenced by main().

◆ av_fifo_drain2()

void av_fifo_drain2 ( AVFifo f,
size_t  size 
)

Discard the specified amount of data from an AVFifo.

Parameters
sizenumber of elements to discard, MUST NOT be larger than av_fifo_can_read(f)

Definition at line 266 of file fifo.c.

Referenced by activate(), av_audio_fifo_drain(), av_fifo_read(), av_fifo_read_to_cb(), copy_hdr10_plus_to_pkt(), dv_assemble_frame(), receive_for_stream(), ring_read(), and udp_read().

◆ av_fifo_reset2()

void av_fifo_reset2 ( AVFifo f)

Definition at line 280 of file fifo.c.

Referenced by av_audio_fifo_reset(), ff_nvenc_encode_flush(), and ring_reset().

◆ av_fifo_freep2()

void av_fifo_freep2 ( AVFifo **  f)

◆ av_fifo_alloc_array()

AVFifoBuffer* av_fifo_alloc_array ( size_t  nmemb,
size_t  size 
)

Initialize an AVFifoBuffer.

Parameters
nmembnumber of elements
sizesize of the single element
Returns
AVFifoBuffer or NULL in case of memory allocation failure
Deprecated:
use av_fifo_alloc2()

Definition at line 299 of file fifo.c.

Referenced by av_fifo_alloc().

◆ av_fifo_alloc()

AVFifoBuffer* av_fifo_alloc ( unsigned int  size)

Initialize an AVFifoBuffer.

Parameters
sizeof FIFO
Returns
AVFifoBuffer or NULL in case of memory allocation failure
Deprecated:
use av_fifo_alloc2()

Definition at line 321 of file fifo.c.

◆ av_fifo_free()

void av_fifo_free ( AVFifoBuffer f)

Free an AVFifoBuffer.

Parameters
fAVFifoBuffer to free
Deprecated:
use the AVFifo API with av_fifo_freep2()

Definition at line 326 of file fifo.c.

Referenced by av_fifo_freep().

◆ av_fifo_freep()

void av_fifo_freep ( AVFifoBuffer **  f)

Free an AVFifoBuffer and reset pointer to NULL.

Parameters
fAVFifoBuffer to free
Deprecated:
use the AVFifo API with av_fifo_freep2()

Definition at line 334 of file fifo.c.

◆ av_fifo_reset()

void av_fifo_reset ( AVFifoBuffer f)

Reset the AVFifoBuffer to the state right after av_fifo_alloc, in particular it is emptied.

Parameters
fAVFifoBuffer to reset
Deprecated:
use av_fifo_reset2() with the new AVFifo-API

Definition at line 342 of file fifo.c.

Referenced by av_fifo_alloc_array().

◆ av_fifo_size()

int av_fifo_size ( const AVFifoBuffer f)

Return the amount of data in bytes in the AVFifoBuffer, that is the amount of data you can read from it.

Parameters
fAVFifoBuffer to read from
Returns
size
Deprecated:
use av_fifo_can_read() with the new AVFifo-API

Definition at line 348 of file fifo.c.

Referenced by av_fifo_drain(), av_fifo_generic_peek_at(), av_fifo_generic_read(), av_fifo_grow(), av_fifo_realloc2(), and av_fifo_space().

◆ av_fifo_space()

int av_fifo_space ( const AVFifoBuffer f)

Return the amount of space in bytes in the AVFifoBuffer, that is the amount of data you can write into it.

Parameters
fAVFifoBuffer to write into
Returns
size
Deprecated:
use av_fifo_can_write() with the new AVFifo-API

Definition at line 353 of file fifo.c.

Referenced by av_fifo_generic_write().

◆ av_fifo_realloc2()

int av_fifo_realloc2 ( AVFifoBuffer f,
unsigned int  size 
)

Resize an AVFifoBuffer.

In case of reallocation failure, the old FIFO is kept unchanged.

Parameters
fAVFifoBuffer to resize
sizenew AVFifoBuffer size in bytes
Returns
<0 for failure, >=0 otherwise
Deprecated:
use the new AVFifo-API with av_fifo_grow2() to increase FIFO size, decreasing FIFO size is not supported

Definition at line 358 of file fifo.c.

Referenced by av_fifo_grow().

◆ av_fifo_grow()

int av_fifo_grow ( AVFifoBuffer f,
unsigned int  additional_space 
)

Enlarge an AVFifoBuffer.

In case of reallocation failure, the old FIFO is kept unchanged. The new fifo size may be larger than the requested size.

Parameters
fAVFifoBuffer to resize
additional_spacethe amount of space in bytes to allocate in addition to av_fifo_size()
Returns
<0 for failure, >=0 otherwise
Deprecated:
use the new AVFifo-API with av_fifo_grow2(); note that unlike this function it adds to the allocated size, rather than to the used size

Definition at line 395 of file fifo.c.

◆ av_fifo_generic_write()

int av_fifo_generic_write ( AVFifoBuffer f,
void *  src,
int  size,
int(*)(void *, void *, int func 
)

Feed data from a user-supplied callback to an AVFifoBuffer.

Parameters
fAVFifoBuffer to write to
srcdata source; non-const since it may be used as a modifiable context by the function defined in func
sizenumber of bytes to write
funcgeneric write function; the first parameter is src, the second is dest_buf, the third is dest_buf_size. func must return the number of bytes written to dest_buf, or <= 0 to indicate no more data available to write. If func is NULL, src is interpreted as a simple byte array for source data.
Returns
the number of bytes written to the FIFO or a negative error code on failure
Deprecated:
use the new AVFifo-API with av_fifo_write() when func == NULL, av_fifo_write_from_cb() otherwise

Definition at line 410 of file fifo.c.

◆ av_fifo_generic_peek_at()

int av_fifo_generic_peek_at ( AVFifoBuffer f,
void *  dest,
int  offset,
int  buf_size,
void(*)(void *, void *, int func 
)

Feed data at specific position from an AVFifoBuffer to a user-supplied callback.

Similar as av_fifo_gereric_read but without discarding data.

Parameters
fAVFifoBuffer to read from
offsetoffset from current read position
buf_sizenumber of bytes to read
funcgeneric read function
destdata destination
Returns
a non-negative number on success, a negative error code on failure
Deprecated:
use the new AVFifo-API with av_fifo_peek() when func == NULL, av_fifo_peek_to_cb() otherwise

Definition at line 441 of file fifo.c.

Referenced by av_fifo_generic_peek().

◆ av_fifo_generic_peek()

int av_fifo_generic_peek ( AVFifoBuffer f,
void *  dest,
int  buf_size,
void(*)(void *, void *, int func 
)

Feed data from an AVFifoBuffer to a user-supplied callback.

Similar as av_fifo_gereric_read but without discarding data.

Parameters
fAVFifoBuffer to read from
buf_sizenumber of bytes to read
funcgeneric read function
destdata destination
Returns
a non-negative number on success, a negative error code on failure
Deprecated:
use the new AVFifo-API with av_fifo_peek() when func == NULL, av_fifo_peek_to_cb() otherwise

Definition at line 474 of file fifo.c.

◆ av_fifo_generic_read()

int av_fifo_generic_read ( AVFifoBuffer f,
void *  dest,
int  buf_size,
void(*)(void *, void *, int func 
)

Feed data from an AVFifoBuffer to a user-supplied callback.

Parameters
fAVFifoBuffer to read from
buf_sizenumber of bytes to read
funcgeneric read function
destdata destination
Returns
a non-negative number on success, a negative error code on failure
Deprecated:
use the new AVFifo-API with av_fifo_read() when func == NULL, av_fifo_read_to_cb() otherwise

Definition at line 480 of file fifo.c.

◆ av_fifo_drain()

void av_fifo_drain ( AVFifoBuffer f,
int  size 
)

Discard data from the FIFO.

Read and discard the specified amount of data from an AVFifoBuffer.

Definition at line 501 of file fifo.c.

Referenced by av_fifo_generic_read().