//! Sample-for-sample equality with ffmpeg's TrueHD decoder on the committed //! //! The committed fixtures are made with ffmpeg's own TrueHD encoder //! (`-c:a truehd -strict -2`): 2.0 and 5.1 at 16-bit (s16p input) and 24-bit //! (s32p input), 48 kHz. The file name carries the channel count: `2ch` or `51`. //! //! Each file is decoded here and by ffmpeg (`-f s32le`), and every sample must use std::fs::File; use std::io::BufReader; use std::path::{Path, PathBuf}; use crate::common::raw_access_units; use ec_core::{CodecId, Demuxer}; use ec_matroska::MatroskaDemuxer; use ec_truehd::TrueHdDecoder; fn fixtures() -> PathBuf { Path::new(env!("CARGO_MANIFEST_DIR")).join("fixtures") } fn s32le(bytes: &[u8]) -> Vec { bytes .as_chunks::<4>() .0 .iter() .map(|c| i32::from_le_bytes(*c)) .collect() } /// Channel count from the file name: `2ch` or `51`. fn expected_channels(name: &str) -> usize { if name.contains("2ch") { 2 } else if name.contains("51") { 6 } else { panic!("fixture {name}: no channel count in the name") } } /// Decodes every access unit of the file's TrueHD track: (stream index, /// interleaved S32 samples, channels, sample rate). fn decode_ours(path: &Path) -> (u32, Vec, usize, u32) { let mut demux = MatroskaDemuxer::new(BufReader::new(File::open(path).unwrap())).unwrap(); let stream = demux .streams() .iter() .find(|s| s.params.codec == CodecId::TrueHd) .expect("a TrueHD track") .index; let mut decoder = TrueHdDecoder::new(); let mut samples = Vec::new(); let mut channels = 0; let mut rate = 0; loop { let packet = match demux.next_packet() { Ok(p) => p, Err(e) if e.is_eof() => break, Err(e) => panic!("{}: demux: {e}", path.display()), }; if packet.stream != stream { continue; } let frame = decoder .decode_access_unit(&packet.data) .unwrap_or_else(|e| panic!("{}: {e}", path.display())); if let Some(f) = frame { channels = f.channels(); rate = f.rate; samples.extend(s32le(&f.data[0])); } } let stats = decoder.check_stats(); assert_eq!( stats.lossless_check_failures, 0, "{}: {stats:?}", path.display() ); assert_eq!(stats.parity_failures, 0, "{}: {stats:?}", path.display()); assert_eq!( stats.restart_crc_failures, 0, "{}: {stats:?}", path.display() ); assert_eq!(stats.length_mismatches, 0, "{}: {stats:?}", path.display()); (stream, samples, channels, rate) } /// ffmpeg's decode of track `stream` as interleaved S32 little-endian. fn decode_ffmpeg(path: &Path, stream: u32) -> Vec { let out = ec_testkit::ffmpeg() .args(["-v", "error", "-nostdin", "-i"]) .arg(path) .args(["-map", &format!("0:{stream}"), "-f", "s32le", "-"]) .output() .expect("run ffmpeg"); assert!( out.status.success(), "ffmpeg {}: {}", path.display(), String::from_utf8_lossy(&out.stderr) ); s32le(&out.stdout) } #[test] fn fixtures_are_bit_exact_against_ffmpeg() { let dir = fixtures(); let mut paths: Vec = std::fs::read_dir(&dir) .expect("fixtures dir") .flatten() .map(|e| e.path()) .filter(|p| p.extension().is_some_and(|x| x == "mkv")) .collect(); paths.sort(); assert!(!paths.is_empty(), "no TrueHD fixtures in {}", dir.display()); for path in &paths { let name = path.file_name().unwrap().to_string_lossy().into_owned(); let want_channels = expected_channels(&name); let (stream, ours, channels, rate) = decode_ours(path); let reference = decode_ffmpeg(path, stream); assert_eq!(channels, want_channels, "{name}: channels"); assert_eq!(rate, 48_000, "{name}: rate"); assert!(!ours.is_empty(), "{name}: no samples decoded"); assert_eq!( ours.len(), reference.len(), "{name}: sample count vs ffmpeg" ); let mismatch = ours.iter().zip(&reference).position(|(a, b)| a != b); assert_eq!( mismatch, None, "{name}: first mismatch at sample {mismatch:?} (channel {:?})", mismatch.map(|i| i % channels) ); eprintln!( "{name}: {channels} ch, {} frames, {} samples, bit-exact", ours.len() / channels, ours.len() ); } } #[test] fn atmos_71_is_bit_exact_against_ffmpeg() { let path = ec_testkit::fixture("open/fate/truehd/atmos.thd"); let data = std::fs::read(&path).expect("read atmos.thd"); let mut decoder = TrueHdDecoder::new(); let mut ours = Vec::new(); let mut channels = 0; for au in raw_access_units(&data) { if let Some(f) = decoder.decode_access_unit(&au).expect("decode") { channels = f.channels(); ours.extend(s32le(&f.data[0])); } } assert_eq!(channels, 8, "atmos.thd channels"); let out = ec_testkit::ffmpeg() .args(["-v", "error", "-nostdin", "-i"]) .arg(&path) .args(["-f", "s32le", "-"]) .output() .expect("run ffmpeg"); assert!( out.status.success(), "ffmpeg: {}", String::from_utf8_lossy(&out.stderr) ); let reference = s32le(&out.stdout); assert_eq!(ours.len(), reference.len(), "sample count vs ffmpeg"); let mismatch = ours.iter().zip(&reference).position(|(a, b)| a != b); assert_eq!( mismatch, None, "first mismatch at sample {mismatch:?} (channel {:?})", mismatch.map(|i| i % channels) ); eprintln!( "atmos 7.1: {channels} ch, {} samples, bit-exact", ours.len() ); }