blob: 4400afb3b01517f8b43441e762fc48c1522d9d16 [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
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000011#include "webrtc/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
17#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
kwiberg@webrtc.orge04a93b2014-12-09 10:12:53 +000018#include "webrtc/modules/audio_coding/codecs/audio_decoder.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000019#include "webrtc/modules/audio_coding/neteq/audio_multi_vector.h"
20#include "webrtc/modules/audio_coding/neteq/background_noise.h"
21#include "webrtc/modules/audio_coding/neteq/decoder_database.h"
22#include "webrtc/modules/audio_coding/neteq/expand.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000023
24namespace webrtc {
25
26int Normal::Process(const int16_t* input,
27 size_t length,
28 Modes last_mode,
29 int16_t* external_mute_factor_array,
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000030 AudioMultiVector* output) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000031 if (length == 0) {
32 // Nothing to process.
33 output->Clear();
turaj@webrtc.org362a55e2013-09-20 16:25:28 +000034 return static_cast<int>(length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000035 }
36
37 assert(output->Empty());
38 // Output should be empty at this point.
henrik.lundin@webrtc.orgee0fb182014-09-02 13:22:11 +000039 if (length % output->Channels() != 0) {
40 // The length does not match the number of channels.
41 output->Clear();
42 return 0;
43 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000044 output->PushBackInterleaved(input, length);
45 int16_t* signal = &(*output)[0][0];
46
Peter Kastingdce40cf2015-08-24 14:52:23 -070047 const int fs_mult = fs_hz_ / 8000;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000048 assert(fs_mult > 0);
49 // 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
66 for (size_t channel_ix = 0; channel_ix < output->Channels(); ++channel_ix) {
67 // Adjust muting factor (main muting factor times expand muting factor).
68 external_mute_factor_array[channel_ix] = static_cast<int16_t>(
bjornv@webrtc.org600587d2015-03-09 13:30:28 +000069 (external_mute_factor_array[channel_ix] *
70 expand_->MuteFactor(channel_ix)) >> 14);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000071
72 int16_t* signal = &(*output)[channel_ix][0];
73 size_t length_per_channel = length / output->Channels();
74 // Find largest absolute value in new data.
Peter Kastingdce40cf2015-08-24 14:52:23 -070075 int16_t decoded_max =
76 WebRtcSpl_MaxAbsValueW16(signal, length_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000077 // Adjust muting factor if needed (to BGN level).
Peter Kastingdce40cf2015-08-24 14:52:23 -070078 size_t energy_length =
79 std::min(static_cast<size_t>(fs_mult * 64), length_per_channel);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000080 int scaling = 6 + fs_shift
81 - WebRtcSpl_NormW32(decoded_max * decoded_max);
82 scaling = std::max(scaling, 0); // |scaling| should always be >= 0.
83 int32_t energy = WebRtcSpl_DotProductWithScale(signal, signal,
84 energy_length, scaling);
Peter Kastingf045e4d2015-06-10 21:15:38 -070085 int32_t scaled_energy_length =
86 static_cast<int32_t>(energy_length >> scaling);
87 if (scaled_energy_length > 0) {
88 energy = energy / scaled_energy_length;
henrik.lundin@webrtc.orgee0fb182014-09-02 13:22:11 +000089 } else {
90 energy = 0;
91 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000092
93 int mute_factor;
94 if ((energy != 0) &&
95 (energy > background_noise_.Energy(channel_ix))) {
96 // Normalize new frame energy to 15 bits.
97 scaling = WebRtcSpl_NormW32(energy) - 16;
98 // We want background_noise_.energy() / energy in Q14.
99 int32_t bgn_energy =
100 background_noise_.Energy(channel_ix) << (scaling+14);
henrik.lundin6608d9a2016-02-10 02:47:52 -0800101 int16_t energy_scaled =
102 static_cast<int16_t>(WEBRTC_SPL_SHIFT_W32(energy, scaling));
Peter Kastingb7e50542015-06-11 12:55:50 -0700103 int32_t ratio = WebRtcSpl_DivW32W16(bgn_energy, energy_scaled);
104 mute_factor = WebRtcSpl_SqrtFloor(ratio << 14);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000105 } else {
106 mute_factor = 16384; // 1.0 in Q14.
107 }
108 if (mute_factor > external_mute_factor_array[channel_ix]) {
Peter Kastingb7e50542015-06-11 12:55:50 -0700109 external_mute_factor_array[channel_ix] =
110 static_cast<int16_t>(std::min(mute_factor, 16384));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000111 }
112
113 // 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 -0700114 int increment = 64 / fs_mult;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000115 for (size_t i = 0; i < length_per_channel; i++) {
116 // Scale with mute factor.
117 assert(channel_ix < output->Channels());
118 assert(i < output->Size());
119 int32_t scaled_signal = (*output)[channel_ix][i] *
120 external_mute_factor_array[channel_ix];
121 // Shift 14 with proper rounding.
Peter Kastingb7e50542015-06-11 12:55:50 -0700122 (*output)[channel_ix][i] =
123 static_cast<int16_t>((scaled_signal + 8192) >> 14);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000124 // Increase mute_factor towards 16384.
Peter Kastingb7e50542015-06-11 12:55:50 -0700125 external_mute_factor_array[channel_ix] = static_cast<int16_t>(std::min(
126 external_mute_factor_array[channel_ix] + increment, 16384));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000127 }
128
129 // Interpolate the expanded data into the new vector.
130 // (NB/WB/SWB32/SWB48 8/16/32/48 samples.)
131 assert(fs_shift < 3); // Will always be 0, 1, or, 2.
132 increment = 4 >> fs_shift;
133 int fraction = increment;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700134 for (size_t i = 0; i < static_cast<size_t>(8 * fs_mult); i++) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000135 // TODO(hlundin): Add 16 instead of 8 for correct rounding. Keeping 8
136 // now for legacy bit-exactness.
137 assert(channel_ix < output->Channels());
138 assert(i < output->Size());
139 (*output)[channel_ix][i] =
Peter Kastingb7e50542015-06-11 12:55:50 -0700140 static_cast<int16_t>((fraction * (*output)[channel_ix][i] +
141 (32 - fraction) * expanded[channel_ix][i] + 8) >> 5);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000142 fraction += increment;
143 }
144 }
145 } else if (last_mode == kModeRfc3389Cng) {
146 assert(output->Channels() == 1); // Not adapted for multi-channel yet.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700147 static const size_t kCngLength = 32;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000148 int16_t cng_output[kCngLength];
149 // Reset mute factor and start up fresh.
150 external_mute_factor_array[0] = 16384;
ossu97ba30e2016-04-25 07:55:58 -0700151 ComfortNoiseDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000152
153 if (cng_decoder) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000154 // Generate long enough for 32kHz.
ossu97ba30e2016-04-25 07:55:58 -0700155 if (!cng_decoder->Generate(cng_output, 0)) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000156 // Error returned; set return vector to all zeros.
157 memset(cng_output, 0, sizeof(cng_output));
158 }
159 } else {
160 // If no CNG instance is defined, just copy from the decoded data.
161 // (This will result in interpolating the decoded with itself.)
162 memcpy(cng_output, signal, fs_mult * 8 * sizeof(int16_t));
163 }
164 // Interpolate the CNG into the new vector.
165 // (NB/WB/SWB32/SWB48 8/16/32/48 samples.)
166 assert(fs_shift < 3); // Will always be 0, 1, or, 2.
167 int16_t increment = 4 >> fs_shift;
168 int16_t fraction = increment;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700169 for (size_t i = 0; i < static_cast<size_t>(8 * fs_mult); i++) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000170 // TODO(hlundin): Add 16 instead of 8 for correct rounding. Keeping 8 now
171 // for legacy bit-exactness.
172 signal[i] =
173 (fraction * signal[i] + (32 - fraction) * cng_output[i] + 8) >> 5;
174 fraction += increment;
175 }
176 } else if (external_mute_factor_array[0] < 16384) {
177 // Previous was neither of Expand, FadeToBGN or RFC3389_CNG, but we are
178 // still ramping up from previous muting.
179 // 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 -0700180 int increment = 64 / fs_mult;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000181 size_t length_per_channel = length / output->Channels();
182 for (size_t i = 0; i < length_per_channel; i++) {
183 for (size_t channel_ix = 0; channel_ix < output->Channels();
184 ++channel_ix) {
185 // Scale with mute factor.
186 assert(channel_ix < output->Channels());
187 assert(i < output->Size());
188 int32_t scaled_signal = (*output)[channel_ix][i] *
189 external_mute_factor_array[channel_ix];
190 // Shift 14 with proper rounding.
Peter Kastingb7e50542015-06-11 12:55:50 -0700191 (*output)[channel_ix][i] =
192 static_cast<int16_t>((scaled_signal + 8192) >> 14);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000193 // Increase mute_factor towards 16384.
Peter Kastingb7e50542015-06-11 12:55:50 -0700194 external_mute_factor_array[channel_ix] = static_cast<int16_t>(std::min(
195 16384, external_mute_factor_array[channel_ix] + increment));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000196 }
197 }
198 }
199
turaj@webrtc.org362a55e2013-09-20 16:25:28 +0000200 return static_cast<int>(length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000201}
202
203} // namespace webrtc