diff mbox series

[FFmpeg-devel] avformat/utils: Avoid duplicating extradata from extract_extradata BSF

Message ID 20200907135558.10039-1-andreas.rheinhardt@gmail.com
State Accepted
Commit 5fc3eda8de40a7fb19a8052dde39325eaad38ba5
Headers show
Series [FFmpeg-devel] avformat/utils: Avoid duplicating extradata from extract_extradata BSF | expand

Checks

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

Commit Message

Andreas Rheinhardt Sept. 7, 2020, 1:55 p.m. UTC
Instead move the extradata contained in packet side-data to its
destination. This is possible because the side data already has zeroed
padding.

Notice that the check for FF_MAX_EXTRADATA_SIZE has been dropped,
because said constant is from libavcodec/internal.h. If libavcodec
wanted to enforce this, it should do so in the extract_extradata BSF
instead.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavformat/utils.c | 23 ++++++++---------------
 1 file changed, 8 insertions(+), 15 deletions(-)

Comments

Andreas Rheinhardt Sept. 10, 2020, 10:41 a.m. UTC | #1
Andreas Rheinhardt:
> Instead move the extradata contained in packet side-data to its
> destination. This is possible because the side data already has zeroed
> padding.
> 
> Notice that the check for FF_MAX_EXTRADATA_SIZE has been dropped,
> because said constant is from libavcodec/internal.h. If libavcodec
> wanted to enforce this, it should do so in the extract_extradata BSF
> instead.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavformat/utils.c | 23 ++++++++---------------
>  1 file changed, 8 insertions(+), 15 deletions(-)
> 
> diff --git a/libavformat/utils.c b/libavformat/utils.c
> index 807d9f10cb..63952e31a0 100644
> --- a/libavformat/utils.c
> +++ b/libavformat/utils.c
> @@ -3567,9 +3567,6 @@ static int extract_extradata(AVStream *st, const AVPacket *pkt)
>      }
>  
>      while (ret >= 0 && !sti->avctx->extradata) {
> -        int extradata_size;
> -        uint8_t *extradata;
> -
>          ret = av_bsf_receive_packet(sti->extract_extradata.bsf, pkt_ref);
>          if (ret < 0) {
>              if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
> @@ -3577,19 +3574,15 @@ static int extract_extradata(AVStream *st, const AVPacket *pkt)
>              continue;
>          }
>  
> -        extradata = av_packet_get_side_data(pkt_ref, AV_PKT_DATA_NEW_EXTRADATA,
> -                                            &extradata_size);
> -
> -        if (extradata) {
> -            av_assert0(!sti->avctx->extradata);
> -            if ((unsigned)extradata_size < FF_MAX_EXTRADATA_SIZE)
> -                sti->avctx->extradata = av_mallocz(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
> -            if (!sti->avctx->extradata) {
> -                av_packet_unref(pkt_ref);
> -                return AVERROR(ENOMEM);
> +        for (int i = 0; i < pkt_ref->side_data_elems; i++) {
> +            AVPacketSideData *side_data = &pkt_ref->side_data[i];
> +            if (side_data->type == AV_PKT_DATA_NEW_EXTRADATA) {
> +                sti->avctx->extradata      = side_data->data;
> +                sti->avctx->extradata_size = side_data->size;
> +                side_data->data = NULL;
> +                side_data->size = 0;
> +                break;
>              }
> -            memcpy(sti->avctx->extradata, extradata, extradata_size);
> -            sti->avctx->extradata_size = extradata_size;
>          }
>          av_packet_unref(pkt_ref);
>      }
> 
Will apply tomorrow unless there are objections.

- Andreas
diff mbox series

Patch

diff --git a/libavformat/utils.c b/libavformat/utils.c
index 807d9f10cb..63952e31a0 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -3567,9 +3567,6 @@  static int extract_extradata(AVStream *st, const AVPacket *pkt)
     }
 
     while (ret >= 0 && !sti->avctx->extradata) {
-        int extradata_size;
-        uint8_t *extradata;
-
         ret = av_bsf_receive_packet(sti->extract_extradata.bsf, pkt_ref);
         if (ret < 0) {
             if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
@@ -3577,19 +3574,15 @@  static int extract_extradata(AVStream *st, const AVPacket *pkt)
             continue;
         }
 
-        extradata = av_packet_get_side_data(pkt_ref, AV_PKT_DATA_NEW_EXTRADATA,
-                                            &extradata_size);
-
-        if (extradata) {
-            av_assert0(!sti->avctx->extradata);
-            if ((unsigned)extradata_size < FF_MAX_EXTRADATA_SIZE)
-                sti->avctx->extradata = av_mallocz(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
-            if (!sti->avctx->extradata) {
-                av_packet_unref(pkt_ref);
-                return AVERROR(ENOMEM);
+        for (int i = 0; i < pkt_ref->side_data_elems; i++) {
+            AVPacketSideData *side_data = &pkt_ref->side_data[i];
+            if (side_data->type == AV_PKT_DATA_NEW_EXTRADATA) {
+                sti->avctx->extradata      = side_data->data;
+                sti->avctx->extradata_size = side_data->size;
+                side_data->data = NULL;
+                side_data->size = 0;
+                break;
             }
-            memcpy(sti->avctx->extradata, extradata, extradata_size);
-            sti->avctx->extradata_size = extradata_size;
         }
         av_packet_unref(pkt_ref);
     }