forge
Explore
Log in

Don't you dare go Hollow

Palette-Y decode order -- UV mode_info + defer index map past filter_intra

Browse files at this commit

37efa9f7c42a0557640191725096ac781d3a9c0aAhmet Kaan GümüşParent: 85c5246
  • fix
    • palette-Y decode order -- UV mode_info + defer index map past filter_intra
Show full message
fix(av1): palette-Y decode order -- UV mode_info + defer index map past filter_intra

r6 pinned the root cause by range trace: decode_color_index_map was called
inline right after the Y colours, but libaom's av1_visit_palette runs it
only after the WHOLE mode-info read (UV palette mode_info +
filter_intra), from parse_decode_block. Ports the UV palette mode_info
read (mode symbol + refusal-by-name if it fires, no need to read
size/colours since we bail immediately either way), moves the Y
colour-index-map decode to after filter_intra, and removes the r3/r4
"index map decodes but pixels don't match" refusal now that the real
gate is pixel-exact against ffmpeg.

Gate a_real_aomenc_stream_with_palette_y_decodes_pixel_exact (renamed
from *_refuses_by_name) now asserts a real pixel match instead of the
old named refusal; refusal_inventory.rs drops the retired string.
crates/ec-av1/src/decode.rsmodified+38 -27
@@ -3007,7 +3007,19 @@ fn read_intra_mode(
3007 // per `read_palette_mode_info`'s own `pmi->palette_size[0] > 0` (not a3007 // per `read_palette_mode_info`'s own `pmi->palette_size[0] > 0` (not a
3008 // neighbour lookup at all), so it is exact even at the excluded call3008 // neighbour lookup at all), so it is exact even at the excluded call
3009 // sites once Y decodes real syntax there too.3009 // sites once Y decodes real syntax there too.
3010- let mut palette_y = None;3010+ //
3011+ // lane-palette r7: libaom's `decode_color_map_tokens` is NOT called
3012+ // inline from `read_palette_mode_info` -- it runs later, from
3013+ // `parse_decode_block` (`decodeframe.c:1135`, `av1_visit_palette`),
3014+ // after the WHOLE mode-info read for the block, including UV palette
3015+ // mode_info (`read_palette_mode_info`'s own second half, right below)
3016+ // and `read_filter_intra_mode_info`. So this branch now only reads
3017+ // mode/size/colours for both planes (deferring the two colour-index-map
3018+ // decodes to after `filter_intra`, below) -- r6's range trace pinned
3019+ // this exact ordering bug (`compare-range-not-tell`: ranges matched
3020+ // bit-for-bit through the Y colours, diverged only at the old inline
3021+ // `decode_color_index_map` call).
3022+ let mut palette_y_pending: Option<(usize, [u16; 8])> = None;
3011 if allow_screen_content_tools3023 if allow_screen_content_tools
3012 && let Some(bsize_ctx) = palette_bsize_ctx(side)3024 && let Some(bsize_ctx) = palette_bsize_ctx(side)
3013 {3025 {
@@ -3042,32 +3054,18 @@ fn read_intra_mode(
3042 let (rng, _) = dec.debug_state();3054 let (rng, _) = dec.debug_state();
3043 eprintln!("TRACE palette_y_colors colors={:?} rng={rng}", &colors[..n]);3055 eprintln!("TRACE palette_y_colors colors={:?} rng={rng}", &colors[..n]);
3044 }3056 }
3045- let map = decode_color_index_map(dec, cdfs, n, side);3057+ palette_y_pending = Some((n, colors));
3046- PALETTE_HITS.with(|c| c.set(c.get() + 1));3058+ }
3047- if trace {3059+ // `read_palette_mode_info`'s UV half (decodemv.c:588): read
3048- eprintln!("TRACE palette_y size={n} colors={:?}", &colors[..n]);3060+ // unconditionally at this bit position whenever `uv_mode ==
3049- }3061+ // UV_DC_PRED`, regardless of whether Y just fired -- this decoder's
3050- // lane-palette r3/r4: `read_palette_colors_y`, `read_uniform`,3062+ // scope always reconstructs chroma alongside luma (`decode_block`
3051- // `palette_color_index_context` and the `PALETTE_Y_COLOR_INDEX`3063+ // always calls `u.reconstruct`/`v.reconstruct`), so `is_chroma_ref`
3052- // CDF table all match the oracle line-for-line (r3), and the3064+ // is always true here and does not need threading in. UV palette
3053- // wavefront's decoded base colours are independently correct3065+ // reconstruction stays out of scope: refusing the moment the mode
3054- // (`[112, 131, 162, 180]`, real SMPTE-bar levels) -- but the3066+ // symbol fires needs no further reads (the whole decode aborts),
3055- // reconstructed pixels this index map produces still do not3067+ // so `palette_uv_size`/`read_palette_colors_uv` are never reached.
3056- // match ffmpeg's decode of the same bytes (r3's3068+ let palette_uv_mode_ctx = usize::from(use_palette_y);
3057- // `a_real_aomenc_stream_with_palette_y_refuses_by_name`).
3058- // Refuse by name rather than hand back wrong pixels: every
3059- // symbol above is still consumed exactly as read (so
3060- // `palette_hits()` keeps proving this path is reached), the
3061- // reconstruction code below stays in place for when the
3062- // desync is found, and the caller sees a named refusal instead
3063- // of silently-wrong output.
3064- let _ = PaletteY { size: n, colors, map };
3065- return Err(unsupported(
3066- "a block that actually uses a palette (Y) -- the index map decodes but the \
3067- reconstructed pixels do not match libaom yet (lane-palette r3/r4)",
3068- ));
3069- }
3070- let palette_uv_mode_ctx = usize::from(palette_y.is_some());
3071 if uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 {3069 if uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 {
3072 return Err(unsupported(3070 return Err(unsupported(
3073 "a block that actually uses a palette (UV) -- reconstruction is out of scope",3071 "a block that actually uses a palette (UV) -- reconstruction is out of scope",
@@ -3102,6 +3100,19 @@ fn read_intra_mode(
3102 filter_intra = Some(fi_mode);3100 filter_intra = Some(fi_mode);
3103 }3101 }
3104 }3102 }
3103+ // `av1_visit_palette` (decoder.c:234): the colour-index-map decode runs
3104+ // here, after every mode-info symbol for the block (including UV
3105+ // palette and filter_intra above) -- plane 0 (Y) only in this decoder's
3106+ // scope, since UV palette already refused by name above and never left
3107+ // a pending map to decode.
3108+ let palette_y = palette_y_pending.map(|(n, colors)| {
3109+ let map = decode_color_index_map(dec, cdfs, n, side);
3110+ PALETTE_HITS.with(|c| c.set(c.get() + 1));
3111+ if trace {
3112+ eprintln!("TRACE palette_y size={n} colors={:?}", &colors[..n]);
3113+ }
3114+ PaletteY { size: n, colors, map }
3115+ });
3105 Ok((3116 Ok((
3106 skip,3117 skip,
3107 mode,3118 mode,
crates/ec-av1/src/refusal_inventory.rsmodified+0 -1
@@ -46,7 +46,6 @@ const REFUSALS: &[&str] = &[
46 "a HORZ/VERT intra strip with a split transform (per-unit rect prediction is not ported)",46 "a HORZ/VERT intra strip with a split transform (per-unit rect prediction is not ported)",
47 "a block that actually uses a palette (UV) -- reconstruction is out of scope",47 "a block that actually uses a palette (UV) -- reconstruction is out of scope",
48 "a block that actually uses a palette (Y) -- reconstruction is out of scope",48 "a block that actually uses a palette (Y) -- reconstruction is out of scope",
49- "a block that actually uses a palette (Y) -- the index map decodes but the reconstructed pixels do not match libaom yet (lane-palette r3/r4)",
50 "a block that actually uses intrabc (this decoder never reconstructs one)",49 "a block that actually uses intrabc (this decoder never reconstructs one)",
51 "a frame OBU with no tile group",50 "a frame OBU with no tile group",
52 "a frame naming primary_ref_frame at a reference slot with no saved CDF state",51 "a frame naming primary_ref_frame at a reference slot with no saved CDF state",
crates/ec-av1/src/stream.rsmodified+22 -28
@@ -1499,21 +1499,20 @@ mod tests {
1499 /// palette block would refuse (or not) by construction without proving1499 /// palette block would refuse (or not) by construction without proving
1500 /// this milestone at all.1500 /// this milestone at all.
1501 ///1501 ///
1502- /// lane-palette r4: r3 found that this fixture's reconstructed pixels do1502+ /// lane-palette r4/r6/r7: r3/r4 found that this fixture's reconstructed
1503- /// not match ffmpeg's decode of the same bytes, with every symbol read1503+ /// pixels did not match ffmpeg's decode of the same bytes even though
1504- /// checked line-for-line against the oracle and matching (see1504+ /// every symbol read by `decode_color_index_map` checked line-for-line
1505- /// `decode.rs`'s `read_palette_colors_y`/`palette_color_index_context`1505+ /// against the oracle and matched. r6's range trace (`compare-range-not-tell`)
1506- /// doc comments). A real palette-Y block must not decode to silently1506+ /// pinned the real cause: `decode_color_index_map` was called from the
1507- /// wrong pixels, so `decode.rs`'s palette-Y syntax reader now refuses by1507+ /// wrong syntax position -- inline right after the Y colours, instead of
1508- /// name right after decoding the colours and index map (proving1508+ /// after the whole mode-info read (UV palette mode_info + `filter_intra`,
1509- /// `palette_hits` still fires) instead of handing the wrong1509+ /// `av1_visit_palette`'s real call order). r7 ported the UV palette
1510- /// reconstruction back -- this gate asserts THAT refusal, not a pixel1510+ /// mode_info read and moved the colour-index-map decode after
1511- /// match. Flip back to a pixel-exact assertion once the desync1511+ /// `filter_intra`; this gate now asserts a real pixel match instead of
1512- /// (decode.rs's early `return Err` in the `palette_y_mode` arm) is1512+ /// the old named refusal.
1513- /// found and removed.
1514 #[test]1513 #[test]
1515- fn a_real_aomenc_stream_with_palette_y_refuses_by_name() {1514+ fn a_real_aomenc_stream_with_palette_y_decodes_pixel_exact() {
1516- const NAME: &str = "a_real_aomenc_stream_with_palette_y_refuses_by_name";1515+ const NAME: &str = "a_real_aomenc_stream_with_palette_y_decodes_pixel_exact";
1517 if !have_ffmpeg() {1516 if !have_ffmpeg() {
1518 eprintln!("SKIP {NAME}: no ffmpeg");1517 eprintln!("SKIP {NAME}: no ffmpeg");
1519 return;1518 return;
@@ -1608,23 +1607,18 @@ mod tests {
1608 );1607 );
1609 let stream = out.stdout;1608 let stream = out.stdout;
1610 let before = decode::palette_hits();1609 let before = decode::palette_hits();
1611- match decode_stream(&stream) {1610+ let frames = match decode_stream(&stream) {
1612- Ok(_) => panic!(1611+ Ok(frames) => frames,
1613- "{NAME}: decode_stream succeeded -- a real palette-Y block must refuse by \1612+ Err(e) => panic!("{NAME}: decode_stream refused: {e}"),
1614- name until the pixel desync is fixed, not decode to wrong pixels"1613+ };
1615- ),
1616- Err(e) => {
1617- let msg = e.to_string();
1618- assert!(
1619- msg.contains("reconstructed pixels do not match libaom"),
1620- "{NAME}: wrong refusal: {msg}"
1621- );
1622- }
1623- }
1624 assert!(1614 assert!(
1625 decode::palette_hits() > before,1615 decode::palette_hits() > before,
1626- "palette_y_mode never fired decoding this stream -- refusal is vacuous"1616+ "palette_y_mode never fired decoding this stream -- gate is vacuous"
1627 );1617 );
1618+ let ffmpeg_frames = ffmpeg_decode_sequence(&stream, 64, 64, 1);
1619+ assert_eq!(frames[0].y, ffmpeg_frames[0].y, "luma vs ffmpeg");
1620+ assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg");
1621+ assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg");
1628 }1622 }
1629 1623
1630 /// As [`a_real_aomenc_filter_intra_stream_decodes_pixel_exact`]'s1624 /// As [`a_real_aomenc_filter_intra_stream_decodes_pixel_exact`]'s