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 commit44ea57ea05237204e8eaf498069391a7d430d698Parent: 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
1 changed file +64 -34
- crates/ec-aac/src/encode.rs +64 -34
crates/ec-aac/src/encode.rsmodified+64 -34
| @@ -523,10 +523,22 @@ impl AacEncoder { | ||
| 523 | 523 | // so the finest quantisation that fits the budget is a bisection -- |
| 524 | 524 | // which also means the psychoacoustic model only has to get the shape |
| 525 | 525 | // 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(); | |
| 526 | 538 | let cost = |enc: &AacEncoder, offset: i32| -> (i32, Vec<Coded>) { |
| 527 | 539 | let mut coded = Vec::new(); |
| 528 | 540 | let mut bits = 10i32; // END element plus byte-alignment slack |
| 529 | - for plan in &elements { | |
| 541 | + for (plan, profiles) in elements.iter().zip(&profiles) { | |
| 530 | 542 | bits += 3 |
| 531 | 543 | + 4 |
| 532 | 544 | + if plan.element == Element::Cpe { |
| @@ -534,8 +546,8 @@ impl AacEncoder { | ||
| 534 | 546 | } else { |
| 535 | 547 | 0 |
| 536 | 548 | }; |
| 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); | |
| 539 | 551 | bits += c.bits; |
| 540 | 552 | coded.push(c); |
| 541 | 553 | } |
| @@ -543,8 +555,8 @@ impl AacEncoder { | ||
| 543 | 555 | (bits, coded) |
| 544 | 556 | }; |
| 545 | 557 | 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 { | |
| 548 | 560 | // Even the coarsest setting overflows: take it and let the |
| 549 | 561 | // reservoir absorb the overshoot. |
| 550 | 562 | lo = hi; |
| @@ -554,21 +566,16 @@ impl AacEncoder { | ||
| 554 | 566 | let (b, c) = cost(self, mid); |
| 555 | 567 | if b <= budget { |
| 556 | 568 | hi = mid; |
| 557 | - bits = b; | |
| 558 | 569 | coded = c; |
| 559 | 570 | } else { |
| 560 | 571 | lo = mid + 1; |
| 561 | 572 | } |
| 562 | 573 | } |
| 563 | - if lo < 240 { | |
| 564 | - let (b, c) = cost(self, lo); | |
| 565 | - if b <= budget { | |
| 566 | - bits = b; | |
| 567 | - coded = c; | |
| 568 | - } | |
| 569 | - } | |
| 570 | 574 | } |
| 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; | |
| 572 | 579 | |
| 573 | 580 | let mut w = BitWriter::new(); |
| 574 | 581 | let mut at = 0usize; |
| @@ -612,6 +619,7 @@ impl AacEncoder { | ||
| 612 | 619 | fn quantise( |
| 613 | 620 | &self, |
| 614 | 621 | frame: &ChannelFrame, |
| 622 | + profile: &BandProfile, | |
| 615 | 623 | seq: WindowSequence, |
| 616 | 624 | swb: &'static [u16], |
| 617 | 625 | max_sfb: usize, |
| @@ -623,15 +631,7 @@ impl AacEncoder { | ||
| 623 | 631 | let mut floor = vec![0i32; max_sfb]; |
| 624 | 632 | let mut zero = vec![true; max_sfb]; |
| 625 | 633 | 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 { | |
| 635 | 635 | continue; |
| 636 | 636 | } |
| 637 | 637 | // The rate loop scales the whole mask rather than shifting the |
| @@ -639,19 +639,10 @@ impl AacEncoder { | ||
| 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 | 641 | 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 { | |
| 652 | 643 | continue; |
| 653 | 644 | } |
| 654 | - floor[b] = sf_floor(peak).max(0); | |
| 645 | + floor[b] = profile.floor[b]; | |
| 655 | 646 | // Noise is non-decreasing in level (coarser step -> more error), |
| 656 | 647 | // the same assumption the original linear scan relied on to break |
| 657 | 648 | // 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: | ||
| 827 | 818 | ms > lr * 1.5 |
| 828 | 819 | } |
| 829 | 820 | |
| 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 | + | |
| 830 | 860 | fn apply_ms(pair: &mut [ChannelFrame], ms: &[bool], swb: &[u16], max_sfb: usize, short: bool) { |
| 831 | 861 | let windows = if short { 8 } else { 1 }; |
| 832 | 862 | for b in 0..max_sfb { |