forge
Explore
Log in

Don't you dare go Hollow

Inter blocks search the var-tx tree one halving deep

Browse files at this commit

710cb11ccd7b8806434ba5b629eebcfb15ea6628Ahmet Kaan GümüşParent: b8e3b16
  • feat
    • inter blocks search the var-tx tree one halving deep
Show full message
feat(av1): inter blocks search the var-tx tree one halving deep

With the inter frame at TxMode::Select an inter block now codes its
residual either as one transform over the whole block or as the four
transforms of half the side, chosen by RD against the txfm_split symbols
both arms spend. The writer codes the split root plus one unsplit flag
per child off the interleaved TXFM_CONTEXT bands, and the split units
read the inter coefficient tables. Blocks touching the true frame edge
stay flat: the reader skips var-tx units past the edge.

Still behind EC_AV1_TX_SELECT_INTER=1.
crates/ec-av1/src/encode.rsmodified+126 -8
@@ -1790,6 +1790,111 @@ pub(crate) fn take_tx_depth_hits() -> [usize; 3] {
17901790 std::array::from_fn(|d| TX_DEPTH_HITS[d].swap(0, std::sync::atomic::Ordering::Relaxed))
17911791 }
17921792
1793+/// How many inter blocks resolved to each var-tx depth (0 = one transform
1794+/// over the whole block, 1 = its four halves), the fire count a gate prints
1795+/// to see the split search reach its candidates at all.
1796+static INTER_TX_SPLIT_HITS: [std::sync::atomic::AtomicUsize; 2] =
1797+ [const { std::sync::atomic::AtomicUsize::new(0) }; 2];
1798+
1799+/// Reads [`INTER_TX_SPLIT_HITS`] and zeroes it.
1800+#[cfg(test)]
1801+pub(crate) fn take_inter_tx_split_hits() -> [usize; 2] {
1802+ std::array::from_fn(|d| INTER_TX_SPLIT_HITS[d].swap(0, std::sync::atomic::Ordering::Relaxed))
1803+}
1804+
1805+/// What the `txfm_split` symbols of one inter block cost at `depth`, priced
1806+/// off the neighbour-free context row the way [`tx_depth_bits`] is: depth 0
1807+/// is one flag, depth 1 is a split root plus one unsplit flag per child
1808+/// (decode.rs `read_var_tx_size` stops symbol-free only at `MAX_VARTX_DEPTH`
1809+/// or a 4x4 sub-size).
1810+fn txfm_split_bits(side: usize, depth: usize) -> f64 {
1811+ let max_tx = side.min(64);
1812+ let ctx = |tx: usize| {
1813+ crate::decode::txfm_partition_ctx_rect(
1814+ crate::decode::TXFM_CTX_INIT,
1815+ crate::decode::TXFM_CTX_INIT,
1816+ side,
1817+ tx,
1818+ tx,
1819+ )
1820+ };
1821+ if depth == 0 {
1822+ return symbol_bits(&cdf::TXFM_PARTITION[ctx(max_tx)], 0);
1823+ }
1824+ symbol_bits(&cdf::TXFM_PARTITION[ctx(max_tx)], 1)
1825+ + 4.0 * symbol_bits(&cdf::TXFM_PARTITION[ctx(max_tx / 2)], 0)
1826+}
1827+
1828+/// The inter block's own var-tx decision, once its motion vector is settled:
1829+/// its residual as ONE transform over the whole block, or as the four
1830+/// transforms of half the side the tree's first halving names. The prediction
1831+/// is the same motion compensation either way, so the split trial
1832+/// re-transforms the identical residual in four pieces -- it wins where four
1833+/// small transforms carry a locally varying residual cheaper than one big
1834+/// one. Commits the winner into `luma` and hands back its block-coordinate
1835+/// levels, the depth the writer codes and the cost the winner adds to (or
1836+/// takes off) the caller's already-computed flat cost.
1837+///
1838+/// Only for a block wholly inside the frame: past the true edge the reader's
1839+/// var-tx recursion skips the units that fall outside
1840+/// (decode.rs `read_var_tx_size`'s `max_w_mi`/`max_h_mi` guard), which would
1841+/// leave the residual units this writes unpaired.
1842+#[allow(clippy::too_many_arguments)]
1843+fn commit_inter_luma(
1844+ luma: &mut Plane,
1845+ (x, y): (usize, usize),
1846+ side: usize,
1847+ mv: (i32, i32),
1848+ reference: (&[u16], usize, usize, usize),
1849+ search: &Search,
1850+ flat: &Trial,
1851+ skip: bool, fctx: &crate::decode::FrameCtx,
1852+) -> (Vec<i32>, u8, f64) {
1853+ let eligible = tx_select()
1854+ && tx_select_inter()
1855+ && !skip
1856+ && side >= 16
1857+ && x + side <= luma.true_width
1858+ && y + side <= luma.true_height;
1859+ if !eligible {
1860+ luma.commit(x, y, side, flat);
1861+ return (flat.levels.clone(), 0, 0.0);
1862+ }
1863+ let tx = side / 2;
1864+ let set = if tx >= 16 { TxbSet::Luma16Inter } else { TxbSet::Luma8Inter };
1865+ let mut levels = vec![0i32; side * side];
1866+ let (mut sse, mut bits) = (0.0, 0.0);
1867+ let mut units = Vec::with_capacity(4);
1868+ for tu_row in 0..2 {
1869+ for tu_col in 0..2 {
1870+ let (tu_x, tu_y) = (x + tu_col * tx, y + tu_row * tx);
1871+ let trial = mc_trial(
1872+ luma, tu_x, tu_y, tx, mv, true, reference.0, reference.1, reference.2,
1873+ reference.3, false, search.base_q_idx, search.deadzone, set, fctx,
1874+ );
1875+ sse += trial.sse;
1876+ bits += trial.bits;
1877+ for row in 0..tx {
1878+ levels[(tu_row * tx + row) * side + tu_col * tx..][..tx]
1879+ .copy_from_slice(&trial.levels[row * tx..][..tx]);
1880+ }
1881+ units.push((tu_x, tu_y, trial));
1882+ }
1883+ }
1884+ let split_cost = sse + search.lambda * (bits + txfm_split_bits(side, 1));
1885+ let flat_cost = flat.sse + search.lambda * (flat.bits + txfm_split_bits(side, 0));
1886+ if split_cost < flat_cost {
1887+ for (tu_x, tu_y, trial) in &units {
1888+ luma.commit(*tu_x, *tu_y, tx, trial);
1889+ }
1890+ INTER_TX_SPLIT_HITS[1].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1891+ return (levels, 1, split_cost - flat_cost);
1892+ }
1893+ INTER_TX_SPLIT_HITS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
1894+ luma.commit(x, y, side, flat);
1895+ (flat.levels.clone(), 0, 0.0)
1896+}
1897+
17931898 #[allow(clippy::too_many_arguments)]
17941899 fn code_square(
17951900 luma: &mut Plane,
@@ -2187,47 +2292,51 @@ fn code_square_inter(
21872292 }
21882293 if let (cost, Some((luma_new, u_new, v_new, skip_new, new_mv))) = best {
21892294 if cost < intra_cost {
2190- luma.commit(x, y, side, &luma_new);
2295+ let (levels, tx_depth, dcost) = commit_inter_luma(
2296+ luma, (x, y), side, new_mv, (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3), search, &luma_new, skip_new, fctx,
2297+ );
21912298 chroma[0].commit(x / 2, y / 2, side / 2, &u_new);
21922299 chroma[1].commit(x / 2, y / 2, side / 2, &v_new);
21932300 return (
21942301 BlockCoeffs {
2195- luma: coeffs(&luma_new.levels, side),
2302+ luma: coeffs(&levels, side),
21962303 u: coeffs(&u_new.levels, side / 2),
21972304 v: coeffs(&v_new.levels, side / 2),
21982305 mode: DC_PRED as u8,
21992306 uv_mode: DC_PRED,
22002307 skip: skip_new,
22012308 eight: None,
2202- tx_depth: 0,
2309+ tx_depth,
22032310 inter: Some(InterInfo {
22042311 mode: InterMode::NewMv,
22052312 mv: new_mv,
22062313 }),
22072314 },
2208- cost,
2315+ cost + dcost,
22092316 );
22102317 }
22112318 }
22122319
22132320 if inter_cost < intra_cost {
2214- luma.commit(x, y, side, &luma_trial);
2321+ let (levels, tx_depth, dcost) = commit_inter_luma(
2322+ luma, (x, y), side, mv, (ref_luma.0.as_slice(), ref_luma.1, ref_luma.2, ref_luma.3), search, &luma_trial, skip, fctx,
2323+ );
22152324 chroma[0].commit(x / 2, y / 2, side / 2, &u);
22162325 chroma[1].commit(x / 2, y / 2, side / 2, &v);
22172326 (BlockCoeffs {
2218- luma: coeffs(&luma_trial.levels, side),
2327+ luma: coeffs(&levels, side),
22192328 u: coeffs(&u.levels, side / 2),
22202329 v: coeffs(&v.levels, side / 2),
22212330 mode: DC_PRED as u8,
22222331 uv_mode: DC_PRED,
22232332 skip,
22242333 eight: None,
2225- tx_depth: 0,
2334+ tx_depth,
22262335 inter: Some(InterInfo {
22272336 mode: InterMode::NearestMv,
22282337 mv,
22292338 }),
2230- }, inter_cost)
2339+ }, inter_cost + dcost)
22312340 } else {
22322341 (intra_block, intra_cost)
22332342 }
@@ -5905,7 +6014,16 @@ mod tests {
59056014 let (width, height) = (128usize, 64usize);
59066015 let pictures: Vec<Picture> =
59076016 (0..5).map(|i| panned_test_card(width, height, i * 3)).collect();
6017+ let _ = take_inter_tx_split_hits();
59086018 let encoded = encode_sequence_with_ctx(&pictures, 100, 0.5, fctx).unwrap();
6019+ let split = take_inter_tx_split_hits();
6020+ eprintln!("inter var-tx: {} blocks flat, {} split", split[0], split[1]);
6021+ // [[gate-blind-to-feature]]: with inter `TxMode::Select` on, this
6022+ // content splits -- a green round trip in which the split never fires
6023+ // would prove only the flat path.
6024+ if tx_select() && tx_select_inter() {
6025+ assert!(split[1] > 0, "the inter var-tx split never fired");
6026+ }
59096027 let decoded = crate::stream::decode_stream(&encoded.stream).expect("our decoder");
59106028 assert_eq!(decoded.len(), encoded.frames.len(), "frame count");
59116029 for (i, (frame, dec)) in encoded.frames.iter().zip(&decoded).enumerate() {
crates/ec-av1/src/tile.rsmodified+51 -12
@@ -1717,6 +1717,9 @@ fn write_luma_tus(
17171717 grid: &[i32],
17181718 mode: usize,
17191719 scans: &[Vec<u16>; 4],
1720+ // lane-av1tx2 r2: an INTER block's split units read the inter coefficient
1721+ // tables, not the intra ones (`TxbSet::Luma*Inter`).
1722+ inter: bool,
17201723 ) -> Result<()> {
17211724 if tx < side && side > 32 {
17221725 // A 64x64 block's own levels reach the writer as the 32x32 corner a
@@ -1729,11 +1732,15 @@ fn write_luma_tus(
17291732 }
17301733 let coeff_side = tx.min(32);
17311734 let n = side / tx;
1732- let set = match tx {
1733- 32 => TxbSet::Luma32,
1734- 16 => TxbSet::Luma16,
1735- 8 => TxbSet::Luma8,
1736- 4 => TxbSet::Luma4,
1735+ let set = match (tx, inter) {
1736+ (32, false) => TxbSet::Luma32,
1737+ (16, false) => TxbSet::Luma16,
1738+ (8, false) => TxbSet::Luma8,
1739+ (4, false) => TxbSet::Luma4,
1740+ (32, true) => TxbSet::Luma32Inter,
1741+ (16, true) => TxbSet::Luma16Inter,
1742+ (8, true) => TxbSet::Luma8Inter,
1743+ (4, true) => TxbSet::Luma4Inter,
17371744 _ => TxbSet::Luma64,
17381745 };
17391746 let scan = match coeff_side {
@@ -1819,7 +1826,7 @@ fn write_luma_select(
18191826 }
18201827 }
18211828 let tx = max_tx >> depth;
1822- write_luma_tus(enc, cdfs, neighbours, at_mi, side, tx, grid, mode, scans)?;
1829+ write_luma_tus(enc, cdfs, neighbours, at_mi, side, tx, grid, mode, scans, false)?;
18231830 neighbours.record_tx(at_mi, side, tx);
18241831 Ok(tx)
18251832 }
@@ -1860,11 +1867,42 @@ fn write_tx_syntax_inter(
18601867 max_tx,
18611868 max_tx,
18621869 );
1863- enc.symbol(0, &mut cdfs.txfm_partition[ctx]);
1864- neighbours.record_txfm(at_mi, max_tx, max_tx, max_tx);
1865- } else {
1866- neighbours.record_txfm(at_mi, side, side, side);
1870+ enc.symbol(usize::from(depth != 0), &mut cdfs.txfm_partition[ctx]);
1871+ if depth == 0 {
1872+ neighbours.record_txfm(at_mi, max_tx, max_tx, max_tx);
1873+ return max_tx;
1874+ }
1875+ // lane-av1tx2 r2: one halving of the var-tx tree. Each child of a
1876+ // split root codes its own `txfm_split` (decode.rs
1877+ // `read_var_tx_size` only stops symbol-free at `MAX_VARTX_DEPTH`
1878+ // or at a 4x4 sub-size), off the bands the children before it
1879+ // published -- so the context read and the update interleave per
1880+ // unit, exactly as the reader's recursion does.
1881+ let tx = max_tx / 2;
1882+ for row in (0..max_tx / MI).step_by(tx / MI) {
1883+ for col in (0..max_tx / MI).step_by(tx / MI) {
1884+ let unit = (mi_r + row, mi_c + col);
1885+ // The encoder only picks depth 1 for a block wholly
1886+ // inside the frame; the reader skips a unit past the true
1887+ // edge, which would leave the residual units unpaired.
1888+ debug_assert!(
1889+ unit.0 < neighbours.mi_rows && unit.1 < neighbours.mi_cols,
1890+ "a split inter transform unit past the frame edge"
1891+ );
1892+ let ctx = txfm_partition_ctx_rect(
1893+ neighbours.above_txfm[unit.1],
1894+ neighbours.left_txfm[unit.0],
1895+ side,
1896+ tx,
1897+ tx,
1898+ );
1899+ enc.symbol(0, &mut cdfs.txfm_partition[ctx]);
1900+ neighbours.record_txfm(unit, tx, tx, tx);
1901+ }
1902+ }
1903+ return tx;
18671904 }
1905+ neighbours.record_txfm(at_mi, side, side, side);
18681906 return max_tx;
18691907 }
18701908 let ctx = neighbours.tx_size_ctx_txfm(at_mi, max_tx);
@@ -3128,6 +3166,7 @@ pub fn sb_coeff_inter_frame_tile_tx(
31283166 &grids[0],
31293167 mode_for_tx,
31303168 &all_scans,
3169+ is_inter,
31313170 )?;
31323171 }
31333172 write_block_planes(
@@ -3336,7 +3375,7 @@ fn write_inter_frame_leaf(
33363375 ];
33373376 if split {
33383377 write_luma_tus(
3339- enc, cdfs, neighbours, at_mi, SUB, tx, &grids[0], mode_for_tx, all_scans,
3378+ enc, cdfs, neighbours, at_mi, SUB, tx, &grids[0], mode_for_tx, all_scans, is_inter,
33403379 )?;
33413380 }
33423381 write_block_planes(
@@ -3561,7 +3600,7 @@ fn write_inter_frame_leaf8(
35613600 ];
35623601 if split {
35633602 write_luma_tus(
3564- enc, cdfs, neighbours, leaf_mi, 8, tx, &grids[0], mode_for_tx, all_scans,
3603+ enc, cdfs, neighbours, leaf_mi, 8, tx, &grids[0], mode_for_tx, all_scans, is_inter,
35653604 )?;
35663605 }
35673606 write_block_planes(