Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers
@ 2022-03-15 20:03 Andreas Rheinhardt
  2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 02/21] avcodec/zlib_wrapper: Add wrappers for zlib inflateInit, inflateEnd Andreas Rheinhardt
                   ` (19 more replies)
  0 siblings, 20 replies; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:03 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

So use 64bits for max_packet_size instead of size_t which might be
32 bits; this is consistent with ff_alloc_packet().
Also remove a redundant size check (ff_alloc_packet() already
checks for that).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/pngenc.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/libavcodec/pngenc.c b/libavcodec/pngenc.c
index 3ebcc1e571..64a9f5cc95 100644
--- a/libavcodec/pngenc.c
+++ b/libavcodec/pngenc.c
@@ -528,7 +528,7 @@ static int encode_png(AVCodecContext *avctx, AVPacket *pkt,
     PNGEncContext *s = avctx->priv_data;
     int ret;
     int enc_row_size;
-    size_t max_packet_size;
+    int64_t max_packet_size;
 
     enc_row_size    = deflateBound(&s->zstream, (avctx->width * s->bits_per_pixel + 7) >> 3);
     max_packet_size =
@@ -537,8 +537,6 @@ static int encode_png(AVCodecContext *avctx, AVPacket *pkt,
             enc_row_size +
             12 * (((int64_t)enc_row_size + IOBUF_SIZE - 1) / IOBUF_SIZE) // IDAT * ceil(enc_row_size / IOBUF_SIZE)
         );
-    if (max_packet_size > INT_MAX)
-        return AVERROR(ENOMEM);
     ret = ff_alloc_packet(avctx, pkt, max_packet_size);
     if (ret < 0)
         return ret;
@@ -845,7 +843,7 @@ static int encode_apng(AVCodecContext *avctx, AVPacket *pkt,
     PNGEncContext *s = avctx->priv_data;
     int ret;
     int enc_row_size;
-    size_t max_packet_size;
+    int64_t max_packet_size;
     APNGFctlChunk fctl_chunk = {0};
 
     if (pict && s->color_type == PNG_COLOR_TYPE_PALETTE) {
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [FFmpeg-devel] [PATCH 02/21] avcodec/zlib_wrapper: Add wrappers for zlib inflateInit, inflateEnd
  2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
@ 2022-03-15 20:05 ` Andreas Rheinhardt
  2022-03-16 19:24   ` Tomas Härdin
  2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 03/21] avcodec/zmbv: Use ff_inflate_init/end() Andreas Rheinhardt
                   ` (18 subsequent siblings)
  19 siblings, 1 reply; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:05 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

It is not documented to be safe to call inflateEnd() on a z_stream
that has never been successfully been initialized by inflateInit(),
but just zeroed. It just happens to work and several codecs rely
on this (they have FF_CODEC_CAP_INIT_CLEANUP set and even call
inflateEnd() when inflateInit() failed or has never been called).
To avoid this, other codecs recorded whether their zstream has been
initialized successfully or not.

This commit adds wrappers for inflateInit() and inflateEnd() that
do what these other codecs do; furthermore, they also take care of
properly setting up the zstream before inflateInit() and emit
an error message in case of error.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 configure                 |  2 ++
 libavcodec/Makefile       |  1 +
 libavcodec/zlib_wrapper.c | 57 +++++++++++++++++++++++++++++++++++++++
 libavcodec/zlib_wrapper.h | 51 +++++++++++++++++++++++++++++++++++
 4 files changed, 111 insertions(+)
 create mode 100644 libavcodec/zlib_wrapper.c
 create mode 100644 libavcodec/zlib_wrapper.h

diff --git a/configure b/configure
index 82642deabe..6254dc9dc1 100755
--- a/configure
+++ b/configure
@@ -2461,6 +2461,7 @@ CONFIG_EXTRA="
     idctdsp
     iirfilter
     mdct15
+    inflate_wrapper
     intrax8
     iso_media
     ividsp
@@ -2722,6 +2723,7 @@ faanidct_select="idctdsp"
 h264dsp_select="startcode"
 hevcparse_select="atsc_a53 golomb"
 frame_thread_encoder_deps="encoders threads"
+inflate_wrapper_deps="zlib"
 intrax8_select="blockdsp idctdsp"
 iso_media_select="mpeg4audio"
 mdct_select="fft"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index f36b2e992d..62c8e34963 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -104,6 +104,7 @@ OBJS-$(CONFIG_HUFFYUVENCDSP)           += huffyuvencdsp.o
 OBJS-$(CONFIG_IDCTDSP)                 += idctdsp.o simple_idct.o jrevdct.o
 OBJS-$(CONFIG_IIRFILTER)               += iirfilter.o
 OBJS-$(CONFIG_MDCT15)                  += mdct15.o
+OBJS-$(CONFIG_INFLATE_WRAPPER)         += zlib_wrapper.o
 OBJS-$(CONFIG_INTRAX8)                 += intrax8.o intrax8dsp.o msmpeg4data.o
 OBJS-$(CONFIG_IVIDSP)                  += ivi_dsp.o
 OBJS-$(CONFIG_JNI)                     += ffjni.o jni.o
diff --git a/libavcodec/zlib_wrapper.c b/libavcodec/zlib_wrapper.c
new file mode 100644
index 0000000000..b15d5be2b8
--- /dev/null
+++ b/libavcodec/zlib_wrapper.c
@@ -0,0 +1,57 @@
+/*
+ * Wrappers for zlib
+ * Copyright (C) 2022 Andreas Rheinhardt
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <zlib.h>
+
+#include "libavutil/error.h"
+#include "libavutil/log.h"
+#include "zlib_wrapper.h"
+
+int ff_inflate_init(FFZStream *z, void *logctx)
+{
+    z_stream *const zstream = &z->zstream;
+    int zret;
+
+    z->inited = 0;
+    zstream->next_in  = Z_NULL;
+    zstream->avail_in = 0;
+    zstream->zalloc   = Z_NULL;
+    zstream->zfree    = Z_NULL;
+    zstream->opaque   = Z_NULL;
+
+    zret = inflateInit(zstream);
+    if (zret == Z_OK) {
+        z->inited = 1;
+    } else {
+        av_log(logctx, AV_LOG_ERROR, "inflateInit error %d, message: %s\n",
+               zret, zstream->msg ? zstream->msg : "");
+        return AVERROR_EXTERNAL;
+    }
+    return 0;
+}
+
+void ff_inflate_end(FFZStream *z)
+{
+    if (z->inited) {
+        z->inited = 0;
+        inflateEnd(&z->zstream);
+    }
+}
diff --git a/libavcodec/zlib_wrapper.h b/libavcodec/zlib_wrapper.h
new file mode 100644
index 0000000000..0e91713b25
--- /dev/null
+++ b/libavcodec/zlib_wrapper.h
@@ -0,0 +1,51 @@
+/*
+ * Wrappers for zlib
+ * Copyright (C) 2022 Andreas Rheinhardt
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVCODEC_ZLIB_WRAPPER_H
+#define AVCODEC_ZLIB_WRAPPER_H
+
+#include <zlib.h>
+
+typedef struct FFZStream {
+    z_stream zstream;
+    int inited;
+} FFZStream;
+
+/**
+ * Wrapper around inflateInit(). It initializes the fields that zlib
+ * requires to be initialized before inflateInit().
+ * In case of error it also returns an error message to the provided logctx;
+ * in any case, it sets zstream->inited to indicate whether inflateInit()
+ * succeeded.
+ * @return Returns 0 on success or a negative error code on failure
+ */
+int ff_inflate_init(FFZStream *zstream, void *logctx);
+
+/**
+ * Wrapper around inflateEnd(). It calls inflateEnd() iff
+ * zstream->inited is set and resets zstream->inited.
+ * It is therefore safe to be called even if
+ * ff_inflate_init() has never been called on it (or errored out)
+ * provided that the FFZStream (or just FFZStream.inited) has been zeroed.
+ */
+void ff_inflate_end(FFZStream *zstream);
+
+#endif /* AVCODEC_ZLIB_WRAPPER_H */
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [FFmpeg-devel] [PATCH 03/21] avcodec/zmbv: Use ff_inflate_init/end()
  2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
  2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 02/21] avcodec/zlib_wrapper: Add wrappers for zlib inflateInit, inflateEnd Andreas Rheinhardt
@ 2022-03-15 20:05 ` Andreas Rheinhardt
  2022-03-16 19:31   ` Tomas Härdin
  2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 04/21] avcodec/zerocodec: " Andreas Rheinhardt
                   ` (17 subsequent siblings)
  19 siblings, 1 reply; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:05 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

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

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@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

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [FFmpeg-devel] [PATCH 04/21] avcodec/zerocodec: Use ff_inflate_init/end()
  2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
  2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 02/21] avcodec/zlib_wrapper: Add wrappers for zlib inflateInit, inflateEnd Andreas Rheinhardt
  2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 03/21] avcodec/zmbv: Use ff_inflate_init/end() Andreas Rheinhardt
@ 2022-03-15 20:05 ` Andreas Rheinhardt
  2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 05/21] avcodec/wcmv: " Andreas Rheinhardt
                   ` (16 subsequent siblings)
  19 siblings, 0 replies; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:05 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

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

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 configure              |  2 +-
 libavcodec/zerocodec.c | 21 +++++----------------
 2 files changed, 6 insertions(+), 17 deletions(-)

diff --git a/configure b/configure
index c86e70e985..7c87548359 100755
--- a/configure
+++ b/configure
@@ -2990,7 +2990,7 @@ wmv3image_decoder_select="wmv3_decoder"
 xma1_decoder_select="wmapro_decoder"
 xma2_decoder_select="wmapro_decoder"
 ylc_decoder_select="bswapdsp"
-zerocodec_decoder_deps="zlib"
+zerocodec_decoder_select="inflate_wrapper"
 zlib_decoder_deps="zlib"
 zlib_encoder_deps="zlib"
 zmbv_decoder_select="inflate_wrapper"
diff --git a/libavcodec/zerocodec.c b/libavcodec/zerocodec.c
index 3bd04567a1..86cdf96f5a 100644
--- a/libavcodec/zerocodec.c
+++ b/libavcodec/zerocodec.c
@@ -20,11 +20,12 @@
 
 #include "avcodec.h"
 #include "internal.h"
+#include "zlib_wrapper.h"
 #include "libavutil/common.h"
 
 typedef struct ZeroCodecContext {
     AVFrame  *previous_frame;
-    z_stream zstream;
+    FFZStream zstream;
 } ZeroCodecContext;
 
 static int zerocodec_decode_frame(AVCodecContext *avctx, void *data,
@@ -33,7 +34,7 @@ static int zerocodec_decode_frame(AVCodecContext *avctx, void *data,
     ZeroCodecContext *zc = avctx->priv_data;
     AVFrame *pic         = data;
     AVFrame *prev_pic    = zc->previous_frame;
-    z_stream *zstream    = &zc->zstream;
+    z_stream *const zstream = &zc->zstream.zstream;
     uint8_t *prev        = prev_pic->data[0];
     uint8_t *dst;
     int i, j, zret, ret;
@@ -106,7 +107,7 @@ static av_cold int zerocodec_decode_close(AVCodecContext *avctx)
 
     av_frame_free(&zc->previous_frame);
 
-    inflateEnd(&zc->zstream);
+    ff_inflate_end(&zc->zstream);
 
     return 0;
 }
@@ -114,27 +115,15 @@ static av_cold int zerocodec_decode_close(AVCodecContext *avctx)
 static av_cold int zerocodec_decode_init(AVCodecContext *avctx)
 {
     ZeroCodecContext *zc = avctx->priv_data;
-    z_stream *zstream    = &zc->zstream;
-    int zret;
 
     avctx->pix_fmt             = AV_PIX_FMT_UYVY422;
     avctx->bits_per_raw_sample = 8;
 
-    zstream->zalloc = Z_NULL;
-    zstream->zfree  = Z_NULL;
-    zstream->opaque = Z_NULL;
-
-    zret = inflateInit(zstream);
-    if (zret != Z_OK) {
-        av_log(avctx, AV_LOG_ERROR, "Could not initialize inflate: %d.\n", zret);
-        return AVERROR(ENOMEM);
-    }
-
     zc->previous_frame = av_frame_alloc();
     if (!zc->previous_frame)
         return AVERROR(ENOMEM);
 
-    return 0;
+    return ff_inflate_init(&zc->zstream, avctx);
 }
 
 static void zerocodec_decode_flush(AVCodecContext *avctx)
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [FFmpeg-devel] [PATCH 05/21] avcodec/wcmv: Use ff_inflate_init/end()
  2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
                   ` (2 preceding siblings ...)
  2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 04/21] avcodec/zerocodec: " Andreas Rheinhardt
@ 2022-03-15 20:05 ` Andreas Rheinhardt
  2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 06/21] avcodec/tscc: " Andreas Rheinhardt
                   ` (15 subsequent siblings)
  19 siblings, 0 replies; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:05 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

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

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 configure         |  2 +-
 libavcodec/wcmv.c | 46 +++++++++++++++++++---------------------------
 2 files changed, 20 insertions(+), 28 deletions(-)

