forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

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