forge
Explore
Log in

Don't you dare go Hollow

The tx_depth symbol is priced at the writer's context on both frame types

Browse files at this commit

b6c03310a75bd242ed8a914aef01c6d961b8d6fbAhmet Kaan GümüşParent: b3d0d7c
  • fix
    • the tx_depth symbol is priced at the writer's context on both frame types
Show full message
fix(av1): the tx_depth symbol is priced at the writer's context on both frame types

Class pricer-context-zero, the last site. A key frame reads the deblock
grid (tile::tx_size_ctx_of over Neighbours::record_tx's bands, lane-ctx2's
half, restored here); an INTER frame's intra block reads the TXFM_CONTEXT
bands instead (tx_size_ctx_txfm, where an inter neighbour votes its own
BLOCK size and a skipped inter block publishes its block size), which the
encoder published nowhere -- so lane-ctx2 shipped half a fix and the two
frame types judged the same depth-versus-split margin against different
rows.

The bands ride beside MiGrid::cells (one u8 per mi cell, like skips: the
cell must stay 16 bytes), filled by record_mi at every publication point
-- every coded block, skipped inter blocks and var-tx roots included --
and the rule itself is one function, tile::tx_size_ctx_txfm_of, that the
writer's Neighbours::tx_size_ctx_txfm now calls too.

