diff --git a/crates/ec-mp3/src/encode.rs b/crates/ec-mp3/src/encode.rs index 47cb9d76..1286087a 100644 --- a/crates/ec-mp3/src/encode.rs +++ b/crates/ec-mp3/src/encode.rs @@ -20,7 +20,10 @@ use crate::filterbank::{Analysis, alias_expand}; use crate::header::{ChannelMode, FrameHeader, Version}; use crate::huffman::{self, BitBuf, Table}; -use crate::tables::{MAX_QUANT, SLEN, long_starts, power43, short_starts, short_widths, windows}; +use crate::tables::{ + MAX_QUANT, SLEN, forward_mdct_long, forward_mdct_short, long_starts, power43, short_starts, + short_widths, windows, +}; use ec_core::bitio::BitWriter; use ec_core::error::{Error, Result}; use ec_dsp::{RealFft, Window}; @@ -1043,17 +1046,17 @@ impl Mp3Encode { /// unscaled inverse reconstructs the input under overlap-add. fn mdct_long(input: &[f32; 36], block_type: u8, out: &mut [f32]) { let window = &windows()[usize::from(block_type)]; + let kernel = forward_mdct_long(); let mut windowed = [0.0f32; 36]; for i in 0..36 { windowed[i] = input[i] * window[i]; } - for (k, slot) in out.iter_mut().enumerate() { + for (k, row) in kernel.iter().enumerate() { let mut sum = 0.0f64; for (n, value) in windowed.iter().enumerate() { - let angle = std::f64::consts::PI / 72.0 * ((2 * n + 19) * (2 * k + 1)) as f64; - sum += f64::from(*value) * angle.cos(); + sum += f64::from(*value) * f64::from(row[n]); } - *slot = (sum / 9.0) as f32; + out[k] = (sum / 9.0) as f32; } } @@ -1061,13 +1064,13 @@ fn mdct_long(input: &[f32; 36], block_type: u8, out: &mut [f32]) { /// into the spectrum the way the decoder reads them back. fn mdct_short(input: &[f32; 36], out: &mut [f32]) { let window = &windows()[2]; + let kernel = forward_mdct_short(); for w in 0..3 { - for k in 0..6 { + for (k, row) in kernel.iter().enumerate() { let mut sum = 0.0f64; for n in 0..12 { let sample = input[6 + 6 * w + n] * window[n]; - let angle = std::f64::consts::PI / 24.0 * ((2 * n + 7) * (2 * k + 1)) as f64; - sum += f64::from(sample) * angle.cos(); + sum += f64::from(sample) * f64::from(row[n]); } out[k * 3 + w] = (sum / 3.0) as f32; } diff --git a/crates/ec-mp3/src/tables.rs b/crates/ec-mp3/src/tables.rs index bd54f882..abd906a2 100644 --- a/crates/ec-mp3/src/tables.rs +++ b/crates/ec-mp3/src/tables.rs @@ -7,7 +7,7 @@ //! band attenuated at a time), which is also why they are stated as widths: a //! width table cannot silently disagree with itself about where 576 ends. -use std::sync::OnceLock; +use std::sync::{LazyLock, OnceLock}; /// Widths of the 22 long scalefactor bands, by sample rate. pub(crate) fn long_widths(sample_rate: u32) -> &'static [u16; 22] { @@ -175,6 +175,45 @@ pub(crate) fn alias_coefficients() -> &'static [(f32, f32); 8] { }) } +/// The forward MDCT's cosine kernel, `cos(pi/72 · (2n+19)(2k+1))`, laid out +/// `[k][n]` so each of the 18 output coefficients of a long block is one dot +/// product. The encoder recomputed these with libm for every subband of +/// every granule — 23% of its runtime — where the decoder has had its +/// inverse table since it was written. +static FORWARD_MDCT_LONG: LazyLock<[[f32; 36]; 18]> = LazyLock::new(|| { + let mut table = [[0.0f32; 36]; 18]; + for (k, row) in table.iter_mut().enumerate() { + for (n, slot) in row.iter_mut().enumerate() { + let angle = std::f64::consts::PI / 72.0 * ((2 * n + 19) * (2 * k + 1)) as f64; + *slot = angle.cos() as f32; + } + } + table +}); + +/// The forward MDCT kernel for a long block. +pub(crate) fn forward_mdct_long() -> &'static [[f32; 36]; 18] { + &FORWARD_MDCT_LONG +} + +/// The short-block kernel, `cos(pi/24 · (2n+7)(2k+1))`, laid out `[k][n]` +/// for one 12-sample window; the three windows share it at 6-sample offsets. +static FORWARD_MDCT_SHORT: LazyLock<[[f32; 12]; 6]> = LazyLock::new(|| { + let mut table = [[0.0f32; 12]; 6]; + for (k, row) in table.iter_mut().enumerate() { + for (n, slot) in row.iter_mut().enumerate() { + let angle = std::f64::consts::PI / 24.0 * ((2 * n + 7) * (2 * k + 1)) as f64; + *slot = angle.cos() as f32; + } + } + table +}); + +/// The forward MDCT kernel for one short window. +pub(crate) fn forward_mdct_short() -> &'static [[f32; 12]; 6] { + &FORWARD_MDCT_SHORT +} + #[cfg(test)] mod tests { use super::*;