From b52bd176bb79806982546274e6e627649966b95f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 9 Sep 2026 17:18:47 +0300 Subject: [PATCH] feat(av1): the writer records a rectangular block's two neighbour bands, and codes a 128-root HORZ/VERT half The tile writer had no rect path at all: record/record_inter/record_planes/ record_txfm and the palette bands each took one side and stamped it into both the above and the left band. Each now has an (above = width, left = height) twin, the square call delegating to it, and write_inter_block_128_rect codes one 128x64 / 64x128 half at the root's own square CDF context, skip only. Quadrant::Rect128 carries the pair. The search side is unchanged, so the pins are byte-identical. --- crates/ec-av1/src/encode.rs | 110 ++++++---- crates/ec-av1/src/tile.rs | 425 +++++++++++++++++++++++++++++++++--- 2 files changed, 463 insertions(+), 72 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 53cc82b8..50220905 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -3079,6 +3079,17 @@ impl Plane<'_> { rows_of(self.source, self.width, x, y, side) } + /// lane-b128hv: [`Self::source_block`] over a rectangle, for the 128 + /// root's own HORZ/VERT motion search. + fn source_block_rect(&self, x: usize, y: usize, w: usize, h: usize) -> Vec { + let mut out = vec![0u8; w * h]; + for row in 0..h { + out[row * w..][..w] + .copy_from_slice(&self.source[(y + row) * self.width + x..][..w]); + } + out + } + /// The reconstructed samples of one square, so that a partition trial can /// be undone. /// The reconstructed samples of one square AND the coefficient contexts @@ -9549,7 +9560,7 @@ fn search_root_128( luma, chroma, (x128, y128), - SIDE, + (SIDE, SIDE), &root_search, false, reference, @@ -9662,12 +9673,14 @@ fn search_skip_64( luma: &mut Plane, chroma: &mut [Plane; 2], (x, y): (usize, usize), - // The root's side in samples: [`SUPERBLOCK`] at a 64 root, twice that at - // the 128 one [`search_root_128`] drives (lane-b128). Every trial below - // is a square of this side and its half in chroma, and the residual arm - // is only ever asked for at 64 -- a 128 block's residual is four + // The root's footprint in samples: [`SUPERBLOCK`] square at a 64 root, + // twice that at the 128 one [`search_root_128`] drives (lane-b128), and + // 128x64 / 64x128 at the rect halves of [`search_root_128_rect`] + // (lane-b128hv). Every trial below tiles it into squares of its SHORTER + // side (one square when it is square, two otherwise), and the residual + // arm is only ever asked for at 64 -- a 128 block's residual is four // TX_64X64 units the writer has no path for. - side: usize, + (bw, bh): (usize, usize), search: &Search, // Whether the non-skip arm is priced at all ([`b64_residual`], and off on // screen content -- see that function's doc). @@ -9680,7 +9693,23 @@ fn search_skip_64( compound: &[(i8, &Picture, crate::mvstack::CompoundMvStack)], fctx: &crate::decode::FrameCtx, ) -> Option<(f64, BlockCoeffs)> { - debug_assert!(side == SUPERBLOCK || !residual, "a 128 root has no residual arm"); + debug_assert!( + (bw, bh) == (SUPERBLOCK, SUPERBLOCK) || !residual, + "only a 64 root has a residual arm" + ); + // The square units every trial below is taken on: the block itself when + // it is square, its two halves otherwise. Luma first (in raster order), + // then each chroma plane over the same tiling at half the side -- which is + // exactly `[(0, x, y, side), (1, ..), (2, ..)]` at bw == bh. + let sq = bw.min(bh); + let units: Vec<(usize, usize, usize, usize, bool)> = (0..3usize) + .flat_map(|plane| { + let (ox, oy, s) = if plane == 0 { (x, y, sq) } else { (x / 2, y / 2, sq / 2) }; + (0..bh / sq).flat_map(move |j| { + (0..bw / sq).map(move |i| (plane, ox + i * s, oy + j * s, s, plane == 0)) + }) + }) + .collect(); let not_new = symbol_bits(&cdf::NEW_MV[stack.new_mv_ctx], 1); let not_zero = symbol_bits(&cdf::ZERO_MV[stack.zero_mv_ctx], 1); let ref_bits = single_ref_bits(crate::mvstack::LAST_FRAME, stack); @@ -9718,7 +9747,7 @@ fn search_skip_64( // candidate below to pair with the second reference's NEAREST half. let mut last_new_mv: Option<(i32, i32)> = None; if leaf_new_mv() { - let source_block = luma.source_block(x, y, side); + let source_block = luma.source_block_rect(x, y, bw, bh); let (seeds, seed_n) = mv_seeds(stack); let found = motion::search( &reference.y, @@ -9728,8 +9757,8 @@ fn search_skip_64( &source_block, x, y, - side, - side, + bw, + bh, stack.pred_mv, &seeds[..seed_n], search.lambda, @@ -9746,14 +9775,11 @@ fn search_skip_64( } } - let mut best: Option<(f64, f64, InterInfo, [Trial; 3])> = None; + let mut best: Option<(f64, f64, InterInfo, Vec)> = None; for (bits, info) in cands { - let trials = [ - (0usize, x, y, side, true), - (1, x / 2, y / 2, side / 2, false), - (2, x / 2, y / 2, side / 2, false), - ] - .map(|(plane, px, py, s, is_luma)| { + let trials: Vec = units + .iter() + .map(|&(plane, px, py, s, is_luma)| { let (samples, stride, w, h) = match plane { 0 => (&reference.y, reference.width, luma.true_width, luma.true_height), 1 => ( @@ -9784,11 +9810,10 @@ fn search_skip_64( search.deadzone, TxbSet::Luma32Inter, ) - }); - let cost = trials[0].sse - + trials[1].sse - + trials[2].sse - + search.lambda * (fixed + bits); + }) + .collect(); + let cost = + trials.iter().map(|t| t.sse).sum::() + search.lambda * (fixed + bits); if best.as_ref().is_none_or(|b| cost < b.0) { best = Some((cost, fixed + bits, info, trials)); } @@ -9847,12 +9872,9 @@ fn search_skip_64( ccands.sort_by(|a, b| a.0.cmp(&b.0).then(a.1.total_cmp(&b.1))); ccands.dedup_by_key(|c| c.0); for (mvs, bits, info) in ccands { - let trials = [ - (0usize, x, y, side, true), - (1, x / 2, y / 2, side / 2, false), - (2, x / 2, y / 2, side / 2, false), - ] - .map(|(plane, px, py, s, is_luma)| { + let trials: Vec = units + .iter() + .map(|&(plane, px, py, s, is_luma)| { let (r0, r1) = match plane { 0 => ( (reference.y.as_slice(), reference.width, luma.true_width, luma.true_height), @@ -9890,10 +9912,10 @@ fn search_skip_64( search.deadzone, TxbSet::Luma32Inter, ) - }); + }) + .collect(); let syntax = fixed - ref_bits + bits; - let cost = - trials[0].sse + trials[1].sse + trials[2].sse + search.lambda * syntax; + let cost = trials.iter().map(|t| t.sse).sum::() + search.lambda * syntax; if best.as_ref().is_none_or(|b| cost < b.0) { best = Some((cost, syntax, info, trials)); } @@ -9923,11 +9945,13 @@ fn search_skip_64( let mut luma_levels: Option<(Vec, u8, Vec)> = None; if residual { let sets = [TxbSet::Luma64, TxbSet::Chroma32, TxbSet::Chroma32]; + // Square by construction (the `debug_assert` above), so `units` is + // one entry per plane and `chosen.2[plane]` is that plane's trial. let residual_trials = [0usize, 1, 2].map(|plane| { let (target, px, py, s) = if plane == 0 { - (&*luma, x, y, side) + (&*luma, x, y, bw) } else { - (&chroma[plane - 1], x / 2, y / 2, side / 2) + (&chroma[plane - 1], x / 2, y / 2, bw / 2) }; target.code_from_prediction( px, @@ -9959,7 +9983,7 @@ fn search_skip_64( commit_inter_luma( luma, (x, y), - side, + bw, info.mv, (&reference.y, reference.width, luma.true_width, luma.true_height), search, @@ -9980,18 +10004,20 @@ fn search_skip_64( B64_VAR_TX_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); } luma_levels = Some((levels, depth, tx_types)); - chosen = (cost, false, residual_trials); + chosen = (cost, false, residual_trials.to_vec()); } } let (cost, skip, trials) = chosen; // `commit_inter_luma` already wrote the residual arm's luma (its four // split units are not one 64-sided trial); a skip winner overwrites it. - if skip || !luma_committed { - luma.commit(x, y, side, &trials[0]); + for (&(plane, px, py, s, _), trial) in units.iter().zip(&trials) { + match plane { + 0 if skip || !luma_committed => luma.commit(px, py, s, trial), + 0 => {} + _ => chroma[plane - 1].commit(px, py, s, trial), + } } - chroma[0].commit(x / 2, y / 2, side / 2, &trials[1]); - chroma[1].commit(x / 2, y / 2, side / 2, &trials[2]); Some(( cost, BlockCoeffs { @@ -10011,8 +10037,8 @@ fn search_skip_64( (false, Some((levels, _, _))) => coeffs(levels, BLOCK), (false, None) => coeffs(&trials[0].levels, BLOCK), }, - u: if skip { Vec::new() } else { coeffs(&trials[1].levels, side / 2) }, - v: if skip { Vec::new() } else { coeffs(&trials[2].levels, side / 2) }, + u: if skip { Vec::new() } else { coeffs(&trials[1].levels, bw / 2) }, + v: if skip { Vec::new() } else { coeffs(&trials[2].levels, bw / 2) }, ..BlockCoeffs::default() }, )) @@ -12462,7 +12488,7 @@ pub(crate) fn encode_inter_frame( &mut luma, &mut chroma, (x64, y64), - SUPERBLOCK, + (SUPERBLOCK, SUPERBLOCK), &sb_search, b64_residual() && !screen, reference, diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index a935fd4e..838c6e6d 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -464,6 +464,184 @@ fn write_inter_block_128( Ok(()) } +/// lane-b128hv: one 128x64 (HORZ) or 64x128 (VERT) inter block, the two +/// halves a `PARTITION_HORZ`/`PARTITION_VERT` 128 superblock root carries -- +/// the syntax `decode_inter_block` reads for `sb128_rect` (decode.rs ~35343): +/// the same chain [`write_inter_block_128`] writes, at the block's own +/// rectangular window. Every CDF/context decision is taken at the enclosing +/// square `side` of 128 (what the decoder's `inter_piece!` passes), and the +/// block's true footprint governs only its mv-stack window, its mi/grid stamp +/// and its neighbour bands (decode.rs' own `write_w`/`write_h` split). +/// +/// SKIP only: the decoder reads a rect half's residual through the mu-chunk +/// loop, but the 128 NONE residual arm it would share is itself parked +/// (`EC_AV1_B128RES`), so offering one here would buy the same open filter +/// mismatch for a candidate the RD takes 0-2 times a frame. +/// +/// # Errors +/// A block that is intra or that is not skipped. +#[allow(clippy::too_many_arguments)] +fn write_inter_block_128_rect( + enc: &mut SymbolEncoder, + cdfs: &mut Cdfs, + neighbours: &mut Neighbours, + grid: &mut MiGrid, + // The half's own top-left position in mode-info (4x4) units. + at_mi: (usize, usize), + // Its true footprint in samples: (128, 64) at HORZ, (64, 128) at VERT. + (w, h): (usize, usize), + block: &BlockCoeffs, + mi_cols: u32, + mi_rows: u32, + tx_select: bool, +) -> Result<()> { + let info = block.inter.ok_or_else(|| { + Error::unsupported("AV1 tile", "a 128-root rectangular half is coded inter") + })?; + if !block.skip { + return Err(Error::unsupported( + "AV1 tile", + "a 128-root rectangular half carries a residual", + )); + } + let (mi_r, mi_c) = at_mi; + let (w_mi, h_mi) = (w / MI, h / MI); + let has_above = neighbours.has_above(mi_r); + let has_left = neighbours.has_left(mi_c); + let skip_ctx = + usize::from(neighbours.above_skip[mi_c]) + usize::from(neighbours.left_skip[mi_r]); + enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]); + write_cdef_idx(enc, (mi_r, mi_c), block.skip); + // NOT a whole superblock: decode.rs' `whole_sb` is `write_w == write_h == + // sb_px`, so a rect half reads its `delta_q` group even when skipped. + write_delta_q(enc, cdfs, (mi_r, mi_c), false, block.skip); + let ii_ctx = intra_inter_ctx( + has_above, + has_left, + neighbours.above_inter[mi_c], + neighbours.left_inter[mi_r], + ); + enc.symbol(1, &mut cdfs.intra_inter[ii_ctx]); + write_comp_mode( + enc, + cdfs, + neighbours, + (mi_r, mi_c), + (has_above, has_left), + info.ref1.is_some(), + ); + let (mv, mv1, is_new_mv) = if info.ref1.is_some() { + write_compound_block( + enc, + cdfs, + neighbours, + grid, + (mi_r, mi_c), + (w_mi, h_mi), + (has_above, has_left), + info, + (mi_cols as usize, mi_rows as usize), + )? + } else { + write_single_ref( + enc, + cdfs, + info.ref_frame, + neighbours.above_ref[mi_c], + neighbours.above_ref1[mi_c], + neighbours.left_ref[mi_r], + neighbours.left_ref1[mi_r], + ); + let stack = find_mv_stack( + grid, + mi_r, + mi_c, + w_mi, + h_mi, + info.ref_frame, + mi_cols as usize, + mi_rows as usize, + ); + let (mv, is_new_mv) = write_inter_mode(enc, cdfs, info, &stack)?; + (mv, (0, 0), is_new_mv) + }; + for dr in 0..h_mi { + for dc in 0..w_mi { + grid.set( + mi_r + dr, + mi_c + dc, + MiInfo { + is_inter: true, + ref_frame: info.ref_frame, + ref_frame1: info.ref1.unwrap_or(NO_REF1), + mv1: mv16(mv1), + mv: mv16(mv), + is_new_mv, + size: w_mi as u8, + size_h: h_mi as u8, + is_global_mv0: false, + is_global_mv1: false, + }, + ); + } + } + if info.ref1.is_none() { + write_motion_mode( + enc, + cdfs, + grid, + (mi_r, mi_c), + (w_mi, h_mi), + (w, h), + (mi_cols as usize, mi_rows as usize), + info.ref_frame, + block.motion_mode, + )?; + } + if tx_select { + // `read_block_tx_size_rect`'s skip arm: `txfm_partition_update_rect` + // with the BLOCK's own size in both bands, no symbol. + neighbours.record_txfm_rect((mi_r, mi_c), (w, h), (w, h)); + } + let zero_grids = [ + vec![0i32; TX32 * TX32], + vec![0i32; TX32 * TX32], + vec![0i32; TX32 * TX32], + ]; + // A 128 root's half is always SUB (16 px) aligned, so the coarse mode / + // side bands are named by the same SUB-grid origin every square block + // uses. + let at_sub = (mi_r / (SUB / MI), mi_c / (SUB / MI)); + neighbours.record_planes_rect(at_sub, (w, h), 0, &zero_grids, true); + neighbours.record_inter_rect_mi((mi_r, mi_c), (w, h), block.skip, true, block_ref(block)); + if let Some(r1) = info.ref1 { + neighbours.record_compound_rect_mi((mi_r, mi_c), (w, h), r1, 0, 1); + } + if w > h { + SB128_HORZ_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + } else { + SB128_VERT_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + } + Ok(()) +} + +/// lane-b128hv: how many 128x64 / 64x128 halves the inter writer has coded +/// since the last [`take_sb128_horz_hits`] / [`take_sb128_vert_hits`] (class +/// `gate-blind-to-feature`). +static SB128_HORZ_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); +/// The VERT twin of [`SB128_HORZ_HITS`]. +static SB128_VERT_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + +/// The 128x64-half count since the last call, and zero it. +pub fn take_sb128_horz_hits() -> usize { + SB128_HORZ_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) +} + +/// The 64x128-half count since the last call, and zero it. +pub fn take_sb128_vert_hits() -> usize { + SB128_VERT_HITS.swap(0, std::sync::atomic::Ordering::Relaxed) +} + /// lane-b128r: how many of those blocks carried a REAL residual (four /// TX_64X64 luma units plus their per-chunk chroma pair) rather than being /// skipped, since the last [`take_sb128_residual_hits`]. @@ -1634,6 +1812,10 @@ fn ec_rng_trace(msg: impl FnOnce() -> String) { /// `PARTITION_NONE` (spec 6.10.4): the whole block, undivided. const PARTITION_NONE: usize = 0; +/// `PARTITION_HORZ` (spec 6.10.4): the block cut into a top and a bottom half. +const PARTITION_HORZ: usize = 1; +/// `PARTITION_VERT` (spec 6.10.4): the block cut into a left and a right half. +const PARTITION_VERT: usize = 2; /// `PARTITION_SPLIT` (spec 6.10.4): the block cut into four quadrants. const PARTITION_SPLIT: usize = 3; @@ -2365,6 +2547,15 @@ pub enum Quadrant { /// interleaved with its chroma per 64x64 mu chunk, which this writer has /// no path for; [`crate::tile::write_inter_block_128`] refuses one. Whole128(BlockCoeffs), + /// lane-b128hv: the two 128x64 (`true`) or 64x128 (`false`) inter blocks a + /// `PARTITION_HORZ` / `PARTITION_VERT` 128 superblock root carries, in the + /// order the decoder reads them (top then bottom, left then right). + /// Carried by the top-left 32x32 entry of the root's sixteen, exactly as + /// [`Whole128`](Quadrant::Whole128) is. + /// + /// SKIP only ([`crate::tile::write_inter_block_128_rect`] refuses a + /// residual), single reference or compound. + Rect128(bool, Vec), /// A quadrant a [`Whole64`](Quadrant::Whole64) at its superblock's /// top-left already coded; it carries no block of its own. Covered, @@ -2377,7 +2568,7 @@ impl Quadrant { Quadrant::Whole(block) | Quadrant::Whole64(block) | Quadrant::Whole128(block) => { std::slice::from_ref(block) } - Quadrant::Split(blocks) => blocks.as_slice(), + Quadrant::Split(blocks) | Quadrant::Rect128(_, blocks) => blocks.as_slice(), Quadrant::Covered => &[], } } @@ -2722,14 +2913,18 @@ impl Neighbours { /// [`Self::record_palette_y`] for the chroma bands (decode.rs /// `record_palette_uv_rect`) -- `size == 0` clears stale state the same /// way, and the span is in LUMA pixels for both planes' bands alike. - fn record_palette_uv(&mut self, (r, c): (usize, usize), side: usize, size: u8, colors: [u16; 8]) { - for cell in 0..(side / MI).max(1) { + fn record_palette_uv(&mut self, (r, c): (usize, usize), (w, h): (usize, usize), size: u8, colors: [u16; 8]) { + // lane-b128hv: the above band spans the block's WIDTH and the left + // band its HEIGHT; identical to the one loop this replaces at w == h. + for cell in 0..(w / MI).max(1) { if let Some(e) = self.above_palette_uv_size.get_mut(c + cell) { *e = size; if size > 0 { self.above_palette_uv_colors[c + cell] = colors; } } + } + for cell in 0..(h / MI).max(1) { if let Some(e) = self.left_palette_uv_size.get_mut(r + cell) { *e = size; if size > 0 { @@ -2742,14 +2937,18 @@ impl Neighbours { /// What a just-written block leaves in the palette bands (decode.rs /// `record_palette_y_rect`): its size and colours over every 4x4 unit it /// spans, `0` for a block that took no palette. - fn record_palette_y(&mut self, (r, c): (usize, usize), side: usize, size: u8, colors: [u16; 8]) { - for cell in 0..(side / MI).max(1) { + fn record_palette_y(&mut self, (r, c): (usize, usize), (w, h): (usize, usize), size: u8, colors: [u16; 8]) { + // lane-b128hv: the above band spans the block's WIDTH and the left + // band its HEIGHT; identical to the one loop this replaces at w == h. + for cell in 0..(w / MI).max(1) { if let Some(e) = self.above_palette_size.get_mut(c + cell) { *e = size; if size > 0 { self.above_palette_colors[c + cell] = colors; } } + } + for cell in 0..(h / MI).max(1) { if let Some(e) = self.left_palette_size.get_mut(r + cell) { *e = size; if size > 0 { @@ -2776,6 +2975,27 @@ impl Neighbours { } } + /// lane-b128hv: decode.rs `txfm_partition_update_rect` -- the two bands + /// take the transform's own HEIGHT (left) and WIDTH (above), over the + /// txb's own rect span. [`Self::record_txfm`] is this at `tx_w == tx_h`. + fn record_txfm_rect( + &mut self, + (mi_r, mi_c): (usize, usize), + (tx_w, tx_h): (usize, usize), + (txb_w, txb_h): (usize, usize), + ) { + for i in 0..txb_h / MI { + if let Some(cell) = self.left_txfm.get_mut(mi_r + i) { + *cell = tx_h as u8; + } + } + for i in 0..txb_w / MI { + if let Some(cell) = self.above_txfm.get_mut(mi_c + i) { + *cell = tx_w as u8; + } + } + } + /// `get_tx_size_context` as libaom writes it for a block inside an INTER /// frame (decode.rs `tx_size_context_txfm_rect`): the `TXFM_CONTEXT` /// bands, except that an *inter* neighbour contributes its own BLOCK size. @@ -2897,15 +3117,31 @@ impl Neighbours { mode: usize, grids: &[Vec; 3], luma: bool, + ) { + self.record_planes_rect(at, (side, side), mode, grids, luma); + } + + /// lane-b128hv: [`Self::record_planes`] with independent above (width) / + /// left (height) extents, decode.rs `record_rect_mi`'s own split -- the + /// 128 root's HORZ/VERT halves are the writer's first rect blocks. + fn record_planes_rect( + &mut self, + at: (usize, usize), + (w, h): (usize, usize), + mode: usize, + grids: &[Vec; 3], + luma: bool, ) { let (r, c) = at; - for cell in 0..side / SUB { + for cell in 0..w / SUB { self.above_mode[c + cell] = mode; + self.above_side[c + cell] = w; + } + for cell in 0..h / SUB { self.left_mode[r + cell] = mode; - self.above_side[c + cell] = side; - self.left_side[r + cell] = side; + self.left_side[r + cell] = h; } - self.record_mi_planes((r * (SUB / MI), c * (SUB / MI)), side, grids, luma); + self.record_mi_planes_rect((r * (SUB / MI), c * (SUB / MI)), (w, h), grids, luma); } /// The coefficient-context half of [`Self::record`], taking the block's @@ -2926,19 +3162,33 @@ impl Neighbours { side: usize, grids: &[Vec; 3], luma: bool, + ) { + self.record_mi_planes_rect(at_mi, (side, side), grids, luma); + } + + /// [`Self::record_mi_planes`] with independent above (width) / left + /// (height) extents (lane-b128hv, decode.rs `record_mi_rect`). + fn record_mi_planes_rect( + &mut self, + at_mi: (usize, usize), + (w, h): (usize, usize), + grids: &[Vec; 3], + luma: bool, ) { let (mi_r, mi_c) = at_mi; if screen_armed() { let (size, colors) = self.pending_palette.take().unwrap_or((0, [0u16; 8])); - self.record_palette_y(at_mi, side, size, colors); + self.record_palette_y(at_mi, (w, h), size, colors); let (uv_size, uv_colors) = self.pending_palette_uv.take().unwrap_or((0, [0u16; 8])); - self.record_palette_uv(at_mi, side, uv_size, uv_colors); + self.record_palette_uv(at_mi, (w, h), uv_size, uv_colors); } let states: [Neighbour; 3] = std::array::from_fn(|plane| neighbour_state(&grids[plane])); - let side_mi = side / MI; - for cell in 0..side_mi { - self.left_side_mi[mi_r + cell] = side; - self.above_side_mi[mi_c + cell] = side; + let (w_mi, h_mi) = (w / MI, h / MI); + for cell in 0..h_mi { + self.left_side_mi[mi_r + cell] = h; + } + for cell in 0..w_mi { + self.above_side_mi[mi_c + cell] = w; } // 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, @@ -2957,25 +3207,29 @@ impl Neighbours { round_up_even(self.mi_cols), round_up_even(self.mi_cols), ]; - for cell in 0..side_mi { + for cell in 0..h_mi { let keep_left = self.left[mi_r + cell][0]; - let keep_above = self.above[mi_c + cell][0]; self.left[mi_r + cell] = std::array::from_fn(|plane| { - if cell < side_mi.min(bound_h[plane].saturating_sub(mi_r)) { + if cell < h_mi.min(bound_h[plane].saturating_sub(mi_r)) { states[plane] } else { Default::default() } }); + if !luma { + self.left[mi_r + cell][0] = keep_left; + } + } + for cell in 0..w_mi { + let keep_above = self.above[mi_c + cell][0]; self.above[mi_c + cell] = std::array::from_fn(|plane| { - if cell < side_mi.min(bound_w[plane].saturating_sub(mi_c)) { + if cell < w_mi.min(bound_w[plane].saturating_sub(mi_c)) { states[plane] } else { Default::default() } }); if !luma { - self.left[mi_r + cell][0] = keep_left; self.above[mi_c + cell][0] = keep_above; } } @@ -2989,6 +3243,56 @@ impl Neighbours { self.record_inter_mi((r * (SUB / MI), c * (SUB / MI)), side, skip, is_inter, ref_frame); } + /// lane-b128hv: [`Self::record_inter_mi`] with independent above (width) + /// / left (height) spans, decode.rs `record_inter_rect_mi`'s own split. + fn record_inter_rect_mi( + &mut self, + (mi_r, mi_c): (usize, usize), + (w, h): (usize, usize), + skip: bool, + is_inter: bool, + ref_frame: i8, + ) { + let altref = is_inter && ref_frame == crate::mvstack::ALTREF_FRAME; + for cell in 0..h / MI { + self.left_skip[mi_r + cell] = skip; + self.left_inter[mi_r + cell] = is_inter; + self.left_ref[mi_r + cell] = ref_frame; + self.left_ref1[mi_r + cell] = None; + self.left_comp_group_idx[mi_r + cell] = if altref { 3 } else { 0 }; + self.left_compound_idx[mi_r + cell] = u8::from(altref); + } + for cell in 0..w / MI { + self.above_skip[mi_c + cell] = skip; + self.above_inter[mi_c + cell] = is_inter; + self.above_ref[mi_c + cell] = ref_frame; + self.above_ref1[mi_c + cell] = None; + self.above_comp_group_idx[mi_c + cell] = if altref { 3 } else { 0 }; + self.above_compound_idx[mi_c + cell] = u8::from(altref); + } + } + + /// [`Self::record_compound_mi`] over a rect block's own two spans. + fn record_compound_rect_mi( + &mut self, + (mi_r, mi_c): (usize, usize), + (w, h): (usize, usize), + ref1: i8, + comp_group_idx: u8, + compound_idx: u8, + ) { + for cell in 0..h / MI { + self.left_ref1[mi_r + cell] = Some(ref1); + self.left_comp_group_idx[mi_r + cell] = comp_group_idx; + self.left_compound_idx[mi_r + cell] = compound_idx; + } + for cell in 0..w / MI { + self.above_ref1[mi_c + cell] = Some(ref1); + self.above_comp_group_idx[mi_c + cell] = comp_group_idx; + self.above_compound_idx[mi_c + cell] = compound_idx; + } + } + /// [`Self::record_inter_mi`]'s compound half (decode.rs /// `Neighbours::record_compound_ctx_rect_mi`): overwrites the ALTREF /// single-reference default with a real compound block's own second @@ -3397,7 +3701,10 @@ pub(crate) fn sb_coeff_key_frame_tile_cdfs( match quadrant { // A 64x64 root is an INTER-frame partition; a // key frame codes one as `Superblock::Whole`. - Quadrant::Whole64(_) | Quadrant::Whole128(_) | Quadrant::Covered => { + Quadrant::Whole64(_) + | Quadrant::Whole128(_) + | Quadrant::Rect128(..) + | Quadrant::Covered => { return Err(Error::unsupported( "AV1 tile", "a key frame codes a 64x64 block as `Superblock::Whole`", @@ -5065,7 +5372,7 @@ pub(crate) fn predicted_coeff_bits(blocks: &[Quadrant], base_q_idx: u8) -> f64 { // lane-b128: a 128x128 root is SKIP by construction (the // writer refuses a residual at that size), so it names no // coefficients on any plane. - Quadrant::Whole128(_) => return 0.0, + Quadrant::Whole128(_) | Quadrant::Rect128(..) => return 0.0, Quadrant::Whole(_) => 32, Quadrant::Split(_) => 16, // lane-tx64: a 64x64 root that codes a residual pays for one @@ -5687,6 +5994,22 @@ pub(crate) fn partition_bits(side: usize, split: bool) -> f64 { crate::encode::symbol_bits(cdf, symbol) } +/// lane-b128hv: [`partition_bits`] for any partition symbol, at the same +/// fixed (round-2) tables -- what the 128 root's HORZ/VERT candidate is priced +/// with (`PARTITION_HORZ` = 1, `PARTITION_VERT` = 2). +pub(crate) fn partition_bits_sym(side: usize, symbol: usize) -> f64 { + static CDFS: LazyLock = LazyLock::new(|| Cdfs::new(2)); + let cdfs = &*CDFS; + let cdf: &[u16] = match side { + 128 => &cdfs.partition_w128[0], + SB => &cdfs.partition_w64[0], + BLOCK => &cdfs.partition_w32[0], + TX8 => &cdfs.partition_w8[0], + _ => &cdfs.partition_w16[0], + }; + crate::encode::symbol_bits(cdf, symbol) +} + /// coeffs() for one plane's transform block of any coefficients the base and /// base-range syntax reach (spec 5.11.39). /// @@ -6769,12 +7092,17 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( if sb128 && sb_start { // The 128x128 block a `PARTITION_NONE` root carries, held by the // top-left 32x32 entry of the root's sixteen. - let root_block = ((sb_r & !1) * 2 < rows && (sb_c & !1) * 2 < cols) - .then(|| &blocks[(((sb_r & !1) * 2) * cols + (sb_c & !1) * 2) as usize]) - .and_then(|q| match q { - Quadrant::Whole128(block) => Some(block), - _ => None, - }); + let root_entry = ((sb_r & !1) * 2 < rows && (sb_c & !1) * 2 < cols) + .then(|| &blocks[(((sb_r & !1) * 2) * cols + (sb_c & !1) * 2) as usize]); + let root_block = root_entry.and_then(|q| match q { + Quadrant::Whole128(block) => Some(block), + _ => None, + }); + // lane-b128hv: the two rectangular halves a HORZ/VERT root carries. + let root_rect = root_entry.and_then(|q| match q { + Quadrant::Rect128(horz, halves) => Some((*horz, halves)), + _ => None, + }); write_sb128_root( &mut enc, &mut cdfs, @@ -6783,7 +7111,12 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( sb_c, mi_cols, mi_rows, - if root_block.is_some() { PARTITION_NONE } else { PARTITION_SPLIT }, + match (root_block.is_some(), root_rect) { + (true, _) => PARTITION_NONE, + (_, Some((true, _))) => PARTITION_HORZ, + (_, Some((false, _))) => PARTITION_VERT, + _ => PARTITION_SPLIT, + }, ); if let Some(block) = root_block { let at128 = ((sb_r & !1) as usize * 4, (sb_c & !1) as usize * 4); @@ -6801,6 +7134,35 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( sb128_none = true; continue; } + if let Some((horz, halves)) = root_rect { + // libaom `decode_partition` at BLOCK_128X128: `subsize` at + // (mi_row, mi_col) and a second one at + hbs (16 mi) along the + // cut axis, the second only when it is inside the frame -- the + // search only offers a root wholly inside, so both are. + let base_mi = ((sb_r & !1) as usize * SB_MI as usize, (sb_c & !1) as usize * SB_MI as usize); + let (w, h) = if horz { (128usize, 64usize) } else { (64, 128) }; + for (i, half) in halves.iter().enumerate() { + let at_mi = if horz { + (base_mi.0 + i * 16, base_mi.1) + } else { + (base_mi.0, base_mi.1 + i * 16) + }; + write_inter_block_128_rect( + &mut enc, + &mut cdfs, + &mut neighbours, + &mut grid, + at_mi, + (w, h), + half, + mi_cols, + mi_rows, + tx_select, + )?; + } + sb128_none = true; + continue; + } } { if !sb128 { @@ -7046,7 +7408,10 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( let block = match site { // Already coded by the `Whole64` above -- but only inside // a superblock that really carried one. - Quadrant::Whole64(_) | Quadrant::Whole128(_) | Quadrant::Covered => { + Quadrant::Whole64(_) + | Quadrant::Whole128(_) + | Quadrant::Rect128(..) + | Quadrant::Covered => { return Err(Error::unsupported( "AV1 tile", "a `Whole64`/`Covered` quadrant needs its superblock's \