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+138 -76
@@ -6380,8 +6380,10 @@ struct Neighbours {
63806380 /// block (lane-av1-rect) share a single [`SUB`]-grid cell, so a coarse
63816381 /// array cannot tell the second leaf's partition symbol that the first
63826382 /// one was coded at 8x8.
6383- above_side_mi: Vec<usize>,
6384- left_side_mi: Vec<usize>,
6383+ /// lane-publish2: `u8` (a block side is at most 128 px, and so is
6384+ /// [`NO_NEIGHBOUR_SIDE`]) -- every block stamps these per mi.
6385+ above_side_mi: Vec<u8>,
6386+ left_side_mi: Vec<u8>,
63856387 /// The luma `mode` of the last `BLOCK_4X4` leaf decoded at each mi column
63866388 /// / row, with the mi position it was written at
63876389 /// (`(usize::MAX, _)` = never written). `above_mode`/`left_mode` are on
@@ -6393,15 +6395,20 @@ struct Neighbours {
63936395 /// and read only by [`decode_leaf_split4`]; the position guard makes it
63946396 /// fall back to the coarse slot whenever the real neighbour was a block
63956397 /// of 8x8 or larger (whose one mode is correct for every row it spans).
6396- sub8_mode_col: Vec<(usize, usize)>,
6397- sub8_mode_row: Vec<(usize, usize)>,
6398+ /// lane-publish2: `(mi position, mode)` narrowed from `(usize, usize)` to
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)>,
63986405 /// The `uv_mode` twin of `sub8_mode_col`/`sub8_mode_row` (lane-sub8 r5):
63996406 /// chroma's edge-filter type reads the CHROMA neighbour's `uv_mode`
64006407 /// (`get_intra_edge_filter_type`, reconintra.c:974), and the coarse
64016408 /// [`SUB`] slots cannot name it once a 16x16 is split into 8x8 leaves --
64026409 /// the leaf above writes the same row slot the block to the left owns.
6403- uv_mode_col: Vec<(usize, usize)>,
6404- uv_mode_row: Vec<(usize, usize)>,
6410+ uv_mode_col: Vec<(u16, u8)>,
6411+ uv_mode_row: Vec<(u16, u8)>,
64056412 /// Every mi cell's own `uv_mode`, the mi grid libaom's
64066413 /// `chroma_above_mbmi`/`chroma_left_mbmi` read directly (`set_mi_row_col`,
64076414 /// blockd.h). lane-t900 r12: the one-slot-per-column
@@ -6454,8 +6461,14 @@ struct Neighbours {
64546461 /// compound (spec `RefFrames[1]`), or `None` for single-ref/intra --
64556462 /// lane-av1comp: `mvstack::reference_mode_ctx`/`comp_reference_type_ctx`
64566463 /// need a full [`crate::mvstack::NeighbourRef`], not just `above_ref`'s
6457- /// `i8`, once a compound `read_ref_frames` lands. Always `None` today
6458- /// (no decode path here ever produces a compound block yet).
6464+ /// `i8`, once a compound `read_ref_frames` lands.
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).
64596472 above_ref1: Vec<Option<i8>>,
64606473 left_ref1: Vec<Option<i8>>,
64616474 /// The precomputed `get_comp_group_idx_context`/`get_comp_index_context`
@@ -6542,15 +6555,15 @@ struct Neighbours {
65426555 /// and its base colours -- `av1_get_palette_mode_ctx`/
65436556 /// `av1_get_palette_cache` (pred_common.c)'s neighbour lookup, lane-palette
65446557 /// r2. UV has no equivalent yet (stage 2, unreconstructed).
6545- above_palette_size: Vec<usize>,
6546- left_palette_size: Vec<usize>,
6558+ above_palette_size: Vec<u8>,
6559+ left_palette_size: Vec<u8>,
65476560 above_palette_colors: Vec<[u16; 8]>,
65486561 left_palette_colors: Vec<[u16; 8]>,
65496562 /// As the four fields above, for the neighbour's own chroma (U-channel
65506563 /// only -- `av1_get_palette_cache`'s plane-1 lookup, [`Self::palette_uv_cache`])
65516564 /// palette size/colours (lane-palette2 r1).
6552- above_palette_uv_size: Vec<usize>,
6553- left_palette_uv_size: Vec<usize>,
6565+ above_palette_uv_size: Vec<u8>,
6566+ left_palette_uv_size: Vec<u8>,
65546567 above_palette_uv_colors: Vec<[u16; 8]>,
65556568 left_palette_uv_colors: Vec<[u16; 8]>,
65566569 /// This tile's own top-left corner, in 4x4 mode-info units (spec
@@ -6590,12 +6603,12 @@ impl Neighbours {
65906603 left_uv_mode: vec![DC_PRED; rows],
65916604 above_side: vec![NO_NEIGHBOUR_SIDE; cols],
65926605 left_side: vec![NO_NEIGHBOUR_SIDE; rows],
6593- above_side_mi: vec![NO_NEIGHBOUR_SIDE; cols * (SUB / MI)],
6594- left_side_mi: vec![NO_NEIGHBOUR_SIDE; rows * (SUB / MI)],
6595- sub8_mode_col: vec![(usize::MAX, 0); cols * (SUB / MI)],
6596- sub8_mode_row: vec![(usize::MAX, 0); rows * (SUB / MI)],
6597- uv_mode_col: vec![(usize::MAX, 0); cols * (SUB / MI)],
6598- uv_mode_row: vec![(usize::MAX, 0); rows * (SUB / MI)],
6606+ above_side_mi: vec![NO_NEIGHBOUR_SIDE as u8; cols * (SUB / MI)],
6607+ left_side_mi: vec![NO_NEIGHBOUR_SIDE as u8; rows * (SUB / MI)],
6608+ sub8_mode_col: vec![(u16::MAX, 0); cols * (SUB / MI)],
6609+ sub8_mode_row: vec![(u16::MAX, 0); rows * (SUB / MI)],
6610+ uv_mode_col: vec![(u16::MAX, 0); cols * (SUB / MI)],
6611+ uv_mode_row: vec![(u16::MAX, 0); rows * (SUB / MI)],
65996612 uv_mode_grid: vec![u8::MAX; cols * (SUB / MI) * rows * (SUB / MI)],
66006613 uv_grid_cols: cols * (SUB / MI),
66016614 above_txfm: vec![TXFM_CTX_INIT; cols * (SUB / MI)],
@@ -6661,7 +6674,7 @@ impl Neighbours {
66616674 let end = col1_mi.min(self.above.len());
66626675 for i in col0_mi.min(end)..end {
66636676 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;
66656678 }
66666679 let (sc0, sc1) = (
66676680 col0_mi / (SUB / MI),
@@ -6689,8 +6702,8 @@ impl Neighbours {
66896702 // tile's first block would read as its own above neighbour.
66906703 let mcol_end = col1_mi.min(self.sub8_mode_col.len());
66916704 for i in col0_mi.min(mcol_end)..mcol_end {
6692- self.sub8_mode_col[i] = (usize::MAX, 0);
6693- self.uv_mode_col[i] = (usize::MAX, 0);
6705+ self.sub8_mode_col[i] = (u16::MAX, 0);
6706+ self.uv_mode_col[i] = (u16::MAX, 0);
66946707 }
66956708 // lane-mergefix r2: `av1_zero_above_context` also memsets the txfm row
66966709 // over the tile's own column span (to `tx_size_wide[TX_SIZES_LARGEST]`,
@@ -6735,14 +6748,21 @@ impl Neighbours {
67356748 // cache came out one entry short (class [[context-read-from-one-cell]]).
67366749 for cell in 0..(w / MI).max(1) {
67376750 if let Some(e) = self.above_palette_size.get_mut(c + cell) {
6738- *e = size;
6739- self.above_palette_colors[c + cell] = colors;
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 {
6756+ self.above_palette_colors[c + cell] = colors;
6757+ }
67406758 }
67416759 }
67426760 for cell in 0..(h / MI).max(1) {
67436761 if let Some(e) = self.left_palette_size.get_mut(r + cell) {
6744- *e = size;
6745- self.left_palette_colors[r + cell] = colors;
6762+ *e = size as u8;
6763+ if size > 0 {
6764+ self.left_palette_colors[r + cell] = colors;
6765+ }
67466766 }
67476767 }
67486768 }
@@ -6777,7 +6797,7 @@ impl Neighbours {
67776797 // `xd->above_mbmi` exists for every row but the tile's own first.
67786798 let above_is_palette = r > self.tile_row0_mi && self.above_palette_size[c] > 0;
67796799 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])
67816801 } else {
67826802 (0, [0u16; 8])
67836803 };
@@ -6788,7 +6808,7 @@ impl Neighbours {
67886808 // and its `palette_y_mode` ctx.
67896809 let left_ok = c > self.tile_col0_mi;
67906810 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])
67926812 } else {
67936813 (0, [0u16; 8])
67946814 };
@@ -6853,14 +6873,18 @@ impl Neighbours {
68536873 // mi granularity, see [`Self::record_palette_y_rect`].
68546874 for cell in 0..(w / MI).max(1) {
68556875 if let Some(e) = self.above_palette_uv_size.get_mut(c + cell) {
6856- *e = size;
6857- self.above_palette_uv_colors[c + cell] = colors;
6876+ *e = size as u8;
6877+ if size > 0 {
6878+ self.above_palette_uv_colors[c + cell] = colors;
6879+ }
68586880 }
68596881 }
68606882 for cell in 0..(h / MI).max(1) {
68616883 if let Some(e) = self.left_palette_uv_size.get_mut(r + cell) {
6862- *e = size;
6863- self.left_palette_uv_colors[r + cell] = colors;
6884+ *e = size as u8;
6885+ if size > 0 {
6886+ self.left_palette_uv_colors[r + cell] = colors;
6887+ }
68646888 }
68656889 }
68666890 }
@@ -6880,14 +6904,14 @@ impl Neighbours {
68806904 let (r, c) = mi;
68816905 let above_ok = r % 16 != 0;
68826906 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])
68846908 } else {
68856909 (0, [0u16; 8])
68866910 };
68876911 // Same tile-relative left availability as the luma cache above.
68886912 let left_ok = c > self.tile_col0_mi;
68896913 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])
68916915 } else {
68926916 (0, [0u16; 8])
68936917 };
@@ -7087,7 +7111,7 @@ impl Neighbours {
70877111 self.left_mode.iter_mut().for_each(|m| *m = DC_PRED);
70887112 self.left_uv_mode.iter_mut().for_each(|m| *m = DC_PRED);
70897113 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);
70917115 self.left_skip.iter_mut().for_each(|s| *s = false);
70927116 self.left_skip_mode.iter_mut().for_each(|s| *s = false);
70937117 self.left_inter.iter_mut().for_each(|i| *i = false);
@@ -7098,8 +7122,8 @@ impl Neighbours {
70987122 self.left_filter.iter_mut().for_each(|f| *f = [3u8; 2]);
70997123 // lane-sub8 r6: mi-granular row maps reset with the rest of the left
71007124 // context (a tile/SB-row boundary has no left neighbour).
7101- self.sub8_mode_row.iter_mut().for_each(|s| *s = (usize::MAX, 0));
7102- self.uv_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));
7126+ self.uv_mode_row.iter_mut().for_each(|s| *s = (u16::MAX, 0));
71037127 // lane-tiles r11 (COMMON's NEIGHBOUR MAPS rule): the palette side bands
71047128 // are per-mi maps like every other one here, so they reset with the
71057129 // row too -- the left guard above is what actually stops a cross-tile
@@ -7207,9 +7231,9 @@ impl Neighbours {
72077231 self.above_skip_mode[c..c + w_mi].fill(skip_mode);
72087232 self.above_inter[c..c + w_mi].fill(is_inter);
72097233 self.above_ref[c..c + w_mi].fill(ref_frame);
7210- // lane-av1comp: no caller of `record_inter` ever decodes a
7211- // compound block yet (see [`Self::above_ref1`]'s doc); the
7212- // parameter this becomes lands with `read_ref_frames`.
7234+ // The single-ref default: `record_compound_ctx_rect_mi` overwrites
7235+ // this with the block's real second reference for a compound block,
7236+ // called right after this one (lane-publish2 audit).
72137237 self.above_ref1[c..c + w_mi].fill(None);
72147238 // libaom `get_comp_group_idx_context`/`get_comp_index_context`'s
72157239 // single-ref special case: `ref_frame[0] == ALTREF_FRAME` reads
@@ -7315,8 +7339,8 @@ impl Neighbours {
73157339 /// arrays so the second leaf sees the first leaf's own 8x8 side rather
73167340 /// than the enclosing 16x16 slot's stale, shared state.
73177341 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)
7319- + usize::from(self.above_side_mi[mi_c] * 2 <= side)
7342+ 2 * usize::from(usize::from(self.left_side_mi[mi_r]) * 2 <= side)
7343+ + usize::from(usize::from(self.above_side_mi[mi_c]) * 2 <= side)
73207344 }
73217345
73227346 /// The gathered coded/DC-sign state of the blocks above and to the left
@@ -7476,7 +7500,8 @@ impl Neighbours {
74767500 mi_h: usize,
74777501 mode: usize,
74787502 ) {
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;
74807505 for cell in 0..mi_w {
74817506 if let Some(slot) = self.sub8_mode_col.get_mut(mi_c + cell) {
74827507 *slot = (last_r, mode);
@@ -7495,14 +7520,14 @@ impl Neighbours {
74957520 /// construction). See [`Self::record_mode_mi`].
74967521 fn mode_above_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> {
74977522 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)),
74997524 _ => None,
75007525 }
75017526 }
75027527
75037528 fn mode_left_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> {
75047529 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)),
75067531 _ => None,
75077532 }
75087533 }
@@ -7518,8 +7543,8 @@ impl Neighbours {
75187543 uv_mode: usize,
75197544 ) {
75207545 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));
7522- fill_span(&mut self.uv_mode_row, mi_r, mi_h, (last_c, uv_mode));
7546+ fill_span(&mut self.uv_mode_col, mi_c, mi_w, (last_r as u16, uv_mode as u8));
7547+ fill_span(&mut self.uv_mode_row, mi_r, mi_h, (last_c as u16, uv_mode as u8));
75237548 // libaom writes one `MB_MODE_INFO` pointer into every mi cell a block
75247549 // covers, so the chroma neighbour read below is a plain grid read.
75257550 let stride = self.uv_grid_cols;
@@ -7590,20 +7615,20 @@ impl Neighbours {
75907615 };
75917616 let above = match self.uv_mode_col.get(mi_c) {
75927617 _ 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),
75947619 _ if have_above => self.above_uv_mode[c],
75957620 _ => DC_PRED,
75967621 };
75977622 let left = match self.uv_mode_row.get(mi_r) {
75987623 _ 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),
76007625 _ if have_left => self.left_uv_mode[r],
76017626 _ => DC_PRED,
76027627 };
76037628 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)
76057630 }) || 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)
76077632 }) {
76087633 UV_MODE_GRID_OVERRIDE_HITS.with(|h| h.set(h.get() + 1));
76097634 }
@@ -7762,8 +7787,8 @@ impl Neighbours {
77627787 if crate::envflags::env_flag!("EC_ECPUB") {
77637788 eprintln!("EC_ECPUB blk mi=({mi_r},{mi_c}) wh=({w},{h}) lvl={}", states[0].level);
77647789 }
7765- fill_span(&mut self.left_side_mi, mi_r, h_mi, h);
7766- fill_span(&mut self.above_side_mi, mi_c, w_mi, w);
7790+ fill_span(&mut self.left_side_mi, mi_r, h_mi, h as u8);
7791+ fill_span(&mut self.above_side_mi, mi_c, w_mi, w as u8);
77677792 // A chroma 4x4 unit straddling the true luma edge is still whole in
77687793 // chroma's own halved grid, so libaom rounds the luma bound up to the
77697794 // plane's own 4x4 unit before clamping a subsampled plane
@@ -7952,8 +7977,8 @@ impl Neighbours {
79527977 let (w_mi, h_mi) = (w / MI, h / MI);
79537978 self.record_mode_mi(mi_r, mi_c, w_mi, h_mi, mode);
79547979 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);
7956- fill_span(&mut self.above_side_mi, mi_c, w_mi, w);
7980+ fill_span(&mut self.left_side_mi, mi_r, h_mi, h as u8);
7981+ fill_span(&mut self.above_side_mi, mi_c, w_mi, w as u8);
79577982 let round_up_even = |n: usize| n.div_ceil(2) * 2;
79587983 let (bound_h, bound_w) = (round_up_even(self.mi_rows), round_up_even(self.mi_cols));
79597984 for (plane_idx, grid) in chroma_grids.into_iter().enumerate() {
@@ -8694,10 +8719,16 @@ fn record_strip_palette(
86948719 {
86958720 for cell in 0..(bw / MI).max(1) {
86968721 if let Some(e) = n.above_palette_size.get_mut(c + cell) {
8697- *e = y_size;
8698- n.above_palette_colors[c + cell] = y_colors;
8699- n.above_palette_uv_size[c + cell] = uv_size;
8700- n.above_palette_uv_colors[c + cell] = uv_colors;
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 {
8727+ n.above_palette_colors[c + cell] = y_colors;
8728+ }
8729+ if uv_size > 0 {
8730+ n.above_palette_uv_colors[c + cell] = uv_colors;
8731+ }
87018732 }
87028733 }
87038734 }
@@ -8706,10 +8737,14 @@ fn record_strip_palette(
87068737 if n.left_palette_size.get(r + cell).is_none() {
87078738 break;
87088739 }
8709- n.left_palette_size[r + cell] = y_size;
8710- n.left_palette_colors[r + cell] = y_colors;
8711- n.left_palette_uv_size[r + cell] = uv_size;
8712- n.left_palette_uv_colors[r + cell] = uv_colors;
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 {
8743+ n.left_palette_colors[r + cell] = y_colors;
8744+ }
8745+ if uv_size > 0 {
8746+ n.left_palette_uv_colors[r + cell] = uv_colors;
8747+ }
87138748 }
87148749 }
87158750 }
@@ -11663,12 +11698,12 @@ fn decode_rect4_16_strip(
1166311698 // the left (mirrored under VERT_4).
1166411699 for cell in 0..mi_w {
1166511700 if let Some(slot) = neighbours.above_side_mi.get_mut(lmi.1 + cell) {
11666- *slot = bw;
11701+ *slot = bw as u8;
1166711702 }
1166811703 }
1166911704 for cell in 0..mi_h {
1167011705 if let Some(slot) = neighbours.left_side_mi.get_mut(lmi.0 + cell) {
11671- *slot = bh;
11706+ *slot = bh as u8;
1167211707 }
1167311708 }
1167411709 if has_chroma {
@@ -15744,8 +15779,8 @@ fn decode_leaf_rect8(
1574415779 // -- the mirror of `decode_leaf_split4`'s `{31, 31}`, which
1574515780 // `partition_ctx_mi` reads back off these side values.
1574615781 for cell in 0..2 {
15747- neighbours.above_side_mi[leaf_mi.1 + cell] = bw;
15748- neighbours.left_side_mi[leaf_mi.0 + cell] = bh;
15782+ neighbours.above_side_mi[leaf_mi.1 + cell] = bw as u8;
15783+ neighbours.left_side_mi[leaf_mi.0 + cell] = bh as u8;
1574915784 }
1575015785 let round_up_even = |n: usize| n.div_ceil(2) * 2;
1575115786 let bound_h = round_up_even(neighbours.mi_rows);
@@ -16542,10 +16577,10 @@ fn tx_size_context_txfm_rect(
1654216577 let mut above = usize::from(n.above_txfm[mi_c]) >= own_w;
1654316578 let mut left = usize::from(n.left_txfm[mi_r]) >= own_h;
1654416579 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;
1654616581 }
1654716582 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;
1654916584 }
1655016585 if crate::envflags::env_flag!("EC_TXCTX") {
1655116586 eprintln!(
@@ -16602,10 +16637,10 @@ fn tx_size_context_txfm(n: &Neighbours, (mi_r, mi_c): (usize, usize), side: usiz
1660216637 // then missed libaom's "an inter neighbour contributes its BLOCK size"
1660316638 // override and read `tx_size_cat1` row 1 where aomdec reads row 2.
1660416639 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;
1660616641 }
1660716642 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;
1660916644 }
1661016645 if crate::envflags::env_flag!("EC_TXCTX") {
1661116646 eprintln!(
@@ -29276,14 +29311,14 @@ fn decode_inter_sub8_rect2(
2927629311 if i == 1 {
2927729312 if vert {
2927829313 for cell in 0..h_mi {
29279- neighbours.left_side_mi[gr + cell] = bh;
29314+ neighbours.left_side_mi[gr + cell] = bh as u8;
2928029315 }
29281- neighbours.above_side_mi[gc] = bw;
29316+ neighbours.above_side_mi[gc] = bw as u8;
2928229317 } else {
2928329318 for cell in 0..w_mi {
29284- neighbours.above_side_mi[gc + cell] = bw;
29319+ neighbours.above_side_mi[gc + cell] = bw as u8;
2928529320 }
29286- neighbours.left_side_mi[gr] = bh;
29321+ neighbours.left_side_mi[gr] = bh as u8;
2928729322 }
2928829323 }
2928929324 let (rmi, cmi) = lmi;
@@ -29762,8 +29797,8 @@ fn decode_inter_sub8_rect2(
2976229797 // over the whole 8x8 span -- BLOCK_4X8's width above and height left, the
2976329798 // same values [`decode_leaf_rect8`] writes for the intra shape.
2976429799 for cell in 0..2usize {
29765- neighbours.above_side_mi[gc + cell] = bw;
29766- neighbours.left_side_mi[gr + cell] = bh;
29800+ neighbours.above_side_mi[gc + cell] = bw as u8;
29801+ neighbours.left_side_mi[gr + cell] = bh as u8;
2976729802 }
2976829803 Ok(())
2976929804 }
@@ -35838,6 +35873,33 @@ mod tests {
3583835873 assert!(n.palette_uv_cache((1, 1)).is_empty());
3583935874 }
3584035875
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+
3584135903 /// [`cdf::COMPOUND_MODE_CTX_MAP`] folded by hand against libaom's
3584235904 /// `av1_mode_context_analyzer` compound branch (`mvref_common.h`):
3584335905 /// `comp_ctx = compound_mode_ctx_map[refmv_ctx >> 1][min(newmv_ctx, 4)]`