Inter blocks search the var-tx tree one halving deep
Browse files at this commit710cb11ccd7b8806434ba5b629eebcfb15ea6628Parent: b8e3b16- feat
- inter blocks search the var-tx tree one halving deep
2 changed files +177 -20
- crates/ec-av1/src/encode.rs +126 -8
- crates/ec-av1/src/tile.rs +51 -12
crates/ec-av1/src/encode.rsmodified+126 -8
| @@ -1790,6 +1790,111 @@ pub(crate) fn take_tx_depth_hits() -> [usize; 3] { | ||
| 1790 | 1790 | std::array::from_fn(|d| TX_DEPTH_HITS[d].swap(0, std::sync::atomic::Ordering::Relaxed)) |
| 1791 | 1791 | } |
| 1792 | 1792 | |
| 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 | + | |
| 1793 | 1898 | #[allow(clippy::too_many_arguments)] |
| 1794 | 1899 | fn code_square( |
| 1795 | 1900 | luma: &mut Plane, |
| @@ -2187,47 +2292,51 @@ fn code_square_inter( | ||
| 2187 | 2292 | } |
| 2188 | 2293 | if let (cost, Some((luma_new, u_new, v_new, skip_new, new_mv))) = best { |
| 2189 | 2294 | 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 | + ); | |
| 2191 | 2298 | chroma[0].commit(x / 2, y / 2, side / 2, &u_new); |
| 2192 | 2299 | chroma[1].commit(x / 2, y / 2, side / 2, &v_new); |
| 2193 | 2300 | return ( |
| 2194 | 2301 | BlockCoeffs { |
| 2195 | - luma: coeffs(&luma_new.levels, side), | |
| 2302 | + luma: coeffs(&levels, side), | |
| 2196 | 2303 | u: coeffs(&u_new.levels, side / 2), |
| 2197 | 2304 | v: coeffs(&v_new.levels, side / 2), |
| 2198 | 2305 | mode: DC_PRED as u8, |
| 2199 | 2306 | uv_mode: DC_PRED, |
| 2200 | 2307 | skip: skip_new, |
| 2201 | 2308 | eight: None, |
| 2202 | - tx_depth: 0, | |
| 2309 | + tx_depth, | |
| 2203 | 2310 | inter: Some(InterInfo { |
| 2204 | 2311 | mode: InterMode::NewMv, |
| 2205 | 2312 | mv: new_mv, |
| 2206 | 2313 | }), |
| 2207 | 2314 | }, |
| 2208 | - cost, | |
| 2315 | + cost + dcost, | |
| 2209 | 2316 | ); |
| 2210 | 2317 | } |
| 2211 | 2318 | } |
| 2212 | 2319 | |
| 2213 | 2320 | 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 | + ); | |
| 2215 | 2324 | chroma[0].commit(x / 2, y / 2, side / 2, &u); |
| 2216 | 2325 | chroma[1].commit(x / 2, y / 2, side / 2, &v); |
| 2217 | 2326 | (BlockCoeffs { |
| 2218 | - luma: coeffs(&luma_trial.levels, side), | |
| 2327 | + luma: coeffs(&levels, side), | |
| 2219 | 2328 | u: coeffs(&u.levels, side / 2), |
| 2220 | 2329 | v: coeffs(&v.levels, side / 2), |
| 2221 | 2330 | mode: DC_PRED as u8, |
| 2222 | 2331 | uv_mode: DC_PRED, |
| 2223 | 2332 | skip, |
| 2224 | 2333 | eight: None, |
| 2225 | - tx_depth: 0, | |
| 2334 | + tx_depth, | |
| 2226 | 2335 | inter: Some(InterInfo { |
| 2227 | 2336 | mode: InterMode::NearestMv, |
| 2228 | 2337 | mv, |
| 2229 | 2338 | }), |
| 2230 | - }, inter_cost) | |
| 2339 | + }, inter_cost + dcost) | |
| 2231 | 2340 | } else { |
| 2232 | 2341 | (intra_block, intra_cost) |
| 2233 | 2342 | } |
| @@ -5905,7 +6014,16 @@ mod tests { | ||
| 5905 | 6014 | let (width, height) = (128usize, 64usize); |
| 5906 | 6015 | let pictures: Vec<Picture> = |
| 5907 | 6016 | (0..5).map(|i| panned_test_card(width, height, i * 3)).collect(); |
| 6017 | + let _ = take_inter_tx_split_hits(); | |
| 5908 | 6018 | 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 | + } | |
| 5909 | 6027 | let decoded = crate::stream::decode_stream(&encoded.stream).expect("our decoder"); |
| 5910 | 6028 | assert_eq!(decoded.len(), encoded.frames.len(), "frame count"); |
| 5911 | 6029 | 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( | ||
| 1717 | 1717 | grid: &[i32], |
| 1718 | 1718 | mode: usize, |
| 1719 | 1719 | 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, | |
| 1720 | 1723 | ) -> Result<()> { |
| 1721 | 1724 | if tx < side && side > 32 { |
| 1722 | 1725 | // A 64x64 block's own levels reach the writer as the 32x32 corner a |
| @@ -1729,11 +1732,15 @@ fn write_luma_tus( | ||
| 1729 | 1732 | } |
| 1730 | 1733 | let coeff_side = tx.min(32); |
| 1731 | 1734 | 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, | |
| 1737 | 1744 | _ => TxbSet::Luma64, |
| 1738 | 1745 | }; |
| 1739 | 1746 | let scan = match coeff_side { |
| @@ -1819,7 +1826,7 @@ fn write_luma_select( | ||
| 1819 | 1826 | } |
| 1820 | 1827 | } |
| 1821 | 1828 | 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)?; | |
| 1823 | 1830 | neighbours.record_tx(at_mi, side, tx); |
| 1824 | 1831 | Ok(tx) |
| 1825 | 1832 | } |
| @@ -1860,11 +1867,42 @@ fn write_tx_syntax_inter( | ||
| 1860 | 1867 | max_tx, |
| 1861 | 1868 | max_tx, |
| 1862 | 1869 | ); |
| 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; | |
| 1867 | 1904 | } |
| 1905 | + neighbours.record_txfm(at_mi, side, side, side); | |
| 1868 | 1906 | return max_tx; |
| 1869 | 1907 | } |
| 1870 | 1908 | let ctx = neighbours.tx_size_ctx_txfm(at_mi, max_tx); |
| @@ -3128,6 +3166,7 @@ pub fn sb_coeff_inter_frame_tile_tx( | ||
| 3128 | 3166 | &grids[0], |
| 3129 | 3167 | mode_for_tx, |
| 3130 | 3168 | &all_scans, |
| 3169 | + is_inter, | |
| 3131 | 3170 | )?; |
| 3132 | 3171 | } |
| 3133 | 3172 | write_block_planes( |
| @@ -3336,7 +3375,7 @@ fn write_inter_frame_leaf( | ||
| 3336 | 3375 | ]; |
| 3337 | 3376 | if split { |
| 3338 | 3377 | 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, | |
| 3340 | 3379 | )?; |
| 3341 | 3380 | } |
| 3342 | 3381 | write_block_planes( |
| @@ -3561,7 +3600,7 @@ fn write_inter_frame_leaf8( | ||
| 3561 | 3600 | ]; |
| 3562 | 3601 | if split { |
| 3563 | 3602 | 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, | |
| 3565 | 3604 | )?; |
| 3566 | 3605 | } |
| 3567 | 3606 | write_block_planes( |