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