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 @@
20 use crate::filterbank::{Analysis, alias_expand};20 use crate::filterbank::{Analysis, alias_expand};
21 use crate::header::{ChannelMode, FrameHeader, Version};21 use crate::header::{ChannelMode, FrameHeader, Version};
22 use crate::huffman::{self, BitBuf, Table};22 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+};
24 use ec_core::bitio::BitWriter;27 use ec_core::bitio::BitWriter;
25 use ec_core::error::{Error, Result};28 use ec_core::error::{Error, Result};
26 use ec_dsp::{RealFft, Window};29 use ec_dsp::{RealFft, Window};
@@ -1043,17 +1046,17 @@ impl Mp3Encode {
1043 /// unscaled inverse reconstructs the input under overlap-add.1046 /// unscaled inverse reconstructs the input under overlap-add.
1044 fn mdct_long(input: &[f32; 36], block_type: u8, out: &mut [f32]) {1047 fn mdct_long(input: &[f32; 36], block_type: u8, out: &mut [f32]) {
1045 let window = &windows()[usize::from(block_type)];1048 let window = &windows()[usize::from(block_type)];
1049+ let kernel = forward_mdct_long();
1046 let mut windowed = [0.0f32; 36];1050 let mut windowed = [0.0f32; 36];
1047 for i in 0..36 {1051 for i in 0..36 {
1048 windowed[i] = input[i] * window[i];1052 windowed[i] = input[i] * window[i];
1049 }1053 }
1050- for (k, slot) in out.iter_mut().enumerate() {1054+ for (k, row) in kernel.iter().enumerate() {
1051 let mut sum = 0.0f64;1055 let mut sum = 0.0f64;
1052 for (n, value) in windowed.iter().enumerate() {1056 for (n, value) in windowed.iter().enumerate() {
1053- let angle = std::f64::consts::PI / 72.0 * ((2 * n + 19) * (2 * k + 1)) as f64;1057+ sum += f64::from(*value) * f64::from(row[n]);
1054- sum += f64::from(*value) * angle.cos();
1055 }1058 }
1056- *slot = (sum / 9.0) as f32;1059+ out[k] = (sum / 9.0) as f32;
1057 }1060 }
1058 }1061 }
1059 1062
@@ -1061,13 +1064,13 @@ fn mdct_long(input: &[f32; 36], block_type: u8, out: &mut [f32]) {
1061 /// into the spectrum the way the decoder reads them back.1064 /// into the spectrum the way the decoder reads them back.
1062 fn mdct_short(input: &[f32; 36], out: &mut [f32]) {1065 fn mdct_short(input: &[f32; 36], out: &mut [f32]) {
1063 let window = &windows()[2];1066 let window = &windows()[2];
1067+ let kernel = forward_mdct_short();
1064 for w in 0..3 {1068 for w in 0..3 {
1065- for k in 0..6 {1069+ for (k, row) in kernel.iter().enumerate() {
1066 let mut sum = 0.0f64;1070 let mut sum = 0.0f64;
1067 for n in 0..12 {1071 for n in 0..12 {
1068 let sample = input[6 + 6 * w + n] * window[n];1072 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;1073+ sum += f64::from(sample) * f64::from(row[n]);
1070- sum += f64::from(sample) * angle.cos();
1071 }1074 }
1072 out[k * 3 + w] = (sum / 3.0) as f32;1075 out[k * 3 + w] = (sum / 3.0) as f32;
1073 }1076 }
crates/ec-mp3/src/tables.rsmodified+40 -1
@@ -7,7 +7,7 @@
7 //! band attenuated at a time), which is also why they are stated as widths: a7 //! band attenuated at a time), which is also why they are stated as widths: a
8 //! width table cannot silently disagree with itself about where 576 ends.8 //! width table cannot silently disagree with itself about where 576 ends.
9 9
10-use std::sync::OnceLock;10+use std::sync::{LazyLock, OnceLock};
11 11
12 /// Widths of the 22 long scalefactor bands, by sample rate.12 /// Widths of the 22 long scalefactor bands, by sample rate.
13 pub(crate) fn long_widths(sample_rate: u32) -> &'static [u16; 22] {13 pub(crate) fn long_widths(sample_rate: u32) -> &'static [u16; 22] {
@@ -175,6 +175,45 @@ pub(crate) fn alias_coefficients() -> &'static [(f32, f32); 8] {
175 })175 })
176 }176 }
177 177
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+
178 #[cfg(test)]217 #[cfg(test)]
179 mod tests {218 mod tests {
180 use super::*;219 use super::*;