[5/5] movenc: Use avio_reset_dyn_buf for fragment data payloads

Message ID 1370205623-45186-5-git-send-email-martin@martin.st
State New
Headers show

Commit Message

Martin Storsjö June 2, 2013, 8:40 p.m.
From: Andrey Semashev <andysem@mail.ru>

This avoids reallocations for subsequent fragments (when all
fragments are roughly the same size).
---
 libavformat/movenc.c |   12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

Patch

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index b702805..c229633 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -2786,11 +2786,9 @@  static int mov_flush_fragment(AVFormatContext *s)
         track->entry = 0;
         if (!track->mdat_buf)
             continue;
-        buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
-        track->mdat_buf = NULL;
+        buf_size = avio_reset_dyn_buf(track->mdat_buf, &buf);
 
         avio_write(s->pb, buf, buf_size);
-        av_free(buf);
     }
 
     mov->mdat_size = 0;
@@ -3222,7 +3220,15 @@  static int mov_write_trailer(AVFormatContext *s)
 
         mov_write_moov_tag(pb, mov, s);
     } else {
+        uint8_t *buf;
         mov_flush_fragment(s);
+        for (i = 0; i < mov->nb_streams; ++i) {
+            if (mov->tracks[i].mdat_buf) {
+                avio_close_dyn_buf(mov->tracks[i].mdat_buf, &buf);
+                mov->tracks[i].mdat_buf = NULL;
+                av_free(buf);
+            }
+        }
         mov_write_mfra_tag(pb, mov);
     }