henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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 | |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 11 | #include "webrtc/modules/audio_coding/neteq/normal.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <string.h> // memset, memcpy |
| 14 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 15 | #include <algorithm> // min |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 16 | |
henrik.lundin | 80c06fa | 2016-11-14 08:18:52 -0800 | [diff] [blame] | 17 | #include "webrtc/base/checks.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 18 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 19 | #include "webrtc/modules/audio_coding/codecs/audio_decoder.h" |
henrik.lundin@webrtc.org | 9c55f0f | 2014-06-09 08:10:28 +0000 | [diff] [blame] | 20 | #include "webrtc/modules/audio_coding/neteq/audio_multi_vector.h" |
| 21 | #include "webrtc/modules/audio_coding/neteq/background_noise.h" |
| 22 | #include "webrtc/modules/audio_coding/neteq/decoder_database.h" |
| 23 | #include "webrtc/modules/audio_coding/neteq/expand.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
| 27 | int Normal::Process(const int16_t* input, |
| 28 | size_t length, |
| 29 | Modes last_mode, |
| 30 | int16_t* external_mute_factor_array, |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 31 | AudioMultiVector* output) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 32 | if (length == 0) { |
| 33 | // Nothing to process. |
| 34 | output->Clear(); |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 35 | return static_cast<int>(length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 36 | } |
| 37 | |
henrik.lundin | 80c06fa | 2016-11-14 08:18:52 -0800 | [diff] [blame] | 38 | RTC_DCHECK(output->Empty()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 39 | // Output should be empty at this point. |
henrik.lundin@webrtc.org | ee0fb18 | 2014-09-02 13:22:11 +0000 | [diff] [blame] | 40 | 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.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 45 | output->PushBackInterleaved(input, length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 46 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 47 | const int fs_mult = fs_hz_ / 8000; |
henrik.lundin | 80c06fa | 2016-11-14 08:18:52 -0800 | [diff] [blame] | 48 | RTC_DCHECK_GT(fs_mult, 0); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 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 Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 52 | const int fs_shift = 30 - WebRtcSpl_NormW32(fs_mult); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 53 | |
| 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.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 62 | AudioMultiVector expanded(output->Channels()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 63 | expand_->Process(&expanded); |
| 64 | expand_->Reset(); |
| 65 | |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 66 | size_t length_per_channel = length / output->Channels(); |
| 67 | std::unique_ptr<int16_t[]> signal(new int16_t[length_per_channel]); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 68 | 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.org | 600587d | 2015-03-09 13:30:28 +0000 | [diff] [blame] | 71 | (external_mute_factor_array[channel_ix] * |
| 72 | expand_->MuteFactor(channel_ix)) >> 14); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 73 | |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 74 | (*output)[channel_ix].CopyTo(length_per_channel, 0, signal.get()); |
| 75 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 76 | // Find largest absolute value in new data. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 77 | int16_t decoded_max = |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 78 | WebRtcSpl_MaxAbsValueW16(signal.get(), length_per_channel); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 79 | // Adjust muting factor if needed (to BGN level). |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 80 | size_t energy_length = |
| 81 | std::min(static_cast<size_t>(fs_mult * 64), length_per_channel); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 82 | int scaling = 6 + fs_shift |
| 83 | - WebRtcSpl_NormW32(decoded_max * decoded_max); |
| 84 | scaling = std::max(scaling, 0); // |scaling| should always be >= 0. |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 85 | int32_t energy = WebRtcSpl_DotProductWithScale(signal.get(), signal.get(), |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 86 | energy_length, scaling); |
Peter Kasting | f045e4d | 2015-06-10 21:15:38 -0700 | [diff] [blame] | 87 | 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.org | ee0fb18 | 2014-09-02 13:22:11 +0000 | [diff] [blame] | 91 | } else { |
| 92 | energy = 0; |
| 93 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 94 | |
| 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. |
ivoc | 03392d0 | 2016-12-13 01:05:27 -0800 | [diff] [blame^] | 101 | int32_t bgn_energy = WEBRTC_SPL_SHIFT_W32( |
| 102 | background_noise_.Energy(channel_ix), scaling + 14); |
henrik.lundin | 6608d9a | 2016-02-10 02:47:52 -0800 | [diff] [blame] | 103 | int16_t energy_scaled = |
| 104 | static_cast<int16_t>(WEBRTC_SPL_SHIFT_W32(energy, scaling)); |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 105 | int32_t ratio = WebRtcSpl_DivW32W16(bgn_energy, energy_scaled); |
| 106 | mute_factor = WebRtcSpl_SqrtFloor(ratio << 14); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 107 | } else { |
| 108 | mute_factor = 16384; // 1.0 in Q14. |
| 109 | } |
| 110 | if (mute_factor > external_mute_factor_array[channel_ix]) { |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 111 | external_mute_factor_array[channel_ix] = |
| 112 | static_cast<int16_t>(std::min(mute_factor, 16384)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // If muted increase by 0.64 for every 20 ms (NB/WB 0.0040/0.0020 in Q14). |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 116 | int increment = 64 / fs_mult; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 117 | for (size_t i = 0; i < length_per_channel; i++) { |
| 118 | // Scale with mute factor. |
henrik.lundin | 80c06fa | 2016-11-14 08:18:52 -0800 | [diff] [blame] | 119 | RTC_DCHECK_LT(channel_ix, output->Channels()); |
| 120 | RTC_DCHECK_LT(i, output->Size()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 121 | int32_t scaled_signal = (*output)[channel_ix][i] * |
| 122 | external_mute_factor_array[channel_ix]; |
| 123 | // Shift 14 with proper rounding. |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 124 | (*output)[channel_ix][i] = |
| 125 | static_cast<int16_t>((scaled_signal + 8192) >> 14); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 126 | // Increase mute_factor towards 16384. |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 127 | external_mute_factor_array[channel_ix] = static_cast<int16_t>(std::min( |
| 128 | external_mute_factor_array[channel_ix] + increment, 16384)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // Interpolate the expanded data into the new vector. |
| 132 | // (NB/WB/SWB32/SWB48 8/16/32/48 samples.) |
henrik.lundin | 80c06fa | 2016-11-14 08:18:52 -0800 | [diff] [blame] | 133 | RTC_DCHECK_LT(fs_shift, 3); // Will always be 0, 1, or, 2. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 134 | increment = 4 >> fs_shift; |
| 135 | int fraction = increment; |
henrik.lundin | 80c06fa | 2016-11-14 08:18:52 -0800 | [diff] [blame] | 136 | // Don't interpolate over more samples than what is in output. When this |
| 137 | // cap strikes, the interpolation will likely sound worse, but this is an |
| 138 | // emergency operation in response to unexpected input. |
| 139 | const size_t interp_len_samples = |
| 140 | std::min(static_cast<size_t>(8 * fs_mult), output->Size()); |
| 141 | for (size_t i = 0; i < interp_len_samples; ++i) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 142 | // TODO(hlundin): Add 16 instead of 8 for correct rounding. Keeping 8 |
| 143 | // now for legacy bit-exactness. |
henrik.lundin | 80c06fa | 2016-11-14 08:18:52 -0800 | [diff] [blame] | 144 | RTC_DCHECK_LT(channel_ix, output->Channels()); |
| 145 | RTC_DCHECK_LT(i, output->Size()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 146 | (*output)[channel_ix][i] = |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 147 | static_cast<int16_t>((fraction * (*output)[channel_ix][i] + |
| 148 | (32 - fraction) * expanded[channel_ix][i] + 8) >> 5); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 149 | fraction += increment; |
| 150 | } |
| 151 | } |
| 152 | } else if (last_mode == kModeRfc3389Cng) { |
henrik.lundin | 80c06fa | 2016-11-14 08:18:52 -0800 | [diff] [blame] | 153 | RTC_DCHECK_EQ(output->Channels(), 1); // Not adapted for multi-channel yet. |
henrik.lundin | c766804 | 2016-08-25 23:53:38 -0700 | [diff] [blame] | 154 | static const size_t kCngLength = 48; |
kwiberg | 352444f | 2016-11-28 15:58:53 -0800 | [diff] [blame] | 155 | RTC_DCHECK_LE(8 * fs_mult, kCngLength); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 156 | int16_t cng_output[kCngLength]; |
| 157 | // Reset mute factor and start up fresh. |
| 158 | external_mute_factor_array[0] = 16384; |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 159 | ComfortNoiseDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 160 | |
| 161 | if (cng_decoder) { |
henrik.lundin | c766804 | 2016-08-25 23:53:38 -0700 | [diff] [blame] | 162 | // Generate long enough for 48kHz. |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 163 | if (!cng_decoder->Generate(cng_output, 0)) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 164 | // Error returned; set return vector to all zeros. |
| 165 | memset(cng_output, 0, sizeof(cng_output)); |
| 166 | } |
| 167 | } else { |
| 168 | // If no CNG instance is defined, just copy from the decoded data. |
| 169 | // (This will result in interpolating the decoded with itself.) |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 170 | (*output)[0].CopyTo(fs_mult * 8, 0, cng_output); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 171 | } |
| 172 | // Interpolate the CNG into the new vector. |
| 173 | // (NB/WB/SWB32/SWB48 8/16/32/48 samples.) |
henrik.lundin | 80c06fa | 2016-11-14 08:18:52 -0800 | [diff] [blame] | 174 | RTC_DCHECK_LT(fs_shift, 3); // Will always be 0, 1, or, 2. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 175 | int16_t increment = 4 >> fs_shift; |
| 176 | int16_t fraction = increment; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 177 | for (size_t i = 0; i < static_cast<size_t>(8 * fs_mult); i++) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 178 | // TODO(hlundin): Add 16 instead of 8 for correct rounding. Keeping 8 now |
| 179 | // for legacy bit-exactness. |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 180 | (*output)[0][i] = (fraction * (*output)[0][i] + |
| 181 | (32 - fraction) * cng_output[i] + 8) >> 5; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 182 | fraction += increment; |
| 183 | } |
| 184 | } else if (external_mute_factor_array[0] < 16384) { |
| 185 | // Previous was neither of Expand, FadeToBGN or RFC3389_CNG, but we are |
| 186 | // still ramping up from previous muting. |
| 187 | // If muted increase by 0.64 for every 20 ms (NB/WB 0.0040/0.0020 in Q14). |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 188 | int increment = 64 / fs_mult; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 189 | size_t length_per_channel = length / output->Channels(); |
| 190 | for (size_t i = 0; i < length_per_channel; i++) { |
| 191 | for (size_t channel_ix = 0; channel_ix < output->Channels(); |
| 192 | ++channel_ix) { |
| 193 | // Scale with mute factor. |
henrik.lundin | 80c06fa | 2016-11-14 08:18:52 -0800 | [diff] [blame] | 194 | RTC_DCHECK_LT(channel_ix, output->Channels()); |
| 195 | RTC_DCHECK_LT(i, output->Size()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 196 | int32_t scaled_signal = (*output)[channel_ix][i] * |
| 197 | external_mute_factor_array[channel_ix]; |
| 198 | // Shift 14 with proper rounding. |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 199 | (*output)[channel_ix][i] = |
| 200 | static_cast<int16_t>((scaled_signal + 8192) >> 14); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 201 | // Increase mute_factor towards 16384. |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 202 | external_mute_factor_array[channel_ix] = static_cast<int16_t>(std::min( |
| 203 | 16384, external_mute_factor_array[channel_ix] + increment)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 208 | return static_cast<int>(length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | } // namespace webrtc |