The outer rate loop warm-starts at last frame's working point and gallops to the edge -- bits fall monotonically as the offset rises, so fits-the-budget is true on an upper interval and the full-range bisection and a gallop from the previous frame's answer find the same left edge
Browse files at this commit17417bc6f1627c5d5ed17cdea9ffa9aa3e12c7c0Parent: 6d09e0a- perf
- the outer rate loop warm-starts at last frame's working point and gallops to the edge -- bits fall monotonically as the offset rises, so fits-the-budget is true on an upper interval and the full-range bisection and a gallop from the previous frame's answer find the same left edge
- steady state costs a probe at 240, one at the warm offset and two-to-three bracket probes instead of nine (warm_offset field, AacEncoder). The requant passes skip bands the +/-60 delta chain left alone (dirty vector) -- measured neutral on its own, kept because it is the same class of work the noise memo already avoids. Byte-identical on stereo 256k and 5.1 512k vs the base build
- stereo 59.1 -> 103.1x RT (1.31x rusty_aac's 78.4x single-thread, was 0.44x at the lane start), 5.1 21.8 -> 44.3x (1.44x rusty's 30.8x)
- ec-aac suite green 75/75
- quality rows unchanged (0.8857/0.9827/0.9829 worst-channel vs the same synthetic program, ec still clearing rusty on every one)
1 changed file +90 -15
- crates/ec-aac/src/encode.rs +90 -15
crates/ec-aac/src/encode.rsmodified+90 -15
| @@ -122,6 +122,9 @@ pub struct AacEncoder { | |||
| 122 | queue: std::collections::VecDeque<EncodedPacket>, | 122 | queue: std::collections::VecDeque<EncodedPacket>, |
| 123 | frames_out: i64, | 123 | frames_out: i64, |
| 124 | reservoir: i32, | 124 | reservoir: i32, |
| 125 | + /// The rate loop's working point from the previous frame, where the next | ||
| 126 | + /// frame's search starts looking for its own edge. | ||
| 127 | + warm_offset: i32, | ||
| 125 | flushed: bool, | 128 | flushed: bool, |
| 126 | initialised: bool, | 129 | initialised: bool, |
| 127 | } | 130 | } |
| @@ -155,6 +158,7 @@ impl AacEncoder { | |||
| 155 | queue: std::collections::VecDeque::new(), | 158 | queue: std::collections::VecDeque::new(), |
| 156 | frames_out: 0, | 159 | frames_out: 0, |
| 157 | reservoir: 0, | 160 | reservoir: 0, |
| 161 | + warm_offset: 0, | ||
| 158 | flushed: false, | 162 | flushed: false, |
| 159 | initialised: false, | 163 | initialised: false, |
| 160 | } | 164 | } |
| @@ -561,28 +565,88 @@ impl AacEncoder { | |||
| 561 | } | 565 | } |
| 562 | (bits, coded) | 566 | (bits, coded) |
| 563 | }; | 567 | }; |
| 564 | - let (mut lo, mut hi) = (-240i32, 240i32); | 568 | + let (probe_bits, mut coded) = cost(self, 240); |
| 565 | - let (probe_bits, mut coded) = cost(self, hi); | 569 | + let mut answer = 240i32; |
| 566 | if probe_bits > budget { | 570 | if probe_bits > budget { |
| 567 | // Even the coarsest setting overflows: take it and let the | 571 | // Even the coarsest setting overflows: take it and let the |
| 568 | // reservoir absorb the overshoot. | 572 | // reservoir absorb the overshoot. |
| 569 | - lo = hi; | ||
| 570 | } else { | 573 | } else { |
| 571 | - while lo < hi { | 574 | + // Bits fall monotonically as the offset rises, so "fits the |
| 572 | - let mid = lo + (hi - lo) / 2; | 575 | + // budget" is true on an upper interval and the finest fitting |
| 573 | - let (b, c) = cost(self, mid); | 576 | + // setting is its left edge. Last frame's working point sits next |
| 574 | - if b <= budget { | 577 | + // to that edge, so probe it first and gallop outward until one |
| 575 | - hi = mid; | 578 | + // side is known to fail, then bisect the short bracket -- the |
| 576 | - coded = c; | 579 | + // same edge a full-range bisection would find, in a few probes. |
| 577 | - } else { | 580 | + let start = self.warm_offset.clamp(-240, 240); |
| 578 | - lo = mid + 1; | 581 | + let (start_bits, start_coded) = cost(self, start); |
| 582 | + if start_bits <= budget { | ||
| 583 | + // Known to fit: the edge is at or below `start`. Walk down. | ||
| 584 | + let mut hi = start; | ||
| 585 | + coded = start_coded; | ||
| 586 | + let mut fail = None; | ||
| 587 | + let mut step = 1i32; | ||
| 588 | + loop { | ||
| 589 | + let probe = (start - step).max(-240); | ||
| 590 | + let (b, c) = cost(self, probe); | ||
| 591 | + if b <= budget { | ||
| 592 | + hi = probe; | ||
| 593 | + coded = c; | ||
| 594 | + } else { | ||
| 595 | + fail = Some(probe); | ||
| 596 | + } | ||
| 597 | + let done = probe == -240 || fail.is_some(); | ||
| 598 | + if done { | ||
| 599 | + break; | ||
| 600 | + } | ||
| 601 | + step *= 2; | ||
| 602 | + } | ||
| 603 | + if let Some(mut lo) = fail { | ||
| 604 | + while hi - lo > 1 { | ||
| 605 | + let mid = lo + (hi - lo + 1) / 2; | ||
| 606 | + let (b, c) = cost(self, mid); | ||
| 607 | + if b <= budget { | ||
| 608 | + hi = mid; | ||
| 609 | + coded = c; | ||
| 610 | + } else { | ||
| 611 | + lo = mid; | ||
| 612 | + } | ||
| 613 | + } | ||
| 614 | + } | ||
| 615 | + answer = hi; | ||
| 616 | + } else { | ||
| 617 | + // `start` sits past the edge: the answer is above it. Walk up. | ||
| 618 | + let mut lo = start; | ||
| 619 | + let mut hi = 240i32; // known to fit from the first probe | ||
| 620 | + let mut step = 1i32; | ||
| 621 | + loop { | ||
| 622 | + let probe = (start + step).min(240); | ||
| 623 | + if probe == 240 { | ||
| 624 | + // Already probed and known to fit; `hi` keeps 240. | ||
| 625 | + break; | ||
| 626 | + } | ||
| 627 | + let (b, c) = cost(self, probe); | ||
| 628 | + if b <= budget { | ||
| 629 | + hi = probe; | ||
| 630 | + coded = c; | ||
| 631 | + break; | ||
| 632 | + } | ||
| 633 | + lo = probe; | ||
| 634 | + step *= 2; | ||
| 635 | + } | ||
| 636 | + while hi - lo > 1 { | ||
| 637 | + let mid = lo + (hi - lo + 1) / 2; | ||
| 638 | + let (b, c) = cost(self, mid); | ||
| 639 | + if b <= budget { | ||
| 640 | + hi = mid; | ||
| 641 | + coded = c; | ||
| 642 | + } else { | ||
| 643 | + lo = mid; | ||
| 644 | + } | ||
| 579 | } | 645 | } |
| 646 | + answer = hi; | ||
| 580 | } | 647 | } |
| 581 | } | 648 | } |
| 582 | - // When the loop ends, `coded` is the probe at `lo`: `hi` only ever | 649 | + self.warm_offset = answer; |
| 583 | - // moves to a probe that was taken, and `lo` converges to `hi` (the | ||
| 584 | - // overflow guard above leaves both at the coarsest setting). | ||
| 585 | - let _ = lo; | ||
| 586 | 650 | ||
| 587 | let mut w = BitWriter::new(); | 651 | let mut w = BitWriter::new(); |
| 588 | let mut at = 0usize; | 652 | let mut at = 0usize; |
| @@ -684,6 +748,9 @@ impl AacEncoder { | |||
| 684 | 748 | ||
| 685 | let mut quant = vec![0i32; FRAME_LEN]; | 749 | let mut quant = vec![0i32; FRAME_LEN]; |
| 686 | let mut books = vec![0u8; max_sfb]; | 750 | let mut books = vec![0u8; max_sfb]; |
| 751 | + // Bands whose scalefactor the delta chain moved since they were last | ||
| 752 | + // quantised; every band is dirty on the first pass. | ||
| 753 | + let mut dirty = vec![true; max_sfb]; | ||
| 687 | for _ in 0..4 { | 754 | for _ in 0..4 { |
| 688 | let mut settled = true; | 755 | let mut settled = true; |
| 689 | let mut prev: Option<i32> = None; | 756 | let mut prev: Option<i32> = None; |
| @@ -697,6 +764,7 @@ impl AacEncoder { | |||
| 697 | }; | 764 | }; |
| 698 | if fixed != sf[b] { | 765 | if fixed != sf[b] { |
| 699 | sf[b] = fixed; | 766 | sf[b] = fixed; |
| 767 | + dirty[b] = true; | ||
| 700 | settled = false; | 768 | settled = false; |
| 701 | } | 769 | } |
| 702 | prev = Some(sf[b]); | 770 | prev = Some(sf[b]); |
| @@ -709,6 +777,12 @@ impl AacEncoder { | |||
| 709 | let base = if short { w * SHORT_LEN } else { 0 }; | 777 | let base = if short { w * SHORT_LEN } else { 0 }; |
| 710 | quant[base + lo..base + hi].fill(0); | 778 | quant[base + lo..base + hi].fill(0); |
| 711 | } | 779 | } |
| 780 | + dirty[b] = false; | ||
| 781 | + continue; | ||
| 782 | + } | ||
| 783 | + // A band the delta chain left alone keeps its quantised | ||
| 784 | + // values, its peak and its codebook from the previous pass. | ||
| 785 | + if !dirty[b] { | ||
| 712 | continue; | 786 | continue; |
| 713 | } | 787 | } |
| 714 | let gain = pow_quarter(SF_OFFSET - sf[b]); | 788 | let gain = pow_quarter(SF_OFFSET - sf[b]); |
| @@ -722,6 +796,7 @@ impl AacEncoder { | |||
| 722 | } | 796 | } |
| 723 | } | 797 | } |
| 724 | books[b] = codebook_for(peak); | 798 | books[b] = codebook_for(peak); |
| 799 | + dirty[b] = false; | ||
| 725 | if peak == 0 { | 800 | if peak == 0 { |
| 726 | // Nothing survived: the band leaves the delta chain, which | 801 | // Nothing survived: the band leaves the delta chain, which |
| 727 | // may free the bands after it to go coarser. | 802 | // may free the bands after it to go coarser. |