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