/*1* This file is part of FFmpeg.2*3* FFmpeg is free software; you can redistribute it and/or4* modify it under the terms of the GNU Lesser General Public5* License as published by the Free Software Foundation; either6* version 2.1 of the License, or (at your option) any later version.7*8* FFmpeg is distributed in the hope that it will be useful,9* but WITHOUT ANY WARRANTY; without even the implied warranty of10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU11* Lesser General Public License for more details.12*13* You should have received a copy of the GNU Lesser General Public14* License along with FFmpeg; if not, write to the Free Software15* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA16*/1718/**19* @file20* @ingroup lavu_buffer21* refcounted data buffer API22*/2324#ifndef AVUTIL_BUFFER_H25#define AVUTIL_BUFFER_H2627#include <stdint.h>2829/**30* @defgroup lavu_buffer AVBuffer31* @ingroup lavu_data32*33* @{34* AVBuffer is an API for reference-counted data buffers.35*36* There are two core objects in this API -- AVBuffer and AVBufferRef. AVBuffer37* represents the data buffer itself; it is opaque and not meant to be accessed38* by the caller directly, but only through AVBufferRef. However, the caller may39* e.g. compare two AVBuffer pointers to check whether two different references40* are describing the same data buffer. AVBufferRef represents a single41* reference to an AVBuffer and it is the object that may be manipulated by the42* caller directly.43*44* There are two functions provided for creating a new AVBuffer with a single45* reference -- av_buffer_alloc() to just allocate a new buffer, and46* av_buffer_create() to wrap an existing array in an AVBuffer. From an existing47* reference, additional references may be created with av_buffer_ref().48* Use av_buffer_unref() to free a reference (this will automatically free the49* data once all the references are freed).50*51* The convention throughout this API and the rest of FFmpeg is such that the52* buffer is considered writable if there exists only one reference to it (and53* it has not been marked as read-only). The av_buffer_is_writable() function is54* provided to check whether this is true and av_buffer_make_writable() will55* automatically create a new writable buffer when necessary.56* Of course nothing prevents the calling code from violating this convention,57* however that is safe only when all the existing references are under its58* control.59*60* @note Referencing and unreferencing the buffers is thread-safe and thus61* may be done from multiple threads simultaneously without any need for62* additional locking.63*64* @note Two different references to the same buffer can point to different65* parts of the buffer (i.e. their AVBufferRef.data will not be equal).66*/6768/**69* A reference counted buffer type. It is opaque and is meant to be used through70* references (AVBufferRef).71*/72typedef struct AVBuffer AVBuffer;7374/**75* A reference to a data buffer.76*77* The size of this struct is not a part of the public ABI and it is not meant78* to be allocated directly.79*/80typedef struct AVBufferRef {81AVBuffer *buffer;8283/**84* The data buffer. It is considered writable if and only if85* this is the only reference to the buffer, in which case86* av_buffer_is_writable() returns 1.87*/88uint8_t *data;89/**90* Size of data in bytes.91*/92int size;93} AVBufferRef;9495/**96* Allocate an AVBuffer of the given size using av_malloc().97*98* @return an AVBufferRef of given size or NULL when out of memory99*/100AVBufferRef *av_buffer_alloc(int size);101102/**103* Same as av_buffer_alloc(), except the returned buffer will be initialized104* to zero.105*/106AVBufferRef *av_buffer_allocz(int size);107108/**109* Always treat the buffer as read-only, even when it has only one110* reference.111*/112#define AV_BUFFER_FLAG_READONLY (1 << 0)113114/**115* Create an AVBuffer from an existing array.116*117* If this function is successful, data is owned by the AVBuffer. The caller may118* only access data through the returned AVBufferRef and references derived from119* it.120* If this function fails, data is left untouched.121* @param data data array122* @param size size of data in bytes123* @param free a callback for freeing this buffer's data124* @param opaque parameter to be got for processing or passed to free125* @param flags a combination of AV_BUFFER_FLAG_*126*127* @return an AVBufferRef referring to data on success, NULL on failure.128*/129AVBufferRef *av_buffer_create(uint8_t *data, int size,130void (*free)(void *opaque, uint8_t *data),131void *opaque, int flags);132133/**134* Default free callback, which calls av_free() on the buffer data.135* This function is meant to be passed to av_buffer_create(), not called136* directly.137*/138void av_buffer_default_free(void *opaque, uint8_t *data);139140/**141* Create a new reference to an AVBuffer.142*143* @return a new AVBufferRef referring to the same AVBuffer as buf or NULL on144* failure.145*/146AVBufferRef *av_buffer_ref(AVBufferRef *buf);147148/**149* Free a given reference and automatically free the buffer if there are no more150* references to it.151*152* @param buf the reference to be freed. The pointer is set to NULL on return.153*/154void av_buffer_unref(AVBufferRef **buf);155156/**157* @return 1 if the caller may write to the data referred to by buf (which is158* true if and only if buf is the only reference to the underlying AVBuffer).159* Return 0 otherwise.160* A positive answer is valid until av_buffer_ref() is called on buf.161*/162int av_buffer_is_writable(const AVBufferRef *buf);163164/**165* @return the opaque parameter set by av_buffer_create.166*/167void *av_buffer_get_opaque(const AVBufferRef *buf);168169int av_buffer_get_ref_count(const AVBufferRef *buf);170171/**172* Create a writable reference from a given buffer reference, avoiding data copy173* if possible.174*175* @param buf buffer reference to make writable. On success, buf is either left176* untouched, or it is unreferenced and a new writable AVBufferRef is177* written in its place. On failure, buf is left untouched.178* @return 0 on success, a negative AVERROR on failure.179*/180int av_buffer_make_writable(AVBufferRef **buf);181182/**183* Reallocate a given buffer.184*185* @param buf a buffer reference to reallocate. On success, buf will be186* unreferenced and a new reference with the required size will be187* written in its place. On failure buf will be left untouched. *buf188* may be NULL, then a new buffer is allocated.189* @param size required new buffer size.190* @return 0 on success, a negative AVERROR on failure.191*192* @note the buffer is actually reallocated with av_realloc() only if it was193* initially allocated through av_buffer_realloc(NULL) and there is only one194* reference to it (i.e. the one passed to this function). In all other cases195* a new buffer is allocated and the data is copied.196*/197int av_buffer_realloc(AVBufferRef **buf, int size);198199/**200* @}201*/202203/**204* @defgroup lavu_bufferpool AVBufferPool205* @ingroup lavu_data206*207* @{208* AVBufferPool is an API for a lock-free thread-safe pool of AVBuffers.209*210* Frequently allocating and freeing large buffers may be slow. AVBufferPool is211* meant to solve this in cases when the caller needs a set of buffers of the212* same size (the most obvious use case being buffers for raw video or audio213* frames).214*215* At the beginning, the user must call av_buffer_pool_init() to create the216* buffer pool. Then whenever a buffer is needed, call av_buffer_pool_get() to217* get a reference to a new buffer, similar to av_buffer_alloc(). This new218* reference works in all aspects the same way as the one created by219* av_buffer_alloc(). However, when the last reference to this buffer is220* unreferenced, it is returned to the pool instead of being freed and will be221* reused for subsequent av_buffer_pool_get() calls.222*223* When the caller is done with the pool and no longer needs to allocate any new224* buffers, av_buffer_pool_uninit() must be called to mark the pool as freeable.225* Once all the buffers are released, it will automatically be freed.226*227* Allocating and releasing buffers with this API is thread-safe as long as228* either the default alloc callback is used, or the user-supplied one is229* thread-safe.230*/231232/**233* The buffer pool. This structure is opaque and not meant to be accessed234* directly. It is allocated with av_buffer_pool_init() and freed with235* av_buffer_pool_uninit().236*/237typedef struct AVBufferPool AVBufferPool;238239/**240* Allocate and initialize a buffer pool.241*242* @param size size of each buffer in this pool243* @param alloc a function that will be used to allocate new buffers when the244* pool is empty. May be NULL, then the default allocator will be used245* (av_buffer_alloc()).246* @return newly created buffer pool on success, NULL on error.247*/248AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size));249250/**251* Mark the pool as being available for freeing. It will actually be freed only252* once all the allocated buffers associated with the pool are released. Thus it253* is safe to call this function while some of the allocated buffers are still254* in use.255*256* @param pool pointer to the pool to be freed. It will be set to NULL.257* @see av_buffer_pool_can_uninit()258*/259void av_buffer_pool_uninit(AVBufferPool **pool);260261/**262* Allocate a new AVBuffer, reusing an old buffer from the pool when available.263* This function may be called simultaneously from multiple threads.264*265* @return a reference to the new buffer on success, NULL on error.266*/267AVBufferRef *av_buffer_pool_get(AVBufferPool *pool);268269/**270* @}271*/272273#endif /* AVUTIL_BUFFER_H */274275276