forge
Explore
Log in

Don't you dare go Hollow

The frame-tables coefficient price ships as a knob

Browse files at this commit

1f18beab9c5717848d2fed3a47b94957c09cb027Ahmet Kaan GümüşParent: 95c183b
  • feat
    • the frame-tables coefficient price ships as a knob
    • the compound and 32x32 var-tx trials measured again on top of it
Show full message
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.rsmodified+22 -7
@@ -3821,7 +3821,14 @@ fn code_square_inter(
3821 // compound block's residual is small enough that the four extra3821 // compound block's residual is small enough that the four extra
3822 // per-unit txb_skip/eob symbols cost more ladder bytes than this3822 // per-unit txb_skip/eob symbols cost more ladder bytes than this
3823 // encoder's static-CDF estimate prices them at, so the local RD3823 // 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 let (levels, tx_depth, dcost) = if info.ref1.is_some() {3832 let (levels, tx_depth, dcost) = if info.ref1.is_some() {
3826 luma.commit(x, y, side, &luma_new);3833 luma.commit(x, y, side, &luma_new);
3827 (luma_new.levels.clone(), 0, 0.0)3834 (luma_new.levels.clone(), 0, 0.0)
@@ -6233,6 +6240,15 @@ fn search_inter_block(
6233 }6240 }
6234 6241
6235 let best = best.expect("the search offers at least the intra modes");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 luma.commit(x, y, BLOCK, &best.luma);6252 luma.commit(x, y, BLOCK, &best.luma);
6237 chroma[0].commit(x / 2, y / 2, BLOCK / 2, &best.u);6253 chroma[0].commit(x / 2, y / 2, BLOCK / 2, &best.u);
6238 chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v);6254 chroma[1].commit(x / 2, y / 2, BLOCK / 2, &best.v);
@@ -9470,13 +9486,12 @@ mod tests {
9470 // (compound share 8.8% -> 11.5%): a compound block carries a second9486 // (compound share 8.8% -> 11.5%): a compound block carries a second
9471 // reference tree, a compound mode symbol and -- for the half-new modes9487 // reference tree, a compound mode symbol and -- for the half-new modes
9472 // -- an MV residual, none of which the coefficient sum counts.9488 // -- an MV residual, none of which the coefficient sum counts.
9473- // lane-av1txbits: re-measured at +32.6% once the search started9489+ // lane-av1txbits: under `EC_AV1_PRICE_FRAME_CDFS=1` this reads +32.6%
9474- // pricing against the tables the frame's writer really starts from9490+ // instead -- pricing against the tables the frame's writer really
9475- // (`arm_pricing_cdfs`) instead of the defaults -- the over-price the9491+ // starts from collapses the over-price above to nothing, so the whole
9476- // bound above catches collapsed to nothing, so the whole remaining9492+ // remaining gap is the syntax the coefficient sum leaves out.
9477- // gap is the non-coefficient syntax the sum leaves out.
9478 assert!(9493 assert!(
9479- worst_under <= 0.40,9494+ worst_under <= 0.32,
9480 "the writer spent {:.2}% more than the search priced -- more than \9495 "the writer spent {:.2}% more than the search priced -- more than \
9481 the mode/mv syntax outside the coefficient sum explains",9496 the mode/mv syntax outside the coefficient sum explains",
9482 worst_under * 100.09497 worst_under * 100.0
crates/ec-av1/src/tile.rsmodified+30 -1
@@ -10,6 +10,7 @@
10 //! off as they arrive.10 //! off as they arrive.
11 11
12 use std::cell::RefCell;12 use std::cell::RefCell;
13+#[cfg(test)]
13 use std::collections::BTreeMap;14 use std::collections::BTreeMap;
14 use std::sync::LazyLock;15 use std::sync::LazyLock;
15 16
@@ -3514,7 +3515,9 @@ fn write_block_planes(
3514 } else {3515 } else {
3515 usize::from(around[plane].above_coded) + usize::from(around[plane].left_coded)3516 usize::from(around[plane].above_coded) + usize::from(around[plane].left_coded)
3516 };3517 };
3518+ #[cfg(test)]
3517 let before = enc.tell();3519 let before = enc.tell();
3520+ #[cfg(test)]
3518 let q_ctx = cdfs.q_ctx;3521 let q_ctx = cdfs.q_ctx;
3519 write_coeffs(3522 write_coeffs(
3520 enc,3523 enc,
@@ -3525,6 +3528,7 @@ fn write_block_planes(
3525 dc_sign_ctx(around[plane].dc_vote),3528 dc_sign_ctx(around[plane].dc_vote),
3526 Some(plane),3529 Some(plane),
3527 );3530 );
3531+ #[cfg(test)]
3528 census_record(planes[plane], q_ctx, grid, f64::from(enc.tell() - before));3532 census_record(planes[plane], q_ctx, grid, f64::from(enc.tell() - before));
3529 ec_rng_trace(|| {3533 ec_rng_trace(|| {
3530 format!(3534 format!(
@@ -3606,7 +3610,9 @@ fn write_luma_tus(
3606 neighbours.luma_skip_ctx(tu_mi, tx / MI)3610 neighbours.luma_skip_ctx(tu_mi, tx / MI)
3607 };3611 };
3608 let around = neighbours.around_mi(tu_mi, tx)[0];3612 let around = neighbours.around_mi(tu_mi, tx)[0];
3613+ #[cfg(test)]
3609 let before = enc.tell();3614 let before = enc.tell();
3615+ #[cfg(test)]
3610 let q_ctx = cdfs.q_ctx;3616 let q_ctx = cdfs.q_ctx;
3611 write_coeffs(3617 write_coeffs(
3612 enc,3618 enc,
@@ -3617,6 +3623,7 @@ fn write_luma_tus(
3617 dc_sign_ctx(around.dc_vote),3623 dc_sign_ctx(around.dc_vote),
3618 Some(0),3624 Some(0),
3619 );3625 );
3626+ #[cfg(test)]
3620 census_record(set, q_ctx, &unit, f64::from(enc.tell() - before));3627 census_record(set, q_ctx, &unit, f64::from(enc.tell() - before));
3621 if n > 1 {3628 if n > 1 {
3622 neighbours.record_mi_luma(tu_mi, tx, &unit);3629 neighbours.record_mi_luma(tu_mi, tx, &unit);
@@ -4038,9 +4045,25 @@ thread_local! {
4038 /// Arms the tables [`coeff_bits`] prices against on this thread: the state4045 /// Arms the tables [`coeff_bits`] prices against on this thread: the state
4039 /// this frame's tile writer starts from. `None` goes back to the per-q-context4046 /// this frame's tile writer starts from. `None` goes back to the per-q-context
4040 /// defaults (a key frame, whose writer really does start there).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 pub(crate) fn arm_pricing_cdfs(base: Option<&Cdfs>) {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 PRICING_BASE.with_borrow_mut(|slot| {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 /// encode running beside it; a multi-tile, multi-threaded encode would4078 /// encode running beside it; a multi-tile, multi-threaded encode would
4056 /// therefore only census the tiles this thread wrote, which is every tile of4079 /// therefore only census the tiles this thread wrote, which is every tile of
4057 /// the one-tile-one-thread encodes the gates print it from.4080 /// the one-tile-one-thread encodes the gates print it from.
4081+#[cfg(test)]
4058 type CensusRows = BTreeMap<(String, usize), (u64, f64, f64)>;4082 type CensusRows = BTreeMap<(String, usize), (u64, f64, f64)>;
4083+#[cfg(test)]
4059 thread_local! {4084 thread_local! {
4060 static CENSUS: RefCell<Option<CensusRows>> = const { RefCell::new(None) };4085 static CENSUS: RefCell<Option<CensusRows>> = const { RefCell::new(None) };
4061 }4086 }
4062 4087
4063 /// Starts a census on this thread, discarding anything a previous one left.4088 /// Starts a census on this thread, discarding anything a previous one left.
4089+#[cfg(test)]
4064 pub(crate) fn pricer_census_on() {4090 pub(crate) fn pricer_census_on() {
4065 CENSUS.with_borrow_mut(|c| *c = Some(BTreeMap::new()));4091 CENSUS.with_borrow_mut(|c| *c = Some(BTreeMap::new()));
4066 }4092 }
4067 4093
4068 /// Stops the census and hands back its rows: (set, non-zero bucket) ->4094 /// Stops the census and hands back its rows: (set, non-zero bucket) ->
4069 /// (blocks, priced bits, written bits).4095 /// (blocks, priced bits, written bits).
4096+#[cfg(test)]
4070 pub(crate) fn take_pricer_census() -> CensusRows {4097 pub(crate) fn take_pricer_census() -> CensusRows {
4071 CENSUS.with_borrow_mut(|c| c.take()).unwrap_or_default()4098 CENSUS.with_borrow_mut(|c| c.take()).unwrap_or_default()
4072 }4099 }
4073 4100
4074 /// The label of a non-zero bucket, for the tables the gates print.4101 /// The label of a non-zero bucket, for the tables the gates print.
4102+#[cfg(test)]
4075 pub(crate) fn census_bucket_label(bucket: usize) -> &'static str {4103 pub(crate) fn census_bucket_label(bucket: usize) -> &'static str {
4076 ["0", "1", "2", "3-4", "5-8", "9-16", "17+"][bucket]4104 ["0", "1", "2", "3-4", "5-8", "9-16", "17+"][bucket]
4077 }4105 }
4078 4106
4107+#[cfg(test)]
4079 fn census_record(set: TxbSet, q_ctx: usize, grid: &[i32], written: f64) {4108 fn census_record(set: TxbSet, q_ctx: usize, grid: &[i32], written: f64) {
4080 if CENSUS.with_borrow(|c| c.is_none()) {4109 if CENSUS.with_borrow(|c| c.is_none()) {
4081 return;4110 return;