forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-publish2' (3737303f): palette colour caches stamped only by palette blocks (spec reads them only where size > 0), sub-8 and uv mode edge arrays narrowed to (u16,u8), side and palette-size bands to u8; second-reference edge array audited as correct (stale comments fixed); byte-exact, -0.13% instructions

Browse files at this commit

b032d070b0c354061d4b44ef59d4c4aabff49b1fAhmet Kaan GümüşParent: 59bce73ce0464d
crates/ec-av1/src/decode.rsmodified+138 -76
@@ -6388,8 +6388,10 @@ struct Neighbours {
6388 /// block (lane-av1-rect) share a single [`SUB`]-grid cell, so a coarse6388 /// block (lane-av1-rect) share a single [`SUB`]-grid cell, so a coarse
6389 /// array cannot tell the second leaf's partition symbol that the first6389 /// array cannot tell the second leaf's partition symbol that the first
6390 /// one was coded at 8x8.6390 /// one was coded at 8x8.
6391- above_side_mi: Vec<usize>,6391+ /// lane-publish2: `u8` (a block side is at most 128 px, and so is
6392- left_side_mi: Vec<usize>,6392+ /// [`NO_NEIGHBOUR_SIDE`]) -- every block stamps these per mi.
6393+ above_side_mi: Vec<u8>,
6394+ left_side_mi: Vec<u8>,
6393 /// The luma `mode` of the last `BLOCK_4X4` leaf decoded at each mi column6395 /// The luma `mode` of the last `BLOCK_4X4` leaf decoded at each mi column
6394 /// / row, with the mi position it was written at6396 /// / row, with the mi position it was written at
6395 /// (`(usize::MAX, _)` = never written). `above_mode`/`left_mode` are on6397 /// (`(usize::MAX, _)` = never written). `above_mode`/`left_mode` are on
@@ -6401,15 +6403,20 @@ struct Neighbours {
6401 /// and read only by [`decode_leaf_split4`]; the position guard makes it6403 /// and read only by [`decode_leaf_split4`]; the position guard makes it
6402 /// fall back to the coarse slot whenever the real neighbour was a block6404 /// fall back to the coarse slot whenever the real neighbour was a block
6403 /// of 8x8 or larger (whose one mode is correct for every row it spans).6405 /// of 8x8 or larger (whose one mode is correct for every row it spans).
6404- sub8_mode_col: Vec<(usize, usize)>,6406+ /// lane-publish2: `(mi position, mode)` narrowed from `(usize, usize)` to
6405- sub8_mode_row: Vec<(usize, usize)>,6407+ /// `(u16, u8)` -- every block stamps these mi-granular maps, and 16 bytes
6408+ /// per mi per side of publish traffic was pure width. `u16::MAX` is the
6409+ /// "never written" sentinel (an mi index cannot reach it: AV1's largest
6410+ /// frame is 65536 px = 16384 mi).
6411+ sub8_mode_col: Vec<(u16, u8)>,
6412+ sub8_mode_row: Vec<(u16, u8)>,
6406 /// The `uv_mode` twin of `sub8_mode_col`/`sub8_mode_row` (lane-sub8 r5):6413 /// The `uv_mode` twin of `sub8_mode_col`/`sub8_mode_row` (lane-sub8 r5):
6407 /// chroma's edge-filter type reads the CHROMA neighbour's `uv_mode`6414 /// chroma's edge-filter type reads the CHROMA neighbour's `uv_mode`
6408 /// (`get_intra_edge_filter_type`, reconintra.c:974), and the coarse6415 /// (`get_intra_edge_filter_type`, reconintra.c:974), and the coarse
6409 /// [`SUB`] slots cannot name it once a 16x16 is split into 8x8 leaves --6416 /// [`SUB`] slots cannot name it once a 16x16 is split into 8x8 leaves --
6410 /// the leaf above writes the same row slot the block to the left owns.6417 /// the leaf above writes the same row slot the block to the left owns.
6411- uv_mode_col: Vec<(usize, usize)>,6418+ uv_mode_col: Vec<(u16, u8)>,
6412- uv_mode_row: Vec<(usize, usize)>,6419+ uv_mode_row: Vec<(u16, u8)>,
6413 /// Every mi cell's own `uv_mode`, the mi grid libaom's6420 /// Every mi cell's own `uv_mode`, the mi grid libaom's
6414 /// `chroma_above_mbmi`/`chroma_left_mbmi` read directly (`set_mi_row_col`,6421 /// `chroma_above_mbmi`/`chroma_left_mbmi` read directly (`set_mi_row_col`,
6415 /// blockd.h). lane-t900 r12: the one-slot-per-column6422 /// blockd.h). lane-t900 r12: the one-slot-per-column
@@ -6462,8 +6469,14 @@ struct Neighbours {
6462 /// compound (spec `RefFrames[1]`), or `None` for single-ref/intra --6469 /// compound (spec `RefFrames[1]`), or `None` for single-ref/intra --
6463 /// lane-av1comp: `mvstack::reference_mode_ctx`/`comp_reference_type_ctx`6470 /// lane-av1comp: `mvstack::reference_mode_ctx`/`comp_reference_type_ctx`
6464 /// need a full [`crate::mvstack::NeighbourRef`], not just `above_ref`'s6471 /// need a full [`crate::mvstack::NeighbourRef`], not just `above_ref`'s
6465- /// `i8`, once a compound `read_ref_frames` lands. Always `None` today6472+ /// `i8`, once a compound `read_ref_frames` lands.
6466- /// (no decode path here ever produces a compound block yet).6473+ /// lane-publish2 audit: the "always `None` today" note this doc used to
6474+ /// carry is long stale -- compound blocks do occur, and
6475+ /// [`Self::record_compound_ctx_rect_mi`] stamps the real second reference
6476+ /// over their span right after [`Self::record_inter_rect_mi`]'s reset.
6477+ /// The array IS read: `single_ref_p*`/`comp_ref` contexts and every
6478+ /// [`crate::mvstack::NeighbourRef`] construction (class
6479+ /// [[branch-dropped-as-unreachable]] -- the note, not the code, was wrong).
6467 above_ref1: Vec<Option<i8>>,6480 above_ref1: Vec<Option<i8>>,
6468 left_ref1: Vec<Option<i8>>,6481 left_ref1: Vec<Option<i8>>,
6469 /// The precomputed `get_comp_group_idx_context`/`get_comp_index_context`6482 /// The precomputed `get_comp_group_idx_context`/`get_comp_index_context`
@@ -6550,15 +6563,15 @@ struct Neighbours {
6550 /// and its base colours -- `av1_get_palette_mode_ctx`/6563 /// and its base colours -- `av1_get_palette_mode_ctx`/
6551 /// `av1_get_palette_cache` (pred_common.c)'s neighbour lookup, lane-palette6564 /// `av1_get_palette_cache` (pred_common.c)'s neighbour lookup, lane-palette
6552 /// r2. UV has no equivalent yet (stage 2, unreconstructed).6565 /// r2. UV has no equivalent yet (stage 2, unreconstructed).
6553- above_palette_size: Vec<usize>,6566+ above_palette_size: Vec<u8>,
6554- left_palette_size: Vec<usize>,6567+ left_palette_size: Vec<u8>,
6555 above_palette_colors: Vec<[u16; 8]>,6568 above_palette_colors: Vec<[u16; 8]>,
6556 left_palette_colors: Vec<[u16; 8]>,6569 left_palette_colors: Vec<[u16; 8]>,
6557 /// As the four fields above, for the neighbour's own chroma (U-channel6570 /// As the four fields above, for the neighbour's own chroma (U-channel
6558 /// only -- `av1_get_palette_cache`'s plane-1 lookup, [`Self::palette_uv_cache`])6571 /// only -- `av1_get_palette_cache`'s plane-1 lookup, [`Self::palette_uv_cache`])
6559 /// palette size/colours (lane-palette2 r1).6572 /// palette size/colours (lane-palette2 r1).
6560- above_palette_uv_size: Vec<usize>,6573+ above_palette_uv_size: Vec<u8>,
6561- left_palette_uv_size: Vec<usize>,6574+ left_palette_uv_size: Vec<u8>,
6562 above_palette_uv_colors: Vec<[u16; 8]>,6575 above_palette_uv_colors: Vec<[u16; 8]>,
6563 left_palette_uv_colors: Vec<[u16; 8]>,6576 left_palette_uv_colors: Vec<[u16; 8]>,
6564 /// This tile's own top-left corner, in 4x4 mode-info units (spec6577 /// This tile's own top-left corner, in 4x4 mode-info units (spec
@@ -6598,12 +6611,12 @@ impl Neighbours {
6598 left_uv_mode: vec![DC_PRED; rows],6611 left_uv_mode: vec![DC_PRED; rows],
6599 above_side: vec![NO_NEIGHBOUR_SIDE; cols],6612 above_side: vec![NO_NEIGHBOUR_SIDE; cols],
6600 left_side: vec![NO_NEIGHBOUR_SIDE; rows],6613 left_side: vec![NO_NEIGHBOUR_SIDE; rows],
6601- above_side_mi: vec![NO_NEIGHBOUR_SIDE; cols * (SUB / MI)],6614+ above_side_mi: vec![NO_NEIGHBOUR_SIDE as u8; cols * (SUB / MI)],
6602- left_side_mi: vec![NO_NEIGHBOUR_SIDE; rows * (SUB / MI)],6615+ left_side_mi: vec![NO_NEIGHBOUR_SIDE as u8; rows * (SUB / MI)],
6603- sub8_mode_col: vec![(usize::MAX, 0); cols * (SUB / MI)],6616+ sub8_mode_col: vec![(u16::MAX, 0); cols * (SUB / MI)],
6604- sub8_mode_row: vec![(usize::MAX, 0); rows * (SUB / MI)],6617+ sub8_mode_row: vec![(u16::MAX, 0); rows * (SUB / MI)],
6605- uv_mode_col: vec![(usize::MAX, 0); cols * (SUB / MI)],6618+ uv_mode_col: vec![(u16::MAX, 0); cols * (SUB / MI)],
6606- uv_mode_row: vec![(usize::MAX, 0); rows * (SUB / MI)],6619+ uv_mode_row: vec![(u16::MAX, 0); rows * (SUB / MI)],
6607 uv_mode_grid: vec![u8::MAX; cols * (SUB / MI) * rows * (SUB / MI)],6620 uv_mode_grid: vec![u8::MAX; cols * (SUB / MI) * rows * (SUB / MI)],
6608 uv_grid_cols: cols * (SUB / MI),6621 uv_grid_cols: cols * (SUB / MI),
6609 above_txfm: vec![TXFM_CTX_INIT; cols * (SUB / MI)],6622 above_txfm: vec![TXFM_CTX_INIT; cols * (SUB / MI)],
@@ -6669,7 +6682,7 @@ impl Neighbours {
6669 let end = col1_mi.min(self.above.len());6682 let end = col1_mi.min(self.above.len());
6670 for i in col0_mi.min(end)..end {6683 for i in col0_mi.min(end)..end {
6671 self.above[i] = Default::default();6684 self.above[i] = Default::default();
6672- self.above_side_mi[i] = NO_NEIGHBOUR_SIDE;6685+ self.above_side_mi[i] = NO_NEIGHBOUR_SIDE as u8;
6673 }6686 }
6674 let (sc0, sc1) = (6687 let (sc0, sc1) = (
6675 col0_mi / (SUB / MI),6688 col0_mi / (SUB / MI),
@@ -6697,8 +6710,8 @@ impl Neighbours {
6697 // tile's first block would read as its own above neighbour.6710 // tile's first block would read as its own above neighbour.
6698 let mcol_end = col1_mi.min(self.sub8_mode_col.len());6711 let mcol_end = col1_mi.min(self.sub8_mode_col.len());
6699 for i in col0_mi.min(mcol_end)..mcol_end {6712 for i in col0_mi.min(mcol_end)..mcol_end {
6700- self.sub8_mode_col[i] = (usize::MAX, 0);6713+ self.sub8_mode_col[i] = (u16::MAX, 0);
6701- self.uv_mode_col[i] = (usize::MAX, 0);6714+ self.uv_mode_col[i] = (u16::MAX, 0);
6702 }6715 }
6703 // lane-mergefix r2: `av1_zero_above_context` also memsets the txfm row6716 // lane-mergefix r2: `av1_zero_above_context` also memsets the txfm row
6704 // over the tile's own column span (to `tx_size_wide[TX_SIZES_LARGEST]`,6717 // over the tile's own column span (to `tx_size_wide[TX_SIZES_LARGEST]`,
@@ -6743,14 +6756,21 @@ impl Neighbours {
6743 // cache came out one entry short (class [[context-read-from-one-cell]]).6756 // cache came out one entry short (class [[context-read-from-one-cell]]).
6744 for cell in 0..(w / MI).max(1) {6757 for cell in 0..(w / MI).max(1) {
6745 if let Some(e) = self.above_palette_size.get_mut(c + cell) {6758 if let Some(e) = self.above_palette_size.get_mut(c + cell) {
6746- *e = size;6759+ *e = size as u8;
6747- self.above_palette_colors[c + cell] = colors;6760+ // lane-publish2: spec 5.11.46 `get_palette_cache` reads a
6761+ // neighbour's colours only where its palette size > 0, so a
6762+ // non-palette block need not stamp 16 bytes of zeroes here.
6763+ if size > 0 {
6764+ self.above_palette_colors[c + cell] = colors;
6765+ }
6748 }6766 }
6749 }6767 }
6750 for cell in 0..(h / MI).max(1) {6768 for cell in 0..(h / MI).max(1) {
6751 if let Some(e) = self.left_palette_size.get_mut(r + cell) {6769 if let Some(e) = self.left_palette_size.get_mut(r + cell) {
6752- *e = size;6770+ *e = size as u8;
6753- self.left_palette_colors[r + cell] = colors;6771+ if size > 0 {
6772+ self.left_palette_colors[r + cell] = colors;
6773+ }
6754 }6774 }
6755 }6775 }
6756 }6776 }
@@ -6785,7 +6805,7 @@ impl Neighbours {
6785 // `xd->above_mbmi` exists for every row but the tile's own first.6805 // `xd->above_mbmi` exists for every row but the tile's own first.
6786 let above_is_palette = r > self.tile_row0_mi && self.above_palette_size[c] > 0;6806 let above_is_palette = r > self.tile_row0_mi && self.above_palette_size[c] > 0;
6787 let (above_n, above_colors) = if above_ok && self.above_palette_size[c] > 0 {6807 let (above_n, above_colors) = if above_ok && self.above_palette_size[c] > 0 {
6788- (self.above_palette_size[c], self.above_palette_colors[c])6808+ (usize::from(self.above_palette_size[c]), self.above_palette_colors[c])
6789 } else {6809 } else {
6790 (0, [0u16; 8])6810 (0, [0u16; 8])
6791 };6811 };
@@ -6796,7 +6816,7 @@ impl Neighbours {
6796 // and its `palette_y_mode` ctx.6816 // and its `palette_y_mode` ctx.
6797 let left_ok = c > self.tile_col0_mi;6817 let left_ok = c > self.tile_col0_mi;
6798 let (left_n, left_colors) = if left_ok && self.left_palette_size[r] > 0 {6818 let (left_n, left_colors) = if left_ok && self.left_palette_size[r] > 0 {
6799- (self.left_palette_size[r], self.left_palette_colors[r])6819+ (usize::from(self.left_palette_size[r]), self.left_palette_colors[r])
6800 } else {6820 } else {
6801 (0, [0u16; 8])6821 (0, [0u16; 8])
6802 };6822 };
@@ -6861,14 +6881,18 @@ impl Neighbours {
6861 // mi granularity, see [`Self::record_palette_y_rect`].6881 // mi granularity, see [`Self::record_palette_y_rect`].
6862 for cell in 0..(w / MI).max(1) {6882 for cell in 0..(w / MI).max(1) {
6863 if let Some(e) = self.above_palette_uv_size.get_mut(c + cell) {6883 if let Some(e) = self.above_palette_uv_size.get_mut(c + cell) {
6864- *e = size;6884+ *e = size as u8;
6865- self.above_palette_uv_colors[c + cell] = colors;6885+ if size > 0 {
6886+ self.above_palette_uv_colors[c + cell] = colors;
6887+ }
6866 }6888 }
6867 }6889 }
6868 for cell in 0..(h / MI).max(1) {6890 for cell in 0..(h / MI).max(1) {
6869 if let Some(e) = self.left_palette_uv_size.get_mut(r + cell) {6891 if let Some(e) = self.left_palette_uv_size.get_mut(r + cell) {
6870- *e = size;6892+ *e = size as u8;
6871- self.left_palette_uv_colors[r + cell] = colors;6893+ if size > 0 {
6894+ self.left_palette_uv_colors[r + cell] = colors;
6895+ }
6872 }6896 }
6873 }6897 }
6874 }6898 }
@@ -6888,14 +6912,14 @@ impl Neighbours {
6888 let (r, c) = mi;6912 let (r, c) = mi;
6889 let above_ok = r % 16 != 0;6913 let above_ok = r % 16 != 0;
6890 let (above_n, above_colors) = if above_ok && self.above_palette_uv_size[c] > 0 {6914 let (above_n, above_colors) = if above_ok && self.above_palette_uv_size[c] > 0 {
6891- (self.above_palette_uv_size[c], self.above_palette_uv_colors[c])6915+ (usize::from(self.above_palette_uv_size[c]), self.above_palette_uv_colors[c])
6892 } else {6916 } else {
6893 (0, [0u16; 8])6917 (0, [0u16; 8])
6894 };6918 };
6895 // Same tile-relative left availability as the luma cache above.6919 // Same tile-relative left availability as the luma cache above.
6896 let left_ok = c > self.tile_col0_mi;6920 let left_ok = c > self.tile_col0_mi;
6897 let (left_n, left_colors) = if left_ok && self.left_palette_uv_size[r] > 0 {6921 let (left_n, left_colors) = if left_ok && self.left_palette_uv_size[r] > 0 {
6898- (self.left_palette_uv_size[r], self.left_palette_uv_colors[r])6922+ (usize::from(self.left_palette_uv_size[r]), self.left_palette_uv_colors[r])
6899 } else {6923 } else {
6900 (0, [0u16; 8])6924 (0, [0u16; 8])
6901 };6925 };
@@ -7095,7 +7119,7 @@ impl Neighbours {
7095 self.left_mode.iter_mut().for_each(|m| *m = DC_PRED);7119 self.left_mode.iter_mut().for_each(|m| *m = DC_PRED);
7096 self.left_uv_mode.iter_mut().for_each(|m| *m = DC_PRED);7120 self.left_uv_mode.iter_mut().for_each(|m| *m = DC_PRED);
7097 self.left_side.iter_mut().for_each(|s| *s = NO_NEIGHBOUR_SIDE);7121 self.left_side.iter_mut().for_each(|s| *s = NO_NEIGHBOUR_SIDE);
7098- self.left_side_mi.iter_mut().for_each(|s| *s = NO_NEIGHBOUR_SIDE);7122+ self.left_side_mi.iter_mut().for_each(|s| *s = NO_NEIGHBOUR_SIDE as u8);
7099 self.left_skip.iter_mut().for_each(|s| *s = false);7123 self.left_skip.iter_mut().for_each(|s| *s = false);
7100 self.left_skip_mode.iter_mut().for_each(|s| *s = false);7124 self.left_skip_mode.iter_mut().for_each(|s| *s = false);
7101 self.left_inter.iter_mut().for_each(|i| *i = false);7125 self.left_inter.iter_mut().for_each(|i| *i = false);
@@ -7106,8 +7130,8 @@ impl Neighbours {
7106 self.left_filter.iter_mut().for_each(|f| *f = [3u8; 2]);7130 self.left_filter.iter_mut().for_each(|f| *f = [3u8; 2]);
7107 // lane-sub8 r6: mi-granular row maps reset with the rest of the left7131 // lane-sub8 r6: mi-granular row maps reset with the rest of the left
7108 // context (a tile/SB-row boundary has no left neighbour).7132 // context (a tile/SB-row boundary has no left neighbour).
7109- self.sub8_mode_row.iter_mut().for_each(|s| *s = (usize::MAX, 0));7133+ self.sub8_mode_row.iter_mut().for_each(|s| *s = (u16::MAX, 0));
7110- self.uv_mode_row.iter_mut().for_each(|s| *s = (usize::MAX, 0));7134+ self.uv_mode_row.iter_mut().for_each(|s| *s = (u16::MAX, 0));
7111 // lane-tiles r11 (COMMON's NEIGHBOUR MAPS rule): the palette side bands7135 // lane-tiles r11 (COMMON's NEIGHBOUR MAPS rule): the palette side bands
7112 // are per-mi maps like every other one here, so they reset with the7136 // are per-mi maps like every other one here, so they reset with the
7113 // row too -- the left guard above is what actually stops a cross-tile7137 // row too -- the left guard above is what actually stops a cross-tile
@@ -7215,9 +7239,9 @@ impl Neighbours {
7215 self.above_skip_mode[c..c + w_mi].fill(skip_mode);7239 self.above_skip_mode[c..c + w_mi].fill(skip_mode);
7216 self.above_inter[c..c + w_mi].fill(is_inter);7240 self.above_inter[c..c + w_mi].fill(is_inter);
7217 self.above_ref[c..c + w_mi].fill(ref_frame);7241 self.above_ref[c..c + w_mi].fill(ref_frame);
7218- // lane-av1comp: no caller of `record_inter` ever decodes a7242+ // The single-ref default: `record_compound_ctx_rect_mi` overwrites
7219- // compound block yet (see [`Self::above_ref1`]'s doc); the7243+ // this with the block's real second reference for a compound block,
7220- // parameter this becomes lands with `read_ref_frames`.7244+ // called right after this one (lane-publish2 audit).
7221 self.above_ref1[c..c + w_mi].fill(None);7245 self.above_ref1[c..c + w_mi].fill(None);
7222 // libaom `get_comp_group_idx_context`/`get_comp_index_context`'s7246 // libaom `get_comp_group_idx_context`/`get_comp_index_context`'s
7223 // single-ref special case: `ref_frame[0] == ALTREF_FRAME` reads7247 // single-ref special case: `ref_frame[0] == ALTREF_FRAME` reads
@@ -7323,8 +7347,8 @@ impl Neighbours {
7323 /// arrays so the second leaf sees the first leaf's own 8x8 side rather7347 /// arrays so the second leaf sees the first leaf's own 8x8 side rather
7324 /// than the enclosing 16x16 slot's stale, shared state.7348 /// than the enclosing 16x16 slot's stale, shared state.
7325 fn partition_ctx_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> usize {7349 fn partition_ctx_mi(&self, (mi_r, mi_c): (usize, usize), side: usize) -> usize {
7326- 2 * usize::from(self.left_side_mi[mi_r] * 2 <= side)7350+ 2 * usize::from(usize::from(self.left_side_mi[mi_r]) * 2 <= side)
7327- + usize::from(self.above_side_mi[mi_c] * 2 <= side)7351+ + usize::from(usize::from(self.above_side_mi[mi_c]) * 2 <= side)
7328 }7352 }
7329 7353
7330 /// The gathered coded/DC-sign state of the blocks above and to the left7354 /// The gathered coded/DC-sign state of the blocks above and to the left
@@ -7484,7 +7508,8 @@ impl Neighbours {
7484 mi_h: usize,7508 mi_h: usize,
7485 mode: usize,7509 mode: usize,
7486 ) {7510 ) {
7487- let (last_r, last_c) = (mi_r + mi_h - 1, mi_c + mi_w - 1);7511+ let (last_r, last_c) = ((mi_r + mi_h - 1) as u16, (mi_c + mi_w - 1) as u16);
7512+ let mode = mode as u8;
7488 for cell in 0..mi_w {7513 for cell in 0..mi_w {
7489 if let Some(slot) = self.sub8_mode_col.get_mut(mi_c + cell) {7514 if let Some(slot) = self.sub8_mode_col.get_mut(mi_c + cell) {
7490 *slot = (last_r, mode);7515 *slot = (last_r, mode);
@@ -7503,14 +7528,14 @@ impl Neighbours {
7503 /// construction). See [`Self::record_mode_mi`].7528 /// construction). See [`Self::record_mode_mi`].
7504 fn mode_above_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> {7529 fn mode_above_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> {
7505 match self.sub8_mode_col.get(mi_c) {7530 match self.sub8_mode_col.get(mi_c) {
7506- Some(&(row, m)) if mi_r > self.tile_row0_mi && row == mi_r - 1 => Some(m),7531+ Some(&(row, m)) if mi_r > self.tile_row0_mi && usize::from(row) == mi_r - 1 => Some(usize::from(m)),
7507 _ => None,7532 _ => None,
7508 }7533 }
7509 }7534 }
7510 7535
7511 fn mode_left_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> {7536 fn mode_left_mi(&self, mi_r: usize, mi_c: usize) -> Option<usize> {
7512 match self.sub8_mode_row.get(mi_r) {7537 match self.sub8_mode_row.get(mi_r) {
7513- Some(&(col, m)) if mi_c > self.tile_col0_mi && col == mi_c - 1 => Some(m),7538+ Some(&(col, m)) if mi_c > self.tile_col0_mi && usize::from(col) == mi_c - 1 => Some(usize::from(m)),
7514 _ => None,7539 _ => None,
7515 }7540 }
7516 }7541 }
@@ -7526,8 +7551,8 @@ impl Neighbours {
7526 uv_mode: usize,7551 uv_mode: usize,
7527 ) {7552 ) {
7528 let (last_r, last_c) = (mi_r + mi_h - 1, mi_c + mi_w - 1);7553 let (last_r, last_c) = (mi_r + mi_h - 1, mi_c + mi_w - 1);
7529- fill_span(&mut self.uv_mode_col, mi_c, mi_w, (last_r, uv_mode));7554+ fill_span(&mut self.uv_mode_col, mi_c, mi_w, (last_r as u16, uv_mode as u8));
7530- fill_span(&mut self.uv_mode_row, mi_r, mi_h, (last_c, uv_mode));7555+ fill_span(&mut self.uv_mode_row, mi_r, mi_h, (last_c as u16, uv_mode as u8));
7531 // libaom writes one `MB_MODE_INFO` pointer into every mi cell a block7556 // libaom writes one `MB_MODE_INFO` pointer into every mi cell a block
7532 // covers, so the chroma neighbour read below is a plain grid read.7557 // covers, so the chroma neighbour read below is a plain grid read.
7533 let stride = self.uv_grid_cols;7558 let stride = self.uv_grid_cols;
@@ -7598,20 +7623,20 @@ impl Neighbours {
7598 };7623 };
7599 let above = match self.uv_mode_col.get(mi_c) {7624 let above = match self.uv_mode_col.get(mi_c) {
7600 _ if have_above && let Some(m) = cell(&self.uv_mode_grid, mi_r - 1, mi_c) => m,7625 _ if have_above && let Some(m) = cell(&self.uv_mode_grid, mi_r - 1, mi_c) => m,
7601- Some(&(row, m)) if have_above && row == mi_r - 1 => m,7626+ Some(&(row, m)) if have_above && usize::from(row) == mi_r - 1 => usize::from(m),
7602 _ if have_above => self.above_uv_mode[c],7627 _ if have_above => self.above_uv_mode[c],
7603 _ => DC_PRED,7628 _ => DC_PRED,
7604 };7629 };
7605 let left = match self.uv_mode_row.get(mi_r) {7630 let left = match self.uv_mode_row.get(mi_r) {
7606 _ if have_left && let Some(m) = cell(&self.uv_mode_grid, mi_r, mi_c - 1) => m,7631 _ if have_left && let Some(m) = cell(&self.uv_mode_grid, mi_r, mi_c - 1) => m,
7607- Some(&(col, m)) if have_left && col == mi_c - 1 => m,7632+ Some(&(col, m)) if have_left && usize::from(col) == mi_c - 1 => usize::from(m),
7608 _ if have_left => self.left_uv_mode[r],7633 _ if have_left => self.left_uv_mode[r],
7609 _ => DC_PRED,7634 _ => DC_PRED,
7610 };7635 };
7611 if have_above && cell(&self.uv_mode_grid, mi_r - 1, mi_c).is_some_and(|m| {7636 if have_above && cell(&self.uv_mode_grid, mi_r - 1, mi_c).is_some_and(|m| {
7612- !matches!(self.uv_mode_col.get(mi_c), Some(&(row, cm)) if row == mi_r - 1 && cm == m)7637+ !matches!(self.uv_mode_col.get(mi_c), Some(&(row, cm)) if usize::from(row) == mi_r - 1 && usize::from(cm) == m)
7613 }) || have_left && cell(&self.uv_mode_grid, mi_r, mi_c - 1).is_some_and(|m| {7638 }) || have_left && cell(&self.uv_mode_grid, mi_r, mi_c - 1).is_some_and(|m| {
7614- !matches!(self.uv_mode_row.get(mi_r), Some(&(col, cm)) if col == mi_c - 1 && cm == m)7639+ !matches!(self.uv_mode_row.get(mi_r), Some(&(col, cm)) if usize::from(col) == mi_c - 1 && usize::from(cm) == m)
7615 }) {7640 }) {
7616 UV_MODE_GRID_OVERRIDE_HITS.with(|h| h.set(h.get() + 1));7641 UV_MODE_GRID_OVERRIDE_HITS.with(|h| h.set(h.get() + 1));
7617 }7642 }
@@ -7770,8 +7795,8 @@ impl Neighbours {
7770 if crate::envflags::env_flag!("EC_ECPUB") {7795 if crate::envflags::env_flag!("EC_ECPUB") {
7771 eprintln!("EC_ECPUB blk mi=({mi_r},{mi_c}) wh=({w},{h}) lvl={}", states[0].level);7796 eprintln!("EC_ECPUB blk mi=({mi_r},{mi_c}) wh=({w},{h}) lvl={}", states[0].level);
7772 }7797 }
7773- fill_span(&mut self.left_side_mi, mi_r, h_mi, h);7798+ fill_span(&mut self.left_side_mi, mi_r, h_mi, h as u8);
7774- fill_span(&mut self.above_side_mi, mi_c, w_mi, w);7799+ fill_span(&mut self.above_side_mi, mi_c, w_mi, w as u8);
7775 // A chroma 4x4 unit straddling the true luma edge is still whole in7800 // A chroma 4x4 unit straddling the true luma edge is still whole in
7776 // chroma's own halved grid, so libaom rounds the luma bound up to the7801 // chroma's own halved grid, so libaom rounds the luma bound up to the
7777 // plane's own 4x4 unit before clamping a subsampled plane7802 // plane's own 4x4 unit before clamping a subsampled plane
@@ -7960,8 +7985,8 @@ impl Neighbours {
7960 let (w_mi, h_mi) = (w / MI, h / MI);7985 let (w_mi, h_mi) = (w / MI, h / MI);
7961 self.record_mode_mi(mi_r, mi_c, w_mi, h_mi, mode);7986 self.record_mode_mi(mi_r, mi_c, w_mi, h_mi, mode);
7962 self.record_uv_mode_mi(mi_r, mi_c, w_mi, h_mi, uv_mode);7987 self.record_uv_mode_mi(mi_r, mi_c, w_mi, h_mi, uv_mode);
7963- fill_span(&mut self.left_side_mi, mi_r, h_mi, h);7988+ fill_span(&mut self.left_side_mi, mi_r, h_mi, h as u8);
7964- fill_span(&mut self.above_side_mi, mi_c, w_mi, w);7989+ fill_span(&mut self.above_side_mi, mi_c, w_mi, w as u8);
7965 let round_up_even = |n: usize| n.div_ceil(2) * 2;7990 let round_up_even = |n: usize| n.div_ceil(2) * 2;
7966 let (bound_h, bound_w) = (round_up_even(self.mi_rows), round_up_even(self.mi_cols));7991 let (bound_h, bound_w) = (round_up_even(self.mi_rows), round_up_even(self.mi_cols));
7967 for (plane_idx, grid) in chroma_grids.into_iter().enumerate() {7992 for (plane_idx, grid) in chroma_grids.into_iter().enumerate() {
@@ -8702,10 +8727,16 @@ fn record_strip_palette(
8702 {8727 {
8703 for cell in 0..(bw / MI).max(1) {8728 for cell in 0..(bw / MI).max(1) {
8704 if let Some(e) = n.above_palette_size.get_mut(c + cell) {8729 if let Some(e) = n.above_palette_size.get_mut(c + cell) {
8705- *e = y_size;8730+ *e = y_size as u8;
8706- n.above_palette_colors[c + cell] = y_colors;8731+ n.above_palette_uv_size[c + cell] = uv_size as u8;
8707- n.above_palette_uv_size[c + cell] = uv_size;8732+ // lane-publish2, see [`Neighbours::record_palette_y_rect`]:
8708- n.above_palette_uv_colors[c + cell] = uv_colors;8733+ // colours of a size-0 neighbour are never read.
8734+ if y_size > 0 {
8735+ n.above_palette_colors[c + cell] = y_colors;
8736+ }
8737+ if uv_size > 0 {
8738+ n.above_palette_uv_colors[c + cell] = uv_colors;
8739+ }
8709 }8740 }
8710 }8741 }
8711 }8742 }
@@ -8714,10 +8745,14 @@ fn record_strip_palette(
8714 if n.left_palette_size.get(r + cell).is_none() {8745 if n.left_palette_size.get(r + cell).is_none() {
8715 break;8746 break;
8716 }8747 }
8717- n.left_palette_size[r + cell] = y_size;8748+ n.left_palette_size[r + cell] = y_size as u8;
8718- n.left_palette_colors[r + cell] = y_colors;8749+ n.left_palette_uv_size[r + cell] = uv_size as u8;
8719- n.left_palette_uv_size[r + cell] = uv_size;8750+ if y_size > 0 {
8720- n.left_palette_uv_colors[r + cell] = uv_colors;8751+ n.left_palette_colors[r + cell] = y_colors;
8752+ }
8753+ if uv_size > 0 {
8754+ n.left_palette_uv_colors[r + cell] = uv_colors;
8755+ }
8721 }8756 }
8722 }8757 }
8723 }8758 }
@@ -11671,12 +11706,12 @@ fn decode_rect4_16_strip(
11671 // the left (mirrored under VERT_4).11706 // the left (mirrored under VERT_4).
11672 for cell in 0..mi_w {11707 for cell in 0..mi_w {
11673 if let Some(slot) = neighbours.above_side_mi.get_mut(lmi.1 + cell) {11708 if let Some(slot) = neighbours.above_side_mi.get_mut(lmi.1 + cell) {
11674- *slot = bw;11709+ *slot = bw as u8;
11675 }11710 }
11676 }11711 }
11677 for cell in 0..mi_h {11712 for cell in 0..mi_h {
11678 if let Some(slot) = neighbours.left_side_mi.get_mut(lmi.0 + cell) {11713 if let Some(slot) = neighbours.left_side_mi.get_mut(lmi.0 + cell) {
11679- *slot = bh;11714+ *slot = bh as u8;
11680 }11715 }
11681 }11716 }
11682 if has_chroma {11717 if has_chroma {
@@ -15745,8 +15780,8 @@ fn decode_leaf_rect8(
15745 // -- the mirror of `decode_leaf_split4`'s `{31, 31}`, which15780 // -- the mirror of `decode_leaf_split4`'s `{31, 31}`, which
15746 // `partition_ctx_mi` reads back off these side values.15781 // `partition_ctx_mi` reads back off these side values.
15747 for cell in 0..2 {15782 for cell in 0..2 {
15748- neighbours.above_side_mi[leaf_mi.1 + cell] = bw;15783+ neighbours.above_side_mi[leaf_mi.1 + cell] = bw as u8;
15749- neighbours.left_side_mi[leaf_mi.0 + cell] = bh;15784+ neighbours.left_side_mi[leaf_mi.0 + cell] = bh as u8;
15750 }15785 }
15751 let round_up_even = |n: usize| n.div_ceil(2) * 2;15786 let round_up_even = |n: usize| n.div_ceil(2) * 2;
15752 let bound_h = round_up_even(neighbours.mi_rows);15787 let bound_h = round_up_even(neighbours.mi_rows);
@@ -16543,10 +16578,10 @@ fn tx_size_context_txfm_rect(
16543 let mut above = usize::from(n.above_txfm[mi_c]) >= own_w;16578 let mut above = usize::from(n.above_txfm[mi_c]) >= own_w;
16544 let mut left = usize::from(n.left_txfm[mi_r]) >= own_h;16579 let mut left = usize::from(n.left_txfm[mi_r]) >= own_h;
16545 if has_above && n.above_inter[mi_c] {16580 if has_above && n.above_inter[mi_c] {
16546- above = n.above_side_mi[mi_c] >= own_w;16581+ above = usize::from(n.above_side_mi[mi_c]) >= own_w;
16547 }16582 }
16548 if has_left && n.left_inter[mi_r] {16583 if has_left && n.left_inter[mi_r] {
16549- left = n.left_side_mi[mi_r] >= own_h;16584+ left = usize::from(n.left_side_mi[mi_r]) >= own_h;
16550 }16585 }
16551 if crate::envflags::env_flag!("EC_TXCTX") {16586 if crate::envflags::env_flag!("EC_TXCTX") {
16552 eprintln!(16587 eprintln!(
@@ -16603,10 +16638,10 @@ fn tx_size_context_txfm(n: &Neighbours, (mi_r, mi_c): (usize, usize), side: usiz
16603 // then missed libaom's "an inter neighbour contributes its BLOCK size"16638 // then missed libaom's "an inter neighbour contributes its BLOCK size"
16604 // override and read `tx_size_cat1` row 1 where aomdec reads row 2.16639 // override and read `tx_size_cat1` row 1 where aomdec reads row 2.
16605 if has_above && n.above_inter[mi_c] {16640 if has_above && n.above_inter[mi_c] {
16606- above = n.above_side_mi[mi_c] >= side;16641+ above = usize::from(n.above_side_mi[mi_c]) >= side;
16607 }16642 }
16608 if has_left && n.left_inter[mi_r] {16643 if has_left && n.left_inter[mi_r] {
16609- left = n.left_side_mi[mi_r] >= side;16644+ left = usize::from(n.left_side_mi[mi_r]) >= side;
16610 }16645 }
16611 if crate::envflags::env_flag!("EC_TXCTX") {16646 if crate::envflags::env_flag!("EC_TXCTX") {
16612 eprintln!(16647 eprintln!(
@@ -29277,14 +29312,14 @@ fn decode_inter_sub8_rect2(
29277 if i == 1 {29312 if i == 1 {
29278 if vert {29313 if vert {
29279 for cell in 0..h_mi {29314 for cell in 0..h_mi {
29280- neighbours.left_side_mi[gr + cell] = bh;29315+ neighbours.left_side_mi[gr + cell] = bh as u8;
29281 }29316 }
29282- neighbours.above_side_mi[gc] = bw;29317+ neighbours.above_side_mi[gc] = bw as u8;
29283 } else {29318 } else {
29284 for cell in 0..w_mi {29319 for cell in 0..w_mi {
29285- neighbours.above_side_mi[gc + cell] = bw;29320+ neighbours.above_side_mi[gc + cell] = bw as u8;
29286 }29321 }
29287- neighbours.left_side_mi[gr] = bh;29322+ neighbours.left_side_mi[gr] = bh as u8;
29288 }29323 }
29289 }29324 }
29290 let (rmi, cmi) = lmi;29325 let (rmi, cmi) = lmi;
@@ -29763,8 +29798,8 @@ fn decode_inter_sub8_rect2(
29763 // over the whole 8x8 span -- BLOCK_4X8's width above and height left, the29798 // over the whole 8x8 span -- BLOCK_4X8's width above and height left, the
29764 // same values [`decode_leaf_rect8`] writes for the intra shape.29799 // same values [`decode_leaf_rect8`] writes for the intra shape.
29765 for cell in 0..2usize {29800 for cell in 0..2usize {
29766- neighbours.above_side_mi[gc + cell] = bw;29801+ neighbours.above_side_mi[gc + cell] = bw as u8;
29767- neighbours.left_side_mi[gr + cell] = bh;29802+ neighbours.left_side_mi[gr + cell] = bh as u8;
29768 }29803 }
29769 Ok(())29804 Ok(())
29770 }29805 }
@@ -35839,6 +35874,33 @@ mod tests {
35839 assert!(n.palette_uv_cache((1, 1)).is_empty());35874 assert!(n.palette_uv_cache((1, 1)).is_empty());
35840 }35875 }
35841 35876
35877+ /// lane-publish2 (the mode-info publish surface narrowed): two invariants
35878+ /// the byte-exact stream gate only covers indirectly -- a size-0 palette
35879+ /// stamp leaves the PREVIOUS block's colours in place (spec 5.11.46
35880+ /// `get_palette_cache` reads a neighbour's colours only where its size is
35881+ /// non-zero, so writing 16 bytes of zeroes was pure traffic), and the
35882+ /// narrowed `(u16, u8)` mi maps keep their "never written" sentinel.
35883+ #[test]
35884+ fn narrowed_publish_surface_keeps_neighbour_semantics() {
35885+ assert_eq!(std::mem::size_of::<(u16, u8)>(), 4, "the narrowing must actually be 4 bytes");
35886+ let fctx = &crate::decode::FrameCtx::new();
35887+ let mut n = Neighbours::new(4, 4, 16, 16, fctx);
35888+ n.record_palette_y_rect((4, 4), 16, 16, 2, [7, 9, 0, 0, 0, 0, 0, 0]);
35889+ n.record_palette_y_rect((4, 4), 16, 16, 0, [0u16; 8]);
35890+ assert_eq!(n.above_palette_size[4], 0, "the size is what a reader gates on");
35891+ assert_eq!(
35892+ n.above_palette_colors[4],
35893+ [7, 9, 0, 0, 0, 0, 0, 0],
35894+ "a size-0 stamp deliberately leaves the stale colours behind"
35895+ );
35896+ let (ctx, cache) = n.palette_ctx_and_cache((1, 1));
35897+ assert_eq!(ctx, 0, "and they stay invisible to every reader");
35898+ assert!(cache.is_empty());
35899+ assert_eq!(n.mode_above_mi(4, 4), None, "u16::MAX sentinel = never written");
35900+ n.record_mode_mi(3, 4, 1, 1, DC_PRED);
35901+ assert_eq!(n.mode_above_mi(4, 4), Some(DC_PRED));
35902+ }
35903+
35842 /// [`cdf::COMPOUND_MODE_CTX_MAP`] folded by hand against libaom's35904 /// [`cdf::COMPOUND_MODE_CTX_MAP`] folded by hand against libaom's
35843 /// `av1_mode_context_analyzer` compound branch (`mvref_common.h`):35905 /// `av1_mode_context_analyzer` compound branch (`mvref_common.h`):
35844 /// `comp_ctx = compound_mode_ctx_map[refmv_ctx >> 1][min(newmv_ctx, 4)]`35906 /// `comp_ctx = compound_mode_ctx_map[refmv_ctx >> 1][min(newmv_ctx, 4)]`