diff mbox series

[FFmpeg-devel] avutil/fifo: Use av_fifo_generic_peek_at() for av_fifo_generic_peek()

Message ID AM7PR03MB6660C9DCD5E3B7F8BF9EAA818F539@AM7PR03MB6660.eurprd03.prod.outlook.com
State New
Headers show
Series [FFmpeg-devel] avutil/fifo: Use av_fifo_generic_peek_at() for av_fifo_generic_peek()
Related show

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished
andriy/make_aarch64_jetson success Make finished
andriy/make_fate_aarch64_jetson success Make fate finished
andriy/make_armv7_RPi4 success Make finished
andriy/make_fate_armv7_RPi4 success Make fate finished

Commit Message

Andreas Rheinhardt Jan. 13, 2022, 4:39 p.m. UTC
Avoids code duplication. It furthermore properly checks
for buf_size to be > 0 before doing anything.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
It also avoids having to update yet another function when
the FIFO implementation is updated.
I pondered changing av_fifo_generic_read() to

int ret = av_fifo_generic_peek();
if (ret < 0)
    return ret;
av_fifo_drain(f, buf_size);
return 0;

but decided against it because the current implementation already
drains the FIFO after each call to the read function, so that
the user may already see the FIFO internals updated on the second
call to the read function.
Of course, one could use a common backend for all three functions.

 libavutil/fifo.c | 21 +--------------------
 1 file changed, 1 insertion(+), 20 deletions(-)
diff mbox series

Patch

diff --git a/libavutil/fifo.c b/libavutil/fifo.c
index d741bdd395..e1f2175530 100644
--- a/libavutil/fifo.c
+++ b/libavutil/fifo.c
@@ -194,26 +194,7 @@  int av_fifo_generic_peek_at(AVFifoBuffer *f, void *dest, int offset, int buf_siz
 int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size,
                          void (*func)(void *, void *, int))
 {
-    uint8_t *rptr = f->rptr;
-
-    if (buf_size > av_fifo_size(f))
-        return AVERROR(EINVAL);
-
-    do {
-        int len = FFMIN(f->end - rptr, buf_size);
-        if (func)
-            func(dest, rptr, len);
-        else {
-            memcpy(dest, rptr, len);
-            dest = (uint8_t *)dest + len;
-        }
-        rptr += len;
-        if (rptr >= f->end)
-            rptr -= f->end - f->buffer;
-        buf_size -= len;
-    } while (buf_size > 0);
-
-    return 0;
+    return av_fifo_generic_peek_at(f, dest, 0, buf_size, func);
 }
 
 int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size,