diff --git a/configure b/configure
index 7c87548359..fdc3f3d28c 100755
--- a/configure
+++ b/configure
@@ -2972,7 +2972,7 @@ vp6f_decoder_select="vp6_decoder"
 vp7_decoder_select="h264pred videodsp vp8dsp"
 vp8_decoder_select="h264pred videodsp vp8dsp"
 vp9_decoder_select="videodsp vp9_parser vp9_superframe_split_bsf"
-wcmv_decoder_deps="zlib"
+wcmv_decoder_select="inflate_wrapper"
 webp_decoder_select="vp8_decoder exif"
 wmalossless_decoder_select="llauddsp"
 wmapro_decoder_select="mdct sinewin wma_freqs"
diff --git a/libavcodec/wcmv.c b/libavcodec/wcmv.c
index 04c597e767..d98d2d52cf 100644
--- a/libavcodec/wcmv.c
+++ b/libavcodec/wcmv.c
@@ -29,12 +29,13 @@
 #include "avcodec.h"
 #include "bytestream.h"
 #include "internal.h"
+#include "zlib_wrapper.h"
 
 #include <zlib.h>
 
 typedef struct WCMVContext {
     int         bpp;
-    z_stream    zstream;
+    FFZStream   zstream;
     AVFrame    *prev_frame;
     uint8_t     block_data[65536*8];
 } WCMVContext;
