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