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(
30073007 // per `read_palette_mode_info`'s own `pmi->palette_size[0] > 0` (not a
30083008 // neighbour lookup at all), so it is exact even at the excluded call
30093009 // 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;
30113023 if allow_screen_content_tools
30123024 && let Some(bsize_ctx) = palette_bsize_ctx(side)
30133025 {
@@ -3042,32 +3054,18 @@ fn read_intra_mode(
30423054 let (rng, _) = dec.debug_state();
30433055 eprintln!("TRACE palette_y_colors colors={:?} rng={rng}", &colors[..n]);
30443056 }
3045- let map = decode_color_index_map(dec, cdfs, n, side);
3046- PALETTE_HITS.with(|c| c.set(c.get() + 1));
3047- if trace {
3048- eprintln!("TRACE palette_y size={n} colors={:?}", &colors[..n]);
3049- }
3050- // lane-palette r3/r4: `read_palette_colors_y`, `read_uniform`,
3051- // `palette_color_index_context` and the `PALETTE_Y_COLOR_INDEX`
3052- // CDF table all match the oracle line-for-line (r3), and the
3053- // wavefront's decoded base colours are independently correct
3054- // (`[112, 131, 162, 180]`, real SMPTE-bar levels) -- but the
3055- // reconstructed pixels this index map produces still do not
3056- // match ffmpeg's decode of the same bytes (r3's
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());
3057+ palette_y_pending = Some((n, colors));
3058+ }
3059+ // `read_palette_mode_info`'s UV half (decodemv.c:588): read
3060+ // unconditionally at this bit position whenever `uv_mode ==
3061+ // UV_DC_PRED`, regardless of whether Y just fired -- this decoder's
3062+ // scope always reconstructs chroma alongside luma (`decode_block`
3063+ // always calls `u.reconstruct`/`v.reconstruct`), so `is_chroma_ref`
3064+ // is always true here and does not need threading in. UV palette
3065+ // reconstruction stays out of scope: refusing the moment the mode
3066+ // symbol fires needs no further reads (the whole decode aborts),
3067+ // so `palette_uv_size`/`read_palette_colors_uv` are never reached.
3068+ let palette_uv_mode_ctx = usize::from(use_palette_y);
30713069 if uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 {
30723070 return Err(unsupported(
30733071 "a block that actually uses a palette (UV) -- reconstruction is out of scope",
@@ -3102,6 +3100,19 @@ fn read_intra_mode(
31023100 filter_intra = Some(fi_mode);
31033101 }
31043102 }
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+ });
31053116 Ok((
31063117 skip,
31073118 mode,
crates/ec-av1/src/refusal_inventory.rsmodified+0 -1
@@ -46,7 +46,6 @@ const REFUSALS: &[&str] = &[
4646 "a HORZ/VERT intra strip with a split transform (per-unit rect prediction is not ported)",
4747 "a block that actually uses a palette (UV) -- reconstruction is out of scope",
4848 "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)",
5049 "a block that actually uses intrabc (this decoder never reconstructs one)",
5150 "a frame OBU with no tile group",
5251 "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 {
14991499 /// palette block would refuse (or not) by construction without proving
15001500 /// this milestone at all.
15011501 ///
1502- /// lane-palette r4: r3 found that this fixture's reconstructed pixels do
1503- /// not match ffmpeg's decode of the same bytes, with every symbol read
1504- /// checked line-for-line against the oracle and matching (see
1505- /// `decode.rs`'s `read_palette_colors_y`/`palette_color_index_context`
1506- /// doc comments). A real palette-Y block must not decode to silently
1507- /// wrong pixels, so `decode.rs`'s palette-Y syntax reader now refuses by
1508- /// name right after decoding the colours and index map (proving
1509- /// `palette_hits` still fires) instead of handing the wrong
1510- /// reconstruction back -- this gate asserts THAT refusal, not a pixel
1511- /// match. Flip back to a pixel-exact assertion once the desync
1512- /// (decode.rs's early `return Err` in the `palette_y_mode` arm) is
1513- /// found and removed.
1502+ /// lane-palette r4/r6/r7: r3/r4 found that this fixture's reconstructed
1503+ /// pixels did not match ffmpeg's decode of the same bytes even though
1504+ /// every symbol read by `decode_color_index_map` checked line-for-line
1505+ /// against the oracle and matched. r6's range trace (`compare-range-not-tell`)
1506+ /// pinned the real cause: `decode_color_index_map` was called from the
1507+ /// wrong syntax position -- inline right after the Y colours, instead of
1508+ /// after the whole mode-info read (UV palette mode_info + `filter_intra`,
1509+ /// `av1_visit_palette`'s real call order). r7 ported the UV palette
1510+ /// mode_info read and moved the colour-index-map decode after
1511+ /// `filter_intra`; this gate now asserts a real pixel match instead of
1512+ /// the old named refusal.
15141513 #[test]
1515- fn a_real_aomenc_stream_with_palette_y_refuses_by_name() {
1516- const NAME: &str = "a_real_aomenc_stream_with_palette_y_refuses_by_name";
1514+ fn a_real_aomenc_stream_with_palette_y_decodes_pixel_exact() {
1515+ const NAME: &str = "a_real_aomenc_stream_with_palette_y_decodes_pixel_exact";
15171516 if !have_ffmpeg() {
15181517 eprintln!("SKIP {NAME}: no ffmpeg");
15191518 return;
@@ -1608,23 +1607,18 @@ mod tests {
16081607 );
16091608 let stream = out.stdout;
16101609 let before = decode::palette_hits();
1611- match decode_stream(&stream) {
1612- Ok(_) => panic!(
1613- "{NAME}: decode_stream succeeded -- a real palette-Y block must refuse by \
1614- name until the pixel desync is fixed, not decode to wrong pixels"
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- }
1610+ let frames = match decode_stream(&stream) {
1611+ Ok(frames) => frames,
1612+ Err(e) => panic!("{NAME}: decode_stream refused: {e}"),
1613+ };
16241614 assert!(
16251615 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"
16271617 );
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");
16281622 }
16291623
16301624 /// As [`a_real_aomenc_filter_intra_stream_decodes_pixel_exact`]'s