@@ -44,12 +45,13 @@ static int decode_frame(AVCodecContext *avctx,
                         AVPacket *avpkt)
 {
     WCMVContext *s = avctx->priv_data;
+    z_stream *const zstream = &s->zstream.zstream;
     AVFrame *frame = data;
     int skip, blocks, zret, ret, intra = 0, flags = 0, bpp = s->bpp;
     GetByteContext gb;
     uint8_t *dst;
 
-    ret = inflateReset(&s->zstream);
+    ret = inflateReset(zstream);
     if (ret != Z_OK) {
         av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
         return AVERROR_EXTERNAL;
@@ -78,19 +80,19 @@ static int decode_frame(AVCodecContext *avctx,
         if (size > avpkt->size - skip)
             return AVERROR_INVALIDDATA;
 
-        s->zstream.next_in  = avpkt->data + skip;
-        s->zstream.avail_in = size;
-        s->zstream.next_out  = s->block_data;
-        s->zstream.avail_out = sizeof(s->block_data);
+        zstream->next_in  = avpkt->data + skip;
+        zstream->avail_in = size;
+        zstream->next_out  = s->block_data;
+        zstream->avail_out = sizeof(s->block_data);
 
-        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);
             return AVERROR_INVALIDDATA;
         }
 
-        ret = inflateReset(&s->zstream);
+        ret = inflateReset(zstream);
         if (ret != Z_OK) {
             av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
             return AVERROR_EXTERNAL;
@@ -119,8 +121,8 @@ static int decode_frame(AVCodecContext *avctx,
 
         skip = bytestream2_tell(&gb);
 
-        s->zstream.next_in  = avpkt->data + skip;
-        s->zstream.avail_in = avpkt->size - skip;
+        zstream->next_in  = avpkt->data + skip;
+        zstream->avail_in = avpkt->size - skip;
 
         bytestream2_init(&gb, s->block_data, blocks * 8);
     } else if (blocks) {
@@ -148,8 +150,8 @@ static int decode_frame(AVCodecContext *avctx,
 
         skip = bytestream2_tell(&gb);
 
-        s->zstream.next_in  = avpkt->data + skip;
-        s->zstream.avail_in = avpkt->size - skip;
+        zstream->next_in  = avpkt->data + skip;
+        zstream->avail_in = avpkt->size - skip;
 
         bytestream2_seek(&gb, 2, SEEK_SET);
     }
@@ -182,10 +184,10 @@ static int decode_frame(AVCodecContext *avctx,
 
         dst = s->prev_frame->data[0] + (avctx->height - y - 1) * s->prev_frame->linesize[0] + x * bpp;
         for (int i = 0; i < h; i++) {
-            s->zstream.next_out  = dst;
-            s->zstream.avail_out = w * bpp;
+            zstream->next_out  = dst;
+            zstream->avail_out = w * bpp;
 
-            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);
@@ -210,7 +212,6 @@ static int decode_frame(AVCodecContext *avctx,
 static av_cold int decode_init(AVCodecContext *avctx)
 {
     WCMVContext *s = avctx->priv_data;
-    int zret;
 
     switch (avctx->bits_per_coded_sample) {
     case 16: avctx->pix_fmt = AV_PIX_FMT_RGB565LE; break;
@@ -223,20 +224,11 @@ static av_cold int decode_init(AVCodecContext *avctx)
 
     s->bpp = avctx->bits_per_coded_sample >> 3;
 
-    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->prev_frame = av_frame_alloc();
     if (!s->prev_frame)
         return AVERROR(ENOMEM);
 
-    return 0;
+    return ff_inflate_init(&s->zstream, avctx);
 }
 
 static av_cold int decode_close(AVCodecContext *avctx)
@@ -244,7 +236,7 @@ static av_cold int decode_close(AVCodecContext *avctx)
     WCMVContext *s = avctx->priv_data;
 
     av_frame_free(&s->prev_frame);
-    inflateEnd(&s->zstream);
+    ff_inflate_end(&s->zstream);
 
     return 0;
 }
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [FFmpeg-devel] [PATCH 06/21] avcodec/tscc: Use ff_inflate_init/end()
  2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
                   ` (3 preceding siblings ...)
  2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 05/21] avcodec/wcmv: " Andreas Rheinhardt
@ 2022-03-15 20:05 ` Andreas Rheinhardt
  2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 07/21] avcodec/rasc: " Andreas Rheinhardt
                   ` (14 subsequent siblings)
  19 siblings, 0 replies; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:05 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

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

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 configure         |  2 +-
 libavcodec/tscc.c | 36 ++++++++++++------------------------
 2 files changed, 13 insertions(+), 25 deletions(-)

diff --git a/configure b/configure
index fdc3f3d28c..ad5b5f1727 100755
--- a/configure
+++ b/configure
@@ -2953,7 +2953,7 @@ truehd_decoder_select="mlp_parser"
 truehd_encoder_select="lpc audio_frame_queue"
 truemotion2_decoder_select="bswapdsp"
 truespeech_decoder_select="bswapdsp"
-tscc_decoder_deps="zlib"
+tscc_decoder_select="inflate_wrapper"
 twinvq_decoder_select="mdct lsp sinewin"
 txd_decoder_select="texturedsp"
 utvideo_decoder_select="bswapdsp llviddsp"
diff --git a/libavcodec/tscc.c b/libavcodec/tscc.c
index 841f0c0d14..cba4d5bdc9 100644
--- a/libavcodec/tscc.c
+++ b/libavcodec/tscc.c
@@ -41,6 +41,7 @@
 #include "decode.h"
 #include "internal.h"
 #include "msrledec.h"
+#include "zlib_wrapper.h"
 
 #include <zlib.h>
 
@@ -57,8 +58,7 @@ typedef struct TsccContext {
     unsigned char* decomp_buf;
     GetByteContext gb;
     int height;
-    int zlib_init_ok;
-    z_stream zstream;
+    FFZStream zstream;
 
     uint32_t pal[256];
 } CamtasiaContext;
@@ -69,6 +69,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
     CamtasiaContext * const c = avctx->priv_data;
+    z_stream *const zstream = &c->zstream.zstream;
     AVFrame *frame = c->frame;
     int ret;
     int palette_has_changed = 0;
@@ -77,16 +78,16 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
         palette_has_changed = ff_copy_palette(c->pal, avpkt, avctx);
     }
 
-    ret = inflateReset(&c->zstream);
+    ret = inflateReset(zstream);
     if (ret != Z_OK) {
         av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
         return AVERROR_UNKNOWN;
     }
-    c->zstream.next_in   = buf;
-    c->zstream.avail_in  = buf_size;
-    c->zstream.next_out = c->decomp_buf;
-    c->zstream.avail_out = c->decomp_size;
-    ret = inflate(&c->zstream, Z_FINISH);
+    zstream->next_in   = buf;
+    zstream->avail_in  = buf_size;
+    zstream->next_out  = c->decomp_buf;
+    zstream->avail_out = c->decomp_size;
+    ret = inflate(zstream, Z_FINISH);
     // Z_DATA_ERROR means empty picture
     if (ret == Z_DATA_ERROR && !palette_has_changed) {
         return buf_size;
@@ -102,7 +103,7 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
 
     if (ret != Z_DATA_ERROR) {
         bytestream2_init(&c->gb, c->decomp_buf,
-                         c->decomp_size - c->zstream.avail_out);
+                         c->decomp_size - zstream->avail_out);
         ff_msrle_decode(avctx, frame, c->bpp, &c->gb);
     }
 
@@ -123,7 +124,6 @@ static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
 static av_cold int decode_init(AVCodecContext *avctx)
 {
     CamtasiaContext * const c = avctx->priv_data;
-    int zret; // Zlib return code
 
     c->avctx = avctx;
 
@@ -151,21 +151,11 @@ static av_cold int decode_init(AVCodecContext *avctx)
         }
     }
 
-    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;
-
     c->frame = av_frame_alloc();
     if (!c->frame)
         return AVERROR(ENOMEM);
 
-    return 0;
+    return ff_inflate_init(&c->zstream, avctx);
 }
 
 static av_cold int decode_end(AVCodecContext *avctx)
@@ -174,9 +164,7 @@ static av_cold int decode_end(AVCodecContext *avctx)
 
     av_freep(&c->decomp_buf);
     av_frame_free(&c->frame);
-
-    if (c->zlib_init_ok)
-        inflateEnd(&c->zstream);
+    ff_inflate_end(&c->zstream);
 
     return 0;
 }
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [FFmpeg-devel] [PATCH 07/21] avcodec/rasc: Use ff_inflate_init/end()
  2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
                   ` (4 preceding siblings ...)
  2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 06/21] avcodec/tscc: " Andreas Rheinhardt
@ 2022-03-15 20:05 ` Andreas Rheinhardt
  2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 08/21] avcodec/mwsc: " Andreas Rheinhardt
                   ` (13 subsequent siblings)
  19 siblings, 0 replies; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:05 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

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

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@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

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [FFmpeg-devel] [PATCH 08/21] avcodec/mwsc: Use ff_inflate_init/end()
  2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
                   ` (5 preceding siblings ...)
  2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 07/21] avcodec/rasc: " Andreas Rheinhardt
@ 2022-03-15 20:05 ` Andreas Rheinhardt
  2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 09/21] avcodec/mvha: " Andreas Rheinhardt
                   ` (12 subsequent siblings)
  19 siblings, 0 replies; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:05 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

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

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 configure         |  2 +-
 libavcodec/mwsc.c | 32 ++++++++++++--------------------
 2 files changed, 13 insertions(+), 21 deletions(-)

diff --git a/configure b/configure
index 464ddb019c..dd7adbfac4 100755
--- a/configure
+++ b/configure
@@ -2894,7 +2894,7 @@ mts2_decoder_select="jpegtables mss34dsp"
 mv30_decoder_select="aandcttables blockdsp"
 mvha_decoder_deps="zlib"
 mvha_decoder_select="llviddsp"
-mwsc_decoder_deps="zlib"
+mwsc_decoder_select="inflate_wrapper"
 mxpeg_decoder_select="mjpeg_decoder"
 nellymoser_decoder_select="mdct sinewin"
 nellymoser_encoder_select="audio_frame_queue mdct sinewin"
diff --git a/libavcodec/mwsc.c b/libavcodec/mwsc.c
index b62db67ff5..0ae99f6655 100644
--- a/libavcodec/mwsc.c
+++ b/libavcodec/mwsc.c
@@ -27,14 +27,15 @@
 #include "avcodec.h"
 #include "bytestream.h"
 #include "internal.h"
+#include "zlib_wrapper.h"
 
 #include <zlib.h>
 
 typedef struct MWSCContext {
     unsigned int      decomp_size;
     uint8_t          *decomp_buf;
-    z_stream          zstream;
     AVFrame          *prev_frame;
+    FFZStream         zstream;
 } MWSCContext;
 
 static int rle_uncompress(GetByteContext *gb, PutByteContext *pb, GetByteContext *gbp,
@@ -90,6 +91,7 @@ static int decode_frame(AVCodecContext *avctx,
                         AVPacket *avpkt)
 {
     MWSCContext *s = avctx->priv_data;
+    z_stream *const zstream = &s->zstream.zstream;
     AVFrame *frame = data;
     uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
@@ -98,16 +100,16 @@ static int decode_frame(AVCodecContext *avctx,
     PutByteContext pb;
     int ret;
 
-    ret = inflateReset(&s->zstream);
+    ret = inflateReset(zstream);
     if (ret != Z_OK) {
         av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
         return AVERROR_EXTERNAL;
     }
-    s->zstream.next_in   = buf;
-    s->zstream.avail_in  = buf_size;
-    s->zstream.next_out  = s->decomp_buf;
-    s->zstream.avail_out = s->decomp_size;
-    ret = inflate(&s->zstream, Z_FINISH);
+    zstream->next_in   = buf;
+    zstream->avail_in  = buf_size;
+    zstream->next_out  = s->decomp_buf;
+    zstream->avail_out = s->decomp_size;
+    ret = inflate(zstream, Z_FINISH);
     if (ret != Z_STREAM_END) {
         av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
         return AVERROR_EXTERNAL;
@@ -116,7 +118,7 @@ static int decode_frame(AVCodecContext *avctx,
     if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
         return ret;
 
-    bytestream2_init(&gb, s->decomp_buf, s->zstream.total_out);
+    bytestream2_init(&gb, s->decomp_buf, zstream->total_out);
     bytestream2_init(&gbp, s->prev_frame->data[0], avctx->height * s->prev_frame->linesize[0]);
     bytestream2_init_writer(&pb, frame->data[0], avctx->height * frame->linesize[0]);
 
@@ -138,7 +140,6 @@ static av_cold int decode_init(AVCodecContext *avctx)
 {
     MWSCContext *s = avctx->priv_data;
     int64_t size;
-    int zret;
 
     avctx->pix_fmt = AV_PIX_FMT_BGR24;
 
@@ -149,20 +150,11 @@ static av_cold int decode_init(AVCodecContext *avctx)
     if (!(s->decomp_buf = av_malloc(s->decomp_size)))
         return AVERROR(ENOMEM);
 
-    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->prev_frame = av_frame_alloc();
     if (!s->prev_frame)
         return AVERROR(ENOMEM);
 
-    return 0;
+    return ff_inflate_init(&s->zstream, avctx);
 }
 
 static av_cold int decode_close(AVCodecContext *avctx)
@@ -172,7 +164,7 @@ static av_cold int decode_close(AVCodecContext *avctx)
     av_frame_free(&s->prev_frame);
     av_freep(&s->decomp_buf);
     s->decomp_size = 0;
-    inflateEnd(&s->zstream);
+    ff_inflate_end(&s->zstream);
 
     return 0;
 }
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [FFmpeg-devel] [PATCH 09/21] avcodec/mvha: Use ff_inflate_init/end()
  2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
                   ` (6 preceding siblings ...)
  2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 08/21] avcodec/mwsc: " Andreas Rheinhardt
@ 2022-03-15 20:05 ` Andreas Rheinhardt
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 10/21] avcodec/mscc: " Andreas Rheinhardt
                   ` (11 subsequent siblings)
  19 siblings, 0 replies; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:05 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

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

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 configure         |  3 +--
 libavcodec/mvha.c | 30 +++++++++++-------------------
 2 files changed, 12 insertions(+), 21 deletions(-)

diff --git a/configure b/configure
index dd7adbfac4..46b02275ad 100755
--- a/configure
+++ b/configure
@@ -2892,8 +2892,7 @@ msmpeg4v3_encoder_select="h263_encoder"
 mss2_decoder_select="mpegvideodec qpeldsp vc1_decoder"
 mts2_decoder_select="jpegtables mss34dsp"
 mv30_decoder_select="aandcttables blockdsp"
-mvha_decoder_deps="zlib"
-mvha_decoder_select="llviddsp"
+mvha_decoder_select="inflate_wrapper llviddsp"
 mwsc_decoder_select="inflate_wrapper"
 mxpeg_decoder_select="mjpeg_decoder"
 nellymoser_decoder_select="mdct sinewin"
diff --git a/libavcodec/mvha.c b/libavcodec/mvha.c
index 4048e4625c..05ddcfeb8f 100644
--- a/libavcodec/mvha.c
+++ b/libavcodec/mvha.c
@@ -32,6 +32,7 @@
 #include "get_bits.h"
 #include "internal.h"
 #include "lossless_videodsp.h"
+#include "zlib_wrapper.h"
 
 #include <zlib.h>
 
@@ -43,7 +44,7 @@ typedef struct MVHAContext {
     uint32_t          prob[256];
     VLC               vlc;
 
-    z_stream          zstream;
+    FFZStream         zstream;
     LLVidDSPContext   llviddsp;
 } MVHAContext;
 
@@ -168,21 +169,22 @@ static int decode_frame(AVCodecContext *avctx,
         return ret;
 
     if (type == MKTAG('L','Z','Y','V')) {
-        ret = inflateReset(&s->zstream);
+        z_stream *const zstream = &s->zstream.zstream;
+        ret = inflateReset(zstream);
         if (ret != Z_OK) {
             av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
             return AVERROR_EXTERNAL;
         }
 
-        s->zstream.next_in  = avpkt->data + 8;
-        s->zstream.avail_in = avpkt->size - 8;
+        zstream->next_in  = avpkt->data + 8;
+        zstream->avail_in = avpkt->size - 8;
 
         for (int p = 0; p < 3; p++) {
             for (int y = 0; y < avctx->height; y++) {
-                s->zstream.next_out  = frame->data[p] + (avctx->height - y - 1) * frame->linesize[p];
-                s->zstream.avail_out = avctx->width >> (p > 0);
+                zstream->next_out  = frame->data[p] + (avctx->height - y - 1) * frame->linesize[p];
+                zstream->avail_out = avctx->width >> (p > 0);
 
-                ret = inflate(&s->zstream, Z_SYNC_FLUSH);
+                ret = inflate(zstream, Z_SYNC_FLUSH);
                 if (ret != Z_OK && ret != Z_STREAM_END) {
                     av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
                     return AVERROR_EXTERNAL;
@@ -279,29 +281,19 @@ static int decode_frame(AVCodecContext *avctx,
 static av_cold int decode_init(AVCodecContext *avctx)
 {
     MVHAContext *s = avctx->priv_data;
-    int zret;
 
     avctx->pix_fmt = AV_PIX_FMT_YUV422P;
 
-    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;
-    }
-
     ff_llviddsp_init(&s->llviddsp);
 
-    return 0;
+    return ff_inflate_init(&s->zstream, avctx);
 }
 
 static av_cold int decode_close(AVCodecContext *avctx)
 {
     MVHAContext *s = avctx->priv_data;
 
-    inflateEnd(&s->zstream);
+    ff_inflate_end(&s->zstream);
     ff_free_vlc(&s->vlc);
 
     return 0;
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [FFmpeg-devel] [PATCH 10/21] avcodec/mscc: Use ff_inflate_init/end()
  2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
                   ` (7 preceding siblings ...)
  2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 09/21] avcodec/mvha: " Andreas Rheinhardt
@ 2022-03-15 20:06 ` Andreas Rheinhardt
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 11/21] avcodec/lcldec: Use ff_inflate_init/end(), cleanup generically Andreas Rheinhardt
                   ` (10 subsequent siblings)
  19 siblings, 0 replies; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:06 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

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

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 configure         |  4 ++--
 libavcodec/mscc.c | 33 +++++++++++++--------------------
 2 files changed, 15 insertions(+), 22 deletions(-)

diff --git a/configure b/configure
index 46b02275ad..c79908c1bd 100755
--- a/configure
+++ b/configure
@@ -2883,7 +2883,7 @@ mpeg2video_encoder_select="mpegvideoenc h263dsp"
 mpeg4_decoder_select="h263_decoder mpeg4video_parser"
 mpeg4_encoder_select="h263_encoder"
 msa1_decoder_select="mss34dsp"
-mscc_decoder_deps="zlib"
+mscc_decoder_select="inflate_wrapper"
 msmpeg4v1_decoder_select="h263_decoder"
 msmpeg4v2_decoder_select="h263_decoder"
 msmpeg4v2_encoder_select="h263_encoder"
@@ -2935,7 +2935,7 @@ sonic_ls_encoder_select="golomb rangecoder"
 sp5x_decoder_select="mjpeg_decoder"
 speedhq_decoder_select="mpegvideo"
 speedhq_encoder_select="mpegvideoenc"
-srgc_decoder_deps="zlib"
+srgc_decoder_select="inflate_wrapper"
 svq1_decoder_select="hpeldsp"
 svq1_encoder_select="hpeldsp me_cmp mpegvideoenc"
 svq3_decoder_select="golomb h264dsp h264parse h264pred hpeldsp tpeldsp videodsp"
diff --git a/libavcodec/mscc.c b/libavcodec/mscc.c
index d9a6de094a..19cfce796e 100644
--- a/libavcodec/mscc.c
+++ b/libavcodec/mscc.c
@@ -27,6 +27,7 @@
 #include "avcodec.h"
 #include "bytestream.h"
 #include "internal.h"
+#include "zlib_wrapper.h"
 
 #include <zlib.h>
 
@@ -36,7 +37,7 @@ typedef struct MSCCContext {
     uint8_t          *decomp_buf;
     unsigned int      uncomp_size;
     uint8_t          *uncomp_buf;
-    z_stream          zstream;
+    FFZStream         zstream;
 
     uint32_t          pal[256];
 } MSCCContext;
@@ -132,6 +133,7 @@ static int decode_frame(AVCodecContext *avctx,
                         AVPacket *avpkt)
 {
     MSCCContext *s = avctx->priv_data;
+    z_stream *const zstream = &s->zstream.zstream;
     AVFrame *frame = data;
     uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
@@ -166,22 +168,22 @@ static int decode_frame(AVCodecContext *avctx,
         memcpy(frame->data[1], s->pal, AVPALETTE_SIZE);
     }
 
-    ret = inflateReset(&s->zstream);
+    ret = inflateReset(zstream);
     if (ret != Z_OK) {
         av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", ret);
         return AVERROR_UNKNOWN;
     }
-    s->zstream.next_in   = buf;
-    s->zstream.avail_in  = buf_size;
-    s->zstream.next_out  = s->decomp_buf;
-    s->zstream.avail_out = s->decomp_size;
-    ret = inflate(&s->zstream, Z_FINISH);
+    zstream->next_in   = buf;
+    zstream->avail_in  = buf_size;
+    zstream->next_out  = s->decomp_buf;
+    zstream->avail_out = s->decomp_size;
+    ret = inflate(zstream, Z_FINISH);
     if (ret != Z_STREAM_END) {
         av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", ret);
         return AVERROR_UNKNOWN;
     }
 
-    bytestream2_init(&gb, s->decomp_buf, s->zstream.total_out);
+    bytestream2_init(&gb, s->decomp_buf, zstream->total_out);
     bytestream2_init_writer(&pb, s->uncomp_buf, s->uncomp_size);
 
     ret = rle_uncompress(avctx, &gb, &pb);
@@ -204,7 +206,7 @@ static int decode_frame(AVCodecContext *avctx,
 static av_cold int decode_init(AVCodecContext *avctx)
 {
     MSCCContext *s = avctx->priv_data;
-    int stride, zret;
+    int stride;
 
     switch (avctx->bits_per_coded_sample) {
     case  8: avctx->pix_fmt = AV_PIX_FMT_PAL8;   break;
@@ -227,16 +229,7 @@ static av_cold int decode_init(AVCodecContext *avctx)
     if (!(s->uncomp_buf = av_malloc(s->uncomp_size)))
         return AVERROR(ENOMEM);
 
-    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_UNKNOWN;
-    }
-
-    return 0;
+    return ff_inflate_init(&s->zstream, avctx);
 }
 
 static av_cold int decode_close(AVCodecContext *avctx)
@@ -247,7 +240,7 @@ static av_cold int decode_close(AVCodecContext *avctx)
     s->decomp_size = 0;
     av_freep(&s->uncomp_buf);
     s->uncomp_size = 0;
-    inflateEnd(&s->zstream);
+    ff_inflate_end(&s->zstream);
 
     return 0;
 }
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [FFmpeg-devel] [PATCH 11/21] avcodec/lcldec: Use ff_inflate_init/end(), cleanup generically
  2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
                   ` (8 preceding siblings ...)
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 10/21] avcodec/mscc: " Andreas Rheinhardt
@ 2022-03-15 20:06 ` Andreas Rheinhardt
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 12/21] avcodec/flashsv: Use ff_inflate_init/end() Andreas Rheinhardt
                   ` (9 subsequent siblings)
  19 siblings, 0 replies; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:06 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

Returns better error messages in case of error and deduplicates
the inflateInit() code and also allows to cleanup generically
in case of errors as it is save to call ff_inflate_end() if
ff_inflate_init() has not been called successfully.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 configure           |  2 +-
 libavcodec/lcldec.c | 43 +++++++++++++++++--------------------------
 2 files changed, 18 insertions(+), 27 deletions(-)

diff --git a/configure b/configure
index c79908c1bd..c56b7ba85b 100755
--- a/configure
+++ b/configure
@@ -2990,7 +2990,7 @@ xma1_decoder_select="wmapro_decoder"
 xma2_decoder_select="wmapro_decoder"
 ylc_decoder_select="bswapdsp"
 zerocodec_decoder_select="inflate_wrapper"
-zlib_decoder_deps="zlib"
+zlib_decoder_select="inflate_wrapper"
 zlib_encoder_deps="zlib"
 zmbv_decoder_select="inflate_wrapper"
 zmbv_encoder_deps="zlib"
diff --git a/libavcodec/lcldec.c b/libavcodec/lcldec.c
index f3b7a8ac1b..8a66065800 100644
--- a/libavcodec/lcldec.c
+++ b/libavcodec/lcldec.c
@@ -49,6 +49,7 @@
 #include "thread.h"
 
 #if CONFIG_ZLIB_DECODER
+#include "zlib_wrapper.h"
 #include <zlib.h>
 #endif
 
@@ -64,7 +65,7 @@ typedef struct LclDecContext {
     // Decompression buffer
     unsigned char* decomp_buf;
 #if CONFIG_ZLIB_DECODER
-    z_stream zstream;
+    FFZStream zstream;
 #endif
 } LclDecContext;
 
@@ -131,26 +132,27 @@ static unsigned int mszh_decomp(const unsigned char * srcptr, int srclen, unsign
 static int zlib_decomp(AVCodecContext *avctx, const uint8_t *src, int src_len, int offset, int expected)
 {
     LclDecContext *c = avctx->priv_data;
-    int zret = inflateReset(&c->zstream);
+    z_stream *const zstream = &c->zstream.zstream;
+    int zret = inflateReset(zstream);
     if (zret != Z_OK) {
         av_log(avctx, AV_LOG_ERROR, "Inflate reset error: %d\n", zret);
         return AVERROR_UNKNOWN;
     }
-    c->zstream.next_in = src;
-    c->zstream.avail_in = src_len;
-    c->zstream.next_out = c->decomp_buf + offset;
-    c->zstream.avail_out = c->decomp_size - offset;
-    zret = inflate(&c->zstream, Z_FINISH);
+    zstream->next_in   = src;
+    zstream->avail_in  = src_len;
+    zstream->next_out  = c->decomp_buf + offset;
+    zstream->avail_out = c->decomp_size - offset;
+    zret = inflate(zstream, Z_FINISH);
     if (zret != Z_OK && zret != Z_STREAM_END) {
         av_log(avctx, AV_LOG_ERROR, "Inflate error: %d\n", zret);
         return AVERROR_UNKNOWN;
     }
-    if (expected != (unsigned int)c->zstream.total_out) {
+    if (expected != (unsigned int)zstream->total_out) {
         av_log(avctx, AV_LOG_ERROR, "Decoded size differs (%d != %lu)\n",
-               expected, c->zstream.total_out);
+               expected, zstream->total_out);
         return AVERROR_UNKNOWN;
     }
-    return c->zstream.total_out;
+    return zstream->total_out;
 }
 #endif
 
@@ -606,18 +608,8 @@ static av_cold int decode_init(AVCodecContext *avctx)
 
     /* If needed init zlib */
 #if CONFIG_ZLIB_DECODER
-    if (avctx->codec_id == AV_CODEC_ID_ZLIB) {
-        int zret;
-        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);
-            av_freep(&c->decomp_buf);
-            return AVERROR_UNKNOWN;
-        }
-    }
+    if (avctx->codec_id == AV_CODEC_ID_ZLIB)
+        return ff_inflate_init(&c->zstream, avctx);
 #endif
 
     return 0;
@@ -629,8 +621,7 @@ static av_cold int decode_end(AVCodecContext *avctx)
 
     av_freep(&c->decomp_buf);
 #if CONFIG_ZLIB_DECODER
-    if (avctx->codec_id == AV_CODEC_ID_ZLIB)
-        inflateEnd(&c->zstream);
+    ff_inflate_end(&c->zstream);
 #endif
 
     return 0;
@@ -647,7 +638,7 @@ const AVCodec ff_mszh_decoder = {
     .close          = decode_end,
     .decode         = decode_frame,
     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
-    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
 };
 #endif
 
@@ -662,6 +653,6 @@ const AVCodec ff_zlib_decoder = {
     .close          = decode_end,
     .decode         = decode_frame,
     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
-    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
 };
 #endif
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [FFmpeg-devel] [PATCH 12/21] avcodec/flashsv: Use ff_inflate_init/end()
  2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
                   ` (9 preceding siblings ...)
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 11/21] avcodec/lcldec: Use ff_inflate_init/end(), cleanup generically Andreas Rheinhardt
@ 2022-03-15 20:06 ` Andreas Rheinhardt
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 13/21] avcodec/zlib_wrapper: Use our allocation, freeing functions Andreas Rheinhardt
                   ` (8 subsequent siblings)
  19 siblings, 0 replies; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:06 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

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

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@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

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [FFmpeg-devel] [PATCH 13/21] avcodec/zlib_wrapper: Use our allocation, freeing functions
  2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
                   ` (10 preceding siblings ...)
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 12/21] avcodec/flashsv: Use ff_inflate_init/end() Andreas Rheinhardt
@ 2022-03-15 20:06 ` Andreas Rheinhardt
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 14/21] avcodec/lscrdec: Don't open and close z_streams unnecessarily Andreas Rheinhardt
                   ` (7 subsequent siblings)
  19 siblings, 0 replies; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:06 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/zlib_wrapper.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/libavcodec/zlib_wrapper.c b/libavcodec/zlib_wrapper.c
index b15d5be2b8..5b93c2c74f 100644
--- a/libavcodec/zlib_wrapper.c
+++ b/libavcodec/zlib_wrapper.c
@@ -23,8 +23,19 @@
 
 #include "libavutil/error.h"
 #include "libavutil/log.h"
+#include "libavutil/mem.h"
 #include "zlib_wrapper.h"
 
+static void *alloc_wrapper(void *opaque, uInt items, uInt size)
+{
+    return av_malloc_array(items, size);
+}
+
+static void free_wrapper(void *opaque, void *ptr)
+{
+    av_free(ptr);
+}
+
 int ff_inflate_init(FFZStream *z, void *logctx)
 {
     z_stream *const zstream = &z->zstream;
@@ -33,8 +44,8 @@ int ff_inflate_init(FFZStream *z, void *logctx)
     z->inited = 0;
     zstream->next_in  = Z_NULL;
     zstream->avail_in = 0;
-    zstream->zalloc   = Z_NULL;
-    zstream->zfree    = Z_NULL;
+    zstream->zalloc   = alloc_wrapper;
+    zstream->zfree    = free_wrapper;
     zstream->opaque   = Z_NULL;
 
     zret = inflateInit(zstream);
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [FFmpeg-devel] [PATCH 14/21] avcodec/lscrdec: Don't open and close z_streams unnecessarily
  2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
                   ` (11 preceding siblings ...)
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 13/21] avcodec/zlib_wrapper: Use our allocation, freeing functions Andreas Rheinhardt
@ 2022-03-15 20:06 ` Andreas Rheinhardt
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 15/21] avcodec/pngdec: " Andreas Rheinhardt
                   ` (6 subsequent siblings)
  19 siblings, 0 replies; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:06 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

Instead reuse and reset a single z_stream.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 configure            |  2 +-
 libavcodec/lscrdec.c | 84 +++++++++++++++++---------------------------
 2 files changed, 33 insertions(+), 53 deletions(-)

diff --git a/configure b/configure
index e3d2f590cd..0e5923fced 100755
--- a/configure
+++ b/configure
@@ -2850,7 +2850,7 @@ jpegls_decoder_select="mjpeg_decoder"
 jv_decoder_select="blockdsp"
 lagarith_decoder_select="llviddsp"
 ljpeg_encoder_select="idctdsp jpegtables"
-lscr_decoder_deps="zlib"
+lscr_decoder_select="inflate_wrapper"
 magicyuv_decoder_select="llviddsp"
 magicyuv_encoder_select="llvidencdsp"
 mdec_decoder_select="blockdsp bswapdsp idctdsp mpegvideo"
diff --git a/libavcodec/lscrdec.c b/libavcodec/lscrdec.c
index 18f46bd27c..58dac6e587 100644
--- a/libavcodec/lscrdec.c
+++ b/libavcodec/lscrdec.c
@@ -32,6 +32,7 @@
 #include "packet.h"
 #include "png.h"
 #include "pngdsp.h"
+#include "zlib_wrapper.h"
 
 typedef struct LSCRContext {
     PNGDSPContext   dsp;
@@ -52,7 +53,7 @@ typedef struct LSCRContext {
     int             cur_h;
     int             y;
 
-    z_stream        zstream;
+    FFZStream       zstream;
 } LSCRContext;
 
 static void handle_row(LSCRContext *s)
@@ -71,11 +72,11 @@ static void handle_row(LSCRContext *s)
     s->y++;
 }
 
-static int decode_idat(LSCRContext *s, int length)
+static int decode_idat(LSCRContext *s, z_stream *zstream, int length)
 {
     int ret;
-    s->zstream.avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
-    s->zstream.next_in  = s->gb.buffer;
+    zstream->avail_in = FFMIN(length, bytestream2_get_bytes_left(&s->gb));
+    zstream->next_in  = s->gb.buffer;
 
     if (length <= 0)
         return AVERROR_INVALIDDATA;
@@ -83,22 +84,22 @@ static int decode_idat(LSCRContext *s, int length)
     bytestream2_skip(&s->gb, length);
 
     /* decode one line if possible */
-    while (s->zstream.avail_in > 0) {
-        ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
+    while (zstream->avail_in > 0) {
+        ret = inflate(zstream, Z_PARTIAL_FLUSH);
         if (ret != Z_OK && ret != Z_STREAM_END) {
             av_log(s->avctx, AV_LOG_ERROR, "inflate returned error %d\n", ret);
             return AVERROR_EXTERNAL;
         }
-        if (s->zstream.avail_out == 0) {
+        if (zstream->avail_out == 0) {
             if (s->y < s->cur_h) {
                 handle_row(s);
             }
-            s->zstream.avail_out = s->crow_size;
-            s->zstream.next_out  = s->crow_buf;
+            zstream->avail_out = s->crow_size;
+            zstream->next_out  = s->crow_buf;
         }
-        if (ret == Z_STREAM_END && s->zstream.avail_in > 0) {
+        if (ret == Z_STREAM_END && zstream->avail_in > 0) {
             av_log(s->avctx, AV_LOG_WARNING,
-                   "%d undecompressed bytes left in buffer\n", s->zstream.avail_in);
+                   "%d undecompressed bytes left in buffer\n", zstream->avail_in);
             return 0;
         }
     }
@@ -131,18 +132,12 @@ static int decode_frame_lscr(AVCodecContext *avctx,
         return ret;
 
     for (int b = 0; b < nb_blocks; b++) {
+        z_stream *const zstream = &s->zstream.zstream;
         int x, y, x2, y2, w, h, left;
         uint32_t csize, size;
 
-        s->zstream.zalloc = ff_png_zalloc;
-        s->zstream.zfree  = ff_png_zfree;
-        s->zstream.opaque = NULL;
-
-        if ((ret = inflateInit(&s->zstream)) != Z_OK) {
-            av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
-            ret = AVERROR_EXTERNAL;
-            goto end;
-        }
+        if (inflateReset(zstream) != Z_OK)
+            return AVERROR_EXTERNAL;
 
         bytestream2_seek(gb, 2 + b * 12, SEEK_SET);
 
@@ -154,10 +149,8 @@ static int decode_frame_lscr(AVCodecContext *avctx,
         s->cur_h = h = y2-y;
 
         if (w <= 0 || x < 0 || x >= avctx->width || w + x > avctx->width ||
-            h <= 0 || y < 0 || y >= avctx->height || h + y > avctx->height) {
-            ret = AVERROR_INVALIDDATA;
-            goto end;
-        }
+            h <= 0 || y < 0 || y >= avctx->height || h + y > avctx->height)
+            return AVERROR_INVALIDDATA;
 
         size = bytestream2_get_le32(gb);
 
@@ -168,10 +161,8 @@ static int decode_frame_lscr(AVCodecContext *avctx,
 
         bytestream2_seek(gb, 2 + nb_blocks * 12 + offset, SEEK_SET);
         csize = bytestream2_get_be32(gb);
-        if (bytestream2_get_le32(gb) != MKTAG('I', 'D', 'A', 'T')) {
-            ret = AVERROR_INVALIDDATA;
-            goto end;
-        }
+        if (bytestream2_get_le32(gb) != MKTAG('I', 'D', 'A', 'T'))
+            return AVERROR_INVALIDDATA;
 
         offset += size;
         left = size;
@@ -180,40 +171,32 @@ static int decode_frame_lscr(AVCodecContext *avctx,
         s->row_size          = w * 3;
 
         av_fast_padded_malloc(&s->buffer, &s->buffer_size, s->row_size + 16);
-        if (!s->buffer) {
-            ret = AVERROR(ENOMEM);
-            goto end;
-        }
+        if (!s->buffer)
+            return AVERROR(ENOMEM);
 
         av_fast_padded_malloc(&s->last_row, &s->last_row_size, s->row_size);
-        if (!s->last_row) {
-            ret = AVERROR(ENOMEM);
-            goto end;
-        }
+        if (!s->last_row)
+            return AVERROR(ENOMEM);
 
         s->crow_size         = w * 3 + 1;
         s->crow_buf          = s->buffer + 15;
-        s->zstream.avail_out = s->crow_size;
-        s->zstream.next_out  = s->crow_buf;
+        zstream->avail_out   = s->crow_size;
+        zstream->next_out    = s->crow_buf;
         s->image_buf         = frame->data[0] + (avctx->height - y - 1) * frame->linesize[0] + x * 3;
         s->image_linesize    =-frame->linesize[0];
 
         while (left > 16) {
-            ret = decode_idat(s, csize);
+            ret = decode_idat(s, zstream, csize);
             if (ret < 0)
-                goto end;
+                return ret;
             left -= csize + 16;
             if (left > 16) {
                 bytestream2_skip(gb, 4);
                 csize = bytestream2_get_be32(gb);
-                if (bytestream2_get_le32(gb) != MKTAG('I', 'D', 'A', 'T')) {
-                    ret = AVERROR_INVALIDDATA;
-                    goto end;
-                }
+                if (bytestream2_get_le32(gb) != MKTAG('I', 'D', 'A', 'T'))
+                    return AVERROR_INVALIDDATA;
             }
         }
-
-        inflateEnd(&s->zstream);
     }
 
     frame->pict_type = frame->key_frame ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
@@ -222,11 +205,7 @@ static int decode_frame_lscr(AVCodecContext *avctx,
         return ret;
 
     *got_frame = 1;
-end:
-    inflateEnd(&s->zstream);
 
-    if (ret < 0)
-        return ret;
     return avpkt->size;
 }
 
@@ -237,6 +216,7 @@ static int lscr_decode_close(AVCodecContext *avctx)
     av_frame_free(&s->last_picture);
     av_freep(&s->buffer);
     av_freep(&s->last_row);
+    ff_inflate_end(&s->zstream);
 
     return 0;
 }
@@ -255,7 +235,7 @@ static int lscr_decode_init(AVCodecContext *avctx)
 
     ff_pngdsp_init(&s->dsp);
 
-    return 0;
+    return ff_inflate_init(&s->zstream, avctx);
 }
 
 static void lscr_decode_flush(AVCodecContext *avctx)
@@ -275,5 +255,5 @@ const AVCodec ff_lscr_decoder = {
     .decode         = decode_frame_lscr,
     .flush          = lscr_decode_flush,
     .capabilities   = AV_CODEC_CAP_DR1,
-    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
+    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
 };
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [FFmpeg-devel] [PATCH 15/21] avcodec/pngdec: Don't open and close z_streams unnecessarily
  2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
                   ` (12 preceding siblings ...)
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 14/21] avcodec/lscrdec: Don't open and close z_streams unnecessarily Andreas Rheinhardt
@ 2022-03-15 20:06 ` Andreas Rheinhardt
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 16/21] avcodec/pngenc: Don't use deflateInit2() with default parameters Andreas Rheinhardt
                   ` (5 subsequent siblings)
  19 siblings, 0 replies; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:06 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

Instead reuse and reset a single z_stream.
Also use FFZStream in decode_zbuf(), because it has nicer error
messages.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 configure           |   4 +-
 libavcodec/pngdec.c | 108 +++++++++++++++++++-------------------------
 2 files changed, 48 insertions(+), 64 deletions(-)

diff --git a/configure b/configure
index 0e5923fced..74485fb713 100755
--- a/configure
+++ b/configure
@@ -2758,7 +2758,7 @@ amrwb_decoder_select="lsp"
 amv_decoder_select="sp5x_decoder exif"
 amv_encoder_select="jpegtables mpegvideoenc"
 ape_decoder_select="bswapdsp llauddsp"
-apng_decoder_deps="zlib"
+apng_decoder_select="inflate_wrapper"
 apng_encoder_deps="zlib"
 apng_encoder_select="llvidencdsp"
 aptx_decoder_select="audio_frame_queue"
@@ -2903,7 +2903,7 @@ on2avc_decoder_select="mdct"
 opus_decoder_deps="swresample"
 opus_decoder_select="mdct15"
 opus_encoder_select="audio_frame_queue mdct15"
-png_decoder_deps="zlib"
+png_decoder_select="inflate_wrapper"
 png_encoder_deps="zlib"
 png_encoder_select="llvidencdsp"
 prores_decoder_select="blockdsp idctdsp"
diff --git a/libavcodec/pngdec.c b/libavcodec/pngdec.c
index a335a29b08..67931aad81 100644
--- a/libavcodec/pngdec.c
+++ b/libavcodec/pngdec.c
@@ -37,6 +37,7 @@
 #include "pngdsp.h"
 #include "thread.h"
 #include "threadframe.h"
+#include "zlib_wrapper.h"
 
 #include <zlib.h>
 
@@ -105,7 +106,7 @@ typedef struct PNGDecContext {
     int row_size; /* decompressed row size */
     int pass_row_size; /* decompress row size of the current pass */
     int y;
-    z_stream zstream;
+    FFZStream zstream;
 } PNGDecContext;
 
 /* Mask to determine which pixels are valid in a pass */
@@ -427,27 +428,28 @@ the_end:;
 static int png_decode_idat(PNGDecContext *s, GetByteContext *gb,
                            uint8_t *dst, ptrdiff_t dst_stride)
 {
+    z_stream *const zstream = &s->zstream.zstream;
     int ret;
-    s->zstream.avail_in = bytestream2_get_bytes_left(gb);
-    s->zstream.next_in  = gb->buffer;
+    zstream->avail_in = bytestream2_get_bytes_left(gb);
+    zstream->next_in  = gb->buffer;
 
     /* decode one line if possible */
-    while (s->zstream.avail_in > 0) {
-        ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
+    while (zstream->avail_in > 0) {
+        ret = inflate(zstream, Z_PARTIAL_FLUSH);
         if (ret != Z_OK && ret != Z_STREAM_END) {
             av_log(s->avctx, AV_LOG_ERROR, "inflate returned error %d\n", ret);
             return AVERROR_EXTERNAL;
         }
-        if (s->zstream.avail_out == 0) {
+        if (zstream->avail_out == 0) {
             if (!(s->pic_state & PNG_ALLIMAGE)) {
                 png_handle_row(s, dst, dst_stride);
             }
-            s->zstream.avail_out = s->crow_size;
-            s->zstream.next_out  = s->crow_buf;
+            zstream->avail_out = s->crow_size;
+            zstream->next_out  = s->crow_buf;
         }
-        if (ret == Z_STREAM_END && s->zstream.avail_in > 0) {
+        if (ret == Z_STREAM_END && zstream->avail_in > 0) {
             av_log(s->avctx, AV_LOG_WARNING,
-                   "%d undecompressed bytes left in buffer\n", s->zstream.avail_in);
+                   "%d undecompressed bytes left in buffer\n", zstream->avail_in);
             return 0;
         }
     }
@@ -455,45 +457,43 @@ static int png_decode_idat(PNGDecContext *s, GetByteContext *gb,
 }
 
 static int decode_zbuf(AVBPrint *bp, const uint8_t *data,
-                       const uint8_t *data_end)
+                       const uint8_t *data_end, void *logctx)
 {
-    z_stream zstream;
+    FFZStream z;
+    z_stream *const zstream = &z.zstream;
     unsigned char *buf;
     unsigned buf_size;
-    int ret;
+    int ret = ff_inflate_init(&z, logctx);
+    if (ret < 0)
+        return ret;
 
-    zstream.zalloc = ff_png_zalloc;
-    zstream.zfree  = ff_png_zfree;
-    zstream.opaque = NULL;
-    if (inflateInit(&zstream) != Z_OK)
-        return AVERROR_EXTERNAL;
-    zstream.next_in  = data;
-    zstream.avail_in = data_end - data;
+    zstream->next_in  = data;
+    zstream->avail_in = data_end - data;
     av_bprint_init(bp, 0, AV_BPRINT_SIZE_UNLIMITED);
 
-    while (zstream.avail_in > 0) {
+    while (zstream->avail_in > 0) {
         av_bprint_get_buffer(bp, 2, &buf, &buf_size);
         if (buf_size < 2) {
             ret = AVERROR(ENOMEM);
             goto fail;
         }
-        zstream.next_out  = buf;
-        zstream.avail_out = buf_size - 1;
-        ret = inflate(&zstream, Z_PARTIAL_FLUSH);
+        zstream->next_out  = buf;
+        zstream->avail_out = buf_size - 1;
+        ret = inflate(zstream, Z_PARTIAL_FLUSH);
         if (ret != Z_OK && ret != Z_STREAM_END) {
             ret = AVERROR_EXTERNAL;
             goto fail;
         }
-        bp->len += zstream.next_out - buf;
+        bp->len += zstream->next_out - buf;
         if (ret == Z_STREAM_END)
             break;
     }
-    inflateEnd(&zstream);
+    ff_inflate_end(&z);
     bp->str[bp->len] = 0;
     return 0;
 
 fail:
-    inflateEnd(&zstream);
+    ff_inflate_end(&z);
     av_bprint_finalize(bp, NULL);
     return ret;
 }
@@ -543,7 +543,7 @@ static int decode_text_chunk(PNGDecContext *s, GetByteContext *gb, int compresse
         method = *(data++);
         if (method)
             return AVERROR_INVALIDDATA;
-        if ((ret = decode_zbuf(&bp, data, data_end)) < 0)
+        if ((ret = decode_zbuf(&bp, data, data_end, s->avctx)) < 0)
             return ret;
         text_len = bp.len;
         ret = av_bprint_finalize(&bp, (char **)&text);
@@ -765,8 +765,8 @@ static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s,
 
         /* we want crow_buf+1 to be 16-byte aligned */
         s->crow_buf          = s->buffer + 15;
-        s->zstream.avail_out = s->crow_size;
-        s->zstream.next_out  = s->crow_buf;
+        s->zstream.zstream.avail_out = s->crow_size;
+        s->zstream.zstream.next_out  = s->crow_buf;
     }
 
     s->pic_state |= PNG_IDAT;
@@ -875,7 +875,7 @@ static int decode_iccp_chunk(PNGDecContext *s, GetByteContext *gb, AVFrame *f)
         goto fail;
     }
 
-    if ((ret = decode_zbuf(&bp, gb->buffer, gb->buffer_end)) < 0)
+    if ((ret = decode_zbuf(&bp, gb->buffer, gb->buffer_end, s->avctx)) < 0)
         return ret;
 
     av_freep(&s->iccp_data);
@@ -1540,15 +1540,10 @@ static int decode_frame_png(AVCodecContext *avctx,
     s->hdr_state = 0;
     s->pic_state = 0;
 
-    /* init the zlib */
-    s->zstream.zalloc = ff_png_zalloc;
-    s->zstream.zfree  = ff_png_zfree;
-    s->zstream.opaque = NULL;
-    ret = inflateInit(&s->zstream);
-    if (ret != Z_OK) {
-        av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
+    /* Reset z_stream */
+    ret = inflateReset(&s->zstream.zstream);
+    if (ret != Z_OK)
         return AVERROR_EXTERNAL;
-    }
 
     if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
         goto the_end;
@@ -1572,7 +1567,6 @@ static int decode_frame_png(AVCodecContext *avctx,
 
     ret = bytestream2_tell(&s->gb);
 the_end:
-    inflateEnd(&s->zstream);
     s->crow_buf = NULL;
     return ret;
 }
@@ -1594,37 +1588,30 @@ static int decode_frame_apng(AVCodecContext *avctx,
         if (!avctx->extradata_size)
             return AVERROR_INVALIDDATA;
 
-        /* only init fields, there is no zlib use in extradata */
-        s->zstream.zalloc = ff_png_zalloc;
-        s->zstream.zfree  = ff_png_zfree;
-
+        if ((ret = inflateReset(&s->zstream.zstream)) != Z_OK)
+            return AVERROR_EXTERNAL;
         bytestream2_init(&s->gb, avctx->extradata, avctx->extradata_size);
         if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
-            goto end;
+            return ret;
     }
 
     /* reset state for a new frame */
-    if ((ret = inflateInit(&s->zstream)) != Z_OK) {
-        av_log(avctx, AV_LOG_ERROR, "inflateInit returned error %d\n", ret);
-        ret = AVERROR_EXTERNAL;
-        goto end;
-    }
+    if ((ret = inflateReset(&s->zstream.zstream)) != Z_OK)
+        return AVERROR_EXTERNAL;
     s->y = 0;
     s->pic_state = 0;
     bytestream2_init(&s->gb, avpkt->data, avpkt->size);
     if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
-        goto end;
+        return ret;
 
     if (!(s->pic_state & PNG_ALLIMAGE))
         av_log(avctx, AV_LOG_WARNING, "Frame did not contain a complete image\n");
-    if (!(s->pic_state & (PNG_ALLIMAGE|PNG_IDAT))) {
-        ret = AVERROR_INVALIDDATA;
-        goto end;
-    }
+    if (!(s->pic_state & (PNG_ALLIMAGE|PNG_IDAT)))
+        return AVERROR_INVALIDDATA;
 
     ret = output_frame(s, dst_frame, s->picture.f);
     if (ret < 0)
-        goto end;
+        return ret;
 
     if (!(avctx->active_thread_type & FF_THREAD_FRAME)) {
         if (s->dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
@@ -1636,11 +1623,7 @@ static int decode_frame_apng(AVCodecContext *avctx,
     }
 
     *got_frame = 1;
-    ret = bytestream2_tell(&s->gb);
-
-end:
-    inflateEnd(&s->zstream);
-    return ret;
+    return bytestream2_tell(&s->gb);
 }
 #endif
 
@@ -1706,7 +1689,7 @@ static av_cold int png_dec_init(AVCodecContext *avctx)
 
     ff_pngdsp_init(&s->dsp);
 
-    return 0;
+    return ff_inflate_init(&s->zstream, avctx);
 }
 
 static av_cold int png_dec_end(AVCodecContext *avctx)
@@ -1727,6 +1710,7 @@ static av_cold int png_dec_end(AVCodecContext *avctx)
 
     av_freep(&s->iccp_data);
     av_dict_free(&s->frame_metadata);
+    ff_inflate_end(&s->zstream);
 
     return 0;
 }
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [FFmpeg-devel] [PATCH 16/21] avcodec/pngenc: Don't use deflateInit2() with default parameters
  2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
                   ` (13 preceding siblings ...)
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 15/21] avcodec/pngdec: " Andreas Rheinhardt
@ 2022-03-15 20:06 ` Andreas Rheinhardt
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 17/21] avcodec/zlib_wrapper: Add wrapper for deflateInit() Andreas Rheinhardt
                   ` (4 subsequent siblings)
  19 siblings, 0 replies; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:06 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

Use deflateInit() instead.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/pngenc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libavcodec/pngenc.c b/libavcodec/pngenc.c
index 64a9f5cc95..0b75f948d5 100644
--- a/libavcodec/pngenc.c
+++ b/libavcodec/pngenc.c
@@ -1071,7 +1071,7 @@ static av_cold int png_enc_init(AVCodecContext *avctx)
     compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT
                       ? Z_DEFAULT_COMPRESSION
                       : av_clip(avctx->compression_level, 0, 9);
-    if (deflateInit2(&s->zstream, compression_level, Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY) != Z_OK)
+    if (deflateInit(&s->zstream, compression_level) != Z_OK)
         return -1;
 
     return 0;
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [FFmpeg-devel] [PATCH 17/21] avcodec/zlib_wrapper: Add wrapper for deflateInit()
  2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
                   ` (14 preceding siblings ...)
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 16/21] avcodec/pngenc: Don't use deflateInit2() with default parameters Andreas Rheinhardt
@ 2022-03-15 20:06 ` Andreas Rheinhardt
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 18/21] avcodec/zmbvenc: Use ff_deflate_init/end() wrappers Andreas Rheinhardt
                   ` (3 subsequent siblings)
  19 siblings, 0 replies; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:06 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

The rationale is the same as for the wrappers for inflateInit(),
although the case for it is admittedly not so strong because
there are less users of deflateInit().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 configure                 |  2 ++
 libavcodec/Makefile       |  1 +
 libavcodec/zlib_wrapper.c | 34 ++++++++++++++++++++++++++++++++++
 libavcodec/zlib_wrapper.h | 10 ++++++++++
 4 files changed, 47 insertions(+)

diff --git a/configure b/configure
index 74485fb713..26417f541f 100755
--- a/configure
+++ b/configure
@@ -2433,6 +2433,7 @@ CONFIG_EXTRA="
     cbs_jpeg
     cbs_mpeg2
     cbs_vp9
+    deflate_wrapper
     dirac_parse
     dnn
     dovi_rpu
@@ -2711,6 +2712,7 @@ cbs_jpeg_select="cbs"
 cbs_mpeg2_select="cbs"
 cbs_vp9_select="cbs"
 dct_select="rdft"
+deflate_wrapper_deps="zlib"
 dirac_parse_select="golomb"
 dovi_rpu_select="golomb"
 dnn_suggest="libtensorflow libopenvino"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 62c8e34963..c45503b81b 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -77,6 +77,7 @@ OBJS-$(CONFIG_CBS_MPEG2)               += cbs_mpeg2.o
 OBJS-$(CONFIG_CBS_VP9)                 += cbs_vp9.o
 OBJS-$(CONFIG_CRYSTALHD)               += crystalhd.o
 OBJS-$(CONFIG_DCT)                     += dct.o dct32_fixed.o dct32_float.o
+OBJS-$(CONFIG_DEFLATE_WRAPPER)         += zlib_wrapper.o
 OBJS-$(CONFIG_DOVI_RPU)                += dovi_rpu.o
 OBJS-$(CONFIG_ERROR_RESILIENCE)        += error_resilience.o
 OBJS-$(CONFIG_EXIF)                    += exif.o tiff_common.o
diff --git a/libavcodec/zlib_wrapper.c b/libavcodec/zlib_wrapper.c
index 5b93c2c74f..bf104e5bf6 100644
--- a/libavcodec/zlib_wrapper.c
+++ b/libavcodec/zlib_wrapper.c
@@ -21,6 +21,7 @@
 
 #include <zlib.h>
 
+#include "config.h"
 #include "libavutil/error.h"
 #include "libavutil/log.h"
 #include "libavutil/mem.h"
@@ -36,6 +37,7 @@ static void free_wrapper(void *opaque, void *ptr)
     av_free(ptr);
 }
 
