| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use std::path::Path; |
| 9 | use std::process::Command; |
| 10 | |
| 11 | use crate::common::*; |
| 12 | use ec_core::registry::Demuxer; |
| 13 | use ec_core::{CodecId, MediaParameters}; |
| 14 | use ec_truehd::{AccessUnitHeader, TrueHdDecoder}; |
| 15 | |
| 16 | |
| 17 | fn truehd_ts(name: &str, channels: &str) -> Option<std::path::PathBuf> { |
| 18 | let a = [ |
| 19 | "-f", |
| 20 | "lavfi", |
| 21 | "-i", |
| 22 | "sine=frequency=440:sample_rate=48000:duration=2", |
| 23 | "-ac", |
| 24 | channels, |
| 25 | "-ar", |
| 26 | "48000", |
| 27 | "-c:a", |
| 28 | "truehd", |
| 29 | "-strict", |
| 30 | "-2", |
| 31 | "-f", |
| 32 | "mpegts", |
| 33 | ]; |
| 34 | fixture(name, &a) |
| 35 | } |
| 36 | |
| 37 | |
| 38 | fn ffmpeg_s32le(path: &Path) -> Vec<i32> { |
| 39 | let out = Command::new("ffmpeg") |
| 40 | .args(["-v", "error", "-nostdin", "-i"]) |
| 41 | .arg(path) |
| 42 | .args(["-map", "0:a:0", "-f", "s32le", "-"]) |
| 43 | .output() |
| 44 | .expect("run ffmpeg"); |
| 45 | assert!( |
| 46 | out.status.success(), |
| 47 | "ffmpeg {}: {}", |
| 48 | path.display(), |
| 49 | String::from_utf8_lossy(&out.stderr) |
| 50 | ); |
| 51 | s32le(&out.stdout) |
| 52 | } |
| 53 | |
| 54 | fn s32le(bytes: &[u8]) -> Vec<i32> { |
| 55 | bytes |
| 56 | .as_chunks::<4>() |
| 57 | .0 |
| 58 | .iter() |
| 59 | .map(|c| i32::from_le_bytes(*c)) |
| 60 | .collect() |
| 61 | } |
| 62 | |
| 63 | #[test] |
| 64 | fn truehd_in_ts_decodes_sample_exact_to_ffmpeg() { |
| 65 | if !have_ffmpeg() { |
| 66 | return; |
| 67 | } |
| 68 | for (name, channels, want) in [("truehd_sine_2ch.ts", "2", 2usize)] { |
| 69 | let Some(path) = truehd_ts(name, channels) else { |
| 70 | return; |
| 71 | }; |
| 72 | let mut d = open(&path); |
| 73 | let all = read_all(&mut d); |
| 74 | let stream = d |
| 75 | .streams() |
| 76 | .iter() |
| 77 | .find(|s| s.params.codec == CodecId::TrueHd) |
| 78 | .unwrap_or_else(|| panic!("{name}: no TrueHD stream exposed")) |
| 79 | .clone(); |
| 80 | let pkts: Vec<Pkt> = all |
| 81 | .into_iter() |
| 82 | .filter(|p| p.stream == stream.index) |
| 83 | .collect(); |
| 84 | assert!(!pkts.is_empty(), "{name}: no TrueHD access units"); |
| 85 | |
| 86 | |
| 87 | for p in &pkts { |
| 88 | let head = AccessUnitHeader::parse(&p.data) |
| 89 | .unwrap_or_else(|e| panic!("{name}: AU at {:#x}: {e}", p.off)); |
| 90 | assert_eq!(head.length, p.data.len(), "{name}: AU length"); |
| 91 | } |
| 92 | |
| 93 | let mut dec = TrueHdDecoder::new(); |
| 94 | let mut ours = Vec::new(); |
| 95 | let mut channels_seen = 0; |
| 96 | for p in &pkts { |
| 97 | if let Some(f) = dec |
| 98 | .decode_access_unit(&p.data) |
| 99 | .unwrap_or_else(|e| panic!("{name}: decode: {e}")) |
| 100 | { |
| 101 | channels_seen = f.channels(); |
| 102 | ours.extend(s32le(&f.data[0])); |
| 103 | } |
| 104 | } |
| 105 | let stats = dec.check_stats(); |
| 106 | assert_eq!(stats.lossless_check_failures, 0, "{name}: {stats:?}"); |
| 107 | assert_eq!(stats.parity_failures, 0, "{name}: {stats:?}"); |
| 108 | assert_eq!(stats.restart_crc_failures, 0, "{name}: {stats:?}"); |
| 109 | assert_eq!(channels_seen, want, "{name}: channels"); |
| 110 | let MediaParameters::Audio(a) = &stream.params.media else { |
| 111 | panic!("{name}: TrueHD stream is not audio"); |
| 112 | }; |
| 113 | assert_eq!(a.sample_rate, 48_000, "{name}: rate"); |
| 114 | |
| 115 | let reference = ffmpeg_s32le(&path); |
| 116 | assert!(!ours.is_empty(), "{name}: no samples decoded"); |
| 117 | assert_eq!(ours.len(), reference.len(), "{name}: sample count"); |
| 118 | assert!(ours == reference, "{name}: PCM differs from ffmpeg"); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | #[test] |
| 123 | fn truehd_packets_match_the_container_packet_form() { |
| 124 | |
| 125 | |
| 126 | if !have_ffmpeg() { |
| 127 | return; |
| 128 | } |
| 129 | let Some(path) = truehd_ts("truehd_sine_2ch.ts", "2") else { |
| 130 | return; |
| 131 | }; |
| 132 | let mut d = open(&path); |
| 133 | let all = read_all(&mut d); |
| 134 | let stream = d |
| 135 | .streams() |
| 136 | .iter() |
| 137 | .find(|s| s.params.codec == CodecId::TrueHd) |
| 138 | .expect("a TrueHD stream") |
| 139 | .index; |
| 140 | let ours: Vec<u8> = all |
| 141 | .into_iter() |
| 142 | .filter(|p| p.stream == stream) |
| 143 | .flat_map(|p| p.data) |
| 144 | .collect(); |
| 145 | let o = Command::new("ffmpeg") |
| 146 | .args(["-v", "error", "-nostdin", "-i"]) |
| 147 | .arg(&path) |
| 148 | .args(["-map", "0:a:0", "-c", "copy", "-f", "truehd", "-"]) |
| 149 | .output() |
| 150 | .unwrap(); |
| 151 | assert!(o.status.success()); |
| 152 | let theirs = o.stdout; |
| 153 | assert!(!ours.is_empty() && !theirs.is_empty()); |
| 154 | |
| 155 | |
| 156 | let first = ours[..8].to_vec(); |
| 157 | let at = theirs |
| 158 | .windows(first.len()) |
| 159 | .position(|w| w == first.as_slice()) |
| 160 | .expect("ffmpeg's stream copy starts at one of our access units"); |
| 161 | assert_eq!( |
| 162 | &theirs[at..at + ours.len().min(theirs.len() - at)], |
| 163 | &ours[..ours.len().min(theirs.len() - at)], |
| 164 | "access unit bytes differ from ffmpeg's stream copy" |
| 165 | ); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | fn split_aus(raw: &[u8]) -> Vec<Vec<u8>> { |
| 174 | let mut v = Vec::new(); |
| 175 | let mut at = 0; |
| 176 | while at < raw.len() { |
| 177 | let len = ec_truehd::frame_length(&raw[at..]).expect("an access-unit length word"); |
| 178 | v.push(raw[at..at + len].to_vec()); |
| 179 | at += len; |
| 180 | } |
| 181 | v |
| 182 | } |
| 183 | |
| 184 | |
| 185 | fn fake_ac3_frame() -> Vec<u8> { |
| 186 | let mut f = vec![0u8; 128]; |
| 187 | f[0] = 0x0B; |
| 188 | f[1] = 0x77; |
| 189 | f[4] = 0x00; |
| 190 | f[5] = 8 << 3; |
| 191 | f |
| 192 | } |
| 193 | |
| 194 | |
| 195 | #[derive(Default)] |
| 196 | struct Ts { |
| 197 | out: Vec<u8>, |
| 198 | cc: std::collections::HashMap<u16, u8>, |
| 199 | } |
| 200 | |
| 201 | impl Ts { |
| 202 | |
| 203 | fn packet(&mut self, pid: u16, pusi: bool, payload: &[u8]) { |
| 204 | let cc = self.cc.entry(pid).or_insert(0); |
| 205 | let mut p = [0xFFu8; 188]; |
| 206 | p[0] = 0x47; |
| 207 | p[1] = (u8::from(pusi) << 6) | ((pid >> 8) as u8 & 0x1F); |
| 208 | p[2] = pid as u8; |
| 209 | let n = payload.len(); |
| 210 | if n == 184 { |
| 211 | p[3] = 0x10 | (*cc & 0x0F); |
| 212 | p[4..].copy_from_slice(payload); |
| 213 | } else { |
| 214 | p[3] = 0x30 | (*cc & 0x0F); |
| 215 | let af = 184 - n; |
| 216 | p[4] = (af - 1) as u8; |
| 217 | if af >= 2 { |
| 218 | p[5] = 0; |
| 219 | |
| 220 | } |
| 221 | p[4 + af..].copy_from_slice(payload); |
| 222 | } |
| 223 | *cc = (*cc + 1) & 0x0F; |
| 224 | self.out.extend_from_slice(&p); |
| 225 | } |
| 226 | |
| 227 | |
| 228 | fn psi(&mut self, pid: u16, table: u8, ext: u16, body: &[u8]) { |
| 229 | let mut sec = vec![table, 0, 0]; |
| 230 | sec.extend_from_slice(&ext.to_be_bytes()); |
| 231 | sec.extend_from_slice(&[0xC1, 0, 0]); |
| 232 | sec.extend_from_slice(body); |
| 233 | let len = sec.len() - 3 + 4; |
| 234 | sec[1] = 0xB0 | ((len >> 8) as u8 & 0x0F); |
| 235 | sec[2] = len as u8; |
| 236 | let crc = crc32(&sec); |
| 237 | sec.extend_from_slice(&crc.to_be_bytes()); |
| 238 | let mut payload = vec![0u8]; |
| 239 | payload.extend_from_slice(&sec); |
| 240 | self.packet(pid, true, &payload); |
| 241 | } |
| 242 | |
| 243 | |
| 244 | fn pes(&mut self, pid: u16, pts: u64, data: &[u8]) { |
| 245 | let mut all = vec![0, 0, 1, 0xBD, 0, 0, 0x80, 0x80, 5]; |
| 246 | all.extend_from_slice(&ts5(2, pts)); |
| 247 | all.extend_from_slice(data); |
| 248 | for (i, chunk) in all.chunks(184).enumerate() { |
| 249 | self.packet(pid, i == 0, chunk); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | fn ts5(prefix: u8, v: u64) -> [u8; 5] { |
| 255 | [ |
| 256 | (prefix << 4) | ((v >> 29) & 0x0E) as u8 | 1, |
| 257 | (v >> 22) as u8, |
| 258 | ((v >> 14) & 0xFE) as u8 | 1, |
| 259 | (v >> 7) as u8, |
| 260 | ((v << 1) & 0xFE) as u8 | 1, |
| 261 | ] |
| 262 | } |
| 263 | |
| 264 | #[test] |
| 265 | fn ac3_core_interleaved_in_the_truehd_pid_is_dropped() { |
| 266 | if !have_ffmpeg() { |
| 267 | return; |
| 268 | } |
| 269 | let Some(raw) = fixture( |
| 270 | "truehd_sine_2ch.thd", |
| 271 | &[ |
| 272 | "-f", |
| 273 | "lavfi", |
| 274 | "-i", |
| 275 | "sine=frequency=440:sample_rate=48000:duration=2", |
| 276 | "-ac", |
| 277 | "2", |
| 278 | "-ar", |
| 279 | "48000", |
| 280 | "-c:a", |
| 281 | "truehd", |
| 282 | "-strict", |
| 283 | "-2", |
| 284 | "-f", |
| 285 | "truehd", |
| 286 | ], |
| 287 | ) else { |
| 288 | return; |
| 289 | }; |
| 290 | let aus = split_aus(&std::fs::read(&raw).unwrap()); |
| 291 | assert!(aus.len() > 8, "enough access units"); |
| 292 | |
| 293 | |
| 294 | let mut ts = Ts::default(); |
| 295 | ts.psi(0, 0, 1, &[0x00, 0x01, 0xF0, 0x00]); |
| 296 | let pmt = [ |
| 297 | 0xF1, 0x00, |
| 298 | 0xF0, 0x00, |
| 299 | 0x83, 0xF1, 0x00, |
| 300 | 0xF0, 0x00, |
| 301 | ]; |
| 302 | ts.psi(0x1000, 2, 1, &pmt); |
| 303 | |
| 304 | |
| 305 | let ac3 = fake_ac3_frame(); |
| 306 | for (i, chunk) in aus.chunks(4).enumerate() { |
| 307 | let mut payload = Vec::new(); |
| 308 | for au in chunk { |
| 309 | payload.extend_from_slice(&ac3); |
| 310 | payload.extend_from_slice(au); |
| 311 | } |
| 312 | ts.pes(0x1100, 90_000 + (i * 4) as u64 * 75, &payload); |
| 313 | } |
| 314 | |
| 315 | let path = fixture_dir().join("truehd_ac3_core.ts"); |
| 316 | std::fs::write(&path, &ts.out).unwrap(); |
| 317 | let mut d = open(&path); |
| 318 | let all = read_all(&mut d); |
| 319 | let stream = d |
| 320 | .streams() |
| 321 | .iter() |
| 322 | .find(|s| s.params.codec == CodecId::TrueHd) |
| 323 | .expect("the TrueHD stream is exposed") |
| 324 | .index; |
| 325 | let pkts: Vec<&Pkt> = all.iter().filter(|p| p.stream == stream).collect(); |
| 326 | assert_eq!( |
| 327 | pkts.len(), |
| 328 | aus.len(), |
| 329 | "one packet per access unit, no AC-3 frames" |
| 330 | ); |
| 331 | for (k, (p, au)) in pkts.iter().zip(&aus).enumerate() { |
| 332 | assert_eq!(p.data, *au, "access unit {k} bytes"); |
| 333 | |
| 334 | assert_eq!(p.pts, Some(90_000 + k as i64 * 75), "access unit {k} pts"); |
| 335 | } |
| 336 | } |