diff mbox series

[FFmpeg-devel,16/36] avcodec/mjpega_dump_header_bsf: Only allocate when necessary

Message ID 20200530160541.29517-16-andreas.rheinhardt@gmail.com
State New
Headers show
Series [FFmpeg-devel,01/36] avcodec/vp9_superframe_bsf: Check for existence of data before reading it
Related show

Checks

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

Commit Message

Andreas Rheinhardt May 30, 2020, 4:05 p.m. UTC
This avoids having to free a buffer that hasn't been used at all.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/mjpega_dump_header_bsf.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/libavcodec/mjpega_dump_header_bsf.c b/libavcodec/mjpega_dump_header_bsf.c
index 1cd2b48719..4608fe550b 100644
--- a/libavcodec/mjpega_dump_header_bsf.c
+++ b/libavcodec/mjpega_dump_header_bsf.c
@@ -42,6 +42,13 @@  static int mjpega_dump_header(AVBSFContext *ctx, AVPacket *out)
     if (ret < 0)
         return ret;
 
+    for (i = 0; i < in->size - 3; i++) {
+        if (in->data[i] == 0xff) {
+            switch (in->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;
@@ -61,14 +68,6 @@  static int mjpega_dump_header(AVBSFContext *ctx, AVPacket *out)
     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 */
-
-    for (i = 0; i < in->size - 3; i++) {
-        if (in->data[i] == 0xff) {
-            switch (in->data[i + 1]) {
-            case DQT:  dqt  = i + 46U; break;
-            case DHT:  dht  = i + 46U; break;
-            case SOF0: sof0 = i + 46U; break;
-            case SOS:
                 bytestream_put_be32(&out_buf, dqt); /* quant off */
                 bytestream_put_be32(&out_buf, dht); /* huff off */
                 bytestream_put_be32(&out_buf, sof0); /* image off */
@@ -82,7 +81,6 @@  static int mjpega_dump_header(AVBSFContext *ctx, AVPacket *out)
             case APP1:
                 if (i + 12U <= in->size && AV_RL32(in->data + i + 8) == AV_RL32("mjpg")) {
                     av_log(ctx, AV_LOG_ERROR, "bitstream already formatted\n");
-                    av_packet_unref(out);
                     av_packet_move_ref(out, in);
                     av_packet_free(&in);
                     return 0;