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 {
1768 /// one was coded at 8x8.1768 /// one was coded at 8x8.
1769 above_side_mi: Vec<usize>,1769 above_side_mi: Vec<usize>,
1770 left_side_mi: Vec<usize>,1770 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)>,
1771 /// Whether the block above/left of this column/row was coded `skip` --1784 /// Whether the block above/left of this column/row was coded `skip` --
1772 /// an inter frame's own `skip` context (spec `SkipContext`), which the1785 /// an inter frame's own `skip` context (spec `SkipContext`), which the
1773 /// key-frame writer never tracks (its skip context is always zero); a1786 /// key-frame writer never tracks (its skip context is always zero); a
@@ -1895,6 +1908,8 @@ impl Neighbours {
1895 left_side: vec![SB; rows],1908 left_side: vec![SB; rows],
1896 above_side_mi: vec![SB; cols * (SUB / MI)],1909 above_side_mi: vec![SB; cols * (SUB / MI)],
1897 left_side_mi: vec![SB; rows * (SUB / MI)],1910 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)],
1898 above_skip: vec![false; cols],1913 above_skip: vec![false; cols],
1899 left_skip: vec![false; rows],1914 left_skip: vec![false; rows],
1900 above_skip_mode: vec![false; cols],1915 above_skip_mode: vec![false; cols],
@@ -2456,6 +2471,46 @@ impl Neighbours {
2456 /// [`Self::partition_ctx`]) are the neighbour's own width/height per the2471 /// [`Self::partition_ctx`]) are the neighbour's own width/height per the
2457 /// spec's per-edge partition-context rule, so a true rectangular block2472 /// spec's per-edge partition-context rule, so a true rectangular block
2458 /// naturally wants two different values here rather than one.2473 /// 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+
2459 fn record_rect(2514 fn record_rect(
2460 &mut self,2515 &mut self,
2461 at: (usize, usize),2516 at: (usize, usize),
@@ -2476,6 +2531,7 @@ impl Neighbours {
2476 self.left_uv_mode[r + cell] = uv_mode;2531 self.left_uv_mode[r + cell] = uv_mode;
2477 self.left_side[r + cell] = h;2532 self.left_side[r + cell] = h;
2478 }2533 }
2534+ self.record_mode_mi(r * (SUB / MI), c * (SUB / MI), w / MI, h / MI, mode);
2479 self.record_mi_rect((r * (SUB / MI), c * (SUB / MI)), w, h, grids);2535 self.record_mi_rect((r * (SUB / MI), c * (SUB / MI)), w, h, grids);
2480 }2536 }
2481 2537
@@ -2586,6 +2642,7 @@ impl Neighbours {
2586 }2642 }
2587 let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI));2643 let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI));
2588 let side_mi = side / MI;2644 let side_mi = side / MI;
2645+ self.record_mode_mi(mi_r, mi_c, side_mi, side_mi, mode);
2589 for cell in 0..side_mi {2646 for cell in 0..side_mi {
2590 self.left_side_mi[mi_r + cell] = side;2647 self.left_side_mi[mi_r + cell] = side;
2591 self.above_side_mi[mi_c + cell] = side;2648 self.above_side_mi[mi_c + cell] = side;
@@ -5164,6 +5221,16 @@ fn decode_leaf8(
5164 left_mode = pmode;5221 left_mode = pmode;
5165 }5222 }
5166 }5223 }
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+ }
5167 // See [`PlaneBuf::reconstruct`]'s doc: chroma stays `false` (exact) until5234 // See [`PlaneBuf::reconstruct`]'s doc: chroma stays `false` (exact) until
5168 // a smooth/paeth `uv_mode` can reach decode at all.5235 // a smooth/paeth `uv_mode` can reach decode at all.
5169 let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode);5236 let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode);
@@ -5451,11 +5518,13 @@ fn decode_leaf8(
5451 }5518 }
5452 neighbours.fill_skip_grid(leaf_mi, 2, skip);5519 neighbours.fill_skip_grid(leaf_mi, 2, skip);
5453 neighbours.fill_lf_grid(leaf_mi, 2, 4, 0);5520 neighbours.fill_lf_grid(leaf_mi, 2, 4, 0);
5521+ neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode);
5454 return Ok(mode);5522 return Ok(mode);
5455 }5523 }
5456 neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]);5524 neighbours.record_mi(leaf_mi, 8, &[luma_grid, u_grid, v_grid]);
5457 neighbours.fill_skip_grid(leaf_mi, 2, skip);5525 neighbours.fill_skip_grid(leaf_mi, 2, skip);
5458 neighbours.fill_lf_grid(leaf_mi, 2, 8, 0);5526 neighbours.fill_lf_grid(leaf_mi, 2, 8, 0);
5527+ neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode);
5459 Ok(mode)5528 Ok(mode)
5460 }5529 }
5461 5530
@@ -5594,8 +5663,20 @@ fn decode_leaf_split4(
5594 for i in 0..4usize {5663 for i in 0..4usize {
5595 let (dr, dc) = (i / 2, i % 2);5664 let (dr, dc) = (i / 2, i % 2);
5596 let lmi = (leaf_mi.0 + dr, leaf_mi.1 + dc);5665 let lmi = (leaf_mi.0 + dr, leaf_mi.1 + dc);
5597- let leaf_above = if dr == 0 { above_mode } else { leaf_modes[i - 2] };5666+ // Neighbour modes at real mi granularity: inside the group the
5598- let leaf_left = if dc == 0 { left_mode } else { leaf_modes[i - 1] };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+ };
5599 let has_chroma = i == 3;5680 let has_chroma = i == 3;
5600 let skip_ctx = neighbours.skip_txfm_ctx(lmi.0, lmi.1);5681 let skip_ctx = neighbours.skip_txfm_ctx(lmi.0, lmi.1);
5601 let (skip, mode, angle_delta_y, chroma, filter_intra) = read_intra_mode_sub8(5682 let (skip, mode, angle_delta_y, chroma, filter_intra) = read_intra_mode_sub8(
@@ -5604,6 +5685,7 @@ fn decode_leaf_split4(
5604 )?;5685 )?;
5605 leaf_modes[i] = mode;5686 leaf_modes[i] = mode;
5606 leaf_skips[i] = skip;5687 leaf_skips[i] = skip;
5688+ neighbours.record_mode_mi(lmi.0, lmi.1, 1, 1, mode);
5607 let smooth_neighbor = is_smooth_mode(leaf_above) || is_smooth_mode(leaf_left);5689 let smooth_neighbor = is_smooth_mode(leaf_above) || is_smooth_mode(leaf_left);
5608 if smooth_neighbor {5690 if smooth_neighbor {
5609 SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1));5691 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 {
1020 /// it. Indexed by the block's position in the superblock, in blocks of its own1020 /// it. Indexed by the block's position in the superblock, in blocks of its own
1021 /// size, as `row * (128 / side) + col` (`Reach::table_stride`) -- libaom's1021 /// size, as `row * (128 / side) + col` (`Reach::table_stride`) -- libaom's
1022 /// 128, not this crate's 64 superblock -- bit by bit from the low end.1022 /// 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] = [
1024 &[255, 85, 119, 85, 127, 85, 119, 85],1024 &[255, 85, 119, 85, 127, 85, 119, 85],
1025 &[95, 87],1025 &[95, 87],
1026 // has_tr_8x8 (libaom av1/common/reconintra.c) -- table_stride(8) == 161026 // has_tr_8x8 (libaom av1/common/reconintra.c) -- table_stride(8) == 16
@@ -1029,11 +1029,28 @@ const HAS_TOP_RIGHT: [&[u8]; 3] = [
1029 255, 255, 85, 85, 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85, 255, 127, 85, 85,1029 255, 255, 85, 85, 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85, 255, 127, 85, 85,
1030 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85,1030 119, 119, 85, 85, 127, 127, 85, 85, 119, 119, 85, 85,
1031 ],1031 ],
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+ ],
1032 ];1049 ];
1033 1050
1034 /// `has_bl_16x16` / `has_bl_32x32`: the same, for the block below and to the1051 /// `has_bl_16x16` / `has_bl_32x32`: the same, for the block below and to the
1035 /// left.1052 /// left.
1036-const HAS_BOTTOM_LEFT: [&[u8]; 3] = [1053+const HAS_BOTTOM_LEFT: [&[u8]; 4] = [
1037 &[84, 16, 84, 0, 84, 16, 84, 0],1054 &[84, 16, 84, 0, 84, 16, 84, 0],
1038 &[4, 4],1055 &[4, 4],
1039 // has_bl_8x8 (libaom av1/common/reconintra.c)1056 // has_bl_8x8 (libaom av1/common/reconintra.c)
@@ -1041,6 +1058,16 @@ const HAS_BOTTOM_LEFT: [&[u8]; 3] = [
1041 84, 85, 16, 17, 84, 85, 0, 1, 84, 85, 16, 17, 84, 85, 0, 0, 84, 85, 16, 17, 84, 85, 0, 1,1058 84, 85, 16, 17, 84, 85, 0, 1, 84, 85, 16, 17, 84, 85, 0, 0, 84, 85, 16, 17, 84, 85, 0, 1,
1042 84, 85, 16, 17, 84, 85, 0, 0,1059 84, 85, 16, 17, 84, 85, 0, 0,
1043 ],1060 ],
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+ ],
1044 ];1071 ];
1045 1072
1046 /// `has_tr_32x16`/`has_tr_16x32` (libaom `reconintra.c`) -- lane-intradisp1073 /// `has_tr_32x16`/`has_tr_16x32` (libaom `reconintra.c`) -- lane-intradisp
@@ -1134,17 +1161,9 @@ impl Reach {
1134 if col + 1 == per_side {1161 if col + 1 == per_side {
1135 return false;1162 return false;
1136 }1163 }
1137- // corner-cut: `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` only carry dedicated1164+ // lane-sub8 r4: side 4 now has its own `has_tr_4x4`/`has_bl_4x4`
1138- // rows for side 8/16/32/64 (libaom's own `has_tr_8x8`/`has_tr_16x16`1165+ // row (the corner-cut this comment used to describe); sides 16/32/64
1139- // families); a `TxMode::Select` TX4 transform unit reaches here with1166+ // share row 0 exactly as libaom's tables repeat.
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.
1148 let table = HAS_TOP_RIGHT[Self::table(side)];1167 let table = HAS_TOP_RIGHT[Self::table(side)];
1149 Self::bit(1168 Self::bit(
1150 table,1169 table,
@@ -1164,7 +1183,7 @@ impl Reach {
1164 if row + 1 == per_side {1183 if row + 1 == per_side {
1165 return false;1184 return false;
1166 }1185 }
1167- // corner-cut: see `top_right`'s note -- same clamp, same ceiling.1186+ // See `top_right`'s note.
1168 let table = HAS_BOTTOM_LEFT[Self::table(side)];1187 let table = HAS_BOTTOM_LEFT[Self::table(side)];
1169 Self::bit(1188 Self::bit(
1170 table,1189 table,
@@ -1200,6 +1219,8 @@ impl Reach {
1200 11219 1
1201 } else if side == 8 {1220 } else if side == 8 {
1202 21221 2
1222+ } else if side == 4 {
1223+ 3
1203 } else {1224 } else {
1204 01225 0
1205 }1226 }