[FFmpeg-devel] [PATCH 09/35] lavu/fifo: add a flag for automatically growing the FIFO as needed
Anton Khirnov
anton at khirnov.net
Tue Jan 11 22:45:44 EET 2022
This will not increase the FIFO beyond 1MB, unless the caller explicitly
specifies otherwise.
---
doc/APIchanges | 3 ++-
libavutil/fifo.c | 41 +++++++++++++++++++++++++++++++++++++++--
libavutil/fifo.h | 15 ++++++++++++++-
3 files changed, 55 insertions(+), 4 deletions(-)
diff --git a/doc/APIchanges b/doc/APIchanges
index f64759d69d..52b42762ea 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -20,7 +20,8 @@ API changes, most recent first:
rather than bytes.
Add av_fifo_elem_size(), av_fifo_can_read(), av_fifo_can_write(),
av_fifo_grow2(), av_fifo_drain2(), av_fifo_write(), av_fifo_write_from_cb(),
- av_fifo_read(), av_fifo_read_to_cb(), av_fifo_peek(), av_fifo_peek_to_cb().
+ av_fifo_read(), av_fifo_read_to_cb(), av_fifo_peek(), av_fifo_peek_to_cb(),
+ av_fifo_auto_grow_limit().
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 ea944bc936..2c15df5d5c 100644
--- a/libavutil/fifo.c
+++ b/libavutil/fifo.c
@@ -34,6 +34,9 @@
# define CTX_STRUCT_NAME AVFifoBuffer
#endif
+// by default the FIFO can be auto-grown to 1MB
+#define AUTO_GROW_DEFAULT_BYTES (1024 * 1024)
+
typedef struct CTX_STRUCT_NAME {
// These fields must match then contents of AVFifoBuffer in fifo.h
// until FF_API_FIFO_PUBLIC is removed
@@ -48,6 +51,9 @@ typedef struct CTX_STRUCT_NAME {
size_t offset_r, offset_w;
// distinguishes the ambigous situation offset_r == offset_w
int is_empty;
+
+ unsigned int flags;
+ size_t auto_grow_limit;
} FifoBuffer;
AVFifoBuffer *av_fifo_alloc2(size_t nb_elems, size_t elem_size,
@@ -75,10 +81,19 @@ AVFifoBuffer *av_fifo_alloc2(size_t nb_elems, size_t elem_size,
f->nb_elems = nb_elems;
f->elem_size = elem_size;
+ f->flags = flags;
+ f->auto_grow_limit = FFMAX(AUTO_GROW_DEFAULT_BYTES / elem_size, 1);
+
av_fifo_reset((AVFifoBuffer*)f);
return (AVFifoBuffer*)f;
}
+void av_fifo_auto_grow_limit(AVFifoBuffer *f, size_t max_elems)
+{
+ FifoBuffer *fb = (FifoBuffer*)f;
+ fb->auto_grow_limit = max_elems;
+}
+
AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size)
{
if (nmemb > SIZE_MAX / size)
@@ -207,6 +222,27 @@ void av_fifo_drain2(AVFifoBuffer *f, size_t size)
#endif
}
+static int fifo_check_space(AVFifoBuffer *f, size_t to_write)
+{
+ FifoBuffer *fb = (FifoBuffer*)f;
+ const size_t can_write = av_fifo_can_write(f);
+ const size_t need_grow = to_write > can_write ? to_write - can_write : 0;
+ size_t can_grow;
+
+ if (!need_grow)
+ return 0;
+
+ can_grow = fb->auto_grow_limit > fb->nb_elems ?
+ fb->auto_grow_limit - fb->nb_elems : 0;
+ if ((fb->flags & AV_FIFO_FLAG_AUTO_GROW) && need_grow <= can_grow) {
+ // allocate a bit more than necessary, if we can
+ const size_t inc = (need_grow < can_grow / 2 ) ? need_grow * 2 : can_grow;
+ return av_fifo_grow2(f, inc);
+ }
+
+ return AVERROR(ENOSPC);
+}
+
static int fifo_write_common(AVFifoBuffer *f, const uint8_t *buf, size_t *nb_elems,
AVFifoCB read_cb, void *opaque)
{
@@ -218,8 +254,9 @@ static int fifo_write_common(AVFifoBuffer *f, const uint8_t *buf, size_t *nb_ele
uint32_t wndx= f->wndx;
#endif
- if (to_write > av_fifo_can_write(f))
- return AVERROR(ENOSPC);
+ ret = fifo_check_space(f, to_write);
+ if (ret < 0)
+ return ret;
do {
size_t len = FFMIN(fb->nb_elems - offset_w, to_write);
diff --git a/libavutil/fifo.h b/libavutil/fifo.h
index c7be5e8f7d..11eb36944a 100644
--- a/libavutil/fifo.h
+++ b/libavutil/fifo.h
@@ -83,6 +83,13 @@ AVFifoBuffer *av_fifo_alloc(unsigned int size);
*/
AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size);
+/**
+ * Automatically resize the FIFO on writes, so that the data fits. This
+ * automatic resizing happens up to a limit that can be modified with
+ * av_fifo_auto_grow_limit().
+ */
+#define AV_FIFO_FLAG_AUTO_GROW (1 << 0)
+
/**
* Allocate and initialize an AVFifoBuffer with a given element size.
*
@@ -90,7 +97,7 @@ AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size);
* @param nb_elems initial number of elements that can be stored in the FIFO
* @param elem_size Size in bytes of a single element. Further operations on
* the returned FIFO will implicitly use this element size.
- * @param flags currently unused, must be 0
+ * @param flags a combination of AV_FIFO_FLAG_*
*
* @return newly-allocated AVFifoBuffer on success, a negative error code on failure
*/
@@ -115,6 +122,12 @@ void av_fifo_freep(AVFifoBuffer **f);
*/
size_t av_fifo_elem_size(const AVFifoBuffer *f);
+/**
+ * Set the maximum size (in elements) to which the FIFO can be resized
+ * automatically. Has no effect unless AV_FIFO_FLAG_AUTO_GROW is used.
+ */
+void av_fifo_auto_grow_limit(AVFifoBuffer *f, size_t max_elems);
+
/**
* Reset the AVFifoBuffer to the state right after av_fifo_alloc, in particular it is emptied.
* @param f AVFifoBuffer to reset
--
2.33.0
More information about the ffmpeg-devel
mailing list