forge
Explore
Log in

Don't you dare go Hollow

Revert "fix(av1): a key frame's tx_depth symbol is priced at the writer's context row"

Browse files at this commit

c46955edadf77a0d9e50ac6449a9ff9a384f7de1Ahmet Kaan GümüşParent: ae42b5f
REFUTED on the 12-frame native gate: against its own control (this lane's
comp_mode arm) film A goes +21.0/-4.8 -> +21.1/-4.6 and film B
+24.9/-1.9 -> +25.0/-1.9, i.e. every film column moves the wrong way for
no win anywhere (screen and both bars rows byte-for-byte flat).

The keep rule is not met. The likely reason it costs rather than pays:
this was HALF the fix -- an inter frame's intra block still prices its
tx_depth at row 0 (it reads the TXFM_CONTEXT bands, which the encoder
publishes nowhere), so key and inter frames now judge the same
depth-versus-split margin against different rows.

This reverts commit 7499152e.
crates/ec-av1/src/encode.rsmodified+21 -143
@@ -4227,21 +4227,17 @@ fn max_tx_depth(side: usize) -> usize {
4227 }4227 }
4228 }4228 }
4229 4229
4230-/// What the `tx_depth` symbol itself costs, off the size category's own CDF4230+/// What the `tx_depth` symbol itself costs, off the size category's own CDF.
4231-/// at `ctx` -- the row [`crate::tile::tx_size_ctx_of`] picks from the4231+/// Priced at context row 0 -- the writer picks the real row from its
4232-/// transform sides the blocks above and to the left published, which is what4232+/// neighbours' transform sizes, which the search has no view of; the term is
4233-/// the writer's `write_luma_select` codes the symbol against.4233+/// the same for every depth of one block either way, so it only shifts the
4234-///4234+/// depth-0-versus-split margin, never the ordering between two splits.
4235-/// Before lane-ctx2 this was row 0 for every block (class4235+fn tx_depth_bits(side: usize, depth: usize) -> f64 {
4236-/// `pricer-context-zero`): the term shifts the depth-0-versus-split margin,
4237-/// and row 0 is the row where BOTH neighbours are narrower than this block's
4238-/// transform -- the case in which splitting reads cheapest.
4239-fn tx_depth_bits(ctx: usize, side: usize, depth: usize) -> f64 {
4240 match side {4236 match side {
4241- 8 => symbol_bits(&cdf::TX_SIZE_CAT0[ctx], depth),4237+ 8 => symbol_bits(&cdf::TX_SIZE_CAT0[0], depth),
4242- 16 => symbol_bits(&cdf::TX_SIZE_CAT1[ctx], depth),4238+ 16 => symbol_bits(&cdf::TX_SIZE_CAT1[0], depth),
4243- 32 => symbol_bits(&cdf::TX_SIZE_CAT2[ctx], depth),4239+ 32 => symbol_bits(&cdf::TX_SIZE_CAT2[0], depth),
4244- _ => symbol_bits(&cdf::TX_SIZE_CAT3[ctx], depth),4240+ _ => symbol_bits(&cdf::TX_SIZE_CAT3[0], depth),
4245 }4241 }
4246 }4242 }
4247 4243
@@ -5226,12 +5222,7 @@ fn code_square(
5226 tx_select: bool,5222 tx_select: bool,
5227 // This tile's intra block-copy state ([`Ibc`]), `None` on a frame whose5223 // This tile's intra block-copy state ([`Ibc`]), `None` on a frame whose
5228 // header did not set `allow_intrabc`.5224 // header did not set `allow_intrabc`.
5229- ibc: Option<&mut Ibc>,5225+ ibc: Option<&mut Ibc>, fctx: &crate::decode::FrameCtx,
5230- // The CDF row this block's `tx_depth` symbol is priced against
5231- // ([`tx_depth_bits`]), read by the caller off its own published transform
5232- // bands -- `0` where the caller keeps none.
5233- tx_ctx: usize,
5234- fctx: &crate::decode::FrameCtx,
5235 ) -> (BlockCoeffs, f64) {5226 ) -> (BlockCoeffs, f64) {
5236 let (luma_set, chroma_set) = if side == SUPERBLOCK {5227 let (luma_set, chroma_set) = if side == SUPERBLOCK {
5237 // A whole superblock coded as ONE intra block (lane-i64): one5228 // A whole superblock coded as ONE intra block (lane-i64): one
@@ -5275,11 +5266,11 @@ fn code_square(
5275 // `tx_depth_bits(32, 0)` -- otherwise the 64-versus-four-32 comparison5266 // `tx_depth_bits(32, 0)` -- otherwise the 64-versus-four-32 comparison
5276 // below hands the root a symbol for free.5267 // below hands the root a symbol for free.
5277 if tx_select && side == SUPERBLOCK {5268 if tx_select && side == SUPERBLOCK {
5278- cost += search.lambda * tx_depth_bits(tx_ctx, side, 0);5269+ cost += search.lambda * tx_depth_bits(side, 0);
5279 }5270 }
5280 if tx_select && max_tx_depth(side) > 0 {5271 if tx_select && max_tx_depth(side) > 0 {
5281 let mut best = (5272 let mut best = (
5282- cost + search.lambda * tx_depth_bits(tx_ctx, side, 0),5273+ cost + search.lambda * tx_depth_bits(side, 0),
5283 0usize,5274 0usize,
5284 luma.snapshot(x, y, side),5275 luma.snapshot(x, y, side),
5285 luma_coeffs,5276 luma_coeffs,
@@ -5290,7 +5281,7 @@ fn code_square(
5290 luma.code_tx_depth(at, mode, angle_delta, depth, None, search, fctx);5281 luma.code_tx_depth(at, mode, angle_delta, depth, None, search, fctx);
5291 let cost_d = sse5282 let cost_d = sse
5292 + search.lambda5283 + search.lambda
5293- * (bits + mode_bits[usize::from(mode)] + tx_depth_bits(tx_ctx, side, depth));5284+ * (bits + mode_bits[usize::from(mode)] + tx_depth_bits(side, depth));
5294 if cost_d < best.0 {5285 if cost_d < best.0 {
5295 best = (cost_d, depth, luma.snapshot(x, y, side), coeffs(&levels, side));5286 best = (cost_d, depth, luma.snapshot(x, y, side), coeffs(&levels, side));
5296 best_types = types;5287 best_types = types;
@@ -5338,7 +5329,7 @@ fn code_square(
5338 * (trial.bits5329 * (trial.bits
5339 + mode_bits[DC_PRED as usize]5330 + mode_bits[DC_PRED as usize]
5340 + crate::tile::palette_bits(&pal, side)5331 + crate::tile::palette_bits(&pal, side)
5341- + if tx_select { tx_depth_bits(tx_ctx, side, 0) } else { 0.0 });5332+ + if tx_select { tx_depth_bits(side, 0) } else { 0.0 });
5342 if cost_p < cost {5333 if cost_p < cost {
5343 cost = cost_p;5334 cost = cost_p;
5344 mode = DC_PRED as u8;5335 mode = DC_PRED as u8;
@@ -5404,7 +5395,7 @@ fn code_square(
5404 + mode_bits[DC_PRED as usize]5395 + mode_bits[DC_PRED as usize]
5405 + one_bits5396 + one_bits
5406 + symbol_bits(&cdf::FILTER_INTRA_MODE, usize::from(fi))5397 + symbol_bits(&cdf::FILTER_INTRA_MODE, usize::from(fi))
5407- + if tx_select { tx_depth_bits(tx_ctx, side, depth) } else { 0.0 });5398+ + if tx_select { tx_depth_bits(side, depth) } else { 0.0 });
5408 if cost_f < cost && best.as_ref().is_none_or(|b| cost_f < b.0) {5399 if cost_f < cost && best.as_ref().is_none_or(|b| cost_f < b.0) {
5409 best = Some((5400 best = Some((
5410 cost_f,5401 cost_f,
@@ -6126,11 +6117,6 @@ fn code_square_inter(
6126 mode_bits,6117 mode_bits,
6127 tx_select() && tx_select_inter(),6118 tx_select() && tx_select_inter(),
6128 None,6119 None,
6129- // deferred: an INTER frame's intra block takes its `tx_depth` row
6130- // from the `TXFM_CONTEXT` bands instead (`tx_size_ctx_txfm`, with
6131- // an inter neighbour voting its own BLOCK size), which this path
6132- // publishes nowhere -- priced at row 0 as before.
6133- 0,
6134 fctx,6120 fctx,
6135 );6121 );
6136 let luma_set = if side == 8 {6122 let luma_set = if side == 8 {
@@ -7686,38 +7672,6 @@ pub(crate) fn encode_key_frame_inner(
7686 clip_planes_to_tile(&mut luma, &mut chroma, rect);7672 clip_planes_to_tile(&mut luma, &mut chroma, rect);
7687 let mut above_mode = vec![DC_PRED; cols * 2];7673 let mut above_mode = vec![DC_PRED; cols * 2];
7688 let mut left_mode = vec![DC_PRED; rows * 2];7674 let mut left_mode = vec![DC_PRED; rows * 2];
7689- // lane-ctx2: the two bands `Neighbours::tx_size_ctx` reads -- every
7690- // coded block's resolved luma transform side in pixels, over each 4x4
7691- // unit it covers, the way the writer's `record_tx` publishes it. The
7692- // indices are frame-absolute, so a new superblock row needs no reset
7693- // (the writer's `start_row` clears a band indexed within the tile),
7694- // and a block trial that loses is overwritten by the winner's own
7695- // publish, exactly as `above_mode`/`left_mode` are.
7696- let mut above_tx = vec![0u8; cols * 8];
7697- let mut left_tx = vec![0u8; rows * 8];
7698- let tx_ctx = |above_tx: &[u8], left_tx: &[u8], (x, y): (usize, usize), side: usize| {
7699- let (mi_r, mi_c) = (y / 4, x / 4);
7700- crate::tile::tx_size_ctx_of(
7701- (mi_r > rect.mi_row0 as usize, mi_c > rect.mi_col0 as usize),
7702- (above_tx[mi_c], left_tx[mi_r]),
7703- side.min(64),
7704- )
7705- };
7706- let publish_tx = |above_tx: &mut Vec<u8>,
7707- left_tx: &mut Vec<u8>,
7708- (x, y): (usize, usize),
7709- side: usize,
7710- depth: u8| {
7711- let tx = (side.min(64) >> depth) as u8;
7712- for cell in 0..side / 4 {
7713- if let Some(c) = above_tx.get_mut(x / 4 + cell) {
7714- *c = tx;
7715- }
7716- if let Some(c) = left_tx.get_mut(y / 4 + cell) {
7717- *c = tx;
7718- }
7719- }
7720- };
7721 let mut ibc = allow_intrabc.then(|| {7675 let mut ibc = allow_intrabc.then(|| {
7722 Ibc::new(7676 Ibc::new(
7723 (7677 (
@@ -7797,7 +7751,6 @@ pub(crate) fn encode_key_frame_inner(
7797 &mode_bits(above_mode[c64], left_mode[r64]),7751 &mode_bits(above_mode[c64], left_mode[r64]),
7798 tx_select,7752 tx_select,
7799 None,7753 None,
7800- tx_ctx(&above_tx, &left_tx, (x64, y64), SUPERBLOCK),
7801 fctx,7754 fctx,
7802 );7755 );
7803 let cost = cost + search.lambda * crate::tile::partition_bits(SUPERBLOCK, false);7756 let cost = cost + search.lambda * crate::tile::partition_bits(SUPERBLOCK, false);
@@ -7881,9 +7834,7 @@ pub(crate) fn encode_key_frame_inner(
7881 &search,7834 &search,
7882 &mode_bits(above_mode[c0], left_mode[r0]),7835 &mode_bits(above_mode[c0], left_mode[r0]),
7883 tx_select,7836 tx_select,
7884- ibc.as_mut(),7837+ ibc.as_mut(), fctx,
7885- tx_ctx(&above_tx, &left_tx, (x, y), BLOCK),
7886- fctx,
7887 );7838 );
7888 cost_whole += search.lambda * partition_bits(BLOCK, false);7839 cost_whole += search.lambda * partition_bits(BLOCK, false);
7889 let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK);7840 let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK);
@@ -7918,26 +7869,12 @@ pub(crate) fn encode_key_frame_inner(
7918 &search,7869 &search,
7919 &mode_bits(above_mode[sc], left_mode[sr]),7870 &mode_bits(above_mode[sc], left_mode[sr]),
7920 tx_select,7871 tx_select,
7921- ibc.as_mut(),7872+ ibc.as_mut(), fctx,
7922- tx_ctx(
7923- &above_tx,
7924- &left_tx,
7925- (x + (sub % 2) * SUB, y + (sub / 2) * SUB),
7926- SUB,
7927- ),
7928- fctx,
7929 );7873 );
7930 cost_split += cost;7874 cost_split += cost;
7931 split_modes.push(block.mode);7875 split_modes.push(block.mode);
7932 above_mode[sc] = block.mode;7876 above_mode[sc] = block.mode;
7933 left_mode[sr] = block.mode;7877 left_mode[sr] = block.mode;
7934- publish_tx(
7935- &mut above_tx,
7936- &mut left_tx,
7937- (x + (sub % 2) * SUB, y + (sub / 2) * SUB),
7938- SUB,
7939- block.tx_depth,
7940- );
7941 split.push(block);7878 split.push(block);
7942 } else {7879 } else {
7943 // A straddling 16x16: two (or, at a true corner, one)7880 // A straddling 16x16: two (or, at a true corner, one)
@@ -7965,13 +7902,10 @@ pub(crate) fn encode_key_frame_inner(
7965 &search,7902 &search,
7966 &leaf_mode_bits,7903 &leaf_mode_bits,
7967 tx_select,7904 tx_select,
7968- None,7905+ None, fctx,
7969- tx_ctx(&above_tx, &left_tx, (leaf_x, leaf_y), 8),
7970- fctx,
7971 );7906 );
7972 cost_split += cost;7907 cost_split += cost;
7973 split_modes.push(leaf.mode);7908 split_modes.push(leaf.mode);
7974- publish_tx(&mut above_tx, &mut left_tx, (leaf_x, leaf_y), 8, leaf.tx_depth);
7975 leaves.push(leaf);7909 leaves.push(leaf);
7976 }7910 }
7977 split.push(BlockCoeffs {7911 split.push(BlockCoeffs {
@@ -8000,7 +7934,6 @@ pub(crate) fn encode_key_frame_inner(
8000 above_mode[c0 + cell] = whole.mode;7934 above_mode[c0 + cell] = whole.mode;
8001 left_mode[r0 + cell] = whole.mode;7935 left_mode[r0 + cell] = whole.mode;
8002 }7936 }
8003- publish_tx(&mut above_tx, &mut left_tx, (x, y), BLOCK, whole.tx_depth);
8004 modes.push(whole.mode);7937 modes.push(whole.mode);
8005 blocks.push(Quadrant::Whole(whole));7938 blocks.push(Quadrant::Whole(whole));
8006 }7939 }
@@ -8013,7 +7946,6 @@ pub(crate) fn encode_key_frame_inner(
8013 above_mode[sb_col * 4 + cell] = block.mode;7946 above_mode[sb_col * 4 + cell] = block.mode;
8014 left_mode[sb_row * 4 + cell] = block.mode;7947 left_mode[sb_row * 4 + cell] = block.mode;
8015 }7948 }
8016- publish_tx(&mut above_tx, &mut left_tx, (x64, y64), SUPERBLOCK, block.tx_depth);
8017 I64_HITS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);7949 I64_HITS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
8018 if !whole_inside {7950 if !whole_inside {
8019 I64_HITS[2].fetch_add(1, std::sync::atomic::Ordering::Relaxed);7951 I64_HITS[2].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
@@ -12502,55 +12434,6 @@ mod tests {
12502 }12434 }
12503 }12435 }
12504 12436
12505- /// The `tx_depth` symbol is priced off the row the writer really codes it
12506- /// against -- `Neighbours::tx_size_ctx`, shared with the pricer as
12507- /// [`crate::tile::tx_size_ctx_of`], off the transform sides the blocks
12508- /// above and to the left published -- instead of row 0 for every block
12509- /// (class `pricer-context-zero`). The second half is what was red before
12510- /// lane-ctx2: at a non-zero row the writer's price is a different number.
12511- #[test]
12512- fn the_tx_depth_pricer_uses_the_writers_own_context_row() {
12513- use crate::cdf::{TX_SIZE_CAT1, TX_SIZE_CAT2};
12514- use crate::encode::symbol_bits as b;
12515- use crate::tile::tx_size_ctx_of;
12516- // (has_above, has_left), the two band cells, this block's own largest
12517- // transform -> the row the writer picks. A neighbour outside the tile
12518- // votes nothing however wide its band cell reads.
12519- for (has, bands, max_tx, want) in [
12520- ((false, false), (64u8, 64u8), 32usize, 0usize),
12521- ((true, true), (16, 16), 32, 0),
12522- ((true, true), (32, 16), 32, 1),
12523- ((true, false), (32, 64), 32, 1),
12524- ((false, true), (64, 32), 32, 1),
12525- ((true, true), (32, 64), 32, 2),
12526- ((true, true), (8, 8), 8, 2),
12527- ] {
12528- assert_eq!(
12529- tx_size_ctx_of(has, bands, max_tx),
12530- want,
12531- "tile {has:?} bands {bands:?} max_tx {max_tx}"
12532- );
12533- }
12534- // libaom's default tables give rows 0 and 1 the same probabilities in
12535- // every category, so row 2 -- both neighbours at least as wide as this
12536- // block's transform -- is the row that moves a price.
12537- for (ctx, depth) in [(2usize, 0usize), (2, 1), (2, 2)] {
12538- for (side, table) in [(16usize, &TX_SIZE_CAT1), (32, &TX_SIZE_CAT2)] {
12539- let got = super::tx_depth_bits(ctx, side, depth);
12540- assert!(
12541- (got - b(&table[ctx], depth)).abs() < 1e-9,
12542- "side {side} depth {depth} at ctx {ctx}: priced {got}, \
12543- writer codes {}",
12544- b(&table[ctx], depth)
12545- );
12546- assert!(
12547- (got - b(&table[0], depth)).abs() > 1e-9,
12548- "side {side} depth {depth}: row {ctx} prices the same as row 0"
12549- );
12550- }
12551- }
12552- }
12553-
12554 /// lane-av1tpl2's propagated map, on the two properties every decision12437 /// lane-av1tpl2's propagated map, on the two properties every decision
12555 /// under it rests on: a frame the whole lookahead window repeats exactly12438 /// under it rests on: a frame the whole lookahead window repeats exactly
12556 /// (uniform texture) leans on every cell alike, so every factor is 1 and12439 /// (uniform texture) leans on every cell alike, so every factor is 1 and
@@ -16236,14 +16119,9 @@ mod tests {
16236 // at q=60. Re-taken on lane-ctx2: a single-reference candidate now16119 // at q=60. Re-taken on lane-ctx2: a single-reference candidate now
16237 // pays the `comp_mode = 0` symbol the writer codes ahead of it, so16120 // pays the `comp_mode = 0` symbol the writer codes ahead of it, so
16238 // single and compound are priced against the same syntax -- 8311 ->16121 // single and compound are priced against the same syntax -- 8311 ->
16239- // 8325 bytes at q=150 and 33087 -> 33014 at q=60. Re-taken again in16122+ // 8325 bytes at q=150 and 33087 -> 33014 at q=60.
16240- // the same lane: a key frame's `tx_depth` symbol is priced off the
16241- // row the writer codes it against (the transform sides its above/left
16242- // neighbours published) instead of row 0, so every intra block's
16243- // depth-versus-split margin moved -- 8325 bytes at q=150 (the same
16244- // size, different depths) and 33014 at q=60, both hashes new.
16245 let pins: [(u8, usize, u64); 2] =16123 let pins: [(u8, usize, u64); 2] =
16246- [(150, 8325, 0x774a_4716_1cc1_b0ed), (60, 33014, 0xa19b_4fa6_cde1_1180)];16124+ [(150, 8325, 0xf004_8258_bc60_ff05), (60, 33014, 0x5bc2_5dfd_bd13_18de)];
16247 let coded: Vec<(u8, usize, u64)> = pins16125 let coded: Vec<(u8, usize, u64)> = pins
16248 .iter()16126 .iter()
16249 .map(|&(q, _, _)| {16127 .map(|&(q, _, _)| {
crates/ec-av1/src/tile.rsmodified+2 -22
@@ -2427,11 +2427,8 @@ impl Neighbours {
2427 /// largest transform, plus whether the one to the left is at least as2427 /// largest transform, plus whether the one to the left is at least as
2428 /// tall. A neighbour outside the tile contributes nothing.2428 /// tall. A neighbour outside the tile contributes nothing.
2429 fn tx_size_ctx(&self, (mi_r, mi_c): (usize, usize), max_tx: usize) -> usize {2429 fn tx_size_ctx(&self, (mi_r, mi_c): (usize, usize), max_tx: usize) -> usize {
2430- tx_size_ctx_of(2430+ usize::from(self.has_above(mi_r) && usize::from(self.above_tx[mi_c]) >= max_tx)
2431- (self.has_above(mi_r), self.has_left(mi_c)),2431+ + usize::from(self.has_left(mi_c) && usize::from(self.left_tx[mi_r]) >= max_tx)
2432- (self.above_tx[mi_c], self.left_tx[mi_r]),
2433- max_tx,
2434- )
2435 }2432 }
2436 2433
2437 /// Publishes one block's resolved transform side over every 4x4 unit it2434 /// Publishes one block's resolved transform side over every 4x4 unit it
@@ -4312,23 +4309,6 @@ fn write_luma_tus(
4312 Ok(())4309 Ok(())
4313 }4310 }
4314 4311
4315-/// `get_tx_size_context` (decode.rs [`crate::decode::tx_size_context`]) over
4316-/// the two band cells themselves: whether the transform above is at least as
4317-/// wide as this block's own largest transform, plus whether the one to the
4318-/// left is at least as tall; a neighbour outside the tile contributes nothing.
4319-///
4320-/// Free-standing so the RD pricer can read the row the writer really codes a
4321-/// `tx_depth` symbol against off its own published band, without two
4322-/// transcriptions of the same rule (lane-ctx2).
4323-pub(crate) fn tx_size_ctx_of(
4324- (has_above, has_left): (bool, bool),
4325- (above_tx, left_tx): (u8, u8),
4326- max_tx: usize,
4327-) -> usize {
4328- usize::from(has_above && usize::from(above_tx) >= max_tx)
4329- + usize::from(has_left && usize::from(left_tx) >= max_tx)
4330-}
4331-
4332 /// Writes one intra block's luma residual under `TxMode::Select`: the4312 /// Writes one intra block's luma residual under `TxMode::Select`: the
4333 /// `tx_depth` symbol (spec 5.11.16 `read_selected_tx_size`, off the size4313 /// `tx_depth` symbol (spec 5.11.16 `read_selected_tx_size`, off the size
4334 /// category's own CDF at [`Neighbours::tx_size_ctx`]'s row), then the4314 /// category's own CDF at [`Neighbours::tx_size_ctx`]'s row), then the