census(av1): per-coded-frame syntax census with order hints, and the ARF-vs-ARF table it prints
Browse files at this commit671d20146f5cc34a9b73082c32241e9f59b3638fParent: afec53f4 changed files +127 -7
- crates/ec-av1/examples/syntax_census.rs +35 -2
- crates/ec-av1/src/census.rs +7 -0
- crates/ec-av1/src/motion.rs +22 -5
- lanes/arfcen.report.md +63 -0
crates/ec-av1/examples/syntax_census.rsmodified+35 -2
| @@ -116,6 +116,9 @@ fn pct(n: u64, total: u64) -> String { | ||
| 116 | 116 | format!("{:.1}%", 100.0 * n as f64 / total.max(1) as f64) |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | +const REFS: [&str; 8] = | |
| 120 | + ["INTRA", "LAST", "LAST2", "LAST3", "GOLDEN", "BWDREF", "ALTREF2", "ALTREF"]; | |
| 121 | + | |
| 119 | 122 | fn report(label: &str, f: &Frame, frames: usize) { |
| 120 | 123 | let area = f.area.iter().sum::<u64>().max(1); |
| 121 | 124 | println!("\n== {label} ({frames} frames, {} bytes) ==", f.bytes); |
| @@ -126,8 +129,6 @@ fn report(label: &str, f: &Frame, frames: usize) { | ||
| 126 | 129 | pct(f.area[2], area), |
| 127 | 130 | pct(f.skip_area, area), |
| 128 | 131 | ); |
| 129 | - const REFS: [&str; 8] = | |
| 130 | - ["INTRA", "LAST", "LAST2", "LAST3", "GOLDEN", "BWDREF", "ALTREF2", "ALTREF"]; | |
| 131 | 132 | println!( |
| 132 | 133 | " refs (area): {}", |
| 133 | 134 | REFS.iter() |
| @@ -252,6 +253,7 @@ fn main() { | ||
| 252 | 253 | // the frames above are coded in (a pyramid codes a hidden ARF ahead of the |
| 253 | 254 | // leaves that predict from it, and re-outputs it later with |
| 254 | 255 | // `show_existing_frame`) -- so it is its own table rather than a column. |
| 256 | + let mut psnrs: Vec<f64> = Vec::new(); | |
| 255 | 257 | if let Some(src) = args.get(1).map(|p| std::fs::read(p).expect("source")) { |
| 256 | 258 | let mut sum = 0.0; |
| 257 | 259 | let mut rows = Vec::new(); |
| @@ -261,6 +263,7 @@ fn main() { | ||
| 261 | 263 | let Some(b) = src.get(i * frame_len..(i + 1) * frame_len) else { break }; |
| 262 | 264 | let want: Vec<u16> = b[..luma].iter().map(|&v| u16::from(v)).collect(); |
| 263 | 265 | let p = psnr(&pic.y, &want); |
| 266 | + psnrs.push(p); | |
| 264 | 267 | sum += p; |
| 265 | 268 | rows.push(format!("{i}:{p:.2}")); |
| 266 | 269 | } |
| @@ -273,6 +276,36 @@ fn main() { | ||
| 273 | 276 | ); |
| 274 | 277 | } |
| 275 | 278 | |
| 279 | + // lane-arfcen: one full report PER CODED FRAME, so an ARF of ours can be | |
| 280 | + // put next to the reference encoder's ARF at the same display position. | |
| 281 | + if std::env::var("EC_CENSUS_PERFRAME").as_deref() == Ok("1") { | |
| 282 | + for f in &frames { | |
| 283 | + let p = psnrs.get(f.order_hint as usize).copied().unwrap_or(f64::NAN); | |
| 284 | + let refs: Vec<String> = (0..7) | |
| 285 | + .filter(|i| f.refs[i + 1] > 0) | |
| 286 | + .map(|i| { | |
| 287 | + format!( | |
| 288 | + "{}@{:+}", | |
| 289 | + REFS[i + 1], | |
| 290 | + i64::from(f.ref_hints[i]) - i64::from(f.order_hint), | |
| 291 | + ) | |
| 292 | + }) | |
| 293 | + .collect(); | |
| 294 | + report( | |
| 295 | + &format!( | |
| 296 | + "frame {} {} hint {} q {} PSNR-Y {p:.2} dB refs [{}]", | |
| 297 | + f.idx, | |
| 298 | + f.kind, | |
| 299 | + f.order_hint, | |
| 300 | + f.qindex, | |
| 301 | + refs.join(" "), | |
| 302 | + ), | |
| 303 | + f, | |
| 304 | + 1, | |
| 305 | + ); | |
| 306 | + } | |
| 307 | + } | |
| 308 | + | |
| 276 | 309 | for kind in ["key", "arf", "leaf"] { |
| 277 | 310 | let set: Vec<&Frame> = frames.iter().filter(|f| f.kind == kind).collect(); |
| 278 | 311 | if !set.is_empty() { |
crates/ec-av1/src/census.rsmodified+7 -0
| @@ -50,6 +50,11 @@ pub struct Frame { | ||
| 50 | 50 | pub kind: &'static str, |
| 51 | 51 | /// `base_q_idx`. |
| 52 | 52 | pub qindex: u8, |
| 53 | + /// `OrderHint` of this frame, i.e. its display position (lane-arfcen). | |
| 54 | + pub order_hint: u32, | |
| 55 | + /// `OrderHints[LAST..=ALTREF]`, so a row can name its references' display | |
| 56 | + /// distance rather than only which slot it read (lane-arfcen). | |
| 57 | + pub ref_hints: [u32; 7], | |
| 53 | 58 | /// Tile payload bytes of this frame. |
| 54 | 59 | pub bytes: usize, |
| 55 | 60 | /// `delta_q_present`. |
| @@ -132,6 +137,8 @@ pub(crate) fn frame_start(header: &ec_av1_syntax::FrameHeader, bytes: usize, idx | ||
| 132 | 137 | idx, |
| 133 | 138 | kind, |
| 134 | 139 | qindex: header.quantization.base_q_idx, |
| 140 | + order_hint: header.order_hint, | |
| 141 | + ref_hints: header.order_hints, | |
| 135 | 142 | bytes, |
| 136 | 143 | delta_q: header.delta.q_present, |
| 137 | 144 | segmentation: header.segmentation.enabled, |
crates/ec-av1/src/motion.rsmodified+22 -5
| @@ -340,17 +340,26 @@ pub(crate) fn search( | ||
| 340 | 340 | fn dist_initial_step(dist: u32) -> i32 { |
| 341 | 341 | let base = if seeded() { seeded_step() } else { SEARCH_INITIAL_STEP_PEL }; |
| 342 | 342 | static K: std::sync::OnceLock<f64> = std::sync::OnceLock::new(); |
| 343 | + // lane-arfcen: readable in ANY build, like the encoder's other search | |
| 344 | + // margins -- the constant below was swept when the deepest reference in | |
| 345 | + // the GOP sat at distance 4, and the arm that re-sweeps it at the | |
| 346 | + // shipped mini-GOP of 8 runs through the release `enc_probe`. | |
| 343 | 347 | let k = *K.get_or_init(|| match std::env::var("EC_AV1_MV_DIST_SCALE").ok() { |
| 344 | - Some(v) if cfg!(test) => v.parse().unwrap_or(MV_DIST_SCALE), | |
| 345 | - _ => MV_DIST_SCALE, | |
| 348 | + Some(v) => v.parse().unwrap_or(MV_DIST_SCALE), | |
| 349 | + None => MV_DIST_SCALE, | |
| 346 | 350 | }); |
| 347 | 351 | if dist <= 1 || k <= 0.0 { |
| 348 | 352 | return base; |
| 349 | 353 | } |
| 350 | 354 | let scaled = f64::from(base) * k.mul_add(f64::from(dist - 1), 1.0); |
| 351 | - // Past 4x the base the log stage spends its rounds on vectors no | |
| 352 | - // reference in this encoder's GOP reaches. | |
| 353 | - scaled.round().clamp(f64::from(base), f64::from(base * 4)) as i32 | |
| 355 | + // The ceiling on that widening. `MV_DIST_CAP` x the base; swept by | |
| 356 | + // `EC_AV1_MV_DIST_CAP`. | |
| 357 | + static CAP: std::sync::OnceLock<i32> = std::sync::OnceLock::new(); | |
| 358 | + let cap = *CAP.get_or_init(|| match std::env::var("EC_AV1_MV_DIST_CAP").ok() { | |
| 359 | + Some(v) => v.parse().unwrap_or(MV_DIST_CAP), | |
| 360 | + None => MV_DIST_CAP, | |
| 361 | + }); | |
| 362 | + scaled.round().clamp(f64::from(base), f64::from(base * cap)) as i32 | |
| 354 | 363 | } |
| 355 | 364 | |
| 356 | 365 | /// The distance scale [`dist_initial_step`] ships with. Swept on the BD gate |
| @@ -362,6 +371,14 @@ fn dist_initial_step(dist: u32) -> i32 { | ||
| 362 | 371 | /// is near-tie churn, not a gradient. |
| 363 | 372 | const MV_DIST_SCALE: f64 = 0.5; |
| 364 | 373 | |
| 374 | +/// How many times the base step [`dist_initial_step`] may widen to. Written | |
| 375 | +/// as 4 when the deepest reference in the GOP sat at distance 4 ("past 4x the | |
| 376 | +/// base the log stage spends its rounds on vectors no reference in this | |
| 377 | +/// encoder's GOP reaches"), which the shipped mini-GOP of 8 falsified: the | |
| 378 | +/// top ARF predicts at distance 8 and its own widened step (36 at | |
| 379 | +/// [`MV_DIST_SCALE`] 0.5) was being clamped back to 32. | |
| 380 | +const MV_DIST_CAP: i32 = 4; | |
| 381 | + | |
| 365 | 382 | /// [`search`], plus the running best cost after every round any of its three |
| 366 | 383 | /// stages ran — what `cost_is_monotone_non_increasing_over_the_search` below |
| 367 | 384 | /// checks. |
lanes/arfcen.report.mdadded+63 -0
| @@ -0,0 +1,63 @@ | ||
| 1 | +# lane-arfcen — why a hidden ARF of ours costs what it costs | |
| 2 | + | |
| 3 | +Instrument: `census::Frame` now carries `order_hint` + `ref_hints`, and | |
| 4 | +`examples/syntax_census` prints ONE FULL REPORT PER CODED FRAME under | |
| 5 | +`EC_CENSUS_PERFRAME=1` (display-order PSNR joined to the coded frame through | |
| 6 | +its own order hint, references named by display DISTANCE). Both streams are | |
| 7 | +read by the same decoder, so every number below is comparable. | |
| 8 | + | |
| 9 | +Arming (film B, 48 frames, the long-GOP gate's own window and seek): | |
| 10 | + | |
| 11 | + EC_ENC_SS=00:40:00 EC_ENC_OUT=<dir> enc_probe <film B> gate 0 48 150 | |
| 12 | + -> 90674 B, byte-exact with bd_rate_film_long_gop's q=150 point | |
| 13 | + ffmpeg ... -g 48 -c:v librav1e -rav1e-params speed=6:quantizer=Q:... | |
| 14 | + EC_AV1_BITCENSUS=1 EC_CENSUS_PERFRAME=1 syntax_census <stream> source.yuv | |
| 15 | + | |
| 16 | +rav1e's PSNR-matched point is **quantizer 110** (45.751 dB / 75452 B), not the | |
| 17 | +125 the charter assumed (44.990 dB / 51680 B): ours reads 45.473 dB / 90674 B. | |
| 18 | +Interpolated to our PSNR rav1e needs ~65.6 kB, i.e. **we are +38% at matched | |
| 19 | +quality**. | |
| 20 | + | |
| 21 | +## 1. Level census, same 48 display pictures | |
| 22 | + | |
| 23 | +| | frames | qindex | bytes | share | B/frame | PSNR-Y | | |
| 24 | +|---|---|---|---|---|---|---| | |
| 25 | +| ours key | 1 | 102 | 19321 | 21.6% | 19321 | 47.67 | | |
| 26 | +| ours top ARF (dist 8) | 6 | 118 | 48126 | 53.8% | 8021 | 46.01 | | |
| 27 | +| ours mid ARF (dist 4) | 6 | 142 | 10331 | 11.5% | 1722 | 45.79 | | |
| 28 | +| ours leaf | 35 | 162 | 11736 | 13.1% | 335 | 45.26 | | |
| 29 | +| rav1e key | 1 | 80 | 20763 | 28.0% | 20763 | 47.95 | | |
| 30 | +| rav1e top ARF (dist 4) | 11 | 104 | 42590 | 57.5% | 3872 | 45.97 | | |
| 31 | +| rav1e 2nd ARF (dist 2) | 12 | 122 | 6446 | 8.7% | 537 | 45.48 | | |
| 32 | +| rav1e leaf | 24 | 139 | 4290 | 5.8% | 179 | 45.69 | | |
| 33 | + | |
| 34 | +## 2. ARF vs ARF at the SAME display position | |
| 35 | + | |
| 36 | +| display | ours B/q/PSNR | intra% | comp% | skip% | NEW% | rav1e B/q/PSNR | intra% | comp% | skip% | | |
| 37 | +|---|---|---|---|---|---|---|---|---|---| | |
| 38 | +| 8 | 8449 / 118 / 46.13 | 10.3 | 4.2 | 50.3 | 35.8 | 5945 / 104 / 45.99 | 1.8 | 0.0 | 73.7 | | |
| 39 | +| 16 | 8889 / 118 / 46.18 | 8.8 | 6.2 | 47.7 | 34.5 | 4869 / 104 / 45.92 | 0.3 | 0.0 | 72.8 | | |
| 40 | +| 24 | 9897 / 118 / 45.61 | 8.3 | 13.4 | 47.7 | 31.2 | 5911 / 104 / 45.28 | 0.4 | 0.0 | 73.8 | | |
| 41 | +| 32 | 4941 / 118 / 46.62 | 1.9 | 6.6 | 51.9 | 41.5 | 2087 / 104 / 46.15 | 0.0 | 0.0 | 90.0 | | |
| 42 | +| 40 | 7023 / 118 / 46.04 | 3.3 | 2.8 | 47.7 | 38.5 | 2854 / 104 / 45.53 | 0.1 | 0.0 | 85.4 | | |
| 43 | +| 47/44 | 8927 / 118 / 45.50 | 4.9 | 6.9 | 48.1 | 39.1 | 3151 / 104 / 45.37 | 0.0 | 0.0 | 86.0 | | |
| 44 | + | |
| 45 | +Reference sets: ours `LAST@-8 GOLDEN@-8` (the first ARF names the SAME picture | |
| 46 | +twice) then `LAST@-8 GOLDEN@-16/-24`; rav1e `LAST@-4 LAST2@-4/-8`. Coded mv | |
| 47 | +magnitude, our top ARF: 47.1% in 2-16 px, 40.0% in 16-64 px, 0.0% over 64 px; | |
| 48 | +rav1e's: 76.5% / 8.3% / 0.0%. | |
| 49 | + | |
| 50 | +## 3. Do our expensive ARFs buy cheaper leaves? No. | |
| 51 | + | |
| 52 | +Per display picture, the six positions our encoder codes as top ARFs against | |
| 53 | +the same six positions in rav1e's stream, and everything else: | |
| 54 | + | |
| 55 | +| | our bytes | rav1e bytes | delta | | |
| 56 | +|---|---|---|---| | |
| 57 | +| key (1 picture) | 19321 | 20763 | -1442 | | |
| 58 | +| our 6 ARF positions | 48126 | 24817 | **+23309** | | |
| 59 | +| the other 41 pictures | 22067 | 28509 | -6442 | | |
| 60 | + | |
| 61 | +So the WHOLE +15.4 kB gap (and 6 kB more) sits in the six pictures we code at | |
| 62 | +prediction distance 8; our non-anchor pictures are already 23% CHEAPER than | |
| 63 | +rav1e's -- though 0.3-0.4 dB worse (leaves 45.26 vs 45.69 dB). |