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 | // so the finest quantisation that fits the budget is a bisection -- | 523 | // so the finest quantisation that fits the budget is a bisection -- |
| 524 | // which also means the psychoacoustic model only has to get the shape | 524 | // which also means the psychoacoustic model only has to get the shape |
| 525 | // of the allocation right, not its absolute level. | 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 | let cost = |enc: &AacEncoder, offset: i32| -> (i32, Vec<Coded>) { | 538 | let cost = |enc: &AacEncoder, offset: i32| -> (i32, Vec<Coded>) { |
| 527 | let mut coded = Vec::new(); | 539 | let mut coded = Vec::new(); |
| 528 | let mut bits = 10i32; // END element plus byte-alignment slack | 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 | bits += 3 | 542 | bits += 3 |
| 531 | + 4 | 543 | + 4 |
| 532 | + if plan.element == Element::Cpe { | 544 | + if plan.element == Element::Cpe { |
| @@ -534,8 +546,8 @@ impl AacEncoder { | |||
| 534 | } else { | 546 | } else { |
| 535 | 0 | 547 | 0 |
| 536 | }; | 548 | }; |
| 537 | - for frame in &plan.frames { | 549 | + for (frame, profile) in plan.frames.iter().zip(profiles) { |
| 538 | - let c = enc.quantise(frame, seq, swb, max_sfb, offset); | 550 | + let c = enc.quantise(frame, profile, seq, swb, max_sfb, offset); |
| 539 | bits += c.bits; | 551 | bits += c.bits; |
| 540 | coded.push(c); | 552 | coded.push(c); |
| 541 | } | 553 | } |
| @@ -543,8 +555,8 @@ impl AacEncoder { | |||
| 543 | (bits, coded) | 555 | (bits, coded) |
| 544 | }; | 556 | }; |
| 545 | let (mut lo, mut hi) = (-240i32, 240i32); | 557 | let (mut lo, mut hi) = (-240i32, 240i32); |
| 546 | - let (mut bits, mut coded) = cost(self, hi); | 558 | + let (probe_bits, mut coded) = cost(self, hi); |
| 547 | - if bits > budget { | 559 | + if probe_bits > budget { |
| 548 | // Even the coarsest setting overflows: take it and let the | 560 | // Even the coarsest setting overflows: take it and let the |
| 549 | // reservoir absorb the overshoot. | 561 | // reservoir absorb the overshoot. |
| 550 | lo = hi; | 562 | lo = hi; |
| @@ -554,21 +566,16 @@ impl AacEncoder { | |||
| 554 | let (b, c) = cost(self, mid); | 566 | let (b, c) = cost(self, mid); |
| 555 | if b <= budget { | 567 | if b <= budget { |
| 556 | hi = mid; | 568 | hi = mid; |
| 557 | - bits = b; | ||
| 558 | coded = c; | 569 | coded = c; |
| 559 | } else { | 570 | } else { |
| 560 | lo = mid + 1; | 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 | let mut w = BitWriter::new(); | 580 | let mut w = BitWriter::new(); |
| 574 | let mut at = 0usize; | 581 | let mut at = 0usize; |
| @@ -612,6 +619,7 @@ impl AacEncoder { | |||
| 612 | fn quantise( | 619 | fn quantise( |
| 613 | &self, | 620 | &self, |
| 614 | frame: &ChannelFrame, | 621 | frame: &ChannelFrame, |
| 622 | + profile: &BandProfile, | ||
| 615 | seq: WindowSequence, | 623 | seq: WindowSequence, |
| 616 | swb: &'static [u16], | 624 | swb: &'static [u16], |
| 617 | max_sfb: usize, | 625 | max_sfb: usize, |
| @@ -623,15 +631,7 @@ impl AacEncoder { | |||
| 623 | let mut floor = vec![0i32; max_sfb]; | 631 | let mut floor = vec![0i32; max_sfb]; |
| 624 | let mut zero = vec![true; max_sfb]; | 632 | let mut zero = vec![true; max_sfb]; |
| 625 | for b in 0..max_sfb { | 633 | for b in 0..max_sfb { |
| 626 | - let (lo, hi) = (usize::from(swb[b]), usize::from(swb[b + 1])); | 634 | + if profile.peak[b] < 1e-4 { |
| 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 { | ||
| 635 | continue; | 635 | continue; |
| 636 | } | 636 | } |
| 637 | // The rate loop scales the whole mask rather than shifting the | 637 | // The rate loop scales the whole mask rather than shifting the |
| @@ -639,19 +639,10 @@ impl AacEncoder { | |||
| 639 | // asked for, and lets a band whose signal falls under its own mask | 639 | // asked for, and lets a band whose signal falls under its own mask |
| 640 | // drop out entirely instead of being coded ever more coarsely. | 640 | // drop out entirely instead of being coded ever more coarsely. |
| 641 | let target = frame.threshold[b] * 2f32.powf(offset as f32 * 0.5); | 641 | let target = frame.threshold[b] * 2f32.powf(offset as f32 * 0.5); |
| 642 | - let mut energy = 0.0f32; | 642 | + if profile.energy[b] / profile.count[b].max(1) as f32 <= target { |
| 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 { | ||
| 652 | continue; | 643 | continue; |
| 653 | } | 644 | } |
| 654 | - floor[b] = sf_floor(peak).max(0); | 645 | + floor[b] = profile.floor[b]; |
| 655 | // Noise is non-decreasing in level (coarser step -> more error), | 646 | // Noise is non-decreasing in level (coarser step -> more error), |
| 656 | // the same assumption the original linear scan relied on to break | 647 | // the same assumption the original linear scan relied on to break |
| 657 | // on first overage; bisect for the largest level still under | 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 | ms > lr * 1.5 | 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 | fn apply_ms(pair: &mut [ChannelFrame], ms: &[bool], swb: &[u16], max_sfb: usize, short: bool) { | 860 | fn apply_ms(pair: &mut [ChannelFrame], ms: &[bool], swb: &[u16], max_sfb: usize, short: bool) { |
| 831 | let windows = if short { 8 } else { 1 }; | 861 | let windows = if short { 8 } else { 1 }; |
| 832 | for b in 0..max_sfb { | 862 | for b in 0..max_sfb { |