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> |
| 20 | |
peah | cf02cf1 | 2017-04-05 14:18:07 -0700 | [diff] [blame^] | 21 | #include "webrtc/base/checks.h" |
| 22 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 23 | namespace webrtc { |
| 24 | namespace { |
| 25 | |
peah | a2376e7 | 2017-02-27 01:15:24 -0800 | [diff] [blame] | 26 | void GainPostProcessing(std::array<float, kFftLengthBy2Plus1>* gain_squared) { |
| 27 | // Limit the low frequency gains to avoid the impact of the high-pass filter |
| 28 | // on the lower-frequency gain influencing the overall achieved gain. |
| 29 | (*gain_squared)[1] = std::min((*gain_squared)[1], (*gain_squared)[2]); |
| 30 | (*gain_squared)[0] = (*gain_squared)[1]; |
| 31 | |
| 32 | // Limit the high frequency gains to avoid the impact of the anti-aliasing |
| 33 | // filter on the upper-frequency gains influencing the overall achieved |
| 34 | // gain. TODO(peah): Update this when new anti-aliasing filters are |
| 35 | // implemented. |
| 36 | constexpr size_t kAntiAliasingImpactLimit = 64 * 0.7f; |
| 37 | std::for_each(gain_squared->begin() + kAntiAliasingImpactLimit, |
| 38 | gain_squared->end(), |
| 39 | [gain_squared, kAntiAliasingImpactLimit](float& a) { |
| 40 | a = std::min(a, (*gain_squared)[kAntiAliasingImpactLimit]); |
| 41 | }); |
| 42 | (*gain_squared)[kFftLengthBy2] = (*gain_squared)[kFftLengthBy2Minus1]; |
| 43 | } |
| 44 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 45 | constexpr int kNumIterations = 2; |
| 46 | constexpr float kEchoMaskingMargin = 1.f / 10.f; |
| 47 | constexpr float kBandMaskingFactor = 1.f / 2.f; |
| 48 | constexpr float kTimeMaskingFactor = 1.f / 10.f; |
| 49 | |
| 50 | } // namespace |
| 51 | |
| 52 | namespace aec3 { |
| 53 | |
| 54 | #if defined(WEBRTC_ARCH_X86_FAMILY) |
| 55 | |
| 56 | // Optimized SSE2 code for the gain computation. |
| 57 | // TODO(peah): Add further optimizations, in particular for the divisions. |
| 58 | void ComputeGains_SSE2( |
| 59 | const std::array<float, kFftLengthBy2Plus1>& nearend_power, |
| 60 | const std::array<float, kFftLengthBy2Plus1>& residual_echo_power, |
| 61 | const std::array<float, kFftLengthBy2Plus1>& comfort_noise_power, |
| 62 | float strong_nearend_margin, |
| 63 | std::array<float, kFftLengthBy2Minus1>* previous_gain_squared, |
| 64 | std::array<float, kFftLengthBy2Minus1>* previous_masker, |
| 65 | std::array<float, kFftLengthBy2Plus1>* gain) { |
| 66 | std::array<float, kFftLengthBy2Minus1> masker; |
| 67 | std::array<float, kFftLengthBy2Minus1> same_band_masker; |
| 68 | std::array<float, kFftLengthBy2Minus1> one_by_residual_echo_power; |
| 69 | std::array<bool, kFftLengthBy2Minus1> strong_nearend; |
| 70 | std::array<float, kFftLengthBy2Plus1> neighboring_bands_masker; |
| 71 | std::array<float, kFftLengthBy2Plus1>* gain_squared = gain; |
| 72 | |
| 73 | // Precompute 1/residual_echo_power. |
| 74 | std::transform(residual_echo_power.begin() + 1, residual_echo_power.end() - 1, |
| 75 | one_by_residual_echo_power.begin(), |
| 76 | [](float a) { return a > 0.f ? 1.f / a : -1.f; }); |
| 77 | |
| 78 | // Precompute indicators for bands with strong nearend. |
| 79 | std::transform( |
| 80 | residual_echo_power.begin() + 1, residual_echo_power.end() - 1, |
| 81 | nearend_power.begin() + 1, strong_nearend.begin(), |
| 82 | [&](float a, float b) { return a <= strong_nearend_margin * b; }); |
| 83 | |
| 84 | // Precompute masker for the same band. |
| 85 | std::transform(comfort_noise_power.begin() + 1, comfort_noise_power.end() - 1, |
| 86 | previous_masker->begin(), same_band_masker.begin(), |
| 87 | [&](float a, float b) { return a + kTimeMaskingFactor * b; }); |
| 88 | |
| 89 | for (int k = 0; k < kNumIterations; ++k) { |
| 90 | if (k == 0) { |
| 91 | // Add masker from the same band. |
| 92 | std::copy(same_band_masker.begin(), same_band_masker.end(), |
| 93 | masker.begin()); |
| 94 | } else { |
| 95 | // Add masker for neighboring bands. |
| 96 | std::transform(nearend_power.begin(), nearend_power.end(), |
| 97 | gain_squared->begin(), neighboring_bands_masker.begin(), |
| 98 | std::multiplies<float>()); |
| 99 | std::transform(neighboring_bands_masker.begin(), |
| 100 | neighboring_bands_masker.end(), |
| 101 | comfort_noise_power.begin(), |
| 102 | neighboring_bands_masker.begin(), std::plus<float>()); |
| 103 | std::transform( |
| 104 | neighboring_bands_masker.begin(), neighboring_bands_masker.end() - 2, |
| 105 | neighboring_bands_masker.begin() + 2, masker.begin(), |
| 106 | [&](float a, float b) { return kBandMaskingFactor * (a + b); }); |
| 107 | |
| 108 | // Add masker from the same band. |
| 109 | std::transform(same_band_masker.begin(), same_band_masker.end(), |
| 110 | masker.begin(), masker.begin(), std::plus<float>()); |
| 111 | } |
| 112 | |
| 113 | // Compute new gain as: |
| 114 | // G2(t,f) = (comfort_noise_power(t,f) + G2(t-1)*nearend_power(t-1)) * |
| 115 | // kTimeMaskingFactor |
| 116 | // * kEchoMaskingMargin / residual_echo_power(t,f). |
| 117 | // or |
| 118 | // G2(t,f) = ((comfort_noise_power(t,f) + G2(t-1) * |
| 119 | // nearend_power(t-1)) * kTimeMaskingFactor + |
| 120 | // (comfort_noise_power(t, f-1) + comfort_noise_power(t, f+1) + |
| 121 | // (G2(t,f-1)*nearend_power(t, f-1) + |
| 122 | // G2(t,f+1)*nearend_power(t, f+1)) * |
| 123 | // kTimeMaskingFactor) * kBandMaskingFactor) |
| 124 | // * kEchoMaskingMargin / residual_echo_power(t,f). |
| 125 | std::transform( |
| 126 | masker.begin(), masker.end(), one_by_residual_echo_power.begin(), |
| 127 | gain_squared->begin() + 1, [&](float a, float b) { |
| 128 | return b >= 0 ? std::min(kEchoMaskingMargin * a * b, 1.f) : 1.f; |
| 129 | }); |
| 130 | |
| 131 | // Limit gain for bands with strong nearend. |
| 132 | std::transform(gain_squared->begin() + 1, gain_squared->end() - 1, |
| 133 | strong_nearend.begin(), gain_squared->begin() + 1, |
| 134 | [](float a, bool b) { return b ? 1.f : a; }); |
| 135 | |
| 136 | // Limit the allowed gain update over time. |
| 137 | std::transform(gain_squared->begin() + 1, gain_squared->end() - 1, |
| 138 | previous_gain_squared->begin(), gain_squared->begin() + 1, |
| 139 | [](float a, float b) { |
| 140 | return b < 0.0001f ? std::min(a, 0.0001f) |
| 141 | : std::min(a, b * 2.f); |
| 142 | }); |
| 143 | |
peah | a2376e7 | 2017-02-27 01:15:24 -0800 | [diff] [blame] | 144 | // Process the gains to avoid artefacts caused by gain realization in the |
| 145 | // filterbank and impact of external pre-processing of the signal. |
| 146 | GainPostProcessing(gain_squared); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | std::copy(gain_squared->begin() + 1, gain_squared->end() - 1, |
| 150 | previous_gain_squared->begin()); |
| 151 | |
| 152 | std::transform(gain_squared->begin() + 1, gain_squared->end() - 1, |
| 153 | nearend_power.begin() + 1, previous_masker->begin(), |
| 154 | std::multiplies<float>()); |
| 155 | std::transform(previous_masker->begin(), previous_masker->end(), |
| 156 | comfort_noise_power.begin() + 1, previous_masker->begin(), |
| 157 | std::plus<float>()); |
| 158 | |
| 159 | for (size_t k = 0; k < kFftLengthBy2; k += 4) { |
| 160 | __m128 g = _mm_loadu_ps(&(*gain_squared)[k]); |
| 161 | g = _mm_sqrt_ps(g); |
| 162 | _mm_storeu_ps(&(*gain)[k], g); |
| 163 | } |
| 164 | |
| 165 | (*gain)[kFftLengthBy2] = sqrtf((*gain)[kFftLengthBy2]); |
| 166 | } |
| 167 | |
| 168 | #endif |
| 169 | |
| 170 | void ComputeGains( |
| 171 | const std::array<float, kFftLengthBy2Plus1>& nearend_power, |
| 172 | const std::array<float, kFftLengthBy2Plus1>& residual_echo_power, |
| 173 | const std::array<float, kFftLengthBy2Plus1>& comfort_noise_power, |
| 174 | float strong_nearend_margin, |
| 175 | std::array<float, kFftLengthBy2Minus1>* previous_gain_squared, |
| 176 | std::array<float, kFftLengthBy2Minus1>* previous_masker, |
| 177 | std::array<float, kFftLengthBy2Plus1>* gain) { |
| 178 | std::array<float, kFftLengthBy2Minus1> masker; |
| 179 | std::array<float, kFftLengthBy2Minus1> same_band_masker; |
| 180 | std::array<float, kFftLengthBy2Minus1> one_by_residual_echo_power; |
| 181 | std::array<bool, kFftLengthBy2Minus1> strong_nearend; |
| 182 | std::array<float, kFftLengthBy2Plus1> neighboring_bands_masker; |
| 183 | std::array<float, kFftLengthBy2Plus1>* gain_squared = gain; |
| 184 | |
| 185 | // Precompute 1/residual_echo_power. |
| 186 | std::transform(residual_echo_power.begin() + 1, residual_echo_power.end() - 1, |
| 187 | one_by_residual_echo_power.begin(), |
| 188 | [](float a) { return a > 0.f ? 1.f / a : -1.f; }); |
| 189 | |
| 190 | // Precompute indicators for bands with strong nearend. |
| 191 | std::transform( |
| 192 | residual_echo_power.begin() + 1, residual_echo_power.end() - 1, |
| 193 | nearend_power.begin() + 1, strong_nearend.begin(), |
| 194 | [&](float a, float b) { return a <= strong_nearend_margin * b; }); |
| 195 | |
| 196 | // Precompute masker for the same band. |
| 197 | std::transform(comfort_noise_power.begin() + 1, comfort_noise_power.end() - 1, |
| 198 | previous_masker->begin(), same_band_masker.begin(), |
| 199 | [&](float a, float b) { return a + kTimeMaskingFactor * b; }); |
| 200 | |
| 201 | for (int k = 0; k < kNumIterations; ++k) { |
| 202 | if (k == 0) { |
| 203 | // Add masker from the same band. |
| 204 | std::copy(same_band_masker.begin(), same_band_masker.end(), |
| 205 | masker.begin()); |
| 206 | } else { |
| 207 | // Add masker for neightboring bands. |
| 208 | std::transform(nearend_power.begin(), nearend_power.end(), |
| 209 | gain_squared->begin(), neighboring_bands_masker.begin(), |
| 210 | std::multiplies<float>()); |
| 211 | std::transform(neighboring_bands_masker.begin(), |
| 212 | neighboring_bands_masker.end(), |
| 213 | comfort_noise_power.begin(), |
| 214 | neighboring_bands_masker.begin(), std::plus<float>()); |
| 215 | std::transform( |
| 216 | neighboring_bands_masker.begin(), neighboring_bands_masker.end() - 2, |
| 217 | neighboring_bands_masker.begin() + 2, masker.begin(), |
| 218 | [&](float a, float b) { return kBandMaskingFactor * (a + b); }); |
| 219 | |
| 220 | // Add masker from the same band. |
| 221 | std::transform(same_band_masker.begin(), same_band_masker.end(), |
| 222 | masker.begin(), masker.begin(), std::plus<float>()); |
| 223 | } |
| 224 | |
| 225 | // Compute new gain as: |
| 226 | // G2(t,f) = (comfort_noise_power(t,f) + G2(t-1)*nearend_power(t-1)) * |
| 227 | // kTimeMaskingFactor |
| 228 | // * kEchoMaskingMargin / residual_echo_power(t,f). |
| 229 | // or |
| 230 | // G2(t,f) = ((comfort_noise_power(t,f) + G2(t-1) * |
| 231 | // nearend_power(t-1)) * kTimeMaskingFactor + |
| 232 | // (comfort_noise_power(t, f-1) + comfort_noise_power(t, f+1) + |
| 233 | // (G2(t,f-1)*nearend_power(t, f-1) + |
| 234 | // G2(t,f+1)*nearend_power(t, f+1)) * |
| 235 | // kTimeMaskingFactor) * kBandMaskingFactor) |
| 236 | // * kEchoMaskingMargin / residual_echo_power(t,f). |
| 237 | std::transform( |
| 238 | masker.begin(), masker.end(), one_by_residual_echo_power.begin(), |
| 239 | gain_squared->begin() + 1, [&](float a, float b) { |
| 240 | return b >= 0 ? std::min(kEchoMaskingMargin * a * b, 1.f) : 1.f; |
| 241 | }); |
| 242 | |
| 243 | // Limit gain for bands with strong nearend. |
| 244 | std::transform(gain_squared->begin() + 1, gain_squared->end() - 1, |
| 245 | strong_nearend.begin(), gain_squared->begin() + 1, |
| 246 | [](float a, bool b) { return b ? 1.f : a; }); |
| 247 | |
| 248 | // Limit the allowed gain update over time. |
| 249 | std::transform(gain_squared->begin() + 1, gain_squared->end() - 1, |
| 250 | previous_gain_squared->begin(), gain_squared->begin() + 1, |
| 251 | [](float a, float b) { |
| 252 | return b < 0.0001f ? std::min(a, 0.0001f) |
| 253 | : std::min(a, b * 2.f); |
| 254 | }); |
| 255 | |
peah | a2376e7 | 2017-02-27 01:15:24 -0800 | [diff] [blame] | 256 | // Process the gains to avoid artefacts caused by gain realization in the |
| 257 | // filterbank and impact of external pre-processing of the signal. |
| 258 | GainPostProcessing(gain_squared); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | std::copy(gain_squared->begin() + 1, gain_squared->end() - 1, |
| 262 | previous_gain_squared->begin()); |
| 263 | |
| 264 | std::transform(gain_squared->begin() + 1, gain_squared->end() - 1, |
| 265 | nearend_power.begin() + 1, previous_masker->begin(), |
| 266 | std::multiplies<float>()); |
| 267 | std::transform(previous_masker->begin(), previous_masker->end(), |
| 268 | comfort_noise_power.begin() + 1, previous_masker->begin(), |
| 269 | std::plus<float>()); |
| 270 | |
| 271 | std::transform(gain_squared->begin(), gain_squared->end(), gain->begin(), |
| 272 | [](float a) { return sqrtf(a); }); |
| 273 | } |
| 274 | |
| 275 | } // namespace aec3 |
| 276 | |
| 277 | SuppressionGain::SuppressionGain(Aec3Optimization optimization) |
| 278 | : optimization_(optimization) { |
| 279 | previous_gain_squared_.fill(1.f); |
| 280 | previous_masker_.fill(0.f); |
| 281 | } |
| 282 | |
| 283 | void SuppressionGain::GetGain( |
| 284 | const std::array<float, kFftLengthBy2Plus1>& nearend_power, |
| 285 | const std::array<float, kFftLengthBy2Plus1>& residual_echo_power, |
| 286 | const std::array<float, kFftLengthBy2Plus1>& comfort_noise_power, |
| 287 | float strong_nearend_margin, |
| 288 | std::array<float, kFftLengthBy2Plus1>* gain) { |
| 289 | RTC_DCHECK(gain); |
| 290 | switch (optimization_) { |
| 291 | #if defined(WEBRTC_ARCH_X86_FAMILY) |
| 292 | case Aec3Optimization::kSse2: |
| 293 | aec3::ComputeGains_SSE2(nearend_power, residual_echo_power, |
| 294 | comfort_noise_power, strong_nearend_margin, |
| 295 | &previous_gain_squared_, &previous_masker_, gain); |
| 296 | break; |
| 297 | #endif |
| 298 | default: |
| 299 | aec3::ComputeGains(nearend_power, residual_echo_power, |
| 300 | comfort_noise_power, strong_nearend_margin, |
| 301 | &previous_gain_squared_, &previous_masker_, gain); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | } // namespace webrtc |