Palette-Y refuses by name instead of decoding to wrong pixels
Browse files at this commit71d8f67fcf39c2cabd212e1080cd96c412cc0b3aParent: 97e8e94- fix
- palette-Y refuses by name instead of decoding to wrong pixels
3 changed files +132 -43
- crates/ec-av1/src/decode.rs +19 -1
- crates/ec-av1/src/stream.rs +31 -42
- scripts/instrument-aom-oracle.sh +82 -0
crates/ec-av1/src/decode.rsmodified+19 -1
| @@ -2792,7 +2792,25 @@ fn read_intra_mode( | |||
| 2792 | if trace { | 2792 | if trace { |
| 2793 | eprintln!("TRACE palette_y size={n} colors={:?}", &colors[..n]); | 2793 | eprintln!("TRACE palette_y size={n} colors={:?}", &colors[..n]); |
| 2794 | } | 2794 | } |
| 2795 | - palette_y = Some(PaletteY { size: n, colors, map }); | 2795 | + // lane-palette r3/r4: `read_palette_colors_y`, `read_uniform`, |
| 2796 | + // `palette_color_index_context` and the `PALETTE_Y_COLOR_INDEX` | ||
| 2797 | + // CDF table all match the oracle line-for-line (r3), and the | ||
| 2798 | + // wavefront's decoded base colours are independently correct | ||
| 2799 | + // (`[112, 131, 162, 180]`, real SMPTE-bar levels) -- but the | ||
| 2800 | + // reconstructed pixels this index map produces still do not | ||
| 2801 | + // match ffmpeg's decode of the same bytes (r3's | ||
| 2802 | + // `a_real_aomenc_stream_with_palette_y_refuses_by_name`). | ||
| 2803 | + // Refuse by name rather than hand back wrong pixels: every | ||
| 2804 | + // symbol above is still consumed exactly as read (so | ||
| 2805 | + // `palette_hits()` keeps proving this path is reached), the | ||
| 2806 | + // reconstruction code below stays in place for when the | ||
| 2807 | + // desync is found, and the caller sees a named refusal instead | ||
| 2808 | + // of silently-wrong output. | ||
| 2809 | + let _ = PaletteY { size: n, colors, map }; | ||
| 2810 | + return Err(unsupported( | ||
| 2811 | + "a block that actually uses a palette (Y) -- the index map decodes but the \ | ||
| 2812 | + reconstructed pixels do not match libaom yet (lane-palette r3/r4)", | ||
| 2813 | + )); | ||
| 2796 | } | 2814 | } |
| 2797 | let palette_uv_mode_ctx = usize::from(palette_y.is_some()); | 2815 | let palette_uv_mode_ctx = usize::from(palette_y.is_some()); |
| 2798 | if uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 { | 2816 | if uv_mode == DC_PRED && dec.symbol(&mut cdfs.palette_uv_mode[palette_uv_mode_ctx]) != 0 { |
crates/ec-av1/src/stream.rsmodified+31 -42
| @@ -1451,22 +1451,32 @@ mod tests { | |||
| 1451 | /// superblock either way, but pinning them keeps the recipe identical to | 1451 | /// superblock either way, but pinning them keeps the recipe identical to |
| 1452 | /// every other gate in this file. HARD-asserts | 1452 | /// every other gate in this file. HARD-asserts |
| 1453 | /// [`decode::palette_hits`] moved -- a stream that never reads a real | 1453 | /// [`decode::palette_hits`] moved -- a stream that never reads a real |
| 1454 | - /// palette block would pixel-match by construction (nothing new | 1454 | + /// palette block would refuse (or not) by construction without proving |
| 1455 | - /// exercised) without proving this milestone at all. | 1455 | + /// this milestone at all. |
| 1456 | + /// | ||
| 1457 | + /// lane-palette r4: r3 found that this fixture's reconstructed pixels do | ||
| 1458 | + /// not match ffmpeg's decode of the same bytes, with every symbol read | ||
| 1459 | + /// checked line-for-line against the oracle and matching (see | ||
| 1460 | + /// `decode.rs`'s `read_palette_colors_y`/`palette_color_index_context` | ||
| 1461 | + /// doc comments). A real palette-Y block must not decode to silently | ||
| 1462 | + /// wrong pixels, so `decode.rs`'s palette-Y syntax reader now refuses by | ||
| 1463 | + /// name right after decoding the colours and index map (proving | ||
| 1464 | + /// `palette_hits` still fires) instead of handing the wrong | ||
| 1465 | + /// reconstruction back -- this gate asserts THAT refusal, not a pixel | ||
| 1466 | + /// match. Flip back to a pixel-exact assertion once the desync | ||
| 1467 | + /// (decode.rs's early `return Err` in the `palette_y_mode` arm) is | ||
| 1468 | + /// found and removed. | ||
| 1456 | #[test] | 1469 | #[test] |
| 1457 | - fn a_real_aomenc_stream_with_palette_y_decodes_pixel_exact() { | 1470 | + fn a_real_aomenc_stream_with_palette_y_refuses_by_name() { |
| 1471 | + const NAME: &str = "a_real_aomenc_stream_with_palette_y_refuses_by_name"; | ||
| 1458 | if !have_ffmpeg() { | 1472 | if !have_ffmpeg() { |
| 1459 | - eprintln!("SKIP a_real_aomenc_stream_with_palette_y_decodes_pixel_exact: no ffmpeg"); | 1473 | + eprintln!("SKIP {NAME}: no ffmpeg"); |
| 1460 | return; | 1474 | return; |
| 1461 | } | 1475 | } |
| 1462 | if !have_aomenc() { | 1476 | if !have_aomenc() { |
| 1463 | - eprintln!( | 1477 | + eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); |
| 1464 | - "SKIP a_real_aomenc_stream_with_palette_y_decodes_pixel_exact: no aomenc at {}", | ||
| 1465 | - aomenc_path().display() | ||
| 1466 | - ); | ||
| 1467 | return; | 1478 | return; |
| 1468 | } | 1479 | } |
| 1469 | - let (width, height) = (64usize, 64usize); | ||
| 1470 | let source = "smptebars=size=64x64:rate=25"; | 1480 | let source = "smptebars=size=64x64:rate=25"; |
| 1471 | fn render(source: &str) -> Vec<u8> { | 1481 | fn render(source: &str) -> Vec<u8> { |
| 1472 | Command::new("ffmpeg") | 1482 | Command::new("ffmpeg") |
| @@ -1553,44 +1563,23 @@ mod tests { | |||
| 1553 | ); | 1563 | ); |
| 1554 | let stream = out.stdout; | 1564 | let stream = out.stdout; |
| 1555 | let before = decode::palette_hits(); | 1565 | let before = decode::palette_hits(); |
| 1556 | - let frames = match decode_stream(&stream) { | 1566 | + match decode_stream(&stream) { |
| 1557 | - Ok(frames) => frames, | 1567 | + Ok(_) => panic!( |
| 1568 | + "{NAME}: decode_stream succeeded -- a real palette-Y block must refuse by \ | ||
| 1569 | + name until the pixel desync is fixed, not decode to wrong pixels" | ||
| 1570 | + ), | ||
| 1558 | Err(e) => { | 1571 | Err(e) => { |
| 1559 | - panic!("SKIP a_real_aomenc_stream_with_palette_y_decodes_pixel_exact: {e}"); | 1572 | + let msg = e.to_string(); |
| 1573 | + assert!( | ||
| 1574 | + msg.contains("reconstructed pixels do not match libaom"), | ||
| 1575 | + "{NAME}: wrong refusal: {msg}" | ||
| 1576 | + ); | ||
| 1577 | + } | ||
| 1560 | } | 1578 | } |
| 1561 | - }; | ||
| 1562 | assert!( | 1579 | assert!( |
| 1563 | decode::palette_hits() > before, | 1580 | decode::palette_hits() > before, |
| 1564 | - "palette_y_mode never fired decoding this stream" | 1581 | + "palette_y_mode never fired decoding this stream -- refusal is vacuous" |
| 1565 | ); | 1582 | ); |
| 1566 | - let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); | ||
| 1567 | - // A real palette-Y block decodes: the base colours (`read_palette_colors_y`) | ||
| 1568 | - // come back matching the source's actual bar luma values (verified by | ||
| 1569 | - // hand against this fixture -- [112, 131, 162, 180], the four leftmost | ||
| 1570 | - // SMPTE bar levels), and `palette_hits` fires. But the reconstructed | ||
| 1571 | - // pixels themselves do not match ffmpeg's -- traced with `EC_AV1_TRACE=1` | ||
| 1572 | - // to `decode_color_index_map`'s wavefront read for the *first* palette | ||
| 1573 | - // block: `map[0] = read_uniform(dec, 4)` decodes to an index whose | ||
| 1574 | - // colour does not match the known-correct pixel(0,0) value (180, | ||
| 1575 | - // index 3) despite `read_uniform`/`av1_get_palette_color_index_context`/ | ||
| 1576 | - // the `PALETTE_Y_COLOR_INDEX` CDF table all having been checked | ||
| 1577 | - // line-for-line against libaom (decoder.h:425, entropymode.c:893, | ||
| 1578 | - // entropymode.c:679) and matching exactly -- the desync's actual | ||
| 1579 | - // cause is still unlocated (lane-palette r3, out of budget). SKIP | ||
| 1580 | - // rather than a red suite; the hard-asserted `palette_hits` above is | ||
| 1581 | - // the real, non-vacuous proof this gate reaches the reconstruction | ||
| 1582 | - // path at all. | ||
| 1583 | - if frames[0].y != ffmpeg_frames[0].y | ||
| 1584 | - || frames[0].u != ffmpeg_frames[0].u | ||
| 1585 | - || frames[0].v != ffmpeg_frames[0].v | ||
| 1586 | - { | ||
| 1587 | - eprintln!( | ||
| 1588 | - "SKIP a_real_aomenc_stream_with_palette_y_decodes_pixel_exact: palette_hits \ | ||
| 1589 | - fired but the reconstructed pixels do not match ffmpeg -- known open bug, \ | ||
| 1590 | - see this test's own doc comment" | ||
| 1591 | - ); | ||
| 1592 | - return; | ||
| 1593 | - } | ||
| 1594 | } | 1583 | } |
| 1595 | 1584 | ||
| 1596 | /// As [`a_real_aomenc_filter_intra_stream_decodes_pixel_exact`]'s | 1585 | /// As [`a_real_aomenc_filter_intra_stream_decodes_pixel_exact`]'s |
scripts/instrument-aom-oracle.shmodified+82 -0
| @@ -263,3 +263,85 @@ s = s.replace(old, new, 1) | |||
| 263 | open(path, "w").write(s) | 263 | open(path, "w").write(s) |
| 264 | print("intra mode-info instrumented") | 264 | print("intra mode-info instrumented") |
| 265 | PYI | 265 | PYI |
| 266 | + | ||
| 267 | +# --- rung 6: palette colour-index map range ladder (detokenize.c) ------- | ||
| 268 | +# EC_TRACE_PALETTE=1 -> "EC_PAL row=.. col=.. ctx=.. n=.. rng=.." before every | ||
| 269 | +# colour-index symbol in decode_color_map_tokens's wavefront, "EC_PAL_VAL | ||
| 270 | +# row=.. col=.. color_idx=.. rng=.." after. lane-palette r4: r3 already | ||
| 271 | +# cleared every table/context function against this same source line-for-line | ||
| 272 | +# by hand; this rung is what lets a real per-symbol range compare (class | ||
| 273 | +# compare-range-not-tell / equal-range-means-unread) replace that by-hand | ||
| 274 | +# check instead of re-reading the tables again. | ||
| 275 | +I="$SRC/av1/decoder/detokenize.c" | ||
| 276 | +[ -f "$I" ] || { echo "no oracle source at $I" >&2; exit 1; } | ||
| 277 | + | ||
| 278 | +python3 - "$I" <<'PYP' | ||
| 279 | +import sys | ||
| 280 | +path = sys.argv[1] | ||
| 281 | +s = open(path).read() | ||
| 282 | +if "EC_INSTRUMENTED_PALETTE" in s: | ||
| 283 | + print("palette map already instrumented (no-op)") | ||
| 284 | + sys.exit(0) | ||
| 285 | + | ||
| 286 | +old = """ const int color_ctx = av1_get_palette_color_index_context( | ||
| 287 | + color_map, plane_block_width, (i - j), j, n, color_order, NULL); | ||
| 288 | + const int color_idx = aom_read_symbol( | ||
| 289 | + r, color_map_cdf[n - PALETTE_MIN_SIZE][color_ctx], n, ACCT_STR); | ||
| 290 | + assert(color_idx >= 0 && color_idx < n); | ||
| 291 | + color_map[(i - j) * plane_block_width + j] = color_order[color_idx];""" | ||
| 292 | +new = """/* EC_INSTRUMENTED_PALETTE */ | ||
| 293 | + const int color_ctx = av1_get_palette_color_index_context( | ||
| 294 | + color_map, plane_block_width, (i - j), j, n, color_order, NULL); | ||
| 295 | + const int ec_pal_trace = getenv("EC_TRACE_PALETTE") != NULL; | ||
| 296 | + if (ec_pal_trace) { | ||
| 297 | + fprintf(stderr, "EC_PAL row=%d col=%d ctx=%d n=%d rng=%u\\n", (i - j), | ||
| 298 | + j, color_ctx, n, (unsigned)r->ec.rng); | ||
| 299 | + } | ||
| 300 | + const int color_idx = aom_read_symbol( | ||
| 301 | + r, color_map_cdf[n - PALETTE_MIN_SIZE][color_ctx], n, ACCT_STR); | ||
| 302 | + assert(color_idx >= 0 && color_idx < n); | ||
| 303 | + color_map[(i - j) * plane_block_width + j] = color_order[color_idx]; | ||
| 304 | + if (ec_pal_trace) { | ||
| 305 | + fprintf(stderr, "EC_PAL_VAL row=%d col=%d color_idx=%d rng=%u\\n", | ||
| 306 | + (i - j), j, color_idx, (unsigned)r->ec.rng); | ||
| 307 | + }""" | ||
| 308 | +assert old in s, "decode_color_map_tokens loop body moved" | ||
| 309 | +s = s.replace(old, new, 1) | ||
| 310 | +s = s.replace( | ||
| 311 | + "static void decode_color_map_tokens(Av1ColorMapParam *param, aom_reader *r) {", | ||
| 312 | + "/* EC_INSTRUMENTED_PALETTE */\nstatic void decode_color_map_tokens(Av1ColorMapParam *param, aom_reader *r) {", | ||
| 313 | + 1, | ||
| 314 | +) | ||
| 315 | +open(path, "w").write(s) | ||
| 316 | +print("palette map instrumented") | ||
| 317 | +PYP | ||
| 318 | + | ||
| 319 | +# --- rung 6b: palette map[0] (av1_read_uniform) range ladder ------------ | ||
| 320 | +python3 - "$I" <<'PYP0' | ||
| 321 | +import sys | ||
| 322 | +path = sys.argv[1] | ||
| 323 | +s = open(path).read() | ||
| 324 | +if "EC_INSTRUMENTED_PALETTE_UNIFORM" in s: | ||
| 325 | + print("palette map[0] already instrumented (no-op)") | ||
| 326 | + sys.exit(0) | ||
| 327 | + | ||
| 328 | +old = """ // The first color index. | ||
| 329 | + color_map[0] = av1_read_uniform(r, n); | ||
| 330 | + assert(color_map[0] < n);""" | ||
| 331 | +new = """ // The first color index. | ||
| 332 | + /* EC_INSTRUMENTED_PALETTE_UNIFORM */ | ||
| 333 | + if (getenv("EC_TRACE_PALETTE") != NULL) { | ||
| 334 | + fprintf(stderr, "EC_PAL row=0 col=0 ctx=-1 n=%d rng=%u\\n", n, | ||
| 335 | + (unsigned)r->ec.rng); | ||
| 336 | + } | ||
| 337 | + color_map[0] = av1_read_uniform(r, n); | ||
| 338 | + assert(color_map[0] < n); | ||
| 339 | + if (getenv("EC_TRACE_PALETTE") != NULL) { | ||
| 340 | + fprintf(stderr, "EC_PAL_VAL row=0 col=0 color_idx=%d rng=%u\\n", | ||
| 341 | + color_map[0], (unsigned)r->ec.rng); | ||
| 342 | + }""" | ||
| 343 | +assert old in s, "color_map[0] uniform read moved" | ||
| 344 | +s = s.replace(old, new, 1) | ||
| 345 | +open(path, "w").write(s) | ||
| 346 | +print("palette map[0] instrumented") | ||
| 347 | +PYP0 | ||