peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | |
| 11 | #include "webrtc/modules/audio_processing/aec3/suppression_gain.h" |
| 12 | |
| 13 | #include "webrtc/typedefs.h" |
| 14 | #if defined(WEBRTC_ARCH_X86_FAMILY) |
| 15 | #include <emmintrin.h> |
| 16 | #endif |
| 17 | #include <math.h> |
| 18 | #include <algorithm> |
| 19 | #include <functional> |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 20 | #include <numeric> |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 21 | |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 22 | #include "webrtc/base/checks.h" |
peah | 5e79b29 | 2017-04-12 01:20:45 -0700 | [diff] [blame] | 23 | #include "webrtc/modules/audio_processing/aec3/vector_math.h" |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame] | 24 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 25 | namespace webrtc { |
| 26 | namespace { |
| 27 | |
peah | a2376e7 | 2017-02-27 01:15:24 -0800 | [diff] [blame] | 28 | void GainPostProcessing(std::array<float, kFftLengthBy2Plus1>* gain_squared) { |
| 29 | // Limit the low frequency gains to avoid the impact of the high-pass filter |
| 30 | // on the lower-frequency gain influencing the overall achieved gain. |
| 31 | (*gain_squared)[1] = std::min((*gain_squared)[1], (*gain_squared)[2]); |
| 32 | (*gain_squared)[0] = (*gain_squared)[1]; |
| 33 | |
| 34 | // Limit the high frequency gains to avoid the impact of the anti-aliasing |
| 35 | // filter on the upper-frequency gains influencing the overall achieved |
| 36 | // gain. TODO(peah): Update this when new anti-aliasing filters are |
| 37 | // implemented. |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 38 | constexpr size_t kAntiAliasingImpactLimit = (64 * 2000) / 8000; |
peah | a2376e7 | 2017-02-27 01:15:24 -0800 | [diff] [blame] | 39 | std::for_each(gain_squared->begin() + kAntiAliasingImpactLimit, |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 40 | gain_squared->end() - 1, |
peah | a2376e7 | 2017-02-27 01:15:24 -0800 | [diff] [blame] | 41 | [gain_squared, kAntiAliasingImpactLimit](float& a) { |
| 42 | a = std::min(a, (*gain_squared)[kAntiAliasingImpactLimit]); |
| 43 | }); |
| 44 | (*gain_squared)[kFftLengthBy2] = (*gain_squared)[kFftLengthBy2Minus1]; |
| 45 | } |
| 46 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 47 | constexpr int kNumIterations = 2; |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 48 | constexpr float kEchoMaskingMargin = 1.f / 20.f; |
| 49 | constexpr float kBandMaskingFactor = 1.f / 10.f; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 50 | constexpr float kTimeMaskingFactor = 1.f / 10.f; |
| 51 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 52 | // TODO(peah): Add further optimizations, in particular for the divisions. |
peah | 5e79b29 | 2017-04-12 01:20:45 -0700 | [diff] [blame] | 53 | void ComputeGains( |
| 54 | Aec3Optimization optimization, |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 55 | const std::array<float, kFftLengthBy2Plus1>& nearend_power, |
| 56 | const std::array<float, kFftLengthBy2Plus1>& residual_echo_power, |
| 57 | const std::array<float, kFftLengthBy2Plus1>& comfort_noise_power, |
| 58 | float strong_nearend_margin, |
| 59 | std::array<float, kFftLengthBy2Minus1>* previous_gain_squared, |
| 60 | std::array<float, kFftLengthBy2Minus1>* previous_masker, |
| 61 | std::array<float, kFftLengthBy2Plus1>* gain) { |
| 62 | std::array<float, kFftLengthBy2Minus1> masker; |
| 63 | std::array<float, kFftLengthBy2Minus1> same_band_masker; |
| 64 | std::array<float, kFftLengthBy2Minus1> one_by_residual_echo_power; |
| 65 | std::array<bool, kFftLengthBy2Minus1> strong_nearend; |
| 66 | std::array<float, kFftLengthBy2Plus1> neighboring_bands_masker; |
| 67 | std::array<float, kFftLengthBy2Plus1>* gain_squared = gain; |
peah | 5e79b29 | 2017-04-12 01:20:45 -0700 | [diff] [blame] | 68 | aec3::VectorMath math(optimization); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 69 | |
| 70 | // Precompute 1/residual_echo_power. |
| 71 | std::transform(residual_echo_power.begin() + 1, residual_echo_power.end() - 1, |
| 72 | one_by_residual_echo_power.begin(), |
| 73 | [](float a) { return a > 0.f ? 1.f / a : -1.f; }); |
| 74 | |
| 75 | // Precompute indicators for bands with strong nearend. |
| 76 | std::transform( |
| 77 | residual_echo_power.begin() + 1, residual_echo_power.end() - 1, |
| 78 | nearend_power.begin() + 1, strong_nearend.begin(), |
| 79 | [&](float a, float b) { return a <= strong_nearend_margin * b; }); |
| 80 | |
| 81 | // Precompute masker for the same band. |
| 82 | std::transform(comfort_noise_power.begin() + 1, comfort_noise_power.end() - 1, |
| 83 | previous_masker->begin(), same_band_masker.begin(), |
| 84 | [&](float a, float b) { return a + kTimeMaskingFactor * b; }); |
| 85 | |
| 86 | for (int k = 0; k < kNumIterations; ++k) { |
| 87 | if (k == 0) { |
| 88 | // Add masker from the same band. |
| 89 | std::copy(same_band_masker.begin(), same_band_masker.end(), |
| 90 | masker.begin()); |
| 91 | } else { |
| 92 | // Add masker for neighboring bands. |
peah | 5e79b29 | 2017-04-12 01:20:45 -0700 | [diff] [blame] | 93 | math.Multiply(nearend_power, *gain_squared, neighboring_bands_masker); |
| 94 | math.Accumulate(comfort_noise_power, neighboring_bands_masker); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 95 | std::transform( |
| 96 | neighboring_bands_masker.begin(), neighboring_bands_masker.end() - 2, |
| 97 | neighboring_bands_masker.begin() + 2, masker.begin(), |
| 98 | [&](float a, float b) { return kBandMaskingFactor * (a + b); }); |
| 99 | |
| 100 | // Add masker from the same band. |
peah | 5e79b29 | 2017-04-12 01:20:45 -0700 | [diff] [blame] | 101 | math.Accumulate(same_band_masker, masker); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Compute new gain as: |
| 105 | // G2(t,f) = (comfort_noise_power(t,f) + G2(t-1)*nearend_power(t-1)) * |
| 106 | // kTimeMaskingFactor |
| 107 | // * kEchoMaskingMargin / residual_echo_power(t,f). |
| 108 | // or |
| 109 | // G2(t,f) = ((comfort_noise_power(t,f) + G2(t-1) * |
| 110 | // nearend_power(t-1)) * kTimeMaskingFactor + |
| 111 | // (comfort_noise_power(t, f-1) + comfort_noise_power(t, f+1) + |
| 112 | // (G2(t,f-1)*nearend_power(t, f-1) + |
| 113 | // G2(t,f+1)*nearend_power(t, f+1)) * |
| 114 | // kTimeMaskingFactor) * kBandMaskingFactor) |
| 115 | // * kEchoMaskingMargin / residual_echo_power(t,f). |
| 116 | std::transform( |
| 117 | masker.begin(), masker.end(), one_by_residual_echo_power.begin(), |
| 118 | gain_squared->begin() + 1, [&](float a, float b) { |
| 119 | return b >= 0 ? std::min(kEchoMaskingMargin * a * b, 1.f) : 1.f; |
| 120 | }); |
| 121 | |
| 122 | // Limit gain for bands with strong nearend. |
| 123 | std::transform(gain_squared->begin() + 1, gain_squared->end() - 1, |
| 124 | strong_nearend.begin(), gain_squared->begin() + 1, |
| 125 | [](float a, bool b) { return b ? 1.f : a; }); |
| 126 | |
| 127 | // Limit the allowed gain update over time. |
| 128 | std::transform(gain_squared->begin() + 1, gain_squared->end() - 1, |
| 129 | previous_gain_squared->begin(), gain_squared->begin() + 1, |
| 130 | [](float a, float b) { |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 131 | return b < 0.001f ? std::min(a, 0.001f) |
| 132 | : std::min(a, b * 2.f); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 133 | }); |
| 134 | |
peah | a2376e7 | 2017-02-27 01:15:24 -0800 | [diff] [blame] | 135 | // Process the gains to avoid artefacts caused by gain realization in the |
| 136 | // filterbank and impact of external pre-processing of the signal. |
| 137 | GainPostProcessing(gain_squared); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | std::copy(gain_squared->begin() + 1, gain_squared->end() - 1, |
| 141 | previous_gain_squared->begin()); |
| 142 | |
peah | 5e79b29 | 2017-04-12 01:20:45 -0700 | [diff] [blame] | 143 | math.Multiply( |
| 144 | rtc::ArrayView<const float>(&(*gain_squared)[1], previous_masker->size()), |
| 145 | rtc::ArrayView<const float>(&nearend_power[1], previous_masker->size()), |
| 146 | *previous_masker); |
| 147 | math.Accumulate(rtc::ArrayView<const float>(&comfort_noise_power[1], |
| 148 | previous_masker->size()), |
| 149 | *previous_masker); |
| 150 | math.Sqrt(*gain); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 151 | } |
| 152 | |
peah | 5e79b29 | 2017-04-12 01:20:45 -0700 | [diff] [blame] | 153 | } // namespace |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 154 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 155 | // Computes an upper bound on the gain to apply for high frequencies. |
| 156 | float HighFrequencyGainBound(bool saturated_echo, |
| 157 | const std::vector<std::vector<float>>& render) { |
| 158 | if (render.size() == 1) { |
| 159 | return 1.f; |
| 160 | } |
| 161 | |
| 162 | // Always attenuate the upper bands when there is saturated echo. |
| 163 | if (saturated_echo) { |
| 164 | return 0.001f; |
| 165 | } |
| 166 | |
| 167 | // Compute the upper and lower band energies. |
| 168 | float low_band_energy = |
| 169 | std::accumulate(render[0].begin(), render[0].end(), 0.f, |
| 170 | [](float a, float b) -> float { return a + b * b; }); |
| 171 | float high_band_energies = 0.f; |
| 172 | for (size_t k = 1; k < render.size(); ++k) { |
| 173 | high_band_energies = std::max( |
| 174 | high_band_energies, |
| 175 | std::accumulate(render[k].begin(), render[k].end(), 0.f, |
| 176 | [](float a, float b) -> float { return a + b * b; })); |
| 177 | } |
| 178 | |
| 179 | // If there is more power in the lower frequencies than the upper frequencies, |
| 180 | // or if the power in upper frequencies is low, do not bound the gain in the |
| 181 | // upper bands. |
| 182 | if (high_band_energies < low_band_energy || |
| 183 | high_band_energies < kSubBlockSize * 10.f * 10.f) { |
| 184 | return 1.f; |
| 185 | } |
| 186 | |
| 187 | // In all other cases, bound the gain for upper frequencies. |
| 188 | RTC_DCHECK_LE(low_band_energy, high_band_energies); |
| 189 | return 0.01f * sqrtf(low_band_energy / high_band_energies); |
| 190 | } |
| 191 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 192 | SuppressionGain::SuppressionGain(Aec3Optimization optimization) |
| 193 | : optimization_(optimization) { |
| 194 | previous_gain_squared_.fill(1.f); |
| 195 | previous_masker_.fill(0.f); |
| 196 | } |
| 197 | |
| 198 | void SuppressionGain::GetGain( |
| 199 | const std::array<float, kFftLengthBy2Plus1>& nearend_power, |
| 200 | const std::array<float, kFftLengthBy2Plus1>& residual_echo_power, |
| 201 | const std::array<float, kFftLengthBy2Plus1>& comfort_noise_power, |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 202 | bool saturated_echo, |
| 203 | const std::vector<std::vector<float>>& render, |
| 204 | size_t num_capture_bands, |
peah | 6d822ad | 2017-04-10 13:52:14 -0700 | [diff] [blame] | 205 | bool force_zero_gain, |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 206 | float* high_bands_gain, |
| 207 | std::array<float, kFftLengthBy2Plus1>* low_band_gain) { |
| 208 | RTC_DCHECK(high_bands_gain); |
| 209 | RTC_DCHECK(low_band_gain); |
| 210 | |
peah | 6d822ad | 2017-04-10 13:52:14 -0700 | [diff] [blame] | 211 | if (force_zero_gain) { |
| 212 | previous_gain_squared_.fill(0.f); |
| 213 | std::copy(comfort_noise_power.begin() + 1, comfort_noise_power.end() - 1, |
| 214 | previous_masker_.begin()); |
| 215 | low_band_gain->fill(0.f); |
| 216 | *high_bands_gain = 0.f; |
| 217 | return; |
| 218 | } |
| 219 | |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 220 | // Choose margin to use. |
| 221 | const float margin = saturated_echo ? 0.001f : 0.01f; |
peah | 5e79b29 | 2017-04-12 01:20:45 -0700 | [diff] [blame] | 222 | ComputeGains(optimization_, nearend_power, residual_echo_power, |
| 223 | comfort_noise_power, margin, &previous_gain_squared_, |
| 224 | &previous_masker_, low_band_gain); |
peah | 86afe9d | 2017-04-06 15:45:32 -0700 | [diff] [blame] | 225 | |
| 226 | if (num_capture_bands > 1) { |
| 227 | // Compute the gain for upper frequencies. |
| 228 | const float min_high_band_gain = |
| 229 | HighFrequencyGainBound(saturated_echo, render); |
| 230 | *high_bands_gain = |
| 231 | *std::min_element(low_band_gain->begin() + 32, low_band_gain->end()); |
| 232 | |
| 233 | *high_bands_gain = std::min(*high_bands_gain, min_high_band_gain); |
| 234 | |
| 235 | } else { |
| 236 | *high_bands_gain = 1.f; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | } // namespace webrtc |