[FFmpeg-devel] [PATCH 26/36] avcodec/mjpega_dump_header_bsf: Remove intermediate packet

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Sat May 30 19:05:31 EEST 2020


This commit ends using separate packets for in- and output. Instead,
the input is read directly into the packet destined for output via
ff_bsf_get_packet_ref() and only the buffer-related fields are modified;
the others are not touched.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
 libavcodec/mjpega_dump_header_bsf.c | 39 +++++++++++++----------------
 1 file changed, 17 insertions(+), 22 deletions(-)

diff --git a/libavcodec/mjpega_dump_header_bsf.c b/libavcodec/mjpega_dump_header_bsf.c
index 4608fe550b..2d49b042ec 100644
--- a/libavcodec/mjpega_dump_header_bsf.c
+++ b/libavcodec/mjpega_dump_header_bsf.c
@@ -31,29 +31,27 @@
 #include "mjpeg.h"
 
 
-static int mjpega_dump_header(AVBSFContext *ctx, AVPacket *out)
+static int mjpega_dump_header(AVBSFContext *ctx, AVPacket *pkt)
 {
-    AVPacket *in;
     uint8_t *out_buf;
     unsigned dqt = 0, dht = 0, sof0 = 0;
     int ret = 0, i;
 
-    ret = ff_bsf_get_packet(ctx, &in);
+    ret = ff_bsf_get_packet_ref(ctx, pkt);
     if (ret < 0)
         return ret;
 
-    for (i = 0; i < in->size - 3; i++) {
-        if (in->data[i] == 0xff) {
-            switch (in->data[i + 1]) {
+    for (i = 0; i < pkt->size - 3; i++) {
+        if (pkt->data[i] == 0xff) {
+            switch (pkt->data[i + 1]) {
             case DQT:  dqt  = i + 46U; break;
             case DHT:  dht  = i + 46U; break;
             case SOF0: sof0 = i + 46U; break;
             case SOS:
-    ret = av_new_packet(out, in->size + 44U);
-    if (ret < 0)
-        goto fail;
+            {
+                AVBufferRef *out = NULL;
 
-    ret = av_packet_copy_props(out, in);
+                ret = ff_buffer_padded_realloc(&out, pkt->size + 44U);
     if (ret < 0)
         goto fail;
 
@@ -65,24 +63,22 @@ static int mjpega_dump_header(AVBSFContext *ctx, AVPacket *out)
     bytestream_put_be16(&out_buf, 42); /* size */
     bytestream_put_be32(&out_buf, 0);
     bytestream_put_buffer(&out_buf, "mjpg", 4);
-    bytestream_put_be32(&out_buf, in->size + 44U); /* field size */
-    bytestream_put_be32(&out_buf, in->size + 44U); /* pad field size */
-    bytestream_put_be32(&out_buf, 0);             /* next ptr */
+                bytestream_put_be32(&out_buf, pkt->size + 44U); /* field size */
+                bytestream_put_be32(&out_buf, pkt->size + 44U); /* pad field size */
+                bytestream_put_be32(&out_buf, 0);             /* next ptr */
                 bytestream_put_be32(&out_buf, dqt); /* quant off */
                 bytestream_put_be32(&out_buf, dht); /* huff off */
                 bytestream_put_be32(&out_buf, sof0); /* image off */
                 bytestream_put_be32(&out_buf, i + 46U); /* scan off */
-                bytestream_put_be32(&out_buf, i + 46U + AV_RB16(in->data + i + 2)); /* data off */
-                bytestream_put_buffer(&out_buf, in->data + 2, in->size - 2); /* skip already written SOI */
+                bytestream_put_be32(&out_buf, i + 46U + AV_RB16(pkt->data + i + 2)); /* data off */
+                bytestream_put_buffer(&out_buf, pkt->data + 2, pkt->size - 2); /* skip already written SOI */
 
-                out->size = out_buf - out->data;
-                av_packet_free(&in);
+                ff_packet_replace_buffer(pkt, out);
                 return 0;
+            }
             case APP1:
-                if (i + 12U <= in->size && AV_RL32(in->data + i + 8) == AV_RL32("mjpg")) {
+                if (i + 12U <= pkt->size && AV_RL32(pkt->data + i + 8) == AV_RL32("mjpg")) {
                     av_log(ctx, AV_LOG_ERROR, "bitstream already formatted\n");
-                    av_packet_move_ref(out, in);
-                    av_packet_free(&in);
                     return 0;
                 }
             }
@@ -90,8 +86,7 @@ static int mjpega_dump_header(AVBSFContext *ctx, AVPacket *out)
     }
     av_log(ctx, AV_LOG_ERROR, "No valid SOS marker in bitstream\n");
 fail:
-    av_packet_unref(out);
-    av_packet_free(&in);
+    av_packet_unref(pkt);
     return AVERROR_INVALIDDATA;
 }
 
-- 
2.20.1



More information about the ffmpeg-devel mailing list