forge
Explore
Log in

Don't you dare go Hollow

U8: ec-opus CELT FFT on rustfft only; delete the non-rustfft fft15 backend and dft15 (default build unchanged)

Browse files at this commit

5c36cb7063a3c0170bfbef17594ae088027c2301Ahmet Kaan GümüşParent: 88bd9f9
crates/ec-opus/src/celt.rsmodified+39 -218
@@ -23,6 +23,10 @@
23 23
24 use ec_core::{Error, Result};24 use ec_core::{Error, Result};
25 25
26+use std::sync::Arc;
27+
28+use rustfft::num_complex::Complex;
29+
26 use crate::range::{RangeDecoder, RangeEncoder};30 use crate::range::{RangeDecoder, RangeEncoder};
27 31
28 /// Bands in the 48 kHz mode.32 /// Bands in the 48 kHz mode.
@@ -301,236 +305,53 @@ pub(crate) fn overlap_window() -> Vec<f32> {
301 // ---------------------------------------------------------------------------305 // ---------------------------------------------------------------------------
302 // The N/4 FFT and the inverse MDCT306 // The N/4 FFT and the inverse MDCT
303 // ---------------------------------------------------------------------------307 // ---------------------------------------------------------------------------
304-308+/// A complex inverse FFT of the sizes CELT needs (`15 * 2^k`), planned by
305-/// A complex inverse FFT of the sizes CELT needs (`15 * 2^k`).309+/// rustfft (pure Rust, runtime-detected AVX butterflies). The transform is
306-///310+/// UNSCALED, `X[k] = sum x[n] exp(+2 pi i n k / N)`, over split-complex buffers.
307-/// Two backends behind one signature, selected at compile time:311+#[derive(Clone)]
308-///312+pub(crate) struct Fft15 {
309-/// - default, the in-project shape: [`ec_dsp::Fft`] covers the `2^k` factor313+ n: usize,
310-/// and the factor of 15 is one extra decimation stage (3 x 5), so the whole314+ plan: Arc<dyn rustfft::Fft<f32>>,
311-/// transform stays `O(n log n)` without a mixed-radix kernel in the DSP315+ cbuf: Vec<Complex<f32>>,
312-/// crate;316+ /// Preallocated once: `process` would allocate this per call, and the
313-/// - under the optional `fft-rustfft` feature, a rustfft plan plans the size317+ /// encoder's steady-state loop is a zero-allocation contract.
314-/// directly (pure Rust, runtime-detected AVX butterflies, no C). The318+ scratch: Vec<Complex<f32>>,
315-/// family's dependency-free build stays the default; the feature is the
316-/// measured speed option.
317-///
318-/// Both deliver the same contract: an UNSCALED inverse transform,
319-/// `X[k] = sum x[n] exp(+2 pi i n k / N)`, over split-complex buffers.
320-#[cfg(not(feature = "fft-rustfft"))]
321-mod fft15 {
322- use super::dft15;
323- use ec_dsp::Fft;
324-
325- #[derive(Clone, Debug)]
326- pub(crate) struct Fft15 {
327- n: usize,
328- /// Sub-transform of length `n/15`.
329- sub: Fft<f32>,
330- /// `exp(2 pi i * k1 * n2 / n)` twiddles, split.
331- tw_re: Vec<f32>,
332- tw_im: Vec<f32>,
333- scratch_re: Vec<f32>,
334- scratch_im: Vec<f32>,
335- }
336-
337- impl Fft15 {
338- pub(crate) fn new(n: usize) -> Fft15 {
339- assert!(n.is_multiple_of(15) && (n / 15).is_power_of_two());
340- let l = n / 15;
341- let mut tw_re = vec![0.0; n];
342- let mut tw_im = vec![0.0; n];
343- for k1 in 0..15 {
344- for n2 in 0..l {
345- let a = 2.0 * core::f64::consts::PI * (k1 * n2) as f64 / n as f64;
346- tw_re[k1 * l + n2] = a.cos() as f32;
347- tw_im[k1 * l + n2] = a.sin() as f32;
348- }
349- }
350- Fft15 {
351- n,
352- sub: Fft::new(l),
353- tw_re,
354- tw_im,
355- scratch_re: vec![0.0; n],
356- scratch_im: vec![0.0; n],
357- }
358- }
359-
360- /// Unscaled inverse transform.
361- pub(crate) fn inverse(&mut self, re: &mut [f32], im: &mut [f32]) {
362- let n = self.n;
363- let l = n / 15;
364- debug_assert_eq!(re.len(), n);
365- // Stage 1: 15-point DFTs over n1 at stride L, twiddled on the way
366- // out.
367- for n2 in 0..l {
368- let mut xr = [0.0f32; 15];
369- let mut xi = [0.0f32; 15];
370- for n1 in 0..15 {
371- xr[n1] = re[l * n1 + n2];
372- xi[n1] = im[l * n1 + n2];
373- }
374- let (yr, yi) = dft15(&xr, &xi);
375- for k1 in 0..15 {
376- let (tr, ti) = (self.tw_re[k1 * l + n2], self.tw_im[k1 * l + n2]);
377- self.scratch_re[k1 * l + n2] = yr[k1] * tr - yi[k1] * ti;
378- self.scratch_im[k1 * l + n2] = yr[k1] * ti + yi[k1] * tr;
379- }
380- }
381- // Stage 2: L-point transforms, one per k1, written out at stride
382- // 15. ec-dsp scales its inverse by 1/L; undo that to keep the
383- // transform unscaled, which is what the MDCT expects.
384- for k1 in 0..15 {
385- let rr = &mut self.scratch_re[k1 * l..(k1 + 1) * l];
386- let ii = &mut self.scratch_im[k1 * l..(k1 + 1) * l];
387- self.sub.inverse_split(rr, ii);
388- for (k2, (&r, &i)) in rr.iter().zip(ii.iter()).enumerate() {
389- re[k1 + 15 * k2] = r * l as f32;
390- im[k1 + 15 * k2] = i * l as f32;
391- }
392- }
393- }
394- }
395 }319 }
396 320
397-#[cfg(feature = "fft-rustfft")]321+impl core::fmt::Debug for Fft15 {
398-mod fft15 {322+ fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
399- use rustfft::num_complex::Complex;323+ f.debug_struct("Fft15").field("n", &self.n).finish()
400- use std::sync::Arc;
401-
402- #[derive(Clone)]
403- pub(crate) struct Fft15 {
404- n: usize,
405- plan: Arc<dyn rustfft::Fft<f32>>,
406- cbuf: Vec<Complex<f32>>,
407- /// Preallocated once: `process` would allocate this per call, and the
408- /// encoder's steady-state loop is a zero-allocation contract.
409- scratch: Vec<Complex<f32>>,
410 }324 }
325+}
411 326
412- impl core::fmt::Debug for Fft15 {327+impl Fft15 {
413- fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {328+ pub(crate) fn new(n: usize) -> Fft15 {
414- f.debug_struct("Fft15").field("n", &self.n).finish()329+ assert!(n.is_multiple_of(15) && (n / 15).is_power_of_two());
330+ let mut planner = rustfft::FftPlanner::<f32>::new();
331+ let plan = planner.plan_fft_inverse(n);
332+ let cbuf = vec![Complex::ZERO; n];
333+ let scratch = vec![Complex::ZERO; plan.get_inplace_scratch_len()];
334+ Fft15 {
335+ n,
336+ plan,
337+ cbuf,
338+ scratch,
415 }339 }
416 }340 }
417 341
418- impl Fft15 {342+ pub(crate) fn inverse(&mut self, re: &mut [f32], im: &mut [f32]) {
419- pub(crate) fn new(n: usize) -> Fft15 {343+ for (c, (r, i)) in self.cbuf.iter_mut().zip(re.iter().zip(im.iter())) {
420- assert!(n.is_multiple_of(15) && (n / 15).is_power_of_two());344+ *c = Complex::new(*r, *i);
421- let mut planner = rustfft::FftPlanner::<f32>::new();
422- let plan = planner.plan_fft_inverse(n);
423- let cbuf = vec![Complex::ZERO; n];
424- let scratch = vec![Complex::ZERO; plan.get_inplace_scratch_len()];
425- Fft15 {
426- n,
427- plan,
428- cbuf,
429- scratch,
430- }
431 }345 }
432-346+ self.plan
433- /// Unscaled inverse transform. rustfft does not scale, which is347+ .process_with_scratch(&mut self.cbuf, &mut self.scratch);
434- /// exactly the contract here.348+ for ((c, r), i) in self.cbuf.iter().zip(re.iter_mut()).zip(im.iter_mut()) {
435- pub(crate) fn inverse(&mut self, re: &mut [f32], im: &mut [f32]) {349+ *r = c.re;
436- for (c, (r, i)) in self.cbuf.iter_mut().zip(re.iter().zip(im.iter())) {350+ *i = c.im;
437- *c = Complex::new(*r, *i);
438- }
439- self.plan
440- .process_with_scratch(&mut self.cbuf, &mut self.scratch);
441- for ((c, r), i) in self.cbuf.iter().zip(re.iter_mut()).zip(im.iter_mut()) {
442- *r = c.re;
443- *i = c.im;
444- }
445 }351 }
446 }352 }
447 }353 }
448 354
449-pub(crate) use fft15::Fft15;
450-
451-/// A 15-point DFT as 5 radix-3 stages then 3 radix-5 stages (Cooley-Tukey on
452-/// 15 = 3*5), which costs about a third of the direct evaluation.
453-#[cfg(not(feature = "fft-rustfft"))]
454-#[inline]
455-fn dft15(xr: &[f32; 15], xi: &[f32; 15]) -> ([f32; 15], [f32; 15]) {
456- // exp(+2 pi i / 3) and the two fifth-root cosines/sines.
457- const C3: f32 = -0.5;
458- const S3: f32 = 0.866_025_4;
459- const C5_1: f32 = 0.309_017;
460- const S5_1: f32 = 0.951_056_5;
461- const C5_2: f32 = -0.809_017;
462- const S5_2: f32 = 0.587_785_25;
463- // exp(+2 pi i * (n2*k1) / 15) for n2 in 0..5, k1 in 0..3.
464- const TW_RE: [[f32; 3]; 5] = [
465- [1.0, 1.0, 1.0],
466- [1.0, 0.913_545_5, 0.669_130_6],
467- [1.0, 0.669_130_6, -0.104_528_46],
468- [1.0, 0.309_017, -0.809_017],
469- [1.0, -0.104_528_46, -0.978_147_6],
470- ];
471- const TW_IM: [[f32; 3]; 5] = [
472- [0.0, 0.0, 0.0],
473- [0.0, 0.406_736_65, 0.743_144_8],
474- [0.0, 0.743_144_8, 0.994_521_9],
475- [0.0, 0.951_056_5, 0.587_785_25],
476- [0.0, 0.994_521_9, -0.207_911_69],
477- ];
478- // Radix-3 over n1 (stride 5), one per n2.
479- let mut ar = [[0.0f32; 3]; 5];
480- let mut ai = [[0.0f32; 3]; 5];
481- for n2 in 0..5 {
482- let (r0, i0) = (xr[n2], xi[n2]);
483- let (r1, i1) = (xr[5 + n2], xi[5 + n2]);
484- let (r2, i2) = (xr[10 + n2], xi[10 + n2]);
485- let (sr, si) = (r1 + r2, i1 + i2);
486- let (dr, di) = (r1 - r2, i1 - i2);
487- ar[n2][0] = r0 + sr;
488- ai[n2][0] = i0 + si;
489- let (tr, ti) = (r0 + C3 * sr, i0 + C3 * si);
490- // Multiplying (dr, di) by i gives (-di, dr).
491- ar[n2][1] = tr - S3 * di;
492- ai[n2][1] = ti + S3 * dr;
493- ar[n2][2] = tr + S3 * di;
494- ai[n2][2] = ti - S3 * dr;
495- // Twiddle by exp(+2 pi i n2 k1 / 15).
496- for k1 in 1..3 {
497- let (tr, ti) = (TW_RE[n2][k1], TW_IM[n2][k1]);
498- let (r, i) = (ar[n2][k1], ai[n2][k1]);
499- ar[n2][k1] = r * tr - i * ti;
500- ai[n2][k1] = r * ti + i * tr;
501- }
502- }
503- // Radix-5 over n2, one per k1; output index is k1 + 3*k2.
504- let mut yr = [0.0f32; 15];
505- let mut yi = [0.0f32; 15];
506- for k1 in 0..3 {
507- let (r0, i0) = (ar[0][k1], ai[0][k1]);
508- let (r1, i1) = (ar[1][k1], ai[1][k1]);
509- let (r2, i2) = (ar[2][k1], ai[2][k1]);
510- let (r3, i3) = (ar[3][k1], ai[3][k1]);
511- let (r4, i4) = (ar[4][k1], ai[4][k1]);
512- let (s1r, s1i) = (r1 + r4, i1 + i4);
513- let (d1r, d1i) = (r1 - r4, i1 - i4);
514- let (s2r, s2i) = (r2 + r3, i2 + i3);
515- let (d2r, d2i) = (r2 - r3, i2 - i3);
516- yr[k1] = r0 + s1r + s2r;
517- yi[k1] = i0 + s1i + s2i;
518- let (m1r, m1i) = (r0 + C5_1 * s1r + C5_2 * s2r, i0 + C5_1 * s1i + C5_2 * s2i);
519- let (n1r, n1i) = (S5_1 * d1r + S5_2 * d2r, S5_1 * d1i + S5_2 * d2i);
520- let (m2r, m2i) = (r0 + C5_2 * s1r + C5_1 * s2r, i0 + C5_2 * s1i + C5_1 * s2i);
521- let (n2r, n2i) = (S5_2 * d1r - S5_1 * d2r, S5_2 * d1i - S5_1 * d2i);
522- yr[k1 + 3] = m1r - n1i;
523- yi[k1 + 3] = m1i + n1r;
524- yr[k1 + 12] = m1r + n1i;
525- yi[k1 + 12] = m1i - n1r;
526- yr[k1 + 6] = m2r - n2i;
527- yi[k1 + 6] = m2i + n2r;
528- yr[k1 + 9] = m2r + n2i;
529- yi[k1 + 9] = m2i - n2r;
530- }
531- (yr, yi)
532-}
533-
534 /// One inverse-MDCT plan: spectrum length `l = 15 * 2^k`, window `2*l`.355 /// One inverse-MDCT plan: spectrum length `l = 15 * 2^k`, window `2*l`.
535 #[derive(Clone, Debug)]356 #[derive(Clone, Debug)]
536 struct ImdctPlan {357 struct ImdctPlan {