mbtree_bench/
mbtree_bench.rs1use rusty_h264_common::types::YuvFrame;
14use rusty_h264_encoder::{Encoder, EncoderConfig, Preset};
15
16fn fnv1a(d: &[u8]) -> u64 {
17 let mut h = 0xcbf29ce484222325u64;
18 for &b in d {
19 h ^= b as u64;
20 h = h.wrapping_mul(0x100000001b3);
21 }
22 h
23}
24
25fn read_y4m(path: &str, max: usize) -> (usize, usize, Vec<YuvFrame>) {
26 let raw = std::fs::read(path).expect("read clip");
27 let e = raw.iter().position(|&b| b == b'\n').expect("y4m header");
28 let hdr = std::str::from_utf8(&raw[..e]).expect("utf8 header");
29 let (mut w, mut h) = (0usize, 0usize);
30 for t in hdr.split_whitespace() {
31 match t.as_bytes().first() {
32 Some(b'W') => w = t[1..].parse().expect("W"),
33 Some(b'H') => h = t[1..].parse().expect("H"),
34 _ => {}
35 }
36 }
37 let (ys, cs) = (w * h, (w / 2) * (h / 2));
38 let (mut f, mut p) = (Vec::new(), e + 1);
39 while f.len() < max {
40 let Some(r) = raw[p..].iter().position(|&b| b == b'\n') else { break };
41 p += r + 1;
42 if p + ys + 2 * cs > raw.len() {
43 break;
44 }
45 f.push(YuvFrame {
46 width: w,
47 height: h,
48 y: raw[p..p + ys].to_vec(),
49 u: raw[p + ys..p + ys + cs].to_vec(),
50 v: raw[p + ys + cs..p + ys + 2 * cs].to_vec(),
51 });
52 p += ys + 2 * cs;
53 }
54 (w, h, f)
55}
56
57fn main() {
58 let path = std::env::args().nth(1).expect("usage: mbtree_bench <clip.y4m>");
59 let env = |k: &str, d: usize| -> usize {
60 std::env::var(k).ok().and_then(|v| v.parse().ok()).unwrap_or(d)
61 };
62 let (n, qp, gop, reps) = (env("MB_FRAMES", 48), env("MB_QP", 27), env("MB_GOP", 30), env("MB_REPS", 3));
63 if let Ok(la) = std::env::var("MB_LA") {
64 std::env::set_var("RFF_MBTREE_LA", la);
65 }
66 let (w, h, frames) = read_y4m(&path, n);
67 println!("mbtree_bench {}x{} x{} qp{qp} gop{gop} (best-of-{reps})", w, h, frames.len());
68
69 let mut off_ms = f64::MAX;
70 let mut on_ms = f64::MAX;
71 let (mut off_h, mut on_h, mut off_b, mut on_b) = (0u64, 0u64, 0usize, 0usize);
72 let mut calls = 0u64;
73 let mut ratios: Vec<f64> = Vec::with_capacity(reps);
79 for r in 0..reps {
80 let (mut r_off, mut r_on) = (0.0f64, 0.0f64);
81 let order: [bool; 2] = if r % 2 == 0 { [false, true] } else { [true, false] };
84 for on in order {
85 let mut cfg = EncoderConfig::new(w, h);
86 cfg.qp = qp as u8;
87 cfg.gop_size = gop as u32;
88 cfg.preset = Preset::Quality;
89 cfg.mbtree = on;
90 let enc = Encoder::new(cfg).expect("cfg");
91 rusty_h264_encoder::mbtree_satd_reset();
92 let t = std::time::Instant::now();
93 let out: Vec<u8> = enc.encode_all(&frames).expect("encode").concat();
94 let ms = t.elapsed().as_secs_f64() * 1e3;
95 if on {
96 on_ms = on_ms.min(ms);
97 on_h = fnv1a(&out);
98 on_b = out.len();
99 calls = rusty_h264_encoder::mbtree_satd_calls();
100 r_on = ms;
101 } else {
102 off_ms = off_ms.min(ms);
103 off_h = fnv1a(&out);
104 off_b = out.len();
105 r_off = ms;
106 }
107 }
108 ratios.push(r_on / r_off);
109 }
110 println!(" mbtree OFF: {off_ms:8.1} ms {off_b:>8} bytes hash {off_h:016x}");
111 println!(" mbtree ON : {on_ms:8.1} ms {on_b:>8} bytes hash {on_h:016x}");
112 println!(
113 " lookahead work: {calls} candidate evals ({:.0}/MB/frame, DETERMINISTIC)",
114 calls as f64 / ((w / 16 * (h / 16)) as f64 * frames.len() as f64)
115 );
116 println!(
118 " lookahead overhead (UNPAIRED, min-of-N per arm — noisy, historical): {:+.1}%",
119 100.0 * (on_ms / off_ms - 1.0)
120 );
121 let mut sorted = ratios.clone();
122 sorted.sort_by(|a, b| a.partial_cmp(b).expect("finite"));
123 let med = sorted[sorted.len() / 2];
124 let wins = ratios.iter().filter(|&&r| r < 1.0).count();
125 let n = ratios.len();
126 let z = (wins as f64 - n as f64 / 2.0) / (0.5 * (n as f64).sqrt());
127 print!(" per-round ON/OFF ratios:");
128 for r in &ratios {
129 print!(" {r:.3}");
130 }
131 println!();
132 println!(
133 " lookahead overhead (PAIRED median): {:+.1}% [ON faster in {wins}/{n}, z={z:+.2} {}]",
134 100.0 * (med - 1.0),
135 if z.abs() > 2.0 { "VERDICT" } else { "no directional verdict" }
136 );
137 println!(" size {:+.2}% (deterministic)", 100.0 * (on_b as f64 / off_b as f64 - 1.0));
138}