forge
Explore
Log in

Don't you dare go Hollow

Palette under a split luma transform, plus two palette desync fixes

Browse files at this commit

4fbf70151d10a507fce619058e3aea312c93dabaAhmet Kaan GümüşParent: 7ed7ffc
  • feat
    • palette under a split luma transform, plus two palette desync fixes
Show full message
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.rsmodified+71 -18
@@ -581,9 +581,11 @@ pub(crate) fn rect_screen_content_hits() -> usize {
581581 RECT_SCREEN_CONTENT_HITS.with(|c| c.get())
582582 }
583583
584-// lane-palette2 r1: how many palette-Y blocks decoded with a split luma
585-// transform (`tx_select && logical_tx != side`) -- proves the per-TU index-map
586-// slicing actually ran, not just that the whole-block path stayed exact.
584+// lane-palette2 r1: how many palette-Y luma transform UNITS decoded under a
585+// split luma transform (`logical_tx != side`) -- proves the per-TU index-map
586+// windowing actually ran, not just that the whole-block path stayed exact.
587+// Counts units, not blocks (a 64x64 palette block split to 16x16 TUs adds 16);
588+// first actually incremented in r7, when the refusal it guarded was lifted.
587589 thread_local! {
588590 static PALETTE_SPLIT_TX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
589591 }
@@ -2003,12 +2005,22 @@ impl Neighbours {
20032005 /// `av1_get_palette_mode_ctx` (pred_common.h:197): count of the above/left
20042006 /// neighbours that are themselves a palette-Y block, plus
20052007 /// `av1_get_palette_cache`'s own merged, ascending, deduplicated colour
2006- /// cache (pred_common.c:73) -- the above neighbour is excluded at a
2007- /// superblock's own top row (`r % 4 == 0` in [`SUB`]-grid units, 4 cells
2008- /// per 64px SB, `MIN_SB_SIZE_LOG2`), the left neighbour never is.
2008+ /// cache (pred_common.c:73).
2009+ ///
2010+ /// The SB-top-row exclusion of the above neighbour (`row % 64` in
2011+ /// pred_common.c:76, `r % 4 == 0` here in [`SUB`]-grid units) belongs to
2012+ /// the CACHE ALONE -- `av1_get_palette_mode_ctx` reads plain
2013+ /// `xd->above_mbmi` with no such guard (lane-palette2 r7). Applying it to
2014+ /// the ctx too made every superblock in the frame's second SB row read
2015+ /// `palette_y_mode` off CDF row 0 while libaom used row 1, so a real
2016+ /// palette block decoded as "no palette" and the tile desynced from
2017+ /// there (class [[cdf-row-held-constant]]).
20092018 fn palette_ctx_and_cache(&self, at: (usize, usize)) -> (usize, Vec<u16>) {
20102019 let (r, c) = at;
20112020 let above_ok = r % 4 != 0;
2021+ // `xd->above_mbmi` exists for every row but the tile's own first.
2022+ let above_is_palette =
2023+ r * (SUB / MI) > self.tile_row0_mi && self.above_palette_size[c] > 0;
20122024 let (above_n, above_colors) = if above_ok && self.above_palette_size[c] > 0 {
20132025 (self.above_palette_size[c], self.above_palette_colors[c])
20142026 } else {
@@ -2019,7 +2031,7 @@ impl Neighbours {
20192031 } else {
20202032 (0, [0u16; 8])
20212033 };
2022- let ctx = usize::from(above_n > 0) + usize::from(left_n > 0);
2034+ let ctx = usize::from(above_is_palette) + usize::from(left_n > 0);
20232035 let mut cache = Vec::with_capacity(16);
20242036 let push_dedup = |cache: &mut Vec<u16>, v: u16| {
20252037 if cache.last() != Some(&v) {
@@ -3141,8 +3153,11 @@ fn read_intra_mode_rect(
31413153 filter_intra_size_class_rect(bw, bh)
31423154 );
31433155 }
3156+ // As the square path: `av1_filter_intra_allowed` excludes a palette-Y
3157+ // block (reconintra.h:77), so no `use_filter_intra` symbol exists there.
31443158 if mode == DC_PRED
31453159 && enable_filter_intra
3160+ && palette_y_pending.is_none()
31463161 && let Some(class) = filter_intra_size_class_rect(bw, bh)
31473162 {
31483163 let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0;
@@ -3659,7 +3674,16 @@ fn decode_leaf_rect(
36593674 if smooth_neighbor {
36603675 SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1));
36613676 }
3662- let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra) =
3677+ // The palette-Y mode CDF row is picked by the neighbours' palette use
3678+ // (`palette_ctx_and_cache`), so this sub-16x16 leaf must pass its own real
3679+ // ctx/cache -- reading `palette_y_mode` off row 0 when a neighbour did use
3680+ // a palette narrows the wrong interval and desyncs the tile even when the
3681+ // decoded bit is the same. The cell is the 16px-SUB cell containing the
3682+ // leaf (the palette neighbour arrays have no finer granularity).
3683+ let pal_at = (leaf_mi.0 / (SUB / MI), leaf_mi.1 / (SUB / MI));
3684+ let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache(pal_at);
3685+ let palette_uv_cache = neighbours.palette_uv_cache(pal_at);
3686+ let (skip, mode, angle_delta_y, uv_mode, angle_delta_uv, alpha, filter_intra, palette_y, palette_uv) =
36633687 read_intra_mode_rect(
36643688 dec,
36653689 cdfs,
@@ -3671,9 +3695,19 @@ fn decode_leaf_rect(
36713695 enable_filter_intra,
36723696 neighbours.skip_txfm_ctx(leaf_mi.0, leaf_mi.1),
36733697 allow_screen_content_tools,
3698+ Some((palette_ctx, &palette_cache)),
3699+ &palette_uv_cache,
36743700 leaf_mi.0,
36753701 leaf_mi.1,
36763702 )?;
3703+ if palette_y.is_some() || palette_uv.is_some() {
3704+ // Symbols (mode/size/colours/index map) are all read above, so the
3705+ // tile stays in sync; only the *pixel* reconstruction of a palette on
3706+ // a below-16x16 strip is unported and ungated (lane-palette2 r7).
3707+ return Err(unsupported(
3708+ "a palette block on a HORZ/VERT intra strip below 16x16 (reconstruction not ported)",
3709+ ));
3710+ }
36773711 if filter_intra.is_some() {
36783712 return Err(unsupported(
36793713 "filter intra on a HORZ/VERT strip (this decoder predicts square-only)",
@@ -4381,8 +4415,14 @@ fn read_intra_mode(
43814415 // matched bit-exact, then `eob_pt` read 5 instead of 6, because this
43824416 // symbol's bits were never consumed).
43834417 let mut filter_intra = None;
4418+ // `av1_filter_intra_allowed` (reconintra.h:77) also requires
4419+ // `palette_mode_info.palette_size[0] == 0`: a palette-Y block never gets a
4420+ // `use_filter_intra` symbol, so reading one here consumed a bit the
4421+ // encoder never wrote and desynced the tile from a palette block at
4422+ // <=32x32 onward (lane-palette2 r7, class [[symbol-consumption-gap]]).
43844423 if mode == DC_PRED
43854424 && enable_filter_intra
4425+ && palette_y_pending.is_none()
43864426 && let Some(class) = filter_intra_size_class(side)
43874427 {
43884428 let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0;
@@ -5016,18 +5056,21 @@ fn decode_block(
50165056 // would need the map sliced per-TU -- untested against a real
50175057 // `--enable-palette=1` stream this round, so refuse by name rather than
50185058 // guess at the slicing (charter's own named-acceptable fallback).
5019- if let Some(ref py) = palette_y {
5020- if tx_select && logical_tx != side {
5021- return Err(unsupported(
5022- "a palette block with a split luma transform (round 1)",
5023- ));
5024- }
5025- let buf: Vec<u16> = py
5026- .map
5059+ let palette_y_buf: Option<Vec<u16>> = palette_y.as_ref().map(|py| {
5060+ py.map
50275061 .iter()
50285062 .map(|&idx| py.colors[idx as usize])
5029- .collect();
5030- PALETTE_PRED.with(|c| *c.borrow_mut() = Some(buf));
5063+ .collect()
5064+ });
5065+ // With one luma transform unit the whole `side`x`side` map is the
5066+ // prediction; with several (`logical_tx != side`) each unit takes its own
5067+ // `logical_tx`-sized window of that same map, set fresh inside the TU loop
5068+ // below (lane-palette2 r7) -- setting the full-block buffer here would be
5069+ // taken by the FIRST unit and indexed at the wrong stride.
5070+ if let Some(buf) = &palette_y_buf
5071+ && logical_tx == side
5072+ {
5073+ PALETTE_PRED.with(|c| *c.borrow_mut() = Some(buf.clone()));
50315074 }
50325075 // A UV-palette block's own chroma prediction override (lane-palette2 r1),
50335076 // same [`PALETTE_PRED`] slot as Y above -- set fresh right before each
@@ -5198,6 +5241,16 @@ fn decode_block(
51985241 // the neighbour-magnitude table, not the lone-TU 0 (spec
51995242 // `get_txb_ctx_general`'s `plane_bsize != tx_size` branch).
52005243 let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, logical_tx / MI);
5244+ if let Some(buf) = &palette_y_buf {
5245+ PALETTE_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1));
5246+ let mut window = vec![0u16; logical_tx * logical_tx];
5247+ for row in 0..logical_tx {
5248+ let src = (tu_row * logical_tx + row) * side + tu_col * logical_tx;
5249+ window[row * logical_tx..][..logical_tx]
5250+ .copy_from_slice(&buf[src..][..logical_tx]);
5251+ }
5252+ PALETTE_PRED.with(|c| *c.borrow_mut() = Some(window));
5253+ }
52015254 let tu_grid = read_plane(
52025255 dec,
52035256 cdfs,
crates/ec-av1/src/refusal_inventory.rsmodified+1 -1
@@ -49,6 +49,7 @@ const REFUSALS: &[&str] = &[
4949 "an INTER 32x32 partition type this decoder does not code (value={part32})",
5050 "a superblock-level partition type other than NONE or SPLIT (this decoder's intra tile path codes only those two at 64x64)",
5151 "a HORZ/VERT intra strip with a split transform (per-unit rect prediction is not ported)",
52+ "a palette block on a HORZ/VERT intra strip below 16x16 (reconstruction not ported)",
5253 "a palette block with a real transform on a superblock-level HORZ/VERT strip (corner-cropped luma coefficients not ported for palette)",
5354 "a block that actually uses a palette (UV) -- reconstruction is out of scope",
5455 "a block that actually uses a palette (Y) -- reconstruction is out of scope",
@@ -58,7 +59,6 @@ const REFUSALS: &[&str] = &[
5859 "a frame with no mode-info grid",
5960 "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)",
6061 "a non-skip rectangular (HORZ/VERT/HORZ_B) strip needs rectangular residual coding",
61- "a palette block with a split luma transform (round 1)",
6262 "a reference frame other than LAST_FRAME (round 2)",
6363 "a reference frame selected with no picture at this frame's own ref_frame_idx slot for it",
6464 "a reference picture whose height does not match this frame's own true size",
crates/ec-av1/src/stream.rsmodified+21 -5
@@ -2126,7 +2126,7 @@ mod tests {
21262126 "--row-mt=0",
21272127 "--sb-size=64",
21282128 "--enable-palette=1",
2129- "--min-partition-size=64",
2129+ "--min-partition-size=16",
21302130 "--max-partition-size=64",
21312131 "--obu",
21322132 "-o",
@@ -2242,6 +2242,10 @@ mod tests {
22422242 return;
22432243 }
22442244 let mut matched = 0u32;
2245+ // Blocks that reconstructed a palette-Y prediction through a SPLIT
2246+ // luma transform (lane-palette2 r7's lifted refusal): the same
2247+ // screen-content streams exercise it, so this gate proves both.
2248+ let mut split_palette_matched = 0u32;
22452249 let mut named_refusals = 0u32;
22462250 let mut attempts = 0u32;
22472251 let sizes = [(64, 128), (128, 64), (128, 128), (128, 192), (192, 192)];
@@ -2278,7 +2282,7 @@ mod tests {
22782282 "--row-mt=0",
22792283 "--sb-size=64",
22802284 "--enable-palette=1",
2281- "--min-partition-size=64",
2285+ "--min-partition-size=16",
22822286 "--max-partition-size=64",
22832287 "--obu",
22842288 "-o",
@@ -2304,6 +2308,7 @@ mod tests {
23042308 );
23052309 let stream = out.stdout;
23062310 let before = crate::decode::rect_screen_content_hits();
2311+ let split_before = crate::decode::palette_split_tx_hits();
23072312 let frames = match decode_stream(&stream) {
23082313 Err(e) => {
23092314 let msg = e.to_string();
@@ -2353,9 +2358,17 @@ mod tests {
23532358 );
23542359 }
23552360 matched += 1;
2361+ if crate::decode::palette_split_tx_hits() > split_before {
2362+ split_palette_matched += 1;
2363+ }
23562364 }
23572365 }
23582366 }
2367+ assert!(
2368+ split_palette_matched > 0,
2369+ "{NAME}: no pixel-exact attempt ever reconstructed a palette block with a \
2370+ split luma transform -- the lifted refusal is ungated"
2371+ );
23592372 assert!(
23602373 matched > 0,
23612374 "{NAME}: no attempt out of {attempts} ever landed a matching rect \
@@ -2363,9 +2376,12 @@ mod tests {
23632376 rect_screen_content_hits never proven pixel-exact"
23642377 );
23652378 eprintln!(
2366- "{NAME}: {matched}/{attempts} attempts matched pixel-exact, {named_refusals} \
2367- named refusals, rect_screen_content_hits={}",
2368- crate::decode::rect_screen_content_hits()
2379+ "{NAME}: {matched}/{attempts} attempts matched pixel-exact \
2380+ ({split_palette_matched} of them through a split-transform palette block), \
2381+ {named_refusals} named refusals, rect_screen_content_hits={}, \
2382+ palette_split_tx_hits={}",
2383+ crate::decode::rect_screen_content_hits(),
2384+ crate::decode::palette_split_tx_hits()
23692385 );
23702386 }
23712387