From 2f6b7e8a1c1500ebcf50ceb2dc5ae4ccec58e336 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 21 Aug 2026 01:01:57 +0300 Subject: [PATCH] =?UTF-8?q?feat(truehd):=20lossless=20substream=20decode?= =?UTF-8?q?=20=E2=80=94=202/6/8-ch=20presentations=20bit-exact,=2016-ch=20?= =?UTF-8?q?object=20substream=20refused?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Decodes: restart headers (14-bit sync, CRC-8 poly 0x1D verified), decoding parameters (presence flags, block size, primitive matrices, output shift, quant step, FIR≤8/IIR≤4 with carried history, Huffman offset/codebook/LSBs), residuals through the three codebooks (read off an external encoder's mono output, then oracle-checked), per-substream parity and the lossless-check byte, rematrixing with both noise generators (23-bit LFSR 2-channel noise for 0x31EA; 256-entry dither table for 0x31EB, every entry solved by constraint intersection against the external decoder on the real 7.1 track), output shift, interleaved S32 left-justified 24-bit. TrueHD 7.1 (assignment 0x4F) lands on FL FR FC LFE BL BR SL SR via [0,1,2,3,6,7,4,5]. MLP major sync's rate-code offset fixed (data[9], not data[8]). Refused by name (Error::Unsupported): the 4th / 16-channel object substream, any substream with more than 8 matrix channels, 8-ch presentations whose channel assignment is not the standard 7.1 mask, reserved rate codes. Not validated: the per-substream CRC-8 check byte (generator/span unknown; parsed and skipped, see corner-cut in decode.rs). Oracle (tests/oracle.rs, skipped without ffmpeg / the remux): first 60 s of the real 7.1 remux 23 040 000 / 23 040 000 samples exact, 0 lossless-check, 0 parity, 0 restart-CRC, 0 length failures over 72 000 access units; externally encoded MLP and TrueHD mono/stereo/5.1 streams all bit-exact. --- crates/ec-truehd/src/decode.rs | 839 +++++++++++++++++++++++++++++++ crates/ec-truehd/src/lib.rs | 13 +- crates/ec-truehd/src/sync.rs | 105 +++- crates/ec-truehd/tests/oracle.rs | 178 +++++++ 4 files changed, 1106 insertions(+), 29 deletions(-) create mode 100644 crates/ec-truehd/src/decode.rs create mode 100644 crates/ec-truehd/tests/oracle.rs diff --git a/crates/ec-truehd/src/decode.rs b/crates/ec-truehd/src/decode.rs new file mode 100644 index 00000000..bebb14c3 --- /dev/null +++ b/crates/ec-truehd/src/decode.rs @@ -0,0 +1,839 @@ +//! The lossless substream decode: restart headers, decoding parameters, +//! Huffman/LSB residuals, FIR/IIR prediction, rematrixing with the seeded +//! noise generators, output shift — and the stream's own integrity checks +//! (restart-header CRC, per-substream parity, the lossless-check byte). +//! +//! Written from the public description of the MLP/TrueHD bitstream. Every +//! numeric constant a from-memory description could get wrong (the three +//! Huffman codebooks, the noise table, the generators' feedback taps, the +//! restart-header CRC) was *derived or confirmed* against an external +//! decoder: the codebooks were read off externally encoded mono streams +//! whose PCM is known, the 256-entry noise table was solved for by +//! constraint intersection over the real 7.1 track, and `tests/oracle.rs` +//! keeps both bit-exact. +//! +//! **Per-substream syntax** (one substream's bytes, bit-serial MSB-first): +//! +//! ```text +//! loop { +//! params_present(1) +//! restart_present(1) → restart header: +//! sync(14) = 0x31EA|0x31EB (low bit = noise type), output_timing(16), +//! min_ch(4), max_ch(4), max_matrix_ch(4), noise_shift(4), +//! noise_seed(23), reserved(19), data_check_present(1), +//! lossless_check(8), reserved(16), ch_assign(6) × (max_matrix_ch+1), +//! crc8(8, poly 0x1D) +//! decoding params (each group gated by a presence flag): +//! presence flags(8); block size(9); matrices: count(4), per matrix +//! out_ch(4) frac_bits(4) lsb_bypass(1) [present(1) coeff(frac+2)] per +//! input channel (+2 noise channels for noise type 0) [noise_shift(4) +//! for noise type 1]; output shift(4 signed) per matrix channel; +//! quant step(4) per channel; per channel: FIR/IIR (order(4), shift(4), +//! coeff_bits(5), coeff_shift(3), coeffs, [IIR state]), huff_offset(15 +//! signed), codebook(2), huff_lsbs(5) +//! block: [data check: bit count(16)] per sample: lsb-bypass bits, then +//! per channel Huffman index (codebook 1..3) + lsb bits; [crc(8)] +//! end_of_segment(1) +//! } +//! align(16) [0xD234D234 end-of-stream] [parity(8) crc8(8, not validated)] +//! ``` +//! +//! **Reconstruction**: `residual = ((index << lsb_bits) + lsbs + +//! sign_offset) << quant_step`; `sample = (predict(FIR over past samples, +//! IIR over past residual-after-FIR) + residual) & !((1 << quant_step) - 1)`; +//! then, for the highest substream only, each primitive matrix rewrites one +//! channel as `(Σ coeff·chan + noise) >> 14` masked to the quant step plus +//! its bypassed LSB; then `<< output_shift`; then `<< 8` to a left-justified +//! 24-in-32 sample. +//! +//! **Channel order**: TrueHD's 8-channel presentation with assignment `0x4F` +//! (L R C LFE Ls Rs Lrs Rrs) lands on [`ChannelLayout::Surround7_1`]'s +//! FL FR FC LFE BL BR SL SR as output index `[0,1,2,3,6,7,4,5]` — TrueHD's +//! Ls/Rs pair is the *side* pair, Lrs/Rrs the *back* pair. 2- and 6-channel +//! presentations map 1:1. + +use ec_core::bitio::BitReader; +use ec_core::error::{Error, Result}; +use ec_core::frame::{AudioFrame, ChannelLayout, SampleFormat}; +use ec_core::packet::Buf; + +use crate::sync::AccessUnitHeader; + +/// Highest matrix/output channel index the 2/6/8-channel presentations use; +/// anything above is the 16-channel object substream, refused. +const MAX_MATRIX_CHANNEL: usize = 7; +/// Sample-buffer width: 8 PCM channels plus the 2 noise channels noise type 0 +/// appends after the last matrix channel. +const MAX_CHANNELS: usize = MAX_MATRIX_CHANNEL + 3; +const MAX_MATRICES: usize = 8; +const MAX_FILTER_ORDER: usize = 8; +const END_OF_STREAM: u32 = 0xD234_D234; + +/// The fixed 256-entry dither table noise type 1 (TrueHD's `0x31EB`) draws +/// matrix noise from, indexed by the generator's top byte. A constant of the +/// format; every entry was solved for exactly (constraint intersection over +/// 72 000 access units of the real 7.1 track against the external decoder's +/// output) and is kept bit-exact by `tests/oracle.rs`. +#[rustfmt::skip] +const NOISE_TABLE: [i8; 256] = [ + 30, 51, 22, 54, 3, 7, -4, 38, 14, 55, 46, 81, 22, 58, -3, 2, + 52, 31, -7, 51, 15, 44, 74, 30, 85, -17, 10, 33, 18, 80, 28, 62, + 10, 32, 23, 69, 72, 26, 35, 17, 73, 60, 8, 56, 2, 6, -2, -5, + 51, 4, 11, 50, 66, 76, 21, 44, 33, 47, 1, 26, 64, 48, 57, 40, + 38, 16, -10, -28, 92, 22, -18, 29, -10, 5, -13, 49, 19, 24, 70, 34, + 61, 48, 30, 14, -6, 25, 58, 33, 42, 60, 67, 17, 54, 17, 22, 30, + 67, 44, -9, 50, -11, 43, 40, 32, 59, 82, 13, 49, -14, 55, 60, 36, + 48, 49, 31, 47, 15, 12, 4, 65, 1, 23, 29, 39, 45, -2, 84, 69, + 0, 72, 37, 57, 27, 41, -15, -16, 35, 31, 14, 61, 24, 0, 27, 24, + 16, 41, 55, 34, 53, 9, 56, 12, 25, 29, 53, 5, 20, -20, -8, 20, + 13, 28, -3, 78, 38, 16, 11, 62, 46, 29, 21, 24, 46, 65, 43, -23, + 89, 18, 74, 21, 38, -12, 19, 12, -19, 8, 15, 33, 4, 57, 9, -8, + 36, 35, 26, 28, 7, 83, 63, 79, 75, 11, 3, 87, 37, 47, 34, 40, + 39, 19, 20, 42, 27, 34, 39, 77, 13, 42, 59, 64, 45, -1, 32, 37, + 45, -5, 53, -6, 7, 36, 50, 23, 6, 32, 9, -21, 18, 71, 27, 52, + -25, 31, 35, 42, -1, 68, 63, 52, 26, 43, 66, 37, 41, 25, 40, 70, +]; + +const PARAM_BLOCKSIZE: u8 = 1 << 7; +const PARAM_MATRIX: u8 = 1 << 6; +const PARAM_OUTSHIFT: u8 = 1 << 5; +const PARAM_QUANTSTEP: u8 = 1 << 4; +const PARAM_FIR: u8 = 1 << 3; +const PARAM_IIR: u8 = 1 << 2; +const PARAM_HUFFOFFSET: u8 = 1 << 1; +const PARAM_PRESENCE: u8 = 1 << 0; + +/// The three residual codebooks as `(code, length)` per index: codebook 1 +/// covers indices 0..18, codebook 2 0..16, codebook 3 0..15. Unused tail +/// entries have length 0. Shape: a unary run `0…01` of length `7 - i + 2` +/// below the centre, `010…01` above it, and 1-3 short codes at the centre +/// (the three books differ only there). Verified code-for-code against an +/// external encoder's output (`tests/oracle.rs`). +pub const HUFFMAN_TABLES: [[(u16, u8); 18]; 3] = [ + [ + (0x01, 9), (0x01, 8), (0x01, 7), (0x01, 6), (0x01, 5), (0x01, 4), (0x01, 3), (0x04, 3), (0x05, 3), + (0x06, 3), (0x07, 3), (0x03, 3), (0x05, 4), (0x09, 5), (0x11, 6), (0x21, 7), (0x41, 8), (0x81, 9), + ], + [ + (0x01, 9), (0x01, 8), (0x01, 7), (0x01, 6), (0x01, 5), (0x01, 4), (0x01, 3), (0x02, 2), (0x03, 2), + (0x03, 3), (0x05, 4), (0x09, 5), (0x11, 6), (0x21, 7), (0x41, 8), (0x81, 9), (0, 0), (0, 0), + ], + [ + (0x01, 9), (0x01, 8), (0x01, 7), (0x01, 6), (0x01, 5), (0x01, 4), (0x01, 3), (0x01, 1), (0x03, 3), + (0x05, 4), (0x09, 5), (0x11, 6), (0x21, 7), (0x41, 8), (0x81, 9), (0, 0), (0, 0), (0, 0), + ], +]; + +/// 512-entry lookup per codebook: `(index, length)`, length 0 = invalid code. +struct HuffLut([[(u8, u8); 512]; 3]); + +impl HuffLut { + fn build() -> HuffLut { + let mut lut = [[(0u8, 0u8); 512]; 3]; + for (book, table) in HUFFMAN_TABLES.iter().enumerate() { + for (index, &(code, len)) in table.iter().enumerate() { + if len == 0 { + continue; + } + let base = usize::from(code) << (9 - len); + for k in 0..(1usize << (9 - len)) { + lut[book][base + k] = (index as u8, len); + } + } + } + HuffLut(lut) + } + + fn decode(&self, br: &mut BitReader, book: usize) -> Result { + let avail = br.bits_remaining().min(9) as u32; + if avail == 0 { + return Err(Error::NeedMore); + } + let bits = br.peek_bits(avail)? << (9 - avail); + let (index, len) = self.0[book][bits as usize]; + if len == 0 || u64::from(len) > br.bits_remaining() { + return Err(Error::corrupt("TrueHD: invalid Huffman code")); + } + br.skip_bits(u64::from(len))?; + Ok(i32::from(index)) + } +} + +/// Bit-serial CRC-8 (MSB first, no reflection, no final xor). +fn crc8(poly: u8, init: u8, bytes: &[u8]) -> u8 { + let mut crc = init; + for &b in bytes { + crc ^= b; + for _ in 0..8 { + crc = if crc & 0x80 != 0 { (crc << 1) ^ poly } else { crc << 1 }; + } + } + crc +} + +/// The restart header's own CRC-8 (poly 0x1D), over the header from its +/// first byte (`buf[0]`, whose two leading bits — the params/restart flags — +/// are masked off) through its last full byte and the trailing partial bits. +fn restart_checksum(buf: &[u8], bit_size: u64) -> u8 { + let bit_size = bit_size as usize + 2; + let num_bytes = bit_size / 8; + let mut crc = crc8(0x1D, 0, &[buf[0] & 0x3F]); + crc = crc8(0x1D, crc, &buf[1..num_bytes - 1]); + crc ^= buf[num_bytes - 1]; + for i in 0..(bit_size & 7) { + let top = crc & 0x80 != 0; + crc <<= 1; + if top { + crc ^= 0x1D; + } + crc ^= (buf[num_bytes] >> (7 - i)) & 1; + } + crc +} + +/// XOR-fold a 32-bit word to 8 bits. +fn xor_32_to_8(v: u32) -> u8 { + (v ^ (v >> 8) ^ (v >> 16) ^ (v >> 24)) as u8 +} + +#[derive(Debug, Clone, Copy, Default)] +struct Filter { + order: usize, + shift: u32, + coeffs: [i32; MAX_FILTER_ORDER], + /// History, most recent first. + state: [i32; MAX_FILTER_ORDER], +} + +#[derive(Debug, Clone, Copy, Default)] +struct ChannelParams { + fir: Filter, + iir: Filter, + huff_offset: i32, + sign_huff_offset: i32, + codebook: u8, + huff_lsbs: u8, +} + +#[derive(Debug, Clone, Copy, Default)] +struct Matrix { + out_ch: usize, + lsb_bypass: bool, + noise_shift: u32, + coeffs: [i32; MAX_CHANNELS], +} + +#[derive(Debug, Clone)] +struct Substream { + restart_seen: bool, + noise_type: bool, + min_channel: usize, + max_channel: usize, + max_matrix_channel: usize, + noise_shift: u32, + noisegen_seed: u32, + data_check_present: bool, + lossless_check_data: u32, + ch_assign: [usize; MAX_CHANNELS], + param_presence_flags: u8, + blocksize: usize, + matrices: Vec, + output_shift: [i32; MAX_CHANNELS], + quant_step_size: [u32; MAX_CHANNELS], + channels: [ChannelParams; MAX_CHANNELS], + /// Samples decoded so far in this access unit. + blockpos: usize, +} + +impl Substream { + fn new() -> Substream { + Substream { + restart_seen: false, + noise_type: false, + min_channel: 0, + max_channel: 0, + max_matrix_channel: 0, + noise_shift: 0, + noisegen_seed: 0, + data_check_present: false, + lossless_check_data: 0, + ch_assign: [0; MAX_CHANNELS], + param_presence_flags: 0xFF, + blocksize: 8, + matrices: Vec::new(), + output_shift: [0; MAX_CHANNELS], + quant_step_size: [0; MAX_CHANNELS], + channels: [ChannelParams::default(); MAX_CHANNELS], + blockpos: 0, + } + } + + fn recompute_sign_offset(&mut self, ch: usize) { + let cp = &mut self.channels[ch]; + let lsb_bits = i32::from(cp.huff_lsbs) - self.quant_step_size[ch] as i32; + let sign_shift = lsb_bits + if cp.codebook > 0 { 2 - i32::from(cp.codebook) } else { -1 }; + let mut off = cp.huff_offset; + if cp.codebook > 0 { + off -= 7 << lsb_bits; + } + if sign_shift >= 0 { + off -= 1 << sign_shift; + } + cp.sign_huff_offset = off; + } +} + +/// Integrity counters the decoder keeps across access units; every one is a +/// stream-side self-check that stays zero on a clean decode. +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] +pub struct CheckStats { + /// Restart headers whose CRC-8 did not match. + pub restart_crc_failures: u64, + /// Restart headers seen (the lossless-check byte is verified at each + /// one after the first, against the samples output since the previous). + pub restart_headers: u64, + /// Lossless-check bytes that did not match the output. + pub lossless_check_failures: u64, + /// Substream segments whose parity byte did not match. + pub parity_failures: u64, + /// Substream segments whose bit count did not land on the directory's + /// end pointer. + pub length_mismatches: u64, +} + +/// Decoder state shared across the substreams of one stream. +#[derive(Debug, Clone)] +pub(crate) struct Core { + huff: std::sync::Arc, + substreams: Vec, + /// Samples per access unit (40 at 48 kHz) and its next power of two. + access_unit_size: usize, + access_unit_size_pow2: usize, + sample_rate: u32, + /// The major sync's 8-channel presentation assignment mask. + ch8_assignment: u16, + sample_buffer: Vec<[i32; MAX_CHANNELS]>, + bypassed_lsbs: Vec<[i32; MAX_MATRICES]>, + noise_buffer: Vec, + /// Self-check tallies. + pub stats: CheckStats, +} + +struct HuffLutBox(HuffLut); + +impl std::fmt::Debug for HuffLutBox { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("HuffLut") + } +} + +impl Core { + pub(crate) fn new() -> Core { + Core { + huff: std::sync::Arc::new(HuffLutBox(HuffLut::build())), + substreams: Vec::new(), + access_unit_size: 0, + access_unit_size_pow2: 0, + sample_rate: 0, + ch8_assignment: 0, + sample_buffer: Vec::new(), + bypassed_lsbs: Vec::new(), + noise_buffer: Vec::new(), + stats: CheckStats::default(), + } + } + + pub(crate) fn reset(&mut self) { + self.substreams.clear(); + self.access_unit_size = 0; + } + + /// Decodes one access unit whose header already parsed; `None` until the + /// first major sync and every substream's first restart header are seen. + pub(crate) fn decode(&mut self, header: &AccessUnitHeader, data: &[u8]) -> Result> { + if !self.prepare(header)? || !self.decode_substreams(header, data)? { + return Ok(None); + } + self.finish() + } + + /// Takes a major sync's stream parameters on board; `Ok(false)` while no + /// major sync has been seen yet. + fn prepare(&mut self, header: &AccessUnitHeader) -> Result { + if let Some(sync) = &header.major_sync { + let shift = sync.rate_code & 7; + if shift > 2 { + return Err(Error::unsupported( + format!("TrueHD sample rate code {}", sync.rate_code), + "the format reserves it", + )); + } + self.access_unit_size = 40 << shift; + self.access_unit_size_pow2 = 64 << shift; + self.sample_rate = sync.sample_rate; + if self.substreams.len() != header.substreams.len() { + self.substreams = vec![Substream::new(); header.substreams.len()]; + } + self.sample_buffer = vec![[0; MAX_CHANNELS]; self.access_unit_size]; + self.bypassed_lsbs = vec![[0; MAX_MATRICES]; self.access_unit_size]; + self.noise_buffer = vec![0; self.access_unit_size_pow2]; + self.ch8_assignment = sync.ch8_assignment; + } + Ok(self.access_unit_size != 0 && self.substreams.len() == header.substreams.len()) + } + + /// Every substream's blocks into `sample_buffer`; `Ok(false)` while some + /// substream still awaits its first restart header. + fn decode_substreams(&mut self, header: &AccessUnitHeader, data: &[u8]) -> Result { + let mut all_seen = true; + for (i, info) in header.substreams.iter().enumerate() { + let (start, end) = header.substream_span(i); + let seg = data.get(start..end).ok_or(Error::NeedMore)?; + let seen = self.decode_substream(i, seg, info.flags & 0x2 != 0)?; + all_seen &= seen; + } + Ok(all_seen) + } + + /// Rematrix + output of the top substream for the current access unit. + fn finish(&mut self) -> Result> { + let top = self.substreams.len() - 1; + let samples = self.substreams[top].blockpos; + let channels = self.substreams[top].max_matrix_channel + 1; + let layout = ChannelLayout::from_count(channels); + if layout == ChannelLayout::Surround7_1 && self.ch8_assignment != 0x4F { + return Err(Error::unsupported( + format!("TrueHD 8-channel presentation with channel assignment {:#x}", self.ch8_assignment), + "only the standard 7.1 (L R C LFE Ls Rs Lrs Rrs) assignment is mapped", + )); + } + self.rematrix(top); + let frame = self.output(top, layout, samples)?; + Ok(Some(frame)) + } + + /// One substream's segment; `Ok(false)` when no restart header has been + /// seen yet (nothing to decode until one arrives). + fn decode_substream(&mut self, idx: usize, seg: &[u8], checkdata: bool) -> Result { + let mut br = BitReader::new(seg); + self.substreams[idx].blockpos = 0; + loop { + if br.read_bit()? { + if br.read_bit()? { + self.read_restart_header(idx, &mut br, seg)?; + } + if !self.substreams[idx].restart_seen { + return Ok(false); + } + self.read_decoding_params(idx, &mut br)?; + } + if !self.substreams[idx].restart_seen { + return Ok(false); + } + self.read_block(idx, &mut br)?; + if br.bits_remaining() == 0 { + self.stats.length_mismatches += 1; + return Ok(true); + } + if br.read_bit()? { + break; + } + } + let pad = (16 - (br.bit_position() % 16)) % 16; + br.skip_bits(pad)?; + if br.bits_remaining() >= 32 && br.peek_bits(32)? == END_OF_STREAM { + br.skip_bits(32)?; + } + if checkdata { + let data_len = (br.bit_position() / 8) as usize; + if seg.len() < data_len + 2 { + return Err(Error::corrupt("TrueHD: substream check bytes missing")); + } + let parity = seg[..data_len].iter().fold(0u8, |a, &b| a ^ b) ^ 0xA9; + let parity_bits = br.read_bits(8)? as u8; + // corner-cut: the second check byte is a CRC-8 whose generator + // and span this build could not pin down (no plain CRC-8 over + // the segment, with any polynomial/init/reflection, matched + // either the real track or an external encoder's output) — it + // is skipped, not validated. Parity, the restart-header CRC and + // the lossless check cover the same data. + br.skip_bits(8)?; + if parity != parity_bits { + self.stats.parity_failures += 1; + } + } + if br.bits_remaining() != 0 { + self.stats.length_mismatches += 1; + } + Ok(true) + } + + fn read_restart_header(&mut self, idx: usize, br: &mut BitReader, seg: &[u8]) -> Result<()> { + let start = br.bit_position(); + // 0x31EA / 0x31EB as a 14-bit field: 13 bits of sync, 1 bit of + // noise type. + let sync = br.read_bits(13)?; + if sync != 0x31EA >> 1 { + return Err(Error::corrupt(format!("TrueHD: restart sync {sync:#x}"))); + } + let noise_type = br.read_bit()?; + br.skip_bits(16)?; // output timing + let min_channel = br.read_bits(4)? as usize; + let max_channel = br.read_bits(4)? as usize; + let max_matrix_channel = br.read_bits(4)? as usize; + if max_matrix_channel > MAX_MATRIX_CHANNEL || max_channel > MAX_MATRIX_CHANNEL { + return Err(Error::unsupported( + "TrueHD substream with more than 8 matrix channels", + "the 16-channel object substream is not implemented", + )); + } + if min_channel > max_channel { + return Err(Error::corrupt("TrueHD: min channel above max channel")); + } + let noise_shift = br.read_bits(4)?; + let noisegen_seed = br.read_bits(23)?; + br.skip_bits(19)?; + let data_check_present = br.read_bit()?; + let lossless_check = br.read_bits(8)? as u8; + br.skip_bits(16)?; + // Per matrix channel, the presentation's output slot it lands on; + // stored inverted (output slot → matrix channel) for `output`. + let mut ch_assign = [0usize; MAX_CHANNELS]; + for ch in 0..=max_matrix_channel { + let a = br.read_bits(6)? as usize; + if a > max_matrix_channel { + return Err(Error::corrupt("TrueHD: channel assignment out of range")); + } + ch_assign[a] = ch; + } + let expected = restart_checksum(&seg[(start / 8) as usize..], br.bit_position() - start); + let crc = br.read_bits(8)? as u8; + self.stats.restart_headers += 1; + if crc != expected { + self.stats.restart_crc_failures += 1; + } + + let is_top = idx == self.substreams.len() - 1; + let s = &mut self.substreams[idx]; + if s.restart_seen && is_top && xor_32_to_8(s.lossless_check_data) != lossless_check { + self.stats.lossless_check_failures += 1; + } + let s = &mut self.substreams[idx]; + *s = Substream { + restart_seen: true, + noise_type, + min_channel, + max_channel, + max_matrix_channel, + noise_shift, + noisegen_seed, + data_check_present, + ch_assign, + ..Substream::new() + }; + Ok(()) + } + + fn read_decoding_params(&mut self, idx: usize, br: &mut BitReader) -> Result<()> { + let s = &mut self.substreams[idx]; + if s.param_presence_flags & PARAM_PRESENCE != 0 && br.read_bit()? { + s.param_presence_flags = br.read_bits(8)? as u8; + } + let flags = s.param_presence_flags; + if flags & PARAM_BLOCKSIZE != 0 && br.read_bit()? { + s.blocksize = br.read_bits(9)? as usize; + if s.blocksize < 8 || s.blocksize > self.access_unit_size { + return Err(Error::corrupt(format!("TrueHD: block size {}", s.blocksize))); + } + } + if flags & PARAM_MATRIX != 0 && br.read_bit()? { + let count = br.read_bits(4)? as usize; + if count > MAX_MATRICES { + return Err(Error::corrupt("TrueHD: too many primitive matrices")); + } + let max_chan = s.max_matrix_channel + if s.noise_type { 0 } else { 2 }; + s.matrices.clear(); + for _ in 0..count { + let out_ch = br.read_bits(4)? as usize; + let frac_bits = br.read_bits(4)?; + let lsb_bypass = br.read_bit()?; + if out_ch > s.max_matrix_channel || frac_bits > 14 { + return Err(Error::corrupt("TrueHD: matrix parameters out of range")); + } + let mut coeffs = [0i32; MAX_CHANNELS]; + for c in coeffs.iter_mut().take(max_chan + 1) { + if br.read_bit()? { + *c = br.read_signed(frac_bits + 2)? << (14 - frac_bits); + } + } + let noise_shift = if s.noise_type { br.read_bits(4)? } else { 0 }; + s.matrices.push(Matrix { + out_ch, + lsb_bypass, + noise_shift, + coeffs, + }); + } + } + if flags & PARAM_OUTSHIFT != 0 && br.read_bit()? { + for ch in 0..=s.max_matrix_channel { + s.output_shift[ch] = br.read_signed(4)?; + } + } + if flags & PARAM_QUANTSTEP != 0 && br.read_bit()? { + for ch in 0..=s.max_channel { + s.quant_step_size[ch] = br.read_bits(4)?; + s.recompute_sign_offset(ch); + } + } + for ch in s.min_channel..=s.max_channel { + if br.read_bit()? { + if flags & PARAM_FIR != 0 && br.read_bit()? { + read_filter(br, &mut s.channels[ch].fir, false)?; + } + if flags & PARAM_IIR != 0 && br.read_bit()? { + read_filter(br, &mut s.channels[ch].iir, true)?; + } + let cp = &s.channels[ch]; + if cp.fir.order + cp.iir.order > MAX_FILTER_ORDER { + return Err(Error::corrupt("TrueHD: FIR+IIR order above 8")); + } + if cp.fir.order > 0 && cp.iir.order > 0 && cp.fir.shift != cp.iir.shift { + return Err(Error::corrupt("TrueHD: FIR and IIR shifts differ")); + } + if flags & PARAM_HUFFOFFSET != 0 && br.read_bit()? { + s.channels[ch].huff_offset = br.read_signed(15)?; + } + s.channels[ch].codebook = br.read_bits(2)? as u8; + s.channels[ch].huff_lsbs = br.read_bits(5)? as u8; + if s.channels[ch].huff_lsbs > 24 { + return Err(Error::corrupt("TrueHD: huff_lsbs above 24")); + } + s.recompute_sign_offset(ch); + } + } + Ok(()) + } + + fn read_block(&mut self, idx: usize, br: &mut BitReader) -> Result<()> { + let s = &mut self.substreams[idx]; + let expected_end = if s.data_check_present { + Some(br.bit_position() + u64::from(br.read_bits(16)?)) + } else { + None + }; + if s.blockpos + s.blocksize > self.access_unit_size { + return Err(Error::corrupt("TrueHD: blocks exceed the access unit")); + } + for i in s.blockpos..s.blockpos + s.blocksize { + for (m, mat) in s.matrices.iter().enumerate() { + if mat.lsb_bypass { + self.bypassed_lsbs[i][m] = br.read_bit()? as i32; + } + } + for ch in s.min_channel..=s.max_channel { + let cp = &s.channels[ch]; + let qss = s.quant_step_size[ch]; + let lsb_bits = i32::from(cp.huff_lsbs) - qss as i32; + let mut result = 0i32; + if cp.codebook > 0 { + result = self.huff.0.decode(br, usize::from(cp.codebook) - 1)?; + } + if lsb_bits > 0 { + result = (result << lsb_bits) + br.read_bits(lsb_bits as u32)? as i32; + } else if lsb_bits < 0 { + return Err(Error::corrupt("TrueHD: quant step above huff_lsbs")); + } + result = result.wrapping_add(cp.sign_huff_offset).wrapping_shl(qss); + self.sample_buffer[i][ch] = result; + } + } + for ch in s.min_channel..=s.max_channel { + filter_channel(&mut s.channels[ch], s.quant_step_size[ch], &mut self.sample_buffer[s.blockpos..s.blockpos + s.blocksize], ch); + } + s.blockpos += s.blocksize; + if let Some(end) = expected_end { + if br.bit_position() != end { + self.stats.length_mismatches += 1; + } + br.skip_bits(8)?; + } + Ok(()) + } + + fn rematrix(&mut self, idx: usize) { + let s = &mut self.substreams[idx]; + let mut maxchan = s.max_matrix_channel; + if !s.noise_type { + // Two noise channels after the last matrix channel, from the + // 23-bit generator seeded by the restart header and carried on. + let mut seed = s.noisegen_seed; + for row in self.sample_buffer.iter_mut().take(s.blockpos) { + let shr7 = (seed >> 7) as u16; + row[maxchan + 1] = i32::from((seed >> 15) as u8 as i8) << s.noise_shift; + row[maxchan + 2] = i32::from(shr7 as u8 as i8) << s.noise_shift; + seed = (seed << 16) ^ (u32::from(shr7) << 5) ^ (u32::from(shr7) << 7); + } + s.noisegen_seed = seed; + maxchan += 2; + } else { + let mut seed = s.noisegen_seed; + for slot in self.noise_buffer.iter_mut() { + let shr15 = (seed >> 15) as u8; + *slot = NOISE_TABLE[usize::from(shr15)]; + seed = (seed << 8) ^ u32::from(shr15) ^ (u32::from(shr15) << 5); + } + s.noisegen_seed = seed; + } + let pow2_mask = self.access_unit_size_pow2 - 1; + let count = s.matrices.len(); + for (m, mat) in s.matrices.iter().enumerate() { + let mask = !((1i32 << s.quant_step_size[mat.out_ch]) - 1); + let mut index = count - m; + let index2 = 2 * index + 1; + for (i, row) in self.sample_buffer.iter_mut().take(s.blockpos).enumerate() { + let mut accum: i64 = 0; + for (sample, coeff) in row.iter().zip(&mat.coeffs).take(maxchan + 1) { + accum += i64::from(*sample) * i64::from(*coeff); + } + if mat.noise_shift != 0 { + index &= pow2_mask; + accum += i64::from(self.noise_buffer[index]) << (mat.noise_shift + 7); + index += index2; + } + row[mat.out_ch] = ((accum >> 14) as i32 & mask) + self.bypassed_lsbs[i][m]; + } + } + } + + fn output(&mut self, idx: usize, layout: ChannelLayout, samples: usize) -> Result { + let s = &mut self.substreams[idx]; + let channels = layout.channel_count(); + // TrueHD's Ls/Rs (output 4,5) are our side pair, Lrs/Rrs (6,7) our + // back pair. + const PERM_7_1: [usize; 8] = [0, 1, 2, 3, 6, 7, 4, 5]; + let mut bytes = Vec::with_capacity(samples * channels * 4); + for row in self.sample_buffer.iter().take(samples) { + for (out_ch, &perm) in PERM_7_1.iter().enumerate().take(channels) { + let thd_ch = if channels == 8 { perm } else { out_ch }; + let mat_ch = s.ch_assign[thd_ch]; + let sample = row[mat_ch].wrapping_shl(s.output_shift[mat_ch] as u32); + s.lossless_check_data ^= ((sample as u32) & 0xFF_FFFF) << mat_ch; + bytes.extend_from_slice(&(sample << 8).to_ne_bytes()); + } + } + AudioFrame::try_new( + SampleFormat::S32, + false, + layout, + self.sample_rate, + samples, + vec![Buf::from_vec(bytes)], + ) + } +} + +/// New coefficients for `f`; its history carries over unless the stream +/// sends an explicit (IIR-only) state. +fn read_filter(br: &mut BitReader, f: &mut Filter, iir: bool) -> Result<()> { + f.order = br.read_bits(4)? as usize; + if f.order > MAX_FILTER_ORDER || (!iir && f.order > 8) || (iir && f.order > 4) { + return Err(Error::corrupt("TrueHD: filter order out of range")); + } + if f.order > 0 { + f.shift = br.read_bits(4)?; + let coeff_bits = br.read_bits(5)?; + let coeff_shift = br.read_bits(3)?; + if !(1..=16).contains(&coeff_bits) || coeff_bits + coeff_shift > 16 { + return Err(Error::corrupt("TrueHD: filter coefficient precision out of range")); + } + for c in f.coeffs.iter_mut().take(f.order) { + *c = br.read_signed(coeff_bits)? << coeff_shift; + } + if br.read_bit()? { + if !iir { + return Err(Error::corrupt("TrueHD: FIR filter with explicit state")); + } + let state_bits = br.read_bits(4)?; + let state_shift = br.read_bits(4)?; + for st in f.state.iter_mut().take(f.order) { + *st = if state_bits > 0 { br.read_signed(state_bits)? << state_shift } else { 0 }; + } + } + } + Ok(()) +} + +/// Residual → sample through the channel's FIR (over past samples) and IIR +/// (over past residual-after-prediction) filters, in place on column `ch`. +fn filter_channel(cp: &mut ChannelParams, qss: u32, rows: &mut [[i32; MAX_CHANNELS]], ch: usize) { + let mask = !((1i32 << qss) - 1); + let shift = if cp.fir.order > 0 { cp.fir.shift } else { cp.iir.shift }; + for row in rows { + let mut accum: i64 = 0; + for j in 0..cp.fir.order { + accum += i64::from(cp.fir.state[j]) * i64::from(cp.fir.coeffs[j]); + } + for j in 0..cp.iir.order { + accum += i64::from(cp.iir.state[j]) * i64::from(cp.iir.coeffs[j]); + } + let accum = (accum >> shift) as i32; + let result = accum.wrapping_add(row[ch]) & mask; + cp.fir.state.copy_within(0..MAX_FILTER_ORDER - 1, 1); + cp.fir.state[0] = result; + cp.iir.state.copy_within(0..MAX_FILTER_ORDER - 1, 1); + cp.iir.state[0] = result.wrapping_sub(accum); + row[ch] = result; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Every code in the public tables decodes back to its own index when + /// written bit-exactly, with and without trailing garbage. + #[test] + fn every_codebook_entry_decodes_to_its_index() { + use ec_core::bitio::BitWriter; + let lut = HuffLut::build(); + for (book, table) in HUFFMAN_TABLES.iter().enumerate() { + for (index, &(code, len)) in table.iter().enumerate() { + if len == 0 { + continue; + } + let mut w = BitWriter::new(); + w.write_bits(u32::from(code), u32::from(len)); + w.write_bits(0x5A5, 11); + let bytes = w.into_bytes(); + let mut br = BitReader::new(&bytes); + assert_eq!(lut.decode(&mut br, book).unwrap(), index as i32, "book {book} index {index}"); + assert_eq!(br.bit_position(), u64::from(len)); + // Bare code at the very end of the buffer must decode too. + let mut w = BitWriter::new(); + w.write_bits(u32::from(code), u32::from(len)); + let bytes = w.into_bytes(); + let mut br = BitReader::new(&bytes); + assert_eq!(lut.decode(&mut br, book).unwrap(), index as i32); + } + } + // An all-zero 9-bit word is not a code in any book. + for book in 0..3 { + assert!(lut.decode(&mut BitReader::new(&[0, 0]), book).is_err()); + } + } + + #[test] + fn codebooks_are_prefix_free() { + for table in &HUFFMAN_TABLES { + let used: Vec<_> = table.iter().filter(|e| e.1 > 0).collect(); + for (i, a) in used.iter().enumerate() { + for b in &used[i + 1..] { + let l = a.1.min(b.1); + assert_ne!(a.0 >> (a.1 - l), b.0 >> (b.1 - l), "{a:?} vs {b:?}"); + } + } + } + } +} + diff --git a/crates/ec-truehd/src/lib.rs b/crates/ec-truehd/src/lib.rs index 800f5b5f..5df560fc 100644 --- a/crates/ec-truehd/src/lib.rs +++ b/crates/ec-truehd/src/lib.rs @@ -3,15 +3,18 @@ //! Written from Dolby's public TrueHD/MLP documentation of the bitstream's //! *shape* — major sync, per-substream access-unit directory, FIR/IIR filter //! parameters, the Huffman/LSB entropy coder and matrixing — not from any -//! third party's source. The syntax layer ([`sync`]) is parsed and verified -//! against a real 7.1 Blu-ray remux; the substream *decode* itself -//! ([`TrueHdDecoder::decode_access_unit`]) is not implemented yet. +//! third party's source. The syntax layer ([`sync`]) and the lossless +//! substream decode ([`decode`], reached through +//! [`TrueHdDecoder::decode_access_unit`]) are both verified bit-exact +//! against an external decoder on a real 7.1 Blu-ray remux (first 60 s, +//! 23 040 000 of 23 040 000 samples) and on externally encoded MLP/TrueHD +//! mono, stereo and 5.1 streams. //! //! Scope, stated once so a caller knows what to expect: //! //! - **In scope**: major sync detection and parsing (format id, sample rate, //! channel presentations), the access-unit header's per-substream -//! directory (end pointers, checksum/parity), and — once implemented — the +//! directory (end pointers, checksum/parity), and the //! three PCM-presentation substreams a real Blu-ray track carries: 2-ch, //! 6-ch (5.1) and 8-ch (7.1), each built from restart headers, FIR/IIR //! predictor filters, Huffman/LSB residual entropy and the channel @@ -33,7 +36,9 @@ #![forbid(unsafe_code)] #![warn(missing_docs)] +pub mod decode; pub mod sync; pub use ec_core::Error; +pub use decode::CheckStats; pub use sync::{AccessUnitHeader, MajorSyncInfo, MajorSyncFormat, SubstreamInfo, TrueHdDecoder, frame_length}; diff --git a/crates/ec-truehd/src/sync.rs b/crates/ec-truehd/src/sync.rs index 010dd708..3c3ddb09 100644 --- a/crates/ec-truehd/src/sync.rs +++ b/crates/ec-truehd/src/sync.rs @@ -105,6 +105,13 @@ pub struct MajorSyncInfo { pub format: MajorSyncFormat, /// The primary sample rate, decoded from `data[8]`'s high nibble. pub sample_rate: u32, + /// The raw 4-bit rate code (`& 7` is the `2^n` multiplier, which also + /// sizes the access unit: `40 << n` samples). + pub rate_code: u8, + /// The 8-channel presentation's 13-bit channel-assignment mask (bit 0 = + /// L/R, 1 = C, 2 = LFE, 3 = Ls/Rs, 6 = Lrs/Rrs; `0x4F` is standard 7.1); + /// 0 when the major sync is plain MLP, which has no such field. + pub ch8_assignment: u16, } /// A rate code's `44.1k*2^n` / `48k*2^n` value, or [`Error::Unsupported`] for @@ -134,10 +141,23 @@ impl MajorSyncInfo { MAJOR_SYNC_MLP => MajorSyncFormat::Mlp, _ => return Err(Error::corrupt("TrueHD: no major sync word")), }; - let sample_rate = sample_rate(data[4] >> 4)?; + // TrueHD: format_info starts with the rate code; MLP puts its two + // 4-bit word-length codes first and the rate codes one byte later. + let rate_code = match format { + MajorSyncFormat::TrueHd => data[4] >> 4, + MajorSyncFormat::Mlp => data[5] >> 4, + }; + let sample_rate = sample_rate(rate_code)?; + // format_info's last 13 bits: data[6] low 5 bits + data[7]. + let ch8_assignment = match format { + MajorSyncFormat::TrueHd => (u16::from(data[6] & 0x1F) << 8) | u16::from(data[7]), + MajorSyncFormat::Mlp => 0, + }; Ok(MajorSyncInfo { format, sample_rate, + rate_code, + ch8_assignment, }) } } @@ -168,6 +188,9 @@ pub struct AccessUnitHeader { pub input_timing: u16, /// Present on roughly one access unit per video frame. pub major_sync: Option, + /// Byte offset where the substream directory ends and substream 0's + /// coded data begins. + pub data_start: usize, /// One entry per substream, in stream order (`substreams.len()` is the /// substream count). pub substreams: Vec, @@ -234,10 +257,22 @@ impl AccessUnitHeader { length, input_timing, major_sync, + data_start: pos, substreams, }) } + /// Byte span `(start, end)` of substream `i`'s coded data within the + /// access unit, from the directory's cumulative end pointers. + pub fn substream_span(&self, i: usize) -> (usize, usize) { + let start = if i == 0 { + self.data_start + } else { + self.data_start + usize::from(self.substreams[i - 1].end_offset_words) * 2 + }; + (start, self.data_start + usize::from(self.substreams[i].end_offset_words) * 2) + } + /// The channel layout Dolby's substream convention implies: substream 0 /// is always a 2-ch presentation, substream 1 (when present) a discrete /// 5.1, substream 2 (when present) a discrete 7.1. [`parse`](Self::parse) @@ -252,14 +287,20 @@ impl AccessUnitHeader { } } -/// A TrueHD/MLP decoder for one stream. -/// -/// Access-unit framing and major-sync/substream-directory parsing are fully -/// implemented; substream decoding is not — see the crate-level docs for the -/// scope this covers once it is. +/// A TrueHD/MLP decoder for one stream: access units in, one interleaved +/// S32 (24-bit left-justified) [`AudioFrame`] of the highest presentation +/// out per access unit. See the crate-level docs for scope. #[derive(Debug)] pub struct TrueHdDecoder { params: CodecParameters, + core: crate::decode::Core, + pending: Option, +} + +impl Default for TrueHdDecoder { + fn default() -> Self { + Self::new() + } } impl TrueHdDecoder { @@ -269,25 +310,28 @@ impl TrueHdDecoder { pub fn new() -> TrueHdDecoder { TrueHdDecoder { params: CodecParameters::new(CodecId::TrueHd), + core: crate::decode::Core::new(), + pending: None, } } - /// One access unit in; parses its header and — TODO(H2): decode the - /// substreams (restart headers, FIR/IIR predictor filters, Huffman/LSB - /// residual entropy, channel matrixing, the lossless check) into PCM. - /// Until then this always answers [`Error::Unsupported`] once the header - /// itself parses cleanly, and any framing error before that. + /// The stream-side self-check tallies so far (restart CRC, parity, + /// lossless check); all stay zero on a clean decode. + pub fn check_stats(&self) -> crate::decode::CheckStats { + self.core.stats + } + + /// One access unit in; one frame of the highest presentation out — + /// `None` until the first major sync and every substream's first + /// restart header have been seen. pub fn push(&mut self, data: &[u8]) -> Result> { self.decode_access_unit(data) } /// See [`TrueHdDecoder::push`]. pub fn decode_access_unit(&mut self, data: &[u8]) -> Result> { - AccessUnitHeader::parse(data)?; - Err(Error::unsupported( - "TrueHD substream decode", - "substream decode pending", - )) + let header = AccessUnitHeader::parse(data)?; + self.core.decode(&header, data) } } @@ -297,22 +341,27 @@ impl Decoder for TrueHdDecoder { } fn send_packet(&mut self, packet: &Packet) -> Result<()> { - self.decode_access_unit(&packet.data)?; + if let Some(mut frame) = self.decode_access_unit(&packet.data)? { + frame.pts = packet + .pts + .map(|ticks| ec_core::timebase::Timestamp::new(ticks, packet.time_base)); + self.pending = Some(frame); + } Ok(()) } fn receive_frame(&mut self) -> Result { - // decode_access_unit never returns Ok, so send_packet never - // completes and this is unreachable in practice; NeedMore is the - // honest answer for "nothing queued" regardless. - Err(Error::NeedMore) + self.pending.take().map(Frame::Audio).ok_or(Error::NeedMore) } fn flush(&mut self) -> Result<()> { Ok(()) } - fn reset(&mut self) {} + fn reset(&mut self) { + self.pending = None; + self.core.reset(); + } } #[cfg(test)] @@ -417,9 +466,15 @@ mod tests { } #[test] - fn decoder_names_substream_decode_as_pending() { + fn decoder_yields_nothing_before_a_major_sync() { let mut decoder = TrueHdDecoder::new(); - let err = decoder.decode_access_unit(&stereo_au()).unwrap_err(); - assert!(matches!(err, Error::Unsupported { .. })); + assert_eq!(decoder.decode_access_unit(&stereo_au()).unwrap(), None); + } + + #[test] + fn substream_spans_follow_the_directory() { + let au = AccessUnitHeader::parse(&surround51_au()).unwrap(); + assert_eq!(au.substream_span(0), (36, 38)); + assert_eq!(au.substream_span(1), (38, 44)); } } diff --git a/crates/ec-truehd/tests/oracle.rs b/crates/ec-truehd/tests/oracle.rs new file mode 100644 index 00000000..ed91882c --- /dev/null +++ b/crates/ec-truehd/tests/oracle.rs @@ -0,0 +1,178 @@ +//! Bit-exactness against an external decoder: the first 60 s of a real 7.1 +//! Blu-ray TrueHD remux (3 substreams, noise type 1 matrices), and +//! externally encoded MLP/TrueHD streams (mono, stereo, 5.1 pink noise — +//! every codebook, matrices with the 2-channel noise generator, two +//! substreams). Both need `ffmpeg` on PATH and skip loudly without it; the +//! remux is a live discovery over `~/Downloads`. + +use std::fs::File; +use std::io::BufReader; +use std::path::{Path, PathBuf}; +use std::process::Command; + +use ec_core::{CodecId, Demuxer}; +use ec_matroska::MatroskaDemuxer; +use ec_truehd::{frame_length, TrueHdDecoder}; + +fn find_file() -> Option { + fn walk(dir: &Path, out: &mut Vec) { + let Ok(entries) = std::fs::read_dir(dir) else { return }; + for e in entries.flatten() { + let p = e.path(); + if p.is_dir() { + walk(&p, out); + } else { + out.push(p); + } + } + } + let home = std::env::var("HOME").ok()?; + let mut all = Vec::new(); + walk(&PathBuf::from(home).join("Downloads"), &mut all); + all.into_iter().find(|p| { + p.file_name() + .and_then(|n| n.to_str()) + .is_some_and(|n| n.contains("ReelR") && n.ends_with(".mkv")) + }) +} + +fn have_ffmpeg() -> bool { + Command::new("ffmpeg").arg("-version").output().map(|o| o.status.success()).unwrap_or(false) +} + +fn ffmpeg(args: &[&str]) -> Vec { + let out = Command::new("ffmpeg").args(["-v", "error", "-nostdin"]).args(args).output().expect("run ffmpeg"); + assert!(out.status.success(), "ffmpeg {args:?}: {}", String::from_utf8_lossy(&out.stderr)); + out.stdout +} + +fn s32le(bytes: &[u8]) -> Vec { + bytes.chunks_exact(4).map(|c| i32::from_le_bytes([c[0], c[1], c[2], c[3]])).collect() +} + +/// Compares `ours` with `reference` sample-for-sample; returns (exact, +/// total, first mismatch index). +fn compare(ours: &[i32], reference: &[i32]) -> (usize, usize, Option) { + let n = ours.len().min(reference.len()); + let mut exact = 0; + let mut first = None; + for i in 0..n { + if ours[i] == reference[i] { + exact += 1; + } else if first.is_none() { + first = Some(i); + } + } + (exact, n, first) +} + +#[test] +fn first_60_seconds_of_a_real_7_1_remux_are_bit_exact() { + let Some(path) = find_file() else { + eprintln!("skip: no 'ReelR ... TrueHD ...mkv' under ~/Downloads"); + return; + }; + if !have_ffmpeg() { + eprintln!("skip: ffmpeg not on PATH"); + return; + } + const SECONDS: usize = 60; + let mut demux = MatroskaDemuxer::new(BufReader::new(File::open(&path).unwrap())).unwrap(); + let track = demux.streams().iter().find(|s| s.params.codec == CodecId::TrueHd).expect("a TrueHD track"); + let stream_index = track.index; + + let reference = s32le(&ffmpeg(&[ + "-i", path.to_str().unwrap(), "-map", &format!("0:{stream_index}"), "-t", &SECONDS.to_string(), + "-f", "s32le", "-", + ])); + let channels = 8; + let wanted = SECONDS * 48_000 * channels; + + let mut decoder = TrueHdDecoder::new(); + let mut ours: Vec = Vec::with_capacity(wanted); + let mut aus = 0; + while ours.len() < wanted { + let packet = match demux.next_packet() { + Ok(p) => p, + Err(e) if e.is_eof() => break, + Err(e) => panic!("demux: {e}"), + }; + if packet.stream != stream_index { + continue; + } + let frame = decoder.decode_access_unit(&packet.data).unwrap_or_else(|e| panic!("AU {aus}: {e}")); + aus += 1; + if let Some(f) = frame { + assert_eq!(f.channels(), channels); + ours.extend(s32le(&f.data[0])); + } + } + let (exact, total, first) = compare(&ours, &reference); + let stats = decoder.check_stats(); + for ch in 0..channels { + let n = total / channels; + let e = (0..n).filter(|&i| ours[i * channels + ch] == reference[i * channels + ch]).count(); + eprintln!("channel {ch}: {e}/{n} exact"); + } + eprintln!( + "{aus} access units, {total} samples compared ({} s), {exact} exact ({:.4}%), first mismatch {first:?}, {stats:?}", + total / channels / 48_000, + 100.0 * exact as f64 / total as f64 + ); + assert!(total >= (SECONDS - 1) * 48_000 * channels, "only {total} samples"); + assert_eq!(stats.lossless_check_failures, 0, "{stats:?}"); + assert_eq!(stats.parity_failures, 0, "{stats:?}"); + assert_eq!(stats.restart_crc_failures, 0, "{stats:?}"); + assert_eq!(stats.length_mismatches, 0, "{stats:?}"); + assert!(exact as f64 >= 0.9999 * total as f64, "{exact}/{total} exact, first mismatch {first:?}"); +} + +/// An externally encoded MLP stream (raw access units) decoded by us must +/// match the external decoder's own output exactly; noise drives every +/// codebook and a wide range of LSB widths. +#[test] +fn externally_encoded_mlp_round_trips_bit_exact() { + if !have_ffmpeg() { + eprintln!("skip: ffmpeg not on PATH"); + return; + } + let dir = std::env::temp_dir().join(format!("ec-truehd-oracle-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let mut failures = Vec::new(); + for (codec, layout, channels) in [ + ("mlp", "mono", 1usize), ("mlp", "stereo", 2), ("mlp", "5.1", 6), + ("truehd", "mono", 1), ("truehd", "stereo", 2), ("truehd", "5.1", 6), + ] { + let name = format!("{codec}-{layout}"); + let mlp = dir.join(format!("{name}.{}", if codec == "mlp" { "mlp" } else { "thd" })); + let src = format!( + "anoisesrc=color=pink:amplitude=0.3:seed=7:sample_rate=48000:duration=3,aformat=sample_fmts=s32:channel_layouts={layout}" + ); + ffmpeg(&["-y", "-f", "lavfi", "-i", &src, "-c:a", codec, "-strict", "-2", mlp.to_str().unwrap()]); + let reference = s32le(&ffmpeg(&["-i", mlp.to_str().unwrap(), "-f", "s32le", "-"])); + let data = std::fs::read(&mlp).unwrap(); + + let mut decoder = TrueHdDecoder::new(); + let mut ours = Vec::new(); + let mut pos = 0; + while pos < data.len() { + let len = frame_length(&data[pos..]).unwrap(); + if let Some(f) = decoder.decode_access_unit(&data[pos..pos + len]).unwrap() { + assert_eq!(f.channels(), channels); + ours.extend(s32le(&f.data[0])); + } + pos += len; + } + let (exact, total, first) = compare(&ours, &reference); + let stats = decoder.check_stats(); + eprintln!("{name}: {exact}/{total} exact (ours {}, reference {}), first mismatch {first:?}, {stats:?}", ours.len(), reference.len()); + if stats.lossless_check_failures + stats.parity_failures + stats.restart_crc_failures != 0 + || exact != total + || total < 2 * 48_000 * channels + { + failures.push(format!("{name}: {exact}/{total}, first mismatch {first:?}, {stats:?}")); + } + } + let _ = std::fs::remove_dir_all(&dir); + assert!(failures.is_empty(), "{failures:#?}"); +}