diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 68e28d34..1997ba45 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -1768,6 +1768,19 @@ struct Neighbours { /// one was coded at 8x8. above_side_mi: Vec, left_side_mi: Vec, + /// The luma `mode` of the last `BLOCK_4X4` leaf decoded at each mi column + /// / row, with the mi position it was written at + /// (`(usize::MAX, _)` = never written). `above_mode`/`left_mode` are on + /// the coarse 16x16 grid, which cannot distinguish the two 4x4 leaves an + /// 8x8 `PARTITION_SPLIT` puts in one mi column; libaom's intra-mode + /// context reads `mi(row, col-1)`/`mi(row-1, col)` exactly, so a sub-8x8 + /// leaf whose neighbour is another split group needs that group's leaf 3 + /// (bottom-right), not the leaf the coarse slot happens to hold. Written + /// and read only by [`decode_leaf_split4`]; the position guard makes it + /// fall back to the coarse slot whenever the real neighbour was a block + /// of 8x8 or larger (whose one mode is correct for every row it spans). + sub8_mode_col: Vec<(usize, usize)>, + sub8_mode_row: Vec<(usize, usize)>, /// Whether the block above/left of this column/row was coded `skip` -- /// an inter frame's own `skip` context (spec `SkipContext`), which the /// key-frame writer never tracks (its skip context is always zero); a @@ -1895,6 +1908,8 @@ impl Neighbours { left_side: vec![SB; rows], above_side_mi: vec![SB; cols * (SUB / MI)], left_side_mi: vec![SB; rows * (SUB / MI)], + sub8_mode_col: vec![(usize::MAX, 0); cols * (SUB / MI)], + sub8_mode_row: vec![(usize::MAX, 0); rows * (SUB / MI)], above_skip: vec![false; cols], left_skip: vec![false; rows], above_skip_mode: vec![false; cols], @@ -2456,6 +2471,46 @@ impl Neighbours { /// [`Self::partition_ctx`]) are the neighbour's own width/height per the /// spec's per-edge partition-context rule, so a true rectangular block /// naturally wants two different values here rather than one. + /// Writes this block's luma `mode` into the mi-granular + /// [`Self::sub8_mode_col`]/[`Self::sub8_mode_row`] maps, keyed by the mi + /// position a *later* neighbour would read it from: the block's LAST mi + /// row for every column it spans (what the block below reads via + /// `mi(row-1, col)`) and its LAST mi column for every row (what the + /// block to the right reads via `mi(row, col-1)`). Only sub-8x8 leaves + /// read these back, and only when the recorded position is exactly their + /// own neighbour -- see [`Self::sub8_mode_col`]'s doc. + fn record_mode_mi(&mut self, mi_r: usize, mi_c: usize, mi_w: usize, mi_h: usize, mode: usize) { + let (last_r, last_c) = (mi_r + mi_h - 1, mi_c + mi_w - 1); + for cell in 0..mi_w { + if let Some(slot) = self.sub8_mode_col.get_mut(mi_c + cell) { + *slot = (last_r, mode); + } + } + for cell in 0..mi_h { + if let Some(slot) = self.sub8_mode_row.get_mut(mi_r + cell) { + *slot = (last_c, mode); + } + } + } + + /// The mi-exact above/left neighbour luma mode for a block whose top-left + /// mi is `(mi_r, mi_c)`, or `None` when no block has recorded that exact + /// mi position (then the coarse [`SUB`]-grid slot is right by + /// construction). See [`Self::record_mode_mi`]. + fn mode_above_mi(&self, mi_r: usize, mi_c: usize) -> Option { + match self.sub8_mode_col.get(mi_c) { + Some(&(row, m)) if mi_r > 0 && row == mi_r - 1 => Some(m), + _ => None, + } + } + + fn mode_left_mi(&self, mi_r: usize, mi_c: usize) -> Option { + match self.sub8_mode_row.get(mi_r) { + Some(&(col, m)) if mi_c > 0 && col == mi_c - 1 => Some(m), + _ => None, + } + } + fn record_rect( &mut self, at: (usize, usize), @@ -2476,6 +2531,7 @@ impl Neighbours { self.left_uv_mode[r + cell] = uv_mode; self.left_side[r + cell] = h; } + self.record_mode_mi(r * (SUB / MI), c * (SUB / MI), w / MI, h / MI, mode); self.record_mi_rect((r * (SUB / MI), c * (SUB / MI)), w, h, grids); } @@ -2586,6 +2642,7 @@ impl Neighbours { } let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); let side_mi = side / MI; + self.record_mode_mi(mi_r, mi_c, side_mi, side_mi, mode); for cell in 0..side_mi { self.left_side_mi[mi_r + cell] = side; self.above_side_mi[mi_c + cell] = side; @@ -5164,6 +5221,16 @@ fn decode_leaf8( left_mode = pmode; } } + // The mi-exact neighbour wins over both the coarse 16x16 slot and + // `prev_leaf`: one 16x16 slot cannot hold the two different modes its + // two 8x8 (or four 4x4) columns leave behind, so a leaf whose above/left + // neighbour sits in a split cell read the wrong `kf_y_mode` row. + if let Some(m) = neighbours.mode_above_mi(leaf_mi.0, leaf_mi.1) { + above_mode = m; + } + if let Some(m) = neighbours.mode_left_mi(leaf_mi.0, leaf_mi.1) { + left_mode = m; + } // See [`PlaneBuf::reconstruct`]'s doc: chroma stays `false` (exact) until // a smooth/paeth `uv_mode` can reach decode at all. let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode); @@ -5451,11 +5518,13 @@ fn decode_leaf8( } neighbours.fill_skip_grid(leaf_mi, 2, skip); neighbours.fill_lf_grid(leaf_mi, 2, 4, 0); + neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode); return Ok(mode); } neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]); neighbours.fill_skip_grid(leaf_mi, 2, skip); neighbours.fill_lf_grid(leaf_mi, 2, 8, 0); + neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode); Ok(mode) } @@ -5594,8 +5663,20 @@ fn decode_leaf_split4( for i in 0..4usize { let (dr, dc) = (i / 2, i % 2); let lmi = (leaf_mi.0 + dr, leaf_mi.1 + dc); - let leaf_above = if dr == 0 { above_mode } else { leaf_modes[i - 2] }; - let leaf_left = if dc == 0 { left_mode } else { leaf_modes[i - 1] }; + // Neighbour modes at real mi granularity: inside the group the + // sibling leaf, outside it the split-leaf map when the adjacent mi + // was itself a 4x4 leaf, else the coarse 16x16 slot (see + // `sub8_mode_col`'s doc). + let leaf_above = if dr == 0 { + neighbours.mode_above_mi(lmi.0, lmi.1).unwrap_or(above_mode) + } else { + leaf_modes[i - 2] + }; + let leaf_left = if dc == 0 { + neighbours.mode_left_mi(lmi.0, lmi.1).unwrap_or(left_mode) + } else { + leaf_modes[i - 1] + }; let has_chroma = i == 3; let skip_ctx = neighbours.skip_txfm_ctx(lmi.0, lmi.1); let (skip, mode, angle_delta_y, chroma, filter_intra) = read_intra_mode_sub8( @@ -5604,6 +5685,7 @@ fn decode_leaf_split4( )?; leaf_modes[i] = mode; leaf_skips[i] = skip; + neighbours.record_mode_mi(lmi.0, lmi.1, 1, 1, mode); let smooth_neighbor = is_smooth_mode(leaf_above) || is_smooth_mode(leaf_left); if smooth_neighbor { SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index b84fdf92..b61d88ba 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -1020,7 +1020,7 @@ pub(crate) struct Reach { /// it. Indexed by the block's position in the superblock, in blocks of its own /// size, as `row * (128 / side) + col` (`Reach::table_stride`) -- libaom's /// 128, not this crate's 64 superblock -- bit by bit from the low end. -const HAS_TOP_RIGHT: [&[u8]; 3] = [ +const HAS_TOP_RIGHT: [&[u8]; 4] = [ &[255, 85, 119, 85, 127, 85, 119, 85], &[95, 87], // has_tr_8x8 (libaom av1/common/reconintra.c) -- table_stride(8) == 16 @@ -1029,11 +1029,28 @@ const HAS_TOP_RIGHT: [&[u8]; 3] = [ 255, 255, 85, 85, 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85, 255, 127, 85, 85, 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85, ], + + // has_tr_4x4 (libaom av1/common/reconintra.c, transcribed verbatim) -- + // table_stride(4) == 32 (128 / 4), so 32 bits per row, 32 rows, 128 + // bytes. Sub-8x8 leaves (lane-sub8) are the only blocks that reach here + // with side 4 outside a TX4 split; without their own row they clamped + // into the 8x8 table and read above-right samples the decoder has not + // written yet (all-zero pixels in the bottom-right triangle of a + // directional 4x4 leaf). + &[ + 255, 255, 255, 255, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85, 127, 127, 127, 127, + 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85, 255, 127, 255, 127, 85, 85, 85, 85, + 119, 119, 119, 119, 85, 85, 85, 85, 127, 127, 127, 127, 85, 85, 85, 85, 119, 119, 119, 119, + 85, 85, 85, 85, 255, 255, 255, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85, + 127, 127, 127, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85, 255, 127, 255, 127, + 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85, 127, 127, 127, 127, 85, 85, 85, 85, + 119, 119, 119, 119, 85, 85, 85, 85, + ], ]; /// `has_bl_16x16` / `has_bl_32x32`: the same, for the block below and to the /// left. -const HAS_BOTTOM_LEFT: [&[u8]; 3] = [ +const HAS_BOTTOM_LEFT: [&[u8]; 4] = [ &[84, 16, 84, 0, 84, 16, 84, 0], &[4, 4], // has_bl_8x8 (libaom av1/common/reconintra.c) @@ -1041,6 +1058,16 @@ const HAS_BOTTOM_LEFT: [&[u8]; 3] = [ 84, 85, 16, 17, 84, 85, 0, 1, 84, 85, 16, 17, 84, 85, 0, 0, 84, 85, 16, 17, 84, 85, 0, 1, 84, 85, 16, 17, 84, 85, 0, 0, ], + + // has_bl_4x4 (same source, same order). + &[ + 84, 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0, 1, 1, 1, 84, 85, 85, 85, 16, 17, 17, 17, + 84, 85, 85, 85, 0, 0, 1, 0, 84, 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0, 1, 1, 1, 84, + 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0, 0, 0, 0, 84, 85, 85, 85, 16, 17, 17, 17, 84, + 85, 85, 85, 0, 1, 1, 1, 84, 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0, 0, 1, 0, 84, 85, + 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0, 1, 1, 1, 84, 85, 85, 85, 16, 17, 17, 17, 84, 85, + 85, 85, 0, 0, 0, 0, + ], ]; /// `has_tr_32x16`/`has_tr_16x32` (libaom `reconintra.c`) -- lane-intradisp @@ -1134,17 +1161,9 @@ impl Reach { if col + 1 == per_side { return false; } - // corner-cut: `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` only carry dedicated - // rows for side 8/16/32/64 (libaom's own `has_tr_8x8`/`has_tr_16x16` - // families); a `TxMode::Select` TX4 transform unit reaches here with - // side 4, whose real table (`has_tr_4x4`, 128 bytes) was never - // transcribed. Clamping into the 16/32 table's own bit range keeps - // this in-bounds and correct at every position that table already - // covers periodically -- wrong only at the small remainder past its - // length, a reach over-/under-estimate (extra or missing above-right - // reference pixels), never a stream desync (Reach never gates a - // symbol read). Ceiling: transcribe `has_tr_4x4`/`has_bl_4x4` - // verbatim and give TX4 its own `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` row. + // lane-sub8 r4: side 4 now has its own `has_tr_4x4`/`has_bl_4x4` + // row (the corner-cut this comment used to describe); sides 16/32/64 + // share row 0 exactly as libaom's tables repeat. let table = HAS_TOP_RIGHT[Self::table(side)]; Self::bit( table, @@ -1164,7 +1183,7 @@ impl Reach { if row + 1 == per_side { return false; } - // corner-cut: see `top_right`'s note -- same clamp, same ceiling. + // See `top_right`'s note. let table = HAS_BOTTOM_LEFT[Self::table(side)]; Self::bit( table, @@ -1200,6 +1219,8 @@ impl Reach { 1 } else if side == 8 { 2 + } else if side == 4 { + 3 } else { 0 }