forge
Explore
Log in

Don't you dare go Hollow

Narrow the mode-info publish surface -- palette colours only from palette blocks, (u16,u8)/u8 edge maps

Browse files at this commit

ce0464d1e1fa476aa75bcc5ee0f86d7268124e44Ahmet Kaan GümüşParent: a263e1e
  • perf
    • narrow the mode-info publish surface -- palette colours only from palette blocks, (u16,u8)/u8 edge maps
Show full message
perf(av1): narrow the mode-info publish surface -- palette colours only from palette blocks, (u16,u8)/u8 edge maps

The parse thread stamps every block's mode info into the per-mi neighbour
bands, and a large part of what it wrote was width, not information:

* `record_palette_y_rect`/`record_palette_uv_rect`/`record_strip_palette`
  wrote 16 bytes of base colours per mi per side for EVERY block, palette
  or not. Spec 5.11.46 `get_palette_cache` (and the palette-mode ctx)
  reads a neighbour's colours only where its size is non-zero, so a
  size-0 stamp can leave the previous colours untouched; only the size
  has to be published.
* `sub8_mode_col`/`sub8_mode_row`/`uv_mode_col`/`uv_mode_row` held
  `(usize, usize)` = 16 bytes for an mi index and a mode: now `(u16, u8)`
  = 4, sentinel `u16::MAX` (an mi index cannot reach it -- the largest
  legal frame is 65536 px = 16384 mi).
* `above_side_mi`/`left_side_mi` and the four palette size bands were
  `usize` for values that never exceed 128 resp. 8: now `u8`.

Measured on the 4K segment, instructions:u (deterministic, +-0.00%),
ABBA-interleaved against the base build:
  palette colours   398.257 -> 398.018 G (1 thr)   403.16 -> 402.93 G (16,4,2)
  (u16,u8) maps     398.019 -> 397.823 G           402.92 -> 402.70 G
  u8 side/palette   397.825 -> 397.742 G           402.72 -> 402.69 G
  total             -0.129% (1 thr)                -0.115% ((16,4,2))
Task-clock was not usable this round (loadavg 7-20 throughout).

Byte-exact: all 12 gate hashes (seg1080/seg4k/seg4k_lr/palette witness at
1, 4 and 16/4/2 threads) unchanged.

