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 {
3933 uv_tx_h_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)],3933 uv_tx_h_grid: vec![0u8; cols * (SUB / MI) * rows * (SUB / MI)],
3934 ref_grid: vec![0i8; cols * (SUB / MI) * rows * (SUB / MI)],3934 ref_grid: vec![0i8; cols * (SUB / MI) * rows * (SUB / MI)],
3935 delta_lf_grid: vec![[0i8; 4]; cols * (SUB / MI) * rows * (SUB / MI)],3935 delta_lf_grid: vec![[0i8; 4]; cols * (SUB / MI) * rows * (SUB / MI)],
3936- above_palette_size: vec![0; cols],3936+ above_palette_size: vec![0; cols * (SUB / MI)],
3937- left_palette_size: vec![0; rows],3937+ left_palette_size: vec![0; rows * (SUB / MI)],
3938- above_palette_colors: vec![[0u16; 8]; cols],3938+ above_palette_colors: vec![[0u16; 8]; cols * (SUB / MI)],
3939- left_palette_colors: vec![[0u16; 8]; rows],3939+ left_palette_colors: vec![[0u16; 8]; rows * (SUB / MI)],
3940- above_palette_uv_size: vec![0; cols],3940+ above_palette_uv_size: vec![0; cols * (SUB / MI)],
3941- left_palette_uv_size: vec![0; rows],3941+ left_palette_uv_size: vec![0; rows * (SUB / MI)],
3942- above_palette_uv_colors: vec![[0u16; 8]; cols],3942+ above_palette_uv_colors: vec![[0u16; 8]; cols * (SUB / MI)],
3943- left_palette_uv_colors: vec![[0u16; 8]; rows],3943+ left_palette_uv_colors: vec![[0u16; 8]; rows * (SUB / MI)],
3944 tile_row0_mi: 0,3944 tile_row0_mi: 0,
3945 tile_col0_mi: 0,3945 tile_col0_mi: 0,
3946 }3946 }
@@ -4000,7 +4000,7 @@ impl Neighbours {
4000 /// mirrors [`Self::record_rect`]'s loop shape, square-only (this lane's4000 /// mirrors [`Self::record_rect`]'s loop shape, square-only (this lane's
4001 /// call sites are all square blocks).4001 /// call sites are all square blocks).
4002 fn record_palette_y(&mut self, at: (usize, usize), side: usize, size: usize, colors: [u16; 8]) {4002 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);
4004 }4004 }
4005 4005
4006 /// [`Self::record_palette_y`] with independent above (`w`)/left (`h`)4006 /// [`Self::record_palette_y`] with independent above (`w`)/left (`h`)
@@ -4009,29 +4009,30 @@ impl Neighbours {
4009 /// differ.4009 /// differ.
4010 fn record_palette_y_rect(4010 fn record_palette_y_rect(
4011 &mut self,4011 &mut self,
4012- at: (usize, usize),4012+ mi: (usize, usize),
4013 w: usize,4013 w: usize,
4014 h: usize,4014 h: usize,
4015 size: usize,4015 size: usize,
4016 colors: [u16; 8],4016 colors: [u16; 8],
4017 ) {4017 ) {
4018- let (r, c) = at;4018+ let (r, c) = mi;
4019- // `.max(1)`: a below-16px span covers no whole SUB cell, so the plain4019+ // lane-kf900 r6: mi (4px) granularity, libaom's own -- `palette_size`
4020- // `w / SUB` loop was a NO-OP for every 16x8/8x16/8x8/4x4 leaf and the4020+ // lives per mi cell in `MB_MODE_INFO`, and `xd->left_mbmi` is the mi
4021- // next block read a long-dead palette out of the above/left arrays4021+ // cell immediately left of the block, not its 16px [`SUB`] cell. The
4022- // (class [[cdf-row-held-constant]], lane-palette2 r8). libaom keeps4022+ // old SUB grid let a 16x8 strip pair share one slot, so the second
4023- // `palette_size` per 4px mi cell; corner-cut: our grid is 16px, so a4023+ // strip of mi(0,8) read the FIRST strip's palette size and the colour
4024- // sub-16 leaf stamps its whole containing cell -- safe today because4024+ // cache came out one entry short (class [[context-read-from-one-cell]]).
4025- // every sub-16 leaf path refuses palette outright (so it can only4025+ for cell in 0..(w / MI).max(1) {
4026- // ever stamp `size == 0`, and its cell holds no palette sibling).4026+ if let Some(e) = self.above_palette_size.get_mut(c + cell) {
4027- // Upgrade path: widen the palette arrays to mi granularity.4027+ *e = size;
4028- for cell in 0..(w / SUB).max(1) {4028+ self.above_palette_colors[c + cell] = colors;
4029- self.above_palette_size[c + cell] = size;4029+ }
4030- self.above_palette_colors[c + cell] = colors;
4031 }4030 }
4032- for cell in 0..(h / SUB).max(1) {4031+ for cell in 0..(h / MI).max(1) {
4033- self.left_palette_size[r + cell] = size;4032+ if let Some(e) = self.left_palette_size.get_mut(r + cell) {
4034- self.left_palette_colors[r + cell] = colors;4033+ *e = size;
4034+ self.left_palette_colors[r + cell] = colors;
4035+ }
4035 }4036 }
4036 }4037 }
4037 4038
@@ -4049,7 +4050,7 @@ impl Neighbours {
4049 /// palette block decoded as "no palette" and the tile desynced from4050 /// palette block decoded as "no palette" and the tile desynced from
4050 /// there (class [[cdf-row-held-constant]]).4051 /// there (class [[cdf-row-held-constant]]).
4051 fn palette_ctx_and_cache(&self, at: (usize, usize)) -> (usize, Vec<u16>) {4052 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)))
4053 }4054 }
4054 4055
4055 /// [`Self::palette_ctx_and_cache`] with the block's REAL mi row, which a4056 /// [`Self::palette_ctx_and_cache`] with the block's REAL mi row, which a
@@ -4059,11 +4060,11 @@ impl Neighbours {
4059 /// block's own PIXEL row), and strip 1 of a HORZ_4 sits 8 px into the4060 /// block's own PIXEL row), and strip 1 of a HORZ_4 sits 8 px into the
4060 /// cell -- reading `at.0 % 4` there excludes strip 0 from the cache of4061 /// cell -- reading `at.0 % 4` there excludes strip 0 from the cache of
4061 /// every strip in an SB's top cell row, which libaom does not do.4062 /// 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+ fn palette_ctx_and_cache_mi(&self, mi: (usize, usize)) -> (usize, Vec<u16>) {
4063- let (r, c) = at;4064+ let (r, c) = mi;
4064- let above_ok = mi_r % 16 != 0;4065+ let above_ok = r % 16 != 0;
4065 // `xd->above_mbmi` exists for every row but the tile's own first.4066 // `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;
4067 let (above_n, above_colors) = if above_ok && self.above_palette_size[c] > 0 {4068 let (above_n, above_colors) = if above_ok && self.above_palette_size[c] > 0 {
4068 (self.above_palette_size[c], self.above_palette_colors[c])4069 (self.above_palette_size[c], self.above_palette_colors[c])
4069 } else {4070 } else {
@@ -4074,7 +4075,7 @@ impl Neighbours {
4074 // (`left_available`), not the frame's -- without this guard a second4075 // (`left_available`), not the frame's -- without this guard a second
4075 // tile column reads the previous tile's palette colours into its cache4076 // tile column reads the previous tile's palette colours into its cache
4076 // and its `palette_y_mode` ctx.4077 // 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;
4078 let (left_n, left_colors) = if left_ok && self.left_palette_size[r] > 0 {4079 let (left_n, left_colors) = if left_ok && self.left_palette_size[r] > 0 {
4079 (self.left_palette_size[r], self.left_palette_colors[r])4080 (self.left_palette_size[r], self.left_palette_colors[r])
4080 } else {4081 } else {
@@ -4116,7 +4117,13 @@ impl Neighbours {
4116 /// (U-channel) palette size/colours -- `size == 0` clears stale state the4117 /// (U-channel) palette size/colours -- `size == 0` clears stale state the
4117 /// same way (lane-palette2 r1).4118 /// same way (lane-palette2 r1).
4118 fn record_palette_uv(&mut self, at: (usize, usize), side: usize, size: usize, colors: [u16; 8]) {4119 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+ );
4120 }4127 }
4121 4128
4122 /// [`Self::record_palette_uv`] with independent above (`w`)/left (`h`)4129 /// [`Self::record_palette_uv`] with independent above (`w`)/left (`h`)
@@ -4125,21 +4132,25 @@ impl Neighbours {
4125 /// luma resolution regardless of which plane the recorded state is for).4132 /// luma resolution regardless of which plane the recorded state is for).
4126 fn record_palette_uv_rect(4133 fn record_palette_uv_rect(
4127 &mut self,4134 &mut self,
4128- at: (usize, usize),4135+ mi: (usize, usize),
4129 w: usize,4136 w: usize,
4130 h: usize,4137 h: usize,
4131 size: usize,4138 size: usize,
4132 colors: [u16; 8],4139 colors: [u16; 8],
4133 ) {4140 ) {
4134- let (r, c) = at;4141+ let (r, c) = mi;
4135- // See [`Self::record_palette_y_rect`] for the `.max(1)`.4142+ // mi granularity, see [`Self::record_palette_y_rect`].
4136- for cell in 0..(w / SUB).max(1) {4143+ for cell in 0..(w / MI).max(1) {
4137- self.above_palette_uv_size[c + cell] = size;4144+ if let Some(e) = self.above_palette_uv_size.get_mut(c + cell) {
4138- self.above_palette_uv_colors[c + cell] = colors;4145+ *e = size;
4146+ self.above_palette_uv_colors[c + cell] = colors;
4147+ }
4139 }4148 }
4140- for cell in 0..(h / SUB).max(1) {4149+ for cell in 0..(h / MI).max(1) {
4141- self.left_palette_uv_size[r + cell] = size;4150+ if let Some(e) = self.left_palette_uv_size.get_mut(r + cell) {
4142- self.left_palette_uv_colors[r + cell] = colors;4151+ *e = size;
4152+ self.left_palette_uv_colors[r + cell] = colors;
4153+ }
4143 }4154 }
4144 }4155 }
4145 4156
@@ -4149,21 +4160,21 @@ impl Neighbours {
4149 /// palette use, `read_palette_mode_info`'s `pmi->palette_size[0] > 0`),4160 /// palette use, `read_palette_mode_info`'s `pmi->palette_size[0] > 0`),
4150 /// so only the cache is wanted here.4161 /// so only the cache is wanted here.
4151 fn palette_uv_cache(&self, at: (usize, usize)) -> Vec<u16> {4162 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)))
4153 }4164 }
4154 4165
4155 /// [`Self::palette_uv_cache`] with the block's real mi row -- see4166 /// [`Self::palette_uv_cache`] with the block's real mi row -- see
4156 /// [`Self::palette_ctx_and_cache_mi`].4167 /// [`Self::palette_ctx_and_cache_mi`].
4157- fn palette_uv_cache_mi(&self, at: (usize, usize), mi_r: usize) -> Vec<u16> {4168+ fn palette_uv_cache_mi(&self, mi: (usize, usize)) -> Vec<u16> {
4158- let (r, c) = at;4169+ let (r, c) = mi;
4159- let above_ok = mi_r % 16 != 0;4170+ let above_ok = r % 16 != 0;
4160 let (above_n, above_colors) = if above_ok && self.above_palette_uv_size[c] > 0 {4171 let (above_n, above_colors) = if above_ok && self.above_palette_uv_size[c] > 0 {
4161 (self.above_palette_uv_size[c], self.above_palette_uv_colors[c])4172 (self.above_palette_uv_size[c], self.above_palette_uv_colors[c])
4162 } else {4173 } else {
4163 (0, [0u16; 8])4174 (0, [0u16; 8])
4164 };4175 };
4165 // Same tile-relative left availability as the luma cache above.4176 // 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;
4167 let (left_n, left_colors) = if left_ok && self.left_palette_uv_size[r] > 0 {4178 let (left_n, left_colors) = if left_ok && self.left_palette_uv_size[r] > 0 {
4168 (self.left_palette_uv_size[r], self.left_palette_uv_colors[r])4179 (self.left_palette_uv_size[r], self.left_palette_uv_colors[r])
4169 } else {4180 } else {
@@ -5777,40 +5788,43 @@ fn read_intra_mode_rect(
5777 ))5788 ))
5778 }5789 }
5779 5790
5780-/// Stamps a 1:4 strip's palette state into the coarse above/left arrays.5791+/// Stamps a 1:4 strip's palette state into the mi-granular above/left arrays,
5781-/// `av1_get_palette_cache` reads `xd->above_mbmi`/`xd->left_mbmi`, and inside5792+/// at the strip's REAL mi position. `av1_get_palette_cache` reads
5782-/// a HORZ_4 (VERT_4) the next strip's ABOVE (LEFT) neighbour is this strip5793+/// `xd->above_mbmi`/`xd->left_mbmi`, i.e. the mi cell above/left of the
5783-/// while its LEFT (ABOVE) neighbour is still the parent block's. The coarse5794+/// block, so each strip owns its own rows and columns and no aliasing rule
5784-/// [`SUB`]-cell arrays alias all four strips onto one entry, so writing BOTH5795+/// is needed (lane-kf900 r6).
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.
5790 #[allow(clippy::too_many_arguments)]5796 #[allow(clippy::too_many_arguments)]
5791 fn record_strip_palette(5797 fn record_strip_palette(
5792 n: &mut Neighbours,5798 n: &mut Neighbours,
5793- at: (usize, usize),5799+ mi: (usize, usize),
5794 bw: usize,5800 bw: usize,
5795 bh: usize,5801 bh: usize,
5796 y_size: usize,5802 y_size: usize,
5797 y_colors: [u16; 8],5803 y_colors: [u16; 8],
5798 uv_size: usize,5804 uv_size: usize,
5799 uv_colors: [u16; 8],5805 uv_colors: [u16; 8],
5800- last: bool,
5801 ) {5806 ) {
5802- let (r, c) = at;5807+ let (r, c) = mi;
5803- let horz = bw > bh;5808+ // lane-kf900 r6: the palette side bands are mi-granular now, so a strip
5804- if horz || last {5809+ // writes its OWN span on BOTH axes -- the old `last` split-axis rule only
5805- for cell in 0..(bw / SUB).max(1) {5810+ // existed because the 16px [`SUB`] cell held one slot for all the strips
5806- n.above_palette_size[c + cell] = y_size;5811+ // of a HORZ_4/VERT_4 and leaf 0 zeroing the other axis threw away the
5807- n.above_palette_colors[c + cell] = y_colors;5812+ // real neighbour leaf 1 reads.
5808- n.above_palette_uv_size[c + cell] = uv_size;5813+ {
5809- n.above_palette_uv_colors[c + cell] = uv_colors;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+ }
5810 }5821 }
5811 }5822 }
5812- if !horz || last {5823+ {
5813- for cell in 0..(bh / SUB).max(1) {5824+ for cell in 0..(bh / MI).max(1) {
5825+ if n.left_palette_size.get(r + cell).is_none() {
5826+ break;
5827+ }
5814 n.left_palette_size[r + cell] = y_size;5828 n.left_palette_size[r + cell] = y_size;
5815 n.left_palette_colors[r + cell] = y_colors;5829 n.left_palette_colors[r + cell] = y_colors;
5816 n.left_palette_uv_size[r + cell] = uv_size;5830 n.left_palette_uv_size[r + cell] = uv_size;
@@ -6659,10 +6673,10 @@ fn decode_block_rect(
6659 // wrong VALUE -- same bit count, so the entropy stream stayed in sync6673 // wrong VALUE -- same bit count, so the entropy stream stayed in sync
6660 // and only those pixels were wrong (lane-palette2 r10).6674 // and only those pixels were wrong (lane-palette2 r10).
6661 let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors));6675 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);
6663 let (puv_size, puv_colors) =6677 let (puv_size, puv_colors) =
6664 palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors));6678 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);
6666 RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1));6680 RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1));
6667 return Ok(());6681 return Ok(());
6668 }6682 }
@@ -6927,10 +6941,10 @@ fn decode_block_rect(
6927 }6941 }
6928 }6942 }
6929 let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors));6943 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);
6931 let (puv_size, puv_colors) =6945 let (puv_size, puv_colors) =
6932 palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors));6946 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);
6934 neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip);6948 neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip);
6935 neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0);6949 neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0);
6936 RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1));6950 RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1));
@@ -7016,12 +7030,13 @@ fn decode_leaf_rect(
7016 // a palette narrows the wrong interval and desyncs the tile even when the7030 // a palette narrows the wrong interval and desyncs the tile even when the
7017 // decoded bit is the same. The cell is the 16px-SUB cell containing the7031 // decoded bit is the same. The cell is the 16px-SUB cell containing the
7018 // leaf (the palette neighbour arrays have no finer granularity).7032 // leaf (the palette neighbour arrays have no finer granularity).
7019- let pal_at = (leaf_mi.0 / (SUB / MI), leaf_mi.1 / (SUB / MI));7033+ // lane-kf900 r6: the leaf's REAL mi position -- the second leaf of a 16x8
7020- // lane-kf900 r4: the leaf's REAL mi row -- the second leaf of a 16x8 pair7034+ // pair sits 8 px into the 16px cell, and both the cache's superblock-top-row
7021- // sits 8 px into the cell, so the cache's superblock-top-row exclusion7035+ // exclusion and the neighbour palette lookup itself are mi-granular
7022- // must not be read off the cell (see [`Neighbours::palette_ctx_and_cache_mi`]).7036+ // (see [`Neighbours::palette_ctx_and_cache_mi`]).
7023- let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(pal_at, leaf_mi.0);7037+ let pal_at = leaf_mi;
7024- let palette_uv_cache = neighbours.palette_uv_cache_mi(pal_at, leaf_mi.0);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);
7025 let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv) =7040 let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv) =
7026 read_intra_mode_rect(7041 read_intra_mode_rect(
7027 dec,7042 dec,
@@ -7059,12 +7074,6 @@ fn decode_leaf_rect(
7059 palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors));7074 palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors));
7060 let (pal_uv_size, pal_uv_colors) =7075 let (pal_uv_size, pal_uv_colors) =
7061 palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors));7076 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- };
7068 if std::env::var_os("EC_AV1_RECTX_TRACE").is_some() {7077 if std::env::var_os("EC_AV1_RECTX_TRACE").is_some() {
7069 eprintln!(" skip={skip} mode={mode} angle_delta_y={angle_delta_y} uv_mode={uv_mode}");7078 eprintln!(" skip={skip} mode={mode} angle_delta_y={angle_delta_y} uv_mode={uv_mode}");
7070 }7079 }
@@ -7147,7 +7156,6 @@ fn decode_leaf_rect(
7147 )?;7156 )?;
7148 record_strip_palette(7157 record_strip_palette(
7149 neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors,7158 neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors,
7150- pal_last,
7151 );7159 );
7152 return Ok(mode);7160 return Ok(mode);
7153 }7161 }
@@ -7271,7 +7279,6 @@ fn decode_leaf_rect(
7271 // real left neighbour leaf 1 reads (lane-kf900 r4).7279 // real left neighbour leaf 1 reads (lane-kf900 r4).
7272 record_strip_palette(7280 record_strip_palette(
7273 neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors,7281 neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors,
7274- pal_last,
7275 );7282 );
7276 // A rect leaf spans mi_w x mi_h, so its mode reaches several columns and7283 // A rect leaf spans mi_w x mi_h, so its mode reaches several columns and
7277 // rows -- record the whole span, not one cell.7284 // rows -- record the whole span, not one cell.
@@ -7341,9 +7348,9 @@ fn decode_block_rect4(
7341 // `palette_colors_y` cache hit off an EMPTY cache -- right symbol count,7348 // `palette_colors_y` cache hit off an EMPTY cache -- right symbol count,
7342 // wrong interval, so a screen-content tile desynced at the first strip7349 // wrong interval, so a screen-content tile desynced at the first strip
7343 // whose neighbour used a palette (class [[cdf-row-held-constant]]).7350 // whose neighbour used a palette (class [[cdf-row-held-constant]]).
7344- let pal_at = (mi_r / (SUB / MI), mi_c / (SUB / MI));7351+ let pal_at = (mi_r, mi_c);
7345- let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(pal_at, mi_r);7352+ let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(pal_at);
7346- let palette_uv_cache = neighbours.palette_uv_cache_mi(pal_at, mi_r);7353+ let palette_uv_cache = neighbours.palette_uv_cache_mi(pal_at);
7347 let (7354 let (
7348 skip,7355 skip,
7349 mode,7356 mode,
@@ -7387,10 +7394,6 @@ fn decode_block_rect4(
7387 p.map.iter().map(|&i| p.v_colors[i as usize]).collect(),7394 p.map.iter().map(|&i| p.v_colors[i as usize]).collect(),
7388 )7395 )
7389 });7396 });
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 };
7394 let (pal_y_size, pal_y_colors) =7397 let (pal_y_size, pal_y_colors) =
7395 palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors));7398 palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors));
7396 let (pal_uv_size, pal_uv_colors) =7399 let (pal_uv_size, pal_uv_colors) =
@@ -7478,7 +7481,7 @@ fn decode_block_rect4(
7478 )?;7481 )?;
7479 record_strip_palette(7482 record_strip_palette(
7480 neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size,7483 neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size,
7481- pal_uv_colors, pal_last,7484+ pal_uv_colors,
7482 );7485 );
7483 if bw > bh {7486 if bw > bh {
7484 RECT4_32_HORZ_HITS.with(|c| c.set(c.get() + 1));7487 RECT4_32_HORZ_HITS.with(|c| c.set(c.get() + 1));
@@ -7631,7 +7634,7 @@ fn decode_block_rect4(
7631 neighbours.fill_lf_grid_rect((mi_r, mi_c), mi_w, mi_h, bw as u8, bh as u8, 0);7634 neighbours.fill_lf_grid_rect((mi_r, mi_c), mi_w, mi_h, bw as u8, bh as u8, 0);
7632 record_strip_palette(7635 record_strip_palette(
7633 neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size,7636 neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size,
7634- pal_uv_colors, pal_last,7637+ pal_uv_colors,
7635 );7638 );
7636 if bw > bh {7639 if bw > bh {
7637 RECT4_32_HORZ_HITS.with(|c| c.set(c.get() + 1));7640 RECT4_32_HORZ_HITS.with(|c| c.set(c.get() + 1));
@@ -7694,7 +7697,6 @@ fn decode_block_rect4(
7694 neighbours.fill_lf_grid_rect((mi_r, mi_c), mi_w, mi_h, bw as u8, bh as u8, 0);7697 neighbours.fill_lf_grid_rect((mi_r, mi_c), mi_w, mi_h, bw as u8, bh as u8, 0);
7695 record_strip_palette(7698 record_strip_palette(
7696 neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors,7699 neighbours, pal_at, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors,
7697- pal_last,
7698 );7700 );
7699 if bw > bh {7701 if bw > bh {
7700 RECT4_32_HORZ_HITS.with(|c| c.set(c.get() + 1));7702 RECT4_32_HORZ_HITS.with(|c| c.set(c.get() + 1));
@@ -7778,9 +7780,8 @@ fn decode_rect4_16(
7778 // lane-kf900 r4: the strip's real palette ctx + colour cache (see7780 // lane-kf900 r4: the strip's real palette ctx + colour cache (see
7779 // [`decode_block_rect4`]); `None`/`&[]` here read `palette_y_mode`7781 // [`decode_block_rect4`]); `None`/`&[]` here read `palette_y_mode`
7780 // off CDF row 0 and every cache hit off an empty cache.7782 // off CDF row 0 and every cache hit off an empty cache.
7781- let (palette_ctx, palette_cache) =7783+ let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(lmi);
7782- neighbours.palette_ctx_and_cache_mi(at16, lmi.0);7784+ let palette_uv_cache = neighbours.palette_uv_cache_mi(lmi);
7783- let palette_uv_cache = neighbours.palette_uv_cache_mi(at16, lmi.0);
7784 let (7785 let (
7785 skip,7786 skip,
7786 mode,7787 mode,
@@ -8180,8 +8181,7 @@ fn decode_rect4_16(
8180 let (pal_uv_size, pal_uv_colors) =8181 let (pal_uv_size, pal_uv_colors) =
8181 palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors));8182 palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors));
8182 record_strip_palette(8183 record_strip_palette(
8183- neighbours, at16, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors,8184+ neighbours, lmi, bw, bh, pal_y_size, pal_y_colors, pal_uv_size, pal_uv_colors,
8184- i == 3,
8185 );8185 );
8186 last_mode = mode;8186 last_mode = mode;
8187 prev = Some((lmi, mode));8187 prev = Some((lmi, mode));
@@ -8376,10 +8376,10 @@ fn decode_block_rect64(
8376 // split-tx superblock strip would otherwise leave the above/left8376 // split-tx superblock strip would otherwise leave the above/left
8377 // palette arrays holding a dead block's colours.8377 // palette arrays holding a dead block's colours.
8378 let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors));8378 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);
8380 let (puv_size, puv_colors) =8380 let (puv_size, puv_colors) =
8381 palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors));8381 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);
8383 RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1));8383 RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1));
8384 return Ok(());8384 return Ok(());
8385 }8385 }
@@ -8691,10 +8691,10 @@ fn decode_block_rect64(
8691 RECT_COEFF_HITS.with(|c| c.set(c.get() + 1));8691 RECT_COEFF_HITS.with(|c| c.set(c.get() + 1));
8692 }8692 }
8693 let (py_size, py_colors) = palette_y.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.colors));8693 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);
8695 let (puv_size, puv_colors) =8695 let (puv_size, puv_colors) =
8696 palette_uv.as_ref().map_or((0, [0u16; 8]), |p| (p.size, p.u_colors));8696 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);
8698 neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip);8698 neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip);
8699 neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0);8699 neighbours.fill_lf_grid_rect((mi_r, mi_c), bw / MI, bh / MI, tx_w as u8, tx_h as u8, 0);
8700 SB_RECT_HITS.with(|c| c.set(c.get() + 1));8700 SB_RECT_HITS.with(|c| c.set(c.get() + 1));
@@ -10657,9 +10657,8 @@ fn decode_leaf8(
10657 // above/left palette state over its own cell so the next block's ctx and10657 // above/left palette state over its own cell so the next block's ctx and
10658 // colour cache do not read a previous block's palette (lane-palette2 r8).10658 // colour cache do not read a previous block's palette (lane-palette2 r8).
10659 {10659 {
10660- let pal_at = (leaf_mi.0 / (SUB / MI), leaf_mi.1 / (SUB / MI));10660+ neighbours.record_palette_y_rect(leaf_mi, 8, 8, 0, [0u16; 8]);
10661- neighbours.record_palette_y_rect(pal_at, 8, 8, 0, [0u16; 8]);10661+ neighbours.record_palette_uv_rect(leaf_mi, 8, 8, 0, [0u16; 8]);
10662- neighbours.record_palette_uv_rect(pal_at, 8, 8, 0, [0u16; 8]);
10663 }10662 }
10664 neighbours.fill_skip_grid(leaf_mi, 2, skip);10663 neighbours.fill_skip_grid(leaf_mi, 2, skip);
10665 neighbours.fill_lf_grid(leaf_mi, 2, 4, 0);10664 neighbours.fill_lf_grid(leaf_mi, 2, 4, 0);
@@ -10671,9 +10670,8 @@ fn decode_leaf8(
10671 // above/left palette state over its own cell so the next block's ctx and10670 // above/left palette state over its own cell so the next block's ctx and
10672 // colour cache do not read a previous block's palette (lane-palette2 r8).10671 // colour cache do not read a previous block's palette (lane-palette2 r8).
10673 {10672 {
10674- let pal_at = (leaf_mi.0 / (SUB / MI), leaf_mi.1 / (SUB / MI));10673+ neighbours.record_palette_y_rect(leaf_mi, 8, 8, 0, [0u16; 8]);
10675- neighbours.record_palette_y_rect(pal_at, 8, 8, 0, [0u16; 8]);10674+ neighbours.record_palette_uv_rect(leaf_mi, 8, 8, 0, [0u16; 8]);
10676- neighbours.record_palette_uv_rect(pal_at, 8, 8, 0, [0u16; 8]);
10677 }10675 }
10678 neighbours.fill_skip_grid(leaf_mi, 2, skip);10676 neighbours.fill_skip_grid(leaf_mi, 2, skip);
10679 // lane-kf900 r1: the leaf's own RESOLVED transform size, never the literal10677 // lane-kf900 r1: the leaf's own RESOLVED transform size, never the literal
@@ -23277,19 +23275,19 @@ mod tests {
23277 fn a_sub16_leaf_after_a_palette_block_yields_palette_ctx_zero() {23275 fn a_sub16_leaf_after_a_palette_block_yields_palette_ctx_zero() {
23278 let mut n = Neighbours::new(4, 4, 16, 16);23276 let mut n = Neighbours::new(4, 4, 16, 16);
23279 // A real 16x16 palette block covering SUB cell (1, 1).23277 // 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]);
23281 let (ctx, cache) = n.palette_ctx_and_cache((1, 1));23279 let (ctx, cache) = n.palette_ctx_and_cache((1, 1));
23282 assert_eq!(ctx, 2, "the palette block itself must be visible");23280 assert_eq!(ctx, 2, "the palette block itself must be visible");
23283 assert_eq!(cache, vec![7, 9]);23281 assert_eq!(cache, vec![7, 9]);
23284 // An 8x8 non-palette leaf in that same cell.23282 // 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]);
23286 let (ctx, cache) = n.palette_ctx_and_cache((1, 1));23284 let (ctx, cache) = n.palette_ctx_and_cache((1, 1));
23287 assert_eq!(ctx, 0, "a sub-16 non-palette leaf must clear its own cell");23285 assert_eq!(ctx, 0, "a sub-16 non-palette leaf must clear its own cell");
23288 assert!(cache.is_empty(), "and its stale colour cache with it");23286 assert!(cache.is_empty(), "and its stale colour cache with it");
23289 // Same for the chroma half.23287 // 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]);
23291 assert_eq!(n.palette_uv_cache((1, 1)), vec![1, 2]);23289 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]);
23293 assert!(n.palette_uv_cache((1, 1)).is_empty());23291 assert!(n.palette_uv_cache((1, 1)).is_empty());
23294 }23292 }
23295 23293
crates/ec-av1/src/stream.rsmodified+21 -1
@@ -2974,10 +2974,30 @@ mod tests {
2974 "--enable-1to4-partitions=1",2974 "--enable-1to4-partitions=1",
2975 "--min-partition-size=8",2975 "--min-partition-size=8",
2976 ];2976 ];
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] = [
2978 ("testsrc2-pinned", "testsrc2=s=256x192:r=25".to_string(), &[]),2996 ("testsrc2-pinned", "testsrc2=s=256x192:r=25".to_string(), &[]),
2979 ("testsrc2-screen", "testsrc2=s=256x192:r=25".to_string(), screen),2997 ("testsrc2-screen", "testsrc2=s=256x192:r=25".to_string(), screen),
2980 ("smptebars-screen", "smptebars=size=256x192:rate=25".to_string(), screen),2998 ("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),
2981 ];3001 ];
2982 let (mut fired_arms, mut out_of_scope, mut out_of_scope_mismatch) = (0u32, 0u32, 0u32);3002 let (mut fired_arms, mut out_of_scope, mut out_of_scope_mismatch) = (0u32, 0u32, 0u32);
2983 let mut refused = 0u32;3003 let mut refused = 0u32;