forge
Explore
Log in

Don't you dare go Hollow

A key frame's tx_depth symbol is priced at the writer's context row

Browse files at this commit

ae42b5f7649a500347c7d6f6c7143fb02321ff57Ahmet Kaan GümüşParent: ef9e91a
  • fix
    • a key frame's tx_depth symbol is priced at the writer's context row
Show full message
fix(av1): a key frame's tx_depth symbol is priced at the writer's context row

The writer codes tx_depth off tx_size_ctx -- whether the transform above
and the one to the left are at least as wide as this block's own largest
transform -- while the RD search priced every block at row 0, the row
where both neighbours are narrower and splitting reads cheapest (class
pricer-context-zero).

The key-frame tile search now keeps the same two bands the writer's
record_tx publishes (each coded block's resolved transform side over its
own 4x4 units, frame-absolute so a superblock row needs no reset), and
code_square prices its tx_depth off tile::tx_size_ctx_of, the writer's
own rule, now shared rather than transcribed.

Pins re-taken: 8325 / 33014, both hashes new.
crates/ec-av1/src/encode.rsmodified+143 -21
@@ -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+ // 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,
61206134 fctx,
61216135 );
61226136 let luma_set = if side == 8 {
@@ -7672,6 +7686,38 @@ pub(crate) fn encode_key_frame_inner(
76727686 clip_planes_to_tile(&mut luma, &mut chroma, rect);
76737687 let mut above_mode = vec![DC_PRED; cols * 2];
76747688 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+ };
76757721 let mut ibc = allow_intrabc.then(|| {
76767722 Ibc::new(
76777723 (
@@ -7751,6 +7797,7 @@ pub(crate) fn encode_key_frame_inner(
77517797 &mode_bits(above_mode[c64], left_mode[r64]),
77527798 tx_select,
77537799 None,
7800+ tx_ctx(&above_tx, &left_tx, (x64, y64), SUPERBLOCK),
77547801 fctx,
77557802 );
77567803 let cost = cost + search.lambda * crate::tile::partition_bits(SUPERBLOCK, false);
@@ -7834,7 +7881,9 @@ pub(crate) fn encode_key_frame_inner(
78347881 &search,
78357882 &mode_bits(above_mode[c0], left_mode[r0]),
78367883 tx_select,
7837- ibc.as_mut(), fctx,
7884+ ibc.as_mut(),
7885+ tx_ctx(&above_tx, &left_tx, (x, y), BLOCK),
7886+ fctx,
78387887 );
78397888 cost_whole += search.lambda * partition_bits(BLOCK, false);
78407889 let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK);
@@ -7869,12 +7918,26 @@ pub(crate) fn encode_key_frame_inner(
78697918 &search,
78707919 &mode_bits(above_mode[sc], left_mode[sr]),
78717920 tx_select,
7872- ibc.as_mut(), fctx,
7921+ ibc.as_mut(),
7922+ tx_ctx(
7923+ &above_tx,
7924+ &left_tx,
7925+ (x + (sub % 2) * SUB, y + (sub / 2) * SUB),
7926+ SUB,
7927+ ),
7928+ fctx,
78737929 );
78747930 cost_split += cost;
78757931 split_modes.push(block.mode);
78767932 above_mode[sc] = block.mode;
78777933 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+ );
78787941 split.push(block);
78797942 } else {
78807943 // A straddling 16x16: two (or, at a true corner, one)
@@ -7902,10 +7965,13 @@ pub(crate) fn encode_key_frame_inner(
79027965 &search,
79037966 &leaf_mode_bits,
79047967 tx_select,
7905- None, fctx,
7968+ None,
7969+ tx_ctx(&above_tx, &left_tx, (leaf_x, leaf_y), 8),
7970+ fctx,
79067971 );
79077972 cost_split += cost;
79087973 split_modes.push(leaf.mode);
7974+ publish_tx(&mut above_tx, &mut left_tx, (leaf_x, leaf_y), 8, leaf.tx_depth);
79097975 leaves.push(leaf);
79107976 }
79117977 split.push(BlockCoeffs {
@@ -7934,6 +8000,7 @@ pub(crate) fn encode_key_frame_inner(
79348000 above_mode[c0 + cell] = whole.mode;
79358001 left_mode[r0 + cell] = whole.mode;
79368002 }
8003+ publish_tx(&mut above_tx, &mut left_tx, (x, y), BLOCK, whole.tx_depth);
79378004 modes.push(whole.mode);
79388005 blocks.push(Quadrant::Whole(whole));
79398006 }
@@ -7946,6 +8013,7 @@ pub(crate) fn encode_key_frame_inner(
79468013 above_mode[sb_col * 4 + cell] = block.mode;
79478014 left_mode[sb_row * 4 + cell] = block.mode;
79488015 }
8016+ publish_tx(&mut above_tx, &mut left_tx, (x64, y64), SUPERBLOCK, block.tx_depth);
79498017 I64_HITS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
79508018 if !whole_inside {
79518019 I64_HITS[2].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
@@ -12434,6 +12502,55 @@ mod tests {
1243412502 }
1243512503 }
1243612504
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+
1243712554 /// lane-av1tpl2's propagated map, on the two properties every decision
1243812555 /// under it rests on: a frame the whole lookahead window repeats exactly
1243912556 /// (uniform texture) leans on every cell alike, so every factor is 1 and
@@ -16119,9 +16236,14 @@ mod tests {
1611916236 // at q=60. Re-taken on lane-ctx2: a single-reference candidate now
1612016237 // pays the `comp_mode = 0` symbol the writer codes ahead of it, so
1612116238 // single and compound are priced against the same syntax -- 8311 ->
16122- // 8325 bytes at q=150 and 33087 -> 33014 at q=60.
16239+ // 8325 bytes at q=150 and 33087 -> 33014 at q=60. Re-taken again in
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.
1612316245 let pins: [(u8, usize, u64); 2] =
16124- [(150, 8325, 0xf004_8258_bc60_ff05), (60, 33014, 0x5bc2_5dfd_bd13_18de)];
16246+ [(150, 8325, 0x774a_4716_1cc1_b0ed), (60, 33014, 0xa19b_4fa6_cde1_1180)];
1612516247 let coded: Vec<(u8, usize, u64)> = pins
1612616248 .iter()
1612716249 .map(|&(q, _, _)| {
crates/ec-av1/src/tile.rsmodified+22 -2
@@ -2427,8 +2427,11 @@ impl Neighbours {
24272427 /// largest transform, plus whether the one to the left is at least as
24282428 /// tall. A neighbour outside the tile contributes nothing.
24292429 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)
2430+ tx_size_ctx_of(
2431+ (self.has_above(mi_r), self.has_left(mi_c)),
2432+ (self.above_tx[mi_c], self.left_tx[mi_r]),
2433+ max_tx,
2434+ )
24322435 }
24332436
24342437 /// Publishes one block's resolved transform side over every 4x4 unit it
@@ -4309,6 +4312,23 @@ fn write_luma_tus(
43094312 Ok(())
43104313 }
43114314
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+
43124332 /// Writes one intra block's luma residual under `TxMode::Select`: the
43134333 /// `tx_depth` symbol (spec 5.11.16 `read_selected_tx_size`, off the size
43144334 /// category's own CDF at [`Neighbours::tx_size_ctx`]'s row), then the