[FFmpeg-devel] [PATCH 13/20] avformat/aviobuf: Add function to reset dynamic buffer

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Wed Jan 1 02:58:30 EET 2020


Resetting a dynamic buffer means to keep the AVIOContext and the
internal buffer used by the dynamic buffer. This is done in order to
save (re)allocations when one has a workflow where one opens and closes
dynamic buffers in sequence.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
This function might also be beneficial in the mpegts muxer when
converting AAC to ADTS (currently a new dynamic buffer is opened for
every packet) or in the nut muxer. Besides the application in this very
patchset, there is another way that this can be used in the Matroska
muxer: By not reallocating the buffer used for writing packets one can
improve speed on platforms where realloc is slow (namely Windows). A
patch for this comes soon.

 libavformat/avio_internal.h |  7 +++++++
 libavformat/aviobuf.c       | 11 +++++++++++
 2 files changed, 18 insertions(+)

diff --git a/libavformat/avio_internal.h b/libavformat/avio_internal.h
index eb628ac493..c575df8035 100644
--- a/libavformat/avio_internal.h
+++ b/libavformat/avio_internal.h
@@ -171,6 +171,13 @@ int ffio_open_whitelist(AVIOContext **s, const char *url, int flags,
  */
 int ffio_close_null_buf(AVIOContext *s);
 
+/**
+ * Reset a dynamic buffer.
+ *
+ * Resets everything, but keeps the allocated buffer for later use.
+ */
+void ffio_reset_dyn_buf(AVIOContext *s);
+
 /**
  * Free a dynamic buffer.
  *
diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
index 70e1d2ca10..d44ee0fa35 100644
--- a/libavformat/aviobuf.c
+++ b/libavformat/aviobuf.c
@@ -1461,6 +1461,17 @@ int avio_get_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
     return d->size;
 }
 
+void ffio_reset_dyn_buf(AVIOContext *s)
+{
+    DynBuffer *d = s->opaque;
+    int max_packet_size = s->max_packet_size;
+
+    ffio_init_context(s, d->io_buffer, d->io_buffer_size, 1, d, NULL,
+                      s->write_packet, s->seek);
+    s->max_packet_size = max_packet_size;
+    d->pos = d->size = 0;
+}
+
 int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
 {
     DynBuffer *d;
-- 
2.20.1



More information about the ffmpeg-devel mailing list