+#if CONFIG_INFLATE_WRAPPER
 int ff_inflate_init(FFZStream *z, void *logctx)
 {
     z_stream *const zstream = &z->zstream;
@@ -58,7 +60,9 @@ int ff_inflate_init(FFZStream *z, void *logctx)
     }
     return 0;
 }
+#endif
 
+#if CONFIG_DEFLATE_WRAPPER
 void ff_inflate_end(FFZStream *z)
 {
     if (z->inited) {
@@ -66,3 +70,33 @@ void ff_inflate_end(FFZStream *z)
         inflateEnd(&z->zstream);
     }
 }
+
+int ff_deflate_init(FFZStream *z, int level, void *logctx)
+{
+    z_stream *const zstream = &z->zstream;
+    int zret;
+
+    z->inited = 0;
+    zstream->zalloc = alloc_wrapper;
+    zstream->zfree  = free_wrapper;
+    zstream->opaque = Z_NULL;
+
+    zret = deflateInit(zstream, level);
+    if (zret == Z_OK) {
+        z->inited = 1;
+    } else {
+        av_log(logctx, AV_LOG_ERROR, "deflateInit error %d, message: %s\n",
+               zret, zstream->msg ? zstream->msg : "");
+        return AVERROR_EXTERNAL;
+    }
+    return 0;
+}
+
+void ff_deflate_end(FFZStream *z)
+{
+    if (z->inited) {
+        z->inited = 0;
+        deflateEnd(&z->zstream);
+    }
+}
+#endif
diff --git a/libavcodec/zlib_wrapper.h b/libavcodec/zlib_wrapper.h
index 0e91713b25..fa8ee654fd 100644
--- a/libavcodec/zlib_wrapper.h
+++ b/libavcodec/zlib_wrapper.h
@@ -48,4 +48,14 @@ int ff_inflate_init(FFZStream *zstream, void *logctx);
  */
 void ff_inflate_end(FFZStream *zstream);
 
