Debian Codegraph @

src/upsampler.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 //! Up-sampling routines
10 //!
11 //! The main upsampling method is a bi-linear interpolation or a "triangle
12 //! filter " or libjpeg turbo `fancy_upsampling` which is a good compromise
13 //! between speed and visual quality
14 //!
15 //! # The filter
16 //! Each output pixel is made from `(3*A+B)/4` where A is the original
17 //! pixel closer to the output and B is the one further.
18 //!
19 //! ```text
20 //!+---+---+
21 //! | A | B |
22 //! +---+---+
23 //! +-+-+-+-+
24 //! | |P| | |
25 //! +-+-+-+-+
26 //! ```
27 //!
28 //! # Horizontal Bi-linear filter
29 //! ```text
30 //! |---+-----------+---+
31 //! | | | |
32 //! | A | |p1 | p2| | B |
33 //! | | | |
34 //! |---+-----------+---+
35 //!
36 //! ```
37 //! For a horizontal bi-linear it's trivial to implement,
38 //!
39 //! `A` becomes the input closest to the output.
40 //!
41 //! `B` varies depending on output.
42 //! - For odd positions, input is the `next` pixel after A
43 //! - For even positions, input is the `previous` value before A.
44 //!
45 //! We iterate in a classic 1-D sliding window with a window of 3.
46 //! For our sliding window approach, `A` is the 1st and `B` is either the 0th term or 2nd term
47 //! depending on position we are writing.(see scalar code).
48 //!
49 //! For vector code see module sse for explanation.
50 //!
51 //! # Vertical bi-linear.
52 //! Vertical up-sampling is a bit trickier.
53 //!
54 //! ```text
55 //! +----+----+
56 //! | A1 | A2 |
57 //! +----+----+
58 //! +----+----+
59 //! | p1 | p2 |
60 //! +----+-+--+
61 //! +----+-+--+
62 //! | p3 | p4 |
63 //! +----+-+--+
64 //! +----+----+
65 //! | B1 | B2 |
66 //! +----+----+
67 //! ```
68 //!
69 //! For `p1`
70 //! - `A1` is given a weight of `3` and `B1` is given a weight of 1.
71 //!
72 //! For `p3`
73 //! - `B1` is given a weight of `3` and `A1` is given a weight of 1
74 //!
75 //! # Horizontal vertical downsampling/chroma quartering.
76 //!
77 //! Carry out a vertical filter in the first pass, then a horizontal filter in the second pass.
78 #![allow(unreachable_code)]
79 use zune_core::options::DecoderOptions;
80
81 use crate::components::UpSampler;
82
83 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
84 #[cfg(feature = "x86")]
85 mod avx2;
86 #[cfg(target_arch = "aarch64")]
87 #[cfg(feature = "neon")]
88 mod neon;
89 #[cfg(feature = "portable_simd")]
90 mod portable_simd;
91 mod scalar;
92
93 // choose the best possible implementation for this platform
94 #[allow(unused_variables)]
95 pub fn choose_horizontal_samp_function(options: &DecoderOptions) -> UpSampler {
96 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
97 #[cfg(feature = "x86")]
98 if options.use_avx2() {
99 return |a: &[i16], b: &[i16], c: &[i16], d: &mut [i16], e: &mut [i16]| {
100 // SAFETY: `options.use_avx2()` only returns true if avx2 is supported.
101 unsafe { avx2::upsample_horizontal_avx2(a, b, c, d, e) }
102 };
103 }
104 #[cfg(target_arch = "aarch64")]
105 #[cfg(feature = "neon")]
106 if options.use_neon() {
107 return |a: &[i16], b: &[i16], c: &[i16], d: &mut [i16], e: &mut [i16]| {
108 // SAFETY: `options.use_neon()` only returns true if neon is supported.
109 unsafe { neon::upsample_horizontal_neon(a, b, c, d, e) }
110 };
111 }
112 #[cfg(feature = "portable_simd")]
113 return portable_simd::upsample_horizontal_simd;
114 return scalar::upsample_horizontal;
115 }
116
117 #[allow(unused_variables)]
118 pub fn choose_hv_samp_function(options: &DecoderOptions) -> UpSampler {
119 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
120 #[cfg(feature = "x86")]
121 if options.use_avx2() {
122 return |a: &[i16], b: &[i16], c: &[i16], d: &mut [i16], e: &mut [i16]| {
123 // SAFETY: `options.use_avx2()` only returns true if avx2 is supported.
124 unsafe { avx2::upsample_hv_avx2(a, b, c, d, e) }
125 };
126 }
127 #[cfg(target_arch = "aarch64")]
128 #[cfg(feature = "neon")]
129 if options.use_neon() {
130 return |a: &[i16], b: &[i16], c: &[i16], d: &mut [i16], e: &mut [i16]| {
131 // SAFETY: `options.use_neon()` only returns true if neon is supported.
132 unsafe { neon::upsample_hv_neon(a, b, c, d, e) }
133 };
134 }
135 #[cfg(feature = "portable_simd")]
136 return portable_simd::upsample_hv_simd;
137 return scalar::upsample_hv;
138 }
139
140 #[allow(unused_variables)]
141 pub fn choose_v_samp_function(options: &DecoderOptions) -> UpSampler {
142 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
143 #[cfg(feature = "x86")]
144 if options.use_avx2() {
145 return |a: &[i16], b: &[i16], c: &[i16], d: &mut [i16], e: &mut [i16]| {
146 // SAFETY: `options.use_avx2()` only returns true if avx2 is supported.
147 unsafe { avx2::upsample_vertical_avx2(a, b, c, d, e) }
148 };
149 }
150 #[cfg(target_arch = "aarch64")]
151 #[cfg(feature = "neon")]
152 if options.use_neon() {
153 return |a: &[i16], b: &[i16], c: &[i16], d: &mut [i16], e: &mut [i16]| {
154 // SAFETY: `options.use_neon()` only returns true if neon is supported.
155 unsafe { neon::upsample_vertical_neon(a, b, c, d, e) }
156 };
157 }
158 #[cfg(feature = "portable_simd")]
159 return portable_simd::upsample_vertical_simd;
160 return scalar::upsample_vertical;
161 }
162
163 /// Upsample nothing
164
165 pub fn upsample_no_op(
166 _input: &[i16],
167 _in_ref: &[i16],
168 _in_near: &[i16],
169 _scratch_space: &mut [i16],
170 _output: &mut [i16],
171 ) {
172 }
173
174 pub fn generic_sampler() -> UpSampler {
175 scalar::upsample_generic
176 }
177
178 #[cfg(test)]
179 mod tests {
180 use super::*;
•181 use alloc::vec::Vec;
182 use alloc::vec;
183
184 #[cfg(feature = "portable_simd")]
185 mod portable_simd_impl {
186 use super::*;
187
188 #[test]
189 fn portable_simd_vertical() {
190 _test_vertical(portable_simd::upsample_vertical_simd)
191 }
192
193 #[test]
194 fn portable_simd_horizontal() {
195 _test_horizontal(portable_simd::upsample_horizontal_simd)
196 }
197
198 #[test]
199 fn portable_simd_hv() {
200 _test_hv(portable_simd::upsample_hv_simd)
201 }
202 }
203
204 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
205 #[cfg(feature = "x86")]
206 #[cfg(target_feature = "avx2")]
207 mod avx2_impl {
208 use super::*;
209
210 #[test]
211 fn avx2_vertical() {
212 _test_vertical(|a: &[i16], b: &[i16], c: &[i16], d: &mut [i16], e: &mut [i16]| {
213 // SAFETY: Test guarded behind `target_feature`
214 unsafe { avx2::upsample_vertical_avx2(a, b, c, d, e) }
215 })
216 }
217
218 #[test]
219 fn avx2_horizontal() {
220 _test_horizontal(|a: &[i16], b: &[i16], c: &[i16], d: &mut [i16], e: &mut [i16]| {
221 // SAFETY: Test guarded behind `target_feature`
222 unsafe { avx2::upsample_horizontal_avx2(a, b, c, d, e) }
223 })
224 }
225
226 #[test]
227 fn avx2_hv() {
228 _test_hv(|a: &[i16], b: &[i16], c: &[i16], d: &mut [i16], e: &mut [i16]| {
229 // SAFETY: Test guarded behind `target_feature`
230 unsafe { avx2::upsample_hv_avx2(a, b, c, d, e) }
231 })
232 }
233 }
234
235 #[cfg(target_arch = "aarch64")]
236 #[cfg(feature = "neon")]
237 #[cfg(target_feature = "neon")]
238 mod neon_impl {
239 use super::*;
240
241 #[test]
242 fn neon_vertical() {
243 _test_vertical(|a: &[i16], b: &[i16], c: &[i16], d: &mut [i16], e: &mut [i16]| {
244 // SAFETY: Test guarded behind `target_feature`
245 unsafe { neon::upsample_vertical_neon(a, b, c, d, e) }
246 })
247 }
248
249 #[test]
250 fn neon_horizontal() {
251 _test_horizontal(|a: &[i16], b: &[i16], c: &[i16], d: &mut [i16], e: &mut [i16]| {
252 // SAFETY: Test guarded behind `target_feature`
253 unsafe { neon::upsample_horizontal_neon(a, b, c, d, e) }
254 })
255 }
256
257 #[test]
258 fn neon_hv() {
259 _test_hv(|a: &[i16], b: &[i16], c: &[i16], d: &mut [i16], e: &mut [i16]| {
260 // SAFETY: Test guarded behind `target_feature`
261 unsafe { neon::upsample_hv_neon(a, b, c, d, e) }
262 })
263 }
264 }
265
266 fn _test_vertical(upsampler: UpSampler) {
267 let width = 1024;
268 let input: Vec<i16> = (0..width).map(|x| ((x + 10) % 256) as i16).collect();
269 let in_near: Vec<i16> = (0..width).map(|x| ((x + 20) % 256) as i16).collect();
270 let in_far: Vec<i16> = (0..width).map(|x| ((x + 30) % 256) as i16).collect();
271 let mut scratch = vec![0i16; width];
272
273 let mut output_scalar = vec![0i16; width * 2];
274 let mut output_fast = vec![0i16; width * 2];
275
276 scalar::upsample_vertical(&input, &in_near, &in_far, &mut scratch, &mut output_scalar);
277 upsampler(&input, &in_near, &in_far, &mut scratch, &mut output_fast);
278
279 assert_eq!(output_scalar, output_fast);
280 }
281
282 fn _test_horizontal(upsampler: UpSampler) {
283 _test_horizontal_even_width(upsampler);
284 _test_horizontal_odd_width(upsampler);
285 }
286
287 fn _test_horizontal_even_width(upsampler: UpSampler) {
288 let width = 1024;
289 let input: Vec<i16> = (0..width).map(|x| ((x + 10) % 256) as i16).collect();
290
291 let mut scratch = vec![0i16; width];
292
293 let mut output_scalar = vec![0i16; width * 2];
294 let mut output_fast = vec![0i16; width * 2];
295
296 scalar::upsample_horizontal(&input, &[], &[], &mut scratch, &mut output_scalar);
297 upsampler(&input, &[], &[], &mut scratch, &mut output_fast);
298
299 assert_eq!(output_scalar, output_fast);
300 }
301
302 fn _test_horizontal_odd_width(upsampler: UpSampler) {
303 let width = 33;
304 let input: Vec<i16> = (0..width).map(|x| ((x + 10) % 256) as i16).collect();
305 let mut scratch = vec![0i16; width];
306 let mut output_scalar = vec![0i16; width * 2];
307 let mut output_fast = vec![0i16; width * 2];
308
309 scalar::upsample_horizontal(&input, &[], &[], &mut scratch, &mut output_scalar);
310 upsampler(&input, &[], &[], &mut scratch, &mut output_fast);
311
312 assert_eq!(output_scalar, output_fast);
313 }
314
315 fn _test_hv(upsampler: UpSampler) {
316 let width = 512;
317 let input: Vec<i16> = (0..width).map(|x| ((x + 10) % 256) as i16).collect();
318 let in_near: Vec<i16> = (0..width).map(|x| ((x + 20) % 256) as i16).collect();
319 let in_far: Vec<i16> = (0..width).map(|x| ((x + 30) % 256) as i16).collect();
320
321 // Output len is width * 4 for HV (vertical * 2, then horizontal * 2 for each row)
322 // scratch is width * 2
323 let mut scratch_scalar = vec![0i16; width * 2];
324 let mut scratch_fast = vec![0i16; width * 2];
325 let mut output_scalar = vec![0i16; width * 4];
326 let mut output_fast = vec![0i16; width * 4];
327
328 scalar::upsample_hv(
329 &input,
330 &in_near,
331 &in_far,
332 &mut scratch_scalar,
333 &mut output_scalar,
334 );
335 upsampler(
336 &input,
337 &in_near,
338 &in_far,
339 &mut scratch_fast,
340 &mut output_fast,
341 );
342
343 assert_eq!(output_scalar, output_fast);
344 }
345 }