FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/fifo.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 126 265 47.5%
Functions: 16 32 50.0%
Branches: 59 130 45.4%

Line Branch Exec Source
1 /*
2 * a very simple circular buffer FIFO implementation
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 * Copyright (c) 2006 Roman Shaposhnik
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <stdint.h>
24 #include <string.h>
25
26 #include "avassert.h"
27 #include "error.h"
28 #include "fifo.h"
29 #include "macros.h"
30 #include "mem.h"
31
32 // by default the FIFO can be auto-grown to 1MB
33 #define AUTO_GROW_DEFAULT_BYTES (1024 * 1024)
34
35 struct AVFifo {
36 uint8_t *buffer;
37
38 size_t elem_size, nb_elems;
39 size_t offset_r, offset_w;
40 // distinguishes the ambiguous situation offset_r == offset_w
41 int is_empty;
42
43 unsigned int flags;
44 size_t auto_grow_limit;
45 };
46
47 49164 AVFifo *av_fifo_alloc2(size_t nb_elems, size_t elem_size,
48 unsigned int flags)
49 {
50 AVFifo *f;
51 49164 void *buffer = NULL;
52
53
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49164 times.
49164 if (!elem_size)
54 return NULL;
55
56
1/2
✓ Branch 0 taken 49164 times.
✗ Branch 1 not taken.
49164 if (nb_elems) {
57 49164 buffer = av_realloc_array(NULL, nb_elems, elem_size);
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49164 times.
49164 if (!buffer)
59 return NULL;
60 }
61 49164 f = av_mallocz(sizeof(*f));
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49164 times.
49164 if (!f) {
63 av_free(buffer);
64 return NULL;
65 }
66 49164 f->buffer = buffer;
67 49164 f->nb_elems = nb_elems;
68 49164 f->elem_size = elem_size;
69 49164 f->is_empty = 1;
70
71 49164 f->flags = flags;
72 49164 f->auto_grow_limit = FFMAX(AUTO_GROW_DEFAULT_BYTES / elem_size, 1);
73
74 49164 return f;
75 }
76
77 void av_fifo_auto_grow_limit(AVFifo *f, size_t max_elems)
78 {
79 f->auto_grow_limit = max_elems;
80 }
81
82 1 size_t av_fifo_elem_size(const AVFifo *f)
83 {
84 1 return f->elem_size;
85 }
86
87 16348257 size_t av_fifo_can_read(const AVFifo *f)
88 {
89
4/4
✓ Branch 0 taken 12331399 times.
✓ Branch 1 taken 4016858 times.
✓ Branch 2 taken 5179306 times.
✓ Branch 3 taken 7152093 times.
16348257 if (f->offset_w <= f->offset_r && !f->is_empty)
90 5179306 return f->nb_elems - f->offset_r + f->offset_w;
91 11168951 return f->offset_w - f->offset_r;
92 }
93
94 4274014 size_t av_fifo_can_write(const AVFifo *f)
95 {
96 4274014 return f->nb_elems - av_fifo_can_read(f);
97 }
98
99 871 int av_fifo_grow2(AVFifo *f, size_t inc)
100 {
101 uint8_t *tmp;
102
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 871 times.
871 if (inc > SIZE_MAX - f->nb_elems)
104 return AVERROR(EINVAL);
105
106 871 tmp = av_realloc_array(f->buffer, f->nb_elems + inc, f->elem_size);
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 871 times.
871 if (!tmp)
108 return AVERROR(ENOMEM);
109 871 f->buffer = tmp;
110
111 // move the data from the beginning of the ring buffer
112 // to the newly allocated space
113
4/4
✓ Branch 0 taken 864 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 846 times.
✓ Branch 3 taken 18 times.
871 if (f->offset_w <= f->offset_r && !f->is_empty) {
114 846 const size_t copy = FFMIN(inc, f->offset_w);
115 846 memcpy(tmp + f->nb_elems * f->elem_size, tmp, copy * f->elem_size);
116
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 795 times.
846 if (copy < f->offset_w) {
117 51 memmove(tmp, tmp + copy * f->elem_size,
118 51 (f->offset_w - copy) * f->elem_size);
119 51 f->offset_w -= copy;
120 } else
121
2/2
✓ Branch 0 taken 790 times.
✓ Branch 1 taken 5 times.
795 f->offset_w = copy == inc ? 0 : f->nb_elems + copy;
122 }
123
124 871 f->nb_elems += inc;
125
126 871 return 0;
127 }
128
129 1893710 static int fifo_check_space(AVFifo *f, size_t to_write)
130 {
131 1893710 const size_t can_write = av_fifo_can_write(f);
132
2/2
✓ Branch 0 taken 539 times.
✓ Branch 1 taken 1893171 times.
1893710 const size_t need_grow = to_write > can_write ? to_write - can_write : 0;
133 size_t can_grow;
134
135
2/2
✓ Branch 0 taken 1893171 times.
✓ Branch 1 taken 539 times.
1893710 if (!need_grow)
136 1893171 return 0;
137
138 1078 can_grow = f->auto_grow_limit > f->nb_elems ?
139
1/2
✓ Branch 0 taken 539 times.
✗ Branch 1 not taken.
539 f->auto_grow_limit - f->nb_elems : 0;
140
2/4
✓ Branch 0 taken 539 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 539 times.
✗ Branch 3 not taken.
539 if ((f->flags & AV_FIFO_FLAG_AUTO_GROW) && need_grow <= can_grow) {
141 // allocate a bit more than necessary, if we can
142
1/2
✓ Branch 0 taken 539 times.
✗ Branch 1 not taken.
539 const size_t inc = (need_grow < can_grow / 2 ) ? need_grow * 2 : can_grow;
143 539 return av_fifo_grow2(f, inc);
144 }
145
146 return AVERROR(ENOSPC);
147 }
148
149 1893710 static int fifo_write_common(AVFifo *f, const uint8_t *buf, size_t *nb_elems,
150 AVFifoCB read_cb, void *opaque)
151 {
152 1893710 size_t to_write = *nb_elems;
153 size_t offset_w;
154 1893710 int ret = 0;
155
156 1893710 ret = fifo_check_space(f, to_write);
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1893710 times.
1893710 if (ret < 0)
158 return ret;
159
160 1893710 offset_w = f->offset_w;
161
162
2/2
✓ Branch 0 taken 1888470 times.
✓ Branch 1 taken 1893678 times.
3782148 while (to_write > 0) {
163 1888470 size_t len = FFMIN(f->nb_elems - offset_w, to_write);
164 1888470 uint8_t *wptr = f->buffer + offset_w * f->elem_size;
165
166
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 1888403 times.
1888470 if (read_cb) {
167 67 ret = read_cb(opaque, wptr, &len);
168
3/4
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
✓ Branch 3 taken 32 times.
67 if (ret < 0 || len == 0)
169 break;
170 } else {
171 1888403 memcpy(wptr, buf, len * f->elem_size);
172 1888403 buf += len * f->elem_size;
173 }
174 1888438 offset_w += len;
175
2/2
✓ Branch 0 taken 250910 times.
✓ Branch 1 taken 1637528 times.
1888438 if (offset_w >= f->nb_elems)
176 250910 offset_w = 0;
177 1888438 to_write -= len;
178 }
179 1893710 f->offset_w = offset_w;
180
181
2/2
✓ Branch 0 taken 1883731 times.
✓ Branch 1 taken 9979 times.
1893710 if (*nb_elems != to_write)
182 1883731 f->is_empty = 0;
183 1893710 *nb_elems -= to_write;
184
185 1893710 return ret;
186 }
187
188 1893678 int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
189 {
190 1893678 return fifo_write_common(f, buf, &nb_elems, NULL, NULL);
191 }
192
193 32 int av_fifo_write_from_cb(AVFifo *f, AVFifoCB read_cb,
194 void *opaque, size_t *nb_elems)
195 {
196 32 return fifo_write_common(f, NULL, nb_elems, read_cb, opaque);
197 }
198
199 3416703 static int fifo_peek_common(const AVFifo *f, uint8_t *buf, size_t *nb_elems,
200 size_t offset, AVFifoCB write_cb, void *opaque)
201 {
202 3416703 size_t to_read = *nb_elems;
203 3416703 size_t offset_r = f->offset_r;
204 3416703 size_t can_read = av_fifo_can_read(f);
205 3416703 int ret = 0;
206
207
3/4
✓ Branch 0 taken 3416703 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1073950 times.
✓ Branch 3 taken 2342753 times.
3416703 if (offset > can_read || to_read > can_read - offset) {
208 1073950 *nb_elems = 0;
209 1073950 return AVERROR(EINVAL);
210 }
211
212
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2342747 times.
2342753 if (offset_r >= f->nb_elems - offset)
213 6 offset_r -= f->nb_elems - offset;
214 else
215 2342747 offset_r += offset;
216
217
2/2
✓ Branch 0 taken 2347552 times.
✓ Branch 1 taken 2342689 times.
4690241 while (to_read > 0) {
218 2347552 size_t len = FFMIN(f->nb_elems - offset_r, to_read);
219 2347552 uint8_t *rptr = f->buffer + offset_r * f->elem_size;
220
221
2/2
✓ Branch 0 taken 13033 times.
✓ Branch 1 taken 2334519 times.
2347552 if (write_cb) {
222 13033 ret = write_cb(opaque, rptr, &len);
223
3/4
✓ Branch 0 taken 13033 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12969 times.
✓ Branch 3 taken 64 times.
13033 if (ret < 0 || len == 0)
224 break;
225 } else {
226 2334519 memcpy(buf, rptr, len * f->elem_size);
227 2334519 buf += len * f->elem_size;
228 }
229 2347488 offset_r += len;
230
2/2
✓ Branch 0 taken 339390 times.
✓ Branch 1 taken 2008098 times.
2347488 if (offset_r >= f->nb_elems)
231 339390 offset_r = 0;
232 2347488 to_read -= len;
233 }
234
235 2342753 *nb_elems -= to_read;
236
237 2342753 return ret;
238 }
239
240 2858276 int av_fifo_read(AVFifo *f, void *buf, size_t nb_elems)
241 {
242 2858276 int ret = fifo_peek_common(f, buf, &nb_elems, 0, NULL, NULL);
243 2858276 av_fifo_drain2(f, nb_elems);
244 2858276 return ret;
245 }
246
247 8202 int av_fifo_read_to_cb(AVFifo *f, AVFifoCB write_cb,
248 void *opaque, size_t *nb_elems)
249 {
250 8202 int ret = fifo_peek_common(f, NULL, nb_elems, 0, write_cb, opaque);
251 8202 av_fifo_drain2(f, *nb_elems);
252 8202 return ret;
253 }
254
255 550193 int av_fifo_peek(const AVFifo *f, void *buf, size_t nb_elems, size_t offset)
256 {
257 550193 return fifo_peek_common(f, buf, &nb_elems, offset, NULL, NULL);
258 }
259
260 32 int av_fifo_peek_to_cb(const AVFifo *f, AVFifoCB write_cb, void *opaque,
261 size_t *nb_elems, size_t offset)
262 {
263 32 return fifo_peek_common(f, NULL, nb_elems, offset, write_cb, opaque);
264 }
265
266 2929716 void av_fifo_drain2(AVFifo *f, size_t size)
267 {
268 2929716 const size_t cur_size = av_fifo_can_read(f);
269
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2929716 times.
2929716 av_assert0(cur_size >= size);
271
2/2
✓ Branch 0 taken 2071763 times.
✓ Branch 1 taken 857953 times.
2929716 if (cur_size == size)
272 2071763 f->is_empty = 1;
273
274
2/2
✓ Branch 0 taken 250119 times.
✓ Branch 1 taken 2679597 times.
2929716 if (f->offset_r >= f->nb_elems - size)
275 250119 f->offset_r -= f->nb_elems - size;
276 else
277 2679597 f->offset_r += size;
278 2929716 }
279
280 void av_fifo_reset2(AVFifo *f)
281 {
282 f->offset_r = f->offset_w = 0;
283 f->is_empty = 1;
284 }
285
286 49442 void av_fifo_freep2(AVFifo **f)
287 {
288
2/2
✓ Branch 0 taken 49164 times.
✓ Branch 1 taken 278 times.
49442 if (*f) {
289 49164 av_freep(&(*f)->buffer);
290 49164 av_freep(f);
291 }
292 49442 }
293
294
295 #if FF_API_FIFO_OLD_API
296 #include "internal.h"
297 FF_DISABLE_DEPRECATION_WARNINGS
298 #define OLD_FIFO_SIZE_MAX (size_t)FFMIN3(INT_MAX, UINT32_MAX, SIZE_MAX)
299
300 AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size)
301 {
302 AVFifoBuffer *f;
303 void *buffer;
304
305 if (nmemb > OLD_FIFO_SIZE_MAX / size)
306 return NULL;
307
308 buffer = av_realloc_array(NULL, nmemb, size);
309 if (!buffer)
310 return NULL;
311 f = av_mallocz(sizeof(AVFifoBuffer));
312 if (!f) {
313 av_free(buffer);
314 return NULL;
315 }
316 f->buffer = buffer;
317 f->end = f->buffer + nmemb * size;
318 av_fifo_reset(f);
319 return f;
320 }
321
322 AVFifoBuffer *av_fifo_alloc(unsigned int size)
323 {
324 return av_fifo_alloc_array(size, 1);
325 }
326
327 void av_fifo_free(AVFifoBuffer *f)
328 {
329 if (f) {
330 av_freep(&f->buffer);
331 av_free(f);
332 }
333 }
334
335 void av_fifo_freep(AVFifoBuffer **f)
336 {
337 if (f) {
338 av_fifo_free(*f);
339 *f = NULL;
340 }
341 }
342
343 void av_fifo_reset(AVFifoBuffer *f)
344 {
345 f->wptr = f->rptr = f->buffer;
346 f->wndx = f->rndx = 0;
347 }
348
349 int av_fifo_size(const AVFifoBuffer *f)
350 {
351 return (uint32_t)(f->wndx - f->rndx);
352 }
353
354 int av_fifo_space(const AVFifoBuffer *f)
355 {
356 return f->end - f->buffer - av_fifo_size(f);
357 }
358
359 int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
360 {
361 unsigned int old_size = f->end - f->buffer;
362
363 if (new_size > OLD_FIFO_SIZE_MAX)
364 return AVERROR(EINVAL);
365
366 if (old_size < new_size) {
367 size_t offset_r = f->rptr - f->buffer;
368 size_t offset_w = f->wptr - f->buffer;
369 uint8_t *tmp;
370
371 tmp = av_realloc(f->buffer, new_size);
372 if (!tmp)
373 return AVERROR(ENOMEM);
374
375 // move the data from the beginning of the ring buffer
376 // to the newly allocated space
377 // the second condition distinguishes full vs empty fifo
378 if (offset_w <= offset_r && av_fifo_size(f)) {
379 const size_t copy = FFMIN(new_size - old_size, offset_w);
380 memcpy(tmp + old_size, tmp, copy);
381 if (copy < offset_w) {
382 memmove(tmp, tmp + copy , offset_w - copy);
383 offset_w -= copy;
384 } else
385 offset_w = old_size + copy;
386 }
387
388 f->buffer = tmp;
389 f->end = f->buffer + new_size;
390 f->rptr = f->buffer + offset_r;
391 f->wptr = f->buffer + offset_w;
392 }
393 return 0;
394 }
395
396 int av_fifo_grow(AVFifoBuffer *f, unsigned int size)
397 {
398 unsigned int old_size = f->end - f->buffer;
399 if(size + (unsigned)av_fifo_size(f) < size)
400 return AVERROR(EINVAL);
401
402 size += av_fifo_size(f);
403
404 if (old_size < size)
405 return av_fifo_realloc2(f, FFMAX(size, 2*old_size));
406 return 0;
407 }
408
409 /* src must NOT be const as it can be a context for func that may need
410 * updating (like a pointer or byte counter) */
411 int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size,
412 int (*func)(void *, void *, int))
413 {
414 int total = size;
415 uint32_t wndx= f->wndx;
416 uint8_t *wptr= f->wptr;
417
418 if (size > av_fifo_space(f))
419 return AVERROR(ENOSPC);
420
421 do {
422 int len = FFMIN(f->end - wptr, size);
423 if (func) {
424 len = func(src, wptr, len);
425 if (len <= 0)
426 break;
427 } else {
428 memcpy(wptr, src, len);
429 src = (uint8_t *)src + len;
430 }
431 wptr += len;
432 if (wptr >= f->end)
433 wptr = f->buffer;
434 wndx += len;
435 size -= len;
436 } while (size > 0);
437 f->wndx= wndx;
438 f->wptr= wptr;
439 return total - size;
440 }
441
442 int av_fifo_generic_peek_at(AVFifoBuffer *f, void *dest, int offset, int buf_size, void (*func)(void*, void*, int))
443 {
444 uint8_t *rptr = f->rptr;
445
446 if (offset < 0 || buf_size > av_fifo_size(f) - offset)
447 return AVERROR(EINVAL);
448
449 if (offset >= f->end - rptr)
450 rptr += offset - (f->end - f->buffer);
451 else
452 rptr += offset;
453
454 while (buf_size > 0) {
455 int len;
456
457 if (rptr >= f->end)
458 rptr -= f->end - f->buffer;
459
460 len = FFMIN(f->end - rptr, buf_size);
461 if (func)
462 func(dest, rptr, len);
463 else {
464 memcpy(dest, rptr, len);
465 dest = (uint8_t *)dest + len;
466 }
467
468 buf_size -= len;
469 rptr += len;
470 }
471
472 return 0;
473 }
474
475 int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size,
476 void (*func)(void *, void *, int))
477 {
478 return av_fifo_generic_peek_at(f, dest, 0, buf_size, func);
479 }
480
481 int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size,
482 void (*func)(void *, void *, int))
483 {
484 if (buf_size > av_fifo_size(f))
485 return AVERROR(EINVAL);
486
487 do {
488 int len = FFMIN(f->end - f->rptr, buf_size);
489 if (func)
490 func(dest, f->rptr, len);
491 else {
492 memcpy(dest, f->rptr, len);
493 dest = (uint8_t *)dest + len;
494 }
495 av_fifo_drain(f, len);
496 buf_size -= len;
497 } while (buf_size > 0);
498 return 0;
499 }
500
501 /** Discard data from the FIFO. */
502 void av_fifo_drain(AVFifoBuffer *f, int size)
503 {
504 av_assert2(av_fifo_size(f) >= size);
505 f->rptr += size;
506 if (f->rptr >= f->end)
507 f->rptr -= f->end - f->buffer;
508 f->rndx += size;
509 }
510 FF_ENABLE_DEPRECATION_WARNINGS
511 #endif
512