The frame-tables coefficient price ships as a knob
Browse files at this commit1f18beab9c5717848d2fed3a47b94957c09cb027Parent: 95c183b- feat
- the frame-tables coefficient price ships as a knob
- the compound and 32x32 var-tx trials measured again on top of it
2 changed files +52 -8
- crates/ec-av1/src/encode.rs +22 -7
- crates/ec-av1/src/tile.rs +30 -1
crates/ec-av1/src/encode.rsmodified+22 -7
| @@ -3821,7 +3821,14 @@ fn code_square_inter( | ||
| 3821 | 3821 | // compound block's residual is small enough that the four extra |
| 3822 | 3822 | // per-unit txb_skip/eob symbols cost more ladder bytes than this |
| 3823 | 3823 | // encoder's static-CDF estimate prices them at, so the local RD |
| 3824 | - // win does not convert. The flat trial stays. | |
| 3824 | + // win does not convert. lane-av1txbits REBUILT it on top of a | |
| 3825 | + // search that prices against the tables the frame is really | |
| 3826 | + // written with (`tile::arm_pricing_cdfs`, the estimate error on | |
| 3827 | + // these very blocks measured down from +31% to +3%) and measured | |
| 3828 | + // it neutral-to-worse again: +78.8/+95.0/+54.5 and +36.5/+56.3/ | |
| 3829 | + // +1.4 against the +78.8/+95.1/+54.5 and +36.1/+56.4/+1.4 of the | |
| 3830 | + // same build without it. The static-CDF estimate was NOT what | |
| 3831 | + // held the compound var-tx trial back. The flat trial stays. | |
| 3825 | 3832 | let (levels, tx_depth, dcost) = if info.ref1.is_some() { |
| 3826 | 3833 | luma.commit(x, y, side, &luma_new); |
| 3827 | 3834 | (luma_new.levels.clone(), 0, 0.0) |
| @@ -6233,6 +6240,15 @@ fn search_inter_block( | ||
| 6233 | 6240 | } |
| 6234 | 6241 | |
| 6235 | 6242 | let best = best.expect("the search offers at least the intra modes"); |
| 6243 | + // lane-av1txbits BUILT the transform-depth search this path lacks -- a | |
| 6244 | + // 32x32 single-reference winner offered `commit_inter_luma`'s four 16x16 | |
| 6245 | + // units against its flat trial, exactly as a 16x16 winner is -- and | |
| 6246 | + // MEASURED it flat to slightly worse: +78.7/+95.1/+54.6 vs libaom and | |
| 6247 | + // +36.1/+56.2/+1.8 vs rav1e, against the +78.8/+95.1/+54.5 and | |
| 6248 | + // +36.1/+56.4/+1.4 this code scores, while the split fired on 40.2% of | |
| 6249 | + // the film's inter blocks and 17.6% of the screen's. The same shape as | |
| 6250 | + // the compound trial set below: a local RD win that does not convert into | |
| 6251 | + // ladder bytes. The flat 32x32 transform stays. | |
| 6236 | 6252 | luma.commit(x, y, BLOCK, &best.luma); |
| 6237 | 6253 | chroma[0].commit(x / 2, y / 2, BLOCK / 2, &best.u); |
| 6238 | 6254 | chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v); |
| @@ -9470,13 +9486,12 @@ mod tests { | ||
| 9470 | 9486 | // (compound share 8.8% -> 11.5%): a compound block carries a second |
| 9471 | 9487 | // reference tree, a compound mode symbol and -- for the half-new modes |
| 9472 | 9488 | // -- an MV residual, none of which the coefficient sum counts. |
| 9473 | - // lane-av1txbits: re-measured at +32.6% once the search started | |
| 9474 | - // pricing against the tables the frame's writer really starts from | |
| 9475 | - // (`arm_pricing_cdfs`) instead of the defaults -- the over-price the | |
| 9476 | - // bound above catches collapsed to nothing, so the whole remaining | |
| 9477 | - // gap is the non-coefficient syntax the sum leaves out. | |
| 9489 | + // lane-av1txbits: under `EC_AV1_PRICE_FRAME_CDFS=1` this reads +32.6% | |
| 9490 | + // instead -- pricing against the tables the frame's writer really | |
| 9491 | + // starts from collapses the over-price above to nothing, so the whole | |
| 9492 | + // remaining gap is the syntax the coefficient sum leaves out. | |
| 9478 | 9493 | assert!( |
| 9479 | - worst_under <= 0.40, | |
| 9494 | + worst_under <= 0.32, | |
| 9480 | 9495 | "the writer spent {:.2}% more than the search priced -- more than \ |
| 9481 | 9496 | the mode/mv syntax outside the coefficient sum explains", |
| 9482 | 9497 | worst_under * 100.0 |
crates/ec-av1/src/tile.rsmodified+30 -1
| @@ -10,6 +10,7 @@ | ||
| 10 | 10 | //! off as they arrive. |
| 11 | 11 | |
| 12 | 12 | use std::cell::RefCell; |
| 13 | +#[cfg(test)] | |
| 13 | 14 | use std::collections::BTreeMap; |
| 14 | 15 | use std::sync::LazyLock; |
| 15 | 16 | |
| @@ -3514,7 +3515,9 @@ fn write_block_planes( | ||
| 3514 | 3515 | } else { |
| 3515 | 3516 | usize::from(around[plane].above_coded) + usize::from(around[plane].left_coded) |
| 3516 | 3517 | }; |
| 3518 | + #[cfg(test)] | |
| 3517 | 3519 | let before = enc.tell(); |
| 3520 | + #[cfg(test)] | |
| 3518 | 3521 | let q_ctx = cdfs.q_ctx; |
| 3519 | 3522 | write_coeffs( |
| 3520 | 3523 | enc, |
| @@ -3525,6 +3528,7 @@ fn write_block_planes( | ||
| 3525 | 3528 | dc_sign_ctx(around[plane].dc_vote), |
| 3526 | 3529 | Some(plane), |
| 3527 | 3530 | ); |
| 3531 | + #[cfg(test)] | |
| 3528 | 3532 | census_record(planes[plane], q_ctx, grid, f64::from(enc.tell() - before)); |
| 3529 | 3533 | ec_rng_trace(|| { |
| 3530 | 3534 | format!( |
| @@ -3606,7 +3610,9 @@ fn write_luma_tus( | ||
| 3606 | 3610 | neighbours.luma_skip_ctx(tu_mi, tx / MI) |
| 3607 | 3611 | }; |
| 3608 | 3612 | let around = neighbours.around_mi(tu_mi, tx)[0]; |
| 3613 | + #[cfg(test)] | |
| 3609 | 3614 | let before = enc.tell(); |
| 3615 | + #[cfg(test)] | |
| 3610 | 3616 | let q_ctx = cdfs.q_ctx; |
| 3611 | 3617 | write_coeffs( |
| 3612 | 3618 | enc, |
| @@ -3617,6 +3623,7 @@ fn write_luma_tus( | ||
| 3617 | 3623 | dc_sign_ctx(around.dc_vote), |
| 3618 | 3624 | Some(0), |
| 3619 | 3625 | ); |
| 3626 | + #[cfg(test)] | |
| 3620 | 3627 | census_record(set, q_ctx, &unit, f64::from(enc.tell() - before)); |
| 3621 | 3628 | if n > 1 { |
| 3622 | 3629 | neighbours.record_mi_luma(tu_mi, tx, &unit); |
| @@ -4038,9 +4045,25 @@ thread_local! { | ||
| 4038 | 4045 | /// Arms the tables [`coeff_bits`] prices against on this thread: the state |
| 4039 | 4046 | /// this frame's tile writer starts from. `None` goes back to the per-q-context |
| 4040 | 4047 | /// defaults (a key frame, whose writer really does start there). |
| 4048 | +/// | |
| 4049 | +/// OFF by default (`EC_AV1_PRICE_FRAME_CDFS=1`). It is the more accurate | |
| 4050 | +/// estimate by measurement -- the pricer census | |
| 4051 | +/// (`pricer_error_census_by_block_class`) goes from +7.6% to +4.8% over every | |
| 4052 | +/// transform block, and the one- and two-coefficient inter blocks the search | |
| 4053 | +/// lives on from +31.4%/+22.3% to +3.1%/+3.6% -- but the accuracy does not | |
| 4054 | +/// convert into ladder bytes on every clip: on the 640x384 gate it scores | |
| 4055 | +/// +78.8/+95.1/+54.5 vs libaom and +36.1/+56.4/+1.4 vs rav1e against the | |
| 4056 | +/// +79.9/+94.6/+54.5 and +37.3/+55.7/+1.1 the defaults score (film 1080p | |
| 4057 | +/// down 1.1, film 2160p up 0.5, screen flat), and on the native crops | |
| 4058 | +/// +18.0/+47.3/+60.7 and +0.7/+19.2/-7.3 against +18.9/+48.2/+59.3 and | |
| 4059 | +/// +1.5/+19.8/-10.0 (both film clips down against both references, screen up | |
| 4060 | +/// 1.4/2.7). Two clips better, one worse: it does not meet this lane's keep | |
| 4061 | +/// rule, so it ships as a knob rather than as the default. | |
| 4041 | 4062 | pub(crate) fn arm_pricing_cdfs(base: Option<&Cdfs>) { |
| 4063 | + static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new(); | |
| 4064 | + let on = *ON.get_or_init(|| std::env::var("EC_AV1_PRICE_FRAME_CDFS").as_deref() == Ok("1")); | |
| 4042 | 4065 | PRICING_BASE.with_borrow_mut(|slot| { |
| 4043 | - *slot = base.map(|c| Box::new((c.clone(), c.clone()))); | |
| 4066 | + *slot = base.filter(|_| on).map(|c| Box::new((c.clone(), c.clone()))); | |
| 4044 | 4067 | }); |
| 4045 | 4068 | } |
| 4046 | 4069 | |
| @@ -4055,27 +4078,33 @@ pub(crate) fn arm_pricing_cdfs(base: Option<&Cdfs>) { | ||
| 4055 | 4078 | /// encode running beside it; a multi-tile, multi-threaded encode would |
| 4056 | 4079 | /// therefore only census the tiles this thread wrote, which is every tile of |
| 4057 | 4080 | /// the one-tile-one-thread encodes the gates print it from. |
| 4081 | +#[cfg(test)] | |
| 4058 | 4082 | type CensusRows = BTreeMap<(String, usize), (u64, f64, f64)>; |
| 4083 | +#[cfg(test)] | |
| 4059 | 4084 | thread_local! { |
| 4060 | 4085 | static CENSUS: RefCell<Option<CensusRows>> = const { RefCell::new(None) }; |
| 4061 | 4086 | } |
| 4062 | 4087 | |
| 4063 | 4088 | /// Starts a census on this thread, discarding anything a previous one left. |
| 4089 | +#[cfg(test)] | |
| 4064 | 4090 | pub(crate) fn pricer_census_on() { |
| 4065 | 4091 | CENSUS.with_borrow_mut(|c| *c = Some(BTreeMap::new())); |
| 4066 | 4092 | } |
| 4067 | 4093 | |
| 4068 | 4094 | /// Stops the census and hands back its rows: (set, non-zero bucket) -> |
| 4069 | 4095 | /// (blocks, priced bits, written bits). |
| 4096 | +#[cfg(test)] | |
| 4070 | 4097 | pub(crate) fn take_pricer_census() -> CensusRows { |
| 4071 | 4098 | CENSUS.with_borrow_mut(|c| c.take()).unwrap_or_default() |
| 4072 | 4099 | } |
| 4073 | 4100 | |
| 4074 | 4101 | /// The label of a non-zero bucket, for the tables the gates print. |
| 4102 | +#[cfg(test)] | |
| 4075 | 4103 | pub(crate) fn census_bucket_label(bucket: usize) -> &'static str { |
| 4076 | 4104 | ["0", "1", "2", "3-4", "5-8", "9-16", "17+"][bucket] |
| 4077 | 4105 | } |
| 4078 | 4106 | |
| 4107 | +#[cfg(test)] | |
| 4079 | 4108 | fn census_record(set: TxbSet, q_ctx: usize, grid: &[i32], written: f64) { |
| 4080 | 4109 | if CENSUS.with_borrow(|c| c.is_none()) { |
| 4081 | 4110 | return; |