From fd360a4890505b748b0c2604d100e05db0d5b53f Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 16 Oct 2026 11:48:13 -0700 Subject: [PATCH 01/10] chore: claim DVB E-AC-3 export quest Co-Authored-By: GPT-6 From 661085b00a74dc977a83fd58d30374dc8497e9d1 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Fri, 16 Oct 2026 12:01:44 -0700 Subject: [PATCH 02/10] fix(ts): split and buffer-model DVB E-AC-3 access units Co-Authored-By: GPT-6 --- quest/m2/README.md | 1 - quest/m2/ts-eac3.md | 29 -------- rs/moq-mux/src/codec/eac3.rs | 32 +++++---- rs/moq-mux/src/container/ts/export.rs | 81 +++++++++++++++++++--- rs/moq-mux/src/container/ts/export_test.rs | 44 +++++++++++- test/ts/compliance.py | 5 +- test/ts/tstd-controls.py | 23 ++++-- 7 files changed, 155 insertions(+), 60 deletions(-) delete mode 100644 quest/m2/ts-eac3.md diff --git a/quest/m2/README.md b/quest/m2/README.md index b41215ace05..6f9676b2ef4 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -82,7 +82,6 @@ ranked just below deterministic TS legs, which it requires. - [Watch decode gate](/quest/m2/watch-decode-gate.md) - video lookahead stays encoded until it is near presentation, like audio - [Deterministic TS legs](/quest/m2/ts-hitless.md) - two `--sync` legs of one broadcast render every common slot identically, continuity counters included, through complementary loss - [TS over RTP legs](/quest/m2/ts-rtp-egress.md) - two legs send identical RTP packets, headers included, so an ST 2022-7 receiver can merge them -- [DVB E-AC-3](/quest/m2/ts-eac3.md) - E-AC-3 private data is split per access unit (independent frame plus its dependent substreams) and buffer-modelled in TS export - [moqsrc A/V sync](/quest/m2/moqsrc-av-sync.md) - a nightly test measures A/V skew from `moqsrc` through clock-synced sinks - [MP4 export](/quest/m2/mp4-export.md) - `moq export mp4 --output` records crash-safe fragments, then finishes a regular MP4 with moov at the end - [fMP4 edit lists](/quest/m2/fmp4-edit-lists.md) - the fMP4 importer applies edit lists to frame timestamps diff --git a/quest/m2/ts-eac3.md b/quest/m2/ts-eac3.md deleted file mode 100644 index d7ee41f0b77..00000000000 --- a/quest/m2/ts-eac3.md +++ /dev/null @@ -1,29 +0,0 @@ -# [S] DVB E-AC-3 gets a buffer model in TS export - -## Goal - -`moq export ts` splits DVB E-AC-3 private-data PES (`stream_type` 0x06 with -an `enhanced_AC3_descriptor`) one access unit per PES, schedules each against -the E-AC-3 buffer ETSI TS 101 154 gives DVB, and `test/ts/compliance.py` -grades it. That's the same treatment DVB AC-3 got in -the fixed-delay export (#4645). - -## Plan - -Found in the fixed-delay export review: E-AC-3 carried as DVB private data still goes out unsplit, -with no buffer model, so a multi-frame PES can overflow B. - -- Split it the way `rs/moq-mux/src/container/ts/export.rs` splits DVB AC-3, but per access unit: an - independent sync frame plus the dependent substreams that follow it share - one presentation interval (ETSI TS 102 366 Annex E), so they go out in one - PES on one decode time. Read sizes from each sync frame's `frmsiz`, and - drop a frame shorter than its header says. -- The buffer is the DVB one, not ATSC's: the export's 12,896 B is A/52 Annex G's - for `stream_type` 0x87, and DVB AC-3 uses 5,696 B. Take the E-AC-3 value - from ETSI TS 101 154, cite the clause, and add it to the schedule's - per-PID admission and to the `tstd` model in `compliance.py`, which - refuses DVB E-AC-3 today. -- Test: a synthetic multi-frame E-AC-3 PES exports one access unit per PES; - an independent-plus-dependent fixture keeps both substreams on one - interval (32 ms for six 48 kHz blocks, not 64); and a control in - `tstd-controls.py` overflows B without the split. diff --git a/rs/moq-mux/src/codec/eac3.rs b/rs/moq-mux/src/codec/eac3.rs index 050e14950d9..54a7158ca86 100644 --- a/rs/moq-mux/src/codec/eac3.rs +++ b/rs/moq-mux/src/codec/eac3.rs @@ -28,6 +28,24 @@ const CHANNELS: [u32; 8] = [2, 1, 2, 3, 3, 4, 4, 5]; /// Parse an E-AC-3 sync frame header from the start of `data` (needs >= 6 bytes). pub(crate) fn parse_header(data: &[u8]) -> legacy::Result { + let header = sync_header(data)?; + // A dependent substream (strmtyp 1) extends a prior program to 7.1+, and a + // nonzero substreamid carries additional programs in the same PID. Either + // would make this track's channel count a lie, so they are rejected rather + // than mis-described. + let strmtyp = data[2] >> 6; + if strmtyp == 1 { + return Err(legacy::Error::Eac3DependentSubstream); + } + let substreamid = (data[2] >> 3) & 0x07; + if substreamid != 0 { + return Err(legacy::Error::Eac3AdditionalSubstream(substreamid)); + } + Ok(header) +} + +/// Parse a sync frame's bounds and duration without assigning its substream to an audio track. +pub(crate) fn sync_header(data: &[u8]) -> legacy::Result { if data.len() < 6 { return Err(legacy::Error::Eac3HeaderTooShort); } @@ -42,21 +60,9 @@ pub(crate) fn parse_header(data: &[u8]) -> legacy::Result { return Err(legacy::Error::Eac3NotEac3Bsid(bsid)); } - // A dependent substream (strmtyp 1) extends a prior program to 7.1+, and a - // nonzero substreamid carries additional programs in the same PID. Either - // would make this track's channel count a lie, so they are rejected rather - // than mis-described. - let strmtyp = data[2] >> 6; - if strmtyp == 3 { + if data[2] >> 6 == 3 { return Err(legacy::Error::Eac3ReservedStreamType); } - if strmtyp == 1 { - return Err(legacy::Error::Eac3DependentSubstream); - } - let substreamid = (data[2] >> 3) & 0x07; - if substreamid != 0 { - return Err(legacy::Error::Eac3AdditionalSubstream(substreamid)); - } let frmsiz = (((data[2] & 0x07) as usize) << 8) | data[3] as usize; let len = (frmsiz + 1) * 2; diff --git a/rs/moq-mux/src/container/ts/export.rs b/rs/moq-mux/src/container/ts/export.rs index afa1d7a998e..24ce148328d 100644 --- a/rs/moq-mux/src/container/ts/export.rs +++ b/rs/moq-mux/src/container/ts/export.rs @@ -317,8 +317,8 @@ impl Track { self.restart = pending.restart; self.clock = DecodeClock::default(); } - if self.carries_ac3() { - return self.queue_ac3(name, pending, jitter); + if self.carries_ac3() || self.carries_eac3() { + return self.queue_dolby(name, pending, jitter); } let Kind::Video(stream_type) = self.kind else { return self.push(name, pending, None, None, jitter); @@ -392,11 +392,11 @@ impl Track { Ok(()) } - /// Queue each AC-3 sync frame of a passed-through PES on its own, due at its own decode + /// Queue each Dolby access unit of a passed-through PES on its own, due at its own decode /// time: a receiver's buffer holds and decodes each on its own, and the whole PES would /// overflow it. A frame the last PES began decodes ahead of this PES, so the jitter buffer /// has to order it on its own time, not the PES's. - fn queue_ac3( + fn queue_dolby( &mut self, name: &str, pending: Pending, @@ -409,7 +409,12 @@ impl Track { arrived, read, } = pending; - let Some(frames) = ac3_frames(&mut self.ac3_carry, skip, frame.timestamp, &frame.payload) else { + let frames = if self.carries_eac3() { + Some(eac3_units(frame.timestamp, &frame.payload)?) + } else { + ac3_frames(&mut self.ac3_carry, skip, frame.timestamp, &frame.payload) + }; + let Some(frames) = frames else { let pending = Pending { frame, restart, @@ -455,6 +460,21 @@ impl Track { .any(|descriptor| descriptor.tag == AC3_DESCRIPTOR) } + /// DVB E-AC-3 remains private data, with its descriptor identifying the decoder buffer. + fn carries_eac3(&self) -> bool { + matches!( + self.kind, + Kind::Verbatim { + stream_type: 0x06, + framing: catalog::Framing::Pes, + .. + } + ) && self + .descriptors + .iter() + .any(|descriptor| descriptor.tag == EAC3_DESCRIPTOR) + } + /// The samples an audio frame of `payload` lasts and their rate, if it may share a PES with /// the frames around it: one whole frame of audio whose receiver removes each frame from /// its buffer at the frame's own decode time. @@ -2948,11 +2968,14 @@ fn audio_buffer(config: &AudioConfig, kind: &Kind) -> Buffer { /// The DVB AC-3 descriptor's tag (ETSI EN 300 468 6.2.1), marking AC-3 as private data. const AC3_DESCRIPTOR: u8 = 0x6a; +/// The DVB Enhanced AC-3 descriptor's tag (ETSI EN 300 468 Annex D). +const EAC3_DESCRIPTOR: u8 = 0x7a; + /// The DVB teletext descriptor's tag (ETSI EN 300 468 6.2.43). const TELETEXT_DESCRIPTOR: u8 = 0x56; -/// A verbatim stream's buffers, for the kinds its descriptors identify: AC-3 as DVB private -/// data (ATSC A/52 Annex A 5.4), and teletext, whose 480-byte transport buffer drains at +/// A verbatim stream's buffers, for the kinds its descriptors identify: Dolby audio as DVB +/// private data (ETSI TS 101 154 4.1.6.11), and teletext, whose 480-byte transport buffer drains at /// 6.75 Mb/s (ETSI EN 300 472 5). Any other stream has none the schedule knows. fn verbatim_buffer(kind: &Kind, descriptors: &[catalog::Descriptor]) -> Option { let Kind::Verbatim { @@ -2964,7 +2987,8 @@ fn verbatim_buffer(kind: &Kind, descriptors: &[catalog::Descriptor]) -> Option Some(Buffer { + // ETSI TS 101 154 V2.9.1 4.1.6.11 recommends 5,696 bytes for both DVB codecs. + AC3_DESCRIPTOR | EAC3_DESCRIPTOR => Some(Buffer { rate: 2_000_000, leak: None, size: Some(5_696), @@ -2982,6 +3006,47 @@ fn verbatim_buffer(kind: &Kind, descriptors: &[catalog::Descriptor]) -> Option anyhow::Result> { + let mut units = Vec::new(); + let (mut offset, mut begin) = (0, None); + let mut at = start; + let mut period = None; + while offset < payload.len() { + let rest = &payload[offset..]; + if rest.len() < 6 { + break; + } + let header = crate::codec::eac3::sync_header(rest)?; + if rest.len() < header.len { + break; + } + let primary = rest[2] >> 6 != 1 && (rest[2] >> 3) & 7 == 0; + if primary { + if let Some(begin) = begin { + units.push((at, payload.slice(begin..offset))); + at = at.checked_add(period.unwrap())?; + } + begin = Some(offset); + period = Some(Timestamp::from_micros( + header.samples * 1_000_000 / u64::from(header.sample_rate), + )?); + } else if begin.is_some() { + let child = Timestamp::from_micros(header.samples * 1_000_000 / u64::from(header.sample_rate))?; + anyhow::ensure!( + Some(child) == period, + "E-AC-3 substreams have different presentation intervals" + ); + } + offset += header.len; + } + if let Some(begin) = begin { + units.push((at, payload.slice(begin..offset))); + } + Ok(units) +} + /// An AC-3 sync frame a passed-through PES left unfinished. struct Ac3Carry { /// The playhead count it was read under ([`Pending::skip`]). diff --git a/rs/moq-mux/src/container/ts/export_test.rs b/rs/moq-mux/src/container/ts/export_test.rs index 1ac00ebca6b..3f0e245c627 100644 --- a/rs/moq-mux/src/container/ts/export_test.rs +++ b/rs/moq-mux/src/container/ts/export_test.rs @@ -1629,6 +1629,44 @@ async fn export_packs_ac3_frames_up_to_their_buffer() { ); } +/// One six-block 48 kHz E-AC-3 sync frame, with an optional dependent substream. +fn eac3_frame(dependent: bool) -> Vec { + let mut frame = vec![0x0b, 0x77, if dependent { 0x40 } else { 0 }, 0xff, 0x3f, 0x80]; + frame.resize(512, 0xaa); + frame +} + +#[tokio::test(start_paused = true)] +async fn export_splits_dvb_eac3_access_units() { + let frame = eac3_frame(false); + let pes = exported_dolby_pes(vec![Ac3Write::Pes(0, frame.repeat(3))], 0x7a).await; + assert_eq!(pes.len(), 3); + for (i, (pts, payload)) in pes.iter().enumerate() { + assert_eq!(*pts, pes[0].0 + i as u64 * 2_880); + assert_eq!(*payload, frame); + } +} + +#[tokio::test(start_paused = true)] +async fn export_keeps_dvb_eac3_dependent_substreams_on_one_interval() { + let unit = [eac3_frame(false), eac3_frame(true)].concat(); + let pes = exported_dolby_pes(vec![Ac3Write::Pes(0, unit.repeat(3))], 0x7a).await; + assert_eq!(pes.len(), 3); + for (i, (pts, payload)) in pes.iter().enumerate() { + assert_eq!(*pts, pes[0].0 + i as u64 * 2_880, "one 32 ms interval per access unit"); + assert_eq!(*payload, unit); + } +} + +#[tokio::test(start_paused = true)] +async fn export_drops_dvb_eac3_partial_frames() { + let frame = eac3_frame(false); + let payload = [frame.clone(), frame[..60].to_vec()].concat(); + let pes = exported_dolby_pes(vec![Ac3Write::Pes(0, payload)], 0x7a).await; + assert_eq!(pes.len(), 1); + assert_eq!(pes[0].1, frame); +} + /// A 32 kb/s 48 kHz 2/0 AC-3 sync frame: 128 bytes, 1,536 samples (32 ms). fn ac3_sync() -> Vec { let mut sync = vec![0x0b, 0x77, 0x00, 0x00, 0x00, 0x40, 0x40]; @@ -1664,6 +1702,10 @@ enum Ac3Write { /// Export `input` on an AC-3 passthrough PID beside bbb's media, and return the PTS and /// payload of every PES on that PID. async fn exported_ac3_pes(input: Vec) -> Vec<(u64, Vec)> { + exported_dolby_pes(input, 0x6a).await +} + +async fn exported_dolby_pes(input: Vec, descriptor: u8) -> Vec<(u64, Vec)> { const AC3_PID: u16 = 0x104; let data = include_bytes!("test_data/bbb.ts"); let mut broadcast = moq_net::broadcast::Info::new().produce(); @@ -1683,7 +1725,7 @@ async fn exported_ac3_pes(input: Vec) -> Vec<(u64, Vec)> { let mut track = tscat::Track::new(AC3_PID); track.verbatim = Some(tscat::Verbatim::new(0x06, tscat::Framing::Pes)); track.descriptors = vec![tscat::Descriptor { - tag: 0x6a, + tag: descriptor, data: Bytes::from_static(&[0x00]), }]; catalog diff --git a/test/ts/compliance.py b/test/ts/compliance.py index 67b8096ae3e..55f6ebfd057 100755 --- a/test/ts/compliance.py +++ b/test/ts/compliance.py @@ -424,6 +424,8 @@ def check_service_descriptors(analysis: dict) -> Check: EAC3_ATSC_BS = 12896 # ATSC A/52 Annex A 5.4: AC-3 carried as DVB private data. AC3_DVB_BS = 5696 +# ETSI TS 101 154 V2.9.1 4.1.6.11 gives Enhanced AC-3 the same DVB buffer. +EAC3_DVB_BS = 5696 MPEG_AUDIO_KBPS = { (1, 1): (0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448), @@ -792,7 +794,7 @@ def stream_kind(stream: Stream) -> str | None: if kind == 0x06 and f"registration:{int.from_bytes(b'Opus', 'big')}" in tags: return "opus" if kind == 0x06 and tags & {"DVB_enhanced_AC3_descriptor", "enhanced_AC3_descriptor"}: - raise Refused("E-AC-3 as DVB private data takes its buffer from ETSI TS 101 154, not modelled") + return "eac3-dvb" if kind in (0x01, 0x02, 0x10, 0x11, 0x1C, 0x20, 0x21, 0x42, 0xD1, 0xEA): raise Refused(f"stream_type 0x{kind:02X} is audio/video this model has no parameters for") return None @@ -830,6 +832,7 @@ def stream_params(kind: str, stream: Stream) -> tuple[Params, object]: "ac3-atsc": ("AC-3 (ATSC)", AC3_ATSC_BS, ac3_frame), "eac3-atsc": ("E-AC-3 (ATSC)", EAC3_ATSC_BS, ac3_frame), "ac3-dvb": ("AC-3 (DVB)", AC3_DVB_BS, ac3_frame), + "eac3-dvb": ("E-AC-3 (DVB)", EAC3_DVB_BS, ac3_frame), }[kind] return Params(label=label, rx=AUDIO_RX, b=bs), parse diff --git a/test/ts/tstd-controls.py b/test/ts/tstd-controls.py index dbcbe4be16d..1ab1a63ce53 100755 --- a/test/ts/tstd-controls.py +++ b/test/ts/tstd-controls.py @@ -38,6 +38,8 @@ last frame is cut off by the end of the capture: straddling audio as built must pass + DVB E-AC-3 reference three 512-byte frames per PES, DVB buffer must pass + DVB E-AC-3 unsplit sixteen frames in one PES B overflow mid-packet overflow seven frames per PES, the first decoded as B overflow B passes its size partway through a packet @@ -173,20 +175,25 @@ def pes(units: list[bytes], dts_s: float) -> None: handle.write(b"".join(out)) -def synthetic_audio(path: str, frames: int, decode_s: float) -> None: - """MPEG-1 Layer II at 192 kb/s, `frames` 576-byte frames per PES, the first decoded - `decode_s` after its PES starts arriving.""" +def synthetic_audio(path: str, frames: int, decode_s: float, enhanced: bool = False) -> None: + """MPEG audio or DVB E-AC-3, `frames` per PES, decoded `decode_s` after arrival.""" audio_pid, pcr_pid = 0x101, 0x102 pat = section(0x00, 1, (1).to_bytes(2, "big") + (0xE000 | PMT_PID).to_bytes(2, "big")) pmt = section(0x02, 1, bytes([0xE1, 0x02, 0xF0, 0x00, 0x03, 0xE1, 0x01, 0xF0, 0x00])) frame = bytes([0xFF, 0xFD, 0xA4, 0x00]) + b"\xaa" * 572 + interval = 0.024 + if enhanced: + # DVB private-data E-AC-3, six 48 kHz blocks per 512-byte sync frame. + pmt = section(0x02, 1, bytes.fromhex("e102 f000 06 e101 f003 7a0100")) + frame = bytes.fromhex("0b7700ff3f80") + b"\xaa" * 506 + interval = 0.032 # An audio packet every six slots, past TB's 0.75 ms drain, so each one meets an # empty TB and its byte j leaves (j + 1) x 4 us after it starts arriving. every = 6 cc = 0 out: list[bytes] = [] for n in range(20): - begin = round(n * frames * 0.024 / SLOT_S) + begin = round(n * frames * interval / SLOT_S) while len(out) < begin: out.append(packet(0x1FFF, 0, b"\xff" * 184)) ticks = round((1.0 + begin * SLOT_S + decode_s) * compliance.PTS_HZ) @@ -204,7 +211,7 @@ def synthetic_audio(path: str, frames: int, decode_s: float) -> None: pcr = round((1.0 + len(out) * SLOT_S) * compliance.PCR_HZ) field = bytes([0x10]) + ((pcr // 300) << 15 | 0x7E00 | pcr % 300).to_bytes(6, "big") out.append(packet(pcr_pid, 0, adaptation=field)) - while len(out) < round(20 * frames * 0.024 / SLOT_S): + while len(out) < round(20 * frames * interval / SLOT_S): out.append(packet(0x1FFF, 0, b"\xff" * 184)) with open(path, "wb") as handle: handle.write(b"".join(out)) @@ -266,11 +273,11 @@ def build(path: str) -> str: return build -def audio(frames: int, decode_s: float): +def audio(frames: int, decode_s: float, enhanced: bool = False): """A builder for one synthetic audio stream.""" def build(path: str) -> str: - synthetic_audio(path, frames, decode_s) + synthetic_audio(path, frames, decode_s, enhanced) return path return build @@ -306,6 +313,8 @@ def build(path: str) -> str: ("PCR PID mismatch", built("PCR PID mismatch"), ("fails", "pcr-presence")), ("straddling audio", audio(4, STRADDLE_S), "pass"), ("mid-packet overflow", audio(7, MID_PACKET_S), {"B overflow"}), + ("DVB E-AC-3 reference", audio(3, 0.04, enhanced=True), "pass"), + ("DVB E-AC-3 unsplit PES", audio(16, 0.1, enhanced=True), {"B overflow"}), ] def grade(path: str) -> dict[str, compliance.Check]: From 4ef69e06b57742bac5e776c7cccd97cfd38ca8c9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 17 Oct 2026 07:45:37 -0700 Subject: [PATCH 03/10] fix(ts): reassemble DVB E-AC-3 access units split across PES Generalize the AC-3 passthrough reassembly to E-AC-3: carry a sync frame a PES leaves unfinished, search for sync at a join, and drop a frame cut by a lost PES. Dependent and additional substreams join their access unit; one the next PES carries goes out at its unit's time. Units count on from the PES's PTS by summed samples, and a substream whose blocks or rate differ from its unit's is refused. Co-Authored-By: Claude Opus 5.5 --- rs/moq-mux/src/codec/eac3.rs | 7 + rs/moq-mux/src/container/ts/export.rs | 360 +++++++++++++-------- rs/moq-mux/src/container/ts/export_test.rs | 65 ++++ 3 files changed, 299 insertions(+), 133 deletions(-) diff --git a/rs/moq-mux/src/codec/eac3.rs b/rs/moq-mux/src/codec/eac3.rs index 54a7158ca86..d8e547615ca 100644 --- a/rs/moq-mux/src/codec/eac3.rs +++ b/rs/moq-mux/src/codec/eac3.rs @@ -44,6 +44,13 @@ pub(crate) fn parse_header(data: &[u8]) -> legacy::Result { Ok(header) } +/// Whether the sync frame at the start of `data` (needs >= 3 bytes) begins an access unit: +/// independent substream 0, which every substream after it until the next one extends +/// (ETSI TS 102 366 E.1.3.1.2). +pub(crate) fn begins_unit(data: &[u8]) -> bool { + data[2] >> 6 != 1 && (data[2] >> 3) & 0x07 == 0 +} + /// Parse a sync frame's bounds and duration without assigning its substream to an audio track. pub(crate) fn sync_header(data: &[u8]) -> legacy::Result { if data.len() < 6 { diff --git a/rs/moq-mux/src/container/ts/export.rs b/rs/moq-mux/src/container/ts/export.rs index 87c25c086f4..6ede0af5b4d 100644 --- a/rs/moq-mux/src/container/ts/export.rs +++ b/rs/moq-mux/src/container/ts/export.rs @@ -277,8 +277,8 @@ struct Track { timing: Timing, /// The receiver's buffers for an audio or verbatim track; video's come from [`Timing`]. buffer: Option, - /// The AC-3 sync frame a passed-through PES left unfinished, for the next PES to complete. - ac3_carry: Option, + /// What the last passed-through Dolby PES left for the next to complete. + dolby_carry: Option, /// The audio frames muxed into a PES that has yet to go out. pack: Option, /// The last frame's decode time, its jitter buffer key ([`Self::release_key`]) and the PES @@ -315,7 +315,7 @@ impl Track { keyframe: None, timing: Timing::default(), buffer: None, - ac3_carry: None, + dolby_carry: None, pack: None, released: None, } @@ -333,8 +333,8 @@ impl Track { self.restart = pending.restart; self.clock = DecodeClock::default(); } - if self.carries_ac3() || self.carries_eac3() { - return self.queue_dolby(name, pending, jitter); + if let Some(codec) = self.dolby() { + return self.queue_dolby(codec, name, pending, jitter); } let Kind::Video(stream_type) = self.kind else { return self.push(name, pending, None, None, jitter); @@ -422,6 +422,7 @@ impl Track { /// has to order it on its own time, not the PES's. fn queue_dolby( &mut self, + codec: Dolby, name: &str, pending: Pending, jitter: &mut jitter::Buffer, @@ -434,11 +435,7 @@ impl Track { arrived, read, } = pending; - let frames = if self.carries_eac3() { - Some(eac3_units(frame.timestamp, &frame.payload)?) - } else { - ac3_frames(&mut self.ac3_carry, skip, frame.timestamp, &frame.payload) - }; + let frames = dolby_units(codec, &mut self.dolby_carry, skip, frame.timestamp, &frame.payload)?; let Some(frames) = frames else { let pending = Pending { frame, @@ -471,35 +468,22 @@ impl Track { Ok(()) } - /// Whether the track passes AC-3 through as DVB private data, whose PES may carry several - /// sync frames. - fn carries_ac3(&self) -> bool { - matches!( - self.kind, - Kind::Verbatim { - stream_type: 0x06, - framing: catalog::Framing::Pes, - .. - } - ) && self - .descriptors - .iter() - .any(|descriptor| descriptor.tag == AC3_DESCRIPTOR) - } - - /// DVB E-AC-3 remains private data, with its descriptor identifying the decoder buffer. - fn carries_eac3(&self) -> bool { - matches!( - self.kind, - Kind::Verbatim { - stream_type: 0x06, - framing: catalog::Framing::Pes, - .. - } - ) && self - .descriptors - .iter() - .any(|descriptor| descriptor.tag == EAC3_DESCRIPTOR) + /// The Dolby audio the track passes through as DVB private data, whose PES may carry + /// several sync frames. + fn dolby(&self) -> Option { + let Kind::Verbatim { + stream_type: 0x06, + framing: catalog::Framing::Pes, + .. + } = self.kind + else { + return None; + }; + self.descriptors.iter().find_map(|descriptor| match descriptor.tag { + AC3_DESCRIPTOR => Some(Dolby::Ac3), + EAC3_DESCRIPTOR => Some(Dolby::Eac3), + _ => None, + }) } /// The samples an audio frame of `payload` lasts and their rate, if it may share a PES with @@ -511,7 +495,7 @@ impl Track { Kind::Mp2 { .. } => crate::codec::mp2::parse_header, Kind::Ac3 => crate::codec::ac3::parse_header, Kind::Eac3 => crate::codec::eac3::parse_header, - _ if self.carries_ac3() => crate::codec::ac3::parse_header, + _ if self.dolby() == Some(Dolby::Ac3) => crate::codec::ac3::parse_header, _ => return None, }; let header = parse(payload).ok()?; @@ -3091,122 +3075,146 @@ fn verbatim_buffer(kind: &Kind, descriptors: &[catalog::Descriptor]) -> Option anyhow::Result> { - let mut units = Vec::new(); - let (mut offset, mut begin) = (0, None); - let mut at = start; - let mut period = None; - while offset < payload.len() { - let rest = &payload[offset..]; - if rest.len() < 6 { - break; - } - let header = crate::codec::eac3::sync_header(rest)?; - if rest.len() < header.len { - break; +/// The Dolby audio a track passes through as DVB private data. +#[derive(Clone, Copy, PartialEq, Eq)] +enum Dolby { + Ac3, + Eac3, +} + +impl Dolby { + /// Bytes needed to attempt a header parse. + fn min_header_len(self) -> usize { + match self { + Self::Ac3 => crate::codec::ac3::DESCRIPTOR.min_header_len, + Self::Eac3 => crate::codec::eac3::DESCRIPTOR.min_header_len, } - let primary = rest[2] >> 6 != 1 && (rest[2] >> 3) & 7 == 0; - if primary { - if let Some(begin) = begin { - units.push((at, payload.slice(begin..offset))); - at = at.checked_add(period.unwrap())?; - } - begin = Some(offset); - period = Some(Timestamp::from_micros( - header.samples * 1_000_000 / u64::from(header.sample_rate), - )?); - } else if begin.is_some() { - let child = Timestamp::from_micros(header.samples * 1_000_000 / u64::from(header.sample_rate))?; - anyhow::ensure!( - Some(child) == period, - "E-AC-3 substreams have different presentation intervals" - ); + } + + /// Parse the header of the sync frame at the start of `data`, of any substream. + fn parse(self, data: &[u8]) -> crate::codec::legacy::Result { + match self { + Self::Ac3 => crate::codec::ac3::parse_header(data), + Self::Eac3 => crate::codec::eac3::sync_header(data), } - offset += header.len; } - if let Some(begin) = begin { - units.push((at, payload.slice(begin..offset))); + + /// Whether the sync frame at the start of `frame`, whose header parses, begins an access + /// unit rather than adding a substream to the one before it. + fn begins_unit(self, frame: &[u8]) -> bool { + match self { + Self::Ac3 => true, + Self::Eac3 => crate::codec::eac3::begins_unit(frame), + } } - Ok(units) } -/// An AC-3 sync frame a passed-through PES left unfinished. -struct Ac3Carry { +/// What a passed-through Dolby PES leaves for the next one. +struct DolbyCarry { /// The playhead count it was read under ([`Pending::skip`]). skip: u64, - /// When it is presented. - at: Timestamp, - /// Its bytes so far. + /// When an access unit that the next sync frame begins is presented. + next: Timestamp, + /// The E-AC-3 access unit still open, which a substream at the next PES's start extends. + unit: Option, + /// The bytes so far of a sync frame left unfinished. bytes: Vec, } -/// The AC-3 sync frames a PES's `payload` completes or holds, each with its presentation time, -/// or `None` if no sync frame can begin in it. +/// An access unit begun: when it is presented, and the blocks and rate its substreams share. +#[derive(Clone, Copy)] +struct DolbyUnit { + at: Timestamp, + samples: u64, + rate: u32, +} + +/// The Dolby access units a PES's `payload` completes or holds, each with its presentation +/// time, or `None` if no sync frame can begin in it. An AC-3 access unit is one sync frame; an +/// E-AC-3 one is independent substream 0 with every substream after it (ETSI TS 102 366 +/// E.1.3.1.2), which share its presentation interval. /// /// A PES need not begin on a sync frame: a source that leaves `data_alignment_indicator` /// clear splits frames, and even their headers, across PES. `carry` holds the frame the last /// PES left unfinished, which this payload's first bytes complete, keeping its own time. The -/// PES's PTS, `start`, times the first frame that begins in it (ISO 13818-1 2.4.3.7), the rest -/// count on from there, and a frame left unfinished at the end is carried into the next PES. +/// PES's PTS, `start`, times the first access unit that begins in it (ISO 13818-1 2.4.3.7), the +/// rest count on from there, and a frame left unfinished at the end is carried into the next PES. /// A carried frame is dropped when the playhead jumped since (`skip`: a skipped group, a /// marker, a resubscription), or when it would not end where the next frame begins, by that /// PTS and its sync word: either way it spans a gap in the source. So are bytes ahead of the /// first sync frame with nothing carried. No decoder can use them. -fn ac3_frames( - carry: &mut Option, +/// +/// An E-AC-3 substream whose access unit went out with the last PES goes out on its own, at +/// that unit's time. One whose access unit began before the join is dropped. +fn dolby_units( + codec: Dolby, + carry: &mut Option, skip: u64, start: Timestamp, payload: &Bytes, -) -> Option> { - let codec = &crate::codec::ac3::DESCRIPTOR; +) -> anyhow::Result>> { let duration = |samples: u64, rate: u32| Timestamp::from_micros(samples * 1_000_000 / u64::from(rate.max(1))); - let mut frames = Vec::new(); + let mut units = Vec::new(); + let mut open = None; let mut offset = None; match carry.take() { None => {} Some(head) if head.skip != skip => { - tracing::debug!("dropped an AC-3 sync frame cut by a jump in the playhead"); + if !head.bytes.is_empty() { + tracing::debug!("dropped a Dolby sync frame cut by a jump in the playhead"); + } + } + Some(head) if head.bytes.is_empty() => { + // A unit left open extends into this PES only if this PES's units follow it. + if let Some(unit) = head.unit { + let length = duration(unit.samples, unit.rate)?; + let end = unit.at.checked_add(length)?; + if end.as_micros().abs_diff(start.as_micros()) <= length.as_micros() / 2 { + open = Some(unit); + } else { + tracing::warn!(?end, ?start, "closed an E-AC-3 access unit at a gap between PES"); + } + } } Some(mut head) => { - let probe: Vec = head - .bytes - .iter() - .chain(payload.iter()) - .take(codec.min_header_len) - .copied() - .collect(); - if probe.len() < codec.min_header_len { + open = head.unit; + let min = codec.min_header_len(); + let probe: Vec = head.bytes.iter().chain(payload.iter()).take(min).copied().collect(); + if probe.len() < min { // Even the header runs on past this PES. head.bytes.extend_from_slice(payload); *carry = Some(head); - return Some(frames); + return Ok(Some(units)); } - match (codec.parse)(&probe) { + match codec.parse(&probe) { Ok(header) => { let need = header.len.saturating_sub(head.bytes.len()); let Some((tail, after)) = payload.split_at_checked(need) else { // The frame runs on past this PES as well. head.bytes.extend_from_slice(payload); *carry = Some(head); - return Some(frames); + return Ok(Some(units)); + }; + let at = match (codec.begins_unit(&probe), open) { + (false, Some(unit)) => unit.at, + _ => head.next, }; - let length = duration(header.samples, header.sample_rate).ok()?; - let end = head.at.checked_add(length).ok()?; + let length = duration(header.samples, header.sample_rate)?; + let end = at.checked_add(length)?; // Only a frame that begins here has a PTS to meet. let next = after.is_empty() || (end.as_micros().abs_diff(start.as_micros()) <= length.as_micros() / 2 && ac3_sync_prefix(after)); if next { head.bytes.extend_from_slice(tail); - frames.push((head.at, Bytes::from(head.bytes))); + let frame = Bytes::from(head.bytes); + dolby_place(codec, frame, &header, head.next, &mut open, &mut units)?; offset = Some(need); } else { - tracing::warn!(?end, ?start, "dropped an AC-3 sync frame cut by a gap between PES"); + tracing::warn!(?end, ?start, "dropped a Dolby sync frame cut by a gap between PES"); } } - Err(_) => tracing::warn!("dropped the start of an AC-3 sync frame whose header does not parse"), + Err(_) => tracing::warn!("dropped the start of a Dolby sync frame whose header does not parse"), } } } @@ -3214,69 +3222,110 @@ fn ac3_frames( Some(offset) => offset, None if payload.is_empty() => 0, None => { - let first = ac3_sync(payload)?; + let Some(first) = dolby_sync(codec, payload) else { + return Ok(None); + }; if first > 0 { tracing::debug!( bytes = first, - "dropped the end of an AC-3 sync frame begun before the join" + "dropped the end of a Dolby sync frame begun before the join" ); } first } }; let (mut samples, mut rate) = (0u64, 0u32); + let mut left = Vec::new(); while offset < payload.len() { let rest = &payload[offset..]; - let at = start.checked_add(duration(samples, rate).ok()?).ok()?; - let header = match (codec.parse)(rest) { + let next = start.checked_add(duration(samples, rate)?)?; + let header = match codec.parse(rest) { Ok(header) => header, // A header cut short at the end, which the next PES completes. - Err(_) if rest.len() < codec.min_header_len && ac3_sync_prefix(rest) => { - *carry = Some(Ac3Carry { - skip, - at, - bytes: rest.to_vec(), - }); + Err(_) if rest.len() < codec.min_header_len() && ac3_sync_prefix(rest) => { + left = rest.to_vec(); break; } Err(_) => { - tracing::warn!(bytes = rest.len(), "dropped the rest of a PES that lost AC-3 sync"); + tracing::warn!(bytes = rest.len(), "dropped the rest of a PES that lost Dolby sync"); break; } }; if rest.len() < header.len { - *carry = Some(Ac3Carry { - skip, - at, - bytes: rest.to_vec(), - }); + left = rest.to_vec(); break; } - frames.push((at, payload.slice(offset..offset + header.len))); - samples += header.samples; - rate = header.sample_rate; + let frame = payload.slice(offset..offset + header.len); + if dolby_place(codec, frame, &header, next, &mut open, &mut units)? { + samples += header.samples; + rate = header.sample_rate; + } offset += header.len; } - Some(frames) + *carry = Some(DolbyCarry { + skip, + next: start.checked_add(duration(samples, rate)?)?, + unit: open.filter(|_| codec == Dolby::Eac3), + bytes: left, + }); + Ok(Some(units)) +} + +/// File the whole sync frame `frame` in `units`: as a new access unit presented at `next`, or +/// as a substream of the `open` one, joining it when it went out from this PES too. Returns +/// whether it began a unit. +fn dolby_place( + codec: Dolby, + frame: Bytes, + header: &crate::codec::legacy::Header, + next: Timestamp, + open: &mut Option, + units: &mut Vec<(Timestamp, Bytes)>, +) -> anyhow::Result { + if codec.begins_unit(&frame) { + *open = Some(DolbyUnit { + at: next, + samples: header.samples, + rate: header.sample_rate, + }); + units.push((next, frame)); + return Ok(true); + } + let Some(unit) = *open else { + tracing::debug!("dropped an E-AC-3 substream whose access unit began before the join"); + return Ok(false); + }; + anyhow::ensure!( + header.samples == unit.samples && header.sample_rate == unit.rate, + "E-AC-3 substream of {} samples at {} Hz in an access unit of {} at {} Hz", + header.samples, + header.sample_rate, + unit.samples, + unit.rate, + ); + match units.last_mut() { + Some((at, bytes)) if *at == unit.at => *bytes = [bytes.as_ref(), frame.as_ref()].concat().into(), + _ => units.push((unit.at, frame)), + } + Ok(false) } -/// Where the first AC-3 sync frame in `payload` begins: a sync word whose header parses and +/// Where the first Dolby sync frame in `payload` begins: a sync word whose header parses and /// whose frame ends at the next sync word. Failing that, the first that may still begin one, /// for the next PES to confirm: a frame that runs to the payload's end or past it, or a /// header the payload cuts short. A sync word in the body of a frame begun before the join can /// pass for a header, so it is taken only when nothing confirmed follows. -fn ac3_sync(payload: &[u8]) -> Option { - let codec = &crate::codec::ac3::DESCRIPTOR; +fn dolby_sync(codec: Dolby, payload: &[u8]) -> Option { let mut fallback = None; - for (i, _) in payload.iter().enumerate().filter(|&(_, &byte)| byte == codec.sync_byte) { + for (i, _) in payload.iter().enumerate().filter(|&(_, &byte)| byte == 0x0b) { let rest = &payload[i..]; - match (codec.parse)(rest) { + match codec.parse(rest) { Ok(header) => match rest.get(header.len..) { Some([0x0b, 0x77, ..]) => return Some(i), Some(next) if !ac3_sync_prefix(next) => continue, _ => {} }, - Err(_) if rest.len() < codec.min_header_len && ac3_sync_prefix(rest) => {} + Err(_) if rest.len() < codec.min_header_len() && ac3_sync_prefix(rest) => {} Err(_) => continue, } fallback.get_or_insert(i); @@ -3284,7 +3333,7 @@ fn ac3_sync(payload: &[u8]) -> Option { fallback } -/// Whether `bytes` could begin an AC-3 sync word, for all they show of it. +/// Whether `bytes` could begin a Dolby sync word, for all they show of it. fn ac3_sync_prefix(bytes: &[u8]) -> bool { bytes.iter().zip([0x0b, 0x77]).all(|(&byte, sync)| byte == sync) } @@ -3580,6 +3629,51 @@ mod tests { assert_eq!(decode, [us(0), us(32), us(64)]); } + /// A six-block E-AC-3 sync frame of 512 bytes: `stream` is byte 2's top bits (strmtyp, + /// substreamid) and `rate` byte 4 (fscod, numblkscod, acmod, lfeon). + fn eac3(stream: u8, rate: u8) -> Vec { + let mut frame = vec![0x0b, 0x77, stream, 0xff, rate, 0x80]; + frame.resize(512, 0xaa); + frame + } + + /// Access units count on from the PES's PTS by the samples before them, so a 44.1 kHz + /// frame's 34,829.9 us does not round down once per frame. + #[test] + fn dolby_units_count_samples_not_rounded_periods() { + let payload = bytes::Bytes::from(eac3(0x00, 0x7f).repeat(30)); + let units = super::dolby_units( + super::Dolby::Eac3, + &mut None, + 0, + Timestamp::from_micros(0).unwrap(), + &payload, + ) + .unwrap() + .unwrap(); + assert_eq!(units.len(), 30); + for (k, (at, _)) in units.iter().enumerate() { + assert_eq!(at.as_micros(), k as u128 * 1_536 * 1_000_000 / 44_100, "unit {k}"); + } + } + + /// Substreams of one access unit share its blocks and rate (ETSI TS 102 366 E.1.3.1.2), so + /// one that differs is malformed rather than timed by a guess. + #[test] + fn dolby_units_refuse_a_substream_off_its_unit_interval() { + // A three-block dependent substream after a six-block independent one. + let payload = bytes::Bytes::from([eac3(0x00, 0x3f), eac3(0x40, 0x2f)].concat()); + let err = super::dolby_units( + super::Dolby::Eac3, + &mut None, + 0, + Timestamp::from_micros(0).unwrap(), + &payload, + ) + .unwrap_err(); + assert!(err.to_string().contains("E-AC-3 substream"), "{err}"); + } + /// Teletext passed through as DVB private data takes its transport buffer's rate from /// EN 300 472, so the schedule spreads it no faster than a receiver passes it on; private /// data nothing identifies has no buffers. diff --git a/rs/moq-mux/src/container/ts/export_test.rs b/rs/moq-mux/src/container/ts/export_test.rs index ec598c6074a..00e88fbef20 100644 --- a/rs/moq-mux/src/container/ts/export_test.rs +++ b/rs/moq-mux/src/container/ts/export_test.rs @@ -1684,6 +1684,71 @@ async fn export_drops_dvb_eac3_partial_frames() { assert_eq!(pes[0].1, frame); } +/// A source that leaves `data_alignment_indicator` clear splits E-AC-3 sync frames across PES, +/// as it does AC-3's, and each still goes out whole, a frame after the last. +#[tokio::test(start_paused = true)] +async fn export_reassembles_dvb_eac3_frames_split_across_pes() { + let frame = eac3_frame(false); + let stream = frame.repeat(6); + // Cut mid-frame into frames 0-1, 1-3 and 3-5, timed by frames 0, 2 and 4. + let pes = exported_dolby_pes( + vec![ + Ac3Write::Pes(0, stream[..800].to_vec()), + Ac3Write::Pes(64, stream[800..2000].to_vec()), + Ac3Write::Pes(128, stream[2000..].to_vec()), + ], + 0x7a, + ) + .await; + assert_eq!(pes.len(), 6, "every sync frame"); + for (k, (pts, payload)) in pes.iter().enumerate() { + assert_eq!(*payload, frame, "frame {k} whole"); + assert_eq!(*pts, pes[0].0 + k as u64 * 2_880, "frame {k} 32 ms after the last"); + } +} + +/// A dependent substream the next PES carries, whole or completing, goes out after its access +/// unit's independent substream at that unit's time, not the next unit's. +#[tokio::test(start_paused = true)] +async fn export_times_a_dvb_eac3_substream_by_its_access_unit_across_pes() { + let (independent, dependent) = (eac3_frame(false), eac3_frame(true)); + let unit = [independent.clone(), dependent.clone()].concat(); + let stream = unit.repeat(3); + // Cut between unit 0's substreams, then inside its dependent substream. + for cut in [512, 800] { + let pes = exported_dolby_pes( + vec![ + Ac3Write::Pes(0, stream[..cut].to_vec()), + Ac3Write::Pes(32, stream[cut..].to_vec()), + ], + 0x7a, + ) + .await; + let start = pes[0].0; + assert_eq!( + pes, + [ + (start, independent.clone()), + (start, dependent.clone()), + (start + 2_880, unit.clone()), + (start + 5_760, unit.clone()), + ], + "cut at {cut}" + ); + } +} + +/// A dependent substream ahead of the first independent one belongs to an access unit that +/// began before the join, so it goes nowhere. +#[tokio::test(start_paused = true)] +async fn export_drops_dvb_eac3_substreams_before_the_join() { + let unit = [eac3_frame(false), eac3_frame(true)].concat(); + let payload = [eac3_frame(true), unit.clone()].concat(); + let pes = exported_dolby_pes(vec![Ac3Write::Pes(0, payload)], 0x7a).await; + assert_eq!(pes.len(), 1); + assert_eq!(pes[0].1, unit); +} + /// A 32 kb/s 48 kHz 2/0 AC-3 sync frame: 128 bytes, 1,536 samples (32 ms). fn ac3_sync() -> Vec { let mut sync = vec![0x0b, 0x77, 0x00, 0x00, 0x00, 0x40, 0x40]; From 7b8154985cd4af1b7b6e919fd1f178c00c63533b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 17 Oct 2026 07:57:34 -0700 Subject: [PATCH 04/10] fix(ts): time placed DVB E-AC-3 PES removals by access unit Placed whole-PES tracks model DVB AC-3 sync frames leaving the decoder buffer one by one. Extend that to E-AC-3 access units, so a substream leaves with its unit rather than the whole PES at its PTS. Co-Authored-By: Claude Opus 5.5 --- rs/moq-mux/src/container/ts/export.rs | 87 ++++++++++++++++++--------- 1 file changed, 60 insertions(+), 27 deletions(-) diff --git a/rs/moq-mux/src/container/ts/export.rs b/rs/moq-mux/src/container/ts/export.rs index da223aadd8c..19383e30a88 100644 --- a/rs/moq-mux/src/container/ts/export.rs +++ b/rs/moq-mux/src/container/ts/export.rs @@ -482,11 +482,7 @@ impl Track { else { return None; }; - self.descriptors.iter().find_map(|descriptor| match descriptor.tag { - AC3_DESCRIPTOR => Some(Dolby::Ac3), - EAC3_DESCRIPTOR => Some(Dolby::Eac3), - _ => None, - }) + Dolby::described(&self.descriptors) } /// The samples an audio frame of `payload` lasts and their rate, if it may share a PES with @@ -2339,8 +2335,8 @@ where /// first byte going out at the frame's timestamp, the source's placement, and no earlier. /// /// A PID the T-STD models holds the PES's bytes in its decoder buffer until they decode, - /// timed by the PTS in its header. For DVB AC-3 that times only the first sync frame - /// that begins in it, so each one after counts on from there and leaves at its own time. + /// timed by the PTS in its header. For DVB Dolby audio that times only the first access + /// unit that begins in it, so each one after counts on from there and leaves at its own time. /// The bytes ahead of the first end a frame an earlier PES began; they leave with it, /// which only overstates what the buffer holds. A PES without a PTS holds nothing there. fn place(&mut self, name: &str, stream_type: u8, frame: &Frame, tables: Vec) -> anyhow::Result<()> { @@ -2350,9 +2346,9 @@ where let pid = track.pid; let place = frame.timestamp.as_nanos(); let decode = pes.pts.map(|pts| unwrap_pts(pts, frame.timestamp)); - let ac3 = stream_type == 0x06 && track.descriptors.iter().any(|d| d.tag == AC3_DESCRIPTOR); - let frames = match decode { - Some(decode) if ac3 => ac3_removals(&frame.payload[pes.payload..], decode), + let dolby = Dolby::described(&track.descriptors).filter(|_| stream_type == 0x06); + let frames = match (decode, dolby) { + (Some(decode), Some(codec)) => dolby_removals(codec, &frame.payload[pes.payload..], decode), _ => Vec::new(), }; let buffer = track.buffer(); @@ -3013,29 +3009,36 @@ fn unwrap_pts(pts: u64, at: Timestamp) -> u128 { (ticks.max(0) as u128) * 1_000_000_000 / 90_000 } -/// When each AC-3 sync frame that begins in a PES's `payload` leaves the decoder buffer and +/// When each Dolby access unit that begins in a PES's `payload` leaves the decoder buffer and /// the bytes it takes, counting on from `decode`, the PES's PTS: [`Schedule::push_placed`]'s -/// frames. The bytes ahead of the first frame go with it, and a frame the PES leaves -/// unfinished keeps its time for the bytes it holds. Empty with fewer than two frames, +/// frames. The bytes ahead of the first unit go with it, and a frame the PES leaves +/// unfinished keeps its unit's time for the bytes it holds. Empty with fewer than two units, /// where the whole PES leaves at once. -fn ac3_removals(payload: &[u8], decode: u128) -> Vec<(u128, usize)> { - let codec = &crate::codec::ac3::DESCRIPTOR; - let Some(first) = ac3_sync(payload) else { +fn dolby_removals(codec: Dolby, payload: &[u8], decode: u128) -> Vec<(u128, usize)> { + let Some(mut offset) = dolby_sync(codec, payload) else { return Vec::new(); }; let mut frames: Vec<(u128, usize)> = Vec::new(); - let (mut at, mut offset) = (decode, first); + let (mut at, mut lead) = (decode, offset); while offset < payload.len() { - let Ok(header) = (codec.parse)(&payload[offset..]) else { + let rest = &payload[offset..]; + let Ok(header) = codec.parse(rest) else { break; }; - let len = header.len.min(payload.len() - offset); - frames.push((at, len)); - at += 1_000_000_000 * u128::from(header.samples) / u128::from(header.sample_rate.max(1)); + let len = header.len.min(rest.len()); + match frames.last_mut() { + // A substream leaves with its access unit, or the first when that began earlier. + Some((_, bytes)) if !codec.begins_unit(rest) => *bytes += len, + None if !codec.begins_unit(rest) => lead += len, + _ => { + frames.push((at, len)); + at += 1_000_000_000 * u128::from(header.samples) / u128::from(header.sample_rate.max(1)); + } + } offset += len.max(1); } if let Some(head) = frames.first_mut() { - head.1 += first; + head.1 += lead; } if let Some(tail) = frames.last_mut() { tail.1 += payload.len().saturating_sub(offset); @@ -3351,6 +3354,15 @@ enum Dolby { } impl Dolby { + /// The Dolby audio a private-data stream's descriptors identify. + fn described(descriptors: &[catalog::Descriptor]) -> Option { + descriptors.iter().find_map(|descriptor| match descriptor.tag { + AC3_DESCRIPTOR => Some(Self::Ac3), + EAC3_DESCRIPTOR => Some(Self::Eac3), + _ => None, + }) + } + /// Bytes needed to attempt a header parse. fn min_header_len(self) -> usize { match self { @@ -3854,8 +3866,8 @@ mod tests { use moq_net::Timestamp; use super::{ - Buffer, DecodeClock, Kind, PSI_INTERVAL, PesPacket, ac3_removals, due, is_complete_section, si_due, slot, - unwrap_pts, verbatim_buffer, + Buffer, DecodeClock, Dolby, Kind, PSI_INTERVAL, PesPacket, dolby_removals, due, is_complete_section, si_due, + slot, unwrap_pts, verbatim_buffer, }; fn ms(value: u64) -> Timestamp { @@ -4352,19 +4364,40 @@ mod tests { } #[test] - fn ac3_removals_count_on_from_the_pts() { + fn dolby_removals_count_on_from_the_pts() { // 32 kb/s 48 kHz sync frames: 128 bytes, 32 ms each. let mut sync = vec![0x0b, 0x77, 0x00, 0x00, 0x00, 0x40, 0x40]; sync.resize(128, 0); let mut payload = vec![0xee; 10]; payload.extend(sync.repeat(2)); payload.extend_from_slice(&sync[..50]); - let removals = ac3_removals(&payload, 1_000_000_000); + let removals = dolby_removals(Dolby::Ac3, &payload, 1_000_000_000); // The tail of an earlier frame goes with the first, and the cut frame keeps its time. assert_eq!( removals, vec![(1_000_000_000, 138), (1_032_000_000, 128), (1_064_000_000, 50)] ); - assert!(ac3_removals(&[0xee; 20], 0).is_empty(), "no sync frame begins"); + assert!( + dolby_removals(Dolby::Ac3, &[0xee; 20], 0).is_empty(), + "no sync frame begins" + ); + } + + /// E-AC-3 substreams leave with their access unit, so a PES's units leave a 32 ms interval + /// apart whatever substreams each carries. + #[test] + fn dolby_removals_keep_eac3_substreams_with_their_unit() { + let (independent, dependent) = (eac3(0x00, 0x3f), eac3(0x40, 0x3f)); + let payload = [ + &dependent[..], + &independent, + &dependent, + &independent, + &dependent[..100], + ] + .concat(); + let removals = dolby_removals(Dolby::Eac3, &payload, 1_000_000_000); + // The substream ending an earlier unit goes with the first, the cut one with the last. + assert_eq!(removals, vec![(1_000_000_000, 1_536), (1_032_000_000, 612)]); } } From 9f7635a7e7e5ee447fbd8bcd3ac81e7140f39607 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 17 Oct 2026 07:59:30 -0700 Subject: [PATCH 05/10] fix(ts): close an E-AC-3 unit when its carried substream is dropped Co-Authored-By: Claude Opus 5.5 --- rs/moq-mux/src/container/ts/export.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rs/moq-mux/src/container/ts/export.rs b/rs/moq-mux/src/container/ts/export.rs index 19383e30a88..cae21f02717 100644 --- a/rs/moq-mux/src/container/ts/export.rs +++ b/rs/moq-mux/src/container/ts/export.rs @@ -3457,7 +3457,6 @@ fn dolby_units( } } Some(mut head) => { - open = head.unit; let min = codec.min_header_len(); let probe: Vec = head.bytes.iter().chain(payload.iter()).take(min).copied().collect(); if probe.len() < min { @@ -3475,7 +3474,7 @@ fn dolby_units( *carry = Some(head); return Ok(Some(units)); }; - let at = match (codec.begins_unit(&probe), open) { + let at = match (codec.begins_unit(&probe), head.unit) { (false, Some(unit)) => unit.at, _ => head.next, }; @@ -3488,6 +3487,8 @@ fn dolby_units( if next { head.bytes.extend_from_slice(tail); let frame = Bytes::from(head.bytes); + // A unit stays open only through a carried frame that completes without a gap. + open = head.unit; dolby_place(codec, frame, &header, head.next, &mut open, &mut units)?; offset = Some(need); } else { From 3ce7e16f83340e93d2670f4b476868fc7468213e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 17 Oct 2026 08:17:25 -0700 Subject: [PATCH 06/10] fix(ts): split E-AC-3 at an AC-3 core, and grade every substream with its unit An E-AC-3 stream may carry AC-3 as independent substream 0; the export now parses it and begins an access unit there instead of losing sync. The compliance model gives any substream after independent substream 0 no duration of its own, so additional independent substreams share their unit's interval as the export schedules them. Co-Authored-By: Claude Opus 5.5 --- rs/moq-mux/src/container/ts/export.rs | 40 ++++++++++++++++++++++----- test/justfile | 1 + test/ts/compliance.py | 7 +++-- test/ts/compliance.test.py | 23 +++++++++++++++ 4 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 test/ts/compliance.test.py diff --git a/rs/moq-mux/src/container/ts/export.rs b/rs/moq-mux/src/container/ts/export.rs index cae21f02717..98c6e34114a 100644 --- a/rs/moq-mux/src/container/ts/export.rs +++ b/rs/moq-mux/src/container/ts/export.rs @@ -3363,19 +3363,20 @@ impl Dolby { }) } - /// Bytes needed to attempt a header parse. + /// Bytes needed to attempt a header parse: an E-AC-3 stream's AC-3 core has the longer one. fn min_header_len(self) -> usize { + let ac3 = crate::codec::ac3::DESCRIPTOR.min_header_len; match self { - Self::Ac3 => crate::codec::ac3::DESCRIPTOR.min_header_len, - Self::Eac3 => crate::codec::eac3::DESCRIPTOR.min_header_len, + Self::Ac3 => ac3, + Self::Eac3 => ac3.max(crate::codec::eac3::DESCRIPTOR.min_header_len), } } /// Parse the header of the sync frame at the start of `data`, of any substream. fn parse(self, data: &[u8]) -> crate::codec::legacy::Result { match self { - Self::Ac3 => crate::codec::ac3::parse_header(data), - Self::Eac3 => crate::codec::eac3::sync_header(data), + Self::Eac3 if !ac3_core(data) => crate::codec::eac3::sync_header(data), + _ => crate::codec::ac3::parse_header(data), } } @@ -3383,12 +3384,18 @@ impl Dolby { /// unit rather than adding a substream to the one before it. fn begins_unit(self, frame: &[u8]) -> bool { match self { - Self::Ac3 => true, - Self::Eac3 => crate::codec::eac3::begins_unit(frame), + Self::Eac3 if !ac3_core(frame) => crate::codec::eac3::begins_unit(frame), + _ => true, } } } +/// Whether the sync frame at the start of `data` is AC-3 (bsid 10 or less), which an E-AC-3 +/// stream may carry as its independent substream 0 (ETSI TS 102 366 E.1.3.1.2). +fn ac3_core(data: &[u8]) -> bool { + data.get(5).is_some_and(|byte| byte >> 3 <= 10) +} + /// What a passed-through Dolby PES leaves for the next one. struct DolbyCarry { /// The playhead count it was read under ([`Pending::skip`]). @@ -3958,6 +3965,25 @@ mod tests { } } + /// An E-AC-3 stream may carry AC-3 as its independent substream 0, which begins each access + /// unit that the E-AC-3 substreams after it extend. + #[test] + fn dolby_units_begin_at_an_ac3_core() { + // 32 kb/s 48 kHz AC-3: 128 bytes, 1,536 samples, as six E-AC-3 blocks are. + let mut core = vec![0x0b, 0x77, 0x00, 0x00, 0x00, 0x40, 0x40]; + core.resize(128, 0); + let unit = [core, eac3(0x40, 0x3f)].concat(); + let payload = bytes::Bytes::from(unit.repeat(2)); + let units = super::dolby_units(Dolby::Eac3, &mut None, 0, Timestamp::from_micros(0).unwrap(), &payload) + .unwrap() + .unwrap(); + let at: Vec<_> = units + .iter() + .map(|(at, bytes)| (at.as_micros(), bytes.to_vec())) + .collect(); + assert_eq!(at, [(0, unit.clone()), (32_000, unit)]); + } + /// Substreams of one access unit share its blocks and rate (ETSI TS 102 366 E.1.3.1.2), so /// one that differs is malformed rather than timed by a guess. #[test] diff --git a/test/justfile b/test/justfile index 236da7ca368..4c972c5e43c 100644 --- a/test/justfile +++ b/test/justfile @@ -132,6 +132,7 @@ drill-sensitivity *args: [positional-arguments] ts *args: python3 ./ts/fragment_pes.test.py + python3 ./ts/compliance.test.py python3 ./ts/pcr-timing.test.py python3 ./ts/open-gop.test.py python3 ./ts/holes.test.py diff --git a/test/ts/compliance.py b/test/ts/compliance.py index 2bdcebf82c3..848f08cabb8 100755 --- a/test/ts/compliance.py +++ b/test/ts/compliance.py @@ -766,7 +766,8 @@ def mpeg_audio_frame(es: bytes, pos: int) -> tuple[int, float]: def ac3_frame(es: bytes, pos: int) -> tuple[int, float]: - """(length, duration) of the AC-3 or E-AC-3 frame at `pos`; 0 duration for a dependent substream.""" + """(length, duration) of the AC-3 or E-AC-3 frame at `pos`; 0 duration for any substream after + independent substream 0, which shares its access unit's interval (ETSI TS 102 366 E.1.3.1.2).""" h = es[pos : pos + 6] if len(h) < 6 or h[0] != 0x0B or h[1] != 0x77: raise Refused(f"no AC-3 sync at ES offset {pos}") @@ -774,8 +775,8 @@ def ac3_frame(es: bytes, pos: int) -> tuple[int, float]: blocks = (1, 2, 3, 6)[(h[4] >> 4) & 0x03] if h[4] >> 6 != 3 else 6 fscod = h[4] >> 6 rate = (48000, 44100, 32000)[fscod] if fscod != 3 else (24000, 22050, 16000)[(h[4] >> 4) & 0x03] - dependent = h[2] >> 6 == 1 - return ((((h[2] & 0x07) << 8) | h[3]) + 1) * 2, 0.0 if dependent else 256 * blocks / rate + extends = h[2] >> 6 == 1 or (h[2] >> 3) & 0x07 != 0 + return ((((h[2] & 0x07) << 8) | h[3]) + 1) * 2, 0.0 if extends else 256 * blocks / rate fscod, code = h[4] >> 6, h[4] & 0x3F if fscod == 3 or code >> 1 >= len(AC3_KBPS): raise Refused(f"AC-3 fscod/frmsizecod reserved at ES offset {pos}") diff --git a/test/ts/compliance.test.py b/test/ts/compliance.test.py new file mode 100644 index 00000000000..7b4769415c4 --- /dev/null +++ b/test/ts/compliance.test.py @@ -0,0 +1,23 @@ +import pathlib +import sys +import unittest + +sys.path.insert(0, str(pathlib.Path(__file__).parent)) +import compliance # noqa: E402 + + +def eac3(stream: int) -> bytes: + """A six-block 48 kHz E-AC-3 sync frame of 512 bytes; `stream` is byte 2's top bits.""" + return bytes([0x0B, 0x77, stream, 0xFF, 0x3F, 0x80]) + b"\xaa" * 506 + + +class Eac3Frames(unittest.TestCase): + def test_only_independent_substream_zero_advances_time(self): + # Independent substream 0, a dependent substream, then independent substream 1. + for stream, duration in ((0x00, 0.032), (0x40, 0.0), (0x08, 0.0)): + with self.subTest(stream=stream): + self.assertEqual(compliance.ac3_frame(eac3(stream), 0), (512, duration)) + + +if __name__ == "__main__": + unittest.main() From e2afd110f556573da0b59d087c05869a2478b80d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 17 Oct 2026 10:07:10 -0700 Subject: [PATCH 07/10] fix(ts): drop a Dolby PES with no sync frame, and refuse a reserved E-AC-3 rate A passed-through Dolby PES in which no sync frame can begin went out whole; no decoder can use it, so it is dropped like the bytes ahead of a first sync frame. The compliance model refuses an E-AC-3 frame with the reserved fscod2 instead of mistaking it for the end of the capture. Co-Authored-By: Claude Opus 5.5 --- rs/moq-mux/src/container/ts/export.rs | 31 +++++++++------------- rs/moq-mux/src/container/ts/export_test.rs | 18 +++++++++++++ test/ts/compliance.py | 2 ++ test/ts/compliance.test.py | 6 +++++ 4 files changed, 38 insertions(+), 19 deletions(-) diff --git a/rs/moq-mux/src/container/ts/export.rs b/rs/moq-mux/src/container/ts/export.rs index 98c6e34114a..f8a771520a5 100644 --- a/rs/moq-mux/src/container/ts/export.rs +++ b/rs/moq-mux/src/container/ts/export.rs @@ -439,17 +439,6 @@ impl Track { read, } = pending; let frames = dolby_units(codec, &mut self.dolby_carry, skip, frame.timestamp, &frame.payload)?; - let Some(frames) = frames else { - let pending = Pending { - frame, - restart, - skip, - cut, - arrived, - read, - }; - return self.push(name, pending, None, None, jitter); - }; for (timestamp, payload) in frames { let frame = Frame { timestamp, @@ -3417,7 +3406,7 @@ struct DolbyUnit { } /// The Dolby access units a PES's `payload` completes or holds, each with its presentation -/// time, or `None` if no sync frame can begin in it. An AC-3 access unit is one sync frame; an +/// time; none when no sync frame can begin in it. An AC-3 access unit is one sync frame; an /// E-AC-3 one is independent substream 0 with every substream after it (ETSI TS 102 366 /// E.1.3.1.2), which share its presentation interval. /// @@ -3439,8 +3428,9 @@ fn dolby_units( skip: u64, start: Timestamp, payload: &Bytes, -) -> anyhow::Result>> { +) -> anyhow::Result> { let duration = |samples: u64, rate: u32| Timestamp::from_micros(samples * 1_000_000 / u64::from(rate.max(1))); + let joined = carry.as_ref().is_none_or(|head| head.skip != skip); let mut units = Vec::new(); let mut open = None; let mut offset = None; @@ -3470,7 +3460,7 @@ fn dolby_units( // Even the header runs on past this PES. head.bytes.extend_from_slice(payload); *carry = Some(head); - return Ok(Some(units)); + return Ok(units); } match codec.parse(&probe) { Ok(header) => { @@ -3479,7 +3469,7 @@ fn dolby_units( // The frame runs on past this PES as well. head.bytes.extend_from_slice(payload); *carry = Some(head); - return Ok(Some(units)); + return Ok(units); }; let at = match (codec.begins_unit(&probe), head.unit) { (false, Some(unit)) => unit.at, @@ -3511,7 +3501,12 @@ fn dolby_units( None if payload.is_empty() => 0, None => { let Some(first) = dolby_sync(codec, payload) else { - return Ok(None); + // No decoder can use a PES without a sync frame either, so it goes nowhere. + match joined { + true => tracing::debug!(bytes = payload.len(), "dropped a PES begun before the join"), + false => tracing::warn!(bytes = payload.len(), "dropped a PES with no Dolby sync frame"), + } + return Ok(units); }; if first > 0 { tracing::debug!( @@ -3556,7 +3551,7 @@ fn dolby_units( unit: open.filter(|_| codec == Dolby::Eac3), bytes: left, }); - Ok(Some(units)) + Ok(units) } /// File the whole sync frame `frame` in `units`: as a new access unit presented at `next`, or @@ -3957,7 +3952,6 @@ mod tests { Timestamp::from_micros(0).unwrap(), &payload, ) - .unwrap() .unwrap(); assert_eq!(units.len(), 30); for (k, (at, _)) in units.iter().enumerate() { @@ -3975,7 +3969,6 @@ mod tests { let unit = [core, eac3(0x40, 0x3f)].concat(); let payload = bytes::Bytes::from(unit.repeat(2)); let units = super::dolby_units(Dolby::Eac3, &mut None, 0, Timestamp::from_micros(0).unwrap(), &payload) - .unwrap() .unwrap(); let at: Vec<_> = units .iter() diff --git a/rs/moq-mux/src/container/ts/export_test.rs b/rs/moq-mux/src/container/ts/export_test.rs index ca492c6603b..02649a50e28 100644 --- a/rs/moq-mux/src/container/ts/export_test.rs +++ b/rs/moq-mux/src/container/ts/export_test.rs @@ -1738,6 +1738,24 @@ async fn export_times_a_dvb_eac3_substream_by_its_access_unit_across_pes() { } } +/// A PES without a sync frame holds nothing a decoder can use, so it goes nowhere rather +/// than out whole. +#[tokio::test(start_paused = true)] +async fn export_drops_a_dvb_eac3_pes_with_no_sync_frame() { + let frame = eac3_frame(false); + let pes = exported_dolby_pes( + vec![ + Ac3Write::Pes(0, frame.clone()), + Ac3Write::Pes(32, vec![0xee; 300]), + Ac3Write::Pes(64, frame.clone()), + ], + 0x7a, + ) + .await; + assert_eq!(pes.len(), 2, "{pes:?}"); + assert!(pes.iter().all(|(_, payload)| *payload == frame)); +} + /// A dependent substream ahead of the first independent one belongs to an access unit that /// began before the join, so it goes nowhere. #[tokio::test(start_paused = true)] diff --git a/test/ts/compliance.py b/test/ts/compliance.py index 848f08cabb8..2c50929eca0 100755 --- a/test/ts/compliance.py +++ b/test/ts/compliance.py @@ -772,6 +772,8 @@ def ac3_frame(es: bytes, pos: int) -> tuple[int, float]: if len(h) < 6 or h[0] != 0x0B or h[1] != 0x77: raise Refused(f"no AC-3 sync at ES offset {pos}") if h[5] >> 3 > 10: # bsid 11-16: E-AC-3 (A/52 Annex E) + if h[4] >> 6 == 3 and (h[4] >> 4) & 0x03 == 3: + raise Refused(f"E-AC-3 fscod2 reserved at ES offset {pos}") blocks = (1, 2, 3, 6)[(h[4] >> 4) & 0x03] if h[4] >> 6 != 3 else 6 fscod = h[4] >> 6 rate = (48000, 44100, 32000)[fscod] if fscod != 3 else (24000, 22050, 16000)[(h[4] >> 4) & 0x03] diff --git a/test/ts/compliance.test.py b/test/ts/compliance.test.py index 7b4769415c4..c12e1ac38a9 100644 --- a/test/ts/compliance.test.py +++ b/test/ts/compliance.test.py @@ -18,6 +18,12 @@ def test_only_independent_substream_zero_advances_time(self): with self.subTest(stream=stream): self.assertEqual(compliance.ac3_frame(eac3(stream), 0), (512, duration)) + def test_reserved_reduced_rate_is_refused(self): + frame = bytearray(eac3(0x00)) + frame[4] = 0xF0 # fscod 3 with fscod2 3 + with self.assertRaises(compliance.Refused): + compliance.ac3_frame(bytes(frame), 0) + if __name__ == "__main__": unittest.main() From 80d014d8e9cdda5c4580e4aad63ac50186a0a4d2 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 17 Oct 2026 10:31:30 -0700 Subject: [PATCH 08/10] fix(ts): keep an E-AC-3 unit open for a substream PES timed by it A PES of only a dependent substream may carry its access unit's PTS, as the export's own output does. Keep that unit open for it, and time the next unit from the unit's end, rather than closing it as a gap and dropping the substream. Co-Authored-By: Claude Opus 5.5 --- rs/moq-mux/src/container/ts/export.rs | 17 +++++++++++++---- rs/moq-mux/src/container/ts/export_test.rs | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/rs/moq-mux/src/container/ts/export.rs b/rs/moq-mux/src/container/ts/export.rs index 8664cf925b2..c3892e71c46 100644 --- a/rs/moq-mux/src/container/ts/export.rs +++ b/rs/moq-mux/src/container/ts/export.rs @@ -3409,7 +3409,8 @@ struct DolbyUnit { /// first sync frame with nothing carried. No decoder can use them. /// /// An E-AC-3 substream whose access unit went out with the last PES goes out on its own, at -/// that unit's time. One whose access unit began before the join is dropped. +/// that unit's time, whether its PES is timed by that unit or the next. One whose access unit +/// began before the join is dropped. fn dolby_units( codec: Dolby, carry: &mut Option, @@ -3422,6 +3423,8 @@ fn dolby_units( let mut units = Vec::new(); let mut open = None; let mut offset = None; + // When the next access unit to begin in this PES is presented. + let mut base = start; match carry.take() { None => {} Some(head) if head.skip != skip => { @@ -3434,8 +3437,14 @@ fn dolby_units( if let Some(unit) = head.unit { let length = duration(unit.samples, unit.rate)?; let end = unit.at.checked_add(length)?; - if end.as_micros().abs_diff(start.as_micros()) <= length.as_micros() / 2 { + let near = |at: Timestamp| at.as_micros().abs_diff(start.as_micros()) <= length.as_micros() / 2; + if near(end) { open = Some(unit); + } else if near(unit.at) { + // Timed by the unit it extends, as a PES of only its substreams is: the + // next unit still begins where that one ends. + open = Some(unit); + base = end; } else { tracing::warn!(?end, ?start, "closed an E-AC-3 access unit at a gap between PES"); } @@ -3509,7 +3518,7 @@ fn dolby_units( let mut left = Vec::new(); while offset < payload.len() { let rest = &payload[offset..]; - let next = start.checked_add(duration(samples, rate)?)?; + let next = base.checked_add(duration(samples, rate)?)?; let header = match codec.parse(rest) { Ok(header) => header, // A header cut short at the end, which the next PES completes. @@ -3535,7 +3544,7 @@ fn dolby_units( } *carry = Some(DolbyCarry { skip, - next: start.checked_add(duration(samples, rate)?)?, + next: base.checked_add(duration(samples, rate)?)?, unit: open.filter(|_| codec == Dolby::Eac3), bytes: left, }); diff --git a/rs/moq-mux/src/container/ts/export_test.rs b/rs/moq-mux/src/container/ts/export_test.rs index 0c7bd9312d2..ac860b04633 100644 --- a/rs/moq-mux/src/container/ts/export_test.rs +++ b/rs/moq-mux/src/container/ts/export_test.rs @@ -1748,6 +1748,24 @@ async fn export_times_a_dvb_eac3_substream_by_its_access_unit_across_pes() { } } +/// A PES of only a dependent substream, timed by the access unit it extends as the export's +/// own output is, still joins that unit, and the next unit keeps its own time. +#[tokio::test(start_paused = true)] +async fn export_keeps_a_dvb_eac3_substream_pes_timed_by_its_unit() { + let (independent, dependent) = (eac3_frame(false), eac3_frame(true)); + let unit = [independent.clone(), dependent.clone()].concat(); + let (pes, _) = exported_dolby_pes( + vec![ + Ac3Write::Pes(0, independent.clone()), + Ac3Write::Pes(0, [dependent.clone(), unit.clone()].concat()), + ], + 0x7a, + ) + .await; + let start = pes[0].0; + assert_eq!(pes, [(start, independent), (start, dependent), (start + 2_880, unit)]); +} + /// A PES without a sync frame holds nothing a decoder can use, so it goes nowhere rather /// than out whole. #[tokio::test(start_paused = true)] From 80e8d1e3174e318603e9586e5375dc9cb37fa374 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 17 Oct 2026 10:41:20 -0700 Subject: [PATCH 09/10] fix(ts): refuse a reserved bsid in the compliance model Co-Authored-By: Claude Opus 5.5 --- test/ts/compliance.py | 2 ++ test/ts/compliance.test.py | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/test/ts/compliance.py b/test/ts/compliance.py index 2c50929eca0..3199ec6310f 100755 --- a/test/ts/compliance.py +++ b/test/ts/compliance.py @@ -771,6 +771,8 @@ def ac3_frame(es: bytes, pos: int) -> tuple[int, float]: h = es[pos : pos + 6] if len(h) < 6 or h[0] != 0x0B or h[1] != 0x77: raise Refused(f"no AC-3 sync at ES offset {pos}") + if h[5] >> 3 > 16: + raise Refused(f"reserved bsid {h[5] >> 3} at ES offset {pos}") if h[5] >> 3 > 10: # bsid 11-16: E-AC-3 (A/52 Annex E) if h[4] >> 6 == 3 and (h[4] >> 4) & 0x03 == 3: raise Refused(f"E-AC-3 fscod2 reserved at ES offset {pos}") diff --git a/test/ts/compliance.test.py b/test/ts/compliance.test.py index c12e1ac38a9..8194a1ef7c2 100644 --- a/test/ts/compliance.test.py +++ b/test/ts/compliance.test.py @@ -18,6 +18,12 @@ def test_only_independent_substream_zero_advances_time(self): with self.subTest(stream=stream): self.assertEqual(compliance.ac3_frame(eac3(stream), 0), (512, duration)) + def test_reserved_bsid_is_refused(self): + frame = bytearray(eac3(0x00)) + frame[5] = 17 << 3 + with self.assertRaises(compliance.Refused): + compliance.ac3_frame(bytes(frame), 0) + def test_reserved_reduced_rate_is_refused(self): frame = bytearray(eac3(0x00)) frame[4] = 0xF0 # fscod 3 with fscod2 3 From 98f4ac00266e5c116356aee8388f556031d8fc6d Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 17 Oct 2026 10:49:46 -0700 Subject: [PATCH 10/10] fix(ts): refuse low-rate AC-3 and reserved E-AC-3 stream types in the compliance model The model now refuses what the export's parsers refuse: bsid 9 and 10, and strmtyp 3, rather than grading them as ordinary frames. Co-Authored-By: Claude Opus 5.5 --- test/ts/compliance.py | 11 ++++++++--- test/ts/compliance.test.py | 14 ++++++++++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/test/ts/compliance.py b/test/ts/compliance.py index 3199ec6310f..829100951ef 100755 --- a/test/ts/compliance.py +++ b/test/ts/compliance.py @@ -771,9 +771,14 @@ def ac3_frame(es: bytes, pos: int) -> tuple[int, float]: h = es[pos : pos + 6] if len(h) < 6 or h[0] != 0x0B or h[1] != 0x77: raise Refused(f"no AC-3 sync at ES offset {pos}") - if h[5] >> 3 > 16: - raise Refused(f"reserved bsid {h[5] >> 3} at ES offset {pos}") - if h[5] >> 3 > 10: # bsid 11-16: E-AC-3 (A/52 Annex E) + # bsid 8 or less is AC-3 and 11-16 E-AC-3 (A/52 Annex E); 9 and 10 are low-rate AC-3 + # variants the export does not parse, and the rest are reserved. + bsid = h[5] >> 3 + if bsid in (9, 10) or bsid > 16: + raise Refused(f"unsupported bsid {bsid} at ES offset {pos}") + if bsid > 10: + if h[2] >> 6 == 3: + raise Refused(f"E-AC-3 strmtyp reserved at ES offset {pos}") if h[4] >> 6 == 3 and (h[4] >> 4) & 0x03 == 3: raise Refused(f"E-AC-3 fscod2 reserved at ES offset {pos}") blocks = (1, 2, 3, 6)[(h[4] >> 4) & 0x03] if h[4] >> 6 != 3 else 6 diff --git a/test/ts/compliance.test.py b/test/ts/compliance.test.py index 8194a1ef7c2..ee93a1f9a29 100644 --- a/test/ts/compliance.test.py +++ b/test/ts/compliance.test.py @@ -18,11 +18,17 @@ def test_only_independent_substream_zero_advances_time(self): with self.subTest(stream=stream): self.assertEqual(compliance.ac3_frame(eac3(stream), 0), (512, duration)) - def test_reserved_bsid_is_refused(self): - frame = bytearray(eac3(0x00)) - frame[5] = 17 << 3 + def test_unsupported_bsid_is_refused(self): + for bsid in (9, 10, 17): + with self.subTest(bsid=bsid): + frame = bytearray(eac3(0x00)) + frame[5] = bsid << 3 + with self.assertRaises(compliance.Refused): + compliance.ac3_frame(bytes(frame), 0) + + def test_reserved_stream_type_is_refused(self): with self.assertRaises(compliance.Refused): - compliance.ac3_frame(bytes(frame), 0) + compliance.ac3_frame(eac3(0xC0), 0) def test_reserved_reduced_rate_is_refused(self): frame = bytearray(eac3(0x00))