[FFmpeg-cvslog] vp9_superframe: Avoid allocations and copies of packet structures

Andreas Rheinhardt git at videolan.org
Thu Oct 17 18:39:23 EEST 2019


ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at gmail.com> | Thu Oct 17 10:29:45 2019 +0200| [ba191c0ce044a71e49b5bef97dd84d00aa37f4e4] | committer: James Almer

vp9_superframe: Avoid allocations and copies of packet structures

Up until now, the vp9_superframe bsf used distinct packets for input and
output. But at no point in the bsf are the input and output packets used
at the same time (except during a call to av_packet_move_ref()), so that
one can avoid using two packets if one switches to ff_bsf_get_packet_ref
at the same time. This also saves one malloc+free of an AVPacket
structure per filtered packet.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
Signed-off-by: James Almer <jamrial at gmail.com>

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

 libavcodec/vp9_superframe_bsf.c | 26 +++++++++++---------------
 1 file changed, 11 insertions(+), 15 deletions(-)

diff --git a/libavcodec/vp9_superframe_bsf.c b/libavcodec/vp9_superframe_bsf.c
index 23933d4136..b79911bb2c 100644
--- a/libavcodec/vp9_superframe_bsf.c
+++ b/libavcodec/vp9_superframe_bsf.c
@@ -97,26 +97,25 @@ static int merge_superframe(AVPacket * const *in, int n_in, AVPacket *out)
     return 0;
 }
 
-static int vp9_superframe_filter(AVBSFContext *ctx, AVPacket *out)
+static int vp9_superframe_filter(AVBSFContext *ctx, AVPacket *pkt)
 {
     GetBitContext gb;
     VP9BSFContext *s = ctx->priv_data;
-    AVPacket *in;
     int res, invisible, profile, marker, uses_superframe_syntax = 0, n;
 
-    res = ff_bsf_get_packet(ctx, &in);
+    res = ff_bsf_get_packet_ref(ctx, pkt);
     if (res < 0)
         return res;
 
-    marker = in->data[in->size - 1];
+    marker = pkt->data[pkt->size - 1];
     if ((marker & 0xe0) == 0xc0) {
         int nbytes = 1 + ((marker >> 3) & 0x3);
         int n_frames = 1 + (marker & 0x7), idx_sz = 2 + n_frames * nbytes;
 
-        uses_superframe_syntax = in->size >= idx_sz && in->data[in->size - idx_sz] == marker;
+        uses_superframe_syntax = pkt->size >= idx_sz && pkt->data[pkt->size - idx_sz] == marker;
     }
 
-    if ((res = init_get_bits8(&gb, in->data, in->size)) < 0)
+    if ((res = init_get_bits8(&gb, pkt->data, pkt->size)) < 0)
         goto done;
 
     get_bits(&gb, 2); // frame marker
@@ -138,8 +137,7 @@ static int vp9_superframe_filter(AVBSFContext *ctx, AVPacket *out)
         goto done;
     } else if ((!invisible || uses_superframe_syntax) && !s->n_cache) {
         // passthrough
-        av_packet_move_ref(out, in);
-        goto done;
+        return 0;
     } else if (s->n_cache + 1 >= MAX_CACHE) {
         av_log(ctx, AV_LOG_ERROR,
                "Too many invisible frames\n");
@@ -147,19 +145,18 @@ static int vp9_superframe_filter(AVBSFContext *ctx, AVPacket *out)
         goto done;
     }
 
-    av_packet_move_ref(s->cache[s->n_cache++], in);
+    av_packet_move_ref(s->cache[s->n_cache++], pkt);
 
     if (invisible) {
-        res = AVERROR(EAGAIN);
-        goto done;
+        return AVERROR(EAGAIN);
     }
     av_assert0(s->n_cache > 0);
 
     // build superframe
-    if ((res = merge_superframe(s->cache, s->n_cache, out)) < 0)
+    if ((res = merge_superframe(s->cache, s->n_cache, pkt)) < 0)
         goto done;
 
-    res = av_packet_copy_props(out, s->cache[s->n_cache - 1]);
+    res = av_packet_copy_props(pkt, s->cache[s->n_cache - 1]);
     if (res < 0)
         goto done;
 
@@ -169,8 +166,7 @@ static int vp9_superframe_filter(AVBSFContext *ctx, AVPacket *out)
 
 done:
     if (res < 0)
-        av_packet_unref(out);
-    av_packet_free(&in);
+        av_packet_unref(pkt);
     return res;
 }
 



More information about the ffmpeg-cvslog mailing list