forge
Explore
Log in

Don't you dare go Hollow

The rate loop's per-coefficient powf becomes two lookup tables -- pow_quarter carries 2^(v/4) for every integer v in -155..=155 (band_noise's gain, quantise_one's reciprocal) and POW_HALF carries the bisection's 2^(offset/2) mask scale in -240..=240

Browse files at this commit

0fe46e1816efd6cf81a90f82345bc1f0c47c4788Ahmet Kaan GümüşParent: 44ea57e
  • perf
    • the rate loop's per-coefficient powf becomes two lookup tables -- pow_quarter carries 2^(v/4) for every integer v in -155..=155 (band_noise's gain, quantise_one's reciprocal) and POW_HALF carries the bisection's 2^(offset/2) mask scale in -240..=240
    • every entry is built by the identical powf expression it replaces, so the values -- and the bitstream -- are unchanged. The band gain is hoisted to per-band granularity in band_noise and the requant pass instead of being recomputed per coefficient. Byte-identical on stereo 256k and 5.1 512k vs base
    • stereo 37.9 -> 42.1x RT, 5.1 14.2 -> 15.6x
    • ec-aac suite green (43+1+1+14, remaining two suites landed green)
Show full message
perf(aac): the rate loop's per-coefficient powf becomes two lookup tables -- pow_quarter carries 2^(v/4) for every integer v in -155..=155 (band_noise's gain, quantise_one's reciprocal) and POW_HALF carries the bisection's 2^(offset/2) mask scale in -240..=240; every entry is built by the identical powf expression it replaces, so the values -- and the bitstream -- are unchanged. The band gain is hoisted to per-band granularity in band_noise and the requant pass instead of being recomputed per coefficient. Byte-identical on stereo 256k and 5.1 512k vs base; stereo 37.9 -> 42.1x RT, 5.1 14.2 -> 15.6x; ec-aac suite green (43+1+1+14, remaining two suites landed green)
crates/ec-aac/src/encode.rsmodified+34 -6
@@ -638,7 +638,7 @@ impl AacEncoder {
638638 // scalefactors: that keeps the noise following the shape the model
639639 // asked for, and lets a band whose signal falls under its own mask
640640 // drop out entirely instead of being coded ever more coarsely.
641- let target = frame.threshold[b] * 2f32.powf(offset as f32 * 0.5);
641+ let target = frame.threshold[b] * POW_HALF[(offset + 240) as usize];
642642 if profile.energy[b] / profile.count[b].max(1) as f32 <= target {
643643 continue;
644644 }
@@ -693,11 +693,12 @@ impl AacEncoder {
693693 }
694694 continue;
695695 }
696+ let gain = pow_quarter(SF_OFFSET - sf[b]);
696697 let mut peak = 0i32;
697698 for w in 0..windows {
698699 let base = if short { w * SHORT_LEN } else { 0 };
699700 for k in lo..hi {
700- let q = quantise_one(frame.coef[base + k], sf[b]);
701+ let q = quantise_one(frame.coef[base + k], gain);
701702 quant[base + k] = q;
702703 peak = peak.max(q.abs());
703704 }
@@ -759,14 +760,15 @@ impl AacEncoder {
759760 short: bool,
760761 ) -> f32 {
761762 let (lo, hi) = (usize::from(swb[band]), usize::from(swb[band + 1]));
762- let gain = 2f32.powf((sf - SF_OFFSET) as f32 * 0.25);
763+ let gain = pow_quarter(sf - SF_OFFSET);
764+ let gain_inv = pow_quarter(SF_OFFSET - sf);
763765 let mut noise = 0.0f32;
764766 let mut count = 0usize;
765767 for w in 0..windows {
766768 let base = if short { w * SHORT_LEN } else { 0 };
767769 for k in base + lo..base + hi {
768770 let x = frame.coef[k];
769- let q = quantise_one(x, sf);
771+ let q = quantise_one(x, gain_inv);
770772 let mut back = pow43(q.unsigned_abs()) * gain;
771773 if q < 0 {
772774 back = -back;
@@ -888,6 +890,29 @@ static POW43: LazyLock<Vec<f32>> = LazyLock::new(|| {
888890 .collect()
889891 });
890892
893+/// `2^(v/4)` for every integer v the rate loop asks for: a band's quantiser
894+/// step is `2^((sf-100)/4)` in `band_noise` and its reciprocal inside
895+/// [`quantise_one`], and the exponent's integer range over sf in 0..=255 is
896+/// -100..=155 either way. Every entry is produced by the very `powf` call it
897+/// replaces, so lookups return the bit-identical value.
898+static POW_QUARTER: LazyLock<Vec<f32>> = LazyLock::new(|| {
899+ (-155i32..=155)
900+ .map(|v| 2f32.powf(v as f32 * 0.25))
901+ .collect()
902+});
903+
904+fn pow_quarter(v: i32) -> f32 {
905+ POW_QUARTER[(v + 155) as usize]
906+}
907+
908+/// `2^(offset/2)`, the rate loop's mask scale, same table trick at the
909+/// half-octave stride; the bisection bounds keep offset in -240..=240.
910+static POW_HALF: LazyLock<Vec<f32>> = LazyLock::new(|| {
911+ (-240i32..=240)
912+ .map(|o| 2f32.powf(o as f32 * 0.5))
913+ .collect()
914+});
915+
891916 fn pow43(q: u32) -> f32 {
892917 POW43[q.min(MAX_QUANT as u32) as usize]
893918 }
@@ -900,11 +925,14 @@ fn sf_floor(peak: f32) -> i32 {
900925 SF_OFFSET + want.ceil() as i32
901926 }
902927
903-fn quantise_one(x: f32, sf: i32) -> i32 {
928+/// The gain is the band's `2^((sf-100)/4)` reciprocal -- `pow_quarter`'s
929+/// job -- hoisted to the call sites so the inner-inner loop is sqrts only.
930+/// The value is the same f32 the old per-coefficient `powf` produced, so the
931+/// quantised integers do not move.
932+fn quantise_one(x: f32, gain: f32) -> i32 {
904933 if x == 0.0 {
905934 return 0;
906935 }
907- let gain = 2f32.powf(-(sf - SF_OFFSET) as f32 * 0.25);
908936 // x^0.75 == sqrt(x * sqrt(x)): two sqrtf beat one powf by a wide margin,
909937 // and this call site is the rate loop's inner-inner loop.
910938 let v = x.abs() * gain;