Merge branch 'simplify/c-truehd'
Browse files at this commit7 changed files +202 -236
- crates/ec-mpegts/src/track.rs +8 -1
- crates/ec-mpegts/tests/it/truehd.rs +3 -1
- crates/ec-truehd/fuzz/fuzz_targets/parse_sync.rs +2 -2
- crates/ec-truehd/src/sync.rs +50 -52
- crates/ec-truehd/tests/it/atmos_synth.rs +131 -155
- crates/ec-truehd/tests/it/common.rs +3 -1
- crates/ec-truehd/tests/it/oracle.rs +5 -24
crates/ec-mpegts/src/track.rsmodified+8 -1
| @@ -90,6 +90,7 @@ pub(crate) struct TrueHdFacts { | |||
| 90 | pub rate: u32, | 90 | pub rate: u32, |
| 91 | pub layout: ChannelLayout, | 91 | pub layout: ChannelLayout, |
| 92 | pub samples: u32, | 92 | pub samples: u32, |
| 93 | + pub substreams: usize, | ||
| 93 | } | 94 | } |
| 94 | 95 | ||
| 95 | /// How the program-stream front end hands pictures over (see `psvideo`). | 96 | /// How the program-stream front end hands pictures over (see `psvideo`). |
| @@ -779,10 +780,15 @@ impl Track { | |||
| 779 | // The access unit goes on in the next PES. | 780 | // The access unit goes on in the next PES. |
| 780 | break; | 781 | break; |
| 781 | } | 782 | } |
| 782 | - let Ok(head) = AccessUnitHeader::parse(&rest[..len]) else { | 783 | + let streams = self.cfg.truehd.as_ref().map_or(0, |t| t.substreams); |
| 784 | + let Ok(head) = AccessUnitHeader::parse(&rest[..len], streams) else { | ||
| 783 | cur += 1; | 785 | cur += 1; |
| 784 | continue; | 786 | continue; |
| 785 | }; | 787 | }; |
| 788 | + if head.substreams.is_empty() { | ||
| 789 | + cur += 1; | ||
| 790 | + continue; | ||
| 791 | + } | ||
| 786 | if let Some(m) = head.major_sync | 792 | if let Some(m) = head.major_sync |
| 787 | && let Ok(layout) = head.channel_layout() | 793 | && let Ok(layout) = head.channel_layout() |
| 788 | { | 794 | { |
| @@ -791,6 +797,7 @@ impl Track { | |||
| 791 | layout, | 797 | layout, |
| 792 | // A major sync's rate code sizes the access units: 40 << n samples. | 798 | // A major sync's rate code sizes the access units: 40 << n samples. |
| 793 | samples: 40 << (m.rate_code & 7), | 799 | samples: 40 << (m.rate_code & 7), |
| 800 | + substreams: head.substreams.len(), | ||
| 794 | }); | 801 | }); |
| 795 | } | 802 | } |
| 796 | if cur >= new_start && !anchored && p.pts.is_some() { | 803 | if cur >= new_start && !anchored && p.pts.is_some() { |
crates/ec-mpegts/tests/it/truehd.rsmodified+3 -1
| @@ -76,10 +76,12 @@ fn truehd_in_ts_decodes_sample_exact_to_ffmpeg() { | |||
| 76 | assert!(!pkts.is_empty(), "{name}: no TrueHD access units"); | 76 | assert!(!pkts.is_empty(), "{name}: no TrueHD access units"); |
| 77 | 77 | ||
| 78 | // Every packet is one whole access unit: its length word covers it exactly. | 78 | // Every packet is one whole access unit: its length word covers it exactly. |
| 79 | + let mut substreams = 0; | ||
| 79 | for p in &pkts { | 80 | for p in &pkts { |
| 80 | - let head = AccessUnitHeader::parse(&p.data) | 81 | + let head = AccessUnitHeader::parse(&p.data, substreams) |
| 81 | .unwrap_or_else(|e| panic!("{name}: AU at {:#x}: {e}", p.off)); | 82 | .unwrap_or_else(|e| panic!("{name}: AU at {:#x}: {e}", p.off)); |
| 82 | assert_eq!(head.length, p.data.len(), "{name}: AU length"); | 83 | assert_eq!(head.length, p.data.len(), "{name}: AU length"); |
| 84 | + substreams = head.substreams.len(); | ||
| 83 | } | 85 | } |
| 84 | 86 | ||
| 85 | let mut dec = TrueHdDecoder::new(); | 87 | let mut dec = TrueHdDecoder::new(); |
crates/ec-truehd/fuzz/fuzz_targets/parse_sync.rsmodified+2 -2
| @@ -13,7 +13,7 @@ const MAX_BODY: usize = 8190 - 8; | |||
| 13 | fuzz_target!(|data: &[u8]| { | 13 | fuzz_target!(|data: &[u8]| { |
| 14 | // Raw bytes: the length word and, if it happens to match, the major sync. | 14 | // Raw bytes: the length word and, if it happens to match, the major sync. |
| 15 | let _ = frame_length(data); | 15 | let _ = frame_length(data); |
| 16 | - let _ = AccessUnitHeader::parse(data); | 16 | + let _ = AccessUnitHeader::parse(data, 0); |
| 17 | 17 | ||
| 18 | // Fixup: wrap the input in a valid AU header and TrueHD major sync word, so the | 18 | // Fixup: wrap the input in a valid AU header and TrueHD major sync word, so the |
| 19 | // mutations land on the sync fields and the directory behind them instead of on | 19 | // mutations land on the sync fields and the directory behind them instead of on |
| @@ -30,5 +30,5 @@ fuzz_target!(|data: &[u8]| { | |||
| 30 | return; | 30 | return; |
| 31 | }; | 31 | }; |
| 32 | au[..2].copy_from_slice(&(words & 0x0FFF).to_be_bytes()); | 32 | au[..2].copy_from_slice(&(words & 0x0FFF).to_be_bytes()); |
| 33 | - let _ = AccessUnitHeader::parse(&au); | 33 | + let _ = AccessUnitHeader::parse(&au, 0); |
| 34 | }); | 34 | }); |
crates/ec-truehd/src/sync.rsmodified+50 -52
| @@ -20,17 +20,6 @@ | |||
| 20 | //! - `data[4..8]`: `0xF8726FBA` (TrueHD) or `0xF8726FBB` (MLP) names a major | 20 | //! - `data[4..8]`: `0xF8726FBA` (TrueHD) or `0xF8726FBB` (MLP) names a major |
| 21 | //! sync at this access unit; absent on most access units, present roughly | 21 | //! sync at this access unit; absent on most access units, present roughly |
| 22 | //! once per video frame. | 22 | //! once per video frame. |
| 23 | -//! - When a major sync is present, it occupies exactly 28 bytes starting at | ||
| 24 | -//! `data[4]` (format id + 24 more bytes, ending where the substream | ||
| 25 | -//! directory begins): `data[8]` packs two 4-bit sample-rate codes, | ||
| 26 | -//! `group1` in the high nibble and `group2` (a secondary/backward-compat | ||
| 27 | -//! rate, `0xF` when unused) in the low nibble. A code maps to a rate via | ||
| 28 | -//! [`sample_rate`]; codes 0-2 are the 48 kHz family (`48000 * 2^n`), codes | ||
| 29 | -//! 8-10 the 44.1 kHz family (`44100 * 2^n`). The rest of the major sync | ||
| 30 | -//! (channel-assignment bitfields, flags) is skipped: this build does not | ||
| 31 | -//! independently verify their bit positions, so [`ChannelLayout`] is | ||
| 32 | -//! derived from the substream *directory* instead (see below), which is | ||
| 33 | -//! fully verified against the sample file. | ||
| 34 | //! - The substream directory starts right after the major sync (or right | 23 | //! - The substream directory starts right after the major sync (or right |
| 35 | //! after the 4-byte access-unit header, on an access unit with none), one | 24 | //! after the 4-byte access-unit header, on an access unit with none), one |
| 36 | //! [`SubstreamInfo`] entry per substream: a 2-byte word whose top 4 bits | 25 | //! [`SubstreamInfo`] entry per substream: a 2-byte word whose top 4 bits |
| @@ -38,11 +27,7 @@ | |||
| 38 | //! word, [`SubstreamInfo::extra`] — follows) and whose low 12 bits are the | 27 | //! word, [`SubstreamInfo::extra`] — follows) and whose low 12 bits are the |
| 39 | //! substream's *end pointer*: the cumulative byte offset, in 16-bit words | 28 | //! substream's *end pointer*: the cumulative byte offset, in 16-bit words |
| 40 | //! from the end of the whole directory, where this substream's coded data | 29 | //! from the end of the whole directory, where this substream's coded data |
| 41 | -//! ends. There is no explicit substream-count field; a directory entry is | 30 | +//! ends. |
| 42 | -//! the *last* one exactly when `directory_end + end_pointer_bytes` lands | ||
| 43 | -//! on the access unit's own total length — which is how | ||
| 44 | -//! [`AccessUnitHeader::parse`] finds the count, bounded at 4 entries | ||
| 45 | -//! (Dolby's own substream cap). | ||
| 46 | //! | 31 | //! |
| 47 | //! Substream count maps to [`ChannelLayout`] by Dolby's own published | 32 | //! Substream count maps to [`ChannelLayout`] by Dolby's own published |
| 48 | //! substream convention — substream 0 always carries a 2-ch presentation, | 33 | //! substream convention — substream 0 always carries a 2-ch presentation, |
| @@ -66,8 +51,6 @@ pub const MAJOR_SYNC_TRUEHD: u32 = 0xF872_6FBA; | |||
| 66 | /// MLP's major sync word (lossless-only, no TrueHD extensions). | 51 | /// MLP's major sync word (lossless-only, no TrueHD extensions). |
| 67 | pub const MAJOR_SYNC_MLP: u32 = 0xF872_6FBB; | 52 | pub const MAJOR_SYNC_MLP: u32 = 0xF872_6FBB; |
| 68 | 53 | ||
| 69 | -/// Bytes a major sync occupies, format id included, ending where the | ||
| 70 | -/// substream directory begins. | ||
| 71 | const MAJOR_SYNC_LEN: usize = 28; | 54 | const MAJOR_SYNC_LEN: usize = 28; |
| 72 | 55 | ||
| 73 | /// Dolby's cap on substream directory entries per access unit: entries 0..=2 | 56 | /// Dolby's cap on substream directory entries per access unit: entries 0..=2 |
| @@ -123,6 +106,10 @@ pub struct MajorSyncInfo { | |||
| 123 | /// L/R, 1 = C, 2 = LFE, 3 = Ls/Rs, 6 = Lrs/Rrs; `0x4F` is standard 7.1); | 106 | /// L/R, 1 = C, 2 = LFE, 3 = Ls/Rs, 6 = Lrs/Rrs; `0x4F` is standard 7.1); |
| 124 | /// 0 when the major sync is plain MLP, which has no such field. | 107 | /// 0 when the major sync is plain MLP, which has no such field. |
| 125 | pub ch8_assignment: u16, | 108 | pub ch8_assignment: u16, |
| 109 | + #[allow(missing_docs)] | ||
| 110 | + pub num_substreams: usize, | ||
| 111 | + #[allow(missing_docs)] | ||
| 112 | + pub len: usize, | ||
| 126 | } | 113 | } |
| 127 | 114 | ||
| 128 | /// A rate code's `44.1k*2^n` / `48k*2^n` value, or [`Error::Unsupported`] for | 115 | /// A rate code's `44.1k*2^n` / `48k*2^n` value, or [`Error::Unsupported`] for |
| @@ -164,11 +151,19 @@ impl MajorSyncInfo { | |||
| 164 | MajorSyncFormat::TrueHd => (u16::from(data[6] & 0x1F) << 8) | u16::from(data[7]), | 151 | MajorSyncFormat::TrueHd => (u16::from(data[6] & 0x1F) << 8) | u16::from(data[7]), |
| 165 | MajorSyncFormat::Mlp => 0, | 152 | MajorSyncFormat::Mlp => 0, |
| 166 | }; | 153 | }; |
| 154 | + let len = match format { | ||
| 155 | + MajorSyncFormat::TrueHd if data[25] & 1 != 0 => { | ||
| 156 | + MAJOR_SYNC_LEN + 2 + 2 * usize::from(data[26] >> 4) | ||
| 157 | + } | ||
| 158 | + _ => MAJOR_SYNC_LEN, | ||
| 159 | + }; | ||
| 167 | Ok(MajorSyncInfo { | 160 | Ok(MajorSyncInfo { |
| 168 | format, | 161 | format, |
| 169 | sample_rate, | 162 | sample_rate, |
| 170 | rate_code, | 163 | rate_code, |
| 171 | ch8_assignment, | 164 | ch8_assignment, |
| 165 | + num_substreams: usize::from(data[16] >> 4), | ||
| 166 | + len, | ||
| 172 | }) | 167 | }) |
| 173 | } | 168 | } |
| 174 | } | 169 | } |
| @@ -210,7 +205,7 @@ pub struct AccessUnitHeader { | |||
| 210 | impl AccessUnitHeader { | 205 | impl AccessUnitHeader { |
| 211 | /// Parses one access unit's header from `data`, which must hold at least | 206 | /// Parses one access unit's header from `data`, which must hold at least |
| 212 | /// [`frame_length`]'s worth of bytes. | 207 | /// [`frame_length`]'s worth of bytes. |
| 213 | - pub fn parse(data: &[u8]) -> Result<AccessUnitHeader> { | 208 | + pub fn parse(data: &[u8], stream_substreams: usize) -> Result<AccessUnitHeader> { |
| 214 | let length = frame_length(data)?; | 209 | let length = frame_length(data)?; |
| 215 | if data.len() < length { | 210 | if data.len() < length { |
| 216 | return Err(Error::NeedMore); | 211 | return Err(Error::NeedMore); |
| @@ -226,21 +221,18 @@ impl AccessUnitHeader { | |||
| 226 | } | 221 | } |
| 227 | _ => None, | 222 | _ => None, |
| 228 | }; | 223 | }; |
| 229 | - let dir_start = AU_HEADER_LEN | 224 | + if major_sync.is_some_and(|s| s.num_substreams == 0) { |
| 230 | - + if major_sync.is_some() { | 225 | + return Err(Error::corrupt("TrueHD: major sync declares no substreams")); |
| 231 | - MAJOR_SYNC_LEN | 226 | + } |
| 232 | - } else { | 227 | + let count = major_sync.map_or(stream_substreams, |s| s.num_substreams); |
| 233 | - 0 | 228 | + if count > MAX_DIRECTORY_ENTRIES { |
| 234 | - }; | 229 | + return Err(Error::corrupt( |
| 235 | - | 230 | + "TrueHD: substream directory has more than 4 entries", |
| 236 | - let mut substreams = Vec::new(); | 231 | + )); |
| 237 | - let mut pos = dir_start; | 232 | + } |
| 238 | - loop { | 233 | + let mut substreams = Vec::with_capacity(count); |
| 239 | - if substreams.len() == MAX_DIRECTORY_ENTRIES { | 234 | + let mut pos = AU_HEADER_LEN + major_sync.map_or(0, |s| s.len); |
| 240 | - return Err(Error::corrupt( | 235 | + for _ in 0..count { |
| 241 | - "TrueHD: substream directory has more than 4 entries", | ||
| 242 | - )); | ||
| 243 | - } | ||
| 244 | let word = au.get(pos..pos + 2).ok_or(Error::NeedMore)?; | 236 | let word = au.get(pos..pos + 2).ok_or(Error::NeedMore)?; |
| 245 | let word = u16::from_be_bytes([word[0], word[1]]); | 237 | let word = u16::from_be_bytes([word[0], word[1]]); |
| 246 | let flags = (word >> 12) as u8; | 238 | let flags = (word >> 12) as u8; |
| @@ -258,14 +250,14 @@ impl AccessUnitHeader { | |||
| 258 | end_offset_words, | 250 | end_offset_words, |
| 259 | extra, | 251 | extra, |
| 260 | }); | 252 | }); |
| 261 | - if pos + usize::from(end_offset_words) * 2 == length { | 253 | + } |
| 262 | - break; | 254 | + if substreams |
| 263 | - } | 255 | + .iter() |
| 264 | - if pos >= length { | 256 | + .any(|s| pos + usize::from(s.end_offset_words) * 2 > length) |
| 265 | - return Err(Error::corrupt( | 257 | + { |
| 266 | - "TrueHD: substream directory did not sum to the access unit's length", | 258 | + return Err(Error::corrupt( |
| 267 | - )); | 259 | + "TrueHD: substream data runs past the access unit", |
| 268 | - } | 260 | + )); |
| 269 | } | 261 | } |
| 270 | 262 | ||
| 271 | Ok(AccessUnitHeader { | 263 | Ok(AccessUnitHeader { |
| @@ -314,6 +306,7 @@ pub struct TrueHdDecoder { | |||
| 314 | params: CodecParameters, | 306 | params: CodecParameters, |
| 315 | core: crate::decode::Core, | 307 | core: crate::decode::Core, |
| 316 | pending: Option<AudioFrame>, | 308 | pending: Option<AudioFrame>, |
| 309 | + substream_count: usize, | ||
| 317 | } | 310 | } |
| 318 | 311 | ||
| 319 | impl Default for TrueHdDecoder { | 312 | impl Default for TrueHdDecoder { |
| @@ -331,6 +324,7 @@ impl TrueHdDecoder { | |||
| 331 | params: CodecParameters::new(CodecId::TrueHd), | 324 | params: CodecParameters::new(CodecId::TrueHd), |
| 332 | core: crate::decode::Core::new(), | 325 | core: crate::decode::Core::new(), |
| 333 | pending: None, | 326 | pending: None, |
| 327 | + substream_count: 0, | ||
| 334 | } | 328 | } |
| 335 | } | 329 | } |
| 336 | 330 | ||
| @@ -349,7 +343,8 @@ impl TrueHdDecoder { | |||
| 349 | 343 | ||
| 350 | /// See [`TrueHdDecoder::push`]. | 344 | /// See [`TrueHdDecoder::push`]. |
| 351 | pub fn decode_access_unit(&mut self, data: &[u8]) -> Result<Option<AudioFrame>> { | 345 | pub fn decode_access_unit(&mut self, data: &[u8]) -> Result<Option<AudioFrame>> { |
| 352 | - let header = AccessUnitHeader::parse(data)?; | 346 | + let header = AccessUnitHeader::parse(data, self.substream_count)?; |
| 347 | + self.substream_count = header.substreams.len(); | ||
| 353 | self.core.decode(&header, data) | 348 | self.core.decode(&header, data) |
| 354 | } | 349 | } |
| 355 | } | 350 | } |
| @@ -379,6 +374,7 @@ impl Decoder for TrueHdDecoder { | |||
| 379 | 374 | ||
| 380 | fn reset(&mut self) { | 375 | fn reset(&mut self) { |
| 381 | self.pending = None; | 376 | self.pending = None; |
| 377 | + self.substream_count = 0; | ||
| 382 | self.core.reset(); | 378 | self.core.reset(); |
| 383 | } | 379 | } |
| 384 | } | 380 | } |
| @@ -409,7 +405,9 @@ mod tests { | |||
| 409 | ]; | 405 | ]; |
| 410 | au.extend_from_slice(&MAJOR_SYNC_TRUEHD.to_be_bytes()); | 406 | au.extend_from_slice(&MAJOR_SYNC_TRUEHD.to_be_bytes()); |
| 411 | au.push(0x0F); // sample rate: group1 code 0 (48000), group2 0xF (absent) | 407 | au.push(0x0F); // sample rate: group1 code 0 (48000), group2 0xF (absent) |
| 412 | - au.extend(std::iter::repeat_n(0u8, 23)); // rest of the major sync, unused here | 408 | + let mut rest = [0u8; 23]; |
| 409 | + rest[11] = 0x20; | ||
| 410 | + au.extend_from_slice(&rest); | ||
| 413 | au.extend_from_slice(&[0x00, 0x01]); // substream 0: end +1 word (not the last) | 411 | au.extend_from_slice(&[0x00, 0x01]); // substream 0: end +1 word (not the last) |
| 414 | au.extend_from_slice(&[0x00, 0x04]); // substream 1: end +4 words (lands on EOF) | 412 | au.extend_from_slice(&[0x00, 0x04]); // substream 1: end +4 words (lands on EOF) |
| 415 | au.extend_from_slice(&[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88]); | 413 | au.extend_from_slice(&[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88]); |
| @@ -430,7 +428,7 @@ mod tests { | |||
| 430 | 428 | ||
| 431 | #[test] | 429 | #[test] |
| 432 | fn parses_a_stereo_access_unit_with_no_major_sync() { | 430 | fn parses_a_stereo_access_unit_with_no_major_sync() { |
| 433 | - let au = AccessUnitHeader::parse(&stereo_au()).unwrap(); | 431 | + let au = AccessUnitHeader::parse(&stereo_au(), 1).unwrap(); |
| 434 | assert_eq!(au.length, 10); | 432 | assert_eq!(au.length, 10); |
| 435 | assert_eq!(au.input_timing, 0x1234); | 433 | assert_eq!(au.input_timing, 0x1234); |
| 436 | assert!(au.major_sync.is_none()); | 434 | assert!(au.major_sync.is_none()); |
| @@ -440,7 +438,7 @@ mod tests { | |||
| 440 | 438 | ||
| 441 | #[test] | 439 | #[test] |
| 442 | fn parses_a_5_1_access_unit_with_a_major_sync() { | 440 | fn parses_a_5_1_access_unit_with_a_major_sync() { |
| 443 | - let au = AccessUnitHeader::parse(&surround51_au()).unwrap(); | 441 | + let au = AccessUnitHeader::parse(&surround51_au(), 0).unwrap(); |
| 444 | assert_eq!(au.length, 44); | 442 | assert_eq!(au.length, 44); |
| 445 | let sync = au.major_sync.expect("major sync detected"); | 443 | let sync = au.major_sync.expect("major sync detected"); |
| 446 | assert_eq!(sync.format, MajorSyncFormat::TrueHd); | 444 | assert_eq!(sync.format, MajorSyncFormat::TrueHd); |
| @@ -457,7 +455,7 @@ mod tests { | |||
| 457 | // major-sync bytes as if they were directory entries and cannot | 455 | // major-sync bytes as if they were directory entries and cannot |
| 458 | // land on the access unit's real end, but must not panic. | 456 | // land on the access unit's real end, but must not panic. |
| 459 | au[5] = 0x00; | 457 | au[5] = 0x00; |
| 460 | - assert!(AccessUnitHeader::parse(&au).is_err()); | 458 | + assert!(AccessUnitHeader::parse(&au, 2).is_err()); |
| 461 | } | 459 | } |
| 462 | 460 | ||
| 463 | #[test] | 461 | #[test] |
| @@ -465,7 +463,7 @@ mod tests { | |||
| 465 | let au = surround51_au(); | 463 | let au = surround51_au(); |
| 466 | // The header claims 44 bytes; hand over fewer. | 464 | // The header claims 44 bytes; hand over fewer. |
| 467 | assert!(matches!( | 465 | assert!(matches!( |
| 468 | - AccessUnitHeader::parse(&au[..20]), | 466 | + AccessUnitHeader::parse(&au[..20], 0), |
| 469 | Err(Error::NeedMore) | 467 | Err(Error::NeedMore) |
| 470 | )); | 468 | )); |
| 471 | } | 469 | } |
| @@ -480,7 +478,7 @@ mod tests { | |||
| 480 | au.extend_from_slice(&[0x00, 0x01]); | 478 | au.extend_from_slice(&[0x00, 0x01]); |
| 481 | } | 479 | } |
| 482 | au.resize(20, 0); | 480 | au.resize(20, 0); |
| 483 | - let err = AccessUnitHeader::parse(&au).unwrap_err(); | 481 | + let err = AccessUnitHeader::parse(&au, 5).unwrap_err(); |
| 484 | assert!(matches!(err, Error::Corrupt { .. }), "{err:?}"); | 482 | assert!(matches!(err, Error::Corrupt { .. }), "{err:?}"); |
| 485 | } | 483 | } |
| 486 | 484 | ||
| @@ -497,7 +495,7 @@ mod tests { | |||
| 497 | au.extend_from_slice(&[0x00, 0x01]); // substream 2: doesn't land on EOF yet | 495 | au.extend_from_slice(&[0x00, 0x01]); // substream 2: doesn't land on EOF yet |
| 498 | au.extend_from_slice(&[0x00, 0x04]); // substream 3: pos(12) + 4*2 == 20, lands on EOF | 496 | au.extend_from_slice(&[0x00, 0x04]); // substream 3: pos(12) + 4*2 == 20, lands on EOF |
| 499 | au.resize(20, 0); | 497 | au.resize(20, 0); |
| 500 | - let parsed = AccessUnitHeader::parse(&au).unwrap(); | 498 | + let parsed = AccessUnitHeader::parse(&au, 4).unwrap(); |
| 501 | assert_eq!(parsed.substreams.len(), 4); | 499 | assert_eq!(parsed.substreams.len(), 4); |
| 502 | // Entry 3's coded data is the object-audio span the decoder skips. | 500 | // Entry 3's coded data is the object-audio span the decoder skips. |
| 503 | assert_eq!(parsed.substream_span(3), (14, 20)); | 501 | assert_eq!(parsed.substream_span(3), (14, 20)); |
| @@ -513,7 +511,7 @@ mod tests { | |||
| 513 | au.extend_from_slice(&[0x00, 0x01]); // substream 1: doesn't land on EOF yet | 511 | au.extend_from_slice(&[0x00, 0x01]); // substream 1: doesn't land on EOF yet |
| 514 | au.extend_from_slice(&[0x00, 0x05]); // substream 2: pos(10) + 5*2 == 20, lands on EOF | 512 | au.extend_from_slice(&[0x00, 0x05]); // substream 2: pos(10) + 5*2 == 20, lands on EOF |
| 515 | au.resize(20, 0); | 513 | au.resize(20, 0); |
| 516 | - let parsed = AccessUnitHeader::parse(&au).unwrap(); | 514 | + let parsed = AccessUnitHeader::parse(&au, 3).unwrap(); |
| 517 | assert_eq!(parsed.substreams.len(), 3); | 515 | assert_eq!(parsed.substreams.len(), 3); |
| 518 | assert_eq!(parsed.channel_layout().unwrap(), ChannelLayout::Surround7_1); | 516 | assert_eq!(parsed.channel_layout().unwrap(), ChannelLayout::Surround7_1); |
| 519 | } | 517 | } |
| @@ -526,7 +524,7 @@ mod tests { | |||
| 526 | 524 | ||
| 527 | #[test] | 525 | #[test] |
| 528 | fn substream_spans_follow_the_directory() { | 526 | fn substream_spans_follow_the_directory() { |
| 529 | - let au = AccessUnitHeader::parse(&surround51_au()).unwrap(); | 527 | + let au = AccessUnitHeader::parse(&surround51_au(), 0).unwrap(); |
| 530 | assert_eq!(au.substream_span(0), (36, 38)); | 528 | assert_eq!(au.substream_span(0), (36, 38)); |
| 531 | assert_eq!(au.substream_span(1), (38, 44)); | 529 | assert_eq!(au.substream_span(1), (38, 44)); |
| 532 | } | 530 | } |
crates/ec-truehd/tests/it/atmos_synth.rsmodified+131 -155
| @@ -1,34 +1,3 @@ | |||
| 1 | -//! Atmos backward compatibility, tested on a synthetic 4-substream stream. | ||
| 2 | -//! | ||
| 3 | -//! [`with_object_substream`] rebuilds it with a 4th directory entry: the | ||
| 4 | -//! object-audio substream, here a run of [`FILLER_WORDS`] stand-in words | ||
| 5 | -//! appended after substream 2's coded data. The rebuilt access unit is: | ||
| 6 | -//! | ||
| 7 | -//! - header: length word (new word count, parity nibble recomputed) and the | ||
| 8 | -//! original input timing; | ||
| 9 | -//! - major sync, when the original has one: `num_substreams` (top nibble of | ||
| 10 | -//! byte 16) set to 4, and the major sync's own CRC-16 (bytes 26..28) | ||
| 11 | -//! recomputed; | ||
| 12 | -//! - directory: the original three entries unchanged (their cumulative end | ||
| 13 | -//! pointers are relative to the directory end, so the coded data they name | ||
| 14 | -//! does not move relative to it), then a fourth entry whose end pointer is | ||
| 15 | -//! the old last pointer plus the filler length; | ||
| 16 | -//! - substream data: the original substreams 0..2 bytes verbatim, then the | ||
| 17 | -//! filler. | ||
| 18 | -//! | ||
| 19 | -//! Decoding the synthetic stream must therefore equal decoding the original | ||
| 20 | -//! bit-exactly: the 8-channel (7.1) presentation is the one decoded and | ||
| 21 | -//! output, substream 3 is skipped via its end pointer, and the parity and | ||
| 22 | -//! checksum of substreams 0..2 are still checked. Object audio itself is out | ||
| 23 | -//! of scope and never decoded, so the filler's contents must not matter; a | ||
| 24 | -//! second test checks that. | ||
| 25 | -//! | ||
| 26 | -//! Optional cross-check: with `EC_ATMOS_SYNTH_OUT=<path>` set, the synthetic | ||
| 27 | -//! access units are also written there as a raw TrueHD elementary stream, so | ||
| 28 | -//! `ffmpeg -f truehd -i <path> -f s32le` can be compared against this | ||
| 29 | -//! decoder's output (ffmpeg decodes the same 8-channel presentation). | ||
| 30 | -//! | ||
| 31 | - | ||
| 32 | use std::path::Path; | 1 | use std::path::Path; |
| 33 | 2 | ||
| 34 | use ec_core::frame::ChannelLayout; | 3 | use ec_core::frame::ChannelLayout; |
| @@ -37,27 +6,26 @@ use ec_truehd::sync::AccessUnitHeader; | |||
| 37 | 6 | ||
| 38 | use crate::common::raw_access_units; | 7 | use crate::common::raw_access_units; |
| 39 | 8 | ||
| 40 | -/// major sync among them. | 9 | +const MIN_ACCESS_UNITS: usize = 100; |
| 41 | -const AUS: usize = 1200; | ||
| 42 | -/// Length of the object-audio stand-in, in 16-bit words. | ||
| 43 | const FILLER_WORDS: u16 = 4; | 10 | const FILLER_WORDS: u16 = 4; |
| 44 | -/// The 12-bit access-unit length word's maximum, in 16-bit words. | ||
| 45 | const MAX_AU_WORDS: usize = 0x0FFF; | 11 | const MAX_AU_WORDS: usize = 0x0FFF; |
| 46 | -const MAJOR_SYNC_LEN: usize = 28; | ||
| 47 | 12 | ||
| 48 | -fn truehd_access_units() -> Vec<Vec<u8>> { | 13 | +fn sample_access_units() -> Vec<Vec<u8>> { |
| 49 | let data = | 14 | let data = |
| 50 | std::fs::read(ec_testkit::fixture("open/fate/truehd/atmos.thd")).expect("read atmos.thd"); | 15 | std::fs::read(ec_testkit::fixture("open/fate/truehd/atmos.thd")).expect("read atmos.thd"); |
| 51 | let aus = raw_access_units(&data); | 16 | let aus = raw_access_units(&data); |
| 52 | assert!( | 17 | assert!( |
| 53 | - aus.len() >= AUS, | 18 | + aus.len() >= MIN_ACCESS_UNITS, |
| 54 | - "atmos.thd has fewer than {AUS} access units" | 19 | + "atmos.thd has only {} access units", |
| 20 | + aus.len() | ||
| 55 | ); | 21 | ); |
| 56 | - aus.into_iter().take(AUS).collect() | 22 | + aus |
| 23 | +} | ||
| 24 | + | ||
| 25 | +fn header_of(au: &[u8], substreams: usize) -> AccessUnitHeader { | ||
| 26 | + AccessUnitHeader::parse(au, substreams).expect("access unit parses") | ||
| 57 | } | 27 | } |
| 58 | 28 | ||
| 59 | -/// The major sync's CRC-16 (poly `0x002D`, MSB-first table form), the same | ||
| 60 | -/// one the crate's encoder writes and ffmpeg checks. | ||
| 61 | fn crc16_table() -> [u32; 256] { | 29 | fn crc16_table() -> [u32; 256] { |
| 62 | let mut table = [0u32; 256]; | 30 | let mut table = [0u32; 256]; |
| 63 | for (i, entry) in table.iter_mut().enumerate() { | 31 | for (i, entry) in table.iter_mut().enumerate() { |
| @@ -71,107 +39,131 @@ fn crc16_table() -> [u32; 256] { | |||
| 71 | table | 39 | table |
| 72 | } | 40 | } |
| 73 | 41 | ||
| 74 | -/// The major sync's checksum field value: CRC-16 over bytes `0..24`, XORed | ||
| 75 | -/// with the little-endian word at `24..26`, stored little-endian at `26..28`. | ||
| 76 | fn major_sync_checksum(sync: &[u8]) -> u16 { | 42 | fn major_sync_checksum(sync: &[u8]) -> u16 { |
| 43 | + let n = sync.len() - 4; | ||
| 77 | let table = crc16_table(); | 44 | let table = crc16_table(); |
| 78 | let mut crc: u32 = 0; | 45 | let mut crc: u32 = 0; |
| 79 | - for &b in &sync[..24] { | 46 | + for &b in &sync[..n] { |
| 80 | crc = table[usize::from((crc as u8) ^ b)] ^ (crc >> 8); | 47 | crc = table[usize::from((crc as u8) ^ b)] ^ (crc >> 8); |
| 81 | } | 48 | } |
| 82 | - (crc as u16) ^ u16::from_le_bytes([sync[24], sync[25]]) | 49 | + (crc as u16) ^ u16::from_le_bytes([sync[n], sync[n + 1]]) |
| 50 | +} | ||
| 51 | + | ||
| 52 | +fn checksum_is_valid(sync: &[u8]) -> bool { | ||
| 53 | + let n = sync.len() - 2; | ||
| 54 | + major_sync_checksum(sync) == u16::from_le_bytes([sync[n], sync[n + 1]]) | ||
| 55 | +} | ||
| 56 | + | ||
| 57 | +fn set_substream_count(sync: &mut [u8], count: u8) { | ||
| 58 | + sync[16] = (sync[16] & 0x0F) | (count << 4); | ||
| 59 | + let checksum = major_sync_checksum(sync); | ||
| 60 | + let n = sync.len() - 2; | ||
| 61 | + sync[n..].copy_from_slice(&checksum.to_le_bytes()); | ||
| 83 | } | 62 | } |
| 84 | 63 | ||
| 85 | -/// The access-unit header's parity fold: the 4 header bytes and every | ||
| 86 | -/// directory byte, XORed together, must leave a nibble pair that is bitwise | ||
| 87 | -/// complementary (`((p >> 4) ^ p) & 0xF == 0xF`). | ||
| 88 | fn parity_ok(header: &[u8], directory: &[u8]) -> bool { | 64 | fn parity_ok(header: &[u8], directory: &[u8]) -> bool { |
| 89 | let p = header.iter().chain(directory).fold(0u8, |a, &b| a ^ b); | 65 | let p = header.iter().chain(directory).fold(0u8, |a, &b| a ^ b); |
| 90 | ((p >> 4) ^ p) & 0xF == 0xF | 66 | ((p >> 4) ^ p) & 0xF == 0xF |
| 91 | } | 67 | } |
| 92 | 68 | ||
| 93 | -/// Rebuilds one access unit of the 7.1 remux with a 4th (object-audio) | 69 | +fn assemble(timing: [u8; 2], sync: Option<&[u8]>, dir: &[u8], coded: &[u8]) -> Vec<u8> { |
| 94 | -/// directory entry whose coded data is `FILLER_WORDS` words of `filler`. | ||
| 95 | -fn with_object_substream(au: &[u8], filler: u8) -> Vec<u8> { | ||
| 96 | - let header = AccessUnitHeader::parse(au).expect("the original access unit parses"); | ||
| 97 | - assert_eq!( | ||
| 98 | - header.substreams.len(), | ||
| 99 | - 3, | ||
| 100 | - "the remux is a 3-substream 7.1 stream" | ||
| 101 | - ); | ||
| 102 | - let sync_len = if header.major_sync.is_some() { | ||
| 103 | - MAJOR_SYNC_LEN | ||
| 104 | - } else { | ||
| 105 | - 0 | ||
| 106 | - }; | ||
| 107 | - let dir_start = 4 + sync_len; | ||
| 108 | - let old_dir = &au[dir_start..header.data_start]; | ||
| 109 | - let old_last_end = header.substreams[2].end_offset_words; | ||
| 110 | - let new_last_end = old_last_end + FILLER_WORDS; | ||
| 111 | - | ||
| 112 | - // The original directory bytes are reused verbatim. The new entry is a | ||
| 113 | - // plain 2-byte word: no extra word, no check data, and the non-restart bit | ||
| 114 | - // (0x4) copied from substream 2. That bit is set exactly on access units | ||
| 115 | - // without a major sync, and ffmpeg rejects an entry that disagrees with | ||
| 116 | - // the access unit's major sync. Its end pointer is cumulative. | ||
| 117 | - let nonrestart = header.substreams[2].flags & 0x4; | ||
| 118 | - let mut dir = old_dir.to_vec(); | ||
| 119 | - let entry = (u16::from(nonrestart) << 12) | new_last_end; | ||
| 120 | - dir.extend_from_slice(&entry.to_be_bytes()); | ||
| 121 | - | ||
| 122 | - let coded = &au[header.data_start..header.length]; | ||
| 123 | - let filler_bytes = usize::from(FILLER_WORDS) * 2; | ||
| 124 | - | ||
| 125 | let mut body = Vec::new(); | 70 | let mut body = Vec::new(); |
| 126 | - if header.major_sync.is_some() { | 71 | + body.extend_from_slice(sync.unwrap_or(&[])); |
| 127 | - let mut sync = au[4..4 + MAJOR_SYNC_LEN].to_vec(); | 72 | + body.extend_from_slice(dir); |
| 128 | - assert_eq!( | ||
| 129 | - u16::from_le_bytes([sync[26], sync[27]]), | ||
| 130 | - major_sync_checksum(&sync), | ||
| 131 | - "the remux's major sync checksum matches the formula" | ||
| 132 | - ); | ||
| 133 | - sync[16] = (sync[16] & 0x0F) | (4 << 4); // num_substreams = 4 | ||
| 134 | - let checksum = major_sync_checksum(&sync); | ||
| 135 | - sync[26..28].copy_from_slice(&checksum.to_le_bytes()); | ||
| 136 | - body.extend_from_slice(&sync); | ||
| 137 | - } | ||
| 138 | - body.extend_from_slice(&dir); | ||
| 139 | body.extend_from_slice(coded); | 73 | body.extend_from_slice(coded); |
| 140 | - body.extend(std::iter::repeat_n(filler, filler_bytes)); | 74 | + let words = (4 + body.len()) / 2; |
| 141 | - | ||
| 142 | - let total = 4 + body.len(); | ||
| 143 | - let words = total / 2; | ||
| 144 | assert!(words <= MAX_AU_WORDS, "synthetic access unit too long"); | 75 | assert!(words <= MAX_AU_WORDS, "synthetic access unit too long"); |
| 145 | - let timing = [au[2], au[3]]; | ||
| 146 | - // The parity nibble is the one value that makes the fold complementary. | ||
| 147 | let nibble = (0u8..16) | 76 | let nibble = (0u8..16) |
| 148 | .find(|&cn| { | 77 | .find(|&cn| { |
| 149 | - let len_hi = (words >> 8) as u8 & 0x0F; | 78 | + let head = [ |
| 150 | - let len_lo = (words & 0xFF) as u8; | 79 | + (cn << 4) | ((words >> 8) as u8 & 0x0F), |
| 151 | - let head = [(cn << 4) | len_hi, len_lo, timing[0], timing[1]]; | 80 | + (words & 0xFF) as u8, |
| 152 | - parity_ok(&head, &dir) | 81 | + timing[0], |
| 82 | + timing[1], | ||
| 83 | + ]; | ||
| 84 | + parity_ok(&head, dir) | ||
| 153 | }) | 85 | }) |
| 154 | .expect("one of the 16 nibble values satisfies the fold"); | 86 | .expect("one of the 16 nibble values satisfies the fold"); |
| 155 | let word = (u16::from(nibble) << 12) | words as u16; | 87 | let word = (u16::from(nibble) << 12) | words as u16; |
| 156 | - | 88 | + let mut out = Vec::with_capacity(4 + body.len()); |
| 157 | - // Sanity: the same fold rule holds for the original access unit. | ||
| 158 | - assert!( | ||
| 159 | - parity_ok(&au[..4], old_dir), | ||
| 160 | - "the remux's own parity fold holds" | ||
| 161 | - ); | ||
| 162 | - | ||
| 163 | - let mut out = Vec::with_capacity(total); | ||
| 164 | out.extend_from_slice(&word.to_be_bytes()); | 89 | out.extend_from_slice(&word.to_be_bytes()); |
| 165 | out.extend_from_slice(&timing); | 90 | out.extend_from_slice(&timing); |
| 166 | out.extend_from_slice(&body); | 91 | out.extend_from_slice(&body); |
| 167 | out | 92 | out |
| 168 | } | 93 | } |
| 169 | 94 | ||
| 170 | -/// One decoded frame: its layout and its planes. | 95 | +fn sync_of(au: &[u8], header: &AccessUnitHeader) -> Option<Vec<u8>> { |
| 96 | + header.major_sync.map(|s| au[4..4 + s.len].to_vec()) | ||
| 97 | +} | ||
| 98 | + | ||
| 99 | +fn directory_start(header: &AccessUnitHeader) -> usize { | ||
| 100 | + 4 + header.major_sync.map_or(0, |s| s.len) | ||
| 101 | +} | ||
| 102 | + | ||
| 103 | +fn convert(aus: &[Vec<u8>], f: impl Fn(&[u8], usize) -> Vec<u8>) -> Vec<Vec<u8>> { | ||
| 104 | + let mut count = 0; | ||
| 105 | + aus.iter() | ||
| 106 | + .map(|au| { | ||
| 107 | + let out = f(au.as_slice(), count); | ||
| 108 | + count = header_of(au.as_slice(), count).substreams.len(); | ||
| 109 | + out | ||
| 110 | + }) | ||
| 111 | + .collect() | ||
| 112 | +} | ||
| 113 | + | ||
| 114 | +fn strip_object_substream(au: &[u8], substreams: usize) -> Vec<u8> { | ||
| 115 | + let header = header_of(au, substreams); | ||
| 116 | + assert_eq!(header.substreams.len(), 4, "the sample has 4 substreams"); | ||
| 117 | + let entry_len = if header.substreams[3].extra.is_some() { | ||
| 118 | + 4 | ||
| 119 | + } else { | ||
| 120 | + 2 | ||
| 121 | + }; | ||
| 122 | + let dir = &au[directory_start(&header)..header.data_start - entry_len]; | ||
| 123 | + let mut sync = sync_of(au, &header); | ||
| 124 | + if let Some(s) = sync.as_mut() { | ||
| 125 | + assert!(checksum_is_valid(s), "the sample's major sync checksum"); | ||
| 126 | + set_substream_count(s, 3); | ||
| 127 | + } | ||
| 128 | + let (_, end) = header.substream_span(2); | ||
| 129 | + assemble( | ||
| 130 | + [au[2], au[3]], | ||
| 131 | + sync.as_deref(), | ||
| 132 | + dir, | ||
| 133 | + &au[header.data_start..end], | ||
| 134 | + ) | ||
| 135 | +} | ||
| 136 | + | ||
| 137 | +fn with_object_substream(au: &[u8], substreams: usize, filler: u8) -> Vec<u8> { | ||
| 138 | + let header = header_of(au, substreams); | ||
| 139 | + assert_eq!(header.substreams.len(), 3, "the input is a 7.1 access unit"); | ||
| 140 | + let last = header.substreams[2]; | ||
| 141 | + let nonrestart = last.flags & 0x4; | ||
| 142 | + let entry = (u16::from(nonrestart) << 12) | (last.end_offset_words + FILLER_WORDS); | ||
| 143 | + let mut dir = au[directory_start(&header)..header.data_start].to_vec(); | ||
| 144 | + dir.extend_from_slice(&entry.to_be_bytes()); | ||
| 145 | + let mut sync = sync_of(au, &header); | ||
| 146 | + if let Some(s) = sync.as_mut() { | ||
| 147 | + set_substream_count(s, 4); | ||
| 148 | + } | ||
| 149 | + let (_, end) = header.substream_span(2); | ||
| 150 | + let mut coded = au[header.data_start..end].to_vec(); | ||
| 151 | + coded.extend(std::iter::repeat_n(filler, usize::from(FILLER_WORDS) * 2)); | ||
| 152 | + assemble([au[2], au[3]], sync.as_deref(), &dir, &coded) | ||
| 153 | +} | ||
| 154 | + | ||
| 155 | +fn with_object_payload(au: &[u8], substreams: usize, fill: u8) -> Vec<u8> { | ||
| 156 | + let header = header_of(au, substreams); | ||
| 157 | + assert_eq!(header.substreams.len(), 4, "the sample has 4 substreams"); | ||
| 158 | + let (start, end) = header.substream_span(3); | ||
| 159 | + assert!(start < end, "the object substream has coded data"); | ||
| 160 | + let mut out = au.to_vec(); | ||
| 161 | + out[start..end].fill(fill); | ||
| 162 | + out | ||
| 163 | +} | ||
| 164 | + | ||
| 171 | type Decoded = Vec<(ChannelLayout, Vec<Vec<u8>>)>; | 165 | type Decoded = Vec<(ChannelLayout, Vec<Vec<u8>>)>; |
| 172 | 166 | ||
| 173 | -/// Decodes `aus` with a fresh decoder and returns every output frame's planes | ||
| 174 | -/// plus the decoder's self-check tallies. | ||
| 175 | fn decode_all(aus: &[Vec<u8>]) -> (Decoded, ec_truehd::CheckStats) { | 167 | fn decode_all(aus: &[Vec<u8>]) -> (Decoded, ec_truehd::CheckStats) { |
| 176 | let mut decoder = TrueHdDecoder::new(); | 168 | let mut decoder = TrueHdDecoder::new(); |
| 177 | let mut frames = Vec::new(); | 169 | let mut frames = Vec::new(); |
| @@ -186,43 +178,28 @@ fn decode_all(aus: &[Vec<u8>]) -> (Decoded, ec_truehd::CheckStats) { | |||
| 186 | 178 | ||
| 187 | #[test] | 179 | #[test] |
| 188 | fn synthetic_atmos_decodes_bit_exact_to_the_original_7_1() { | 180 | fn synthetic_atmos_decodes_bit_exact_to_the_original_7_1() { |
| 189 | - let originals = truehd_access_units(); | 181 | + let originals = sample_access_units(); |
| 190 | - let synthetic: Vec<Vec<u8>> = originals | 182 | + let sevens = convert(&originals, strip_object_substream); |
| 191 | - .iter() | 183 | + let atmos = convert(&sevens, |au, n| with_object_substream(au, n, 0xA5)); |
| 192 | - .map(|au| with_object_substream(au, 0xA5)) | ||
| 193 | - .collect(); | ||
| 194 | - | ||
| 195 | - for au in &synthetic { | ||
| 196 | - let header = AccessUnitHeader::parse(au).expect("synthetic access unit parses"); | ||
| 197 | - assert_eq!(header.substreams.len(), 4, "a 4-entry directory"); | ||
| 198 | - assert_eq!( | ||
| 199 | - header.channel_layout().expect("a layout"), | ||
| 200 | - ChannelLayout::Surround7_1, | ||
| 201 | - "the 8-ch presentation is the top PCM one" | ||
| 202 | - ); | ||
| 203 | - } | ||
| 204 | 184 | ||
| 205 | if let Some(out) = std::env::var_os("EC_ATMOS_SYNTH_OUT") { | 185 | if let Some(out) = std::env::var_os("EC_ATMOS_SYNTH_OUT") { |
| 206 | - let raw: Vec<u8> = synthetic.iter().flatten().copied().collect(); | 186 | + let raw: Vec<u8> = atmos.iter().flatten().copied().collect(); |
| 207 | std::fs::write(&out, raw).expect("write the synthetic TrueHD stream"); | 187 | std::fs::write(&out, raw).expect("write the synthetic TrueHD stream"); |
| 208 | eprintln!("wrote synthetic stream to {}", Path::new(&out).display()); | 188 | eprintln!("wrote synthetic stream to {}", Path::new(&out).display()); |
| 209 | } | 189 | } |
| 210 | 190 | ||
| 211 | let (ours, _) = decode_all(&originals); | 191 | let (ours, _) = decode_all(&originals); |
| 212 | - let (synth, stats) = decode_all(&synthetic); | 192 | + let (seven, _) = decode_all(&sevens); |
| 193 | + let (synth, stats) = decode_all(&atmos); | ||
| 213 | assert!(!ours.is_empty(), "the original stream decodes to frames"); | 194 | assert!(!ours.is_empty(), "the original stream decodes to frames"); |
| 214 | - assert_eq!(synth.len(), ours.len(), "same number of frames"); | 195 | + assert_eq!(seven.len(), ours.len(), "7.1 rebuild: same number of frames"); |
| 196 | + assert!(seven == ours, "the 3-substream 7.1 stream differs from the original"); | ||
| 197 | + assert_eq!(synth.len(), ours.len(), "synthetic: same number of frames"); | ||
| 215 | for (i, ((la, pa), (lb, pb))) in ours.iter().zip(&synth).enumerate() { | 198 | for (i, ((la, pa), (lb, pb))) in ours.iter().zip(&synth).enumerate() { |
| 216 | assert_eq!(*la, ChannelLayout::Surround7_1, "frame {i} is 7.1"); | 199 | assert_eq!(*la, ChannelLayout::Surround7_1, "frame {i} is 7.1"); |
| 217 | assert_eq!(*lb, ChannelLayout::Surround7_1, "frame {i} is 7.1"); | 200 | assert_eq!(*lb, ChannelLayout::Surround7_1, "frame {i} is 7.1"); |
| 218 | assert_eq!(pa, pb, "frame {i} differs from the original"); | 201 | assert_eq!(pa, pb, "frame {i} differs from the original"); |
| 219 | } | 202 | } |
| 220 | - eprintln!( | ||
| 221 | - "{} frames, 8-channel, bit-exact vs the original", | ||
| 222 | - ours.len() | ||
| 223 | - ); | ||
| 224 | - | ||
| 225 | - // The PCM substreams' own checks still run on the synthetic stream. | ||
| 226 | assert_eq!(stats.parity_failures, 0, "substream parity"); | 203 | assert_eq!(stats.parity_failures, 0, "substream parity"); |
| 227 | assert_eq!(stats.restart_crc_failures, 0, "restart header CRC"); | 204 | assert_eq!(stats.restart_crc_failures, 0, "restart header CRC"); |
| 228 | assert_eq!(stats.lossless_check_failures, 0, "lossless check"); | 205 | assert_eq!(stats.lossless_check_failures, 0, "lossless check"); |
| @@ -231,21 +208,20 @@ fn synthetic_atmos_decodes_bit_exact_to_the_original_7_1() { | |||
| 231 | 208 | ||
| 232 | #[test] | 209 | #[test] |
| 233 | fn object_substream_contents_do_not_change_the_pcm_output() { | 210 | fn object_substream_contents_do_not_change_the_pcm_output() { |
| 234 | - let originals = truehd_access_units(); | 211 | + let originals = sample_access_units(); |
| 235 | - // Filler bytes are never read, so any value must decode identically. | 212 | + let zeros = convert(&originals, |au, n| with_object_payload(au, n, 0x00)); |
| 236 | - let zeros: Vec<Vec<u8>> = originals | 213 | + let ones = convert(&originals, |au, n| with_object_payload(au, n, 0xFF)); |
| 237 | - .iter() | 214 | + assert_ne!(zeros, originals, "the object payload was replaced"); |
| 238 | - .map(|au| with_object_substream(au, 0x00)) | 215 | + let (a, sa) = decode_all(&originals); |
| 239 | - .collect(); | 216 | + let (b, sb) = decode_all(&zeros); |
| 240 | - let ones: Vec<Vec<u8>> = originals | 217 | + let (c, sc) = decode_all(&ones); |
| 241 | - .iter() | 218 | + for s in [sa, sb, sc] { |
| 242 | - .map(|au| with_object_substream(au, 0xFF)) | 219 | + assert_eq!(s.parity_failures, 0, "substream parity"); |
| 243 | - .collect(); | 220 | + assert_eq!(s.restart_crc_failures, 0, "restart header CRC"); |
| 244 | - let (a, _) = decode_all(&zeros); | 221 | + assert_eq!(s.lossless_check_failures, 0, "lossless check"); |
| 245 | - let (b, _) = decode_all(&ones); | 222 | + assert_eq!(s.length_mismatches, 0, "substream lengths"); |
| 223 | + } | ||
| 246 | assert!(!a.is_empty()); | 224 | assert!(!a.is_empty()); |
| 247 | - assert_eq!( | 225 | + assert!(a == b, "object payload of zeros changes the PCM"); |
| 248 | - a, b, | 226 | + assert!(a == c, "object payload of ones changes the PCM"); |
| 249 | - "the object substream's bytes are skipped, not decoded" | ||
| 250 | - ); | ||
| 251 | } | 227 | } |
crates/ec-truehd/tests/it/common.rsmodified+3 -1
| @@ -3,11 +3,13 @@ use ec_truehd::sync::AccessUnitHeader; | |||
| 3 | pub fn raw_access_units(data: &[u8]) -> Vec<Vec<u8>> { | 3 | pub fn raw_access_units(data: &[u8]) -> Vec<Vec<u8>> { |
| 4 | let mut aus = Vec::new(); | 4 | let mut aus = Vec::new(); |
| 5 | let mut pos = 0; | 5 | let mut pos = 0; |
| 6 | + let mut substreams = 0; | ||
| 6 | while pos < data.len() { | 7 | while pos < data.len() { |
| 7 | - let header = AccessUnitHeader::parse(&data[pos..]) | 8 | + let header = AccessUnitHeader::parse(&data[pos..], substreams) |
| 8 | .unwrap_or_else(|e| panic!("access unit {}: {e}", aus.len())); | 9 | .unwrap_or_else(|e| panic!("access unit {}: {e}", aus.len())); |
| 9 | aus.push(data[pos..pos + header.length].to_vec()); | 10 | aus.push(data[pos..pos + header.length].to_vec()); |
| 10 | pos += header.length; | 11 | pos += header.length; |
| 12 | + substreams = header.substreams.len(); | ||
| 11 | } | 13 | } |
| 12 | aus | 14 | aus |
| 13 | } | 15 | } |
crates/ec-truehd/tests/it/oracle.rsmodified+5 -24
| @@ -42,29 +42,22 @@ fn compare(ours: &[i32], reference: &[i32]) -> (usize, usize, Option<usize>) { | |||
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | #[test] | 44 | #[test] |
| 45 | -fn first_60_seconds_of_atmos_are_bit_exact() { | 45 | +fn atmos_sample_is_bit_exact_on_every_channel() { |
| 46 | - const SECONDS: usize = 60; | ||
| 47 | let path = ec_testkit::fixture("open/fate/truehd/atmos.thd"); | 46 | let path = ec_testkit::fixture("open/fate/truehd/atmos.thd"); |
| 48 | let reference = s32le(&ffmpeg(&[ | 47 | let reference = s32le(&ffmpeg(&[ |
| 49 | "-i", | 48 | "-i", |
| 50 | path.to_str().unwrap(), | 49 | path.to_str().unwrap(), |
| 51 | - "-t", | ||
| 52 | - &SECONDS.to_string(), | ||
| 53 | "-f", | 50 | "-f", |
| 54 | "s32le", | 51 | "s32le", |
| 55 | "-", | 52 | "-", |
| 56 | ])); | 53 | ])); |
| 57 | let channels = 8; | 54 | let channels = 8; |
| 58 | - let wanted = SECONDS * 48_000 * channels; | ||
| 59 | let data = std::fs::read(&path).expect("read atmos.thd"); | 55 | let data = std::fs::read(&path).expect("read atmos.thd"); |
| 60 | 56 | ||
| 61 | let mut decoder = TrueHdDecoder::new(); | 57 | let mut decoder = TrueHdDecoder::new(); |
| 62 | - let mut ours: Vec<i32> = Vec::with_capacity(wanted); | 58 | + let mut ours: Vec<i32> = Vec::with_capacity(reference.len()); |
| 63 | let mut aus = 0; | 59 | let mut aus = 0; |
| 64 | for au in raw_access_units(&data) { | 60 | for au in raw_access_units(&data) { |
| 65 | - if ours.len() >= wanted { | ||
| 66 | - break; | ||
| 67 | - } | ||
| 68 | let frame = decoder | 61 | let frame = decoder |
| 69 | .decode_access_unit(&au) | 62 | .decode_access_unit(&au) |
| 70 | .unwrap_or_else(|e| panic!("AU {aus}: {e}")); | 63 | .unwrap_or_else(|e| panic!("AU {aus}: {e}")); |
| @@ -76,30 +69,18 @@ fn first_60_seconds_of_atmos_are_bit_exact() { | |||
| 76 | } | 69 | } |
| 77 | let (exact, total, first) = compare(&ours, &reference); | 70 | let (exact, total, first) = compare(&ours, &reference); |
| 78 | let stats = decoder.check_stats(); | 71 | let stats = decoder.check_stats(); |
| 79 | - for ch in 0..channels { | ||
| 80 | - let n = total / channels; | ||
| 81 | - let e = (0..n) | ||
| 82 | - .filter(|&i| ours[i * channels + ch] == reference[i * channels + ch]) | ||
| 83 | - .count(); | ||
| 84 | - eprintln!("channel {ch}: {e}/{n} exact"); | ||
| 85 | - } | ||
| 86 | eprintln!( | 72 | eprintln!( |
| 87 | "{aus} access units, {total} samples compared ({} s), {exact} exact ({:.4}%), first mismatch {first:?}, {stats:?}", | 73 | "{aus} access units, {total} samples compared ({} s), {exact} exact ({:.4}%), first mismatch {first:?}, {stats:?}", |
| 88 | total / channels / 48_000, | 74 | total / channels / 48_000, |
| 89 | 100.0 * exact as f64 / total as f64 | 75 | 100.0 * exact as f64 / total as f64 |
| 90 | ); | 76 | ); |
| 91 | - assert!( | 77 | + assert!(aus >= 100, "only {aus} access units"); |
| 92 | - total >= (SECONDS - 1) * 48_000 * channels, | 78 | + assert_eq!(ours.len(), reference.len(), "sample count vs ffmpeg"); |
| 93 | - "only {total} samples" | ||
| 94 | - ); | ||
| 95 | assert_eq!(stats.lossless_check_failures, 0, "{stats:?}"); | 79 | assert_eq!(stats.lossless_check_failures, 0, "{stats:?}"); |
| 96 | assert_eq!(stats.parity_failures, 0, "{stats:?}"); | 80 | assert_eq!(stats.parity_failures, 0, "{stats:?}"); |
| 97 | assert_eq!(stats.restart_crc_failures, 0, "{stats:?}"); | 81 | assert_eq!(stats.restart_crc_failures, 0, "{stats:?}"); |
| 98 | assert_eq!(stats.length_mismatches, 0, "{stats:?}"); | 82 | assert_eq!(stats.length_mismatches, 0, "{stats:?}"); |
| 99 | - assert!( | 83 | + assert_eq!(exact, total, "first mismatch {first:?}"); |
| 100 | - exact as f64 >= 0.9999 * total as f64, | ||
| 101 | - "{exact}/{total} exact, first mismatch {first:?}" | ||
| 102 | - ); | ||
| 103 | } | 84 | } |
| 104 | 85 | ||
| 105 | /// An externally encoded MLP stream (raw access units) decoded by us must | 86 | /// An externally encoded MLP stream (raw access units) decoded by us must |