xref: /aosp_15_r20/external/webrtc/modules/audio_coding/neteq/normal.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/normal.h"
12 
13 #include <string.h>  // memset, memcpy
14 
15 #include <algorithm>  // min
16 
17 #include "common_audio/signal_processing/include/signal_processing_library.h"
18 #include "modules/audio_coding/neteq/audio_multi_vector.h"
19 #include "modules/audio_coding/neteq/background_noise.h"
20 #include "modules/audio_coding/neteq/decoder_database.h"
21 #include "modules/audio_coding/neteq/expand.h"
22 #include "rtc_base/checks.h"
23 
24 namespace webrtc {
25 
Process(const int16_t * input,size_t length,NetEq::Mode last_mode,AudioMultiVector * output)26 int Normal::Process(const int16_t* input,
27                     size_t length,
28                     NetEq::Mode last_mode,
29                     AudioMultiVector* output) {
30   if (length == 0) {
31     // Nothing to process.
32     output->Clear();
33     return static_cast<int>(length);
34   }
35 
36   RTC_DCHECK(output->Empty());
37   // Output should be empty at this point.
38   if (length % output->Channels() != 0) {
39     // The length does not match the number of channels.
40     output->Clear();
41     return 0;
42   }
43   output->PushBackInterleaved(rtc::ArrayView<const int16_t>(input, length));
44 
45   const int fs_mult = fs_hz_ / 8000;
46   RTC_DCHECK_GT(fs_mult, 0);
47   // fs_shift = log2(fs_mult), rounded down.
48   // Note that `fs_shift` is not "exact" for 48 kHz.
49   // TODO(hlundin): Investigate this further.
50   const int fs_shift = 30 - WebRtcSpl_NormW32(fs_mult);
51 
52   // If last call resulted in a CodedPlc we don't need to do cross-fading but we
53   // need to report the end of the interruption once we are back to normal
54   // operation.
55   if (last_mode == NetEq::Mode::kCodecPlc) {
56     statistics_->EndExpandEvent(fs_hz_);
57   }
58 
59   // Check if last RecOut call resulted in an Expand. If so, we have to take
60   // care of some cross-fading and unmuting.
61   if (last_mode == NetEq::Mode::kExpand) {
62     // Generate interpolation data using Expand.
63     // First, set Expand parameters to appropriate values.
64     expand_->SetParametersForNormalAfterExpand();
65 
66     // Call Expand.
67     AudioMultiVector expanded(output->Channels());
68     expand_->Process(&expanded);
69     expand_->Reset();
70 
71     size_t length_per_channel = length / output->Channels();
72     std::unique_ptr<int16_t[]> signal(new int16_t[length_per_channel]);
73     for (size_t channel_ix = 0; channel_ix < output->Channels(); ++channel_ix) {
74       // Set muting factor to the same as expand muting factor.
75       int16_t mute_factor = expand_->MuteFactor(channel_ix);
76 
77       (*output)[channel_ix].CopyTo(length_per_channel, 0, signal.get());
78 
79       // Find largest absolute value in new data.
80       int16_t decoded_max =
81           WebRtcSpl_MaxAbsValueW16(signal.get(), length_per_channel);
82       // Adjust muting factor if needed (to BGN level).
83       size_t energy_length =
84           std::min(static_cast<size_t>(fs_mult * 64), length_per_channel);
85       int scaling = 6 + fs_shift - WebRtcSpl_NormW32(decoded_max * decoded_max);
86       scaling = std::max(scaling, 0);  // `scaling` should always be >= 0.
87       int32_t energy = WebRtcSpl_DotProductWithScale(signal.get(), signal.get(),
88                                                      energy_length, scaling);
89       int32_t scaled_energy_length =
90           static_cast<int32_t>(energy_length >> scaling);
91       if (scaled_energy_length > 0) {
92         energy = energy / scaled_energy_length;
93       } else {
94         energy = 0;
95       }
96 
97       int local_mute_factor = 16384;  // 1.0 in Q14.
98       if ((energy != 0) && (energy > background_noise_.Energy(channel_ix))) {
99         // Normalize new frame energy to 15 bits.
100         scaling = WebRtcSpl_NormW32(energy) - 16;
101         // We want background_noise_.energy() / energy in Q14.
102         int32_t bgn_energy = WEBRTC_SPL_SHIFT_W32(
103             background_noise_.Energy(channel_ix), scaling + 14);
104         int16_t energy_scaled =
105             static_cast<int16_t>(WEBRTC_SPL_SHIFT_W32(energy, scaling));
106         int32_t ratio = WebRtcSpl_DivW32W16(bgn_energy, energy_scaled);
107         local_mute_factor =
108             std::min(local_mute_factor, WebRtcSpl_SqrtFloor(ratio << 14));
109       }
110       mute_factor = std::max<int16_t>(mute_factor, local_mute_factor);
111       RTC_DCHECK_LE(mute_factor, 16384);
112       RTC_DCHECK_GE(mute_factor, 0);
113 
114       // If muted increase by 0.64 for every 20 ms (NB/WB 0.0040/0.0020 in Q14),
115       // or as fast as it takes to come back to full gain within the frame
116       // length.
117       const int back_to_fullscale_inc =
118           static_cast<int>((16384 - mute_factor) / length_per_channel);
119       const int increment = std::max(64 / fs_mult, back_to_fullscale_inc);
120       for (size_t i = 0; i < length_per_channel; i++) {
121         // Scale with mute factor.
122         RTC_DCHECK_LT(channel_ix, output->Channels());
123         RTC_DCHECK_LT(i, output->Size());
124         int32_t scaled_signal = (*output)[channel_ix][i] * mute_factor;
125         // Shift 14 with proper rounding.
126         (*output)[channel_ix][i] =
127             static_cast<int16_t>((scaled_signal + 8192) >> 14);
128         // Increase mute_factor towards 16384.
129         mute_factor =
130             static_cast<int16_t>(std::min(mute_factor + increment, 16384));
131       }
132 
133       // Interpolate the expanded data into the new vector.
134       // (NB/WB/SWB32/SWB48 8/16/32/48 samples.)
135       size_t win_length = samples_per_ms_;
136       int16_t win_slope_Q14 = default_win_slope_Q14_;
137       RTC_DCHECK_LT(channel_ix, output->Channels());
138       if (win_length > output->Size()) {
139         win_length = output->Size();
140         win_slope_Q14 = (1 << 14) / static_cast<int16_t>(win_length);
141       }
142       int16_t win_up_Q14 = 0;
143       for (size_t i = 0; i < win_length; i++) {
144         win_up_Q14 += win_slope_Q14;
145         (*output)[channel_ix][i] =
146             (win_up_Q14 * (*output)[channel_ix][i] +
147              ((1 << 14) - win_up_Q14) * expanded[channel_ix][i] + (1 << 13)) >>
148             14;
149       }
150       RTC_DCHECK_GT(win_up_Q14,
151                     (1 << 14) - 32);  // Worst case rouding is a length of 34
152     }
153   } else if (last_mode == NetEq::Mode::kRfc3389Cng) {
154     RTC_DCHECK_EQ(output->Channels(), 1);  // Not adapted for multi-channel yet.
155     static const size_t kCngLength = 48;
156     RTC_DCHECK_LE(8 * fs_mult, kCngLength);
157     int16_t cng_output[kCngLength];
158     ComfortNoiseDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
159 
160     if (cng_decoder) {
161       // Generate long enough for 48kHz.
162       if (!cng_decoder->Generate(cng_output, false)) {
163         // Error returned; set return vector to all zeros.
164         memset(cng_output, 0, sizeof(cng_output));
165       }
166     } else {
167       // If no CNG instance is defined, just copy from the decoded data.
168       // (This will result in interpolating the decoded with itself.)
169       (*output)[0].CopyTo(fs_mult * 8, 0, cng_output);
170     }
171     // Interpolate the CNG into the new vector.
172     // (NB/WB/SWB32/SWB48 8/16/32/48 samples.)
173     size_t win_length = samples_per_ms_;
174     int16_t win_slope_Q14 = default_win_slope_Q14_;
175     if (win_length > kCngLength) {
176       win_length = kCngLength;
177       win_slope_Q14 = (1 << 14) / static_cast<int16_t>(win_length);
178     }
179     int16_t win_up_Q14 = 0;
180     for (size_t i = 0; i < win_length; i++) {
181       win_up_Q14 += win_slope_Q14;
182       (*output)[0][i] =
183           (win_up_Q14 * (*output)[0][i] +
184            ((1 << 14) - win_up_Q14) * cng_output[i] + (1 << 13)) >>
185           14;
186     }
187     RTC_DCHECK_GT(win_up_Q14,
188                   (1 << 14) - 32);  // Worst case rouding is a length of 34
189   }
190 
191   return static_cast<int>(length);
192 }
193 
194 }  // namespace webrtc
195