From 49e4498dd02eccd986f9e81cf8813ec30e5b3666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 13 Sep 2026 02:18:53 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A1=EF=B8=8F=20fix(ac3):=20a=20packet?= =?UTF-8?q?=20tail=20that=20begins=20a=20syncframe=20is=20refused=20by=20n?= =?UTF-8?q?ame,=20never=20dropped=20in=20silence?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit decode_frame decoded the first substream and discarded the rest of the packet (let _ = consumed) -- the shape a Matroska block for 7.1 DD+ really carries (independent substream plus its dependent): 1536 samples out, the extension channels gone, no error. Old decoders of record walked that tail, so the silent drop was a regression hiding under the named refusals. Now reject_tail runs after every successful frame: padding that is not itself a syncframe (the container slack the spec allows) still decodes sample-for-sample like the bare frame, but a tail that begins a syncframe is Unsupported naming what it is -- an E-AC-3 dependent substream in the tail (the 7.1 extension channels) or a second syncframe altogether (one packet is one frame and its substreams; hand frames over one at a time). A 0x0B77 coincidence that parses as no header stays junk. Witnesses in decode_matrix: concatenated real fixture frames (AC-3 and E-AC-3) refuse as second syncframes; the E-AC-3 tail flipped to dependent refuses with the dependent wording; padded frames decode bit-identically to bare ones. Fixture matrix vs ffmpeg stays green. --- crates/ec-ac3/src/decoder.rs | 54 ++++++++++++++++++- crates/ec-ac3/tests/decode_matrix.rs | 80 ++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 1 deletion(-) diff --git a/crates/ec-ac3/src/decoder.rs b/crates/ec-ac3/src/decoder.rs index 7847681f..05ee4706 100644 --- a/crates/ec-ac3/src/decoder.rs +++ b/crates/ec-ac3/src/decoder.rs @@ -146,10 +146,17 @@ impl Ac3Decoder { /// broken bit stream [`Error::Corrupt`], and a construct this build does /// not implement [`Error::Unsupported`] with the reason named — never a /// panic, on any input. + /// + /// After the frame, `data` may hold only padding that is not itself a + /// syncframe: container slack is ignored. A tail that *begins* another + /// syncframe is stream this decoder would otherwise drop in silence — a + /// dependent substream whose channels extend the frame, or a second frame + /// altogether — and that is [`Error::Unsupported`] naming what it is; + /// containers hand frames over one at a time. pub fn decode_frame(&mut self, data: &[u8]) -> Result { let mut samples = Vec::new(); let (info, consumed) = self.decode_substream(data, &mut samples)?; - let _ = consumed; + reject_tail(&data[consumed..])?; let layout = self.layout(&info); let interleaved = self.fold(samples, &info); let channels = layout.channel_count(); @@ -387,3 +394,48 @@ impl Decoder for Ac3Decoder { self.flushed = false; } } + +/// What the bytes after a decoded frame may say. Nothing, or padding that is +/// not itself a syncframe — the slack a container's block may carry around a +/// frame. A tail that *begins* a syncframe is stream the caller handed over in +/// one packet that this decoder decodes only the first of: a dependent +/// substream whose channels extend the frame, or a second frame altogether. +/// Dropping either loses audio in silence, so both are named refusals; a +/// `0x0B77` coincidence that does not parse as a header is junk, and junk is +/// the spec's own allowance. +fn reject_tail(tail: &[u8]) -> Result<()> { + // Both syntaxes announce a frame with 0x0B77, and no header is shorter + // than six bytes. + if tail.len() < 6 || u16::from_be_bytes([tail[0], tail[1]]) != syncinfo::SYNCWORD { + return Ok(()); + } + let named = if tail[5] >> 3 > 10 { + match eac3::parse_from(&mut ec_core::BitReader::new(tail)) { + // The 7.1-as-5.1-plus-dependents shape: the first substream is + // exactly what decoded, the tail is the extension channels this + // build does not assemble. + Ok(hdr) if hdr.strmtyp == eac3::StreamType::Dependent => Some(( + "E-AC-3 dependent substream in the tail of the packet", + "a dependent substream carries channel extensions for an \ + independent one that has to be decoded first", + )), + Ok(_) => Some(( + "a second syncframe in the tail of the packet", + "one packet is one syncframe and the substreams that belong \ + to it; hand frames to the decoder one at a time", + )), + // A syncword coincidence that is no header is padding, not stream. + Err(_) => None, + } + } else { + syncinfo::parse(tail).is_ok().then_some(( + "a second syncframe in the tail of the packet", + "one packet is one syncframe and the substreams that belong \ + to it; hand frames to the decoder one at a time", + )) + }; + match named { + Some((what, why)) => Err(Error::unsupported(what, why)), + None => Ok(()), + } +} diff --git a/crates/ec-ac3/tests/decode_matrix.rs b/crates/ec-ac3/tests/decode_matrix.rs index aca3dcb3..181bb137 100644 --- a/crates/ec-ac3/tests/decode_matrix.rs +++ b/crates/ec-ac3/tests/decode_matrix.rs @@ -275,3 +275,83 @@ fn damaged_input_never_panics() { } let _ = decoder.decode_frame(&noise); } + +/// A packet that carries a second frame after the first — the shape a +/// Matroska block for 7.1 DD+ really has (independent substream plus its +/// dependent) — must not decode to the first frame's samples in silence: +/// the tail is stream, and dropping it loses the extension channels without +/// a word. Built from two real frames of the fixture: the tail as it stands +/// (another independent frame) is refused as a second syncframe, and the +/// same packet with the tail's stream-type bits flipped to `dependent` is +/// refused as the dependent substream it then claims to be. The AC-3 path +/// answers the same way for an AC-3 tail. +#[test] +fn a_concatenated_tail_is_refused_by_name_not_dropped() { + let dir = fixtures(); + if !dir.join("eac3-5.1-48000.eac3").exists() { + eprintln!("skipping: no fixtures"); + return; + } + for name in ["ac3-5.1-48000.ac3", "eac3-5.1-48000.eac3"] { + let data = std::fs::read(dir.join(name)).expect("fixture"); + let first = ec_ac3::frame_size(&data).expect("first frame size"); + let second = ec_ac3::frame_size(&data[first..]).expect("second frame size"); + let mut packet = data[..first].to_vec(); + packet.extend_from_slice(&data[first..first + second]); + + let mut dec = Ac3Decoder::new(); + let err = dec + .decode_frame(&packet) + .expect_err("{name}: the tail is stream, not padding"); + let text = err.to_string(); + eprintln!("{name}: {text}"); + assert!( + text.contains("second syncframe"), + "{name}: the independent tail must name itself: {text}" + ); + if name.starts_with("eac3") { + // Flip the tail's stream-type (bits 16..18 of a frame: the top + // two bits of byte 2) to `dependent`. The packet is then the + // shape a 7.1 DD+ block carries, and the refusal names the + // dependent substream. + let mut dep_packet = packet.clone(); + dep_packet[first + 2] = (dep_packet[first + 2] & 0x3F) | 0x40; + let err = dec + .decode_frame(&dep_packet) + .expect_err("the dependent tail is stream"); + let text = err.to_string(); + eprintln!("{name} (tail flipped to dependent): {text}"); + assert!( + text.contains("dependent substream"), + "the dependent tail must name itself: {text}" + ); + } + } +} + +/// The spec's own allowance: a container may pad a block around a frame, and +/// padding that is not itself a syncframe decodes exactly like the bare +/// frame — sample for sample, whatever its length. +#[test] +fn padding_that_is_not_a_syncframe_still_decodes() { + let path = fixtures().join("ac3-5.1-48000.ac3"); + if !path.exists() { + eprintln!("skipping: no fixtures"); + return; + } + let data = std::fs::read(&path).expect("fixture"); + let first = ec_ac3::frame_size(&data).expect("frame size"); + let frame = &data[..first]; + let mut dec = Ac3Decoder::new(); + let bare = dec.decode_frame(frame).expect("bare frame"); + for pad in [1usize, 6, 64, 1024] { + let mut padded = frame.to_vec(); + padded.resize(first + pad, 0x00); + let mut dec = Ac3Decoder::new(); + let got = dec.decode_frame(&padded).expect("padded frame decodes"); + assert!( + got.data[0].iter().eq(bare.data[0].iter()), + "pad of {pad} bytes changed the audio" + ); + } +}