[FFmpeg-devel] [PATCH 1/4] avformat/aviobuf: Avoid allocation when using dynamic buffer

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Wed Aug 4 19:21:40 EEST 2021


This can be achieved by allocating the AVIOContext and
the dynamic buffer's opaque and internal write buffer together.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 libavformat/aviobuf.c | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
index 66fddf1433..e92b3dc610 100644
--- a/libavformat/aviobuf.c
+++ b/libavformat/aviobuf.c
@@ -1339,22 +1339,21 @@ static int64_t dyn_buf_seek(void *opaque, int64_t offset, int whence)
 
 static int url_open_dyn_buf_internal(AVIOContext **s, int max_packet_size)
 {
+    struct { AVIOContext pb; DynBuffer d; } *ret;
     DynBuffer *d;
     unsigned io_buffer_size = max_packet_size ? max_packet_size : 1024;
 
-    if (sizeof(DynBuffer) + io_buffer_size < io_buffer_size)
+    if (sizeof(*ret) + io_buffer_size < io_buffer_size)
         return AVERROR(ERANGE);
-    d = av_mallocz(sizeof(DynBuffer) + io_buffer_size);
-    if (!d)
+    ret = av_mallocz(sizeof(*ret) + io_buffer_size);
+    if (!ret)
         return AVERROR(ENOMEM);
+    d = &ret->d;
     d->io_buffer_size = io_buffer_size;
-    *s = avio_alloc_context(d->io_buffer, d->io_buffer_size, 1, d, NULL,
-                            max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
-                            max_packet_size ? NULL : dyn_buf_seek);
-    if(!*s) {
-        av_free(d);
-        return AVERROR(ENOMEM);
-    }
+    ffio_init_context(&ret->pb, d->io_buffer, d->io_buffer_size, 1, d, NULL,
+                      max_packet_size ? dyn_packet_buf_write : dyn_buf_write,
+                      max_packet_size ? NULL : dyn_buf_seek);
+    *s = &ret->pb;
     (*s)->max_packet_size = max_packet_size;
     return 0;
 }
@@ -1427,7 +1426,6 @@ int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
     d = s->opaque;
     *pbuffer = d->buffer;
     size = d->size;
-    av_free(d);
 
     avio_context_free(&s);
 
@@ -1443,7 +1441,6 @@ void ffio_free_dyn_buf(AVIOContext **s)
 
     d = (*s)->opaque;
     av_free(d->buffer);
-    av_free(d);
     avio_context_free(s);
 }
 
@@ -1475,7 +1472,6 @@ int ffio_close_null_buf(AVIOContext *s)
     avio_flush(s);
 
     size = d->size;
-    av_free(d);
 
     avio_context_free(&s);
 
-- 
2.30.2



More information about the ffmpeg-devel mailing list