forge
Explore
Log in

Don't you dare go Hollow

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

Browse files at this commit

44ea57ea05237204e8eaf498069391a7d430d698Ahmet Kaan GümüşParent: b1fb10a
  • perf
    • 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
Show full message
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.rsmodified+64 -34
@@ -523,10 +523,22 @@ impl AacEncoder {
523523 // so the finest quantisation that fits the budget is a bisection --
524524 // which also means the psychoacoustic model only has to get the shape
525525 // of the allocation right, not its absolute level.
526+ // Per-band peak, energy and scalefactor floor on the post-M/S
527+ // coefficients: none of them depend on the rate loop's offset, so
528+ // they are computed once per channel and read by every cost probe.
529+ let profiles: Vec<Vec<BandProfile>> = elements
530+ .iter()
531+ .map(|plan| {
532+ plan.frames
533+ .iter()
534+ .map(|frame| band_profile(frame, swb, max_sfb, short))
535+ .collect()
536+ })
537+ .collect();
526538 let cost = |enc: &AacEncoder, offset: i32| -> (i32, Vec<Coded>) {
527539 let mut coded = Vec::new();
528540 let mut bits = 10i32; // END element plus byte-alignment slack
529- for plan in &elements {
541+ for (plan, profiles) in elements.iter().zip(&profiles) {
530542 bits += 3
531543 + 4
532544 + if plan.element == Element::Cpe {
@@ -534,8 +546,8 @@ impl AacEncoder {
534546 } else {
535547 0
536548 };
537- for frame in &plan.frames {
538- let c = enc.quantise(frame, seq, swb, max_sfb, offset);
549+ for (frame, profile) in plan.frames.iter().zip(profiles) {
550+ let c = enc.quantise(frame, profile, seq, swb, max_sfb, offset);
539551 bits += c.bits;
540552 coded.push(c);
541553 }
@@ -543,8 +555,8 @@ impl AacEncoder {
543555 (bits, coded)
544556 };
545557 let (mut lo, mut hi) = (-240i32, 240i32);
546- let (mut bits, mut coded) = cost(self, hi);
547- if bits > budget {
558+ let (probe_bits, mut coded) = cost(self, hi);
559+ if probe_bits > budget {
548560 // Even the coarsest setting overflows: take it and let the
549561 // reservoir absorb the overshoot.
550562 lo = hi;
@@ -554,21 +566,16 @@ impl AacEncoder {
554566 let (b, c) = cost(self, mid);
555567 if b <= budget {
556568 hi = mid;
557- bits = b;
558569 coded = c;
559570 } else {
560571 lo = mid + 1;
561572 }
562573 }
563- if lo < 240 {
564- let (b, c) = cost(self, lo);
565- if b <= budget {
566- bits = b;
567- coded = c;
568- }
569- }
570574 }
571- let _ = (lo, bits);
575+ // When the loop ends, `coded` is the probe at `lo`: `hi` only ever
576+ // moves to a probe that was taken, and `lo` converges to `hi` (the
577+ // overflow guard above leaves both at the coarsest setting).
578+ let _ = lo;
572579
573580 let mut w = BitWriter::new();
574581 let mut at = 0usize;
@@ -612,6 +619,7 @@ impl AacEncoder {
612619 fn quantise(
613620 &self,
614621 frame: &ChannelFrame,
622+ profile: &BandProfile,
615623 seq: WindowSequence,
616624 swb: &'static [u16],
617625 max_sfb: usize,
@@ -623,15 +631,7 @@ impl AacEncoder {
623631 let mut floor = vec![0i32; max_sfb];
624632 let mut zero = vec![true; max_sfb];
625633 for b in 0..max_sfb {
626- let (lo, hi) = (usize::from(swb[b]), usize::from(swb[b + 1]));
627- let mut peak = 0.0f32;
628- for w in 0..windows {
629- let base = if short { w * SHORT_LEN } else { 0 };
630- for k in base + lo..base + hi {
631- peak = peak.max(frame.coef[k].abs());
632- }
633- }
634- if peak < 1e-4 {
634+ if profile.peak[b] < 1e-4 {
635635 continue;
636636 }
637637 // The rate loop scales the whole mask rather than shifting the
@@ -639,19 +639,10 @@ impl AacEncoder {
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.
641641 let target = frame.threshold[b] * 2f32.powf(offset as f32 * 0.5);
642- let mut energy = 0.0f32;
643- let mut count = 0usize;
644- for w in 0..windows {
645- let base = if short { w * SHORT_LEN } else { 0 };
646- for k in base + lo..base + hi {
647- energy += frame.coef[k] * frame.coef[k];
648- count += 1;
649- }
650- }
651- if energy / count.max(1) as f32 <= target {
642+ if profile.energy[b] / profile.count[b].max(1) as f32 <= target {
652643 continue;
653644 }
654- floor[b] = sf_floor(peak).max(0);
645+ floor[b] = profile.floor[b];
655646 // Noise is non-decreasing in level (coarser step -> more error),
656647 // the same assumption the original linear scan relied on to break
657648 // 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:
827818 ms > lr * 1.5
828819 }
829820
821+/// Per-band constants of one channel's quantised frame: the peak magnitude,
822+/// the coefficient energy and the escape-codebook scalefactor floor. All
823+/// three are independent of the rate loop's offset, so `write_frame` computes
824+/// them once and every cost probe reads them back.
825+struct BandProfile {
826+ peak: Vec<f32>,
827+ energy: Vec<f32>,
828+ /// Coefficient count per band, the energy's divisor.
829+ count: Vec<usize>,
830+ floor: Vec<i32>,
831+}
832+
833+fn band_profile(frame: &ChannelFrame, swb: &[u16], max_sfb: usize, short: bool) -> BandProfile {
834+ let windows = if short { 8 } else { 1 };
835+ let mut peak = vec![0.0f32; max_sfb];
836+ let mut energy = vec![0.0f32; max_sfb];
837+ let mut count = vec![0usize; max_sfb];
838+ let mut floor = vec![0i32; max_sfb];
839+ for b in 0..max_sfb {
840+ let (lo, hi) = (usize::from(swb[b]), usize::from(swb[b + 1]));
841+ for w in 0..windows {
842+ let base = if short { w * SHORT_LEN } else { 0 };
843+ for k in base + lo..base + hi {
844+ let c = frame.coef[k];
845+ peak[b] = peak[b].max(c.abs());
846+ energy[b] += c * c;
847+ count[b] += 1;
848+ }
849+ }
850+ floor[b] = sf_floor(peak[b]).max(0);
851+ }
852+ BandProfile {
853+ peak,
854+ energy,
855+ count,
856+ floor,
857+ }
858+}
859+
830860 fn apply_ms(pair: &mut [ChannelFrame], ms: &[bool], swb: &[u16], max_sfb: usize, short: bool) {
831861 let windows = if short { 8 } else { 1 };
832862 for b in 0..max_sfb {