diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index b8e4638e..4169b0e8 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -2792,7 +2792,25 @@ fn read_intra_mode( if trace { eprintln!("TRACE palette_y size={n} colors={:?}", &colors[..n]); } - palette_y = Some(PaletteY { size: n, colors, map }); + // 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()); if uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 { diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 3a20cd1d..718dbade 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -1451,22 +1451,32 @@ mod tests { /// superblock either way, but pinning them keeps the recipe identical to /// every other gate in this file. HARD-asserts /// [`decode::palette_hits`] moved -- a stream that never reads a real - /// palette block would pixel-match by construction (nothing new - /// exercised) without proving this milestone at all. + /// 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. #[test] - fn a_real_aomenc_stream_with_palette_y_decodes_pixel_exact() { + fn a_real_aomenc_stream_with_palette_y_refuses_by_name() { + const NAME: &str = "a_real_aomenc_stream_with_palette_y_refuses_by_name"; if !have_ffmpeg() { - eprintln!("SKIP a_real_aomenc_stream_with_palette_y_decodes_pixel_exact: no ffmpeg"); + eprintln!("SKIP {NAME}: no ffmpeg"); return; } if !have_aomenc() { - eprintln!( - "SKIP a_real_aomenc_stream_with_palette_y_decodes_pixel_exact: no aomenc at {}", - aomenc_path().display() - ); + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); return; } - let (width, height) = (64usize, 64usize); let source = "smptebars=size=64x64:rate=25"; fn render(source: &str) -> Vec { Command::new("ffmpeg") @@ -1553,44 +1563,23 @@ mod tests { ); let stream = out.stdout; let before = decode::palette_hits(); - let frames = match decode_stream(&stream) { - Ok(frames) => frames, + 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) => { - panic!("SKIP a_real_aomenc_stream_with_palette_y_decodes_pixel_exact: {e}"); + let msg = e.to_string(); + assert!( + msg.contains("reconstructed pixels do not match libaom"), + "{NAME}: wrong refusal: {msg}" + ); } - }; + } assert!( decode::palette_hits() > before, - "palette_y_mode never fired decoding this stream" + "palette_y_mode never fired decoding this stream -- refusal is vacuous" ); - let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); - // A real palette-Y block decodes: the base colours (`read_palette_colors_y`) - // come back matching the source's actual bar luma values (verified by - // hand against this fixture -- [112, 131, 162, 180], the four leftmost - // SMPTE bar levels), and `palette_hits` fires. But the reconstructed - // pixels themselves do not match ffmpeg's -- traced with `EC_AV1_TRACE=1` - // to `decode_color_index_map`'s wavefront read for the *first* palette - // block: `map[0] = read_uniform(dec, 4)` decodes to an index whose - // colour does not match the known-correct pixel(0,0) value (180, - // index 3) despite `read_uniform`/`av1_get_palette_color_index_context`/ - // the `PALETTE_Y_COLOR_INDEX` CDF table all having been checked - // line-for-line against libaom (decoder.h:425, entropymode.c:893, - // entropymode.c:679) and matching exactly -- the desync's actual - // cause is still unlocated (lane-palette r3, out of budget). SKIP - // rather than a red suite; the hard-asserted `palette_hits` above is - // the real, non-vacuous proof this gate reaches the reconstruction - // path at all. - if frames[0].y != ffmpeg_frames[0].y - || frames[0].u != ffmpeg_frames[0].u - || frames[0].v != ffmpeg_frames[0].v - { - eprintln!( - "SKIP a_real_aomenc_stream_with_palette_y_decodes_pixel_exact: palette_hits \ - fired but the reconstructed pixels do not match ffmpeg -- known open bug, \ - see this test's own doc comment" - ); - return; - } } /// As [`a_real_aomenc_filter_intra_stream_decodes_pixel_exact`]'s diff --git a/scripts/instrument-aom-oracle.sh b/scripts/instrument-aom-oracle.sh index 06c89a3e..8df7b472 100755 --- a/scripts/instrument-aom-oracle.sh +++ b/scripts/instrument-aom-oracle.sh @@ -263,3 +263,85 @@ s = s.replace(old, new, 1) open(path, "w").write(s) print("intra mode-info instrumented") PYI + +# --- rung 6: palette colour-index map range ladder (detokenize.c) ------- +# EC_TRACE_PALETTE=1 -> "EC_PAL row=.. col=.. ctx=.. n=.. rng=.." before every +# colour-index symbol in decode_color_map_tokens's wavefront, "EC_PAL_VAL +# row=.. col=.. color_idx=.. rng=.." after. lane-palette r4: r3 already +# cleared every table/context function against this same source line-for-line +# by hand; this rung is what lets a real per-symbol range compare (class +# compare-range-not-tell / equal-range-means-unread) replace that by-hand +# check instead of re-reading the tables again. +I="$SRC/av1/decoder/detokenize.c" +[ -f "$I" ] || { echo "no oracle source at $I" >&2; exit 1; } + +python3 - "$I" <<'PYP' +import sys +path = sys.argv[1] +s = open(path).read() +if "EC_INSTRUMENTED_PALETTE" in s: + print("palette map already instrumented (no-op)") + sys.exit(0) + +old = """ const int color_ctx = av1_get_palette_color_index_context( + color_map, plane_block_width, (i - j), j, n, color_order, NULL); + const int color_idx = aom_read_symbol( + r, color_map_cdf[n - PALETTE_MIN_SIZE][color_ctx], n, ACCT_STR); + assert(color_idx >= 0 && color_idx < n); + color_map[(i - j) * plane_block_width + j] = color_order[color_idx];""" +new = """/* EC_INSTRUMENTED_PALETTE */ + const int color_ctx = av1_get_palette_color_index_context( + color_map, plane_block_width, (i - j), j, n, color_order, NULL); + const int ec_pal_trace = getenv("EC_TRACE_PALETTE") != NULL; + if (ec_pal_trace) { + fprintf(stderr, "EC_PAL row=%d col=%d ctx=%d n=%d rng=%u\\n", (i - j), + j, color_ctx, n, (unsigned)r->ec.rng); + } + const int color_idx = aom_read_symbol( + r, color_map_cdf[n - PALETTE_MIN_SIZE][color_ctx], n, ACCT_STR); + assert(color_idx >= 0 && color_idx < n); + color_map[(i - j) * plane_block_width + j] = color_order[color_idx]; + if (ec_pal_trace) { + fprintf(stderr, "EC_PAL_VAL row=%d col=%d color_idx=%d rng=%u\\n", + (i - j), j, color_idx, (unsigned)r->ec.rng); + }""" +assert old in s, "decode_color_map_tokens loop body moved" +s = s.replace(old, new, 1) +s = s.replace( + "static void decode_color_map_tokens(Av1ColorMapParam *param, aom_reader *r) {", + "/* EC_INSTRUMENTED_PALETTE */\nstatic void decode_color_map_tokens(Av1ColorMapParam *param, aom_reader *r) {", + 1, +) +open(path, "w").write(s) +print("palette map instrumented") +PYP + +# --- rung 6b: palette map[0] (av1_read_uniform) range ladder ------------ +python3 - "$I" <<'PYP0' +import sys +path = sys.argv[1] +s = open(path).read() +if "EC_INSTRUMENTED_PALETTE_UNIFORM" in s: + print("palette map[0] already instrumented (no-op)") + sys.exit(0) + +old = """ // The first color index. + color_map[0] = av1_read_uniform(r, n); + assert(color_map[0] < n);""" +new = """ // The first color index. + /* EC_INSTRUMENTED_PALETTE_UNIFORM */ + if (getenv("EC_TRACE_PALETTE") != NULL) { + fprintf(stderr, "EC_PAL row=0 col=0 ctx=-1 n=%d rng=%u\\n", n, + (unsigned)r->ec.rng); + } + color_map[0] = av1_read_uniform(r, n); + assert(color_map[0] < n); + if (getenv("EC_TRACE_PALETTE") != NULL) { + fprintf(stderr, "EC_PAL_VAL row=0 col=0 color_idx=%d rng=%u\\n", + color_map[0], (unsigned)r->ec.rng); + }""" +assert old in s, "color_map[0] uniform read moved" +s = s.replace(old, new, 1) +open(path, "w").write(s) +print("palette map[0] instrumented") +PYP0