From 1f18beab9c5717848d2fed3a47b94957c09cb027 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 6 Sep 2026 14:53:46 +0300 Subject: [PATCH] feat(av1): the frame-tables coefficient price ships as a knob; the compound and 32x32 var-tx trials measured again on top of it Pricing against the tables the frame is really written with is the more accurate estimate -- the census goes +7.6% -> +4.8% over every transform block and +31.4%/+22.3% -> +3.1%/+3.6% on the one- and two-coefficient inter blocks -- but it does not convert on every clip (film down, screen up), so it sits behind EC_AV1_PRICE_FRAME_CDFS with both BD tables in the doc comment and the default bytes unchanged. With it on, both trials the accuracy was supposed to unlock were rebuilt and measured: the compound var-tx trial set and a transform-depth search for the 32x32 inter path each came out neutral to slightly worse, so the static-CDF estimate was not what held them back. Both measurements are recorded at their decision sites; neither ships. --- crates/ec-av1/src/encode.rs | 29 ++++++++++++++++++++++------- crates/ec-av1/src/tile.rs | 31 ++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 0ebacfa4..58997555 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -3821,7 +3821,14 @@ fn code_square_inter( // compound block's residual is small enough that the four extra // per-unit txb_skip/eob symbols cost more ladder bytes than this // encoder's static-CDF estimate prices them at, so the local RD - // win does not convert. The flat trial stays. + // win does not convert. lane-av1txbits REBUILT it on top of a + // search that prices against the tables the frame is really + // written with (`tile::arm_pricing_cdfs`, the estimate error on + // these very blocks measured down from +31% to +3%) and measured + // it neutral-to-worse again: +78.8/+95.0/+54.5 and +36.5/+56.3/ + // +1.4 against the +78.8/+95.1/+54.5 and +36.1/+56.4/+1.4 of the + // same build without it. The static-CDF estimate was NOT what + // held the compound var-tx trial back. The flat trial stays. let (levels, tx_depth, dcost) = if info.ref1.is_some() { luma.commit(x, y, side, &luma_new); (luma_new.levels.clone(), 0, 0.0) @@ -6233,6 +6240,15 @@ fn search_inter_block( } let best = best.expect("the search offers at least the intra modes"); + // lane-av1txbits BUILT the transform-depth search this path lacks -- a + // 32x32 single-reference winner offered `commit_inter_luma`'s four 16x16 + // units against its flat trial, exactly as a 16x16 winner is -- and + // MEASURED it flat to slightly worse: +78.7/+95.1/+54.6 vs libaom and + // +36.1/+56.2/+1.8 vs rav1e, against the +78.8/+95.1/+54.5 and + // +36.1/+56.4/+1.4 this code scores, while the split fired on 40.2% of + // the film's inter blocks and 17.6% of the screen's. The same shape as + // the compound trial set below: a local RD win that does not convert into + // ladder bytes. The flat 32x32 transform stays. luma.commit(x, y, BLOCK, &best.luma); chroma[0].commit(x / 2, y / 2, BLOCK / 2, &best.u); chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v); @@ -9470,13 +9486,12 @@ mod tests { // (compound share 8.8% -> 11.5%): a compound block carries a second // reference tree, a compound mode symbol and -- for the half-new modes // -- an MV residual, none of which the coefficient sum counts. - // lane-av1txbits: re-measured at +32.6% once the search started - // pricing against the tables the frame's writer really starts from - // (`arm_pricing_cdfs`) instead of the defaults -- the over-price the - // bound above catches collapsed to nothing, so the whole remaining - // gap is the non-coefficient syntax the sum leaves out. + // lane-av1txbits: under `EC_AV1_PRICE_FRAME_CDFS=1` this reads +32.6% + // instead -- pricing against the tables the frame's writer really + // starts from collapses the over-price above to nothing, so the whole + // remaining gap is the syntax the coefficient sum leaves out. assert!( - worst_under <= 0.40, + worst_under <= 0.32, "the writer spent {:.2}% more than the search priced -- more than \ the mode/mv syntax outside the coefficient sum explains", worst_under * 100.0 diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index f976e891..14cd3f78 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -10,6 +10,7 @@ //! off as they arrive. use std::cell::RefCell; +#[cfg(test)] use std::collections::BTreeMap; use std::sync::LazyLock; @@ -3514,7 +3515,9 @@ fn write_block_planes( } else { usize::from(around[plane].above_coded) + usize::from(around[plane].left_coded) }; + #[cfg(test)] let before = enc.tell(); + #[cfg(test)] let q_ctx = cdfs.q_ctx; write_coeffs( enc, @@ -3525,6 +3528,7 @@ fn write_block_planes( dc_sign_ctx(around[plane].dc_vote), Some(plane), ); + #[cfg(test)] census_record(planes[plane], q_ctx, grid, f64::from(enc.tell() - before)); ec_rng_trace(|| { format!( @@ -3606,7 +3610,9 @@ fn write_luma_tus( neighbours.luma_skip_ctx(tu_mi, tx / MI) }; let around = neighbours.around_mi(tu_mi, tx)[0]; + #[cfg(test)] let before = enc.tell(); + #[cfg(test)] let q_ctx = cdfs.q_ctx; write_coeffs( enc, @@ -3617,6 +3623,7 @@ fn write_luma_tus( dc_sign_ctx(around.dc_vote), Some(0), ); + #[cfg(test)] census_record(set, q_ctx, &unit, f64::from(enc.tell() - before)); if n > 1 { neighbours.record_mi_luma(tu_mi, tx, &unit); @@ -4038,9 +4045,25 @@ thread_local! { /// Arms the tables [`coeff_bits`] prices against on this thread: the state /// this frame's tile writer starts from. `None` goes back to the per-q-context /// defaults (a key frame, whose writer really does start there). +/// +/// OFF by default (`EC_AV1_PRICE_FRAME_CDFS=1`). It is the more accurate +/// estimate by measurement -- the pricer census +/// (`pricer_error_census_by_block_class`) goes from +7.6% to +4.8% over every +/// transform block, and the one- and two-coefficient inter blocks the search +/// lives on from +31.4%/+22.3% to +3.1%/+3.6% -- but the accuracy does not +/// convert into ladder bytes on every clip: on the 640x384 gate it scores +/// +78.8/+95.1/+54.5 vs libaom and +36.1/+56.4/+1.4 vs rav1e against the +/// +79.9/+94.6/+54.5 and +37.3/+55.7/+1.1 the defaults score (film 1080p +/// down 1.1, film 2160p up 0.5, screen flat), and on the native crops +/// +18.0/+47.3/+60.7 and +0.7/+19.2/-7.3 against +18.9/+48.2/+59.3 and +/// +1.5/+19.8/-10.0 (both film clips down against both references, screen up +/// 1.4/2.7). Two clips better, one worse: it does not meet this lane's keep +/// rule, so it ships as a knob rather than as the default. pub(crate) fn arm_pricing_cdfs(base: Option<&Cdfs>) { + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + let on = *ON.get_or_init(|| std::env::var("EC_AV1_PRICE_FRAME_CDFS").as_deref() == Ok("1")); PRICING_BASE.with_borrow_mut(|slot| { - *slot = base.map(|c| Box::new((c.clone(), c.clone()))); + *slot = base.filter(|_| on).map(|c| Box::new((c.clone(), c.clone()))); }); } @@ -4055,27 +4078,33 @@ pub(crate) fn arm_pricing_cdfs(base: Option<&Cdfs>) { /// encode running beside it; a multi-tile, multi-threaded encode would /// therefore only census the tiles this thread wrote, which is every tile of /// the one-tile-one-thread encodes the gates print it from. +#[cfg(test)] type CensusRows = BTreeMap<(String, usize), (u64, f64, f64)>; +#[cfg(test)] thread_local! { static CENSUS: RefCell> = const { RefCell::new(None) }; } /// Starts a census on this thread, discarding anything a previous one left. +#[cfg(test)] pub(crate) fn pricer_census_on() { CENSUS.with_borrow_mut(|c| *c = Some(BTreeMap::new())); } /// Stops the census and hands back its rows: (set, non-zero bucket) -> /// (blocks, priced bits, written bits). +#[cfg(test)] pub(crate) fn take_pricer_census() -> CensusRows { CENSUS.with_borrow_mut(|c| c.take()).unwrap_or_default() } /// The label of a non-zero bucket, for the tables the gates print. +#[cfg(test)] pub(crate) fn census_bucket_label(bucket: usize) -> &'static str { ["0", "1", "2", "3-4", "5-8", "9-16", "17+"][bucket] } +#[cfg(test)] fn census_record(set: TxbSet, q_ctx: usize, grid: &[i32], written: f64) { if CENSUS.with_borrow(|c| c.is_none()) { return;