diff --git a/crates/ec-av1/examples/syntax_census.rs b/crates/ec-av1/examples/syntax_census.rs index e578c73a..5b6c2bd7 100644 --- a/crates/ec-av1/examples/syntax_census.rs +++ b/crates/ec-av1/examples/syntax_census.rs @@ -116,6 +116,9 @@ fn pct(n: u64, total: u64) -> String { format!("{:.1}%", 100.0 * n as f64 / total.max(1) as f64) } +const REFS: [&str; 8] = + ["INTRA", "LAST", "LAST2", "LAST3", "GOLDEN", "BWDREF", "ALTREF2", "ALTREF"]; + fn report(label: &str, f: &Frame, frames: usize) { let area = f.area.iter().sum::().max(1); println!("\n== {label} ({frames} frames, {} bytes) ==", f.bytes); @@ -126,8 +129,6 @@ fn report(label: &str, f: &Frame, frames: usize) { pct(f.area[2], area), pct(f.skip_area, area), ); - const REFS: [&str; 8] = - ["INTRA", "LAST", "LAST2", "LAST3", "GOLDEN", "BWDREF", "ALTREF2", "ALTREF"]; println!( " refs (area): {}", REFS.iter() @@ -252,6 +253,7 @@ fn main() { // the frames above are coded in (a pyramid codes a hidden ARF ahead of the // leaves that predict from it, and re-outputs it later with // `show_existing_frame`) -- so it is its own table rather than a column. + let mut psnrs: Vec = Vec::new(); if let Some(src) = args.get(1).map(|p| std::fs::read(p).expect("source")) { let mut sum = 0.0; let mut rows = Vec::new(); @@ -261,6 +263,7 @@ fn main() { let Some(b) = src.get(i * frame_len..(i + 1) * frame_len) else { break }; let want: Vec = b[..luma].iter().map(|&v| u16::from(v)).collect(); let p = psnr(&pic.y, &want); + psnrs.push(p); sum += p; rows.push(format!("{i}:{p:.2}")); } @@ -273,6 +276,36 @@ fn main() { ); } + // lane-arfcen: one full report PER CODED FRAME, so an ARF of ours can be + // put next to the reference encoder's ARF at the same display position. + if std::env::var("EC_CENSUS_PERFRAME").as_deref() == Ok("1") { + for f in &frames { + let p = psnrs.get(f.order_hint as usize).copied().unwrap_or(f64::NAN); + let refs: Vec = (0..7) + .filter(|i| f.refs[i + 1] > 0) + .map(|i| { + format!( + "{}@{:+}", + REFS[i + 1], + i64::from(f.ref_hints[i]) - i64::from(f.order_hint), + ) + }) + .collect(); + report( + &format!( + "frame {} {} hint {} q {} PSNR-Y {p:.2} dB refs [{}]", + f.idx, + f.kind, + f.order_hint, + f.qindex, + refs.join(" "), + ), + f, + 1, + ); + } + } + for kind in ["key", "arf", "leaf"] { let set: Vec<&Frame> = frames.iter().filter(|f| f.kind == kind).collect(); if !set.is_empty() { diff --git a/crates/ec-av1/src/census.rs b/crates/ec-av1/src/census.rs index d5afe15f..e771d7b4 100644 --- a/crates/ec-av1/src/census.rs +++ b/crates/ec-av1/src/census.rs @@ -50,6 +50,11 @@ pub struct Frame { pub kind: &'static str, /// `base_q_idx`. pub qindex: u8, + /// `OrderHint` of this frame, i.e. its display position (lane-arfcen). + pub order_hint: u32, + /// `OrderHints[LAST..=ALTREF]`, so a row can name its references' display + /// distance rather than only which slot it read (lane-arfcen). + pub ref_hints: [u32; 7], /// Tile payload bytes of this frame. pub bytes: usize, /// `delta_q_present`. @@ -132,6 +137,8 @@ pub(crate) fn frame_start(header: &ec_av1_syntax::FrameHeader, bytes: usize, idx idx, kind, qindex: header.quantization.base_q_idx, + order_hint: header.order_hint, + ref_hints: header.order_hints, bytes, delta_q: header.delta.q_present, segmentation: header.segmentation.enabled, diff --git a/crates/ec-av1/src/motion.rs b/crates/ec-av1/src/motion.rs index d352856a..c55c994c 100644 --- a/crates/ec-av1/src/motion.rs +++ b/crates/ec-av1/src/motion.rs @@ -340,17 +340,26 @@ pub(crate) fn search( fn dist_initial_step(dist: u32) -> i32 { let base = if seeded() { seeded_step() } else { SEARCH_INITIAL_STEP_PEL }; static K: std::sync::OnceLock = std::sync::OnceLock::new(); + // lane-arfcen: readable in ANY build, like the encoder's other search + // margins -- the constant below was swept when the deepest reference in + // the GOP sat at distance 4, and the arm that re-sweeps it at the + // shipped mini-GOP of 8 runs through the release `enc_probe`. let k = *K.get_or_init(|| match std::env::var("EC_AV1_MV_DIST_SCALE").ok() { - Some(v) if cfg!(test) => v.parse().unwrap_or(MV_DIST_SCALE), - _ => MV_DIST_SCALE, + Some(v) => v.parse().unwrap_or(MV_DIST_SCALE), + None => MV_DIST_SCALE, }); if dist <= 1 || k <= 0.0 { return base; } let scaled = f64::from(base) * k.mul_add(f64::from(dist - 1), 1.0); - // Past 4x the base the log stage spends its rounds on vectors no - // reference in this encoder's GOP reaches. - scaled.round().clamp(f64::from(base), f64::from(base * 4)) as i32 + // The ceiling on that widening. `MV_DIST_CAP` x the base; swept by + // `EC_AV1_MV_DIST_CAP`. + static CAP: std::sync::OnceLock = std::sync::OnceLock::new(); + let cap = *CAP.get_or_init(|| match std::env::var("EC_AV1_MV_DIST_CAP").ok() { + Some(v) => v.parse().unwrap_or(MV_DIST_CAP), + None => MV_DIST_CAP, + }); + scaled.round().clamp(f64::from(base), f64::from(base * cap)) as i32 } /// The distance scale [`dist_initial_step`] ships with. Swept on the BD gate @@ -362,6 +371,14 @@ fn dist_initial_step(dist: u32) -> i32 { /// is near-tie churn, not a gradient. const MV_DIST_SCALE: f64 = 0.5; +/// How many times the base step [`dist_initial_step`] may widen to. Written +/// as 4 when the deepest reference in the GOP sat at distance 4 ("past 4x the +/// base the log stage spends its rounds on vectors no reference in this +/// encoder's GOP reaches"), which the shipped mini-GOP of 8 falsified: the +/// top ARF predicts at distance 8 and its own widened step (36 at +/// [`MV_DIST_SCALE`] 0.5) was being clamped back to 32. +const MV_DIST_CAP: i32 = 4; + /// [`search`], plus the running best cost after every round any of its three /// stages ran — what `cost_is_monotone_non_increasing_over_the_search` below /// checks. diff --git a/lanes/arfcen.report.md b/lanes/arfcen.report.md new file mode 100644 index 00000000..5222bf40 --- /dev/null +++ b/lanes/arfcen.report.md @@ -0,0 +1,63 @@ +# lane-arfcen — why a hidden ARF of ours costs what it costs + +Instrument: `census::Frame` now carries `order_hint` + `ref_hints`, and +`examples/syntax_census` prints ONE FULL REPORT PER CODED FRAME under +`EC_CENSUS_PERFRAME=1` (display-order PSNR joined to the coded frame through +its own order hint, references named by display DISTANCE). Both streams are +read by the same decoder, so every number below is comparable. + +Arming (film B, 48 frames, the long-GOP gate's own window and seek): + + EC_ENC_SS=00:40:00 EC_ENC_OUT= enc_probe gate 0 48 150 + -> 90674 B, byte-exact with bd_rate_film_long_gop's q=150 point + ffmpeg ... -g 48 -c:v librav1e -rav1e-params speed=6:quantizer=Q:... + EC_AV1_BITCENSUS=1 EC_CENSUS_PERFRAME=1 syntax_census source.yuv + +rav1e's PSNR-matched point is **quantizer 110** (45.751 dB / 75452 B), not the +125 the charter assumed (44.990 dB / 51680 B): ours reads 45.473 dB / 90674 B. +Interpolated to our PSNR rav1e needs ~65.6 kB, i.e. **we are +38% at matched +quality**. + +## 1. Level census, same 48 display pictures + +| | frames | qindex | bytes | share | B/frame | PSNR-Y | +|---|---|---|---|---|---|---| +| ours key | 1 | 102 | 19321 | 21.6% | 19321 | 47.67 | +| ours top ARF (dist 8) | 6 | 118 | 48126 | 53.8% | 8021 | 46.01 | +| ours mid ARF (dist 4) | 6 | 142 | 10331 | 11.5% | 1722 | 45.79 | +| ours leaf | 35 | 162 | 11736 | 13.1% | 335 | 45.26 | +| rav1e key | 1 | 80 | 20763 | 28.0% | 20763 | 47.95 | +| rav1e top ARF (dist 4) | 11 | 104 | 42590 | 57.5% | 3872 | 45.97 | +| rav1e 2nd ARF (dist 2) | 12 | 122 | 6446 | 8.7% | 537 | 45.48 | +| rav1e leaf | 24 | 139 | 4290 | 5.8% | 179 | 45.69 | + +## 2. ARF vs ARF at the SAME display position + +| display | ours B/q/PSNR | intra% | comp% | skip% | NEW% | rav1e B/q/PSNR | intra% | comp% | skip% | +|---|---|---|---|---|---|---|---|---|---| +| 8 | 8449 / 118 / 46.13 | 10.3 | 4.2 | 50.3 | 35.8 | 5945 / 104 / 45.99 | 1.8 | 0.0 | 73.7 | +| 16 | 8889 / 118 / 46.18 | 8.8 | 6.2 | 47.7 | 34.5 | 4869 / 104 / 45.92 | 0.3 | 0.0 | 72.8 | +| 24 | 9897 / 118 / 45.61 | 8.3 | 13.4 | 47.7 | 31.2 | 5911 / 104 / 45.28 | 0.4 | 0.0 | 73.8 | +| 32 | 4941 / 118 / 46.62 | 1.9 | 6.6 | 51.9 | 41.5 | 2087 / 104 / 46.15 | 0.0 | 0.0 | 90.0 | +| 40 | 7023 / 118 / 46.04 | 3.3 | 2.8 | 47.7 | 38.5 | 2854 / 104 / 45.53 | 0.1 | 0.0 | 85.4 | +| 47/44 | 8927 / 118 / 45.50 | 4.9 | 6.9 | 48.1 | 39.1 | 3151 / 104 / 45.37 | 0.0 | 0.0 | 86.0 | + +Reference sets: ours `LAST@-8 GOLDEN@-8` (the first ARF names the SAME picture +twice) then `LAST@-8 GOLDEN@-16/-24`; rav1e `LAST@-4 LAST2@-4/-8`. Coded mv +magnitude, our top ARF: 47.1% in 2-16 px, 40.0% in 16-64 px, 0.0% over 64 px; +rav1e's: 76.5% / 8.3% / 0.0%. + +## 3. Do our expensive ARFs buy cheaper leaves? No. + +Per display picture, the six positions our encoder codes as top ARFs against +the same six positions in rav1e's stream, and everything else: + +| | our bytes | rav1e bytes | delta | +|---|---|---|---| +| key (1 picture) | 19321 | 20763 | -1442 | +| our 6 ARF positions | 48126 | 24817 | **+23309** | +| the other 41 pictures | 22067 | 28509 | -6442 | + +So the WHOLE +15.4 kB gap (and 6 kB more) sits in the six pictures we code at +prediction distance 8; our non-anchor pictures are already 23% CHEAPER than +rav1e's -- though 0.3-0.4 dB worse (leaves 45.26 vs 45.69 dB).