[FFmpeg-devel] [PATCH 03/21] avcodec/zmbv: Use ff_inflate_init/end()

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Tue Mar 15 22:05:53 EET 2022


Returns better error messages in case of error and deduplicates
the inflateInit() code.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
 configure         |  2 +-
 libavcodec/zmbv.c | 38 ++++++++++++++------------------------
 2 files changed, 15 insertions(+), 25 deletions(-)

diff --git a/configure b/configure
index 6254dc9dc1..c86e70e985 100755
--- a/configure
+++ b/configure
@@ -2993,7 +2993,7 @@ ylc_decoder_select="bswapdsp"
 zerocodec_decoder_deps="zlib"
 zlib_decoder_deps="zlib"
 zlib_encoder_deps="zlib"
-zmbv_decoder_deps="zlib"
+zmbv_decoder_select="inflate_wrapper"
 zmbv_encoder_deps="zlib"
 
 # hardware accelerators
diff --git a/libavcodec/zmbv.c b/libavcodec/zmbv.c
index 0a5bb40ad5..0bc34c81dd 100644
--- a/libavcodec/zmbv.c
+++ b/libavcodec/zmbv.c
@@ -32,6 +32,7 @@
 #include "libavutil/intreadwrite.h"
 #include "avcodec.h"
 #include "internal.h"
+#include "zlib_wrapper.h"
 
 #include <zlib.h>
 
@@ -56,7 +57,6 @@ enum ZmbvFormat {
 typedef struct ZmbvContext {
     AVCodecContext *avctx;
 
-    int zlib_init_ok;
     int bpp;
     int alloc_bpp;
     unsigned int decomp_size;
@@ -71,7 +71,7 @@ typedef struct ZmbvContext {
     int bw, bh, bx, by;
     int decomp_len;
     int got_keyframe;
-    z_stream zstream;
+    FFZStream zstream;
     int (*decode_xor)(struct ZmbvContext *c);
 } ZmbvContext;
 
@@ -493,7 +493,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPac
             return AVERROR_PATCHWELCOME;
         }
 
-        zret = inflateReset(&c->zstream);
+        zret = inflateReset(&c->zstream.zstream);
         if (zret != Z_OK) {
             av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
             return AVERROR_UNKNOWN;
@@ -536,17 +536,19 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPac
         memcpy(c->decomp_buf, buf, len);
         c->decomp_len = len;
     } else { // ZLIB-compressed data
-        c->zstream.total_in = c->zstream.total_out = 0;
-        c->zstream.next_in = buf;
-        c->zstream.avail_in = len;
-        c->zstream.next_out = c->decomp_buf;
-        c->zstream.avail_out = c->decomp_size;
-        zret = inflate(&c->zstream, Z_SYNC_FLUSH);
+        z_stream *const zstream = &c->zstream.zstream;
+
+        zstream->total_in  = zstream->total_out = 0;
+        zstream->next_in   = buf;
+        zstream->avail_in  = len;
+        zstream->next_out  = c->decomp_buf;
+        zstream->avail_out = c->decomp_size;
+        zret = inflate(zstream, Z_SYNC_FLUSH);
         if (zret != Z_OK && zret != Z_STREAM_END) {
             av_log(avctx, AV_LOG_ERROR, "inflate error %d\n", zret);
             return AVERROR_INVALIDDATA;
         }
-        c->decomp_len = c->zstream.total_out;
+        c->decomp_len = zstream->total_out;
     }
     if (expected_size > c->decomp_len ||
         (c->flags & ZMBV_KEYFRAME) && expected_size < c->decomp_len) {
@@ -603,7 +605,6 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPac
 static av_cold int decode_init(AVCodecContext *avctx)
 {
     ZmbvContext * const c = avctx->priv_data;
-    int zret; // Zlib return code
 
     c->avctx = avctx;
 
@@ -627,17 +628,7 @@ static av_cold int decode_init(AVCodecContext *avctx)
         return AVERROR(ENOMEM);
     }
 
-    c->zstream.zalloc = Z_NULL;
-    c->zstream.zfree = Z_NULL;
-    c->zstream.opaque = Z_NULL;
-    zret = inflateInit(&c->zstream);
-    if (zret != Z_OK) {
-        av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
-        return AVERROR_UNKNOWN;
-    }
-    c->zlib_init_ok = 1;
-
-    return 0;
+    return ff_inflate_init(&c->zstream, avctx);
 }
 
 static av_cold int decode_end(AVCodecContext *avctx)
@@ -648,8 +639,7 @@ static av_cold int decode_end(AVCodecContext *avctx)
 
     av_freep(&c->cur);
     av_freep(&c->prev);
-    if (c->zlib_init_ok)
-        inflateEnd(&c->zstream);
+    ff_inflate_end(&c->zstream);
 
     return 0;
 }
-- 
2.32.0



More information about the ffmpeg-devel mailing list