FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/buffer.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 180 220 81.8%
Functions: 20 23 87.0%
Branches: 62 94 66.0%

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