forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

fate_vectors.rs126 lines, 4.7 KiBHistoryRawDownload
1#![allow(clippy::chunks_exact_to_as_chunks, clippy::manual_is_multiple_of)]
2//! The public FFmpeg FATE DTS samples (the dcadec conformance suite) against
3//! ffmpeg's integer decoder, sample for sample: core 5.1, XCH (6.1), X96 (96
4//! kHz, alone and with XCH), the core inside XBR and XLL (DTS-HD) streams and
5//! a DTS-ES elementary stream.
6//!
7//! The samples are not committed. Fetch them with
8//! `devscripts/fate.sh <dir>` and point `EC_DTS_FATE` at the directory
9//! (default `fixtures/audio/dts-fate` at the repository root). A missing
10//! directory or ffmpeg skips loudly; the committed fixtures in
11//! `ffmpeg_exact.rs` are the gate that fails on absence.
12
13use std::path::{Path, PathBuf};
14use std::process::Command;
15
16use ec_dts::DtsDecoder;
17
18fn dir() -> PathBuf {
19 match std::env::var("EC_DTS_FATE") {
20 Ok(p) => PathBuf::from(p),
21 Err(_) => Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/audio/dts-fate"),
22 }
23}
24
25/// The STRMDATA chunk of a `.dtshd` file, or the whole buffer.
26fn strmdata(buf: &[u8]) -> &[u8] {
27 if !buf.starts_with(b"DTSHDHDR") {
28 return buf;
29 }
30 let mut p = 0;
31 while p + 16 <= buf.len() {
32 let size = u64::from_be_bytes(buf[p + 8..p + 16].try_into().unwrap()) as usize;
33 if &buf[p..p + 8] == b"STRMDATA" {
34 return &buf[p + 16..(p + 16 + size).min(buf.len())];
35 }
36 p += 16 + size;
37 }
38 buf
39}
40
41fn decode_ours(data: &[u8]) -> (Vec<i32>, usize, u32) {
42 let mut dec = DtsDecoder::new();
43 let (mut out, mut channels, mut rate) = (Vec::new(), 0, 0);
44 let mut pos = 0;
45 while pos + 4 < data.len() {
46 let Some((s, _)) = ec_dts::find_sync(&data[pos..]) else {
47 break;
48 };
49 pos += s;
50 let Ok(h) = ec_dts::parse_frame_header(&data[pos..]) else {
51 pos += 1;
52 continue;
53 };
54 let end = ec_dts::find_sync(&data[pos + h.stream_frame_size.min(data.len() - pos)..])
55 .map(|(o, _)| pos + h.stream_frame_size + o)
56 .unwrap_or(data.len());
57 let a = dec.decode_frame(&data[pos..end]).expect("decode");
58 channels = a.layout.channel_count();
59 rate = a.rate;
60 for c in a.data[0].chunks_exact(4) {
61 let v = f32::from_le_bytes([c[0], c[1], c[2], c[3]]);
62 out.push((v as f64 * 8388608.0).round() as i32);
63 }
64 pos = end;
65 }
66 (out, channels, rate)
67}
68
69fn decode_ffmpeg(path: &Path, core_only: bool) -> Vec<i32> {
70 let mut cmd = Command::new("ffmpeg");
71 cmd.args(["-v", "error", "-nostdin", "-flags", "+bitexact"]);
72 if core_only {
73 cmd.args(["-core_only", "1"]);
74 }
75 let out = cmd.arg("-i").arg(path).args(["-f", "s32le", "-"]).output().unwrap();
76 assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
77 out.stdout
78 .chunks_exact(4)
79 .map(|c| i32::from_le_bytes([c[0], c[1], c[2], c[3]]) >> 8)
80 .collect()
81}
82
83#[test]
84fn fate_vectors_are_bit_exact_against_ffmpeg() {
85 let d = dir();
86 let ffmpeg = Command::new("ffmpeg").arg("-version").output().is_ok();
87 if !d.is_dir() || !ffmpeg {
88 eprintln!("skipping: FATE samples ({}) or ffmpeg missing", d.display());
89 return;
90 }
91 // (file, ffmpeg -core_only, leading samples ffmpeg trims from .dtshd)
92 let cases = [
93 ("core_51_24_48_768_0.dtshd", true, 1024),
94 ("core_51_24_48_768_1.dtshd", true, 1024),
95 ("xch_61_24_48_768.dtshd", false, 1024),
96 ("x96_51_24_96_1509.dtshd", false, 2048),
97 ("x96_xch_61_24_96_3840.dtshd", false, 2048),
98 ("xbr_51_24_48_3840.dtshd", true, 1024),
99 ("xll_51_24_48_768.dtshd", true, 1024),
100 ("dts_es.dts", false, 0),
101 ];
102 let mut ran = 0;
103 for (name, core_only, skip) in cases {
104 let path = d.join(name);
105 let Ok(raw) = std::fs::read(&path) else {
106 eprintln!("skipping {name}: not present");
107 continue;
108 };
109 let (ours, ch, rate) = decode_ours(strmdata(&raw));
110 let reference = decode_ffmpeg(&path, core_only);
111 // ours is front-to-back; ffmpeg puts the back centre of a 6.1 stream
112 // before the side surrounds.
113 let perm: Vec<usize> = if ch == 7 { vec![0, 1, 2, 3, 6, 4, 5] } else { (0..ch).collect() };
114 let ours: Vec<i32> = ours
115 .chunks_exact(ch)
116 .skip(skip)
117 .flat_map(|f| perm.iter().map(|&p| f[p]).collect::<Vec<_>>())
118 .collect();
119 assert_eq!(ours.len(), reference.len(), "{name}: sample count");
120 let bad = ours.iter().zip(&reference).position(|(a, b)| a != b);
121 assert_eq!(bad, None, "{name}: first mismatch at {bad:?}");
122 eprintln!("{name}: {ch} ch {rate} Hz {} samples/ch bit-exact", ours.len() / ch);
123 ran += 1;
124 }
125 eprintln!("{ran} FATE vectors compared");
126}