blob: 48d723a81b09df9ae5d4bca1cdc2f7bfe95f00c6 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/neteq/normal.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <string.h> // memset, memcpy
14
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000015#include <algorithm> // min
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/audio_codecs/audio_decoder.h"
18#include "common_audio/signal_processing/include/signal_processing_library.h"
19#include "modules/audio_coding/neteq/audio_multi_vector.h"
20#include "modules/audio_coding/neteq/background_noise.h"
21#include "modules/audio_coding/neteq/decoder_database.h"
22#include "modules/audio_coding/neteq/expand.h"
23#include "rtc_base/checks.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000024
25namespace webrtc {
26
27int Normal::Process(const int16_t* input,
28 size_t length,
29 Modes last_mode,
30 int16_t* external_mute_factor_array,
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000031 AudioMultiVector* output) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000032 if (length == 0) {
33 // Nothing to process.
34 output->Clear();
turaj@webrtc.org362a55e2013-09-20 16:25:28 +000035 return static_cast<int>(length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000036 }
37
henrik.lundin80c06fa2016-11-14 08:18:52 -080038 RTC_DCHECK(output->Empty());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000039 // Output should be empty at this point.
henrik.lundin@webrtc.orgee0fb182014-09-02 13:22:11 +000040 if (length % output->Channels() != 0) {
41 // The length does not match the number of channels.
42 output->Clear();
43 return 0;
44 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000045 output->PushBackInterleaved(input, length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000046
Peter Kastingdce40cf2015-08-24 14:52:23 -070047 const int fs_mult = fs_hz_ / 8000;
henrik.lundin80c06fa2016-11-14 08:18:52 -080048 RTC_DCHECK_GT(fs_mult, 0);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000049 // fs_shift = log2(fs_mult), rounded down.
50 // Note that |fs_shift| is not "exact" for 48 kHz.
51 // TODO(hlundin): Investigate this further.
Peter Kastingdce40cf2015-08-24 14:52:23 -070052 const int fs_shift = 30 - WebRtcSpl_NormW32(fs_mult);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000053
54 // Check if last RecOut call resulted in an Expand. If so, we have to take
55 // care of some cross-fading and unmuting.
56 if (last_mode == kModeExpand) {
57 // Generate interpolation data using Expand.
58 // First, set Expand parameters to appropriate values.
59 expand_->SetParametersForNormalAfterExpand();
60
61 // Call Expand.
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000062 AudioMultiVector expanded(output->Channels());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000063 expand_->Process(&expanded);
64 expand_->Reset();
65
minyue-webrtc79553cb2016-05-10 19:55:56 +020066 size_t length_per_channel = length / output->Channels();
67 std::unique_ptr<int16_t[]> signal(new int16_t[length_per_channel]);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000068 for (size_t channel_ix = 0; channel_ix < output->Channels(); ++channel_ix) {
69 // Adjust muting factor (main muting factor times expand muting factor).
70 external_mute_factor_array[channel_ix] = static_cast<int16_t>(
bjornv@webrtc.org600587d2015-03-09 13:30:28 +000071 (external_mute_factor_array[channel_ix] *
72 expand_->MuteFactor(channel_ix)) >> 14);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000073
minyue-webrtc79553cb2016-05-10 19:55:56 +020074 (*output)[channel_ix].CopyTo(length_per_channel, 0, signal.get());
75
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000076 // Find largest absolute value in new data.
Peter Kastingdce40cf2015-08-24 14:52:23 -070077 int16_t decoded_max =
minyue-webrtc79553cb2016-05-10 19:55:56 +020078 WebRtcSpl_MaxAbsValueW16(signal.get(), length_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000079 // Adjust muting factor if needed (to BGN level).
Peter Kastingdce40cf2015-08-24 14:52:23 -070080 size_t energy_length =
81 std::min(static_cast<size_t>(fs_mult * 64), length_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000082 int scaling = 6 + fs_shift
83 - WebRtcSpl_NormW32(decoded_max * decoded_max);
84 scaling = std::max(scaling, 0); // |scaling| should always be >= 0.
minyue-webrtc79553cb2016-05-10 19:55:56 +020085 int32_t energy = WebRtcSpl_DotProductWithScale(signal.get(), signal.get(),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000086 energy_length, scaling);
Peter Kastingf045e4d2015-06-10 21:15:38 -070087 int32_t scaled_energy_length =
88 static_cast<int32_t>(energy_length >> scaling);
89 if (scaled_energy_length > 0) {
90 energy = energy / scaled_energy_length;
henrik.lundin@webrtc.orgee0fb182014-09-02 13:22:11 +000091 } else {
92 energy = 0;
93 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000094
95 int mute_factor;
96 if ((energy != 0) &&
97 (energy > background_noise_.Energy(channel_ix))) {
98 // Normalize new frame energy to 15 bits.
99 scaling = WebRtcSpl_NormW32(energy) - 16;
100 // We want background_noise_.energy() / energy in Q14.
ivoc03392d02016-12-13 01:05:27 -0800101 int32_t bgn_energy = WEBRTC_SPL_SHIFT_W32(
102 background_noise_.Energy(channel_ix), scaling + 14);
henrik.lundin6608d9a2016-02-10 02:47:52 -0800103 int16_t energy_scaled =
104 static_cast<int16_t>(WEBRTC_SPL_SHIFT_W32(energy, scaling));
Peter Kastingb7e50542015-06-11 12:55:50 -0700105 int32_t ratio = WebRtcSpl_DivW32W16(bgn_energy, energy_scaled);
106 mute_factor = WebRtcSpl_SqrtFloor(ratio << 14);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000107 } else {
108 mute_factor = 16384; // 1.0 in Q14.
109 }
110 if (mute_factor > external_mute_factor_array[channel_ix]) {
Peter Kastingb7e50542015-06-11 12:55:50 -0700111 external_mute_factor_array[channel_ix] =
112 static_cast<int16_t>(std::min(mute_factor, 16384));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000113 }
114
115 // If muted increase by 0.64 for every 20 ms (NB/WB 0.0040/0.0020 in Q14).
Peter Kastingdce40cf2015-08-24 14:52:23 -0700116 int increment = 64 / fs_mult;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000117 for (size_t i = 0; i < length_per_channel; i++) {
118 // Scale with mute factor.
henrik.lundin80c06fa2016-11-14 08:18:52 -0800119 RTC_DCHECK_LT(channel_ix, output->Channels());
120 RTC_DCHECK_LT(i, output->Size());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000121 int32_t scaled_signal = (*output)[channel_ix][i] *
122 external_mute_factor_array[channel_ix];
123 // Shift 14 with proper rounding.
Peter Kastingb7e50542015-06-11 12:55:50 -0700124 (*output)[channel_ix][i] =
125 static_cast<int16_t>((scaled_signal + 8192) >> 14);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000126 // Increase mute_factor towards 16384.
Peter Kastingb7e50542015-06-11 12:55:50 -0700127 external_mute_factor_array[channel_ix] = static_cast<int16_t>(std::min(
128 external_mute_factor_array[channel_ix] + increment, 16384));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000129 }
130
131 // Interpolate the expanded data into the new vector.
132 // (NB/WB/SWB32/SWB48 8/16/32/48 samples.)
soren9f2c18e2017-04-10 02:22:46 -0700133 size_t win_length = samples_per_ms_;
134 int16_t win_slope_Q14 = default_win_slope_Q14_;
135 RTC_DCHECK_LT(channel_ix, output->Channels());
136 if (win_length > output->Size()) {
137 win_length = output->Size();
138 win_slope_Q14 = (1 << 14) / static_cast<int16_t>(win_length);
139 }
140 int16_t win_up_Q14 = 0;
141 for (size_t i = 0; i < win_length; i++) {
142 win_up_Q14 += win_slope_Q14;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000143 (*output)[channel_ix][i] =
soren9f2c18e2017-04-10 02:22:46 -0700144 (win_up_Q14 * (*output)[channel_ix][i] +
145 ((1 << 14) - win_up_Q14) * expanded[channel_ix][i] + (1 << 13)) >>
146 14;
147 }
soren0f109be2017-04-24 00:22:05 -0700148 RTC_DCHECK_GT(win_up_Q14,
149 (1 << 14) - 32); // Worst case rouding is a length of 34
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000150 }
151 } else if (last_mode == kModeRfc3389Cng) {
henrik.lundin80c06fa2016-11-14 08:18:52 -0800152 RTC_DCHECK_EQ(output->Channels(), 1); // Not adapted for multi-channel yet.
henrik.lundinc7668042016-08-25 23:53:38 -0700153 static const size_t kCngLength = 48;
kwiberg352444f2016-11-28 15:58:53 -0800154 RTC_DCHECK_LE(8 * fs_mult, kCngLength);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000155 int16_t cng_output[kCngLength];
156 // Reset mute factor and start up fresh.
157 external_mute_factor_array[0] = 16384;
ossu97ba30e2016-04-25 07:55:58 -0700158 ComfortNoiseDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000159
160 if (cng_decoder) {
henrik.lundinc7668042016-08-25 23:53:38 -0700161 // Generate long enough for 48kHz.
ossu97ba30e2016-04-25 07:55:58 -0700162 if (!cng_decoder->Generate(cng_output, 0)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000163 // 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.)
minyue-webrtc79553cb2016-05-10 19:55:56 +0200169 (*output)[0].CopyTo(fs_mult * 8, 0, cng_output);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000170 }
171 // Interpolate the CNG into the new vector.
172 // (NB/WB/SWB32/SWB48 8/16/32/48 samples.)
soren9f2c18e2017-04-10 02:22:46 -0700173 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 }
soren0f109be2017-04-24 00:22:05 -0700187 RTC_DCHECK_GT(win_up_Q14,
188 (1 << 14) - 32); // Worst case rouding is a length of 34
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000189 } else if (external_mute_factor_array[0] < 16384) {
190 // Previous was neither of Expand, FadeToBGN or RFC3389_CNG, but we are
191 // still ramping up from previous muting.
192 // If muted increase by 0.64 for every 20 ms (NB/WB 0.0040/0.0020 in Q14).
Peter Kastingdce40cf2015-08-24 14:52:23 -0700193 int increment = 64 / fs_mult;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000194 size_t length_per_channel = length / output->Channels();
195 for (size_t i = 0; i < length_per_channel; i++) {
196 for (size_t channel_ix = 0; channel_ix < output->Channels();
197 ++channel_ix) {
198 // Scale with mute factor.
henrik.lundin80c06fa2016-11-14 08:18:52 -0800199 RTC_DCHECK_LT(channel_ix, output->Channels());
200 RTC_DCHECK_LT(i, output->Size());
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000201 int32_t scaled_signal = (*output)[channel_ix][i] *
202 external_mute_factor_array[channel_ix];
203 // Shift 14 with proper rounding.
Peter Kastingb7e50542015-06-11 12:55:50 -0700204 (*output)[channel_ix][i] =
205 static_cast<int16_t>((scaled_signal + 8192) >> 14);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000206 // Increase mute_factor towards 16384.
Peter Kastingb7e50542015-06-11 12:55:50 -0700207 external_mute_factor_array[channel_ix] = static_cast<int16_t>(std::min(
208 16384, external_mute_factor_array[channel_ix] + increment));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000209 }
210 }
211 }
212
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000213 return static_cast<int>(length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000214}
215
216} // namespace webrtc