Commit be39b79f9c for ffmpeg
commit be39b79f9c59223a6cf96bf43ee2c4e04abb8986
Author: Lynne <dev@lynne.ee>
Date: Sat Dec 5 11:20:01 2026 +0800
avcodec/pngdec: fully support AVDISCARD_ALL
The issue is that the PNG decoder STILL went and decoded every single
chunk fully, which with 5000x5000 PNG images embedded into FLAC files
meant opening and seeking were slow.
So just stop at the first IDAT.
Also, support it in APNG, because might as well. _ALL skips all frames.
ffprobe -show_streams on a FLAC with a 5000x5000 PNG cover took
0.7-0.9 s, now 40-60 ms; on a 3000x3000 APNG 134-147 ms, now 8-13 ms.
diff --git a/libavcodec/pngdec.c b/libavcodec/pngdec.c
index 58c688e51f..2bb5f5aadc 100644
--- a/libavcodec/pngdec.c
+++ b/libavcodec/pngdec.c
@@ -997,6 +997,12 @@ static int decode_idat_chunk(AVCodecContext *avctx, PNGDecContext *s,
avctx->alpha_mode = AVALPHA_MODE_STRAIGHT;
ff_progress_frame_unref(&s->picture);
+
+ /* Only the parameters are wanted: no picture is allocated or
+ * decompressed */
+ if (avctx->skip_frame >= AVDISCARD_ALL)
+ return populate_avctx_color_fields(avctx, p);
+
if (s->dispose_op == APNG_DISPOSE_OP_PREVIOUS) {
/* We only need a buffer for the current picture. */
ret = ff_thread_get_buffer(avctx, p, 0);
@@ -1534,10 +1540,8 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,
length = bytestream2_get_bytes_left(&s->gb);
if (length <= 0) {
- if (avctx->codec_id == AV_CODEC_ID_PNG &&
- avctx->skip_frame == AVDISCARD_ALL) {
+ if (avctx->skip_frame >= AVDISCARD_ALL)
return 0;
- }
if (CONFIG_APNG_DECODER && avctx->codec_id == AV_CODEC_ID_APNG && length == 0) {
if (!(s->pic_state & PNG_IDAT))
@@ -1630,6 +1634,9 @@ static int decode_frame_common(AVCodecContext *avctx, PNGDecContext *s,
continue;
if ((ret = decode_idat_chunk(avctx, s, &gb_chunk, p)) < 0)
goto fail;
+ /* The parameters are all known at the first IDAT or fdAT */
+ if (avctx->skip_frame >= AVDISCARD_ALL)
+ goto exit_loop;
break;
case MKTAG('P', 'L', 'T', 'E'):
decode_plte_chunk(avctx, s, &gb_chunk);
@@ -1755,10 +1762,8 @@ exit_loop:
if (!p)
return AVERROR_INVALIDDATA;
- if (avctx->codec_id == AV_CODEC_ID_PNG &&
- avctx->skip_frame == AVDISCARD_ALL) {
+ if (avctx->skip_frame >= AVDISCARD_ALL)
return 0;
- }
if (percent_missing(s) > avctx->discard_damaged_percentage) {
ret = AVERROR_INVALIDDATA;
@@ -1998,6 +2003,9 @@ static int decode_frame_apng(AVCodecContext *avctx, AVFrame *p,
if ((ret = decode_frame_common(avctx, s, p, avpkt)) < 0)
return ret;
+ if (avctx->skip_frame >= AVDISCARD_ALL)
+ return bytestream2_tell(&s->gb);
+
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)))
@@ -2098,7 +2106,8 @@ const FFCodec ff_apng_decoder = {
FF_CODEC_DECODE_CB(decode_frame_apng),
UPDATE_THREAD_CONTEXT(update_thread_context),
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
- .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
+ .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM |
+ FF_CODEC_CAP_INIT_CLEANUP |
FF_CODEC_CAP_USES_PROGRESSFRAMES |
FF_CODEC_CAP_ICC_PROFILES,
};