forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

ffmpeg_parity.rs93 lines, 7.5 KiBHistoryRawDownload
1#![allow(clippy::needless_range_loop, clippy::field_reassign_with_default, clippy::too_many_arguments, clippy::type_complexity)]
2//! Byte-exact comparison with ffmpeg's mpeg2video / mpeg1video decoder on
3//! streams ffmpeg's own encoder produces from synthetic sources.
4
5mod common;
6use common::*;
7
8struct Case {
9 tag: &'static str,
10 size: &'static str,
11 frames: u32,
12 src: &'static str,
13 enc: &'static [&'static str],
14 pix: &'static str,
15}
16
17const CASES: &[Case] = &[
18 Case { tag: "prog_i", size: "176x144", frames: 3, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "1", "-qscale:v", "3"], pix: "yuv420p" },
19 Case { tag: "prog_ip", size: "176x144", frames: 12, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "6", "-bf", "0", "-qscale:v", "4"], pix: "yuv420p" },
20 Case { tag: "prog_ipb", size: "176x144", frames: 14, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "9", "-bf", "2", "-qscale:v", "4"], pix: "yuv420p" },
21 Case { tag: "mpeg1_ipb", size: "176x144", frames: 14, src: "testsrc2", enc: &["-c:v", "mpeg1video", "-g", "9", "-bf", "2", "-qscale:v", "4"], pix: "yuv420p" },
22 Case { tag: "mpeg1_noise", size: "176x144", frames: 10, src: "testsrc2,noise=alls=40:allf=t", enc: &["-c:v", "mpeg1video", "-g", "5", "-bf", "2", "-qscale:v", "2"], pix: "yuv420p" },
23 Case { tag: "noise_q1", size: "176x144", frames: 8, src: "testsrc2,noise=alls=60:allf=t", enc: &["-c:v", "mpeg2video", "-g", "4", "-bf", "1", "-qscale:v", "1"], pix: "yuv420p" },
24 Case { tag: "noise_q31", size: "176x144", frames: 8, src: "testsrc2,noise=alls=60:allf=t", enc: &["-c:v", "mpeg2video", "-g", "4", "-bf", "1", "-qscale:v", "31"], pix: "yuv420p" },
25 Case { tag: "ilace_tff", size: "352x288", frames: 10, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "6", "-bf", "2", "-qscale:v", "4", "-flags", "+ildct+ilme", "-top", "1"], pix: "yuv420p" },
26 Case { tag: "ilace_bff", size: "352x288", frames: 10, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "6", "-bf", "2", "-qscale:v", "4", "-flags", "+ildct+ilme", "-top", "0"], pix: "yuv420p" },
27 Case { tag: "ilace_ildct_only", size: "352x288", frames: 8, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "4", "-bf", "1", "-qscale:v", "4", "-flags", "+ildct", "-top", "1"], pix: "yuv420p" },
28 Case { tag: "altscan", size: "352x288", frames: 8, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "4", "-bf", "1", "-qscale:v", "4", "-alternate_scan", "1", "-flags", "+ildct+ilme", "-top", "1"], pix: "yuv420p" },
29 Case { tag: "intra_vlc", size: "176x144", frames: 8, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "4", "-bf", "1", "-qscale:v", "3", "-intra_vlc", "1"], pix: "yuv420p" },
30 Case { tag: "nonlinear", size: "176x144", frames: 8, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "4", "-bf", "1", "-qscale:v", "6", "-non_linear_quant", "1", "-qmax", "28"], pix: "yuv420p" },
31 Case { tag: "dc8", size: "176x144", frames: 6, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "3", "-bf", "1", "-qscale:v", "3", "-intra_dc_precision", "0"], pix: "yuv420p" },
32 Case { tag: "dc9", size: "176x144", frames: 6, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "3", "-bf", "1", "-qscale:v", "3", "-intra_dc_precision", "1"], pix: "yuv420p" },
33 Case { tag: "dc10", size: "176x144", frames: 6, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "3", "-bf", "1", "-qscale:v", "3", "-intra_dc_precision", "2"], pix: "yuv420p" },
34 Case { tag: "dc11", size: "176x144", frames: 6, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "3", "-bf", "1", "-qscale:v", "3", "-intra_dc_precision", "3"], pix: "yuv420p" },
35 Case { tag: "all_tools", size: "352x288", frames: 10, src: "testsrc2,noise=alls=20:allf=t", enc: &["-c:v", "mpeg2video", "-g", "6", "-bf", "2", "-qscale:v", "5", "-alternate_scan", "1", "-intra_vlc", "1", "-non_linear_quant", "1", "-qmax", "28", "-intra_dc_precision", "2", "-flags", "+ildct+ilme", "-top", "0"], pix: "yuv420p" },
36 Case { tag: "p422", size: "176x144", frames: 8, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "4", "-bf", "1", "-qscale:v", "3"], pix: "yuv422p" },
37 Case { tag: "p422_ilace", size: "352x288", frames: 8, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "4", "-bf", "1", "-qscale:v", "3", "-flags", "+ildct+ilme", "-top", "1"], pix: "yuv422p" },
38 Case { tag: "p422_intra", size: "352x288", frames: 4, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "1", "-qscale:v", "2", "-intra_vlc", "1", "-intra_dc_precision", "3"], pix: "yuv422p" },
39 Case { tag: "sd_pal", size: "720x576", frames: 8, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "6", "-bf", "2", "-b:v", "6M", "-flags", "+ildct+ilme", "-top", "1"], pix: "yuv420p" },
40 Case { tag: "sd_ntsc", size: "720x480", frames: 8, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "6", "-bf", "2", "-b:v", "6M", "-flags", "+ildct+ilme", "-top", "0"], pix: "yuv420p" },
41 Case { tag: "hd_1080i", size: "1920x1080", frames: 6, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "6", "-bf", "2", "-b:v", "20M", "-flags", "+ildct+ilme", "-top", "1"], pix: "yuv420p" },
42 Case { tag: "hd_1080p_422", size: "1920x1080", frames: 4, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "4", "-bf", "1", "-b:v", "40M"], pix: "yuv422p" },
43 Case { tag: "mandel", size: "352x288", frames: 8, src: "mandelbrot", enc: &["-c:v", "mpeg2video", "-g", "5", "-bf", "2", "-qscale:v", "3", "-flags", "+ildct+ilme"], pix: "yuv420p" },
44 Case { tag: "odd_size", size: "200x120", frames: 6, src: "testsrc2", enc: &["-c:v", "mpeg2video", "-g", "3", "-bf", "1", "-qscale:v", "4"], pix: "yuv420p" },
45 Case { tag: "mv_big", size: "352x288", frames: 10, src: "testsrc2,scroll=horizontal=0.05:vertical=0.03", enc: &["-c:v", "mpeg2video", "-g", "10", "-bf", "2", "-qscale:v", "4", "-me_range", "64", "-flags", "+ildct+ilme"], pix: "yuv420p" },
46 Case { tag: "mpeg1_big", size: "352x288", frames: 10, src: "testsrc2", enc: &["-c:v", "mpeg1video", "-g", "10", "-bf", "2", "-b:v", "1500k"], pix: "yuv420p" },
47];
48
49fn generate(c: &Case) -> Option<std::path::PathBuf> {
50 let dir = scratch(c.tag);
51 let out = dir.join("in.m2v");
52 let (base, chain) = match c.src.split_once(',') {
53 Some((b, rest)) => (b, format!(",{rest}")),
54 None => (c.src, String::new()),
55 };
56 let lavfi = format!("{base}=size={}:rate=25{chain}", c.size);
57 let frames = c.frames.to_string();
58 let mut args: Vec<&str> = vec!["-f", "lavfi", "-i", &lavfi, "-frames:v", &frames, "-pix_fmt", c.pix];
59 args.extend_from_slice(c.enc);
60 let fmt = if c.enc.contains(&"mpeg1video") { "mpeg1video" } else { "mpeg2video" };
61 args.extend_from_slice(&["-f", fmt, out.to_str().unwrap()]);
62 if std::env::var_os("EC_VERBOSE").is_some() { eprintln!("ffmpeg {args:?}"); }
63 if run(&args) { Some(out) } else { None }
64}
65
66#[test]
67fn parity_matrix() {
68 if !have_ffmpeg() {
69 assert!(!require(), "ffmpeg missing");
70 eprintln!("SKIP: no ffmpeg");
71 return;
72 }
73 let only = std::env::var("EC_MPEG2_CASE").ok();
74 let mut failures = Vec::new();
75 for c in CASES {
76 if only.as_deref().is_some_and(|o| o != c.tag) {
77 continue;
78 }
79 let Some(es) = generate(c) else {
80 assert!(!require(), "{}: ffmpeg cannot encode", c.tag);
81 eprintln!("SKIP {}", c.tag);
82 continue;
83 };
84 match compare(c.tag, &es, c.pix, &[]) {
85 Ok(n) => eprintln!("OK {} ({n} frames)", c.tag),
86 Err(e) => {
87 eprintln!("FAIL {}: {e}", c.tag);
88 failures.push(c.tag);
89 }
90 }
91 }
92 assert!(failures.is_empty(), "mismatches: {failures:?}");
93}