forge
Explore
Log in

Don't you dare go Hollow

The neighbour palette side bands were indexed on the 16px SUB cell, so a 16x8 strip pair shared one slot and the second strip read the first's palette size -- every screen key frame with palette + tx-size-search decoded whole-superblocks wrong

Browse files at this commit

c642e2802cb13b4094e89f21c920c057213f2f0aAhmet Kaan GümüşParent: d68cb84
  • fix
    • the neighbour palette side bands were indexed on the 16px SUB cell, so a 16x8 strip pair shared one slot and the second strip read the first's palette size -- every screen key frame with palette + tx-size-search decoded whole-superblocks wrong
Show full message
fix(av1): the neighbour palette side bands were indexed on the 16px SUB cell, so a 16x8 strip pair shared one slot and the second strip read the first's palette size -- every screen key frame with palette + tx-size-search decoded whole-superblocks wrong

libaom keeps `palette_size`/`palette_colors` per 4x4 mi cell in `MB_MODE_INFO`, and
`av1_get_palette_cache`/`av1_get_palette_mode_ctx` (pred_common.c:73, pred_common.h:197)
read `xd->above_mbmi`/`xd->left_mbmi` -- the mi cell immediately above/left of the
block. Our `Neighbours::{above,left}_palette_{size,colors}` (and the UV pair) were one
entry per 16px SUB cell, a corner-cut whose own comment said it was "safe today because
every sub-16 leaf path refuses palette outright". Since lane-kf900 r4 lifted the rect
palette refusals that stopped being true: a 32x8 / 16x8 strip pair sits inside ONE cell,
so strip 1's left (above) neighbour lookup returned strip 0's -- or an already
overwritten sibling's -- palette, the colour cache came out one entry short, and
`read_palette_colors_y`'s cache-hit branch narrowed a different interval.

