diff mbox series

[FFmpeg-devel,v3,4/5] avcodec/mmaldec: Avoid creating unnecessary reference, simplify code

Message ID AM7PR03MB66605B894C9617272367A5DC8F709@AM7PR03MB6660.eurprd03.prod.outlook.com
State Accepted
Commit 9644ee224f56118e5b1ac4edc1b088a6a6ac2ac7
Headers show
Series Switch mmaldec to receive_frame API | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Andreas Rheinhardt Dec. 9, 2021, 1:08 p.m. UTC
ffmal_add_packet() basically duplicated the logic in
av_packet_make_refcounted() with the added twist that it always
created a reference even if one is already available.
This commit stops doing this.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/mmaldec.c | 23 ++++++-----------------
 1 file changed, 6 insertions(+), 17 deletions(-)

Comments

Anton Khirnov Dec. 13, 2021, 10:15 a.m. UTC | #1
Quoting Andreas Rheinhardt (2021-12-09 14:08:04)
> ffmal_add_packet() basically duplicated the logic in
> av_packet_make_refcounted() with the added twist that it always
> created a reference even if one is already available.
> This commit stops doing this.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>  libavcodec/mmaldec.c | 23 ++++++-----------------
>  1 file changed, 6 insertions(+), 17 deletions(-)
> 
> diff --git a/libavcodec/mmaldec.c b/libavcodec/mmaldec.c
> index 3e11d487ab..0c7df0a0bc 100644
> --- a/libavcodec/mmaldec.c
> +++ b/libavcodec/mmaldec.c
> @@ -485,29 +485,19 @@ static int ffmmal_add_packet(AVCodecContext *avctx, AVPacket *avpkt,
>                               int is_extradata)
>  {
>      MMALDecodeContext *ctx = avctx->priv_data;
> -    AVBufferRef *buf = NULL;
> +    const AVBufferRef *buf = NULL;
>      int size = 0;
>      uint8_t *data = (uint8_t *)"";
>      uint8_t *start;
>      int ret = 0;
>  
>      if (avpkt->size) {
> -        if (avpkt->buf) {
> -            buf = av_buffer_ref(avpkt->buf);
> -            size = avpkt->size;
> -            data = avpkt->data;
> -        } else {
> -            buf = av_buffer_alloc(avpkt->size);
> -            if (buf) {
> -                memcpy(buf->data, avpkt->data, avpkt->size);
> -                size = buf->size;
> -                data = buf->data;
> -            }
> -        }
> -        if (!buf) {
> -            ret = AVERROR(ENOMEM);
> +        ret = av_packet_make_refcounted(avpkt);

It seems better to call this from the place where the extradata packet
is created, since that should be the only source of non-refcounted
packets.
diff mbox series

Patch

diff --git a/libavcodec/mmaldec.c b/libavcodec/mmaldec.c
index 3e11d487ab..0c7df0a0bc 100644
--- a/libavcodec/mmaldec.c
+++ b/libavcodec/mmaldec.c
@@ -485,29 +485,19 @@  static int ffmmal_add_packet(AVCodecContext *avctx, AVPacket *avpkt,
                              int is_extradata)
 {
     MMALDecodeContext *ctx = avctx->priv_data;
-    AVBufferRef *buf = NULL;
+    const AVBufferRef *buf = NULL;
     int size = 0;
     uint8_t *data = (uint8_t *)"";
     uint8_t *start;
     int ret = 0;
 
     if (avpkt->size) {
-        if (avpkt->buf) {
-            buf = av_buffer_ref(avpkt->buf);
-            size = avpkt->size;
-            data = avpkt->data;
-        } else {
-            buf = av_buffer_alloc(avpkt->size);
-            if (buf) {
-                memcpy(buf->data, avpkt->data, avpkt->size);
-                size = buf->size;
-                data = buf->data;
-            }
-        }
-        if (!buf) {
-            ret = AVERROR(ENOMEM);
+        ret = av_packet_make_refcounted(avpkt);
+        if (ret < 0)
             goto done;
-        }
+        buf  = avpkt->buf;
+        data = avpkt->data;
+        size = avpkt->size;
         if (!is_extradata)
             ctx->packets_sent++;
     } else {
@@ -573,7 +563,6 @@  static int ffmmal_add_packet(AVCodecContext *avctx, AVPacket *avpkt,
     } while (size);
 
 done:
-    av_buffer_unref(&buf);
     av_packet_unref(avpkt);
     return ret;
 }