The encoder stops shuffling its own buffers
Browse files at this commitdcaf6bb94c99f7891dc187ec0eb1986edc94331dParent: 59a18cf- perf
- the encoder stops shuffling its own buffers
1 changed file +41 -25
- crates/ec-mp3/src/encode.rs +41 -25
crates/ec-mp3/src/encode.rsmodified+41 -25
| @@ -137,6 +137,8 @@ pub struct Mp3Encode { | |||
| 137 | fft: RealFft<f32>, | 137 | fft: RealFft<f32>, |
| 138 | fft_window: Window<f32>, | 138 | fft_window: Window<f32>, |
| 139 | psy_history: Vec<Vec<f32>>, | 139 | psy_history: Vec<Vec<f32>>, |
| 140 | + /// Deinterleave scratch for `encode_block`, grown once and reused. | ||
| 141 | + pcm_scratch: Vec<f32>, | ||
| 140 | /// The main-data byte stream and how much of it frames have carried. | 142 | /// The main-data byte stream and how much of it frames have carried. |
| 141 | stream: Vec<u8>, | 143 | stream: Vec<u8>, |
| 142 | used: usize, | 144 | used: usize, |
| @@ -219,6 +221,7 @@ impl Mp3Encode { | |||
| 219 | assigned: 0, | 221 | assigned: 0, |
| 220 | written: 0, | 222 | written: 0, |
| 221 | queued: std::collections::VecDeque::new(), | 223 | queued: std::collections::VecDeque::new(), |
| 224 | + pcm_scratch: Vec::new(), | ||
| 222 | ready: std::collections::VecDeque::new(), | 225 | ready: std::collections::VecDeque::new(), |
| 223 | frames: 0, | 226 | frames: 0, |
| 224 | frame_bytes: 0, | 227 | frame_bytes: 0, |
| @@ -350,26 +353,34 @@ impl Mp3Encoder { | |||
| 350 | } | 353 | } |
| 351 | 354 | ||
| 352 | fn drain(&mut self, flush: bool) { | 355 | fn drain(&mut self, flush: bool) { |
| 353 | - let Some(core) = self.core.as_mut() else { | 356 | + let Self { |
| 357 | + core, pcm, frames, .. | ||
| 358 | + } = self; | ||
| 359 | + let Some(core) = core.as_mut() else { | ||
| 354 | return; | 360 | return; |
| 355 | }; | 361 | }; |
| 356 | let channels = core.channels; | 362 | let channels = core.channels; |
| 357 | let need = core.frame_samples() * channels; | 363 | let need = core.frame_samples() * channels; |
| 358 | - while self.pcm.len() >= need { | 364 | + // Encode behind a read cursor and shift the leftovers once: draining |
| 359 | - let block: Vec<f32> = self.pcm.drain(..need).collect(); | 365 | + // per frame memmoved the whole remaining push for every frame, which |
| 360 | - core.encode_block(&block); | 366 | + // on a single large push is quadratic in the file length. |
| 361 | - } | 367 | + let mut pos = 0usize; |
| 368 | + while pcm.len() - pos >= need { | ||
| 369 | + core.encode_block(&pcm[pos..pos + need]); | ||
| 370 | + pos += need; | ||
| 371 | + } | ||
| 372 | + pcm.drain(..pos); | ||
| 362 | if flush { | 373 | if flush { |
| 363 | - if !self.pcm.is_empty() { | 374 | + if !pcm.is_empty() { |
| 364 | - let mut block = std::mem::take(&mut self.pcm); | 375 | + pcm.resize(need, 0.0); |
| 365 | - block.resize(need, 0.0); | 376 | + core.encode_block(pcm); |
| 366 | - core.encode_block(&block); | 377 | + pcm.clear(); |
| 367 | } | 378 | } |
| 368 | core.flush_pending(); | 379 | core.flush_pending(); |
| 369 | core.flush_frames(); | 380 | core.flush_frames(); |
| 370 | } | 381 | } |
| 371 | while let Some(frame) = core.ready.pop_front() { | 382 | while let Some(frame) = core.ready.pop_front() { |
| 372 | - self.frames.push_back(frame); | 383 | + frames.push_back(frame); |
| 373 | } | 384 | } |
| 374 | } | 385 | } |
| 375 | 386 | ||
| @@ -514,10 +525,15 @@ impl Mp3Encode { | |||
| 514 | let granules = self.granules(); | 525 | let granules = self.granules(); |
| 515 | // Polyphase analysis, one channel at a time. | 526 | // Polyphase analysis, one channel at a time. |
| 516 | for ch in 0..channels { | 527 | for ch in 0..channels { |
| 517 | - let mut mono: Vec<f32> = Vec::with_capacity(pcm.len() / channels); | 528 | + // One deinterleave buffer per encoder, grown once: this frame's |
| 529 | + // mono channel used to be a fresh allocation per channel per | ||
| 530 | + // frame. | ||
| 531 | + self.pcm_scratch.clear(); | ||
| 532 | + self.pcm_scratch.reserve(pcm.len() / channels); | ||
| 518 | for frame in pcm.chunks(channels) { | 533 | for frame in pcm.chunks(channels) { |
| 519 | - mono.push(frame[ch]); | 534 | + self.pcm_scratch.push(frame[ch]); |
| 520 | } | 535 | } |
| 536 | + let mono = &self.pcm_scratch; | ||
| 521 | for chunk in mono.chunks(32) { | 537 | for chunk in mono.chunks(32) { |
| 522 | if chunk.len() < 32 { | 538 | if chunk.len() < 32 { |
| 523 | break; | 539 | break; |
| @@ -526,7 +542,13 @@ impl Mp3Encode { | |||
| 526 | self.slots[ch].push(slot); | 542 | self.slots[ch].push(slot); |
| 527 | } | 543 | } |
| 528 | } | 544 | } |
| 529 | - self.psy_push(ch, &mono); | 545 | + // The masking model's history update, inlined from `psy_push` so |
| 546 | + // it can touch `psy_history` while `mono` borrows `pcm_scratch`. | ||
| 547 | + let n = mono.len().min(1024); | ||
| 548 | + let history = &mut self.psy_history[ch]; | ||
| 549 | + history.copy_within(n.., 0); | ||
| 550 | + let keep = 1024 - n; | ||
| 551 | + history[keep..].copy_from_slice(&mono[mono.len() - n..]); | ||
| 530 | } | 552 | } |
| 531 | // A granule is 18 slots; hold one back so block switching can look | 553 | // A granule is 18 slots; hold one back so block switching can look |
| 532 | // ahead at the next granule's energy. | 554 | // ahead at the next granule's energy. |
| @@ -557,15 +579,6 @@ impl Mp3Encode { | |||
| 557 | } | 579 | } |
| 558 | } | 580 | } |
| 559 | 581 | ||
| 560 | - /// One FFT per granule of input, kept for the masking model. | ||
| 561 | - fn psy_push(&mut self, ch: usize, mono: &[f32]) { | ||
| 562 | - let n = 1024.min(mono.len()); | ||
| 563 | - let history = &mut self.psy_history[ch]; | ||
| 564 | - history.copy_within(n.., 0); | ||
| 565 | - let keep = 1024 - n; | ||
| 566 | - history[keep..].copy_from_slice(&mono[mono.len() - n..]); | ||
| 567 | - } | ||
| 568 | - | ||
| 569 | /// Allowed noise per long scalefactor band, from the masking model, in | 582 | /// Allowed noise per long scalefactor band, from the masking model, in |
| 570 | /// the units `code_with` measures quantisation noise in (summed squared | 583 | /// the units `code_with` measures quantisation noise in (summed squared |
| 571 | /// MDCT lines), so noise / threshold is a noise-to-mask ratio. | 584 | /// MDCT lines), so noise / threshold is a noise-to-mask ratio. |
| @@ -702,18 +715,21 @@ impl Mp3Encode { | |||
| 702 | // every bitrate candidate VBR tries below; compute them once. | 715 | // every bitrate candidate VBR tries below; compute them once. |
| 703 | let mut xrs: Vec<Vec<f32>> = Vec::with_capacity(granules * channels); | 716 | let mut xrs: Vec<Vec<f32>> = Vec::with_capacity(granules * channels); |
| 704 | let mut thresholds: Vec<Vec<f32>> = Vec::with_capacity(granules * channels); | 717 | let mut thresholds: Vec<Vec<f32>> = Vec::with_capacity(granules * channels); |
| 718 | + // Take the held-back granules once: the per-granule clone that used | ||
| 719 | + // to feed `mdct` copied every subband and threshold just to keep the | ||
| 720 | + // borrow checker happy about `history`. | ||
| 721 | + let held: Vec<PendingGranule> = self.pending.drain(..granules * channels).collect(); | ||
| 705 | for gr in 0..granules { | 722 | for gr in 0..granules { |
| 706 | for ch in 0..channels { | 723 | for ch in 0..channels { |
| 707 | let index = gr * channels + ch; | 724 | let index = gr * channels + ch; |
| 708 | - let granule = self.pending[index].clone(); | 725 | + let granule = &held[index]; |
| 709 | xrs.push(self.mdct(ch, &granule.subband, types[index])); | 726 | xrs.push(self.mdct(ch, &granule.subband, types[index])); |
| 710 | thresholds.push(match vbr { | 727 | thresholds.push(match vbr { |
| 711 | Some(quality) => quality_threshold(&granule.threshold, quality), | 728 | Some(quality) => quality_threshold(&granule.threshold, quality), |
| 712 | - None => granule.threshold, | 729 | + None => granule.threshold.clone(), |
| 713 | }); | 730 | }); |
| 714 | } | 731 | } |
| 715 | } | 732 | } |
| 716 | - self.pending.drain(..granules * channels); | ||
| 717 | 733 | ||
| 718 | // Mid/side stereo. The two channels of real music are mostly the same | 734 | // Mid/side stereo. The two channels of real music are mostly the same |
| 719 | // signal, and coding their sum and difference puts nearly all of the | 735 | // signal, and coding their sum and difference puts nearly all of the |