| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use std::path::{Path, PathBuf}; |
| 9 | use std::process::{Command, Stdio}; |
| 10 | |
| 11 | fn fixtures() -> PathBuf { |
| 12 | Path::new(env!("CARGO_MANIFEST_DIR")) |
| 13 | .join("../../fixtures") |
| 14 | .canonicalize() |
| 15 | .unwrap_or_else(|_| PathBuf::from("/nonexistent")) |
| 16 | } |
| 17 | |
| 18 | |
| 19 | |
| 20 | fn 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 | |
| 27 | fn 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 | |
| 40 | fn 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 | |
| 49 | fn 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 | |
| 62 | const F32: &[&str] = &["-map", "0:a:0", "-f", "f32le", "-acodec", "pcm_f32le"]; |
| 63 | |
| 64 | #[test] |
| 65 | fn 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] |
| 92 | fn 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 | |
| 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] |
| 119 | fn 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] |
| 160 | fn bad_usage_exits_two() { |
| 161 | assert_eq!(oracle(&["no-such-subcommand"]), 2); |
| 162 | assert_eq!(oracle(&["bit-exact", "only-one-arg"]), 2); |
| 163 | } |