ec-av1: the lossless OUR_DQW twin, plane-tagged, with the raw levels beside dq
Browse files at this commitbb7bd46bfd8a9effa323aeb7ab9f7a43a42b26d6Parent: 37631b32 changed files +63 -8
crates/ec-av1/src/decode.rsmodified+13 -0
| @@ -3028,6 +3028,14 @@ fn cfl_src_rect(px: usize, py: usize, bw: usize, bh: usize) -> CflSrc { | ||
| 3028 | 3028 | /// `reconstruct_mc_rect` reads both of its buffers at. |
| 3029 | 3029 | #[derive(Clone, Copy)] |
| 3030 | 3030 | pub(crate) struct TxParams { |
| 3031 | + /// The plane this unit belongs to (0 luma, 1 U, 2 V), carried from the | |
| 3032 | + /// reader that OWNS the unit. `dequant_and_inverse_wht4x4`'s `EC_DQCOEFF` | |
| 3033 | + /// twin prints it so its ladder pairs with the oracle's: the per-unit | |
| 3034 | + /// census plane could not, because the callers that route a lossless 4x4 | |
| 3035 | + /// unit into the WHT are not all the ones that stamp the census plane | |
| 3036 | + /// (measured on `ll444-lossless-key.obu`: 1663 of 1663 chroma units paired | |
| 3037 | + /// by count, 1660 of them against an empty grid). | |
| 3038 | + pub(crate) plane: usize, | |
| 3031 | 3039 | pub(crate) w: usize, |
| 3032 | 3040 | pub(crate) h: usize, |
| 3033 | 3041 | pub(crate) bit_depth: u8, |
| @@ -3118,6 +3126,7 @@ impl TxParams { | ||
| 3118 | 3126 | } |
| 3119 | 3127 | crate::transform::dequant_and_inverse_wht( |
| 3120 | 3128 | grid, |
| 3129 | + self.plane, | |
| 3121 | 3130 | self.w, |
| 3122 | 3131 | self.h, |
| 3123 | 3132 | self.bit_depth, |
| @@ -21556,6 +21565,7 @@ fn read_plane( | ||
| 21556 | 21565 | let levels = extend_corner(grid, tx_side, tx_side, side, side); |
| 21557 | 21566 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx); |
| 21558 | 21567 | let tx = TxParams { |
| 21568 | + plane: plane_idx, | |
| 21559 | 21569 | w: side, |
| 21560 | 21570 | h: side, |
| 21561 | 21571 | bit_depth: bit_depth(fctx), |
| @@ -27206,6 +27216,7 @@ fn decode_leaf_rect8( | ||
| 27206 | 27216 | // fixture: the DCT route left ±1..30 sample errors on every |
| 27207 | 27217 | // walked leaf's luma. |
| 27208 | 27218 | let tx = TxParams { |
| 27219 | + plane: 0, | |
| 27209 | 27220 | w: tw, |
| 27210 | 27221 | h: th, |
| 27211 | 27222 | bit_depth: crate::decode::bit_depth(fctx), |
| @@ -30608,6 +30619,7 @@ fn read_inter_plane_rect( | ||
| 30608 | 30619 | // stride, so the dense `w x h` residual is re-laid at that stride |
| 30609 | 30620 | // (`TxParams::stride`) after the inverse transform. |
| 30610 | 30621 | let tx = TxParams { |
| 30622 | + plane: plane_idx, | |
| 30611 | 30623 | w, |
| 30612 | 30624 | h, |
| 30613 | 30625 | bit_depth: bit_depth(fctx), |
| @@ -37712,6 +37724,7 @@ fn read_inter_plane( | ||
| 37712 | 37724 | let grid = extend_corner(grid, tx_side, tx_side, side, side); |
| 37713 | 37725 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx); |
| 37714 | 37726 | let tx = TxParams { |
| 37727 | + plane: plane_idx, | |
| 37715 | 37728 | w: side, |
| 37716 | 37729 | h: side, |
| 37717 | 37730 | bit_depth: bit_depth(fctx), |
crates/ec-av1/src/transform.rsmodified+50 -8
| @@ -1381,6 +1381,15 @@ pub fn dequant_and_inverse_typed_wh( | ||
| 1381 | 1381 | // the twin of the oracle's `EC_DQCOEFF` rung (whose own layout is |
| 1382 | 1382 | // column-major `input[c * h + r]`), so a residual mismatch whose |
| 1383 | 1383 | // levels already agree is separable into dequant vs transform. |
| 1384 | + // | |
| 1385 | + // LOSSY PATH ONLY (lane-av1loss444mm): a LOSSLESS 4x4 unit never | |
| 1386 | + // reaches this function -- `decode.rs` routes it to | |
| 1387 | + // `dequant_and_inverse_wht4x4` -- so on a lossless stream this rung | |
| 1388 | + // printed NOTHING while the oracle's printed thousands of lines, and | |
| 1389 | + // that silence reads as "no coefficients" rather than "wrong function". | |
| 1390 | + // The lossless twin is `OUR_DQW` in `dequant_and_inverse_wht4x4`, | |
| 1391 | + // below; both carry `plane=` so a chroma grid can be told from a luma | |
| 1392 | + // one (the oracle's line carries it too). | |
| 1384 | 1393 | if crate::envflags::env_flag!("EC_DQCOEFF") { |
| 1385 | 1394 | let nz: Vec<String> = dq |
| 1386 | 1395 | .iter() |
| @@ -3144,6 +3153,7 @@ pub fn inverse_wht4x4(dq: &[i32]) -> Vec<i32> { | ||
| 3144 | 3153 | /// marker contract). |
| 3145 | 3154 | pub fn dequant_and_inverse_wht4x4( |
| 3146 | 3155 | levels: &[i32], |
| 3156 | + plane: usize, | |
| 3147 | 3157 | bit_depth: u8, |
| 3148 | 3158 | q_idx: i32, |
| 3149 | 3159 | dc_delta: i32, |
| @@ -3160,6 +3170,35 @@ pub fn dequant_and_inverse_wht4x4( | ||
| 3160 | 3170 | crate::quant::dequant_wh_into( |
| 3161 | 3171 | levels, &mut dq, 4, 4, bit_depth, q_idx, dc_delta, ac_delta, None, |
| 3162 | 3172 | ); |
| 3173 | + // lane-av1loss444mm: the LOSSLESS half of the oracle's `EC_DQCOEFF` | |
| 3174 | + // twin. Same env var, same `pos:value` nonzero form, PLUS `plane=` | |
| 3175 | + // -- which is what makes the two ladders pairable: without it a 4x4 | |
| 3176 | + // chroma grid is indistinguishable from a luma one and an | |
| 3177 | + // order-paired comparison of the two sides collapses (measured on | |
| 3178 | + // `ll444-lossless-key.obu`: equal unit counts and value ratios of | |
| 3179 | + // exactly 1.0, yet 16 of 2053 units matched). | |
| 3180 | + if crate::envflags::env_flag!("EC_DQCOEFF") { | |
| 3181 | + let nz: Vec<String> = dq[..16] | |
| 3182 | + .iter() | |
| 3183 | + .enumerate() | |
| 3184 | + .filter(|&(_, &v)| v != 0) | |
| 3185 | + .map(|(i, &v)| format!("{i}:{v}")) | |
| 3186 | + .collect(); | |
| 3187 | + // `lv=` is the RAW level grid the dequant consumed: if `nz` is | |
| 3188 | + // empty while `lv` is not, `dequant_wh_into` is what zeroed it, and | |
| 3189 | + // this line says so instead of leaving the next lane to infer it. | |
| 3190 | + let lv: Vec<String> = levels[..16] | |
| 3191 | + .iter() | |
| 3192 | + .enumerate() | |
| 3193 | + .filter(|&(_, &v)| v != 0) | |
| 3194 | + .map(|(i, &v)| format!("{i}:{v}")) | |
| 3195 | + .collect(); | |
| 3196 | + eprintln!( | |
| 3197 | + "OUR_DQW plane={plane} w=4 h=4 q={q_idx} dcd={dc_delta} acd={ac_delta} nz={} lv={}", | |
| 3198 | + nz.join(","), | |
| 3199 | + lv.join(",") | |
| 3200 | + ); | |
| 3201 | + } | |
| 3163 | 3202 | inverse_wht4x4(&dq[..16]) |
| 3164 | 3203 | }) |
| 3165 | 3204 | } |
| @@ -3200,6 +3239,7 @@ pub fn dequant_and_inverse_wht4x4( | ||
| 3200 | 3239 | /// Returns the same empty-grid marker as [`dequant_and_inverse_wht4x4`]. |
| 3201 | 3240 | pub fn dequant_and_inverse_wht( |
| 3202 | 3241 | levels: &[i32], |
| 3242 | + plane: usize, | |
| 3203 | 3243 | w: usize, |
| 3204 | 3244 | h: usize, |
| 3205 | 3245 | bit_depth: u8, |
| @@ -3208,7 +3248,7 @@ pub fn dequant_and_inverse_wht( | ||
| 3208 | 3248 | ac_delta: i32, |
| 3209 | 3249 | ) -> Vec<i32> { |
| 3210 | 3250 | if w == 4 && h == 4 { |
| 3211 | - return dequant_and_inverse_wht4x4(levels, bit_depth, q_idx, dc_delta, ac_delta); | |
| 3251 | + return dequant_and_inverse_wht4x4(levels, plane, bit_depth, q_idx, dc_delta, ac_delta); | |
| 3212 | 3252 | } |
| 3213 | 3253 | if crate::decode::is_zero_grid(levels) { |
| 3214 | 3254 | return Vec::new(); |
| @@ -3227,7 +3267,8 @@ pub fn dequant_and_inverse_wht( | ||
| 3227 | 3267 | }; |
| 3228 | 3268 | } |
| 3229 | 3269 | } |
| 3230 | - let res = dequant_and_inverse_wht4x4(&tile, bit_depth, q_idx, dc_delta, ac_delta); | |
| 3270 | + let res = | |
| 3271 | + dequant_and_inverse_wht4x4(&tile, plane, bit_depth, q_idx, dc_delta, ac_delta); | |
| 3231 | 3272 | // An all-zero tile dequantizes to the same empty marker a 4x4 |
| 3232 | 3273 | // caller passes straight through, and `out` is already zero -- |
| 3233 | 3274 | // copying out of it would index an empty slice. |
| @@ -3342,12 +3383,13 @@ mod lossless_tx_tests { | ||
| 3342 | 3383 | }; |
| 3343 | 3384 | for &(w, h) in &[(8usize, 4usize), (4, 8), (8, 8), (16, 4), (4, 16), (16, 16)] { |
| 3344 | 3385 | let levels: Vec<i32> = (0..w * h).map(|_| next()).collect(); |
| 3345 | - let got = super::dequant_and_inverse_wht(&levels, w, h, 10, 0, 0, 0); | |
| 3386 | + let got = super::dequant_and_inverse_wht(&levels, 0, w, h, 10, 0, 0, 0); | |
| 3346 | 3387 | let mut want = vec![0i32; w * h]; |
| 3347 | 3388 | for ty in 0..h / 4 { |
| 3348 | 3389 | for tx in 0..w / 4 { |
| 3349 | 3390 | let unit = super::dequant_and_inverse_wht4x4( |
| 3350 | 3391 | &tile_of(&levels, w, tx, ty), |
| 3392 | + 0, | |
| 3351 | 3393 | 10, |
| 3352 | 3394 | 0, |
| 3353 | 3395 | 0, |
| @@ -3382,7 +3424,7 @@ mod lossless_tx_tests { | ||
| 3382 | 3424 | levels[0] = 400; |
| 3383 | 3425 | levels[1] = -240; |
| 3384 | 3426 | levels[2] = 88; |
| 3385 | - let got = super::dequant_and_inverse_wht(&levels, 8, 4, 10, 0, 0, 0); | |
| 3427 | + let got = super::dequant_and_inverse_wht(&levels, 0, 8, 4, 10, 0, 0, 0); | |
| 3386 | 3428 | assert_eq!(got.len(), 32, "8x4 residual length"); |
| 3387 | 3429 | let mut want = vec![0i32; 32]; |
| 3388 | 3430 | for (tx, ty) in [(0usize, 0usize), (1, 0)] { |
| @@ -3392,7 +3434,7 @@ mod lossless_tx_tests { | ||
| 3392 | 3434 | t[r * 4 + c] = levels[(ty * 4 + r) * 8 + tx * 4 + c]; |
| 3393 | 3435 | } |
| 3394 | 3436 | } |
| 3395 | - let unit = super::dequant_and_inverse_wht4x4(&t, 10, 0, 0, 0); | |
| 3437 | + let unit = super::dequant_and_inverse_wht4x4(&t, 0, 10, 0, 0, 0); | |
| 3396 | 3438 | if unit.is_empty() { |
| 3397 | 3439 | continue; |
| 3398 | 3440 | } |
| @@ -3422,8 +3464,8 @@ mod lossless_tx_tests { | ||
| 3422 | 3464 | }) |
| 3423 | 3465 | .collect(); |
| 3424 | 3466 | assert_eq!( |
| 3425 | - super::dequant_and_inverse_wht(&levels, 4, 4, 8, 0, 0, 0), | |
| 3426 | - super::dequant_and_inverse_wht4x4(&levels, 8, 0, 0, 0), | |
| 3467 | + super::dequant_and_inverse_wht(&levels, 0, 4, 4, 8, 0, 0, 0), | |
| 3468 | + super::dequant_and_inverse_wht4x4(&levels, 0, 8, 0, 0, 0), | |
| 3427 | 3469 | "levels={levels:?}" |
| 3428 | 3470 | ); |
| 3429 | 3471 | } |
| @@ -3437,7 +3479,7 @@ mod lossless_tx_tests { | ||
| 3437 | 3479 | #[test] |
| 3438 | 3480 | fn a_non_4x4_all_zero_lossless_unit_keeps_the_empty_marker() { |
| 3439 | 3481 | assert!( |
| 3440 | - super::dequant_and_inverse_wht(&[0i32; 32], 8, 4, 10, 0, 0, 0).is_empty(), | |
| 3482 | + super::dequant_and_inverse_wht(&[0i32; 32], 0, 8, 4, 10, 0, 0, 0).is_empty(), | |
| 3441 | 3483 | "an all-zero 8x4 lossless unit must stay the empty marker" |
| 3442 | 3484 | ); |
| 3443 | 3485 | } |