forge
Explore
Log in

Don't you dare go Hollow

The 3/4-power curve as cube and two square roots

Browse files at this commit

2d2451a65d36c0aed7d7a59820e7d97d0eceba95Ahmet Kaan GümüşParent: d97b1ac
  • perf
    • the 3/4-power curve as cube and two square roots
Show full message
perf(mp3): the 3/4-power curve as cube and two square roots

|xr·scale|^(3/4) ran libm powf for every line of every rate-loop round —
10.8% of the encoder's profile — and the noise pass re-ran band_gain's
exp2 per line. Both are now per-band or table-free arithmetic (a cube and
two sqrts in f64, one band gain per band); the short-block quantiser also
stops searching for each line's band. Decoded correlations are unchanged
to six decimals on every bench fixture (seven of eight byte-identical;
music3s flips one ulp-boundary line). Suite green. encode: stereo CBR
18.2x -> 21.1x realtime, VBR 10.6x -> 13.0x.
crates/ec-mp3/src/encode.rsmodified+42 -9
@@ -765,7 +765,16 @@ impl Mp3Encode {
765765 let count = (granules * channels).max(1);
766766 let demand: Vec<f64> = xrs
767767 .iter()
768- .map(|xr| xr.iter().map(|v| f64::from(v.abs()).powf(0.75)).sum())
768+ .map(|xr| {
769+ xr.iter()
770+ .map(|v| {
771+ let x = f64::from(v.abs());
772+ // |x|^(3/4) as a cube and two square roots, the same
773+ // shape the quantiser uses.
774+ (x * x * x).sqrt().sqrt()
775+ })
776+ .sum()
777+ })
769778 .collect();
770779 let demand_total: f64 = demand.iter().sum();
771780 let demand_share = |budget: usize, index: usize| -> u32 {
@@ -1369,9 +1378,14 @@ fn code_with(
13691378 }
13701379 let mut noise = [0.0f32; SFB_LONG];
13711380 let power = power43();
1381+ // One band gain per band, not one per line: the residual ran an exp2 for
1382+ // every spectral line of the granule.
1383+ let mut scales = [0.0f32; SFB_LONG];
1384+ for (band, slot) in scales.iter_mut().enumerate() {
1385+ *slot = band_gain(gain, scalefac[band], block_type);
1386+ }
13721387 let residual = |i: usize, band: usize| -> f32 {
1373- let scale = band_gain(gain, scalefac[band], block_type);
1374- let magnitude = power[(ix[i].unsigned_abs() as usize).min(MAX_QUANT)] * scale;
1388+ let magnitude = power[(ix[i].unsigned_abs() as usize).min(MAX_QUANT)] * scales[band];
13751389 let reconstructed = if ix[i] < 0 { -magnitude } else { magnitude };
13761390 (xr[i] - reconstructed).powi(2)
13771391 };
@@ -1442,18 +1456,37 @@ fn quantise(
14421456 ix: &mut [i32],
14431457 ) {
14441458 let quantise_line = |xr: f32, scale: f32| -> i32 {
1445- let value = (xr.abs() * scale).powf(0.75);
1446- let magnitude = (value + 0.4054).min(MAX_QUANT as f32) as i32;
1459+ // |xr·scale|^(3/4) as a cube and two square roots: the libm `powf`
1460+ // this replaces was a tenth of the encoder's runtime all by itself.
1461+ // The two forms differ by an ulp or two, which next to the level
1462+ // boundary offset below can flip the rare line by one quantisation
1463+ // step and nothing more.
1464+ let x = f64::from(xr.abs()) * f64::from(scale);
1465+ let value = (x * x * x).sqrt().sqrt();
1466+ let magnitude = (value + 0.4054).min(MAX_QUANT as f64) as i32;
14471467 if xr < 0.0 { -magnitude } else { magnitude }
14481468 };
14491469 if block_type == 2 {
14501470 // Short blocks: twelve bands, each with its own scalefactor shared by
14511471 // the three windows. Without them the quantiser's noise is flat across
14521472 // the spectrum, which is what made a switched granule the worst-coded
1453- // one in the stream.
1454- for (i, slot) in ix.iter_mut().enumerate().take(576) {
1455- let scale = 1.0 / band_gain(gain, scalefac[short_band(i, sample_rate)], block_type);
1456- *slot = quantise_line(xr[i], scale);
1473+ // one in the stream. One band gain per band, not one per line: the
1474+ // interleaved layout means a line's band search ran 576 exp2 calls
1475+ // and 576 linear band searches per quantisation.
1476+ let short = short_starts(sample_rate);
1477+ let mut scales = [0.0f32; 13];
1478+ for (band, slot) in scales.iter_mut().enumerate() {
1479+ *slot = 1.0 / band_gain(gain, scalefac[band], block_type);
1480+ }
1481+ for band in 0..13 {
1482+ let (from, to) = (
1483+ usize::from(short[band]) * 3,
1484+ usize::from(short[band + 1]) * 3,
1485+ );
1486+ let scale = scales[band];
1487+ for i in from..to.min(576) {
1488+ ix[i] = quantise_line(xr[i], scale);
1489+ }
14571490 }
14581491 return;
14591492 }