forge
Explore
Log in

Don't you dare go Hollow

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 commit

17417bc6f1627c5d5ed17cdea9ffa9aa3e12c7c0Ahmet Kaan GümüşParent: 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)
Show full message
perf(aac): 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)
crates/ec-aac/src/encode.rsmodified+90 -15
@@ -122,6 +122,9 @@ pub struct AacEncoder {
122122 queue: std::collections::VecDeque<EncodedPacket>,
123123 frames_out: i64,
124124 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,
125128 flushed: bool,
126129 initialised: bool,
127130 }
@@ -155,6 +158,7 @@ impl AacEncoder {
155158 queue: std::collections::VecDeque::new(),
156159 frames_out: 0,
157160 reservoir: 0,
161+ warm_offset: 0,
158162 flushed: false,
159163 initialised: false,
160164 }
@@ -561,28 +565,88 @@ impl AacEncoder {
561565 }
562566 (bits, coded)
563567 };
564- let (mut lo, mut hi) = (-240i32, 240i32);
565- let (probe_bits, mut coded) = cost(self, hi);
568+ let (probe_bits, mut coded) = cost(self, 240);
569+ let mut answer = 240i32;
566570 if probe_bits > budget {
567571 // Even the coarsest setting overflows: take it and let the
568572 // reservoir absorb the overshoot.
569- lo = hi;
570573 } else {
571- while lo < hi {
572- let mid = lo + (hi - lo) / 2;
573- let (b, c) = cost(self, mid);
574- if b <= budget {
575- hi = mid;
576- coded = c;
577- } else {
578- lo = mid + 1;
574+ // Bits fall monotonically as the offset rises, so "fits the
575+ // budget" is true on an upper interval and the finest fitting
576+ // setting is its left edge. Last frame's working point sits next
577+ // to that edge, so probe it first and gallop outward until one
578+ // side is known to fail, then bisect the short bracket -- the
579+ // same edge a full-range bisection would find, in a few probes.
580+ let start = self.warm_offset.clamp(-240, 240);
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+ }
579645 }
646+ answer = hi;
580647 }
581648 }
582- // When the loop ends, `coded` is the probe at `lo`: `hi` only ever
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;
649+ self.warm_offset = answer;
586650
587651 let mut w = BitWriter::new();
588652 let mut at = 0usize;
@@ -684,6 +748,9 @@ impl AacEncoder {
684748
685749 let mut quant = vec![0i32; FRAME_LEN];
686750 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];
687754 for _ in 0..4 {
688755 let mut settled = true;
689756 let mut prev: Option<i32> = None;
@@ -697,6 +764,7 @@ impl AacEncoder {
697764 };
698765 if fixed != sf[b] {
699766 sf[b] = fixed;
767+ dirty[b] = true;
700768 settled = false;
701769 }
702770 prev = Some(sf[b]);
@@ -709,6 +777,12 @@ impl AacEncoder {
709777 let base = if short { w * SHORT_LEN } else { 0 };
710778 quant[base + lo..base + hi].fill(0);
711779 }
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] {
712786 continue;
713787 }
714788 let gain = pow_quarter(SF_OFFSET - sf[b]);
@@ -722,6 +796,7 @@ impl AacEncoder {
722796 }
723797 }
724798 books[b] = codebook_for(peak);
799+ dirty[b] = false;
725800 if peak == 0 {
726801 // Nothing survived: the band leaves the delta chain, which
727802 // may free the bands after it to go coarser.