| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | use std::collections::HashMap; |
| 19 | use std::process::{Command, Stdio}; |
| 20 | |
| 21 | const USAGE: &str = "\ |
| 22 | oracle — comparison harness (ffmpeg is test tooling only) |
| 23 | |
| 24 | oracle bit-exact <a> <b> |
| 25 | oracle audio-compare <ours.f32le.raw> <ref-file> [--channels N] [--min-corr F] [--max-rms F] |
| 26 | oracle video-compare <ours.raw> <ref-file> [--pix-fmt FMT] [--size WxH] [--min-psnr DB] |
| 27 | |
| 28 | Defaults: --min-corr 0.999 --max-rms 1e-3 --min-psnr 40 |
| 29 | Missing --channels/--pix-fmt/--size are probed from <ref-file> with ffprobe. |
| 30 | "; |
| 31 | |
| 32 | fn main() { |
| 33 | let argv: Vec<String> = std::env::args().skip(1).collect(); |
| 34 | let rc = match argv.first().map(String::as_str) { |
| 35 | Some("bit-exact") => run(cmd_bit_exact(&argv[1..])), |
| 36 | Some("audio-compare") => run(cmd_audio_compare(&argv[1..])), |
| 37 | Some("video-compare") => run(cmd_video_compare(&argv[1..])), |
| 38 | Some("-h") | Some("--help") => { |
| 39 | print!("{USAGE}"); |
| 40 | 0 |
| 41 | } |
| 42 | _ => { |
| 43 | eprint!("{USAGE}"); |
| 44 | 2 |
| 45 | } |
| 46 | }; |
| 47 | std::process::exit(rc); |
| 48 | } |
| 49 | |
| 50 | fn run(r: Result<bool, String>) -> i32 { |
| 51 | match r { |
| 52 | Ok(true) => 0, |
| 53 | Ok(false) => 1, |
| 54 | Err(e) => { |
| 55 | eprintln!("oracle: {e}"); |
| 56 | 2 |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | |
| 62 | |
| 63 | struct Args { |
| 64 | pos: Vec<String>, |
| 65 | flags: HashMap<String, String>, |
| 66 | } |
| 67 | |
| 68 | impl Args { |
| 69 | fn parse(argv: &[String]) -> Result<Self, String> { |
| 70 | let mut pos = Vec::new(); |
| 71 | let mut flags = HashMap::new(); |
| 72 | let mut it = argv.iter(); |
| 73 | while let Some(a) = it.next() { |
| 74 | if let Some(key) = a.strip_prefix("--") { |
| 75 | let v = it |
| 76 | .next() |
| 77 | .ok_or_else(|| format!("flag --{key} needs a value"))?; |
| 78 | flags.insert(key.to_string(), v.clone()); |
| 79 | } else { |
| 80 | pos.push(a.clone()); |
| 81 | } |
| 82 | } |
| 83 | Ok(Self { pos, flags }) |
| 84 | } |
| 85 | |
| 86 | fn num<T: std::str::FromStr>(&self, key: &str, default: T) -> Result<T, String> { |
| 87 | match self.flags.get(key) { |
| 88 | None => Ok(default), |
| 89 | Some(v) => v.parse().map_err(|_| format!("bad --{key} value: {v}")), |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | fn ffmpeg_raw(input: &str, extra: &[&str]) -> Result<Vec<u8>, String> { |
| 99 | let out = Command::new("ffmpeg") |
| 100 | .args(["-nostdin", "-v", "error", "-i", input]) |
| 101 | .args(extra) |
| 102 | .arg("-") |
| 103 | .stdout(Stdio::piped()) |
| 104 | .stderr(Stdio::inherit()) |
| 105 | .output() |
| 106 | .map_err(|e| format!("spawning ffmpeg: {e}"))?; |
| 107 | if !out.status.success() { |
| 108 | return Err(format!("ffmpeg failed on {input} ({})", out.status)); |
| 109 | } |
| 110 | if out.stdout.is_empty() { |
| 111 | return Err(format!("ffmpeg produced no data for {input}")); |
| 112 | } |
| 113 | Ok(out.stdout) |
| 114 | } |
| 115 | |
| 116 | |
| 117 | fn ffprobe(input: &str, select: &str, entries: &str) -> Result<HashMap<String, String>, String> { |
| 118 | let out = Command::new("ffprobe") |
| 119 | .args(["-v", "error", "-select_streams", select, "-show_entries"]) |
| 120 | .arg(format!("stream={entries}")) |
| 121 | .args(["-of", "default=nw=1"]) |
| 122 | .arg(input) |
| 123 | .stderr(Stdio::inherit()) |
| 124 | .output() |
| 125 | .map_err(|e| format!("spawning ffprobe: {e}"))?; |
| 126 | if !out.status.success() { |
| 127 | return Err(format!("ffprobe failed on {input} ({})", out.status)); |
| 128 | } |
| 129 | let mut map = HashMap::new(); |
| 130 | for line in String::from_utf8_lossy(&out.stdout).lines() { |
| 131 | if let Some((k, v)) = line.split_once('=') |
| 132 | && v != "N/A" |
| 133 | { |
| 134 | map.entry(k.to_string()).or_insert_with(|| v.to_string()); |
| 135 | } |
| 136 | } |
| 137 | Ok(map) |
| 138 | } |
| 139 | |
| 140 | fn read_file(path: &str) -> Result<Vec<u8>, String> { |
| 141 | std::fs::read(path).map_err(|e| format!("reading {path}: {e}")) |
| 142 | } |
| 143 | |
| 144 | |
| 145 | |
| 146 | fn cmd_bit_exact(argv: &[String]) -> Result<bool, String> { |
| 147 | let a = Args::parse(argv)?; |
| 148 | if a.pos.len() != 2 { |
| 149 | return Err("bit-exact needs exactly two files".into()); |
| 150 | } |
| 151 | let (x, y) = (read_file(&a.pos[0])?, read_file(&a.pos[1])?); |
| 152 | let first_diff = x.iter().zip(&y).position(|(p, q)| p != q); |
| 153 | let pass = x.len() == y.len() && first_diff.is_none(); |
| 154 | |
| 155 | println!("meta\ta\t{}", a.pos[0]); |
| 156 | println!("meta\tb\t{}", a.pos[1]); |
| 157 | println!("meta\tlen_a\t{}", x.len()); |
| 158 | println!("meta\tlen_b\t{}", y.len()); |
| 159 | match first_diff { |
| 160 | Some(off) => println!("diff\tfirst_offset\t{off}"), |
| 161 | None => println!("diff\tfirst_offset\t-"), |
| 162 | } |
| 163 | println!("verdict\t{}", if pass { "PASS" } else { "FAIL" }); |
| 164 | eprintln!( |
| 165 | "bit-exact: {} ({} vs {} bytes, first diff {})", |
| 166 | if pass { "PASS" } else { "FAIL" }, |
| 167 | x.len(), |
| 168 | y.len(), |
| 169 | first_diff.map_or("-".into(), |o| o.to_string()) |
| 170 | ); |
| 171 | Ok(pass) |
| 172 | } |
| 173 | |
| 174 | |
| 175 | |
| 176 | fn deinterleave_f32(buf: &[u8], channels: usize) -> Vec<Vec<f64>> { |
| 177 | let frames = buf.len() / 4 / channels; |
| 178 | let mut out = vec![Vec::with_capacity(frames); channels]; |
| 179 | for f in 0..frames { |
| 180 | for (c, ch) in out.iter_mut().enumerate() { |
| 181 | let i = (f * channels + c) * 4; |
| 182 | ch.push(f32::from_le_bytes([buf[i], buf[i + 1], buf[i + 2], buf[i + 3]]) as f64); |
| 183 | } |
| 184 | } |
| 185 | out |
| 186 | } |
| 187 | |
| 188 | |
| 189 | fn corr(a: &[f64], b: &[f64]) -> f64 { |
| 190 | let n = a.len().min(b.len()); |
| 191 | if n == 0 { |
| 192 | return 0.0; |
| 193 | } |
| 194 | let (ma, mb) = ( |
| 195 | a[..n].iter().sum::<f64>() / n as f64, |
| 196 | b[..n].iter().sum::<f64>() / n as f64, |
| 197 | ); |
| 198 | let (mut num, mut da, mut db) = (0.0, 0.0, 0.0); |
| 199 | for i in 0..n { |
| 200 | let (x, y) = (a[i] - ma, b[i] - mb); |
| 201 | num += x * y; |
| 202 | da += x * x; |
| 203 | db += y * y; |
| 204 | } |
| 205 | if da == 0.0 || db == 0.0 { |
| 206 | return if da == db && (ma - mb).abs() < 1e-12 { |
| 207 | 1.0 |
| 208 | } else { |
| 209 | 0.0 |
| 210 | }; |
| 211 | } |
| 212 | num / (da * db).sqrt() |
| 213 | } |
| 214 | |
| 215 | fn rms_delta(a: &[f64], b: &[f64]) -> (f64, f64) { |
| 216 | let n = a.len().min(b.len()); |
| 217 | if n == 0 { |
| 218 | return (f64::INFINITY, f64::INFINITY); |
| 219 | } |
| 220 | let mut sum = 0.0; |
| 221 | let mut peak: f64 = 0.0; |
| 222 | for i in 0..n { |
| 223 | let d = a[i] - b[i]; |
| 224 | sum += d * d; |
| 225 | peak = peak.max(d.abs()); |
| 226 | } |
| 227 | ((sum / n as f64).sqrt(), peak) |
| 228 | } |
| 229 | |
| 230 | fn cmd_audio_compare(argv: &[String]) -> Result<bool, String> { |
| 231 | let a = Args::parse(argv)?; |
| 232 | if a.pos.len() != 2 { |
| 233 | return Err("audio-compare needs <ours.raw> <ref-file>".into()); |
| 234 | } |
| 235 | let (ours_path, ref_path) = (&a.pos[0], &a.pos[1]); |
| 236 | let min_corr: f64 = a.num("min-corr", 0.999)?; |
| 237 | let max_rms: f64 = a.num("max-rms", 1e-3)?; |
| 238 | |
| 239 | let probe = ffprobe(ref_path, "a:0", "channels,sample_rate")?; |
| 240 | let channels: usize = match a.flags.get("channels") { |
| 241 | Some(v) => v.parse().map_err(|_| "bad --channels".to_string())?, |
| 242 | None => probe |
| 243 | .get("channels") |
| 244 | .ok_or("could not probe channel count; pass --channels")? |
| 245 | .parse() |
| 246 | .map_err(|_| "bad probed channel count".to_string())?, |
| 247 | }; |
| 248 | if channels == 0 { |
| 249 | return Err("channel count is zero".into()); |
| 250 | } |
| 251 | let rate = probe |
| 252 | .get("sample_rate") |
| 253 | .cloned() |
| 254 | .unwrap_or_else(|| "-".into()); |
| 255 | |
| 256 | let ours = read_file(ours_path)?; |
| 257 | let reference = ffmpeg_raw( |
| 258 | ref_path, |
| 259 | &["-map", "0:a:0", "-f", "f32le", "-acodec", "pcm_f32le"], |
| 260 | )?; |
| 261 | let (na, nb) = (ours.len() / 4 / channels, reference.len() / 4 / channels); |
| 262 | let (ca, cb) = ( |
| 263 | deinterleave_f32(&ours, channels), |
| 264 | deinterleave_f32(&reference, channels), |
| 265 | ); |
| 266 | |
| 267 | println!("meta\tours\t{ours_path}"); |
| 268 | println!("meta\tref\t{ref_path}"); |
| 269 | println!("meta\tchannels\t{channels}"); |
| 270 | println!("meta\tsample_rate\t{rate}"); |
| 271 | println!("meta\tframes_ours\t{na}"); |
| 272 | println!("meta\tframes_ref\t{nb}"); |
| 273 | |
| 274 | |
| 275 | |
| 276 | let len_ok = na.abs_diff(nb) as f64 <= 0.01 * na.max(nb) as f64; |
| 277 | let mut min_c = f64::INFINITY; |
| 278 | let mut max_r: f64 = 0.0; |
| 279 | for c in 0..channels { |
| 280 | let k = corr(&ca[c], &cb[c]); |
| 281 | let (r, p) = rms_delta(&ca[c], &cb[c]); |
| 282 | min_c = min_c.min(k); |
| 283 | max_r = max_r.max(r); |
| 284 | println!("ch\t{c}\tcorr\t{k:.6}\trms\t{r:.8}\tpeak\t{p:.8}"); |
| 285 | } |
| 286 | let pass = len_ok && min_c >= min_corr && max_r <= max_rms; |
| 287 | println!( |
| 288 | "verdict\t{}\tmin_corr={min_c:.6}\tmax_rms={max_r:.8}\tlen_ok={len_ok}", |
| 289 | if pass { "PASS" } else { "FAIL" } |
| 290 | ); |
| 291 | eprintln!( |
| 292 | "audio-compare: {} — {channels}ch @{rate}, min corr {min_c:.6} (>= {min_corr}), \ |
| 293 | max RMS delta {max_r:.8} (<= {max_rms}), lengths {na}/{nb}", |
| 294 | if pass { "PASS" } else { "FAIL" } |
| 295 | ); |
| 296 | Ok(pass) |
| 297 | } |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | struct PixGeom { |
| 304 | samples: usize, |
| 305 | luma: usize, |
| 306 | bps: usize, |
| 307 | max: f64, |
| 308 | } |
| 309 | |
| 310 | fn pix_geom(pix_fmt: &str, w: usize, h: usize) -> Result<PixGeom, String> { |
| 311 | let wh = w * h; |
| 312 | let g = |samples: usize, luma: usize, bps: usize, max: f64| PixGeom { |
| 313 | samples, |
| 314 | luma, |
| 315 | bps, |
| 316 | max, |
| 317 | }; |
| 318 | |
| 319 | match pix_fmt { |
| 320 | "nv12" | "nv21" => return Ok(g(wh * 3 / 2, wh, 1, 255.0)), |
| 321 | "p010le" | "p010be" => return Ok(g(wh * 3 / 2, wh, 2, 1023.0)), |
| 322 | "p016le" | "p016be" => return Ok(g(wh * 3 / 2, wh, 2, 65535.0)), |
| 323 | _ => {} |
| 324 | } |
| 325 | let mut base = pix_fmt; |
| 326 | let mut bps = 1; |
| 327 | let mut max = 255.0; |
| 328 | for (suffix, depth) in [("9", 9u32), ("10", 10), ("12", 12), ("14", 14), ("16", 16)] { |
| 329 | for endian in ["le", "be"] { |
| 330 | let s = format!("{suffix}{endian}"); |
| 331 | if let Some(b) = pix_fmt.strip_suffix(&s) { |
| 332 | base = b; |
| 333 | bps = 2; |
| 334 | max = ((1u32 << depth) - 1) as f64; |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | let geom = match base { |
| 339 | "yuv420p" | "yuvj420p" => g(wh * 3 / 2, wh, bps, max), |
| 340 | "yuv422p" | "yuvj422p" => g(wh * 2, wh, bps, max), |
| 341 | "yuv444p" | "yuvj444p" | "gbrp" => g(wh * 3, wh, bps, max), |
| 342 | "gray" => g(wh, wh, bps, max), |
| 343 | |
| 344 | "yuyv422" | "uyvy422" => g(wh * 2, 0, bps, max), |
| 345 | "rgb24" | "bgr24" => g(wh * 3, 0, bps, max), |
| 346 | "rgba" | "bgra" | "argb" | "abgr" => g(wh * 4, 0, bps, max), |
| 347 | _ => return Err(format!("unsupported pix_fmt for raw compare: {pix_fmt}")), |
| 348 | }; |
| 349 | Ok(geom) |
| 350 | } |
| 351 | |
| 352 | fn sample(buf: &[u8], i: usize, bps: usize) -> f64 { |
| 353 | if bps == 1 { |
| 354 | buf[i] as f64 |
| 355 | } else { |
| 356 | u16::from_le_bytes([buf[2 * i], buf[2 * i + 1]]) as f64 |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | |
| 361 | fn psnr(a: &[u8], b: &[u8], off: usize, n: usize, bps: usize, max: f64) -> f64 { |
| 362 | let mut sum = 0.0; |
| 363 | for i in off..off + n { |
| 364 | let d = sample(a, i, bps) - sample(b, i, bps); |
| 365 | sum += d * d; |
| 366 | } |
| 367 | let mse = sum / n as f64; |
| 368 | if mse == 0.0 { |
| 369 | f64::INFINITY |
| 370 | } else { |
| 371 | 10.0 * (max * max / mse).log10() |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | fn cmd_video_compare(argv: &[String]) -> Result<bool, String> { |
| 376 | let a = Args::parse(argv)?; |
| 377 | if a.pos.len() != 2 { |
| 378 | return Err("video-compare needs <ours.raw> <ref-file>".into()); |
| 379 | } |
| 380 | let (ours_path, ref_path) = (&a.pos[0], &a.pos[1]); |
| 381 | let min_psnr: f64 = a.num("min-psnr", 40.0)?; |
| 382 | |
| 383 | let probe = ffprobe(ref_path, "v:0", "width,height,pix_fmt")?; |
| 384 | let pix = match a.flags.get("pix-fmt") { |
| 385 | Some(v) => v.clone(), |
| 386 | None => probe |
| 387 | .get("pix_fmt") |
| 388 | .cloned() |
| 389 | .ok_or("could not probe pix_fmt; pass --pix-fmt")?, |
| 390 | }; |
| 391 | let (w, h) = match a.flags.get("size") { |
| 392 | Some(s) => { |
| 393 | let (ws, hs) = s.split_once('x').ok_or("--size must look like 1920x1080")?; |
| 394 | ( |
| 395 | ws.parse::<usize>().map_err(|_| "bad --size width")?, |
| 396 | hs.parse::<usize>().map_err(|_| "bad --size height")?, |
| 397 | ) |
| 398 | } |
| 399 | None => ( |
| 400 | probe |
| 401 | .get("width") |
| 402 | .ok_or("could not probe width; pass --size")? |
| 403 | .parse() |
| 404 | .map_err(|_| "bad probed width".to_string())?, |
| 405 | probe |
| 406 | .get("height") |
| 407 | .ok_or("could not probe height; pass --size")? |
| 408 | .parse() |
| 409 | .map_err(|_| "bad probed height".to_string())?, |
| 410 | ), |
| 411 | }; |
| 412 | |
| 413 | let geom = pix_geom(&pix, w, h)?; |
| 414 | let frame_bytes = geom.samples * geom.bps; |
| 415 | if frame_bytes == 0 { |
| 416 | return Err("frame size computed as zero".into()); |
| 417 | } |
| 418 | |
| 419 | let ours = read_file(ours_path)?; |
| 420 | let reference = ffmpeg_raw( |
| 421 | ref_path, |
| 422 | &["-map", "0:v:0", "-f", "rawvideo", "-pix_fmt", &pix], |
| 423 | )?; |
| 424 | let (fa, fb) = (ours.len() / frame_bytes, reference.len() / frame_bytes); |
| 425 | let frames = fa.min(fb); |
| 426 | |
| 427 | println!("meta\tours\t{ours_path}"); |
| 428 | println!("meta\tref\t{ref_path}"); |
| 429 | println!("meta\tpix_fmt\t{pix}"); |
| 430 | println!("meta\tdims\t{w}x{h}"); |
| 431 | println!("meta\tframe_bytes\t{frame_bytes}"); |
| 432 | println!("meta\tframes_ours\t{fa}"); |
| 433 | println!("meta\tframes_ref\t{fb}"); |
| 434 | if frames == 0 { |
| 435 | println!("verdict\tFAIL\tno complete frame in common"); |
| 436 | eprintln!("video-compare: FAIL — no complete frame in common ({fa} vs {fb})"); |
| 437 | return Ok(false); |
| 438 | } |
| 439 | |
| 440 | let luma_n = if geom.luma == 0 { |
| 441 | geom.samples |
| 442 | } else { |
| 443 | geom.luma |
| 444 | }; |
| 445 | let mut min_y = f64::INFINITY; |
| 446 | let mut sum_y = 0.0; |
| 447 | let mut min_all = f64::INFINITY; |
| 448 | let mut sum_all = 0.0; |
| 449 | let mut finite_y = 0usize; |
| 450 | let mut finite_all = 0usize; |
| 451 | for f in 0..frames { |
| 452 | let off = f * geom.samples; |
| 453 | let py = psnr(&ours, &reference, off, luma_n, geom.bps, geom.max); |
| 454 | let pa = psnr(&ours, &reference, off, geom.samples, geom.bps, geom.max); |
| 455 | min_y = min_y.min(py); |
| 456 | min_all = min_all.min(pa); |
| 457 | if py.is_finite() { |
| 458 | sum_y += py; |
| 459 | finite_y += 1; |
| 460 | } |
| 461 | if pa.is_finite() { |
| 462 | sum_all += pa; |
| 463 | finite_all += 1; |
| 464 | } |
| 465 | println!("frame\t{f}\tpsnr_y\t{py:.4}\tpsnr_all\t{pa:.4}"); |
| 466 | } |
| 467 | |
| 468 | |
| 469 | let mean_y = if finite_y == 0 { |
| 470 | f64::INFINITY |
| 471 | } else { |
| 472 | sum_y / finite_y as f64 |
| 473 | }; |
| 474 | let mean_all = if finite_all == 0 { |
| 475 | f64::INFINITY |
| 476 | } else { |
| 477 | sum_all / finite_all as f64 |
| 478 | }; |
| 479 | let len_ok = fa == fb; |
| 480 | let pass = len_ok && min_y >= min_psnr; |
| 481 | println!("stats\tpsnr_y\tmin\t{min_y:.4}\tmean\t{mean_y:.4}"); |
| 482 | println!("stats\tpsnr_all\tmin\t{min_all:.4}\tmean\t{mean_all:.4}"); |
| 483 | println!( |
| 484 | "verdict\t{}\tmin_psnr_y={min_y:.4}\tframes={frames}\tlen_ok={len_ok}", |
| 485 | if pass { "PASS" } else { "FAIL" } |
| 486 | ); |
| 487 | eprintln!( |
| 488 | "video-compare: {} — {frames} frames {w}x{h} {pix}, min luma PSNR {min_y:.4} dB \ |
| 489 | (>= {min_psnr}), mean {mean_y:.4} dB, frame counts {fa}/{fb}", |
| 490 | if pass { "PASS" } else { "FAIL" } |
| 491 | ); |
| 492 | Ok(pass) |
| 493 | } |
| 494 | |
| 495 | #[cfg(test)] |
| 496 | mod tests { |
| 497 | use super::*; |
| 498 | |
| 499 | #[test] |
| 500 | fn corr_identical_and_inverted() { |
| 501 | let a = [0.0, 1.0, -1.0, 0.5, -0.25]; |
| 502 | let inv: Vec<f64> = a.iter().map(|x| -x).collect(); |
| 503 | assert!((corr(&a, &a) - 1.0).abs() < 1e-12); |
| 504 | assert!((corr(&a, &inv) + 1.0).abs() < 1e-12); |
| 505 | |
| 506 | assert_eq!(corr(&[0.0; 4], &[0.0; 4]), 1.0); |
| 507 | assert_eq!(corr(&[0.0; 4], &[1.0; 4]), 0.0); |
| 508 | } |
| 509 | |
| 510 | #[test] |
| 511 | fn rms_and_peak_delta() { |
| 512 | let (r, p) = rms_delta(&[0.0, 0.0, 0.0], &[0.0, 0.0, 0.0]); |
| 513 | assert_eq!((r, p), (0.0, 0.0)); |
| 514 | let (r, p) = rms_delta(&[1.0, -1.0], &[0.0, 0.0]); |
| 515 | assert!((r - 1.0).abs() < 1e-12 && (p - 1.0).abs() < 1e-12); |
| 516 | } |
| 517 | |
| 518 | #[test] |
| 519 | fn psnr_identical_is_infinite_and_differing_is_finite() { |
| 520 | let a = vec![10u8, 20, 30, 40]; |
| 521 | assert!(psnr(&a, &a, 0, 4, 1, 255.0).is_infinite()); |
| 522 | let b = vec![11u8, 21, 31, 41]; |
| 523 | let p = psnr(&a, &b, 0, 4, 1, 255.0); |
| 524 | |
| 525 | assert!((p - 48.1308).abs() < 1e-3, "psnr={p}"); |
| 526 | |
| 527 | let x = [0u8, 1, 0, 1]; |
| 528 | let y = [1u8, 1, 0, 1]; |
| 529 | assert!(psnr(&x, &y, 0, 2, 2, 1023.0).is_finite()); |
| 530 | } |
| 531 | |
| 532 | #[test] |
| 533 | fn pix_geom_covers_the_generated_fixture_formats() { |
| 534 | let (w, h) = (1920, 1080); |
| 535 | let g = pix_geom("yuv420p", w, h).unwrap(); |
| 536 | assert_eq!( |
| 537 | (g.samples, g.luma, g.bps, g.max), |
| 538 | (w * h * 3 / 2, w * h, 1, 255.0) |
| 539 | ); |
| 540 | let g = pix_geom("yuv420p10le", w, h).unwrap(); |
| 541 | assert_eq!( |
| 542 | (g.samples, g.luma, g.bps, g.max), |
| 543 | (w * h * 3 / 2, w * h, 2, 1023.0) |
| 544 | ); |
| 545 | let g = pix_geom("p010le", w, h).unwrap(); |
| 546 | assert_eq!((g.samples, g.bps, g.max), (w * h * 3 / 2, 2, 1023.0)); |
| 547 | assert_eq!(pix_geom("yuv444p", w, h).unwrap().samples, w * h * 3); |
| 548 | assert_eq!(pix_geom("yuyv422", w, h).unwrap().luma, 0); |
| 549 | assert!(pix_geom("no_such_fmt", w, h).is_err()); |
| 550 | } |
| 551 | |
| 552 | #[test] |
| 553 | fn deinterleave_splits_channels() { |
| 554 | let mut buf = Vec::new(); |
| 555 | for f in 0..3u32 { |
| 556 | for c in 0..2u32 { |
| 557 | buf.extend_from_slice(&((f * 10 + c) as f32).to_le_bytes()); |
| 558 | } |
| 559 | } |
| 560 | let ch = deinterleave_f32(&buf, 2); |
| 561 | assert_eq!(ch[0], vec![0.0, 10.0, 20.0]); |
| 562 | assert_eq!(ch[1], vec![1.0, 11.0, 21.0]); |
| 563 | } |
| 564 | |
| 565 | #[test] |
| 566 | fn args_parse_flags_and_positionals() { |
| 567 | let argv: Vec<String> = ["a.raw", "b.mp4", "--min-corr", "0.5"] |
| 568 | .iter() |
| 569 | .map(|s| s.to_string()) |
| 570 | .collect(); |
| 571 | let a = Args::parse(&argv).unwrap(); |
| 572 | assert_eq!(a.pos, ["a.raw", "b.mp4"]); |
| 573 | assert_eq!(a.num::<f64>("min-corr", 0.999).unwrap(), 0.5); |
| 574 | assert_eq!(a.num::<f64>("max-rms", 1e-3).unwrap(), 1e-3); |
| 575 | assert!(Args::parse(&["--dangling".to_string()]).is_err()); |
| 576 | } |
| 577 | } |