xref: /aosp_15_r20/external/webrtc/modules/audio_coding/neteq/accelerate.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "modules/audio_coding/neteq/accelerate.h"
12 
13 
14 #include "api/array_view.h"
15 #include "modules/audio_coding/neteq/audio_multi_vector.h"
16 
17 namespace webrtc {
18 
Process(const int16_t * input,size_t input_length,bool fast_accelerate,AudioMultiVector * output,size_t * length_change_samples)19 Accelerate::ReturnCodes Accelerate::Process(const int16_t* input,
20                                             size_t input_length,
21                                             bool fast_accelerate,
22                                             AudioMultiVector* output,
23                                             size_t* length_change_samples) {
24   // Input length must be (almost) 30 ms.
25   static const size_t k15ms = 120;  // 15 ms = 120 samples at 8 kHz sample rate.
26   if (num_channels_ == 0 ||
27       input_length / num_channels_ < (2 * k15ms - 1) * fs_mult_) {
28     // Length of input data too short to do accelerate. Simply move all data
29     // from input to output.
30     output->PushBackInterleaved(
31         rtc::ArrayView<const int16_t>(input, input_length));
32     return kError;
33   }
34   return TimeStretch::Process(input, input_length, fast_accelerate, output,
35                               length_change_samples);
36 }
37 
SetParametersForPassiveSpeech(size_t,int16_t * best_correlation,size_t *) const38 void Accelerate::SetParametersForPassiveSpeech(size_t /*len*/,
39                                                int16_t* best_correlation,
40                                                size_t* /*peak_index*/) const {
41   // When the signal does not contain any active speech, the correlation does
42   // not matter. Simply set it to zero.
43   *best_correlation = 0;
44 }
45 
CheckCriteriaAndStretch(const int16_t * input,size_t input_length,size_t peak_index,int16_t best_correlation,bool active_speech,bool fast_mode,AudioMultiVector * output) const46 Accelerate::ReturnCodes Accelerate::CheckCriteriaAndStretch(
47     const int16_t* input,
48     size_t input_length,
49     size_t peak_index,
50     int16_t best_correlation,
51     bool active_speech,
52     bool fast_mode,
53     AudioMultiVector* output) const {
54   // Check for strong correlation or passive speech.
55   // Use 8192 (0.5 in Q14) in fast mode.
56   const int correlation_threshold = fast_mode ? 8192 : kCorrelationThreshold;
57   if ((best_correlation > correlation_threshold) || !active_speech) {
58     // Do accelerate operation by overlap add.
59 
60     // Pre-calculate common multiplication with `fs_mult_`.
61     // 120 corresponds to 15 ms.
62     size_t fs_mult_120 = fs_mult_ * 120;
63 
64     if (fast_mode) {
65       // Fit as many multiples of `peak_index` as possible in fs_mult_120.
66       // TODO(henrik.lundin) Consider finding multiple correlation peaks and
67       // pick the one with the longest correlation lag in this case.
68       peak_index = (fs_mult_120 / peak_index) * peak_index;
69     }
70 
71     RTC_DCHECK_GE(fs_mult_120, peak_index);  // Should be handled in Process().
72     // Copy first part; 0 to 15 ms.
73     output->PushBackInterleaved(
74         rtc::ArrayView<const int16_t>(input, fs_mult_120 * num_channels_));
75     // Copy the `peak_index` starting at 15 ms to `temp_vector`.
76     AudioMultiVector temp_vector(num_channels_);
77     temp_vector.PushBackInterleaved(rtc::ArrayView<const int16_t>(
78         &input[fs_mult_120 * num_channels_], peak_index * num_channels_));
79     // Cross-fade `temp_vector` onto the end of `output`.
80     output->CrossFade(temp_vector, peak_index);
81     // Copy the last unmodified part, 15 ms + pitch period until the end.
82     output->PushBackInterleaved(rtc::ArrayView<const int16_t>(
83         &input[(fs_mult_120 + peak_index) * num_channels_],
84         input_length - (fs_mult_120 + peak_index) * num_channels_));
85 
86     if (active_speech) {
87       return kSuccess;
88     } else {
89       return kSuccessLowEnergy;
90     }
91   } else {
92     // Accelerate not allowed. Simply move all data from decoded to outData.
93     output->PushBackInterleaved(
94         rtc::ArrayView<const int16_t>(input, input_length));
95     return kNoStretch;
96   }
97 }
98 
Create(int sample_rate_hz,size_t num_channels,const BackgroundNoise & background_noise) const99 Accelerate* AccelerateFactory::Create(
100     int sample_rate_hz,
101     size_t num_channels,
102     const BackgroundNoise& background_noise) const {
103   return new Accelerate(sample_rate_hz, num_channels, background_noise);
104 }
105 
106 }  // namespace webrtc
107