[FFmpeg-devel] [PATCH 07/21] avcodec/rasc: Use ff_inflate_init/end()

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Tue Mar 15 22:05:57 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         |  2 +-
 libavcodec/rasc.c | 49 ++++++++++++++++++++---------------------------
 2 files changed, 22 insertions(+), 29 deletions(-)

diff --git a/configure b/configure
index ad5b5f1727..464ddb019c 100755
--- a/configure
+++ b/configure
@@ -2914,7 +2914,7 @@ qdm2_decoder_select="mdct rdft mpegaudiodsp"
 ra_144_decoder_select="audiodsp"
 ra_144_encoder_select="audio_frame_queue lpc audiodsp"
 ralf_decoder_select="golomb"
-rasc_decoder_deps="zlib"
+rasc_decoder_select="inflate_wrapper"
 rawvideo_decoder_select="bswapdsp"
 rscc_decoder_deps="zlib"
 rtjpeg_decoder_select="me_cmp"
diff --git a/libavcodec/rasc.c b/libavcodec/rasc.c
index b328e219bb..9b748be9ae 100644
--- a/libavcodec/rasc.c
+++ b/libavcodec/rasc.c
@@ -30,6 +30,7 @@
 #include "avcodec.h"
 #include "bytestream.h"
 #include "internal.h"
+#include "zlib_wrapper.h"
 
 #include <zlib.h>
 
@@ -58,10 +59,10 @@ typedef struct RASCContext {
     unsigned        cursor_y;
     int             stride;
     int             bpp;
-    z_stream        zstream;
     AVFrame        *frame;
     AVFrame        *frame1;
     AVFrame        *frame2;
+    FFZStream       zstream;
 } RASCContext;
 
 static void clear_plane(AVCodecContext *avctx, AVFrame *frame)
@@ -174,10 +175,11 @@ static int decode_zlib(AVCodecContext *avctx, const AVPacket *avpkt,
                        unsigned size, unsigned uncompressed_size)
 {
     RASCContext *s = avctx->priv_data;
+    z_stream *const zstream = &s->zstream.zstream;
     GetByteContext *gb = &s->gb;
     int zret;
 
-    zret = inflateReset(&s->zstream);
+    zret = inflateReset(zstream);
     if (zret != Z_OK) {
         av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
         return AVERROR_EXTERNAL;
@@ -187,13 +189,13 @@ static int decode_zlib(AVCodecContext *avctx, const AVPacket *avpkt,
     if (!s->delta)
         return AVERROR(ENOMEM);
 
-    s->zstream.next_in  = avpkt->data + bytestream2_tell(gb);
-    s->zstream.avail_in = FFMIN(size, bytestream2_get_bytes_left(gb));
+    zstream->next_in  = avpkt->data + bytestream2_tell(gb);
+    zstream->avail_in = FFMIN(size, bytestream2_get_bytes_left(gb));
 
-    s->zstream.next_out  = s->delta;
-    s->zstream.avail_out = s->delta_size;
+    zstream->next_out  = s->delta;
+    zstream->avail_out = s->delta_size;
 
-    zret = inflate(&s->zstream, Z_FINISH);
+    zret = inflate(zstream, Z_FINISH);
     if (zret != Z_STREAM_END) {
         av_log(avctx, AV_LOG_ERROR,
                "Inflate failed with return code: %d.\n", zret);
@@ -473,6 +475,7 @@ static int decode_kfrm(AVCodecContext *avctx,
                        const AVPacket *avpkt, unsigned size)
 {
     RASCContext *s = avctx->priv_data;
+    z_stream *const zstream = &s->zstream.zstream;
     GetByteContext *gb = &s->gb;
     uint8_t *dst;
     unsigned pos;
@@ -488,21 +491,21 @@ static int decode_kfrm(AVCodecContext *avctx,
     if (!s->frame2->data[0])
         return AVERROR_INVALIDDATA;
 
-    zret = inflateReset(&s->zstream);
+    zret = inflateReset(zstream);
     if (zret != Z_OK) {
         av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
         return AVERROR_EXTERNAL;
     }
 
-    s->zstream.next_in  = avpkt->data + bytestream2_tell(gb);
-    s->zstream.avail_in = bytestream2_get_bytes_left(gb);
+    zstream->next_in  = avpkt->data + bytestream2_tell(gb);
+    zstream->avail_in = bytestream2_get_bytes_left(gb);
 
     dst = s->frame2->data[0] + (avctx->height - 1) * s->frame2->linesize[0];
     for (int i = 0; i < avctx->height; i++) {
-        s->zstream.next_out  = dst;
-        s->zstream.avail_out = s->stride;
+        zstream->next_out  = dst;
+        zstream->avail_out = s->stride;
 
-        zret = inflate(&s->zstream, Z_SYNC_FLUSH);
+        zret = inflate(zstream, Z_SYNC_FLUSH);
         if (zret != Z_OK && zret != Z_STREAM_END) {
             av_log(avctx, AV_LOG_ERROR,
                    "Inflate failed with return code: %d.\n", zret);
@@ -514,10 +517,10 @@ static int decode_kfrm(AVCodecContext *avctx,
 
     dst = s->frame1->data[0] + (avctx->height - 1) * s->frame1->linesize[0];
     for (int i = 0; i < avctx->height; i++) {
-        s->zstream.next_out  = dst;
-        s->zstream.avail_out = s->stride;
+        zstream->next_out  = dst;
+        zstream->avail_out = s->stride;
 
-        zret = inflate(&s->zstream, Z_SYNC_FLUSH);
+        zret = inflate(zstream, Z_SYNC_FLUSH);
         if (zret != Z_OK && zret != Z_STREAM_END) {
             av_log(avctx, AV_LOG_ERROR,
                    "Inflate failed with return code: %d.\n", zret);
@@ -751,23 +754,13 @@ static int decode_frame(AVCodecContext *avctx,
 static av_cold int decode_init(AVCodecContext *avctx)
 {
     RASCContext *s = avctx->priv_data;
-    int zret;
-
-    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;
-    }
 
     s->frame1 = av_frame_alloc();
     s->frame2 = av_frame_alloc();
     if (!s->frame1 || !s->frame2)
         return AVERROR(ENOMEM);
 
-    return 0;
+    return ff_inflate_init(&s->zstream, avctx);
 }
 
 static av_cold int decode_close(AVCodecContext *avctx)
@@ -780,7 +773,7 @@ static av_cold int decode_close(AVCodecContext *avctx)
     s->delta_size = 0;
     av_frame_free(&s->frame1);
     av_frame_free(&s->frame2);
-    inflateEnd(&s->zstream);
+    ff_inflate_end(&s->zstream);
 
     return 0;
 }
-- 
2.32.0



More information about the ffmpeg-devel mailing list