[FFmpeg-devel] [PATCH 7/9] avformat/aviobuf: do not allocate a new buffer in ffio_ensure_seekback if not needed

Marton Balint cus at passwd.hu
Wed Sep 30 00:10:19 EEST 2020


Let's move unread data to the start of the old buffer instead.

Signed-off-by: Marton Balint <cus at passwd.hu>
---
 libavformat/aviobuf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/libavformat/aviobuf.c b/libavformat/aviobuf.c
index 75ddf52efc..2f1abe7d81 100644
--- a/libavformat/aviobuf.c
+++ b/libavformat/aviobuf.c
@@ -1015,7 +1015,10 @@ int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
         return 0;
     av_assert0(!s->write_flag);
 
-    buf_size = FFMAX(buf_size, s->buffer_size);
+    if (buf_size <= s->buffer_size) {
+        update_checksum(s);
+        memmove(s->buffer, s->buf_ptr, filled);
+    } else {
     buffer = av_malloc(buf_size);
     if (!buffer)
         return AVERROR(ENOMEM);
@@ -1024,6 +1027,7 @@ int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
     av_free(s->buffer);
     s->buffer = buffer;
     s->buffer_size = buf_size;
+    }
     s->buf_ptr = s->buffer;
     s->buf_end = s->buffer + filled;
     s->checksum_ptr = s->buffer;
-- 
2.26.2



More information about the ffmpeg-devel mailing list