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 | |
| 17 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
kwiberg@webrtc.org | e04a93b | 2014-12-09 10:12:53 +0000 | [diff] [blame] | 18 | #include "webrtc/modules/audio_coding/codecs/audio_decoder.h" |
kjellander@webrtc.org | 3c652b6 | 2015-11-18 23:07:57 +0100 | [diff] [blame] | 19 | #include "webrtc/modules/audio_coding/codecs/cng/webrtc_cng.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 | |
| 38 | assert(output->Empty()); |
| 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); |
| 46 | int16_t* signal = &(*output)[0][0]; |
| 47 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 48 | const int fs_mult = fs_hz_ / 8000; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 49 | assert(fs_mult > 0); |
| 50 | // fs_shift = log2(fs_mult), rounded down. |
| 51 | // Note that |fs_shift| is not "exact" for 48 kHz. |
| 52 | // TODO(hlundin): Investigate this further. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 53 | const int fs_shift = 30 - WebRtcSpl_NormW32(fs_mult); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 54 | |
| 55 | // Check if last RecOut call resulted in an Expand. If so, we have to take |
| 56 | // care of some cross-fading and unmuting. |
| 57 | if (last_mode == kModeExpand) { |
| 58 | // Generate interpolation data using Expand. |
| 59 | // First, set Expand parameters to appropriate values. |
| 60 | expand_->SetParametersForNormalAfterExpand(); |
| 61 | |
| 62 | // Call Expand. |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 63 | AudioMultiVector expanded(output->Channels()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 64 | expand_->Process(&expanded); |
| 65 | expand_->Reset(); |
| 66 | |
| 67 | for (size_t channel_ix = 0; channel_ix < output->Channels(); ++channel_ix) { |
| 68 | // Adjust muting factor (main muting factor times expand muting factor). |
| 69 | external_mute_factor_array[channel_ix] = static_cast<int16_t>( |
bjornv@webrtc.org | 600587d | 2015-03-09 13:30:28 +0000 | [diff] [blame] | 70 | (external_mute_factor_array[channel_ix] * |
| 71 | expand_->MuteFactor(channel_ix)) >> 14); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 72 | |
| 73 | int16_t* signal = &(*output)[channel_ix][0]; |
| 74 | size_t length_per_channel = length / output->Channels(); |
| 75 | // Find largest absolute value in new data. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 76 | int16_t decoded_max = |
| 77 | WebRtcSpl_MaxAbsValueW16(signal, length_per_channel); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 78 | // Adjust muting factor if needed (to BGN level). |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 79 | size_t energy_length = |
| 80 | 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] | 81 | int scaling = 6 + fs_shift |
| 82 | - WebRtcSpl_NormW32(decoded_max * decoded_max); |
| 83 | scaling = std::max(scaling, 0); // |scaling| should always be >= 0. |
| 84 | int32_t energy = WebRtcSpl_DotProductWithScale(signal, signal, |
| 85 | energy_length, scaling); |
Peter Kasting | f045e4d | 2015-06-10 21:15:38 -0700 | [diff] [blame] | 86 | int32_t scaled_energy_length = |
| 87 | static_cast<int32_t>(energy_length >> scaling); |
| 88 | if (scaled_energy_length > 0) { |
| 89 | energy = energy / scaled_energy_length; |
henrik.lundin@webrtc.org | ee0fb18 | 2014-09-02 13:22:11 +0000 | [diff] [blame] | 90 | } else { |
| 91 | energy = 0; |
| 92 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 93 | |
| 94 | int mute_factor; |
| 95 | if ((energy != 0) && |
| 96 | (energy > background_noise_.Energy(channel_ix))) { |
| 97 | // Normalize new frame energy to 15 bits. |
| 98 | scaling = WebRtcSpl_NormW32(energy) - 16; |
| 99 | // We want background_noise_.energy() / energy in Q14. |
| 100 | int32_t bgn_energy = |
| 101 | background_noise_.Energy(channel_ix) << (scaling+14); |
henrik.lundin | 6608d9a | 2016-02-10 02:47:52 -0800 | [diff] [blame^] | 102 | int16_t energy_scaled = |
| 103 | static_cast<int16_t>(WEBRTC_SPL_SHIFT_W32(energy, scaling)); |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 104 | int32_t ratio = WebRtcSpl_DivW32W16(bgn_energy, energy_scaled); |
| 105 | mute_factor = WebRtcSpl_SqrtFloor(ratio << 14); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 106 | } else { |
| 107 | mute_factor = 16384; // 1.0 in Q14. |
| 108 | } |
| 109 | if (mute_factor > external_mute_factor_array[channel_ix]) { |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 110 | external_mute_factor_array[channel_ix] = |
| 111 | static_cast<int16_t>(std::min(mute_factor, 16384)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // 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] | 115 | int increment = 64 / fs_mult; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 116 | for (size_t i = 0; i < length_per_channel; i++) { |
| 117 | // Scale with mute factor. |
| 118 | assert(channel_ix < output->Channels()); |
| 119 | assert(i < output->Size()); |
| 120 | int32_t scaled_signal = (*output)[channel_ix][i] * |
| 121 | external_mute_factor_array[channel_ix]; |
| 122 | // Shift 14 with proper rounding. |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 123 | (*output)[channel_ix][i] = |
| 124 | static_cast<int16_t>((scaled_signal + 8192) >> 14); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 125 | // Increase mute_factor towards 16384. |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 126 | external_mute_factor_array[channel_ix] = static_cast<int16_t>(std::min( |
| 127 | external_mute_factor_array[channel_ix] + increment, 16384)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | // Interpolate the expanded data into the new vector. |
| 131 | // (NB/WB/SWB32/SWB48 8/16/32/48 samples.) |
| 132 | assert(fs_shift < 3); // Will always be 0, 1, or, 2. |
| 133 | increment = 4 >> fs_shift; |
| 134 | int fraction = increment; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 135 | 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] | 136 | // TODO(hlundin): Add 16 instead of 8 for correct rounding. Keeping 8 |
| 137 | // now for legacy bit-exactness. |
| 138 | assert(channel_ix < output->Channels()); |
| 139 | assert(i < output->Size()); |
| 140 | (*output)[channel_ix][i] = |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 141 | static_cast<int16_t>((fraction * (*output)[channel_ix][i] + |
| 142 | (32 - fraction) * expanded[channel_ix][i] + 8) >> 5); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 143 | fraction += increment; |
| 144 | } |
| 145 | } |
| 146 | } else if (last_mode == kModeRfc3389Cng) { |
| 147 | assert(output->Channels() == 1); // Not adapted for multi-channel yet. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 148 | static const size_t kCngLength = 32; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 149 | int16_t cng_output[kCngLength]; |
| 150 | // Reset mute factor and start up fresh. |
| 151 | external_mute_factor_array[0] = 16384; |
| 152 | AudioDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder(); |
| 153 | |
| 154 | if (cng_decoder) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 155 | // Generate long enough for 32kHz. |
kwiberg@webrtc.org | 8b2058e | 2014-11-06 07:54:31 +0000 | [diff] [blame] | 156 | if (WebRtcCng_Generate(cng_decoder->CngDecoderInstance(), cng_output, |
| 157 | kCngLength, 0) < 0) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 158 | // Error returned; set return vector to all zeros. |
| 159 | memset(cng_output, 0, sizeof(cng_output)); |
| 160 | } |
| 161 | } else { |
| 162 | // If no CNG instance is defined, just copy from the decoded data. |
| 163 | // (This will result in interpolating the decoded with itself.) |
| 164 | memcpy(cng_output, signal, fs_mult * 8 * sizeof(int16_t)); |
| 165 | } |
| 166 | // Interpolate the CNG into the new vector. |
| 167 | // (NB/WB/SWB32/SWB48 8/16/32/48 samples.) |
| 168 | assert(fs_shift < 3); // Will always be 0, 1, or, 2. |
| 169 | int16_t increment = 4 >> fs_shift; |
| 170 | int16_t fraction = increment; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 171 | 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] | 172 | // TODO(hlundin): Add 16 instead of 8 for correct rounding. Keeping 8 now |
| 173 | // for legacy bit-exactness. |
| 174 | signal[i] = |
| 175 | (fraction * signal[i] + (32 - fraction) * cng_output[i] + 8) >> 5; |
| 176 | fraction += increment; |
| 177 | } |
| 178 | } else if (external_mute_factor_array[0] < 16384) { |
| 179 | // Previous was neither of Expand, FadeToBGN or RFC3389_CNG, but we are |
| 180 | // still ramping up from previous muting. |
| 181 | // 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] | 182 | int increment = 64 / fs_mult; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 183 | size_t length_per_channel = length / output->Channels(); |
| 184 | for (size_t i = 0; i < length_per_channel; i++) { |
| 185 | for (size_t channel_ix = 0; channel_ix < output->Channels(); |
| 186 | ++channel_ix) { |
| 187 | // Scale with mute factor. |
| 188 | assert(channel_ix < output->Channels()); |
| 189 | assert(i < output->Size()); |
| 190 | int32_t scaled_signal = (*output)[channel_ix][i] * |
| 191 | external_mute_factor_array[channel_ix]; |
| 192 | // Shift 14 with proper rounding. |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 193 | (*output)[channel_ix][i] = |
| 194 | static_cast<int16_t>((scaled_signal + 8192) >> 14); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 195 | // Increase mute_factor towards 16384. |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 196 | external_mute_factor_array[channel_ix] = static_cast<int16_t>(std::min( |
| 197 | 16384, external_mute_factor_array[channel_ix] + increment)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 202 | return static_cast<int>(length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | } // namespace webrtc |