+/**
+ * Wrapper around deflateInit(). It works analogously to ff_inflate_init().
+ */
+int ff_deflate_init(FFZStream *zstream, int level, void *logctx);
+
+/**
+ * Wrapper around deflateEnd(). It works analogously to ff_inflate_end().
+ */
+void ff_deflate_end(FFZStream *zstream);
+
 #endif /* AVCODEC_ZLIB_WRAPPER_H */
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [FFmpeg-devel] [PATCH 18/21] avcodec/zmbvenc: Use ff_deflate_init/end() wrappers
  2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
                   ` (15 preceding siblings ...)
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 17/21] avcodec/zlib_wrapper: Add wrapper for deflateInit() Andreas Rheinhardt
@ 2022-03-15 20:06 ` Andreas Rheinhardt
  2022-03-16 20:03   ` Tomas Härdin
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 19/21] avcodec/pngenc: " Andreas Rheinhardt
                   ` (2 subsequent siblings)
  19 siblings, 1 reply; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:06 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

They emit better error messages (it does not claim that inflateInit
failed upon an error from deflateInit!) and uses our allocation functions.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 configure            |  2 +-
 libavcodec/zmbvenc.c | 41 +++++++++++++++--------------------------
 2 files changed, 16 insertions(+), 27 deletions(-)

