forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB70849e0Tahinli2 months ago3122 commits

transform.rs772 lines, 26.8 KiBHistoryRawDownload
1//! Integer inverse transforms (spec 8.5.3; the libvpx `inv_txfm.c`
2//! butterflies, rounding and shifts transcribed exactly). Release-build
3//! `WRAPLOW` is an identity, so intermediates stay `i32`; only the final
4//! add clamps.
5
6use crate::tables::*;
7
8#[inline]
9fn dcrs(x: i64) -> i32 {
10 ((x + (1 << 13)) >> 14) as i32
11}
12
13const C1: i64 = 16364;
14const C2: i64 = 16305;
15const C3: i64 = 16207;
16const C4: i64 = 16069;
17const C5: i64 = 15893;
18const C6: i64 = 15679;
19const C7: i64 = 15426;
20const C8: i64 = 15137;
21const C9: i64 = 14811;
22const C10: i64 = 14449;
23const C11: i64 = 14053;
24const C12: i64 = 13623;
25const C13: i64 = 13160;
26const C14: i64 = 12665;
27const C15: i64 = 12140;
28const C16: i64 = 11585;
29const C17: i64 = 11003;
30const C18: i64 = 10394;
31const C19: i64 = 9760;
32const C20: i64 = 9102;
33const C21: i64 = 8423;
34const C22: i64 = 7723;
35const C23: i64 = 7005;
36const C24: i64 = 6270;
37const C25: i64 = 5520;
38const C26: i64 = 4756;
39const C27: i64 = 3981;
40const C28: i64 = 3196;
41const C29: i64 = 2404;
42const C30: i64 = 1606;
43const C31: i64 = 804;
44const S1: i64 = 5283;
45const S2: i64 = 9929;
46const S3: i64 = 13377;
47const S4: i64 = 15212;
48
49use crate::Sample;
50
51/// `detect_invalid_highbd_input` (`vpx_dsp/inv_txfm.c:1284`): a 1-D highbitdepth
52/// transform whose input holds a magnitude `>= 1 << 25` emits all zeros instead
53/// of reconstructing. Unreachable for valid streams; ported for fidelity.
54#[inline]
55fn invalid_highbd_input(input: &[i32], bd: u8) -> bool {
56 bd != 8 && input.iter().any(|&v| v.unsigned_abs() >= (1u32 << 25))
57}
58
59/// `clip_pixel_add` / `highbd_clip_pixel_add`.
60#[inline]
61pub(crate) fn clip_add(d: Sample, v: i32, bd: u8) -> Sample {
62 crate::clip_pixel_bd(d as i32 + v, bd)
63}
64
65/// vpx_iwht4x4_16_add_c (lossless).
66pub(crate) fn iwht4x4_add(input: &[i32], dest: &mut [Sample], stride: usize, bd: u8) {
67 let mut output = [0i32; 16];
68 for i in 0..4 {
69 let ip = &input[i * 4..i * 4 + 4];
70 let mut a1 = ip[0] >> 2;
71 let mut c1 = ip[1] >> 2;
72 let mut d1 = ip[2] >> 2;
73 let mut b1 = ip[3] >> 2;
74 a1 += c1;
75 d1 -= b1;
76 let e1 = (a1 - d1) >> 1;
77 b1 = e1 - b1;
78 c1 = e1 - c1;
79 a1 -= b1;
80 d1 += c1;
81 output[i * 4..i * 4 + 4].copy_from_slice(&[a1, b1, c1, d1]);
82 }
83 for i in 0..4 {
84 let a0 = output[i];
85 let c0 = output[4 + i];
86 let d0 = output[8 + i];
87 let b0 = output[12 + i];
88 let a2 = a0 + c0;
89 let d2 = d0 - b0;
90 let e1 = (a2 - d2) >> 1;
91 let b1 = e1 - b0;
92 let c1 = e1 - c0;
93 let a1 = a2 - b1;
94 let d1 = d2 + c1;
95 dest[i] = clip_add(dest[i], a1, bd);
96 dest[stride + i] = clip_add(dest[stride + i], b1, bd);
97 dest[stride * 2 + i] = clip_add(dest[stride * 2 + i], c1, bd);
98 dest[stride * 3 + i] = clip_add(dest[stride * 3 + i], d1, bd);
99 }
100}
101
102pub(crate) fn idct4(input: &[i32], output: &mut [i32], bd: u8) {
103 if invalid_highbd_input(input, bd) {
104 output[..4].fill(0);
105 return;
106 }
107 let g = |v: i32| v as i64;
108 let mut step = [0i32; 4];
109 let t1 = (g(input[0]) + g(input[2])) * C16;
110 let t2 = (g(input[0]) - g(input[2])) * C16;
111 step[0] = dcrs(t1);
112 step[1] = dcrs(t2);
113 let t1 = g(input[1]) * C24 - g(input[3]) * C8;
114 let t2 = g(input[1]) * C8 + g(input[3]) * C24;
115 step[2] = dcrs(t1);
116 step[3] = dcrs(t2);
117 output[0] = step[0] + step[3];
118 output[1] = step[1] + step[2];
119 output[2] = step[1] - step[2];
120 output[3] = step[0] - step[3];
121}
122
123pub(crate) fn idct8(input: &[i32], output: &mut [i32], bd: u8) {
124 if invalid_highbd_input(input, bd) {
125 output[..8].fill(0);
126 return;
127 }
128 let g = |v: i32| v as i64;
129 let mut step1 = [0i32; 8];
130 let mut step2 = [0i32; 8];
131 step1[0] = input[0];
132 step1[1] = input[2];
133 step1[2] = input[4];
134 step1[3] = input[6];
135 step1[4] = dcrs(g(input[1]) * C28 - g(input[7]) * C4);
136 step1[7] = dcrs(g(input[1]) * C4 + g(input[7]) * C28);
137 step1[5] = dcrs(g(input[5]) * C12 - g(input[3]) * C20);
138 step1[6] = dcrs(g(input[5]) * C20 + g(input[3]) * C12);
139 step2[0] = dcrs((g(step1[0]) + g(step1[2])) * C16);
140 step2[1] = dcrs((g(step1[0]) - g(step1[2])) * C16);
141 step2[2] = dcrs(g(step1[1]) * C24 - g(step1[3]) * C8);
142 step2[3] = dcrs(g(step1[1]) * C8 + g(step1[3]) * C24);
143 step2[4] = step1[4] + step1[5];
144 step2[5] = step1[4] - step1[5];
145 step2[6] = -step1[6] + step1[7];
146 step2[7] = step1[6] + step1[7];
147 step1[0] = step2[0] + step2[3];
148 step1[1] = step2[1] + step2[2];
149 step1[2] = step2[1] - step2[2];
150 step1[3] = step2[0] - step2[3];
151 step1[4] = step2[4];
152 step1[5] = dcrs((g(step2[6]) - g(step2[5])) * C16);
153 step1[6] = dcrs((g(step2[5]) + g(step2[6])) * C16);
154 step1[7] = step2[7];
155 output[0] = step1[0] + step1[7];
156 output[1] = step1[1] + step1[6];
157 output[2] = step1[2] + step1[5];
158 output[3] = step1[3] + step1[4];
159 output[4] = step1[3] - step1[4];
160 output[5] = step1[2] - step1[5];
161 output[6] = step1[1] - step1[6];
162 output[7] = step1[0] - step1[7];
163}
164
165pub(crate) fn idct16(input: &[i32], output: &mut [i32], bd: u8) {
166 if invalid_highbd_input(input, bd) {
167 output[..16].fill(0);
168 return;
169 }
170 let g = |v: i32| v as i64;
171 let mut step1 = [0i32; 16];
172 let mut step2 = [0i32; 16];
173 const ORD: [usize; 16] = [0, 16, 8, 24, 4, 20, 12, 28, 2, 18, 10, 26, 6, 22, 14, 30];
174 for i in 0..16 {
175 step1[i] = input[ORD[i] / 2];
176 }
177 step2[8] = dcrs(g(step1[8]) * C30 - g(step1[15]) * C2);
178 step2[15] = dcrs(g(step1[8]) * C2 + g(step1[15]) * C30);
179 step2[9] = dcrs(g(step1[9]) * C14 - g(step1[14]) * C18);
180 step2[14] = dcrs(g(step1[9]) * C18 + g(step1[14]) * C14);
181 step2[10] = dcrs(g(step1[10]) * C22 - g(step1[13]) * C10);
182 step2[13] = dcrs(g(step1[10]) * C10 + g(step1[13]) * C22);
183 step2[11] = dcrs(g(step1[11]) * C6 - g(step1[12]) * C26);
184 step2[12] = dcrs(g(step1[11]) * C26 + g(step1[12]) * C6);
185 step2[0..8].copy_from_slice(&step1[0..8]);
186 step1[4] = dcrs(g(step2[4]) * C28 - g(step2[7]) * C4);
187 step1[7] = dcrs(g(step2[4]) * C4 + g(step2[7]) * C28);
188 step1[5] = dcrs(g(step2[5]) * C12 - g(step2[6]) * C20);
189 step1[6] = dcrs(g(step2[5]) * C20 + g(step2[6]) * C12);
190 step1[0..4].copy_from_slice(&step2[0..4]);
191 step1[8] = step2[8] + step2[9];
192 step1[9] = step2[8] - step2[9];
193 step1[10] = -step2[10] + step2[11];
194 step1[11] = step2[10] + step2[11];
195 step1[12] = step2[12] + step2[13];
196 step1[13] = step2[12] - step2[13];
197 step1[14] = -step2[14] + step2[15];
198 step1[15] = step2[14] + step2[15];
199 step2[0] = dcrs((g(step1[0]) + g(step1[1])) * C16);
200 step2[1] = dcrs((g(step1[0]) - g(step1[1])) * C16);
201 step2[2] = dcrs(g(step1[2]) * C24 - g(step1[3]) * C8);
202 step2[3] = dcrs(g(step1[2]) * C8 + g(step1[3]) * C24);
203 step2[4] = step1[4] + step1[5];
204 step2[5] = step1[4] - step1[5];
205 step2[6] = -step1[6] + step1[7];
206 step2[7] = step1[6] + step1[7];
207 step2[8] = step1[8];
208 step2[15] = step1[15];
209 step2[9] = dcrs(-g(step1[9]) * C8 + g(step1[14]) * C24);
210 step2[14] = dcrs(g(step1[9]) * C24 + g(step1[14]) * C8);
211 step2[10] = dcrs(-g(step1[10]) * C24 - g(step1[13]) * C8);
212 step2[13] = dcrs(-g(step1[10]) * C8 + g(step1[13]) * C24);
213 step2[11] = step1[11];
214 step2[12] = step1[12];
215 step1[0] = step2[0] + step2[3];
216 step1[1] = step2[1] + step2[2];
217 step1[2] = step2[1] - step2[2];
218 step1[3] = step2[0] - step2[3];
219 step1[4] = step2[4];
220 step1[5] = dcrs((g(step2[6]) - g(step2[5])) * C16);
221 step1[6] = dcrs((g(step2[5]) + g(step2[6])) * C16);
222 step1[7] = step2[7];
223 step1[8] = step2[8] + step2[11];
224 step1[9] = step2[9] + step2[10];
225 step1[10] = step2[9] - step2[10];
226 step1[11] = step2[8] - step2[11];
227 step1[12] = -step2[12] + step2[15];
228 step1[13] = -step2[13] + step2[14];
229 step1[14] = step2[13] + step2[14];
230 step1[15] = step2[12] + step2[15];
231 step2[0] = step1[0] + step1[7];
232 step2[1] = step1[1] + step1[6];
233 step2[2] = step1[2] + step1[5];
234 step2[3] = step1[3] + step1[4];
235 step2[4] = step1[3] - step1[4];
236 step2[5] = step1[2] - step1[5];
237 step2[6] = step1[1] - step1[6];
238 step2[7] = step1[0] - step1[7];
239 step2[8] = step1[8];
240 step2[9] = step1[9];
241 step2[10] = dcrs((-g(step1[10]) + g(step1[13])) * C16);
242 step2[13] = dcrs((g(step1[10]) + g(step1[13])) * C16);
243 step2[11] = dcrs((-g(step1[11]) + g(step1[12])) * C16);
244 step2[12] = dcrs((g(step1[11]) + g(step1[12])) * C16);
245 step2[14] = step1[14];
246 step2[15] = step1[15];
247 for i in 0..8 {
248 output[i] = step2[i] + step2[15 - i];
249 output[15 - i] = step2[i] - step2[15 - i];
250 }
251}
252
253pub(crate) fn idct32(input: &[i32], output: &mut [i32], bd: u8) {
254 if invalid_highbd_input(input, bd) {
255 output[..32].fill(0);
256 return;
257 }
258 let g = |v: i32| v as i64;
259 let mut step1 = [0i32; 32];
260 let mut step2 = [0i32; 32];
261 for i in 0..16 {
262 step1[i] = input[[
263 0usize, 16, 8, 24, 4, 20, 12, 28, 2, 18, 10, 26, 6, 22, 14, 30,
264 ][i]];
265 }
266 step1[16] = dcrs(g(input[1]) * C31 - g(input[31]) * C1);
267 step1[31] = dcrs(g(input[1]) * C1 + g(input[31]) * C31);
268 step1[17] = dcrs(g(input[17]) * C15 - g(input[15]) * C17);
269 step1[30] = dcrs(g(input[17]) * C17 + g(input[15]) * C15);
270 step1[18] = dcrs(g(input[9]) * C23 - g(input[23]) * C9);
271 step1[29] = dcrs(g(input[9]) * C9 + g(input[23]) * C23);
272 step1[19] = dcrs(g(input[25]) * C7 - g(input[7]) * C25);
273 step1[28] = dcrs(g(input[25]) * C25 + g(input[7]) * C7);
274 step1[20] = dcrs(g(input[5]) * C27 - g(input[27]) * C5);
275 step1[27] = dcrs(g(input[5]) * C5 + g(input[27]) * C27);
276 step1[21] = dcrs(g(input[21]) * C11 - g(input[11]) * C21);
277 step1[26] = dcrs(g(input[21]) * C21 + g(input[11]) * C11);
278 step1[22] = dcrs(g(input[13]) * C19 - g(input[19]) * C13);
279 step1[25] = dcrs(g(input[13]) * C13 + g(input[19]) * C19);
280 step1[23] = dcrs(g(input[29]) * C3 - g(input[3]) * C29);
281 step1[24] = dcrs(g(input[29]) * C29 + g(input[3]) * C3);
282 step2[0..8].copy_from_slice(&step1[0..8]);
283 step2[8] = dcrs(g(step1[8]) * C30 - g(step1[15]) * C2);
284 step2[15] = dcrs(g(step1[8]) * C2 + g(step1[15]) * C30);
285 step2[9] = dcrs(g(step1[9]) * C14 - g(step1[14]) * C18);
286 step2[14] = dcrs(g(step1[9]) * C18 + g(step1[14]) * C14);
287 step2[10] = dcrs(g(step1[10]) * C22 - g(step1[13]) * C10);
288 step2[13] = dcrs(g(step1[10]) * C10 + g(step1[13]) * C22);
289 step2[11] = dcrs(g(step1[11]) * C6 - g(step1[12]) * C26);
290 step2[12] = dcrs(g(step1[11]) * C26 + g(step1[12]) * C6);
291 step2[16] = step1[16] + step1[17];
292 step2[17] = step1[16] - step1[17];
293 step2[18] = -step1[18] + step1[19];
294 step2[19] = step1[18] + step1[19];
295 step2[20] = step1[20] + step1[21];
296 step2[21] = step1[20] - step1[21];
297 step2[22] = -step1[22] + step1[23];
298 step2[23] = step1[22] + step1[23];
299 step2[24] = step1[24] + step1[25];
300 step2[25] = step1[24] - step1[25];
301 step2[26] = -step1[26] + step1[27];
302 step2[27] = step1[26] + step1[27];
303 step2[28] = step1[28] + step1[29];
304 step2[29] = step1[28] - step1[29];
305 step2[30] = -step1[30] + step1[31];
306 step2[31] = step1[30] + step1[31];
307 step1[0..4].copy_from_slice(&step2[0..4]);
308 step1[4] = dcrs(g(step2[4]) * C28 - g(step2[7]) * C4);
309 step1[7] = dcrs(g(step2[4]) * C4 + g(step2[7]) * C28);
310 step1[5] = dcrs(g(step2[5]) * C12 - g(step2[6]) * C20);
311 step1[6] = dcrs(g(step2[5]) * C20 + g(step2[6]) * C12);
312 step1[8] = step2[8] + step2[9];
313 step1[9] = step2[8] - step2[9];
314 step1[10] = -step2[10] + step2[11];
315 step1[11] = step2[10] + step2[11];
316 step1[12] = step2[12] + step2[13];
317 step1[13] = step2[12] - step2[13];
318 step1[14] = -step2[14] + step2[15];
319 step1[15] = step2[14] + step2[15];
320 step1[16] = step2[16];
321 step1[31] = step2[31];
322 step1[17] = dcrs(-g(step2[17]) * C4 + g(step2[30]) * C28);
323 step1[30] = dcrs(g(step2[17]) * C28 + g(step2[30]) * C4);
324 step1[18] = dcrs(-g(step2[18]) * C28 - g(step2[29]) * C4);
325 step1[29] = dcrs(-g(step2[18]) * C4 + g(step2[29]) * C28);
326 step1[19] = step2[19];
327 step1[20] = step2[20];
328 step1[21] = dcrs(-g(step2[21]) * C20 + g(step2[26]) * C12);
329 step1[26] = dcrs(g(step2[21]) * C12 + g(step2[26]) * C20);
330 step1[22] = dcrs(-g(step2[22]) * C12 - g(step2[25]) * C20);
331 step1[25] = dcrs(-g(step2[22]) * C20 + g(step2[25]) * C12);
332 step1[23] = step2[23];
333 step1[24] = step2[24];
334 step1[27] = step2[27];
335 step1[28] = step2[28];
336 step2[0] = dcrs((g(step1[0]) + g(step1[1])) * C16);
337 step2[1] = dcrs((g(step1[0]) - g(step1[1])) * C16);
338 step2[2] = dcrs(g(step1[2]) * C24 - g(step1[3]) * C8);
339 step2[3] = dcrs(g(step1[2]) * C8 + g(step1[3]) * C24);
340 step2[4] = step1[4] + step1[5];
341 step2[5] = step1[4] - step1[5];
342 step2[6] = -step1[6] + step1[7];
343 step2[7] = step1[6] + step1[7];
344 step2[8] = step1[8];
345 step2[15] = step1[15];
346 step2[9] = dcrs(-g(step1[9]) * C8 + g(step1[14]) * C24);
347 step2[14] = dcrs(g(step1[9]) * C24 + g(step1[14]) * C8);
348 step2[10] = dcrs(-g(step1[10]) * C24 - g(step1[13]) * C8);
349 step2[13] = dcrs(-g(step1[10]) * C8 + g(step1[13]) * C24);
350 step2[11] = step1[11];
351 step2[12] = step1[12];
352 step2[16] = step1[16] + step1[19];
353 step2[17] = step1[17] + step1[18];
354 step2[18] = step1[17] - step1[18];
355 step2[19] = step1[16] - step1[19];
356 step2[20] = -step1[20] + step1[23];
357 step2[21] = -step1[21] + step1[22];
358 step2[22] = step1[21] + step1[22];
359 step2[23] = step1[20] + step1[23];
360 step2[24] = step1[24] + step1[27];
361 step2[25] = step1[25] + step1[26];
362 step2[26] = step1[25] - step1[26];
363 step2[27] = step1[24] - step1[27];
364 step2[28] = -step1[28] + step1[31];
365 step2[29] = -step1[29] + step1[30];
366 step2[30] = step1[29] + step1[30];
367 step2[31] = step1[28] + step1[31];
368 step1[0] = step2[0] + step2[3];
369 step1[1] = step2[1] + step2[2];
370 step1[2] = step2[1] - step2[2];
371 step1[3] = step2[0] - step2[3];
372 step1[4] = step2[4];
373 step1[5] = dcrs((g(step2[6]) - g(step2[5])) * C16);
374 step1[6] = dcrs((g(step2[5]) + g(step2[6])) * C16);
375 step1[7] = step2[7];
376 step1[8] = step2[8] + step2[11];
377 step1[9] = step2[9] + step2[10];
378 step1[10] = step2[9] - step2[10];
379 step1[11] = step2[8] - step2[11];
380 step1[12] = -step2[12] + step2[15];
381 step1[13] = -step2[13] + step2[14];
382 step1[14] = step2[13] + step2[14];
383 step1[15] = step2[12] + step2[15];
384 step1[16] = step2[16];
385 step1[17] = step2[17];
386 step1[18] = dcrs(-g(step2[18]) * C8 + g(step2[29]) * C24);
387 step1[29] = dcrs(g(step2[18]) * C24 + g(step2[29]) * C8);
388 step1[19] = dcrs(-g(step2[19]) * C8 + g(step2[28]) * C24);
389 step1[28] = dcrs(g(step2[19]) * C24 + g(step2[28]) * C8);
390 step1[20] = dcrs(-g(step2[20]) * C24 - g(step2[27]) * C8);
391 step1[27] = dcrs(-g(step2[20]) * C8 + g(step2[27]) * C24);
392 step1[21] = dcrs(-g(step2[21]) * C24 - g(step2[26]) * C8);
393 step1[26] = dcrs(-g(step2[21]) * C8 + g(step2[26]) * C24);
394 step1[22] = step2[22];
395 step1[23] = step2[23];
396 step1[24] = step2[24];
397 step1[25] = step2[25];
398 step1[30] = step2[30];
399 step1[31] = step2[31];
400 step2[0] = step1[0] + step1[7];
401 step2[1] = step1[1] + step1[6];
402 step2[2] = step1[2] + step1[5];
403 step2[3] = step1[3] + step1[4];
404 step2[4] = step1[3] - step1[4];
405 step2[5] = step1[2] - step1[5];
406 step2[6] = step1[1] - step1[6];
407 step2[7] = step1[0] - step1[7];
408 step2[8] = step1[8];
409 step2[9] = step1[9];
410 step2[10] = dcrs((-g(step1[10]) + g(step1[13])) * C16);
411 step2[13] = dcrs((g(step1[10]) + g(step1[13])) * C16);
412 step2[11] = dcrs((-g(step1[11]) + g(step1[12])) * C16);
413 step2[12] = dcrs((g(step1[11]) + g(step1[12])) * C16);
414 step2[14] = step1[14];
415 step2[15] = step1[15];
416 step2[16] = step1[16] + step1[23];
417 step2[17] = step1[17] + step1[22];
418 step2[18] = step1[18] + step1[21];
419 step2[19] = step1[19] + step1[20];
420 step2[20] = step1[19] - step1[20];
421 step2[21] = step1[18] - step1[21];
422 step2[22] = step1[17] - step1[22];
423 step2[23] = step1[16] - step1[23];
424 step2[24] = -step1[24] + step1[31];
425 step2[25] = -step1[25] + step1[30];
426 step2[26] = -step1[26] + step1[29];
427 step2[27] = -step1[27] + step1[28];
428 step2[28] = step1[27] + step1[28];
429 step2[29] = step1[26] + step1[29];
430 step2[30] = step1[25] + step1[30];
431 step2[31] = step1[24] + step1[31];
432 step1[0] = step2[0] + step2[15];
433 step1[1] = step2[1] + step2[14];
434 step1[2] = step2[2] + step2[13];
435 step1[3] = step2[3] + step2[12];
436 step1[4] = step2[4] + step2[11];
437 step1[5] = step2[5] + step2[10];
438 step1[6] = step2[6] + step2[9];
439 step1[7] = step2[7] + step2[8];
440 step1[8] = step2[7] - step2[8];
441 step1[9] = step2[6] - step2[9];
442 step1[10] = step2[5] - step2[10];
443 step1[11] = step2[4] - step2[11];
444 step1[12] = step2[3] - step2[12];
445 step1[13] = step2[2] - step2[13];
446 step1[14] = step2[1] - step2[14];
447 step1[15] = step2[0] - step2[15];
448 // stage 8 (inv_txfm.c:1120): cospi_16 rotation of 20..27 before the
449 // final combine — omitting it desyncs every 32x32 residual.
450 step1[16] = step2[16];
451 step1[17] = step2[17];
452 step1[18] = step2[18];
453 step1[19] = step2[19];
454 step1[20] = dcrs((-g(step2[20]) + g(step2[27])) * C16);
455 step1[27] = dcrs((g(step2[20]) + g(step2[27])) * C16);
456 step1[21] = dcrs((-g(step2[21]) + g(step2[26])) * C16);
457 step1[26] = dcrs((g(step2[21]) + g(step2[26])) * C16);
458 step1[22] = dcrs((-g(step2[22]) + g(step2[25])) * C16);
459 step1[25] = dcrs((g(step2[22]) + g(step2[25])) * C16);
460 step1[23] = dcrs((-g(step2[23]) + g(step2[24])) * C16);
461 step1[24] = dcrs((g(step2[23]) + g(step2[24])) * C16);
462 step1[28] = step2[28];
463 step1[29] = step2[29];
464 step1[30] = step2[30];
465 step1[31] = step2[31];
466 for i in 0..16 {
467 output[i] = step1[i] + step1[31 - i];
468 output[31 - i] = step1[i] - step1[31 - i];
469 }
470}
471
472pub(crate) fn iadst4(input: &[i32], output: &mut [i32], bd: u8) {
473 if invalid_highbd_input(input, bd) {
474 output[..4].fill(0);
475 return;
476 }
477 let (x0, x1, x2, x3) = (
478 input[0] as i64,
479 input[1] as i64,
480 input[2] as i64,
481 input[3] as i64,
482 );
483 if x0 | x1 | x2 | x3 == 0 {
484 output[..4].fill(0);
485 return;
486 }
487 let s0 = S1 * x0 + S4 * x2 + S2 * x3;
488 let s1 = S2 * x0 - S1 * x2 - S4 * x3;
489 let s3 = S3 * x1;
490 let s7 = (x0 - x2 + x3) as i32 as i64;
491 let s2 = S3 * s7;
492 output[0] = dcrs(s0 + s3);
493 output[1] = dcrs(s1 + s3);
494 output[2] = dcrs(s2);
495 output[3] = dcrs(s0 + s1 - s3);
496}
497
498pub(crate) fn iadst8(input: &[i32], output: &mut [i32], bd: u8) {
499 if invalid_highbd_input(input, bd) {
500 output[..8].fill(0);
501 return;
502 }
503 let g = |v: i32| v as i64;
504 let mut x0 = g(input[7]);
505 let mut x1 = g(input[0]);
506 let mut x2 = g(input[5]);
507 let mut x3 = g(input[2]);
508 let mut x4 = g(input[3]);
509 let mut x5 = g(input[4]);
510 let mut x6 = g(input[1]);
511 let mut x7 = g(input[6]);
512 if x0 | x1 | x2 | x3 | x4 | x5 | x6 | x7 == 0 {
513 output[..8].fill(0);
514 return;
515 }
516 let s0 = C2 * x0 + C30 * x1;
517 let s1 = C30 * x0 - C2 * x1;
518 let s2 = C10 * x2 + C22 * x3;
519 let s3 = C22 * x2 - C10 * x3;
520 let s4 = C18 * x4 + C14 * x5;
521 let s5 = C14 * x4 - C18 * x5;
522 let s6 = C26 * x6 + C6 * x7;
523 let s7 = C6 * x6 - C26 * x7;
524 x0 = dcrs(s0 + s4) as i64;
525 x1 = dcrs(s1 + s5) as i64;
526 x2 = dcrs(s2 + s6) as i64;
527 x3 = dcrs(s3 + s7) as i64;
528 x4 = dcrs(s0 - s4) as i64;
529 x5 = dcrs(s1 - s5) as i64;
530 x6 = dcrs(s2 - s6) as i64;
531 x7 = dcrs(s3 - s7) as i64;
532 let (t0, t1, t2, t3) = (x0, x1, x2, x3);
533 let s4 = C8 * x4 + C24 * x5;
534 let s5 = C24 * x4 - C8 * x5;
535 let s6 = -C24 * x6 + C8 * x7;
536 let s7 = C8 * x6 + C24 * x7;
537 x0 = (t0 + t2) as i32 as i64;
538 x1 = (t1 + t3) as i32 as i64;
539 x2 = (t0 - t2) as i32 as i64;
540 x3 = (t1 - t3) as i32 as i64;
541 x4 = dcrs(s4 + s6) as i64;
542 x5 = dcrs(s5 + s7) as i64;
543 x6 = dcrs(s4 - s6) as i64;
544 x7 = dcrs(s5 - s7) as i64;
545 let s2 = C16 * (x2 + x3);
546 let s3 = C16 * (x2 - x3);
547 let s6 = C16 * (x6 + x7);
548 let s7 = C16 * (x6 - x7);
549 x2 = dcrs(s2) as i64;
550 x3 = dcrs(s3) as i64;
551 x6 = dcrs(s6) as i64;
552 x7 = dcrs(s7) as i64;
553 output[0] = x0 as i32;
554 output[1] = -x4 as i32;
555 output[2] = x6 as i32;
556 output[3] = -x2 as i32;
557 output[4] = x3 as i32;
558 output[5] = -x7 as i32;
559 output[6] = x5 as i32;
560 output[7] = -x1 as i32;
561}
562
563pub(crate) fn iadst16(input: &[i32], output: &mut [i32], bd: u8) {
564 if invalid_highbd_input(input, bd) {
565 output[..16].fill(0);
566 return;
567 }
568 let g = |v: i32| v as i64;
569 let mut x = [
570 g(input[15]),
571 g(input[0]),
572 g(input[13]),
573 g(input[2]),
574 g(input[11]),
575 g(input[4]),
576 g(input[9]),
577 g(input[6]),
578 g(input[7]),
579 g(input[8]),
580 g(input[5]),
581 g(input[10]),
582 g(input[3]),
583 g(input[12]),
584 g(input[1]),
585 g(input[14]),
586 ];
587 if x.iter().all(|&v| v == 0) {
588 output[..16].fill(0);
589 return;
590 }
591 let mut s = [0i64; 16];
592 s[0] = x[0] * C1 + x[1] * C31;
593 s[1] = x[0] * C31 - x[1] * C1;
594 s[2] = x[2] * C5 + x[3] * C27;
595 s[3] = x[2] * C27 - x[3] * C5;
596 s[4] = x[4] * C9 + x[5] * C23;
597 s[5] = x[4] * C23 - x[5] * C9;
598 s[6] = x[6] * C13 + x[7] * C19;
599 s[7] = x[6] * C19 - x[7] * C13;
600 s[8] = x[8] * C17 + x[9] * C15;
601 s[9] = x[8] * C15 - x[9] * C17;
602 s[10] = x[10] * C21 + x[11] * C11;
603 s[11] = x[10] * C11 - x[11] * C21;
604 s[12] = x[12] * C25 + x[13] * C7;
605 s[13] = x[12] * C7 - x[13] * C25;
606 s[14] = x[14] * C29 + x[15] * C3;
607 s[15] = x[14] * C3 - x[15] * C29;
608 for i in 0..8 {
609 x[i] = dcrs(s[i] + s[i + 8]) as i64;
610 x[i + 8] = dcrs(s[i] - s[i + 8]) as i64;
611 }
612 s[0..8].copy_from_slice(&x[0..8]);
613 s[8] = x[8] * C4 + x[9] * C28;
614 s[9] = x[8] * C28 - x[9] * C4;
615 s[10] = x[10] * C20 + x[11] * C12;
616 s[11] = x[10] * C12 - x[11] * C20;
617 s[12] = -x[12] * C28 + x[13] * C4;
618 s[13] = x[12] * C4 + x[13] * C28;
619 s[14] = -x[14] * C12 + x[15] * C20;
620 s[15] = x[14] * C20 + x[15] * C12;
621 for i in 0..4 {
622 x[i] = (s[i] + s[i + 4]) as i32 as i64;
623 x[i + 4] = (s[i] - s[i + 4]) as i32 as i64;
624 }
625 for i in 0..4 {
626 x[8 + i] = dcrs(s[8 + i] + s[12 + i]) as i64;
627 x[12 + i] = dcrs(s[8 + i] - s[12 + i]) as i64;
628 }
629 s[0..4].copy_from_slice(&x[0..4]);
630 s[8..12].copy_from_slice(&x[8..12]);
631 s[4] = x[4] * C8 + x[5] * C24;
632 s[5] = x[4] * C24 - x[5] * C8;
633 s[6] = -x[6] * C24 + x[7] * C8;
634 s[7] = x[6] * C8 + x[7] * C24;
635 s[12] = x[12] * C8 + x[13] * C24;
636 s[13] = x[12] * C24 - x[13] * C8;
637 s[14] = -x[14] * C24 + x[15] * C8;
638 s[15] = x[14] * C8 + x[15] * C24;
639 x[0] = (s[0] + s[2]) as i32 as i64;
640 x[1] = (s[1] + s[3]) as i32 as i64;
641 x[2] = (s[0] - s[2]) as i32 as i64;
642 x[3] = (s[1] - s[3]) as i32 as i64;
643 x[4] = dcrs(s[4] + s[6]) as i64;
644 x[5] = dcrs(s[5] + s[7]) as i64;
645 x[6] = dcrs(s[4] - s[6]) as i64;
646 x[7] = dcrs(s[5] - s[7]) as i64;
647 x[8] = (s[8] + s[10]) as i32 as i64;
648 x[9] = (s[9] + s[11]) as i32 as i64;
649 x[10] = (s[8] - s[10]) as i32 as i64;
650 x[11] = (s[9] - s[11]) as i32 as i64;
651 x[12] = dcrs(s[12] + s[14]) as i64;
652 x[13] = dcrs(s[13] + s[15]) as i64;
653 x[14] = dcrs(s[12] - s[14]) as i64;
654 x[15] = dcrs(s[13] - s[15]) as i64;
655 // stage 4 (inv_txfm.c:531): pairs (2,3) and (14,15) take the
656 // NEGATED sum into the first slot; (6,7) and (10,11) take
657 // (-x_i + x_j) into the second.
658 let s2 = -C16 * (x[2] + x[3]);
659 let s3 = C16 * (x[2] - x[3]);
660 let s6 = C16 * (x[6] + x[7]);
661 let s7 = C16 * (x[7] - x[6]);
662 let s10 = C16 * (x[10] + x[11]);
663 let s11 = C16 * (x[11] - x[10]);
664 let s14 = -C16 * (x[14] + x[15]);
665 let s15 = C16 * (x[14] - x[15]);
666 x[2] = dcrs(s2) as i64;
667 x[3] = dcrs(s3) as i64;
668 x[6] = dcrs(s6) as i64;
669 x[7] = dcrs(s7) as i64;
670 x[10] = dcrs(s10) as i64;
671 x[11] = dcrs(s11) as i64;
672 x[14] = dcrs(s14) as i64;
673 x[15] = dcrs(s15) as i64;
674 let o = [
675 x[0], -x[8], x[12], -x[4], x[6], x[14], x[10], x[2], x[3], x[11], x[15], x[7], x[5],
676 -x[13], x[9], -x[1],
677 ];
678 for (i, &v) in o.iter().enumerate() {
679 output[i] = v as i32;
680 }
681}
682
683/// One 2D transform pass: `rows` then `cols` then add with the block's
684/// rounding shift (vpx_idct4x4_16_add_c and friends; vp9_iht* for the
685/// ADST combinations).
686pub(crate) fn inverse_transform_add(
687 tx_size: usize,
688 tx_type: usize,
689 coeffs: &[i32],
690 dest: &mut [Sample],
691 off: usize,
692 stride: usize,
693 lossless: bool,
694 bd: u8,
695) {
696 if lossless {
697 iwht4x4_add(coeffs, &mut dest[off..], stride, bd);
698 return;
699 }
700 if tx_size == TX_32X32 {
701 let mut out = [0i32; 1024];
702 for i in 0..32 {
703 idct32(&coeffs[i * 32..], &mut out[i * 32..], bd);
704 }
705 let mut temp_in = [0i32; 32];
706 let mut temp_out = [0i32; 32];
707 for i in 0..32 {
708 for (j, ti) in temp_in.iter_mut().enumerate() {
709 *ti = out[j * 32 + i];
710 }
711 idct32(&temp_in, &mut temp_out, bd);
712 for j in 0..32 {
713 let d = &mut dest[off + j * stride + i];
714 *d = clip_add(*d, (temp_out[j] + 32) >> 6, bd);
715 }
716 }
717 return;
718 }
719 let (n, shift) = match tx_size {
720 TX_4X4 => (4usize, 4u32),
721 TX_8X8 => (8, 5),
722 _ => (16, 6),
723 };
724 // ADST_DCT codes ADST vertical (= cols pass); DCT_ADST horizontal.
725 let adst_cols = matches!(tx_type, ADST_DCT | ADST_ADST);
726 let adst_rows = matches!(tx_type, DCT_ADST | ADST_ADST);
727 let (row_fn, col_fn): (fn(&[i32], &mut [i32], u8), fn(&[i32], &mut [i32], u8)) = match tx_size {
728 TX_4X4 => (
729 if adst_rows { iadst4 } else { idct4 },
730 if adst_cols { iadst4 } else { idct4 },
731 ),
732 TX_8X8 => (
733 if adst_rows { iadst8 } else { idct8 },
734 if adst_cols { iadst8 } else { idct8 },
735 ),
736 _ => (
737 if adst_rows { iadst16 } else { idct16 },
738 if adst_cols { iadst16 } else { idct16 },
739 ),
740 };
741 let mut out = vec![0i32; n * n];
742 for i in 0..n {
743 row_fn(&coeffs[i * n..], &mut out[i * n..], bd);
744 }
745 let mut temp_in = vec![0i32; n];
746 let mut temp_out = vec![0i32; n];
747 for i in 0..n {
748 for j in 0..n {
749 temp_in[j] = out[j * n + i];
750 }
751 col_fn(&temp_in, &mut temp_out, bd);
752 for j in 0..n {
753 let d = &mut dest[off + j * stride + i];
754 *d = clip_add(*d, (temp_out[j] + (1 << (shift - 1))) >> shift, bd);
755 }
756 }
757}
758
759#[cfg(test)]
760mod scratch_probe {
761 use super::*;
762 #[test]
763 fn probe_dc38() {
764 for (name, tt) in [("dct_dct", DCT_DCT), ("dct_adst", DCT_ADST)] {
765 let mut c = [0i32; 64];
766 c[0] = 38;
767 let mut d = vec![129u16; 64];
768 inverse_transform_add(TX_8X8, tt, &c, &mut d, 0, 8, false, 8);
769 println!("{name}: {:?}", &d[..8]);
770 }
771 }
772}