[FFmpeg-devel,19/31] vp9_metadata: Avoid allocations and copies of packet structures

Submitted by Andreas Rheinhardt on June 19, 2019, 11:45 p.m.

Details

Message ID 20190619234521.15619-11-andreas.rheinhardt@gmail.com
State New
Headers show

Commit Message

Andreas Rheinhardt June 19, 2019, 11:45 p.m.
This commit changes vp9_metadata to (a) use ff_bsf_get_packet_ref
instead of ff_bsf_get_packet (thereby avoiding one malloc and free per
filtered packet) and (b) to use only one packet structure at all,
thereby avoiding a call to av_packet_copy_props.

(b) has been made possible by the recent changes to ff_cbs_write_packet.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/vp9_metadata_bsf.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

Comments

Mark Thompson July 7, 2019, 10:14 p.m.
On 20/06/2019 00:45, Andreas Rheinhardt wrote:
> This commit changes vp9_metadata to (a) use ff_bsf_get_packet_ref
> instead of ff_bsf_get_packet (thereby avoiding one malloc and free per
> filtered packet) and (b) to use only one packet structure at all,
> thereby avoiding a call to av_packet_copy_props.
> 
> (b) has been made possible by the recent changes to ff_cbs_write_packet.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  libavcodec/vp9_metadata_bsf.c | 16 +++++-----------
>  1 file changed, 5 insertions(+), 11 deletions(-)
> 
> diff --git a/libavcodec/vp9_metadata_bsf.c b/libavcodec/vp9_metadata_bsf.c
> index b79f08af6c..1bde1b96aa 100644
> --- a/libavcodec/vp9_metadata_bsf.c
> +++ b/libavcodec/vp9_metadata_bsf.c
> @@ -37,18 +37,17 @@ typedef struct VP9MetadataContext {
>  } VP9MetadataContext;
>  
>  
> -static int vp9_metadata_filter(AVBSFContext *bsf, AVPacket *out)
> +static int vp9_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
>  {
>      VP9MetadataContext *ctx = bsf->priv_data;
> -    AVPacket *in = NULL;
>      CodedBitstreamFragment *frag = &ctx->fragment;
>      int err, i;
>  
> -    err = ff_bsf_get_packet(bsf, &in);
> +    err = ff_bsf_get_packet_ref(bsf, pkt);
>      if (err < 0)
>          return err;
>  
> -    err = ff_cbs_read_packet(ctx->cbc, frag, in);
> +    err = ff_cbs_read_packet(ctx->cbc, frag, pkt);
>      if (err < 0) {
>          av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
>          goto fail;
> @@ -74,23 +73,18 @@ static int vp9_metadata_filter(AVBSFContext *bsf, AVPacket *out)
>          }
>      }
>  
> -    err = ff_cbs_write_packet(ctx->cbc, out, frag);
> +    err = ff_cbs_write_packet(ctx->cbc, pkt, frag);
>      if (err < 0) {
>          av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
>          goto fail;
>      }
>  
> -    err = av_packet_copy_props(out, in);
> -    if (err < 0)
> -        goto fail;
> -
>      err = 0;
>  fail:
>      ff_cbs_fragment_reset(ctx->cbc, frag);
>  
>      if (err < 0)
> -        av_packet_unref(out);
> -    av_packet_free(&in);
> +        av_packet_unref(pkt);
>  
>      return err;
>  }
> 

13-19 LGTM, applied.

Thanks,

- Mark

Patch hide | download patch | download mbox

diff --git a/libavcodec/vp9_metadata_bsf.c b/libavcodec/vp9_metadata_bsf.c
index b79f08af6c..1bde1b96aa 100644
--- a/libavcodec/vp9_metadata_bsf.c
+++ b/libavcodec/vp9_metadata_bsf.c
@@ -37,18 +37,17 @@  typedef struct VP9MetadataContext {
 } VP9MetadataContext;
 
 
-static int vp9_metadata_filter(AVBSFContext *bsf, AVPacket *out)
+static int vp9_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
 {
     VP9MetadataContext *ctx = bsf->priv_data;
-    AVPacket *in = NULL;
     CodedBitstreamFragment *frag = &ctx->fragment;
     int err, i;
 
-    err = ff_bsf_get_packet(bsf, &in);
+    err = ff_bsf_get_packet_ref(bsf, pkt);
     if (err < 0)
         return err;
 
-    err = ff_cbs_read_packet(ctx->cbc, frag, in);
+    err = ff_cbs_read_packet(ctx->cbc, frag, pkt);
     if (err < 0) {
         av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
         goto fail;
@@ -74,23 +73,18 @@  static int vp9_metadata_filter(AVBSFContext *bsf, AVPacket *out)
         }
     }
 
-    err = ff_cbs_write_packet(ctx->cbc, out, frag);
+    err = ff_cbs_write_packet(ctx->cbc, pkt, frag);
     if (err < 0) {
         av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
         goto fail;
     }
 
-    err = av_packet_copy_props(out, in);
-    if (err < 0)
-        goto fail;
-
     err = 0;
 fail:
     ff_cbs_fragment_reset(ctx->cbc, frag);
 
     if (err < 0)
-        av_packet_unref(out);
-    av_packet_free(&in);
+        av_packet_unref(pkt);
 
     return err;
 }