FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
buffer.h
Go to the documentation of this file.
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 #ifndef AVUTIL_BUFFER_H
26 #define AVUTIL_BUFFER_H
27 
28 #include <stdint.h>
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 typedef struct AVBuffer 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 typedef struct AVBufferRef {
83 
84  /**
85  * The data buffer. It is considered writable if and only if
86  * this is the only reference to the buffer, in which case
87  * av_buffer_is_writable() returns 1.
88  */
90  /**
91  * Size of data in bytes.
92  */
93  int size;
94 } AVBufferRef;
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  */
102 
103 /**
104  * Same as av_buffer_alloc(), except the returned buffer will be initialized
105  * to zero.
106  */
108 
109 /**
110  * Always treat the buffer as read-only, even when it has only one
111  * reference.
112  */
113 #define 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  */
131  void (*free)(void *opaque, uint8_t *data),
132  void *opaque, int flags);
133 
134 /**
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 called
137  * directly.
138  */
139 void av_buffer_default_free(void *opaque, uint8_t *data);
140 
141 /**
142  * Create a new reference to an AVBuffer.
143  *
144  * @return a new AVBufferRef referring to the same AVBuffer as buf or NULL on
145  * failure.
146  */
148 
149 /**
150  * Free a given reference and automatically free the buffer if there are no more
151  * references to it.
152  *
153  * @param buf the reference to be freed. The pointer is set to NULL on return.
154  */
156 
157 /**
158  * @return 1 if the caller may write to the data referred to by buf (which is
159  * 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  */
164 
165 /**
166  * @return the opaque parameter set by av_buffer_create.
167  */
168 void *av_buffer_get_opaque(const AVBufferRef *buf);
169 
171 
172 /**
173  * Create a writable reference from a given buffer reference, avoiding data copy
174  * if possible.
175  *
176  * @param buf buffer reference to make writable. On success, buf is either left
177  * untouched, or it is unreferenced and a new writable AVBufferRef is
178  * written in its place. On failure, buf is left untouched.
179  * @return 0 on success, a negative AVERROR on failure.
180  */
182 
183 /**
184  * Reallocate a given buffer.
185  *
186  * @param buf a buffer reference to reallocate. On success, buf will be
187  * unreferenced and a new reference with the required size will be
188  * written in its place. On failure buf will be left untouched. *buf
189  * 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 was
194  * initially allocated through av_buffer_realloc(NULL) and there is only one
195  * reference to it (i.e. the one passed to this function). In all other cases
196  * a new buffer is allocated and the data is copied.
197  */
198 int av_buffer_realloc(AVBufferRef **buf, int size);
199 
200 /**
201  * @}
202  */
203 
204 /**
205  * @defgroup lavu_bufferpool AVBufferPool
206  * @ingroup lavu_data
207  *
208  * @{
209  * AVBufferPool is an API for a lock-free thread-safe pool of AVBuffers.
210  *
211  * Frequently allocating and freeing large buffers may be slow. AVBufferPool is
212  * meant to solve this in cases when the caller needs a set of buffers of the
213  * same size (the most obvious use case being buffers for raw video or audio
214  * frames).
215  *
216  * At the beginning, the user must call av_buffer_pool_init() to create the
217  * buffer pool. Then whenever a buffer is needed, call av_buffer_pool_get() to
218  * get a reference to a new buffer, similar to av_buffer_alloc(). This new
219  * reference works in all aspects the same way as the one created by
220  * av_buffer_alloc(). However, when the last reference to this buffer is
221  * unreferenced, it is returned to the pool instead of being freed and will be
222  * reused for subsequent av_buffer_pool_get() calls.
223  *
224  * When the caller is done with the pool and no longer needs to allocate any new
225  * buffers, av_buffer_pool_uninit() must be called to mark the pool as freeable.
226  * Once all the buffers are released, it will automatically be freed.
227  *
228  * Allocating and releasing buffers with this API is thread-safe as long as
229  * either the default alloc callback is used, or the user-supplied one is
230  * thread-safe.
231  */
232 
233 /**
234  * The buffer pool. This structure is opaque and not meant to be accessed
235  * directly. It is allocated with av_buffer_pool_init() and freed with
236  * av_buffer_pool_uninit().
237  */
238 typedef struct AVBufferPool AVBufferPool;
239 
240 /**
241  * Allocate and initialize a buffer pool.
242  *
243  * @param size size of each buffer in this pool
244  * @param alloc a function that will be used to allocate new buffers when the
245  * pool is empty. May be NULL, then the default allocator will be used
246  * (av_buffer_alloc()).
247  * @return newly created buffer pool on success, NULL on error.
248  */
249 AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size));
250 
251 /**
252  * Allocate and initialize a buffer pool with a more complex allocator.
253  *
254  * @param size size of each buffer in this pool
255  * @param opaque arbitrary user data used by the allocator
256  * @param alloc a function that will be used to allocate new buffers when the
257  * pool is empty.
258  * @param pool_free a function that will be called immediately before the pool
259  * is freed. I.e. after av_buffer_pool_can_uninit() is called
260  * by the pool and all the frames are returned to the pool and
261  * freed. It is intended to uninitialize the user opaque data.
262  * @return newly created buffer pool on success, NULL on error.
263  */
264 AVBufferPool *av_buffer_pool_init2(int size, void *opaque,
265  AVBufferRef* (*alloc)(void *opaque, int size),
266  void (*pool_free)(void *opaque));
267 
268 /**
269  * Mark the pool as being available for freeing. It will actually be freed only
270  * once all the allocated buffers associated with the pool are released. Thus it
271  * is safe to call this function while some of the allocated buffers are still
272  * in use.
273  *
274  * @param pool pointer to the pool to be freed. It will be set to NULL.
275  */
277 
278 /**
279  * Allocate a new AVBuffer, reusing an old buffer from the pool when available.
280  * This function may be called simultaneously from multiple threads.
281  *
282  * @return a reference to the new buffer on success, NULL on error.
283  */
285 
286 /**
287  * @}
288  */
289 
290 #endif /* AVUTIL_BUFFER_H */
int av_buffer_make_writable(AVBufferRef **buf)
Create a writable reference from a given buffer reference, avoiding data copy if possible.
Definition: buffer.c:150
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:124
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
uint8_t
static int flags
Definition: log.c:57
ptrdiff_t size
Definition: opengl_enc.c:101
The buffer pool.
void av_buffer_default_free(void *opaque, uint8_t *data)
Default free callback, which calls av_free() on the buffer data.
Definition: buffer.c:61
void(* pool_free)(void *opaque)
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:28
int av_buffer_realloc(AVBufferRef **buf, int size)
Reallocate a given buffer.
Definition: buffer.c:168
int av_buffer_is_writable(const AVBufferRef *buf)
Definition: buffer.c:132
AVBufferPool * av_buffer_pool_init2(int size, void *opaque, AVBufferRef *(*alloc)(void *opaque, int size), void(*pool_free)(void *opaque))
Allocate and initialize a buffer pool with a more complex allocator.
Definition: buffer.c:217
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:66
uint8_t * data
The data buffer.
Definition: buffer.h:89
int av_buffer_get_ref_count(const AVBufferRef *buf)
Definition: buffer.c:145
void * buf
Definition: avisynth_c.h:690
AVBufferRef * av_buffer_allocz(int size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:82
AVBuffer * buffer
Definition: buffer.h:82
void * av_buffer_get_opaque(const AVBufferRef *buf)
Definition: buffer.c:140
void av_buffer_pool_uninit(AVBufferPool **pool)
Mark the pool as being available for freeing.
Definition: buffer.c:274
A reference counted buffer type.
int size
Size of data in bytes.
Definition: buffer.h:93
A reference to a data buffer.
Definition: buffer.h:81
AVBufferPool * av_buffer_pool_init(int size, AVBufferRef *(*alloc)(int size))
Allocate and initialize a buffer pool.
Definition: buffer.c:237
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
BufferPoolEntry * pool
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
Definition: buffer.c:380
AVBufferRef *(* alloc)(int size)