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