From 65e0b1a9b201b8e31bc0c25194ccf2d4f195633d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 07:43:16 +0300 Subject: [PATCH] FLAC framing: frame end is the first sync whose preceding frame passes its footer CRC-16, not the first header-plausible sync flac-5.1-44100.flac (pinned ffmpeg 9.0.2, sha256 matches fixtures.sha256): packet 7 is at byte 45447, 5908 bytes. A sync-like header at byte 47501 inside the audio passes header CRC-8, so the probe cut the packet there and the subframe decode ran out of data (need more data). ffprobe lists the packet as 5908 bytes; the footer CRC-16 over [45447,51353) equals the stated 53762 at 51355 and does not match at 47501. Falls back to the first plausible sync when no candidate verifies, so damaged streams behave as before --- crates/ec-flac/src/decode.rs | 13 +++++++++++++ crates/ec-probe/src/raw.rs | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/ec-flac/src/decode.rs b/crates/ec-flac/src/decode.rs index 8b6248e6..3cbe64b6 100644 --- a/crates/ec-flac/src/decode.rs +++ b/crates/ec-flac/src/decode.rs @@ -534,6 +534,19 @@ pub fn find_frame_sync(data: &[u8], from: usize, info: Option<&StreamInfo>) -> O None } +pub fn next_frame_start(data: &[u8], from: usize, info: Option<&StreamInfo>) -> Option { + let mut at = from; + let mut first = None; + while let Some(c) = find_frame_sync(data, at, info) { + first.get_or_insert(c); + if c >= 2 && crc16(&data[..c - 2]) == u16::from_be_bytes([data[c - 2], data[c - 1]]) { + return Some(c); + } + at = c + 1; + } + first +} + /// Parse a frame header, resolving the "same as `STREAMINFO`" codes. pub fn parse_frame_header(r: &mut BitReader<'_>, info: Option<&StreamInfo>) -> Result { if r.read_bits(14)? != 0x3ffe { diff --git a/crates/ec-probe/src/raw.rs b/crates/ec-probe/src/raw.rs index a5ec5e0b..cf0c9aa5 100644 --- a/crates/ec-probe/src/raw.rs +++ b/crates/ec-probe/src/raw.rs @@ -320,7 +320,7 @@ fn frame_at(kind: Kind, data: &[u8], flac: Option<&ec_flac::StreamInfo>) -> Resu let header = ec_flac::decode::parse_frame_header(&mut r, flac)?; // A frame ends where the next one starts; the last frame of a file // ends at the file's end, which the caller's `NeedMore` handles. - let bytes = ec_flac::decode::find_frame_sync(data, 1, flac).ok_or(Error::NeedMore)?; + let bytes = ec_flac::decode::next_frame_start(data, 1, flac).ok_or(Error::NeedMore)?; Ok(Framing { bytes, samples: header.block_size as u64,