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

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


They return nicer error messages.

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



More information about the ffmpeg-devel mailing list