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