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 commit0fe46e1816efd6cf81a90f82345bc1f0c47c4788Parent: 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)
1 changed file +34 -6
- crates/ec-aac/src/encode.rs +34 -6
crates/ec-aac/src/encode.rsmodified+34 -6
| @@ -638,7 +638,7 @@ impl AacEncoder { | ||
| 638 | 638 | // scalefactors: that keeps the noise following the shape the model |
| 639 | 639 | // asked for, and lets a band whose signal falls under its own mask |
| 640 | 640 | // 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]; | |
| 642 | 642 | if profile.energy[b] / profile.count[b].max(1) as f32 <= target { |
| 643 | 643 | continue; |
| 644 | 644 | } |
| @@ -693,11 +693,12 @@ impl AacEncoder { | ||
| 693 | 693 | } |
| 694 | 694 | continue; |
| 695 | 695 | } |
| 696 | + let gain = pow_quarter(SF_OFFSET - sf[b]); | |
| 696 | 697 | let mut peak = 0i32; |
| 697 | 698 | for w in 0..windows { |
| 698 | 699 | let base = if short { w * SHORT_LEN } else { 0 }; |
| 699 | 700 | 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); | |
| 701 | 702 | quant[base + k] = q; |
| 702 | 703 | peak = peak.max(q.abs()); |
| 703 | 704 | } |
| @@ -759,14 +760,15 @@ impl AacEncoder { | ||
| 759 | 760 | short: bool, |
| 760 | 761 | ) -> f32 { |
| 761 | 762 | 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); | |
| 763 | 765 | let mut noise = 0.0f32; |
| 764 | 766 | let mut count = 0usize; |
| 765 | 767 | for w in 0..windows { |
| 766 | 768 | let base = if short { w * SHORT_LEN } else { 0 }; |
| 767 | 769 | for k in base + lo..base + hi { |
| 768 | 770 | let x = frame.coef[k]; |
| 769 | - let q = quantise_one(x, sf); | |
| 771 | + let q = quantise_one(x, gain_inv); | |
| 770 | 772 | let mut back = pow43(q.unsigned_abs()) * gain; |
| 771 | 773 | if q < 0 { |
| 772 | 774 | back = -back; |
| @@ -888,6 +890,29 @@ static POW43: LazyLock<Vec<f32>> = LazyLock::new(|| { | ||
| 888 | 890 | .collect() |
| 889 | 891 | }); |
| 890 | 892 | |
| 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 | + | |
| 891 | 916 | fn pow43(q: u32) -> f32 { |
| 892 | 917 | POW43[q.min(MAX_QUANT as u32) as usize] |
| 893 | 918 | } |
| @@ -900,11 +925,14 @@ fn sf_floor(peak: f32) -> i32 { | ||
| 900 | 925 | SF_OFFSET + want.ceil() as i32 |
| 901 | 926 | } |
| 902 | 927 | |
| 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 { | |
| 904 | 933 | if x == 0.0 { |
| 905 | 934 | return 0; |
| 906 | 935 | } |
| 907 | - let gain = 2f32.powf(-(sf - SF_OFFSET) as f32 * 0.25); | |
| 908 | 936 | // x^0.75 == sqrt(x * sqrt(x)): two sqrtf beat one powf by a wide margin, |
| 909 | 937 | // and this call site is the rate loop's inner-inner loop. |
| 910 | 938 | let v = x.abs() * gain; |