team-ljyzipelo

공개
104줄3.6 KB
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
36use zune_core::log::debug;
37use zune_core::options::DecoderOptions;
38
39use crate::decoder::IDCTPtr;
40use crate::idct::scalar::{idct_int, idct_int_1x1};
41
42#[cfg(feature = "x86")]
43pub mod avx2;
44#[cfg(feature = "neon")]
45pub mod neon;
46
47pub mod scalar;
48
49/// Choose an appropriate IDCT function
50#[allow(unused_variables)]
51pub 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**
85pub 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
101pub fn choose_idct_1x1_func(_: &DecoderOptions) -> IDCTPtr {
102 // These are simple stores, no alternative implementation for now
103 idct_int_1x1
104}