]> git.sesse.net Git - ffmpeg/blob - libavutil/buffer.c
configure: Drop weak dependencies on external libraries for webm muxer
[ffmpeg] / libavutil / buffer.c
1 /*
2  * This file is part of Libav.
3  *
4  * Libav 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  * Libav 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 Libav; 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 "buffer_internal.h"
24 #include "common.h"
25 #include "mem.h"
26 #include "thread.h"
27
28 AVBufferRef *av_buffer_create(uint8_t *data, int size,
29                               void (*free)(void *opaque, uint8_t *data),
30                               void *opaque, int flags)
31 {
32     AVBufferRef *ref = NULL;
33     AVBuffer    *buf = NULL;
34
35     buf = av_mallocz(sizeof(*buf));
36     if (!buf)
37         return NULL;
38
39     buf->data     = data;
40     buf->size     = size;
41     buf->free     = free ? free : av_buffer_default_free;
42     buf->opaque   = opaque;
43
44     atomic_init(&buf->refcount, 1);
45
46     if (flags & AV_BUFFER_FLAG_READONLY)
47         buf->flags |= BUFFER_FLAG_READONLY;
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 void av_buffer_unref(AVBufferRef **buf)
108 {
109     AVBuffer *b;
110
111     if (!buf || !*buf)
112         return;
113     b = (*buf)->buffer;
114     av_freep(buf);
115
116     if (atomic_fetch_add_explicit(&b->refcount, -1, memory_order_acq_rel) == 1) {
117         b->free(b->opaque, b->data);
118         av_freep(&b);
119     }
120 }
121
122 int av_buffer_is_writable(const AVBufferRef *buf)
123 {
124     if (buf->buffer->flags & AV_BUFFER_FLAG_READONLY)
125         return 0;
126
127     return atomic_load(&buf->buffer->refcount) == 1;
128 }
129
130 int av_buffer_make_writable(AVBufferRef **pbuf)
131 {
132     AVBufferRef *newbuf, *buf = *pbuf;
133
134     if (av_buffer_is_writable(buf))
135         return 0;
136
137     newbuf = av_buffer_alloc(buf->size);
138     if (!newbuf)
139         return AVERROR(ENOMEM);
140
141     memcpy(newbuf->data, buf->data, buf->size);
142     av_buffer_unref(pbuf);
143     *pbuf = newbuf;
144
145     return 0;
146 }
147
148 int av_buffer_realloc(AVBufferRef **pbuf, int size)
149 {
150     AVBufferRef *buf = *pbuf;
151     uint8_t *tmp;
152
153     if (!buf) {
154         /* allocate a new buffer with av_realloc(), so it will be reallocatable
155          * later */
156         uint8_t *data = av_realloc(NULL, size);
157         if (!data)
158             return AVERROR(ENOMEM);
159
160         buf = av_buffer_create(data, size, av_buffer_default_free, NULL, 0);
161         if (!buf) {
162             av_freep(&data);
163             return AVERROR(ENOMEM);
164         }
165
166         buf->buffer->flags |= BUFFER_FLAG_REALLOCATABLE;
167         *pbuf = buf;
168
169         return 0;
170     } else if (buf->size == size)
171         return 0;
172
173     if (!(buf->buffer->flags & BUFFER_FLAG_REALLOCATABLE) ||
174         !av_buffer_is_writable(buf) || buf->data != buf->buffer->data) {
175         /* cannot realloc, allocate a new reallocable buffer and copy data */
176         AVBufferRef *new = NULL;
177
178         av_buffer_realloc(&new, size);
179         if (!new)
180             return AVERROR(ENOMEM);
181
182         memcpy(new->data, buf->data, FFMIN(size, buf->size));
183
184         av_buffer_unref(pbuf);
185         *pbuf = new;
186         return 0;
187     }
188
189     tmp = av_realloc(buf->buffer->data, size);
190     if (!tmp)
191         return AVERROR(ENOMEM);
192
193     buf->buffer->data = buf->data = tmp;
194     buf->buffer->size = buf->size = size;
195     return 0;
196 }
197
198 AVBufferPool *av_buffer_pool_init2(int size, void *opaque,
199                                    AVBufferRef* (*alloc)(void *opaque, int size),
200                                    void (*pool_free)(void *opaque))
201 {
202     AVBufferPool *pool = av_mallocz(sizeof(*pool));
203     if (!pool)
204         return NULL;
205
206     ff_mutex_init(&pool->mutex, NULL);
207
208     pool->size      = size;
209     pool->opaque    = opaque;
210     pool->alloc2    = alloc;
211     pool->pool_free = pool_free;
212
213     atomic_init(&pool->refcount, 1);
214
215     return pool;
216 }
217
218 AVBufferPool *av_buffer_pool_init(int size, AVBufferRef* (*alloc)(int size))
219 {
220     AVBufferPool *pool = av_mallocz(sizeof(*pool));
221     if (!pool)
222         return NULL;
223
224     ff_mutex_init(&pool->mutex, NULL);
225
226     pool->size     = size;
227     pool->alloc    = alloc ? alloc : av_buffer_alloc;
228
229     atomic_init(&pool->refcount, 1);
230
231     return pool;
232 }
233
234 /*
235  * This function gets called when the pool has been uninited and
236  * all the buffers returned to it.
237  */
238 static void buffer_pool_free(AVBufferPool *pool)
239 {
240     while (pool->pool) {
241         BufferPoolEntry *buf = pool->pool;
242         pool->pool = buf->next;
243
244         buf->free(buf->opaque, buf->data);
245         av_freep(&buf);
246     }
247     ff_mutex_destroy(&pool->mutex);
248
249     if (pool->pool_free)
250         pool->pool_free(pool->opaque);
251
252     av_freep(&pool);
253 }
254
255 void av_buffer_pool_uninit(AVBufferPool **ppool)
256 {
257     AVBufferPool *pool;
258
259     if (!ppool || !*ppool)
260         return;
261     pool   = *ppool;
262     *ppool = NULL;
263
264     if (atomic_fetch_add_explicit(&pool->refcount, -1, memory_order_acq_rel) == 1)
265         buffer_pool_free(pool);
266 }
267
268 static void pool_release_buffer(void *opaque, uint8_t *data)
269 {
270     BufferPoolEntry *buf = opaque;
271     AVBufferPool *pool = buf->pool;
272
273     ff_mutex_lock(&pool->mutex);
274     buf->next = pool->pool;
275     pool->pool = buf;
276     ff_mutex_unlock(&pool->mutex);
277
278     if (atomic_fetch_add_explicit(&pool->refcount, -1, memory_order_acq_rel) == 1)
279         buffer_pool_free(pool);
280 }
281
282 /* allocate a new buffer and override its free() callback so that
283  * it is returned to the pool on free */
284 static AVBufferRef *pool_alloc_buffer(AVBufferPool *pool)
285 {
286     BufferPoolEntry *buf;
287     AVBufferRef     *ret;
288
289     ret = pool->alloc2 ? pool->alloc2(pool->opaque, pool->size) :
290                          pool->alloc(pool->size);
291     if (!ret)
292         return NULL;
293
294     buf = av_mallocz(sizeof(*buf));
295     if (!buf) {
296         av_buffer_unref(&ret);
297         return NULL;
298     }
299
300     buf->data   = ret->buffer->data;
301     buf->opaque = ret->buffer->opaque;
302     buf->free   = ret->buffer->free;
303     buf->pool   = pool;
304
305     ret->buffer->opaque = buf;
306     ret->buffer->free   = pool_release_buffer;
307
308     return ret;
309 }
310
311 AVBufferRef *av_buffer_pool_get(AVBufferPool *pool)
312 {
313     AVBufferRef *ret;
314     BufferPoolEntry *buf;
315
316     ff_mutex_lock(&pool->mutex);
317     buf = pool->pool;
318     if (buf) {
319         ret = av_buffer_create(buf->data, pool->size, pool_release_buffer,
320                                buf, 0);
321         if (ret) {
322             pool->pool = buf->next;
323             buf->next = NULL;
324         }
325     } else {
326         ret = pool_alloc_buffer(pool);
327     }
328     ff_mutex_unlock(&pool->mutex);
329
330     if (ret)
331         atomic_fetch_add_explicit(&pool->refcount, 1, memory_order_relaxed);
332
333     return ret;
334 }