Path: blob/master/dep/ffmpeg/include/libavutil/buffer.h
4216 views
/*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 <stddef.h>28#include <stdint.h>2930/**31* @defgroup lavu_buffer AVBuffer32* @ingroup lavu_data33*34* @{35* AVBuffer is an API for reference-counted data buffers.36*37* There are two core objects in this API -- AVBuffer and AVBufferRef. AVBuffer38* represents the data buffer itself; it is opaque and not meant to be accessed39* by the caller directly, but only through AVBufferRef. However, the caller may40* e.g. compare two AVBuffer pointers to check whether two different references41* are describing the same data buffer. AVBufferRef represents a single42* reference to an AVBuffer and it is the object that may be manipulated by the43* caller directly.44*45* There are two functions provided for creating a new AVBuffer with a single46* reference -- av_buffer_alloc() to just allocate a new buffer, and47* av_buffer_create() to wrap an existing array in an AVBuffer. From an existing48* reference, additional references may be created with av_buffer_ref().49* Use av_buffer_unref() to free a reference (this will automatically free the50* data once all the references are freed).51*52* The convention throughout this API and the rest of FFmpeg is such that the53* buffer is considered writable if there exists only one reference to it (and54* it has not been marked as read-only). The av_buffer_is_writable() function is55* provided to check whether this is true and av_buffer_make_writable() will56* automatically create a new writable buffer when necessary.57* Of course nothing prevents the calling code from violating this convention,58* however that is safe only when all the existing references are under its59* control.60*61* @note Referencing and unreferencing the buffers is thread-safe and thus62* may be done from multiple threads simultaneously without any need for63* additional locking.64*65* @note Two different references to the same buffer can point to different66* parts of the buffer (i.e. their AVBufferRef.data will not be equal).67*/6869/**70* A reference counted buffer type. It is opaque and is meant to be used through71* references (AVBufferRef).72*/73typedef struct AVBuffer AVBuffer;7475/**76* A reference to a data buffer.77*78* The size of this struct is not a part of the public ABI and it is not meant79* to be allocated directly.80*/81typedef struct AVBufferRef {82AVBuffer *buffer;8384/**85* The data buffer. It is considered writable if and only if86* this is the only reference to the buffer, in which case87* av_buffer_is_writable() returns 1.88*/89uint8_t *data;90/**91* Size of data in bytes.92*/93size_t size;94} AVBufferRef;9596/**97* Allocate an AVBuffer of the given size using av_malloc().98*99* @return an AVBufferRef of given size or NULL when out of memory100*/101AVBufferRef *av_buffer_alloc(size_t size);102103/**104* Same as av_buffer_alloc(), except the returned buffer will be initialized105* to zero.106*/107AVBufferRef *av_buffer_allocz(size_t size);108109/**110* Always treat the buffer as read-only, even when it has only one111* reference.112*/113#define AV_BUFFER_FLAG_READONLY (1 << 0)114115/**116* Create an AVBuffer from an existing array.117*118* If this function is successful, data is owned by the AVBuffer. The caller may119* only access data through the returned AVBufferRef and references derived from120* it.121* If this function fails, data is left untouched.122* @param data data array123* @param size size of data in bytes124* @param free a callback for freeing this buffer's data125* @param opaque parameter to be got for processing or passed to free126* @param flags a combination of AV_BUFFER_FLAG_*127*128* @return an AVBufferRef referring to data on success, NULL on failure.129*/130AVBufferRef *av_buffer_create(uint8_t *data, size_t size,131void (*free)(void *opaque, uint8_t *data),132void *opaque, int flags);133134/**135* Default free callback, which calls av_free() on the buffer data.136* This function is meant to be passed to av_buffer_create(), not called137* directly.138*/139void av_buffer_default_free(void *opaque, uint8_t *data);140141/**142* Create a new reference to an AVBuffer.143*144* @return a new AVBufferRef referring to the same AVBuffer as buf or NULL on145* failure.146*/147AVBufferRef *av_buffer_ref(const AVBufferRef *buf);148149/**150* Free a given reference and automatically free the buffer if there are no more151* references to it.152*153* @param buf the reference to be freed. The pointer is set to NULL on return.154*/155void av_buffer_unref(AVBufferRef **buf);156157/**158* @return 1 if the caller may write to the data referred to by buf (which is159* true if and only if buf is the only reference to the underlying AVBuffer).160* Return 0 otherwise.161* A positive answer is valid until av_buffer_ref() is called on buf.162*/163int av_buffer_is_writable(const AVBufferRef *buf);164165/**166* @return the opaque parameter set by av_buffer_create.167*/168void *av_buffer_get_opaque(const AVBufferRef *buf);169170int av_buffer_get_ref_count(const AVBufferRef *buf);171172/**173* Create a writable reference from a given buffer reference, avoiding data copy174* if possible.175*176* @param buf buffer reference to make writable. On success, buf is either left177* untouched, or it is unreferenced and a new writable AVBufferRef is178* written in its place. On failure, buf is left untouched.179* @return 0 on success, a negative AVERROR on failure.180*/181int av_buffer_make_writable(AVBufferRef **buf);182183/**184* Reallocate a given buffer.185*186* @param buf a buffer reference to reallocate. On success, buf will be187* unreferenced and a new reference with the required size will be188* written in its place. On failure buf will be left untouched. *buf189* may be NULL, then a new buffer is allocated.190* @param size required new buffer size.191* @return 0 on success, a negative AVERROR on failure.192*193* @note the buffer is actually reallocated with av_realloc() only if it was194* initially allocated through av_buffer_realloc(NULL) and there is only one195* reference to it (i.e. the one passed to this function). In all other cases196* a new buffer is allocated and the data is copied.197*/198int av_buffer_realloc(AVBufferRef **buf, size_t size);199200/**201* Ensure dst refers to the same data as src.202*203* When *dst is already equivalent to src, do nothing. Otherwise unreference dst204* and replace it with a new reference to src.205*206* @param dst Pointer to either a valid buffer reference or NULL. On success,207* this will point to a buffer reference equivalent to src. On208* failure, dst will be left untouched.209* @param src A buffer reference to replace dst with. May be NULL, then this210* function is equivalent to av_buffer_unref(dst).211* @return 0 on success212* AVERROR(ENOMEM) on memory allocation failure.213*/214int av_buffer_replace(AVBufferRef **dst, const AVBufferRef *src);215216/**217* @}218*/219220/**221* @defgroup lavu_bufferpool AVBufferPool222* @ingroup lavu_data223*224* @{225* AVBufferPool is an API for a lock-free thread-safe pool of AVBuffers.226*227* Frequently allocating and freeing large buffers may be slow. AVBufferPool is228* meant to solve this in cases when the caller needs a set of buffers of the229* same size (the most obvious use case being buffers for raw video or audio230* frames).231*232* At the beginning, the user must call av_buffer_pool_init() to create the233* buffer pool. Then whenever a buffer is needed, call av_buffer_pool_get() to234* get a reference to a new buffer, similar to av_buffer_alloc(). This new235* reference works in all aspects the same way as the one created by236* av_buffer_alloc(). However, when the last reference to this buffer is237* unreferenced, it is returned to the pool instead of being freed and will be238* reused for subsequent av_buffer_pool_get() calls.239*240* When the caller is done with the pool and no longer needs to allocate any new241* buffers, av_buffer_pool_uninit() must be called to mark the pool as freeable.242* Once all the buffers are released, it will automatically be freed.243*244* Allocating and releasing buffers with this API is thread-safe as long as245* either the default alloc callback is used, or the user-supplied one is246* thread-safe.247*/248249/**250* The buffer pool. This structure is opaque and not meant to be accessed251* directly. It is allocated with av_buffer_pool_init() and freed with252* av_buffer_pool_uninit().253*/254typedef struct AVBufferPool AVBufferPool;255256/**257* Allocate and initialize a buffer pool.258*259* @param size size of each buffer in this pool260* @param alloc a function that will be used to allocate new buffers when the261* pool is empty. May be NULL, then the default allocator will be used262* (av_buffer_alloc()).263* @return newly created buffer pool on success, NULL on error.264*/265AVBufferPool *av_buffer_pool_init(size_t size, AVBufferRef* (*alloc)(size_t size));266267/**268* Allocate and initialize a buffer pool with a more complex allocator.269*270* @param size size of each buffer in this pool271* @param opaque arbitrary user data used by the allocator272* @param alloc a function that will be used to allocate new buffers when the273* pool is empty. May be NULL, then the default allocator will be274* used (av_buffer_alloc()).275* @param pool_free a function that will be called immediately before the pool276* is freed. I.e. after av_buffer_pool_uninit() is called277* by the caller and all the frames are returned to the pool278* and freed. It is intended to uninitialize the user opaque279* data. May be NULL.280* @return newly created buffer pool on success, NULL on error.281*/282AVBufferPool *av_buffer_pool_init2(size_t size, void *opaque,283AVBufferRef* (*alloc)(void *opaque, size_t size),284void (*pool_free)(void *opaque));285286/**287* Mark the pool as being available for freeing. It will actually be freed only288* once all the allocated buffers associated with the pool are released. Thus it289* is safe to call this function while some of the allocated buffers are still290* in use.291*292* @param pool pointer to the pool to be freed. It will be set to NULL.293*/294void av_buffer_pool_uninit(AVBufferPool **pool);295296/**297* Allocate a new AVBuffer, reusing an old buffer from the pool when available.298* This function may be called simultaneously from multiple threads.299*300* @return a reference to the new buffer on success, NULL on error.301*/302AVBufferRef *av_buffer_pool_get(AVBufferPool *pool);303304/**305* Query the original opaque parameter of an allocated buffer in the pool.306*307* @param ref a buffer reference to a buffer returned by av_buffer_pool_get.308* @return the opaque parameter set by the buffer allocator function of the309* buffer pool.310*311* @note the opaque parameter of ref is used by the buffer pool implementation,312* therefore you have to use this function to access the original opaque313* parameter of an allocated buffer.314*/315void *av_buffer_pool_buffer_get_opaque(const AVBufferRef *ref);316317/**318* @}319*/320321#endif /* AVUTIL_BUFFER_H */322323324