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