Also drops two stale notes on `above_ref1`/`left_ref1` claiming no decode
path ever produces a compound block: audited this round, compound blocks
do occur, `record_compound_ctx_rect_mi` fills the array correctly and six
context sites read it. Code was right, the comments were not.
crates/ec-av1/src/decode.rsmodified+130 -68
@@ -6380,8 +6380,10 @@ struct Neighbours {
6380 /// block (lane-av1-rect) share a single [`SUB`]-grid cell, so a coarse6380 /// block (lane-av1-rect) share a single [`SUB`]-grid cell, so a coarse
6381 /// array cannot tell the second leaf's partition symbol that the first6381 /// array cannot tell the second leaf's partition symbol that the first
6382 /// one was coded at 8x8.6382 /// one was coded at 8x8.
6383- above_side_mi: Vec<usize>,6383+ /// lane-publish2: `u8` (a block side is at most 128 px, and so is
6384- left_side_mi: Vec<usize>,6384+ /// [`NO_NEIGHBOUR_SIDE`]) -- every block stamps these per mi.
6385+ above_side_mi: Vec<u8>,
6386+ left_side_mi: Vec<u8>,
6385 /// The luma `mode` of the last `BLOCK_4X4` leaf decoded at each mi column6387 /// The luma `mode` of the last `BLOCK_4X4` leaf decoded at each mi column
6386 /// / row, with the mi position it was written at6388 /// / row, with the mi position it was written at
6387 /// (`(usize::MAX, _)` = never written). `above_mode`/`left_mode` are on6389 /// (`(usize::MAX, _)` = never written). `above_mode`/`left_mode` are on
@@ -6393,15 +6395,20 @@ struct Neighbours {
6393 /// and read only by [`decode_leaf_split4`]; the position guard makes it6395 /// and read only by [`decode_leaf_split4`]; the position guard makes it
6394 /// fall back to the coarse slot whenever the real neighbour was a block6396 /// fall back to the coarse slot whenever the real neighbour was a block
6395 /// of 8x8 or larger (whose one mode is correct for every row it spans).6397 /// of 8x8 or larger (whose one mode is correct for every row it spans).
6396- sub8_mode_col: Vec<(usize, usize)>,6398+ /// lane-publish2: `(mi position, mode)` narrowed from `(usize, usize)` to
6397- sub8_mode_row: Vec<(usize, usize)>,6399+ /// `(u16, u8)` -- every block stamps these mi-granular maps, and 16 bytes
6400+ /// per mi per side of publish traffic was pure width. `u16::MAX` is the
6401+ /// "never written" sentinel (an mi index cannot reach it: AV1's largest
6402+ /// frame is 65536 px = 16384 mi).
6403+ sub8_mode_col: Vec<(u16, u8)>,
6404+ sub8_mode_row: Vec<(u16, u8)>,
6398 /// The `uv_mode` twin of `sub8_mode_col`/`sub8_mode_row` (lane-sub8 r5):6405 /// The `uv_mode` twin of `sub8_mode_col`/`sub8_mode_row` (lane-sub8 r5):
6399 /// chroma's edge-filter type reads the CHROMA neighbour's `uv_mode`6406 /// chroma's edge-filter type reads the CHROMA neighbour's `uv_mode`
6400 /// (`get_intra_edge_filter_type`, reconintra.c:974), and the coarse6407 /// (`get_intra_edge_filter_type`, reconintra.c:974), and the coarse
6401 /// [`SUB`] slots cannot name it once a 16x16 is split into 8x8 leaves --6408 /// [`SUB`] slots cannot name it once a 16x16 is split into 8x8 leaves --
6402 /// the leaf above writes the same row slot the block to the left owns.6409 /// the leaf above writes the same row slot the block to the left owns.
6403- uv_mode_col: Vec<(usize, usize)>,6410+ uv_mode_col: Vec<(u16, u8)>,
6404- uv_mode_row: Vec<(usize, usize)>,6411+ uv_mode_row: Vec<(u16, u8)>,
6405 /// Every mi cell's own `uv_mode`, the mi grid libaom's6412 /// Every mi cell's own `uv_mode`, the mi grid libaom's
6406 /// `chroma_above_mbmi`/`chroma_left_mbmi` read directly (`set_mi_row_col`,6413 /// `chroma_above_mbmi`/`chroma_left_mbmi` read directly (`set_mi_row_col`,
6407 /// blockd.h). lane-t900 r12: the one-slot-per-column6414 /// blockd.h). lane-t900 r12: the one-slot-per-column
@@ -6454,8 +6461,14 @@ struct Neighbours {
6454 /// compound (spec `RefFrames[1]`), or `None` for single-ref/intra --6461 /// compound (spec `RefFrames[1]`), or `None` for single-ref/intra --
6455 /// lane-av1comp: `mvstack::reference_mode_ctx`/`comp_reference_type_ctx`6462 /// lane-av1comp: `mvstack::reference_mode_ctx`/`comp_reference_type_ctx`
6456 /// need a full [`crate::mvstack::NeighbourRef`], not just `above_ref`'s6463 /// need a full [`crate::mvstack::NeighbourRef`], not just `above_ref`'s
6457- /// `i8`, once a compound `read_ref_frames` lands. Always `None` today6464+ /// `i8`, once a compound `read_ref_frames` lands.
6458- /// (no decode path here ever produces a compound block yet).6465+ /// lane-publish2 audit: the "always `None` today" note this doc used to
6466+ /// carry is long stale -- compound blocks do occur, and
6467+ /// [`Self::record_compound_ctx_rect_mi`] stamps the real second reference
6468+ /// over their span right after [`Self::record_inter_rect_mi`]'s reset.
6469+ /// The array IS read: `single_ref_p*`/`comp_ref` contexts and every
6470+ /// [`crate::mvstack::NeighbourRef`] construction (class
6471+ /// [[branch-dropped-as-unreachable]] -- the note, not the code, was wrong).
6459 above_ref1: Vec<Option<i8>>,6472 above_ref1: Vec<Option<i8>>,
6460 left_ref1: Vec<Option<i8>>,6473 left_ref1: Vec<Option<i8>>,
6461 /// The precomputed `get_comp_group_idx_context`/`get_comp_index_context`6474 /// The precomputed `get_comp_group_idx_context`/`get_comp_index_context`
@@ -6542,15 +6555,15 @@ struct Neighbours {
6542 /// and its base colours -- `av1_get_palette_mode_ctx`/6555 /// and its base colours -- `av1_get_palette_mode_ctx`/
6543 /// `av1_get_palette_cache` (pred_common.c)'s neighbour lookup, lane-palette6556 /// `av1_get_palette_cache` (pred_common.c)'s neighbour lookup, lane-palette
6544 /// r2. UV has no equivalent yet (stage 2, unreconstructed).6557 /// r2. UV has no equivalent yet (stage 2, unreconstructed).
6545- above_palette_size: Vec<usize>,6558+ above_palette_size: Vec<u8>,
6546- left_palette_size: Vec<usize>,6559+ left_palette_size: Vec<u8>,
6547 above_palette_colors: Vec<[u16; 8]>,6560 above_palette_colors: Vec<[u16; 8]>,
6548 left_palette_colors: Vec<[u16; 8]>,6561 left_palette_colors: Vec<[u16; 8]>,
6549 /// As the four fields above, for the neighbour's own chroma (U-channel6562 /// As the four fields above, for the neighbour's own chroma (U-channel
6550 /// only -- `av1_get_palette_cache`'s plane-1 lookup, [`Self::palette_uv_cache`])6563 /// only -- `av1_get_palette_cache`'s plane-1 lookup, [`Self::palette_uv_cache`])
6551 /// palette size/colours (lane-palette2 r1).6564 /// palette size/colours (lane-palette2 r1).
6552- above_palette_uv_size: Vec<usize>,6565+ above_palette_uv_size: Vec<u8>,
6553- left_palette_uv_size: Vec<usize>,6566+ left_palette_uv_size: Vec<u8>,
6554 above_palette_uv_colors: Vec<[u16; 8]>,6567 above_palette_uv_colors: Vec<[u16; 8]>,
6555 left_palette_uv_colors: Vec<[u16; 8]>,6568 left_palette_uv_colors: Vec<[u16; 8]>,
6556 /// This tile's own top-left corner, in 4x4 mode-info units (spec6569 /// This tile's own top-left corner, in 4x4 mode-info units (spec
@@ -6590,12 +6603,12 @@ impl Neighbours {
6590 left_uv_mode: vec![DC_PRED; rows],6603 left_uv_mode: vec![DC_PRED; rows],
6591 above_side: vec![NO_NEIGHBOUR_SIDE; cols],6604 above_side: vec![NO_NEIGHBOUR_SIDE; cols],
6592 left_side: vec![NO_NEIGHBOUR_SIDE; rows],6605 left_side: vec![NO_NEIGHBOUR_SIDE; rows],
6593- above_side_mi: vec![NO_NEIGHBOUR_SIDE; cols * (SUB / MI)],6606+ above_side_mi: vec![NO_NEIGHBOUR_SIDE as u8; cols * (SUB / MI)],
6594- left_side_mi: vec![NO_NEIGHBOUR_SIDE; rows * (SUB / MI)],6607+ left_side_mi: vec![NO_NEIGHBOUR_SIDE as u8; rows * (SUB / MI)],
6595- sub8_mode_col: vec![(usize::MAX, 0); cols * (SUB / MI)],6608+ sub8_mode_col: vec![(u16::MAX, 0); cols * (SUB / MI)],
6596- sub8_mode_row: vec![(usize::MAX, 0); rows * (SUB / MI)],6609+ sub8_mode_row: vec![(u16::MAX, 0); rows * (SUB / MI)],
6597- uv_mode_col: vec![(usize::MAX, 0); cols * (SUB / MI)],6610+ uv_mode_col: vec![(u16::MAX, 0); cols * (SUB / MI)],
6598- uv_mode_row: vec![(usize::MAX, 0); rows * (SUB / MI)],6611+ uv_mode_row: vec![(u16::MAX, 0); rows * (SUB / MI)],
6599 uv_mode_grid: vec![u8::MAX; cols * (SUB / MI) * rows * (SUB / MI)],6612 uv_mode_grid: vec![u8::MAX; cols * (SUB / MI) * rows * (SUB / MI)],
6600 uv_grid_cols: cols * (SUB / MI),6613 uv_grid_cols: cols * (SUB / MI),
6601 above_txfm: vec![TXFM_CTX_INIT; cols * (SUB / MI)],6614 above_txfm: vec![TXFM_CTX_INIT; cols * (SUB / MI)],
@@ -6661,7 +6674,7 @@ impl Neighbours {
6661 let end = col1_mi.min(self.above.len());6674 let end = col1_mi.min(self.above.len());
6662 for i in col0_mi.min(end)..end {6675 for i in col0_mi.min(end)..end {
6663 self.above[i] = Default::default();6676 self.above[i] = Default::default();
6664- self.above_side_mi[i] = NO_NEIGHBOUR_SIDE;6677+ self.above_side_mi[i] = NO_NEIGHBOUR_SIDE as u8;
6665 }6678 }
6666 let (sc0, sc1) = (6679 let (sc0, sc1) = (
6667 col0_mi / (SUB / MI),6680 col0_mi / (SUB / MI),
@@ -6689,8 +6702,8 @@ impl Neighbours {
6689 // tile's first block would read as its own above neighbour.6702 // tile's first block would read as its own above neighbour.
6690 let mcol_end = col1_mi.min(self.sub8_mode_col.len());6703 let mcol_end = col1_mi.min(self.sub8_mode_col.len());
6691 for i in col0_mi.min(mcol_end)..mcol_end {6704 for i in col0_mi.min(mcol_end)..mcol_end {
6692- self.sub8_mode_col[i] = (usize::MAX, 0);6705+ self.sub8_mode_col[i] = (u16::MAX, 0);
6693- self.uv_mode_col[i] = (usize::MAX, 0);6706+ self.uv_mode_col[i] = (u16::MAX, 0);
6694 }6707 }
6695 // lane-mergefix r2: `av1_zero_above_context` also memsets the txfm row6708 // lane-mergefix r2: `av1_zero_above_context` also memsets the txfm row
6696 // over the tile's own column span (to `tx_size_wide[TX_SIZES_LARGEST]`,6709 // over the tile's own column span (to `tx_size_wide[TX_SIZES_LARGEST]`,
@@ -6735,17 +6748,24 @@ impl Neighbours {
6735 // cache came out one entry short (class [[context-read-from-one-cell]]).6748 // cache came out one entry short (class [[context-read-from-one-cell]]).
6736 for cell in 0..(w / MI).max(1) {6749 for cell in 0..(w / MI).max(1) {
6737 if let Some(e) = self.above_palette_size.get_mut(c + cell) {6750 if let Some(e) = self.above_palette_size.get_mut(c + cell) {
6738- *e = size;6751+ *e = size as u8;
6752+ // lane-publish2: spec 5.11.46 `get_palette_cache` reads a
6753+ // neighbour's colours only where its palette size > 0, so a
6754+ // non-palette block need not stamp 16 bytes of zeroes here.
6755+ if size > 0 {
6739 self.above_palette_colors[c + cell] = colors;6756 self.above_palette_colors[c + cell] = colors;
6740 }6757 }
6741 }6758 }
6759+ }
6742 for cell in 0..(h / MI).max(1) {6760 for cell in 0..(h / MI).max(1) {
6743 if let Some(e) = self.left_palette_size.get_mut(r + cell) {6761 if let Some(e) = self.left_palette_size.get_mut(r + cell) {
6744- *e = size;6762+ *e = size as u8;
6763+ if size > 0 {
6745 self.left_palette_colors[r + cell] = colors;6764 self.left_palette_colors[r + cell] = colors;
6746 }6765 }
6747 }6766 }
6748 }6767 }
6768+ }
6749 6769
6750 /// `av1_get_palette_mode_ctx` (pred_common.h:197): count of the above/left6770 /// `av1_get_palette_mode_ctx` (pred_common.h:197): count of the above/left
6751 /// neighbours that are themselves a palette-Y block, plus6771 /// neighbours that are themselves a palette-Y block, plus
@@ -6777,7 +6797,7 @@ impl Neighbours {
6777 // `xd->above_mbmi` exists for every row but the tile's own first.6797 // `xd->above_mbmi` exists for every row but the tile's own first.
6778 let above_is_palette = r > self.tile_row0_mi && self.above_palette_size[c] > 0;6798 let above_is_palette = r > self.tile_row0_mi && self.above_palette_size[c] > 0;
6779 let (above_n, above_colors) = if above_ok && self.above_palette_size[c] > 0 {6799 let (above_n, above_colors) = if above_ok && self.above_palette_size[c] > 0 {
6780- (self.above_palette_size[c], self.above_palette_colors[c])6800+ (usize::from(self.above_palette_size[c]), self.above_palette_colors[c])
6781 } else {6801 } else {
6782 (0, [0u16; 8])6802 (0, [0u16; 8])
6783 };6803 };
@@ -6788,7 +6808,7 @@ impl Neighbours {
6788 // and its `palette_y_mode` ctx.6808 // and its `palette_y_mode` ctx.
6789 let left_ok = c > self.tile_col0_mi;6809 let left_ok = c > self.tile_col0_mi;
6790 let (left_n, left_colors) = if left_ok && self.left_palette_size[r] > 0 {6810 let (left_n, left_colors) = if left_ok && self.left_palette_size[r] > 0 {
6791- (self.left_palette_size[r], self.left_palette_colors[r])6811+ (usize::from(self.left_palette_size[r]), self.left_palette_colors[r])
6792 } else {6812 } else {
6793 (0, [0u16; 8])6813 (0, [0u16; 8])
6794 };6814 };
@@ -6853,17 +6873,21 @@ impl Neighbours {
6853 // mi granularity, see [`Self::record_palette_y_rect`].6873 // mi granularity, see [`Self::record_palette_y_rect`].
6854 for cell in 0..(w / MI).max(1) {6874 for cell in 0..(w / MI).max(1) {
6855 if let Some(e) = self.above_palette_uv_size.get_mut(c + cell) {6875 if let Some(e) = self.above_palette_uv_size.get_mut(c + cell) {
6856- *e = size;6876+ *e = size as u8;
6877+ if size > 0 {
6857 self.above_palette_uv_colors[c + cell] = colors;6878 self.above_palette_uv_colors[c + cell] = colors;
6858 }6879 }
6859 }6880 }
6881+ }
6860 for cell in 0..(h / MI).max(1) {6882 for cell in 0..(h / MI).max(1) {
6861 if let Some(e) = self.left_palette_uv_size.get_mut(r + cell) {6883 if let Some(e) = self.left_palette_uv_size.get_mut(r + cell) {
6862- *e = size;6884+ *e = size as u8;
6885+ if size > 0 {
6863 self.left_palette_uv_colors[r + cell] = colors;6886 self.left_palette_uv_colors[r + cell] = colors;
6864 }6887 }
6865 }6888 }
6866 }6889 }
6890+ }
6867 6891
6868 /// [`Self::palette_ctx_and_cache`]'s cache half, for the U channel6892 /// [`Self::palette_ctx_and_cache`]'s cache half, for the U channel
6869 /// (`av1_get_palette_cache(xd, 1, cache)`) -- `palette_uv_mode_ctx` needs6893 /// (`av1_get_palette_cache(xd, 1, cache)`) -- `palette_uv_mode_ctx` needs
@@ -6880,14 +6904,14 @@ impl Neighbours {
6880 let (r, c) = mi;6904 let (r, c) = mi;
6881 let above_ok = r % 16 != 0;6905 let above_ok = r % 16 != 0;
6882 let (above_n, above_colors) = if above_ok && self.above_palette_uv_size[c] > 0 {6906 let (above_n, above_colors) = if above_ok && self.above_palette_uv_size[c] > 0 {
6883- (self.above_palette_uv_size[c], self.above_palette_uv_colors[c])6907+ (usize::from(self.above_palette_uv_size[c]), self.above_palette_uv_colors[c])
6884 } else {6908 } else {
6885 (0, [0u16; 8])6909 (0, [0u16; 8])
6886 };6910 };
6887 // Same tile-relative left availability as the luma cache above.6911 // Same tile-relative left availability as the luma cache above.
6888 let left_ok = c > self.tile_col0_mi;6912 let left_ok = c > self.tile_col0_mi;
6889 let (left_n, left_colors) = if left_ok && self.left_palette_uv_size[r] > 0 {6913 let (left_n, left_colors) = if left_ok && self.left_palette_uv_size[r] > 0 {
6890- (self.left_palette_uv_size[r], self.left_palette_uv_colors[r])6914+ (usize::from(self.left_palette_uv_size[r]), self.left_palette_uv_colors[r])
6891 } else {6915 } else {
6892 (0, [0u16; 8])6916 (0, [0u16; 8])
6893 };6917 };
@@ -7087,7 +7111,7 @@ impl Neighbours {
7087 self.left_mode.iter_mut().for_each(|m| *m = DC_PRED);7111 self.left_mode.iter_mut().for_each(|m| *m = DC_PRED);
7088 self.left_uv_mode.iter_mut().for_each(|m| *m = DC_PRED);7112 self.left_uv_mode.iter_mut().for_each(|m| *m = DC_PRED);
7089 self.left_side.iter_mut().for_each(|s| *s = NO_NEIGHBOUR_SIDE);7113 self.left_side.iter_mut().for_each(|s| *s = NO_NEIGHBOUR_SIDE);
7090- self.left_side_mi.iter_mut().for_each(|s| *s = NO_NEIGHBOUR_SIDE);7114+ self.left_side_mi.iter_mut().for_each(|s| *s = NO_NEIGHBOUR_SIDE as u8);
7091 self.left_skip.iter_mut().for_each(|s| *s = false);7115 self.left_skip.iter_mut().for_each(|s| *s = false);
7092 self.left_skip_mode.iter_mut().for_each(|s| *s = false);7116 self.left_skip_mode.iter_mut().for_each(|s| *s = false);
7093 self.left_inter.iter_mut().for_each(|i| *i = false);7117 self.left_inter.iter_mut().for_each(|i| *i = false);
@@ -7098,8 +7122,8 @@ impl Neighbours {
7098 self.left_filter.iter_mut().for_each(|f| *f = [3u8; 2]);7122 self.left_filter.iter_mut().for_each(|f| *f = [3u8; 2]);
7099 // lane-sub8 r6: mi-granular row maps reset with the rest of the left7123 // lane-sub8 r6: mi-granular row maps reset with the rest of the left
7100 // context (a tile/SB-row boundary has no left neighbour).7124 // context (a tile/SB-row boundary has no left neighbour).
7101- self.sub8_mode_row.iter_mut().for_each(|s| *s = (usize::MAX, 0));7125+ self.sub8_mode_row.iter_mut().for_each(|s| *s = (u16::MAX, 0));
7102- self.uv_mode_row.iter_mut().for_each(|s| *s = (usize::MAX, 0));7126+ self.uv_mode_row.iter_mut().for_each(|s| *s = (u16::MAX, 0));
7103 // lane-tiles r11 (COMMON's NEIGHBOUR MAPS rule): the palette side bands7127 // lane-tiles r11 (COMMON's NEIGHBOUR MAPS rule): the palette side bands
7104 // are per-mi maps like every other one here, so they reset with the7128 // are per-mi maps like every other one here, so they reset with the
7105 // row too -- the left guard above is what actually stops a cross-tile7129 // row too -- the left guard above is what actually stops a cross-tile
@@ -7207,9 +7231,9 @@ impl Neighbours {
7207 self.above_skip_mode[c..c + w_mi].fill(skip_mode);7231 self.above_skip_mode[c..c + w_mi].fill(skip_mode);
7208 self.above_inter[c..c + w_mi].fill(is_inter);7232 self.above_inter[c..c + w_mi].fill(is_inter);
7209 self.above_ref[c..c + w_mi].fill(ref_frame);7233 self.above_ref[c..c + w_mi].fill(ref_frame);
7210- // lane-av1comp: no caller of `record_inter` ever decodes a7234+ // The single-ref default: `record_compound_ctx_rect_mi` overwrites
7211- // compound block yet (see [`Self::above_ref1`]'s doc); the7235+ // this with the block's real second reference for a compound block,
7212- // parameter this becomes lands with `read_ref_frames`.7236+ // called right after this one (lane-publish2 audit).
7213 self.above_ref1[c..c + w_mi].fill(None);7237 self.above_ref1[c..c + w_mi].fill(None);
7214 // libaom `get_comp_group_idx_context`/`get_comp_index_context`'s7238 // libaom `get_comp_group_idx_context`/`get_comp_index_context`'s
7215 // single-ref special case: `ref_frame[0] == ALTREF_FRAME` reads7239 // single-ref special case: `ref_frame[0] == ALTREF_FRAME` reads
@@ -7315,8 +7339,8 @@ impl Neighbours {
7315 /// arrays so the second leaf sees the first leaf's own 8x8 side rather7339 /// arrays so the second leaf sees the first leaf's own 8x8 side rather
7316 /// than the enclosing 16x16 slot's stale, shared state.7340 /// than the enclosing 16x16 slot's stale, shared state.
7317 fn partition_ctx_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> usize {7341 fn partition_ctx_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> usize {
7318- 2 * usize::from(self.left_side_mi[mi_r] * 2 <= side)7342+ 2 * usize::from(usize::from(self.left_side_mi[mi_r]) * 2 <= side)
7319- + usize::from(self.above_side_mi[mi_c] * 2 <= side)7343+ + usize::from(usize::from(self.above_side_mi[mi_c]) * 2 <= side)
7320 }7344 }
7321 7345
7322 /// The gathered coded/DC-sign state of the blocks above and to the left7346 /// The gathered coded/DC-sign state of the blocks above and to the left
@@ -7476,7 +7500,8 @@ impl Neighbours {
7476 mi_h: usize,7500 mi_h: usize,
7477 mode: usize,7501 mode: usize,
7478 ) {7502 ) {
7479- let (last_r, last_c) = (mi_r + mi_h - 1, mi_c + mi_w - 1);7503+ let (last_r, last_c) = ((mi_r + mi_h - 1) as u16, (mi_c + mi_w - 1) as u16);
7504+ let mode = mode as u8;
7480 for cell in 0..mi_w {7505 for cell in 0..mi_w {
7481 if let Some(slot) = self.sub8_mode_col.get_mut(mi_c + cell) {7506 if let Some(slot) = self.sub8_mode_col.get_mut(mi_c + cell) {
7482 *slot = (last_r, mode);7507 *slot = (last_r, mode);
@@ -7495,14 +7520,14 @@ impl Neighbours {
7495 /// construction). See [`Self::record_mode_mi`].7520 /// construction). See [`Self::record_mode_mi`].
7496 fn mode_above_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> {7521 fn mode_above_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> {
7497 match self.sub8_mode_col.get(mi_c) {7522 match self.sub8_mode_col.get(mi_c) {
7498- Some(&(row, m)) if mi_r > self.tile_row0_mi && row == mi_r - 1 => Some(m),7523+ Some(&(row, m)) if mi_r > self.tile_row0_mi && usize::from(row) == mi_r - 1 => Some(usize::from(m)),
7499 _ => None,7524 _ => None,
7500 }7525 }
7501 }7526 }
7502 7527
7503 fn mode_left_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> {7528 fn mode_left_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> {
7504 match self.sub8_mode_row.get(mi_r) {7529 match self.sub8_mode_row.get(mi_r) {
7505- Some(&(col, m)) if mi_c > self.tile_col0_mi && col == mi_c - 1 => Some(m),7530+ Some(&(col, m)) if mi_c > self.tile_col0_mi && usize::from(col) == mi_c - 1 => Some(usize::from(m)),
7506 _ => None,7531 _ => None,
7507 }7532 }
7508 }7533 }
@@ -7518,8 +7543,8 @@ impl Neighbours {
7518 uv_mode: usize,7543 uv_mode: usize,
7519 ) {7544 ) {
7520 let (last_r, last_c) = (mi_r + mi_h - 1, mi_c + mi_w - 1);7545 let (last_r, last_c) = (mi_r + mi_h - 1, mi_c + mi_w - 1);
7521- fill_span(&mut self.uv_mode_col, mi_c, mi_w, (last_r, uv_mode));7546+ fill_span(&mut self.uv_mode_col, mi_c, mi_w, (last_r as u16, uv_mode as u8));
7522- fill_span(&mut self.uv_mode_row, mi_r, mi_h, (last_c, uv_mode));7547+ fill_span(&mut self.uv_mode_row, mi_r, mi_h, (last_c as u16, uv_mode as u8));
7523 // libaom writes one `MB_MODE_INFO` pointer into every mi cell a block7548 // libaom writes one `MB_MODE_INFO` pointer into every mi cell a block
7524 // covers, so the chroma neighbour read below is a plain grid read.7549 // covers, so the chroma neighbour read below is a plain grid read.
7525 let stride = self.uv_grid_cols;7550 let stride = self.uv_grid_cols;
@@ -7590,20 +7615,20 @@ impl Neighbours {
7590 };7615 };
7591 let above = match self.uv_mode_col.get(mi_c) {7616 let above = match self.uv_mode_col.get(mi_c) {
7592 _ if have_above && let Some(m) = cell(&self.uv_mode_grid, mi_r - 1, mi_c) => m,7617 _ if have_above && let Some(m) = cell(&self.uv_mode_grid, mi_r - 1, mi_c) => m,
7593- Some(&(row, m)) if have_above && row == mi_r - 1 => m,7618+ Some(&(row, m)) if have_above && usize::from(row) == mi_r - 1 => usize::from(m),
7594 _ if have_above => self.above_uv_mode[c],7619 _ if have_above => self.above_uv_mode[c],
7595 _ => DC_PRED,7620 _ => DC_PRED,
7596 };7621 };
7597 let left = match self.uv_mode_row.get(mi_r) {7622 let left = match self.uv_mode_row.get(mi_r) {
7598 _ if have_left && let Some(m) = cell(&self.uv_mode_grid, mi_r, mi_c - 1) => m,7623 _ if have_left && let Some(m) = cell(&self.uv_mode_grid, mi_r, mi_c - 1) => m,
7599- Some(&(col, m)) if have_left && col == mi_c - 1 => m,7624+ Some(&(col, m)) if have_left && usize::from(col) == mi_c - 1 => usize::from(m),
7600 _ if have_left => self.left_uv_mode[r],7625 _ if have_left => self.left_uv_mode[r],
7601 _ => DC_PRED,7626 _ => DC_PRED,
7602 };7627 };
7603 if have_above && cell(&self.uv_mode_grid, mi_r - 1, mi_c).is_some_and(|m| {7628 if have_above && cell(&self.uv_mode_grid, mi_r - 1, mi_c).is_some_and(|m| {
7604- !matches!(self.uv_mode_col.get(mi_c), Some(&(row, cm)) if row == mi_r - 1 && cm == m)7629+ !matches!(self.uv_mode_col.get(mi_c), Some(&(row, cm)) if usize::from(row) == mi_r - 1 && usize::from(cm) == m)
7605 }) || have_left && cell(&self.uv_mode_grid, mi_r, mi_c - 1).is_some_and(|m| {7630 }) || have_left && cell(&self.uv_mode_grid, mi_r, mi_c - 1).is_some_and(|m| {
7606- !matches!(self.uv_mode_row.get(mi_r), Some(&(col, cm)) if col == mi_c - 1 && cm == m)7631+ !matches!(self.uv_mode_row.get(mi_r), Some(&(col, cm)) if usize::from(col) == mi_c - 1 && usize::from(cm) == m)
7607 }) {7632 }) {
7608 UV_MODE_GRID_OVERRIDE_HITS.with(|h| h.set(h.get() + 1));7633 UV_MODE_GRID_OVERRIDE_HITS.with(|h| h.set(h.get() + 1));
7609 }7634 }
@@ -7762,8 +7787,8 @@ impl Neighbours {
7762 if crate::envflags::env_flag!("EC_ECPUB") {7787 if crate::envflags::env_flag!("EC_ECPUB") {
7763 eprintln!("EC_ECPUB blk mi=({mi_r},{mi_c}) wh=({w},{h}) lvl={}", states[0].level);7788 eprintln!("EC_ECPUB blk mi=({mi_r},{mi_c}) wh=({w},{h}) lvl={}", states[0].level);
7764 }7789 }
7765- fill_span(&mut self.left_side_mi, mi_r, h_mi, h);7790+ fill_span(&mut self.left_side_mi, mi_r, h_mi, h as u8);
7766- fill_span(&mut self.above_side_mi, mi_c, w_mi, w);7791+ fill_span(&mut self.above_side_mi, mi_c, w_mi, w as u8);
7767 // A chroma 4x4 unit straddling the true luma edge is still whole in7792 // A chroma 4x4 unit straddling the true luma edge is still whole in
7768 // chroma's own halved grid, so libaom rounds the luma bound up to the7793 // chroma's own halved grid, so libaom rounds the luma bound up to the
7769 // plane's own 4x4 unit before clamping a subsampled plane7794 // plane's own 4x4 unit before clamping a subsampled plane
@@ -7952,8 +7977,8 @@ impl Neighbours {
7952 let (w_mi, h_mi) = (w / MI, h / MI);7977 let (w_mi, h_mi) = (w / MI, h / MI);
7953 self.record_mode_mi(mi_r, mi_c, w_mi, h_mi, mode);7978 self.record_mode_mi(mi_r, mi_c, w_mi, h_mi, mode);
7954 self.record_uv_mode_mi(mi_r, mi_c, w_mi, h_mi, uv_mode);7979 self.record_uv_mode_mi(mi_r, mi_c, w_mi, h_mi, uv_mode);
7955- fill_span(&mut self.left_side_mi, mi_r, h_mi, h);7980+ fill_span(&mut self.left_side_mi, mi_r, h_mi, h as u8);
7956- fill_span(&mut self.above_side_mi, mi_c, w_mi, w);7981+ fill_span(&mut self.above_side_mi, mi_c, w_mi, w as u8);
7957 let round_up_even = |n: usize| n.div_ceil(2) * 2;7982 let round_up_even = |n: usize| n.div_ceil(2) * 2;
7958 let (bound_h, bound_w) = (round_up_even(self.mi_rows), round_up_even(self.mi_cols));7983 let (bound_h, bound_w) = (round_up_even(self.mi_rows), round_up_even(self.mi_cols));
7959 for (plane_idx, grid) in chroma_grids.into_iter().enumerate() {7984 for (plane_idx, grid) in chroma_grids.into_iter().enumerate() {
@@ -8694,25 +8719,35 @@ fn record_strip_palette(
8694 {8719 {
8695 for cell in 0..(bw / MI).max(1) {8720 for cell in 0..(bw / MI).max(1) {
8696 if let Some(e) = n.above_palette_size.get_mut(c + cell) {8721 if let Some(e) = n.above_palette_size.get_mut(c + cell) {
8697- *e = y_size;8722+ *e = y_size as u8;
8723+ n.above_palette_uv_size[c + cell] = uv_size as u8;
8724+ // lane-publish2, see [`Neighbours::record_palette_y_rect`]:
8725+ // colours of a size-0 neighbour are never read.
8726+ if y_size > 0 {
8698 n.above_palette_colors[c + cell] = y_colors;8727 n.above_palette_colors[c + cell] = y_colors;
8699- n.above_palette_uv_size[c + cell] = uv_size;8728+ }
8729+ if uv_size > 0 {
8700 n.above_palette_uv_colors[c + cell] = uv_colors;8730 n.above_palette_uv_colors[c + cell] = uv_colors;
8701 }8731 }
8702 }8732 }
8703 }8733 }
8734+ }
8704 {8735 {
8705 for cell in 0..(bh / MI).max(1) {8736 for cell in 0..(bh / MI).max(1) {
8706 if n.left_palette_size.get(r + cell).is_none() {8737 if n.left_palette_size.get(r + cell).is_none() {
8707 break;8738 break;
8708 }8739 }
8709- n.left_palette_size[r + cell] = y_size;8740+ n.left_palette_size[r + cell] = y_size as u8;
8741+ n.left_palette_uv_size[r + cell] = uv_size as u8;
8742+ if y_size > 0 {
8710 n.left_palette_colors[r + cell] = y_colors;8743 n.left_palette_colors[r + cell] = y_colors;
8711- n.left_palette_uv_size[r + cell] = uv_size;8744+ }
8745+ if uv_size > 0 {
8712 n.left_palette_uv_colors[r + cell] = uv_colors;8746 n.left_palette_uv_colors[r + cell] = uv_colors;
8713 }8747 }
8714 }8748 }
8715 }8749 }
8750+}
8716 8751
8717 /// [`tx_size_context`] for a true `bw`x`bh` rect strip (lane-intradisp r1):8752 /// [`tx_size_context`] for a true `bw`x`bh` rect strip (lane-intradisp r1):
8718 /// `get_tx_size_context` (libaom `pred_common.h`) compares the neighbour8753 /// `get_tx_size_context` (libaom `pred_common.h`) compares the neighbour
@@ -11663,12 +11698,12 @@ fn decode_rect4_16_strip(
11663 // the left (mirrored under VERT_4).11698 // the left (mirrored under VERT_4).
11664 for cell in 0..mi_w {11699 for cell in 0..mi_w {
11665 if let Some(slot) = neighbours.above_side_mi.get_mut(lmi.1 + cell) {11700 if let Some(slot) = neighbours.above_side_mi.get_mut(lmi.1 + cell) {
11666- *slot = bw;11701+ *slot = bw as u8;
11667 }11702 }
11668 }11703 }
11669 for cell in 0..mi_h {11704 for cell in 0..mi_h {
11670 if let Some(slot) = neighbours.left_side_mi.get_mut(lmi.0 + cell) {11705 if let Some(slot) = neighbours.left_side_mi.get_mut(lmi.0 + cell) {
11671- *slot = bh;11706+ *slot = bh as u8;
11672 }11707 }
11673 }11708 }
11674 if has_chroma {11709 if has_chroma {
@@ -15744,8 +15779,8 @@ fn decode_leaf_rect8(
15744 // -- the mirror of `decode_leaf_split4`'s `{31, 31}`, which15779 // -- the mirror of `decode_leaf_split4`'s `{31, 31}`, which
15745 // `partition_ctx_mi` reads back off these side values.15780 // `partition_ctx_mi` reads back off these side values.
15746 for cell in 0..2 {15781 for cell in 0..2 {
15747- neighbours.above_side_mi[leaf_mi.1 + cell] = bw;15782+ neighbours.above_side_mi[leaf_mi.1 + cell] = bw as u8;
15748- neighbours.left_side_mi[leaf_mi.0 + cell] = bh;15783+ neighbours.left_side_mi[leaf_mi.0 + cell] = bh as u8;
15749 }15784 }
15750 let round_up_even = |n: usize| n.div_ceil(2) * 2;15785 let round_up_even = |n: usize| n.div_ceil(2) * 2;
15751 let bound_h = round_up_even(neighbours.mi_rows);15786 let bound_h = round_up_even(neighbours.mi_rows);
@@ -16542,10 +16577,10 @@ fn tx_size_context_txfm_rect(
16542 let mut above = usize::from(n.above_txfm[mi_c]) >= own_w;16577 let mut above = usize::from(n.above_txfm[mi_c]) >= own_w;
16543 let mut left = usize::from(n.left_txfm[mi_r]) >= own_h;16578 let mut left = usize::from(n.left_txfm[mi_r]) >= own_h;
16544 if has_above && n.above_inter[mi_c] {16579 if has_above && n.above_inter[mi_c] {
16545- above = n.above_side_mi[mi_c] >= own_w;16580+ above = usize::from(n.above_side_mi[mi_c]) >= own_w;
16546 }16581 }
16547 if has_left && n.left_inter[mi_r] {16582 if has_left && n.left_inter[mi_r] {
16548- left = n.left_side_mi[mi_r] >= own_h;16583+ left = usize::from(n.left_side_mi[mi_r]) >= own_h;
16549 }16584 }
16550 if crate::envflags::env_flag!("EC_TXCTX") {16585 if crate::envflags::env_flag!("EC_TXCTX") {
16551 eprintln!(16586 eprintln!(
@@ -16602,10 +16637,10 @@ fn tx_size_context_txfm(n: &Neighbours, (mi_r, mi_c): (usize, usize), side: usiz
16602 // then missed libaom's "an inter neighbour contributes its BLOCK size"16637 // then missed libaom's "an inter neighbour contributes its BLOCK size"
16603 // override and read `tx_size_cat1` row 1 where aomdec reads row 2.16638 // override and read `tx_size_cat1` row 1 where aomdec reads row 2.
16604 if has_above && n.above_inter[mi_c] {16639 if has_above && n.above_inter[mi_c] {
16605- above = n.above_side_mi[mi_c] >= side;16640+ above = usize::from(n.above_side_mi[mi_c]) >= side;
16606 }16641 }
16607 if has_left && n.left_inter[mi_r] {16642 if has_left && n.left_inter[mi_r] {
16608- left = n.left_side_mi[mi_r] >= side;16643+ left = usize::from(n.left_side_mi[mi_r]) >= side;
16609 }16644 }
16610 if crate::envflags::env_flag!("EC_TXCTX") {16645 if crate::envflags::env_flag!("EC_TXCTX") {
16611 eprintln!(16646 eprintln!(
@@ -29276,14 +29311,14 @@ fn decode_inter_sub8_rect2(
29276 if i == 1 {29311 if i == 1 {
29277 if vert {29312 if vert {
29278 for cell in 0..h_mi {29313 for cell in 0..h_mi {
29279- neighbours.left_side_mi[gr + cell] = bh;29314+ neighbours.left_side_mi[gr + cell] = bh as u8;
29280 }29315 }
29281- neighbours.above_side_mi[gc] = bw;29316+ neighbours.above_side_mi[gc] = bw as u8;
29282 } else {29317 } else {
29283 for cell in 0..w_mi {29318 for cell in 0..w_mi {
29284- neighbours.above_side_mi[gc + cell] = bw;29319+ neighbours.above_side_mi[gc + cell] = bw as u8;
29285 }29320 }
29286- neighbours.left_side_mi[gr] = bh;29321+ neighbours.left_side_mi[gr] = bh as u8;
29287 }29322 }
29288 }29323 }
29289 let (rmi, cmi) = lmi;29324 let (rmi, cmi) = lmi;
@@ -29762,8 +29797,8 @@ fn decode_inter_sub8_rect2(
29762 // over the whole 8x8 span -- BLOCK_4X8's width above and height left, the29797 // over the whole 8x8 span -- BLOCK_4X8's width above and height left, the
29763 // same values [`decode_leaf_rect8`] writes for the intra shape.29798 // same values [`decode_leaf_rect8`] writes for the intra shape.
29764 for cell in 0..2usize {29799 for cell in 0..2usize {
29765- neighbours.above_side_mi[gc + cell] = bw;29800+ neighbours.above_side_mi[gc + cell] = bw as u8;
29766- neighbours.left_side_mi[gr + cell] = bh;29801+ neighbours.left_side_mi[gr + cell] = bh as u8;
29767 }29802 }
29768 Ok(())29803 Ok(())
29769 }29804 }
@@ -35838,6 +35873,33 @@ mod tests {
35838 assert!(n.palette_uv_cache((1, 1)).is_empty());35873 assert!(n.palette_uv_cache((1, 1)).is_empty());
35839 }35874 }
35840 35875
35876+ /// lane-publish2 (the mode-info publish surface narrowed): two invariants
35877+ /// the byte-exact stream gate only covers indirectly -- a size-0 palette
35878+ /// stamp leaves the PREVIOUS block's colours in place (spec 5.11.46
35879+ /// `get_palette_cache` reads a neighbour's colours only where its size is
35880+ /// non-zero, so writing 16 bytes of zeroes was pure traffic), and the
35881+ /// narrowed `(u16, u8)` mi maps keep their "never written" sentinel.
35882+ #[test]
35883+ fn narrowed_publish_surface_keeps_neighbour_semantics() {
35884+ assert_eq!(std::mem::size_of::<(u16, u8)>(), 4, "the narrowing must actually be 4 bytes");
35885+ let fctx = &crate::decode::FrameCtx::new();
35886+ let mut n = Neighbours::new(4, 4, 16, 16, fctx);
35887+ n.record_palette_y_rect((4, 4), 16, 16, 2, [7, 9, 0, 0, 0, 0, 0, 0]);
35888+ n.record_palette_y_rect((4, 4), 16, 16, 0, [0u16; 8]);
35889+ assert_eq!(n.above_palette_size[4], 0, "the size is what a reader gates on");
35890+ assert_eq!(
35891+ n.above_palette_colors[4],
35892+ [7, 9, 0, 0, 0, 0, 0, 0],
35893+ "a size-0 stamp deliberately leaves the stale colours behind"
35894+ );
35895+ let (ctx, cache) = n.palette_ctx_and_cache((1, 1));
35896+ assert_eq!(ctx, 0, "and they stay invisible to every reader");
35897+ assert!(cache.is_empty());
35898+ assert_eq!(n.mode_above_mi(4, 4), None, "u16::MAX sentinel = never written");
35899+ n.record_mode_mi(3, 4, 1, 1, DC_PRED);
35900+ assert_eq!(n.mode_above_mi(4, 4), Some(DC_PRED));
35901+ }
35902+
35841 /// [`cdf::COMPOUND_MODE_CTX_MAP`] folded by hand against libaom's35903 /// [`cdf::COMPOUND_MODE_CTX_MAP`] folded by hand against libaom's
35842 /// `av1_mode_context_analyzer` compound branch (`mvref_common.h`):35904 /// `av1_mode_context_analyzer` compound branch (`mvref_common.h`):
35843 /// `comp_ctx = compound_mode_ctx_map[refmv_ctx >> 1][min(newmv_ctx, 4)]`35905 /// `comp_ctx = compound_mode_ctx_map[refmv_ctx >> 1][min(newmv_ctx, 4)]`