From 4fbf70151d10a507fce619058e3aea312c93daba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 1 Sep 2026 21:33:37 +0300 Subject: [PATCH] feat(av1): palette under a split luma transform, plus two palette desync fixes - palette-Y prediction windowed per transform unit (decode.rs decode_block): with logical_tx != side each TU takes its own logical_tx-sized window of the block's index map instead of the whole-block buffer (which the FIRST unit would consume at the wrong stride); refusal "a palette block with a split luma transform (round 1)" lifted, counted by palette_split_tx_hits(). - filter_intra symbol is not written for a palette-Y block (av1_filter_intra_allowed, reconintra.h:77) -- we read one and desynced from every palette block <=32x32 (class symbol-consumption-gap); fixed on both the square (read_intra_mode) and rect (read_intra_mode_rect) paths. - palette_y_mode CDF row: the SB-top-row exclusion of the above neighbour belongs to the colour CACHE alone, not to av1_get_palette_mode_ctx, which reads plain xd->above_mbmi (pred_common.h:197). Applying it to the ctx made every SB in the frame's second SB row read the symbol off CDF row 0 while libaom used row 1 (class cdf-row-held-constant). - decode_leaf_rect reads palette syntax with its own real ctx/cache; the sub-16x16 rect palette RECONSTRUCTION stays refused by name. Gates (aomenc --enable-palette=1 --tune-content=screen --enable-rect-partitions=1, min/max-partition-size=16/64): a_real_aomenc_stream_with_rect_screen_content_ decodes_pixel_exact 14/70 pixel-exact, 13 through a split-transform palette block, palette_split_tx_hits=356; a_real_aomenc_stream_with_rect_palette_decodes_pixel_ exact 14/70, palette_rect_hits=78. --- crates/ec-av1/src/decode.rs | 89 ++++++++++++++++++++------ crates/ec-av1/src/refusal_inventory.rs | 2 +- crates/ec-av1/src/stream.rs | 26 ++++++-- 3 files changed, 93 insertions(+), 24 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index b2907b83..fe463246 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -581,9 +581,11 @@ pub(crate) fn rect_screen_content_hits() -> usize { RECT_SCREEN_CONTENT_HITS.with(|c| c.get()) } -// lane-palette2 r1: how many palette-Y blocks decoded with a split luma -// transform (`tx_select && logical_tx != side`) -- proves the per-TU index-map -// slicing actually ran, not just that the whole-block path stayed exact. +// lane-palette2 r1: how many palette-Y luma transform UNITS decoded under a +// split luma transform (`logical_tx != side`) -- proves the per-TU index-map +// windowing actually ran, not just that the whole-block path stayed exact. +// Counts units, not blocks (a 64x64 palette block split to 16x16 TUs adds 16); +// first actually incremented in r7, when the refusal it guarded was lifted. thread_local! { static PALETTE_SPLIT_TX_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; } @@ -2003,12 +2005,22 @@ impl Neighbours { /// `av1_get_palette_mode_ctx` (pred_common.h:197): count of the above/left /// neighbours that are themselves a palette-Y block, plus /// `av1_get_palette_cache`'s own merged, ascending, deduplicated colour - /// cache (pred_common.c:73) -- the above neighbour is excluded at a - /// superblock's own top row (`r % 4 == 0` in [`SUB`]-grid units, 4 cells - /// per 64px SB, `MIN_SB_SIZE_LOG2`), the left neighbour never is. + /// cache (pred_common.c:73). + /// + /// The SB-top-row exclusion of the above neighbour (`row % 64` in + /// pred_common.c:76, `r % 4 == 0` here in [`SUB`]-grid units) belongs to + /// the CACHE ALONE -- `av1_get_palette_mode_ctx` reads plain + /// `xd->above_mbmi` with no such guard (lane-palette2 r7). Applying it to + /// the ctx too made every superblock in the frame's second SB row read + /// `palette_y_mode` off CDF row 0 while libaom used row 1, so a real + /// palette block decoded as "no palette" and the tile desynced from + /// there (class [[cdf-row-held-constant]]). fn palette_ctx_and_cache(&self, at: (usize, usize)) -> (usize, Vec) { let (r, c) = at; let above_ok = r % 4 != 0; + // `xd->above_mbmi` exists for every row but the tile's own first. + let above_is_palette = + r * (SUB / MI) > self.tile_row0_mi && self.above_palette_size[c] > 0; let (above_n, above_colors) = if above_ok && self.above_palette_size[c] > 0 { (self.above_palette_size[c], self.above_palette_colors[c]) } else { @@ -2019,7 +2031,7 @@ impl Neighbours { } else { (0, [0u16; 8]) }; - let ctx = usize::from(above_n > 0) + usize::from(left_n > 0); + let ctx = usize::from(above_is_palette) + usize::from(left_n > 0); let mut cache = Vec::with_capacity(16); let push_dedup = |cache: &mut Vec, v: u16| { if cache.last() != Some(&v) { @@ -3141,8 +3153,11 @@ fn read_intra_mode_rect( filter_intra_size_class_rect(bw, bh) ); } + // As the square path: `av1_filter_intra_allowed` excludes a palette-Y + // block (reconintra.h:77), so no `use_filter_intra` symbol exists there. if mode == DC_PRED && enable_filter_intra + && palette_y_pending.is_none() && let Some(class) = filter_intra_size_class_rect(bw, bh) { let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0; @@ -3659,7 +3674,16 @@ fn decode_leaf_rect( if smooth_neighbor { SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); } - let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra) = + // The palette-Y mode CDF row is picked by the neighbours' palette use + // (`palette_ctx_and_cache`), so this sub-16x16 leaf must pass its own real + // ctx/cache -- reading `palette_y_mode` off row 0 when a neighbour did use + // a palette narrows the wrong interval and desyncs the tile even when the + // decoded bit is the same. The cell is the 16px-SUB cell containing the + // leaf (the palette neighbour arrays have no finer granularity). + let pal_at = (leaf_mi.0 / (SUB / MI), leaf_mi.1 / (SUB / MI)); + let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache(pal_at); + let palette_uv_cache = neighbours.palette_uv_cache(pal_at); + let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv) = read_intra_mode_rect( dec, cdfs, @@ -3671,9 +3695,19 @@ fn decode_leaf_rect( enable_filter_intra, neighbours.skip_txfm_ctx(leaf_mi.0, leaf_mi.1), allow_screen_content_tools, + Some((palette_ctx, &palette_cache)), + &palette_uv_cache, leaf_mi.0, leaf_mi.1, )?; + if palette_y.is_some() || palette_uv.is_some() { + // Symbols (mode/size/colours/index map) are all read above, so the + // tile stays in sync; only the *pixel* reconstruction of a palette on + // a below-16x16 strip is unported and ungated (lane-palette2 r7). + return Err(unsupported( + "a palette block on a HORZ/VERT intra strip below 16x16 (reconstruction not ported)", + )); + } if filter_intra.is_some() { return Err(unsupported( "filter intra on a HORZ/VERT strip (this decoder predicts square-only)", @@ -4381,8 +4415,14 @@ fn read_intra_mode( // matched bit-exact, then `eob_pt` read 5 instead of 6, because this // symbol's bits were never consumed). let mut filter_intra = None; + // `av1_filter_intra_allowed` (reconintra.h:77) also requires + // `palette_mode_info.palette_size[0] == 0`: a palette-Y block never gets a + // `use_filter_intra` symbol, so reading one here consumed a bit the + // encoder never wrote and desynced the tile from a palette block at + // <=32x32 onward (lane-palette2 r7, class [[symbol-consumption-gap]]). if mode == DC_PRED && enable_filter_intra + && palette_y_pending.is_none() && let Some(class) = filter_intra_size_class(side) { let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0; @@ -5016,18 +5056,21 @@ fn decode_block( // would need the map sliced per-TU -- untested against a real // `--enable-palette=1` stream this round, so refuse by name rather than // guess at the slicing (charter's own named-acceptable fallback). - if let Some(ref py) = palette_y { - if tx_select && logical_tx != side { - return Err(unsupported( - "a palette block with a split luma transform (round 1)", - )); - } - let buf: Vec = py - .map + let palette_y_buf: Option> = palette_y.as_ref().map(|py| { + py.map .iter() .map(|&idx| py.colors[idx as usize]) - .collect(); - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(buf)); + .collect() + }); + // With one luma transform unit the whole `side`x`side` map is the + // prediction; with several (`logical_tx != side`) each unit takes its own + // `logical_tx`-sized window of that same map, set fresh inside the TU loop + // below (lane-palette2 r7) -- setting the full-block buffer here would be + // taken by the FIRST unit and indexed at the wrong stride. + if let Some(buf) = &palette_y_buf + && logical_tx == side + { + PALETTE_PRED.with(|c| *c.borrow_mut() = Some(buf.clone())); } // A UV-palette block's own chroma prediction override (lane-palette2 r1), // same [`PALETTE_PRED`] slot as Y above -- set fresh right before each @@ -5198,6 +5241,16 @@ fn decode_block( // the neighbour-magnitude table, not the lone-TU 0 (spec // `get_txb_ctx_general`'s `plane_bsize != tx_size` branch). let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, logical_tx / MI); + if let Some(buf) = &palette_y_buf { + PALETTE_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1)); + let mut window = vec![0u16; logical_tx * logical_tx]; + for row in 0..logical_tx { + let src = (tu_row * logical_tx + row) * side + tu_col * logical_tx; + window[row * logical_tx..][..logical_tx] + .copy_from_slice(&buf[src..][..logical_tx]); + } + PALETTE_PRED.with(|c| *c.borrow_mut() = Some(window)); + } let tu_grid = read_plane( dec, cdfs, diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index 15a4baee..20ccbda8 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -49,6 +49,7 @@ const REFUSALS: &[&str] = &[ "an INTER 32x32 partition type this decoder does not code (value={part32})", "a superblock-level partition type other than NONE or SPLIT (this decoder's intra tile path codes only those two at 64x64)", "a HORZ/VERT intra strip with a split transform (per-unit rect prediction is not ported)", + "a palette block on a HORZ/VERT intra strip below 16x16 (reconstruction not ported)", "a palette block with a real transform on a superblock-level HORZ/VERT strip (corner-cropped luma coefficients not ported for palette)", "a block that actually uses a palette (UV) -- reconstruction is out of scope", "a block that actually uses a palette (Y) -- reconstruction is out of scope", @@ -58,7 +59,6 @@ const REFUSALS: &[&str] = &[ "a frame with no mode-info grid", "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)", "a non-skip rectangular (HORZ/VERT/HORZ_B) strip needs rectangular residual coding", - "a palette block with a split luma transform (round 1)", "a reference frame other than LAST_FRAME (round 2)", "a reference frame selected with no picture at this frame's own ref_frame_idx slot for it", "a reference picture whose height does not match this frame's own true size", diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index a6436544..80da628c 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -2126,7 +2126,7 @@ mod tests { "--row-mt=0", "--sb-size=64", "--enable-palette=1", - "--min-partition-size=64", + "--min-partition-size=16", "--max-partition-size=64", "--obu", "-o", @@ -2242,6 +2242,10 @@ mod tests { return; } let mut matched = 0u32; + // Blocks that reconstructed a palette-Y prediction through a SPLIT + // luma transform (lane-palette2 r7's lifted refusal): the same + // screen-content streams exercise it, so this gate proves both. + let mut split_palette_matched = 0u32; let mut named_refusals = 0u32; let mut attempts = 0u32; let sizes = [(64, 128), (128, 64), (128, 128), (128, 192), (192, 192)]; @@ -2278,7 +2282,7 @@ mod tests { "--row-mt=0", "--sb-size=64", "--enable-palette=1", - "--min-partition-size=64", + "--min-partition-size=16", "--max-partition-size=64", "--obu", "-o", @@ -2304,6 +2308,7 @@ mod tests { ); let stream = out.stdout; let before = crate::decode::rect_screen_content_hits(); + let split_before = crate::decode::palette_split_tx_hits(); let frames = match decode_stream(&stream) { Err(e) => { let msg = e.to_string(); @@ -2353,9 +2358,17 @@ mod tests { ); } matched += 1; + if crate::decode::palette_split_tx_hits() > split_before { + split_palette_matched += 1; + } } } } + assert!( + split_palette_matched > 0, + "{NAME}: no pixel-exact attempt ever reconstructed a palette block with a \ + split luma transform -- the lifted refusal is ungated" + ); assert!( matched > 0, "{NAME}: no attempt out of {attempts} ever landed a matching rect \ @@ -2363,9 +2376,12 @@ mod tests { rect_screen_content_hits never proven pixel-exact" ); eprintln!( - "{NAME}: {matched}/{attempts} attempts matched pixel-exact, {named_refusals} \ - named refusals, rect_screen_content_hits={}", - crate::decode::rect_screen_content_hits() + "{NAME}: {matched}/{attempts} attempts matched pixel-exact \ + ({split_palette_matched} of them through a split-transform palette block), \ + {named_refusals} named refusals, rect_screen_content_hits={}, \ + palette_split_tx_hits={}", + crate::decode::rect_screen_content_hits(), + crate::decode::palette_split_tx_hits() ); }