PR #26381 opened by Forgejo_Fairy
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26381
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26381.patch

The original Syndicate `INTRO.DAT` declares a 2,412-byte first frame whose 
header and two subchunks occupy 2,204 bytes. Reading the declared size consumes 
the following frame headers, so master emits only one frame. The [public 
sample](https://samples.ffmpeg.org/game-formats/flic-bullfrog/intro-syndicate.dat)
 has CRC32 `36518235`.

For the first frame in a short-header Bullfrog file, walk the declared 
subchunks. If the bytes immediately after the last subchunk contain a frame 
preamble, leave it for the next packet and correct the size in the current 
packet. The check is confined to the subchunk boundary and supports nonseekable 
input.

The original sample now decodes to 998 frames from both a file and a pipe. All 
RGB frame checksums matched a control copy with only the first size field 
corrected. The new FATE test reproduces the 208-byte overstatement using the 
existing Magic Carpet sample. Unpatched master emitted one of its 42 frames. 
With the patch, all 42 matched the existing reference and `make fate-flic` 
passed.

Addresses the Syndicate case in #26326.


>From dec6ae19cec5102355ce0d08a18a6010fd3d99ce Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Thu, 26 Nov 2026 20:13:12 +0000
Subject: [PATCH] avformat/flic: recover the first frame boundary in Bullfrog's
 Syndicate

The first frame in INTRO.DAT declares 2412 bytes. Its two subchunks
end after 2204 bytes, immediately before the next frame header. Using
the outer size consumes part of the following frames and loses sync.

For the first frame in a short-header Bullfrog file, walk the declared
subchunks and check for a frame header at their end. Leave the next
preamble buffered for the next packet, including on nonseekable input.

Exercise the same discrepancy with the existing Magic Carpet FATE sample
and compare every frame against its original reference.

Refs: #26326
Assisted-by: Fairy
---
 libavformat/flic.c   | 60 ++++++++++++++++++++++++++++++++++++++++----
 tests/fate/video.mak | 11 ++++++++
 2 files changed, 66 insertions(+), 5 deletions(-)

diff --git a/libavformat/flic.c b/libavformat/flic.c
index 01cd4698cf3..6c8624f550c 100644
--- a/libavformat/flic.c
+++ b/libavformat/flic.c
@@ -51,6 +51,7 @@
 #define FLIC_TFTD_SAMPLE_RATE 22050
 
 #define FLIC_HEADER_SIZE 128
+#define FLIC_BULLFROG_HEADER_SIZE 12
 #define FLIC_PREAMBLE_SIZE 6
 
 typedef struct FlicDemuxContext {
@@ -173,12 +174,12 @@ static int flic_read_header(AVFormatContext *s)
         avpriv_set_pts_info(st, 64, FLIC_MC_SPEED, 70);
 
         /* rewind the stream since the first chunk is at offset 12 */
-        avio_seek(pb, 12, SEEK_SET);
+        avio_seek(pb, FLIC_BULLFROG_HEADER_SIZE, SEEK_SET);
 
         /* send over abbreviated FLIC header chunk */
-        if ((ret = ff_alloc_extradata(st->codecpar, 12)) < 0)
+        if ((ret = ff_alloc_extradata(st->codecpar, 
FLIC_BULLFROG_HEADER_SIZE)) < 0)
             return ret;
-        memcpy(st->codecpar->extradata, header, 12);
+        memcpy(st->codecpar->extradata, header, FLIC_BULLFROG_HEADER_SIZE);
 
     } else if (magic_number == FLIC_FILE_MAGIC_1) {
         avpriv_set_pts_info(st, 64, speed, 70);
@@ -193,6 +194,50 @@ static int flic_read_header(AVFormatContext *s)
     return 0;
 }
 
+/* Syndicate's intro overstates the size of its first frame. Follow the
+ * subchunks to find the next frame, preserving any actual trailing padding. */
+static int flic_read_bullfrog_first_frame(AVIOContext *pb, AVPacket *pkt)
+{
+    int pos = 16, ret;
+
+    if ((ret = ffio_read_size(pb, pkt->data + FLIC_PREAMBLE_SIZE,
+                              pos - FLIC_PREAMBLE_SIZE)) < 0)
+        return ret;
+
+    for (int chunks = AV_RL16(pkt->data + 6);
+         pkt->size - pos >= FLIC_PREAMBLE_SIZE; chunks--) {
+        unsigned size;
+        int start = pos;
+
+        if (!chunks && (ret = ffio_ensure_seekback(pb, FLIC_PREAMBLE_SIZE)) < 
0)
+            return ret;
+        if ((ret = ffio_read_size(pb, pkt->data + pos, FLIC_PREAMBLE_SIZE)) < 
0)
+            return ret;
+        pos += FLIC_PREAMBLE_SIZE;
+        size = AV_RL32(pkt->data + start);
+
+        if (!chunks) {
+            if (size >= 16 && AV_RL16(pkt->data + start + 4) == 
FLIC_CHUNK_MAGIC_1) {
+                int64_t offset = avio_skip(pb, -FLIC_PREAMBLE_SIZE);
+                if (offset < 0)
+                    return offset;
+                av_shrink_packet(pkt, start);
+                AV_WL32(pkt->data, start);
+                return 0;
+            }
+            break;
+        }
+
+        if (size < FLIC_PREAMBLE_SIZE || size > pkt->size - start)
+            break;
+        if ((ret = ffio_read_size(pb, pkt->data + pos, size - 
FLIC_PREAMBLE_SIZE)) < 0)
+            return ret;
+        pos = start + size;
+    }
+
+    return ffio_read_size(pb, pkt->data + pos, pkt->size - pos);
+}
+
 static int flic_read_packet(AVFormatContext *s,
                             AVPacket *pkt)
 {
@@ -220,8 +265,13 @@ static int flic_read_packet(AVFormatContext *s,
             pkt->stream_index = flic->video_stream_index;
             pkt->pos = pos;
             memcpy(pkt->data, preamble, FLIC_PREAMBLE_SIZE);
-            ret = ffio_read_size(pb, pkt->data + FLIC_PREAMBLE_SIZE,
-                size - FLIC_PREAMBLE_SIZE);
+            if (pos == FLIC_BULLFROG_HEADER_SIZE && size >= 16)
+                ret = flic_read_bullfrog_first_frame(pb, pkt);
+            else
+                ret = ffio_read_size(pb, pkt->data + FLIC_PREAMBLE_SIZE,
+                                     size - FLIC_PREAMBLE_SIZE);
+            if (ret < 0)
+                break;
             pkt->flags = flic->frame_number == 0 ? AV_PKT_FLAG_KEY : 0;
             pkt->pts = flic->frame_number;
             if (flic->frame_number == 0)
diff --git a/tests/fate/video.mak b/tests/fate/video.mak
index e663b807a84..8b358f392d0 100644
--- a/tests/fate/video.mak
+++ b/tests/fate/video.mak
@@ -189,6 +189,17 @@ fate-flic-af12: CMD = framecrc -i 
$(TARGET_SAMPLES)/fli/jj00c2.fli -pix_fmt rgb2
 FATE_FLIC += fate-flic-magiccarpet
 fate-flic-magiccarpet: CMD = framecrc -i $(TARGET_SAMPLES)/fli/intel.dat 
-pix_fmt rgb24 -vf scale
 
+# Reproduce the oversized first frame in Syndicate using the Magic Carpet
+# sample. The first frame occupies 2200 bytes but declares 2408 (+208).
+tests/data/flic-bullfrog-size.dat: TAG = GEN
+tests/data/flic-bullfrog-size.dat: $(SAMPLES)/fli/intel.dat | tests/data
+       $(M)cat $< > $@ && printf '\150\011' | dd of=$@ bs=1 seek=12 
conv=notrunc 2>/dev/null
+
+FATE_FLIC += fate-flic-bullfrog-size
+fate-flic-bullfrog-size: tests/data/flic-bullfrog-size.dat
+fate-flic-bullfrog-size: CMD = framecrc -i 
$(TARGET_PATH)/tests/data/flic-bullfrog-size.dat -pix_fmt rgb24 -vf scale
+fate-flic-bullfrog-size: REF = $(SRC_PATH)/tests/ref/fate/flic-magiccarpet
+
 FATE_FLIC-$(call FRAMECRC, FLIC, FLIC, SCALE_FILTER) += $(FATE_FLIC)
 FATE_VIDEO += $(FATE_FLIC-yes)
 fate-flic: $(FATE_FLIC-yes)
-- 
2.54.1

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

Reply via email to