From 44ea57ea05237204e8eaf498069391a7d430d698 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 13 Sep 2026 05:17:38 +0300 Subject: [PATCH] perf(aac): rate-loop band invariants hoisted out of the offset probes -- per-band peak, energy, count and escape floor on the post-M/S coefficients are offset-independent, so write_frame computes them once per channel (band_profile) and every bisection probe reads them back instead of rescanning 1024 lines ten-plus times per frame; the redundant final cost probe at the bisection's end is gone (coded already is the probe at lo: hi only moves to taken probes and lo converges to hi). Byte-identical output on stereo 256k and 5.1 512k synthetic program vs the base build; stereo 34.4 -> 37.9x RT, 5.1 12.9 -> 14.2x; ec-aac suite green --- crates/ec-aac/src/encode.rs | 98 ++++++++++++++++++++++++------------- 1 file changed, 64 insertions(+), 34 deletions(-) diff --git a/crates/ec-aac/src/encode.rs b/crates/ec-aac/src/encode.rs index 9a2dfbce..d5fc308d 100644 --- a/crates/ec-aac/src/encode.rs +++ b/crates/ec-aac/src/encode.rs @@ -523,10 +523,22 @@ impl AacEncoder { // so the finest quantisation that fits the budget is a bisection -- // which also means the psychoacoustic model only has to get the shape // of the allocation right, not its absolute level. + // Per-band peak, energy and scalefactor floor on the post-M/S + // coefficients: none of them depend on the rate loop's offset, so + // they are computed once per channel and read by every cost probe. + let profiles: Vec> = elements + .iter() + .map(|plan| { + plan.frames + .iter() + .map(|frame| band_profile(frame, swb, max_sfb, short)) + .collect() + }) + .collect(); let cost = |enc: &AacEncoder, offset: i32| -> (i32, Vec) { let mut coded = Vec::new(); let mut bits = 10i32; // END element plus byte-alignment slack - for plan in &elements { + for (plan, profiles) in elements.iter().zip(&profiles) { bits += 3 + 4 + if plan.element == Element::Cpe { @@ -534,8 +546,8 @@ impl AacEncoder { } else { 0 }; - for frame in &plan.frames { - let c = enc.quantise(frame, seq, swb, max_sfb, offset); + for (frame, profile) in plan.frames.iter().zip(profiles) { + let c = enc.quantise(frame, profile, seq, swb, max_sfb, offset); bits += c.bits; coded.push(c); } @@ -543,8 +555,8 @@ impl AacEncoder { (bits, coded) }; let (mut lo, mut hi) = (-240i32, 240i32); - let (mut bits, mut coded) = cost(self, hi); - if bits > budget { + let (probe_bits, mut coded) = cost(self, hi); + if probe_bits > budget { // Even the coarsest setting overflows: take it and let the // reservoir absorb the overshoot. lo = hi; @@ -554,21 +566,16 @@ impl AacEncoder { let (b, c) = cost(self, mid); if b <= budget { hi = mid; - bits = b; coded = c; } else { lo = mid + 1; } } - if lo < 240 { - let (b, c) = cost(self, lo); - if b <= budget { - bits = b; - coded = c; - } - } } - let _ = (lo, bits); + // When the loop ends, `coded` is the probe at `lo`: `hi` only ever + // moves to a probe that was taken, and `lo` converges to `hi` (the + // overflow guard above leaves both at the coarsest setting). + let _ = lo; let mut w = BitWriter::new(); let mut at = 0usize; @@ -612,6 +619,7 @@ impl AacEncoder { fn quantise( &self, frame: &ChannelFrame, + profile: &BandProfile, seq: WindowSequence, swb: &'static [u16], max_sfb: usize, @@ -623,15 +631,7 @@ impl AacEncoder { let mut floor = vec![0i32; max_sfb]; let mut zero = vec![true; max_sfb]; for b in 0..max_sfb { - let (lo, hi) = (usize::from(swb[b]), usize::from(swb[b + 1])); - let mut peak = 0.0f32; - for w in 0..windows { - let base = if short { w * SHORT_LEN } else { 0 }; - for k in base + lo..base + hi { - peak = peak.max(frame.coef[k].abs()); - } - } - if peak < 1e-4 { + if profile.peak[b] < 1e-4 { continue; } // The rate loop scales the whole mask rather than shifting the @@ -639,19 +639,10 @@ impl AacEncoder { // asked for, and lets a band whose signal falls under its own mask // drop out entirely instead of being coded ever more coarsely. let target = frame.threshold[b] * 2f32.powf(offset as f32 * 0.5); - let mut energy = 0.0f32; - let mut count = 0usize; - for w in 0..windows { - let base = if short { w * SHORT_LEN } else { 0 }; - for k in base + lo..base + hi { - energy += frame.coef[k] * frame.coef[k]; - count += 1; - } - } - if energy / count.max(1) as f32 <= target { + if profile.energy[b] / profile.count[b].max(1) as f32 <= target { continue; } - floor[b] = sf_floor(peak).max(0); + floor[b] = profile.floor[b]; // Noise is non-decreasing in level (coarser step -> more error), // the same assumption the original linear scan relied on to break // on first overage; bisect for the largest level still under @@ -827,6 +818,45 @@ fn ms_wins(l: &ChannelFrame, r: &ChannelFrame, swb: &[u16], band: usize, short: ms > lr * 1.5 } +/// Per-band constants of one channel's quantised frame: the peak magnitude, +/// the coefficient energy and the escape-codebook scalefactor floor. All +/// three are independent of the rate loop's offset, so `write_frame` computes +/// them once and every cost probe reads them back. +struct BandProfile { + peak: Vec, + energy: Vec, + /// Coefficient count per band, the energy's divisor. + count: Vec, + floor: Vec, +} + +fn band_profile(frame: &ChannelFrame, swb: &[u16], max_sfb: usize, short: bool) -> BandProfile { + let windows = if short { 8 } else { 1 }; + let mut peak = vec![0.0f32; max_sfb]; + let mut energy = vec![0.0f32; max_sfb]; + let mut count = vec![0usize; max_sfb]; + let mut floor = vec![0i32; max_sfb]; + for b in 0..max_sfb { + let (lo, hi) = (usize::from(swb[b]), usize::from(swb[b + 1])); + for w in 0..windows { + let base = if short { w * SHORT_LEN } else { 0 }; + for k in base + lo..base + hi { + let c = frame.coef[k]; + peak[b] = peak[b].max(c.abs()); + energy[b] += c * c; + count[b] += 1; + } + } + floor[b] = sf_floor(peak[b]).max(0); + } + BandProfile { + peak, + energy, + count, + floor, + } +} + fn apply_ms(pair: &mut [ChannelFrame], ms: &[bool], swb: &[u16], max_sfb: usize, short: bool) { let windows = if short { 8 } else { 1 }; for b in 0..max_sfb {