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 <stdint.h>
20 #include <string.h>
21
27
29 void (*free)(
void *opaque,
uint8_t *data),
30 void *opaque,
int flags)
31 {
34
36 if (!buf)
38
44
47
49 if (!ref) {
52 }
53
57
59 }
60
62 {
64 }
65
67 {
70
72 if (!data)
74
76 if (!ret)
78
79 return ret;
80 }
81
83 {
85 if (!ret)
87
88 memset(ret->
data, 0, size);
89 return ret;
90 }
91
93 {
95
96 if (!ret)
98
100
102
103 return ret;
104 }
105
107 {
109
110 b = (*dst)->buffer;
111
112 if (src) {
115 } else
117
121 }
122 }
123
125 {
126 if (!buf || !*buf)
127 return;
128
130 }
131
133 {
135 return 0;
136
138 }
139
141 {
143 }
144
146 {
148 }
149
151 {
153
155 return 0;
156
158 if (!newbuf)
160
162
164
165 return 0;
166 }
167
169 {
172
173 if (!buf) {
174 /* allocate a new buffer with av_realloc(), so it will be reallocatable
175 * later */
177 if (!data)
179
181 if (!buf) {
184 }
185
188
189 return 0;
190 }
else if (buf->
size == size)
191 return 0;
192
195 /* cannot realloc, allocate a new reallocable buffer and copy data */
197
199 if (!new)
201
203
205 return 0;
206 }
207
209 if (!tmp)
211
214 return 0;
215 }
216
219 void (*pool_free)(void *opaque))
220 {
222 if (!pool)
224
226
231
233
234 return pool;
235 }
236
238 {
240 if (!pool)
242
244
247
249
250 return pool;
251 }
252
253 /*
254 * This function gets called when the pool has been uninited and
255 * all the buffers returned to it.
256 */
258 {
262
265 }
267
270
272 }
273
275 {
277
278 if (!ppool || !*ppool)
279 return;
280 pool = *ppool;
282
285 }
286
287 #if USE_ATOMICS
288 /* remove the whole buffer list from the pool and return it */
290 {
292
293 while (cur != last) {
294 last = cur;
296 if (!cur)
298 }
299
300 return cur;
301 }
302
304 {
307
308 if (!buf)
309 return;
311
314
316 /* pool is not empty, retrieve it and append it to our list */
317 cur = get_pool(pool);
321 }
322 }
323 #endif
324
326 {
329
330 if(CONFIG_MEMORY_POISONING)
332
333 #if USE_ATOMICS
334 add_to_pool(buf);
335 #else
340 #endif
341
344 }
345
346 /* allocate a new buffer and override its free() callback so that
347 * it is returned to the pool on free */
349 {
352
355 if (!ret)
357
359 if (!buf) {
362 }
363
368
371
372 #if USE_ATOMICS
375 #endif
376
377 return ret;
378 }
379
381 {
384
385 #if USE_ATOMICS
386 /* check whether the pool is empty */
387 buf = get_pool(pool);
391 buf = get_pool(pool);
392 }
393
394 if (!buf)
396
397 /* keep the first entry, return the rest of the list to the pool */
398 add_to_pool(buf->
next);
400
402 buf, 0);
403 if (!ret) {
404 add_to_pool(buf);
406 }
407 #else
410 if (buf) {
412 buf, 0);
413 if (ret) {
416 }
417 } else {
419 }
421 #endif
422
423 if (ret)
425
426 return ret;
427 }
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.
#define avpriv_atomic_int_add_and_fetch
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
volatile int refcount
number of existing AVBufferRef instances referring to this buffer
static AVBufferRef * pool_alloc_buffer(AVBufferPool *pool)
uint8_t * data
data described by this buffer
static av_cold int end(AVCodecContext *avctx)
#define avpriv_atomic_int_set
#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 avpriv_atomic_ptr_cas
#define avpriv_atomic_int_get
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
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)
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().
volatile int nb_allocated
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
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)