forge
Explore
Log in

Don't you dare go Hollow

WIP lane-av1tx: orchestrator snapshot at the agent turn cap (unverified)

Browse files at this commit

b56ffce72627c78d759aec513fc3101bdc91baeeAhmet Kaan GümüşParent: 13f5083
crates/ec-av1/src/decode.rsmodified+2 -2
@@ -16764,7 +16764,7 @@ fn txfm_partition_ctx(above_px: u8, left_px: u8, blk_max_px: usize, tx_px: usize
1676416764 /// category from `txsize_sqr_up_map[tx_size]` -- all three collapse to one
1676516765 /// side for a square transform, which is why the square form above is a
1676616766 /// special case of this one.
16767-fn txfm_partition_ctx_rect(
16767+pub(crate) fn txfm_partition_ctx_rect(
1676816768 above_px: u8,
1676916769 left_px: u8,
1677016770 blk_max_px: usize,
@@ -16894,7 +16894,7 @@ fn set_txfm_ctxs(
1689416894 /// with `tx_size_high[TX_SIZES_LARGEST]` (`av1_common_int.h`), i.e. 64
1689516895 /// pixels, so an unwritten neighbour reads as the *widest* possible
1689616896 /// transform, not the narrowest.
16897-const TXFM_CTX_INIT: u8 = 64;
16897+pub(crate) const TXFM_CTX_INIT: u8 = 64;
1689816898
1689916899 /// libaom's `MAX_VARTX_DEPTH`: an inter block's transform tree halves at most
1690016900 /// twice below its own largest transform.
crates/ec-av1/src/tile.rsmodified+263 -36
@@ -15,6 +15,7 @@ use ec_core::{Error, Result};
1515
1616 use crate::cdf;
1717 use crate::cdf_state::{Cdfs, MvComponentCdfs, TxbSet, TxbTables};
18+use crate::decode::{TXFM_CTX_INIT, txfm_partition_ctx_rect};
1819 use crate::msac::SymbolEncoder;
1920 use crate::mvstack::{MiGrid, MiInfo, NO_REF1, find_mv_stack, mv16, single_ref_ctx};
2021
@@ -661,6 +662,14 @@ struct Neighbours {
661662 /// The same, down the left edge (the transform's HEIGHT, `tx_h_px_at`;
662663 /// every transform this writer codes is square, so one value serves).
663664 left_tx: Vec<u8>,
665+ /// libaom's `above_txfm_context`/`left_txfm_context` (`TXFM_CONTEXT`), per
666+ /// 4x4 mode-info unit: what an INTER frame's `txfm_split` and `tx_depth`
667+ /// symbols read their context from (decode.rs `txfm_partition_ctx_rect`/
668+ /// `tx_size_context_txfm_rect`). Initialised to the widest transform, not
669+ /// zero (decode.rs `TXFM_CTX_INIT`).
670+ above_txfm: Vec<u8>,
671+ /// The same, down the left edge (transform HEIGHT).
672+ left_txfm: Vec<u8>,
664673 /// Whether the neighbour was coded inter, for the `is_inter` context.
665674 /// Unused by the key frame writers.
666675 above_inter: Vec<bool>,
@@ -711,6 +720,8 @@ impl Neighbours {
711720 left_inter: vec![false; rows * (SUB / MI)],
712721 above_tx: vec![0; cols * (SUB / MI)],
713722 left_tx: vec![0; rows * (SUB / MI)],
723+ above_txfm: vec![TXFM_CTX_INIT; cols * (SUB / MI)],
724+ left_txfm: vec![TXFM_CTX_INIT; rows * (SUB / MI)],
714725 mi_cols,
715726 mi_rows,
716727 }
@@ -725,6 +736,39 @@ impl Neighbours {
725736 self.left_skip.iter_mut().for_each(|s| *s = false);
726737 self.left_inter.iter_mut().for_each(|i| *i = false);
727738 self.left_tx.iter_mut().for_each(|t| *t = 0);
739+ self.left_txfm.iter_mut().for_each(|t| *t = TXFM_CTX_INIT);
740+ }
741+
742+ /// `txfm_partition_update`/`set_txfm_ctxs` (decode.rs): what every block
743+ /// of an INTER frame leaves in the two `TXFM_CONTEXT` bands -- its
744+ /// resolved transform size over the span `(w_px, h_px)`, which for a
745+ /// SKIPPED inter block is its own block size instead.
746+ fn record_txfm(&mut self, (mi_r, mi_c): (usize, usize), tx_px: usize, w_px: usize, h_px: usize) {
747+ for i in 0..h_px / MI {
748+ if let Some(cell) = self.left_txfm.get_mut(mi_r + i) {
749+ *cell = tx_px as u8;
750+ }
751+ }
752+ for i in 0..w_px / MI {
753+ if let Some(cell) = self.above_txfm.get_mut(mi_c + i) {
754+ *cell = tx_px as u8;
755+ }
756+ }
757+ }
758+
759+ /// `get_tx_size_context` as libaom writes it for a block inside an INTER
760+ /// frame (decode.rs `tx_size_context_txfm_rect`): the `TXFM_CONTEXT`
761+ /// bands, except that an *inter* neighbour contributes its own BLOCK size.
762+ fn tx_size_ctx_txfm(&self, (mi_r, mi_c): (usize, usize), own_side: usize) -> usize {
763+ let mut above = usize::from(self.above_txfm[mi_c]) >= own_side;
764+ let mut left = usize::from(self.left_txfm[mi_r]) >= own_side;
765+ if mi_r > 0 && self.above_inter[mi_c] {
766+ above = self.above_side_mi[mi_c] >= own_side;
767+ }
768+ if mi_c > 0 && self.left_inter[mi_r] {
769+ left = self.left_side_mi[mi_r] >= own_side;
770+ }
771+ usize::from(above) + usize::from(left)
728772 }
729773
730774 /// `get_tx_size_context` (decode.rs [`crate::decode::tx_size_context`]):
@@ -1648,45 +1692,24 @@ fn write_block_planes(
16481692 }
16491693 }
16501694
1651-/// Writes one intra block's luma residual under `TxMode::Select`: the
1652-/// `tx_depth` symbol (spec 5.11.16 `read_selected_tx_size`, off the size
1653-/// category's own CDF at [`Neighbours::tx_size_ctx`]'s row), then the
1654-/// `(side / tx)^2` transform units in raster order -- each predicted and
1655-/// coded against what the units before it already left behind, which is why
1656-/// every unit publishes its own coefficient context before the next one reads
1657-/// it (decode.rs `decode_block`'s multi-transform-unit branch).
1658-///
1659-/// `grid` is the block's levels in BLOCK coordinates (`side` by `side`) when
1660-/// the transform splits, and the single transform's own coefficient grid when
1661-/// it does not. Hands back the resolved transform side in pixels.
1695+/// The `(side / tx)^2` transform units of one block's luma residual, raster
1696+/// order, each with its own coefficient context and each published before the
1697+/// next one reads it (decode.rs `decode_block`'s multi-transform-unit branch).
1698+/// `grid` is the block's levels in BLOCK coordinates when the transform
1699+/// splits, and the single transform's own coefficient grid when it does not.
16621700 #[allow(clippy::too_many_arguments)]
1663-fn write_luma_select(
1701+fn write_luma_tus(
16641702 enc: &mut SymbolEncoder,
16651703 cdfs: &mut Cdfs,
16661704 neighbours: &mut Neighbours,
16671705 at_mi: (usize, usize),
16681706 side: usize,
1669- depth: usize,
1707+ tx: usize,
16701708 grid: &[i32],
16711709 mode: usize,
16721710 scans: &[Vec<u16>; 4],
1673-) -> Result<usize> {
1674- let max_tx = side.min(64);
1675- let ctx = neighbours.tx_size_ctx(at_mi, max_tx);
1676- match max_tx {
1677- 8 => enc.symbol(depth, &mut cdfs.tx_size_cat0[ctx]),
1678- 16 => enc.symbol(depth, &mut cdfs.tx_size_cat1[ctx]),
1679- 32 => enc.symbol(depth, &mut cdfs.tx_size_cat2[ctx]),
1680- 64 => enc.symbol(depth, &mut cdfs.tx_size_cat3[ctx]),
1681- _ => {
1682- return Err(Error::unsupported(
1683- "AV1 tile",
1684- "a tx_depth symbol is only coded at 8/16/32/64",
1685- ));
1686- }
1687- }
1688- let tx = max_tx >> depth;
1689- if depth > 0 && side > 32 {
1711+) -> Result<()> {
1712+ if tx < side && side > 32 {
16901713 // A 64x64 block's own levels reach the writer as the 32x32 corner a
16911714 // 64-point transform codes, not as a 64x64 block grid, so there is
16921715 // nothing to slice per unit here.
@@ -1746,10 +1769,107 @@ fn write_luma_select(
17461769 }
17471770 }
17481771 }
1772+ Ok(())
1773+}
1774+
1775+/// Writes one intra block's luma residual under `TxMode::Select`: the
1776+/// `tx_depth` symbol (spec 5.11.16 `read_selected_tx_size`, off the size
1777+/// category's own CDF at [`Neighbours::tx_size_ctx`]'s row), then the
1778+/// `(side / tx)^2` transform units in raster order -- each predicted and
1779+/// coded against what the units before it already left behind, which is why
1780+/// every unit publishes its own coefficient context before the next one reads
1781+/// it (decode.rs `decode_block`'s multi-transform-unit branch).
1782+///
1783+/// `grid` is the block's levels in BLOCK coordinates (`side` by `side`) when
1784+/// the transform splits, and the single transform's own coefficient grid when
1785+/// it does not. Hands back the resolved transform side in pixels.
1786+#[allow(clippy::too_many_arguments)]
1787+fn write_luma_select(
1788+ enc: &mut SymbolEncoder,
1789+ cdfs: &mut Cdfs,
1790+ neighbours: &mut Neighbours,
1791+ at_mi: (usize, usize),
1792+ side: usize,
1793+ depth: usize,
1794+ grid: &[i32],
1795+ mode: usize,
1796+ scans: &[Vec<u16>; 4],
1797+) -> Result<usize> {
1798+ let max_tx = side.min(64);
1799+ let ctx = neighbours.tx_size_ctx(at_mi, max_tx);
1800+ match max_tx {
1801+ 8 => enc.symbol(depth, &mut cdfs.tx_size_cat0[ctx]),
1802+ 16 => enc.symbol(depth, &mut cdfs.tx_size_cat1[ctx]),
1803+ 32 => enc.symbol(depth, &mut cdfs.tx_size_cat2[ctx]),
1804+ 64 => enc.symbol(depth, &mut cdfs.tx_size_cat3[ctx]),
1805+ _ => {
1806+ return Err(Error::unsupported(
1807+ "AV1 tile",
1808+ "a tx_depth symbol is only coded at 8/16/32/64",
1809+ ));
1810+ }
1811+ }
1812+ let tx = max_tx >> depth;
1813+ write_luma_tus(enc, cdfs, neighbours, at_mi, side, tx, grid, mode, scans)?;
17491814 neighbours.record_tx(at_mi, side, tx);
17501815 Ok(tx)
17511816 }
17521817
1818+/// The transform syntax one block of an INTER frame coded with
1819+/// `tx_mode == TxMode::Select` carries, in the place spec 5.11.16's
1820+/// `read_block_tx_size` reads it -- after the modes and the motion vector,
1821+/// before the residual:
1822+/// * an inter block that codes a residual walks a var-tx tree; this writer
1823+/// keeps it at its own largest transform, so that is one `txfm_split` flag
1824+/// of zero, off `txfm_partition_ctx_rect`'s row, publishing the bands per
1825+/// unit the way `read_var_tx_size` does.
1826+/// * a skipped inter block codes nothing and records its own BLOCK size
1827+/// (`set_txfm_ctxs`'s `skip_inter` term).
1828+/// * an intra block codes `tx_depth` like a key frame's, but off the
1829+/// `TXFM_CONTEXT` bands rather than the deblock grid.
1830+///
1831+/// Hands back the block's resolved luma transform side.
1832+#[allow(clippy::too_many_arguments)]
1833+fn write_tx_syntax_inter(
1834+ enc: &mut SymbolEncoder,
1835+ cdfs: &mut Cdfs,
1836+ neighbours: &mut Neighbours,
1837+ at_mi: (usize, usize),
1838+ side: usize,
1839+ is_inter: bool,
1840+ skip: bool,
1841+ depth: usize,
1842+) -> usize {
1843+ let max_tx = side.min(64);
1844+ let (mi_r, mi_c) = at_mi;
1845+ if is_inter {
1846+ if !skip {
1847+ let ctx = txfm_partition_ctx_rect(
1848+ neighbours.above_txfm[mi_c],
1849+ neighbours.left_txfm[mi_r],
1850+ side,
1851+ max_tx,
1852+ max_tx,
1853+ );
1854+ enc.symbol(0, &mut cdfs.txfm_partition[ctx]);
1855+ neighbours.record_txfm(at_mi, max_tx, max_tx, max_tx);
1856+ } else {
1857+ neighbours.record_txfm(at_mi, side, side, side);
1858+ }
1859+ return max_tx;
1860+ }
1861+ let ctx = neighbours.tx_size_ctx_txfm(at_mi, max_tx);
1862+ match max_tx {
1863+ 8 => enc.symbol(depth, &mut cdfs.tx_size_cat0[ctx]),
1864+ 16 => enc.symbol(depth, &mut cdfs.tx_size_cat1[ctx]),
1865+ 32 => enc.symbol(depth, &mut cdfs.tx_size_cat2[ctx]),
1866+ _ => enc.symbol(depth, &mut cdfs.tx_size_cat3[ctx]),
1867+ }
1868+ let tx = max_tx >> depth;
1869+ neighbours.record_txfm(at_mi, tx, side, side);
1870+ tx
1871+}
1872+
17531873 /// What a coded transform block leaves behind for the blocks that read it as a
17541874 /// neighbour.
17551875 fn neighbour_state(grid: &[i32]) -> Neighbour {
@@ -2498,6 +2618,25 @@ pub fn sb_coeff_inter_frame_tile(
24982618 mi_rows: u32,
24992619 base_q_idx: u8,
25002620 blocks: &[Quadrant],
2621+) -> Result<Vec<u8>> {
2622+ sb_coeff_inter_frame_tile_tx(mi_cols, mi_rows, base_q_idx, blocks, false)
2623+}
2624+
2625+/// [`sb_coeff_inter_frame_tile`] for a frame header carrying
2626+/// `tx_mode == TxMode::Select`: every block then codes the transform syntax
2627+/// [`write_tx_syntax_inter`] writes -- one `txfm_split` flag on an inter
2628+/// block that carries a residual, a `tx_depth` symbol on an intra one -- and
2629+/// an intra block whose depth is nonzero codes its luma as several transform
2630+/// units. With `tx_select` false this writes exactly the stream it always did.
2631+///
2632+/// # Errors
2633+/// As [`sb_coeff_inter_frame_tile`].
2634+pub fn sb_coeff_inter_frame_tile_tx(
2635+ mi_cols: u32,
2636+ mi_rows: u32,
2637+ base_q_idx: u8,
2638+ blocks: &[Quadrant],
2639+ tx_select: bool,
25012640 ) -> Result<Vec<u8>> {
25022641 check_blocks(mi_cols, mi_rows)?;
25032642 // `block_grid`'s ceiling, not a plain division: a true frame size that is
@@ -2552,6 +2691,14 @@ pub fn sb_coeff_inter_frame_tile(
25522691 let scan16 = default_scan(TX16);
25532692 let scan8 = default_scan(TX8);
25542693 let scan4 = default_scan(TX4);
2694+ // The same four tables again as one array, which is what the per-unit
2695+ // luma writer indexes by transform side.
2696+ let all_scans = [
2697+ scan32.clone(),
2698+ scan16.clone(),
2699+ scan8.clone(),
2700+ scan4.clone(),
2701+ ];
25552702 let zero_grids = [
25562703 vec![0i32; TX32 * TX32],
25572704 vec![0i32; TX16 * TX16],
@@ -2715,6 +2862,8 @@ pub fn sb_coeff_inter_frame_tile(
27152862 leaf_mi,
27162863 &scan8,
27172864 &scan4,
2865+ tx_select,
2866+ &all_scans,
27182867 )?;
27192868 let _ = (skip, is_inter);
27202869 }
@@ -2732,6 +2881,8 @@ pub fn sb_coeff_inter_frame_tile(
27322881 at16,
27332882 &scan16,
27342883 &scan8,
2884+ tx_select,
2885+ &all_scans,
27352886 )?;
27362887 } else {
27372888 let ctx16 = neighbours.partition_ctx(at16, SUB);
@@ -2780,6 +2931,8 @@ pub fn sb_coeff_inter_frame_tile(
27802931 leaf_mi,
27812932 &scan8,
27822933 &scan4,
2934+ tx_select,
2935+ &all_scans,
27832936 )?;
27842937 let _ = (skip, is_inter);
27852938 }
@@ -2931,6 +3084,22 @@ pub fn sb_coeff_inter_frame_tile(
29313084 }
29323085 }
29333086
3087+ let at_mi = (at.0 * (SUB / MI), at.1 * (SUB / MI));
3088+ let tx = if tx_select {
3089+ write_tx_syntax_inter(
3090+ &mut enc,
3091+ &mut cdfs,
3092+ &mut neighbours,
3093+ at_mi,
3094+ BLOCK,
3095+ is_inter,
3096+ block.skip,
3097+ usize::from(block.tx_depth),
3098+ )
3099+ } else {
3100+ BLOCK
3101+ };
3102+ let split = tx < BLOCK;
29343103 if block.skip {
29353104 neighbours.record(at, BLOCK, mode_for_tx, &zero_grids);
29363105 } else {
@@ -2939,6 +3108,19 @@ pub fn sb_coeff_inter_frame_tile(
29393108 level_grid(&block.u, TX16)?,
29403109 level_grid(&block.v, TX16)?,
29413110 ];
3111+ if split {
3112+ write_luma_tus(
3113+ &mut enc,
3114+ &mut cdfs,
3115+ &mut neighbours,
3116+ at_mi,
3117+ BLOCK,
3118+ tx,
3119+ &grids[0],
3120+ mode_for_tx,
3121+ &all_scans,
3122+ )?;
3123+ }
29423124 write_block_planes(
29433125 &mut enc,
29443126 &mut cdfs,
@@ -2951,9 +3133,9 @@ pub fn sb_coeff_inter_frame_tile(
29513133 &[&scan32, &scan16, &scan16],
29523134 &neighbours.around(at, BLOCK),
29533135 mode_for_tx,
2954- false,
3136+ split,
29553137 );
2956- neighbours.record(at, BLOCK, mode_for_tx, &grids);
3138+ neighbours.record_planes(at, BLOCK, mode_for_tx, &grids, !split);
29573139 }
29583140 neighbours.record_inter(at, BLOCK, block.skip, is_inter);
29593141 }
@@ -2984,6 +3166,8 @@ fn write_inter_frame_leaf(
29843166 at: (usize, usize),
29853167 scan16: &Vec<u16>,
29863168 scan8: &Vec<u16>,
3169+ tx_select: bool,
3170+ all_scans: &[Vec<u16>; 4],
29873171 ) -> Result<()> {
29883172 if block.inter.is_none() && usize::from(block.mode) >= INTRA_MODES {
29893173 return Err(Error::unsupported(
@@ -3112,6 +3296,22 @@ fn write_inter_frame_leaf(
31123296 }
31133297 }
31143298
3299+ let at_mi = (at.0 * (SUB / MI), at.1 * (SUB / MI));
3300+ let tx = if tx_select {
3301+ write_tx_syntax_inter(
3302+ enc,
3303+ cdfs,
3304+ neighbours,
3305+ at_mi,
3306+ SUB,
3307+ is_inter,
3308+ block.skip,
3309+ usize::from(block.tx_depth),
3310+ )
3311+ } else {
3312+ SUB
3313+ };
3314+ let split = tx < SUB;
31153315 if block.skip {
31163316 let zero_grids = [
31173317 vec![0i32; TX16 * TX16],
@@ -3125,6 +3325,11 @@ fn write_inter_frame_leaf(
31253325 level_grid(&block.u, TX8)?,
31263326 level_grid(&block.v, TX8)?,
31273327 ];
3328+ if split {
3329+ write_luma_tus(
3330+ enc, cdfs, neighbours, at_mi, SUB, tx, &grids[0], mode_for_tx, all_scans,
3331+ )?;
3332+ }
31283333 write_block_planes(
31293334 enc,
31303335 cdfs,
@@ -3137,9 +3342,9 @@ fn write_inter_frame_leaf(
31373342 &[scan16, scan8, scan8],
31383343 &neighbours.around(at, SUB),
31393344 mode_for_tx,
3140- false,
3345+ split,
31413346 );
3142- neighbours.record(at, SUB, mode_for_tx, &grids);
3347+ neighbours.record_planes(at, SUB, mode_for_tx, &grids, !split);
31433348 }
31443349 neighbours.record_inter(at, SUB, block.skip, is_inter);
31453350 Ok(())
@@ -3174,6 +3379,8 @@ fn write_inter_frame_leaf8(
31743379 leaf_mi: (usize, usize),
31753380 scan8: &Vec<u16>,
31763381 scan4: &Vec<u16>,
3382+ tx_select: bool,
3383+ all_scans: &[Vec<u16>; 4],
31773384 ) -> Result<(bool, bool)> {
31783385 if block.inter.is_none() && usize::from(block.mode) >= INTRA_MODES {
31793386 return Err(Error::unsupported(
@@ -3314,6 +3521,21 @@ fn write_inter_frame_leaf8(
33143521 } else {
33153522 [TxbSet::Luma8, TxbSet::Chroma4, TxbSet::Chroma4]
33163523 };
3524+ let tx = if tx_select {
3525+ write_tx_syntax_inter(
3526+ enc,
3527+ cdfs,
3528+ neighbours,
3529+ leaf_mi,
3530+ 8,
3531+ is_inter,
3532+ block.skip,
3533+ usize::from(block.tx_depth),
3534+ )
3535+ } else {
3536+ 8
3537+ };
3538+ let split = tx < 8;
33173539 if block.skip {
33183540 let zero_grids = [
33193541 vec![0i32; TX8 * TX8],
@@ -3328,6 +3550,11 @@ fn write_inter_frame_leaf8(
33283550 level_grid(&block.u, TX4)?,
33293551 level_grid(&block.v, TX4)?,
33303552 ];
3553+ if split {
3554+ write_luma_tus(
3555+ enc, cdfs, neighbours, leaf_mi, 8, tx, &grids[0], mode_for_tx, all_scans,
3556+ )?;
3557+ }
33313558 write_block_planes(
33323559 enc,
33333560 cdfs,
@@ -3336,9 +3563,9 @@ fn write_inter_frame_leaf8(
33363563 &[scan8, scan4, scan4],
33373564 &neighbours.around_mi(leaf_mi, 8),
33383565 mode_for_tx,
3339- false,
3566+ split,
33403567 );
3341- neighbours.record_mi(leaf_mi, 8, &grids);
3568+ neighbours.record_mi_planes(leaf_mi, 8, &grids, !split);
33423569 neighbours.record_inter_mi(leaf_mi, 8, block.skip, is_inter);
33433570 }
33443571 Ok((block.skip, is_inter))