forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB30d372eTahinli3 months ago42 commits

transform.rs226 lines, 7.6 KiBHistoryRawDownload
1//! Inverse transforms and scaling (spec 8.5.9 - 8.5.12).
2
3use crate::tables::{NORM_ADJUST_4X4, NORM_ADJUST_CLASS_4X4, ZIGZAG_4X4};
4
5/// Per-QP scaling factors for one 4x4 weight list: `LevelScale4x4(m, i, j) =
6/// weightScale(i, j) * normAdjust4x4(m, i, j)` (Equation 8-313), in raster
7/// order, for m = qP % 6.
8///
9/// Built once per parameter-set activation; the decode loop indexes it flat.
10#[derive(Debug, Clone)]
11pub struct LevelScale4x4 {
12 /// `[m][raster]`.
13 pub scale: [[i32; 16]; 6],
14}
15
16impl LevelScale4x4 {
17 /// Build from a raster-order weight list (flat 16s when no scaling matrix).
18 pub fn new(weight: &[u8; 16]) -> LevelScale4x4 {
19 let mut scale = [[0i32; 16]; 6];
20 for (m, row) in scale.iter_mut().enumerate() {
21 for (idx, s) in row.iter_mut().enumerate() {
22 let class = NORM_ADJUST_CLASS_4X4[idx] as usize;
23 *s = i32::from(weight[idx]) * i32::from(NORM_ADJUST_4X4[m][class]);
24 }
25 }
26 LevelScale4x4 { scale }
27 }
28}
29
30/// Scale AC/luma-4x4 coefficients in place (spec 8.5.12.1): `block` is in
31/// raster order. `skip_dc` is true for Intra_16x16 luma AC and chroma AC
32/// blocks whose DC came through the separate DC path (d00 = c00 untouched).
33#[inline]
34pub fn dequant_4x4(block: &mut [i32; 16], ls: &LevelScale4x4, qp: i32, skip_dc: bool) {
35 let m = (qp % 6) as usize;
36 let shift = qp / 6;
37 let scale = &ls.scale[m];
38 let start = usize::from(skip_dc);
39 if shift >= 4 {
40 let sh = (shift - 4) as u32;
41 for i in start..16 {
42 block[i] = (block[i] * scale[i]) << sh;
43 }
44 } else {
45 let sh = (4 - shift) as u32;
46 let round = 1i32 << (3 - shift);
47 for i in start..16 {
48 block[i] = (block[i] * scale[i] + round) >> sh;
49 }
50 }
51}
52
53/// Place scan-order coefficients into raster order using the 4x4 zig-zag.
54#[inline]
55pub fn unzigzag(scan: &[i32; 16], out: &mut [i32; 16]) {
56 for (s, &raster) in ZIGZAG_4X4.iter().enumerate() {
57 out[raster as usize] = scan[s];
58 }
59}
60
61/// Un-zigzag a 15-coefficient AC block (Intra_16x16 luma AC / chroma AC):
62/// scan position `k` maps to zig-zag position `k + 1`; the DC slot is
63/// cleared for the caller to fill from the separate DC path.
64#[inline]
65pub fn unzigzag_ac15(scan: &[i32; 16], out: &mut [i32; 16]) {
66 out[0] = 0;
67 for k in 0..15 {
68 out[ZIGZAG_4X4[k + 1] as usize] = scan[k];
69 }
70}
71
72/// Inverse 4x4 core transform (spec 8.5.12.2): raster-order input `d`,
73/// output residuals `r` including the final `(x + 32) >> 6`.
74#[inline]
75pub fn inverse_transform_4x4(d: &[i32; 16], r: &mut [i32; 16]) {
76 // Horizontal (each row).
77 let mut e = [0i32; 16];
78 for i in 0..4 {
79 let o = i * 4;
80 let (d0, d1, d2, d3) = (d[o], d[o + 1], d[o + 2], d[o + 3]);
81 let e0 = d0 + d2;
82 let e1 = d0 - d2;
83 let e2 = (d1 >> 1) - d3;
84 let e3 = d1 + (d3 >> 1);
85 e[o] = e0 + e3;
86 e[o + 1] = e1 + e2;
87 e[o + 2] = e1 - e2;
88 e[o + 3] = e0 - e3;
89 }
90 // Vertical (each column) + rounding.
91 for j in 0..4 {
92 let (g0, g1, g2, g3) = (e[j], e[4 + j], e[8 + j], e[12 + j]);
93 let h0 = g0 + g2;
94 let h1 = g0 - g2;
95 let h2 = (g1 >> 1) - g3;
96 let h3 = g1 + (g3 >> 1);
97 r[j] = (h0 + h3 + 32) >> 6;
98 r[4 + j] = (h1 + h2 + 32) >> 6;
99 r[8 + j] = (h1 - h2 + 32) >> 6;
100 r[12 + j] = (h0 - h3 + 32) >> 6;
101 }
102}
103
104/// Intra_16x16 luma DC: 4x4 Hadamard + scaling (spec 8.5.10). `c` is the DC
105/// coefficient array in raster order (dc of block (bx, by) at `by * 4 + bx`);
106/// output overwrites it.
107pub fn luma_dc_transform(c: &mut [i32; 16], ls: &LevelScale4x4, qp: i32) {
108 // Hadamard rows then columns (Equation 8-320).
109 let mut f = [0i32; 16];
110 for i in 0..4 {
111 let o = i * 4;
112 let (a, b, cc, d) = (c[o], c[o + 1], c[o + 2], c[o + 3]);
113 f[o] = a + b + cc + d;
114 f[o + 1] = a + b - cc - d;
115 f[o + 2] = a - b - cc + d;
116 f[o + 3] = a - b + cc - d;
117 }
118 let mut g = [0i32; 16];
119 for j in 0..4 {
120 let (a, b, cc, d) = (f[j], f[4 + j], f[8 + j], f[12 + j]);
121 g[j] = a + b + cc + d;
122 g[4 + j] = a + b - cc - d;
123 g[8 + j] = a - b - cc + d;
124 g[12 + j] = a - b + cc - d;
125 }
126 let scale = ls.scale[(qp % 6) as usize][0];
127 if qp >= 36 {
128 let sh = (qp / 6 - 6) as u32;
129 for (o, &v) in c.iter_mut().zip(&g) {
130 *o = (v * scale) << sh;
131 }
132 } else {
133 let sh = (6 - qp / 6) as u32;
134 let round = 1i32 << (5 - qp / 6);
135 for (o, &v) in c.iter_mut().zip(&g) {
136 *o = (v * scale + round) >> sh;
137 }
138 }
139}
140
141/// Chroma DC for 4:2:0: 2x2 Hadamard + scaling (spec 8.5.11). `c` holds the
142/// four DC levels in raster order of the chroma 4x4 blocks; overwritten with
143/// the scaled DC values.
144pub fn chroma_dc_transform_420(c: &mut [i32; 4], ls: &LevelScale4x4, qp: i32) {
145 let (c00, c01, c10, c11) = (c[0], c[1], c[2], c[3]);
146 let f = [
147 c00 + c01 + c10 + c11,
148 c00 - c01 + c10 - c11,
149 c00 + c01 - c10 - c11,
150 c00 - c01 - c10 + c11,
151 ];
152 let scale = ls.scale[(qp % 6) as usize][0];
153 let sh = (qp / 6) as u32;
154 for (o, &v) in c.iter_mut().zip(&f) {
155 *o = ((v * scale) << sh) >> 5;
156 }
157}
158
159#[cfg(test)]
160mod tests {
161 use super::*;
162
163 fn flat_ls() -> LevelScale4x4 {
164 LevelScale4x4::new(&[16; 16])
165 }
166
167 /// A pure DC level reconstructs to a flat block whose value follows the
168 /// dequant ladder: doubling every 6 QP steps.
169 #[test]
170 fn dc_only_block_flat_and_qp_ladder() {
171 let ls = flat_ls();
172 let recon = |qp: i32, level: i32| -> i32 {
173 let mut block = [0i32; 16];
174 block[0] = level;
175 dequant_4x4(&mut block, &ls, qp, false);
176 let mut r = [0i32; 16];
177 inverse_transform_4x4(&block.clone(), &mut r);
178 assert!(r.iter().all(|&x| x == r[0]), "DC block must be flat");
179 r[0]
180 };
181 // LevelScale(m = 0, 0, 0) = 16 * 10 = 160. qp 24: d00 = 160 << 0.
182 // A DC-only block comes out flat at (d00 + 32) >> 6.
183 assert_eq!(recon(24, 1), (160 + 32) >> 6);
184 assert_eq!(recon(30, 1), (320 + 32) >> 6);
185 assert_eq!(recon(36, 1), (640 + 32) >> 6);
186 assert_eq!(recon(24, -3), (-480 + 32) >> 6); // floor shift: -7
187 }
188
189 /// The inverse transform of zeros is zero, and rounding is symmetric
190 /// enough that a lone +1/-1 AC level stays bounded.
191 #[test]
192 fn zero_in_zero_out_and_unzigzag() {
193 let mut r = [7i32; 16];
194 inverse_transform_4x4(&[0; 16], &mut r);
195 assert_eq!(r, [0; 16]);
196
197 let mut scan = [0i32; 16];
198 scan[1] = 5; // second scan position = raster (0,1)
199 let mut raster = [0i32; 16];
200 unzigzag(&scan, &mut raster);
201 assert_eq!(raster[1], 5);
202 assert_eq!(raster.iter().filter(|&&x| x != 0).count(), 1);
203 }
204
205 /// Luma DC Hadamard of a single coefficient spreads evenly; scaling per
206 /// 8.5.10 with qp < 36 uses the rounding shift.
207 #[test]
208 fn luma_dc_spread() {
209 let ls = flat_ls();
210 let mut c = [0i32; 16];
211 c[0] = 8;
212 luma_dc_transform(&mut c, &ls, 28);
213 // f = 8 everywhere; scale = LevelScale(28 % 6 = 4, 0, 0) = 16 * 16.
214 let expected = (8 * 16 * 16 + (1 << (5 - 28 / 6))) >> (6 - 28 / 6);
215 assert!(c.iter().all(|&x| x == expected));
216 }
217
218 #[test]
219 fn chroma_dc_hadamard() {
220 let ls = flat_ls();
221 let mut c = [4, 0, 0, 0];
222 chroma_dc_transform_420(&mut c, &ls, 30);
223 // f = [4,4,4,4]; scale = LS(0,0,0) = 160; ((4 * 160) << 5) >> 5 = 640.
224 assert!(c.iter().all(|&x| x == 640), "{c:?}");
225 }
226}