diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index c2f17bec..f96c7346 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -3007,7 +3007,19 @@ fn read_intra_mode( // per `read_palette_mode_info`'s own `pmi->palette_size[0] > 0` (not a // neighbour lookup at all), so it is exact even at the excluded call // sites once Y decodes real syntax there too. - let mut palette_y = None; + // + // lane-palette r7: libaom's `decode_color_map_tokens` is NOT called + // inline from `read_palette_mode_info` -- it runs later, from + // `parse_decode_block` (`decodeframe.c:1135`, `av1_visit_palette`), + // after the WHOLE mode-info read for the block, including UV palette + // mode_info (`read_palette_mode_info`'s own second half, right below) + // and `read_filter_intra_mode_info`. So this branch now only reads + // mode/size/colours for both planes (deferring the two colour-index-map + // decodes to after `filter_intra`, below) -- r6's range trace pinned + // this exact ordering bug (`compare-range-not-tell`: ranges matched + // bit-for-bit through the Y colours, diverged only at the old inline + // `decode_color_index_map` call). + let mut palette_y_pending: Option<(usize, [u16; 8])> = None; if allow_screen_content_tools && let Some(bsize_ctx) = palette_bsize_ctx(side) { @@ -3042,32 +3054,18 @@ fn read_intra_mode( let (rng, _) = dec.debug_state(); eprintln!("TRACE palette_y_colors colors={:?} rng={rng}", &colors[..n]); } - let map = decode_color_index_map(dec, cdfs, n, side); - PALETTE_HITS.with(|c| c.set(c.get() + 1)); - if trace { - eprintln!("TRACE palette_y size={n} colors={:?}", &colors[..n]); - } - // lane-palette r3/r4: `read_palette_colors_y`, `read_uniform`, - // `palette_color_index_context` and the `PALETTE_Y_COLOR_INDEX` - // CDF table all match the oracle line-for-line (r3), and the - // wavefront's decoded base colours are independently correct - // (`[112, 131, 162, 180]`, real SMPTE-bar levels) -- but the - // reconstructed pixels this index map produces still do not - // match ffmpeg's decode of the same bytes (r3's - // `a_real_aomenc_stream_with_palette_y_refuses_by_name`). - // Refuse by name rather than hand back wrong pixels: every - // symbol above is still consumed exactly as read (so - // `palette_hits()` keeps proving this path is reached), the - // reconstruction code below stays in place for when the - // desync is found, and the caller sees a named refusal instead - // of silently-wrong output. - let _ = PaletteY { size: n, colors, map }; - return Err(unsupported( - "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)", - )); - } - let palette_uv_mode_ctx = usize::from(palette_y.is_some()); + palette_y_pending = Some((n, colors)); + } + // `read_palette_mode_info`'s UV half (decodemv.c:588): read + // unconditionally at this bit position whenever `uv_mode == + // UV_DC_PRED`, regardless of whether Y just fired -- this decoder's + // scope always reconstructs chroma alongside luma (`decode_block` + // always calls `u.reconstruct`/`v.reconstruct`), so `is_chroma_ref` + // is always true here and does not need threading in. UV palette + // reconstruction stays out of scope: refusing the moment the mode + // symbol fires needs no further reads (the whole decode aborts), + // so `palette_uv_size`/`read_palette_colors_uv` are never reached. + let palette_uv_mode_ctx = usize::from(use_palette_y); if uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 { return Err(unsupported( "a block that actually uses a palette (UV) -- reconstruction is out of scope", @@ -3102,6 +3100,19 @@ fn read_intra_mode( filter_intra = Some(fi_mode); } } + // `av1_visit_palette` (decoder.c:234): the colour-index-map decode runs + // here, after every mode-info symbol for the block (including UV + // palette and filter_intra above) -- plane 0 (Y) only in this decoder's + // scope, since UV palette already refused by name above and never left + // a pending map to decode. + let palette_y = palette_y_pending.map(|(n, colors)| { + let map = decode_color_index_map(dec, cdfs, n, side); + PALETTE_HITS.with(|c| c.set(c.get() + 1)); + if trace { + eprintln!("TRACE palette_y size={n} colors={:?}", &colors[..n]); + } + PaletteY { size: n, colors, map } + }); Ok(( skip, mode, diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index 1903a31f..d3534d1e 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -46,7 +46,6 @@ const REFUSALS: &[&str] = &[ "a HORZ/VERT intra strip with a split transform (per-unit rect prediction is not ported)", "a block that actually uses a palette (UV) -- reconstruction is out of scope", "a block that actually uses a palette (Y) -- reconstruction is out of scope", - "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)", "a block that actually uses intrabc (this decoder never reconstructs one)", "a frame OBU with no tile group", "a frame naming primary_ref_frame at a reference slot with no saved CDF state", diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 92137166..234399d6 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -1499,21 +1499,20 @@ mod tests { /// palette block would refuse (or not) by construction without proving /// this milestone at all. /// - /// lane-palette r4: r3 found that this fixture's reconstructed pixels do - /// not match ffmpeg's decode of the same bytes, with every symbol read - /// checked line-for-line against the oracle and matching (see - /// `decode.rs`'s `read_palette_colors_y`/`palette_color_index_context` - /// doc comments). A real palette-Y block must not decode to silently - /// wrong pixels, so `decode.rs`'s palette-Y syntax reader now refuses by - /// name right after decoding the colours and index map (proving - /// `palette_hits` still fires) instead of handing the wrong - /// reconstruction back -- this gate asserts THAT refusal, not a pixel - /// match. Flip back to a pixel-exact assertion once the desync - /// (decode.rs's early `return Err` in the `palette_y_mode` arm) is - /// found and removed. + /// lane-palette r4/r6/r7: r3/r4 found that this fixture's reconstructed + /// pixels did not match ffmpeg's decode of the same bytes even though + /// every symbol read by `decode_color_index_map` checked line-for-line + /// against the oracle and matched. r6's range trace (`compare-range-not-tell`) + /// pinned the real cause: `decode_color_index_map` was called from the + /// wrong syntax position -- inline right after the Y colours, instead of + /// after the whole mode-info read (UV palette mode_info + `filter_intra`, + /// `av1_visit_palette`'s real call order). r7 ported the UV palette + /// mode_info read and moved the colour-index-map decode after + /// `filter_intra`; this gate now asserts a real pixel match instead of + /// the old named refusal. #[test] - fn a_real_aomenc_stream_with_palette_y_refuses_by_name() { - const NAME: &str = "a_real_aomenc_stream_with_palette_y_refuses_by_name"; + fn a_real_aomenc_stream_with_palette_y_decodes_pixel_exact() { + const NAME: &str = "a_real_aomenc_stream_with_palette_y_decodes_pixel_exact"; if !have_ffmpeg() { eprintln!("SKIP {NAME}: no ffmpeg"); return; @@ -1608,23 +1607,18 @@ mod tests { ); let stream = out.stdout; let before = decode::palette_hits(); - match decode_stream(&stream) { - Ok(_) => panic!( - "{NAME}: decode_stream succeeded -- a real palette-Y block must refuse by \ - name until the pixel desync is fixed, not decode to wrong pixels" - ), - Err(e) => { - let msg = e.to_string(); - assert!( - msg.contains("reconstructed pixels do not match libaom"), - "{NAME}: wrong refusal: {msg}" - ); - } - } + let frames = match decode_stream(&stream) { + Ok(frames) => frames, + Err(e) => panic!("{NAME}: decode_stream refused: {e}"), + }; assert!( decode::palette_hits() > before, - "palette_y_mode never fired decoding this stream -- refusal is vacuous" + "palette_y_mode never fired decoding this stream -- gate is vacuous" ); + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, 64, 64, 1); + assert_eq!(frames[0].y, ffmpeg_frames[0].y, "luma vs ffmpeg"); + assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg"); + assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg"); } /// As [`a_real_aomenc_filter_intra_stream_decodes_pixel_exact`]'s