From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> To: ffmpeg-devel@ffmpeg.org Cc: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> Subject: [FFmpeg-devel] [PATCH 15/21] avcodec/pngdec: Don't open and close z_streams unnecessarily Date: Tue, 15 Mar 2022 21:06:05 +0100 Message-ID: <AS1PR01MB956435F841B5A18CEC05D50E8F109@AS1PR01MB9564.eurprd01.prod.exchangelabs.com> (raw) In-Reply-To: <AS1PR01MB956473F7529A463D0DFC230B8F109@AS1PR01MB9564.eurprd01.prod.exchangelabs.com> 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".
next prev parent reply other threads:[~2022-03-15 20:08 UTC|newest] Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top 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 ` Andreas Rheinhardt [this message] 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
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=AS1PR01MB956435F841B5A18CEC05D50E8F109@AS1PR01MB9564.eurprd01.prod.exchangelabs.com \ --to=andreas.rheinhardt@outlook.com \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
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