The palette colour-index map is read over the block's on-screen part only
Browse files at this commit2461f8080e272b18509aa243e875deba71690b32Parent: bab01c8- fix
- the palette colour-index map is read over the block's on-screen part only
1 changed file +114 -20
- crates/ec-av1/src/decode.rs +114 -20
crates/ec-av1/src/decode.rsmodified+114 -20
| @@ -2149,6 +2149,24 @@ pub fn reset_skip_split_tx_override_hits() { | |||
| 2149 | SKIP_SPLIT_TX_OVERRIDE_HITS.with(|c| c.set(0)); | 2149 | SKIP_SPLIT_TX_OVERRIDE_HITS.with(|c| c.set(0)); |
| 2150 | } | 2150 | } |
| 2151 | 2151 | ||
| 2152 | +// lane-svtinter: palette colour-index maps whose block is cut by the frame's | ||
| 2153 | +// bottom/right mi edge, so spec 5.11.50's `onscreenWidth`/`onscreenHeight` | ||
| 2154 | +// really are smaller than the block and FEWER symbols are read than the | ||
| 2155 | +// block has cells (class map-read-past-frame-edge). | ||
| 2156 | +thread_local! { | ||
| 2157 | + static PALETTE_ONSCREEN_CUT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; | ||
| 2158 | +} | ||
| 2159 | + | ||
| 2160 | +/// Current value of [`PALETTE_ONSCREEN_CUT_HITS`] (colour-index maps). | ||
| 2161 | +pub fn palette_onscreen_cut_hits() -> usize { | ||
| 2162 | + PALETTE_ONSCREEN_CUT_HITS.with(|c| c.get()) | ||
| 2163 | +} | ||
| 2164 | + | ||
| 2165 | +/// Zeroes [`PALETTE_ONSCREEN_CUT_HITS`] before a gate's own decode attempt. | ||
| 2166 | +pub fn reset_palette_onscreen_cut_hits() { | ||
| 2167 | + PALETTE_ONSCREEN_CUT_HITS.with(|c| c.set(0)); | ||
| 2168 | +} | ||
| 2169 | + | ||
| 2152 | /// Zeroes [`SKIP_SPLIT_TX_HITS`] before a gate's own decode attempt. | 2170 | /// Zeroes [`SKIP_SPLIT_TX_HITS`] before a gate's own decode attempt. |
| 2153 | pub fn reset_skip_split_tx_hits() { | 2171 | pub fn reset_skip_split_tx_hits() { |
| 2154 | SKIP_SPLIT_TX_HITS.with(|c| c.set(0)); | 2172 | SKIP_SPLIT_TX_HITS.with(|c| c.set(0)); |
| @@ -8803,21 +8821,53 @@ pub(crate) fn palette_color_index_context( | |||
| 8803 | (ctx as usize, color_order) | 8821 | (ctx as usize, color_order) |
| 8804 | } | 8822 | } |
| 8805 | 8823 | ||
| 8824 | +/// spec 5.11.50 `palette_tokens` / `av1_get_block_dimensions` (blockd.h:1512): | ||
| 8825 | +/// the ON-SCREEN part of a block at `(mi_r, mi_c)`, in luma pixels. A block | ||
| 8826 | +/// cut by the frame's bottom or right mi edge codes its colour-index map only | ||
| 8827 | +/// over this part and replicates it outward -- reading the full `bw x bh` grid | ||
| 8828 | +/// consumed symbols libaom never wrote and desynced the rest of the tile | ||
| 8829 | +/// (lane-svtinter; class map-read-past-frame-edge). | ||
| 8830 | +fn palette_onscreen( | ||
| 8831 | + mi_r: usize, | ||
| 8832 | + mi_c: usize, | ||
| 8833 | + bw: usize, | ||
| 8834 | + bh: usize, | ||
| 8835 | + fctx: &crate::decode::FrameCtx, | ||
| 8836 | +) -> (usize, usize) { | ||
| 8837 | + let (mi_rows, mi_cols) = fctx.seg_mi_dims.with(|c| c.get()); | ||
| 8838 | + ( | ||
| 8839 | + bw.min(mi_cols.saturating_sub(mi_c) * MI), | ||
| 8840 | + bh.min(mi_rows.saturating_sub(mi_r) * MI), | ||
| 8841 | + ) | ||
| 8842 | +} | ||
| 8843 | + | ||
| 8844 | +/// [`palette_onscreen`] carried into the chroma plane: `>> subsampling` plus | ||
| 8845 | +/// the SAME sub-8 bump the caller's own `(bw / 2).max(4)` applies to the block | ||
| 8846 | +/// dimensions (`is_chroma_sub8_x/y`, blockd.h:1533) -- both must move together | ||
| 8847 | +/// or a cut 8-wide block reads its chroma map at the wrong width. | ||
| 8848 | +fn palette_onscreen_uv( | ||
| 8849 | + luma: (usize, usize), | ||
| 8850 | + on: (usize, usize), | ||
| 8851 | + chroma: (usize, usize), | ||
| 8852 | +) -> (usize, usize) { | ||
| 8853 | + ( | ||
| 8854 | + (on.0 >> 1) + (chroma.0 - (luma.0 >> 1)), | ||
| 8855 | + (on.1 >> 1) + (chroma.1 - (luma.1 >> 1)), | ||
| 8856 | + ) | ||
| 8857 | +} | ||
| 8858 | + | ||
| 8806 | /// `decode_color_map_tokens` (detokenize.c:25): the palette-Y colour-index | 8859 | /// `decode_color_map_tokens` (detokenize.c:25): the palette-Y colour-index |
| 8807 | -/// map's wavefront decode over a `side x side` grid -- this decoder's | 8860 | +/// map's wavefront decode over a `side x side` grid, read over the on-screen |
| 8808 | -/// palette blocks are always square and never straddle the frame's true | 8861 | +/// `on_side x on_side` corner (see [`palette_onscreen`]). |
| 8809 | -/// (possibly odd) edge without already allocating the full `side*side` | ||
| 8810 | -/// region (every other skip/residual path here does the same), so no | ||
| 8811 | -/// rows/cols-vs-plane_width/height cropping special case is needed, unlike | ||
| 8812 | -/// libaom's own non-block-aligned split. | ||
| 8813 | fn decode_color_index_map( | 8862 | fn decode_color_index_map( |
| 8814 | dec: &mut SymbolDecoder, | 8863 | dec: &mut SymbolDecoder, |
| 8815 | cdfs: &mut Cdfs, | 8864 | cdfs: &mut Cdfs, |
| 8816 | n: usize, | 8865 | n: usize, |
| 8817 | side: usize, | 8866 | side: usize, |
| 8867 | + on: (usize, usize), | ||
| 8818 | uv: bool, | 8868 | uv: bool, |
| 8819 | ) -> Vec<u8> { | 8869 | ) -> Vec<u8> { |
| 8820 | - decode_color_index_map_wh(dec, cdfs, n, side, side, uv) | 8870 | + decode_color_index_map_wh(dec, cdfs, n, side, side, on, uv) |
| 8821 | } | 8871 | } |
| 8822 | 8872 | ||
| 8823 | /// [`decode_color_index_map`], generalised to a true `bw`x`bh` rect strip | 8873 | /// [`decode_color_index_map`], generalised to a true `bw`x`bh` rect strip |
| @@ -8831,9 +8881,15 @@ fn decode_color_index_map_wh( | |||
| 8831 | n: usize, | 8881 | n: usize, |
| 8832 | bw: usize, | 8882 | bw: usize, |
| 8833 | bh: usize, | 8883 | bh: usize, |
| 8884 | + // `(onscreenWidth, onscreenHeight)` in this plane's own units. | ||
| 8885 | + on: (usize, usize), | ||
| 8834 | uv: bool, | 8886 | uv: bool, |
| 8835 | ) -> Vec<u8> { | 8887 | ) -> Vec<u8> { |
| 8836 | let trace = crate::envflags::env_flag!("EC_AV1_TRACE"); | 8888 | let trace = crate::envflags::env_flag!("EC_AV1_TRACE"); |
| 8889 | + let (on_bw, on_bh) = (on.0.clamp(1, bw), on.1.clamp(1, bh)); | ||
| 8890 | + if on_bw < bw || on_bh < bh { | ||
| 8891 | + hit!(PALETTE_ONSCREEN_CUT_HITS); | ||
| 8892 | + } | ||
| 8837 | let mut map = vec![0u8; bw * bh]; | 8893 | let mut map = vec![0u8; bw * bh]; |
| 8838 | if trace { | 8894 | if trace { |
| 8839 | let (rng, _) = dec.debug_state(); | 8895 | let (rng, _) = dec.debug_state(); |
| @@ -8844,9 +8900,9 @@ fn decode_color_index_map_wh( | |||
| 8844 | let (rng, _) = dec.debug_state(); | 8900 | let (rng, _) = dec.debug_state(); |
| 8845 | eprintln!("EC_PAL_VAL row=0 col=0 color_idx={} rng={rng}", map[0]); | 8901 | eprintln!("EC_PAL_VAL row=0 col=0 color_idx={} rng={rng}", map[0]); |
| 8846 | } | 8902 | } |
| 8847 | - for i in 1..(bw + bh - 1) { | 8903 | + for i in 1..(on_bw + on_bh - 1) { |
| 8848 | - let j_hi = i.min(bw - 1); | 8904 | + let j_hi = i.min(on_bw - 1); |
| 8849 | - let j_lo = i.saturating_sub(bh - 1); | 8905 | + let j_lo = i.saturating_sub(on_bh - 1); |
| 8850 | for j in (j_lo..=j_hi).rev() { | 8906 | for j in (j_lo..=j_hi).rev() { |
| 8851 | let row = i - j; | 8907 | let row = i - j; |
| 8852 | let col = j; | 8908 | let col = j; |
| @@ -8872,6 +8928,17 @@ fn decode_color_index_map_wh( | |||
| 8872 | } | 8928 | } |
| 8873 | } | 8929 | } |
| 8874 | } | 8930 | } |
| 8931 | + // `decode_color_map_tokens`'s own two extension loops (detokenize.c:71): | ||
| 8932 | + // the last on-screen column then the last on-screen row are replicated | ||
| 8933 | + // over the off-screen remainder, so the prediction still covers the whole | ||
| 8934 | + // block. | ||
| 8935 | + for row in 0..on_bh { | ||
| 8936 | + let last = map[row * bw + on_bw - 1]; | ||
| 8937 | + map[row * bw + on_bw..row * bw + bw].fill(last); | ||
| 8938 | + } | ||
| 8939 | + for row in on_bh..bh { | ||
| 8940 | + map.copy_within((on_bh - 1) * bw..on_bh * bw, row * bw); | ||
| 8941 | + } | ||
| 8875 | map | 8942 | map |
| 8876 | } | 8943 | } |
| 8877 | 8944 | ||
| @@ -9115,7 +9182,8 @@ fn read_intra_mode_rect( | |||
| 9115 | // `av1_visit_palette`: plane 0 (Y) then plane 1 (chroma, shared U/V map), | 9182 | // `av1_visit_palette`: plane 0 (Y) then plane 1 (chroma, shared U/V map), |
| 9116 | // same order as [`read_intra_mode`]'s own square path. | 9183 | // same order as [`read_intra_mode`]'s own square path. |
| 9117 | let palette_y = palette_y_pending.map(|(n, colors)| { | 9184 | let palette_y = palette_y_pending.map(|(n, colors)| { |
| 9118 | - let map = decode_color_index_map_wh(dec, cdfs, n, bw, bh, false); | 9185 | + let map = |
| 9186 | + decode_color_index_map_wh(dec, cdfs, n, bw, bh, palette_onscreen(mi_r, mi_c, bw, bh, fctx), false); | ||
| 9119 | hit!(PALETTE_HITS); | 9187 | hit!(PALETTE_HITS); |
| 9120 | hit!(PALETTE_RECT_HITS); | 9188 | hit!(PALETTE_RECT_HITS); |
| 9121 | PaletteY { size: n, colors, map } | 9189 | PaletteY { size: n, colors, map } |
| @@ -9127,7 +9195,8 @@ fn read_intra_mode_rect( | |||
| 9127 | // axis to 2 read HALF the colour-index symbols of a 1:4 strip's | 9195 | // axis to 2 read HALF the colour-index symbols of a 1:4 strip's |
| 9128 | // chroma palette and desynced the tile from there. | 9196 | // chroma palette and desynced the tile from there. |
| 9129 | let (cw, ch) = ((bw / 2).max(4), (bh / 2).max(4)); | 9197 | let (cw, ch) = ((bw / 2).max(4), (bh / 2).max(4)); |
| 9130 | - let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, true); | 9198 | + let on = palette_onscreen_uv((bw, bh), palette_onscreen(mi_r, mi_c, bw, bh, fctx), (cw, ch)); |
| 9199 | + let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true); | ||
| 9131 | hit!(PALETTE_UV_HITS); | 9200 | hit!(PALETTE_UV_HITS); |
| 9132 | PaletteUv { size: n, u_colors, v_colors, map } | 9201 | PaletteUv { size: n, u_colors, v_colors, map } |
| 9133 | }); | 9202 | }); |
| @@ -10419,7 +10488,8 @@ fn decode_intra_rect_in_inter( | |||
| 10419 | } | 10488 | } |
| 10420 | // `av1_visit_palette`: Y's colour-index map then the shared chroma one. | 10489 | // `av1_visit_palette`: Y's colour-index map then the shared chroma one. |
| 10421 | let palette_y = palette_y_pending.map(|(n, colors)| { | 10490 | let palette_y = palette_y_pending.map(|(n, colors)| { |
| 10422 | - let map = decode_color_index_map_wh(dec, cdfs, n, bw, bh, false); | 10491 | + let map = |
| 10492 | + decode_color_index_map_wh(dec, cdfs, n, bw, bh, palette_onscreen(mi_r, mi_c, bw, bh, fctx), false); | ||
| 10423 | hit!(PALETTE_HITS); | 10493 | hit!(PALETTE_HITS); |
| 10424 | hit!(PALETTE_RECT_HITS); | 10494 | hit!(PALETTE_RECT_HITS); |
| 10425 | hit!(INTRA_IN_INTER_PALETTE_Y_HITS); | 10495 | hit!(INTRA_IN_INTER_PALETTE_Y_HITS); |
| @@ -10427,7 +10497,8 @@ fn decode_intra_rect_in_inter( | |||
| 10427 | }); | 10497 | }); |
| 10428 | let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { | 10498 | let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { |
| 10429 | let (cw, ch) = ((bw / 2).max(4), (bh / 2).max(4)); | 10499 | let (cw, ch) = ((bw / 2).max(4), (bh / 2).max(4)); |
| 10430 | - let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, true); | 10500 | + let on = palette_onscreen_uv((bw, bh), palette_onscreen(mi_r, mi_c, bw, bh, fctx), (cw, ch)); |
| 10501 | + let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true); | ||
| 10431 | hit!(PALETTE_UV_HITS); | 10502 | hit!(PALETTE_UV_HITS); |
| 10432 | hit!(INTRA_IN_INTER_PALETTE_UV_HITS); | 10503 | hit!(INTRA_IN_INTER_PALETTE_UV_HITS); |
| 10433 | PaletteUv { size: n, u_colors, v_colors, map } | 10504 | PaletteUv { size: n, u_colors, v_colors, map } |
| @@ -13290,7 +13361,8 @@ fn read_intra_mode( | |||
| 13290 | // `av1_visit_palette` (decoder.c:234): plane 0 (Y) then plane 1 (chroma, | 13361 | // `av1_visit_palette` (decoder.c:234): plane 0 (Y) then plane 1 (chroma, |
| 13291 | // shared U/V map) -- the same order libaom's own loop visits them in. | 13362 | // shared U/V map) -- the same order libaom's own loop visits them in. |
| 13292 | let palette_y = palette_y_pending.map(|(n, colors)| { | 13363 | let palette_y = palette_y_pending.map(|(n, colors)| { |
| 13293 | - let map = decode_color_index_map(dec, cdfs, n, side, false); | 13364 | + let map = |
| 13365 | + decode_color_index_map(dec, cdfs, n, side, palette_onscreen(mi_r, mi_c, side, side, fctx), false); | ||
| 13294 | hit!(PALETTE_HITS); | 13366 | hit!(PALETTE_HITS); |
| 13295 | if trace { | 13367 | if trace { |
| 13296 | eprintln!("TRACE palette_y size={n} colors={:?}", &colors[..n]); | 13368 | eprintln!("TRACE palette_y size={n} colors={:?}", &colors[..n]); |
| @@ -13298,7 +13370,12 @@ fn read_intra_mode( | |||
| 13298 | PaletteY { size: n, colors, map } | 13370 | PaletteY { size: n, colors, map } |
| 13299 | }); | 13371 | }); |
| 13300 | let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { | 13372 | let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { |
| 13301 | - let map = decode_color_index_map(dec, cdfs, n, side / 2, true); | 13373 | + let on = palette_onscreen_uv( |
| 13374 | + (side, side), | ||
| 13375 | + palette_onscreen(mi_r, mi_c, side, side, fctx), | ||
| 13376 | + (side / 2, side / 2), | ||
| 13377 | + ); | ||
| 13378 | + let map = decode_color_index_map(dec, cdfs, n, side / 2, on, true); | ||
| 13302 | hit!(PALETTE_UV_HITS); | 13379 | hit!(PALETTE_UV_HITS); |
| 13303 | if trace { | 13380 | if trace { |
| 13304 | eprintln!( | 13381 | eprintln!( |
| @@ -29712,7 +29789,10 @@ fn decode_inter_block( | |||
| 29712 | // `av1_visit_palette`: the two colour-index maps, Y then the shared | 29789 | // `av1_visit_palette`: the two colour-index maps, Y then the shared |
| 29713 | // chroma one, after the WHOLE mode-info read. | 29790 | // chroma one, after the WHOLE mode-info read. |
| 29714 | let palette_y = palette_y_pending.map(|(n, colors)| { | 29791 | let palette_y = palette_y_pending.map(|(n, colors)| { |
| 29715 | - let map = decode_color_index_map_wh(dec, cdfs, n, write_w, write_h, false); | 29792 | + let map = decode_color_index_map_wh( |
| 29793 | + dec, cdfs, n, write_w, write_h, | ||
| 29794 | + palette_onscreen(rmi, cmi, write_w, write_h, fctx), false, | ||
| 29795 | + ); | ||
| 29716 | hit!(PALETTE_HITS); | 29796 | hit!(PALETTE_HITS); |
| 29717 | hit!(INTRA_IN_INTER_PALETTE_Y_HITS); | 29797 | hit!(INTRA_IN_INTER_PALETTE_Y_HITS); |
| 29718 | PaletteY { size: n, colors, map } | 29798 | PaletteY { size: n, colors, map } |
| @@ -29721,7 +29801,12 @@ fn decode_inter_block( | |||
| 29721 | // `av1_get_plane_block_size(bsize, 1, 1)` floors at the 4-px | 29801 | // `av1_get_plane_block_size(bsize, 1, 1)` floors at the 4-px |
| 29722 | // transform, as the rect reader's own copy. | 29802 | // transform, as the rect reader's own copy. |
| 29723 | let (cw, ch) = ((write_w / 2).max(4), (write_h / 2).max(4)); | 29803 | let (cw, ch) = ((write_w / 2).max(4), (write_h / 2).max(4)); |
| 29724 | - let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, true); | 29804 | + let on = palette_onscreen_uv( |
| 29805 | + (write_w, write_h), | ||
| 29806 | + palette_onscreen(rmi, cmi, write_w, write_h, fctx), | ||
| 29807 | + (cw, ch), | ||
| 29808 | + ); | ||
| 29809 | + let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true); | ||
| 29725 | hit!(PALETTE_UV_HITS); | 29810 | hit!(PALETTE_UV_HITS); |
| 29726 | hit!(INTRA_IN_INTER_PALETTE_UV_HITS); | 29811 | hit!(INTRA_IN_INTER_PALETTE_UV_HITS); |
| 29727 | PaletteUv { size: n, u_colors, v_colors, map } | 29812 | PaletteUv { size: n, u_colors, v_colors, map } |
| @@ -33854,7 +33939,10 @@ fn decode_inter_block8( | |||
| 33854 | // lane-t900 r23: `av1_visit_palette` -- the two colour-index maps, Y | 33939 | // lane-t900 r23: `av1_visit_palette` -- the two colour-index maps, Y |
| 33855 | // then the shared chroma one, after the WHOLE mode-info read. | 33940 | // then the shared chroma one, after the WHOLE mode-info read. |
| 33856 | let palette_y = palette_y_pending.map(|(n, colors)| { | 33941 | let palette_y = palette_y_pending.map(|(n, colors)| { |
| 33857 | - let map = decode_color_index_map_wh(dec, cdfs, n, SIDE, SIDE, false); | 33942 | + let map = decode_color_index_map_wh( |
| 33943 | + dec, cdfs, n, SIDE, SIDE, | ||
| 33944 | + palette_onscreen(rmi, cmi, SIDE, SIDE, fctx), false, | ||
| 33945 | + ); | ||
| 33858 | hit!(PALETTE_HITS); | 33946 | hit!(PALETTE_HITS); |
| 33859 | hit!(INTRA_IN_INTER_PALETTE_Y_HITS); | 33947 | hit!(INTRA_IN_INTER_PALETTE_Y_HITS); |
| 33860 | hit_do! { | 33948 | hit_do! { |
| @@ -33868,7 +33956,13 @@ fn decode_inter_block8( | |||
| 33868 | }); | 33956 | }); |
| 33869 | let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { | 33957 | let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { |
| 33870 | // `av1_get_plane_block_size(BLOCK_8X8, 1, 1)` = BLOCK_4X4. | 33958 | // `av1_get_plane_block_size(BLOCK_8X8, 1, 1)` = BLOCK_4X4. |
| 33871 | - let map = decode_color_index_map_wh(dec, cdfs, n, CHROMA_SIDE, CHROMA_SIDE, true); | 33959 | + let on = palette_onscreen_uv( |
| 33960 | + (SIDE, SIDE), | ||
| 33961 | + palette_onscreen(rmi, cmi, SIDE, SIDE, fctx), | ||
| 33962 | + (CHROMA_SIDE, CHROMA_SIDE), | ||
| 33963 | + ); | ||
| 33964 | + let map = | ||
| 33965 | + decode_color_index_map_wh(dec, cdfs, n, CHROMA_SIDE, CHROMA_SIDE, on, true); | ||
| 33872 | hit!(PALETTE_UV_HITS); | 33966 | hit!(PALETTE_UV_HITS); |
| 33873 | hit!(INTRA_IN_INTER_PALETTE_UV_HITS); | 33967 | hit!(INTRA_IN_INTER_PALETTE_UV_HITS); |
| 33874 | hit_do! { | 33968 | hit_do! { |