A key frame's tx_depth symbol is priced at the writer's context row
Browse files at this commitae42b5f7649a500347c7d6f6c7143fb02321ff57Parent: ef9e91a- fix
- a key frame's tx_depth symbol is priced at the writer's context row
2 changed files +165 -23
- crates/ec-av1/src/encode.rs +143 -21
- crates/ec-av1/src/tile.rs +22 -2
crates/ec-av1/src/encode.rsmodified+143 -21
| @@ -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. | |
| 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 { | |
| 4236 | 4240 | 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), | |
| 4241 | 4245 | } |
| 4242 | 4246 | } |
| 4243 | 4247 | |
| @@ -5222,7 +5226,12 @@ fn code_square( | ||
| 5222 | 5226 | tx_select: bool, |
| 5223 | 5227 | // This tile's intra block-copy state ([`Ibc`]), `None` on a frame whose |
| 5224 | 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 | 5235 | ) -> (BlockCoeffs, f64) { |
| 5227 | 5236 | let (luma_set, chroma_set) = if side == SUPERBLOCK { |
| 5228 | 5237 | // A whole superblock coded as ONE intra block (lane-i64): one |
| @@ -5266,11 +5275,11 @@ fn code_square( | ||
| 5266 | 5275 | // `tx_depth_bits(32, 0)` -- otherwise the 64-versus-four-32 comparison |
| 5267 | 5276 | // below hands the root a symbol for free. |
| 5268 | 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 | 5280 | if tx_select && max_tx_depth(side) > 0 { |
| 5272 | 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 | 5283 | 0usize, |
| 5275 | 5284 | luma.snapshot(x, y, side), |
| 5276 | 5285 | luma_coeffs, |
| @@ -5281,7 +5290,7 @@ fn code_square( | ||
| 5281 | 5290 | luma.code_tx_depth(at, mode, angle_delta, depth, None, search, fctx); |
| 5282 | 5291 | let cost_d = sse |
| 5283 | 5292 | + 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 | 5294 | if cost_d < best.0 { |
| 5286 | 5295 | best = (cost_d, depth, luma.snapshot(x, y, side), coeffs(&levels, side)); |
| 5287 | 5296 | best_types = types; |
| @@ -5329,7 +5338,7 @@ fn code_square( | ||
| 5329 | 5338 | * (trial.bits |
| 5330 | 5339 | + mode_bits[DC_PRED as usize] |
| 5331 | 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 | 5342 | if cost_p < cost { |
| 5334 | 5343 | cost = cost_p; |
| 5335 | 5344 | mode = DC_PRED as u8; |
| @@ -5395,7 +5404,7 @@ fn code_square( | ||
| 5395 | 5404 | + mode_bits[DC_PRED as usize] |
| 5396 | 5405 | + one_bits |
| 5397 | 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 | 5408 | if cost_f < cost && best.as_ref().is_none_or(|b| cost_f < b.0) { |
| 5400 | 5409 | best = Some(( |
| 5401 | 5410 | cost_f, |
| @@ -6117,6 +6126,11 @@ fn code_square_inter( | ||
| 6117 | 6126 | mode_bits, |
| 6118 | 6127 | tx_select() && tx_select_inter(), |
| 6119 | 6128 | 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, | |
| 6120 | 6134 | fctx, |
| 6121 | 6135 | ); |
| 6122 | 6136 | let luma_set = if side == 8 { |
| @@ -7672,6 +7686,38 @@ pub(crate) fn encode_key_frame_inner( | ||
| 7672 | 7686 | clip_planes_to_tile(&mut luma, &mut chroma, rect); |
| 7673 | 7687 | let mut above_mode = vec![DC_PRED; cols * 2]; |
| 7674 | 7688 | 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 | + }; | |
| 7675 | 7721 | let mut ibc = allow_intrabc.then(|| { |
| 7676 | 7722 | Ibc::new( |
| 7677 | 7723 | ( |
| @@ -7751,6 +7797,7 @@ pub(crate) fn encode_key_frame_inner( | ||
| 7751 | 7797 | &mode_bits(above_mode[c64], left_mode[r64]), |
| 7752 | 7798 | tx_select, |
| 7753 | 7799 | None, |
| 7800 | + tx_ctx(&above_tx, &left_tx, (x64, y64), SUPERBLOCK), | |
| 7754 | 7801 | fctx, |
| 7755 | 7802 | ); |
| 7756 | 7803 | let cost = cost + search.lambda * crate::tile::partition_bits(SUPERBLOCK, false); |
| @@ -7834,7 +7881,9 @@ pub(crate) fn encode_key_frame_inner( | ||
| 7834 | 7881 | &search, |
| 7835 | 7882 | &mode_bits(above_mode[c0], left_mode[r0]), |
| 7836 | 7883 | tx_select, |
| 7837 | - ibc.as_mut(), fctx, | |
| 7884 | + ibc.as_mut(), | |
| 7885 | + tx_ctx(&above_tx, &left_tx, (x, y), BLOCK), | |
| 7886 | + fctx, | |
| 7838 | 7887 | ); |
| 7839 | 7888 | cost_whole += search.lambda * partition_bits(BLOCK, false); |
| 7840 | 7889 | let after_whole = snapshot(&luma, &chroma, (x, y), BLOCK); |
| @@ -7869,12 +7918,26 @@ pub(crate) fn encode_key_frame_inner( | ||
| 7869 | 7918 | &search, |
| 7870 | 7919 | &mode_bits(above_mode[sc], left_mode[sr]), |
| 7871 | 7920 | 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, | |
| 7873 | 7929 | ); |
| 7874 | 7930 | cost_split += cost; |
| 7875 | 7931 | split_modes.push(block.mode); |
| 7876 | 7932 | above_mode[sc] = block.mode; |
| 7877 | 7933 | 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 | + ); | |
| 7878 | 7941 | split.push(block); |
| 7879 | 7942 | } else { |
| 7880 | 7943 | // A straddling 16x16: two (or, at a true corner, one) |
| @@ -7902,10 +7965,13 @@ pub(crate) fn encode_key_frame_inner( | ||
| 7902 | 7965 | &search, |
| 7903 | 7966 | &leaf_mode_bits, |
| 7904 | 7967 | tx_select, |
| 7905 | - None, fctx, | |
| 7968 | + None, | |
| 7969 | + tx_ctx(&above_tx, &left_tx, (leaf_x, leaf_y), 8), | |
| 7970 | + fctx, | |
| 7906 | 7971 | ); |
| 7907 | 7972 | cost_split += cost; |
| 7908 | 7973 | split_modes.push(leaf.mode); |
| 7974 | + publish_tx(&mut above_tx, &mut left_tx, (leaf_x, leaf_y), 8, leaf.tx_depth); | |
| 7909 | 7975 | leaves.push(leaf); |
| 7910 | 7976 | } |
| 7911 | 7977 | split.push(BlockCoeffs { |
| @@ -7934,6 +8000,7 @@ pub(crate) fn encode_key_frame_inner( | ||
| 7934 | 8000 | above_mode[c0 + cell] = whole.mode; |
| 7935 | 8001 | left_mode[r0 + cell] = whole.mode; |
| 7936 | 8002 | } |
| 8003 | + publish_tx(&mut above_tx, &mut left_tx, (x, y), BLOCK, whole.tx_depth); | |
| 7937 | 8004 | modes.push(whole.mode); |
| 7938 | 8005 | blocks.push(Quadrant::Whole(whole)); |
| 7939 | 8006 | } |
| @@ -7946,6 +8013,7 @@ pub(crate) fn encode_key_frame_inner( | ||
| 7946 | 8013 | above_mode[sb_col * 4 + cell] = block.mode; |
| 7947 | 8014 | left_mode[sb_row * 4 + cell] = block.mode; |
| 7948 | 8015 | } |
| 8016 | + publish_tx(&mut above_tx, &mut left_tx, (x64, y64), SUPERBLOCK, block.tx_depth); | |
| 7949 | 8017 | I64_HITS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 7950 | 8018 | if !whole_inside { |
| 7951 | 8019 | I64_HITS[2].fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| @@ -12434,6 +12502,55 @@ mod tests { | ||
| 12434 | 12502 | } |
| 12435 | 12503 | } |
| 12436 | 12504 | |
| 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 | + | |
| 12437 | 12554 | /// lane-av1tpl2's propagated map, on the two properties every decision |
| 12438 | 12555 | /// under it rests on: a frame the whole lookahead window repeats exactly |
| 12439 | 12556 | /// (uniform texture) leans on every cell alike, so every factor is 1 and |
| @@ -16119,9 +16236,14 @@ mod tests { | ||
| 16119 | 16236 | // at q=60. Re-taken on lane-ctx2: a single-reference candidate now |
| 16120 | 16237 | // pays the `comp_mode = 0` symbol the writer codes ahead of it, so |
| 16121 | 16238 | // 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. | |
| 16123 | 16245 | 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)]; | |
| 16125 | 16247 | let coded: Vec<(u8, usize, u64)> = pins |
| 16126 | 16248 | .iter() |
| 16127 | 16249 | .map(|&(q, _, _)| { |
crates/ec-av1/src/tile.rsmodified+22 -2
| @@ -2427,8 +2427,11 @@ impl Neighbours { | ||
| 2427 | 2427 | /// largest transform, plus whether the one to the left is at least as |
| 2428 | 2428 | /// tall. A neighbour outside the tile contributes nothing. |
| 2429 | 2429 | 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 | + ) | |
| 2432 | 2435 | } |
| 2433 | 2436 | |
| 2434 | 2437 | /// Publishes one block's resolved transform side over every 4x4 unit it |
| @@ -4309,6 +4312,23 @@ fn write_luma_tus( | ||
| 4309 | 4312 | Ok(()) |
| 4310 | 4313 | } |
| 4311 | 4314 | |
| 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 | + | |
| 4312 | 4332 | /// Writes one intra block's luma residual under `TxMode::Select`: the |
| 4313 | 4333 | /// `tx_depth` symbol (spec 5.11.16 `read_selected_tx_size`, off the size |
| 4314 | 4334 | /// category's own CDF at [`Neighbours::tx_size_ctx`]'s row), then the |