lossless WHT: a non-4x4 unit is reconstructed as a 4x4 raster, not asserted away
Browse files at this commitc465d0644591ad81664dada9d6e8d8894ffd4457Parent: a460dca4 changed files +598 -14
- crates/ec-av1/fixtures/ll444_sb64_1to4_lossless.obu +0 -0
- crates/ec-av1/src/decode.rs +179 -11
- crates/ec-av1/src/stream.rs +199 -0
- crates/ec-av1/src/transform.rs +220 -3
crates/ec-av1/fixtures/ll444_sb64_1to4_lossless.obuadded+0 -0
Binary file changed
crates/ec-av1/src/decode.rsmodified+179 -11
| @@ -2777,6 +2777,30 @@ pub(crate) fn lossless_wht_units() -> usize { | |||
| 2777 | LOSSLESS_WHT_UNITS.load(std::sync::atomic::Ordering::Relaxed) | 2777 | LOSSLESS_WHT_UNITS.load(std::sync::atomic::Ordering::Relaxed) |
| 2778 | } | 2778 | } |
| 2779 | 2779 | ||
| 2780 | +/// lane-whtshape: 4x4 Walsh-Hadamard units reconstructed as part of a LOSSLESS | ||
| 2781 | +/// transform unit that was itself NOT 4x4 -- a shape libaom cannot code. | ||
| 2782 | +/// `read_tx_mode` (`av1/decoder/decodeframe.c:140`) returns `ONLY_4X4` for a | ||
| 2783 | +/// `coded_lossless` frame before it looks at the `tx_mode` symbol, | ||
| 2784 | +/// `read_tx_size` (`:1117`) returns `TX_4X4` before it looks at a tree, | ||
| 2785 | +/// `get_vartx_max_txsize` (`av1/common/blockd.h:1447`) returns `TX_4X4` for | ||
| 2786 | +/// EVERY plane of a lossless block, and of `av1/common/idct.c`'s | ||
| 2787 | +/// `highbd_inv_txfm_add_*_c` only `highbd_inv_txfm_add_4x4_c` carries a | ||
| 2788 | +/// `lossless` branch -- every other size falls through to the IDCT, because | ||
| 2789 | +/// there is no non-4x4 lossless inverse transform to dispatch to. So one of | ||
| 2790 | +/// these is a CALLER sizing a lossless unit wrongly, not a bitstream shape; | ||
| 2791 | +/// read as a before/after delta around one decode it names the caller. Must | ||
| 2792 | +/// read 0 on a stream whose lossless unit geometry is right. Atomic: the | ||
| 2793 | +/// wavefront workers run the transform on their own threads. | ||
| 2794 | +static LOSSLESS_WHT_SPLIT_UNITS: std::sync::atomic::AtomicUsize = | ||
| 2795 | + std::sync::atomic::AtomicUsize::new(0); | ||
| 2796 | + | ||
| 2797 | +/// [`LOSSLESS_WHT_SPLIT_UNITS`] -- total over the process, read as a | ||
| 2798 | +/// before/after delta around one decode. | ||
| 2799 | +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates | ||
| 2800 | +pub(crate) fn lossless_wht_split_units() -> usize { | ||
| 2801 | + LOSSLESS_WHT_SPLIT_UNITS.load(std::sync::atomic::Ordering::Relaxed) | ||
| 2802 | +} | ||
| 2803 | + | ||
| 2780 | impl TxParams { | 2804 | impl TxParams { |
| 2781 | /// The one copy of the produce-the-residual code: the inline path calls it | 2805 | /// The one copy of the produce-the-residual code: the inline path calls it |
| 2782 | /// on the parse thread, the wavefront worker calls it on its own, so both | 2806 | /// on the parse thread, the wavefront worker calls it on its own, so both |
| @@ -2784,10 +2808,34 @@ impl TxParams { | |||
| 2784 | fn run(self, grid: &[i32]) -> Vec<i32> { | 2808 | fn run(self, grid: &[i32]) -> Vec<i32> { |
| 2785 | let residual = if self.lossless { | 2809 | let residual = if self.lossless { |
| 2786 | LOSSLESS_WHT_UNITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); | 2810 | LOSSLESS_WHT_UNITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); |
| 2787 | - debug_assert_eq!((self.w, self.h), (4, 4)); | 2811 | + // lane-whtshape: a non-4x4 unit here is this decoder's own |
| 2788 | - debug_assert_eq!(self.tx_type, TxType::DctDct); | 2812 | + // transform-unit geometry being wrong (see |
| 2789 | - crate::transform::dequant_and_inverse_wht4x4( | 2813 | + // [`LOSSLESS_WHT_SPLIT_UNITS`] for the libaom evidence). It used |
| 2814 | + // to be stopped by a `debug_assert_eq!` that RELEASE never ran: | ||
| 2815 | + // the 4x4 WHT returned 16 values for a `w * h` block and the | ||
| 2816 | + // release build died one frame later in the strided copy with an | ||
| 2817 | + // opaque `range end index 8 out of range for slice of length 0` | ||
| 2818 | + // that never named lossless or the transform. Counted, and | ||
| 2819 | + // reconstructed as the raster of 4x4 WHT units | ||
| 2820 | + // ([`crate::transform::dequant_and_inverse_wht`]) instead: total, | ||
| 2821 | + // the same in both profiles, and the 4x4 fast path is the same | ||
| 2822 | + // single call it always was. The old | ||
| 2823 | + // `debug_assert_eq!(self.tx_type, TxType::DctDct)` is gone with | ||
| 2824 | + // it: the WHT takes no tx_type (libaom's `av1_iwht4x4_add` does | ||
| 2825 | + // not either), and `av1_get_tx_type` already answers `DCT_DCT` on | ||
| 2826 | + // every plane of a lossless block, so asserting an argument | ||
| 2827 | + // nothing reads was a debug-only panic on a value release | ||
| 2828 | + // ignores. | ||
| 2829 | + if self.w != 4 || self.h != 4 { | ||
| 2830 | + LOSSLESS_WHT_SPLIT_UNITS.fetch_add( | ||
| 2831 | + self.w.div_ceil(4) * self.h.div_ceil(4), | ||
| 2832 | + std::sync::atomic::Ordering::Relaxed, | ||
| 2833 | + ); | ||
| 2834 | + } | ||
| 2835 | + crate::transform::dequant_and_inverse_wht( | ||
| 2790 | grid, | 2836 | grid, |
| 2837 | + self.w, | ||
| 2838 | + self.h, | ||
| 2791 | self.bit_depth, | 2839 | self.bit_depth, |
| 2792 | self.q_idx, | 2840 | self.q_idx, |
| 2793 | self.dc_delta, | 2841 | self.dc_delta, |
| @@ -4521,6 +4569,17 @@ thread_local! { | |||
| 4521 | /// invisible to every gate because they only counted CODED strips. | 4569 | /// invisible to every gate because they only counted CODED strips. |
| 4522 | static INTRABC_RECT_SKIPPED_HITS: std::cell::Cell<[usize; 2]> = | 4570 | static INTRABC_RECT_SKIPPED_HITS: std::cell::Cell<[usize; 2]> = |
| 4523 | const { std::cell::Cell::new([0, 0]) }; | 4571 | const { std::cell::Cell::new([0, 0]) }; |
| 4572 | + /// lane-whtshape: LOSSLESS rect intrabc blocks whose chroma plane block | ||
| 4573 | + /// [`decode_intrabc_rect`] walked as a `(cw/4) * (ch/4)` PLANE-MAJOR | ||
| 4574 | + /// raster of TX_4X4 units -- the shape libaom's lossless geometry codes | ||
| 4575 | + /// (`read_tx_size` returns `TX_4X4` for EVERY plane of a lossless block, | ||
| 4576 | + /// `get_vartx_max_txsize` likewise, and `read_tx_mode` forced the frame's | ||
| 4577 | + /// `ONLY_4X4`) -- instead of the single rect unit, which is what put a | ||
| 4578 | + /// non-4x4 unit on the lossless Walsh-Hadamard path. EXCLUSIVE to this arm: | ||
| 4579 | + /// no other caller walks a rect intrabc block's chroma, so `>= 1` cannot | ||
| 4580 | + /// be satisfied by any older route. | ||
| 4581 | + static INTRABC_RECT_LOSSLESS_CHROMA4_HITS: std::cell::Cell<usize> = | ||
| 4582 | + const { std::cell::Cell::new(0) }; | ||
| 4524 | } | 4583 | } |
| 4525 | 4584 | ||
| 4526 | /// lane-av1-intrabc r4: coded (`skip == 0`) rect intrabc blocks reconstructed, | 4585 | /// lane-av1-intrabc r4: coded (`skip == 0`) rect intrabc blocks reconstructed, |
| @@ -4552,6 +4611,18 @@ pub(crate) fn reset_intrabc_rect_hits() { | |||
| 4552 | INTRABC_RECT_SKIPPED_HITS.with(|c| c.set([0, 0])); | 4611 | INTRABC_RECT_SKIPPED_HITS.with(|c| c.set([0, 0])); |
| 4553 | } | 4612 | } |
| 4554 | 4613 | ||
| 4614 | +/// lane-whtshape: [`INTRABC_RECT_LOSSLESS_CHROMA4_HITS`]. | ||
| 4615 | +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates | ||
| 4616 | +pub fn intrabc_rect_lossless_chroma4_hits() -> usize { | ||
| 4617 | + INTRABC_RECT_LOSSLESS_CHROMA4_HITS.with(|c| c.get()) | ||
| 4618 | +} | ||
| 4619 | + | ||
| 4620 | +/// lane-whtshape: zeroes [`INTRABC_RECT_LOSSLESS_CHROMA4_HITS`]. | ||
| 4621 | +#[allow(dead_code)] // read only from the `#[cfg(test)]` gates | ||
| 4622 | +pub(crate) fn reset_intrabc_rect_lossless_chroma4_hits() { | ||
| 4623 | + INTRABC_RECT_LOSSLESS_CHROMA4_HITS.with(|c| c.set(0)); | ||
| 4624 | +} | ||
| 4625 | + | ||
| 4555 | /// Current value of [`INTRABC_RECT_VARTX_HITS`]. | 4626 | /// Current value of [`INTRABC_RECT_VARTX_HITS`]. |
| 4556 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates | 4627 | #[allow(dead_code)] // read only from the `#[cfg(test)]` gates |
| 4557 | pub fn intrabc_rect_vartx_hits() -> usize { | 4628 | pub fn intrabc_rect_vartx_hits() -> usize { |
| @@ -13062,12 +13133,34 @@ fn decode_intrabc_rect( | |||
| 13062 | }); | 13133 | }); |
| 13063 | } | 13134 | } |
| 13064 | let (px, py) = (mi_c * MI, mi_r * MI); | 13135 | let (px, py) = (mi_c * MI, mi_r * MI); |
| 13065 | - let (cpx, cpy) = (px / 2, py / 2); | 13136 | + // lane-whtshape: a LOSSLESS block's chroma plane block is |
| 13137 | + // `bw >> ss_x` by `bh >> ss_y` at its own origin -- `av1_get_max_uv_txsize` | ||
| 13138 | + // is `ss_size_lookup[bsize]` (no halving of its own), so at 4:4:4 that is | ||
| 13139 | + // the block's OWN footprint, not the 4:2:0 halving the lossy arm hard-codes. | ||
| 13140 | + // Measured on this lane's 4:4:4 lossless witness: with the halving, a | ||
| 13141 | + // 16x8 intrabc block's chroma plane block came out 8x4 and the lossless | ||
| 13142 | + // walk below coded 2 units per plane where libaom codes 8. | ||
| 13143 | + // | ||
| 13144 | + // Scoped to `lossless` ON PURPOSE. The 4:2:0 halving is right at 4:2:0, | ||
| 13145 | + // and every pinned 4:4:4 stream that reaches this helper does so LOSSY | ||
| 13146 | + // (measured: zero pinned fixtures decode a LOSSLESS intrabc rect block), | ||
| 13147 | + // whose footprint is a separate, already-named open cell -- | ||
| 13148 | + // lanes/av1444rect.report.md's 4:4:4 intra-BC chroma. This lane must not | ||
| 13149 | + // move it, and a lossless-only split here changes no green pin either way. | ||
| 13150 | + let (cpx, cpy) = if lossless(fctx) { | ||
| 13151 | + (px >> ss_x(fctx), py >> ss_y(fctx)) | ||
| 13152 | + } else { | ||
| 13153 | + (px / 2, py / 2) | ||
| 13154 | + }; | ||
| 13066 | // `av1_get_max_uv_txsize(bw x bh)`: 4:2:0 halves the footprint and the | 13155 | // `av1_get_max_uv_txsize(bw x bh)`: 4:2:0 halves the footprint and the |
| 13067 | // halved shape's own `max_txsize_rect_lookup` entry IS that size for every | 13156 | // halved shape's own `max_txsize_rect_lookup` entry IS that size for every |
| 13068 | // shape this helper is called at, so the uv plane block is ONE unit -- | 13157 | // shape this helper is called at, so the uv plane block is ONE unit -- |
| 13069 | // the same call `decode_token_recon_block`'s own chroma loop makes. | 13158 | // the same call `decode_token_recon_block`'s own chroma loop makes. |
| 13070 | - let (cw, ch) = (bw / 2, bh / 2); | 13159 | + let (cw, ch) = if lossless(fctx) { |
| 13160 | + (bw >> ss_x(fctx), bh >> ss_y(fctx)) | ||
| 13161 | + } else { | ||
| 13162 | + (bw / 2, bh / 2) | ||
| 13163 | + }; | ||
| 13071 | // lane-av1-intrabc r2: the square stride the proven INTER path uses | 13164 | // lane-av1-intrabc r2: the square stride the proven INTER path uses |
| 13072 | // (`side`). The residual grids, the prediction windows AND | 13165 | // (`side`). The residual grids, the prediction windows AND |
| 13073 | // `TxParams::stride` are all laid out at it -- the block's own width | 13166 | // `TxParams::stride` are all laid out at it -- the block's own width |
| @@ -13157,6 +13250,10 @@ fn decode_intrabc_rect( | |||
| 13157 | let reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get); | 13250 | let reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get); |
| 13158 | let luma_tx_type: TxType; | 13251 | let luma_tx_type: TxType; |
| 13159 | let (luma_grid, u_grid, v_grid); | 13252 | let (luma_grid, u_grid, v_grid); |
| 13253 | + // lane-whtshape: the LOSSLESS chroma walk's composed plane-major grids, | ||
| 13254 | + // kept so their per-4x4 slices can be re-stamped over the whole-block | ||
| 13255 | + // record in the tail. `None` on every other arm. | ||
| 13256 | + let mut ll_chroma: Option<(Vec<i32>, Vec<i32>)> = None; | ||
| 13160 | if skip { | 13257 | if skip { |
| 13161 | // No residual syntax at all: the prediction IS the block. | 13258 | // No residual syntax at all: the prediction IS the block. |
| 13162 | // Residual stride is `side` (and `cside`), not `bw`: reconstruct_mc_rect indexes | 13259 | // Residual stride is `side` (and `cside`), not `bw`: reconstruct_mc_rect indexes |
| @@ -13379,6 +13476,57 @@ fn decode_intrabc_rect( | |||
| 13379 | around[1] = c[1]; | 13476 | around[1] = c[1]; |
| 13380 | around[2] = c[2]; | 13477 | around[2] = c[2]; |
| 13381 | } | 13478 | } |
| 13479 | + if lossless(fctx) { | ||
| 13480 | + // lane-whtshape: at lossless libaom codes TX_4X4 on EVERY | ||
| 13481 | + // plane of a block -- `read_tx_size` returns `TX_4X4` for a | ||
| 13482 | + // lossless block before it looks at a tree, `read_tx_mode` | ||
| 13483 | + // already forced `ONLY_4X4` for the frame, and | ||
| 13484 | + // `get_vartx_max_txsize` returns `TX_4X4` for every plane | ||
| 13485 | + // -- so this block's chroma plane block is a | ||
| 13486 | + // `(cw/4) * (ch/4)` raster of 4x4 units, PLANE-MAJOR | ||
| 13487 | + // (`decode_token_recon_block`: every U unit, then every V | ||
| 13488 | + // unit), not the single rect unit the two | ||
| 13489 | + // `read_inter_plane_rect` calls below read. That single | ||
| 13490 | + // 8x4 unit is what reached the lossless WHT with the wrong | ||
| 13491 | + // shape (the `debug_assert_eq!((w, h), (4, 4))` this lane | ||
| 13492 | + // replaced). Same helper, same walk, as the inter 8x8 and | ||
| 13493 | + // 128 paths. | ||
| 13494 | + hit!(INTRABC_RECT_LOSSLESS_CHROMA4_HITS); | ||
| 13495 | + let mut uo = vec![0i32; cside * cside]; | ||
| 13496 | + let mut vo = vec![0i32; cside * cside]; | ||
| 13497 | + read_inter_chroma_lossless( | ||
| 13498 | + dec, | ||
| 13499 | + cdfs, | ||
| 13500 | + neighbours, | ||
| 13501 | + u, | ||
| 13502 | + v, | ||
| 13503 | + Pred::Inline(&pred_u, cside), | ||
| 13504 | + Pred::Inline(&pred_v, cside), | ||
| 13505 | + (mi_r, mi_c), | ||
| 13506 | + (cpx, cpy), | ||
| 13507 | + (0, 0), | ||
| 13508 | + (cw, ch), | ||
| 13509 | + (cw, ch), | ||
| 13510 | + cside, | ||
| 13511 | + 0, | ||
| 13512 | + base_q_idx, | ||
| 13513 | + &mut uo, | ||
| 13514 | + &mut vo, | ||
| 13515 | + fctx, | ||
| 13516 | + )?; | ||
| 13517 | + // The per-unit walk stamped every unit's own coefficient | ||
| 13518 | + // context, but the whole-block `record_split_luma_rect_mi` | ||
| 13519 | + // in the tail rewrites every chroma cell with ONE composed | ||
| 13520 | + // state, which drops the per-unit DC signs and levels (class | ||
| 13521 | + // `override-slot-on-one-arm` -- the same one | ||
| 13522 | + // `decode_rect_split`'s `ll_chroma_units` replay exists | ||
| 13523 | + // for). The grids are kept here and replayed AFTER that | ||
| 13524 | + // record, from the same 4x4 slices, so the same states. | ||
| 13525 | + u_grid = embed_grid_stride(&Grid::Own(uo.clone()), cw, ch, cside); | ||
| 13526 | + v_grid = embed_grid_stride(&Grid::Own(vo.clone()), cw, ch, cside); | ||
| 13527 | + ll_chroma = Some((uo, vo)); | ||
| 13528 | + luma_grid = Grid::Zero(side * side); | ||
| 13529 | + } else { | ||
| 13382 | let (ug, _) = read_inter_plane_rect( | 13530 | let (ug, _) = read_inter_plane_rect( |
| 13383 | dec, | 13531 | dec, |
| 13384 | cdfs, | 13532 | cdfs, |
| @@ -13419,6 +13567,7 @@ fn decode_intrabc_rect( | |||
| 13419 | } | 13567 | } |
| 13420 | } | 13568 | } |
| 13421 | } | 13569 | } |
| 13570 | + } | ||
| 13422 | let _ = luma_tx_type; | 13571 | let _ = luma_tx_type; |
| 13423 | // Bookkeeping. An intrabc block publishes mode `DC_PRED` everywhere the | 13572 | // Bookkeeping. An intrabc block publishes mode `DC_PRED` everywhere the |
| 13424 | // next block reads a neighbour mode, its own block size in both `TXFM` | 13573 | // next block reads a neighbour mode, its own block size in both `TXFM` |
| @@ -13444,6 +13593,29 @@ fn decode_intrabc_rect( | |||
| 13444 | &[&luma_grid, &u_grid, &v_grid], | 13593 | &[&luma_grid, &u_grid, &v_grid], |
| 13445 | ); | 13594 | ); |
| 13446 | } | 13595 | } |
| 13596 | + // lane-whtshape: replay the LOSSLESS walk's per-4x4 chroma states over | ||
| 13597 | + // the whole-block record above, so the NEXT block reads the per-unit DC | ||
| 13598 | + // signs and levels libaom's plane-major walk left behind instead of one | ||
| 13599 | + // composed state for the whole plane block. | ||
| 13600 | + if let Some((uo, vo)) = ll_chroma { | ||
| 13601 | + for (plane_idx, comp) in [(1usize, &uo), (2, &vo)] { | ||
| 13602 | + for ur in 0..ch / 4 { | ||
| 13603 | + for uc in 0..cw / 4 { | ||
| 13604 | + let mut unit = Vec::with_capacity(16); | ||
| 13605 | + for r in 0..4 { | ||
| 13606 | + unit.extend_from_slice(&comp[(ur * 4 + r) * cside + uc * 4..][..4]); | ||
| 13607 | + } | ||
| 13608 | + neighbours.record_mi_chroma( | ||
| 13609 | + (mi_r + ur * (4 >> ss_y(fctx)), mi_c + uc * (4 >> ss_x(fctx))), | ||
| 13610 | + 4 << ss_x(fctx), | ||
| 13611 | + 4 << ss_y(fctx), | ||
| 13612 | + plane_idx, | ||
| 13613 | + &Grid::Own(unit), | ||
| 13614 | + ); | ||
| 13615 | + } | ||
| 13616 | + } | ||
| 13617 | + } | ||
| 13618 | + } | ||
| 13447 | neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, 0, [0u16; 8]); | 13619 | neighbours.record_palette_y_rect((mi_r, mi_c), bw, bh, 0, [0u16; 8]); |
| 13448 | neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, 0, [0u16; 8]); | 13620 | neighbours.record_palette_uv_rect((mi_r, mi_c), bw, bh, 0, [0u16; 8]); |
| 13449 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); | 13621 | neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip); |
| @@ -35661,10 +35833,7 @@ fn read_intra_chroma_lossless( | |||
| 35661 | /// skipped (the returned order is the identity). `changed` is `true` only | 35833 | /// skipped (the returned order is the identity). `changed` is `true` only |
| 35662 | /// when the sort actually permutes the list, so a no-op ordering reads zero | 35834 | /// when the sort actually permutes the list, so a no-op ordering reads zero |
| 35663 | /// on [`MU_CHUNK_ORDER_HITS`] and cannot make a gate look armed. | 35835 | /// on [`MU_CHUNK_ORDER_HITS`] and cannot make a gate look armed. |
| 35664 | -fn mu_chunk_order( | 35836 | +fn mu_chunk_order(leaves: &[(usize, usize, usize, usize)], side: usize) -> (Vec<usize>, bool) { |
| 35665 | - leaves: &[(usize, usize, usize, usize)], | ||
| 35666 | - side: usize, | ||
| 35667 | -) -> (Vec<usize>, bool) { | ||
| 35668 | let identity: Vec<usize> = (0..leaves.len()).collect(); | 35837 | let identity: Vec<usize> = (0..leaves.len()).collect(); |
| 35669 | if side <= 64 { | 35838 | if side <= 64 { |
| 35670 | return (identity, false); | 35839 | return (identity, false); |
| @@ -42316,8 +42485,7 @@ fn decode_inter_block( | |||
| 42316 | if side > 64 { | 42485 | if side > 64 { |
| 42317 | let chunk = (row / 16, col / 16); | 42486 | let chunk = (row / 16, col / 16); |
| 42318 | let done = pos + 1 == order.len() | 42487 | let done = pos + 1 == order.len() |
| 42319 | - || (leaves[order[pos + 1]].0 / 16, leaves[order[pos + 1]].1 / 16) | 42488 | + || (leaves[order[pos + 1]].0 / 16, leaves[order[pos + 1]].1 / 16) != chunk; |
| 42320 | - != chunk; | ||
| 42321 | if !done { | 42489 | if !done { |
| 42322 | continue; | 42490 | continue; |
| 42323 | } | 42491 | } |
crates/ec-av1/src/stream.rsmodified+199 -0
| @@ -43597,4 +43597,203 @@ pub(crate) mod tests { | |||
| 43597 | } | 43597 | } |
| 43598 | assert!(seen, "{name}: no sequence header parsed"); | 43598 | assert!(seen, "{name}: no sequence header parsed"); |
| 43599 | } | 43599 | } |
| 43600 | + /// lane-whtshape: a LOSSLESS 4:4:4 frame whose rect intra-BC blocks used | ||
| 43601 | + /// to code their chroma plane block as ONE rect transform unit -- a shape | ||
| 43602 | + /// libaom cannot code. `read_tx_mode` (`av1/decoder/decodeframe.c:140`) | ||
| 43603 | + /// returns `ONLY_4X4` for a `coded_lossless` frame BEFORE it looks at the | ||
| 43604 | + /// `tx_mode` symbol, `read_tx_size` (`:1117`) returns `TX_4X4` before it | ||
| 43605 | + /// looks at a tree, `get_vartx_max_txsize` (`av1/common/blockd.h:1447`) | ||
| 43606 | + /// returns `TX_4X4` for EVERY plane of a lossless block, and of | ||
| 43607 | + /// `av1/common/idct.c`'s `highbd_inv_txfm_add_*_c` only the 4x4 one carries | ||
| 43608 | + /// a `lossless` branch -- the rest fall through to the IDCT, because there | ||
| 43609 | + /// is no non-4x4 lossless inverse transform to dispatch to. | ||
| 43610 | + /// | ||
| 43611 | + /// The 8x4 unit that resulted reached the lossless Walsh-Hadamard path, | ||
| 43612 | + /// where a `debug_assert_eq!` named it in DEBUG and the RELEASE build died | ||
| 43613 | + /// one frame later with an opaque `range end index 8 out of range for slice | ||
| 43614 | + /// of length 0` (measured, not assumed -- see | ||
| 43615 | + /// lanes/whtshape.report.md r1). | ||
| 43616 | + /// | ||
| 43617 | + /// The witness is `fixtures/ll444_sb64_1to4_lossless.obu` (51107 bytes, | ||
| 43618 | + /// sha256 `fdb6467987095b99c8eb51b48743691d906b2d99d069f71f1d51b1abb6d2056e`), | ||
| 43619 | + /// encoded with the instrumented oracle | ||
| 43620 | + /// (`scripts/build-aom-oracle.sh`'s `aomenc`, 4:4:4 10-bit, `--lossless=1 | ||
| 43621 | + /// --profile=1 --i444 --enable-rect-partitions=1 | ||
| 43622 | + /// --enable-1to4-partitions=1 --min-partition-size=4 --max-partition-size=64 | ||
| 43623 | + /// --sb-size=64 --tune-content=screen --enable-intrabc=1 --enable-palette=1 | ||
| 43624 | + /// --cq-level=60 --enable-tx-size-search=0 --cpu-used=0 --limit=2`) over | ||
| 43625 | + /// `ffmpeg -f lavfi -i testsrc2=size=256x256:rate=25:duration=1 -pix_fmt | ||
| 43626 | + /// yuv444p10le -f rawvideo`. 4:2:0 cannot produce the shape at all -- its | ||
| 43627 | + /// smallest lossless chroma plane block is already 4x4 -- which is why the | ||
| 43628 | + /// 4:4:4 10-bit header is asserted. | ||
| 43629 | + /// | ||
| 43630 | + /// Four assertions, each with its own failure: | ||
| 43631 | + /// | ||
| 43632 | + /// 1. the route fired: `INTRABC_RECT_LOSSLESS_CHROMA4_HITS` is EXCLUSIVE to | ||
| 43633 | + /// the per-4x4 chroma walk (no other caller walks a rect intra-BC | ||
| 43634 | + /// block's chroma), so `>= 1` cannot be satisfied by the old | ||
| 43635 | + /// single-unit read; | ||
| 43636 | + /// 2. NO non-4x4 lossless transform unit was reconstructed | ||
| 43637 | + /// (`LOSSLESS_WHT_SPLIT_UNITS` delta 0) -- the shape-total WHT entry is | ||
| 43638 | + /// not quietly papering over a caller that is still wrong; | ||
| 43639 | + /// 3. the stream DECODES both frames (before the fix the same bytes were a | ||
| 43640 | + /// RELEASE panic and a Golomb-tail refusal in DEBUG); and | ||
| 43641 | + /// 4. a PIXEL comparison against the instrumented oracle's own | ||
| 43642 | + /// `EC_AV1_FINAL_DUMP`, in DECODE order. | ||
| 43643 | + /// | ||
| 43644 | + /// **(4) is a PREFIX floor, not an exactness claim, and it says so.** | ||
| 43645 | + /// Measured against the oracle, decode-order frame 0 (the key frame) is | ||
| 43646 | + /// byte-exact on all three planes for its first 32992 of 65536 samples -- | ||
| 43647 | + /// the whole frame above row 128, plus row 128 out to column 224. The | ||
| 43648 | + /// first differing sample there is (x=224, y=128) on Y, U and V alike: the | ||
| 43649 | + /// first column PAST the 16x8 lossless intra-BC block at mi (32, 52), | ||
| 43650 | + /// whose own 16x8 luma and chroma are byte-exact -- so the fork is at the | ||
| 43651 | + /// block boundary, in the block that FOLLOWS it. Decode-order frame 1 (the | ||
| 43652 | + /// inter frame) is exact to 55008 on luma and to 9378 on chroma. Both forks | ||
| 43653 | + /// are a SEPARATE defect this lane does not own; each is named with its | ||
| 43654 | + /// measurement in lanes/whtshape.report.md r3. Asserting "our output is | ||
| 43655 | + /// wrong by exactly N" would encode those defects as expected behaviour; | ||
| 43656 | + /// the per-frame, per-plane floor is the honest form, and it only grows. | ||
| 43657 | + #[test] | ||
| 43658 | + fn a_444_lossless_sb64_intrabc_rect_chroma_walks_4x4_units() { | ||
| 43659 | + const NAME: &str = "a_444_lossless_sb64_intrabc_rect_chroma_walks_4x4_units"; | ||
| 43660 | + const FIXTURE: &str = "ll444_sb64_1to4_lossless.obu"; | ||
| 43661 | + const FIXTURE_LEN: usize = 51107; | ||
| 43662 | + const FIXTURE_FNV: u64 = 0x7664_6a62_ddd3_4df2; | ||
| 43663 | + const FRAMES: usize = 2; | ||
| 43664 | + const PLANE: usize = 256 * 256; | ||
| 43665 | + /// Per decode-order frame, per plane (0=Y, 1=U, 2=V), the number of | ||
| 43666 | + /// leading samples MEASURED byte-exact against the oracle's | ||
| 43667 | + /// `EC_AV1_FINAL_DUMP`; the first differing sample is exactly this | ||
| 43668 | + /// index on that plane. Key frame: the whole frame above row 128 plus | ||
| 43669 | + /// row 128 out to column 224. Inter frame: luma exact to (224, 214), | ||
| 43670 | + /// chroma diverging early at (162, 36). | ||
| 43671 | + const EXACT_PREFIX: [[usize; 3]; 2] = [[32992, 32992, 32992], [55008, 9378, 9378]]; | ||
| 43672 | + let obu = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) | ||
| 43673 | + .join(format!("fixtures/{FIXTURE}")); | ||
| 43674 | + let stream = read_pin(&obu, FIXTURE_LEN, FIXTURE_FNV, NAME); | ||
| 43675 | + assert_444_header(&stream, NAME, 10); | ||
| 43676 | + | ||
| 43677 | + let _guard = lock_gate_counters(); | ||
| 43678 | + crate::decode::reset_intrabc_rect_lossless_chroma4_hits(); | ||
| 43679 | + let split_before = crate::decode::lossless_wht_split_units(); | ||
| 43680 | + let wht_before = crate::decode::lossless_wht_units(); | ||
| 43681 | + let frames = decode_stream(&stream) | ||
| 43682 | + .unwrap_or_else(|e| panic!("{NAME}: the pinned witness was refused: {e}")); | ||
| 43683 | + let chroma4 = crate::decode::intrabc_rect_lossless_chroma4_hits(); | ||
| 43684 | + let split = crate::decode::lossless_wht_split_units() - split_before; | ||
| 43685 | + let wht = crate::decode::lossless_wht_units() - wht_before; | ||
| 43686 | + | ||
| 43687 | + assert!( | ||
| 43688 | + chroma4 > 0, | ||
| 43689 | + "{NAME}: no LOSSLESS rect intra-BC block walked its chroma as 4x4 units \ | ||
| 43690 | + -- the corrected route never ran, so this gate is measuring the old \ | ||
| 43691 | + single rect unit (class gate-blind-to-feature)" | ||
| 43692 | + ); | ||
| 43693 | + assert_eq!( | ||
| 43694 | + split, 0, | ||
| 43695 | + "{NAME}: {split} 4x4 WHT unit(s) were reconstructed inside a LOSSLESS \ | ||
| 43696 | + transform unit that was itself not 4x4 -- a caller is still sizing a \ | ||
| 43697 | + lossless unit wrongly and the shape-total WHT entry is hiding it" | ||
| 43698 | + ); | ||
| 43699 | + assert!( | ||
| 43700 | + wht > 0, | ||
| 43701 | + "{NAME}: no lossless WHT unit ran at all -- vacuous" | ||
| 43702 | + ); | ||
| 43703 | + assert_eq!(frames.len(), FRAMES, "{NAME}: frame count"); | ||
| 43704 | + | ||
| 43705 | + if !aomdec_path().is_file() { | ||
| 43706 | + eprintln!( | ||
| 43707 | + "{NAME}: {chroma4} lossless rect intra-BC block(s) walked 4x4 chroma \ | ||
| 43708 | + units, {wht} WHT units, 0 split; no oracle aomdec at {}, pixel arm \ | ||
| 43709 | + skipped", | ||
| 43710 | + aomdec_path().display() | ||
| 43711 | + ); | ||
| 43712 | + return; | ||
| 43713 | + } | ||
| 43714 | + // The oracle's own final-frame dumps, in DECODE order, against ours. | ||
| 43715 | + let dir = std::env::temp_dir().join(format!("ec-av1-{NAME}-{}", std::process::id())); | ||
| 43716 | + let _ = std::fs::remove_dir_all(&dir); | ||
| 43717 | + std::fs::create_dir_all(&dir).expect("scratch dir"); | ||
| 43718 | + let in_obu = dir.join("in.obu"); | ||
| 43719 | + std::fs::write(&in_obu, &stream).expect("writing the stream"); | ||
| 43720 | + let aom_prefix = dir.join("aom"); | ||
| 43721 | + let out = Command::new(aomdec_path()) | ||
| 43722 | + .args(["--codec=av1", "-o"]) | ||
| 43723 | + .arg(dir.join("out.y4m")) | ||
| 43724 | + .arg(&in_obu) | ||
| 43725 | + .env("EC_AV1_FINAL_DUMP", &aom_prefix) | ||
| 43726 | + .stdin(Stdio::null()) | ||
| 43727 | + .stdout(Stdio::piped()) | ||
| 43728 | + .stderr(Stdio::piped()) | ||
| 43729 | + .output() | ||
| 43730 | + .expect("aomdec failed to run"); | ||
| 43731 | + assert!( | ||
| 43732 | + out.status.success(), | ||
| 43733 | + "{NAME}: the oracle aomdec refused the stream: {}", | ||
| 43734 | + String::from_utf8_lossy(&out.stderr) | ||
| 43735 | + ); | ||
| 43736 | + let ours_prefix = dir.join("ours"); | ||
| 43737 | + set_final_dump_prefix(Some(ours_prefix.display().to_string())); | ||
| 43738 | + let shown = decode_stream(&stream) | ||
| 43739 | + .unwrap_or_else(|e| panic!("{NAME}: the second decode was refused: {e}")) | ||
| 43740 | + .len(); | ||
| 43741 | + set_final_dump_prefix(None); | ||
| 43742 | + let count = |prefix: &std::path::Path| -> usize { | ||
| 43743 | + let mut n = 0usize; | ||
| 43744 | + while std::path::PathBuf::from(format!("{}.f{n}", prefix.display())).is_file() { | ||
| 43745 | + n += 1; | ||
| 43746 | + } | ||
| 43747 | + n | ||
| 43748 | + }; | ||
| 43749 | + let oracle_frames = count(&aom_prefix); | ||
| 43750 | + let our_frames = count(&ours_prefix); | ||
| 43751 | + assert_eq!( | ||
| 43752 | + our_frames, oracle_frames, | ||
| 43753 | + "{NAME}: decoded {our_frames} frames in decode order, the oracle decoded \ | ||
| 43754 | + {oracle_frames}" | ||
| 43755 | + ); | ||
| 43756 | + for i in 0..oracle_frames { | ||
| 43757 | + let want = | ||
| 43758 | + std::fs::read(format!("{}.f{i}", aom_prefix.display())).expect("oracle dump"); | ||
| 43759 | + let got = std::fs::read(format!("{}.f{i}", ours_prefix.display())).expect("our dump"); | ||
| 43760 | + assert_eq!( | ||
| 43761 | + got.len(), | ||
| 43762 | + want.len(), | ||
| 43763 | + "{NAME}: decode-order frame {i} is {} bytes, the oracle's is {}", | ||
| 43764 | + got.len(), | ||
| 43765 | + want.len() | ||
| 43766 | + ); | ||
| 43767 | + assert_eq!( | ||
| 43768 | + got.len(), | ||
| 43769 | + 3 * PLANE * 2, | ||
| 43770 | + "{NAME}: decode-order frame {i} is not three 256x256 16-bit planes" | ||
| 43771 | + ); | ||
| 43772 | + for (plane, name) in [(0usize, "Y"), (1, "U"), (2, "V")] { | ||
| 43773 | + let at = plane * PLANE * 2; | ||
| 43774 | + let floor = EXACT_PREFIX[i.min(EXACT_PREFIX.len() - 1)][plane].min(PLANE); | ||
| 43775 | + if got[at..at + floor * 2] == want[at..at + floor * 2] { | ||
| 43776 | + continue; | ||
| 43777 | + } | ||
| 43778 | + let first = (0..floor) | ||
| 43779 | + .find(|&s| got[at + s * 2..at + s * 2 + 2] != want[at + s * 2..at + s * 2 + 2]) | ||
| 43780 | + .unwrap_or(floor); | ||
| 43781 | + panic!( | ||
| 43782 | + "{NAME}: decode-order frame {i} plane {name} diverges from the oracle at \ | ||
| 43783 | + sample {first} of {PLANE} (x={}, y={}) -- the exact prefix this gate pins \ | ||
| 43784 | + is {floor} and it only grows; the rest is the OPEN defect named in \ | ||
| 43785 | + lanes/whtshape.report.md r3", | ||
| 43786 | + first % 256, | ||
| 43787 | + first / 256 | ||
| 43788 | + ); | ||
| 43789 | + } | ||
| 43790 | + } | ||
| 43791 | + let _ = std::fs::remove_dir_all(&dir); | ||
| 43792 | + eprintln!( | ||
| 43793 | + "{NAME}: {chroma4} lossless rect intra-BC block(s) walked 4x4 chroma units, \ | ||
| 43794 | + {wht} WHT units, 0 split; {oracle_frames} decode-order frame(s) ({shown} shown) \ | ||
| 43795 | + byte-exact against the oracle up to the per-frame, per-plane exact prefix \ | ||
| 43796 | + -- the tail is the OPEN r3 defect, not an assertion" | ||
| 43797 | + ); | ||
| 43798 | + } | ||
| 43600 | } | 43799 | } |
crates/ec-av1/src/transform.rsmodified+220 -3
| @@ -186,7 +186,7 @@ impl Lane for [i32; 8] { | |||
| 186 | #[cfg(target_arch = "x86_64")] | 186 | #[cfg(target_arch = "x86_64")] |
| 187 | #[allow(unsafe_code)] | 187 | #[allow(unsafe_code)] |
| 188 | mod simd { | 188 | mod simd { |
| 189 | - use super::{inverse_dct_n, Lane, COS_BITS}; | 189 | + use super::{COS_BITS, Lane, inverse_dct_n}; |
| 190 | use std::arch::x86_64::*; | 190 | use std::arch::x86_64::*; |
| 191 | 191 | ||
| 192 | /// Eight `i32` lanes in one AVX2 register. | 192 | /// Eight `i32` lanes in one AVX2 register. |
| @@ -2717,7 +2717,9 @@ mod tests { | |||
| 2717 | inverse_adst16(&mut t, 16); | 2717 | inverse_adst16(&mut t, 16); |
| 2718 | assert_eq!( | 2718 | assert_eq!( |
| 2719 | t, | 2719 | t, |
| 2720 | - [32, 73, 28, 6, -45, -28, 111, -52, 135, -54, 85, -67, 305, 190, 102, 145] | 2720 | + [ |
| 2721 | + 32, 73, 28, 6, -45, -28, 111, -52, 135, -54, 85, -67, 305, 190, 102, 145 | ||
| 2722 | + ] | ||
| 2721 | ); | 2723 | ); |
| 2722 | } | 2724 | } |
| 2723 | 2725 | ||
| @@ -2737,7 +2739,9 @@ mod tests { | |||
| 2737 | inverse_identity(&mut t16, 16); | 2739 | inverse_identity(&mut t16, 16); |
| 2738 | assert_eq!( | 2740 | assert_eq!( |
| 2739 | t16, | 2741 | t16, |
| 2740 | - [283, -141, 71, 20, -8, 170, -34, 23, 42, -113, 93, -14, 62, 3, -198, 124] | 2742 | + [ |
| 2743 | + 283, -141, 71, 20, -8, 170, -34, 23, 42, -113, 93, -14, 62, 3, -198, 124 | ||
| 2744 | + ] | ||
| 2741 | ); | 2745 | ); |
| 2742 | 2746 | ||
| 2743 | let mut t32 = [0i32; 32]; | 2747 | let mut t32 = [0i32; 32]; |
| @@ -3152,6 +3156,92 @@ pub fn dequant_and_inverse_wht4x4( | |||
| 3152 | }) | 3156 | }) |
| 3153 | } | 3157 | } |
| 3154 | 3158 | ||
| 3159 | +/// lane-whtshape: the lossless inverse transform for a unit of ANY shape. | ||
| 3160 | +/// | ||
| 3161 | +/// libaom never codes a lossless transform unit that is not 4x4, and it says | ||
| 3162 | +/// so in two places this decoder already mirrors: | ||
| 3163 | +/// | ||
| 3164 | +/// * `read_tx_mode` (`av1/decoder/decodeframe.c:140`) returns `ONLY_4X4` | ||
| 3165 | +/// for a `coded_lossless` frame BEFORE it looks at the `tx_mode` symbol, so | ||
| 3166 | +/// a lossless frame has no var-tx tree at all; and | ||
| 3167 | +/// * `av1_get_tx_size` (`av1/common/blockd.h`) returns `TX_4X4` for EVERY | ||
| 3168 | +/// plane of a lossless block before it looks at a tree. | ||
| 3169 | +/// | ||
| 3170 | +/// The WHT dispatch proves the same from the other side: of | ||
| 3171 | +/// `av1/common/idct.c`'s `highbd_inv_txfm_add_{4x8,8x4,16x4,...}_c`, ONLY | ||
| 3172 | +/// `highbd_inv_txfm_add_4x4_c` carries a lossless branch | ||
| 3173 | +/// (`idct.c:50`: `if (lossless) { av1_highbd_iwht4x4_add(...); return; }`). | ||
| 3174 | +/// Every other size falls straight through to the IDCT network -- libaom has | ||
| 3175 | +/// no lossless inverse transform for a non-4x4 unit to dispatch to, because | ||
| 3176 | +/// `read_tx_mode`/`av1_get_tx_size` make that unit unreachable. | ||
| 3177 | +/// | ||
| 3178 | +/// So a non-4x4 `lossless: true` [`TxParams`](crate::decode::TxParams) is | ||
| 3179 | +/// never a bitstream libaom could have produced: it is THIS decoder's own | ||
| 3180 | +/// transform-unit geometry being wrong. The function is nevertheless TOTAL | ||
| 3181 | +/// (it used to be a `debug_assert_eq!` plus a 16-value result for a | ||
| 3182 | +/// `w * h` block, which panicked one frame later in RELEASE with an opaque | ||
| 3183 | +/// `range end index .. out of range for slice of length 0`): a unit is | ||
| 3184 | +/// reconstructed as the raster of 4x4 WHT units that libaom's lossless | ||
| 3185 | +/// geometry would have produced for that footprint -- the same numbers, in | ||
| 3186 | +/// the same places, the correct per-4x4 caller would have written. The 4x4 | ||
| 3187 | +/// case is the same single call it always was, so the lossless fast path is | ||
| 3188 | +/// bit-for-bit unchanged. | ||
| 3189 | +/// | ||
| 3190 | +/// `w`/`h` need not be multiples of four: a partial tile is zero-filled, so | ||
| 3191 | +/// no shape panics and no shape returns a buffer the caller cannot index. | ||
| 3192 | +/// Returns the same empty-grid marker as [`dequant_and_inverse_wht4x4`]. | ||
| 3193 | +pub fn dequant_and_inverse_wht( | ||
| 3194 | + levels: &[i32], | ||
| 3195 | + w: usize, | ||
| 3196 | + h: usize, | ||
| 3197 | + bit_depth: u8, | ||
| 3198 | + q_idx: i32, | ||
| 3199 | + dc_delta: i32, | ||
| 3200 | + ac_delta: i32, | ||
| 3201 | +) -> Vec<i32> { | ||
| 3202 | + if w == 4 && h == 4 { | ||
| 3203 | + return dequant_and_inverse_wht4x4(levels, bit_depth, q_idx, dc_delta, ac_delta); | ||
| 3204 | + } | ||
| 3205 | + if crate::decode::is_zero_grid(levels) { | ||
| 3206 | + return Vec::new(); | ||
| 3207 | + } | ||
| 3208 | + let mut out = vec![0i32; w * h]; | ||
| 3209 | + let mut tile = [0i32; 16]; | ||
| 3210 | + for ty in 0..h.div_ceil(4) { | ||
| 3211 | + for tx in 0..w.div_ceil(4) { | ||
| 3212 | + for r in 0..4 { | ||
| 3213 | + for c in 0..4 { | ||
| 3214 | + let (sy, sx) = (ty * 4 + r, tx * 4 + c); | ||
| 3215 | + tile[r * 4 + c] = if sy < h && sx < w { | ||
| 3216 | + levels[sy * w + sx] | ||
| 3217 | + } else { | ||
| 3218 | + 0 | ||
| 3219 | + }; | ||
| 3220 | + } | ||
| 3221 | + } | ||
| 3222 | + let res = dequant_and_inverse_wht4x4(&tile, bit_depth, q_idx, dc_delta, ac_delta); | ||
| 3223 | + // An all-zero tile dequantizes to the same empty marker a 4x4 | ||
| 3224 | + // caller passes straight through, and `out` is already zero -- | ||
| 3225 | + // copying out of it would index an empty slice. | ||
| 3226 | + if res.is_empty() { | ||
| 3227 | + continue; | ||
| 3228 | + } | ||
| 3229 | + for r in 0..4 { | ||
| 3230 | + let (sy, sx) = (ty * 4 + r, tx * 4); | ||
| 3231 | + if sy >= h { | ||
| 3232 | + continue; | ||
| 3233 | + } | ||
| 3234 | + for c in 0..4 { | ||
| 3235 | + if sx + c < w { | ||
| 3236 | + out[sy * w + sx + c] = res[r * 4 + c]; | ||
| 3237 | + } | ||
| 3238 | + } | ||
| 3239 | + } | ||
| 3240 | + } | ||
| 3241 | + } | ||
| 3242 | + out | ||
| 3243 | +} | ||
| 3244 | + | ||
| 3155 | #[cfg(test)] | 3245 | #[cfg(test)] |
| 3156 | mod lossless_tx_tests { | 3246 | mod lossless_tx_tests { |
| 3157 | /// The WHT is its own inverse up to the `UNIT_QUANT_SHIFT` scaling: a | 3247 | /// The WHT is its own inverse up to the `UNIT_QUANT_SHIFT` scaling: a |
| @@ -3216,6 +3306,133 @@ mod lossless_tx_tests { | |||
| 3216 | ); | 3306 | ); |
| 3217 | } | 3307 | } |
| 3218 | } | 3308 | } |
| 3309 | + | ||
| 3310 | + /// lane-whtshape: the shape-total lossless entry must be EXACTLY the | ||
| 3311 | + /// per-4x4 raster libaom's lossless geometry produces -- the same numbers | ||
| 3312 | + /// in the same places that a caller walking `(w/4) * (h/4)` real TX_4X4 | ||
| 3313 | + /// units would have written. Comparing against independently-composed | ||
| 3314 | + /// per-unit [`super::dequant_and_inverse_wht4x4`] calls is what makes the | ||
| 3315 | + /// split a total, not a guess. | ||
| 3316 | + #[test] | ||
| 3317 | + fn a_non_4x4_lossless_unit_is_the_raster_of_4x4_wht_units() { | ||
| 3318 | + let mut state = 0x0bad_c0deu32; | ||
| 3319 | + let mut next = || { | ||
| 3320 | + state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); | ||
| 3321 | + ((state >> 19) as i32 % 2001) - 1000 | ||
| 3322 | + }; | ||
| 3323 | + // A `w x h` coefficient grid is ROW-MAJOR at width `w`, so a 4x4 tile | ||
| 3324 | + // at `(tx * 4, ty * 4)` is a strided gather, not a contiguous run -- | ||
| 3325 | + // the reference below gathers it the same way the split does. | ||
| 3326 | + let tile_of = |levels: &[i32], w: usize, tx: usize, ty: usize| -> Vec<i32> { | ||
| 3327 | + let mut t = vec![0i32; 16]; | ||
| 3328 | + for r in 0..4 { | ||
| 3329 | + for c in 0..4 { | ||
| 3330 | + t[r * 4 + c] = levels[(ty * 4 + r) * w + tx * 4 + c]; | ||
| 3331 | + } | ||
| 3332 | + } | ||
| 3333 | + t | ||
| 3334 | + }; | ||
| 3335 | + for &(w, h) in &[(8usize, 4usize), (4, 8), (8, 8), (16, 4), (4, 16), (16, 16)] { | ||
| 3336 | + let levels: Vec<i32> = (0..w * h).map(|_| next()).collect(); | ||
| 3337 | + let got = super::dequant_and_inverse_wht(&levels, w, h, 10, 0, 0, 0); | ||
| 3338 | + let mut want = vec![0i32; w * h]; | ||
| 3339 | + for ty in 0..h / 4 { | ||
| 3340 | + for tx in 0..w / 4 { | ||
| 3341 | + let unit = super::dequant_and_inverse_wht4x4( | ||
| 3342 | + &tile_of(&levels, w, tx, ty), | ||
| 3343 | + 10, | ||
| 3344 | + 0, | ||
| 3345 | + 0, | ||
| 3346 | + 0, | ||
| 3347 | + ); | ||
| 3348 | + // An all-zero tile is the empty marker on both sides; the | ||
| 3349 | + // output is already zero there. | ||
| 3350 | + if unit.is_empty() { | ||
| 3351 | + continue; | ||
| 3352 | + } | ||
| 3353 | + for r in 0..4 { | ||
| 3354 | + let dst = (ty * 4 + r) * w + tx * 4; | ||
| 3355 | + want[dst..dst + 4].copy_from_slice(&unit[r * 4..][..4]); | ||
| 3356 | + } | ||
| 3357 | + } | ||
| 3358 | + } | ||
| 3359 | + assert_eq!(got.len(), w * h, "{w}x{h}: residual length"); | ||
| 3360 | + assert_eq!(got, want, "{w}x{h}: split differs from the 4x4 raster"); | ||
| 3361 | + } | ||
| 3362 | + } | ||
| 3363 | + | ||
| 3364 | + /// A grid whose non-zero coefficients sit in ONE tile: every other tile is | ||
| 3365 | + /// all zero, so it dequantizes to the empty marker rather than to 16 | ||
| 3366 | + /// zeros. Copying out of that marker is an index panic -- the split has to | ||
| 3367 | + /// leave those samples at zero, exactly as a per-unit caller would. | ||
| 3368 | + #[test] | ||
| 3369 | + fn a_lossless_split_whose_other_tiles_are_all_zero_still_reconstructs() { | ||
| 3370 | + // Row 0 columns 0, 1 and 2 of an 8-wide grid: all three land in the | ||
| 3371 | + // LEFT 4x4 tile, so the right tile is all zero and dequantizes to the | ||
| 3372 | + // empty marker rather than to 16 zeros. | ||
| 3373 | + let mut levels = vec![0i32; 8 * 4]; | ||
| 3374 | + levels[0] = 400; | ||
| 3375 | + levels[1] = -240; | ||
| 3376 | + levels[2] = 88; | ||
| 3377 | + let got = super::dequant_and_inverse_wht(&levels, 8, 4, 10, 0, 0, 0); | ||
| 3378 | + assert_eq!(got.len(), 32, "8x4 residual length"); | ||
| 3379 | + let mut want = vec![0i32; 32]; | ||
| 3380 | + for (tx, ty) in [(0usize, 0usize), (1, 0)] { | ||
| 3381 | + let mut t = vec![0i32; 16]; | ||
| 3382 | + for r in 0..4 { | ||
| 3383 | + for c in 0..4 { | ||
| 3384 | + t[r * 4 + c] = levels[(ty * 4 + r) * 8 + tx * 4 + c]; | ||
| 3385 | + } | ||
| 3386 | + } | ||
| 3387 | + let unit = super::dequant_and_inverse_wht4x4(&t, 10, 0, 0, 0); | ||
| 3388 | + if unit.is_empty() { | ||
| 3389 | + continue; | ||
| 3390 | + } | ||
| 3391 | + for r in 0..4 { | ||
| 3392 | + let dst = (ty * 4 + r) * 8 + tx * 4; | ||
| 3393 | + want[dst..dst + 4].copy_from_slice(&unit[r * 4..][..4]); | ||
| 3394 | + } | ||
| 3395 | + } | ||
| 3396 | + assert_eq!(got, want, "a partially-zero 8x4 lossless grid"); | ||
| 3397 | + assert!( | ||
| 3398 | + got.iter().any(|&v| v != 0), | ||
| 3399 | + "the fixture's non-zero tile produced an all-zero residual -- the arm is vacuous" | ||
| 3400 | + ); | ||
| 3401 | + } | ||
| 3402 | + | ||
| 3403 | + /// The 4x4 fast path is the same single call it has always been: a | ||
| 3404 | + /// lossless unit that IS 4x4 must produce byte-identical numbers with and | ||
| 3405 | + /// without the shape dispatch, so no currently-green lossless pin moves. | ||
| 3406 | + #[test] | ||
| 3407 | + fn the_4x4_lossless_fast_path_is_unchanged() { | ||
| 3408 | + let mut state = 0x5eed_1234u32; | ||
| 3409 | + for _ in 0..64 { | ||
| 3410 | + let levels: Vec<i32> = (0..16) | ||
| 3411 | + .map(|_| { | ||
| 3412 | + state = state.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); | ||
| 3413 | + ((state >> 19) as i32 % 2001) - 1000 | ||
| 3414 | + }) | ||
| 3415 | + .collect(); | ||
| 3416 | + assert_eq!( | ||
| 3417 | + super::dequant_and_inverse_wht(&levels, 4, 4, 8, 0, 0, 0), | ||
| 3418 | + super::dequant_and_inverse_wht4x4(&levels, 8, 0, 0, 0), | ||
| 3419 | + "levels={levels:?}" | ||
| 3420 | + ); | ||
| 3421 | + } | ||
| 3422 | + } | ||
| 3423 | + | ||
| 3424 | + /// The empty-grid marker contract is the one every residual caller | ||
| 3425 | + /// branches on (`TxParams::run` passes it straight through as "this unit | ||
| 3426 | + /// codes nothing"): a shape that is not 4x4 must keep it, or a skipped | ||
| 3427 | + /// 8x4 unit would hand its caller a 32-long zero buffer it then re-adds | ||
| 3428 | + /// onto the prediction twice. | ||
| 3429 | + #[test] | ||
| 3430 | + fn a_non_4x4_all_zero_lossless_unit_keeps_the_empty_marker() { | ||
| 3431 | + assert!( | ||
| 3432 | + super::dequant_and_inverse_wht(&[0i32; 32], 8, 4, 10, 0, 0, 0).is_empty(), | ||
| 3433 | + "an all-zero 8x4 lossless unit must stay the empty marker" | ||
| 3434 | + ); | ||
| 3435 | + } | ||
| 3219 | } | 3436 | } |
| 3220 | 3437 | ||
| 3221 | #[cfg(test)] | 3438 | #[cfg(test)] |