forge
Explore
Log in

Don't you dare go Hollow

Mi-exact intra mode neighbours + has_tr_4x4/has_bl_4x4

Browse files at this commit

c24ba5daf8c4fb02cad8112c9655a741f94cb3a7Ahmet Kaan GümüşParent: 00f1f0d
  • fix
    • mi-exact intra mode neighbours + has_tr_4x4/has_bl_4x4
Show full message
fix(av1): mi-exact intra mode neighbours + has_tr_4x4/has_bl_4x4

The coarse 16x16 above_mode/left_mode slots cannot hold the two (or four)
different modes a split cell leaves behind, so every 8x8/4x4 leaf whose
neighbour sat in a split cell read the wrong kf_y_mode CDF row and desynced
the tile -- which surfaced as hallucinated HORZ/VERT/AB partition symbols
(the reference stream contains only PARTITION_NONE/SPLIT). Neighbours now
keeps a mi-granular mode map (sub8_mode_col/row, written by record_rect,
record_split_luma, decode_leaf8 and every 4x4 leaf) that wins over the
coarse slot when it holds the exact neighbouring mi. Reach gains libaom's
has_tr_4x4/has_bl_4x4 rows (the corner-cut the old comment named): sub-8x8
leaves were clamping into the 8x8 table and predicting from above-right
samples the decoder has not written yet.
crates/ec-av1/src/decode.rsmodified+84 -2
@@ -1768,6 +1768,19 @@ struct Neighbours {
17681768 /// one was coded at 8x8.
17691769 above_side_mi: Vec<usize>,
17701770 left_side_mi: Vec<usize>,
1771+ /// The luma `mode` of the last `BLOCK_4X4` leaf decoded at each mi column
1772+ /// / row, with the mi position it was written at
1773+ /// (`(usize::MAX, _)` = never written). `above_mode`/`left_mode` are on
1774+ /// the coarse 16x16 grid, which cannot distinguish the two 4x4 leaves an
1775+ /// 8x8 `PARTITION_SPLIT` puts in one mi column; libaom's intra-mode
1776+ /// context reads `mi(row, col-1)`/`mi(row-1, col)` exactly, so a sub-8x8
1777+ /// leaf whose neighbour is another split group needs that group's leaf 3
1778+ /// (bottom-right), not the leaf the coarse slot happens to hold. Written
1779+ /// and read only by [`decode_leaf_split4`]; the position guard makes it
1780+ /// fall back to the coarse slot whenever the real neighbour was a block
1781+ /// of 8x8 or larger (whose one mode is correct for every row it spans).
1782+ sub8_mode_col: Vec<(usize, usize)>,
1783+ sub8_mode_row: Vec<(usize, usize)>,
17711784 /// Whether the block above/left of this column/row was coded `skip` --
17721785 /// an inter frame's own `skip` context (spec `SkipContext`), which the
17731786 /// key-frame writer never tracks (its skip context is always zero); a
@@ -1895,6 +1908,8 @@ impl Neighbours {
18951908 left_side: vec![SB; rows],
18961909 above_side_mi: vec![SB; cols * (SUB / MI)],
18971910 left_side_mi: vec![SB; rows * (SUB / MI)],
1911+ sub8_mode_col: vec![(usize::MAX, 0); cols * (SUB / MI)],
1912+ sub8_mode_row: vec![(usize::MAX, 0); rows * (SUB / MI)],
18981913 above_skip: vec![false; cols],
18991914 left_skip: vec![false; rows],
19001915 above_skip_mode: vec![false; cols],
@@ -2456,6 +2471,46 @@ impl Neighbours {
24562471 /// [`Self::partition_ctx`]) are the neighbour's own width/height per the
24572472 /// spec's per-edge partition-context rule, so a true rectangular block
24582473 /// naturally wants two different values here rather than one.
2474+ /// Writes this block's luma `mode` into the mi-granular
2475+ /// [`Self::sub8_mode_col`]/[`Self::sub8_mode_row`] maps, keyed by the mi
2476+ /// position a *later* neighbour would read it from: the block's LAST mi
2477+ /// row for every column it spans (what the block below reads via
2478+ /// `mi(row-1, col)`) and its LAST mi column for every row (what the
2479+ /// block to the right reads via `mi(row, col-1)`). Only sub-8x8 leaves
2480+ /// read these back, and only when the recorded position is exactly their
2481+ /// own neighbour -- see [`Self::sub8_mode_col`]'s doc.
2482+ fn record_mode_mi(&mut self, mi_r: usize, mi_c: usize, mi_w: usize, mi_h: usize, mode: usize) {
2483+ let (last_r, last_c) = (mi_r + mi_h - 1, mi_c + mi_w - 1);
2484+ for cell in 0..mi_w {
2485+ if let Some(slot) = self.sub8_mode_col.get_mut(mi_c + cell) {
2486+ *slot = (last_r, mode);
2487+ }
2488+ }
2489+ for cell in 0..mi_h {
2490+ if let Some(slot) = self.sub8_mode_row.get_mut(mi_r + cell) {
2491+ *slot = (last_c, mode);
2492+ }
2493+ }
2494+ }
2495+
2496+ /// The mi-exact above/left neighbour luma mode for a block whose top-left
2497+ /// mi is `(mi_r, mi_c)`, or `None` when no block has recorded that exact
2498+ /// mi position (then the coarse [`SUB`]-grid slot is right by
2499+ /// construction). See [`Self::record_mode_mi`].
2500+ fn mode_above_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> {
2501+ match self.sub8_mode_col.get(mi_c) {
2502+ Some(&(row, m)) if mi_r > 0 && row == mi_r - 1 => Some(m),
2503+ _ => None,
2504+ }
2505+ }
2506+
2507+ fn mode_left_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> {
2508+ match self.sub8_mode_row.get(mi_r) {
2509+ Some(&(col, m)) if mi_c > 0 && col == mi_c - 1 => Some(m),
2510+ _ => None,
2511+ }
2512+ }
2513+
24592514 fn record_rect(
24602515 &mut self,
24612516 at: (usize, usize),
@@ -2476,6 +2531,7 @@ impl Neighbours {
24762531 self.left_uv_mode[r + cell] = uv_mode;
24772532 self.left_side[r + cell] = h;
24782533 }
2534+ self.record_mode_mi(r * (SUB / MI), c * (SUB / MI), w / MI, h / MI, mode);
24792535 self.record_mi_rect((r * (SUB / MI), c * (SUB / MI)), w, h, grids);
24802536 }
24812537
@@ -2586,6 +2642,7 @@ impl Neighbours {
25862642 }
25872643 let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI));
25882644 let side_mi = side / MI;
2645+ self.record_mode_mi(mi_r, mi_c, side_mi, side_mi, mode);
25892646 for cell in 0..side_mi {
25902647 self.left_side_mi[mi_r + cell] = side;
25912648 self.above_side_mi[mi_c + cell] = side;
@@ -5164,6 +5221,16 @@ fn decode_leaf8(
51645221 left_mode = pmode;
51655222 }
51665223 }
5224+ // The mi-exact neighbour wins over both the coarse 16x16 slot and
5225+ // `prev_leaf`: one 16x16 slot cannot hold the two different modes its
5226+ // two 8x8 (or four 4x4) columns leave behind, so a leaf whose above/left
5227+ // neighbour sits in a split cell read the wrong `kf_y_mode` row.
5228+ if let Some(m) = neighbours.mode_above_mi(leaf_mi.0, leaf_mi.1) {
5229+ above_mode = m;
5230+ }
5231+ if let Some(m) = neighbours.mode_left_mi(leaf_mi.0, leaf_mi.1) {
5232+ left_mode = m;
5233+ }
51675234 // See [`PlaneBuf::reconstruct`]'s doc: chroma stays `false` (exact) until
51685235 // a smooth/paeth `uv_mode` can reach decode at all.
51695236 let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode);
@@ -5451,11 +5518,13 @@ fn decode_leaf8(
54515518 }
54525519 neighbours.fill_skip_grid(leaf_mi, 2, skip);
54535520 neighbours.fill_lf_grid(leaf_mi, 2, 4, 0);
5521+ neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode);
54545522 return Ok(mode);
54555523 }
54565524 neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]);
54575525 neighbours.fill_skip_grid(leaf_mi, 2, skip);
54585526 neighbours.fill_lf_grid(leaf_mi, 2, 8, 0);
5527+ neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode);
54595528 Ok(mode)
54605529 }
54615530
@@ -5594,8 +5663,20 @@ fn decode_leaf_split4(
55945663 for i in 0..4usize {
55955664 let (dr, dc) = (i / 2, i % 2);
55965665 let lmi = (leaf_mi.0 + dr, leaf_mi.1 + dc);
5597- let leaf_above = if dr == 0 { above_mode } else { leaf_modes[i - 2] };
5598- let leaf_left = if dc == 0 { left_mode } else { leaf_modes[i - 1] };
5666+ // Neighbour modes at real mi granularity: inside the group the
5667+ // sibling leaf, outside it the split-leaf map when the adjacent mi
5668+ // was itself a 4x4 leaf, else the coarse 16x16 slot (see
5669+ // `sub8_mode_col`'s doc).
5670+ let leaf_above = if dr == 0 {
5671+ neighbours.mode_above_mi(lmi.0, lmi.1).unwrap_or(above_mode)
5672+ } else {
5673+ leaf_modes[i - 2]
5674+ };
5675+ let leaf_left = if dc == 0 {
5676+ neighbours.mode_left_mi(lmi.0, lmi.1).unwrap_or(left_mode)
5677+ } else {
5678+ leaf_modes[i - 1]
5679+ };
55995680 let has_chroma = i == 3;
56005681 let skip_ctx = neighbours.skip_txfm_ctx(lmi.0, lmi.1);
56015682 let (skip, mode, angle_delta_y, chroma, filter_intra) = read_intra_mode_sub8(
@@ -5604,6 +5685,7 @@ fn decode_leaf_split4(
56045685 )?;
56055686 leaf_modes[i] = mode;
56065687 leaf_skips[i] = skip;
5688+ neighbours.record_mode_mi(lmi.0, lmi.1, 1, 1, mode);
56075689 let smooth_neighbor = is_smooth_mode(leaf_above) || is_smooth_mode(leaf_left);
56085690 if smooth_neighbor {
56095691 SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1));
crates/ec-av1/src/encode.rsmodified+35 -14
@@ -1020,7 +1020,7 @@ pub(crate) struct Reach {
10201020 /// it. Indexed by the block's position in the superblock, in blocks of its own
10211021 /// size, as `row * (128 / side) + col` (`Reach::table_stride`) -- libaom's
10221022 /// 128, not this crate's 64 superblock -- bit by bit from the low end.
1023-const HAS_TOP_RIGHT: [&[u8]; 3] = [
1023+const HAS_TOP_RIGHT: [&[u8]; 4] = [
10241024 &[255, 85, 119, 85, 127, 85, 119, 85],
10251025 &[95, 87],
10261026 // has_tr_8x8 (libaom av1/common/reconintra.c) -- table_stride(8) == 16
@@ -1029,11 +1029,28 @@ const HAS_TOP_RIGHT: [&[u8]; 3] = [
10291029 255, 255, 85, 85, 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85, 255, 127, 85, 85,
10301030 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85,
10311031 ],
1032+
1033+ // has_tr_4x4 (libaom av1/common/reconintra.c, transcribed verbatim) --
1034+ // table_stride(4) == 32 (128 / 4), so 32 bits per row, 32 rows, 128
1035+ // bytes. Sub-8x8 leaves (lane-sub8) are the only blocks that reach here
1036+ // with side 4 outside a TX4 split; without their own row they clamped
1037+ // into the 8x8 table and read above-right samples the decoder has not
1038+ // written yet (all-zero pixels in the bottom-right triangle of a
1039+ // directional 4x4 leaf).
1040+ &[
1041+ 255, 255, 255, 255, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85, 127, 127, 127, 127,
1042+ 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85, 255, 127, 255, 127, 85, 85, 85, 85,
1043+ 119, 119, 119, 119, 85, 85, 85, 85, 127, 127, 127, 127, 85, 85, 85, 85, 119, 119, 119, 119,
1044+ 85, 85, 85, 85, 255, 255, 255, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85,
1045+ 127, 127, 127, 127, 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85, 255, 127, 255, 127,
1046+ 85, 85, 85, 85, 119, 119, 119, 119, 85, 85, 85, 85, 127, 127, 127, 127, 85, 85, 85, 85,
1047+ 119, 119, 119, 119, 85, 85, 85, 85,
1048+ ],
10321049 ];
10331050
10341051 /// `has_bl_16x16` / `has_bl_32x32`: the same, for the block below and to the
10351052 /// left.
1036-const HAS_BOTTOM_LEFT: [&[u8]; 3] = [
1053+const HAS_BOTTOM_LEFT: [&[u8]; 4] = [
10371054 &[84, 16, 84, 0, 84, 16, 84, 0],
10381055 &[4, 4],
10391056 // has_bl_8x8 (libaom av1/common/reconintra.c)
@@ -1041,6 +1058,16 @@ const HAS_BOTTOM_LEFT: [&[u8]; 3] = [
10411058 84, 85, 16, 17, 84, 85, 0, 1, 84, 85, 16, 17, 84, 85, 0, 0, 84, 85, 16, 17, 84, 85, 0, 1,
10421059 84, 85, 16, 17, 84, 85, 0, 0,
10431060 ],
1061+
1062+ // has_bl_4x4 (same source, same order).
1063+ &[
1064+ 84, 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0, 1, 1, 1, 84, 85, 85, 85, 16, 17, 17, 17,
1065+ 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,
1066+ 85, 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0, 0, 0, 0, 84, 85, 85, 85, 16, 17, 17, 17, 84,
1067+ 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,
1068+ 85, 85, 16, 17, 17, 17, 84, 85, 85, 85, 0, 1, 1, 1, 84, 85, 85, 85, 16, 17, 17, 17, 84, 85,
1069+ 85, 85, 0, 0, 0, 0,
1070+ ],
10441071 ];
10451072
10461073 /// `has_tr_32x16`/`has_tr_16x32` (libaom `reconintra.c`) -- lane-intradisp
@@ -1134,17 +1161,9 @@ impl Reach {
11341161 if col + 1 == per_side {
11351162 return false;
11361163 }
1137- // corner-cut: `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` only carry dedicated
1138- // rows for side 8/16/32/64 (libaom's own `has_tr_8x8`/`has_tr_16x16`
1139- // families); a `TxMode::Select` TX4 transform unit reaches here with
1140- // side 4, whose real table (`has_tr_4x4`, 128 bytes) was never
1141- // transcribed. Clamping into the 16/32 table's own bit range keeps
1142- // this in-bounds and correct at every position that table already
1143- // covers periodically -- wrong only at the small remainder past its
1144- // length, a reach over-/under-estimate (extra or missing above-right
1145- // reference pixels), never a stream desync (Reach never gates a
1146- // symbol read). Ceiling: transcribe `has_tr_4x4`/`has_bl_4x4`
1147- // verbatim and give TX4 its own `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` row.
1164+ // lane-sub8 r4: side 4 now has its own `has_tr_4x4`/`has_bl_4x4`
1165+ // row (the corner-cut this comment used to describe); sides 16/32/64
1166+ // share row 0 exactly as libaom's tables repeat.
11481167 let table = HAS_TOP_RIGHT[Self::table(side)];
11491168 Self::bit(
11501169 table,
@@ -1164,7 +1183,7 @@ impl Reach {
11641183 if row + 1 == per_side {
11651184 return false;
11661185 }
1167- // corner-cut: see `top_right`'s note -- same clamp, same ceiling.
1186+ // See `top_right`'s note.
11681187 let table = HAS_BOTTOM_LEFT[Self::table(side)];
11691188 Self::bit(
11701189 table,
@@ -1200,6 +1219,8 @@ impl Reach {
12001219 1
12011220 } else if side == 8 {
12021221 2
1222+ } else if side == 4 {
1223+ 3
12031224 } else {
12041225 0
12051226 }