forge
Explore
Log in

Don't you dare go Hollow

The forward MDCT reads its cosine kernel from a table

Browse files at this commit

f4ce2b8901eca8a6e46e192e4b165be56ec96ad7Ahmet Kaan GümüşParent: 2d2451a
  • perf
    • the forward MDCT reads its cosine kernel from a table
Show full message
perf(mp3): the forward MDCT reads its cosine kernel from a table

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.rsmodified+11 -8
@@ -20,7 +20,10 @@
2020 use crate::filterbank::{Analysis, alias_expand};
2121 use crate::header::{ChannelMode, FrameHeader, Version};
2222 use crate::huffman::{self, BitBuf, Table};
23-use crate::tables::{MAX_QUANT, SLEN, long_starts, power43, short_starts, short_widths, windows};
23+use crate::tables::{
24+ MAX_QUANT, SLEN, forward_mdct_long, forward_mdct_short, long_starts, power43, short_starts,
25+ short_widths, windows,
26+};
2427 use ec_core::bitio::BitWriter;
2528 use ec_core::error::{Error, Result};
2629 use ec_dsp::{RealFft, Window};
@@ -1043,17 +1046,17 @@ impl Mp3Encode {
10431046 /// unscaled inverse reconstructs the input under overlap-add.
10441047 fn mdct_long(input: &[f32; 36], block_type: u8, out: &mut [f32]) {
10451048 let window = &windows()[usize::from(block_type)];
1049+ let kernel = forward_mdct_long();
10461050 let mut windowed = [0.0f32; 36];
10471051 for i in 0..36 {
10481052 windowed[i] = input[i] * window[i];
10491053 }
1050- for (k, slot) in out.iter_mut().enumerate() {
1054+ for (k, row) in kernel.iter().enumerate() {
10511055 let mut sum = 0.0f64;
10521056 for (n, value) in windowed.iter().enumerate() {
1053- let angle = std::f64::consts::PI / 72.0 * ((2 * n + 19) * (2 * k + 1)) as f64;
1054- sum += f64::from(*value) * angle.cos();
1057+ sum += f64::from(*value) * f64::from(row[n]);
10551058 }
1056- *slot = (sum / 9.0) as f32;
1059+ out[k] = (sum / 9.0) as f32;
10571060 }
10581061 }
10591062
@@ -1061,13 +1064,13 @@ fn mdct_long(input: &[f32; 36], block_type: u8, out: &mut [f32]) {
10611064 /// into the spectrum the way the decoder reads them back.
10621065 fn mdct_short(input: &[f32; 36], out: &mut [f32]) {
10631066 let window = &windows()[2];
1067+ let kernel = forward_mdct_short();
10641068 for w in 0..3 {
1065- for k in 0..6 {
1069+ for (k, row) in kernel.iter().enumerate() {
10661070 let mut sum = 0.0f64;
10671071 for n in 0..12 {
10681072 let sample = input[6 + 6 * w + n] * window[n];
1069- let angle = std::f64::consts::PI / 24.0 * ((2 * n + 7) * (2 * k + 1)) as f64;
1070- sum += f64::from(sample) * angle.cos();
1073+ sum += f64::from(sample) * f64::from(row[n]);
10711074 }
10721075 out[k * 3 + w] = (sum / 3.0) as f32;
10731076 }
crates/ec-mp3/src/tables.rsmodified+40 -1
@@ -7,7 +7,7 @@
77 //! band attenuated at a time), which is also why they are stated as widths: a
88 //! width table cannot silently disagree with itself about where 576 ends.
99
10-use std::sync::OnceLock;
10+use std::sync::{LazyLock, OnceLock};
1111
1212 /// Widths of the 22 long scalefactor bands, by sample rate.
1313 pub(crate) fn long_widths(sample_rate: u32) -> &'static [u16; 22] {
@@ -175,6 +175,45 @@ pub(crate) fn alias_coefficients() -> &'static [(f32, f32); 8] {
175175 })
176176 }
177177
178+/// The forward MDCT's cosine kernel, `cos(pi/72 · (2n+19)(2k+1))`, laid out
179+/// `[k][n]` so each of the 18 output coefficients of a long block is one dot
180+/// product. The encoder recomputed these with libm for every subband of
181+/// every granule — 23% of its runtime — where the decoder has had its
182+/// inverse table since it was written.
183+static FORWARD_MDCT_LONG: LazyLock<[[f32; 36]; 18]> = LazyLock::new(|| {
184+ let mut table = [[0.0f32; 36]; 18];
185+ for (k, row) in table.iter_mut().enumerate() {
186+ for (n, slot) in row.iter_mut().enumerate() {
187+ let angle = std::f64::consts::PI / 72.0 * ((2 * n + 19) * (2 * k + 1)) as f64;
188+ *slot = angle.cos() as f32;
189+ }
190+ }
191+ table
192+});
193+
194+/// The forward MDCT kernel for a long block.
195+pub(crate) fn forward_mdct_long() -> &'static [[f32; 36]; 18] {
196+ &FORWARD_MDCT_LONG
197+}
198+
199+/// The short-block kernel, `cos(pi/24 · (2n+7)(2k+1))`, laid out `[k][n]`
200+/// for one 12-sample window; the three windows share it at 6-sample offsets.
201+static FORWARD_MDCT_SHORT: LazyLock<[[f32; 12]; 6]> = LazyLock::new(|| {
202+ let mut table = [[0.0f32; 12]; 6];
203+ for (k, row) in table.iter_mut().enumerate() {
204+ for (n, slot) in row.iter_mut().enumerate() {
205+ let angle = std::f64::consts::PI / 24.0 * ((2 * n + 7) * (2 * k + 1)) as f64;
206+ *slot = angle.cos() as f32;
207+ }
208+ }
209+ table
210+});
211+
212+/// The forward MDCT kernel for one short window.
213+pub(crate) fn forward_mdct_short() -> &'static [[f32; 12]; 6] {
214+ &FORWARD_MDCT_SHORT
215+}
216+
178217 #[cfg(test)]
179218 mod tests {
180219 use super::*;