forge
Explore
Log in

Don't you dare go Hollow

Keep the tonality analysis allocation-free in the steady-state encode loop

Browse files at this commit

8a2183bdd97291c25a1cfef2d4b65f39b77725a5Ahmet Kaan GümüşParent: 467beea
  • fix
    • keep the tonality analysis allocation-free in the steady-state encode loop
Show full message
fix(opus): keep the tonality analysis allocation-free in the steady-state encode loop
crates/ec-opus/src/analysis.rsmodified+44 -16
@@ -283,6 +283,13 @@ pub(crate) struct TonalityAnalysis {
283283 rnn_state: [f32; MAX_NEURONS],
284284 downmix_state: [f32; 3],
285285 info: Vec<AnalysisInfo>,
286+ /// Per-frame scratch, sized once so the steady-state encode loop stays
287+ /// allocation-free (`steady_state_encode_loop_zero_alloc`).
288+ dm_tmp: Vec<f32>,
289+ dm_tmp3x: Vec<f32>,
290+ chunk: Vec<f32>,
291+ fft_re: Vec<f32>,
292+ fft_im: Vec<f32>,
286293 }
287294
288295 impl TonalityAnalysis {
@@ -322,6 +329,11 @@ impl TonalityAnalysis {
322329 rnn_state: [0.0; MAX_NEURONS],
323330 downmix_state: [0.0; 3],
324331 info: vec![AnalysisInfo::default(); DETECT_SIZE],
332+ dm_tmp: Vec::with_capacity(2 * ANALYSIS_BUF_SIZE),
333+ dm_tmp3x: Vec::new(),
334+ chunk: Vec::with_capacity(ANALYSIS_BUF_SIZE),
335+ fft_re: vec![0.0; N],
336+ fft_im: vec![0.0; N],
325337 }
326338 }
327339
@@ -370,7 +382,9 @@ impl TonalityAnalysis {
370382 16000 => (subframe * 2 / 3, offset * 2 / 3),
371383 _ => (subframe, offset),
372384 };
373- let mut tmp = vec![0.0f32; subframe];
385+ let mut tmp = std::mem::take(&mut self.dm_tmp);
386+ tmp.clear();
387+ tmp.resize(subframe, 0.0);
374388 for (j, t) in tmp.iter_mut().enumerate() {
375389 let base = (j + offset) * channels;
376390 let mut acc = pcm[base];
@@ -379,30 +393,30 @@ impl TonalityAnalysis {
379393 }
380394 *t = if channels == 2 { 0.5 * acc } else { acc };
381395 }
382- match self.fs {
383- 48000 => {
384- let mut st = self.downmix_state;
385- let e = resampler_down2_hp(&mut st, y, &tmp);
386- self.downmix_state = st;
387- e
388- }
396+ let mut st = self.downmix_state;
397+ let ret = match self.fs {
398+ 48000 => resampler_down2_hp(&mut st, y, &tmp),
389399 16000 => {
390- let mut tmp3x = vec![0.0f32; 3 * subframe];
400+ let mut tmp3x = std::mem::take(&mut self.dm_tmp3x);
401+ tmp3x.clear();
402+ tmp3x.resize(3 * subframe, 0.0);
391403 for j in 0..subframe {
392404 tmp3x[3 * j] = tmp[j];
393405 tmp3x[3 * j + 1] = tmp[j];
394406 tmp3x[3 * j + 2] = tmp[j];
395407 }
396- let mut st = self.downmix_state;
397408 resampler_down2_hp(&mut st, y, &tmp3x);
398- self.downmix_state = st;
409+ self.dm_tmp3x = tmp3x;
399410 0.0
400411 }
401412 _ => {
402413 y[..subframe].copy_from_slice(&tmp);
403414 0.0
404415 }
405- }
416+ };
417+ self.downmix_state = st;
418+ self.dm_tmp = tmp;
419+ ret
406420 }
407421
408422 /// libopus `tonality_analysis()`: appends one 20 ms chunk of input and,
@@ -429,9 +443,12 @@ impl TonalityAnalysis {
429443 let (len, offset) = (len as usize, offset as usize);
430444
431445 let take = len.min(ANALYSIS_BUF_SIZE - self.mem_fill);
432- let mut chunk = vec![0.0f32; take];
446+ let mut chunk = std::mem::take(&mut self.chunk);
447+ chunk.clear();
448+ chunk.resize(take, 0.0);
433449 let ener = self.downmix_and_resample(pcm, &mut chunk, take, offset, channels);
434450 self.inmem[self.mem_fill..self.mem_fill + take].copy_from_slice(&chunk);
451+ self.chunk = chunk;
435452 self.hp_ener_accum += ener;
436453 if self.mem_fill + len < ANALYSIS_BUF_SIZE {
437454 self.mem_fill += len;
@@ -446,8 +463,8 @@ impl TonalityAnalysis {
446463
447464 let is_silence = self.inmem.iter().all(|&v| v == 0.0);
448465
449- let mut re = vec![0.0f32; N];
450- let mut im = vec![0.0f32; N];
466+ let mut re = std::mem::take(&mut self.fft_re);
467+ let mut im = std::mem::take(&mut self.fft_im);
451468 for i in 0..N2 {
452469 let w = ANALYSIS_WINDOW[i];
453470 re[i] = w * self.inmem[i];
@@ -457,15 +474,20 @@ impl TonalityAnalysis {
457474 }
458475 self.inmem.copy_within(ANALYSIS_BUF_SIZE - 240.., 0);
459476 let remaining = len - (ANALYSIS_BUF_SIZE - self.mem_fill);
460- let mut chunk = vec![0.0f32; remaining];
477+ let mut chunk = std::mem::take(&mut self.chunk);
478+ chunk.clear();
479+ chunk.resize(remaining, 0.0);
461480 let ener = self.downmix_and_resample(pcm, &mut chunk, remaining, offset + ANALYSIS_BUF_SIZE - self.mem_fill, channels);
462481 self.inmem[240..240 + remaining].copy_from_slice(&chunk);
482+ self.chunk = chunk;
463483 self.hp_ener_accum = ener;
464484 self.mem_fill = 240 + remaining;
465485 if is_silence {
466486 // On silence, copy the previous analysis.
467487 let prev = (write + DETECT_SIZE - 1) % DETECT_SIZE;
468488 self.info[write] = self.info[prev];
489+ self.fft_re = re;
490+ self.fft_im = im;
469491 return;
470492 }
471493 // libopus takes `opus_fft` (forward, scaled by 1/N); ours is the
@@ -481,6 +503,8 @@ impl TonalityAnalysis {
481503 }
482504 if re[0].is_nan() {
483505 self.info[write].valid = false;
506+ self.fft_re = re;
507+ self.fft_im = im;
484508 return;
485509 }
486510
@@ -567,6 +591,8 @@ impl TonalityAnalysis {
567591 // Check for extreme band energies that could cause NaNs later.
568592 if !(e < 1e9) || e.is_nan() {
569593 self.info[write].valid = false;
594+ self.fft_re = re;
595+ self.fft_im = im;
570596 return;
571597 }
572598 self.e[self.e_count][b] = e;
@@ -816,6 +842,8 @@ impl TonalityAnalysis {
816842 bandwidth,
817843 leak_boost,
818844 };
845+ self.fft_re = re;
846+ self.fft_im = im;
819847 }
820848
821849 /// libopus `tonality_get_info()`: picks the window that covers the frame