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 #include <stdatomic.h>
20 #include <stdint.h>
21 #include <string.h>
22
27
29 void (*free)(
void *opaque,
uint8_t *data),
30 void *opaque,
int flags)
31 {
34
36 if (!buf)
38
43
45
48
50 if (!ref) {
53 }
54
58
60 }
61
63 {
65 }
66
68 {
71
73 if (!data)
75
77 if (!ret)
79
80 return ret;
81 }
82
84 {
86 if (!ret)
88
89 memset(ret->
data, 0, size);
90 return ret;
91 }
92
94 {
96
97 if (!ret)
99
101
103
104 return ret;
105 }
106
108 {
110
111 b = (*dst)->buffer;
112
113 if (src) {
116 } else
118
122 }
123 }
124
126 {
127 if (!buf || !*buf)
128 return;
129
131 }
132
134 {
136 return 0;
137
139 }
140
142 {
144 }
145
147 {
149 }
150
152 {
154
156 return 0;
157
159 if (!newbuf)
161
163
165
166 return 0;
167 }
168
170 {
173
174 if (!buf) {
175 /* allocate a new buffer with av_realloc(), so it will be reallocatable
176 * later */
178 if (!data)
180
182 if (!buf) {
185 }
186
189
190 return 0;
191 }
else if (buf->
size == size)
192 return 0;
193
196 /* cannot realloc, allocate a new reallocable buffer and copy data */
198
200 if (!new)
202
204
206 return 0;
207 }
208
210 if (!tmp)
212
215 return 0;
216 }
217
220 void (*pool_free)(void *opaque))
221 {
223 if (!pool)
225
227
232
234
235 return pool;
236 }
237
239 {
241 if (!pool)
243
245
248
250
251 return pool;
252 }
253
254 /*
255 * This function gets called when the pool has been uninited and
256 * all the buffers returned to it.
257 */
259 {
263
266 }
268
271
273 }
274
276 {
278
279 if (!ppool || !*ppool)
280 return;
281 pool = *ppool;
283
286 }
287
289 {
292
293 if(CONFIG_MEMORY_POISONING)
295
300
303 }
304
305 /* allocate a new buffer and override its free() callback so that
306 * it is returned to the pool on free */
308 {
311
314 if (!ret)
316
318 if (!buf) {
321 }
322
327
330
331 return ret;
332 }
333
335 {
338
341 if (buf) {
343 buf, 0);
344 if (ret) {
347 }
348 } else {
350 }
352
353 if (ret)
355
356 return ret;
357 }
AVBufferRef *(* alloc2)(void *opaque, int size)
void(* free)(void *opaque, uint8_t *data)
a callback for freeing the data
int av_buffer_make_writable(AVBufferRef **pbuf)
Create a writable reference from a given buffer reference, avoiding data copy if possible.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
ptrdiff_t const GLvoid * data
Memory handling functions.
#define ff_mutex_unlock(mutex)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
#define BUFFER_FLAG_READONLY
The buffer is always treated as read-only.
struct BufferPoolEntry * next
static AVBufferRef * pool_alloc_buffer(AVBufferPool *pool)
uint8_t * data
data described by this buffer
atomic_uint refcount
number of existing AVBufferRef instances referring to this buffer
#define AV_BUFFER_FLAG_READONLY
Always treat the buffer as read-only, even when it has only one reference.
void av_buffer_default_free(void *opaque, uint8_t *data)
Default free callback, which calls av_free() on the buffer data.
void(* pool_free)(void *opaque)
#define atomic_load(object)
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.
int av_buffer_realloc(AVBufferRef **pbuf, int size)
Reallocate a given buffer.
#define ff_mutex_destroy(mutex)
int av_buffer_is_writable(const AVBufferRef *buf)
#define ff_mutex_lock(mutex)
void(* free)(void *opaque, uint8_t *data)
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.
int flags
A combination of BUFFER_FLAG_*.
#define ff_mutex_init(mutex, attr)
#define atomic_fetch_add_explicit(object, operand, order)
static void pool_release_buffer(void *opaque, uint8_t *data)
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
uint8_t * data
The data buffer.
int av_buffer_get_ref_count(const AVBufferRef *buf)
AVBufferRef * av_buffer_allocz(int size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
void * av_buffer_get_opaque(const AVBufferRef *buf)
static void buffer_pool_free(AVBufferPool *pool)
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
static void buffer_replace(AVBufferRef **dst, AVBufferRef **src)
A reference counted buffer type.
int size
Size of data in bytes.
void * opaque
an opaque pointer, to be used by the freeing callback
A reference to a data buffer.
common internal and external API header
AVBufferPool * av_buffer_pool_init(int size, AVBufferRef *(*alloc)(int size))
Allocate and initialize a buffer pool.
static int ref[MAX_W *MAX_W]
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
int size
size of data in bytes
#define atomic_init(obj, value)
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
#define BUFFER_FLAG_REALLOCATABLE
The buffer was av_realloc()ed, so it is reallocatable.
AVBufferRef *(* alloc)(int size)