[FFmpeg-cvslog] avcodec/extract_extradata: don't uninitialize the H2645Packet on every processed packet

James Almer git at videolan.org
Wed Feb 20 18:58:15 EET 2019


ffmpeg | branch: master | James Almer <jamrial at gmail.com> | Sat Mar 10 19:57:35 2018 -0300| [3365e91b1ed82f225aaa15c5c27504873f4b8c7a] | committer: Luca Barbato

avcodec/extract_extradata: don't uninitialize the H2645Packet on every processed packet

Based on hevc_parser code. This prevents repeated unnecessary allocations
and frees on every packet processed by the bsf.

Reviewed-by: Jun Zhao <mypopydev at gmail.com>
Signed-off-by: James Almer <jamrial at gmail.com>
Signed-off-by: Luca Barbato <lu_zero at gentoo.org>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=3365e91b1ed82f225aaa15c5c27504873f4b8c7a
---

 libavcodec/extract_extradata_bsf.c | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)

diff --git a/libavcodec/extract_extradata_bsf.c b/libavcodec/extract_extradata_bsf.c
index d2773ce87a..f631331766 100644
--- a/libavcodec/extract_extradata_bsf.c
+++ b/libavcodec/extract_extradata_bsf.c
@@ -36,6 +36,9 @@ typedef struct ExtractExtradataContext {
     int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
                    uint8_t **data, int *size);
 
+    /* H264/HEVC specifc fields */
+    H2645Packet h2645_pkt;
+
     /* AVOptions */
     int remove;
 } ExtractExtradataContext;
@@ -61,7 +64,6 @@ static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
 
     ExtractExtradataContext *s = ctx->priv_data;
 
-    H2645Packet h2645_pkt = { 0 };
     int extradata_size = 0, filtered_size = 0;
     const int *extradata_nal_types;
     int nb_extradata_nal_types;
@@ -75,13 +77,13 @@ static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
         nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_h264);
     }
 
-    ret = ff_h2645_packet_split(&h2645_pkt, pkt->data, pkt->size,
+    ret = ff_h2645_packet_split(&s->h2645_pkt, pkt->data, pkt->size,
                                 ctx, 0, 0, ctx->par_in->codec_id);
     if (ret < 0)
-        goto fail;
+        return ret;
 
-    for (i = 0; i < h2645_pkt.nb_nals; i++) {
-        H2645NAL *nal = &h2645_pkt.nals[i];
+    for (i = 0; i < s->h2645_pkt.nb_nals; i++) {
+        H2645NAL *nal = &s->h2645_pkt.nals[i];
         if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type)) {
             extradata_size += nal->raw_size + 3;
             if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
@@ -104,8 +106,7 @@ static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
         if (s->remove) {
             filtered_buf = av_buffer_alloc(filtered_size + AV_INPUT_BUFFER_PADDING_SIZE);
             if (!filtered_buf) {
-                ret = AVERROR(ENOMEM);
-                goto fail;
+                return AVERROR(ENOMEM);
             }
             memset(filtered_buf->data + filtered_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
 
@@ -115,16 +116,15 @@ static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
         extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
         if (!extradata) {
             av_buffer_unref(&filtered_buf);
-            ret = AVERROR(ENOMEM);
-            goto fail;
+            return AVERROR(ENOMEM);
         }
         memset(extradata + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
 
         *data = extradata;
         *size = extradata_size;
 
-        for (i = 0; i < h2645_pkt.nb_nals; i++) {
-            H2645NAL *nal = &h2645_pkt.nals[i];
+        for (i = 0; i < s->h2645_pkt.nb_nals; i++) {
+            H2645NAL *nal = &s->h2645_pkt.nals[i];
             if (val_in_array(extradata_nal_types, nb_extradata_nal_types,
                              nal->type)) {
                 AV_WB24(extradata, 1); // startcode
@@ -145,9 +145,7 @@ static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
         }
     }
 
-fail:
-    ff_h2645_packet_uninit(&h2645_pkt);
-    return ret;
+    return 0;
 }
 
 static int extract_extradata_vc1(AVBSFContext *ctx, AVPacket *pkt,
@@ -283,6 +281,12 @@ fail:
     return ret;
 }
 
+static void extract_extradata_close(AVBSFContext *ctx)
+{
+    ExtractExtradataContext *s = ctx->priv_data;
+    ff_h2645_packet_uninit(&s->h2645_pkt);
+}
+
 static const enum AVCodecID codec_ids[] = {
     AV_CODEC_ID_CAVS,
     AV_CODEC_ID_H264,
@@ -315,4 +319,5 @@ const AVBitStreamFilter ff_extract_extradata_bsf = {
     .priv_class     = &extract_extradata_class,
     .init           = extract_extradata_init,
     .filter         = extract_extradata_filter,
+    .close          = extract_extradata_close,
 };



More information about the ffmpeg-cvslog mailing list