Merge branch 'lane-svtinter' (477ee608): the palette colour-index map is read over the on-screen part of a block cut by the frame edge and replicated outward; 192/192 SVT-AV1 screen streams decode exact
Browse files at this commit3 changed files +326 -20
- crates/ec-av1/src/decode.rs +125 -20
- crates/ec-av1/src/stream.rs +62 -0
- lanes/svtinter.report.md +139 -0
crates/ec-av1/src/decode.rsmodified+125 -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,64 @@ 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 | + // corner-cut: no mi dims means this is not a real frame decode but the | ||
| 8839 | + // ENCODER's own in-process reconstruct (`FrameCtx::for_encoder`, where | ||
| 8840 | + // `set_segmentation` never runs), whose writer half | ||
| 8841 | + // (`tile::write_color_index_map`) is unclamped too -- the two mirror each | ||
| 8842 | + // other, and clamping only one of them read every map as 1x1. Ceiling: | ||
| 8843 | + // our encoder still writes a full map for a palette block cut by the | ||
| 8844 | + // frame edge, which a conformant decoder reads short. Upgrade path: clamp | ||
| 8845 | + // the writer, publish the mi dims on the encoder's ctx, drop this branch. | ||
| 8846 | + if mi_rows == 0 || mi_cols == 0 { | ||
| 8847 | + return (bw, bh); | ||
| 8848 | + } | ||
| 8849 | + ( | ||
| 8850 | + bw.min(mi_cols.saturating_sub(mi_c) * MI), | ||
| 8851 | + bh.min(mi_rows.saturating_sub(mi_r) * MI), | ||
| 8852 | + ) | ||
| 8853 | +} | ||
| 8854 | + | ||
| 8855 | +/// [`palette_onscreen`] carried into the chroma plane: `>> subsampling` plus | ||
| 8856 | +/// the SAME sub-8 bump the caller's own `(bw / 2).max(4)` applies to the block | ||
| 8857 | +/// dimensions (`is_chroma_sub8_x/y`, blockd.h:1533) -- both must move together | ||
| 8858 | +/// or a cut 8-wide block reads its chroma map at the wrong width. | ||
| 8859 | +fn palette_onscreen_uv( | ||
| 8860 | + luma: (usize, usize), | ||
| 8861 | + on: (usize, usize), | ||
| 8862 | + chroma: (usize, usize), | ||
| 8863 | +) -> (usize, usize) { | ||
| 8864 | + ( | ||
| 8865 | + (on.0 >> 1) + (chroma.0 - (luma.0 >> 1)), | ||
| 8866 | + (on.1 >> 1) + (chroma.1 - (luma.1 >> 1)), | ||
| 8867 | + ) | ||
| 8868 | +} | ||
| 8869 | + | ||
| 8806 | /// `decode_color_map_tokens` (detokenize.c:25): the palette-Y colour-index | 8870 | /// `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 | 8871 | +/// 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 | 8872 | +/// `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( | 8873 | fn decode_color_index_map( |
| 8814 | dec: &mut SymbolDecoder, | 8874 | dec: &mut SymbolDecoder, |
| 8815 | cdfs: &mut Cdfs, | 8875 | cdfs: &mut Cdfs, |
| 8816 | n: usize, | 8876 | n: usize, |
| 8817 | side: usize, | 8877 | side: usize, |
| 8878 | + on: (usize, usize), | ||
| 8818 | uv: bool, | 8879 | uv: bool, |
| 8819 | ) -> Vec<u8> { | 8880 | ) -> Vec<u8> { |
| 8820 | - decode_color_index_map_wh(dec, cdfs, n, side, side, uv) | 8881 | + decode_color_index_map_wh(dec, cdfs, n, side, side, on, uv) |
| 8821 | } | 8882 | } |
| 8822 | 8883 | ||
| 8823 | /// [`decode_color_index_map`], generalised to a true `bw`x`bh` rect strip | 8884 | /// [`decode_color_index_map`], generalised to a true `bw`x`bh` rect strip |
| @@ -8831,9 +8892,15 @@ fn decode_color_index_map_wh( | |||
| 8831 | n: usize, | 8892 | n: usize, |
| 8832 | bw: usize, | 8893 | bw: usize, |
| 8833 | bh: usize, | 8894 | bh: usize, |
| 8895 | + // `(onscreenWidth, onscreenHeight)` in this plane's own units. | ||
| 8896 | + on: (usize, usize), | ||
| 8834 | uv: bool, | 8897 | uv: bool, |
| 8835 | ) -> Vec<u8> { | 8898 | ) -> Vec<u8> { |
| 8836 | let trace = crate::envflags::env_flag!("EC_AV1_TRACE"); | 8899 | let trace = crate::envflags::env_flag!("EC_AV1_TRACE"); |
| 8900 | + let (on_bw, on_bh) = (on.0.clamp(1, bw), on.1.clamp(1, bh)); | ||
| 8901 | + if on_bw < bw || on_bh < bh { | ||
| 8902 | + hit!(PALETTE_ONSCREEN_CUT_HITS); | ||
| 8903 | + } | ||
| 8837 | let mut map = vec![0u8; bw * bh]; | 8904 | let mut map = vec![0u8; bw * bh]; |
| 8838 | if trace { | 8905 | if trace { |
| 8839 | let (rng, _) = dec.debug_state(); | 8906 | let (rng, _) = dec.debug_state(); |
| @@ -8844,9 +8911,9 @@ fn decode_color_index_map_wh( | |||
| 8844 | let (rng, _) = dec.debug_state(); | 8911 | let (rng, _) = dec.debug_state(); |
| 8845 | eprintln!("EC_PAL_VAL row=0 col=0 color_idx={} rng={rng}", map[0]); | 8912 | eprintln!("EC_PAL_VAL row=0 col=0 color_idx={} rng={rng}", map[0]); |
| 8846 | } | 8913 | } |
| 8847 | - for i in 1..(bw + bh - 1) { | 8914 | + for i in 1..(on_bw + on_bh - 1) { |
| 8848 | - let j_hi = i.min(bw - 1); | 8915 | + let j_hi = i.min(on_bw - 1); |
| 8849 | - let j_lo = i.saturating_sub(bh - 1); | 8916 | + let j_lo = i.saturating_sub(on_bh - 1); |
| 8850 | for j in (j_lo..=j_hi).rev() { | 8917 | for j in (j_lo..=j_hi).rev() { |
| 8851 | let row = i - j; | 8918 | let row = i - j; |
| 8852 | let col = j; | 8919 | let col = j; |
| @@ -8872,6 +8939,17 @@ fn decode_color_index_map_wh( | |||
| 8872 | } | 8939 | } |
| 8873 | } | 8940 | } |
| 8874 | } | 8941 | } |
| 8942 | + // `decode_color_map_tokens`'s own two extension loops (detokenize.c:71): | ||
| 8943 | + // the last on-screen column then the last on-screen row are replicated | ||
| 8944 | + // over the off-screen remainder, so the prediction still covers the whole | ||
| 8945 | + // block. | ||
| 8946 | + for row in 0..on_bh { | ||
| 8947 | + let last = map[row * bw + on_bw - 1]; | ||
| 8948 | + map[row * bw + on_bw..row * bw + bw].fill(last); | ||
| 8949 | + } | ||
| 8950 | + for row in on_bh..bh { | ||
| 8951 | + map.copy_within((on_bh - 1) * bw..on_bh * bw, row * bw); | ||
| 8952 | + } | ||
| 8875 | map | 8953 | map |
| 8876 | } | 8954 | } |
| 8877 | 8955 | ||
| @@ -9115,7 +9193,8 @@ fn read_intra_mode_rect( | |||
| 9115 | // `av1_visit_palette`: plane 0 (Y) then plane 1 (chroma, shared U/V map), | 9193 | // `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. | 9194 | // same order as [`read_intra_mode`]'s own square path. |
| 9117 | let palette_y = palette_y_pending.map(|(n, colors)| { | 9195 | let palette_y = palette_y_pending.map(|(n, colors)| { |
| 9118 | - let map = decode_color_index_map_wh(dec, cdfs, n, bw, bh, false); | 9196 | + let map = |
| 9197 | + decode_color_index_map_wh(dec, cdfs, n, bw, bh, palette_onscreen(mi_r, mi_c, bw, bh, fctx), false); | ||
| 9119 | hit!(PALETTE_HITS); | 9198 | hit!(PALETTE_HITS); |
| 9120 | hit!(PALETTE_RECT_HITS); | 9199 | hit!(PALETTE_RECT_HITS); |
| 9121 | PaletteY { size: n, colors, map } | 9200 | PaletteY { size: n, colors, map } |
| @@ -9127,7 +9206,8 @@ fn read_intra_mode_rect( | |||
| 9127 | // axis to 2 read HALF the colour-index symbols of a 1:4 strip's | 9206 | // axis to 2 read HALF the colour-index symbols of a 1:4 strip's |
| 9128 | // chroma palette and desynced the tile from there. | 9207 | // chroma palette and desynced the tile from there. |
| 9129 | let (cw, ch) = ((bw / 2).max(4), (bh / 2).max(4)); | 9208 | 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); | 9209 | + let on = palette_onscreen_uv((bw, bh), palette_onscreen(mi_r, mi_c, bw, bh, fctx), (cw, ch)); |
| 9210 | + let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true); | ||
| 9131 | hit!(PALETTE_UV_HITS); | 9211 | hit!(PALETTE_UV_HITS); |
| 9132 | PaletteUv { size: n, u_colors, v_colors, map } | 9212 | PaletteUv { size: n, u_colors, v_colors, map } |
| 9133 | }); | 9213 | }); |
| @@ -10419,7 +10499,8 @@ fn decode_intra_rect_in_inter( | |||
| 10419 | } | 10499 | } |
| 10420 | // `av1_visit_palette`: Y's colour-index map then the shared chroma one. | 10500 | // `av1_visit_palette`: Y's colour-index map then the shared chroma one. |
| 10421 | let palette_y = palette_y_pending.map(|(n, colors)| { | 10501 | let palette_y = palette_y_pending.map(|(n, colors)| { |
| 10422 | - let map = decode_color_index_map_wh(dec, cdfs, n, bw, bh, false); | 10502 | + let map = |
| 10503 | + decode_color_index_map_wh(dec, cdfs, n, bw, bh, palette_onscreen(mi_r, mi_c, bw, bh, fctx), false); | ||
| 10423 | hit!(PALETTE_HITS); | 10504 | hit!(PALETTE_HITS); |
| 10424 | hit!(PALETTE_RECT_HITS); | 10505 | hit!(PALETTE_RECT_HITS); |
| 10425 | hit!(INTRA_IN_INTER_PALETTE_Y_HITS); | 10506 | hit!(INTRA_IN_INTER_PALETTE_Y_HITS); |
| @@ -10427,7 +10508,8 @@ fn decode_intra_rect_in_inter( | |||
| 10427 | }); | 10508 | }); |
| 10428 | let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { | 10509 | let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { |
| 10429 | let (cw, ch) = ((bw / 2).max(4), (bh / 2).max(4)); | 10510 | 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); | 10511 | + let on = palette_onscreen_uv((bw, bh), palette_onscreen(mi_r, mi_c, bw, bh, fctx), (cw, ch)); |
| 10512 | + let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true); | ||
| 10431 | hit!(PALETTE_UV_HITS); | 10513 | hit!(PALETTE_UV_HITS); |
| 10432 | hit!(INTRA_IN_INTER_PALETTE_UV_HITS); | 10514 | hit!(INTRA_IN_INTER_PALETTE_UV_HITS); |
| 10433 | PaletteUv { size: n, u_colors, v_colors, map } | 10515 | PaletteUv { size: n, u_colors, v_colors, map } |
| @@ -13290,7 +13372,8 @@ fn read_intra_mode( | |||
| 13290 | // `av1_visit_palette` (decoder.c:234): plane 0 (Y) then plane 1 (chroma, | 13372 | // `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. | 13373 | // shared U/V map) -- the same order libaom's own loop visits them in. |
| 13292 | let palette_y = palette_y_pending.map(|(n, colors)| { | 13374 | let palette_y = palette_y_pending.map(|(n, colors)| { |
| 13293 | - let map = decode_color_index_map(dec, cdfs, n, side, false); | 13375 | + let map = |
| 13376 | + decode_color_index_map(dec, cdfs, n, side, palette_onscreen(mi_r, mi_c, side, side, fctx), false); | ||
| 13294 | hit!(PALETTE_HITS); | 13377 | hit!(PALETTE_HITS); |
| 13295 | if trace { | 13378 | if trace { |
| 13296 | eprintln!("TRACE palette_y size={n} colors={:?}", &colors[..n]); | 13379 | eprintln!("TRACE palette_y size={n} colors={:?}", &colors[..n]); |
| @@ -13298,7 +13381,12 @@ fn read_intra_mode( | |||
| 13298 | PaletteY { size: n, colors, map } | 13381 | PaletteY { size: n, colors, map } |
| 13299 | }); | 13382 | }); |
| 13300 | let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { | 13383 | 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); | 13384 | + let on = palette_onscreen_uv( |
| 13385 | + (side, side), | ||
| 13386 | + palette_onscreen(mi_r, mi_c, side, side, fctx), | ||
| 13387 | + (side / 2, side / 2), | ||
| 13388 | + ); | ||
| 13389 | + let map = decode_color_index_map(dec, cdfs, n, side / 2, on, true); | ||
| 13302 | hit!(PALETTE_UV_HITS); | 13390 | hit!(PALETTE_UV_HITS); |
| 13303 | if trace { | 13391 | if trace { |
| 13304 | eprintln!( | 13392 | eprintln!( |
| @@ -29712,7 +29800,10 @@ fn decode_inter_block( | |||
| 29712 | // `av1_visit_palette`: the two colour-index maps, Y then the shared | 29800 | // `av1_visit_palette`: the two colour-index maps, Y then the shared |
| 29713 | // chroma one, after the WHOLE mode-info read. | 29801 | // chroma one, after the WHOLE mode-info read. |
| 29714 | let palette_y = palette_y_pending.map(|(n, colors)| { | 29802 | 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); | 29803 | + let map = decode_color_index_map_wh( |
| 29804 | + dec, cdfs, n, write_w, write_h, | ||
| 29805 | + palette_onscreen(rmi, cmi, write_w, write_h, fctx), false, | ||
| 29806 | + ); | ||
| 29716 | hit!(PALETTE_HITS); | 29807 | hit!(PALETTE_HITS); |
| 29717 | hit!(INTRA_IN_INTER_PALETTE_Y_HITS); | 29808 | hit!(INTRA_IN_INTER_PALETTE_Y_HITS); |
| 29718 | PaletteY { size: n, colors, map } | 29809 | PaletteY { size: n, colors, map } |
| @@ -29721,7 +29812,12 @@ fn decode_inter_block( | |||
| 29721 | // `av1_get_plane_block_size(bsize, 1, 1)` floors at the 4-px | 29812 | // `av1_get_plane_block_size(bsize, 1, 1)` floors at the 4-px |
| 29722 | // transform, as the rect reader's own copy. | 29813 | // transform, as the rect reader's own copy. |
| 29723 | let (cw, ch) = ((write_w / 2).max(4), (write_h / 2).max(4)); | 29814 | 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); | 29815 | + let on = palette_onscreen_uv( |
| 29816 | + (write_w, write_h), | ||
| 29817 | + palette_onscreen(rmi, cmi, write_w, write_h, fctx), | ||
| 29818 | + (cw, ch), | ||
| 29819 | + ); | ||
| 29820 | + let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true); | ||
| 29725 | hit!(PALETTE_UV_HITS); | 29821 | hit!(PALETTE_UV_HITS); |
| 29726 | hit!(INTRA_IN_INTER_PALETTE_UV_HITS); | 29822 | hit!(INTRA_IN_INTER_PALETTE_UV_HITS); |
| 29727 | PaletteUv { size: n, u_colors, v_colors, map } | 29823 | PaletteUv { size: n, u_colors, v_colors, map } |
| @@ -33854,7 +33950,10 @@ fn decode_inter_block8( | |||
| 33854 | // lane-t900 r23: `av1_visit_palette` -- the two colour-index maps, Y | 33950 | // lane-t900 r23: `av1_visit_palette` -- the two colour-index maps, Y |
| 33855 | // then the shared chroma one, after the WHOLE mode-info read. | 33951 | // then the shared chroma one, after the WHOLE mode-info read. |
| 33856 | let palette_y = palette_y_pending.map(|(n, colors)| { | 33952 | let palette_y = palette_y_pending.map(|(n, colors)| { |
| 33857 | - let map = decode_color_index_map_wh(dec, cdfs, n, SIDE, SIDE, false); | 33953 | + let map = decode_color_index_map_wh( |
| 33954 | + dec, cdfs, n, SIDE, SIDE, | ||
| 33955 | + palette_onscreen(rmi, cmi, SIDE, SIDE, fctx), false, | ||
| 33956 | + ); | ||
| 33858 | hit!(PALETTE_HITS); | 33957 | hit!(PALETTE_HITS); |
| 33859 | hit!(INTRA_IN_INTER_PALETTE_Y_HITS); | 33958 | hit!(INTRA_IN_INTER_PALETTE_Y_HITS); |
| 33860 | hit_do! { | 33959 | hit_do! { |
| @@ -33868,7 +33967,13 @@ fn decode_inter_block8( | |||
| 33868 | }); | 33967 | }); |
| 33869 | let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { | 33968 | let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| { |
| 33870 | // `av1_get_plane_block_size(BLOCK_8X8, 1, 1)` = BLOCK_4X4. | 33969 | // `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); | 33970 | + let on = palette_onscreen_uv( |
| 33971 | + (SIDE, SIDE), | ||
| 33972 | + palette_onscreen(rmi, cmi, SIDE, SIDE, fctx), | ||
| 33973 | + (CHROMA_SIDE, CHROMA_SIDE), | ||
| 33974 | + ); | ||
| 33975 | + let map = | ||
| 33976 | + decode_color_index_map_wh(dec, cdfs, n, CHROMA_SIDE, CHROMA_SIDE, on, true); | ||
| 33872 | hit!(PALETTE_UV_HITS); | 33977 | hit!(PALETTE_UV_HITS); |
| 33873 | hit!(INTRA_IN_INTER_PALETTE_UV_HITS); | 33978 | hit!(INTRA_IN_INTER_PALETTE_UV_HITS); |
| 33874 | hit_do! { | 33979 | hit_do! { |
crates/ec-av1/src/stream.rsmodified+62 -0
| @@ -3344,6 +3344,68 @@ pub(crate) mod tests { | |||
| 3344 | .collect() | 3344 | .collect() |
| 3345 | } | 3345 | } |
| 3346 | 3346 | ||
| 3347 | + /// lane-svtinter: an SVT-AV1 screen stream whose frame height is not a | ||
| 3348 | + /// multiple of the superblock size codes PALETTE blocks in the bottom | ||
| 3349 | + /// (and right) superblock row that hang off the mi grid. Spec 5.11.50 | ||
| 3350 | + /// `palette_tokens` reads the colour-index map only over the block's | ||
| 3351 | + /// `onscreenWidth x onscreenHeight` corner and replicates it outward; | ||
| 3352 | + /// this decoder read the full block grid, so it consumed colour-index | ||
| 3353 | + /// symbols libaom never wrote and desynced the rest of the tile -- either | ||
| 3354 | + /// a wrong bottom superblock row (1599 luma samples in this stream's | ||
| 3355 | + /// frame 0, ~80k per inter frame after) or, further along the same tile, | ||
| 3356 | + /// a bogus "a Golomb tail longer than this decoder reads" refusal. | ||
| 3357 | + /// | ||
| 3358 | + /// Unlike its lane-svt1 sibling above this one BUILDS its stream: the | ||
| 3359 | + /// recipe is synthetic (`smptebars,drawgrid` through `libsvtav1 -preset 8 | ||
| 3360 | + /// -svtav1-params screen-content-mode=1`), and 384x152 puts a 32x32 | ||
| 3361 | + /// palette block at mi row 32 of a 38-mi-tall frame, 24 of its 32 rows | ||
| 3362 | + /// on screen. `palette_onscreen_cut_hits` is asserted non-zero so the row | ||
| 3363 | + /// cannot pass on a stream that never cuts a palette map (class | ||
| 3364 | + /// gate-blind-to-feature). | ||
| 3365 | + #[test] | ||
| 3366 | + fn an_svt_screen_palette_map_cut_by_the_frame_edge_decodes_exactly() { | ||
| 3367 | + const NAME: &str = "an_svt_screen_palette_map_cut_by_the_frame_edge_decodes_exactly"; | ||
| 3368 | + if !have_ffmpeg() { | ||
| 3369 | + eprintln!("SKIP {NAME}: no ffmpeg"); | ||
| 3370 | + return; | ||
| 3371 | + } | ||
| 3372 | + let (width, height) = (384usize, 152usize); | ||
| 3373 | + let out = Command::new("ffmpeg") | ||
| 3374 | + .args([ | ||
| 3375 | + "-v", "error", "-f", "lavfi", "-i", | ||
| 3376 | + "smptebars=s=384x152:r=12,drawgrid=w=8:h=8:t=1:c=black", | ||
| 3377 | + "-frames:v", "4", "-c:v", "libsvtav1", "-preset", "8", "-crf", "30", | ||
| 3378 | + "-svtav1-params", "lp=1:screen-content-mode=1", "-g", "12", "-f", "obu", "-", | ||
| 3379 | + ]) | ||
| 3380 | + .stdin(Stdio::null()) | ||
| 3381 | + .stdout(Stdio::piped()) | ||
| 3382 | + .stderr(Stdio::piped()) | ||
| 3383 | + .output() | ||
| 3384 | + .expect("ffmpeg failed to run"); | ||
| 3385 | + if !out.status.success() || out.stdout.is_empty() { | ||
| 3386 | + eprintln!( | ||
| 3387 | + "SKIP {NAME}: no libsvtav1 in this ffmpeg: {}", | ||
| 3388 | + String::from_utf8_lossy(&out.stderr) | ||
| 3389 | + ); | ||
| 3390 | + return; | ||
| 3391 | + } | ||
| 3392 | + let stream = out.stdout; | ||
| 3393 | + crate::decode::reset_palette_onscreen_cut_hits(); | ||
| 3394 | + let decoded = decode_stream(&stream).expect("the SVT-AV1 screen stream decodes"); | ||
| 3395 | + assert_eq!(decoded.len(), 4, "{NAME}: frame count"); | ||
| 3396 | + assert!( | ||
| 3397 | + crate::decode::palette_onscreen_cut_hits() > 0, | ||
| 3398 | + "{NAME}: no palette map is cut by the frame edge here -- the gate would pass blind" | ||
| 3399 | + ); | ||
| 3400 | + let want = ffmpeg_decode_sequence(&stream, width, height, decoded.len()); | ||
| 3401 | + assert_eq!(want.len(), decoded.len(), "{NAME}: ffmpeg frame count"); | ||
| 3402 | + for (i, (got, want)) in decoded.iter().zip(&want).enumerate() { | ||
| 3403 | + assert_eq!(got.y, want.y, "{NAME}: frame {i} luma vs ffmpeg"); | ||
| 3404 | + assert_eq!(got.u, want.u, "{NAME}: frame {i} U vs ffmpeg"); | ||
| 3405 | + assert_eq!(got.v, want.v, "{NAME}: frame {i} V vs ffmpeg"); | ||
| 3406 | + } | ||
| 3407 | + } | ||
| 3408 | + | ||
| 3347 | /// lane-svt1: an SVT-AV1 (v3.1.2, `-preset 8 -crf 35`) screen key frame | 3409 | /// lane-svt1: an SVT-AV1 (v3.1.2, `-preset 8 -crf 35`) screen key frame |
| 3348 | /// codes 16x16 PALETTE blocks with `skip == 1` whose `tx_depth` split the | 3410 | /// codes 16x16 PALETTE blocks with `skip == 1` whose `tx_depth` split the |
| 3349 | /// luma transform. [`crate::decode::decode_block`] armed the palette | 3411 | /// luma transform. [`crate::decode::decode_block`] armed the palette |
lanes/svtinter.report.mdadded+139 -0
| @@ -0,0 +1,139 @@ | |||
| 1 | +# lane-svtinter — the palette colour-index map read past the frame edge | ||
| 2 | + | ||
| 3 | +Class: **map-read-past-frame-edge** — a syntax loop whose extent the spec | ||
| 4 | +clamps to the block's ON-SCREEN part, read at the full block size, so the | ||
| 5 | +decoder consumes symbols the encoder never wrote. | ||
| 6 | + | ||
| 7 | +## First divergent symbol | ||
| 8 | + | ||
| 9 | +Stream `smptebars=s=384x152:r=12,drawgrid=w=8:h=8:t=1:c=black` through | ||
| 10 | +`libsvtav1 -preset 8 -crf 30 -svtav1-params lp=1:screen-content-mode=1`, | ||
| 11 | +frame 0 (the KEY frame — the defect is not inter-only; lane-svt1's | ||
| 12 | +"key frames are exact" held only for its own kept stream). | ||
| 13 | + | ||
| 14 | +* Pixels: first wrong luma sample at row 128, col 296; everything after it in | ||
| 15 | + the raster diverges (1599 luma + 28 chroma in frame 0, ~80k per inter | ||
| 16 | + frame). | ||
| 17 | +* Partitions agree with an instrumented aomdec (`EC_TRACE`) through the whole | ||
| 18 | + frame up to `mi_row=32 mi_col=72 bsize=BLOCK_32X32 PARTITION_NONE`, which is | ||
| 19 | + exactly the block covering pixel (128, 288). | ||
| 20 | +* Mode ladder agrees symbol for symbol into that block (`EC_TRACE_MODE_STEP`, | ||
| 21 | + `EC_PALSYN`): `skip=1`, `y_mode=DC`, `uv_mode=DC`, `palette_y_mode=1`, | ||
| 22 | + `palette_y_size=3`, colours `[7,16,24]`, `rng=39432` on both sides. | ||
| 23 | +* **First divergent symbol**: the colour-index map's wavefront, diagonal | ||
| 24 | + `i = 24`. aomdec's last symbol on that diagonal is `row=23 col=1` and it | ||
| 25 | + moves on to `row=0 col=25`; ours reads one more, `row=24 col=0`, and every | ||
| 26 | + symbol after that is one position out of step (`rng` 42060 vs 42068 from | ||
| 27 | + there on). | ||
| 28 | + | ||
| 29 | +## Root cause | ||
| 30 | + | ||
| 31 | +The block is 32x32 at mi row 32 of a 38-mi-tall frame: only 24 of its 32 rows | ||
| 32 | +are inside the mi grid. Spec 5.11.50 `palette_tokens` / | ||
| 33 | +`av1_get_block_dimensions` (blockd.h:1512) read the colour-index map over | ||
| 34 | +`onscreenWidth x onscreenHeight` only and REPLICATE the last on-screen column | ||
| 35 | +and row over the rest (detokenize.c:71). `decode_color_index_map_wh` walked | ||
| 36 | +the full `bw x bh` grid — its own doc comment asserted the clamp was | ||
| 37 | +unnecessary ("palette blocks ... never straddle the frame's true edge"). | ||
| 38 | + | ||
| 39 | +Both reported failure kinds are this one defect: the extra symbols either | ||
| 40 | +shift the rest of the tile (~200k-sample misses) or eventually make a coeff | ||
| 41 | +Golomb tail read as absurdly long ("a Golomb tail longer than this decoder | ||
| 42 | +reads" — a desync symptom, never a reader bound, as the standing class says). | ||
| 43 | + | ||
| 44 | +## Fix | ||
| 45 | + | ||
| 46 | +`crates/ec-av1/src/decode.rs`: | ||
| 47 | + | ||
| 48 | +* `palette_onscreen(mi_r, mi_c, bw, bh, fctx)` — the on-screen extent in luma | ||
| 49 | + pixels, from `fctx.seg_mi_dims` (set once per frame for every frame). | ||
| 50 | +* `palette_onscreen_uv(luma, on, chroma)` — the same carried into chroma, | ||
| 51 | + keeping the `is_chroma_sub8` +2 bump in step with the caller's own | ||
| 52 | + `(bw / 2).max(4)`. | ||
| 53 | +* `decode_color_index_map{,_wh}` take `(onscreenWidth, onscreenHeight)`, read | ||
| 54 | + the wavefront over that corner at the full-block stride, then replicate the | ||
| 55 | + last on-screen column and row (libaom's own two extension loops). | ||
| 56 | +* A second commit (fe850816's successor) restricts the clamp to contexts | ||
| 57 | + where the frame's mi dims are known: the ENCODER's own in-process | ||
| 58 | + reconstruct runs on a `FrameCtx::for_encoder` that never saw | ||
| 59 | + `set_segmentation`, so its dims are `(0, 0)` and the first version read | ||
| 60 | + every map as 1x1 there -- 16 round-trip gates (10 in s1, 6 in s2) refused | ||
| 61 | + with a bogus "a reference frame selected with no picture at this frame's own | ||
| 62 | + ref_frame_idx slot" (verified NOT pre-existing: the same 10 tests pass on | ||
| 63 | + base 5e14d407, `$HOME/.cache/svtinter/sub-base.log`). That path's writer | ||
| 64 | + half is unclamped too, so the two mirror each other; the branch carries a | ||
| 65 | + `corner-cut:` comment naming the ceiling (see Open). | ||
| 66 | + | ||
| 67 | +### Same-shape sweep (class: a per-cell map read over a block that straddles | ||
| 68 | +the frame edge) | ||
| 69 | + | ||
| 70 | +Reader call sites of the fixed function — all five take the clamp: key-frame square | ||
| 71 | +(`read_intra_mode`), rect strip (`read_intra_mode_rect`), intra-in-inter rect | ||
| 72 | +(`decode_intra_rect_in_inter`), inter block intra (`decode_inter_block`), 8x8 | ||
| 73 | +inter leaf (`decode_inter_block8`), luma and chroma each. | ||
| 74 | + | ||
| 75 | +Every OTHER syntax family whose symbol COUNT is clipped by the mi grid was | ||
| 76 | +checked and already carries its clip — this was the last unclipped one: | ||
| 77 | + | ||
| 78 | +| family | site | state | | ||
| 79 | +|---|---|---| | ||
| 80 | +| residual transform units, luma | decode.rs:9546 `if tu_px >= y.true_width \|\| tu_py >= y.true_height` | clipped (lane-hgkf) | | ||
| 81 | +| residual transform units, chroma | decode.rs:24773, 24881 (`max_blocks_wide/high`) | clipped | | ||
| 82 | +| var-tx recursion | decode.rs:17745 `if blk_row >= max_h_mi \|\| blk_col >= max_w_mi` | clipped | | ||
| 83 | +| segment ids | decode.rs:989, 1012 (`x_mis`/`y_mis`) | clipped | | ||
| 84 | +| partition | `hasRows`/`hasCols` inferred split | clipped | | ||
| 85 | +| palette colour-index map | decode.rs `decode_color_index_map_wh` | **fixed here** | | ||
| 86 | + | ||
| 87 | +CfL and intrabc read no per-cell map (CfL is one alpha pair per block, intrabc | ||
| 88 | +one vector), so neither can spend symbols past the edge. | ||
| 89 | + | ||
| 90 | +## Pass table | ||
| 91 | + | ||
| 92 | +Every decode compares EVERY plane of EVERY frame against `ffmpeg -pix_fmt | ||
| 93 | +yuv420p` (harness asserts our decode exited 0 and produced N frames first). | ||
| 94 | + | ||
| 95 | +| streams | before | after | | ||
| 96 | +|---|---|---| | ||
| 97 | +| fresh sweep: `smptebars,drawgrid` x {384x152, 320x184, 640x366} x crf {20,30,35,45}, 4 frames | 8 exact, 2 DIFF (22397/1599 samples in frame 0 alone), 3 REFUSED (Golomb tail) — 12 total | **12/12 exact** | | ||
| 98 | +| kept `$HOME/.cache/svt1-keep/w3/*.obu` (180 streams) | (lane-svt1: 2 refusals + 6 x ~200k-sample misses among the 8 that reach its arm) | **180/180 exact** | | ||
| 99 | + | ||
| 100 | +No refusal string survives on any stream in either set. | ||
| 101 | + | ||
| 102 | +## Witness | ||
| 103 | + | ||
| 104 | +`stream::tests::an_svt_screen_palette_map_cut_by_the_frame_edge_decodes_exactly` | ||
| 105 | +BUILDS its stream in-test (the recipe above at crf 30), SKIPs with a printed | ||
| 106 | +reason when ffmpeg/libsvtav1 is missing, asserts the new | ||
| 107 | +`decode::palette_onscreen_cut_hits()` counter is non-zero (so it cannot pass | ||
| 108 | +on a stream that never cuts a palette map) and compares all three planes of | ||
| 109 | +all four frames against ffmpeg. | ||
| 110 | + | ||
| 111 | +* RED (reader reverted to the full-grid walk, counter kept): `test result: | ||
| 112 | + FAILED. 0 passed; 1 failed`, panic `frame 0 luma vs ffmpeg`. | ||
| 113 | +* GREEN: `test result: ok. 1 passed`. | ||
| 114 | + | ||
| 115 | +## Open | ||
| 116 | + | ||
| 117 | +* `open|` The WRITE side has the same shape: `tile.rs:3457 | ||
| 118 | + write_color_index_map` walks `side x side` with no on-screen clamp | ||
| 119 | + (callers at 3498 / 3908 / 3913). Reachable only when our own encoder codes | ||
| 120 | + a palette block straddling the mi grid (screen-content mode at a frame size | ||
| 121 | + whose bottom/right block is cut) — not fixed here because a writer change | ||
| 122 | + moves the encoder pins, which this lane must leave byte-identical. | ||
| 123 | + | ||
| 124 | +## Gates on 58b83958 | ||
| 125 | + | ||
| 126 | +| gate | result | | ||
| 127 | +|---|---| | ||
| 128 | +| `cargo check --workspace --all-targets -j4` | RC=0, 0 ec-av1 warnings (25 pre-existing `missing documentation` warnings, all in ec-opus) | | ||
| 129 | +| `encode::tests::the_encoders_own_streams_are_byte_identical_to_their_pins` | ok, 1 passed — pins unchanged | | ||
| 130 | +| the new witness | ok, 1 passed | | ||
| 131 | +| kept-film SVT gate (`EC_AV1_SVT1_STREAM`, `an_svt_screen_palette_block_with_a_split_transform_decodes_exactly`) | ok, 1 passed | | ||
| 132 | +| s1 `--skip stream::` | ok, 341 passed, 0 failed, 33 ignored (389 s) | | ||
| 133 | +| s2 `stream:: --skip 10bit` | ok, 202 passed, 0 failed, 15 ignored (607 s) | | ||
| 134 | +| s3 `10bit` | ok, 42 passed, 0 failed, 1 ignored (43 s) | | ||
| 135 | +| 180 kept `w3` streams vs ffmpeg | 180/180 exact | | ||
| 136 | +| fresh sweep (12 streams) vs ffmpeg | 12/12 exact | | ||
| 137 | + | ||
| 138 | +The first fix alone (f70ec3f9) reddened 16 of these rows; both suites are | ||
| 139 | +green only with 58b83958 on top. | ||