blob: 86af60f316fa867e6f2276c8aac7311fd867fa44 [file] [log] [blame]
peah522d71b2017-02-23 05:16:26 -08001/*
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>
peah86afe9d2017-04-06 15:45:32 -070020#include <numeric>
peah522d71b2017-02-23 05:16:26 -080021
peahcf02cf12017-04-05 14:18:07 -070022#include "webrtc/base/checks.h"
peah5e79b292017-04-12 01:20:45 -070023#include "webrtc/modules/audio_processing/aec3/vector_math.h"
peahcf02cf12017-04-05 14:18:07 -070024
peah522d71b2017-02-23 05:16:26 -080025namespace webrtc {
26namespace {
27
peaha2376e72017-02-27 01:15:24 -080028void 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.
peah86afe9d2017-04-06 15:45:32 -070038 constexpr size_t kAntiAliasingImpactLimit = (64 * 2000) / 8000;
peaha2376e72017-02-27 01:15:24 -080039 std::for_each(gain_squared->begin() + kAntiAliasingImpactLimit,
peah86afe9d2017-04-06 15:45:32 -070040 gain_squared->end() - 1,
peaha2376e72017-02-27 01:15:24 -080041 [gain_squared, kAntiAliasingImpactLimit](float& a) {
42 a = std::min(a, (*gain_squared)[kAntiAliasingImpactLimit]);
43 });
44 (*gain_squared)[kFftLengthBy2] = (*gain_squared)[kFftLengthBy2Minus1];
45}
46
peah522d71b2017-02-23 05:16:26 -080047constexpr int kNumIterations = 2;
peah86afe9d2017-04-06 15:45:32 -070048constexpr float kEchoMaskingMargin = 1.f / 20.f;
49constexpr float kBandMaskingFactor = 1.f / 10.f;
peah522d71b2017-02-23 05:16:26 -080050constexpr float kTimeMaskingFactor = 1.f / 10.f;
51
peah522d71b2017-02-23 05:16:26 -080052// TODO(peah): Add further optimizations, in particular for the divisions.
peah5e79b292017-04-12 01:20:45 -070053void ComputeGains(
54 Aec3Optimization optimization,
peah522d71b2017-02-23 05:16:26 -080055 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;
peah5e79b292017-04-12 01:20:45 -070068 aec3::VectorMath math(optimization);
peah522d71b2017-02-23 05:16:26 -080069
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.
peah5e79b292017-04-12 01:20:45 -070093 math.Multiply(nearend_power, *gain_squared, neighboring_bands_masker);
94 math.Accumulate(comfort_noise_power, neighboring_bands_masker);
peah522d71b2017-02-23 05:16:26 -080095 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.
peah5e79b292017-04-12 01:20:45 -0700101 math.Accumulate(same_band_masker, masker);
peah522d71b2017-02-23 05:16:26 -0800102 }
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) {
peah86afe9d2017-04-06 15:45:32 -0700131 return b < 0.001f ? std::min(a, 0.001f)
132 : std::min(a, b * 2.f);
peah522d71b2017-02-23 05:16:26 -0800133 });
134
peaha2376e72017-02-27 01:15:24 -0800135 // 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);
peah522d71b2017-02-23 05:16:26 -0800138 }
139
140 std::copy(gain_squared->begin() + 1, gain_squared->end() - 1,
141 previous_gain_squared->begin());
142
peah5e79b292017-04-12 01:20:45 -0700143 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);
peah522d71b2017-02-23 05:16:26 -0800151}
152
peah5e79b292017-04-12 01:20:45 -0700153} // namespace
peah522d71b2017-02-23 05:16:26 -0800154
peah86afe9d2017-04-06 15:45:32 -0700155// Computes an upper bound on the gain to apply for high frequencies.
156float 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
peah522d71b2017-02-23 05:16:26 -0800192SuppressionGain::SuppressionGain(Aec3Optimization optimization)
193 : optimization_(optimization) {
194 previous_gain_squared_.fill(1.f);
195 previous_masker_.fill(0.f);
196}
197
198void 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,
peah86afe9d2017-04-06 15:45:32 -0700202 bool saturated_echo,
203 const std::vector<std::vector<float>>& render,
204 size_t num_capture_bands,
peah6d822ad2017-04-10 13:52:14 -0700205 bool force_zero_gain,
peah86afe9d2017-04-06 15:45:32 -0700206 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
peah6d822ad2017-04-10 13:52:14 -0700211 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
peah86afe9d2017-04-06 15:45:32 -0700220 // Choose margin to use.
221 const float margin = saturated_echo ? 0.001f : 0.01f;
peah5e79b292017-04-12 01:20:45 -0700222 ComputeGains(optimization_, nearend_power, residual_echo_power,
223 comfort_noise_power, margin, &previous_gain_squared_,
224 &previous_masker_, low_band_gain);
peah86afe9d2017-04-06 15:45:32 -0700225
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;
peah522d71b2017-02-23 05:16:26 -0800237 }
238}
239
240} // namespace webrtc