1static CONST1: i64 = 20091;
2static CONST2: i64 = 35468;
3
4pub(crate) fn idct4x4(block: &mut [i32]) {
5 // The intermediate results may overflow the types, so we stretch the type.
6 fn fetch(block: &[i32], idx: usize) -> i64 {
7 i64::from(block[idx])
8 }
9
10 for i in 0usize..4 {
11 let a1 = fetch(block, i) + fetch(block, 8 + i);
12 let b1 = fetch(block, i) - fetch(block, 8 + i);
13
14 let t1 = (fetch(block, 4 + i) * CONST2) >> 16;
15 let t2 = fetch(block, 12 + i) + ((fetch(block, 12 + i) * CONST1) >> 16);
16 let c1 = t1 - t2;
17
18 let t1 = fetch(block, 4 + i) + ((fetch(block, 4 + i) * CONST1) >> 16);
19 let t2 = (fetch(block, 12 + i) * CONST2) >> 16;
20 let d1 = t1 + t2;
21
22 block[i] = (a1 + d1) as i32;
23 block[4 + i] = (b1 + c1) as i32;
24 block[4 * 3 + i] = (a1 - d1) as i32;
25 block[4 * 2 + i] = (b1 - c1) as i32;
26 }
27
28 for i in 0usize..4 {
29 let a1 = fetch(block, 4 * i) + fetch(block, 4 * i + 2);
30 let b1 = fetch(block, 4 * i) - fetch(block, 4 * i + 2);
31
32 let t1 = (fetch(block, 4 * i + 1) * CONST2) >> 16;
33 let t2 = fetch(block, 4 * i + 3) + ((fetch(block, 4 * i + 3) * CONST1) >> 16);
34 let c1 = t1 - t2;
35
36 let t1 = fetch(block, 4 * i + 1) + ((fetch(block, 4 * i + 1) * CONST1) >> 16);
37 let t2 = (fetch(block, 4 * i + 3) * CONST2) >> 16;
38 let d1 = t1 + t2;
39
40 block[4 * i] = ((a1 + d1 + 4) >> 3) as i32;
41 block[4 * i + 3] = ((a1 - d1 + 4) >> 3) as i32;
42 block[4 * i + 1] = ((b1 + c1 + 4) >> 3) as i32;
43 block[4 * i + 2] = ((b1 - c1 + 4) >> 3) as i32;
44 }
45}
46
47// 14.3
48pub(crate) fn iwht4x4(block: &mut [i32]) {
49 for i in 0usize..4 {
50 let a1 = block[i] + block[12 + i];
51 let b1 = block[4 + i] + block[8 + i];
52 let c1 = block[4 + i] - block[8 + i];
53 let d1 = block[i] - block[12 + i];
54
55 block[i] = a1 + b1;
56 block[4 + i] = c1 + d1;
57 block[8 + i] = a1 - b1;
58 block[12 + i] = d1 - c1;
59 }
60
61 for i in 0usize..4 {
62 let a1 = block[4 * i] + block[4 * i + 3];
63 let b1 = block[4 * i + 1] + block[4 * i + 2];
64 let c1 = block[4 * i + 1] - block[4 * i + 2];
65 let d1 = block[4 * i] - block[4 * i + 3];
66
67 let a2 = a1 + b1;
68 let b2 = c1 + d1;
69 let c2 = a1 - b1;
70 let d2 = d1 - c1;
71
72 block[4 * i] = (a2 + 3) >> 3;
73 block[4 * i + 1] = (b2 + 3) >> 3;
74 block[4 * i + 2] = (c2 + 3) >> 3;
75 block[4 * i + 3] = (d2 + 3) >> 3;
76 }
77}
78