diff --git a/configure b/configure
index 26417f541f..8f36699d0c 100755
--- a/configure
+++ b/configure
@@ -2995,7 +2995,7 @@ zerocodec_decoder_select="inflate_wrapper"
 zlib_decoder_select="inflate_wrapper"
 zlib_encoder_deps="zlib"
 zmbv_decoder_select="inflate_wrapper"
-zmbv_encoder_deps="zlib"
+zmbv_encoder_select="deflate_wrapper"
 
 # hardware accelerators
 crystalhd_deps="libcrystalhd_libcrystalhd_if_h"
diff --git a/libavcodec/zmbvenc.c b/libavcodec/zmbvenc.c
index 8efdbc963e..065d390a92 100644
--- a/libavcodec/zmbvenc.c
+++ b/libavcodec/zmbvenc.c
@@ -32,6 +32,7 @@
 #include "avcodec.h"
 #include "encode.h"
 #include "internal.h"
+#include "zlib_wrapper.h"
 
 #include <zlib.h>
 
@@ -74,8 +75,7 @@ typedef struct ZmbvEncContext {
     int keyint, curfrm;
     int bypp;
     enum ZmbvFormat fmt;
-    int zlib_init_ok;
-    z_stream zstream;
+    FFZStream zstream;
 
     int score_tab[ZMBV_BLOCK * ZMBV_BLOCK * 4 + 1];
 } ZmbvEncContext;
@@ -169,6 +169,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
                         const AVFrame *pict, int *got_packet)
 {
     ZmbvEncContext * const c = avctx->priv_data;
+    z_stream  *const zstream = &c->zstream.zstream;
     const AVFrame * const p = pict;
     uint8_t *src, *prev, *buf;
     uint32_t *palptr;
@@ -262,21 +263,21 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     }
 
     if (keyframe)
-        deflateReset(&c->zstream);
+        deflateReset(zstream);
 
-    c->zstream.next_in = c->work_buf;
-    c->zstream.avail_in = work_size;
-    c->zstream.total_in = 0;
+    zstream->next_in   = c->work_buf;
+    zstream->avail_in  = work_size;
+    zstream->total_in  = 0;
 
-    c->zstream.next_out = c->comp_buf;
-    c->zstream.avail_out = c->comp_size;
-    c->zstream.total_out = 0;
-    if(deflate(&c->zstream, Z_SYNC_FLUSH) != Z_OK){
+    zstream->next_out  = c->comp_buf;
+    zstream->avail_out = c->comp_size;
+    zstream->total_out = 0;
+    if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
         av_log(avctx, AV_LOG_ERROR, "Error compressing data\n");
         return -1;
     }
 
-    pkt_size = c->zstream.total_out + 1 + 6*keyframe;
+    pkt_size = zstream->total_out + 1 + 6 * keyframe;
     if ((ret = ff_get_encode_buffer(avctx, pkt, pkt_size, 0)) < 0)
         return ret;
     buf = pkt->data;
@@ -292,7 +293,7 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
         *buf++ = ZMBV_BLOCK; // block height
         pkt->flags |= AV_PKT_FLAG_KEY;
     }
-    memcpy(buf, c->comp_buf, c->zstream.total_out);
+    memcpy(buf, c->comp_buf, zstream->total_out);
 
     *got_packet = 1;
 
@@ -307,8 +308,7 @@ static av_cold int encode_end(AVCodecContext *avctx)
     av_freep(&c->work_buf);
 
     av_freep(&c->prev_buf);
-    if (c->zlib_init_ok)
-        deflateEnd(&c->zstream);
+    ff_deflate_end(&c->zstream);
 
     return 0;
 }
@@ -319,7 +319,6 @@ static av_cold int encode_end(AVCodecContext *avctx)
 static av_cold int encode_init(AVCodecContext *avctx)
 {
     ZmbvEncContext * const c = avctx->priv_data;
-    int zret; // Zlib return code
     int i;
     int lvl = 9;
     int prev_size, prev_offset;
@@ -408,17 +407,7 @@ static av_cold int encode_init(AVCodecContext *avctx)
     }
     c->prev = c->prev_buf + prev_offset;
 
-    c->zstream.zalloc = Z_NULL;
-    c->zstream.zfree = Z_NULL;
-    c->zstream.opaque = Z_NULL;
-    zret = deflateInit(&c->zstream, lvl);
-    if (zret != Z_OK) {
-        av_log(avctx, AV_LOG_ERROR, "Inflate init error: %d\n", zret);
-        return -1;
-    }
-    c->zlib_init_ok = 1;
-
-    return 0;
+    return ff_deflate_init(&c->zstream, lvl, avctx);
 }
 
 const AVCodec ff_zmbv_encoder = {
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [FFmpeg-devel] [PATCH 19/21] avcodec/pngenc: Use ff_deflate_init/end() wrappers
  2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
                   ` (16 preceding siblings ...)
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 18/21] avcodec/zmbvenc: Use ff_deflate_init/end() wrappers Andreas Rheinhardt
@ 2022-03-15 20:06 ` Andreas Rheinhardt
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 20/21] avcodec/lclenc: " Andreas Rheinhardt
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 21/21] avcodec/flashsv2enc: Avoid opening and closing z_stream Andreas Rheinhardt
  19 siblings, 0 replies; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:06 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

They return nicer error messages.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 configure           |  6 ++----
 libavcodec/png.c    | 10 ---------
 libavcodec/png.h    |  4 ----
 libavcodec/pngenc.c | 49 ++++++++++++++++++++++-----------------------
 4 files changed, 26 insertions(+), 43 deletions(-)

diff --git a/configure b/configure
index 8f36699d0c..a3d4b224f4 100755
--- a/configure
+++ b/configure
@@ -2761,8 +2761,7 @@ amv_decoder_select="sp5x_decoder exif"
 amv_encoder_select="jpegtables mpegvideoenc"
 ape_decoder_select="bswapdsp llauddsp"
 apng_decoder_select="inflate_wrapper"
