Dequantize a rectangular intra transform unit with the block's segment quantizer, not the frame's
Browse files at this commitcd170cb0dca887410c7bebacde2a168116b2068cParent: 4caf191- fix
- dequantize a rectangular intra transform unit with the block's segment quantizer, not the frame's
3 changed files +77 -7
- crates/ec-av1/src/decode.rs +23 -7
- crates/ec-av1/src/transform.rs +16 -0
- lanes/d792.report.md +38 -0
crates/ec-av1/src/decode.rsmodified+23 -7
| @@ -927,10 +927,17 @@ fn seg_feature(segment_id: usize, feature: usize, fctx: &crate::decode::FrameCtx | |||
| 927 | fn block_q_idx(fctx: &crate::decode::FrameCtx) -> i32 { | 927 | fn block_q_idx(fctx: &crate::decode::FrameCtx) -> i32 { |
| 928 | let base = fctx.current_q_idx.with(|c| c.get()); | 928 | let base = fctx.current_q_idx.with(|c| c.get()); |
| 929 | let seg_id = fctx.cur_segment_id.with(|c| c.get()) as usize; | 929 | 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) { |
| 931 | Some(data) => (base + data).clamp(0, 255), | 931 | Some(data) => (base + data).clamp(0, 255), |
| 932 | None => base, | 932 | 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}"); | ||
| 933 | } | 939 | } |
| 940 | + q | ||
| 934 | } | 941 | } |
| 935 | 942 | ||
| 936 | /// The segment id recorded for one mi cell of the current frame's map. | 943 | /// The segment id recorded for one mi cell of the current frame's map. |
| @@ -9715,7 +9722,12 @@ fn decode_rect_split( | |||
| 9715 | chroma_w, | 9722 | chroma_w, |
| 9716 | chroma_h, | 9723 | chroma_h, |
| 9717 | crate::decode::bit_depth(fctx), | 9724 | 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), | ||
| 9719 | plane_q_delta(plane_idx, fctx).0, | 9731 | plane_q_delta(plane_idx, fctx).0, |
| 9720 | plane_q_delta(plane_idx, fctx).1, | 9732 | plane_q_delta(plane_idx, fctx).1, |
| 9721 | tx_type, | 9733 | tx_type, |
| @@ -11222,7 +11234,7 @@ fn decode_leaf_rect( | |||
| 11222 | luma_levels = l_levels; | 11234 | luma_levels = l_levels; |
| 11223 | let luma_residual = dequant_and_inverse_typed_wh( | 11235 | let luma_residual = dequant_and_inverse_typed_wh( |
| 11224 | &luma_levels, bw, bh, crate::decode::bit_depth(fctx), | 11236 | &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, |
| 11226 | ); | 11238 | ); |
| 11227 | if let Some(b) = &palette_y_buf { | 11239 | if let Some(b) = &palette_y_buf { |
| 11228 | set_palette_pred(b.clone(), fctx); | 11240 | set_palette_pred(b.clone(), fctx); |
| @@ -11242,7 +11254,7 @@ fn decode_leaf_rect( | |||
| 11242 | u_levels = u_l; | 11254 | u_levels = u_l; |
| 11243 | let u_residual = dequant_and_inverse_typed_wh( | 11255 | let u_residual = dequant_and_inverse_typed_wh( |
| 11244 | &u_levels, chroma_w, chroma_h, crate::decode::bit_depth(fctx), | 11256 | &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, |
| 11246 | ); | 11258 | ); |
| 11247 | if let Some((ub, _)) = &palette_uv_bufs { | 11259 | if let Some((ub, _)) = &palette_uv_bufs { |
| 11248 | set_palette_pred(ub.clone(), fctx); | 11260 | set_palette_pred(ub.clone(), fctx); |
| @@ -11261,7 +11273,7 @@ fn decode_leaf_rect( | |||
| 11261 | v_levels = v_l; | 11273 | v_levels = v_l; |
| 11262 | let v_residual = dequant_and_inverse_typed_wh( | 11274 | let v_residual = dequant_and_inverse_typed_wh( |
| 11263 | &v_levels, chroma_w, chroma_h, crate::decode::bit_depth(fctx), | 11275 | &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, |
| 11265 | ); | 11277 | ); |
| 11266 | if let Some((_, vb)) = &palette_uv_bufs { | 11278 | if let Some((_, vb)) = &palette_uv_bufs { |
| 11267 | set_palette_pred(vb.clone(), fctx); | 11279 | set_palette_pred(vb.clone(), fctx); |
| @@ -16225,7 +16237,9 @@ fn decode_leaf_rect8( | |||
| 16225 | bw, | 16237 | bw, |
| 16226 | bh, | 16238 | bh, |
| 16227 | crate::decode::bit_depth(fctx), | 16239 | 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), | ||
| 16229 | plane_q_delta(0, fctx).0, | 16243 | plane_q_delta(0, fctx).0, |
| 16230 | plane_q_delta(0, fctx).1, | 16244 | plane_q_delta(0, fctx).1, |
| 16231 | tx_type, | 16245 | tx_type, |
| @@ -31131,7 +31145,9 @@ fn decode_intra_sub8_leaf( | |||
| 31131 | bw, | 31145 | bw, |
| 31132 | bh, | 31146 | bh, |
| 31133 | crate::decode::bit_depth(fctx), | 31147 | 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), | ||
| 31135 | plane_q_delta(0, fctx).0, | 31151 | plane_q_delta(0, fctx).0, |
| 31136 | plane_q_delta(0, fctx).1, | 31152 | plane_q_delta(0, fctx).1, |
| 31137 | tx_type, | 31153 | tx_type, |
crates/ec-av1/src/transform.rsmodified+16 -0
| @@ -1346,6 +1346,22 @@ pub fn dequant_and_inverse_typed_wh( | |||
| 1346 | DQ_SCRATCH.with(|cell| { | 1346 | DQ_SCRATCH.with(|cell| { |
| 1347 | let mut dq = cell.borrow_mut(); | 1347 | let mut dq = cell.borrow_mut(); |
| 1348 | crate::quant::dequant_wh_into(levels, &mut dq, w, h, bit_depth, q_idx, dc_delta, ac_delta); | 1348 | 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 | + } | ||
| 1349 | inverse_transform_2d_typed_wh(&dq, w, h, bit_depth, tx_type) | 1365 | inverse_transform_2d_typed_wh(&dq, w, h, bit_depth, tx_type) |
| 1350 | }) | 1366 | }) |
| 1351 | } | 1367 | } |
lanes/d792.report.mdmodified+38 -0
| @@ -127,3 +127,41 @@ q index / DC-AC delta that site passes. | |||
| 127 | * `deferred: the synthetic straddling fixtures (384x152 / 384x184 gates), the | 127 | * `deferred: the synthetic straddling fixtures (384x152 / 384x184 gates), the |
| 128 | 1920x792 green arm, the suite and the workspace check -- they gate a fix | 128 | 1920x792 green arm, the suite and the workspace check -- they gate a fix |
| 129 | that does not exist yet` | 129 | 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`). | ||