TrueHD tests: adapt to open FATE atmos.thd (128 AUs, 4 substreams, 32-byte ext sync)
Browse files at this commit0892e43567b98f9a734cdb8fb5836c0bf3b7cd4eParent: 731634a2 changed files +132 -178
crates/ec-truehd/tests/it/atmos_synth.rsmodified+127 -154
| @@ -1,33 +1,5 @@ | |||
| 1 | -//! Atmos backward compatibility, tested on a synthetic 4-substream stream. | 1 | +//! Atmos backward compatibility on the open FATE sample (4 substreams, the 4th |
| 2 | -//! | 2 | +//! being object audio, and a 32-byte extended major sync). |
| 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 | 3 | ||
| 32 | use std::path::Path; | 4 | use std::path::Path; |
| 33 | 5 | ||
| @@ -37,27 +9,26 @@ use ec_truehd::sync::AccessUnitHeader; | |||
| 37 | 9 | ||
| 38 | use crate::common::raw_access_units; | 10 | use crate::common::raw_access_units; |
| 39 | 11 | ||
| 40 | -/// major sync among them. | 12 | +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; | 13 | 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; | 14 | const MAX_AU_WORDS: usize = 0x0FFF; |
| 46 | -const MAJOR_SYNC_LEN: usize = 28; | ||
| 47 | 15 | ||
| 48 | -fn truehd_access_units() -> Vec<Vec<u8>> { | 16 | +fn sample_access_units() -> Vec<Vec<u8>> { |
| 49 | let data = | 17 | let data = |
| 50 | std::fs::read(ec_testkit::fixture("open/fate/truehd/atmos.thd")).expect("read atmos.thd"); | 18 | std::fs::read(ec_testkit::fixture("open/fate/truehd/atmos.thd")).expect("read atmos.thd"); |
| 51 | let aus = raw_access_units(&data); | 19 | let aus = raw_access_units(&data); |
| 52 | assert!( | 20 | assert!( |
| 53 | - aus.len() >= AUS, | 21 | + aus.len() >= MIN_ACCESS_UNITS, |
| 54 | - "atmos.thd has fewer than {AUS} access units" | 22 | + "atmos.thd has only {} access units", |
| 23 | + aus.len() | ||
| 55 | ); | 24 | ); |
| 56 | - aus.into_iter().take(AUS).collect() | 25 | + aus |
| 26 | +} | ||
| 27 | + | ||
| 28 | +fn header_of(au: &[u8], substreams: usize) -> AccessUnitHeader { | ||
| 29 | + AccessUnitHeader::parse(au, substreams).expect("access unit parses") | ||
| 57 | } | 30 | } |
| 58 | 31 | ||
| 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] { | 32 | fn crc16_table() -> [u32; 256] { |
| 62 | let mut table = [0u32; 256]; | 33 | let mut table = [0u32; 256]; |
| 63 | for (i, entry) in table.iter_mut().enumerate() { | 34 | for (i, entry) in table.iter_mut().enumerate() { |
| @@ -71,107 +42,131 @@ fn crc16_table() -> [u32; 256] { | |||
| 71 | table | 42 | table |
| 72 | } | 43 | } |
| 73 | 44 | ||
| 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 { | 45 | fn major_sync_checksum(sync: &[u8]) -> u16 { |
| 46 | + let n = sync.len() - 4; | ||
| 77 | let table = crc16_table(); | 47 | let table = crc16_table(); |
| 78 | let mut crc: u32 = 0; | 48 | let mut crc: u32 = 0; |
| 79 | - for &b in &sync[..24] { | 49 | + for &b in &sync[..n] { |
| 80 | crc = table[usize::from((crc as u8) ^ b)] ^ (crc >> 8); | 50 | crc = table[usize::from((crc as u8) ^ b)] ^ (crc >> 8); |
| 81 | } | 51 | } |
| 82 | - (crc as u16) ^ u16::from_le_bytes([sync[24], sync[25]]) | 52 | + (crc as u16) ^ u16::from_le_bytes([sync[n], sync[n + 1]]) |
| 53 | +} | ||
| 54 | + | ||
| 55 | +fn checksum_is_valid(sync: &[u8]) -> bool { | ||
| 56 | + let n = sync.len() - 2; | ||
| 57 | + major_sync_checksum(sync) == u16::from_le_bytes([sync[n], sync[n + 1]]) | ||
| 58 | +} | ||
| 59 | + | ||
| 60 | +fn set_substream_count(sync: &mut [u8], count: u8) { | ||
| 61 | + sync[16] = (sync[16] & 0x0F) | (count << 4); | ||
| 62 | + let checksum = major_sync_checksum(sync); | ||
| 63 | + let n = sync.len() - 2; | ||
| 64 | + sync[n..].copy_from_slice(&checksum.to_le_bytes()); | ||
| 83 | } | 65 | } |
| 84 | 66 | ||
| 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 { | 67 | fn parity_ok(header: &[u8], directory: &[u8]) -> bool { |
| 89 | let p = header.iter().chain(directory).fold(0u8, |a, &b| a ^ b); | 68 | let p = header.iter().chain(directory).fold(0u8, |a, &b| a ^ b); |
| 90 | ((p >> 4) ^ p) & 0xF == 0xF | 69 | ((p >> 4) ^ p) & 0xF == 0xF |
| 91 | } | 70 | } |
| 92 | 71 | ||
| 93 | -/// Rebuilds one access unit of the 7.1 remux with a 4th (object-audio) | 72 | +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, 0).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(); | 73 | let mut body = Vec::new(); |
| 126 | - if header.major_sync.is_some() { | 74 | + body.extend_from_slice(sync.unwrap_or(&[])); |
| 127 | - let mut sync = au[4..4 + MAJOR_SYNC_LEN].to_vec(); | 75 | + 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); | 76 | body.extend_from_slice(coded); |
| 140 | - body.extend(std::iter::repeat_n(filler, filler_bytes)); | 77 | + 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"); | 78 | 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) | 79 | let nibble = (0u8..16) |
| 148 | .find(|&cn| { | 80 | .find(|&cn| { |
| 149 | - let len_hi = (words >> 8) as u8 & 0x0F; | 81 | + let head = [ |
| 150 | - let len_lo = (words & 0xFF) as u8; | 82 | + (cn << 4) | ((words >> 8) as u8 & 0x0F), |
| 151 | - let head = [(cn << 4) | len_hi, len_lo, timing[0], timing[1]]; | 83 | + (words & 0xFF) as u8, |
| 152 | - parity_ok(&head, &dir) | 84 | + timing[0], |
| 85 | + timing[1], | ||
| 86 | + ]; | ||
| 87 | + parity_ok(&head, dir) | ||
| 153 | }) | 88 | }) |
| 154 | .expect("one of the 16 nibble values satisfies the fold"); | 89 | .expect("one of the 16 nibble values satisfies the fold"); |
| 155 | let word = (u16::from(nibble) << 12) | words as u16; | 90 | let word = (u16::from(nibble) << 12) | words as u16; |
| 156 | - | 91 | + 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()); | 92 | out.extend_from_slice(&word.to_be_bytes()); |
| 165 | out.extend_from_slice(&timing); | 93 | out.extend_from_slice(&timing); |
| 166 | out.extend_from_slice(&body); | 94 | out.extend_from_slice(&body); |
| 167 | out | 95 | out |
| 168 | } | 96 | } |
| 169 | 97 | ||
| 170 | -/// One decoded frame: its layout and its planes. | 98 | +fn sync_of(au: &[u8], header: &AccessUnitHeader) -> Option<Vec<u8>> { |
| 99 | + header.major_sync.map(|s| au[4..4 + s.len].to_vec()) | ||
| 100 | +} | ||
| 101 | + | ||
| 102 | +fn directory_start(header: &AccessUnitHeader) -> usize { | ||
| 103 | + 4 + header.major_sync.map_or(0, |s| s.len) | ||
| 104 | +} | ||
| 105 | + | ||
| 106 | +fn convert(aus: &[Vec<u8>], f: impl Fn(&[u8], usize) -> Vec<u8>) -> Vec<Vec<u8>> { | ||
| 107 | + let mut count = 0; | ||
| 108 | + aus.iter() | ||
| 109 | + .map(|au| { | ||
| 110 | + let out = f(au.as_slice(), count); | ||
| 111 | + count = header_of(au.as_slice(), count).substreams.len(); | ||
| 112 | + out | ||
| 113 | + }) | ||
| 114 | + .collect() | ||
| 115 | +} | ||
| 116 | + | ||
| 117 | +fn strip_object_substream(au: &[u8], substreams: usize) -> Vec<u8> { | ||
| 118 | + let header = header_of(au, substreams); | ||
| 119 | + assert_eq!(header.substreams.len(), 4, "the sample has 4 substreams"); | ||
| 120 | + let entry_len = if header.substreams[3].extra.is_some() { | ||
| 121 | + 4 | ||
| 122 | + } else { | ||
| 123 | + 2 | ||
| 124 | + }; | ||
| 125 | + let dir = &au[directory_start(&header)..header.data_start - entry_len]; | ||
| 126 | + let mut sync = sync_of(au, &header); | ||
| 127 | + if let Some(s) = sync.as_mut() { | ||
| 128 | + assert!(checksum_is_valid(s), "the sample's major sync checksum"); | ||
| 129 | + set_substream_count(s, 3); | ||
| 130 | + } | ||
| 131 | + let (_, end) = header.substream_span(2); | ||
| 132 | + assemble( | ||
| 133 | + [au[2], au[3]], | ||
| 134 | + sync.as_deref(), | ||
| 135 | + dir, | ||
| 136 | + &au[header.data_start..end], | ||
| 137 | + ) | ||
| 138 | +} | ||
| 139 | + | ||
| 140 | +fn with_object_substream(au: &[u8], substreams: usize, filler: u8) -> Vec<u8> { | ||
| 141 | + let header = header_of(au, substreams); | ||
| 142 | + assert_eq!(header.substreams.len(), 3, "the input is a 7.1 access unit"); | ||
| 143 | + let last = header.substreams[2]; | ||
| 144 | + let nonrestart = last.flags & 0x4; | ||
| 145 | + let entry = (u16::from(nonrestart) << 12) | (last.end_offset_words + FILLER_WORDS); | ||
| 146 | + let mut dir = au[directory_start(&header)..header.data_start].to_vec(); | ||
| 147 | + dir.extend_from_slice(&entry.to_be_bytes()); | ||
| 148 | + let mut sync = sync_of(au, &header); | ||
| 149 | + if let Some(s) = sync.as_mut() { | ||
| 150 | + set_substream_count(s, 4); | ||
| 151 | + } | ||
| 152 | + let (_, end) = header.substream_span(2); | ||
| 153 | + let mut coded = au[header.data_start..end].to_vec(); | ||
| 154 | + coded.extend(std::iter::repeat_n(filler, usize::from(FILLER_WORDS) * 2)); | ||
| 155 | + assemble([au[2], au[3]], sync.as_deref(), &dir, &coded) | ||
| 156 | +} | ||
| 157 | + | ||
| 158 | +fn with_object_payload(au: &[u8], substreams: usize, fill: u8) -> Vec<u8> { | ||
| 159 | + let header = header_of(au, substreams); | ||
| 160 | + assert_eq!(header.substreams.len(), 4, "the sample has 4 substreams"); | ||
| 161 | + let (start, end) = header.substream_span(3); | ||
| 162 | + assert!(start < end, "the object substream has coded data"); | ||
| 163 | + let mut out = au.to_vec(); | ||
| 164 | + out[start..end].fill(fill); | ||
| 165 | + out | ||
| 166 | +} | ||
| 167 | + | ||
| 171 | type Decoded = Vec<(ChannelLayout, Vec<Vec<u8>>)>; | 168 | type Decoded = Vec<(ChannelLayout, Vec<Vec<u8>>)>; |
| 172 | 169 | ||
| 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) { | 170 | fn decode_all(aus: &[Vec<u8>]) -> (Decoded, ec_truehd::CheckStats) { |
| 176 | let mut decoder = TrueHdDecoder::new(); | 171 | let mut decoder = TrueHdDecoder::new(); |
| 177 | let mut frames = Vec::new(); | 172 | let mut frames = Vec::new(); |
| @@ -186,43 +181,28 @@ fn decode_all(aus: &[Vec<u8>]) -> (Decoded, ec_truehd::CheckStats) { | |||
| 186 | 181 | ||
| 187 | #[test] | 182 | #[test] |
| 188 | fn synthetic_atmos_decodes_bit_exact_to_the_original_7_1() { | 183 | fn synthetic_atmos_decodes_bit_exact_to_the_original_7_1() { |
| 189 | - let originals = truehd_access_units(); | 184 | + let originals = sample_access_units(); |
| 190 | - let synthetic: Vec<Vec<u8>> = originals | 185 | + let sevens = convert(&originals, strip_object_substream); |
| 191 | - .iter() | 186 | + 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, 0).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 | 187 | ||
| 205 | if let Some(out) = std::env::var_os("EC_ATMOS_SYNTH_OUT") { | 188 | if let Some(out) = std::env::var_os("EC_ATMOS_SYNTH_OUT") { |
| 206 | - let raw: Vec<u8> = synthetic.iter().flatten().copied().collect(); | 189 | + let raw: Vec<u8> = atmos.iter().flatten().copied().collect(); |
| 207 | std::fs::write(&out, raw).expect("write the synthetic TrueHD stream"); | 190 | std::fs::write(&out, raw).expect("write the synthetic TrueHD stream"); |
| 208 | eprintln!("wrote synthetic stream to {}", Path::new(&out).display()); | 191 | eprintln!("wrote synthetic stream to {}", Path::new(&out).display()); |
| 209 | } | 192 | } |
| 210 | 193 | ||
| 211 | let (ours, _) = decode_all(&originals); | 194 | let (ours, _) = decode_all(&originals); |
| 212 | - let (synth, stats) = decode_all(&synthetic); | 195 | + let (seven, _) = decode_all(&sevens); |
| 196 | + let (synth, stats) = decode_all(&atmos); | ||
| 213 | assert!(!ours.is_empty(), "the original stream decodes to frames"); | 197 | assert!(!ours.is_empty(), "the original stream decodes to frames"); |
| 214 | - assert_eq!(synth.len(), ours.len(), "same number of frames"); | 198 | + assert_eq!(seven.len(), ours.len(), "7.1 rebuild: same number of frames"); |
| 199 | + assert!(seven == ours, "the 3-substream 7.1 stream differs from the original"); | ||
| 200 | + assert_eq!(synth.len(), ours.len(), "synthetic: same number of frames"); | ||
| 215 | for (i, ((la, pa), (lb, pb))) in ours.iter().zip(&synth).enumerate() { | 201 | for (i, ((la, pa), (lb, pb))) in ours.iter().zip(&synth).enumerate() { |
| 216 | assert_eq!(*la, ChannelLayout::Surround7_1, "frame {i} is 7.1"); | 202 | assert_eq!(*la, ChannelLayout::Surround7_1, "frame {i} is 7.1"); |
| 217 | assert_eq!(*lb, ChannelLayout::Surround7_1, "frame {i} is 7.1"); | 203 | assert_eq!(*lb, ChannelLayout::Surround7_1, "frame {i} is 7.1"); |
| 218 | assert_eq!(pa, pb, "frame {i} differs from the original"); | 204 | assert_eq!(pa, pb, "frame {i} differs from the original"); |
| 219 | } | 205 | } |
| 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"); | 206 | assert_eq!(stats.parity_failures, 0, "substream parity"); |
| 227 | assert_eq!(stats.restart_crc_failures, 0, "restart header CRC"); | 207 | assert_eq!(stats.restart_crc_failures, 0, "restart header CRC"); |
| 228 | assert_eq!(stats.lossless_check_failures, 0, "lossless check"); | 208 | assert_eq!(stats.lossless_check_failures, 0, "lossless check"); |
| @@ -231,21 +211,14 @@ fn synthetic_atmos_decodes_bit_exact_to_the_original_7_1() { | |||
| 231 | 211 | ||
| 232 | #[test] | 212 | #[test] |
| 233 | fn object_substream_contents_do_not_change_the_pcm_output() { | 213 | fn object_substream_contents_do_not_change_the_pcm_output() { |
| 234 | - let originals = truehd_access_units(); | 214 | + let originals = sample_access_units(); |
| 235 | - // Filler bytes are never read, so any value must decode identically. | 215 | + let zeros = convert(&originals, |au, n| with_object_payload(au, n, 0x00)); |
| 236 | - let zeros: Vec<Vec<u8>> = originals | 216 | + let ones = convert(&originals, |au, n| with_object_payload(au, n, 0xFF)); |
| 237 | - .iter() | 217 | + assert_ne!(zeros, originals, "the object payload was replaced"); |
| 238 | - .map(|au| with_object_substream(au, 0x00)) | 218 | + let (a, _) = decode_all(&originals); |
| 239 | - .collect(); | 219 | + let (b, _) = decode_all(&zeros); |
| 240 | - let ones: Vec<Vec<u8>> = originals | 220 | + let (c, _) = decode_all(&ones); |
| 241 | - .iter() | ||
| 242 | - .map(|au| with_object_substream(au, 0xFF)) | ||
| 243 | - .collect(); | ||
| 244 | - let (a, _) = decode_all(&zeros); | ||
| 245 | - let (b, _) = decode_all(&ones); | ||
| 246 | assert!(!a.is_empty()); | 221 | assert!(!a.is_empty()); |
| 247 | - assert_eq!( | 222 | + assert!(a == b, "object payload of zeros changes the PCM"); |
| 248 | - a, b, | 223 | + assert!(a == c, "object payload of ones changes the PCM"); |
| 249 | - "the object substream's bytes are skipped, not decoded" | ||
| 250 | - ); | ||
| 251 | } | 224 | } |
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 |