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