From 83656d86106585448b1debcf6b7305a841719491 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 05:42:23 +0300 Subject: [PATCH] feat(av1): 8x8-leaf CDF, mi-precise neighbour contexts, write_leaf8 (r6, unwired) Lands the groundwork for lane-av1-rect's 8x8-leaf write path: the PARTITION_W8 CDF (+ adapted state field), Neighbours::record_mi/around_mi (mi-precise coeff-context entry points, the SUB-granularity above_mode/ above_side arrays untouched), and write_leaf8 (mirrors write_block for one 8x8 leaf, reading its intra-mode context from the enclosing 16x16 slot). BlockCoeffs gained an `eight: Option>` carrier and the coded-mode-validation loop flat_maps it. The key-frame Quadrant::Split writer now wires write_leaf8 for a straddling 16x16 (one axis only, mod-32==8 targets): the gathered split bit + per-leaf partition_w8 symbol + write_leaf8, matching the existing 32x32/64x64 edge pattern. encode.rs's block-generation loop does NOT populate `.eight` yet: an attempt to wire it hit an index-out-of-bounds in code_square's edge-sample read (Plane::edges, encode.rs ~1027) for a leaf at the frame's own edge, and a "exactly one straddler" geometry assumption that also turned out wrong (a 32x32-aligned straddling 16x16 row/column band is generically two leaves, not one) -- both are next-round work, not this round's budget. The refusal at encode.rs's split loop is therefore unchanged; both_writers_refuse_the_ same_strictly_before_half_straddle_size is unchanged and still green. Suite: 134/0/9, unchanged from cf71e1e. --- crates/ec-av1/src/cdf.rs | 12 ++ crates/ec-av1/src/cdf_state.rs | 5 + crates/ec-av1/src/encode.rs | 15 ++- crates/ec-av1/src/tile.rs | 240 ++++++++++++++++++++++++++------- 4 files changed, 224 insertions(+), 48 deletions(-) diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index 7fb33929..b55ee31f 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -920,6 +920,18 @@ pub const COEFF_BR_CHROMA_8: [[u16; 5]; 21] = [ [7625, 13801, 19144, 32768, 0], ]; +/// `Default_Partition_Cdf[0..4]`, `PARTITION_W16` format (spec 5.11.4 codes an +/// 8x8 block's partition as a 4-symbol read that only ever terminates +/// `PARTITION_NONE` for this writer -- `PARTITION_SPLIT` is illegal at 8x8, so +/// row 4 of libaom's real (8-symbol) 8x8 table is dropped and only the shared +/// leading four symbols, common to every partition-symbol alphabet, are kept. +pub const PARTITION_W8: [[u16; 5]; 4] = [ + [19132, 25510, 30392, 32768, 0], + [13928, 19855, 28540, 32768, 0], + [12522, 23679, 28629, 32768, 0], + [9896, 18783, 25853, 32768, 0], +]; + /// `Default_Partition_Cdf[0..4]`: the partition symbol of a 16x16 block. pub const PARTITION_W16: [[u16; 11]; 4] = [ [ diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index a299ba68..3755b0a8 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -150,6 +150,10 @@ pub(crate) struct Cdfs { pub br_chroma_4: [[u16; 5]; 21], /// The partition symbol of a 16x16 block. pub partition_w16: [[u16; 11]; 4], + /// The partition symbol of an 8x8 block (lane-av1-rect): only + /// `PARTITION_NONE` is ever coded against it, but the alphabet still + /// adapts on every read, same as every other partition table. + pub partition_w8: [[u16; 5]; 4], /// The one-context all-zero flag of a 32x32 luma transform. pub txb_skip_luma_32: [[u16; 3]; 1], /// The same, for a 64x64 luma transform. @@ -463,6 +467,7 @@ impl Cdfs { cdf::COEFF_BR_CHROMA_4_Q3, ), partition_w16: cdf::PARTITION_W16, + partition_w8: cdf::PARTITION_W8, txb_skip_luma_32: [pick( q_ctx, cdf::TXB_SKIP_LUMA_32_Q0, diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 072a9c63..52798ae5 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -1250,6 +1250,7 @@ fn code_square_inter( v: coeffs(&v.levels, SUB / 2), mode: DC_PRED as u8, skip, + eight: None, inter: Some(InterInfo { mode: InterMode::NearestMv, mv, @@ -1671,6 +1672,15 @@ pub(crate) fn encode_key_frame_inner( ) }); if !whole_legal && !split_legal { + // lane-av1-rect r6: the 8x8-leaf path this branch could + // take (a straddling 16x16 coded as two 8x8 leaves, see + // `crate::tile::write_leaf8`) hits an index-out-of-bounds + // in `code_square`'s edge-sample read (`Plane::edges`, + // this file ~line 1027) the moment a leaf sits at the + // true frame's own edge -- not yet debugged (r6 ran out + // of budget on it) -- so this refuses exactly as before + // rather than ship a panic. See the r6 handoff for the + // ranked next step. return Err(Error::unsupported( "AV1 encode", format!( @@ -2312,6 +2322,7 @@ fn search_inter_block( mode: best.mode, skip: best.skip, inter: best.inter, + eight: None, } } @@ -2847,6 +2858,7 @@ mod tests { v: Vec::new(), mode: 0, skip: false, + eight: None, inter: Some(InterInfo { mode: InterMode::NearestMv, mv: (0, 0), @@ -3200,7 +3212,8 @@ mod tests { /// key frame and inter frame writers: 360 mod 32 == 8, so a 16x16 leaf's /// own half (`has_half` at the SUB level) itself straddles the true edge /// -- the genuine "needs an 8x8/rectangular transform" case neither - /// writer codes. Both must refuse it the same way. + /// writer codes (r6: the key writer's 8x8-leaf path exists but is not + /// wired live yet, see the r6 handoff). Both must refuse it the same way. #[test] fn both_writers_refuse_the_same_strictly_before_half_straddle_size() { let (width, height) = (640usize, 360usize); diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 454107a1..d3901dad 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -465,6 +465,13 @@ pub struct BlockCoeffs { /// block. Only [`sb_coeff_inter_frame_tile`] codes `is_inter`; the key /// frame writers never read this field. pub inter: Option, + /// The 8x8 leaves a straddling 16x16 block is split into (lane-av1-rect), + /// each with its own 8x8 luma transform and 4x4 chroma transforms, in + /// raster order among the leaves that are inside the true frame. `Some` + /// overrides this `BlockCoeffs`'s own `luma`/`u`/`v`/`mode`, which are left + /// at their defaults and unused, the same way a `Whole` [`Superblock`]'s + /// per-quadrant fields are unused. + pub eight: Option>, } /// One inter mode [`sb_coeff_inter_frame_tile`]'s blocks may take (spec @@ -509,8 +516,11 @@ const BLOCK: usize = 32; /// Side of the chroma transform beside it at 4:2:0, and of the luma transform /// of a 16x16 block. const TX16: usize = 16; -/// Side of the chroma transform of a 16x16 block at 4:2:0. +/// Side of the chroma transform of a 16x16 block at 4:2:0, and of the luma +/// transform of an 8x8 leaf under a straddling 16x16 (lane-av1-rect). const TX8: usize = 8; +/// Side of the chroma transform of an 8x8 leaf at 4:2:0 (lane-av1-rect). +const TX4: usize = 4; /// Side of a superblock, in samples, which is the size a block outside the /// tile reads as. const SB: usize = 64; @@ -680,14 +690,26 @@ impl Neighbours { /// side). fn record(&mut self, at: (usize, usize), side: usize, mode: usize, grids: &[Vec; 3]) { let (r, c) = at; - let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane])); for cell in 0..side / SUB { self.above_mode[c + cell] = mode; self.left_mode[r + cell] = mode; self.above_side[c + cell] = side; self.left_side[r + cell] = side; } - let (mi_r, mi_c, side_mi) = (r * (SUB / MI), c * (SUB / MI), side / MI); + self.record_mi((r * (SUB / MI), c * (SUB / MI)), side, grids); + } + + /// The coefficient-context half of [`Self::record`], taking the block's + /// position directly in 4x4 mode-info units rather than [`SUB`]-grid + /// (r, c): an 8x8 leaf under a straddling 16x16 (lane-av1-rect) sits at a + /// mi offset [`Self::record`]'s SUB-unit `at` cannot name, but the + /// `above`/`left` arrays it writes into are already sized to the full mi + /// grid, so no resizing is needed to write into them at this + /// finer-than-SUB granularity. + fn record_mi(&mut self, at_mi: (usize, usize), side: usize, grids: &[Vec; 3]) { + let (mi_r, mi_c) = at_mi; + let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane])); + let side_mi = side / MI; // libaom rounds the luma edge up to the plane's own 4x4 unit before // clamping a subsampled plane (`ROUND_POWER_OF_TWO(max_blocks_high, // subsampling_y)` in av1_write_intra_coeffs_mb, encodetxb.c:456-459): @@ -741,7 +763,13 @@ impl Neighbours { /// The gathered state of the blocks above and to the left of one block, /// per plane. fn around(&self, (r, c): (usize, usize), side: usize) -> [Around; 3] { - let (mi_r, mi_c, side_mi) = (r * (SUB / MI), c * (SUB / MI), side / MI); + self.around_mi((r * (SUB / MI), c * (SUB / MI)), side) + } + + /// [`Self::around`] taking the block's position directly in 4x4 mode-info + /// units, for the same reason [`Self::record_mi`] does. + fn around_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> [Around; 3] { + let side_mi = side / MI; std::array::from_fn(|plane| { let mut around = Around::default(); for cell in 0..side_mi { @@ -800,7 +828,12 @@ pub fn sb_coeff_key_frame_tile( match superblock { Superblock::Whole(block) => coded.push(block), Superblock::Split(quadrants) => { - coded.extend(quadrants.iter().flat_map(Quadrant::blocks)); + for block in quadrants.iter().flat_map(Quadrant::blocks) { + match &block.eight { + Some(leaves) => coded.extend(leaves.iter()), + None => coded.push(block), + } + } } } } @@ -820,7 +853,12 @@ pub fn sb_coeff_key_frame_tile( let sub_planes = [TxbSet::Luma16, TxbSet::Chroma8, TxbSet::Chroma8]; let split_planes = [TxbSet::Luma32, TxbSet::Chroma16, TxbSet::Chroma16]; let whole_planes = [TxbSet::Luma64, TxbSet::Chroma32, TxbSet::Chroma32]; - let scans = [default_scan(TX32), default_scan(TX16), default_scan(TX8)]; + let scans = [ + default_scan(TX32), + default_scan(TX16), + default_scan(TX8), + default_scan(TX4), + ]; // The blocks above and to the left, on the 16x16 grid. The left edge is // reset at every superblock row because a tile starts each row with no // left neighbour. @@ -1027,26 +1065,14 @@ pub fn sb_coeff_key_frame_tile( (false, false) => {} } for (block, (sr, sc)) in blocks.iter().zip(sub_positions) { - // A 16x16 leaf's own hasRows/hasCols: this - // writer has no smaller partition than - // 16x16 (no 8x8 or rectangular transform), - // so a leaf the true edge itself cuts - // through -- legal in the spec only via a - // `PARTITION_HORZ`/`VERT` this writer does - // not code -- is refused rather than - // silently miscoded. + // A 16x16 leaf's own hasRows/hasCols, + // recomputed at this leaf's own half + // (same three-way signaling as the 32x32 + // and 64x64 levels above). let (has_cols16, has_rows16) = ( has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols), has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows), ); - if !has_cols16 || !has_rows16 { - return Err(Error::unsupported( - "AV1 tile", - "a 16x16 block the true frame edge cuts through \ - needs a rectangular transform this writer does \ - not code yet", - )); - } let at = (sr, sc); let ctx = neighbours.partition_ctx(at, SUB); ec_rng_trace(|| { @@ -1058,32 +1084,111 @@ pub fn sb_coeff_key_frame_tile( enc.tell() ) }); - enc.symbol(PARTITION_NONE, &mut cdfs.partition_w16[ctx]); - let grids = [ - level_grid(&block.luma, TX16)?, - level_grid(&block.u, TX8)?, - level_grid(&block.v, TX8)?, - ]; - write_block( - &mut enc, - &mut cdfs, - &mut neighbours, - block, - at, - SUB, - &sub_planes, - &grids, - [&scans[1], &scans[2], &scans[2]], - true, - ); - ec_rng_trace(|| { - format!( - "EC_TOK mi_row={} mi_col={} tell={}", - at.0 * 4, - at.1 * 4, - enc.tell() + if has_cols16 && has_rows16 { + enc.symbol(PARTITION_NONE, &mut cdfs.partition_w16[ctx]); + let grids = [ + level_grid(&block.luma, TX16)?, + level_grid(&block.u, TX8)?, + level_grid(&block.v, TX8)?, + ]; + write_block( + &mut enc, + &mut cdfs, + &mut neighbours, + block, + at, + SUB, + &sub_planes, + &grids, + [&scans[1], &scans[2], &scans[2]], + true, + ); + ec_rng_trace(|| { + format!( + "EC_TOK mi_row={} mi_col={} tell={}", + at.0 * 4, + at.1 * 4, + enc.tell() + ) + }); + continue; + } + // The true edge falls inside this 16x16 + // leaf itself (mod-32==8 target sizes): + // one axis only, per r6's charter -- an + // 8x8 leaf never itself straddles, so the + // block splits cleanly along whichever + // axis is short and only the in-frame + // 8x8s are coded, each its own leaf. + if !has_cols16 && !has_rows16 { + return Err(Error::unsupported( + "AV1 tile", + "a 16x16 block whose true edge cuts through both \ + axes needs a rectangular transform this writer \ + does not code yet", + )); + } + if has_cols16 { + enc.symbol_fixed( + 1, + &gather(&cdfs.partition_w16[ctx], VERT_ALIKE), + ); + } else { + enc.symbol_fixed( + 1, + &gather(&cdfs.partition_w16[ctx], HORZ_ALIKE), + ); + } + let leaves = block.eight.as_ref().ok_or_else(|| { + Error::unsupported( + "AV1 tile", + "a 16x16 block the true frame edge cuts through \ + needs its `eight` leaves populated", ) - }); + })?; + let (mi_row0, mi_col0) = + (sr as u32 * SUB_MI, sc as u32 * SUB_MI); + let leaf_positions: Vec<(u32, u32)> = (0..4) + .map(|i| (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2)) + .filter(|&(mr, mc)| mr < mi_rows && mc < mi_cols) + .collect(); + if leaves.len() != leaf_positions.len() { + return Err(Error::unsupported( + "AV1 tile", + "a straddling 16x16 block needs one `eight` entry \ + per 8x8 leaf inside the true frame", + )); + } + for (leaf, (mr, mc)) in leaves.iter().zip(leaf_positions) { + let leaf_mi = (mr as usize, mc as usize); + // Per r6's charter, this must be + // recomputed per leaf; nothing this + // writer's [`Neighbours`] tracks at + // [`SUB`] granularity changes between + // the two leaves of one straddling + // 16x16, so both read the same + // enclosing-slot context. + let leaf_ctx = neighbours.partition_ctx(at, SUB); + enc.symbol( + PARTITION_NONE, + &mut cdfs.partition_w8[leaf_ctx], + ); + let grids = [ + level_grid(&leaf.luma, TX8)?, + level_grid(&leaf.u, TX4)?, + level_grid(&leaf.v, TX4)?, + ]; + write_leaf8( + &mut enc, + &mut cdfs, + &mut neighbours, + leaf, + at, + leaf_mi, + &grids, + [&scans[2], &scans[3], &scans[3]], + ); + } } } } @@ -1163,6 +1268,47 @@ fn write_block( neighbours.record(at, side, mode, grids); } +/// Writes one 8x8 leaf of a straddling 16x16 block (lane-av1-rect): its own +/// luma transform and 4x4 chroma transforms, coded exactly like +/// [`write_block`] but reading its intra-mode context from the *enclosing* +/// 16x16 slot -- `outer_at`, in [`SUB`]-grid units -- rather than from its own +/// finer position, since [`Neighbours`]'s `above_mode`/`left_mode` arrays stay +/// at [`SUB`] (16-sample) granularity. `leaf_mi` is this leaf's own position +/// in 4x4 mode-info units, which is what its coefficient context (finer than +/// [`SUB`]) is kept and read at. +#[allow(clippy::too_many_arguments)] +fn write_leaf8( + enc: &mut SymbolEncoder, + cdfs: &mut Cdfs, + neighbours: &mut Neighbours, + block: &BlockCoeffs, + outer_at: (usize, usize), + leaf_mi: (usize, usize), + grids: &[Vec; 3], + scans: [&Vec; 3], +) { + let (r, c) = outer_at; + let mode = write_intra_mode( + enc, + cdfs, + block, + neighbours.above_mode[c], + neighbours.left_mode[r], + false, + ); + let planes = [TxbSet::Luma8, TxbSet::Chroma4, TxbSet::Chroma4]; + write_block_planes( + enc, + cdfs, + &planes, + grids, + &scans, + &neighbours.around_mi(leaf_mi, 8), + mode, + ); + neighbours.record_mi(leaf_mi, 8, grids); +} + /// Writes the three transform blocks of one coded block, in the order a /// decoder reads them. #[allow(clippy::too_many_arguments)]