PR #26169 opened by Jun Zhao (mypopydev)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26169
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26169.patch

An ancillary chunk placed before the first IDAT can exceed the initial 
2048-byte probe buffer. apng_probe() then returned 0, so png_pipe was selected 
and only one frame was decoded. This happens when a C2PA caBX manifest sits 
before IDAT. The probe now returns EAGAIN for an ancillary chunk that does not 
fit, so the buffer grows until acTL and IDAT are visible. A critical chunk 
still returns 0.

FATE coverage is generated locally. apnggen gains a biganc mode that writes a 
4096-byte caBX chunk before the first IDAT, and a plain mode for a non-animated 
PNG with the same chunk. The animated file must probe as apng and decode all 
six frames. The plain file must still probe as png_pipe.


>From 6949c6da0e36c5bf878bec3a2af73bfd743ef537 Mon Sep 17 00:00:00 2001
From: Jun Zhao <[email protected]>
Date: Wed, 18 Nov 2026 15:09:46 +0800
Subject: [PATCH 1/2] avformat/apngdec: retry probing when an ancillary chunk
 exceeds the buffer

A large ancillary chunk before the first IDAT (e.g. a C2PA caBX manifest)
can be bigger than the initial 2048-byte probe buffer. apng_probe() then
returns 0 before reaching acTL/IDAT, so png_pipe wins with score 99 and only
one frame is decoded. Return AVERROR(EAGAIN) for ancillary chunks so the
undecided retry loop grows the buffer until acTL/IDAT are visible; critical
chunks still return 0.

Fixes: https://code.ffmpeg.org/FFmpeg/FFmpeg/issues/26132

Signed-off-by: Jun Zhao <[email protected]>
---
 libavformat/apngdec.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/libavformat/apngdec.c b/libavformat/apngdec.c
