Palette under a split luma transform, plus two palette desync fixes
Browse files at this commit4fbf70151d10a507fce619058e3aea312c93dabaParent: 7ed7ffc- feat
- palette under a split luma transform, plus two palette desync fixes
3 changed files +93 -24
- crates/ec-av1/src/decode.rs +71 -18
- crates/ec-av1/src/refusal_inventory.rs +1 -1
- crates/ec-av1/src/stream.rs +21 -5
crates/ec-av1/src/decode.rsmodified+71 -18
| @@ -581,9 +581,11 @@ pub(crate) fn rect_screen_content_hits() -> usize { | |||
| 581 | RECT_SCREEN_CONTENT_HITS.with(|c| c.get()) | 581 | RECT_SCREEN_CONTENT_HITS.with(|c| c.get()) |
| 582 | } | 582 | } |
| 583 | 583 | ||
| 584 | -// lane-palette2 r1: how many palette-Y blocks decoded with a split luma | 584 | +// lane-palette2 r1: how many palette-Y luma transform UNITS decoded under a |
| 585 | -// transform (`tx_select && logical_tx != side`) -- proves the per-TU index-map | 585 | +// split luma transform (`logical_tx != side`) -- proves the per-TU index-map |
| 586 | -// slicing actually ran, not just that the whole-block path stayed exact. | 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. | ||
| 587 | thread_local! { | 589 | thread_local! { |
| 588 | static PALETTE_SPLIT_TX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; | 590 | static PALETTE_SPLIT_TX_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; |
| 589 | } | 591 | } |
| @@ -2003,12 +2005,22 @@ impl Neighbours { | |||
| 2003 | /// `av1_get_palette_mode_ctx` (pred_common.h:197): count of the above/left | 2005 | /// `av1_get_palette_mode_ctx` (pred_common.h:197): count of the above/left |
| 2004 | /// neighbours that are themselves a palette-Y block, plus | 2006 | /// neighbours that are themselves a palette-Y block, plus |
| 2005 | /// `av1_get_palette_cache`'s own merged, ascending, deduplicated colour | 2007 | /// `av1_get_palette_cache`'s own merged, ascending, deduplicated colour |
| 2006 | - /// cache (pred_common.c:73) -- the above neighbour is excluded at a | 2008 | + /// cache (pred_common.c:73). |
| 2007 | - /// superblock's own top row (`r % 4 == 0` in [`SUB`]-grid units, 4 cells | 2009 | + /// |
| 2008 | - /// per 64px SB, `MIN_SB_SIZE_LOG2`), the left neighbour never is. | 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]]). | ||
| 2009 | fn palette_ctx_and_cache(&self, at: (usize, usize)) -> (usize, Vec<u16>) { | 2018 | fn palette_ctx_and_cache(&self, at: (usize, usize)) -> (usize, Vec<u16>) { |
| 2010 | let (r, c) = at; | 2019 | let (r, c) = at; |
| 2011 | let above_ok = r % 4 != 0; | 2020 | 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; | ||
| 2012 | let (above_n, above_colors) = if above_ok && self.above_palette_size[c] > 0 { | 2024 | let (above_n, above_colors) = if above_ok && self.above_palette_size[c] > 0 { |
| 2013 | (self.above_palette_size[c], self.above_palette_colors[c]) | 2025 | (self.above_palette_size[c], self.above_palette_colors[c]) |
| 2014 | } else { | 2026 | } else { |
| @@ -2019,7 +2031,7 @@ impl Neighbours { | |||
| 2019 | } else { | 2031 | } else { |
| 2020 | (0, [0u16; 8]) | 2032 | (0, [0u16; 8]) |
| 2021 | }; | 2033 | }; |
| 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); |
| 2023 | let mut cache = Vec::with_capacity(16); | 2035 | let mut cache = Vec::with_capacity(16); |
| 2024 | let push_dedup = |cache: &mut Vec<u16>, v: u16| { | 2036 | let push_dedup = |cache: &mut Vec<u16>, v: u16| { |
| 2025 | if cache.last() != Some(&v) { | 2037 | if cache.last() != Some(&v) { |
| @@ -3141,8 +3153,11 @@ fn read_intra_mode_rect( | |||
| 3141 | filter_intra_size_class_rect(bw, bh) | 3153 | filter_intra_size_class_rect(bw, bh) |
| 3142 | ); | 3154 | ); |
| 3143 | } | 3155 | } |
| 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. | ||
| 3144 | if mode == DC_PRED | 3158 | if mode == DC_PRED |
| 3145 | && enable_filter_intra | 3159 | && enable_filter_intra |
| 3160 | + && palette_y_pending.is_none() | ||
| 3146 | && let Some(class) = filter_intra_size_class_rect(bw, bh) | 3161 | && let Some(class) = filter_intra_size_class_rect(bw, bh) |
| 3147 | { | 3162 | { |
| 3148 | let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0; | 3163 | let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0; |
| @@ -3659,7 +3674,16 @@ fn decode_leaf_rect( | |||
| 3659 | if smooth_neighbor { | 3674 | if smooth_neighbor { |
| 3660 | SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); | 3675 | SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); |
| 3661 | } | 3676 | } |
| 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) = | ||
| 3663 | read_intra_mode_rect( | 3687 | read_intra_mode_rect( |
| 3664 | dec, | 3688 | dec, |
| 3665 | cdfs, | 3689 | cdfs, |
| @@ -3671,9 +3695,19 @@ fn decode_leaf_rect( | |||
| 3671 | enable_filter_intra, | 3695 | enable_filter_intra, |
| 3672 | neighbours.skip_txfm_ctx(leaf_mi.0, leaf_mi.1), | 3696 | neighbours.skip_txfm_ctx(leaf_mi.0, leaf_mi.1), |
| 3673 | allow_screen_content_tools, | 3697 | allow_screen_content_tools, |
| 3698 | + Some((palette_ctx, &palette_cache)), | ||
| 3699 | + &palette_uv_cache, | ||
| 3674 | leaf_mi.0, | 3700 | leaf_mi.0, |
| 3675 | leaf_mi.1, | 3701 | leaf_mi.1, |
| 3676 | )?; | 3702 | )?; |
| 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 | + } | ||
| 3677 | if filter_intra.is_some() { | 3711 | if filter_intra.is_some() { |
| 3678 | return Err(unsupported( | 3712 | return Err(unsupported( |
| 3679 | "filter intra on a HORZ/VERT strip (this decoder predicts square-only)", | 3713 | "filter intra on a HORZ/VERT strip (this decoder predicts square-only)", |
| @@ -4381,8 +4415,14 @@ fn read_intra_mode( | |||
| 4381 | // matched bit-exact, then `eob_pt` read 5 instead of 6, because this | 4415 | // matched bit-exact, then `eob_pt` read 5 instead of 6, because this |
| 4382 | // symbol's bits were never consumed). | 4416 | // symbol's bits were never consumed). |
| 4383 | let mut filter_intra = None; | 4417 | 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]]). | ||
| 4384 | if mode == DC_PRED | 4423 | if mode == DC_PRED |
| 4385 | && enable_filter_intra | 4424 | && enable_filter_intra |
| 4425 | + && palette_y_pending.is_none() | ||
| 4386 | && let Some(class) = filter_intra_size_class(side) | 4426 | && let Some(class) = filter_intra_size_class(side) |
| 4387 | { | 4427 | { |
| 4388 | let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0; | 4428 | let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[class]) != 0; |
| @@ -5016,18 +5056,21 @@ fn decode_block( | |||
| 5016 | // would need the map sliced per-TU -- untested against a real | 5056 | // would need the map sliced per-TU -- untested against a real |
| 5017 | // `--enable-palette=1` stream this round, so refuse by name rather than | 5057 | // `--enable-palette=1` stream this round, so refuse by name rather than |
| 5018 | // guess at the slicing (charter's own named-acceptable fallback). | 5058 | // guess at the slicing (charter's own named-acceptable fallback). |
| 5019 | - if let Some(ref py) = palette_y { | 5059 | + let palette_y_buf: Option<Vec<u16>> = palette_y.as_ref().map(|py| { |
| 5020 | - if tx_select && logical_tx != side { | 5060 | + py.map |
| 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 | ||
| 5027 | .iter() | 5061 | .iter() |
| 5028 | .map(|&idx| py.colors[idx as usize]) | 5062 | .map(|&idx| py.colors[idx as usize]) |
| 5029 | - .collect(); | 5063 | + .collect() |
| 5030 | - PALETTE_PRED.with(|c| *c.borrow_mut() = Some(buf)); | 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())); | ||
| 5031 | } | 5074 | } |
| 5032 | // A UV-palette block's own chroma prediction override (lane-palette2 r1), | 5075 | // A UV-palette block's own chroma prediction override (lane-palette2 r1), |
| 5033 | // same [`PALETTE_PRED`] slot as Y above -- set fresh right before each | 5076 | // same [`PALETTE_PRED`] slot as Y above -- set fresh right before each |
| @@ -5198,6 +5241,16 @@ fn decode_block( | |||
| 5198 | // the neighbour-magnitude table, not the lone-TU 0 (spec | 5241 | // the neighbour-magnitude table, not the lone-TU 0 (spec |
| 5199 | // `get_txb_ctx_general`'s `plane_bsize != tx_size` branch). | 5242 | // `get_txb_ctx_general`'s `plane_bsize != tx_size` branch). |
| 5200 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, logical_tx / MI); | 5243 | 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 | + } | ||
| 5201 | let tu_grid = read_plane( | 5254 | let tu_grid = read_plane( |
| 5202 | dec, | 5255 | dec, |
| 5203 | cdfs, | 5256 | cdfs, |
crates/ec-av1/src/refusal_inventory.rsmodified+1 -1
| @@ -49,6 +49,7 @@ const REFUSALS: &[&str] = &[ | |||
| 49 | "an INTER 32x32 partition type this decoder does not code (value={part32})", | 49 | "an INTER 32x32 partition type this decoder does not code (value={part32})", |
| 50 | "a superblock-level partition type other than NONE or SPLIT (this decoder's intra tile path codes only those two at 64x64)", | 50 | "a superblock-level partition type other than NONE or SPLIT (this decoder's intra tile path codes only those two at 64x64)", |
| 51 | "a HORZ/VERT intra strip with a split transform (per-unit rect prediction is not ported)", | 51 | "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)", | ||
| 52 | "a palette block with a real transform on a superblock-level HORZ/VERT strip (corner-cropped luma coefficients not ported for palette)", | 53 | "a palette block with a real transform on a superblock-level HORZ/VERT strip (corner-cropped luma coefficients not ported for palette)", |
| 53 | "a block that actually uses a palette (UV) -- reconstruction is out of scope", | 54 | "a block that actually uses a palette (UV) -- reconstruction is out of scope", |
| 54 | "a block that actually uses a palette (Y) -- reconstruction is out of scope", | 55 | "a block that actually uses a palette (Y) -- reconstruction is out of scope", |
| @@ -58,7 +59,6 @@ const REFUSALS: &[&str] = &[ | |||
| 58 | "a frame with no mode-info grid", | 59 | "a frame with no mode-info grid", |
| 59 | "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)", | 60 | "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)", |
| 60 | "a non-skip rectangular (HORZ/VERT/HORZ_B) strip needs rectangular residual coding", | 61 | "a non-skip rectangular (HORZ/VERT/HORZ_B) strip needs rectangular residual coding", |
| 61 | - "a palette block with a split luma transform (round 1)", | ||
| 62 | "a reference frame other than LAST_FRAME (round 2)", | 62 | "a reference frame other than LAST_FRAME (round 2)", |
| 63 | "a reference frame selected with no picture at this frame's own ref_frame_idx slot for it", | 63 | "a reference frame selected with no picture at this frame's own ref_frame_idx slot for it", |
| 64 | "a reference picture whose height does not match this frame's own true size", | 64 | "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 { | |||
| 2126 | "--row-mt=0", | 2126 | "--row-mt=0", |
| 2127 | "--sb-size=64", | 2127 | "--sb-size=64", |
| 2128 | "--enable-palette=1", | 2128 | "--enable-palette=1", |
| 2129 | - "--min-partition-size=64", | 2129 | + "--min-partition-size=16", |
| 2130 | "--max-partition-size=64", | 2130 | "--max-partition-size=64", |
| 2131 | "--obu", | 2131 | "--obu", |
| 2132 | "-o", | 2132 | "-o", |
| @@ -2242,6 +2242,10 @@ mod tests { | |||
| 2242 | return; | 2242 | return; |
| 2243 | } | 2243 | } |
| 2244 | let mut matched = 0u32; | 2244 | 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; | ||
| 2245 | let mut named_refusals = 0u32; | 2249 | let mut named_refusals = 0u32; |
| 2246 | let mut attempts = 0u32; | 2250 | let mut attempts = 0u32; |
| 2247 | let sizes = [(64, 128), (128, 64), (128, 128), (128, 192), (192, 192)]; | 2251 | let sizes = [(64, 128), (128, 64), (128, 128), (128, 192), (192, 192)]; |
| @@ -2278,7 +2282,7 @@ mod tests { | |||
| 2278 | "--row-mt=0", | 2282 | "--row-mt=0", |
| 2279 | "--sb-size=64", | 2283 | "--sb-size=64", |
| 2280 | "--enable-palette=1", | 2284 | "--enable-palette=1", |
| 2281 | - "--min-partition-size=64", | 2285 | + "--min-partition-size=16", |
| 2282 | "--max-partition-size=64", | 2286 | "--max-partition-size=64", |
| 2283 | "--obu", | 2287 | "--obu", |
| 2284 | "-o", | 2288 | "-o", |
| @@ -2304,6 +2308,7 @@ mod tests { | |||
| 2304 | ); | 2308 | ); |
| 2305 | let stream = out.stdout; | 2309 | let stream = out.stdout; |
| 2306 | let before = crate::decode::rect_screen_content_hits(); | 2310 | let before = crate::decode::rect_screen_content_hits(); |
| 2311 | + let split_before = crate::decode::palette_split_tx_hits(); | ||
| 2307 | let frames = match decode_stream(&stream) { | 2312 | let frames = match decode_stream(&stream) { |
| 2308 | Err(e) => { | 2313 | Err(e) => { |
| 2309 | let msg = e.to_string(); | 2314 | let msg = e.to_string(); |
| @@ -2353,9 +2358,17 @@ mod tests { | |||
| 2353 | ); | 2358 | ); |
| 2354 | } | 2359 | } |
| 2355 | matched += 1; | 2360 | matched += 1; |
| 2361 | + if crate::decode::palette_split_tx_hits() > split_before { | ||
| 2362 | + split_palette_matched += 1; | ||
| 2363 | + } | ||
| 2356 | } | 2364 | } |
| 2357 | } | 2365 | } |
| 2358 | } | 2366 | } |
| 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 | + ); | ||
| 2359 | assert!( | 2372 | assert!( |
| 2360 | matched > 0, | 2373 | matched > 0, |
| 2361 | "{NAME}: no attempt out of {attempts} ever landed a matching rect \ | 2374 | "{NAME}: no attempt out of {attempts} ever landed a matching rect \ |
| @@ -2363,9 +2376,12 @@ mod tests { | |||
| 2363 | rect_screen_content_hits never proven pixel-exact" | 2376 | rect_screen_content_hits never proven pixel-exact" |
| 2364 | ); | 2377 | ); |
| 2365 | eprintln!( | 2378 | eprintln!( |
| 2366 | - "{NAME}: {matched}/{attempts} attempts matched pixel-exact, {named_refusals} \ | 2379 | + "{NAME}: {matched}/{attempts} attempts matched pixel-exact \ |
| 2367 | - named refusals, rect_screen_content_hits={}", | 2380 | + ({split_palette_matched} of them through a split-transform palette block), \ |
| 2368 | - crate::decode::rect_screen_content_hits() | 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() | ||
| 2369 | ); | 2385 | ); |
| 2370 | } | 2386 | } |
| 2371 | 2387 | ||