• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 #include "avassert.h"
24 #include "buffer_internal.h"
25 #include "common.h"
26 #include "mem.h"
27 #include "thread.h"
28 
av_buffer_create(uint8_t * data,int size,void (* free)(void * opaque,uint8_t * data),void * opaque,int flags)29 AVBufferRef *av_buffer_create(uint8_t *data, int size,
30                               void (*free)(void *opaque, uint8_t *data),
31                               void *opaque, int flags)
32 {
33     AVBufferRef *ref = NULL;
34     AVBuffer    *buf = NULL;
35 
36     buf = av_mallocz(sizeof(*buf));
37     if (!buf)
38         return NULL;
39 
40     buf->data     = data;
41     buf->size     = size;
42     buf->free     = free ? free : av_buffer_default_free;
43     buf->opaque   = opaque;
44 
45     atomic_init(&buf->refcount, 1);
46 
47     buf->flags = flags;
48 
49     ref = av_mallocz(sizeof(*ref));
50     if (!ref) {
51         av_freep(&buf);
52         return NULL;
53     }
54 
55     ref->buffer = buf;
56     ref->data   = data;
57     ref->size   = size;
58 
59     return ref;
60 }
61 
av_buffer_default_free(void * opaque,uint8_t * data)62 void av_buffer_default_free(void *opaque, uint8_t *data)
63 {
64     av_free(data);
65 }
66 
av_buffer_alloc(int size)67 AVBufferRef *av_buffer_alloc(int size)
68 {
69     AVBufferRef *ret = NULL;
70     uint8_t    *data = NULL;
71 
72     data = av_malloc(size);
73     if (!data)
74         return NULL;
75 
76     ret = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
77     if (!ret)
78         av_freep(&data);
79 
80     return ret;
81 }
82 
av_buffer_allocz(int size)83 AVBufferRef *av_buffer_allocz(int size)
84 {
85     AVBufferRef *ret = av_buffer_alloc(size);
86     if (!ret)
87         return NULL;
88 
89     memset(ret->data, 0, size);
90     return ret;
91 }
92 
av_buffer_ref(AVBufferRef * buf)93 AVBufferRef *av_buffer_ref(AVBufferRef *buf)
94 {
95     AVBufferRef *ret = av_mallocz(sizeof(*ret));
96 
97     if (!ret)
98         return NULL;
99 
100     *ret = *buf;
101 
102     atomic_fetch_add_explicit(&buf->buffer->refcount, 1, memory_order_relaxed);
103 
104     return ret;
105 }
106 
buffer_replace(AVBufferRef ** dst,AVBufferRef ** src)107 static void buffer_replace(AVBufferRef **dst, AVBufferRef **src)
108 {
109     AVBuffer *b;
110 
111     b = (*dst)->buffer;
112 
113     if (src) {
114         **dst = **src;
115         av_freep(src);
116     } else
117         av_freep(dst);
118 
119     if (atomic_fetch_sub_explicit(&b->refcount, 1, memory_order_acq_rel) == 1) {
120         b->free(b->opaque, b->data);
121         av_freep(&b);
122     }
123 }
124 
av_buffer_unref(AVBufferRef ** buf)125 void av_buffer_unref(AVBufferRef **buf)
126 {
127     if (!buf || !*buf)
128         return;
129 
130     buffer_replace(buf, NULL);
131 }
132 
av_buffer_is_writable(const AVBufferRef * buf)133 int av_buffer_is_writable(const AVBufferRef *buf)
134 {
135     if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)
136         return 0;
137 
138     return atomic_load(&buf->buffer->refcount) == 1;
139 }
140 
av_buffer_get_opaque(const AVBufferRef * buf)141 void *av_buffer_get_opaque(const AVBufferRef *buf)
142 {
143     return buf->buffer->opaque;
144 }
145 
av_buffer_get_ref_count(const AVBufferRef * buf)146 int av_buffer_get_ref_count(const AVBufferRef *buf)
147 {
148     return atomic_load(&buf->buffer->refcount);
149 }
150 
av_buffer_make_writable(AVBufferRef ** pbuf)151 int av_buffer_make_writable(AVBufferRef **pbuf)
152 {
153     AVBufferRef *newbuf, *buf = *pbuf;
154 
155     if (av_buffer_is_writable(buf))
156         return 0;
157 
158     newbuf = av_buffer_alloc(buf->size);
159     if (!newbuf)
160         return AVERROR(ENOMEM);
161 
162     memcpy(newbuf->data, buf->data, buf->size);
163 
164     buffer_replace(pbuf, &newbuf);
165 
166     return 0;
167 }
168 
av_buffer_realloc(AVBufferRef ** pbuf,int size)169 int av_buffer_realloc(AVBufferRef **pbuf, int size)
170 {
171     AVBufferRef *buf = *pbuf;
172     uint8_t *tmp;
173 
174     if (!buf) {
175         /* allocate a new buffer with av_realloc(), so it will be reallocatable
176          * later */
177         uint8_t *data = av_realloc(NULL, size);
178         if (!data)
179             return AVERROR(ENOMEM);
180 
181         buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
182         if (!buf) {
183             av_freep(&data);
184             return AVERROR(ENOMEM);
185         }
186 
187         buf->buffer->flags_internal |= BUFFER_FLAG_REALLOCATABLE;
188         *pbuf = buf;
189 
190         return 0;
191     } else if (buf->size == size)
192         return 0;
193 
194     if (!(buf->buffer->flags_internal & BUFFER_FLAG_REALLOCATABLE) ||
195         !av_buffer_is_writable(buf) || buf->data != buf->buffer->data) {
196         /* cannot realloc, allocate a new reallocable buffer and copy data */
197         AVBufferRef *new = NULL;
198 
199         av_buffer_realloc(&new, size);
200         if (!new)
201             return AVERROR(ENOMEM);
202 
203         memcpy(new->data, buf->data, FFMIN(size, buf->size));
204 
205         buffer_replace(pbuf, &new);
206         return 0;
207     }
208 
209     tmp = av_realloc(buf->buffer->data, size);
210     if (!tmp)
211         return AVERROR(ENOMEM);
212 
213     buf->buffer->data = buf->data = tmp;
214     buf->buffer->size = buf->size = size;
215     return 0;
216 }
217 
av_buffer_pool_init2(int size,void * opaque,AVBufferRef * (* alloc)(void * opaque,int size),void (* pool_free)(void * opaque))218 AVBufferPool *av_buffer_pool_init2(int size, void *opaque,
219                                    AVBufferRef* (*alloc)(void *opaque, int size),
220                                    void (*pool_free)(void *opaque))
221 {
222     AVBufferPool *pool = av_mallocz(sizeof(*pool));
223     if (!pool)
224         return NULL;
225 
226     ff_mutex_init(&pool->mutex, NULL);
227 
228     pool->size      = size;
229     pool->opaque    = opaque;
230     pool->alloc2    = alloc;
231     pool->alloc     = av_buffer_alloc; // fallback
232     pool->pool_free = pool_free;
233 
234     atomic_init(&pool->refcount, 1);
235 
236     return pool;
237 }
238 
av_buffer_pool_init(int size,AVBufferRef * (* alloc)(int size))239 AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size))
240 {
241     AVBufferPool *pool = av_mallocz(sizeof(*pool));
242     if (!pool)
243         return NULL;
244 
245     ff_mutex_init(&pool->mutex, NULL);
246 
247     pool->size     = size;
248     pool->alloc    = alloc ? alloc : av_buffer_alloc;
249 
250     atomic_init(&pool->refcount, 1);
251 
252     return pool;
253 }
254 
255 /*
256  * This function gets called when the pool has been uninited and
257  * all the buffers returned to it.
258  */
buffer_pool_free(AVBufferPool * pool)259 static void buffer_pool_free(AVBufferPool *pool)
260 {
261     while (pool->pool) {
262         BufferPoolEntry *buf = pool->pool;
263         pool->pool = buf->next;
264 
265         buf->free(buf->opaque, buf->data);
266         av_freep(&buf);
267     }
268     ff_mutex_destroy(&pool->mutex);
269 
270     if (pool->pool_free)
271         pool->pool_free(pool->opaque);
272 
273     av_freep(&pool);
274 }
275 
av_buffer_pool_uninit(AVBufferPool ** ppool)276 void av_buffer_pool_uninit(AVBufferPool **ppool)
277 {
278     AVBufferPool *pool;
279 
280     if (!ppool || !*ppool)
281         return;
282     pool   = *ppool;
283     *ppool = NULL;
284 
285     if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
286         buffer_pool_free(pool);
287 }
288 
pool_release_buffer(void * opaque,uint8_t * data)289 static void pool_release_buffer(void *opaque, uint8_t *data)
290 {
291     BufferPoolEntry *buf = opaque;
292     AVBufferPool *pool = buf->pool;
293 
294     if(CONFIG_MEMORY_POISONING)
295         memset(buf->data, FF_MEMORY_POISON, pool->size);
296 
297     ff_mutex_lock(&pool->mutex);
298     buf->next = pool->pool;
299     pool->pool = buf;
300     ff_mutex_unlock(&pool->mutex);
301 
302     if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
303         buffer_pool_free(pool);
304 }
305 
306 /* allocate a new buffer and override its free() callback so that
307  * it is returned to the pool on free */
pool_alloc_buffer(AVBufferPool * pool)308 static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
309 {
310     BufferPoolEntry *buf;
311     AVBufferRef     *ret;
312 
313     av_assert0(pool->alloc || pool->alloc2);
314 
315     ret = pool->alloc2 ? pool->alloc2(pool->opaque, pool->size) :
316                          pool->alloc(pool->size);
317     if (!ret)
318         return NULL;
319 
320     buf = av_mallocz(sizeof(*buf));
321     if (!buf) {
322         av_buffer_unref(&ret);
323         return NULL;
324     }
325 
326     buf->data   = ret->buffer->data;
327     buf->opaque = ret->buffer->opaque;
328     buf->free   = ret->buffer->free;
329     buf->pool   = pool;
330 
331     ret->buffer->opaque = buf;
332     ret->buffer->free   = pool_release_buffer;
333 
334     return ret;
335 }
336 
av_buffer_pool_get(AVBufferPool * pool)337 AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
338 {
339     AVBufferRef *ret;
340     BufferPoolEntry *buf;
341 
342     ff_mutex_lock(&pool->mutex);
343     buf = pool->pool;
344     if (buf) {
345         ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
346                                buf, 0);
347         if (ret) {
348             pool->pool = buf->next;
349             buf->next = NULL;
350         }
351     } else {
352         ret = pool_alloc_buffer(pool);
353     }
354     ff_mutex_unlock(&pool->mutex);
355 
356     if (ret)
357         atomic_fetch_add_explicit(&pool->refcount, 1, memory_order_relaxed);
358 
359     return ret;
360 }
361 
av_buffer_pool_buffer_get_opaque(AVBufferRef * ref)362 void *av_buffer_pool_buffer_get_opaque(AVBufferRef *ref)
363 {
364     BufferPoolEntry *buf = ref->buffer->opaque;
365     av_assert0(buf);
366     return buf->opaque;
367 }
368