FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/buffer.c
Date: 2026-04-30 18:27:06
Exec Total Coverage
Lines: 206 224 92.0%
Functions: 23 23 100.0%
Branches: 76 98 77.6%

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