FFmpeg
buffer.h
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /**
20  * @file
21  * @ingroup lavu_buffer
22  * refcounted data buffer API
23  */
24 
25 #ifndef AVUTIL_BUFFER_H
26 #define AVUTIL_BUFFER_H
27 
28 #include <stddef.h>
29 #include <stdint.h>
30 
31 /**
32  * @defgroup lavu_buffer AVBuffer
33  * @ingroup lavu_data
34  *
35  * @{
36  * AVBuffer is an API for reference-counted data buffers.
37  *
38  * There are two core objects in this API -- AVBuffer and AVBufferRef. AVBuffer
39  * represents the data buffer itself; it is opaque and not meant to be accessed
40  * by the caller directly, but only through AVBufferRef. However, the caller may
41  * e.g. compare two AVBuffer pointers to check whether two different references
42  * are describing the same data buffer. AVBufferRef represents a single
43  * reference to an AVBuffer and it is the object that may be manipulated by the
44  * caller directly.
45  *
46  * There are two functions provided for creating a new AVBuffer with a single
47  * reference -- av_buffer_alloc() to just allocate a new buffer, and
48  * av_buffer_create() to wrap an existing array in an AVBuffer. From an existing
49  * reference, additional references may be created with av_buffer_ref().
50  * Use av_buffer_unref() to free a reference (this will automatically free the
51  * data once all the references are freed).
52  *
53  * The convention throughout this API and the rest of FFmpeg is such that the
54  * buffer is considered writable if there exists only one reference to it (and
55  * it has not been marked as read-only). The av_buffer_is_writable() function is
56  * provided to check whether this is true and av_buffer_make_writable() will
57  * automatically create a new writable buffer when necessary.
58  * Of course nothing prevents the calling code from violating this convention,
59  * however that is safe only when all the existing references are under its
60  * control.
61  *
62  * @note Referencing and unreferencing the buffers is thread-safe and thus
63  * may be done from multiple threads simultaneously without any need for
64  * additional locking.
65  *
66  * @note Two different references to the same buffer can point to different
67  * parts of the buffer (i.e. their AVBufferRef.data will not be equal).
68  */
69 
70 /**
71  * A reference counted buffer type. It is opaque and is meant to be used through
72  * references (AVBufferRef).
73  */
74 typedef struct AVBuffer AVBuffer;
75 
76 /**
77  * A reference to a data buffer.
78  *
79  * The size of this struct is not a part of the public ABI and it is not meant
80  * to be allocated directly.
81  */
82 typedef struct AVBufferRef {
84 
85  /**
86  * The data buffer. It is considered writable if and only if
87  * this is the only reference to the buffer, in which case
88  * av_buffer_is_writable() returns 1.
89  */
90  uint8_t *data;
91  /**
92  * Size of data in bytes.
93  */
94  size_t size;
95 } AVBufferRef;
96 
97 /**
98  * Allocate an AVBuffer of the given size using av_malloc().
99  *
100  * @return an AVBufferRef of given size or NULL when out of memory
101  */
103 
104 /**
105  * Same as av_buffer_alloc(), except the returned buffer will be initialized
106  * to zero.
107  */
109 
110 /**
111  * Always treat the buffer as read-only, even when it has only one
112  * reference.
113  */
114 #define AV_BUFFER_FLAG_READONLY (1 << 0)
115 
116 /**
117  * Create an AVBuffer from an existing array.
118  *
119  * If this function is successful, data is owned by the AVBuffer. The caller may
120  * only access data through the returned AVBufferRef and references derived from
121  * it.
122  * If this function fails, data is left untouched.
123  * @param data data array
124  * @param size size of data in bytes
125  * @param free a callback for freeing this buffer's data
126  * @param opaque parameter to be got for processing or passed to free
127  * @param flags a combination of AV_BUFFER_FLAG_*
128  *
129  * @return an AVBufferRef referring to data on success, NULL on failure.
130  */
131 AVBufferRef *av_buffer_create(uint8_t *data, size_t size,
132  void (*free)(void *opaque, uint8_t *data),
133  void *opaque, int flags);
134 
135 /**
136  * Default free callback, which calls av_free() on the buffer data.
137  * This function is meant to be passed to av_buffer_create(), not called
138  * directly.
139  */
140 void av_buffer_default_free(void *opaque, uint8_t *data);
141 
142 /**
143  * Create a new reference to an AVBuffer.
144  *
145  * @return a new AVBufferRef referring to the same AVBuffer as buf or NULL on
146  * failure.
147  */
149 
150 /**
151  * Free a given reference and automatically free the buffer if there are no more
152  * references to it.
153  *
154  * @param buf the reference to be freed. The pointer is set to NULL on return.
155  */
156 void av_buffer_unref(AVBufferRef **buf);
157 
158 /**
159  * @return 1 if the caller may write to the data referred to by buf (which is
160  * true if and only if buf is the only reference to the underlying AVBuffer).
161  * Return 0 otherwise.
162  * A positive answer is valid until av_buffer_ref() is called on buf.
163  */
164 int av_buffer_is_writable(const AVBufferRef *buf);
165 
166 /**
167  * @return the opaque parameter set by av_buffer_create.
168  */
169 void *av_buffer_get_opaque(const AVBufferRef *buf);
170 
171 int av_buffer_get_ref_count(const AVBufferRef *buf);
172 
173 /**
174  * Create a writable reference from a given buffer reference, avoiding data copy
175  * if possible.
176  *
177  * @param buf buffer reference to make writable. On success, buf is either left
178  * untouched, or it is unreferenced and a new writable AVBufferRef is
179  * written in its place. On failure, buf is left untouched.
180  * @return 0 on success, a negative AVERROR on failure.
181  */
183 
184 /**
185  * Reallocate a given buffer.
186  *
187  * @param buf a buffer reference to reallocate. On success, buf will be
188  * unreferenced and a new reference with the required size will be
189  * written in its place. On failure buf will be left untouched. *buf
190  * may be NULL, then a new buffer is allocated.
191  * @param size required new buffer size.
192  * @return 0 on success, a negative AVERROR on failure.
193  *
194  * @note the buffer is actually reallocated with av_realloc() only if it was
195  * initially allocated through av_buffer_realloc(NULL) and there is only one
196  * reference to it (i.e. the one passed to this function). In all other cases
197  * a new buffer is allocated and the data is copied.
198  */
199 int av_buffer_realloc(AVBufferRef **buf, size_t size);
200 
201 /**
202  * Ensure dst refers to the same data as src.
203  *
204  * When *dst is already equivalent to src, do nothing. Otherwise unreference dst
205  * and replace it with a new reference to src.
206  *
207  * @param dst Pointer to either a valid buffer reference or NULL. On success,
208  * this will point to a buffer reference equivalent to src. On
209  * failure, dst will be left untouched.
210  * @param src A buffer reference to replace dst with. May be NULL, then this
211  * function is equivalent to av_buffer_unref(dst).
212  * @return 0 on success
213  * AVERROR(ENOMEM) on memory allocation failure.
214  */
215 int av_buffer_replace(AVBufferRef **dst, const AVBufferRef *src);
216 
217 /**
218  * @}
219  */
220 
221 /**
222  * @defgroup lavu_bufferpool AVBufferPool
223  * @ingroup lavu_data
224  *
225  * @{
226  * AVBufferPool is an API for a lock-free thread-safe pool of AVBuffers.
227  *
228  * Frequently allocating and freeing large buffers may be slow. AVBufferPool is
229  * meant to solve this in cases when the caller needs a set of buffers of the
230  * same size (the most obvious use case being buffers for raw video or audio
231  * frames).
232  *
233  * At the beginning, the user must call av_buffer_pool_init() to create the
234  * buffer pool. Then whenever a buffer is needed, call av_buffer_pool_get() to
235  * get a reference to a new buffer, similar to av_buffer_alloc(). This new
236  * reference works in all aspects the same way as the one created by
237  * av_buffer_alloc(). However, when the last reference to this buffer is
238  * unreferenced, it is returned to the pool instead of being freed and will be
239  * reused for subsequent av_buffer_pool_get() calls.
240  *
241  * When the caller is done with the pool and no longer needs to allocate any new
242  * buffers, av_buffer_pool_uninit() must be called to mark the pool as freeable.
243  * Once all the buffers are released, it will automatically be freed.
244  *
245  * Allocating and releasing buffers with this API is thread-safe as long as
246  * either the default alloc callback is used, or the user-supplied one is
247  * thread-safe.
248  */
249 
250 /**
251  * The buffer pool. This structure is opaque and not meant to be accessed
252  * directly. It is allocated with av_buffer_pool_init() and freed with
253  * av_buffer_pool_uninit().
254  */
255 typedef struct AVBufferPool AVBufferPool;
256 
257 /**
258  * Allocate and initialize a buffer pool.
259  *
260  * @param size size of each buffer in this pool
261  * @param alloc a function that will be used to allocate new buffers when the
262  * pool is empty. May be NULL, then the default allocator will be used
263  * (av_buffer_alloc()).
264  * @return newly created buffer pool on success, NULL on error.
265  */
267 
268 /**
269  * Allocate and initialize a buffer pool with a more complex allocator.
270  *
271  * @param size size of each buffer in this pool
272  * @param opaque arbitrary user data used by the allocator
273  * @param alloc a function that will be used to allocate new buffers when the
274  * pool is empty. May be NULL, then the default allocator will be
275  * used (av_buffer_alloc()).
276  * @param pool_free a function that will be called immediately before the pool
277  * is freed. I.e. after av_buffer_pool_uninit() is called
278  * by the caller and all the frames are returned to the pool
279  * and freed. It is intended to uninitialize the user opaque
280  * data. May be NULL.
281  * @return newly created buffer pool on success, NULL on error.
282  */
284  AVBufferRef* (*alloc)(void *opaque, size_t size),
285  void (*pool_free)(void *opaque));
286 
287 /**
288  * Mark the pool as being available for freeing. It will actually be freed only
289  * once all the allocated buffers associated with the pool are released. Thus it
290  * is safe to call this function while some of the allocated buffers are still
291  * in use.
292  *
293  * @param pool pointer to the pool to be freed. It will be set to NULL.
294  */
296 
297 /**
298  * Allocate a new AVBuffer, reusing an old buffer from the pool when available.
299  * This function may be called simultaneously from multiple threads.
300  *
301  * @return a reference to the new buffer on success, NULL on error.
302  */
304 
305 /**
306  * Query the original opaque parameter of an allocated buffer in the pool.
307  *
308  * @param ref a buffer reference to a buffer returned by av_buffer_pool_get.
309  * @return the opaque parameter set by the buffer allocator function of the
310  * buffer pool.
311  *
312  * @note the opaque parameter of ref is used by the buffer pool implementation,
313  * therefore you have to use this function to access the original opaque
314  * parameter of an allocated buffer.
315  */
317 
318 /**
319  * @}
320  */
321 
322 #endif /* AVUTIL_BUFFER_H */
av_buffer_pool_init
AVBufferPool * av_buffer_pool_init(size_t size, AVBufferRef *(*alloc)(size_t size))
Allocate and initialize a buffer pool.
Definition: buffer.c:280
AVBufferPool
The buffer pool.
Definition: buffer_internal.h:89
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
data
const char data[16]
Definition: mxf.c:143
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
av_buffer_pool_init2
AVBufferPool * av_buffer_pool_init2(size_t size, void *opaque, AVBufferRef *(*alloc)(void *opaque, size_t size), void(*pool_free)(void *opaque))
Allocate and initialize a buffer pool with a more complex allocator.
Definition: buffer.c:259
AVBufferPool::opaque
void * opaque
Definition: buffer_internal.h:105
av_buffer_pool_get
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
Definition: buffer.c:387
av_buffer_default_free
void av_buffer_default_free(void *opaque, uint8_t *data)
Default free callback, which calls av_free() on the buffer data.
Definition: buffer.c:72
AVBufferPool::pool_free
void(* pool_free)(void *opaque)
Definition: buffer_internal.h:108
av_buffer_unref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:139
av_buffer_pool_uninit
void av_buffer_pool_uninit(AVBufferPool **pool)
Mark the pool as being available for freeing.
Definition: buffer.c:322
av_buffer_create
AVBufferRef * av_buffer_create(uint8_t *data, size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:55
size
int size
Definition: twinvq_data.h:10344
AVBufferPool::alloc
AVBufferRef *(* alloc)(size_t size)
Definition: buffer_internal.h:106
av_buffer_make_writable
int av_buffer_make_writable(AVBufferRef **buf)
Create a writable reference from a given buffer reference, avoiding data copy if possible.
Definition: buffer.c:165
av_buffer_get_ref_count
int av_buffer_get_ref_count(const AVBufferRef *buf)
Definition: buffer.c:160
AVBufferRef::buffer
AVBuffer * buffer
Definition: buffer.h:83
av_buffer_pool_buffer_get_opaque
void * av_buffer_pool_buffer_get_opaque(const AVBufferRef *ref)
Query the original opaque parameter of an allocated buffer in the pool.
Definition: buffer.c:414
av_buffer_alloc
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:77
AVBufferRef::size
size_t size
Size of data in bytes.
Definition: buffer.h:94
AVBuffer
A reference counted buffer type.
Definition: buffer_internal.h:39
AVBufferPool::pool
BufferPoolEntry * pool
Definition: buffer_internal.h:91
av_buffer_replace
int av_buffer_replace(AVBufferRef **dst, const AVBufferRef *src)
Ensure dst refers to the same data as src.
Definition: buffer.c:233
av_buffer_is_writable
int av_buffer_is_writable(const AVBufferRef *buf)
Definition: buffer.c:147
av_buffer_allocz
AVBufferRef * av_buffer_allocz(size_t size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:93
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
av_buffer_realloc
int av_buffer_realloc(AVBufferRef **buf, size_t size)
Reallocate a given buffer.
Definition: buffer.c:183
av_buffer_get_opaque
void * av_buffer_get_opaque(const AVBufferRef *buf)
Definition: buffer.c:155
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561