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 {
112 channels: usize,112 channels: usize,
113 /// Input not yet turned into frames, per channel.113 /// Input not yet turned into frames, per channel.
114 pcm: Vec<Vec<f32>>,114 pcm: Vec<Vec<f32>>,
115+ head: usize,
115 /// The previous frame's samples, the MDCT block's left half.116 /// The previous frame's samples, the MDCT block's left half.
116 prev: Vec<Vec<f32>>,117 prev: Vec<Vec<f32>>,
117 seq: WindowSequence,118 seq: WindowSequence,
@@ -149,6 +150,7 @@ impl AacEncoder {
149 sf_index: 3,150 sf_index: 3,
150 channels: 0,151 channels: 0,
151 pcm: Vec::new(),152 pcm: Vec::new(),
153+ head: 0,
152 prev: Vec::new(),154 prev: Vec::new(),
153 seq: WindowSequence::OnlyLong,155 seq: WindowSequence::OnlyLong,
154 energy: 0.0,156 energy: 0.0,
@@ -239,6 +241,7 @@ impl AacEncoder {
239 return;241 return;
240 }242 }
241 self.flushed = true;243 self.flushed = true;
244+ self.compact();
242 let left = self.pcm.iter().map(|c| c.len()).max().unwrap_or(0);245 let left = self.pcm.iter().map(|c| c.len()).max().unwrap_or(0);
243 if left > 0 {246 if left > 0 {
244 // One frame of lookahead is held back for the window decision, and247 // One frame of lookahead is held back for the window decision, and
@@ -257,9 +260,17 @@ impl AacEncoder {
257 }260 }
258 261
259 fn drain_frames(&mut self) {262 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) {
261 self.encode_frame();264 self.encode_frame();
262 }265 }
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+ }
263 }274 }
264 275
265 fn long_window(&self) -> &[f32] {276 fn long_window(&self) -> &[f32] {
@@ -302,7 +313,7 @@ impl AacEncoder {
302 313
303 let mut frames = Vec::with_capacity(self.channels);314 let mut frames = Vec::with_capacity(self.channels);
304 for ch in 0..self.channels {315 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]
306 .iter()317 .iter()
307 .map(|v| v * INPUT_SCALE)318 .map(|v| v * INPUT_SCALE)
308 .collect();319 .collect();
@@ -314,9 +325,7 @@ impl AacEncoder {
314 let threshold = self.psychoacoustics(&block, &coef, seq);325 let threshold = self.psychoacoustics(&block, &coef, seq);
315 frames.push(ChannelFrame { coef, threshold });326 frames.push(ChannelFrame { coef, threshold });
316 }327 }
317- for c in self.pcm.iter_mut() {328+ self.head += FRAME_LEN;
318- c.drain(..FRAME_LEN);
319- }
320 329
321 // The stream's bitrate counts every byte written, so with ADTS on the 56-bit header330 // The stream's bitrate counts every byte written, so with ADTS on the 56-bit header
322 // comes out of this frame's share before the raw block gets its bits.331 // comes out of this frame's share before the raw block gets its bits.
@@ -368,7 +377,7 @@ impl AacEncoder {
368 for i in 0..8 {377 for i in 0..8 {
369 let mut e = 0.0f32;378 let mut e = 0.0f32;
370 for ch in 0..self.channels {379 for ch in 0..self.channels {
371- let lo = FRAME_LEN + i * SHORT_LEN;380+ let lo = self.head + FRAME_LEN + i * SHORT_LEN;
372 for v in &self.pcm[ch][lo..lo + SHORT_LEN] {381 for v in &self.pcm[ch][lo..lo + SHORT_LEN] {
373 e += v * v;382 e += v * v;
374 }383 }
crates/ec-ac3/src/encode.rsmodified+4 -3
@@ -369,9 +369,10 @@ impl Ac3Encoder {
369 369
370 fn drain(&mut self) {370 fn drain(&mut self) {
371 let need = FRAME_SAMPLES * self.coded;371 let need = FRAME_SAMPLES * self.coded;
372- while self.pcm.len() >= need {372+ let rest = self.pcm.split_off(self.pcm.len() / need * need);
373- let block: Vec<f32> = self.pcm.drain(..need).collect();373+ let full = std::mem::replace(&mut self.pcm, rest);
374- let packet = self.encode_frame(&block);374+ for block in full.chunks_exact(need) {
375+ let packet = self.encode_frame(block);
375 self.packets.push_back(packet);376 self.packets.push_back(packet);
376 }377 }
377 }378 }
crates/ec-alac/src/encode.rsmodified+5 -3
@@ -610,9 +610,11 @@ impl Encoder for AlacEncoder {
610 }610 }
611 self.pending.extend(interleaved);611 self.pending.extend(interleaved);
612 let per_frame = self.cookie.frame_length as usize * channels;612 let per_frame = self.cookie.frame_length as usize * channels;
613- while self.pending.len() >= per_frame {613+ let rest = self.pending.split_off(self.pending.len() / per_frame * per_frame);
614- let frame: Vec<i32> = self.pending.drain(..per_frame).collect();614+ let full = std::mem::replace(&mut self.pending, rest);
615- self.packets.push_back(self.encode_frame(&frame));615+ for frame in full.chunks_exact(per_frame) {
616+ let packet = self.encode_frame(frame);
617+ self.packets.push_back(packet);
616 }618 }
617 Ok(())619 Ok(())
618 }620 }
crates/ec-truehd/src/encode.rsmodified+4 -3
@@ -656,9 +656,10 @@ impl TrueHdEncoder {
656 656
657 fn drain_full_units(&mut self) {657 fn drain_full_units(&mut self) {
658 let per_au = self.access_unit_size * 2;658 let per_au = self.access_unit_size * 2;
659- while self.pending.len() >= per_au {659+ let rest = self.pending.split_off(self.pending.len() / per_au * per_au);
660- let samples: Vec<i32> = self.pending.drain(..per_au).collect();660+ let full = std::mem::replace(&mut self.pending, rest);
661- let bytes = self.encode_access_unit(&samples);661+ for samples in full.chunks_exact(per_au) {
662+ let bytes = self.encode_access_unit(samples);
662 self.packets.push_back(bytes);663 self.packets.push_back(bytes);
663 }664 }
664 }665 }