forge
Explore
Log in

Don't you dare go Hollow

wip(palette2): builder's in-flight edits, committed verbatim at the 75-turn cap (state unverified by me)

Browse files at this commit

7d58d310b46477845137b52dc5f52859b3f3dc5bAhmet Kaan GümüşParent: 5feaca3
crates/ec-av1/src/decode.rsmodified+280 -24
@@ -451,6 +451,42 @@ pub(crate) fn palette_hits() -> usize {
451 PALETTE_HITS.with(|c| c.get())451 PALETTE_HITS.with(|c| c.get())
452 }452 }
453 453
454+// lane-palette2 r1: as [`PALETTE_HITS`], for a real (nonzero-size) chroma
455+// palette use -- proves a stream actually reconstructed UV palette pixels,
456+// not just read and refused the `palette_uv_mode` symbol.
457+thread_local! {
458+ static PALETTE_UV_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
459+}
460+
461+/// Current value of [`PALETTE_UV_HITS`].
462+pub(crate) fn palette_uv_hits() -> usize {
463+ PALETTE_UV_HITS.with(|c| c.get())
464+}
465+
466+// lane-palette2 r1: how many HORZ/VERT rect intra strips ([`read_intra_mode_rect`])
467+// decoded a real palette-Y use -- proves the rect strip's own palette syntax
468+// (not just the square path's) actually fired.
469+thread_local! {
470+ static PALETTE_RECT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
471+}
472+
473+/// Current value of [`PALETTE_RECT_HITS`].
474+pub(crate) fn palette_rect_hits() -> usize {
475+ PALETTE_RECT_HITS.with(|c| c.get())
476+}
477+
478+// lane-palette2 r1: how many palette-Y blocks decoded with a split luma
479+// transform (`tx_select && logical_tx != side`) -- proves the per-TU index-map
480+// slicing actually ran, not just that the whole-block path stayed exact.
481+thread_local! {
482+ static PALETTE_SPLIT_TX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
483+}
484+
485+/// Current value of [`PALETTE_SPLIT_TX_HITS`].
486+pub(crate) fn palette_split_tx_hits() -> usize {
487+ PALETTE_SPLIT_TX_HITS.with(|c| c.get())
488+}
489+
454 thread_local! {490 thread_local! {
455 /// A just-decoded palette-Y block's own predicted pixels491 /// A just-decoded palette-Y block's own predicted pixels
456 /// (`side*side`, row-major, `colors[map[i]]`), consumed by492 /// (`side*side`, row-major, `colors[map[i]]`), consumed by
@@ -1630,6 +1666,13 @@ struct Neighbours {
1630 left_palette_size: Vec<usize>,1666 left_palette_size: Vec<usize>,
1631 above_palette_colors: Vec<[u16; 8]>,1667 above_palette_colors: Vec<[u16; 8]>,
1632 left_palette_colors: Vec<[u16; 8]>,1668 left_palette_colors: Vec<[u16; 8]>,
1669+ /// As the four fields above, for the neighbour's own chroma (U-channel
1670+ /// only -- `av1_get_palette_cache`'s plane-1 lookup, [`Self::palette_uv_cache`])
1671+ /// palette size/colours (lane-palette2 r1).
1672+ above_palette_uv_size: Vec<usize>,
1673+ left_palette_uv_size: Vec<usize>,
1674+ above_palette_uv_colors: Vec<[u16; 8]>,
1675+ left_palette_uv_colors: Vec<[u16; 8]>,
1633 /// This tile's own top-left corner, in 4x4 mode-info units (spec1676 /// This tile's own top-left corner, in 4x4 mode-info units (spec
1634 /// `MiRowStart`/`MiColStart`) -- lane-tiles: every "is there really a1677 /// `MiRowStart`/`MiColStart`) -- lane-tiles: every "is there really a
1635 /// neighbour" check compares against this instead of literal `0`, since a1678 /// neighbour" check compares against this instead of literal `0`, since a
@@ -1684,6 +1727,10 @@ impl Neighbours {
1684 left_palette_size: vec![0; rows],1727 left_palette_size: vec![0; rows],
1685 above_palette_colors: vec![[0u16; 8]; cols],1728 above_palette_colors: vec![[0u16; 8]; cols],
1686 left_palette_colors: vec![[0u16; 8]; rows],1729 left_palette_colors: vec![[0u16; 8]; rows],
1730+ above_palette_uv_size: vec![0; cols],
1731+ left_palette_uv_size: vec![0; rows],
1732+ above_palette_uv_colors: vec![[0u16; 8]; cols],
1733+ left_palette_uv_colors: vec![[0u16; 8]; rows],
1687 tile_row0_mi: 0,1734 tile_row0_mi: 0,
1688 tile_col0_mi: 0,1735 tile_col0_mi: 0,
1689 }1736 }
@@ -1789,6 +1836,70 @@ impl Neighbours {
1789 (ctx, cache)1836 (ctx, cache)
1790 }1837 }
1791 1838
1839+ /// As [`Self::record_palette_y`], for a just-decoded block's own chroma
1840+ /// (U-channel) palette size/colours -- `size == 0` clears stale state the
1841+ /// same way (lane-palette2 r1).
1842+ fn record_palette_uv(&mut self, at: (usize, usize), side: usize, size: usize, colors: [u16; 8]) {
1843+ let (r, c) = at;
1844+ for cell in 0..side / SUB {
1845+ self.above_palette_uv_size[c + cell] = size;
1846+ self.above_palette_uv_colors[c + cell] = colors;
1847+ }
1848+ for cell in 0..side / SUB {
1849+ self.left_palette_uv_size[r + cell] = size;
1850+ self.left_palette_uv_colors[r + cell] = colors;
1851+ }
1852+ }
1853+
1854+ /// [`Self::palette_ctx_and_cache`]'s cache half, for the U channel
1855+ /// (`av1_get_palette_cache(xd, 1, cache)`) -- `palette_uv_mode_ctx` needs
1856+ /// no neighbour lookup at all (it is this block's own just-decided Y
1857+ /// palette use, `read_palette_mode_info`'s `pmi->palette_size[0] > 0`),
1858+ /// so only the cache is wanted here.
1859+ fn palette_uv_cache(&self, at: (usize, usize)) -> Vec<u16> {
1860+ let (r, c) = at;
1861+ let above_ok = r % 4 != 0;
1862+ let (above_n, above_colors) = if above_ok && self.above_palette_uv_size[c] > 0 {
1863+ (self.above_palette_uv_size[c], self.above_palette_uv_colors[c])
1864+ } else {
1865+ (0, [0u16; 8])
1866+ };
1867+ let (left_n, left_colors) = if self.left_palette_uv_size[r] > 0 {
1868+ (self.left_palette_uv_size[r], self.left_palette_uv_colors[r])
1869+ } else {
1870+ (0, [0u16; 8])
1871+ };
1872+ let mut cache = Vec::with_capacity(16);
1873+ let push_dedup = |cache: &mut Vec<u16>, v: u16| {
1874+ if cache.last() != Some(&v) {
1875+ cache.push(v);
1876+ }
1877+ };
1878+ let (mut ai, mut li) = (0usize, 0usize);
1879+ while ai < above_n && li < left_n {
1880+ let (va, vl) = (above_colors[ai], left_colors[li]);
1881+ if vl < va {
1882+ push_dedup(&mut cache, vl);
1883+ li += 1;
1884+ } else {
1885+ push_dedup(&mut cache, va);
1886+ ai += 1;
1887+ if vl == va {
1888+ li += 1;
1889+ }
1890+ }
1891+ }
1892+ while ai < above_n {
1893+ push_dedup(&mut cache, above_colors[ai]);
1894+ ai += 1;
1895+ }
1896+ while li < left_n {
1897+ push_dedup(&mut cache, left_colors[li]);
1898+ li += 1;
1899+ }
1900+ cache
1901+ }
1902+
1792 /// Marks every 4x4 mi cell a just-decoded coded block covers with its own1903 /// Marks every 4x4 mi cell a just-decoded coded block covers with its own
1793 /// transform width (in pixels) and `is_inter` flag -- the loop filter's1904 /// transform width (in pixels) and `is_inter` flag -- the loop filter's
1794 /// per-edge lookup (`apply_deblock`), filled at the same call sites and1905 /// per-edge lookup (`apply_deblock`), filled at the same call sites and
@@ -2339,15 +2450,22 @@ fn filter_intra_size_class_rect(bw: usize, bh: usize) -> Option<usize> {
2339 /// `av1_allow_palette`'s size bound (blockd.h: `block_size_wide/high <=2450 /// `av1_allow_palette`'s size bound (blockd.h: `block_size_wide/high <=
2340 /// MAX_PALETTE_BLOCK_WIDTH/HEIGHT` (64) and `sb_type >= BLOCK_8X8`) plus2451 /// MAX_PALETTE_BLOCK_WIDTH/HEIGHT` (64) and `sb_type >= BLOCK_8X8`) plus
2341 /// `av1_get_palette_bsize_ctx` (pred_common.h: `num_pels_log2_lookup[bsize]2452 /// `av1_get_palette_bsize_ctx` (pred_common.h: `num_pels_log2_lookup[bsize]
2342-/// - num_pels_log2_lookup[BLOCK_8X8]`, i.e. `2*log2(side) - 6` for this2453+/// - num_pels_log2_lookup[BLOCK_8X8]`, i.e. `log2(bw*bh) - 6`) -- `None` past
2343-/// decoder's square-only blocks) -- `None` past the bound (never gates a2454+/// the bound (never gates a palette read), `Some(bsize_ctx)` otherwise.
2344-/// palette read), `Some(bsize_ctx)` otherwise.
2345 fn palette_bsize_ctx(side: usize) -> Option<usize> {2455 fn palette_bsize_ctx(side: usize) -> Option<usize> {
2346- match side {2456+ palette_bsize_ctx_wh(side, side)
2347- 8 => Some(0),2457+}
2348- 16 => Some(2),2458+
2349- 32 => Some(4),2459+/// [`palette_bsize_ctx`], generalised to a true `bw`x`bh` rect strip
2350- 64 => Some(6),2460+/// (lane-palette2 r1) -- the same `log2(bw*bh) - 6` formula, this decoder's
2461+/// square call sites are just `bw == bh`.
2462+fn palette_bsize_ctx_wh(bw: usize, bh: usize) -> Option<usize> {
2463+ match (bw, bh) {
2464+ (8, 8) => Some(0),
2465+ (16, 16) => Some(2),
2466+ (32, 16) | (16, 32) => Some(3),
2467+ (32, 32) => Some(4),
2468+ (64, 64) => Some(6),
2351 _ => None,2469 _ => None,
2352 }2470 }
2353 }2471 }
@@ -2361,6 +2479,17 @@ struct PaletteY {
2361 map: Vec<u8>,2479 map: Vec<u8>,
2362 }2480 }
2363 2481
2482+/// As [`PaletteY`], for a just-decoded chroma palette block: one shared
2483+/// per-pixel colour-index map (spec `av1_visit_palette`'s plane-1 pass is
2484+/// shared between U and V, both co-located after 4:2:0 subsampling) plus
2485+/// each plane's own base colours.
2486+struct PaletteUv {
2487+ size: usize,
2488+ u_colors: [u16; 8],
2489+ v_colors: [u16; 8],
2490+ map: Vec<u8>,
2491+}
2492+
2364 /// `aom_ceil_log2` (aom_dsp/bitwriter_buffer.h-adjacent helper, used both by2493 /// `aom_ceil_log2` (aom_dsp/bitwriter_buffer.h-adjacent helper, used both by
2365 /// `read_palette_colors_y`'s shrinking `bits` and `av1_read_uniform`'s2494 /// `read_palette_colors_y`'s shrinking `bits` and `av1_read_uniform`'s
2366 /// `get_unsigned_bits`, which is the same formula): smallest `n` with2495 /// `get_unsigned_bits`, which is the same formula): smallest `n` with
@@ -2446,6 +2575,74 @@ fn read_palette_colors_y(dec: &mut SymbolDecoder, n: usize, cache: &[u16]) -> [u
2446 colors2575 colors
2447 }2576 }
2448 2577
2578+/// `read_palette_colors_uv` (decodemv.c:509), bit_depth fixed at 8: U reads
2579+/// exactly [`read_palette_colors_y`]'s cache/delta scheme (against the U-only
2580+/// cache, `range` starting at `255 - prev` with no `-1`, and each raw `delta`
2581+/// unbiased by `+1` -- both differences from Y's own reader, matching the C
2582+/// side by side). V never uses a cache: a leading bit picks either `n` raw
2583+/// literals, or a first raw value plus `n-1` signed deltas that wrap mod 256
2584+/// rather than clamp.
2585+fn read_palette_colors_uv(dec: &mut SymbolDecoder, n: usize, cache: &[u16]) -> ([u16; 8], [u16; 8]) {
2586+ let mut cached = Vec::with_capacity(n);
2587+ for &c in cache {
2588+ if cached.len() >= n {
2589+ break;
2590+ }
2591+ if dec.literal(1) == 1 {
2592+ cached.push(c);
2593+ }
2594+ }
2595+ let mut u_colors = [0u16; 8];
2596+ let n_cached = cached.len();
2597+ if n_cached < n {
2598+ let mut transmitted = Vec::with_capacity(n - n_cached);
2599+ let first = dec.literal(8) as u16;
2600+ transmitted.push(first);
2601+ if n_cached + 1 < n {
2602+ let min_bits = 8u32 - 3;
2603+ let mut bits = min_bits + dec.literal(2);
2604+ let mut range = 255i32 - i32::from(first);
2605+ for _ in (n_cached + 1)..n {
2606+ let delta = dec.literal(bits) as i32;
2607+ let prev = *transmitted.last().unwrap();
2608+ let val = (i32::from(prev) + delta).clamp(0, 255) as u16;
2609+ range -= i32::from(val) - i32::from(prev);
2610+ bits = bits.min(ceil_log2(range.max(0) as u32));
2611+ transmitted.push(val);
2612+ }
2613+ }
2614+ let merged = merge_colors(&transmitted, &cached);
2615+ u_colors[..n].copy_from_slice(&merged);
2616+ } else {
2617+ u_colors[..n].copy_from_slice(&cached[..n]);
2618+ }
2619+ let mut v_colors = [0u16; 8];
2620+ if dec.literal(1) == 1 {
2621+ let min_bits_v = 8u32 - 4;
2622+ let bits = min_bits_v + dec.literal(2);
2623+ v_colors[0] = dec.literal(8) as u16;
2624+ for i in 1..n {
2625+ let mut delta = dec.literal(bits) as i32;
2626+ if delta != 0 && dec.literal(1) == 1 {
2627+ delta = -delta;
2628+ }
2629+ let mut val = i32::from(v_colors[i - 1]) + delta;
2630+ if val < 0 {
2631+ val += 256;
2632+ }
2633+ if val >= 256 {
2634+ val -= 256;
2635+ }
2636+ v_colors[i] = val as u16;
2637+ }
2638+ } else {
2639+ for i in 0..n {
2640+ v_colors[i] = dec.literal(8) as u16;
2641+ }
2642+ }
2643+ (u_colors, v_colors)
2644+}
2645+
2449 /// `av1_palette_color_index_context_lookup` (entropymode.c:889): maps a2646 /// `av1_palette_color_index_context_lookup` (entropymode.c:889): maps a
2450 /// `NUM_PALETTE_NEIGHBORS`-weighted score hash (`0..=8`) to the real2647 /// `NUM_PALETTE_NEIGHBORS`-weighted score hash (`0..=8`) to the real
2451 /// `0..=4` context, `-1` for hashes no real neighbour configuration produces.2648 /// `0..=4` context, `-1` for hashes no real neighbour configuration produces.
@@ -2514,7 +2711,13 @@ fn palette_color_index_context(
2514 /// region (every other skip/residual path here does the same), so no2711 /// region (every other skip/residual path here does the same), so no
2515 /// rows/cols-vs-plane_width/height cropping special case is needed, unlike2712 /// rows/cols-vs-plane_width/height cropping special case is needed, unlike
2516 /// libaom's own non-block-aligned split.2713 /// libaom's own non-block-aligned split.
2517-fn decode_color_index_map(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, n: usize, side: usize) -> Vec<u8> {2714+fn decode_color_index_map(
2715+ dec: &mut SymbolDecoder,
2716+ cdfs: &mut Cdfs,
2717+ n: usize,
2718+ side: usize,
2719+ uv: bool,
2720+) -> Vec<u8> {
2518 let trace = std::env::var_os("EC_AV1_TRACE").is_some();2721 let trace = std::env::var_os("EC_AV1_TRACE").is_some();
2519 let mut map = vec![0u8; side * side];2722 let mut map = vec![0u8; side * side];
2520 if trace {2723 if trace {
@@ -2537,7 +2740,11 @@ fn decode_color_index_map(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, n: usize, si
2537 let (rng, _) = dec.debug_state();2740 let (rng, _) = dec.debug_state();
2538 eprintln!("EC_PAL row={row} col={col} ctx={ctx} n={n} rng={rng}");2741 eprintln!("EC_PAL row={row} col={col} ctx={ctx} n={n} rng={rng}");
2539 }2742 }
2540- let symbol = dec.symbol(&mut cdfs.palette_y_color_index[n - 2][ctx][..=n]);2743+ let symbol = if uv {
2744+ dec.symbol(&mut cdfs.palette_uv_color_index[n - 2][ctx][..=n])
2745+ } else {
2746+ dec.symbol(&mut cdfs.palette_y_color_index[n - 2][ctx][..=n])
2747+ };
2541 map[row * side + col] = color_order[symbol];2748 map[row * side + col] = color_order[symbol];
2542 if trace {2749 if trace {
2543 let (rng, _) = dec.debug_state();2750 let (rng, _) = dec.debug_state();
@@ -3056,6 +3263,11 @@ fn read_intra_mode(
3056 // decoder reads either way -- but refuse by name the moment it fires,3263 // decoder reads either way -- but refuse by name the moment it fires,
3057 // exactly the old behaviour.3264 // exactly the old behaviour.
3058 palette: Option<(usize, &[u16])>,3265 palette: Option<(usize, &[u16])>,
3266+ // The U-channel colour cache for a chroma palette read ([`Neighbours::palette_uv_cache`]),
3267+ // `&[]` at the excluded call sites -- same corner-cut as `palette` above,
3268+ // no `mode_ctx` needed (`palette_uv_mode_ctx` is this block's own just-read
3269+ // `use_palette_y`, not a neighbour lookup).
3270+ palette_uv_cache: &[u16],
3059 mi_r: usize,3271 mi_r: usize,
3060 mi_c: usize,3272 mi_c: usize,
3061 ) -> Result<(3273 ) -> Result<(
@@ -3067,6 +3279,7 @@ fn read_intra_mode(
3067 Option<(i32, i32)>,3279 Option<(i32, i32)>,
3068 Option<usize>,3280 Option<usize>,
3069 Option<PaletteY>,3281 Option<PaletteY>,
3282+ Option<PaletteUv>,
3070 )> {3283 )> {
3071 let trace = std::env::var_os("EC_AV1_TRACE").is_some();3284 let trace = std::env::var_os("EC_AV1_TRACE").is_some();
3072 let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0;3285 let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0;
@@ -3174,6 +3387,7 @@ fn read_intra_mode(
3174 // bit-for-bit through the Y colours, diverged only at the old inline3387 // bit-for-bit through the Y colours, diverged only at the old inline
3175 // `decode_color_index_map` call).3388 // `decode_color_index_map` call).
3176 let mut palette_y_pending: Option<(usize, [u16; 8])> = None;3389 let mut palette_y_pending: Option<(usize, [u16; 8])> = None;
3390+ let mut palette_uv_pending: Option<(usize, [u16; 8], [u16; 8])> = None;
3177 if allow_screen_content_tools3391 if allow_screen_content_tools
3178 && let Some(bsize_ctx) = palette_bsize_ctx(side)3392 && let Some(bsize_ctx) = palette_bsize_ctx(side)
3179 {3393 {
@@ -3215,15 +3429,12 @@ fn read_intra_mode(
3215 // UV_DC_PRED`, regardless of whether Y just fired -- this decoder's3429 // UV_DC_PRED`, regardless of whether Y just fired -- this decoder's
3216 // scope always reconstructs chroma alongside luma (`decode_block`3430 // scope always reconstructs chroma alongside luma (`decode_block`
3217 // always calls `u.reconstruct`/`v.reconstruct`), so `is_chroma_ref`3431 // always calls `u.reconstruct`/`v.reconstruct`), so `is_chroma_ref`
3218- // is always true here and does not need threading in. UV palette3432+ // is always true here and does not need threading in.
3219- // reconstruction stays out of scope: refusing the moment the mode
3220- // symbol fires needs no further reads (the whole decode aborts),
3221- // so `palette_uv_size`/`read_palette_colors_uv` are never reached.
3222 let palette_uv_mode_ctx = usize::from(use_palette_y);3433 let palette_uv_mode_ctx = usize::from(use_palette_y);
3223 if uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 {3434 if uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 {
3224- return Err(unsupported(3435+ let n = 2 + dec.symbol(&mut cdfs.palette_uv_size[bsize_ctx]);
3225- "a block that actually uses a palette (UV) -- reconstruction is out of scope",3436+ let (u_colors, v_colors) = read_palette_colors_uv(dec, n, palette_uv_cache);
3226- ));3437+ palette_uv_pending = Some((n, u_colors, v_colors));
3227 }3438 }
3228 }3439 }
3229 // `read_filter_intra_mode_info` (spec 5.11.14, libaom decodemv.c): a3440 // `read_filter_intra_mode_info` (spec 5.11.14, libaom decodemv.c): a
@@ -3254,19 +3465,28 @@ fn read_intra_mode(
3254 filter_intra = Some(fi_mode);3465 filter_intra = Some(fi_mode);
3255 }3466 }
3256 }3467 }
3257- // `av1_visit_palette` (decoder.c:234): the colour-index-map decode runs3468+ // `av1_visit_palette` (decoder.c:234): plane 0 (Y) then plane 1 (chroma,
3258- // here, after every mode-info symbol for the block (including UV3469+ // shared U/V map) -- the same order libaom's own loop visits them in.
3259- // palette and filter_intra above) -- plane 0 (Y) only in this decoder's
3260- // scope, since UV palette already refused by name above and never left
3261- // a pending map to decode.
3262 let palette_y = palette_y_pending.map(|(n, colors)| {3470 let palette_y = palette_y_pending.map(|(n, colors)| {
3263- let map = decode_color_index_map(dec, cdfs, n, side);3471+ let map = decode_color_index_map(dec, cdfs, n, side, false);
3264 PALETTE_HITS.with(|c| c.set(c.get() + 1));3472 PALETTE_HITS.with(|c| c.set(c.get() + 1));
3265 if trace {3473 if trace {
3266 eprintln!("TRACE palette_y size={n} colors={:?}", &colors[..n]);3474 eprintln!("TRACE palette_y size={n} colors={:?}", &colors[..n]);
3267 }3475 }
3268 PaletteY { size: n, colors, map }3476 PaletteY { size: n, colors, map }
3269 });3477 });
3478+ let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| {
3479+ let map = decode_color_index_map(dec, cdfs, n, side / 2, true);
3480+ PALETTE_UV_HITS.with(|c| c.set(c.get() + 1));
3481+ if trace {
3482+ eprintln!(
3483+ "TRACE palette_uv size={n} u_colors={:?} v_colors={:?}",
3484+ &u_colors[..n],
3485+ &v_colors[..n]
3486+ );
3487+ }
3488+ PaletteUv { size: n, u_colors, v_colors, map }
3489+ });
3270 Ok((3490 Ok((
3271 skip,3491 skip,
3272 mode,3492 mode,
@@ -3276,6 +3496,7 @@ fn read_intra_mode(
3276 alpha,3496 alpha,
3277 filter_intra,3497 filter_intra,
3278 palette_y,3498 palette_y,
3499+ palette_uv,
3279 ))3500 ))
3280 }3501 }
3281 3502
@@ -3766,7 +3987,8 @@ fn decode_block(
3766 let (r, c) = at;3987 let (r, c) = at;
3767 let (px, py) = (c * SUB, r * SUB);3988 let (px, py) = (c * SUB, r * SUB);
3768 let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache(at);3989 let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache(at);
3769- let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y) =3990+ let palette_uv_cache = neighbours.palette_uv_cache(at);
3991+ let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv) =
3770 read_intra_mode(3992 read_intra_mode(
3771 dec,3993 dec,
3772 cdfs,3994 cdfs,
@@ -3779,6 +4001,7 @@ fn decode_block(
3779 allow_screen_content_tools,4001 allow_screen_content_tools,
3780 allow_intrabc,4002 allow_intrabc,
3781 Some((palette_ctx, &palette_cache)),4003 Some((palette_ctx, &palette_cache)),
4004+ &palette_uv_cache,
3782 r * (SUB / MI),4005 r * (SUB / MI),
3783 c * (SUB / MI),4006 c * (SUB / MI),
3784 )?;4007 )?;
@@ -3842,6 +4065,17 @@ fn decode_block(
3842 let buf: Vec<u8> = py.map.iter().map(|&idx| py.colors[idx as usize] as u8).collect();4065 let buf: Vec<u8> = py.map.iter().map(|&idx| py.colors[idx as usize] as u8).collect();
3843 PALETTE_PRED.with(|c| *c.borrow_mut() = Some(buf));4066 PALETTE_PRED.with(|c| *c.borrow_mut() = Some(buf));
3844 }4067 }
4068+ // A UV-palette block's own chroma prediction override (lane-palette2 r1),
4069+ // same [`PALETTE_PRED`] slot as Y above -- set fresh right before each
4070+ // plane's own `reconstruct`/`read_plane` call below (never both planes at
4071+ // once), since Y's own set-then-take already emptied the slot by the time
4072+ // either of these run.
4073+ let palette_uv_bufs: Option<(Vec<u8>, Vec<u8>)> = palette_uv.as_ref().map(|puv| {
4074+ (
4075+ puv.map.iter().map(|&idx| puv.u_colors[idx as usize] as u8).collect(),
4076+ puv.map.iter().map(|&idx| puv.v_colors[idx as usize] as u8).collect(),
4077+ )
4078+ });
3845 if skip {4079 if skip {
3846 // A skipped block codes no residual syntax at all (spec 5.11.34):4080 // A skipped block codes no residual syntax at all (spec 5.11.34):
3847 // straight prediction, on every plane.4081 // straight prediction, on every plane.
@@ -3858,6 +4092,9 @@ fn decode_block(
3858 smooth_neighbor,4092 smooth_neighbor,
3859 );4093 );
3860 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));4094 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));
4095+ if let Some((ub, _)) = &palette_uv_bufs {
4096+ PALETTE_PRED.with(|c| *c.borrow_mut() = Some(ub.clone()));
4097+ }
3861 u.reconstruct(4098 u.reconstruct(
3862 cpx,4099 cpx,
3863 cpy,4100 cpy,
@@ -3870,6 +4107,9 @@ fn decode_block(
3870 None,4107 None,
3871 smooth_neighbor_uv,4108 smooth_neighbor_uv,
3872 );4109 );
4110+ if let Some((_, vb)) = &palette_uv_bufs {
4111+ PALETTE_PRED.with(|c| *c.borrow_mut() = Some(vb.clone()));
4112+ }
3873 v.reconstruct(4113 v.reconstruct(
3874 cpx,4114 cpx,
3875 cpy,4115 cpy,
@@ -3914,6 +4154,9 @@ fn decode_block(
3914 smooth_neighbor,4154 smooth_neighbor,
3915 )?;4155 )?;
3916 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));4156 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));
4157+ if let Some((ub, _)) = &palette_uv_bufs {
4158+ PALETTE_PRED.with(|c| *c.borrow_mut() = Some(ub.clone()));
4159+ }
3917 let u_grid = read_plane(4160 let u_grid = read_plane(
3918 dec,4161 dec,
3919 cdfs,4162 cdfs,
@@ -3942,6 +4185,9 @@ fn decode_block(
3942 &u.data[cpy * u.width + cpx..][..chroma_side]4185 &u.data[cpy * u.width + cpx..][..chroma_side]
3943 );4186 );
3944 }4187 }
4188+ if let Some((_, vb)) = &palette_uv_bufs {
4189+ PALETTE_PRED.with(|c| *c.borrow_mut() = Some(vb.clone()));
4190+ }
3945 let v_grid = read_plane(4191 let v_grid = read_plane(
3946 dec,4192 dec,
3947 cdfs,4193 cdfs,
@@ -4023,6 +4269,9 @@ fn decode_block(
4023 }4269 }
4024 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));4270 let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side));
4025 let chroma_around = neighbours.around(at, side);4271 let chroma_around = neighbours.around(at, side);
4272+ if let Some((ub, _)) = &palette_uv_bufs {
4273+ PALETTE_PRED.with(|c| *c.borrow_mut() = Some(ub.clone()));
4274+ }
4026 let u_grid = read_plane(4275 let u_grid = read_plane(
4027 dec,4276 dec,
4028 cdfs,4277 cdfs,
@@ -4045,6 +4294,9 @@ fn decode_block(
4045 None,4294 None,
4046 smooth_neighbor_uv,4295 smooth_neighbor_uv,
4047 )?;4296 )?;
4297+ if let Some((_, vb)) = &palette_uv_bufs {
4298+ PALETTE_PRED.with(|c| *c.borrow_mut() = Some(vb.clone()));
4299+ }
4048 let v_grid = read_plane(4300 let v_grid = read_plane(
4049 dec,4301 dec,
4050 cdfs,4302 cdfs,
@@ -4078,6 +4330,10 @@ fn decode_block(
4078 .as_ref()4330 .as_ref()
4079 .map_or((0, [0u16; 8]), |p| (p.size, p.colors));4331 .map_or((0, [0u16; 8]), |p| (p.size, p.colors));
4080 neighbours.record_palette_y(at, side, py_size, py_colors);4332 neighbours.record_palette_y(at, side, py_size, py_colors);
4333+ let (puv_size, puv_colors) = palette_uv
4334+ .as_ref()
4335+ .map_or((0, [0u16; 8]), |p| (p.size, p.u_colors));
4336+ neighbours.record_palette_uv(at, side, puv_size, puv_colors);
4081 neighbours.fill_skip_grid((r * (SUB / MI), c * (SUB / MI)), side / MI, skip);4337 neighbours.fill_skip_grid((r * (SUB / MI), c * (SUB / MI)), side / MI, skip);
4082 neighbours.fill_lf_grid(4338 neighbours.fill_lf_grid(
4083 (r * (SUB / MI), c * (SUB / MI)),4339 (r * (SUB / MI), c * (SUB / MI)),