diff mbox series

[FFmpeg-devel,06/35] lavu/fifo: add a new function for draining the FIFO

Message ID 20220111204610.14262-6-anton@khirnov.net
State New
Headers show
Series [FFmpeg-devel,01/35] lavu/fifo: disallow overly large fifo sizes | expand

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

Commit Message

Anton Khirnov Jan. 11, 2022, 8:45 p.m. UTC
Consistently use size_t for sizes
---
 doc/APIchanges   |  2 +-
 libavutil/fifo.c | 36 +++++++++++++++++++++---------------
 libavutil/fifo.h |  7 +++++++
 3 files changed, 29 insertions(+), 16 deletions(-)
diff mbox series

Patch

diff --git a/doc/APIchanges b/doc/APIchanges
index 98eae55719..f2769d4165 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -19,7 +19,7 @@  API changes, most recent first:
   Operations on FIFOs created with this function on these elements
   rather than bytes.
   Add av_fifo_elem_size(), av_fifo_can_read(), av_fifo_can_write(),
-  av_fifo_grow2().
+  av_fifo_grow2(), av_fifo_drain2().
 
 2022-01-xx - xxxxxxxxxx - lavu fifo.h
   Access to all AVFifoBuffer members is deprecated. The struct will
diff --git a/libavutil/fifo.c b/libavutil/fifo.c
index 28d6d1b2e6..e9f439e219 100644
--- a/libavutil/fifo.c
+++ b/libavutil/fifo.c
@@ -187,6 +187,26 @@  int av_fifo_grow2(AVFifoBuffer *f, size_t inc)
     return 0;
 }
 
+void av_fifo_drain2(AVFifoBuffer *f, size_t size)
+{
+    FifoBuffer *fb = (FifoBuffer*)f;
+    const size_t cur_size = av_fifo_can_read(f);
+
+    av_assert0(cur_size >= size);
+    if (cur_size == size)
+        fb->is_empty = 1;
+
+    if (fb->offset_r >= fb->nb_elems - size)
+        fb->offset_r -= fb->nb_elems - size;
+    else
+        fb->offset_r += size;
+
+#if FF_API_FIFO_PUBLIC
+    f->rptr  = f->buffer + fb->offset_r * fb->elem_size;
+    f->rndx += size;
+#endif
+}
+
 int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
 {
     FifoBuffer *fb = (FifoBuffer*)f;
@@ -343,19 +363,5 @@  int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size,
 /** Discard data from the FIFO. */
 void av_fifo_drain(AVFifoBuffer *f, int size)
 {
-    FifoBuffer *fb = (FifoBuffer*)f;
-    const size_t cur_size = av_fifo_size(f);
-
-    av_assert2(cur_size >= size);
-    if (cur_size == size)
-        fb->is_empty = 1;
-
-    if (fb->offset_r >= fb->nb_elems - size)
-        fb->offset_r -= fb->nb_elems - size;
-    else
-        fb->offset_r += size;
-#if FF_API_FIFO_PUBLIC
-    f->rptr  = f->buffer + fb->offset_r * fb->elem_size;
-    f->rndx += size;
-#endif
+    return av_fifo_drain2(f, size);
 }
diff --git a/libavutil/fifo.h b/libavutil/fifo.h
index 375d0d133b..d593304edb 100644
--- a/libavutil/fifo.h
+++ b/libavutil/fifo.h
@@ -143,6 +143,13 @@  size_t av_fifo_can_write(const AVFifoBuffer *f);
  */
 int av_fifo_grow2(AVFifoBuffer *f, size_t inc);
 
+/**
+ * Discard the specified amount of data from an AVFifoBuffer.
+ * @param size number of elements to discard, MUST NOT be larger than
+ *             av_fifo_can_read(f)
+ */
+void av_fifo_drain2(AVFifoBuffer *f, size_t size);
+
 /**
  * Feed data at specific position from an AVFifoBuffer to a user-supplied callback.
  * Similar as av_fifo_gereric_read but without discarding data.