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

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Thu Jul 9 13:35:40 EEST 2020


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 at gmail.com>
---
 libavformat/sdp.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

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;
 }
 
-- 
2.20.1



More information about the ffmpeg-devel mailing list