[FFmpeg-devel] [PATCH 12/21] avcodec/flashsv: Use ff_inflate_init/end()

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Tue Mar 15 22:06:02 EET 2022


This fixes the problem of potentially closing a z_stream
that has never been successfully initialized.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 configure            |  4 +--
 libavcodec/flashsv.c | 73 +++++++++++++++++++++-----------------------
 2 files changed, 36 insertions(+), 41 deletions(-)

diff --git a/configure b/configure
index c56b7ba85b..e3d2f590cd 100755
--- a/configure
+++ b/configure
@@ -2810,9 +2810,9 @@ ffvhuff_encoder_select="huffyuv_encoder"
 fic_decoder_select="golomb"
 flac_decoder_select="flacdsp"
 flac_encoder_select="bswapdsp flacdsp lpc"
-flashsv2_decoder_deps="zlib"
+flashsv2_decoder_select="inflate_wrapper"
 flashsv2_encoder_deps="zlib"
-flashsv_decoder_deps="zlib"
+flashsv_decoder_select="inflate_wrapper"
 flashsv_encoder_deps="zlib"
 flv_decoder_select="h263_decoder"
 flv_encoder_select="h263_encoder"
diff --git a/libavcodec/flashsv.c b/libavcodec/flashsv.c
index e284439972..eccf568f85 100644
--- a/libavcodec/flashsv.c
+++ b/libavcodec/flashsv.c
@@ -42,6 +42,7 @@
 #include "bytestream.h"
 #include "get_bits.h"
 #include "internal.h"