-apng_encoder_deps="zlib"
-apng_encoder_select="llvidencdsp"
+apng_encoder_select="deflate_wrapper llvidencdsp"
 aptx_decoder_select="audio_frame_queue"
 aptx_encoder_select="audio_frame_queue"
 aptx_hd_decoder_select="audio_frame_queue"
@@ -2906,8 +2905,7 @@ opus_decoder_deps="swresample"
 opus_decoder_select="mdct15"
 opus_encoder_select="audio_frame_queue mdct15"
 png_decoder_select="inflate_wrapper"
-png_encoder_deps="zlib"
-png_encoder_select="llvidencdsp"
+png_encoder_select="deflate_wrapper llvidencdsp"
 prores_decoder_select="blockdsp idctdsp"
 prores_encoder_select="fdctdsp"
 qcelp_decoder_select="lsp"
diff --git a/libavcodec/png.c b/libavcodec/png.c
index e772eaad26..34f649815a 100644
--- a/libavcodec/png.c
+++ b/libavcodec/png.c
@@ -38,16 +38,6 @@ static const uint8_t ff_png_pass_xshift[NB_PASSES] = {
     3, 3, 2, 2, 1, 1, 0
 };
 
-void *ff_png_zalloc(void *opaque, unsigned int items, unsigned int size)
-{
-    return av_calloc(items, size);
-}
-
-void ff_png_zfree(void *opaque, void *ptr)
-{
-    av_free(ptr);
-}
-
 int ff_png_get_nb_channels(int color_type)
 {
     int channels;
diff --git a/libavcodec/png.h b/libavcodec/png.h
index a15560131f..01171e682e 100644
--- a/libavcodec/png.h
+++ b/libavcodec/png.h
@@ -52,10 +52,6 @@
 /* Mask to determine which y pixels are valid in a pass */
 extern const uint8_t ff_png_pass_ymask[NB_PASSES];
 
-void *ff_png_zalloc(void *opaque, unsigned int items, unsigned int size);
-
-void ff_png_zfree(void *opaque, void *ptr);
-
 int ff_png_get_nb_channels(int color_type);
 
 /* compute the row size of an interleaved pass */
diff --git a/libavcodec/pngenc.c b/libavcodec/pngenc.c
index 0b75f948d5..b34a94db21 100644
--- a/libavcodec/pngenc.c
+++ b/libavcodec/pngenc.c
@@ -26,6 +26,7 @@
 #include "lossless_videoencdsp.h"
 #include "png.h"
 #include "apng.h"
+#include "zlib_wrapper.h"
 
 #include "libavutil/avassert.h"
 #include "libavutil/crc.h"
@@ -56,7 +57,7 @@ typedef struct PNGEncContext {
 
     int filter_type;
 
-    z_stream zstream;
+    FFZStream zstream;
     uint8_t buf[IOBUF_SIZE];
     int dpi;                     ///< Physical pixel density, in dots per inch, if set
     int dpm;                     ///< Physical pixel density, in dots per meter, if set
@@ -272,19 +273,20 @@ static void png_write_image_data(AVCodecContext *avctx,
 static int png_write_row(AVCodecContext *avctx, const uint8_t *data, int size)
 {
     PNGEncContext *s = avctx->priv_data;
+    z_stream *const zstream = &s->zstream.zstream;
     int ret;
 
-    s->zstream.avail_in = size;
-    s->zstream.next_in  = data;
-    while (s->zstream.avail_in > 0) {
-        ret = deflate(&s->zstream, Z_NO_FLUSH);
+    zstream->avail_in = size;
+    zstream->next_in  = data;
+    while (zstream->avail_in > 0) {
+        ret = deflate(zstream, Z_NO_FLUSH);
         if (ret != Z_OK)
             return -1;
-        if (s->zstream.avail_out == 0) {
+        if (zstream->avail_out == 0) {
             if (s->bytestream_end - s->bytestream > IOBUF_SIZE + 100)
                 png_write_image_data(avctx, s->buf, IOBUF_SIZE);
-            s->zstream.avail_out = IOBUF_SIZE;
-            s->zstream.next_out  = s->buf;
+            zstream->avail_out = IOBUF_SIZE;
+            zstream->next_out  = s->buf;
         }
     }
     return 0;
@@ -432,6 +434,7 @@ static int encode_headers(AVCodecContext *avctx, const AVFrame *pict)
 static int encode_frame(AVCodecContext *avctx, const AVFrame *pict)
 {
     PNGEncContext *s       = avctx->priv_data;
+    z_stream *const zstream = &s->zstream.zstream;
     const AVFrame *const p = pict;
     int y, len, ret;
     int row_size, pass_row_size;
@@ -459,8 +462,8 @@ static int encode_frame(AVCodecContext *avctx, const AVFrame *pict)
     }
 
     /* put each row */
-    s->zstream.avail_out = IOBUF_SIZE;
-    s->zstream.next_out  = s->buf;
+    zstream->avail_out = IOBUF_SIZE;
+    zstream->next_out  = s->buf;
     if (s->is_progressive) {
         int pass;
 
@@ -496,14 +499,14 @@ static int encode_frame(AVCodecContext *avctx, const AVFrame *pict)
     }
     /* compress last bytes */
     for (;;) {
-        ret = deflate(&s->zstream, Z_FINISH);
+        ret = deflate(zstream, Z_FINISH);
         if (ret == Z_OK || ret == Z_STREAM_END) {
-            len = IOBUF_SIZE - s->zstream.avail_out;
+            len = IOBUF_SIZE - zstream->avail_out;
             if (len > 0 && s->bytestream_end - s->bytestream > len + 100) {
                 png_write_image_data(avctx, s->buf, len);
             }
-            s->zstream.avail_out = IOBUF_SIZE;
-            s->zstream.next_out  = s->buf;
+            zstream->avail_out = IOBUF_SIZE;
+            zstream->next_out  = s->buf;
             if (ret == Z_STREAM_END)
                 break;
         } else {
@@ -518,7 +521,7 @@ the_end:
     av_freep(&crow_base);
     av_freep(&progressive_buf);
     av_freep(&top_buf);
-    deflateReset(&s->zstream);
+    deflateReset(zstream);
     return ret;
 }
 
@@ -530,7 +533,8 @@ static int encode_png(AVCodecContext *avctx, AVPacket *pkt,
     int enc_row_size;
     int64_t max_packet_size;
 
-    enc_row_size    = deflateBound(&s->zstream, (avctx->width * s->bits_per_pixel + 7) >> 3);
+    enc_row_size    = deflateBound(&s->zstream.zstream,
+                                   (avctx->width * s->bits_per_pixel + 7) >> 3);
     max_packet_size =
         AV_INPUT_BUFFER_MIN_SIZE + // headers
         avctx->height * (
@@ -858,7 +862,8 @@ static int encode_apng(AVCodecContext *avctx, AVPacket *pkt,
         }
     }
 
-    enc_row_size    = deflateBound(&s->zstream, (avctx->width * s->bits_per_pixel + 7) >> 3);
+    enc_row_size    = deflateBound(&s->zstream.zstream,
+                                   (avctx->width * s->bits_per_pixel + 7) >> 3);
     max_packet_size =
         AV_INPUT_BUFFER_MIN_SIZE + // headers
         avctx->height * (
@@ -1065,23 +1070,17 @@ static av_cold int png_enc_init(AVCodecContext *avctx)
     }
     s->bits_per_pixel = ff_png_get_nb_channels(s->color_type) * s->bit_depth;
 
-    s->zstream.zalloc = ff_png_zalloc;
-    s->zstream.zfree  = ff_png_zfree;
-    s->zstream.opaque = NULL;
     compression_level = avctx->compression_level == FF_COMPRESSION_DEFAULT
                       ? Z_DEFAULT_COMPRESSION
                       : av_clip(avctx->compression_level, 0, 9);
-    if (deflateInit(&s->zstream, compression_level) != Z_OK)
-        return -1;
-
-    return 0;
+    return ff_deflate_init(&s->zstream, compression_level, avctx);
 }
 
 static av_cold int png_enc_close(AVCodecContext *avctx)
 {
     PNGEncContext *s = avctx->priv_data;
 
-    deflateEnd(&s->zstream);
+    ff_deflate_end(&s->zstream);
     av_frame_free(&s->last_frame);
     av_frame_free(&s->prev_frame);
     av_freep(&s->last_frame_packet);
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [FFmpeg-devel] [PATCH 20/21] avcodec/lclenc: Use ff_deflate_init/end() wrappers
  2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
                   ` (17 preceding siblings ...)
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 19/21] avcodec/pngenc: " Andreas Rheinhardt
@ 2022-03-15 20:06 ` Andreas Rheinhardt
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 21/21] avcodec/flashsv2enc: Avoid opening and closing z_stream Andreas Rheinhardt
  19 siblings, 0 replies; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:06 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

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@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

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* [FFmpeg-devel] [PATCH 21/21] avcodec/flashsv2enc: Avoid opening and closing z_stream
  2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
                   ` (18 preceding siblings ...)
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 20/21] avcodec/lclenc: " Andreas Rheinhardt
@ 2022-03-15 20:06 ` Andreas Rheinhardt
  19 siblings, 0 replies; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-15 20:06 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Andreas Rheinhardt

Instead initialize a z_stream during init and reset it when needed.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 configure                |  2 +-
 libavcodec/flashsv2enc.c | 70 +++++++++++++++++++++++-----------------
 2 files changed, 42 insertions(+), 30 deletions(-)

diff --git a/configure b/configure
index ee3c6783f3..b175019a61 100755
--- a/configure
+++ b/configure
@@ -2812,7 +2812,7 @@ fic_decoder_select="golomb"
 flac_decoder_select="flacdsp"
 flac_encoder_select="bswapdsp flacdsp lpc"
 flashsv2_decoder_select="inflate_wrapper"
-flashsv2_encoder_deps="zlib"
+flashsv2_encoder_select="deflate_wrapper"
 flashsv_decoder_select="inflate_wrapper"
 flashsv_encoder_deps="zlib"
 flv_decoder_select="h263_decoder"
diff --git a/libavcodec/flashsv2enc.c b/libavcodec/flashsv2enc.c
index 56ea0ed0a4..9b8a891ae4 100644
--- a/libavcodec/flashsv2enc.c
+++ b/libavcodec/flashsv2enc.c
@@ -52,6 +52,7 @@
 #include "internal.h"
 #include "put_bits.h"
 #include "bytestream.h"
+#include "zlib_wrapper.h"
 
 #define HAS_IFRAME_IMAGE 0x02
 #define HAS_PALLET_INFO 0x01
@@ -112,6 +113,7 @@ typedef struct FlashSV2Context {
     uint8_t use_custom_palette;
     uint8_t palette_type;       ///< 0=>default, 1=>custom - changed when palette regenerated.
     Palette palette;
+    FFZStream zstream;
 #ifndef FLASHSV2_DUMB
     double tot_blocks;          ///< blocks encoded since last keyframe
     double diff_blocks;         ///< blocks that were different since last keyframe
@@ -136,6 +138,7 @@ static av_cold void cleanup(FlashSV2Context * s)
 
     av_freep(&s->frame_blocks);
     av_freep(&s->key_blocks);
+    ff_deflate_end(&s->zstream);
 }
 
 static void init_blocks(FlashSV2Context * s, Block * blocks,
@@ -234,7 +237,9 @@ static av_cold int flashsv2_encode_init(AVCodecContext * avctx)
     if ((ret = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
         return ret;
 
-
+    ret = ff_deflate_init(&s->zstream, s->comp, avctx);
+    if (ret < 0)
+        return ret;
     s->last_key_frame = 0;
 
     s->image_width  = avctx->width;
@@ -357,41 +362,47 @@ static int write_block(Block * b, uint8_t * buf, int buf_size)
     return buf_pos;
 }
 
-static int encode_zlib(Block * b, uint8_t * buf, unsigned long *buf_size, int comp)
+static int encode_zlib(Block *b, uint8_t *buf, unsigned long *buf_size,
+                       z_stream *zstream)
 {
-    int res = compress2(buf, buf_size, b->sl_begin, b->sl_end - b->sl_begin, comp);
-    return res == Z_OK ? 0 : -1;
+    int res;
+
+    if (deflateReset(zstream) != Z_OK)
+        return AVERROR_EXTERNAL;
+    zstream->next_out  = buf;
+    zstream->avail_out = *buf_size;
+    zstream->next_in   = b->sl_begin;
+    zstream->avail_in  = b->sl_end - b->sl_begin;
+    res = deflate(zstream, Z_FINISH);
+    if (res != Z_STREAM_END)
+        return AVERROR_EXTERNAL;
+    *buf_size -= zstream->avail_out;
+    return 0;
 }
 
 static int encode_zlibprime(Block * b, Block * prime, uint8_t * buf,
-                            int *buf_size, int comp)
+                            int *buf_size, z_stream *zstream)
 {
-    z_stream s;
     int res;
-    s.zalloc = NULL;
-    s.zfree  = NULL;
-    s.opaque = NULL;
-    res = deflateInit(&s, comp);
-    if (res < 0)
-        return -1;
 
-    s.next_in  = prime->enc;
-    s.avail_in = prime->enc_size;
-    while (s.avail_in > 0) {
-        s.next_out  = buf;
-        s.avail_out = *buf_size;
-        res = deflate(&s, Z_SYNC_FLUSH);
+    if (deflateReset(zstream) != Z_OK)
+        return AVERROR_EXTERNAL;
+    zstream->next_in  = prime->enc;
+    zstream->avail_in = prime->enc_size;
+    while (zstream->avail_in > 0) {
+        zstream->next_out  = buf;
+        zstream->avail_out = *buf_size;
+        res = deflate(zstream, Z_SYNC_FLUSH);
         if (res < 0)
             return -1;
     }
 
-    s.next_in   = b->sl_begin;
-    s.avail_in  = b->sl_end - b->sl_begin;
-    s.next_out  = buf;
-    s.avail_out = *buf_size;
-    res = deflate(&s, Z_FINISH);
-    deflateEnd(&s);
-    *buf_size -= s.avail_out;
+    zstream->next_in   = b->sl_begin;
+    zstream->avail_in  = b->sl_end - b->sl_begin;
+    zstream->next_out  = buf;
+    zstream->avail_out = *buf_size;
+    res = deflate(zstream, Z_FINISH);
+    *buf_size -= zstream->avail_out;
     if (res != Z_STREAM_END)
         return -1;
     return 0;
@@ -559,7 +570,7 @@ static int encode_15_7(Palette * palette, Block * b, const uint8_t * src,
 }
 
 static int encode_block(FlashSV2Context *s, Palette * palette, Block * b,
-                        Block * prev, const uint8_t * src, int stride, int comp,
+                        Block *prev, const uint8_t *src, int stride,
                         int dist, int keyframe)
 {
     unsigned buf_size = b->width * b->height * 6;
@@ -574,12 +585,12 @@ static int encode_block(FlashSV2Context *s, Palette * palette, Block * b,
 
     if (b->len > 0) {
         b->data_size = buf_size;
-        res = encode_zlib(b, b->data, &b->data_size, comp);
+        res = encode_zlib(b, b->data, &b->data_size, &s->zstream.zstream);
         if (res)
             return res;
 
         if (!keyframe) {
-            res = encode_zlibprime(b, prev, buf, &buf_size, comp);
+            res = encode_zlibprime(b, prev, buf, &buf_size, &s->zstream.zstream);
             if (res)
                 return res;
 
@@ -656,7 +667,8 @@ static int encode_all_blocks(FlashSV2Context * s, int keyframe)
                 b->flags |= HAS_DIFF_BLOCKS;
             }
             data = s->current_frame + s->image_width * 3 * s->block_height * row + s->block_width * col * 3;
-            res = encode_block(s, &s->palette, b, prev, data, s->image_width * 3, s->comp, s->dist, keyframe);
+            res = encode_block(s, &s->palette, b, prev, data,
+                               s->image_width * 3, s->dist, keyframe);
 #ifndef FLASHSV2_DUMB
             if (b->dirty)
                 s->diff_blocks++;
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [FFmpeg-devel] [PATCH 02/21] avcodec/zlib_wrapper: Add wrappers for zlib inflateInit, inflateEnd
  2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 02/21] avcodec/zlib_wrapper: Add wrappers for zlib inflateInit, inflateEnd Andreas Rheinhardt
@ 2022-03-16 19:24   ` Tomas Härdin
  2022-03-16 19:32     ` Andreas Rheinhardt
  0 siblings, 1 reply; 27+ messages in thread
From: Tomas Härdin @ 2022-03-16 19:24 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

> +int ff_inflate_init(FFZStream *z, void *logctx)
> +{
> +    z_stream *const zstream = &z->zstream;
> +    int zret;
> +
> +    z->inited = 0;
> +    zstream->next_in  = Z_NULL;
> +    zstream->avail_in = 0;
> +    zstream->zalloc   = Z_NULL;
> +    zstream->zfree    = Z_NULL;
> +    zstream->opaque   = Z_NULL;

why not bzero()?

Rest of the patch looks fine

/Tomas

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [FFmpeg-devel] [PATCH 03/21] avcodec/zmbv: Use ff_inflate_init/end()
  2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 03/21] avcodec/zmbv: Use ff_inflate_init/end() Andreas Rheinhardt
@ 2022-03-16 19:31   ` Tomas Härdin
  0 siblings, 0 replies; 27+ messages in thread
From: Tomas Härdin @ 2022-03-16 19:31 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

tis 2022-03-15 klockan 21:05 +0100 skrev Andreas Rheinhardt:
> Returns better error messages in case of error and deduplicates
> the inflateInit() code.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>  configure         |  2 +-
>  libavcodec/zmbv.c | 38 ++++++++++++++------------------------
>  2 files changed, 15 insertions(+), 25 deletions(-)

Looks OK

/Tomas

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [FFmpeg-devel] [PATCH 02/21] avcodec/zlib_wrapper: Add wrappers for zlib inflateInit, inflateEnd
  2022-03-16 19:24   ` Tomas Härdin
@ 2022-03-16 19:32     ` Andreas Rheinhardt
  2022-03-16 20:07       ` Tomas Härdin
  0 siblings, 1 reply; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-16 19:32 UTC (permalink / raw)
  To: ffmpeg-devel

Tomas Härdin:
>> +int ff_inflate_init(FFZStream *z, void *logctx)
>> +{
>> +    z_stream *const zstream = &z->zstream;
>> +    int zret;
>> +
>> +    z->inited = 0;
>> +    zstream->next_in  = Z_NULL;
>> +    zstream->avail_in = 0;
>> +    zstream->zalloc   = Z_NULL;
>> +    zstream->zfree    = Z_NULL;
>> +    zstream->opaque   = Z_NULL;
> 
> why not bzero()?
> 
> Rest of the patch looks fine
> 

bzero()? You mean memset to zero? The reason that I initialize exactly
these fields is because these are exactly the fields required to be
initialized by zlib (for inflate; next_in and avail_in are not required
to be initialized for deflate): "The fields next_in, avail_in, zalloc,
zfree and opaque must be initialized before by the caller." zlib treats
all the other fields as uninitialized, so why should we initialize them
(Actually reinitialize them -- most FFZStreams are already zeroed
initially as part of a codec's private context.)? The way it is done in
this patch shows directly which elements zlib expects to be set; setting
everything would not achieve the same.

- Andreas
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [FFmpeg-devel] [PATCH 18/21] avcodec/zmbvenc: Use ff_deflate_init/end() wrappers
  2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 18/21] avcodec/zmbvenc: Use ff_deflate_init/end() wrappers Andreas Rheinhardt
@ 2022-03-16 20:03   ` Tomas Härdin
  0 siblings, 0 replies; 27+ messages in thread
From: Tomas Härdin @ 2022-03-16 20:03 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

tis 2022-03-15 klockan 21:06 +0100 skrev Andreas Rheinhardt:
> They emit better error messages (it does not claim that inflateInit
> failed upon an error from deflateInit!) and uses our allocation
> functions.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>  configure            |  2 +-
>  libavcodec/zmbvenc.c | 41 +++++++++++++++--------------------------
>  2 files changed, 16 insertions(+), 27 deletions(-)

Looks OK

/Tomas

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [FFmpeg-devel] [PATCH 02/21] avcodec/zlib_wrapper: Add wrappers for zlib inflateInit, inflateEnd
  2022-03-16 19:32     ` Andreas Rheinhardt
@ 2022-03-16 20:07       ` Tomas Härdin
  2022-03-17  7:29         ` Andreas Rheinhardt
  0 siblings, 1 reply; 27+ messages in thread
From: Tomas Härdin @ 2022-03-16 20:07 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

ons 2022-03-16 klockan 20:32 +0100 skrev Andreas Rheinhardt:
> Tomas Härdin:
> > > +int ff_inflate_init(FFZStream *z, void *logctx)
> > > +{
> > > +    z_stream *const zstream = &z->zstream;
> > > +    int zret;
> > > +
> > > +    z->inited = 0;
> > > +    zstream->next_in  = Z_NULL;
> > > +    zstream->avail_in = 0;
> > > +    zstream->zalloc   = Z_NULL;
> > > +    zstream->zfree    = Z_NULL;
> > > +    zstream->opaque   = Z_NULL;
> > 
> > why not bzero()?
> > 
> > Rest of the patch looks fine
> > 
> 
> bzero()? You mean memset to zero? The reason that I initialize
> exactly
> these fields is because these are exactly the fields required to be
> initialized by zlib (for inflate; next_in and avail_in are not
> required
> to be initialized for deflate): "The fields next_in, avail_in,
> zalloc,
> zfree and opaque must be initialized before by the caller." zlib
> treats
> all the other fields as uninitialized, so why should we initialize
> them
> (Actually reinitialize them -- most FFZStreams are already zeroed
> initially as part of a codec's private context.)? The way it is done
> in
> this patch shows directly which elements zlib expects to be set;
> setting
> everything would not achieve the same.

Right. Looks OK then

/Tomas

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [FFmpeg-devel] [PATCH 02/21] avcodec/zlib_wrapper: Add wrappers for zlib inflateInit, inflateEnd
  2022-03-16 20:07       ` Tomas Härdin
@ 2022-03-17  7:29         ` Andreas Rheinhardt
  0 siblings, 0 replies; 27+ messages in thread
From: Andreas Rheinhardt @ 2022-03-17  7:29 UTC (permalink / raw)
  To: ffmpeg-devel

Tomas Härdin:
> ons 2022-03-16 klockan 20:32 +0100 skrev Andreas Rheinhardt:
>> Tomas Härdin:
>>>> +int ff_inflate_init(FFZStream *z, void *logctx)
>>>> +{
>>>> +    z_stream *const zstream = &z->zstream;
>>>> +    int zret;
>>>> +
>>>> +    z->inited = 0;
>>>> +    zstream->next_in  = Z_NULL;
>>>> +    zstream->avail_in = 0;
>>>> +    zstream->zalloc   = Z_NULL;
>>>> +    zstream->zfree    = Z_NULL;
>>>> +    zstream->opaque   = Z_NULL;
>>>
>>> why not bzero()?
>>>
>>> Rest of the patch looks fine
>>>
>>
>> bzero()? You mean memset to zero? The reason that I initialize
>> exactly
>> these fields is because these are exactly the fields required to be
>> initialized by zlib (for inflate; next_in and avail_in are not
>> required
>> to be initialized for deflate): "The fields next_in, avail_in,
>> zalloc,
>> zfree and opaque must be initialized before by the caller." zlib
>> treats
>> all the other fields as uninitialized, so why should we initialize
>> them
>> (Actually reinitialize them -- most FFZStreams are already zeroed
>> initially as part of a codec's private context.)? The way it is done
>> in
>> this patch shows directly which elements zlib expects to be set;
>> setting
>> everything would not achieve the same.
> 
> Right. Looks OK then
> 

Thanks for the review. Will apply this patchset tonight unless there are
objections.

- Andreas
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2022-03-17  7:29 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-15 20:03 [FFmpeg-devel] [PATCH 01/21] avcodec/pngenc: Avoid potentially truncating integers Andreas Rheinhardt
2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 02/21] avcodec/zlib_wrapper: Add wrappers for zlib inflateInit, inflateEnd Andreas Rheinhardt
2022-03-16 19:24   ` Tomas Härdin
2022-03-16 19:32     ` Andreas Rheinhardt
2022-03-16 20:07       ` Tomas Härdin
2022-03-17  7:29         ` Andreas Rheinhardt
2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 03/21] avcodec/zmbv: Use ff_inflate_init/end() Andreas Rheinhardt
2022-03-16 19:31   ` Tomas Härdin
2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 04/21] avcodec/zerocodec: " Andreas Rheinhardt
2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 05/21] avcodec/wcmv: " Andreas Rheinhardt
2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 06/21] avcodec/tscc: " Andreas Rheinhardt
2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 07/21] avcodec/rasc: " Andreas Rheinhardt
2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 08/21] avcodec/mwsc: " Andreas Rheinhardt
2022-03-15 20:05 ` [FFmpeg-devel] [PATCH 09/21] avcodec/mvha: " Andreas Rheinhardt
2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 10/21] avcodec/mscc: " Andreas Rheinhardt
2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 11/21] avcodec/lcldec: Use ff_inflate_init/end(), cleanup generically Andreas Rheinhardt
2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 12/21] avcodec/flashsv: Use ff_inflate_init/end() Andreas Rheinhardt
2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 13/21] avcodec/zlib_wrapper: Use our allocation, freeing functions Andreas Rheinhardt
2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 14/21] avcodec/lscrdec: Don't open and close z_streams unnecessarily Andreas Rheinhardt
2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 15/21] avcodec/pngdec: " Andreas Rheinhardt
2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 16/21] avcodec/pngenc: Don't use deflateInit2() with default parameters Andreas Rheinhardt
2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 17/21] avcodec/zlib_wrapper: Add wrapper for deflateInit() Andreas Rheinhardt
2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 18/21] avcodec/zmbvenc: Use ff_deflate_init/end() wrappers Andreas Rheinhardt
2022-03-16 20:03   ` Tomas Härdin
2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 19/21] avcodec/pngenc: " Andreas Rheinhardt
2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 20/21] avcodec/lclenc: " Andreas Rheinhardt
2022-03-15 20:06 ` [FFmpeg-devel] [PATCH 21/21] avcodec/flashsv2enc: Avoid opening and closing z_stream Andreas Rheinhardt

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git