diff --git a/quest/m2/README.md b/quest/m2/README.md index 3a6cd263fde..dcf4c97fa28 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -93,7 +93,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..d8e547615ca 100644 --- a/rs/moq-mux/src/codec/eac3.rs +++ b/rs/moq-mux/src/codec/eac3.rs @@ -28,6 +28,31 @@ 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) +} + +/// 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 { return Err(legacy::Error::Eac3HeaderTooShort); } @@ -42,21 +67,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 48b79e2ff84..c3892e71c46 100644 --- a/rs/moq-mux/src/container/ts/export.rs +++ b/rs/moq-mux/src/container/ts/export.rs @@ -278,8 +278,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 @@ -316,7 +316,7 @@ impl Track { keyframe: None, timing: Timing::default(), buffer: None, - ac3_carry: None, + dolby_carry: None, pack: None, released: None, } @@ -334,8 +334,8 @@ impl Track { self.restart = pending.restart; self.clock = DecodeClock::default(); } - if self.carries_ac3() { - return self.queue_ac3(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); @@ -419,12 +419,13 @@ 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, + codec: Dolby, name: &str, pending: Pending, jitter: &mut jitter::Buffer, @@ -437,17 +438,7 @@ impl Track { arrived, read, } = pending; - let Some(frames) = ac3_frames(&mut self.ac3_carry, skip, frame.timestamp, &frame.payload) else { - let pending = Pending { - frame, - restart, - skip, - cut, - arrived, - read, - }; - return self.push(name, pending, None, None, jitter); - }; + let frames = dolby_units(codec, &mut self.dolby_carry, skip, frame.timestamp, &frame.payload)?; for (timestamp, payload) in frames { let frame = Frame { timestamp, @@ -469,20 +460,18 @@ 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) + /// 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; + }; + Dolby::described(&self.descriptors) } /// The samples an audio frame of `payload` lasts and their rate, if it may share a PES with @@ -494,7 +483,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()?; @@ -2323,8 +2312,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<()> { @@ -2334,9 +2323,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(); @@ -2997,29 +2986,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); @@ -3288,11 +3284,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 { @@ -3305,7 +3304,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), @@ -3323,81 +3323,173 @@ fn verbatim_buffer(kind: &Kind, descriptors: &[catalog::Descriptor]) -> Option 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: 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 => 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::Eac3 if !ac3_core(data) => crate::codec::eac3::sync_header(data), + _ => crate::codec::ac3::parse_header(data), + } + } + + /// 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::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`]). 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; 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. /// /// 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, 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, 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 joined = carry.as_ref().is_none_or(|head| head.skip != skip); + 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 => { - 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)?; + 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"); + } + } } 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 { + 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(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(units); }; - let length = duration(header.samples, header.sample_rate).ok()?; - let end = head.at.checked_add(length).ok()?; + let at = match (codec.begins_unit(&probe), head.unit) { + (false, Some(unit)) => unit.at, + _ => head.next, + }; + 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); + // 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 { - 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"), } } } @@ -3405,69 +3497,115 @@ 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 { + // 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!( 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 = 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. - 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: base.checked_add(duration(samples, rate)?)?, + unit: open.filter(|_| codec == Dolby::Eac3), + bytes: left, + }); + Ok(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); @@ -3475,7 +3613,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) } @@ -3722,8 +3860,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 { @@ -3785,6 +3923,68 @@ 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(); + 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}"); + } + } + + /// 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(); + 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] + 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. @@ -4175,19 +4375,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)]); } } diff --git a/rs/moq-mux/src/container/ts/export_test.rs b/rs/moq-mux/src/container/ts/export_test.rs index 36a9a8900fe..ac860b04633 100644 --- a/rs/moq-mux/src/container/ts/export_test.rs +++ b/rs/moq-mux/src/container/ts/export_test.rs @@ -1656,6 +1656,145 @@ 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 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 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)] +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)] +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]; @@ -1691,6 +1830,11 @@ 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, and the export's stats. async fn exported_ac3_pes(input: Vec) -> (Vec<(u64, Vec)>, super::stats::Export) { + exported_dolby_pes(input, 0x6a).await +} + +/// [`exported_ac3_pes`] for the Dolby audio `descriptor` tags. +async fn exported_dolby_pes(input: Vec, descriptor: u8) -> (Vec<(u64, Vec)>, super::stats::Export) { const AC3_PID: u16 = 0x104; let data = include_bytes!("test_data/bbb.ts"); let mut broadcast = moq_net::broadcast::Info::new().produce(); @@ -1710,7 +1854,7 @@ async fn exported_ac3_pes(input: Vec) -> (Vec<(u64, Vec)>, super:: 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/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 a90fce43d1d..829100951ef 100755 --- a/test/ts/compliance.py +++ b/test/ts/compliance.py @@ -449,6 +449,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), @@ -764,16 +766,26 @@ 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}") - 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 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}") @@ -822,7 +834,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 @@ -860,6 +872,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/compliance.test.py b/test/ts/compliance.test.py new file mode 100644 index 00000000000..ee93a1f9a29 --- /dev/null +++ b/test/ts/compliance.test.py @@ -0,0 +1,41 @@ +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)) + + 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(eac3(0xC0), 0) + + 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() diff --git a/test/ts/tstd-controls.py b/test/ts/tstd-controls.py index ba63172f6b6..f915be75785 100755 --- a/test/ts/tstd-controls.py +++ b/test/ts/tstd-controls.py @@ -75,6 +75,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 @@ -255,20 +257,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) @@ -286,7 +293,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)) @@ -470,11 +477,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 @@ -531,6 +538,8 @@ def build(path: str) -> str: ("unclocked tables", system("unclocked tables"), ("system-buffers", {"outside a clocked time base"})), ("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]: