edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits
| 1 | //! AV1 inverse quantisation matrices: spec 5.9.12's `using_qmatrix` + |
| 2 | //! `qm_y`/`qm_u`/`qm_v` and the per-position `dqv` scale of spec 7.12.3, |
| 3 | //! ported bug-for-bug from libaom (`av1/common/quant_common.c`'s |
| 4 | //! `iwt_matrix_ref` + `av1_qm_init` + `av1_get_iqmatrix`, |
| 5 | //! `av1/decoder/decodetxb.c`'s `get_dqv`). lane-av1-qmatrix. |
| 6 | //! |
| 7 | //! The shape of the port, and why each piece looks like it does: |
| 8 | //! |
| 9 | //! - The parsed 4-bit `qm_y`/`qm_u`/`qm_v` ARE the table level (libaom |
| 10 | //! `quant_params->qmatrix_level_y/u/v`, `decodeframe.c:1902-1917`). The |
| 11 | //! `aom_get_qmlevel(qindex, ...)` derivation that maps base_q_idx onto a |
| 12 | //! level is ENCODER-side only (how aomenc picks what to write); a decoder |
| 13 | //! never sees it. |
| 14 | //! - Level 15 is libaom's flat sentinel: `av1_qm_init` stores NULL pointers |
| 15 | //! for `NUM_QM_LEVELS - 1`, and 1D/identity transforms read that NULL slot |
| 16 | //! unconditionally (`av1_get_iqmatrix`'s `is_2d_transform` branch), so |
| 17 | //! `V_DCT`/`H_DCT`/`IDTX`-class units never scale their `dqv`. Both facts |
| 18 | //! land here as "return [`Iqm`] or `None`", and `None` dequantises exactly |
| 19 | //! as before this module existed. |
| 20 | //! - Per `av1_use_qmatrix` (`setup_segmentation_dequant`), a LOSSLESS |
| 21 | //! segment (quantizer index clamped to 0 with all five deltas zero) also |
| 22 | //! dequantises flat; the decode-side caller checks that per block through |
| 23 | //! the same quantizer index every dequant call site already uses. |
| 24 | //! - The tables are per plane KIND, not just per level: `iwt_matrix_ref` |
| 25 | //! is `[level][luma/chroma][...]` and chroma matrices are genuinely |
| 26 | //! different data (`av1_qm_init`'s `&iwt_matrix_ref[q][c >= 1][..]`). |
| 27 | //! - libaom stores one flat 3344-entry buffer per (level, kind); the |
| 28 | //! 14 own-storage shapes live at [`shape_range`]'s offsets in |
| 29 | //! `av1_qm_init`'s `TX_SIZES_ALL` walk order (the C file's own |
| 30 | //! `/* Size ... */` comments mark that same order), and the 64-axis |
| 31 | //! shapes REUSE a smaller shape's matrix (`av1_get_adjusted_tx_size`): |
| 32 | //! 64x64/64x32/32x64 point at the 32x32 matrix, 64x16 at 32x16, 16x64 at |
| 33 | //! 16x32. libaom indexes those reuses through its ACTIVE-AREA coeff |
| 34 | //! buffer (whose eob `av1_get_max_eob` caps at 1024), which is why |
| 35 | //! [`dqv_at`] re-strides our true-w raster onto `stride`. |
| 36 | |
| 37 | /// libaom `NUM_QM_LEVELS` (`quant_common.h`: `1 << QM_LEVEL_BITS`). |
| 38 | pub const NUM_QM_LEVELS: usize = 16; |
| 39 | /// libaom `AOM_QM_BITS` (`aom_dsp/aom_dsp_common.h`): the fixed-point |
| 40 | /// shift of the matrix multiply, `(qm * dqv + 16) >> 5`. |
| 41 | pub const AOM_QM_BITS: u32 = 5; |
| 42 | |
| 43 | /// One selected inverse matrix: libaom's `const qm_val_t *` plus the |
| 44 | /// stride that pointer runs at -- the ACTIVE-AREA HEIGHT, because libaom's |
| 45 | /// coefficient positions are COLUMN-major ([`dqv_at`]). |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | /// `av1_get_adjusted_tx_size` (`av1/common/blockd.h:1361`): the matrix to |
| 56 | /// use for a transform whose true shape stores no matrix of its own. |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | /// `[av1_qm_init]`'s flat-buffer offsets for the 14 own-storage shapes, in |
| 67 | /// its `TX_SIZES_ALL` walk order. `(offset, length)` into one (level, kind) |
| 68 | /// slice; `None` for a shape that stores no matrix (the 64-axis clamps land |
| 69 | /// here only if `adjusted` ever misses one -- it does not). |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | /// `av1_get_iqmatrix` narrowed to the decode path: the matrix for transform |
| 91 | /// `(w, h)` at `level` (0..=14) of plane kind (`chroma` = planes 1/2), or |
| 92 | /// `None` for libaom's NULL matrix. Level >= 15 is the flat sentinel, so a |
| 93 | /// qm-off frame (levels parked at 15) never selects a matrix. |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | /// `get_dqv` (`av1/decoder/decodetxb.c:54-60`): the DC/AC quantizer scaled |
| 107 | /// by the coefficient's own matrix entry, `((m * dqv) + 16) >> 5`. The |
| 108 | /// subtle half is the index: libaom's scan positions -- and so |
| 109 | /// `iqmatrix[coeff_idx]` and its whole txb coefficient buffer -- live in a |
| 110 | /// COLUMN-major position space (its `mcol_scan_8x4` is the identity |
| 111 | /// `0,1,2,...`: position `p` strides DOWN the columns of the `h`-tall |
| 112 | /// transposed block), so the coefficient of our row-major `w`-wide grid at |
| 113 | /// `(row, col)` reads matrix entry `col * stride + row`. Verified |
| 114 | /// coefficient-for-coefficient against the instrumented oracle on the |
| 115 | /// `--enable-qm=1` repro stream -- including 8x8 units, whose stored |
| 116 | /// matrices are NOT symmetric, so the transposition is observable there |
| 117 | /// and only this mapping matches. |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | // GENERATED from libaom's `iwt_matrix_ref[NUM_QM_LEVELS - 1][2][QM_TOTAL_SIZE]` |
| 139 | // (`av1/common/quant_common.c`, oracle at ~/.cache/aom-oracle) -- verbatim values. |
| 140 | // 15 levels x [luma, chroma] x 3344 flat entries; per (level, kind) the 14 |
| 141 | // own-storage shapes follow `av1_qm_init`'s `TX_SIZES_ALL` walk order (the |
| 142 | // 64-variants reuse smaller shapes -- see `SHAPE_OFFSET`), which is also the |
| 143 | // order of the `/* Size ... */` comment markers in the C file itself. |
| 144 | // Level 15 is absent by construction: libaom stores NULL (flat) for it. |
| 145 | |
| 146 | static IWT: = |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | ; |
| 163 | |
| 164 | |
| 165 |