From f4ce2b8901eca8a6e46e192e4b165be56ec96ad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 13 Sep 2026 05:31:01 +0300 Subject: [PATCH] perf(mp3): the forward MDCT reads its cosine kernel from a table MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit mdct_long and mdct_short recomputed cos(pi/72(2n+19)(2k+1)) (and the short-block twin) with libm for every subband of every granule — 23% of the encode profile, and roughly the whole cost of the transform. Both kernels are now LazyLock f32 tables ([k][n], one dot product per output coefficient) built from the same angles; the f64 accumulation and the f32 windowing order are kept, so every bench fixture still encodes byte-identically and the crate suite is green. encode: stereo CBR 21.1x -> 37.3x realtime, VBR 13.0x -> 17.8x. --- crates/ec-mp3/src/encode.rs | 19 +++++++++-------- crates/ec-mp3/src/tables.rs | 41 ++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 9 deletions(-) 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::*;