forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

mod.rs165 lines, 7.1 KiBHistoryRawDownload
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`).
38pub 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`.
41pub 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#[derive(Clone, Copy)]
47pub struct Iqm {
48 /// The matrix entries, in libaom's transposed position order.
49 pub data: &'static [u8],
50 /// The column stride of libaom's position space: the active-area
51 /// height.
52 pub stride: usize,
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.
57fn adjusted(w: usize, h: usize) -> (usize, usize) {
58 match (w, h) {
59 (64, 64) | (64, 32) | (32, 64) => (32, 32),
60 (64, 16) => (32, 16),
61 (16, 64) => (16, 32),
62 _ => (w, h),
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).
70fn shape_range(w: usize, h: usize) -> Option<(usize, usize)> {
71 match (w, h) {
72 (4, 4) => Some((0, 16)),
73 (8, 8) => Some((16, 64)),
74 (16, 16) => Some((80, 256)),
75 (32, 32) => Some((336, 1024)),
76 (4, 8) => Some((1360, 32)),
77 (8, 4) => Some((1392, 32)),
78 (8, 16) => Some((1424, 128)),
79 (16, 8) => Some((1552, 128)),
80 (16, 32) => Some((1680, 512)),
81 (32, 16) => Some((2192, 512)),
82 (4, 16) => Some((2704, 64)),
83 (16, 4) => Some((2768, 64)),
84 (8, 32) => Some((2832, 256)),
85 (32, 8) => Some((3088, 256)),
86 _ => None,
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.
94pub fn iwt_matrix(level: usize, chroma: bool, w: usize, h: usize) -> Option<Iqm> {
95 if level >= NUM_QM_LEVELS - 1 {
96 return None;
97 }
98 let (sw, sh) = adjusted(w, h);
99 let (off, len) = shape_range(sw, sh)?;
100 Some(Iqm {
101 data: &IWT[level][usize::from(chroma)][off..off + len],
102 stride: sh,
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#[inline]
119pub fn dqv_at(qm: &Iqm, pos: usize, w: usize, dqv: i64) -> i64 {
120 let (row, col) = (pos / w, pos % w);
121 match qm.data.get(col * qm.stride + row) {
122 Some(&m) => ((i64::from(m) * dqv) + (1 << (AOM_QM_BITS - 1))) >> AOM_QM_BITS,
123 // libaom's `av1_get_max_eob` caps eob at 1024 for the clamped 64
124 // shapes, so a NONZERO level never reaches a position past the
125 // reused matrix -- `dequant_wh_into`'s zero-level skip guards this
126 // arm and it is unreachable on spec streams. Flat (dqv) if ever
127 // reached.
128 None => dqv,
129 }
130}
131
132mod iwt_a;
133
134mod iwt_b;
135
136mod iwt_c;
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#[rustfmt::skip]
146static IWT: [[[u8; 3344]; 2]; 15] = [
147 iwt_a::IWT_L0,
148 iwt_a::IWT_L1,
149 iwt_a::IWT_L2,
150 iwt_a::IWT_L3,
151 iwt_a::IWT_L4,
152 iwt_b::IWT_L5,
153 iwt_b::IWT_L6,
154 iwt_b::IWT_L7,
155 iwt_b::IWT_L8,
156 iwt_b::IWT_L9,
157 iwt_c::IWT_L10,
158 iwt_c::IWT_L11,
159 iwt_c::IWT_L12,
160 iwt_c::IWT_L13,
161 iwt_c::IWT_L14,
162];
163
164#[cfg(test)]
165mod tests;