edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·370.3 MiB·9b67700·Tahinli·3 days ago·4021 commits
| 1 | //! Byte-exact comparison with ffmpeg's `prores` decoder on streams ffmpeg's own |
| 2 | //! `prores_ks` / `prores_aw` encoders produce from synthetic sources, via .mov and ec-mp4. |
| 3 | |
| 4 | mod common; |
| 5 | use common::*; |
| 6 | |
| 7 | struct Case { |
| 8 | tag: &'static str, |
| 9 | size: &'static str, |
| 10 | frames: u32, |
| 11 | src: &'static str, |
| 12 | enc: &'static [&'static str], |
| 13 | /// Encoder input pixel format. |
| 14 | pix: &'static str, |
| 15 | /// What ffmpeg decodes to / we are compared in. |
| 16 | out: &'static str, |
| 17 | } |
| 18 | |
| 19 | macro_rules! c { |
| 20 | ($tag:expr, $size:expr, $n:expr, $src:expr, [$($e:expr),*], $pix:expr, $out:expr) => { |
| 21 | Case { tag: $tag, size: $size, frames: $n, src: $src, enc: &["-c:v", $($e),*], pix: $pix, out: $out } |
| 22 | }; |
| 23 | } |
| 24 | |
| 25 | const P422: &str = "yuv422p10le"; |
| 26 | const P444: &str = "yuv444p10le"; |
| 27 | const PA: &str = "yuva444p10le"; |
| 28 | const O422: &str = "yuv422p10le"; |
| 29 | const O444: &str = "yuv444p12le"; |
| 30 | const OA: &str = "yuva444p12le"; |
| 31 | |
| 32 | const CASES: &[Case] = &[ |
| 33 | c!("ks_proxy", "96x64", 2, "testsrc2", ["prores_ks", "-profile", "0"], P422, O422), |
| 34 | c!("ks_lt", "96x64", 2, "testsrc2", ["prores_ks", "-profile", "1"], P422, O422), |
| 35 | c!("ks_std", "96x64", 2, "testsrc2", ["prores_ks", "-profile", "2"], P422, O422), |
| 36 | c!("ks_hq", "96x64", 2, "testsrc2", ["prores_ks", "-profile", "3"], P422, O422), |
| 37 | c!("ks_4444", "96x64", 2, "testsrc2", ["prores_ks", "-profile", "4"], P444, O444), |
| 38 | c!("ks_4444xq", "96x64", 2, "testsrc2", ["prores_ks", "-profile", "5"], P444, O444), |
| 39 | c!("ks_4444_alpha16", "96x64", 2, "testsrc2,format=yuva444p10le,geq=lum='lum(X,Y)':cb='cb(X,Y)':cr='cr(X,Y)':a='mod(X*13+Y*7,1024)'", ["prores_ks", "-profile", "4", "-alpha_bits", "16"], PA, OA), |
| 40 | c!("ks_4444_alpha8", "96x64", 2, "testsrc2,format=yuva444p10le,geq=lum='lum(X,Y)':cb='cb(X,Y)':cr='cr(X,Y)':a='if(gt(X,40),1023,mod(X*5,1024))'", ["prores_ks", "-profile", "4", "-alpha_bits", "8"], PA, OA), |
| 41 | c!("ks_4444xq_alpha", "100x70", 2, "testsrc2,format=yuva444p10le,geq=lum='lum(X,Y)':cb='cb(X,Y)':cr='cr(X,Y)':a='255*mod(floor(X/9)+floor(Y/7),2)*4'", ["prores_ks", "-profile", "5"], PA, OA), |
| 42 | c!("aw_proxy", "96x64", 2, "testsrc2", ["prores_aw", "-profile", "0"], P422, O422), |
| 43 | c!("aw_lt", "96x64", 2, "testsrc2", ["prores_aw", "-profile", "1"], P422, O422), |
| 44 | c!("aw_std", "96x64", 2, "testsrc2", ["prores_aw", "-profile", "2"], P422, O422), |
| 45 | c!("aw_hq", "96x64", 2, "testsrc2", ["prores_aw", "-profile", "3"], P422, O422), |
| 46 | c!("aw_4444", "96x64", 2, "testsrc2", ["prores_aw", "-profile", "4"], P444, O444), |
| 47 | c!("odd_size", "101x67", 2, "testsrc2", ["prores_ks", "-profile", "3"], P422, O422), |
| 48 | c!("odd_size_444", "99x35", 2, "mandelbrot", ["prores_ks", "-profile", "4"], P444, O444), |
| 49 | c!("tiny", "16x16", 2, "testsrc2", ["prores_ks", "-profile", "2"], P422, O422), |
| 50 | c!("one_px_over", "17x17", 2, "testsrc2", ["prores_ks", "-profile", "2"], P422, O422), |
| 51 | c!("noise_hq", "160x96", 3, "testsrc2,noise=alls=80:allf=t", ["prores_ks", "-profile", "3"], P422, O422), |
| 52 | c!("noise_4444", "160x96", 3, "testsrc2,noise=alls=80:allf=t", ["prores_ks", "-profile", "4"], P444, O444), |
| 53 | c!("noise_xq", "160x96", 3, "testsrc2,noise=alls=100:allf=t", ["prores_ks", "-profile", "5"], P444, O444), |
| 54 | c!("saturated", "160x96", 2, "testsrc2,eq=contrast=3:brightness=0.2", ["prores_ks", "-profile", "3"], P422, O422), |
| 55 | c!("flat", "64x48", 2, "color=c=gray", ["prores_ks", "-profile", "3"], P422, O422), |
| 56 | c!("black", "64x48", 2, "color=c=black", ["prores_ks", "-profile", "0"], P422, O422), |
| 57 | c!("white_xq", "64x48", 2, "color=c=white", ["prores_ks", "-profile", "5"], P444, O444), |
| 58 | // Slice sizes. |
| 59 | c!("mbs1", "176x96", 2, "testsrc2", ["prores_ks", "-profile", "3", "-mbs_per_slice", "1"], P422, O422), |
| 60 | c!("mbs2", "176x96", 2, "testsrc2", ["prores_ks", "-profile", "3", "-mbs_per_slice", "2"], P422, O422), |
| 61 | c!("mbs4", "176x96", 2, "testsrc2", ["prores_ks", "-profile", "3", "-mbs_per_slice", "4"], P422, O422), |
| 62 | c!("mbs8", "176x96", 2, "testsrc2", ["prores_ks", "-profile", "3", "-mbs_per_slice", "8"], P422, O422), |
| 63 | c!("mbs1_444", "176x96", 2, "testsrc2", ["prores_ks", "-profile", "4", "-mbs_per_slice", "1"], P444, O444), |
| 64 | c!("mbs4_444", "176x96", 2, "testsrc2", ["prores_ks", "-profile", "4", "-mbs_per_slice", "4"], P444, O444), |
| 65 | c!("mbs3_width", "208x64", 2, "testsrc2", ["prores_ks", "-profile", "3", "-mbs_per_slice", "8"], P422, O422), |
| 66 | // Quantisation: matrices and quantiser index. |
| 67 | c!("qm_proxy", "96x64", 2, "testsrc2", ["prores_ks", "-profile", "2", "-quant_mat", "proxy"], P422, O422), |
| 68 | c!("qm_lt", "96x64", 2, "testsrc2", ["prores_ks", "-profile", "2", "-quant_mat", "lt"], P422, O422), |
| 69 | c!("qm_std", "96x64", 2, "testsrc2", ["prores_ks", "-profile", "3", "-quant_mat", "standard"], P422, O422), |
| 70 | c!("qm_hq", "96x64", 2, "testsrc2", ["prores_ks", "-profile", "0", "-quant_mat", "hq"], P422, O422), |
| 71 | c!("qm_default", "96x64", 2, "testsrc2", ["prores_ks", "-profile", "3", "-quant_mat", "default"], P422, O422), |
| 72 | c!("qm_4444", "96x64", 2, "testsrc2", ["prores_ks", "-profile", "4", "-quant_mat", "proxy"], P444, O444), |
| 73 | c!("q1", "96x64", 2, "testsrc2,noise=alls=40:allf=t", ["prores_ks", "-profile", "3", "-qscale:v", "1"], P422, O422), |
| 74 | c!("q4", "96x64", 2, "testsrc2,noise=alls=40:allf=t", ["prores_ks", "-profile", "3", "-qscale:v", "4"], P422, O422), |
| 75 | c!("q16", "96x64", 2, "testsrc2,noise=alls=40:allf=t", ["prores_ks", "-profile", "3", "-qscale:v", "16"], P422, O422), |
| 76 | c!("q31", "96x64", 2, "testsrc2,noise=alls=40:allf=t", ["prores_ks", "-profile", "3", "-qscale:v", "31"], P422, O422), |
| 77 | c!("q1_444", "96x64", 2, "testsrc2,noise=alls=40:allf=t", ["prores_ks", "-profile", "4", "-qscale:v", "1"], P444, O444), |
| 78 | c!("bpm_small", "96x64", 2, "testsrc2,noise=alls=40:allf=t", ["prores_ks", "-profile", "3", "-bits_per_mb", "300"], P422, O422), |
| 79 | c!("bpm_big", "96x64", 2, "testsrc2,noise=alls=40:allf=t", ["prores_ks", "-profile", "5", "-bits_per_mb", "4000"], P444, O444), |
| 80 | // Interlaced. |
| 81 | c!("ilace_tff", "128x96", 3, "testsrc2", ["prores_ks", "-profile", "3", "-flags", "+ildct", "-top", "1"], P422, O422), |
| 82 | c!("ilace_bff", "128x96", 3, "testsrc2", ["prores_ks", "-profile", "3", "-flags", "+ildct", "-top", "0"], P422, O422), |
| 83 | c!("ilace_444", "128x96", 2, "testsrc2", ["prores_ks", "-profile", "4", "-flags", "+ildct", "-top", "1"], P444, O444), |
| 84 | c!("ilace_odd", "98x70", 2, "testsrc2", ["prores_ks", "-profile", "2", "-flags", "+ildct", "-top", "1"], P422, O422), |
| 85 | c!("ilace_alpha", "96x64", 2, "testsrc2,format=yuva444p10le,geq=lum='lum(X,Y)':cb='cb(X,Y)':cr='cr(X,Y)':a='mod(X*13+Y*7,1024)'", ["prores_ks", "-profile", "4", "-flags", "+ildct", "-top", "0"], PA, OA), |
| 86 | c!("ilace_mbs2", "128x96", 2, "testsrc2", ["prores_ks", "-profile", "1", "-flags", "+ildct", "-mbs_per_slice", "2"], P422, O422), |
| 87 | // Bigger. |
| 88 | c!("sd_pal", "720x576", 2, "testsrc2", ["prores_ks", "-profile", "3", "-flags", "+ildct", "-top", "1"], P422, O422), |
| 89 | c!("hd_1080", "1920x1080", 2, "testsrc2,noise=alls=10:allf=t", ["prores_ks", "-profile", "3"], P422, O422), |
| 90 | c!("hd_1080_4444", "1920x1080", 1, "mandelbrot", ["prores_ks", "-profile", "4"], P444, O444), |
| 91 | ]; |
| 92 | |
| 93 | fn generate(c: &Case) -> std::path::PathBuf { |
| 94 | let dir = scratch(c.tag); |
| 95 | let out = dir.join("in.mov"); |
| 96 | let (base, chain) = match c.src.split_once(',') { |
| 97 | Some((b, rest)) => (b, format!(",{rest}")), |
| 98 | None => (c.src, String::new()), |
| 99 | }; |
| 100 | let lavfi = if base.starts_with("color") { |
| 101 | format!("{}:size={}:rate=25", c.src, c.size) |
| 102 | } else { |
| 103 | format!("{base}=size={}:rate=25{chain}", c.size) |
| 104 | }; |
| 105 | let frames = c.frames.to_string(); |
| 106 | let mut args: Vec<&str> = vec!["-f", "lavfi", "-i", &lavfi, "-frames:v", &frames, "-pix_fmt", c.pix]; |
| 107 | args.extend_from_slice(c.enc); |
| 108 | args.push(out.to_str().unwrap()); |
| 109 | assert!(run(&args), "{}: ffmpeg encode failed", c.tag); |
| 110 | out |
| 111 | } |
| 112 | |
| 113 | #[test] |
| 114 | fn matches_ffmpeg() { |
| 115 | if !ffmpeg_or_skip() { |
| 116 | return; |
| 117 | } |
| 118 | let mut bad = Vec::new(); |
| 119 | for c in CASES { |
| 120 | let mov = generate(c); |
| 121 | let reference = raw_ffmpeg(c.tag, &mov, c.out).unwrap(); |
| 122 | let bytes = std::fs::read(&mov).unwrap(); |
| 123 | for threads in [1usize, 3] { |
| 124 | match decode_ours(&bytes, threads) { |
| 125 | Ok(frames) => { |
| 126 | let ours = raw_ours(&frames); |
| 127 | if let Some(at) = first_diff(&ours, &reference) { |
| 128 | bad.push(format!("{} (threads {threads}): first diff at byte {at} (ours {} ref {})", c.tag, ours.len(), reference.len())); |
| 129 | } |
| 130 | } |
| 131 | Err(e) => bad.push(format!("{} (threads {threads}): {e}", c.tag)), |
| 132 | } |
| 133 | } |
| 134 | let _ = std::fs::remove_dir_all(scratch(c.tag)); |
| 135 | } |
| 136 | assert!(bad.is_empty(), "mismatches:\n{}", bad.join("\n")); |
| 137 | } |