index 87be195df5c..da9d0ac0b9a 100644
--- a/libavformat/apngdec.c
+++ b/libavformat/apngdec.c
@@ -92,8 +92,15 @@ static int apng_probe(const AVProbeData *p)
         /* we don't check IDAT size, as this is the last tag
          * we check, and it may be larger than the probe buffer */
         if (tag != MKTAG('I', 'D', 'A', 'T') &&
-            len + 4 > bytestream2_get_bytes_left(&gb))
+            len + 4 > bytestream2_get_bytes_left(&gb)) {
+            /* Ancillary chunks (bit 5 of the first type byte set, i.e. a
+             * lowercase first letter) may be large, e.g. a C2PA manifest,
+             * and acTL/IDAT may follow them beyond the probe buffer.  Ask
+             * for more data instead of rejecting an otherwise valid APNG. */
+            if (tag & 0x20)
+                return AVERROR(EAGAIN);
             return 0;
+        }
 
         switch (tag) {
         case MKTAG('I', 'H', 'D', 'R'):
-- 
2.54.0


>From d8a776eb7e8e01dc83478cc5429e1f93f39d1e8f Mon Sep 17 00:00:00 2001
From: Jun Zhao <[email protected]>
Date: Wed, 18 Nov 2026 15:09:47 +0800
Subject: [PATCH 2/2] tests/fate/apng: cover a large ancillary chunk before the
 first IDAT

Extend apnggen with a "biganc" mode that emits a 4096-byte caBX chunk before
the first IDAT, plus a "plain" mode for a non-animated PNG control. Add three
FATE targets: probefmt asserting apng, probefmt asserting png_pipe for the
control, and framecrc asserting all six frames. Generated locally, so no
fate-suite media is needed.

Signed-off-by: Jun Zhao <[email protected]>
---
 tests/Makefile                    |  5 ++++
 tests/apnggen.c                   | 46 ++++++++++++++++++++-----------
 tests/fate/apng.mak               | 24 +++++++++++++++-
 tests/ref/fate/apng-biganc-frames | 11 ++++++++
 4 files changed, 69 insertions(+), 17 deletions(-)
 create mode 100644 tests/ref/fate/apng-biganc-frames

diff --git a/tests/Makefile b/tests/Makefile
index 0656946f11f..ac5f1e45c82 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -77,6 +77,11 @@ tests/data/fate/apng-small-bpp-%.png: TAG = GEN
 tests/data/fate/apng-small-bpp-%.png: tests/apnggen$(HOSTEXESUF) | 
tests/data/fate
        $(M)./$< $@
 
+# Large ancillary chunk before the first IDAT (issue #26132).
+tests/data/fate/apng-biganc-%.png: TAG = GEN
+tests/data/fate/apng-biganc-%.png: tests/apnggen$(HOSTEXESUF) | tests/data/fate
+       $(M)./$< $@
+
 RUNNING_FATE := $(filter check fate%,$(filter-out fate-rsync,$(MAKECMDGOALS)))
 
 # Check sanity of dependencies when running FATE tests.
diff --git a/tests/apnggen.c b/tests/apnggen.c
index daf69f1ecb7..6fe2c4ace21 100644
--- a/tests/apnggen.c
+++ b/tests/apnggen.c
@@ -7,6 +7,11 @@
  * here, the same way audiogen builds its wave files, so the tests do not
  * need media from the fate-suite.
  *
+ * A file name containing "biganc" gets a large private ancillary chunk
+ * before the first IDAT, to check that probing still finds the animation
+ * (issue #26132). A file name containing "plain" omits acTL/fcTL and emits
+ * a single frame, giving a non-animated PNG control case.
+ *
  * Copyright (c) 2026 Jun Zhao
  *
  * This file is part of FFmpeg.
@@ -32,7 +37,10 @@
 #include <stdlib.h>
 #include <string.h>
 
-enum { CANVAS_W = 24, CANVAS_H = 20, FRAMES = 6 };
+/* Large enough to push the first IDAT past the 2048-byte probe buffer. */
+enum { CANVAS_W = 24, CANVAS_H = 20, FRAMES = 6, ANC_SIZE = 4096 };
+
+static const uint8_t ancillary[ANC_SIZE] = { 0 };
 
 static const uint8_t palette[16][3] = {
     {   0,   0,   0 }, { 255,   0,   0 }, {   0, 255,   0 }, {   0,   0, 255 },
@@ -42,7 +50,7 @@ static const uint8_t palette[16][3] = {
 };
 
 typedef struct {
-    uint8_t data[2048];
+    uint8_t data[ANC_SIZE + 2048];
     int len;
 } Buf;
 
@@ -153,7 +161,7 @@ static void put_stored(Buf *b, const uint8_t *raw, int n)
     put_be32(b, (s << 16) | a);
 }
 
-static void write_apng(const char *path, int depth)
+static void write_apng(const char *path, int depth, int big_anc, int plain)
 {
     static const uint8_t sig[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
     uint8_t ihdr[13] = { 0, 0, 0, CANVAS_W, 0, 0, 0, CANVAS_H, depth, 3 };
@@ -161,32 +169,37 @@ static void write_apng(const char *path, int depth)
     uint8_t plte[16 * 3], raw[512], fctl[26];
     Buf out = { 0 }, image = { 0 };
     FILE *f;
-    int colors = 1 << depth, seq = 0, frame, i;
+    int colors = 1 << depth, seq = 0, frame, frames = plain ? 1 : FRAMES, i;
 
     for (i = 0; i < colors; i++)
         memcpy(plte + 3 * i, palette[i], 3);
 
     put(&out, sig, 8);
     chunk(&out, "IHDR", ihdr, sizeof(ihdr));
-    chunk(&out, "acTL", actl, sizeof(actl));
+    if (!plain)
+        chunk(&out, "acTL", actl, sizeof(actl));
     chunk(&out, "PLTE", plte, colors * 3);
+    if (big_anc)
+        chunk(&out, "caBX", ancillary, sizeof(ancillary));
 
-    for (frame = 0; frame < FRAMES; frame++) {
+    for (frame = 0; frame < frames; frame++) {
         int sub = frame != 0;
         int x = sub ? 1 + 2 * (frame - 1) : 0;
         int y = sub ? 1 + (frame - 1) : 0;
         int w = sub ? 7 : CANVAS_W;
         int h = sub ? 5 : CANVAS_H;
 
-        memset(fctl, 0, sizeof(fctl));
-        fctl[3] = seq++;
-        fctl[7] = w;
-        fctl[11] = h;
-        fctl[15] = x;
-        fctl[19] = y;
-        fctl[21] = 1;  /* delay numerator */
-        fctl[23] = 10; /* delay denominator: 1/10 s */
-        chunk(&out, "fcTL", fctl, sizeof(fctl));
+        if (!plain) {
+            memset(fctl, 0, sizeof(fctl));
+            fctl[3] = seq++;
+            fctl[7] = w;
+            fctl[11] = h;
+            fctl[15] = x;
+            fctl[19] = y;
+            fctl[21] = 1;  /* delay numerator */
+            fctl[23] = 10; /* delay denominator: 1/10 s */
+            chunk(&out, "fcTL", fctl, sizeof(fctl));
+        }
 
         image.len = 0;
         if (sub)
@@ -220,6 +233,7 @@ int main(int argc, char **argv)
         fprintf(stderr, "apnggen: unsupported file name %s\n", path);
         return 1;
     }
-    write_apng(path, depth);
+    write_apng(path, depth, strstr(path, "biganc") != NULL,
+               strstr(path, "plain") != NULL);
     return 0;
 }
diff --git a/tests/fate/apng.mak b/tests/fate/apng.mak
index 5aec0ad7b71..19f122e697c 100644
--- a/tests/fate/apng.mak
+++ b/tests/fate/apng.mak
@@ -34,4 +34,26 @@ fate-apng-small-bpp-4bit: 
tests/data/fate/apng-small-bpp-4bit.png
 FATE_APNG_SMALL_BPP-$(call FRAMECRC, APNG, APNG) += $(FATE_APNG_SMALL_BPP)
 FATE_FFMPEG += $(FATE_APNG_SMALL_BPP-yes)
 
-fate-apng: $(FATE_APNG-yes) $(FATE_APNG_SMALL_BPP-yes)
+# A large ancillary chunk before the first IDAT must not hide the animation
+# from the probe (issue #26132). "plain" is a non-animated control: it has
+# the same big chunk but no acTL, so png_pipe must still win.
+FATE_APNG_BIGANC-$(call ALLYES, APNG_DEMUXER IMAGE_PNG_PIPE_DEMUXER) += 
fate-apng-biganc-probe
+fate-apng-biganc-probe: CMD = probefmt 
$(TARGET_PATH)/tests/data/fate/apng-biganc-1bit.png
+fate-apng-biganc-probe: REF = apng
+fate-apng-biganc-probe: CMP = oneline
+fate-apng-biganc-probe: tests/data/fate/apng-biganc-1bit.png
+
+FATE_APNG_BIGANC-$(call ALLYES, APNG_DEMUXER IMAGE_PNG_PIPE_DEMUXER) += 
fate-apng-biganc-plain-probe
+fate-apng-biganc-plain-probe: CMD = probefmt 
$(TARGET_PATH)/tests/data/fate/apng-biganc-plain-1bit.png
+fate-apng-biganc-plain-probe: REF = png_pipe
+fate-apng-biganc-plain-probe: CMP = oneline
+fate-apng-biganc-plain-probe: tests/data/fate/apng-biganc-plain-1bit.png
+
+FATE_FFPROBE += $(FATE_APNG_BIGANC-yes)
+
+FATE_APNG_BIGANC_FRAMES-$(call FRAMECRC, APNG, APNG) += fate-apng-biganc-frames
+fate-apng-biganc-frames: CMD = framecrc -i 
$(TARGET_PATH)/tests/data/fate/apng-biganc-1bit.png -fps_mode passthrough
+fate-apng-biganc-frames: tests/data/fate/apng-biganc-1bit.png
+FATE_FFMPEG += $(FATE_APNG_BIGANC_FRAMES-yes)
+
+fate-apng: $(FATE_APNG-yes) $(FATE_APNG_SMALL_BPP-yes) $(FATE_APNG_BIGANC-yes) 
$(FATE_APNG_BIGANC_FRAMES-yes)
diff --git a/tests/ref/fate/apng-biganc-frames 
b/tests/ref/fate/apng-biganc-frames
new file mode 100644
index 00000000000..b5b509025c7
--- /dev/null
+++ b/tests/ref/fate/apng-biganc-frames
@@ -0,0 +1,11 @@
+#tb 0: 1/10
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 24x20
+#sar 0: 0/1
+0,          0,          0,        1,     1920, 0x119ecd4e
+0,          1,          1,        1,     1920, 0x4ffdcc4f
+0,          2,          2,        1,     1920, 0x877cce4d
+0,          3,          3,        1,     1920, 0x1e02ce4d
+0,          4,          4,        1,     1920, 0x1551cf4c
+0,          5,          5,        1,     1920, 0x2f7ad447
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to