forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB5a2f5efTahinli3 months ago1409 commits

transform.rs361 lines, 11.4 KiBHistoryRawDownload
1//! Inverse transforms and scaling (spec 8.5.9 - 8.5.13).
2
3use crate::tables::{
4 NORM_ADJUST_4X4, NORM_ADJUST_8X8, NORM_ADJUST_CLASS_4X4, ZIGZAG_4X4, ZIGZAG_8X8,
5};
6
7/// Per-QP scaling factors for one 4x4 weight list: `LevelScale4x4(m, i, j) =
8/// weightScale(i, j) * normAdjust4x4(m, i, j)` (Equation 8-313), in raster
9/// order, for m = qP % 6.
10///
11/// Built once per parameter-set activation; the decode loop indexes it flat.
12#[derive(Debug, Clone)]
13pub struct LevelScale4x4 {
14 /// `[m][raster]`.
15 pub scale: [[i32; 16]; 6],
16}
17
18impl LevelScale4x4 {
19 /// Build from a raster-order weight list (flat 16s when no scaling matrix).
20 pub fn new(weight: &[u8; 16]) -> LevelScale4x4 {
21 let mut scale = [[0i32; 16]; 6];
22 for (m, row) in scale.iter_mut().enumerate() {
23 for (idx, s) in row.iter_mut().enumerate() {
24 let class = NORM_ADJUST_CLASS_4X4[idx] as usize;
25 *s = i32::from(weight[idx]) * i32::from(NORM_ADJUST_4X4[m][class]);
26 }
27 }
28 LevelScale4x4 { scale }
29 }
30}
31
32/// Scale AC/luma-4x4 coefficients in place (spec 8.5.12.1): `block` is in
33/// raster order. `skip_dc` is true for Intra_16x16 luma AC and chroma AC
34/// blocks whose DC came through the separate DC path (d00 = c00 untouched).
35#[inline]
36pub fn dequant_4x4(block: &mut [i32; 16], ls: &LevelScale4x4, qp: i32, skip_dc: bool) {
37 let m = (qp % 6) as usize;
38 let shift = qp / 6;
39 let scale = &ls.scale[m];
40 let start = usize::from(skip_dc);
41 if shift >= 4 {
42 let sh = (shift - 4) as u32;
43 for i in start..16 {
44 block[i] = (block[i] * scale[i]) << sh;
45 }
46 } else {
47 let sh = (4 - shift) as u32;
48 let round = 1i32 << (3 - shift);
49 for i in start..16 {
50 block[i] = (block[i] * scale[i] + round) >> sh;
51 }
52 }
53}
54
55/// Per-QP scaling factors for one 8x8 weight list: `LevelScale8x8(m, i, j) =
56/// weightScale8x8(i, j) * normAdjust8x8(m, i, j)` (Equation 8-316), in raster
57/// order, for m = qP % 6.
58#[derive(Debug, Clone)]
59pub struct LevelScale8x8 {
60 /// `[m][raster]`.
61 pub scale: [[i32; 64]; 6],
62}
63
64/// `normAdjust8x8` position class of raster position `(i, j)` (Equation 8-317),
65/// where `i` is the row and `j` the column.
66const fn norm_class_8x8(i: usize, j: usize) -> usize {
67 let (i4, j4, i2, j2) = (i % 4, j % 4, i % 2, j % 2);
68 if i4 == 0 && j4 == 0 {
69 0
70 } else if i2 == 1 && j2 == 1 {
71 1
72 } else if i4 == 2 && j4 == 2 {
73 2
74 } else if (i4 == 0 && j2 == 1) || (i2 == 1 && j4 == 0) {
75 3
76 } else if (i4 == 0 && j4 == 2) || (i4 == 2 && j4 == 0) {
77 4
78 } else {
79 5
80 }
81}
82
83impl LevelScale8x8 {
84 /// Build from a raster-order 8x8 weight list (flat 16s when no scaling
85 /// matrix is in force).
86 pub fn new(weight: &[u8; 64]) -> LevelScale8x8 {
87 let mut scale = [[0i32; 64]; 6];
88 for (m, row) in scale.iter_mut().enumerate() {
89 for (idx, s) in row.iter_mut().enumerate() {
90 let class = norm_class_8x8(idx / 8, idx % 8);
91 *s = i32::from(weight[idx]) * i32::from(NORM_ADJUST_8X8[m][class]);
92 }
93 }
94 LevelScale8x8 { scale }
95 }
96}
97
98/// Scale an 8x8 luma block in place (spec 8.5.13.1), raster order.
99#[inline]
100pub fn dequant_8x8(block: &mut [i32; 64], ls: &LevelScale8x8, qp: i32) {
101 let scale = &ls.scale[(qp % 6) as usize];
102 if qp >= 36 {
103 let sh = (qp / 6 - 6) as u32;
104 for i in 0..64 {
105 block[i] = (block[i] * scale[i]) << sh;
106 }
107 } else {
108 let sh = (6 - qp / 6) as u32;
109 let round = 1i32 << (5 - qp / 6);
110 for i in 0..64 {
111 block[i] = (block[i] * scale[i] + round) >> sh;
112 }
113 }
114}
115
116/// Place scan-order coefficients of an 8x8 block into raster order using the
117/// 8x8 zig-zag (Table 8-14).
118#[inline]
119pub fn unzigzag_8x8(scan: &[i32; 64], out: &mut [i32; 64]) {
120 for (s, &raster) in ZIGZAG_8X8.iter().enumerate() {
121 out[raster as usize] = scan[s];
122 }
123}
124
125/// One pass of the 8x8 inverse core transform (Equations 8-358 to 8-381) over
126/// eight values.
127#[inline]
128fn idct8_1d(d: [i32; 8]) -> [i32; 8] {
129 let e = [
130 d[0] + d[4],
131 -d[3] + d[5] - d[7] - (d[7] >> 1),
132 d[0] - d[4],
133 d[1] + d[7] - d[3] - (d[3] >> 1),
134 (d[2] >> 1) - d[6],
135 -d[1] + d[7] + d[5] + (d[5] >> 1),
136 d[2] + (d[6] >> 1),
137 d[3] + d[5] + d[1] + (d[1] >> 1),
138 ];
139 let f = [
140 e[0] + e[6],
141 e[1] + (e[7] >> 2),
142 e[2] + e[4],
143 e[3] + (e[5] >> 2),
144 e[2] - e[4],
145 (e[3] >> 2) - e[5],
146 e[0] - e[6],
147 e[7] - (e[1] >> 2),
148 ];
149 [
150 f[0] + f[7],
151 f[2] + f[5],
152 f[4] + f[3],
153 f[6] + f[1],
154 f[6] - f[1],
155 f[4] - f[3],
156 f[2] - f[5],
157 f[0] - f[7],
158 ]
159}
160
161/// Inverse 8x8 core transform (spec 8.5.13.2): raster-order input `d`, output
162/// residuals `r` including the final `(x + 32) >> 6`.
163pub fn inverse_transform_8x8(d: &[i32; 64], r: &mut [i32; 64]) {
164 let mut g = [0i32; 64];
165 for i in 0..8 {
166 let o = i * 8;
167 let row: [i32; 8] = d[o..o + 8].try_into().expect("eight coefficients");
168 g[o..o + 8].copy_from_slice(&idct8_1d(row));
169 }
170 for j in 0..8 {
171 let col = [
172 g[j],
173 g[8 + j],
174 g[16 + j],
175 g[24 + j],
176 g[32 + j],
177 g[40 + j],
178 g[48 + j],
179 g[56 + j],
180 ];
181 let m = idct8_1d(col);
182 for (i, &v) in m.iter().enumerate() {
183 r[i * 8 + j] = (v + 32) >> 6;
184 }
185 }
186}
187
188/// Place scan-order coefficients into raster order using the 4x4 zig-zag.
189#[inline]
190pub fn unzigzag(scan: &[i32; 16], out: &mut [i32; 16]) {
191 for (s, &raster) in ZIGZAG_4X4.iter().enumerate() {
192 out[raster as usize] = scan[s];
193 }
194}
195
196/// Un-zigzag a 15-coefficient AC block (Intra_16x16 luma AC / chroma AC):
197/// scan position `k` maps to zig-zag position `k + 1`; the DC slot is
198/// cleared for the caller to fill from the separate DC path.
199#[inline]
200pub fn unzigzag_ac15(scan: &[i32; 16], out: &mut [i32; 16]) {
201 out[0] = 0;
202 for k in 0..15 {
203 out[ZIGZAG_4X4[k + 1] as usize] = scan[k];
204 }
205}
206
207/// Inverse 4x4 core transform (spec 8.5.12.2): raster-order input `d`,
208/// output residuals `r` including the final `(x + 32) >> 6`.
209#[inline]
210pub fn inverse_transform_4x4(d: &[i32; 16], r: &mut [i32; 16]) {
211 // Horizontal (each row).
212 let mut e = [0i32; 16];
213 for i in 0..4 {
214 let o = i * 4;
215 let (d0, d1, d2, d3) = (d[o], d[o + 1], d[o + 2], d[o + 3]);
216 let e0 = d0 + d2;
217 let e1 = d0 - d2;
218 let e2 = (d1 >> 1) - d3;
219 let e3 = d1 + (d3 >> 1);
220 e[o] = e0 + e3;
221 e[o + 1] = e1 + e2;
222 e[o + 2] = e1 - e2;
223 e[o + 3] = e0 - e3;
224 }
225 // Vertical (each column) + rounding.
226 for j in 0..4 {
227 let (g0, g1, g2, g3) = (e[j], e[4 + j], e[8 + j], e[12 + j]);
228 let h0 = g0 + g2;
229 let h1 = g0 - g2;
230 let h2 = (g1 >> 1) - g3;
231 let h3 = g1 + (g3 >> 1);
232 r[j] = (h0 + h3 + 32) >> 6;
233 r[4 + j] = (h1 + h2 + 32) >> 6;
234 r[8 + j] = (h1 - h2 + 32) >> 6;
235 r[12 + j] = (h0 - h3 + 32) >> 6;
236 }
237}
238
239/// Intra_16x16 luma DC: 4x4 Hadamard + scaling (spec 8.5.10). `c` is the DC
240/// coefficient array in raster order (dc of block (bx, by) at `by * 4 + bx`);
241/// output overwrites it.
242pub fn luma_dc_transform(c: &mut [i32; 16], ls: &LevelScale4x4, qp: i32) {
243 // Hadamard rows then columns (Equation 8-320).
244 let mut f = [0i32; 16];
245 for i in 0..4 {
246 let o = i * 4;
247 let (a, b, cc, d) = (c[o], c[o + 1], c[o + 2], c[o + 3]);
248 f[o] = a + b + cc + d;
249 f[o + 1] = a + b - cc - d;
250 f[o + 2] = a - b - cc + d;
251 f[o + 3] = a - b + cc - d;
252 }
253 let mut g = [0i32; 16];
254 for j in 0..4 {
255 let (a, b, cc, d) = (f[j], f[4 + j], f[8 + j], f[12 + j]);
256 g[j] = a + b + cc + d;
257 g[4 + j] = a + b - cc - d;
258 g[8 + j] = a - b - cc + d;
259 g[12 + j] = a - b + cc - d;
260 }
261 let scale = ls.scale[(qp % 6) as usize][0];
262 if qp >= 36 {
263 let sh = (qp / 6 - 6) as u32;
264 for (o, &v) in c.iter_mut().zip(&g) {
265 *o = (v * scale) << sh;
266 }
267 } else {
268 let sh = (6 - qp / 6) as u32;
269 let round = 1i32 << (5 - qp / 6);
270 for (o, &v) in c.iter_mut().zip(&g) {
271 *o = (v * scale + round) >> sh;
272 }
273 }
274}
275
276/// Chroma DC for 4:2:0: 2x2 Hadamard + scaling (spec 8.5.11). `c` holds the
277/// four DC levels in raster order of the chroma 4x4 blocks; overwritten with
278/// the scaled DC values.
279pub fn chroma_dc_transform_420(c: &mut [i32; 4], ls: &LevelScale4x4, qp: i32) {
280 let (c00, c01, c10, c11) = (c[0], c[1], c[2], c[3]);
281 let f = [
282 c00 + c01 + c10 + c11,
283 c00 - c01 + c10 - c11,
284 c00 + c01 - c10 - c11,
285 c00 - c01 - c10 + c11,
286 ];
287 let scale = ls.scale[(qp % 6) as usize][0];
288 let sh = (qp / 6) as u32;
289 for (o, &v) in c.iter_mut().zip(&f) {
290 *o = ((v * scale) << sh) >> 5;
291 }
292}
293
294#[cfg(test)]
295mod tests {
296 use super::*;
297
298 fn flat_ls() -> LevelScale4x4 {
299 LevelScale4x4::new(&[16; 16])
300 }
301
302 /// A pure DC level reconstructs to a flat block whose value follows the
303 /// dequant ladder: doubling every 6 QP steps.
304 #[test]
305 fn dc_only_block_flat_and_qp_ladder() {
306 let ls = flat_ls();
307 let recon = |qp: i32, level: i32| -> i32 {
308 let mut block = [0i32; 16];
309 block[0] = level;
310 dequant_4x4(&mut block, &ls, qp, false);
311 let mut r = [0i32; 16];
312 inverse_transform_4x4(&block.clone(), &mut r);
313 assert!(r.iter().all(|&x| x == r[0]), "DC block must be flat");
314 r[0]
315 };
316 // LevelScale(m = 0, 0, 0) = 16 * 10 = 160. qp 24: d00 = 160 << 0.
317 // A DC-only block comes out flat at (d00 + 32) >> 6.
318 assert_eq!(recon(24, 1), (160 + 32) >> 6);
319 assert_eq!(recon(30, 1), (320 + 32) >> 6);
320 assert_eq!(recon(36, 1), (640 + 32) >> 6);
321 assert_eq!(recon(24, -3), (-480 + 32) >> 6); // floor shift: -7
322 }
323
324 /// The inverse transform of zeros is zero, and rounding is symmetric
325 /// enough that a lone +1/-1 AC level stays bounded.
326 #[test]
327 fn zero_in_zero_out_and_unzigzag() {
328 let mut r = [7i32; 16];
329 inverse_transform_4x4(&[0; 16], &mut r);
330 assert_eq!(r, [0; 16]);
331
332 let mut scan = [0i32; 16];
333 scan[1] = 5; // second scan position = raster (0,1)
334 let mut raster = [0i32; 16];
335 unzigzag(&scan, &mut raster);
336 assert_eq!(raster[1], 5);
337 assert_eq!(raster.iter().filter(|&&x| x != 0).count(), 1);
338 }
339
340 /// Luma DC Hadamard of a single coefficient spreads evenly; scaling per
341 /// 8.5.10 with qp < 36 uses the rounding shift.
342 #[test]
343 fn luma_dc_spread() {
344 let ls = flat_ls();
345 let mut c = [0i32; 16];
346 c[0] = 8;
347 luma_dc_transform(&mut c, &ls, 28);
348 // f = 8 everywhere; scale = LevelScale(28 % 6 = 4, 0, 0) = 16 * 16.
349 let expected = (8 * 16 * 16 + (1 << (5 - 28 / 6))) >> (6 - 28 / 6);
350 assert!(c.iter().all(|&x| x == expected));
351 }
352
353 #[test]
354 fn chroma_dc_hadamard() {
355 let ls = flat_ls();
356 let mut c = [4, 0, 0, 0];
357 chroma_dc_transform_420(&mut c, &ls, 30);
358 // f = [4,4,4,4]; scale = LS(0,0,0) = 160; ((4 * 160) << 5) >> 5 = 640.
359 assert!(c.iter().all(|&x| x == 640), "{c:?}");
360 }
361}