FFmpeg
Functions

Utilities to make an array grow when needed. More...

Functions

void av_dynarray_add (void *tab_ptr, int *nb_ptr, void *elem)
 Add the pointer to an element to a dynamic array. More...
 
av_warn_unused_result int av_dynarray_add_nofree (void *tab_ptr, int *nb_ptr, void *elem)
 Add an element to a dynamic array. More...
 
void * av_dynarray2_add (void **tab_ptr, int *nb_ptr, size_t elem_size, const uint8_t *elem_data)
 Add an element of size elem_size to a dynamic array. More...
 

Detailed Description

Utilities to make an array grow when needed.

Sometimes, the programmer would want to have an array that can grow when needed. The libavutil dynamic array utilities fill that need.

libavutil supports two systems of appending elements onto a dynamically allocated array, the first one storing the pointer to the value in the array, and the second storing the value directly. In both systems, the caller is responsible for maintaining a variable containing the length of the array, as well as freeing of the array after use.

The first system stores pointers to values in a block of dynamically allocated memory. Since only pointers are stored, the function does not need to know the size of the type. Both av_dynarray_add() and av_dynarray_add_nofree() implement this system.

type **array = NULL; //< an array of pointers to values
int nb = 0; //< a variable to keep track of the length of the array
type to_be_added = ...;
type to_be_added2 = ...;
av_dynarray_add(&array, &nb, &to_be_added);
if (nb == 0)
return AVERROR(ENOMEM);
av_dynarray_add(&array, &nb, &to_be_added2);
if (nb == 0)
return AVERROR(ENOMEM);
// Now:
// nb == 2
// &to_be_added == array[0]
// &to_be_added2 == array[1]

The second system stores the value directly in a block of memory. As a result, the function has to know the size of the type. av_dynarray2_add() implements this mechanism.

type *array = NULL; //< an array of values
int nb = 0; //< a variable to keep track of the length of the array
type to_be_added = ...;
type to_be_added2 = ...;
type *addr = av_dynarray2_add((void **)&array, &nb, sizeof(*array), NULL);
if (!addr)
return AVERROR(ENOMEM);
memcpy(addr, &to_be_added, sizeof(to_be_added));
// Shortcut of the above.
type *addr = av_dynarray2_add((void **)&array, &nb, sizeof(*array),
(const void *)&to_be_added2);
if (!addr)
return AVERROR(ENOMEM);
// Now:
// nb == 2
// to_be_added == array[0]
// to_be_added2 == array[1]

Function Documentation

◆ av_dynarray_add()

void av_dynarray_add ( void *  tab_ptr,
int nb_ptr,
void *  elem 
)

Add the pointer to an element to a dynamic array.

The array to grow is supposed to be an array of pointers to structures, and the element to add must be a pointer to an already allocated structure.

The array is reallocated when its size reaches powers of 2. Therefore, the amortized cost of adding an element is constant.

In case of success, the pointer to the array is updated in order to point to the new grown array, and the number pointed to by nb_ptr is incremented. In case of failure, the array is freed, *tab_ptr is set to NULL and *nb_ptr is set to 0.

Parameters
[in,out]tab_ptrPointer to the array to grow
[in,out]nb_ptrPointer to the number of elements in the array
[in]elemElement to add
See also
av_dynarray_add_nofree(), av_dynarray2_add()

Definition at line 327 of file mem.c.

Referenced by encode_sample_description(), and main().

◆ av_dynarray_add_nofree()

av_warn_unused_result int av_dynarray_add_nofree ( void *  tab_ptr,
int nb_ptr,
void *  elem 
)

◆ av_dynarray2_add()

void* av_dynarray2_add ( void **  tab_ptr,
int nb_ptr,
size_t  elem_size,
const uint8_t *  elem_data 
)

Add an element of size elem_size to a dynamic array.

The array is reallocated when its number of elements reaches powers of 2. Therefore, the amortized cost of adding an element is constant.

In case of success, the pointer to the array is updated in order to point to the new grown array, and the number pointed to by nb_ptr is incremented. In case of failure, the array is freed, *tab_ptr is set to NULL and *nb_ptr is set to 0.

Parameters
[in,out]tab_ptrPointer to the array to grow
[in,out]nb_ptrPointer to the number of elements in the array
[in]elem_sizeSize in bytes of an element in the array
[in]elem_dataPointer to the data of the element to add. If NULL, the space of the newly added element is allocated but left uninitialized.
Returns
Pointer to the data of the element to copy in the newly allocated space
See also
av_dynarray_add(), av_dynarray_add_nofree()

Definition at line 341 of file mem.c.

Referenced by color_get(), color_inc(), ip_parse_addr_list(), mov_read_iprp(), and parse_channel_list().

AVERROR
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
av_dynarray2_add
void * av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size, const uint8_t *elem_data)
Add an element of size elem_size to a dynamic array.
Definition: mem.c:341
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
NULL
#define NULL
Definition: coverity.c:32
av_dynarray_add
void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem)
Add the pointer to an element to a dynamic array.
Definition: mem.c:327
array
static int array[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:111
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34