encode::tests::the_tx_depth_pricer_uses_the_writers_own_context_row is
red before: the grid's row is 0 where the writer codes row 2, and at row
2 a 16x16 depth-0 symbol prices 1.4169 bits against the writer's 0.8110.
crates/ec-av1/src/encode.rsmodified+211 -27
@@ -4227,17 +4227,21 @@ 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 CDF.4230+/// What the `tx_depth` symbol itself costs, off the size category's own CDF
4231-/// Priced at context row 0 -- the writer picks the real row from its4231+/// at `ctx` -- the row [`crate::tile::tx_size_ctx_of`] picks from the
4232-/// neighbours' transform sizes, which the search has no view of; the term is4232+/// transform sides the blocks above and to the left published, which is what
4233-/// the same for every depth of one block either way, so it only shifts the4233+/// the writer's `write_luma_select` codes the symbol against.
4234-/// depth-0-versus-split margin, never the ordering between two splits.4234+///
4235-fn tx_depth_bits(side: usize, depth: usize) -> f64 {4235+/// Before lane-ctx2 this was row 0 for every block (class
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 {
4236 match side {4240 match side {
4237- 8 => symbol_bits(&cdf::TX_SIZE_CAT0[0], depth),4241+ 8 => symbol_bits(&cdf::TX_SIZE_CAT0[ctx], depth),
4238- 16 => symbol_bits(&cdf::TX_SIZE_CAT1[0], depth),4242+ 16 => symbol_bits(&cdf::TX_SIZE_CAT1[ctx], depth),
4239- 32 => symbol_bits(&cdf::TX_SIZE_CAT2[0], depth),4243+ 32 => symbol_bits(&cdf::TX_SIZE_CAT2[ctx], depth),
4240- _ => symbol_bits(&cdf::TX_SIZE_CAT3[0], depth),4244+ _ => symbol_bits(&cdf::TX_SIZE_CAT3[ctx], depth),
4241 }4245 }
4242 }4246 }
4243 4247
@@ -5222,7 +5226,12 @@ fn code_square(
5222 tx_select: bool,5226 tx_select: bool,
5223 // This tile's intra block-copy state ([`Ibc`]), `None` on a frame whose5227 // This tile's intra block-copy state ([`Ibc`]), `None` on a frame whose
5224 // header did not set `allow_intrabc`.5228 // header did not set `allow_intrabc`.
5225- ibc: Option<&mut Ibc>, fctx: &crate::decode::FrameCtx,5229+ ibc: Option<&mut Ibc>,
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,
5226 ) -> (BlockCoeffs, f64) {5235 ) -> (BlockCoeffs, f64) {
5227 let (luma_set, chroma_set) = if side == SUPERBLOCK {5236 let (luma_set, chroma_set) = if side == SUPERBLOCK {
5228 // A whole superblock coded as ONE intra block (lane-i64): one5237 // A whole superblock coded as ONE intra block (lane-i64): one
@@ -5266,11 +5275,11 @@ fn code_square(
5266 // `tx_depth_bits(32, 0)` -- otherwise the 64-versus-four-32 comparison5275 // `tx_depth_bits(32, 0)` -- otherwise the 64-versus-four-32 comparison
5267 // below hands the root a symbol for free.5276 // below hands the root a symbol for free.
5268 if tx_select && side == SUPERBLOCK {5277 if tx_select && side == SUPERBLOCK {
5269- cost += search.lambda * tx_depth_bits(side, 0);5278+ cost += search.lambda * tx_depth_bits(tx_ctx, side, 0);
5270 }5279 }
5271 if tx_select && max_tx_depth(side) > 0 {5280 if tx_select && max_tx_depth(side) > 0 {
5272 let mut best = (5281 let mut best = (
5273- cost + search.lambda * tx_depth_bits(side, 0),5282+ cost + search.lambda * tx_depth_bits(tx_ctx, side, 0),
5274 0usize,5283 0usize,
5275 luma.snapshot(x, y, side),5284 luma.snapshot(x, y, side),
5276 luma_coeffs,5285 luma_coeffs,
@@ -5281,7 +5290,7 @@ fn code_square(
5281 luma.code_tx_depth(at, mode, angle_delta, depth, None, search, fctx);5290 luma.code_tx_depth(at, mode, angle_delta, depth, None, search, fctx);
5282 let cost_d = sse5291 let cost_d = sse
5283 + search.lambda5292 + search.lambda
5284- * (bits + mode_bits[usize::from(mode)] + tx_depth_bits(side, depth));5293+ * (bits + mode_bits[usize::from(mode)] + tx_depth_bits(tx_ctx, side, depth));
5285 if cost_d < best.0 {5294 if cost_d < best.0 {
5286 best = (cost_d, depth, luma.snapshot(x, y, side), coeffs(&levels, side));5295 best = (cost_d, depth, luma.snapshot(x, y, side), coeffs(&levels, side));
5287 best_types = types;5296 best_types = types;
@@ -5329,7 +5338,7 @@ fn code_square(
5329 * (trial.bits5338 * (trial.bits
5330 + mode_bits[DC_PRED as usize]5339 + mode_bits[DC_PRED as usize]
5331 + crate::tile::palette_bits(&pal, side)5340 + crate::tile::palette_bits(&pal, side)
5332- + if tx_select { tx_depth_bits(side, 0) } else { 0.0 });5341+ + if tx_select { tx_depth_bits(tx_ctx, side, 0) } else { 0.0 });
5333 if cost_p < cost {5342 if cost_p < cost {
5334 cost = cost_p;5343 cost = cost_p;
5335 mode = DC_PRED as u8;5344 mode = DC_PRED as u8;
@@ -5395,7 +5404,7 @@ fn code_square(
5395 + mode_bits[DC_PRED as usize]5404 + mode_bits[DC_PRED as usize]
5396 + one_bits5405 + one_bits
5397 + symbol_bits(&cdf::FILTER_INTRA_MODE, usize::from(fi))5406 + symbol_bits(&cdf::FILTER_INTRA_MODE, usize::from(fi))
5398- + if tx_select { tx_depth_bits(side, depth) } else { 0.0 });5407+ + if tx_select { tx_depth_bits(tx_ctx, side, depth) } else { 0.0 });
5399 if cost_f < cost && best.as_ref().is_none_or(|b| cost_f < b.0) {5408 if cost_f < cost && best.as_ref().is_none_or(|b| cost_f < b.0) {
5400 best = Some((5409 best = Some((
5401 cost_f,5410 cost_f,
@@ -6117,6 +6126,11 @@ fn code_square_inter(
6117 mode_bits,6126 mode_bits,
6118 tx_select() && tx_select_inter(),6127 tx_select() && tx_select_inter(),
6119 None,6128 None,
6129+ // lane-txd: an INTER frame's intra block takes its `tx_depth` row
6130+ // from the `TXFM_CONTEXT` bands (`tx_size_ctx_txfm`, where an
6131+ // inter neighbour votes its own BLOCK size), which `record_mi`
6132+ // now publishes per cell.
6133+ tx_ctx_txfm(grid, (mi_row, mi_col), side.min(64)),
6120 fctx,6134 fctx,
6121 );6135 );
6122 let luma_set = if side == 8 {6136 let luma_set = if side == 8 {
@@ -6820,6 +6834,31 @@ fn code_square_inter(
6820 /// MV stack reads, over the `size` x `size` 4x4 units it covers. An intra6834 /// MV stack reads, over the `size` x `size` 4x4 units it covers. An intra
6821 /// block casts no vote but is still a coded cell (see `code_square_inter`'s6835 /// block casts no vote but is still a coded cell (see `code_square_inter`'s
6822 /// note on `processed_rows`/`processed_cols`).6836 /// note on `processed_rows`/`processed_cols`).
6837+/// What one coded block publishes into the `TXFM_CONTEXT` bands, in pixels:
6838+/// a SKIPPED inter block records its own BLOCK size (`set_txfm_ctxs`'s
6839+/// `skip_inter` term), every other block its resolved transform
6840+/// (`tile::write_tx_syntax_inter`'s `record_txfm` calls, all of which cover
6841+/// the block's whole span with one value).
6842+fn txfm_px(side: usize, inter: bool, skip: bool, tx_depth: u8) -> u8 {
6843+ if inter && skip { side as u8 } else { (side.min(64) >> tx_depth) as u8 }
6844+}
6845+
6846+/// The CDF row an INTER frame's intra block has its `tx_depth` symbol coded
6847+/// against ([`crate::tile::Neighbours::tx_size_ctx_txfm`]), read off the grid
6848+/// the encoder publishes its own blocks into -- the above/left 4x4 cell, each
6849+/// contributing nothing when it falls outside this tile (`MiGrid::get`'s own
6850+/// window, which is the writer's `has_above`/`has_left`).
6851+fn tx_ctx_txfm(grid: &MiGrid, (mi_row, mi_col): (usize, usize), own_side: usize) -> usize {
6852+ let cell = |r: usize, c: usize| {
6853+ grid.get(r, c).map(|i| (grid.txfm_at(r, c), i.is_inter, usize::from(i.size) * 4))
6854+ };
6855+ crate::tile::tx_size_ctx_txfm_of(
6856+ mi_row.checked_sub(1).and_then(|r| cell(r, mi_col)),
6857+ mi_col.checked_sub(1).and_then(|c| cell(mi_row, c)),
6858+ own_side,
6859+ )
6860+}
6861+
6823 fn record_mi(6862 fn record_mi(
6824 grid: &mut MiGrid,6863 grid: &mut MiGrid,
6825 mi_row: usize,6864 mi_row: usize,
@@ -6829,6 +6868,10 @@ fn record_mi(
6829 // lane-ctx: the block's own `skip`, so the next block's MV stack can hand6868 // lane-ctx: the block's own `skip`, so the next block's MV stack can hand
6830 // the RD pricer the two cells the writer's `skip_ctx` sums.6869 // the RD pricer the two cells the writer's `skip_ctx` sums.
6831 skip: bool,6870 skip: bool,
6871+ // lane-txd: the transform side, in pixels, this block leaves in the
6872+ // `TXFM_CONTEXT` bands ([`txfm_px`]), which is the state the next intra
6873+ // block's `tx_depth` row is read from.
6874+ txfm: u8,
6832 ) {6875 ) {
6833 let info = match inter {6876 let info = match inter {
6834 Some(info) => MiInfo {6877 Some(info) => MiInfo {
@@ -6874,6 +6917,7 @@ fn record_mi(
6874 for dc in 0..usize::from(size) {6917 for dc in 0..usize::from(size) {
6875 grid.set(mi_row + dr, mi_col + dc, info);6918 grid.set(mi_row + dr, mi_col + dc, info);
6876 grid.set_skip(mi_row + dr, mi_col + dc, skip);6919 grid.set_skip(mi_row + dr, mi_col + dc, skip);
6920+ grid.set_txfm(mi_row + dr, mi_col + dc, txfm);
6877 }6921 }
6878 }6922 }
6879 }6923 }
@@ -7672,6 +7716,38 @@ pub(crate) fn encode_key_frame_inner(
7672 clip_planes_to_tile(&mut luma, &mut chroma, rect);7716 clip_planes_to_tile(&mut luma, &mut chroma, rect);
7673 let mut above_mode = vec![DC_PRED; cols * 2];7717 let mut above_mode = vec![DC_PRED; cols * 2];
7674 let mut left_mode = vec![DC_PRED; rows * 2];7718 let mut left_mode = vec![DC_PRED; rows * 2];
7719+ // lane-ctx2: the two bands `Neighbours::tx_size_ctx` reads -- every
7720+ // coded block's resolved luma transform side in pixels, over each 4x4
7721+ // unit it covers, the way the writer's `record_tx` publishes it. The
7722+ // indices are frame-absolute, so a new superblock row needs no reset
7723+ // (the writer's `start_row` clears a band indexed within the tile),
7724+ // and a block trial that loses is overwritten by the winner's own
7725+ // publish, exactly as `above_mode`/`left_mode` are.
7726+ let mut above_tx = vec![0u8; cols * 8];
7727+ let mut left_tx = vec![0u8; rows * 8];
7728+ let tx_ctx = |above_tx: &[u8], left_tx: &[u8], (x, y): (usize, usize), side: usize| {
7729+ let (mi_r, mi_c) = (y / 4, x / 4);
7730+ crate::tile::tx_size_ctx_of(
7731+ (mi_r > rect.mi_row0 as usize, mi_c > rect.mi_col0 as usize),
7732+ (above_tx[mi_c], left_tx[mi_r]),
7733+ side.min(64),
7734+ )
7735+ };
7736+ let publish_tx = |above_tx: &mut Vec<u8>,
7737+ left_tx: &mut Vec<u8>,
7738+ (x, y): (usize, usize),
7739+ side: usize,
7740+ depth: u8| {
7741+ let tx = (side.min(64) >> depth) as u8;
7742+ for cell in 0..side / 4 {
7743+ if let Some(c) = above_tx.get_mut(x / 4 + cell) {
7744+ *c = tx;
7745+ }
7746+ if let Some(c) = left_tx.get_mut(y / 4 + cell) {
7747+ *c = tx;
7748+ }
7749+ }
7750+ };
7675 let mut ibc = allow_intrabc.then(|| {7751 let mut ibc = allow_intrabc.then(|| {
7676 Ibc::new(7752 Ibc::new(
7677 (7753 (
@@ -7751,6 +7827,7 @@ pub(crate) fn encode_key_frame_inner(
7751 &mode_bits(above_mode[c64], left_mode[r64]),7827 &mode_bits(above_mode[c64], left_mode[r64]),
7752 tx_select,7828 tx_select,
7753 None,7829 None,
7830+ tx_ctx(&above_tx, &left_tx, (x64, y64), SUPERBLOCK),
7754 fctx,7831 fctx,
7755 );7832 );
7756 let cost = cost + search.lambda * crate::tile::partition_bits(SUPERBLOCK, false);7833 let cost = cost + search.lambda * crate::tile::partition_bits(SUPERBLOCK, false);
@@ -7834,7 +7911,9 @@ pub(crate) fn encode_key_frame_inner(
7834 &search,7911 &search,
7835 &mode_bits(above_mode[c0], left_mode[r0]),7912 &mode_bits(above_mode[c0], left_mode[r0]),
7836 tx_select,7913 tx_select,
7837- ibc.as_mut(), fctx,7914+ ibc.as_mut(),
7915+ tx_ctx(&above_tx, &left_tx, (x, y), BLOCK),
7916+ fctx,
7838 );7917 );
7839 cost_whole += search.lambda * partition_bits(BLOCK, false);7918 cost_whole += search.lambda * partition_bits(BLOCK, false);
7840 let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK);7919 let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK);
@@ -7869,12 +7948,26 @@ pub(crate) fn encode_key_frame_inner(
7869 &search,7948 &search,
7870 &mode_bits(above_mode[sc], left_mode[sr]),7949 &mode_bits(above_mode[sc], left_mode[sr]),
7871 tx_select,7950 tx_select,
7872- ibc.as_mut(), fctx,7951+ ibc.as_mut(),
7952+ tx_ctx(
7953+ &above_tx,
7954+ &left_tx,
7955+ (x + (sub % 2) * SUB, y + (sub / 2) * SUB),
7956+ SUB,
7957+ ),
7958+ fctx,
7873 );7959 );
7874 cost_split += cost;7960 cost_split += cost;
7875 split_modes.push(block.mode);7961 split_modes.push(block.mode);
7876 above_mode[sc] = block.mode;7962 above_mode[sc] = block.mode;
7877 left_mode[sr] = block.mode;7963 left_mode[sr] = block.mode;
7964+ publish_tx(
7965+ &mut above_tx,
7966+ &mut left_tx,
7967+ (x + (sub % 2) * SUB, y + (sub / 2) * SUB),
7968+ SUB,
7969+ block.tx_depth,
7970+ );
7878 split.push(block);7971 split.push(block);
7879 } else {7972 } else {
7880 // A straddling 16x16: two (or, at a true corner, one)7973 // A straddling 16x16: two (or, at a true corner, one)
@@ -7902,10 +7995,13 @@ pub(crate) fn encode_key_frame_inner(
7902 &search,7995 &search,
7903 &leaf_mode_bits,7996 &leaf_mode_bits,
7904 tx_select,7997 tx_select,
7905- None, fctx,7998+ None,
7999+ tx_ctx(&above_tx, &left_tx, (leaf_x, leaf_y), 8),
8000+ fctx,
7906 );8001 );
7907 cost_split += cost;8002 cost_split += cost;
7908 split_modes.push(leaf.mode);8003 split_modes.push(leaf.mode);
8004+ publish_tx(&mut above_tx, &mut left_tx, (leaf_x, leaf_y), 8, leaf.tx_depth);
7909 leaves.push(leaf);8005 leaves.push(leaf);
7910 }8006 }
7911 split.push(BlockCoeffs {8007 split.push(BlockCoeffs {
@@ -7934,6 +8030,7 @@ pub(crate) fn encode_key_frame_inner(
7934 above_mode[c0 + cell] = whole.mode;8030 above_mode[c0 + cell] = whole.mode;
7935 left_mode[r0 + cell] = whole.mode;8031 left_mode[r0 + cell] = whole.mode;
7936 }8032 }
8033+ publish_tx(&mut above_tx, &mut left_tx, (x, y), BLOCK, whole.tx_depth);
7937 modes.push(whole.mode);8034 modes.push(whole.mode);
7938 blocks.push(Quadrant::Whole(whole));8035 blocks.push(Quadrant::Whole(whole));
7939 }8036 }
@@ -7946,6 +8043,7 @@ pub(crate) fn encode_key_frame_inner(
7946 above_mode[sb_col * 4 + cell] = block.mode;8043 above_mode[sb_col * 4 + cell] = block.mode;
7947 left_mode[sb_row * 4 + cell] = block.mode;8044 left_mode[sb_row * 4 + cell] = block.mode;
7948 }8045 }
8046+ publish_tx(&mut above_tx, &mut left_tx, (x64, y64), SUPERBLOCK, block.tx_depth);
7949 I64_HITS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);8047 I64_HITS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
7950 if !whole_inside {8048 if !whole_inside {
7951 I64_HITS[2].fetch_add(1, std::sync::atomic::Ordering::Relaxed);8049 I64_HITS[2].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
@@ -11495,7 +11593,8 @@ pub(crate) fn encode_inter_frame(
11495 fctx,11593 fctx,
11496 );11594 );
11497 cost8 += cost;11595 cost8 += cost;
11498- record_mi(&mut grid, mr, mc, 2, leaf8.inter, leaf8.skip);11596+ record_mi(&mut grid, mr, mc, 2, leaf8.inter, leaf8.skip,
11597+ txfm_px(8, leaf8.inter.is_some(), leaf8.skip, leaf8.tx_depth));
11499 eight.push(leaf8);11598 eight.push(leaf8);
11500 }11599 }
11501 (eight, cost8)11600 (eight, cost8)
@@ -11517,7 +11616,8 @@ pub(crate) fn encode_inter_frame(
11517 }11616 }
11518 partition_hit(2);11617 partition_hit(2);
11519 cost += leaf_cost;11618 cost += leaf_cost;
11520- record_mi(&mut grid, mi_row, mi_col, 4, leaf.inter, leaf.skip);11619+ record_mi(&mut grid, mi_row, mi_col, 4, leaf.inter, leaf.skip,
11620+ txfm_px(SUB, leaf.inter.is_some(), leaf.skip, leaf.tx_depth));
11521 leaves.push(leaf);11621 leaves.push(leaf);
11522 }11622 }
11523 (leaves, cost)11623 (leaves, cost)
@@ -11536,7 +11636,8 @@ pub(crate) fn encode_inter_frame(
11536 }11636 }
11537 partition_hit(0);11637 partition_hit(0);
11538 sb_cost += cost_whole;11638 sb_cost += cost_whole;
11539- record_mi(&mut grid, mi_row, mi_col, 8, block.inter, block.skip);11639+ record_mi(&mut grid, mi_row, mi_col, 8, block.inter, block.skip,
11640+ txfm_px(BLOCK, block.inter.is_some(), block.skip, block.tx_depth));
11540 blocks.push((r32 * cols + c32, Quadrant::Whole(block)));11641 blocks.push((r32 * cols + c32, Quadrant::Whole(block)));
11541 } else {11642 } else {
11542 // The sub-positions actually inside the true frame, same11643 // The sub-positions actually inside the true frame, same
@@ -11603,7 +11704,8 @@ pub(crate) fn encode_inter_frame(
11603 // to (0,0), which is the encoder-grid-drift class11704 // to (0,0), which is the encoder-grid-drift class
11604 // -- correct only for as long as a leaf can never11705 // -- correct only for as long as a leaf can never
11605 // be compound.11706 // be compound.
11606- record_mi(&mut grid, mi_row, mi_col, 4, block.inter, block.skip);11707+ record_mi(&mut grid, mi_row, mi_col, 4, block.inter, block.skip,
11708+ txfm_px(SUB, block.inter.is_some(), block.skip, block.tx_depth));
11607 leaves.push(block);11709 leaves.push(block);
11608 } else {11710 } else {
11609 let (x_sub, y_sub) = (sc * SUB, sr * SUB);11711 let (x_sub, y_sub) = (sc * SUB, sr * SUB);
@@ -11642,7 +11744,8 @@ pub(crate) fn encode_inter_frame(
11642 );11744 );
11643 // Same publication point as the 16x1611745 // Same publication point as the 16x16
11644 // straddling leaf above (`record_mi`).11746 // straddling leaf above (`record_mi`).
11645- record_mi(&mut grid, mi_row, mi_col, 2, leaf.inter, leaf.skip);11747+ record_mi(&mut grid, mi_row, mi_col, 2, leaf.inter, leaf.skip,
11748+ txfm_px(8, leaf.inter.is_some(), leaf.skip, leaf.tx_depth));
11646 eight.push(leaf);11749 eight.push(leaf);
11647 }11750 }
11648 leaves.push(BlockCoeffs {11751 leaves.push(BlockCoeffs {
@@ -11673,7 +11776,8 @@ pub(crate) fn encode_inter_frame(
11673 // Overwrites every 16x16 mi cell the quadrant searches11776 // Overwrites every 16x16 mi cell the quadrant searches
11674 // published under this superblock, so the next block's stack11777 // published under this superblock, so the next block's stack
11675 // is built off the block that was really coded.11778 // is built off the block that was really coded.
11676- record_mi(&mut grid, sb_r * 16, sb_c * 16, 16, block.inter, block.skip);11779+ record_mi(&mut grid, sb_r * 16, sb_c * 16, 16, block.inter, block.skip,
11780+ txfm_px(SUPERBLOCK, block.inter.is_some(), block.skip, block.tx_depth));
11677 blocks.push(((sb_r * 2) * cols + sb_c * 2, Quadrant::Whole64(block)));11781 blocks.push(((sb_r * 2) * cols + sb_c * 2, Quadrant::Whole64(block)));
11678 for q in 1..4 {11782 for q in 1..4 {
11679 let (r32, c32) = (sb_r * 2 + q / 2, sb_c * 2 + q % 2);11783 let (r32, c32) = (sb_r * 2 + q / 2, sb_c * 2 + q % 2);
@@ -12434,6 +12538,81 @@ mod tests {
12434 }12538 }
12435 }12539 }
12436 12540
12541+ /// The `tx_depth` symbol is priced off the row the WRITER codes it
12542+ /// against, on BOTH frame types (class `pricer-context-zero`; lane-ctx2
12543+ /// built only the key-frame half, which lost on its own).
12544+ ///
12545+ /// Key frames read the deblock grid (`tile::tx_size_ctx_of`, off
12546+ /// `Neighbours::record_tx`'s bands); an inter frame's INTRA block reads
12547+ /// the `TXFM_CONTEXT` bands instead (`tile::tx_size_ctx_txfm_of`, where an
12548+ /// inter neighbour votes its own BLOCK size and a skipped inter block
12549+ /// publishes its block size), which the encoder now publishes per mi cell
12550+ /// through `record_mi`. Row 0 -- both neighbours narrower, where splitting
12551+ /// reads cheapest -- is what both halves used to price at.
12552+ #[test]
12553+ fn the_tx_depth_pricer_uses_the_writers_own_context_row() {
12554+ use crate::cdf::{TX_SIZE_CAT1, TX_SIZE_CAT2};
12555+ use crate::encode::symbol_bits as b;
12556+ use crate::tile::{tx_size_ctx_of, tx_size_ctx_txfm_of};
12557+ // The key-frame rule: (has_above, has_left), the two band cells, this
12558+ // block's own largest transform -> the row. A neighbour outside the
12559+ // tile contributes nothing however wide its band cell reads.
12560+ for (has, bands, max_tx, want) in [
12561+ ((false, false), (64u8, 64u8), 32usize, 0usize),
12562+ ((true, true), (16, 16), 32, 0),
12563+ ((true, true), (32, 16), 32, 1),
12564+ ((true, false), (32, 64), 32, 1),
12565+ ((true, true), (32, 64), 32, 2),
12566+ ((true, true), (8, 8), 8, 2),
12567+ ] {
12568+ assert_eq!(tx_size_ctx_of(has, bands, max_tx), want, "key {has:?} {bands:?} {max_tx}");
12569+ }
12570+ // An inter frame's intra block, its above/left neighbours coded INTER
12571+ // and published through `record_mi`: a 16x16 intra block at mi (4, 4)
12572+ // with 32x32 inter neighbours, which vote their BLOCK size (32 >= 16)
12573+ // whatever transform they left in the band.
12574+ let mut grid = MiGrid::new(16, 16);
12575+ let inter = Some(crate::tile::InterInfo {
12576+ ref1: None,
12577+ mv1: (0, 0),
12578+ ref_frame: crate::mvstack::LAST_FRAME,
12579+ mode: crate::tile::InterMode::NearestMv,
12580+ mv: (0, 0),
12581+ ref_mv_idx: 0,
12582+ });
12583+ // A SKIPPED 32x32 inter block publishes its block size (32), an
12584+ // unskipped one its resolved transform (32 at depth 0).
12585+ assert_eq!(txfm_px(BLOCK, true, true, 0), 32);
12586+ assert_eq!(txfm_px(BLOCK, true, false, 1), 16);
12587+ for (mi_row, mi_col) in [(0usize, 0usize), (0, 4), (4, 0)] {
12588+ record_mi(&mut grid, mi_row, mi_col, 8, inter, true, txfm_px(BLOCK, true, true, 0));
12589+ }
12590+ let ctx = tx_ctx_txfm(&grid, (4, 4), 16);
12591+ assert_eq!(
12592+ ctx,
12593+ tx_size_ctx_txfm_of(Some((32, true, 32)), Some((32, true, 32)), 16),
12594+ "the grid's row is not the writer's"
12595+ );
12596+ assert_eq!(ctx, 2, "two inter neighbours at least as wide vote row 2");
12597+ // libaom's default tables give rows 0 and 1 IDENTICAL probabilities in
12598+ // every category, so row 2 is the only row that moves a price -- and
12599+ // pricing at it is exactly what was red before this lane.
12600+ for (side, table) in [(16usize, &TX_SIZE_CAT1), (32, &TX_SIZE_CAT2)] {
12601+ for depth in 0..3 {
12602+ let got = tx_depth_bits(ctx, side, depth);
12603+ assert!(
12604+ (got - b(&table[ctx], depth)).abs() < 1e-9,
12605+ "side {side} depth {depth} at ctx {ctx}: priced {got}, writer codes {}",
12606+ b(&table[ctx], depth)
12607+ );
12608+ assert!(
12609+ (got - b(&table[0], depth)).abs() > 1e-9,
12610+ "side {side} depth {depth}: row {ctx} prices the same as row 0"
12611+ );
12612+ }
12613+ }
12614+ }
12615+
12437 /// lane-av1tpl2's propagated map, on the two properties every decision12616 /// lane-av1tpl2's propagated map, on the two properties every decision
12438 /// under it rests on: a frame the whole lookahead window repeats exactly12617 /// under it rests on: a frame the whole lookahead window repeats exactly
12439 /// (uniform texture) leans on every cell alike, so every factor is 1 and12618 /// (uniform texture) leans on every cell alike, so every factor is 1 and
@@ -16119,9 +16298,14 @@ mod tests {
16119 // at q=60. Re-taken on lane-ctx2: a single-reference candidate now16298 // at q=60. Re-taken on lane-ctx2: a single-reference candidate now
16120 // pays the `comp_mode = 0` symbol the writer codes ahead of it, so16299 // pays the `comp_mode = 0` symbol the writer codes ahead of it, so
16121 // single and compound are priced against the same syntax -- 8311 ->16300 // single and compound are priced against the same syntax -- 8311 ->
16122- // 8325 bytes at q=150 and 33087 -> 33014 at q=60.16301+ // 8325 bytes at q=150 and 33087 -> 33014 at q=60. Re-taken again in
16302+ // the same lane: a key frame's `tx_depth` symbol is priced off the
16303+ // row the writer codes it against (the transform sides its above/left
16304+ // neighbours published) instead of row 0, so every intra block's
16305+ // depth-versus-split margin moved -- 8325 bytes at q=150 (the same
16306+ // size, different depths) and 33014 at q=60, both hashes new.
16123 let pins: [(u8, usize, u64); 2] =16307 let pins: [(u8, usize, u64); 2] =
16124- [(150, 8325, 0xf004_8258_bc60_ff05), (60, 33014, 0x5bc2_5dfd_bd13_18de)];16308+ [(150, 8325, 0x774a_4716_1cc1_b0ed), (60, 33014, 0xa19b_4fa6_cde1_1180)];
16125 let coded: Vec<(u8, usize, u64)> = pins16309 let coded: Vec<(u8, usize, u64)> = pins
16126 .iter()16310 .iter()
16127 .map(|&(q, _, _)| {16311 .map(|&(q, _, _)| {
crates/ec-av1/src/mvstack.rsmodified+26 -0
@@ -259,6 +259,14 @@ pub struct MiGrid {
259 /// writer and the decoder read their `skip` neighbours from their own259 /// writer and the decoder read their `skip` neighbours from their own
260 /// bands and leave this `false` (lane-ctx).260 /// bands and leave this `false` (lane-ctx).
261 skips: Vec<bool>,261 skips: Vec<bool>,
262+ /// The transform side, in pixels, each cell's block published into libaom's
263+ /// `TXFM_CONTEXT` bands (`tile::write_tx_syntax_inter`'s `record_txfm`) --
264+ /// a skipped inter block's own BLOCK size, everything else's resolved
265+ /// transform. Beside `cells` for the same reason `skips` is: the cell must
266+ /// stay 16 bytes, and the mv scan never reads this. Only the ENCODER fills
267+ /// it (`encode::record_mi`), so the RD pricer can read an inter frame's
268+ /// intra block `tx_depth` row off the writer's own bands (lane-txd).
269+ txfm: Vec<u8>,
262 /// lane-tiles r6: the current tile's own mi-unit bounds (spec: an MV270 /// lane-tiles r6: the current tile's own mi-unit bounds (spec: an MV
263 /// candidate scan never reaches across a tile boundary, mirroring271 /// candidate scan never reaches across a tile boundary, mirroring
264 /// `PlaneBuf`'s `tile_x0`/`tile_x1` reach clamp for intra prediction).272 /// `PlaneBuf`'s `tile_x0`/`tile_x1` reach clamp for intra prediction).
@@ -288,6 +296,7 @@ impl MiGrid {
288 rows,296 rows,
289 cells: vec![None; cols * rows],297 cells: vec![None; cols * rows],
290 skips: vec![false; cols * rows],298 skips: vec![false; cols * rows],
299+ txfm: vec![0; cols * rows],
291 tile_row0: 0,300 tile_row0: 0,
292 tile_col0: 0,301 tile_col0: 0,
293 tile_row1: rows,302 tile_row1: rows,
@@ -343,6 +352,23 @@ impl MiGrid {
343 }352 }
344 }353 }
345 354
355+ /// Records the transform side one block published at `(row, col)` -- see
356+ /// `txfm`.
357+ pub fn set_txfm(&mut self, row: usize, col: usize, tx_px: u8) {
358+ if row < self.rows && col < self.cols {
359+ self.txfm[row * self.cols + col] = tx_px;
360+ }
361+ }
362+
363+ /// The transform side published at `(row, col)`, `0` outside the grid.
364+ pub fn txfm_at(&self, row: usize, col: usize) -> u8 {
365+ if row < self.rows && col < self.cols {
366+ self.txfm[row * self.cols + col]
367+ } else {
368+ 0
369+ }
370+ }
371+
346 /// The `skip` flag of the unit at `(row, col)`, `false` outside this372 /// The `skip` flag of the unit at `(row, col)`, `false` outside this
347 /// tile's own window (the same clamp [`Self::get`] applies).373 /// tile's own window (the same clamp [`Self::get`] applies).
348 pub fn skip_at(&self, row: usize, col: usize) -> bool {374 pub fn skip_at(&self, row: usize, col: usize) -> bool {
crates/ec-av1/src/tile.rsmodified+52 -12
@@ -2410,16 +2410,13 @@ impl Neighbours {
2410 /// off a CDF row one higher than the decoder's, which is the first2410 /// off a CDF row one higher than the decoder's, which is the first
2411 /// divergence of the inter `TxMode::Select` stream.2411 /// divergence of the inter `TxMode::Select` stream.
2412 fn tx_size_ctx_txfm(&self, (mi_r, mi_c): (usize, usize), own_side: usize) -> usize {2412 fn tx_size_ctx_txfm(&self, (mi_r, mi_c): (usize, usize), own_side: usize) -> usize {
2413- let (has_above, has_left) = (self.has_above(mi_r), self.has_left(mi_c));2413+ tx_size_ctx_txfm_of(
2414- let mut above = usize::from(self.above_txfm[mi_c]) >= own_side;2414+ self.has_above(mi_r)
2415- let mut left = usize::from(self.left_txfm[mi_r]) >= own_side;2415+ .then(|| (self.above_txfm[mi_c], self.above_inter[mi_c], self.above_side_mi[mi_c])),
2416- if has_above && self.above_inter[mi_c] {2416+ self.has_left(mi_c)
2417- above = self.above_side_mi[mi_c] >= own_side;2417+ .then(|| (self.left_txfm[mi_r], self.left_inter[mi_r], self.left_side_mi[mi_r])),
2418- }2418+ own_side,
2419- if has_left && self.left_inter[mi_r] {2419+ )
2420- left = self.left_side_mi[mi_r] >= own_side;
2421- }
2422- usize::from(has_above && above) + usize::from(has_left && left)
2423 }2420 }
2424 2421
2425 /// `get_tx_size_context` (decode.rs [`crate::decode::tx_size_context`]):2422 /// `get_tx_size_context` (decode.rs [`crate::decode::tx_size_context`]):
@@ -2427,8 +2424,11 @@ impl Neighbours {
2427 /// largest transform, plus whether the one to the left is at least as2424 /// largest transform, plus whether the one to the left is at least as
2428 /// tall. A neighbour outside the tile contributes nothing.2425 /// tall. A neighbour outside the tile contributes nothing.
2429 fn tx_size_ctx(&self, (mi_r, mi_c): (usize, usize), max_tx: usize) -> usize {2426 fn tx_size_ctx(&self, (mi_r, mi_c): (usize, usize), max_tx: usize) -> usize {
2430- usize::from(self.has_above(mi_r) && usize::from(self.above_tx[mi_c]) >= max_tx)2427+ tx_size_ctx_of(
2431- + usize::from(self.has_left(mi_c) && usize::from(self.left_tx[mi_r]) >= max_tx)2428+ (self.has_above(mi_r), self.has_left(mi_c)),
2429+ (self.above_tx[mi_c], self.left_tx[mi_r]),
2430+ max_tx,
2431+ )
2432 }2432 }
2433 2433
2434 /// Publishes one block's resolved transform side over every 4x4 unit it2434 /// Publishes one block's resolved transform side over every 4x4 unit it
@@ -4309,6 +4309,46 @@ fn write_luma_tus(
4309 Ok(())4309 Ok(())
4310 }4310 }
4311 4311
4312+/// `get_tx_size_context` (decode.rs [`crate::decode::tx_size_context`]) over
4313+/// the two band cells themselves: whether the transform above is at least as
4314+/// wide as this block's own largest transform, plus whether the one to the
4315+/// left is at least as tall; a neighbour outside the tile contributes nothing.
4316+///
4317+/// Free-standing so the RD pricer can read the row the writer really codes a
4318+/// `tx_depth` symbol against off its own published band, without two
4319+/// transcriptions of the same rule (lane-ctx2).
4320+pub(crate) fn tx_size_ctx_of(
4321+ (has_above, has_left): (bool, bool),
4322+ (above_tx, left_tx): (u8, u8),
4323+ max_tx: usize,
4324+) -> usize {
4325+ usize::from(has_above && usize::from(above_tx) >= max_tx)
4326+ + usize::from(has_left && usize::from(left_tx) >= max_tx)
4327+}
4328+
4329+/// [`Neighbours::tx_size_ctx_txfm`] over the neighbours' own published state:
4330+/// each of `above`/`left` is `None` outside the tile (which contributes
4331+/// nothing), else its `TXFM_CONTEXT` band cell, whether it was coded inter,
4332+/// and its BLOCK side in pixels -- an INTER neighbour votes that block side
4333+/// rather than its transform.
4334+///
4335+/// Free-standing so the RD pricer reads the row an inter frame's intra block
4336+/// is really coded against off its own published bands, with no second
4337+/// transcription of the rule (lane-txd).
4338+pub(crate) fn tx_size_ctx_txfm_of(
4339+ above: Option<(u8, bool, usize)>,
4340+ left: Option<(u8, bool, usize)>,
4341+ own_side: usize,
4342+) -> usize {
4343+ let vote = |n: Option<(u8, bool, usize)>| match n {
4344+ Some((txfm, is_inter, block_side)) => {
4345+ if is_inter { block_side >= own_side } else { usize::from(txfm) >= own_side }
4346+ }
4347+ None => false,
4348+ };
4349+ usize::from(vote(above)) + usize::from(vote(left))
4350+}
4351+
4312 /// Writes one intra block's luma residual under `TxMode::Select`: the4352 /// Writes one intra block's luma residual under `TxMode::Select`: the
4313 /// `tx_depth` symbol (spec 5.11.16 `read_selected_tx_size`, off the size4353 /// `tx_depth` symbol (spec 5.11.16 `read_selected_tx_size`, off the size
4314 /// category's own CDF at [`Neighbours::tx_size_ctx`]'s row), then the4354 /// category's own CDF at [`Neighbours::tx_size_ctx`]'s row), then the