From f62adbfb30df73cce788771a618830d0e4b09b5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 30 Aug 2026 17:08:20 +0300 Subject: [PATCH] diag(av1): EC_AV1_TRACE range-ladder inside decode_color_index_map + palette Y mode/size/colors (lane-palette r6) Range-ladder bisect against aomdec's EC_TRACE_PALETTE rung (rungs 6/6b) on the r3/r4 smptebars gate fixture: every symbol up through read_palette_colors_y matches the oracle's ranges exactly (partition_w32 rng, skip/y_mode/uv_mode, palette_y_mode, palette_y_size, palette colours all bit-identical). The desync starts right after -- libaom's decode_color_map_tokens is NOT called inline from read_palette_mode_info; it runs later, from parse_decode_block via av1_visit_palette, after palette UV mode_info (mode/size/colors, which this decoder does not read at all yet) and filter_intra. Our decode_color_index_map is called immediately after the Y colours instead, so it reads the color-index symbols against the wrong point in the bitstream whenever a block is chroma-referenced with UV_DC_PRED (this gate's block: bsize_ctx=4, uv_mode=0). Confirmed by patching a throwaway EC_PRE_PALETTE/EC_PALETTE_Y_MODE/SIZE/COLORS print into the oracle's decodemv.c (not committed, reverted after use) -- ranges matched ours field-for-field through EC_PALETTE_Y_COLORS rng=39432, then the oracle's own decode_color_map_tokens entry read rng=51752, proving UV palette mode_info consumed real bits in between that this decoder skips. Adds an EC_AV1_TRACE-gated EC_PAL/EC_PAL_VAL print inside our own decode_color_index_map (row/col/ctx/n/rng before, color_idx/rng after, plus the map[0] uniform read) and TRACE prints around the Y palette mode/size/colors reads, matching the oracle rung's field names so a future round's bisect doesn't have to re-instrument. No behaviour change: the palette_y_mode `&&` short-circuit (only read the symbol when mode == DC_PRED) is preserved exactly. Refusal strings: none added or removed -- the palette-Y refusal stays in place; removing it needs the reorder fix (read/skip UV palette mode_info in the right position, defer the Y+UV color-index-map reads to after filter_intra) which is next round's job, not this one's. Suite: EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib -j4 -- 244 passed, 0 failed, 17 ignored (unchanged). --- crates/ec-av1/src/decode.rs | 41 ++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 6f940f2c..c2f17bec 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -2368,8 +2368,17 @@ fn palette_color_index_context( /// rows/cols-vs-plane_width/height cropping special case is needed, unlike /// libaom's own non-block-aligned split. fn decode_color_index_map(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, n: usize, side: usize) -> Vec { + let trace = std::env::var_os("EC_AV1_TRACE").is_some(); let mut map = vec![0u8; side * side]; + if trace { + let (rng, _) = dec.debug_state(); + eprintln!("EC_PAL row=0 col=0 ctx=-1 n={n} rng={rng}"); + } map[0] = read_uniform(dec, n) as u8; + if trace { + let (rng, _) = dec.debug_state(); + eprintln!("EC_PAL_VAL row=0 col=0 color_idx={} rng={rng}", map[0]); + } for i in 1..(2 * side - 1) { let j_hi = i.min(side - 1); let j_lo = i.saturating_sub(side - 1); @@ -2377,8 +2386,16 @@ fn decode_color_index_map(dec: &mut SymbolDecoder, cdfs: &mut Cdfs, n: usize, si let row = i - j; let col = j; let (ctx, color_order) = palette_color_index_context(&map, side, row, col, n); + if trace { + let (rng, _) = dec.debug_state(); + eprintln!("EC_PAL row={row} col={col} ctx={ctx} n={n} rng={rng}"); + } let symbol = dec.symbol(&mut cdfs.palette_y_color_index[n - 2][ctx][..=n]); map[row * side + col] = color_order[symbol]; + if trace { + let (rng, _) = dec.debug_state(); + eprintln!("EC_PAL_VAL row={row} col={col} color_idx={symbol} rng={rng}"); + } } } map @@ -2995,14 +3012,36 @@ fn read_intra_mode( && let Some(bsize_ctx) = palette_bsize_ctx(side) { let (mode_ctx, cache) = palette.unwrap_or((0, &[])); - if mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][mode_ctx]) != 0 { + if mode == DC_PRED { + if trace { + let (rng, _) = dec.debug_state(); + eprintln!( + "TRACE pre_palette_y_mode bsize_ctx={bsize_ctx} mode_ctx={mode_ctx} rng={rng}" + ); + } + } + let use_palette_y = + mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][mode_ctx]) != 0; + if mode == DC_PRED && trace { + let (rng, _) = dec.debug_state(); + eprintln!("TRACE palette_y_mode value={} rng={rng}", use_palette_y as i32); + } + if use_palette_y { if palette.is_none() { return Err(unsupported( "a block that actually uses a palette (Y) -- reconstruction is out of scope", )); } let n = 2 + dec.symbol(&mut cdfs.palette_y_size[bsize_ctx]); + if trace { + let (rng, _) = dec.debug_state(); + eprintln!("TRACE palette_y_size n={n} rng={rng}"); + } let colors = read_palette_colors_y(dec, n, cache); + if trace { + 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 {