[FFmpeg-devel] [PATCH 20/21] avcodec/lclenc: Use ff_deflate_init/end() wrappers

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


They return nicer error messages on error; furthermore,
they also use our allocation functions. It also stops
calling deflateEnd() on a z_stream that might not have been
successfully initialized.

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

diff --git a/configure b/configure
index a3d4b224f4..ee3c6783f3 100755
--- a/configure
+++ b/configure
@@ -2991,7 +2991,7 @@ xma2_decoder_select="wmapro_decoder"
 ylc_decoder_select="bswapdsp"
 zerocodec_decoder_select="inflate_wrapper"
 zlib_decoder_select="inflate_wrapper"
-zlib_encoder_deps="zlib"
+zlib_encoder_select="deflate_wrapper"
 zmbv_decoder_select="inflate_wrapper"
 zmbv_encoder_select="deflate_wrapper"
 
diff --git a/libavcodec/lclenc.c b/libavcodec/lclenc.c
index 7deea4f20a..afa99d1f92 100644
--- a/libavcodec/lclenc.c
+++ b/libavcodec/lclenc.c
@@ -45,6 +45,7 @@
 #include "encode.h"
 #include "internal.h"
 #include "lcl.h"
+#include "zlib_wrapper.h"
 #include "libavutil/internal.h"
 #include "libavutil/mem.h"
 
@@ -60,16 +61,17 @@ typedef struct LclEncContext {
     int compression;
     // Flags
     int flags;
-    z_stream zstream;
+    FFZStream zstream;
 } LclEncContext;
 
 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
                         const AVFrame *p, int *got_packet)
 {
     LclEncContext *c = avctx->priv_data;
+    z_stream *const zstream = &c->zstream.zstream;
     int i, ret;
     int zret; // Zlib return code
-    int max_size = deflateBound(&c->zstream, avctx->width * avctx->height * 3);
+    int max_size = deflateBound(zstream, avctx->width * avctx->height * 3);
 
     if ((ret = ff_alloc_packet(avctx, pkt, max_size)) < 0)
         return ret;
@@ -79,30 +81,30 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
         return -1;
     }
 
-    zret = deflateReset(&c->zstream);
+    zret = deflateReset(zstream);
     if (zret != Z_OK) {
         av_log(avctx, AV_LOG_ERROR, "Deflate reset error: %d\n", zret);
         return -1;
     }
-    c->zstream.next_out  = pkt->data;
-    c->zstream.avail_out = pkt->size;
+    zstream->next_out  = pkt->data;
+    zstream->avail_out = pkt->size;
 
     for(i = avctx->height - 1; i >= 0; i--) {
-        c->zstream.next_in = p->data[0]+p->linesize[0]*i;
-        c->zstream.avail_in = avctx->width*3;
-        zret = deflate(&c->zstream, Z_NO_FLUSH);
+        zstream->next_in  = p->data[0] + p->linesize[0] * i;
+        zstream->avail_in = avctx->width * 3;
+        zret = deflate(zstream, Z_NO_FLUSH);
         if (zret != Z_OK) {
             av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
             return -1;
         }
     }
-    zret = deflate(&c->zstream, Z_FINISH);
+    zret = deflate(zstream, Z_FINISH);
     if (zret != Z_STREAM_END) {
         av_log(avctx, AV_LOG_ERROR, "Deflate error: %d\n", zret);
         return -1;
     }
 
-    pkt->size   = c->zstream.total_out;
+    pkt->size   = zstream->total_out;
     *got_packet = 1;
 
     return 0;
@@ -111,7 +113,6 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
 static av_cold int encode_init(AVCodecContext *avctx)
 {
     LclEncContext *c = avctx->priv_data;
-    int zret; // Zlib return code
 
     c->avctx= avctx;
 
@@ -138,23 +139,14 @@ static av_cold int encode_init(AVCodecContext *avctx)
     avctx->extradata[7]= CODEC_ZLIB;
     c->avctx->extradata_size= 8;
 
-    c->zstream.zalloc = Z_NULL;
-    c->zstream.zfree = Z_NULL;
-    c->zstream.opaque = Z_NULL;
-    zret = deflateInit(&c->zstream, c->compression);
-    if (zret != Z_OK) {
-        av_log(avctx, AV_LOG_ERROR, "Deflate init error: %d\n", zret);
-        return AVERROR_UNKNOWN;
-    }
-
-    return 0;
+    return ff_deflate_init(&c->zstream, c->compression, avctx);
 }
 
 static av_cold int encode_end(AVCodecContext *avctx)
 {
     LclEncContext *c = avctx->priv_data;
 
-    deflateEnd(&c->zstream);
+    ff_deflate_end(&c->zstream);
 
     return 0;
 }
-- 
2.32.0



More information about the ffmpeg-devel mailing list