+#include "zlib_wrapper.h"
 
 typedef struct BlockInfo {
     const uint8_t *pos;
@@ -55,7 +56,6 @@ typedef struct FlashSVContext {
     int             block_width, block_height;
     uint8_t        *tmpblock;
     int             block_size;
-    z_stream        zstream;
     int             ver;
     const uint32_t *pal;
     int             is_keyframe;
@@ -66,6 +66,7 @@ typedef struct FlashSVContext {
     int             color_depth;
     int             zlibprime_curr, zlibprime_prev;
     int             diff_start, diff_height;
+    FFZStream       zstream;
     uint8_t         tmp[UINT16_MAX];
 } FlashSVContext;
 
@@ -104,7 +105,8 @@ static int decode_hybrid(const uint8_t *sptr, const uint8_t *sptr_end, uint8_t *
 static av_cold int flashsv_decode_end(AVCodecContext *avctx)
 {
     FlashSVContext *s = avctx->priv_data;
-    inflateEnd(&s->zstream);
+
+    ff_inflate_end(&s->zstream);
     /* release the frame if needed */
     av_frame_free(&s->frame);
 
@@ -117,17 +119,8 @@ static av_cold int flashsv_decode_end(AVCodecContext *avctx)
 static av_cold int flashsv_decode_init(AVCodecContext *avctx)
 {
     FlashSVContext *s = avctx->priv_data;
-    int zret; // Zlib return code
 
     s->avctx          = avctx;
-    s->zstream.zalloc = Z_NULL;
-    s->zstream.zfree  = Z_NULL;
-    s->zstream.opaque = Z_NULL;
-    zret = inflateInit(&s->zstream);
-    if (zret != Z_OK) {
-        av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
-        return AVERROR_EXTERNAL;
-    }
     avctx->pix_fmt = AV_PIX_FMT_BGR24;
 
     s->frame = av_frame_alloc();
@@ -135,27 +128,28 @@ static av_cold int flashsv_decode_init(AVCodecContext *avctx)
         return AVERROR(ENOMEM);
     }
 
-    return 0;
+    return ff_inflate_init(&s->zstream, avctx);
 }
 
 static int flashsv2_prime(FlashSVContext *s, const uint8_t *src, int size)
 {
     int zret; // Zlib return code
     static const uint8_t zlib_header[] = { 0x78, 0x01 };
+    z_stream *const zstream = &s->zstream.zstream;
     uint8_t *data = s->tmpblock;
     unsigned remaining;
 
     if (!src)
         return AVERROR_INVALIDDATA;
 
-    s->zstream.next_in   = src;
-    s->zstream.avail_in  = size;
-    s->zstream.next_out  = data;
-    s->zstream.avail_out = s->block_size * 3;
-    inflate(&s->zstream, Z_SYNC_FLUSH);
-    remaining = s->block_size * 3 - s->zstream.avail_out;
+    zstream->next_in   = src;
+    zstream->avail_in  = size;
+    zstream->next_out  = data;
+    zstream->avail_out = s->block_size * 3;
+    inflate(zstream, Z_SYNC_FLUSH);
+    remaining = s->block_size * 3 - zstream->avail_out;
 
-    if ((zret = inflateReset(&s->zstream)) != Z_OK) {
+    if ((zret = inflateReset(zstream)) != Z_OK) {
         av_log(s->avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
         return AVERROR_UNKNOWN;
     }
@@ -166,9 +160,9 @@ static int flashsv2_prime(FlashSVContext *s, const uint8_t *src, int size)
      * the adler32 checksum is correctly initialized).
      * This is accomplished by synthetizing blocks of uncompressed data
      * out of the output from above. See section 3.2.4 of RFC 1951. */
-    s->zstream.next_in  = zlib_header;
-    s->zstream.avail_in = sizeof(zlib_header);
-    inflate(&s->zstream, Z_SYNC_FLUSH);
+    zstream->next_in  = zlib_header;
+    zstream->avail_in = sizeof(zlib_header);
+    inflate(zstream, Z_SYNC_FLUSH);
     while (remaining > 0) {
         unsigned block_size = FFMIN(UINT16_MAX, remaining);
         uint8_t header[5];
@@ -178,16 +172,16 @@ static int flashsv2_prime(FlashSVContext *s, const uint8_t *src, int size)
         AV_WL16(header + 1, block_size);
         /* Block size (one's complement) */
         AV_WL16(header + 3, block_size ^ 0xFFFF);
-        s->zstream.next_in   = header;
-        s->zstream.avail_in  = sizeof(header);
-        s->zstream.next_out  = s->tmp;
-        s->zstream.avail_out = sizeof(s->tmp);
-        zret = inflate(&s->zstream, Z_SYNC_FLUSH);
+        zstream->next_in   = header;
+        zstream->avail_in  = sizeof(header);
+        zstream->next_out  = s->tmp;
+        zstream->avail_out = sizeof(s->tmp);
+        zret = inflate(zstream, Z_SYNC_FLUSH);
         if (zret != Z_OK)
             return AVERROR_UNKNOWN;
-        s->zstream.next_in   = data;
-        s->zstream.avail_in  = block_size;
-        zret = inflate(&s->zstream, Z_SYNC_FLUSH);
+        zstream->next_in   = data;
+        zstream->avail_in  = block_size;
+        zret = inflate(zstream, Z_SYNC_FLUSH);
         if (zret != Z_OK)
             return AVERROR_UNKNOWN;
         data      += block_size;
@@ -203,9 +197,10 @@ static int flashsv_decode_block(AVCodecContext *avctx, const AVPacket *avpkt,
                                 int blk_idx)
 {
     struct FlashSVContext *s = avctx->priv_data;
+    z_stream *const zstream = &s->zstream.zstream;
     uint8_t *line = s->tmpblock;
     int k;
-    int ret = inflateReset(&s->zstream);
+    int ret = inflateReset(zstream);
     if (ret != Z_OK) {
         av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
         return AVERROR_UNKNOWN;
@@ -217,15 +212,15 @@ static int flashsv_decode_block(AVCodecContext *avctx, const AVPacket *avpkt,
         if (ret < 0)
             return ret;
     }
-    s->zstream.next_in   = avpkt->data + get_bits_count(gb) / 8;
-    s->zstream.avail_in  = block_size;
-    s->zstream.next_out  = s->tmpblock;
-    s->zstream.avail_out = s->block_size * 3;
-    ret = inflate(&s->zstream, Z_FINISH);
+    zstream->next_in   = avpkt->data + get_bits_count(gb) / 8;
+    zstream->avail_in  = block_size;
+    zstream->next_out  = s->tmpblock;
+    zstream->avail_out = s->block_size * 3;
+    ret = inflate(zstream, Z_FINISH);
     if (ret == Z_DATA_ERROR) {
         av_log(avctx, AV_LOG_ERROR, "Zlib resync occurred\n");
-        inflateSync(&s->zstream);
-        ret = inflate(&s->zstream, Z_FINISH);
+        inflateSync(zstream);
+        ret = inflate(zstream, Z_FINISH);
     }
 
     if (ret != Z_OK && ret != Z_STREAM_END) {
@@ -251,7 +246,7 @@ static int flashsv_decode_block(AVCodecContext *avctx, const AVPacket *avpkt,
         }
     } else {
         /* hybrid 15-bit/palette mode */
-        ret = decode_hybrid(s->tmpblock, s->zstream.next_out,
+        ret = decode_hybrid(s->tmpblock, zstream->next_out,
                       s->frame->data[0],
                       s->image_height - (y_pos + 1 + s->diff_height),
                       x_pos, s->diff_height, width,
-- 
2.32.0



More information about the ffmpeg-devel mailing list