diff mbox series

[FFmpeg-devel,5/7] avformat/sdp: Avoid allocation for small HEVC annex B extradata

Message ID 20200709103542.19909-5-andreas.rheinhardt@gmail.com
State New
Headers show
Series [FFmpeg-devel,1/7] avformat/avc: Fix undefined shift and assert when reading exp-golomb num
Related show

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Andreas Rheinhardt July 9, 2020, 10:35 a.m. UTC
In this case, extradata2psets_hevc would have used avio_open_dyn_buf() +
avio_close_dyn_buf() to convert the annex B extradata to the hvcc format
(which is easier parseable); the temporary buffer would then be freed.
avio_close_dyn_buf() + av_free() can be replaced by avio_get_dyn_buf() +
ffio_free_dyn_buf(). This saves an allocation and a memcpy if the hvcc
is so small that it fits into the dynamic buffer's write buffer.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavformat/sdp.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/libavformat/sdp.c b/libavformat/sdp.c
index 2ce1a62262..3acbf5d197 100644
--- a/libavformat/sdp.c
+++ b/libavformat/sdp.c
@@ -27,6 +27,7 @@ 
 #include "libavcodec/xiph.h"
 #include "libavcodec/mpeg4audio.h"
 #include "avformat.h"
+#include "avio_internal.h"
 #include "internal.h"
 #include "avc.h"
 #include "hevc.h"
@@ -228,7 +229,7 @@  static char *extradata2psets_hevc(AVCodecParameters *par)
     char *psets;
     uint8_t *extradata = par->extradata;
     int extradata_size = par->extradata_size;
-    uint8_t *tmpbuf = NULL;
+    AVIOContext *pb = NULL;
     int ps_pos[3] = { 0 };
     static const char * const ps_names[3] = { "vps", "sps", "pps" };
     int num_arrays, num_nalus;
@@ -239,15 +240,12 @@  static char *extradata2psets_hevc(AVCodecParameters *par)
     // other anyway, we get away with a little less work by using the hvcc
     // format.
     if (par->extradata[0] != 1) {
-        AVIOContext *pb;
         if (avio_open_dyn_buf(&pb) < 0)
             return NULL;
         if (ff_isom_write_hvcc(pb, par->extradata, par->extradata_size, 0) < 0) {
-            avio_close_dyn_buf(pb, &tmpbuf);
             goto err;
         }
-        extradata_size = avio_close_dyn_buf(pb, &extradata);
-        tmpbuf = extradata;
+        extradata_size = avio_get_dyn_buf(pb, &extradata);
     }
 
     if (extradata_size < 23)
@@ -315,12 +313,12 @@  static char *extradata2psets_hevc(AVCodecParameters *par)
             pos += len;
         }
     }
-    av_free(tmpbuf);
+    ffio_free_dyn_buf(&pb);
 
     return psets;
 
 err:
-    av_free(tmpbuf);
+    ffio_free_dyn_buf(&pb);
     return NULL;
 }