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
6 * License 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 GNU
12 * Lesser General Public License for more details.
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
21 * @ingroup lavu_buffer
22 * refcounted data buffer API
25 #ifndef AVUTIL_BUFFER_H
26 #define AVUTIL_BUFFER_H
34 * @defgroup lavu_buffer AVBuffer
38 * AVBuffer is an API for reference-counted data buffers.
40 * There are two core objects in this API -- AVBuffer and AVBufferRef. AVBuffer
41 * represents the data buffer itself; it is opaque and not meant to be accessed
42 * by the caller directly, but only through AVBufferRef. However, the caller may
43 * e.g. compare two AVBuffer pointers to check whether two different references
44 * are describing the same data buffer. AVBufferRef represents a single
45 * reference to an AVBuffer and it is the object that may be manipulated by the
48 * There are two functions provided for creating a new AVBuffer with a single
49 * reference -- av_buffer_alloc() to just allocate a new buffer, and
50 * av_buffer_create() to wrap an existing array in an AVBuffer. From an existing
51 * reference, additional references may be created with av_buffer_ref().
52 * Use av_buffer_unref() to free a reference (this will automatically free the
53 * data once all the references are freed).
55 * The convention throughout this API and the rest of FFmpeg is such that the
56 * buffer is considered writable if there exists only one reference to it (and
57 * it has not been marked as read-only). The av_buffer_is_writable() function is
58 * provided to check whether this is true and av_buffer_make_writable() will
59 * automatically create a new writable buffer when necessary.
60 * Of course nothing prevents the calling code from violating this convention,
61 * however that is safe only when all the existing references are under its
64 * @note Referencing and unreferencing the buffers is thread-safe and thus
65 * may be done from multiple threads simultaneously without any need for
68 * @note Two different references to the same buffer can point to different
69 * parts of the buffer (i.e. their AVBufferRef.data will not be equal).
73 * A reference counted buffer type. It is opaque and is meant to be used through
74 * references (AVBufferRef).
76 typedef struct AVBuffer AVBuffer;
79 * A reference to a data buffer.
81 * The size of this struct is not a part of the public ABI and it is not meant
82 * to be allocated directly.
84 typedef struct AVBufferRef {
88 * The data buffer. It is considered writable if and only if
89 * this is the only reference to the buffer, in which case
90 * av_buffer_is_writable() returns 1.
94 * Size of data in bytes.
96 #if FF_API_BUFFER_SIZE_T
104 * Allocate an AVBuffer of the given size using av_malloc().
106 * @return an AVBufferRef of given size or NULL when out of memory
108 #if FF_API_BUFFER_SIZE_T
109 AVBufferRef *av_buffer_alloc(int size);
111 AVBufferRef *av_buffer_alloc(size_t size);
115 * Same as av_buffer_alloc(), except the returned buffer will be initialized
118 #if FF_API_BUFFER_SIZE_T
119 AVBufferRef *av_buffer_allocz(int size);
121 AVBufferRef *av_buffer_allocz(size_t size);
125 * Always treat the buffer as read-only, even when it has only one
128 #define AV_BUFFER_FLAG_READONLY (1 << 0)
131 * Create an AVBuffer from an existing array.
133 * If this function is successful, data is owned by the AVBuffer. The caller may
134 * only access data through the returned AVBufferRef and references derived from
136 * If this function fails, data is left untouched.
137 * @param data data array
138 * @param size size of data in bytes
139 * @param free a callback for freeing this buffer's data
140 * @param opaque parameter to be got for processing or passed to free
141 * @param flags a combination of AV_BUFFER_FLAG_*
143 * @return an AVBufferRef referring to data on success, NULL on failure.
145 #if FF_API_BUFFER_SIZE_T
146 AVBufferRef *av_buffer_create(uint8_t *data, int size,
148 AVBufferRef *av_buffer_create(uint8_t *data, size_t size,
150 void (*free)(void *opaque, uint8_t *data),
151 void *opaque, int flags);
154 * Default free callback, which calls av_free() on the buffer data.
155 * This function is meant to be passed to av_buffer_create(), not called
158 void av_buffer_default_free(void *opaque, uint8_t *data);
161 * Create a new reference to an AVBuffer.
163 * @return a new AVBufferRef referring to the same AVBuffer as buf or NULL on
166 AVBufferRef *av_buffer_ref(AVBufferRef *buf);
169 * Free a given reference and automatically free the buffer if there are no more
172 * @param buf the reference to be freed. The pointer is set to NULL on return.
174 void av_buffer_unref(AVBufferRef **buf);
177 * @return 1 if the caller may write to the data referred to by buf (which is
178 * true if and only if buf is the only reference to the underlying AVBuffer).
179 * Return 0 otherwise.
180 * A positive answer is valid until av_buffer_ref() is called on buf.
182 int av_buffer_is_writable(const AVBufferRef *buf);
185 * @return the opaque parameter set by av_buffer_create.
187 void *av_buffer_get_opaque(const AVBufferRef *buf);
189 int av_buffer_get_ref_count(const AVBufferRef *buf);
192 * Create a writable reference from a given buffer reference, avoiding data copy
195 * @param buf buffer reference to make writable. On success, buf is either left
196 * untouched, or it is unreferenced and a new writable AVBufferRef is
197 * written in its place. On failure, buf is left untouched.
198 * @return 0 on success, a negative AVERROR on failure.
200 int av_buffer_make_writable(AVBufferRef **buf);
203 * Reallocate a given buffer.
205 * @param buf a buffer reference to reallocate. On success, buf will be
206 * unreferenced and a new reference with the required size will be
207 * written in its place. On failure buf will be left untouched. *buf
208 * may be NULL, then a new buffer is allocated.
209 * @param size required new buffer size.
210 * @return 0 on success, a negative AVERROR on failure.
212 * @note the buffer is actually reallocated with av_realloc() only if it was
213 * initially allocated through av_buffer_realloc(NULL) and there is only one
214 * reference to it (i.e. the one passed to this function). In all other cases
215 * a new buffer is allocated and the data is copied.
217 #if FF_API_BUFFER_SIZE_T
218 int av_buffer_realloc(AVBufferRef **buf, int size);
220 int av_buffer_realloc(AVBufferRef **buf, size_t size);
224 * Ensure dst refers to the same data as src.
226 * When *dst is already equivalent to src, do nothing. Otherwise unreference dst
227 * and replace it with a new reference to src.
229 * @param dst Pointer to either a valid buffer reference or NULL. On success,
230 * this will point to a buffer reference equivalent to src. On
231 * failure, dst will be left untouched.
232 * @param src A buffer reference to replace dst with. May be NULL, then this
233 * function is equivalent to av_buffer_unref(dst).
234 * @return 0 on success
235 * AVERROR(ENOMEM) on memory allocation failure.
237 int av_buffer_replace(AVBufferRef **dst, AVBufferRef *src);
244 * @defgroup lavu_bufferpool AVBufferPool
248 * AVBufferPool is an API for a lock-free thread-safe pool of AVBuffers.
250 * Frequently allocating and freeing large buffers may be slow. AVBufferPool is
251 * meant to solve this in cases when the caller needs a set of buffers of the
252 * same size (the most obvious use case being buffers for raw video or audio
255 * At the beginning, the user must call av_buffer_pool_init() to create the
256 * buffer pool. Then whenever a buffer is needed, call av_buffer_pool_get() to
257 * get a reference to a new buffer, similar to av_buffer_alloc(). This new
258 * reference works in all aspects the same way as the one created by
259 * av_buffer_alloc(). However, when the last reference to this buffer is
260 * unreferenced, it is returned to the pool instead of being freed and will be
261 * reused for subsequent av_buffer_pool_get() calls.
263 * When the caller is done with the pool and no longer needs to allocate any new
264 * buffers, av_buffer_pool_uninit() must be called to mark the pool as freeable.
265 * Once all the buffers are released, it will automatically be freed.
267 * Allocating and releasing buffers with this API is thread-safe as long as
268 * either the default alloc callback is used, or the user-supplied one is
273 * The buffer pool. This structure is opaque and not meant to be accessed
274 * directly. It is allocated with av_buffer_pool_init() and freed with
275 * av_buffer_pool_uninit().
277 typedef struct AVBufferPool AVBufferPool;
280 * Allocate and initialize a buffer pool.
282 * @param size size of each buffer in this pool
283 * @param alloc a function that will be used to allocate new buffers when the
284 * pool is empty. May be NULL, then the default allocator will be used
285 * (av_buffer_alloc()).
286 * @return newly created buffer pool on success, NULL on error.
288 #if FF_API_BUFFER_SIZE_T
289 AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size));
291 AVBufferPool *av_buffer_pool_init(size_t size, AVBufferRef* (*alloc)(size_t size));
295 * Allocate and initialize a buffer pool with a more complex allocator.
297 * @param size size of each buffer in this pool
298 * @param opaque arbitrary user data used by the allocator
299 * @param alloc a function that will be used to allocate new buffers when the
300 * pool is empty. May be NULL, then the default allocator will be
301 * used (av_buffer_alloc()).
302 * @param pool_free a function that will be called immediately before the pool
303 * is freed. I.e. after av_buffer_pool_uninit() is called
304 * by the caller and all the frames are returned to the pool
305 * and freed. It is intended to uninitialize the user opaque
307 * @return newly created buffer pool on success, NULL on error.
309 #if FF_API_BUFFER_SIZE_T
310 AVBufferPool *av_buffer_pool_init2(int size, void *opaque,
311 AVBufferRef* (*alloc)(void *opaque, int size),
313 AVBufferPool *av_buffer_pool_init2(size_t size, void *opaque,
314 AVBufferRef* (*alloc)(void *opaque, size_t size),
316 void (*pool_free)(void *opaque));
319 * Mark the pool as being available for freeing. It will actually be freed only
320 * once all the allocated buffers associated with the pool are released. Thus it
321 * is safe to call this function while some of the allocated buffers are still
324 * @param pool pointer to the pool to be freed. It will be set to NULL.
326 void av_buffer_pool_uninit(AVBufferPool **pool);
329 * Allocate a new AVBuffer, reusing an old buffer from the pool when available.
330 * This function may be called simultaneously from multiple threads.
332 * @return a reference to the new buffer on success, NULL on error.
334 AVBufferRef *av_buffer_pool_get(AVBufferPool *pool);
337 * Query the original opaque parameter of an allocated buffer in the pool.
339 * @param ref a buffer reference to a buffer returned by av_buffer_pool_get.
340 * @return the opaque parameter set by the buffer allocator function of the
343 * @note the opaque parameter of ref is used by the buffer pool implementation,
344 * therefore you have to use this function to access the original opaque
345 * parameter of an allocated buffer.
347 void *av_buffer_pool_buffer_get_opaque(AVBufferRef *ref);
353 #endif /* AVUTIL_BUFFER_H */