forge
Explore
Log in

Don't you dare go Hollow

The frame's noise search is memoised per (channel, band, scalefactor) -- band_noise is pure for one frame's coefficients, and the outer bisection's probes re-ask the same pairs, so each pair computes at most once per frame (stamp/cur generation, no clearing pass)

Browse files at this commit

6d09e0a133d2be310b24e77e63efb2037fc52b19Ahmet Kaan GümüşParent: 0fe46e1
  • perf
    • the frame's noise search is memoised per (channel, band, scalefactor) -- band_noise is pure for one frame's coefficients, and the outer bisection's probes re-ask the same pairs, so each pair computes at most once per frame (stamp/cur generation, no clearing pass)
    • fresh NoiseMemo per write_frame, capacity channels*max_sfb*256. The first cut keyed (band, sf) only and the byte-identity gate caught it instantly: stereo's two channels collided in the slots and one channel replayed the other's noise (10x deltas) -- the key now carries the channel slot. Byte-identical on stereo 256k and 5.1 512k vs base
    • stereo 42.1 -> 59.1x RT, 5.1 15.6 -> 21.8x (quiet-core min-of-7, both harnesses agreeing)
    • ec-aac suite green 75/75
Show full message
perf(aac): the frame's noise search is memoised per (channel, band, scalefactor) -- band_noise is pure for one frame's coefficients, and the outer bisection's probes re-ask the same pairs, so each pair computes at most once per frame (stamp/cur generation, no clearing pass); fresh NoiseMemo per write_frame, capacity channels*max_sfb*256. The first cut keyed (band, sf) only and the byte-identity gate caught it instantly: stereo's two channels collided in the slots and one channel replayed the other's noise (10x deltas) -- the key now carries the channel slot. Byte-identical on stereo 256k and 5.1 512k vs base; stereo 42.1 -> 59.1x RT, 5.1 15.6 -> 21.8x (quiet-core min-of-7, both harnesses agreeing); ec-aac suite green 75/75
crates/ec-aac/src/encode.rsmodified+61 -4
@@ -535,9 +535,14 @@ impl AacEncoder {
535 .collect()535 .collect()
536 })536 })
537 .collect();537 .collect();
538- let cost = |enc: &AacEncoder, offset: i32| -> (i32, Vec<Coded>) {538+ // Every noise query a probe makes is memoised for the frame: the
539+ // bisection probes revisit the same (band, scalefactor) pairs across
540+ // the outer loop, and band_noise is pure per frame.
541+ let mut memo = NoiseMemo::new(self.channels * max_sfb * 256);
542+ let mut cost = |enc: &AacEncoder, offset: i32| -> (i32, Vec<Coded>) {
539 let mut coded = Vec::new();543 let mut coded = Vec::new();
540 let mut bits = 10i32; // END element plus byte-alignment slack544 let mut bits = 10i32; // END element plus byte-alignment slack
545+ let mut ch_slot = 0usize;
541 for (plan, profiles) in elements.iter().zip(&profiles) {546 for (plan, profiles) in elements.iter().zip(&profiles) {
542 bits += 3547 bits += 3
543 + 4548 + 4
@@ -547,9 +552,11 @@ impl AacEncoder {
547 0552 0
548 };553 };
549 for (frame, profile) in plan.frames.iter().zip(profiles) {554 for (frame, profile) in plan.frames.iter().zip(profiles) {
550- let c = enc.quantise(frame, profile, seq, swb, max_sfb, offset);555+ let c =
556+ enc.quantise(frame, profile, &mut memo, ch_slot, seq, swb, max_sfb, offset);
551 bits += c.bits;557 bits += c.bits;
552 coded.push(c);558 coded.push(c);
559+ ch_slot += 1;
553 }560 }
554 }561 }
555 (bits, coded)562 (bits, coded)
@@ -620,6 +627,8 @@ impl AacEncoder {
620 &self,627 &self,
621 frame: &ChannelFrame,628 frame: &ChannelFrame,
622 profile: &BandProfile,629 profile: &BandProfile,
630+ memo: &mut NoiseMemo,
631+ ch_slot: usize,
623 seq: WindowSequence,632 seq: WindowSequence,
624 swb: &'static [u16],633 swb: &'static [u16],
625 max_sfb: usize,634 max_sfb: usize,
@@ -648,11 +657,20 @@ impl AacEncoder {
648 // on first overage; bisect for the largest level still under657 // on first overage; bisect for the largest level still under
649 // target instead of walking it one step at a time.658 // target instead of walking it one step at a time.
650 let mut best = floor[b];659 let mut best = floor[b];
651- if self.band_noise(frame, swb, b, floor[b], windows, short) <= target {660+ if memo.noise(
661+ ch_slot,
662+ max_sfb,
663+ b,
664+ floor[b],
665+ || self.band_noise(frame, swb, b, floor[b], windows, short),
666+ ) <= target
667+ {
652 let (mut lo, mut hi) = (floor[b], 255);668 let (mut lo, mut hi) = (floor[b], 255);
653 while lo < hi {669 while lo < hi {
654 let mid = lo + (hi - lo + 1) / 2;670 let mid = lo + (hi - lo + 1) / 2;
655- if self.band_noise(frame, swb, b, mid, windows, short) <= target {671+ if memo.noise(ch_slot, max_sfb, b, mid, || {
672+ self.band_noise(frame, swb, b, mid, windows, short)
673+ }) <= target {
656 lo = mid;674 lo = mid;
657 } else {675 } else {
658 hi = mid - 1;676 hi = mid - 1;
@@ -859,6 +877,45 @@ fn band_profile(frame: &ChannelFrame, swb: &[u16], max_sfb: usize, short: bool)
859 }877 }
860 }878 }
861 879
880+/// Per-frame cache for the noise search: `band_noise(band, sf)` is pure for
881+/// one frame's coefficients, and the outer rate loop's probes re-ask the same
882+/// (band, scalefactor) pairs, so each pair is computed at most once per frame
883+/// clearing pass.
884+struct NoiseMemo {
885+ stamp: Vec<u32>,
886+ val: Vec<f32>,
887+ cur: u32,
888+}
889+
890+impl NoiseMemo {
891+ fn new(capacity: usize) -> NoiseMemo {
892+ NoiseMemo {
893+ stamp: vec![0; capacity],
894+ val: vec![0.0; capacity],
895+ cur: 1,
896+ }
897+ }
898+
899+ /// The memoised noise at `(band, sf)`, computing through `f` on a miss.
900+ fn noise(
901+ &mut self,
902+ ch_slot: usize,
903+ max_sfb: usize,
904+ band: usize,
905+ sf: i32,
906+ f: impl FnOnce() -> f32,
907+ ) -> f32 {
908+ let idx = (ch_slot * max_sfb + band) * 256 + sf as usize;
909+ if self.stamp[idx] == self.cur {
910+ return self.val[idx];
911+ }
912+ let noise = f();
913+ self.stamp[idx] = self.cur;
914+ self.val[idx] = noise;
915+ noise
916+ }
917+}
918+
862 fn apply_ms(pair: &mut [ChannelFrame], ms: &[bool], swb: &[u16], max_sfb: usize, short: bool) {919 fn apply_ms(pair: &mut [ChannelFrame], ms: &[bool], swb: &[u16], max_sfb: usize, short: bool) {
863 let windows = if short { 8 } else { 1 };920 let windows = if short { 8 } else { 1 };
864 for b in 0..max_sfb {921 for b in 0..max_sfb {