[FFmpeg-devel] [PATCH 7/8] avutil/fifo: Simplify growing FIFO

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Tue Jul 5 23:26:49 EEST 2022


In case the data in the FIFO currently wraps around,
move the data from the end of the old buffer to the end
of the new buffer instead of moving the data from the start
of the old buffer partially to the end of the new buffer
and partially to the start of the new buffer.
This simplifies the code.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavutil/fifo.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/libavutil/fifo.c b/libavutil/fifo.c
index 51a5af6f39..53359a2112 100644
--- a/libavutil/fifo.c
+++ b/libavutil/fifo.c
@@ -108,17 +108,12 @@ int av_fifo_grow2(AVFifo *f, size_t inc)
         return AVERROR(ENOMEM);
     f->buffer = tmp;
 
-    // move the data from the beginning of the ring buffer
-    // to the newly allocated space
+    // move the data from the end of the ring buffer
+    // to the end of the newly allocated space
     if (f->offset_w <= f->offset_r && !f->is_empty) {
-        const size_t copy = FFMIN(inc, f->offset_w);
-        memcpy(tmp + f->nb_elems * f->elem_size, tmp, copy * f->elem_size);
-        if (copy < f->offset_w) {
-            memmove(tmp, tmp + copy * f->elem_size,
-                    (f->offset_w - copy) * f->elem_size);
-            f->offset_w -= copy;
-        } else
-            f->offset_w = copy == inc ? 0 : f->nb_elems + copy;
+        memmove(tmp + (f->offset_r + inc) * f->elem_size, tmp + f->offset_r * f->elem_size,
+                (f->nb_elems - f->offset_r) * f->elem_size);
+        f->offset_r += inc;
     }
 
     f->nb_elems += inc;
-- 
2.34.1



More information about the ffmpeg-devel mailing list