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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #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.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, |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 30 | AudioMultiVector* output) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 31 | if (length == 0) { |
| 32 | // Nothing to process. |
| 33 | output->Clear(); |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 34 | return static_cast<int>(length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 35 | } |
| 36 | |
henrik.lundin | 80c06fa | 2016-11-14 08:18:52 -0800 | [diff] [blame] | 37 | RTC_DCHECK(output->Empty()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 38 | // Output should be empty at this point. |
henrik.lundin@webrtc.org | ee0fb18 | 2014-09-02 13:22:11 +0000 | [diff] [blame] | 39 | 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.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 44 | output->PushBackInterleaved(input, length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 45 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 46 | const int fs_mult = fs_hz_ / 8000; |
henrik.lundin | 80c06fa | 2016-11-14 08:18:52 -0800 | [diff] [blame] | 47 | RTC_DCHECK_GT(fs_mult, 0); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 48 | // fs_shift = log2(fs_mult), rounded down. |
| 49 | // Note that |fs_shift| is not "exact" for 48 kHz. |
| 50 | // TODO(hlundin): Investigate this further. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 51 | const int fs_shift = 30 - WebRtcSpl_NormW32(fs_mult); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 52 | |
| 53 | // Check if last RecOut call resulted in an Expand. If so, we have to take |
| 54 | // care of some cross-fading and unmuting. |
| 55 | if (last_mode == kModeExpand) { |
| 56 | // Generate interpolation data using Expand. |
| 57 | // First, set Expand parameters to appropriate values. |
| 58 | expand_->SetParametersForNormalAfterExpand(); |
| 59 | |
| 60 | // Call Expand. |
henrik.lundin@webrtc.org | fd11bbf | 2013-09-30 20:38:44 +0000 | [diff] [blame] | 61 | AudioMultiVector expanded(output->Channels()); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 62 | expand_->Process(&expanded); |
| 63 | expand_->Reset(); |
| 64 | |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 65 | size_t length_per_channel = length / output->Channels(); |
| 66 | 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] | 67 | for (size_t channel_ix = 0; channel_ix < output->Channels(); ++channel_ix) { |
Henrik Lundin | 6dc82e8 | 2018-05-22 10:40:23 +0200 | [diff] [blame] | 68 | // Set muting factor to the same as expand muting factor. |
| 69 | int16_t mute_factor = expand_->MuteFactor(channel_ix); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 70 | |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 71 | (*output)[channel_ix].CopyTo(length_per_channel, 0, signal.get()); |
| 72 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 73 | // Find largest absolute value in new data. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 74 | int16_t decoded_max = |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 75 | WebRtcSpl_MaxAbsValueW16(signal.get(), length_per_channel); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 76 | // Adjust muting factor if needed (to BGN level). |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 77 | size_t energy_length = |
| 78 | std::min(static_cast<size_t>(fs_mult * 64), length_per_channel); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 79 | int scaling = 6 + fs_shift - WebRtcSpl_NormW32(decoded_max * decoded_max); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 80 | scaling = std::max(scaling, 0); // |scaling| should always be >= 0. |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 81 | int32_t energy = WebRtcSpl_DotProductWithScale(signal.get(), signal.get(), |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 82 | energy_length, scaling); |
Peter Kasting | f045e4d | 2015-06-10 21:15:38 -0700 | [diff] [blame] | 83 | int32_t scaled_energy_length = |
| 84 | static_cast<int32_t>(energy_length >> scaling); |
| 85 | if (scaled_energy_length > 0) { |
| 86 | energy = energy / scaled_energy_length; |
henrik.lundin@webrtc.org | ee0fb18 | 2014-09-02 13:22:11 +0000 | [diff] [blame] | 87 | } else { |
| 88 | energy = 0; |
| 89 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 90 | |
Henrik Lundin | 6dc82e8 | 2018-05-22 10:40:23 +0200 | [diff] [blame] | 91 | int local_mute_factor = 16384; // 1.0 in Q14. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 92 | if ((energy != 0) && (energy > background_noise_.Energy(channel_ix))) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 93 | // Normalize new frame energy to 15 bits. |
| 94 | scaling = WebRtcSpl_NormW32(energy) - 16; |
| 95 | // We want background_noise_.energy() / energy in Q14. |
ivoc | 03392d0 | 2016-12-13 01:05:27 -0800 | [diff] [blame] | 96 | int32_t bgn_energy = WEBRTC_SPL_SHIFT_W32( |
| 97 | background_noise_.Energy(channel_ix), scaling + 14); |
henrik.lundin | 6608d9a | 2016-02-10 02:47:52 -0800 | [diff] [blame] | 98 | int16_t energy_scaled = |
| 99 | static_cast<int16_t>(WEBRTC_SPL_SHIFT_W32(energy, scaling)); |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 100 | int32_t ratio = WebRtcSpl_DivW32W16(bgn_energy, energy_scaled); |
Henrik Lundin | 6dc82e8 | 2018-05-22 10:40:23 +0200 | [diff] [blame] | 101 | local_mute_factor = |
| 102 | std::min(local_mute_factor, WebRtcSpl_SqrtFloor(ratio << 14)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 103 | } |
Henrik Lundin | 6dc82e8 | 2018-05-22 10:40:23 +0200 | [diff] [blame] | 104 | mute_factor = std::max<int16_t>(mute_factor, local_mute_factor); |
| 105 | RTC_DCHECK_LE(mute_factor, 16384); |
| 106 | RTC_DCHECK_GE(mute_factor, 0); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 107 | |
Henrik Lundin | 6dc82e8 | 2018-05-22 10:40:23 +0200 | [diff] [blame] | 108 | // If muted increase by 0.64 for every 20 ms (NB/WB 0.0040/0.0020 in Q14), |
| 109 | // or as fast as it takes to come back to full gain within the frame |
| 110 | // length. |
| 111 | const int back_to_fullscale_inc = |
| 112 | static_cast<int>((16384 - mute_factor) / length_per_channel); |
| 113 | const int increment = std::max(64 / fs_mult, back_to_fullscale_inc); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 114 | for (size_t i = 0; i < length_per_channel; i++) { |
| 115 | // Scale with mute factor. |
henrik.lundin | 80c06fa | 2016-11-14 08:18:52 -0800 | [diff] [blame] | 116 | RTC_DCHECK_LT(channel_ix, output->Channels()); |
| 117 | RTC_DCHECK_LT(i, output->Size()); |
Henrik Lundin | 6dc82e8 | 2018-05-22 10:40:23 +0200 | [diff] [blame] | 118 | int32_t scaled_signal = (*output)[channel_ix][i] * mute_factor; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 119 | // Shift 14 with proper rounding. |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 120 | (*output)[channel_ix][i] = |
| 121 | static_cast<int16_t>((scaled_signal + 8192) >> 14); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 122 | // Increase mute_factor towards 16384. |
Henrik Lundin | 6dc82e8 | 2018-05-22 10:40:23 +0200 | [diff] [blame] | 123 | mute_factor = |
| 124 | static_cast<int16_t>(std::min(mute_factor + increment, 16384)); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // Interpolate the expanded data into the new vector. |
| 128 | // (NB/WB/SWB32/SWB48 8/16/32/48 samples.) |
soren | 9f2c18e | 2017-04-10 02:22:46 -0700 | [diff] [blame] | 129 | size_t win_length = samples_per_ms_; |
| 130 | int16_t win_slope_Q14 = default_win_slope_Q14_; |
| 131 | RTC_DCHECK_LT(channel_ix, output->Channels()); |
| 132 | if (win_length > output->Size()) { |
| 133 | win_length = output->Size(); |
| 134 | win_slope_Q14 = (1 << 14) / static_cast<int16_t>(win_length); |
| 135 | } |
| 136 | int16_t win_up_Q14 = 0; |
| 137 | for (size_t i = 0; i < win_length; i++) { |
| 138 | win_up_Q14 += win_slope_Q14; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 139 | (*output)[channel_ix][i] = |
soren | 9f2c18e | 2017-04-10 02:22:46 -0700 | [diff] [blame] | 140 | (win_up_Q14 * (*output)[channel_ix][i] + |
| 141 | ((1 << 14) - win_up_Q14) * expanded[channel_ix][i] + (1 << 13)) >> |
| 142 | 14; |
| 143 | } |
soren | 0f109be | 2017-04-24 00:22:05 -0700 | [diff] [blame] | 144 | RTC_DCHECK_GT(win_up_Q14, |
| 145 | (1 << 14) - 32); // Worst case rouding is a length of 34 |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 146 | } |
| 147 | } else if (last_mode == kModeRfc3389Cng) { |
henrik.lundin | 80c06fa | 2016-11-14 08:18:52 -0800 | [diff] [blame] | 148 | RTC_DCHECK_EQ(output->Channels(), 1); // Not adapted for multi-channel yet. |
henrik.lundin | c766804 | 2016-08-25 23:53:38 -0700 | [diff] [blame] | 149 | static const size_t kCngLength = 48; |
kwiberg | 352444f | 2016-11-28 15:58:53 -0800 | [diff] [blame] | 150 | RTC_DCHECK_LE(8 * fs_mult, kCngLength); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 151 | int16_t cng_output[kCngLength]; |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 152 | ComfortNoiseDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 153 | |
| 154 | if (cng_decoder) { |
henrik.lundin | c766804 | 2016-08-25 23:53:38 -0700 | [diff] [blame] | 155 | // Generate long enough for 48kHz. |
ossu | 97ba30e | 2016-04-25 07:55:58 -0700 | [diff] [blame] | 156 | if (!cng_decoder->Generate(cng_output, 0)) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 157 | // Error returned; set return vector to all zeros. |
| 158 | memset(cng_output, 0, sizeof(cng_output)); |
| 159 | } |
| 160 | } else { |
| 161 | // If no CNG instance is defined, just copy from the decoded data. |
| 162 | // (This will result in interpolating the decoded with itself.) |
minyue-webrtc | 79553cb | 2016-05-10 19:55:56 +0200 | [diff] [blame] | 163 | (*output)[0].CopyTo(fs_mult * 8, 0, cng_output); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 164 | } |
| 165 | // Interpolate the CNG into the new vector. |
| 166 | // (NB/WB/SWB32/SWB48 8/16/32/48 samples.) |
soren | 9f2c18e | 2017-04-10 02:22:46 -0700 | [diff] [blame] | 167 | size_t win_length = samples_per_ms_; |
| 168 | int16_t win_slope_Q14 = default_win_slope_Q14_; |
| 169 | if (win_length > kCngLength) { |
| 170 | win_length = kCngLength; |
| 171 | win_slope_Q14 = (1 << 14) / static_cast<int16_t>(win_length); |
| 172 | } |
| 173 | int16_t win_up_Q14 = 0; |
| 174 | for (size_t i = 0; i < win_length; i++) { |
| 175 | win_up_Q14 += win_slope_Q14; |
| 176 | (*output)[0][i] = |
| 177 | (win_up_Q14 * (*output)[0][i] + |
| 178 | ((1 << 14) - win_up_Q14) * cng_output[i] + (1 << 13)) >> |
| 179 | 14; |
| 180 | } |
soren | 0f109be | 2017-04-24 00:22:05 -0700 | [diff] [blame] | 181 | RTC_DCHECK_GT(win_up_Q14, |
| 182 | (1 << 14) - 32); // Worst case rouding is a length of 34 |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 183 | } |
| 184 | |
turaj@webrtc.org | 362a55e | 2013-09-20 16:25:28 +0000 | [diff] [blame] | 185 | return static_cast<int>(length); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | } // namespace webrtc |