annotate ffmpeg/libavutil/buffer.h @ 13:844d341cf643 tip

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