forge
Explore
Log in

Don't you dare go Hollow

A packet tail that begins a syncframe is refused by name, never dropped in silence

Browse files at this commit

49e4498dd02eccd986f9e81cf8813ec30e5b3666Ahmet Kaan GümüşParent: 9fcbbdf
  • fix
    • a packet tail that begins a syncframe is refused by name, never dropped in silence
Show full message
🛡️ fix(ac3): a packet tail that begins a syncframe is refused by name, never dropped in silence

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.rsmodified+53 -1
@@ -146,10 +146,17 @@ impl Ac3Decoder {
146146 /// broken bit stream [`Error::Corrupt`], and a construct this build does
147147 /// not implement [`Error::Unsupported`] with the reason named — never a
148148 /// panic, on any input.
149+ ///
150+ /// After the frame, `data` may hold only padding that is not itself a
151+ /// syncframe: container slack is ignored. A tail that *begins* another
152+ /// syncframe is stream this decoder would otherwise drop in silence — a
153+ /// dependent substream whose channels extend the frame, or a second frame
154+ /// altogether — and that is [`Error::Unsupported`] naming what it is;
155+ /// containers hand frames over one at a time.
149156 pub fn decode_frame(&mut self, data: &[u8]) -> Result<AudioFrame> {
150157 let mut samples = Vec::new();
151158 let (info, consumed) = self.decode_substream(data, &mut samples)?;
152- let _ = consumed;
159+ reject_tail(&data[consumed..])?;
153160 let layout = self.layout(&info);
154161 let interleaved = self.fold(samples, &info);
155162 let channels = layout.channel_count();
@@ -387,3 +394,48 @@ impl Decoder for Ac3Decoder {
387394 self.flushed = false;
388395 }
389396 }
397+
398+/// What the bytes after a decoded frame may say. Nothing, or padding that is
399+/// not itself a syncframe — the slack a container's block may carry around a
400+/// frame. A tail that *begins* a syncframe is stream the caller handed over in
401+/// one packet that this decoder decodes only the first of: a dependent
402+/// substream whose channels extend the frame, or a second frame altogether.
403+/// Dropping either loses audio in silence, so both are named refusals; a
404+/// `0x0B77` coincidence that does not parse as a header is junk, and junk is
405+/// the spec's own allowance.
406+fn reject_tail(tail: &[u8]) -> Result<()> {
407+ // Both syntaxes announce a frame with 0x0B77, and no header is shorter
408+ // than six bytes.
409+ if tail.len() < 6 || u16::from_be_bytes([tail[0], tail[1]]) != syncinfo::SYNCWORD {
410+ return Ok(());
411+ }
412+ let named = if tail[5] >> 3 > 10 {
413+ match eac3::parse_from(&mut ec_core::BitReader::new(tail)) {
414+ // The 7.1-as-5.1-plus-dependents shape: the first substream is
415+ // exactly what decoded, the tail is the extension channels this
416+ // build does not assemble.
417+ Ok(hdr) if hdr.strmtyp == eac3::StreamType::Dependent => Some((
418+ "E-AC-3 dependent substream in the tail of the packet",
419+ "a dependent substream carries channel extensions for an \
420+ independent one that has to be decoded first",
421+ )),
422+ Ok(_) => Some((
423+ "a second syncframe in the tail of the packet",
424+ "one packet is one syncframe and the substreams that belong \
425+ to it; hand frames to the decoder one at a time",
426+ )),
427+ // A syncword coincidence that is no header is padding, not stream.
428+ Err(_) => None,
429+ }
430+ } else {
431+ syncinfo::parse(tail).is_ok().then_some((
432+ "a second syncframe in the tail of the packet",
433+ "one packet is one syncframe and the substreams that belong \
434+ to it; hand frames to the decoder one at a time",
435+ ))
436+ };
437+ match named {
438+ Some((what, why)) => Err(Error::unsupported(what, why)),
439+ None => Ok(()),
440+ }
441+}
crates/ec-ac3/tests/decode_matrix.rsmodified+80 -0
@@ -275,3 +275,83 @@ fn damaged_input_never_panics() {
275275 }
276276 let _ = decoder.decode_frame(&noise);
277277 }
278+
279+/// A packet that carries a second frame after the first — the shape a
280+/// Matroska block for 7.1 DD+ really has (independent substream plus its
281+/// dependent) — must not decode to the first frame's samples in silence:
282+/// the tail is stream, and dropping it loses the extension channels without
283+/// a word. Built from two real frames of the fixture: the tail as it stands
284+/// (another independent frame) is refused as a second syncframe, and the
285+/// same packet with the tail's stream-type bits flipped to `dependent` is
286+/// refused as the dependent substream it then claims to be. The AC-3 path
287+/// answers the same way for an AC-3 tail.
288+#[test]
289+fn a_concatenated_tail_is_refused_by_name_not_dropped() {
290+ let dir = fixtures();
291+ if !dir.join("eac3-5.1-48000.eac3").exists() {
292+ eprintln!("skipping: no fixtures");
293+ return;
294+ }
295+ for name in ["ac3-5.1-48000.ac3", "eac3-5.1-48000.eac3"] {
296+ let data = std::fs::read(dir.join(name)).expect("fixture");
297+ let first = ec_ac3::frame_size(&data).expect("first frame size");
298+ let second = ec_ac3::frame_size(&data[first..]).expect("second frame size");
299+ let mut packet = data[..first].to_vec();
300+ packet.extend_from_slice(&data[first..first + second]);
301+
302+ let mut dec = Ac3Decoder::new();
303+ let err = dec
304+ .decode_frame(&packet)
305+ .expect_err("{name}: the tail is stream, not padding");
306+ let text = err.to_string();
307+ eprintln!("{name}: {text}");
308+ assert!(
309+ text.contains("second syncframe"),
310+ "{name}: the independent tail must name itself: {text}"
311+ );
312+ if name.starts_with("eac3") {
313+ // Flip the tail's stream-type (bits 16..18 of a frame: the top
314+ // two bits of byte 2) to `dependent`. The packet is then the
315+ // shape a 7.1 DD+ block carries, and the refusal names the
316+ // dependent substream.
317+ let mut dep_packet = packet.clone();
318+ dep_packet[first + 2] = (dep_packet[first + 2] & 0x3F) | 0x40;
319+ let err = dec
320+ .decode_frame(&dep_packet)
321+ .expect_err("the dependent tail is stream");
322+ let text = err.to_string();
323+ eprintln!("{name} (tail flipped to dependent): {text}");
324+ assert!(
325+ text.contains("dependent substream"),
326+ "the dependent tail must name itself: {text}"
327+ );
328+ }
329+ }
330+}
331+
332+/// The spec's own allowance: a container may pad a block around a frame, and
333+/// padding that is not itself a syncframe decodes exactly like the bare
334+/// frame — sample for sample, whatever its length.
335+#[test]
336+fn padding_that_is_not_a_syncframe_still_decodes() {
337+ let path = fixtures().join("ac3-5.1-48000.ac3");
338+ if !path.exists() {
339+ eprintln!("skipping: no fixtures");
340+ return;
341+ }
342+ let data = std::fs::read(&path).expect("fixture");
343+ let first = ec_ac3::frame_size(&data).expect("frame size");
344+ let frame = &data[..first];
345+ let mut dec = Ac3Decoder::new();
346+ let bare = dec.decode_frame(frame).expect("bare frame");
347+ for pad in [1usize, 6, 64, 1024] {
348+ let mut padded = frame.to_vec();
349+ padded.resize(first + pad, 0x00);
350+ let mut dec = Ac3Decoder::new();
351+ let got = dec.decode_frame(&padded).expect("padded frame decodes");
352+ assert!(
353+ got.data[0].iter().eq(bare.data[0].iter()),
354+ "pad of {pad} bytes changed the audio"
355+ );
356+ }
357+}