]> git.sesse.net Git - ffmpeg/blob - libavutil/buffer.c
avutil/tx: Fix declaration after statement
[ffmpeg] / libavutil / buffer.c
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
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
62 void av_buffer_default_free(void *opaque, uint8_t *data)
63 {
64     av_free(data);
65 }
66
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
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
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
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
125 void av_buffer_unref(AVBufferRef **buf)
126 {
127     if (!buf || !*buf)
128         return;
129
130     buffer_replace(buf, NULL);
131 }
132
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
141 void *av_buffer_get_opaque(const AVBufferRef *buf)
142 {
143     return buf->buffer->opaque;
144 }
145
146 int av_buffer_get_ref_count(const AVBufferRef *buf)
147 {
148     return atomic_load(&buf->buffer->refcount);
149 }
150
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
169 int av_buffer_realloc(AVBufferRef **pbuf, int size)
170 {
171     AVBufferRef *buf = *pbuf;
172     uint8_t *tmp;
173     int ret;
174
175     if (!buf) {
176         /* allocate a new buffer with av_realloc(), so it will be reallocatable
177          * later */
178         uint8_t *data = av_realloc(NULL, size);
179         if (!data)
180             return AVERROR(ENOMEM);
181
182         buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
183         if (!buf) {
184             av_freep(&data);
185             return AVERROR(ENOMEM);
186         }
187
188         buf->buffer->flags_internal |= BUFFER_FLAG_REALLOCATABLE;
189         *pbuf = buf;
190
191         return 0;
192     } else if (buf->size == size)
193         return 0;
194
195     if (!(buf->buffer->flags_internal & BUFFER_FLAG_REALLOCATABLE) ||
196         !av_buffer_is_writable(buf) || buf->data != buf->buffer->data) {
197         /* cannot realloc, allocate a new reallocable buffer and copy data */
198         AVBufferRef *new = NULL;
199
200         ret = av_buffer_realloc(&new, size);
201         if (ret < 0)
202             return ret;
203
204         memcpy(new->data, buf->data, FFMIN(size, buf->size));
205
206         buffer_replace(pbuf, &new);
207         return 0;
208     }
209
210     tmp = av_realloc(buf->buffer->data, size);
211     if (!tmp)
212         return AVERROR(ENOMEM);
213
214     buf->buffer->data = buf->data = tmp;
215     buf->buffer->size = buf->size = size;
216     return 0;
217 }
218
219 int av_buffer_replace(AVBufferRef **pdst, AVBufferRef *src)
220 {
221     AVBufferRef *dst = *pdst;
222     AVBufferRef *tmp;
223
224     if (!src) {
225         av_buffer_unref(pdst);
226         return 0;
227     }
228
229     if (dst && dst->buffer == src->buffer) {
230         /* make sure the data pointers match */
231         dst->data = src->data;
232         dst->size = src->size;
233         return 0;
234     }
235
236     tmp = av_buffer_ref(src);
237     if (!tmp)
238         return AVERROR(ENOMEM);
239
240     av_buffer_unref(pdst);
241     *pdst = tmp;
242     return 0;
243 }
244
245 AVBufferPool *av_buffer_pool_init2(int size, void *opaque,
246                                    AVBufferRef* (*alloc)(void *opaque, int size),
247                                    void (*pool_free)(void *opaque))
248 {
249     AVBufferPool *pool = av_mallocz(sizeof(*pool));
250     if (!pool)
251         return NULL;
252
253     ff_mutex_init(&pool->mutex, NULL);
254
255     pool->size      = size;
256     pool->opaque    = opaque;
257     pool->alloc2    = alloc;
258     pool->alloc     = av_buffer_alloc; // fallback
259     pool->pool_free = pool_free;
260
261     atomic_init(&pool->refcount, 1);
262
263     return pool;
264 }
265
266 AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size))
267 {
268     AVBufferPool *pool = av_mallocz(sizeof(*pool));
269     if (!pool)
270         return NULL;
271
272     ff_mutex_init(&pool->mutex, NULL);
273
274     pool->size     = size;
275     pool->alloc    = alloc ? alloc : av_buffer_alloc;
276
277     atomic_init(&pool->refcount, 1);
278
279     return pool;
280 }
281
282 /*
283  * This function gets called when the pool has been uninited and
284  * all the buffers returned to it.
285  */
286 static void buffer_pool_free(AVBufferPool *pool)
287 {
288     while (pool->pool) {
289         BufferPoolEntry *buf = pool->pool;
290         pool->pool = buf->next;
291
292         buf->free(buf->opaque, buf->data);
293         av_freep(&buf);
294     }
295     ff_mutex_destroy(&pool->mutex);
296
297     if (pool->pool_free)
298         pool->pool_free(pool->opaque);
299
300     av_freep(&pool);
301 }
302
303 void av_buffer_pool_uninit(AVBufferPool **ppool)
304 {
305     AVBufferPool *pool;
306
307     if (!ppool || !*ppool)
308         return;
309     pool   = *ppool;
310     *ppool = NULL;
311
312     if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
313         buffer_pool_free(pool);
314 }
315
316 static void pool_release_buffer(void *opaque, uint8_t *data)
317 {
318     BufferPoolEntry *buf = opaque;
319     AVBufferPool *pool = buf->pool;
320
321     if(CONFIG_MEMORY_POISONING)
322         memset(buf->data, FF_MEMORY_POISON, pool->size);
323
324     ff_mutex_lock(&pool->mutex);
325     buf->next = pool->pool;
326     pool->pool = buf;
327     ff_mutex_unlock(&pool->mutex);
328
329     if (atomic_fetch_sub_explicit(&pool->refcount, 1, memory_order_acq_rel) == 1)
330         buffer_pool_free(pool);
331 }
332
333 /* allocate a new buffer and override its free() callback so that
334  * it is returned to the pool on free */
335 static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
336 {
337     BufferPoolEntry *buf;
338     AVBufferRef     *ret;
339
340     av_assert0(pool->alloc || pool->alloc2);
341
342     ret = pool->alloc2 ? pool->alloc2(pool->opaque, pool->size) :
343                          pool->alloc(pool->size);
344     if (!ret)
345         return NULL;
346
347     buf = av_mallocz(sizeof(*buf));
348     if (!buf) {
349         av_buffer_unref(&ret);
350         return NULL;
351     }
352
353     buf->data   = ret->buffer->data;
354     buf->opaque = ret->buffer->opaque;
355     buf->free   = ret->buffer->free;
356     buf->pool   = pool;
357
358     ret->buffer->opaque = buf;
359     ret->buffer->free   = pool_release_buffer;
360
361     return ret;
362 }
363
364 AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
365 {
366     AVBufferRef *ret;
367     BufferPoolEntry *buf;
368
369     ff_mutex_lock(&pool->mutex);
370     buf = pool->pool;
371     if (buf) {
372         ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
373                                buf, 0);
374         if (ret) {
375             pool->pool = buf->next;
376             buf->next = NULL;
377         }
378     } else {
379         ret = pool_alloc_buffer(pool);
380     }
381     ff_mutex_unlock(&pool->mutex);
382
383     if (ret)
384         atomic_fetch_add_explicit(&pool->refcount, 1, memory_order_relaxed);
385
386     return ret;
387 }
388
389 void *av_buffer_pool_buffer_get_opaque(AVBufferRef *ref)
390 {
391     BufferPoolEntry *buf = ref->buffer->opaque;
392     av_assert0(buf);
393     return buf->opaque;
394 }