Debian Codegraph @

src/idct.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Copyright (c) 2023.
3 *
4 * This software is free software;
5 *
6 * You can redistribute it or modify it under terms of the MIT, Apache License or Zlib license
7 */
8
9 //! Routines for IDCT
10 //!
11 //! Essentially we provide 2 routines for IDCT, a scalar implementation and a not super optimized
12 //! AVX2 one, i'll talk about them here.
13 //!
14 //! There are 2 reasons why we have the avx one
15 //! 1. No one compiles with -C target-features=avx2 hence binaries won't probably take advantage(even
16 //! if it exists).
17 //! 2. AVX employs zero short circuit in a way the scalar code cannot employ it.
18 //! - AVX does this by checking for MCU's whose 63 AC coefficients are zero and if true, it writes
19 //! values directly, if false, it goes the long way of calculating.
20 //! - Although this can be trivially implemented in the scalar version, it generates code
21 //! I'm not happy width(scalar version that basically loops and that is too many branches for me)
22 //! The avx one does a better job of using bitwise or's with (`_mm256_or_si256`) which is magnitudes of faster
23 //! than anything I could come up with
24 //!
25 //! The AVX code also has some cool transpose_u16 instructions which look so complicated to be cool
26 //! (spoiler alert, i barely understand how it works, that's why I credited the owner).
27 //!
28 #![allow(
29 clippy::excessive_precision,
30 clippy::unreadable_literal,
31 clippy::module_name_repetitions,
32 unused_parens,
33 clippy::wildcard_imports
34 )]
35
36 use zune_core::log::debug;
37 use zune_core::options::DecoderOptions;
38
39 use crate::decoder::IDCTPtr;
40 use crate::idct::scalar::{idct_int, idct_int_1x1};
41
42 #[cfg(feature = "x86")]
43 pub mod avx2;
44 #[cfg(feature = "neon")]
45 pub mod neon;
46
47 pub mod scalar;
48
49 /// Choose an appropriate IDCT function
50 #[allow(unused_variables)]
51 pub fn choose_idct_func(options: &DecoderOptions) -> IDCTPtr {
52 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
53 #[cfg(feature = "x86")]
54 {
55 if options.use_avx2() {
56 debug!("Using vector integer IDCT");
57 return |a: &mut [i32; 64], b: &mut [i16], c: usize| {
58 // SAFETY: `options.use_avx2()` only returns true if avx2 is supported.
59 unsafe { avx2::idct_avx2(a,b,c) }
60 };
61 }
62 }
63 #[cfg(target_arch = "aarch64")]
64 #[cfg(feature = "neon")]
65 {
66 if options.use_neon() {
67 debug!("Using vector integer IDCT");
68 return |a: &mut [i32; 64], b: &mut [i16], c: usize| {
69 // SAFETY: `options.use_neon()` only returns true if neon is supported.
70 unsafe { neon::idct_neon(a,b,c) }
71 };
72 }
73 }
74 debug!("Using scalar integer IDCT");
75 // use generic one
76 return idct_int;
77 }
78
79 /// Choose a function to implement 4x4 IDCT.
80 ///
81 /// These functions get the same input but have an extra contract: Only the first 4x4 block of
82 /// coefficients are non-zero. All other entries are zeroed.
83 ///
84 /// **The callee must uphold that contract on return**
85 pub fn choose_idct_4x4_func(_options: &DecoderOptions) -> IDCTPtr {
86 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
87 #[cfg(feature = "x86")]
88 {
89 if _options.use_avx2() {
90 debug!("Using vector integer IDCT");
91 return |a: &mut [i32; 64], b: &mut [i16], c: usize| {
92 // SAFETY: `options.use_avx2()` only returns true if avx2 is supported.
93 unsafe { avx2::idct_avx2_4x4(a,b,c) }
94 };
95 }
96 }
97
98 scalar::idct4x4
99 }
100
101 pub fn choose_idct_1x1_func(_: &DecoderOptions) -> IDCTPtr {
102 // These are simple stores, no alternative implementation for now
103 idct_int_1x1
104 }
105
106 #[cfg(test)]
107 #[allow(unreachable_code)]
108 #[allow(dead_code)]
109 mod tests {
110 use super::*;
•111 use alloc::vec::Vec;
112 use alloc::vec;
113
114 #[test]
115 fn idct_test0() {
116 let stride = 8;
117 let mut coeff = [10; 64];
118 let mut coeff2 = [10; 64];
119 let mut output_scalar = [0; 64];
120 let mut output_vector = [0; 64];
121 let idct_func = choose_idct_func(&DecoderOptions::new_fast());
122 idct_func(&mut coeff, &mut output_vector, stride);
123 idct_int(&mut coeff2, &mut output_scalar, stride);
124 assert_eq!(output_scalar, output_vector, "IDCT and scalar do not match");
125 }
126
127 #[test]
128 fn do_idct_test1() {
129 let stride = 8;
130 let mut coeff = [14; 64];
131 let mut coeff2 = [14; 64];
132 let mut output_scalar = [0; 64];
133 let mut output_vector = [0; 64];
134 let idct_func = choose_idct_func(&DecoderOptions::new_fast());
135 idct_func(&mut coeff, &mut output_vector, stride);
136 idct_int(&mut coeff2, &mut output_scalar, stride);
137 assert_eq!(output_scalar, output_vector, "IDCT and scalar do not match");
138 }
139
140 #[test]
141 fn do_idct_test2() {
142 let stride = 8;
143 let mut coeff = [0; 64];
144 coeff[0] = 255;
145 coeff[63] = -256;
146 let mut coeff2 = coeff;
147 let mut output_scalar = [0; 64];
148 let mut output_vector = [0; 64];
149 let idct_func = choose_idct_func(&DecoderOptions::new_fast());
150 idct_func(&mut coeff, &mut output_vector, stride);
151 idct_int(&mut coeff2, &mut output_scalar, stride);
152 assert_eq!(output_scalar, output_vector, "IDCT and scalar do not match");
153 }
154
155 #[test]
156 fn do_idct_zeros() {
157 let stride = 8;
158 let mut coeff = [0; 64];
159 let mut coeff2 = [0; 64];
160 let mut output_scalar = [0; 64];
161 let mut output_vector = [0; 64];
162 let idct_func = choose_idct_func(&DecoderOptions::new_fast());
163 idct_func(&mut coeff, &mut output_vector, stride);
164 idct_int(&mut coeff2, &mut output_scalar, stride);
165 assert_eq!(output_scalar, output_vector, "IDCT and scalar do not match");
166 }
167
168 #[test]
169 fn idct_4x4() {
170 #[rustfmt::skip]
171 const A: [i32; 32] = [
172 -254, -7, 0, 0, 0, 0, 0, 0,
173 7, 0, -30, 32, 0, 0, 0, 0,
174 7, 0, -30, 32, 0, 0, 0, 0,
175 7, 0, -30, 32, 0, 0, 0, 0,
176 ];
177
178 let v: Vec<IDCTPtr> = vec![
179 choose_idct_func(&DecoderOptions::new_safe()),
180 choose_idct_4x4_func(&DecoderOptions::new_safe()),
181 choose_idct_func(&DecoderOptions::new_fast()),
182 choose_idct_4x4_func(&DecoderOptions::new_fast()),
183 ];
184 let dct_names = vec![
185 "safe idct",
186 "safe idct 4x4",
187 "fast idct",
188 "fast idct 4x4",
189 ];
190
191 let mut color = vec![];
192
193 for idct in v {
194 let mut a = [0i32; 64];
195 a[..32].copy_from_slice(&A);
196 let mut b = [0i16; 64];
197
198 idct(&mut a, &mut b, 8);
199
200 color.push(b);
201 }
202
203 for (wnd, name) in color.windows(2).zip(&dct_names) {
204 let [a, b] = wnd else { unreachable!() };
205 assert_eq!(a, b, "{name}");
206 }
207 }
208 }