forge
Explore
Log in

Don't you dare go Hollow

ec-aac, ec-alac, ec-ac3, ec-truehd: encoders no longer shift the whole input buffer per frame (quadratic); AAC 120 s encode 15.1 s -> 1.3 s, output identical

Browse files at this commit

4da3bdfec2552e044a9b78bd43a426fa8b096143Ahmet Kaan GümüşParent: ccbbd43
crates/ec-aac/src/encode.rsmodified+15 -6
@@ -112,6 +112,7 @@ pub struct AacEncoder {
112112 channels: usize,
113113 /// Input not yet turned into frames, per channel.
114114 pcm: Vec<Vec<f32>>,
115+ head: usize,
115116 /// The previous frame's samples, the MDCT block's left half.
116117 prev: Vec<Vec<f32>>,
117118 seq: WindowSequence,
@@ -149,6 +150,7 @@ impl AacEncoder {
149150 sf_index: 3,
150151 channels: 0,
151152 pcm: Vec::new(),
153+ head: 0,
152154 prev: Vec::new(),
153155 seq: WindowSequence::OnlyLong,
154156 energy: 0.0,
@@ -239,6 +241,7 @@ impl AacEncoder {
239241 return;
240242 }
241243 self.flushed = true;
244+ self.compact();
242245 let left = self.pcm.iter().map(|c| c.len()).max().unwrap_or(0);
243246 if left > 0 {
244247 // One frame of lookahead is held back for the window decision, and
@@ -257,9 +260,17 @@ impl AacEncoder {
257260 }
258261
259262 fn drain_frames(&mut self) {
260- while self.pcm.iter().all(|c| c.len() >= 2 * FRAME_LEN) {
263+ while self.pcm.iter().all(|c| c.len() >= self.head + 2 * FRAME_LEN) {
261264 self.encode_frame();
262265 }
266+ self.compact();
267+ }
268+
269+ fn compact(&mut self) {
270+ let head = std::mem::take(&mut self.head);
271+ for c in self.pcm.iter_mut() {
272+ c.drain(..head);
273+ }
263274 }
264275
265276 fn long_window(&self) -> &[f32] {
@@ -302,7 +313,7 @@ impl AacEncoder {
302313
303314 let mut frames = Vec::with_capacity(self.channels);
304315 for ch in 0..self.channels {
305- let cur: Vec<f32> = self.pcm[ch][..FRAME_LEN]
316+ let cur: Vec<f32> = self.pcm[ch][self.head..self.head + FRAME_LEN]
306317 .iter()
307318 .map(|v| v * INPUT_SCALE)
308319 .collect();
@@ -314,9 +325,7 @@ impl AacEncoder {
314325 let threshold = self.psychoacoustics(&block, &coef, seq);
315326 frames.push(ChannelFrame { coef, threshold });
316327 }
317- for c in self.pcm.iter_mut() {
318- c.drain(..FRAME_LEN);
319- }
328+ self.head += FRAME_LEN;
320329
321330 // The stream's bitrate counts every byte written, so with ADTS on the 56-bit header
322331 // comes out of this frame's share before the raw block gets its bits.
@@ -368,7 +377,7 @@ impl AacEncoder {
368377 for i in 0..8 {
369378 let mut e = 0.0f32;
370379 for ch in 0..self.channels {
371- let lo = FRAME_LEN + i * SHORT_LEN;
380+ let lo = self.head + FRAME_LEN + i * SHORT_LEN;
372381 for v in &self.pcm[ch][lo..lo + SHORT_LEN] {
373382 e += v * v;
374383 }
crates/ec-ac3/src/encode.rsmodified+4 -3
@@ -369,9 +369,10 @@ impl Ac3Encoder {
369369
370370 fn drain(&mut self) {
371371 let need = FRAME_SAMPLES * self.coded;
372- while self.pcm.len() >= need {
373- let block: Vec<f32> = self.pcm.drain(..need).collect();
374- let packet = self.encode_frame(&block);
372+ let rest = self.pcm.split_off(self.pcm.len() / need * need);
373+ let full = std::mem::replace(&mut self.pcm, rest);
374+ for block in full.chunks_exact(need) {
375+ let packet = self.encode_frame(block);
375376 self.packets.push_back(packet);
376377 }
377378 }
crates/ec-alac/src/encode.rsmodified+5 -3
@@ -610,9 +610,11 @@ impl Encoder for AlacEncoder {
610610 }
611611 self.pending.extend(interleaved);
612612 let per_frame = self.cookie.frame_length as usize * channels;
613- while self.pending.len() >= per_frame {
614- let frame: Vec<i32> = self.pending.drain(..per_frame).collect();
615- self.packets.push_back(self.encode_frame(&frame));
613+ let rest = self.pending.split_off(self.pending.len() / per_frame * per_frame);
614+ let full = std::mem::replace(&mut self.pending, rest);
615+ for frame in full.chunks_exact(per_frame) {
616+ let packet = self.encode_frame(frame);
617+ self.packets.push_back(packet);
616618 }
617619 Ok(())
618620 }
crates/ec-truehd/src/encode.rsmodified+4 -3
@@ -656,9 +656,10 @@ impl TrueHdEncoder {
656656
657657 fn drain_full_units(&mut self) {
658658 let per_au = self.access_unit_size * 2;
659- while self.pending.len() >= per_au {
660- let samples: Vec<i32> = self.pending.drain(..per_au).collect();
661- let bytes = self.encode_access_unit(&samples);
659+ let rest = self.pending.split_off(self.pending.len() / per_au * per_au);
660+ let full = std::mem::replace(&mut self.pending, rest);
661+ for samples in full.chunks_exact(per_au) {
662+ let bytes = self.encode_access_unit(samples);
662663 self.packets.push_back(bytes);
663664 }
664665 }