forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-aacperf': the rate loop stops calling powf

Browse files at this commit

ec41fcd655671173f645887baf275c8773d73921Ahmet Kaan GümüşParent: 3115219d08a7f7
82% of encode time was libm powf inside the scalefactor search: a
lookup table for q^(4/3), sqrt-composed x^0.75, and bisection over the
already-monotonic noise walk take the bench row from 3.5x to 37.8x
realtime with every quality test green. Also fixes the oracle test's
esds parser, which scanned for a literal tag byte instead of walking
the descriptor tree and false-positived on shifted bitrate bytes.
crates/ec-aac/src/encode.rsmodified+34 -8
@@ -20,6 +20,8 @@
20 //! rather than iterators.20 //! rather than iterators.
21 #![allow(clippy::needless_range_loop)]21 #![allow(clippy::needless_range_loop)]
22 22
23+use std::sync::LazyLock;
24+
23 use ec_core::{BitWriter, Error, Result};25 use ec_core::{BitWriter, Error, Result};
24 use ec_dsp::{Complex, Mdct, RealFft, Window};26 use ec_dsp::{Complex, Mdct, RealFft, Window};
25 27
@@ -650,14 +652,22 @@ impl AacEncoder {
650 continue;652 continue;
651 }653 }
652 floor[b] = sf_floor(peak).max(0);654 floor[b] = sf_floor(peak).max(0);
655+ // Noise is non-decreasing in level (coarser step -> more error),
656+ // the same assumption the original linear scan relied on to break
657+ // on first overage; bisect for the largest level still under
658+ // target instead of walking it one step at a time.
653 let mut best = floor[b];659 let mut best = floor[b];
654- let mut level = best;660+ if self.band_noise(frame, swb, b, floor[b], windows, short) <= target {
655- while level < 255 {661+ let (mut lo, mut hi) = (floor[b], 255);
656- if self.band_noise(frame, swb, b, level, windows, short) > target {662+ while lo < hi {
657- break;663+ let mid = lo + (hi - lo + 1) / 2;
664+ if self.band_noise(frame, swb, b, mid, windows, short) <= target {
665+ lo = mid;
666+ } else {
667+ hi = mid - 1;
668+ }
658 }669 }
659- best = level;670+ best = lo;
660- level += 1;
661 }671 }
662 sf[b] = best.clamp(floor[b], 255);672 sf[b] = best.clamp(floor[b], 255);
663 zero[b] = false;673 zero[b] = false;
@@ -766,7 +776,7 @@ impl AacEncoder {
766 for k in base + lo..base + hi {776 for k in base + lo..base + hi {
767 let x = frame.coef[k];777 let x = frame.coef[k];
768 let q = quantise_one(x, sf);778 let q = quantise_one(x, sf);
769- let mut back = (q.unsigned_abs() as f32).powf(4.0 / 3.0) * gain;779+ let mut back = pow43(q.unsigned_abs()) * gain;
770 if q < 0 {780 if q < 0 {
771 back = -back;781 back = -back;
772 }782 }
@@ -839,6 +849,19 @@ fn apply_ms(pair: &mut [ChannelFrame], ms: &[bool], swb: &[u16], max_sfb: usize,
839 }849 }
840 }850 }
841 851
852+/// `q^(4/3)` for the (small, bounded-by-`MAX_QUANT`) integer magnitudes the
853+/// rate loop's noise search re-tests at every candidate scalefactor: a table
854+/// lookup replaces what was otherwise the encoder's hottest `powf` call site.
855+static POW43: LazyLock<Vec<f32>> = LazyLock::new(|| {
856+ (0..=MAX_QUANT as u32)
857+ .map(|q| (q as f32).powf(4.0 / 3.0))
858+ .collect()
859+});
860+
861+fn pow43(q: u32) -> f32 {
862+ POW43[q.min(MAX_QUANT as u32) as usize]
863+}
864+
842 /// The finest scalefactor at which a peak of this size still fits the escape865 /// The finest scalefactor at which a peak of this size still fits the escape
843 /// codebook: below it the quantiser saturates, which costs bits *and* adds866 /// codebook: below it the quantiser saturates, which costs bits *and* adds
844 /// distortion.867 /// distortion.
@@ -852,7 +875,10 @@ fn quantise_one(x: f32, sf: i32) -> i32 {
852 return 0;875 return 0;
853 }876 }
854 let gain = 2f32.powf(-(sf - SF_OFFSET) as f32 * 0.25);877 let gain = 2f32.powf(-(sf - SF_OFFSET) as f32 * 0.25);
855- let q = ((x.abs() * gain).powf(0.75) + ROUND_BIAS) as i32;878+ // x^0.75 == sqrt(x * sqrt(x)): two sqrtf beat one powf by a wide margin,
879+ // and this call site is the rate loop's inner-inner loop.
880+ let v = x.abs() * gain;
881+ let q = (v.sqrt() * v.sqrt().sqrt() + ROUND_BIAS) as i32;
856 let q = q.min(MAX_QUANT);882 let q = q.min(MAX_QUANT);
857 if x < 0.0 { -q } else { q }883 if x < 0.0 { -q } else { q }
858 }884 }
crates/ec-aac/tests/oracle.rsmodified+38 -12
@@ -627,23 +627,49 @@ fn asc_matches_the_one_ffmpeg_writes() {
627 /// The DecoderSpecificInfo payload of the first `esds` box: tag 0x05 inside the627 /// The DecoderSpecificInfo payload of the first `esds` box: tag 0x05 inside the
628 /// DecoderConfigDescriptor, with the usual 7-bit length encoding.628 /// DecoderConfigDescriptor, with the usual 7-bit length encoding.
629 fn esds_asc(mp4: &[u8]) -> Option<Vec<u8>> {629 fn esds_asc(mp4: &[u8]) -> Option<Vec<u8>> {
630+ // Walks the MPEG-4 descriptor tree (tag, BER length, payload) rather than
631+ // scanning raw bytes for 0x05: a scalar field elsewhere in the tree
632+ // (e.g. avgBitrate) can legitimately contain that byte value, which a
633+ // blind byte scan would mistake for the DecSpecificInfoTag.
630 let at = mp4.windows(4).position(|w| w == b"esds")?;634 let at = mp4.windows(4).position(|w| w == b"esds")?;
631- let mut i = at + 4;635+ let mut i = at + 4 + 4; // "esds" tag, then the box's version+flags
632 while i < mp4.len() {636 while i < mp4.len() {
633- if mp4[i] == 0x05 {637+ let tag = mp4[i];
634- let mut j = i + 1;638+ i += 1;
635- let mut len = 0usize;639+ let mut len = 0usize;
636- loop {640+ loop {
637- let b = *mp4.get(j)?;641+ let b = *mp4.get(i)?;
638- len = (len << 7) | usize::from(b & 0x7F);642+ i += 1;
639- j += 1;643+ len = (len << 7) | usize::from(b & 0x7F);
640- if b & 0x80 == 0 {644+ if b & 0x80 == 0 {
641- break;645+ break;
646+ }
647+ }
648+ match tag {
649+ 0x05 => return mp4.get(i..i + len).map(<[u8]>::to_vec),
650+ // ES_DescrTag: ES_ID(2) + flags(1), then optional fields the
651+ // flags select, before its nested descriptors.
652+ 0x03 => {
653+ let flags = *mp4.get(i + 2)?;
654+ i += 3;
655+ if flags & 0x80 != 0 {
656+ i += 2; // dependsOn ES_ID
657+ }
658+ if flags & 0x40 != 0 {
659+ let url_len = usize::from(*mp4.get(i)?);
660+ i += 1 + url_len;
661+ }
662+ if flags & 0x20 != 0 {
663+ i += 2; // OCR ES_ID
642 }664 }
643 }665 }
644- return mp4.get(j..j + len).map(<[u8]>::to_vec);666+ // DecoderConfigDescrTag: objectType(1) + streamType/flags(1) +
667+ // bufferSizeDB(3) + maxBitrate(4) + avgBitrate(4), then its
668+ // nested DecSpecificInfo.
669+ 0x04 => i += 13,
670+ // An opaque leaf descriptor: its whole payload is `len` bytes.
671+ _ => i += len,
645 }672 }
646- i += 1;
647 }673 }
648 None674 None
649 }675 }