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

4eb77f997075706ed3a9e5583f27c59cb6480ba3Ahmet Kaan GümüşParent: 8fa4f6a
  • 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 {
42274227 }
42284228 }
42294229
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 its
4232-/// neighbours' transform sizes, which the search has no view of; the term is
4233-/// the same for every depth of one block either way, so it only shifts the
4234-/// depth-0-versus-split margin, never the ordering between two splits.
4235-fn tx_depth_bits(side: usize, depth: usize) -> f64 {
4230+/// 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 the
4232+/// transform sides the blocks above and to the left published, which is what
4233+/// the writer's `write_luma_select` codes the symbol against.
4234+///
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 {
42364240 match side {
4237- 8 => symbol_bits(&cdf::TX_SIZE_CAT0[0], depth),
4238- 16 => symbol_bits(&cdf::TX_SIZE_CAT1[0], depth),
4239- 32 => symbol_bits(&cdf::TX_SIZE_CAT2[0], depth),
4240- _ => symbol_bits(&cdf::TX_SIZE_CAT3[0], depth),
4241+ 8 => symbol_bits(&cdf::TX_SIZE_CAT0[ctx], depth),
4242+ 16 => symbol_bits(&cdf::TX_SIZE_CAT1[ctx], depth),
4243+ 32 => symbol_bits(&cdf::TX_SIZE_CAT2[ctx], depth),
4244+ _ => symbol_bits(&cdf::TX_SIZE_CAT3[ctx], depth),
42414245 }
42424246 }
42434247
@@ -5222,7 +5226,12 @@ fn code_square(
52225226 tx_select: bool,
52235227 // This tile's intra block-copy state ([`Ibc`]), `None` on a frame whose
52245228 // 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,
52265235 ) -> (BlockCoeffs, f64) {
52275236 let (luma_set, chroma_set) = if side == SUPERBLOCK {
52285237 // A whole superblock coded as ONE intra block (lane-i64): one
@@ -5266,11 +5275,11 @@ fn code_square(
52665275 // `tx_depth_bits(32, 0)` -- otherwise the 64-versus-four-32 comparison
52675276 // below hands the root a symbol for free.
52685277 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);
52705279 }
52715280 if tx_select && max_tx_depth(side) > 0 {
52725281 let mut best = (
5273- cost + search.lambda * tx_depth_bits(side, 0),
5282+ cost + search.lambda * tx_depth_bits(tx_ctx, side, 0),
52745283 0usize,
52755284 luma.snapshot(x, y, side),
52765285 luma_coeffs,
@@ -5281,7 +5290,7 @@ fn code_square(
52815290 luma.code_tx_depth(at, mode, angle_delta, depth, None, search, fctx);
52825291 let cost_d = sse
52835292 + 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));
52855294 if cost_d < best.0 {
52865295 best = (cost_d, depth, luma.snapshot(x, y, side), coeffs(&levels, side));
52875296 best_types = types;
@@ -5329,7 +5338,7 @@ fn code_square(
53295338 * (trial.bits
53305339 + mode_bits[DC_PRED as usize]
53315340 + 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 });
53335342 if cost_p < cost {
53345343 cost = cost_p;
53355344 mode = DC_PRED as u8;
@@ -5395,7 +5404,7 @@ fn code_square(
53955404 + mode_bits[DC_PRED as usize]
53965405 + one_bits
53975406 + 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 });
53995408 if cost_f < cost && best.as_ref().is_none_or(|b| cost_f < b.0) {
54005409 best = Some((
54015410 cost_f,
@@ -6117,6 +6126,11 @@ fn code_square_inter(
61176126 mode_bits,
61186127 tx_select() && tx_select_inter(),
61196128 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)),
61206134 fctx,
61216135 );
61226136 let luma_set = if side == 8 {
@@ -6820,6 +6834,31 @@ fn code_square_inter(
68206834 /// MV stack reads, over the `size` x `size` 4x4 units it covers. An intra
68216835 /// block casts no vote but is still a coded cell (see `code_square_inter`'s
68226836 /// 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+
68236862 fn record_mi(
68246863 grid: &mut MiGrid,
68256864 mi_row: usize,
@@ -6829,6 +6868,10 @@ fn record_mi(
68296868 // lane-ctx: the block's own `skip`, so the next block's MV stack can hand
68306869 // the RD pricer the two cells the writer's `skip_ctx` sums.
68316870 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,
68326875 ) {
68336876 let info = match inter {
68346877 Some(info) => MiInfo {
@@ -6874,6 +6917,7 @@ fn record_mi(
68746917 for dc in 0..usize::from(size) {
68756918 grid.set(mi_row + dr, mi_col + dc, info);
68766919 grid.set_skip(mi_row + dr, mi_col + dc, skip);
6920+ grid.set_txfm(mi_row + dr, mi_col + dc, txfm);
68776921 }
68786922 }
68796923 }
@@ -7672,6 +7716,38 @@ pub(crate) fn encode_key_frame_inner(
76727716 clip_planes_to_tile(&mut luma, &mut chroma, rect);
76737717 let mut above_mode = vec![DC_PRED; cols * 2];
76747718 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+ };
76757751 let mut ibc = allow_intrabc.then(|| {
76767752 Ibc::new(
76777753 (
@@ -7751,6 +7827,7 @@ pub(crate) fn encode_key_frame_inner(
77517827 &mode_bits(above_mode[c64], left_mode[r64]),
77527828 tx_select,
77537829 None,
7830+ tx_ctx(&above_tx, &left_tx, (x64, y64), SUPERBLOCK),
77547831 fctx,
77557832 );
77567833 let cost = cost + search.lambda * crate::tile::partition_bits(SUPERBLOCK, false);
@@ -7834,7 +7911,9 @@ pub(crate) fn encode_key_frame_inner(
78347911 &search,
78357912 &mode_bits(above_mode[c0], left_mode[r0]),
78367913 tx_select,
7837- ibc.as_mut(), fctx,
7914+ ibc.as_mut(),
7915+ tx_ctx(&above_tx, &left_tx, (x, y), BLOCK),
7916+ fctx,
78387917 );
78397918 cost_whole += search.lambda * partition_bits(BLOCK, false);
78407919 let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK);
@@ -7869,12 +7948,26 @@ pub(crate) fn encode_key_frame_inner(
78697948 &search,
78707949 &mode_bits(above_mode[sc], left_mode[sr]),
78717950 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,
78737959 );
78747960 cost_split += cost;
78757961 split_modes.push(block.mode);
78767962 above_mode[sc] = block.mode;
78777963 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+ );
78787971 split.push(block);
78797972 } else {
78807973 // A straddling 16x16: two (or, at a true corner, one)
@@ -7902,10 +7995,13 @@ pub(crate) fn encode_key_frame_inner(
79027995 &search,
79037996 &leaf_mode_bits,
79047997 tx_select,
7905- None, fctx,
7998+ None,
7999+ tx_ctx(&above_tx, &left_tx, (leaf_x, leaf_y), 8),
8000+ fctx,
79068001 );
79078002 cost_split += cost;
79088003 split_modes.push(leaf.mode);
8004+ publish_tx(&mut above_tx, &mut left_tx, (leaf_x, leaf_y), 8, leaf.tx_depth);
79098005 leaves.push(leaf);
79108006 }
79118007 split.push(BlockCoeffs {
@@ -7934,6 +8030,7 @@ pub(crate) fn encode_key_frame_inner(
79348030 above_mode[c0 + cell] = whole.mode;
79358031 left_mode[r0 + cell] = whole.mode;
79368032 }
8033+ publish_tx(&mut above_tx, &mut left_tx, (x, y), BLOCK, whole.tx_depth);
79378034 modes.push(whole.mode);
79388035 blocks.push(Quadrant::Whole(whole));
79398036 }
@@ -7946,6 +8043,7 @@ pub(crate) fn encode_key_frame_inner(
79468043 above_mode[sb_col * 4 + cell] = block.mode;
79478044 left_mode[sb_row * 4 + cell] = block.mode;
79488045 }
8046+ publish_tx(&mut above_tx, &mut left_tx, (x64, y64), SUPERBLOCK, block.tx_depth);
79498047 I64_HITS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
79508048 if !whole_inside {
79518049 I64_HITS[2].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
@@ -11495,7 +11593,8 @@ pub(crate) fn encode_inter_frame(
1149511593 fctx,
1149611594 );
1149711595 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));
1149911598 eight.push(leaf8);
1150011599 }
1150111600 (eight, cost8)
@@ -11517,7 +11616,8 @@ pub(crate) fn encode_inter_frame(
1151711616 }
1151811617 partition_hit(2);
1151911618 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));
1152111621 leaves.push(leaf);
1152211622 }
1152311623 (leaves, cost)
@@ -11536,7 +11636,8 @@ pub(crate) fn encode_inter_frame(
1153611636 }
1153711637 partition_hit(0);
1153811638 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));
1154011641 blocks.push((r32 * cols + c32, Quadrant::Whole(block)));
1154111642 } else {
1154211643 // The sub-positions actually inside the true frame, same
@@ -11603,7 +11704,8 @@ pub(crate) fn encode_inter_frame(
1160311704 // to (0,0), which is the encoder-grid-drift class
1160411705 // -- correct only for as long as a leaf can never
1160511706 // 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));
1160711709 leaves.push(block);
1160811710 } else {
1160911711 let (x_sub, y_sub) = (sc * SUB, sr * SUB);
@@ -11642,7 +11744,8 @@ pub(crate) fn encode_inter_frame(
1164211744 );
1164311745 // Same publication point as the 16x16
1164411746 // 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));
1164611749 eight.push(leaf);
1164711750 }
1164811751 leaves.push(BlockCoeffs {
@@ -11673,7 +11776,8 @@ pub(crate) fn encode_inter_frame(
1167311776 // Overwrites every 16x16 mi cell the quadrant searches
1167411777 // published under this superblock, so the next block's stack
1167511778 // 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));
1167711781 blocks.push(((sb_r * 2) * cols + sb_c * 2, Quadrant::Whole64(block)));
1167811782 for q in 1..4 {
1167911783 let (r32, c32) = (sb_r * 2 + q / 2, sb_c * 2 + q % 2);
@@ -12434,6 +12538,81 @@ mod tests {
1243412538 }
1243512539 }
1243612540
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+
1243712616 /// lane-av1tpl2's propagated map, on the two properties every decision
1243812617 /// under it rests on: a frame the whole lookahead window repeats exactly
1243912618 /// (uniform texture) leans on every cell alike, so every factor is 1 and
@@ -16119,9 +16298,14 @@ mod tests {
1611916298 // at q=60. Re-taken on lane-ctx2: a single-reference candidate now
1612016299 // pays the `comp_mode = 0` symbol the writer codes ahead of it, so
1612116300 // 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.
1612316307 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)];
1612516309 let coded: Vec<(u8, usize, u64)> = pins
1612616310 .iter()
1612716311 .map(|&(q, _, _)| {
crates/ec-av1/src/mvstack.rsmodified+26 -0
@@ -259,6 +259,14 @@ pub struct MiGrid {
259259 /// writer and the decoder read their `skip` neighbours from their own
260260 /// bands and leave this `false` (lane-ctx).
261261 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>,
262270 /// lane-tiles r6: the current tile's own mi-unit bounds (spec: an MV
263271 /// candidate scan never reaches across a tile boundary, mirroring
264272 /// `PlaneBuf`'s `tile_x0`/`tile_x1` reach clamp for intra prediction).
@@ -288,6 +296,7 @@ impl MiGrid {
288296 rows,
289297 cells: vec![None; cols * rows],
290298 skips: vec![false; cols * rows],
299+ txfm: vec![0; cols * rows],
291300 tile_row0: 0,
292301 tile_col0: 0,
293302 tile_row1: rows,
@@ -343,6 +352,23 @@ impl MiGrid {
343352 }
344353 }
345354
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+
346372 /// The `skip` flag of the unit at `(row, col)`, `false` outside this
347373 /// tile's own window (the same clamp [`Self::get`] applies).
348374 pub fn skip_at(&self, row: usize, col: usize) -> bool {
crates/ec-av1/src/tile.rsmodified+52 -12
@@ -2410,16 +2410,13 @@ impl Neighbours {
24102410 /// off a CDF row one higher than the decoder's, which is the first
24112411 /// divergence of the inter `TxMode::Select` stream.
24122412 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));
2414- let mut above = usize::from(self.above_txfm[mi_c]) >= own_side;
2415- let mut left = usize::from(self.left_txfm[mi_r]) >= own_side;
2416- if has_above && self.above_inter[mi_c] {
2417- above = self.above_side_mi[mi_c] >= own_side;
2418- }
2419- if has_left && self.left_inter[mi_r] {
2420- left = self.left_side_mi[mi_r] >= own_side;
2421- }
2422- usize::from(has_above && above) + usize::from(has_left && left)
2413+ tx_size_ctx_txfm_of(
2414+ self.has_above(mi_r)
2415+ .then(|| (self.above_txfm[mi_c], self.above_inter[mi_c], self.above_side_mi[mi_c])),
2416+ self.has_left(mi_c)
2417+ .then(|| (self.left_txfm[mi_r], self.left_inter[mi_r], self.left_side_mi[mi_r])),
2418+ own_side,
2419+ )
24232420 }
24242421
24252422 /// `get_tx_size_context` (decode.rs [`crate::decode::tx_size_context`]):
@@ -2427,8 +2424,11 @@ impl Neighbours {
24272424 /// largest transform, plus whether the one to the left is at least as
24282425 /// tall. A neighbour outside the tile contributes nothing.
24292426 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)
2431- + usize::from(self.has_left(mi_c) && usize::from(self.left_tx[mi_r]) >= max_tx)
2427+ tx_size_ctx_of(
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+ )
24322432 }
24332433
24342434 /// Publishes one block's resolved transform side over every 4x4 unit it
@@ -4309,6 +4309,46 @@ fn write_luma_tus(
43094309 Ok(())
43104310 }
43114311
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+
43124352 /// Writes one intra block's luma residual under `TxMode::Select`: the
43134353 /// `tx_depth` symbol (spec 5.11.16 `read_selected_tx_size`, off the size
43144354 /// category's own CDF at [`Neighbours::tx_size_ctx`]'s row), then the