forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB255bb48Tahinli2 months ago3097 commits

cli.rs163 lines, 4.9 KiBHistoryRawDownload
1//! Self-test of the `oracle` CLI itself: drives the built binary the way a codec
2//! crate's test will, using ffmpeg as both the "our decoder" stand-in and the
3//! reference. Exercises the PASS and the FAIL path of every subcommand.
4//!
5//! Fixture-gated: fixtures/ is gitignored, so a checkout without
6//! `scripts/gen-fixtures.sh` run skips loudly instead of failing.
7
8use std::path::{Path, PathBuf};
9use std::process::{Command, Stdio};
10
11fn fixtures() -> PathBuf {
12 Path::new(env!("CARGO_MANIFEST_DIR"))
13 .join("../../fixtures")
14 .canonicalize()
15 .unwrap_or_else(|_| PathBuf::from("/nonexistent"))
16}
17
18// Per-test directory: the test binary runs its cases as threads of one process,
19// so a pid-only name would let one case delete another's raw files.
20fn workdir(name: &str) -> PathBuf {
21 let d = std::env::temp_dir().join(format!("oracle-selftest-{}-{name}", std::process::id()));
22 std::fs::create_dir_all(&d).unwrap();
23 d
24}
25
26/// Decode `input` to a raw file with ffmpeg — stands in for "our decoder's output".
27fn decode_raw(input: &Path, out: &Path, extra: &[&str]) {
28 let f = std::fs::File::create(out).unwrap();
29 let st = Command::new("ffmpeg")
30 .args(["-nostdin", "-y", "-v", "error", "-i"])
31 .arg(input)
32 .args(extra)
33 .arg("-")
34 .stdout(Stdio::from(f))
35 .status()
36 .expect("ffmpeg must be installed to run the oracle self-test");
37 assert!(st.success(), "ffmpeg failed decoding {}", input.display());
38}
39
40fn oracle(args: &[&str]) -> i32 {
41 Command::new(env!("CARGO_BIN_EXE_oracle"))
42 .args(args)
43 .status()
44 .expect("oracle binary")
45 .code()
46 .expect("oracle exited by signal")
47}
48
49fn require(paths: &[PathBuf]) -> bool {
50 for p in paths {
51 if !p.is_file() {
52 eprintln!(
53 "SKIP: missing fixture {} — run scripts/gen-fixtures.sh",
54 p.display()
55 );
56 return false;
57 }
58 }
59 true
60}
61
62const F32: &[&str] = &["-map", "0:a:0", "-f", "f32le", "-acodec", "pcm_f32le"];
63
64#[test]
65fn bit_exact_passes_on_same_decode_and_fails_on_a_different_one() {
66 let fx = fixtures();
67 let a = fx.join("audio/flac-stereo-48000.flac");
68 let b = fx.join("audio/flac-mono-48000.flac");
69 if !require(&[a.clone(), b.clone()]) {
70 return;
71 }
72 let w = workdir("bitexact");
73 let (r1, r2, r3) = (w.join("a1.raw"), w.join("a2.raw"), w.join("b.raw"));
74 decode_raw(&a, &r1, F32);
75 decode_raw(&a, &r2, F32);
76 decode_raw(&b, &r3, F32);
77
78 assert_eq!(
79 oracle(&["bit-exact", r1.to_str().unwrap(), r2.to_str().unwrap()]),
80 0,
81 "same file decoded twice must be bit-exact"
82 );
83 assert_eq!(
84 oracle(&["bit-exact", r1.to_str().unwrap(), r3.to_str().unwrap()]),
85 1,
86 "different fixtures must not be bit-exact"
87 );
88 let _ = std::fs::remove_dir_all(&w);
89}
90
91#[test]
92fn audio_compare_passes_against_its_own_source_and_fails_across_rates() {
93 let fx = fixtures();
94 let a = fx.join("audio/flac-stereo-48000.flac");
95 let b = fx.join("audio/flac-stereo-44100.flac");
96 if !require(&[a.clone(), b.clone()]) {
97 return;
98 }
99 let w = workdir("audio");
100 let (ra, rb) = (w.join("a.raw"), w.join("b.raw"));
101 decode_raw(&a, &ra, F32);
102 decode_raw(&b, &rb, F32);
103
104 assert_eq!(
105 oracle(&["audio-compare", ra.to_str().unwrap(), a.to_str().unwrap()]),
106 0,
107 "lossless decode vs its own source must correlate at 1.0"
108 );
109 // 44.1 kHz samples against a 48 kHz reference: wrong length and wrong phase.
110 assert_eq!(
111 oracle(&["audio-compare", rb.to_str().unwrap(), a.to_str().unwrap()]),
112 1,
113 "a different-rate decode must be rejected"
114 );
115 let _ = std::fs::remove_dir_all(&w);
116}
117
118#[test]
119fn video_compare_passes_against_its_own_source_and_fails_on_frame_count() {
120 let fx = fixtures();
121 let a = fx.join("video/h264-1080p-23.976-8bit.mp4");
122 let b = fx.join("video/h264-1080p-60-8bit.mp4");
123 if !require(&[a.clone(), b.clone()]) {
124 return;
125 }
126 let w = workdir("video");
127 let raw = w.join("a.raw");
128 decode_raw(
129 &a,
130 &raw,
131 &["-map", "0:v:0", "-f", "rawvideo", "-pix_fmt", "yuv420p"],
132 );
133
134 assert_eq!(
135 oracle(&[
136 "video-compare",
137 raw.to_str().unwrap(),
138 a.to_str().unwrap(),
139 "--pix-fmt",
140 "yuv420p",
141 ]),
142 0,
143 "decode vs its own source must be lossless (infinite PSNR)"
144 );
145 assert_eq!(
146 oracle(&[
147 "video-compare",
148 raw.to_str().unwrap(),
149 b.to_str().unwrap(),
150 "--pix-fmt",
151 "yuv420p",
152 ]),
153 1,
154 "48 frames against a 120-frame reference must fail"
155 );
156 let _ = std::fs::remove_dir_all(&w);
157}
158
159#[test]
160fn bad_usage_exits_two() {
161 assert_eq!(oracle(&["no-such-subcommand"]), 2);
162 assert_eq!(oracle(&["bit-exact", "only-one-arg"]), 2);
163}