From cd170cb0dca887410c7bebacde2a168116b2068c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 8 Sep 2026 05:02:27 +0300 Subject: [PATCH] fix(av1 decoder): dequantize a rectangular intra transform unit with the block's segment quantizer, not the frame's The rect intra paths passed FrameCtx::current_q_idx where every square path passes block_q_idx, so a block in a segment carrying SEG_LVL_ALT_Q was dequantized with the frame's own index. Only a frame edge forces rectangular partitions in the reference encoders' streams, so this showed up as a bottom-straddle-only mismatch: the whole 1920x792 reference ladder (libaom and rav1e, 8 points) now decodes sample-exact against ffmpeg, as does a 384x152 straddle repro. --- crates/ec-av1/src/decode.rs | 30 ++++++++++++++++++++------- crates/ec-av1/src/transform.rs | 16 ++++++++++++++ lanes/d792.report.md | 38 ++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 7 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 713c137a..92232ef9 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -927,10 +927,17 @@ fn seg_feature(segment_id: usize, feature: usize, fctx: &crate::decode::FrameCtx fn block_q_idx(fctx: &crate::decode::FrameCtx) -> i32 { let base = fctx.current_q_idx.with(|c| c.get()); let seg_id = fctx.cur_segment_id.with(|c| c.get()) as usize; - match seg_feature(seg_id, SEG_LVL_ALT_Q, fctx) { + let q = match seg_feature(seg_id, SEG_LVL_ALT_Q, fctx) { Some(data) => (base + data).clamp(0, 255), None => base, + }; + // lane-d792: which SEGMENT a block's quantizer came out of, beside the + // `EC_DQCOEFF` grid rung -- a dequant that is too low with the levels + // already proven equal is a segment-id defect, not a quantizer one. + if crate::envflags::env_flag!("EC_DQCOEFF") { + eprintln!("OUR_Q base={base} seg={seg_id} q={q}"); } + q } /// The segment id recorded for one mi cell of the current frame's map. @@ -9715,7 +9722,12 @@ fn decode_rect_split( chroma_w, chroma_h, crate::decode::bit_depth(fctx), - fctx.current_q_idx.with(|c| c.get()), + // lane-d792: the BLOCK's quantizer index, i.e. the frame's + // own index PLUS this block's segment `SEG_LVL_ALT_Q` -- + // reading `current_q_idx` here dropped the segment feature + // that every other dequant site applies (class + // `parsed-then-discarded`). + block_q_idx(fctx), plane_q_delta(plane_idx, fctx).0, plane_q_delta(plane_idx, fctx).1, tx_type, @@ -11222,7 +11234,7 @@ fn decode_leaf_rect( luma_levels = l_levels; let luma_residual = dequant_and_inverse_typed_wh( &luma_levels, bw, bh, crate::decode::bit_depth(fctx), - fctx.current_q_idx.with(|c| c.get()), plane_q_delta(0, fctx).0, plane_q_delta(0, fctx).1, luma_tx_type, + block_q_idx(fctx), plane_q_delta(0, fctx).0, plane_q_delta(0, fctx).1, luma_tx_type, ); if let Some(b) = &palette_y_buf { set_palette_pred(b.clone(), fctx); @@ -11242,7 +11254,7 @@ fn decode_leaf_rect( u_levels = u_l; let u_residual = dequant_and_inverse_typed_wh( &u_levels, chroma_w, chroma_h, crate::decode::bit_depth(fctx), - fctx.current_q_idx.with(|c| c.get()), plane_q_delta(1, fctx).0, plane_q_delta(1, fctx).1, u_tx_type, + block_q_idx(fctx), plane_q_delta(1, fctx).0, plane_q_delta(1, fctx).1, u_tx_type, ); if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); @@ -11261,7 +11273,7 @@ fn decode_leaf_rect( v_levels = v_l; let v_residual = dequant_and_inverse_typed_wh( &v_levels, chroma_w, chroma_h, crate::decode::bit_depth(fctx), - fctx.current_q_idx.with(|c| c.get()), plane_q_delta(2, fctx).0, plane_q_delta(2, fctx).1, v_tx_type, + block_q_idx(fctx), plane_q_delta(2, fctx).0, plane_q_delta(2, fctx).1, v_tx_type, ); if let Some((_, vb)) = &palette_uv_bufs { set_palette_pred(vb.clone(), fctx); @@ -16225,7 +16237,9 @@ fn decode_leaf_rect8( bw, bh, crate::decode::bit_depth(fctx), - fctx.current_q_idx.with(|q| q.get()), + // lane-d792 sweep: the block's segment `SEG_LVL_ALT_Q`, as at + // every other dequant site (class `parsed-then-discarded`). + block_q_idx(fctx), plane_q_delta(0, fctx).0, plane_q_delta(0, fctx).1, tx_type, @@ -31131,7 +31145,9 @@ fn decode_intra_sub8_leaf( bw, bh, crate::decode::bit_depth(fctx), - fctx.current_q_idx.with(|q| q.get()), + // lane-d792 sweep: the block's segment `SEG_LVL_ALT_Q`, as at + // every other dequant site (class `parsed-then-discarded`). + block_q_idx(fctx), plane_q_delta(0, fctx).0, plane_q_delta(0, fctx).1, tx_type, diff --git a/crates/ec-av1/src/transform.rs b/crates/ec-av1/src/transform.rs index 5b985e3b..34dbe3f5 100644 --- a/crates/ec-av1/src/transform.rs +++ b/crates/ec-av1/src/transform.rs @@ -1346,6 +1346,22 @@ pub fn dequant_and_inverse_typed_wh( DQ_SCRATCH.with(|cell| { let mut dq = cell.borrow_mut(); crate::quant::dequant_wh_into(levels, &mut dq, w, h, bit_depth, q_idx, dc_delta, ac_delta); + // lane-d792: the DEQUANTIZED grid, in row-major `(row/col)` pairs -- + // the twin of the oracle's `EC_DQCOEFF` rung (whose own layout is + // column-major `input[c * h + r]`), so a residual mismatch whose + // levels already agree is separable into dequant vs transform. + if crate::envflags::env_flag!("EC_DQCOEFF") { + let nz: Vec = dq + .iter() + .enumerate() + .filter(|&(_, &v)| v != 0) + .map(|(i, &v)| format!("{}/{}:{v}", i / w, i % w)) + .collect(); + eprintln!( + "OUR_DQ w={w} h={h} q={q_idx} dcd={dc_delta} acd={ac_delta} tx={tx_type:?} nz={}", + nz.join(",") + ); + } inverse_transform_2d_typed_wh(&dq, w, h, bit_depth, tx_type) }) } diff --git a/lanes/d792.report.md b/lanes/d792.report.md index b50fc460..5cf826ef 100644 --- a/lanes/d792.report.md +++ b/lanes/d792.report.md @@ -127,3 +127,41 @@ q index / DC-AC delta that site passes. * `deferred: the synthetic straddling fixtures (384x152 / 384x184 gates), the 1920x792 green arm, the suite and the workspace check -- they gate a fix that does not exist yet` + +## ROOT CAUSE (round 2) — the rect intra dequant dropped the block's segment quantizer + +`OUR_DQ` (our new dequantized-grid rung) against the oracle's `EC_DQCOEFF` +for the same 16x8 unit: the level grids agree, but ours dequantizes with +`q = 75` where libaom uses the quantizer of q-index ~96 (dc 111 / ac 104 vs +our 82 / 82) -- the 0.77 residual factor exactly. + +`OUR_Q` (the segment rung) named it: frame 0 of the small repro has THREE +segments (`base = 75`; seg 0 -> 67, seg 1 -> 81, seg 2 -> 96 through +`SEG_LVL_ALT_Q`), and the rect intra path was dequantizing with +`fctx.current_q_idx` -- the FRAME's index -- instead of +[`block_q_idx`], which adds the block's own segment feature. Every square +path already used `block_q_idx`, which is why the defect was invisible until +a frame edge forced rectangular partitions: only the cut superblock row codes +16x8 / 8x4 units in these streams. + +**Class**: `parsed-then-discarded` -- the segmentation ALT_Q is parsed, +applied at 21 dequant sites, and dropped at 6 of them. (Sibling shape to +`override slot on one arm`: the rect ARM of a two-arm path missed the +adjustment its square twin had.) + +**Fix**: `block_q_idx(fctx)` at all six, in `crates/ec-av1/src/decode.rs` +(the split rect path's chroma, the 16x8/8x16 luma+chroma trio, and two +8x4/rect leaf sites at 16240 / 31147). + +**Sweep**: every `dequant_and_inverse*` call site in the decoder (27) now +takes either `block_q_idx` or a `TxParams::q_idx` that was itself built from +`block_q_idx`; `grep -n "current_q_idx" decode.rs` leaves only the frame-level +cell itself, `block_q_idx`'s own read of it and a `RECT64_QIDX_DRIFT_HITS` +counter. + +## Result + +* small repro `384x152` rav1e q100 and q150: **EXACT vs ffmpeg**, all frames. +* the whole 1920x792 reference ladder -- libaom crf 5/20/35/45 AND rav1e + quantizer 50/100/150/200, 12 frames each: **EXACT vs ffmpeg on every + point** (`$HOME/.cache/d792/streams`, decoded with `examples/dump_yuv`).