Measured on `$HOME/.cache/kf900-r6/DIFF_45_1_1_test.obu` (r6's ladder): exact through
mi(0,8) `angle_uv` rng=42372, then the first colour-index-map symbol at 34824 (oracle)
vs 51354 (ours) -- libaom's `xd->left_mbmi` for mi(0,8) is mi(0,7), palette size 3, ours
had been overwritten by mi(2,4)'s size 2.

Fix: the eight palette side arrays are mi-granular (`cols * (SUB / MI)`), every
record/lookup takes the block's REAL mi position, and `record_strip_palette`'s
split-axis `last` hack is gone -- it only existed because all four 1:4 strips aliased
onto one SUB slot. `palette_ctx_and_cache_mi`/`palette_uv_cache_mi` drop their separate
`mi_r` argument (the whole coordinate is mi now); the SUB-taking wrappers convert.

Gate: `a_real_aomenc_rect_strip_palette_decodes_pixel_exact` grows arms 3-4,
`smptebars-screen-txs-cq40/cq55` -- `--tune-content=screen --enable-palette=1
--enable-tx-size-search=1 --enable-1to4-partitions=1 --enable-intrabc=1`, 8 and 10 bit,
whole-frame compare. Both streams were DIFF before this fix and are pixel-exact after
(same bytes: `$HOME/.cache/kf900-r6/DIFF_{40,55}_1_1_smpt.obu`). Gate reports 5 firing
attempts, 4 named refusals, 6 frames compared, 0 out-of-scope mismatches.
crates/ec-av1/src/decode.rsmodified+114 -116
@@ -3933,14 +3933,14 @@ impl Neighbours {
39333933 uv_tx_h_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)],
39343934 ref_grid: vec![0i8; cols * (SUB / MI) * rows * (SUB / MI)],
39353935 delta_lf_grid: vec![[0i8; 4]; cols * (SUB / MI) * rows * (SUB / MI)],
3936- above_palette_size: vec![0; cols],
3937- left_palette_size: vec![0; rows],
3938- above_palette_colors: vec![[0u16; 8]; cols],
3939- left_palette_colors: vec![[0u16; 8]; rows],
3940- above_palette_uv_size: vec![0; cols],
3941- left_palette_uv_size: vec![0; rows],
3942- above_palette_uv_colors: vec![[0u16; 8]; cols],
3943- left_palette_uv_colors: vec![[0u16; 8]; rows],
3936+ above_palette_size: vec![0; cols * (SUB / MI)],
3937+ left_palette_size: vec![0; rows * (SUB / MI)],
3938+ above_palette_colors: vec![[0u16; 8]; cols * (SUB / MI)],
3939+ left_palette_colors: vec![[0u16; 8]; rows * (SUB / MI)],
3940+ above_palette_uv_size: vec![0; cols * (SUB / MI)],
3941+ left_palette_uv_size: vec![0; rows * (SUB / MI)],
3942+ above_palette_uv_colors: vec![[0u16; 8]; cols * (SUB / MI)],
3943+ left_palette_uv_colors: vec![[0u16; 8]; rows * (SUB / MI)],
39443944 tile_row0_mi: 0,
39453945 tile_col0_mi: 0,
39463946 }
@@ -4000,7 +4000,7 @@ impl Neighbours {
40004000 /// mirrors [`Self::record_rect`]'s loop shape, square-only (this lane's
40014001 /// call sites are all square blocks).
40024002 fn record_palette_y(&mut self, at: (usize, usize), side: usize, size: usize, colors: [u16; 8]) {
4003- self.record_palette_y_rect(at, side, side, size, colors);
4003+ self.record_palette_y_rect((at.0 * (SUB / MI), at.1 * (SUB / MI)), side, side, size, colors);
40044004 }
40054005
40064006 /// [`Self::record_palette_y`] with independent above (`w`)/left (`h`)
@@ -4009,29 +4009,30 @@ impl Neighbours {
40094009 /// differ.
40104010 fn record_palette_y_rect(
40114011 &mut self,
4012- at: (usize, usize),
4012+ mi: (usize, usize),
40134013 w: usize,
40144014 h: usize,
40154015 size: usize,
40164016 colors: [u16; 8],
40174017 ) {
4018- let (r, c) = at;
4019- // `.max(1)`: a below-16px span covers no whole SUB cell, so the plain
4020- // `w / SUB` loop was a NO-OP for every 16x8/8x16/8x8/4x4 leaf and the
4021- // next block read a long-dead palette out of the above/left arrays
4022- // (class [[cdf-row-held-constant]], lane-palette2 r8). libaom keeps
4023- // `palette_size` per 4px mi cell; corner-cut: our grid is 16px, so a
4024- // sub-16 leaf stamps its whole containing cell -- safe today because
4025- // every sub-16 leaf path refuses palette outright (so it can only
4026- // ever stamp `size == 0`, and its cell holds no palette sibling).
4027- // Upgrade path: widen the palette arrays to mi granularity.
4028- for cell in 0..(w / SUB).max(1) {
4029- self.above_palette_size[c + cell] = size;
4030- self.above_palette_colors[c + cell] = colors;
4018+ let (r, c) = mi;
4019+ // lane-kf900 r6: mi (4px) granularity, libaom's own -- `palette_size`
4020+ // lives per mi cell in `MB_MODE_INFO`, and `xd->left_mbmi` is the mi
4021+ // cell immediately left of the block, not its 16px [`SUB`] cell. The
4022+ // old SUB grid let a 16x8 strip pair share one slot, so the second
4023+ // strip of mi(0,8) read the FIRST strip's palette size and the colour
4024+ // cache came out one entry short (class [[context-read-from-one-cell]]).
4025+ for cell in 0..(w / MI).max(1) {
4026+ if let Some(e) = self.above_palette_size.get_mut(c + cell) {
4027+ *e = size;
4028+ self.above_palette_colors[c + cell] = colors;
4029+ }
40314030 }
4032- for cell in 0..(h / SUB).max(1) {
4033- self.left_palette_size[r + cell] = size;
4034- self.left_palette_colors[r + cell] = colors;
4031+ for cell in 0..(h / MI).max(1) {
4032+ if let Some(e) = self.left_palette_size.get_mut(r + cell) {
4033+ *e = size;
4034+ self.left_palette_colors[r + cell] = colors;
4035+ }
40354036 }
40364037 }
40374038
@@ -4049,7 +4050,7 @@ impl Neighbours {
40494050 /// palette block decoded as "no palette" and the tile desynced from
40504051 /// there (class [[cdf-row-held-constant]]).
40514052 fn palette_ctx_and_cache(&self, at: (usize, usize)) -> (usize, Vec<u16>) {
4052- self.palette_ctx_and_cache_mi(at, at.0 * (SUB / MI))
4053+ self.palette_ctx_and_cache_mi((at.0 * (SUB / MI), at.1 * (SUB / MI)))
40534054 }
40544055
40554056 /// [`Self::palette_ctx_and_cache`] with the block's REAL mi row, which a
@@ -4059,11 +4060,11 @@ impl Neighbours {
40594060 /// block's own PIXEL row), and strip 1 of a HORZ_4 sits 8 px into the
40604061 /// cell -- reading `at.0 % 4` there excludes strip 0 from the cache of
40614062 /// every strip in an SB's top cell row, which libaom does not do.
4062- fn palette_ctx_and_cache_mi(&self, at: (usize, usize), mi_r: usize) -> (usize, Vec<u16>) {
4063- let (r, c) = at;
4064- let above_ok = mi_r % 16 != 0;
4063+ fn palette_ctx_and_cache_mi(&self, mi: (usize, usize)) -> (usize, Vec<u16>) {
4064+ let (r, c) = mi;
4065+ let above_ok = r % 16 != 0;
40654066 // `xd->above_mbmi` exists for every row but the tile's own first.
4066- let above_is_palette = mi_r > self.tile_row0_mi && self.above_palette_size[c] > 0;
4067+ let above_is_palette = r > self.tile_row0_mi && self.above_palette_size[c] > 0;
40674068 let (above_n, above_colors) = if above_ok && self.above_palette_size[c] > 0 {
40684069 (self.above_palette_size[c], self.above_palette_colors[c])
40694070 } else {
@@ -4074,7 +4075,7 @@ impl Neighbours {
40744075 // (`left_available`), not the frame's -- without this guard a second
40754076 // tile column reads the previous tile's palette colours into its cache
40764077 // and its `palette_y_mode` ctx.
4077- let left_ok = c > self.tile_col0_mi / (SUB / MI);
4078+ let left_ok = c > self.tile_col0_mi;
40784079 let (left_n, left_colors) = if left_ok && self.left_palette_size[r] > 0 {
40794080 (self.left_palette_size[r], self.left_palette_colors[r])
40804081 } else {
@@ -4116,7 +4117,13 @@ impl Neighbours {
41164117 /// (U-channel) palette size/colours -- `size == 0` clears stale state the
41174118 /// same way (lane-palette2 r1).
41184119 fn record_palette_uv(&mut self, at: (usize, usize), side: usize, size: usize, colors: [u16; 8]) {
4119- self.record_palette_uv_rect(at, side, side, size, colors);
4120+ self.record_palette_uv_rect(
4121+ (at.0 * (SUB / MI), at.1 * (SUB / MI)),
4122+ side,
4123+ side,
4124+ size,
4125+ colors,
4126+ );
41204127 }
41214128
41224129 /// [`Self::record_palette_uv`] with independent above (`w`)/left (`h`)
@@ -4125,21 +4132,25 @@ impl Neighbours {
41254132 /// luma resolution regardless of which plane the recorded state is for).
41264133 fn record_palette_uv_rect(
41274134 &mut self,
4128- at: (usize, usize),
4135+ mi: (usize, usize),
41294136 w: usize,
41304137 h: usize,
41314138 size: usize,
41324139 colors: [u16; 8],
41334140 ) {
4134- let (r, c) = at;
4135- // See [`Self::record_palette_y_rect`] for the `.max(1)`.
4136- for cell in 0..(w / SUB).max(1) {
4137- self.above_palette_uv_size[c + cell] = size;
4138- self.above_palette_uv_colors[c + cell] = colors;
4141+ let (r, c) = mi;
4142+ // mi granularity, see [`Self::record_palette_y_rect`].
4143+ for cell in 0..(w / MI).max(1) {
4144+ if let Some(e) = self.above_palette_uv_size.get_mut(c + cell) {
4145+ *e = size;
4146+ self.above_palette_uv_colors[c + cell] = colors;
4147+ }
41394148 }
4140- for cell in 0..(h / SUB).max(1) {
4141- self.left_palette_uv_size[r + cell] = size;
4142- self.left_palette_uv_colors[r + cell] = colors;
4149+ for cell in 0..(h / MI).max(1) {
4150+ if let Some(e) = self.left_palette_uv_size.get_mut(r + cell) {
4151+ *e = size;
4152+ self.left_palette_uv_colors[r + cell] = colors;
4153+ }
41434154 }
41444155 }
41454156
@@ -4149,21 +4160,21 @@ impl Neighbours {
41494160 /// palette use, `read_palette_mode_info`'s `pmi->palette_size[0] > 0`),
41504161 /// so only the cache is wanted here.
41514162 fn palette_uv_cache(&self, at: (usize, usize)) -> Vec<u16> {
4152- self.palette_uv_cache_mi(at, at.0 * (SUB / MI))
4163+ self.palette_uv_cache_mi((at.0 * (SUB / MI), at.1 * (SUB / MI)))
41534164 }
41544165
41554166 /// [`Self::palette_uv_cache`] with the block's real mi row -- see
41564167 /// [`Self::palette_ctx_and_cache_mi`].
4157- fn palette_uv_cache_mi(&self, at: (usize, usize), mi_r: usize) -> Vec<u16> {
4158- let (r, c) = at;
4159- let above_ok = mi_r % 16 != 0;
4168+ fn palette_uv_cache_mi(&self, mi: (usize, usize)) -> Vec<u16> {
4169+ let (r, c) = mi;
4170+ let above_ok = r % 16 != 0;
41604171 let (above_n, above_colors) = if above_ok && self.above_palette_uv_size[c] > 0 {
41614172 (self.above_palette_uv_size[c], self.above_palette_uv_colors[c])
41624173 } else {
41634174 (0, [0u16; 8])
41644175 };
41654176 // Same tile-relative left availability as the luma cache above.
4166- let left_ok = c > self.tile_col0_mi / (SUB / MI);
4177+ let left_ok = c > self.tile_col0_mi;
41674178 let (left_n, left_colors) = if left_ok && self.left_palette_uv_size[r] > 0 {
41684179 (self.left_palette_uv_size[r], self.left_palette_uv_colors[r])
41694180 } else {
@@ -5777,40 +5788,43 @@ fn read_intra_mode_rect(
57775788 ))
57785789 }
57795790
5780-/// Stamps a 1:4 strip's palette state into the coarse above/left arrays.
5781-/// `av1_get_palette_cache` reads `xd->above_mbmi`/`xd->left_mbmi`, and inside
5782-/// a HORZ_4 (VERT_4) the next strip's ABOVE (LEFT) neighbour is this strip
5783-/// while its LEFT (ABOVE) neighbour is still the parent block's. The coarse
5784-/// [`SUB`]-cell arrays alias all four strips onto one entry, so writing BOTH
5785-/// axes after every strip would hand strip i+1 this strip's colours on the
5786-/// wrong axis too -- a wrong cache costs the same bits, so the tile stays in
5787-/// sync and only the pixels are wrong (class [[cdf-row-held-constant]]).
5788-/// Only the split axis is written until the closing strip, which stamps both
5789-/// for whatever follows the parent block.
5791+/// Stamps a 1:4 strip's palette state into the mi-granular above/left arrays,
5792+/// at the strip's REAL mi position. `av1_get_palette_cache` reads
5793+/// `xd->above_mbmi`/`xd->left_mbmi`, i.e. the mi cell above/left of the
5794+/// block, so each strip owns its own rows and columns and no aliasing rule
5795+/// is needed (lane-kf900 r6).
57905796 #[allow(clippy::too_many_arguments)]
57915797 fn record_strip_palette(
57925798 n: &mut Neighbours,
5793- at: (usize, usize),
5799+ mi: (usize, usize),
57945800 bw: usize,
57955801 bh: usize,
57965802 y_size: usize,
57975803 y_colors: [u16; 8],
57985804 uv_size: usize,
57995805 uv_colors: [u16; 8],
5800- last: bool,
58015806 ) {
5802- let (r, c) = at;
5803- let horz = bw > bh;
5804- if horz || last {
5805- for cell in 0..(bw / SUB).max(1) {
5806- n.above_palette_size[c + cell] = y_size;
5807- n.above_palette_colors[c + cell] = y_colors;
5808- n.above_palette_uv_size[c + cell] = uv_size;
5809- n.above_palette_uv_colors[c + cell] = uv_colors;
5807+ let (r, c) = mi;
5808+ // lane-kf900 r6: the palette side bands are mi-granular now, so a strip
5809+ // writes its OWN span on BOTH axes -- the old `last` split-axis rule only
5810+ // existed because the 16px [`SUB`] cell held one slot for all the strips
5811+ // of a HORZ_4/VERT_4 and leaf 0 zeroing the other axis threw away the
5812+ // real neighbour leaf 1 reads.
5813+ {
5814+ for cell in 0..(bw / MI).max(1) {
5815+ if let Some(e) = n.above_palette_size.get_mut(c + cell) {
5816+ *e = y_size;
5817+ n.above_palette_colors[c + cell] = y_colors;
5818+ n.above_palette_uv_size[c + cell] = uv_size;
5819+ n.above_palette_uv_colors[c + cell] = uv_colors;
5820+ }
58105821 }
58115822 }
5812- if !horz || last {
5813- for cell in 0..(bh / SUB).max(1) {
5823+ {
5824+ for cell in 0..(bh / MI).max(1) {
5825+ if n.left_palette_size.get(r + cell).is_none() {
5826+ break;
5827+ }
58145828 n.left_palette_size[r + cell] = y_size;
58155829 n.left_palette_colors[r + cell] = y_colors;
58165830 n.left_palette_uv_size[r + cell] = uv_size;
@@ -6659,10 +6673,10 @@ fn decode_block_rect(
66596673 // wrong VALUE -- same bit count, so the entropy stream stayed in sync
66606674 // and only those pixels were wrong (lane-palette2 r10).
66616675 let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors));
6662- neighbours.record_palette_y_rect(at, bw, bh, py_size, py_colors);
6676+ neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, py_size, py_colors);
66636677 let (puv_size, puv_colors) =
66646678 palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors));
6665- neighbours.record_palette_uv_rect(at, bw, bh, puv_size, puv_colors);
6679+ neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, puv_size, puv_colors);
66666680 RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1));
66676681 return Ok(());
66686682 }
@@ -6927,10 +6941,10 @@ fn decode_block_rect(
69276941 }
69286942 }
69296943 let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors));
6930- neighbours.record_palette_y_rect(at, bw, bh, py_size, py_colors);
6944+ neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, py_size, py_colors);
69316945 let (puv_size, puv_colors) =
69326946 palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors));
6933- neighbours.record_palette_uv_rect(at, bw, bh, puv_size, puv_colors);
6947+ neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, puv_size, puv_colors);
69346948 neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip);
69356949 neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0);
69366950 RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1));
@@ -7016,12 +7030,13 @@ fn decode_leaf_rect(
70167030 // a palette narrows the wrong interval and desyncs the tile even when the
70177031 // decoded bit is the same. The cell is the 16px-SUB cell containing the
70187032 // leaf (the palette neighbour arrays have no finer granularity).
7019- let pal_at = (leaf_mi.0 / (SUB / MI), leaf_mi.1 / (SUB / MI));
7020- // lane-kf900 r4: the leaf's REAL mi row -- the second leaf of a 16x8 pair
7021- // sits 8 px into the cell, so the cache's superblock-top-row exclusion
7022- // must not be read off the cell (see [`Neighbours::palette_ctx_and_cache_mi`]).
7023- let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(pal_at, leaf_mi.0);
7024- let palette_uv_cache = neighbours.palette_uv_cache_mi(pal_at, leaf_mi.0);
7033+ // lane-kf900 r6: the leaf's REAL mi position -- the second leaf of a 16x8
7034+ // pair sits 8 px into the 16px cell, and both the cache's superblock-top-row
7035+ // exclusion and the neighbour palette lookup itself are mi-granular
7036+ // (see [`Neighbours::palette_ctx_and_cache_mi`]).
7037+ let pal_at = leaf_mi;
7038+ let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(pal_at);
7039+ let palette_uv_cache = neighbours.palette_uv_cache_mi(pal_at);
70257040 let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv) =
70267041 read_intra_mode_rect(
70277042 dec,
@@ -7059,12 +7074,6 @@ fn decode_leaf_rect(
70597074 palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors));
70607075 let (pal_uv_size, pal_uv_colors) =
70617076 palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors));
7062- // The closing leaf of the pair -- see [`record_strip_palette`].
7063- let pal_last = if bw > bh {
7064- leaf_mi.0 % 4 == 2
7065- } else {
7066- leaf_mi.1 % 4 == 2
7067- };
70687077 if std::env::var_os("EC_AV1_RECTX_TRACE").is_some() {
70697078 eprintln!(" skip={skip} mode={mode} angle_delta_y={angle_delta_y} uv_mode={uv_mode}");
70707079 }
@@ -7147,7 +7156,6 @@ fn decode_leaf_rect(
71477156 )?;
71487157 record_strip_palette(
71497158 neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors,
7150- pal_last,
71517159 );
71527160 return Ok(mode);
71537161 }
@@ -7271,7 +7279,6 @@ fn decode_leaf_rect(
72717279 // real left neighbour leaf 1 reads (lane-kf900 r4).
72727280 record_strip_palette(
72737281 neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors,
7274- pal_last,
72757282 );
72767283 // A rect leaf spans mi_w x mi_h, so its mode reaches several columns and
72777284 // rows -- record the whole span, not one cell.
@@ -7341,9 +7348,9 @@ fn decode_block_rect4(
73417348 // `palette_colors_y` cache hit off an EMPTY cache -- right symbol count,
73427349 // wrong interval, so a screen-content tile desynced at the first strip
73437350 // whose neighbour used a palette (class [[cdf-row-held-constant]]).
7344- let pal_at = (mi_r / (SUB / MI), mi_c / (SUB / MI));
7345- let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(pal_at, mi_r);
7346- let palette_uv_cache = neighbours.palette_uv_cache_mi(pal_at, mi_r);
7351+ let pal_at = (mi_r, mi_c);
7352+ let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(pal_at);
7353+ let palette_uv_cache = neighbours.palette_uv_cache_mi(pal_at);
73477354 let (
73487355 skip,
73497356 mode,
@@ -7387,10 +7394,6 @@ fn decode_block_rect4(
73877394 p.map.iter().map(|&i| p.v_colors[i as usize]).collect(),
73887395 )
73897396 });
7390- // `last`: strip 3 of the parent 32x32's HORZ_4 (VERT_4), the only one
7391- // whose colours a block outside the parent sees -- see
7392- // [`record_strip_palette`].
7393- let pal_last = if bw > bh { mi_r % 8 == 6 } else { mi_c % 8 == 6 };
73947397 let (pal_y_size, pal_y_colors) =
73957398 palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors));
73967399 let (pal_uv_size, pal_uv_colors) =
@@ -7478,7 +7481,7 @@ fn decode_block_rect4(
74787481 )?;
74797482 record_strip_palette(
74807483 neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size,
7481- pal_uv_colors, pal_last,
7484+ pal_uv_colors,
74827485 );
74837486 if bw > bh {
74847487 RECT4_32_HORZ_HITS.with(|c| c.set(c.get() + 1));
@@ -7631,7 +7634,7 @@ fn decode_block_rect4(
76317634 neighbours.fill_lf_grid_rect((mi_r, mi_c), mi_w, mi_h, bw as u8, bh as u8, 0);
76327635 record_strip_palette(
76337636 neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size,
7634- pal_uv_colors, pal_last,
7637+ pal_uv_colors,
76357638 );
76367639 if bw > bh {
76377640 RECT4_32_HORZ_HITS.with(|c| c.set(c.get() + 1));
@@ -7694,7 +7697,6 @@ fn decode_block_rect4(
76947697 neighbours.fill_lf_grid_rect((mi_r, mi_c), mi_w, mi_h, bw as u8, bh as u8, 0);
76957698 record_strip_palette(
76967699 neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors,
7697- pal_last,
76987700 );
76997701 if bw > bh {
77007702 RECT4_32_HORZ_HITS.with(|c| c.set(c.get() + 1));
@@ -7778,9 +7780,8 @@ fn decode_rect4_16(
77787780 // lane-kf900 r4: the strip's real palette ctx + colour cache (see
77797781 // [`decode_block_rect4`]); `None`/`&[]` here read `palette_y_mode`
77807782 // off CDF row 0 and every cache hit off an empty cache.
7781- let (palette_ctx, palette_cache) =
7782- neighbours.palette_ctx_and_cache_mi(at16, lmi.0);
7783- let palette_uv_cache = neighbours.palette_uv_cache_mi(at16, lmi.0);
7783+ let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(lmi);
7784+ let palette_uv_cache = neighbours.palette_uv_cache_mi(lmi);
77847785 let (
77857786 skip,
77867787 mode,
@@ -8180,8 +8181,7 @@ fn decode_rect4_16(
81808181 let (pal_uv_size, pal_uv_colors) =
81818182 palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors));
81828183 record_strip_palette(
8183- neighbours, at16, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors,
8184- i == 3,
8184+ neighbours, lmi, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors,
81858185 );
81868186 last_mode = mode;
81878187 prev = Some((lmi, mode));
@@ -8376,10 +8376,10 @@ fn decode_block_rect64(
83768376 // split-tx superblock strip would otherwise leave the above/left
83778377 // palette arrays holding a dead block's colours.
83788378 let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors));
8379- neighbours.record_palette_y_rect(at, bw, bh, py_size, py_colors);
8379+ neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, py_size, py_colors);
83808380 let (puv_size, puv_colors) =
83818381 palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors));
8382- neighbours.record_palette_uv_rect(at, bw, bh, puv_size, puv_colors);
8382+ neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, puv_size, puv_colors);
83838383 RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1));
83848384 return Ok(());
83858385 }
@@ -8691,10 +8691,10 @@ fn decode_block_rect64(
86918691 RECT_COEFF_HITS.with(|c| c.set(c.get() + 1));
86928692 }
86938693 let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors));
8694- neighbours.record_palette_y_rect(at, bw, bh, py_size, py_colors);
8694+ neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, py_size, py_colors);
86958695 let (puv_size, puv_colors) =
86968696 palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors));
8697- neighbours.record_palette_uv_rect(at, bw, bh, puv_size, puv_colors);
8697+ neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, puv_size, puv_colors);
86988698 neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip);
86998699 neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0);
87008700 SB_RECT_HITS.with(|c| c.set(c.get() + 1));
@@ -10657,9 +10657,8 @@ fn decode_leaf8(
1065710657 // above/left palette state over its own cell so the next block's ctx and
1065810658 // colour cache do not read a previous block's palette (lane-palette2 r8).
1065910659 {
10660- let pal_at = (leaf_mi.0 / (SUB / MI), leaf_mi.1 / (SUB / MI));
10661- neighbours.record_palette_y_rect(pal_at, 8, 8, 0, [0u16; 8]);
10662- neighbours.record_palette_uv_rect(pal_at, 8, 8, 0, [0u16; 8]);
10660+ neighbours.record_palette_y_rect(leaf_mi, 8, 8, 0, [0u16; 8]);
10661+ neighbours.record_palette_uv_rect(leaf_mi, 8, 8, 0, [0u16; 8]);
1066310662 }
1066410663 neighbours.fill_skip_grid(leaf_mi, 2, skip);
1066510664 neighbours.fill_lf_grid(leaf_mi, 2, 4, 0);
@@ -10671,9 +10670,8 @@ fn decode_leaf8(
1067110670 // above/left palette state over its own cell so the next block's ctx and
1067210671 // colour cache do not read a previous block's palette (lane-palette2 r8).
1067310672 {
10674- let pal_at = (leaf_mi.0 / (SUB / MI), leaf_mi.1 / (SUB / MI));
10675- neighbours.record_palette_y_rect(pal_at, 8, 8, 0, [0u16; 8]);
10676- neighbours.record_palette_uv_rect(pal_at, 8, 8, 0, [0u16; 8]);
10673+ neighbours.record_palette_y_rect(leaf_mi, 8, 8, 0, [0u16; 8]);
10674+ neighbours.record_palette_uv_rect(leaf_mi, 8, 8, 0, [0u16; 8]);
1067710675 }
1067810676 neighbours.fill_skip_grid(leaf_mi, 2, skip);
1067910677 // lane-kf900 r1: the leaf's own RESOLVED transform size, never the literal
@@ -23277,19 +23275,19 @@ mod tests {
2327723275 fn a_sub16_leaf_after_a_palette_block_yields_palette_ctx_zero() {
2327823276 let mut n = Neighbours::new(4, 4, 16, 16);
2327923277 // A real 16x16 palette block covering SUB cell (1, 1).
23280- n.record_palette_y_rect((1, 1), 16, 16, 2, [7, 9, 0, 0, 0, 0, 0, 0]);
23278+ n.record_palette_y_rect((4, 4), 16, 16, 2, [7, 9, 0, 0, 0, 0, 0, 0]);
2328123279 let (ctx, cache) = n.palette_ctx_and_cache((1, 1));
2328223280 assert_eq!(ctx, 2, "the palette block itself must be visible");
2328323281 assert_eq!(cache, vec![7, 9]);
2328423282 // An 8x8 non-palette leaf in that same cell.
23285- n.record_palette_y_rect((1, 1), 8, 8, 0, [0u16; 8]);
23283+ n.record_palette_y_rect((4, 4), 8, 8, 0, [0u16; 8]);
2328623284 let (ctx, cache) = n.palette_ctx_and_cache((1, 1));
2328723285 assert_eq!(ctx, 0, "a sub-16 non-palette leaf must clear its own cell");
2328823286 assert!(cache.is_empty(), "and its stale colour cache with it");
2328923287 // Same for the chroma half.
23290- n.record_palette_uv_rect((1, 1), 16, 16, 2, [1, 2, 0, 0, 0, 0, 0, 0]);
23288+ n.record_palette_uv_rect((4, 4), 16, 16, 2, [1, 2, 0, 0, 0, 0, 0, 0]);
2329123289 assert_eq!(n.palette_uv_cache((1, 1)), vec![1, 2]);
23292- n.record_palette_uv_rect((1, 1), 8, 8, 0, [0u16; 8]);
23290+ n.record_palette_uv_rect((4, 4), 8, 8, 0, [0u16; 8]);
2329323291 assert!(n.palette_uv_cache((1, 1)).is_empty());
2329423292 }
2329523293
crates/ec-av1/src/stream.rsmodified+21 -1
@@ -2974,10 +2974,30 @@ mod tests {
29742974 "--enable-1to4-partitions=1",
29752975 "--min-partition-size=8",
29762976 ];
2977- let arms: [(&str, String, &[&str]); 3] = [
2977+ // lane-kf900 r6: arms 3-4 are the palette + `--enable-tx-size-search=1`
2978+ // shape whose neighbour palette lookup was SUB-granular -- a 16x8 strip
2979+ // pair shares one 16px cell, so strip 1 read strip 0's palette size and
2980+ // the colour cache came out short. Both were whole-frame wrong before
2981+ // the mi-granular fix (`$HOME/.cache/kf900-r6/sweep.log`: smptebars
2982+ // cq40/cq55 txs=1 pal=1 = DIFF).
2983+ let screen_txs: &[&str] = &[
2984+ "--tune-content=screen",
2985+ "--enable-palette=1",
2986+ "--enable-1to4-partitions=1",
2987+ "--min-partition-size=8",
2988+ "--max-partition-size=32",
2989+ "--sb-size=64",
2990+ "--enable-intrabc=1",
2991+ "--enable-tx-size-search=1",
2992+ ];
2993+ let cq40: Vec<&str> = screen_txs.iter().copied().chain(["--cq-level=40"]).collect();
2994+ let cq55: Vec<&str> = screen_txs.iter().copied().chain(["--cq-level=55"]).collect();
2995+ let arms: [(&str, String, &[&str]); 5] = [
29782996 ("testsrc2-pinned", "testsrc2=s=256x192:r=25".to_string(), &[]),
29792997 ("testsrc2-screen", "testsrc2=s=256x192:r=25".to_string(), screen),
29802998 ("smptebars-screen", "smptebars=size=256x192:rate=25".to_string(), screen),
2999+ ("smptebars-screen-txs-cq40", "smptebars=size=256x192:rate=25".to_string(), &cq40),
3000+ ("smptebars-screen-txs-cq55", "smptebars=size=256x192:rate=25".to_string(), &cq55),
29813001 ];
29823002 let (mut fired_arms, mut out_of_scope, mut out_of_scope_mismatch) = (0u32, 0u32, 0u32);
29833003 let mut refused = 0u32;