diff --git a/crates/ec-opus/src/celt.rs b/crates/ec-opus/src/celt.rs index b3040593..219051bb 100644 --- a/crates/ec-opus/src/celt.rs +++ b/crates/ec-opus/src/celt.rs @@ -23,6 +23,10 @@ use ec_core::{Error, Result}; +use std::sync::Arc; + +use rustfft::num_complex::Complex; + use crate::range::{RangeDecoder, RangeEncoder}; /// Bands in the 48 kHz mode. @@ -301,236 +305,53 @@ pub(crate) fn overlap_window() -> Vec { // --------------------------------------------------------------------------- // The N/4 FFT and the inverse MDCT // --------------------------------------------------------------------------- - -/// A complex inverse FFT of the sizes CELT needs (`15 * 2^k`). -/// -/// Two backends behind one signature, selected at compile time: -/// -/// - default, the in-project shape: [`ec_dsp::Fft`] covers the `2^k` factor -/// and the factor of 15 is one extra decimation stage (3 x 5), so the whole -/// transform stays `O(n log n)` without a mixed-radix kernel in the DSP -/// crate; -/// - under the optional `fft-rustfft` feature, a rustfft plan plans the size -/// directly (pure Rust, runtime-detected AVX butterflies, no C). The -/// family's dependency-free build stays the default; the feature is the -/// measured speed option. -/// -/// Both deliver the same contract: an UNSCALED inverse transform, -/// `X[k] = sum x[n] exp(+2 pi i n k / N)`, over split-complex buffers. -#[cfg(not(feature = "fft-rustfft"))] -mod fft15 { - use super::dft15; - use ec_dsp::Fft; - - #[derive(Clone, Debug)] - pub(crate) struct Fft15 { - n: usize, - /// Sub-transform of length `n/15`. - sub: Fft, - /// `exp(2 pi i * k1 * n2 / n)` twiddles, split. - tw_re: Vec, - tw_im: Vec, - scratch_re: Vec, - scratch_im: Vec, - } - - impl Fft15 { - pub(crate) fn new(n: usize) -> Fft15 { - assert!(n.is_multiple_of(15) && (n / 15).is_power_of_two()); - let l = n / 15; - let mut tw_re = vec![0.0; n]; - let mut tw_im = vec![0.0; n]; - for k1 in 0..15 { - for n2 in 0..l { - let a = 2.0 * core::f64::consts::PI * (k1 * n2) as f64 / n as f64; - tw_re[k1 * l + n2] = a.cos() as f32; - tw_im[k1 * l + n2] = a.sin() as f32; - } - } - Fft15 { - n, - sub: Fft::new(l), - tw_re, - tw_im, - scratch_re: vec![0.0; n], - scratch_im: vec![0.0; n], - } - } - - /// Unscaled inverse transform. - pub(crate) fn inverse(&mut self, re: &mut [f32], im: &mut [f32]) { - let n = self.n; - let l = n / 15; - debug_assert_eq!(re.len(), n); - // Stage 1: 15-point DFTs over n1 at stride L, twiddled on the way - // out. - for n2 in 0..l { - let mut xr = [0.0f32; 15]; - let mut xi = [0.0f32; 15]; - for n1 in 0..15 { - xr[n1] = re[l * n1 + n2]; - xi[n1] = im[l * n1 + n2]; - } - let (yr, yi) = dft15(&xr, &xi); - for k1 in 0..15 { - let (tr, ti) = (self.tw_re[k1 * l + n2], self.tw_im[k1 * l + n2]); - self.scratch_re[k1 * l + n2] = yr[k1] * tr - yi[k1] * ti; - self.scratch_im[k1 * l + n2] = yr[k1] * ti + yi[k1] * tr; - } - } - // Stage 2: L-point transforms, one per k1, written out at stride - // 15. ec-dsp scales its inverse by 1/L; undo that to keep the - // transform unscaled, which is what the MDCT expects. - for k1 in 0..15 { - let rr = &mut self.scratch_re[k1 * l..(k1 + 1) * l]; - let ii = &mut self.scratch_im[k1 * l..(k1 + 1) * l]; - self.sub.inverse_split(rr, ii); - for (k2, (&r, &i)) in rr.iter().zip(ii.iter()).enumerate() { - re[k1 + 15 * k2] = r * l as f32; - im[k1 + 15 * k2] = i * l as f32; - } - } - } - } +/// A complex inverse FFT of the sizes CELT needs (`15 * 2^k`), planned by +/// rustfft (pure Rust, runtime-detected AVX butterflies). The transform is +/// UNSCALED, `X[k] = sum x[n] exp(+2 pi i n k / N)`, over split-complex buffers. +#[derive(Clone)] +pub(crate) struct Fft15 { + n: usize, + plan: Arc>, + cbuf: Vec>, + /// Preallocated once: `process` would allocate this per call, and the + /// encoder's steady-state loop is a zero-allocation contract. + scratch: Vec>, } -#[cfg(feature = "fft-rustfft")] -mod fft15 { - use rustfft::num_complex::Complex; - use std::sync::Arc; - - #[derive(Clone)] - pub(crate) struct Fft15 { - n: usize, - plan: Arc>, - cbuf: Vec>, - /// Preallocated once: `process` would allocate this per call, and the - /// encoder's steady-state loop is a zero-allocation contract. - scratch: Vec>, +impl core::fmt::Debug for Fft15 { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.debug_struct("Fft15").field("n", &self.n).finish() } +} - impl core::fmt::Debug for Fft15 { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.debug_struct("Fft15").field("n", &self.n).finish() +impl Fft15 { + pub(crate) fn new(n: usize) -> Fft15 { + assert!(n.is_multiple_of(15) && (n / 15).is_power_of_two()); + let mut planner = rustfft::FftPlanner::::new(); + let plan = planner.plan_fft_inverse(n); + let cbuf = vec![Complex::ZERO; n]; + let scratch = vec![Complex::ZERO; plan.get_inplace_scratch_len()]; + Fft15 { + n, + plan, + cbuf, + scratch, } } - impl Fft15 { - pub(crate) fn new(n: usize) -> Fft15 { - assert!(n.is_multiple_of(15) && (n / 15).is_power_of_two()); - let mut planner = rustfft::FftPlanner::::new(); - let plan = planner.plan_fft_inverse(n); - let cbuf = vec![Complex::ZERO; n]; - let scratch = vec![Complex::ZERO; plan.get_inplace_scratch_len()]; - Fft15 { - n, - plan, - cbuf, - scratch, - } + pub(crate) fn inverse(&mut self, re: &mut [f32], im: &mut [f32]) { + for (c, (r, i)) in self.cbuf.iter_mut().zip(re.iter().zip(im.iter())) { + *c = Complex::new(*r, *i); } - - /// Unscaled inverse transform. rustfft does not scale, which is - /// exactly the contract here. - pub(crate) fn inverse(&mut self, re: &mut [f32], im: &mut [f32]) { - for (c, (r, i)) in self.cbuf.iter_mut().zip(re.iter().zip(im.iter())) { - *c = Complex::new(*r, *i); - } - self.plan - .process_with_scratch(&mut self.cbuf, &mut self.scratch); - for ((c, r), i) in self.cbuf.iter().zip(re.iter_mut()).zip(im.iter_mut()) { - *r = c.re; - *i = c.im; - } + self.plan + .process_with_scratch(&mut self.cbuf, &mut self.scratch); + for ((c, r), i) in self.cbuf.iter().zip(re.iter_mut()).zip(im.iter_mut()) { + *r = c.re; + *i = c.im; } } } -pub(crate) use fft15::Fft15; - -/// A 15-point DFT as 5 radix-3 stages then 3 radix-5 stages (Cooley-Tukey on -/// 15 = 3*5), which costs about a third of the direct evaluation. -#[cfg(not(feature = "fft-rustfft"))] -#[inline] -fn dft15(xr: &[f32; 15], xi: &[f32; 15]) -> ([f32; 15], [f32; 15]) { - // exp(+2 pi i / 3) and the two fifth-root cosines/sines. - const C3: f32 = -0.5; - const S3: f32 = 0.866_025_4; - const C5_1: f32 = 0.309_017; - const S5_1: f32 = 0.951_056_5; - const C5_2: f32 = -0.809_017; - const S5_2: f32 = 0.587_785_25; - // exp(+2 pi i * (n2*k1) / 15) for n2 in 0..5, k1 in 0..3. - const TW_RE: [[f32; 3]; 5] = [ - [1.0, 1.0, 1.0], - [1.0, 0.913_545_5, 0.669_130_6], - [1.0, 0.669_130_6, -0.104_528_46], - [1.0, 0.309_017, -0.809_017], - [1.0, -0.104_528_46, -0.978_147_6], - ]; - const TW_IM: [[f32; 3]; 5] = [ - [0.0, 0.0, 0.0], - [0.0, 0.406_736_65, 0.743_144_8], - [0.0, 0.743_144_8, 0.994_521_9], - [0.0, 0.951_056_5, 0.587_785_25], - [0.0, 0.994_521_9, -0.207_911_69], - ]; - // Radix-3 over n1 (stride 5), one per n2. - let mut ar = [[0.0f32; 3]; 5]; - let mut ai = [[0.0f32; 3]; 5]; - for n2 in 0..5 { - let (r0, i0) = (xr[n2], xi[n2]); - let (r1, i1) = (xr[5 + n2], xi[5 + n2]); - let (r2, i2) = (xr[10 + n2], xi[10 + n2]); - let (sr, si) = (r1 + r2, i1 + i2); - let (dr, di) = (r1 - r2, i1 - i2); - ar[n2][0] = r0 + sr; - ai[n2][0] = i0 + si; - let (tr, ti) = (r0 + C3 * sr, i0 + C3 * si); - // Multiplying (dr, di) by i gives (-di, dr). - ar[n2][1] = tr - S3 * di; - ai[n2][1] = ti + S3 * dr; - ar[n2][2] = tr + S3 * di; - ai[n2][2] = ti - S3 * dr; - // Twiddle by exp(+2 pi i n2 k1 / 15). - for k1 in 1..3 { - let (tr, ti) = (TW_RE[n2][k1], TW_IM[n2][k1]); - let (r, i) = (ar[n2][k1], ai[n2][k1]); - ar[n2][k1] = r * tr - i * ti; - ai[n2][k1] = r * ti + i * tr; - } - } - // Radix-5 over n2, one per k1; output index is k1 + 3*k2. - let mut yr = [0.0f32; 15]; - let mut yi = [0.0f32; 15]; - for k1 in 0..3 { - let (r0, i0) = (ar[0][k1], ai[0][k1]); - let (r1, i1) = (ar[1][k1], ai[1][k1]); - let (r2, i2) = (ar[2][k1], ai[2][k1]); - let (r3, i3) = (ar[3][k1], ai[3][k1]); - let (r4, i4) = (ar[4][k1], ai[4][k1]); - let (s1r, s1i) = (r1 + r4, i1 + i4); - let (d1r, d1i) = (r1 - r4, i1 - i4); - let (s2r, s2i) = (r2 + r3, i2 + i3); - let (d2r, d2i) = (r2 - r3, i2 - i3); - yr[k1] = r0 + s1r + s2r; - yi[k1] = i0 + s1i + s2i; - let (m1r, m1i) = (r0 + C5_1 * s1r + C5_2 * s2r, i0 + C5_1 * s1i + C5_2 * s2i); - let (n1r, n1i) = (S5_1 * d1r + S5_2 * d2r, S5_1 * d1i + S5_2 * d2i); - let (m2r, m2i) = (r0 + C5_2 * s1r + C5_1 * s2r, i0 + C5_2 * s1i + C5_1 * s2i); - let (n2r, n2i) = (S5_2 * d1r - S5_1 * d2r, S5_2 * d1i - S5_1 * d2i); - yr[k1 + 3] = m1r - n1i; - yi[k1 + 3] = m1i + n1r; - yr[k1 + 12] = m1r + n1i; - yi[k1 + 12] = m1i - n1r; - yr[k1 + 6] = m2r - n2i; - yi[k1 + 6] = m2i + n2r; - yr[k1 + 9] = m2r + n2i; - yi[k1 + 9] = m2i - n2r; - } - (yr, yi) -} - /// One inverse-MDCT plan: spectrum length `l = 15 * 2^k`, window `2*l`. #[derive(Clone, Debug)] struct ImdctPlan {