forge
Explore
Log in

Don't you dare go Hollow

Dequantize a rectangular intra transform unit with the block's segment quantizer, not the frame's

Browse files at this commit

cd170cb0dca887410c7bebacde2a168116b2068cAhmet Kaan GümüşParent: 4caf191
  • fix
    • dequantize a rectangular intra transform unit with the block's segment quantizer, not the frame's
Show full message
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.rsmodified+23 -7
@@ -927,10 +927,17 @@ fn seg_feature(segment_id: usize, feature: usize, fctx: &crate::decode::FrameCtx
927927 fn block_q_idx(fctx: &crate::decode::FrameCtx) -> i32 {
928928 let base = fctx.current_q_idx.with(|c| c.get());
929929 let seg_id = fctx.cur_segment_id.with(|c| c.get()) as usize;
930- match seg_feature(seg_id, SEG_LVL_ALT_Q, fctx) {
930+ let q = match seg_feature(seg_id, SEG_LVL_ALT_Q, fctx) {
931931 Some(data) => (base + data).clamp(0, 255),
932932 None => base,
933+ };
934+ // lane-d792: which SEGMENT a block's quantizer came out of, beside the
935+ // `EC_DQCOEFF` grid rung -- a dequant that is too low with the levels
936+ // already proven equal is a segment-id defect, not a quantizer one.
937+ if crate::envflags::env_flag!("EC_DQCOEFF") {
938+ eprintln!("OUR_Q base={base} seg={seg_id} q={q}");
933939 }
940+ q
934941 }
935942
936943 /// The segment id recorded for one mi cell of the current frame's map.
@@ -9715,7 +9722,12 @@ fn decode_rect_split(
97159722 chroma_w,
97169723 chroma_h,
97179724 crate::decode::bit_depth(fctx),
9718- fctx.current_q_idx.with(|c| c.get()),
9725+ // lane-d792: the BLOCK's quantizer index, i.e. the frame's
9726+ // own index PLUS this block's segment `SEG_LVL_ALT_Q` --
9727+ // reading `current_q_idx` here dropped the segment feature
9728+ // that every other dequant site applies (class
9729+ // `parsed-then-discarded`).
9730+ block_q_idx(fctx),
97199731 plane_q_delta(plane_idx, fctx).0,
97209732 plane_q_delta(plane_idx, fctx).1,
97219733 tx_type,
@@ -11222,7 +11234,7 @@ fn decode_leaf_rect(
1122211234 luma_levels = l_levels;
1122311235 let luma_residual = dequant_and_inverse_typed_wh(
1122411236 &luma_levels, bw, bh, crate::decode::bit_depth(fctx),
11225- fctx.current_q_idx.with(|c| c.get()), plane_q_delta(0, fctx).0, plane_q_delta(0, fctx).1, luma_tx_type,
11237+ block_q_idx(fctx), plane_q_delta(0, fctx).0, plane_q_delta(0, fctx).1, luma_tx_type,
1122611238 );
1122711239 if let Some(b) = &palette_y_buf {
1122811240 set_palette_pred(b.clone(), fctx);
@@ -11242,7 +11254,7 @@ fn decode_leaf_rect(
1124211254 u_levels = u_l;
1124311255 let u_residual = dequant_and_inverse_typed_wh(
1124411256 &u_levels, chroma_w, chroma_h, crate::decode::bit_depth(fctx),
11245- fctx.current_q_idx.with(|c| c.get()), plane_q_delta(1, fctx).0, plane_q_delta(1, fctx).1, u_tx_type,
11257+ block_q_idx(fctx), plane_q_delta(1, fctx).0, plane_q_delta(1, fctx).1, u_tx_type,
1124611258 );
1124711259 if let Some((ub, _)) = &palette_uv_bufs {
1124811260 set_palette_pred(ub.clone(), fctx);
@@ -11261,7 +11273,7 @@ fn decode_leaf_rect(
1126111273 v_levels = v_l;
1126211274 let v_residual = dequant_and_inverse_typed_wh(
1126311275 &v_levels, chroma_w, chroma_h, crate::decode::bit_depth(fctx),
11264- fctx.current_q_idx.with(|c| c.get()), plane_q_delta(2, fctx).0, plane_q_delta(2, fctx).1, v_tx_type,
11276+ block_q_idx(fctx), plane_q_delta(2, fctx).0, plane_q_delta(2, fctx).1, v_tx_type,
1126511277 );
1126611278 if let Some((_, vb)) = &palette_uv_bufs {
1126711279 set_palette_pred(vb.clone(), fctx);
@@ -16225,7 +16237,9 @@ fn decode_leaf_rect8(
1622516237 bw,
1622616238 bh,
1622716239 crate::decode::bit_depth(fctx),
16228- fctx.current_q_idx.with(|q| q.get()),
16240+ // lane-d792 sweep: the block's segment `SEG_LVL_ALT_Q`, as at
16241+ // every other dequant site (class `parsed-then-discarded`).
16242+ block_q_idx(fctx),
1622916243 plane_q_delta(0, fctx).0,
1623016244 plane_q_delta(0, fctx).1,
1623116245 tx_type,
@@ -31131,7 +31145,9 @@ fn decode_intra_sub8_leaf(
3113131145 bw,
3113231146 bh,
3113331147 crate::decode::bit_depth(fctx),
31134- fctx.current_q_idx.with(|q| q.get()),
31148+ // lane-d792 sweep: the block's segment `SEG_LVL_ALT_Q`, as at
31149+ // every other dequant site (class `parsed-then-discarded`).
31150+ block_q_idx(fctx),
3113531151 plane_q_delta(0, fctx).0,
3113631152 plane_q_delta(0, fctx).1,
3113731153 tx_type,
crates/ec-av1/src/transform.rsmodified+16 -0
@@ -1346,6 +1346,22 @@ pub fn dequant_and_inverse_typed_wh(
13461346 DQ_SCRATCH.with(|cell| {
13471347 let mut dq = cell.borrow_mut();
13481348 crate::quant::dequant_wh_into(levels, &mut dq, w, h, bit_depth, q_idx, dc_delta, ac_delta);
1349+ // lane-d792: the DEQUANTIZED grid, in row-major `(row/col)` pairs --
1350+ // the twin of the oracle's `EC_DQCOEFF` rung (whose own layout is
1351+ // column-major `input[c * h + r]`), so a residual mismatch whose
1352+ // levels already agree is separable into dequant vs transform.
1353+ if crate::envflags::env_flag!("EC_DQCOEFF") {
1354+ let nz: Vec<String> = dq
1355+ .iter()
1356+ .enumerate()
1357+ .filter(|&(_, &v)| v != 0)
1358+ .map(|(i, &v)| format!("{}/{}:{v}", i / w, i % w))
1359+ .collect();
1360+ eprintln!(
1361+ "OUR_DQ w={w} h={h} q={q_idx} dcd={dc_delta} acd={ac_delta} tx={tx_type:?} nz={}",
1362+ nz.join(",")
1363+ );
1364+ }
13491365 inverse_transform_2d_typed_wh(&dq, w, h, bit_depth, tx_type)
13501366 })
13511367 }
lanes/d792.report.mdmodified+38 -0
@@ -127,3 +127,41 @@ q index / DC-AC delta that site passes.
127127 * `deferred: the synthetic straddling fixtures (384x152 / 384x184 gates), the
128128 1920x792 green arm, the suite and the workspace check -- they gate a fix
129129 that does not exist yet`
130+
131+## ROOT CAUSE (round 2) — the rect intra dequant dropped the block's segment quantizer
132+
133+`OUR_DQ` (our new dequantized-grid rung) against the oracle's `EC_DQCOEFF`
134+for the same 16x8 unit: the level grids agree, but ours dequantizes with
135+`q = 75` where libaom uses the quantizer of q-index ~96 (dc 111 / ac 104 vs
136+our 82 / 82) -- the 0.77 residual factor exactly.
137+
138+`OUR_Q` (the segment rung) named it: frame 0 of the small repro has THREE
139+segments (`base = 75`; seg 0 -> 67, seg 1 -> 81, seg 2 -> 96 through
140+`SEG_LVL_ALT_Q`), and the rect intra path was dequantizing with
141+`fctx.current_q_idx` -- the FRAME's index -- instead of
142+[`block_q_idx`], which adds the block's own segment feature. Every square
143+path already used `block_q_idx`, which is why the defect was invisible until
144+a frame edge forced rectangular partitions: only the cut superblock row codes
145+16x8 / 8x4 units in these streams.
146+
147+**Class**: `parsed-then-discarded` -- the segmentation ALT_Q is parsed,
148+applied at 21 dequant sites, and dropped at 6 of them. (Sibling shape to
149+`override slot on one arm`: the rect ARM of a two-arm path missed the
150+adjustment its square twin had.)
151+
152+**Fix**: `block_q_idx(fctx)` at all six, in `crates/ec-av1/src/decode.rs`
153+(the split rect path's chroma, the 16x8/8x16 luma+chroma trio, and two
154+8x4/rect leaf sites at 16240 / 31147).
155+
156+**Sweep**: every `dequant_and_inverse*` call site in the decoder (27) now
157+takes either `block_q_idx` or a `TxParams::q_idx` that was itself built from
158+`block_q_idx`; `grep -n "current_q_idx" decode.rs` leaves only the frame-level
159+cell itself, `block_q_idx`'s own read of it and a `RECT64_QIDX_DRIFT_HITS`
160+counter.
161+
162+## Result
163+
164+* small repro `384x152` rav1e q100 and q150: **EXACT vs ffmpeg**, all frames.
165+* the whole 1920x792 reference ladder -- libaom crf 5/20/35/45 AND rav1e
166+ quantizer 50/100/150/200, 12 frames each: **EXACT vs ffmpeg on every
167+ point** (`$HOME/.cache/d792/streams`, decoded with `examples/dump_yuv`).