xref: /aosp_15_r20/external/webrtc/modules/audio_processing/aec3/adaptive_fir_filter.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 "modules/audio_processing/aec3/adaptive_fir_filter.h"
12 
13 // Defines WEBRTC_ARCH_X86_FAMILY, used below.
14 #include "rtc_base/system/arch.h"
15 
16 #if defined(WEBRTC_HAS_NEON)
17 #include <arm_neon.h>
18 #endif
19 #if defined(WEBRTC_ARCH_X86_FAMILY)
20 #include <emmintrin.h>
21 #endif
22 #include <math.h>
23 
24 #include <algorithm>
25 #include <functional>
26 
27 #include "modules/audio_processing/aec3/fft_data.h"
28 #include "rtc_base/checks.h"
29 
30 namespace webrtc {
31 
32 namespace aec3 {
33 
34 // Computes and stores the frequency response of the filter.
ComputeFrequencyResponse(size_t num_partitions,const std::vector<std::vector<FftData>> & H,std::vector<std::array<float,kFftLengthBy2Plus1>> * H2)35 void ComputeFrequencyResponse(
36     size_t num_partitions,
37     const std::vector<std::vector<FftData>>& H,
38     std::vector<std::array<float, kFftLengthBy2Plus1>>* H2) {
39   for (auto& H2_ch : *H2) {
40     H2_ch.fill(0.f);
41   }
42 
43   const size_t num_render_channels = H[0].size();
44   RTC_DCHECK_EQ(H.size(), H2->capacity());
45   for (size_t p = 0; p < num_partitions; ++p) {
46     RTC_DCHECK_EQ(kFftLengthBy2Plus1, (*H2)[p].size());
47     for (size_t ch = 0; ch < num_render_channels; ++ch) {
48       for (size_t j = 0; j < kFftLengthBy2Plus1; ++j) {
49         float tmp =
50             H[p][ch].re[j] * H[p][ch].re[j] + H[p][ch].im[j] * H[p][ch].im[j];
51         (*H2)[p][j] = std::max((*H2)[p][j], tmp);
52       }
53     }
54   }
55 }
56 
57 #if defined(WEBRTC_HAS_NEON)
58 // Computes and stores the frequency response of the filter.
ComputeFrequencyResponse_Neon(size_t num_partitions,const std::vector<std::vector<FftData>> & H,std::vector<std::array<float,kFftLengthBy2Plus1>> * H2)59 void ComputeFrequencyResponse_Neon(
60     size_t num_partitions,
61     const std::vector<std::vector<FftData>>& H,
62     std::vector<std::array<float, kFftLengthBy2Plus1>>* H2) {
63   for (auto& H2_ch : *H2) {
64     H2_ch.fill(0.f);
65   }
66 
67   const size_t num_render_channels = H[0].size();
68   RTC_DCHECK_EQ(H.size(), H2->capacity());
69   for (size_t p = 0; p < num_partitions; ++p) {
70     RTC_DCHECK_EQ(kFftLengthBy2Plus1, (*H2)[p].size());
71     auto& H2_p = (*H2)[p];
72     for (size_t ch = 0; ch < num_render_channels; ++ch) {
73       const FftData& H_p_ch = H[p][ch];
74       for (size_t j = 0; j < kFftLengthBy2; j += 4) {
75         const float32x4_t re = vld1q_f32(&H_p_ch.re[j]);
76         const float32x4_t im = vld1q_f32(&H_p_ch.im[j]);
77         float32x4_t H2_new = vmulq_f32(re, re);
78         H2_new = vmlaq_f32(H2_new, im, im);
79         float32x4_t H2_p_j = vld1q_f32(&H2_p[j]);
80         H2_p_j = vmaxq_f32(H2_p_j, H2_new);
81         vst1q_f32(&H2_p[j], H2_p_j);
82       }
83       float H2_new = H_p_ch.re[kFftLengthBy2] * H_p_ch.re[kFftLengthBy2] +
84                      H_p_ch.im[kFftLengthBy2] * H_p_ch.im[kFftLengthBy2];
85       H2_p[kFftLengthBy2] = std::max(H2_p[kFftLengthBy2], H2_new);
86     }
87   }
88 }
89 #endif
90 
91 #if defined(WEBRTC_ARCH_X86_FAMILY)
92 // Computes and stores the frequency response of the filter.
ComputeFrequencyResponse_Sse2(size_t num_partitions,const std::vector<std::vector<FftData>> & H,std::vector<std::array<float,kFftLengthBy2Plus1>> * H2)93 void ComputeFrequencyResponse_Sse2(
94     size_t num_partitions,
95     const std::vector<std::vector<FftData>>& H,
96     std::vector<std::array<float, kFftLengthBy2Plus1>>* H2) {
97   for (auto& H2_ch : *H2) {
98     H2_ch.fill(0.f);
99   }
100 
101   const size_t num_render_channels = H[0].size();
102   RTC_DCHECK_EQ(H.size(), H2->capacity());
103   // constexpr __mmmask8 kMaxMask = static_cast<__mmmask8>(256u);
104   for (size_t p = 0; p < num_partitions; ++p) {
105     RTC_DCHECK_EQ(kFftLengthBy2Plus1, (*H2)[p].size());
106     auto& H2_p = (*H2)[p];
107     for (size_t ch = 0; ch < num_render_channels; ++ch) {
108       const FftData& H_p_ch = H[p][ch];
109       for (size_t j = 0; j < kFftLengthBy2; j += 4) {
110         const __m128 re = _mm_loadu_ps(&H_p_ch.re[j]);
111         const __m128 re2 = _mm_mul_ps(re, re);
112         const __m128 im = _mm_loadu_ps(&H_p_ch.im[j]);
113         const __m128 im2 = _mm_mul_ps(im, im);
114         const __m128 H2_new = _mm_add_ps(re2, im2);
115         __m128 H2_k_j = _mm_loadu_ps(&H2_p[j]);
116         H2_k_j = _mm_max_ps(H2_k_j, H2_new);
117         _mm_storeu_ps(&H2_p[j], H2_k_j);
118       }
119       float H2_new = H_p_ch.re[kFftLengthBy2] * H_p_ch.re[kFftLengthBy2] +
120                      H_p_ch.im[kFftLengthBy2] * H_p_ch.im[kFftLengthBy2];
121       H2_p[kFftLengthBy2] = std::max(H2_p[kFftLengthBy2], H2_new);
122     }
123   }
124 }
125 #endif
126 
127 // Adapts the filter partitions as H(t+1)=H(t)+G(t)*conj(X(t)).
AdaptPartitions(const RenderBuffer & render_buffer,const FftData & G,size_t num_partitions,std::vector<std::vector<FftData>> * H)128 void AdaptPartitions(const RenderBuffer& render_buffer,
129                      const FftData& G,
130                      size_t num_partitions,
131                      std::vector<std::vector<FftData>>* H) {
132   rtc::ArrayView<const std::vector<FftData>> render_buffer_data =
133       render_buffer.GetFftBuffer();
134   size_t index = render_buffer.Position();
135   const size_t num_render_channels = render_buffer_data[index].size();
136   for (size_t p = 0; p < num_partitions; ++p) {
137     for (size_t ch = 0; ch < num_render_channels; ++ch) {
138       const FftData& X_p_ch = render_buffer_data[index][ch];
139       FftData& H_p_ch = (*H)[p][ch];
140       for (size_t k = 0; k < kFftLengthBy2Plus1; ++k) {
141         H_p_ch.re[k] += X_p_ch.re[k] * G.re[k] + X_p_ch.im[k] * G.im[k];
142         H_p_ch.im[k] += X_p_ch.re[k] * G.im[k] - X_p_ch.im[k] * G.re[k];
143       }
144     }
145     index = index < (render_buffer_data.size() - 1) ? index + 1 : 0;
146   }
147 }
148 
149 #if defined(WEBRTC_HAS_NEON)
150 // Adapts the filter partitions. (Neon variant)
AdaptPartitions_Neon(const RenderBuffer & render_buffer,const FftData & G,size_t num_partitions,std::vector<std::vector<FftData>> * H)151 void AdaptPartitions_Neon(const RenderBuffer& render_buffer,
152                           const FftData& G,
153                           size_t num_partitions,
154                           std::vector<std::vector<FftData>>* H) {
155   rtc::ArrayView<const std::vector<FftData>> render_buffer_data =
156       render_buffer.GetFftBuffer();
157   const size_t num_render_channels = render_buffer_data[0].size();
158   const size_t lim1 = std::min(
159       render_buffer_data.size() - render_buffer.Position(), num_partitions);
160   const size_t lim2 = num_partitions;
161   constexpr size_t kNumFourBinBands = kFftLengthBy2 / 4;
162 
163   size_t X_partition = render_buffer.Position();
164   size_t limit = lim1;
165   size_t p = 0;
166   do {
167     for (; p < limit; ++p, ++X_partition) {
168       for (size_t ch = 0; ch < num_render_channels; ++ch) {
169         FftData& H_p_ch = (*H)[p][ch];
170         const FftData& X = render_buffer_data[X_partition][ch];
171         for (size_t k = 0, n = 0; n < kNumFourBinBands; ++n, k += 4) {
172           const float32x4_t G_re = vld1q_f32(&G.re[k]);
173           const float32x4_t G_im = vld1q_f32(&G.im[k]);
174           const float32x4_t X_re = vld1q_f32(&X.re[k]);
175           const float32x4_t X_im = vld1q_f32(&X.im[k]);
176           const float32x4_t H_re = vld1q_f32(&H_p_ch.re[k]);
177           const float32x4_t H_im = vld1q_f32(&H_p_ch.im[k]);
178           const float32x4_t a = vmulq_f32(X_re, G_re);
179           const float32x4_t e = vmlaq_f32(a, X_im, G_im);
180           const float32x4_t c = vmulq_f32(X_re, G_im);
181           const float32x4_t f = vmlsq_f32(c, X_im, G_re);
182           const float32x4_t g = vaddq_f32(H_re, e);
183           const float32x4_t h = vaddq_f32(H_im, f);
184           vst1q_f32(&H_p_ch.re[k], g);
185           vst1q_f32(&H_p_ch.im[k], h);
186         }
187       }
188     }
189 
190     X_partition = 0;
191     limit = lim2;
192   } while (p < lim2);
193 
194   X_partition = render_buffer.Position();
195   limit = lim1;
196   p = 0;
197   do {
198     for (; p < limit; ++p, ++X_partition) {
199       for (size_t ch = 0; ch < num_render_channels; ++ch) {
200         FftData& H_p_ch = (*H)[p][ch];
201         const FftData& X = render_buffer_data[X_partition][ch];
202 
203         H_p_ch.re[kFftLengthBy2] += X.re[kFftLengthBy2] * G.re[kFftLengthBy2] +
204                                     X.im[kFftLengthBy2] * G.im[kFftLengthBy2];
205         H_p_ch.im[kFftLengthBy2] += X.re[kFftLengthBy2] * G.im[kFftLengthBy2] -
206                                     X.im[kFftLengthBy2] * G.re[kFftLengthBy2];
207       }
208     }
209     X_partition = 0;
210     limit = lim2;
211   } while (p < lim2);
212 }
213 #endif
214 
215 #if defined(WEBRTC_ARCH_X86_FAMILY)
216 // Adapts the filter partitions. (SSE2 variant)
AdaptPartitions_Sse2(const RenderBuffer & render_buffer,const FftData & G,size_t num_partitions,std::vector<std::vector<FftData>> * H)217 void AdaptPartitions_Sse2(const RenderBuffer& render_buffer,
218                           const FftData& G,
219                           size_t num_partitions,
220                           std::vector<std::vector<FftData>>* H) {
221   rtc::ArrayView<const std::vector<FftData>> render_buffer_data =
222       render_buffer.GetFftBuffer();
223   const size_t num_render_channels = render_buffer_data[0].size();
224   const size_t lim1 = std::min(
225       render_buffer_data.size() - render_buffer.Position(), num_partitions);
226   const size_t lim2 = num_partitions;
227   constexpr size_t kNumFourBinBands = kFftLengthBy2 / 4;
228 
229   size_t X_partition = render_buffer.Position();
230   size_t limit = lim1;
231   size_t p = 0;
232   do {
233     for (; p < limit; ++p, ++X_partition) {
234       for (size_t ch = 0; ch < num_render_channels; ++ch) {
235         FftData& H_p_ch = (*H)[p][ch];
236         const FftData& X = render_buffer_data[X_partition][ch];
237 
238         for (size_t k = 0, n = 0; n < kNumFourBinBands; ++n, k += 4) {
239           const __m128 G_re = _mm_loadu_ps(&G.re[k]);
240           const __m128 G_im = _mm_loadu_ps(&G.im[k]);
241           const __m128 X_re = _mm_loadu_ps(&X.re[k]);
242           const __m128 X_im = _mm_loadu_ps(&X.im[k]);
243           const __m128 H_re = _mm_loadu_ps(&H_p_ch.re[k]);
244           const __m128 H_im = _mm_loadu_ps(&H_p_ch.im[k]);
245           const __m128 a = _mm_mul_ps(X_re, G_re);
246           const __m128 b = _mm_mul_ps(X_im, G_im);
247           const __m128 c = _mm_mul_ps(X_re, G_im);
248           const __m128 d = _mm_mul_ps(X_im, G_re);
249           const __m128 e = _mm_add_ps(a, b);
250           const __m128 f = _mm_sub_ps(c, d);
251           const __m128 g = _mm_add_ps(H_re, e);
252           const __m128 h = _mm_add_ps(H_im, f);
253           _mm_storeu_ps(&H_p_ch.re[k], g);
254           _mm_storeu_ps(&H_p_ch.im[k], h);
255         }
256       }
257     }
258     X_partition = 0;
259     limit = lim2;
260   } while (p < lim2);
261 
262   X_partition = render_buffer.Position();
263   limit = lim1;
264   p = 0;
265   do {
266     for (; p < limit; ++p, ++X_partition) {
267       for (size_t ch = 0; ch < num_render_channels; ++ch) {
268         FftData& H_p_ch = (*H)[p][ch];
269         const FftData& X = render_buffer_data[X_partition][ch];
270 
271         H_p_ch.re[kFftLengthBy2] += X.re[kFftLengthBy2] * G.re[kFftLengthBy2] +
272                                     X.im[kFftLengthBy2] * G.im[kFftLengthBy2];
273         H_p_ch.im[kFftLengthBy2] += X.re[kFftLengthBy2] * G.im[kFftLengthBy2] -
274                                     X.im[kFftLengthBy2] * G.re[kFftLengthBy2];
275       }
276     }
277 
278     X_partition = 0;
279     limit = lim2;
280   } while (p < lim2);
281 }
282 #endif
283 
284 // Produces the filter output.
ApplyFilter(const RenderBuffer & render_buffer,size_t num_partitions,const std::vector<std::vector<FftData>> & H,FftData * S)285 void ApplyFilter(const RenderBuffer& render_buffer,
286                  size_t num_partitions,
287                  const std::vector<std::vector<FftData>>& H,
288                  FftData* S) {
289   S->re.fill(0.f);
290   S->im.fill(0.f);
291 
292   rtc::ArrayView<const std::vector<FftData>> render_buffer_data =
293       render_buffer.GetFftBuffer();
294   size_t index = render_buffer.Position();
295   const size_t num_render_channels = render_buffer_data[index].size();
296   for (size_t p = 0; p < num_partitions; ++p) {
297     RTC_DCHECK_EQ(num_render_channels, H[p].size());
298     for (size_t ch = 0; ch < num_render_channels; ++ch) {
299       const FftData& X_p_ch = render_buffer_data[index][ch];
300       const FftData& H_p_ch = H[p][ch];
301       for (size_t k = 0; k < kFftLengthBy2Plus1; ++k) {
302         S->re[k] += X_p_ch.re[k] * H_p_ch.re[k] - X_p_ch.im[k] * H_p_ch.im[k];
303         S->im[k] += X_p_ch.re[k] * H_p_ch.im[k] + X_p_ch.im[k] * H_p_ch.re[k];
304       }
305     }
306     index = index < (render_buffer_data.size() - 1) ? index + 1 : 0;
307   }
308 }
309 
310 #if defined(WEBRTC_HAS_NEON)
311 // Produces the filter output (Neon variant).
ApplyFilter_Neon(const RenderBuffer & render_buffer,size_t num_partitions,const std::vector<std::vector<FftData>> & H,FftData * S)312 void ApplyFilter_Neon(const RenderBuffer& render_buffer,
313                       size_t num_partitions,
314                       const std::vector<std::vector<FftData>>& H,
315                       FftData* S) {
316   // const RenderBuffer& render_buffer,
317   //                     rtc::ArrayView<const FftData> H,
318   //                     FftData* S) {
319   RTC_DCHECK_GE(H.size(), H.size() - 1);
320   S->Clear();
321 
322   rtc::ArrayView<const std::vector<FftData>> render_buffer_data =
323       render_buffer.GetFftBuffer();
324   const size_t num_render_channels = render_buffer_data[0].size();
325   const size_t lim1 = std::min(
326       render_buffer_data.size() - render_buffer.Position(), num_partitions);
327   const size_t lim2 = num_partitions;
328   constexpr size_t kNumFourBinBands = kFftLengthBy2 / 4;
329 
330   size_t X_partition = render_buffer.Position();
331   size_t p = 0;
332   size_t limit = lim1;
333   do {
334     for (; p < limit; ++p, ++X_partition) {
335       for (size_t ch = 0; ch < num_render_channels; ++ch) {
336         const FftData& H_p_ch = H[p][ch];
337         const FftData& X = render_buffer_data[X_partition][ch];
338         for (size_t k = 0, n = 0; n < kNumFourBinBands; ++n, k += 4) {
339           const float32x4_t X_re = vld1q_f32(&X.re[k]);
340           const float32x4_t X_im = vld1q_f32(&X.im[k]);
341           const float32x4_t H_re = vld1q_f32(&H_p_ch.re[k]);
342           const float32x4_t H_im = vld1q_f32(&H_p_ch.im[k]);
343           const float32x4_t S_re = vld1q_f32(&S->re[k]);
344           const float32x4_t S_im = vld1q_f32(&S->im[k]);
345           const float32x4_t a = vmulq_f32(X_re, H_re);
346           const float32x4_t e = vmlsq_f32(a, X_im, H_im);
347           const float32x4_t c = vmulq_f32(X_re, H_im);
348           const float32x4_t f = vmlaq_f32(c, X_im, H_re);
349           const float32x4_t g = vaddq_f32(S_re, e);
350           const float32x4_t h = vaddq_f32(S_im, f);
351           vst1q_f32(&S->re[k], g);
352           vst1q_f32(&S->im[k], h);
353         }
354       }
355     }
356     limit = lim2;
357     X_partition = 0;
358   } while (p < lim2);
359 
360   X_partition = render_buffer.Position();
361   p = 0;
362   limit = lim1;
363   do {
364     for (; p < limit; ++p, ++X_partition) {
365       for (size_t ch = 0; ch < num_render_channels; ++ch) {
366         const FftData& H_p_ch = H[p][ch];
367         const FftData& X = render_buffer_data[X_partition][ch];
368         S->re[kFftLengthBy2] += X.re[kFftLengthBy2] * H_p_ch.re[kFftLengthBy2] -
369                                 X.im[kFftLengthBy2] * H_p_ch.im[kFftLengthBy2];
370         S->im[kFftLengthBy2] += X.re[kFftLengthBy2] * H_p_ch.im[kFftLengthBy2] +
371                                 X.im[kFftLengthBy2] * H_p_ch.re[kFftLengthBy2];
372       }
373     }
374     limit = lim2;
375     X_partition = 0;
376   } while (p < lim2);
377 }
378 #endif
379 
380 #if defined(WEBRTC_ARCH_X86_FAMILY)
381 // Produces the filter output (SSE2 variant).
ApplyFilter_Sse2(const RenderBuffer & render_buffer,size_t num_partitions,const std::vector<std::vector<FftData>> & H,FftData * S)382 void ApplyFilter_Sse2(const RenderBuffer& render_buffer,
383                       size_t num_partitions,
384                       const std::vector<std::vector<FftData>>& H,
385                       FftData* S) {
386   // const RenderBuffer& render_buffer,
387   //                     rtc::ArrayView<const FftData> H,
388   //                     FftData* S) {
389   RTC_DCHECK_GE(H.size(), H.size() - 1);
390   S->re.fill(0.f);
391   S->im.fill(0.f);
392 
393   rtc::ArrayView<const std::vector<FftData>> render_buffer_data =
394       render_buffer.GetFftBuffer();
395   const size_t num_render_channels = render_buffer_data[0].size();
396   const size_t lim1 = std::min(
397       render_buffer_data.size() - render_buffer.Position(), num_partitions);
398   const size_t lim2 = num_partitions;
399   constexpr size_t kNumFourBinBands = kFftLengthBy2 / 4;
400 
401   size_t X_partition = render_buffer.Position();
402   size_t p = 0;
403   size_t limit = lim1;
404   do {
405     for (; p < limit; ++p, ++X_partition) {
406       for (size_t ch = 0; ch < num_render_channels; ++ch) {
407         const FftData& H_p_ch = H[p][ch];
408         const FftData& X = render_buffer_data[X_partition][ch];
409         for (size_t k = 0, n = 0; n < kNumFourBinBands; ++n, k += 4) {
410           const __m128 X_re = _mm_loadu_ps(&X.re[k]);
411           const __m128 X_im = _mm_loadu_ps(&X.im[k]);
412           const __m128 H_re = _mm_loadu_ps(&H_p_ch.re[k]);
413           const __m128 H_im = _mm_loadu_ps(&H_p_ch.im[k]);
414           const __m128 S_re = _mm_loadu_ps(&S->re[k]);
415           const __m128 S_im = _mm_loadu_ps(&S->im[k]);
416           const __m128 a = _mm_mul_ps(X_re, H_re);
417           const __m128 b = _mm_mul_ps(X_im, H_im);
418           const __m128 c = _mm_mul_ps(X_re, H_im);
419           const __m128 d = _mm_mul_ps(X_im, H_re);
420           const __m128 e = _mm_sub_ps(a, b);
421           const __m128 f = _mm_add_ps(c, d);
422           const __m128 g = _mm_add_ps(S_re, e);
423           const __m128 h = _mm_add_ps(S_im, f);
424           _mm_storeu_ps(&S->re[k], g);
425           _mm_storeu_ps(&S->im[k], h);
426         }
427       }
428     }
429     limit = lim2;
430     X_partition = 0;
431   } while (p < lim2);
432 
433   X_partition = render_buffer.Position();
434   p = 0;
435   limit = lim1;
436   do {
437     for (; p < limit; ++p, ++X_partition) {
438       for (size_t ch = 0; ch < num_render_channels; ++ch) {
439         const FftData& H_p_ch = H[p][ch];
440         const FftData& X = render_buffer_data[X_partition][ch];
441         S->re[kFftLengthBy2] += X.re[kFftLengthBy2] * H_p_ch.re[kFftLengthBy2] -
442                                 X.im[kFftLengthBy2] * H_p_ch.im[kFftLengthBy2];
443         S->im[kFftLengthBy2] += X.re[kFftLengthBy2] * H_p_ch.im[kFftLengthBy2] +
444                                 X.im[kFftLengthBy2] * H_p_ch.re[kFftLengthBy2];
445       }
446     }
447     limit = lim2;
448     X_partition = 0;
449   } while (p < lim2);
450 }
451 #endif
452 
453 }  // namespace aec3
454 
455 namespace {
456 
457 // Ensures that the newly added filter partitions after a size increase are set
458 // to zero.
ZeroFilter(size_t old_size,size_t new_size,std::vector<std::vector<FftData>> * H)459 void ZeroFilter(size_t old_size,
460                 size_t new_size,
461                 std::vector<std::vector<FftData>>* H) {
462   RTC_DCHECK_GE(H->size(), old_size);
463   RTC_DCHECK_GE(H->size(), new_size);
464 
465   for (size_t p = old_size; p < new_size; ++p) {
466     RTC_DCHECK_EQ((*H)[p].size(), (*H)[0].size());
467     for (size_t ch = 0; ch < (*H)[0].size(); ++ch) {
468       (*H)[p][ch].Clear();
469     }
470   }
471 }
472 
473 }  // namespace
474 
AdaptiveFirFilter(size_t max_size_partitions,size_t initial_size_partitions,size_t size_change_duration_blocks,size_t num_render_channels,Aec3Optimization optimization,ApmDataDumper * data_dumper)475 AdaptiveFirFilter::AdaptiveFirFilter(size_t max_size_partitions,
476                                      size_t initial_size_partitions,
477                                      size_t size_change_duration_blocks,
478                                      size_t num_render_channels,
479                                      Aec3Optimization optimization,
480                                      ApmDataDumper* data_dumper)
481     : data_dumper_(data_dumper),
482       fft_(),
483       optimization_(optimization),
484       num_render_channels_(num_render_channels),
485       max_size_partitions_(max_size_partitions),
486       size_change_duration_blocks_(
487           static_cast<int>(size_change_duration_blocks)),
488       current_size_partitions_(initial_size_partitions),
489       target_size_partitions_(initial_size_partitions),
490       old_target_size_partitions_(initial_size_partitions),
491       H_(max_size_partitions_, std::vector<FftData>(num_render_channels_)) {
492   RTC_DCHECK(data_dumper_);
493   RTC_DCHECK_GE(max_size_partitions, initial_size_partitions);
494 
495   RTC_DCHECK_LT(0, size_change_duration_blocks_);
496   one_by_size_change_duration_blocks_ = 1.f / size_change_duration_blocks_;
497 
498   ZeroFilter(0, max_size_partitions_, &H_);
499 
500   SetSizePartitions(current_size_partitions_, true);
501 }
502 
503 AdaptiveFirFilter::~AdaptiveFirFilter() = default;
504 
HandleEchoPathChange()505 void AdaptiveFirFilter::HandleEchoPathChange() {
506   // TODO(peah): Check the value and purpose of the code below.
507   ZeroFilter(current_size_partitions_, max_size_partitions_, &H_);
508 }
509 
SetSizePartitions(size_t size,bool immediate_effect)510 void AdaptiveFirFilter::SetSizePartitions(size_t size, bool immediate_effect) {
511   RTC_DCHECK_EQ(max_size_partitions_, H_.capacity());
512   RTC_DCHECK_LE(size, max_size_partitions_);
513 
514   target_size_partitions_ = std::min(max_size_partitions_, size);
515   if (immediate_effect) {
516     size_t old_size_partitions_ = current_size_partitions_;
517     current_size_partitions_ = old_target_size_partitions_ =
518         target_size_partitions_;
519     ZeroFilter(old_size_partitions_, current_size_partitions_, &H_);
520 
521     partition_to_constrain_ =
522         std::min(partition_to_constrain_, current_size_partitions_ - 1);
523     size_change_counter_ = 0;
524   } else {
525     size_change_counter_ = size_change_duration_blocks_;
526   }
527 }
528 
UpdateSize()529 void AdaptiveFirFilter::UpdateSize() {
530   RTC_DCHECK_GE(size_change_duration_blocks_, size_change_counter_);
531   size_t old_size_partitions_ = current_size_partitions_;
532   if (size_change_counter_ > 0) {
533     --size_change_counter_;
534 
535     auto average = [](float from, float to, float from_weight) {
536       return from * from_weight + to * (1.f - from_weight);
537     };
538 
539     float change_factor =
540         size_change_counter_ * one_by_size_change_duration_blocks_;
541 
542     current_size_partitions_ = average(old_target_size_partitions_,
543                                        target_size_partitions_, change_factor);
544 
545     partition_to_constrain_ =
546         std::min(partition_to_constrain_, current_size_partitions_ - 1);
547   } else {
548     current_size_partitions_ = old_target_size_partitions_ =
549         target_size_partitions_;
550   }
551   ZeroFilter(old_size_partitions_, current_size_partitions_, &H_);
552   RTC_DCHECK_LE(0, size_change_counter_);
553 }
554 
Filter(const RenderBuffer & render_buffer,FftData * S) const555 void AdaptiveFirFilter::Filter(const RenderBuffer& render_buffer,
556                                FftData* S) const {
557   RTC_DCHECK(S);
558   switch (optimization_) {
559 #if defined(WEBRTC_ARCH_X86_FAMILY)
560     case Aec3Optimization::kSse2:
561       aec3::ApplyFilter_Sse2(render_buffer, current_size_partitions_, H_, S);
562       break;
563     case Aec3Optimization::kAvx2:
564       aec3::ApplyFilter_Avx2(render_buffer, current_size_partitions_, H_, S);
565       break;
566 #endif
567 #if defined(WEBRTC_HAS_NEON)
568     case Aec3Optimization::kNeon:
569       aec3::ApplyFilter_Neon(render_buffer, current_size_partitions_, H_, S);
570       break;
571 #endif
572     default:
573       aec3::ApplyFilter(render_buffer, current_size_partitions_, H_, S);
574   }
575 }
576 
Adapt(const RenderBuffer & render_buffer,const FftData & G)577 void AdaptiveFirFilter::Adapt(const RenderBuffer& render_buffer,
578                               const FftData& G) {
579   // Adapt the filter and update the filter size.
580   AdaptAndUpdateSize(render_buffer, G);
581 
582   // Constrain the filter partitions in a cyclic manner.
583   Constrain();
584 }
585 
Adapt(const RenderBuffer & render_buffer,const FftData & G,std::vector<float> * impulse_response)586 void AdaptiveFirFilter::Adapt(const RenderBuffer& render_buffer,
587                               const FftData& G,
588                               std::vector<float>* impulse_response) {
589   // Adapt the filter and update the filter size.
590   AdaptAndUpdateSize(render_buffer, G);
591 
592   // Constrain the filter partitions in a cyclic manner.
593   ConstrainAndUpdateImpulseResponse(impulse_response);
594 }
595 
ComputeFrequencyResponse(std::vector<std::array<float,kFftLengthBy2Plus1>> * H2) const596 void AdaptiveFirFilter::ComputeFrequencyResponse(
597     std::vector<std::array<float, kFftLengthBy2Plus1>>* H2) const {
598   RTC_DCHECK_GE(max_size_partitions_, H2->capacity());
599 
600   H2->resize(current_size_partitions_);
601 
602   switch (optimization_) {
603 #if defined(WEBRTC_ARCH_X86_FAMILY)
604     case Aec3Optimization::kSse2:
605       aec3::ComputeFrequencyResponse_Sse2(current_size_partitions_, H_, H2);
606       break;
607     case Aec3Optimization::kAvx2:
608       aec3::ComputeFrequencyResponse_Avx2(current_size_partitions_, H_, H2);
609       break;
610 #endif
611 #if defined(WEBRTC_HAS_NEON)
612     case Aec3Optimization::kNeon:
613       aec3::ComputeFrequencyResponse_Neon(current_size_partitions_, H_, H2);
614       break;
615 #endif
616     default:
617       aec3::ComputeFrequencyResponse(current_size_partitions_, H_, H2);
618   }
619 }
620 
AdaptAndUpdateSize(const RenderBuffer & render_buffer,const FftData & G)621 void AdaptiveFirFilter::AdaptAndUpdateSize(const RenderBuffer& render_buffer,
622                                            const FftData& G) {
623   // Update the filter size if needed.
624   UpdateSize();
625 
626   // Adapt the filter.
627   switch (optimization_) {
628 #if defined(WEBRTC_ARCH_X86_FAMILY)
629     case Aec3Optimization::kSse2:
630       aec3::AdaptPartitions_Sse2(render_buffer, G, current_size_partitions_,
631                                  &H_);
632       break;
633     case Aec3Optimization::kAvx2:
634       aec3::AdaptPartitions_Avx2(render_buffer, G, current_size_partitions_,
635                                  &H_);
636       break;
637 #endif
638 #if defined(WEBRTC_HAS_NEON)
639     case Aec3Optimization::kNeon:
640       aec3::AdaptPartitions_Neon(render_buffer, G, current_size_partitions_,
641                                  &H_);
642       break;
643 #endif
644     default:
645       aec3::AdaptPartitions(render_buffer, G, current_size_partitions_, &H_);
646   }
647 }
648 
649 // Constrains the partition of the frequency domain filter to be limited in
650 // time via setting the relevant time-domain coefficients to zero and updates
651 // the corresponding values in an externally stored impulse response estimate.
ConstrainAndUpdateImpulseResponse(std::vector<float> * impulse_response)652 void AdaptiveFirFilter::ConstrainAndUpdateImpulseResponse(
653     std::vector<float>* impulse_response) {
654   RTC_DCHECK_EQ(GetTimeDomainLength(max_size_partitions_),
655                 impulse_response->capacity());
656   impulse_response->resize(GetTimeDomainLength(current_size_partitions_));
657   std::array<float, kFftLength> h;
658   impulse_response->resize(GetTimeDomainLength(current_size_partitions_));
659   std::fill(
660       impulse_response->begin() + partition_to_constrain_ * kFftLengthBy2,
661       impulse_response->begin() + (partition_to_constrain_ + 1) * kFftLengthBy2,
662       0.f);
663 
664   for (size_t ch = 0; ch < num_render_channels_; ++ch) {
665     fft_.Ifft(H_[partition_to_constrain_][ch], &h);
666 
667     static constexpr float kScale = 1.0f / kFftLengthBy2;
668     std::for_each(h.begin(), h.begin() + kFftLengthBy2,
669                   [](float& a) { a *= kScale; });
670     std::fill(h.begin() + kFftLengthBy2, h.end(), 0.f);
671 
672     if (ch == 0) {
673       std::copy(
674           h.begin(), h.begin() + kFftLengthBy2,
675           impulse_response->begin() + partition_to_constrain_ * kFftLengthBy2);
676     } else {
677       for (size_t k = 0, j = partition_to_constrain_ * kFftLengthBy2;
678            k < kFftLengthBy2; ++k, ++j) {
679         if (fabsf((*impulse_response)[j]) < fabsf(h[k])) {
680           (*impulse_response)[j] = h[k];
681         }
682       }
683     }
684 
685     fft_.Fft(&h, &H_[partition_to_constrain_][ch]);
686   }
687 
688   partition_to_constrain_ =
689       partition_to_constrain_ < (current_size_partitions_ - 1)
690           ? partition_to_constrain_ + 1
691           : 0;
692 }
693 
694 // Constrains the a partiton of the frequency domain filter to be limited in
695 // time via setting the relevant time-domain coefficients to zero.
Constrain()696 void AdaptiveFirFilter::Constrain() {
697   std::array<float, kFftLength> h;
698   for (size_t ch = 0; ch < num_render_channels_; ++ch) {
699     fft_.Ifft(H_[partition_to_constrain_][ch], &h);
700 
701     static constexpr float kScale = 1.0f / kFftLengthBy2;
702     std::for_each(h.begin(), h.begin() + kFftLengthBy2,
703                   [](float& a) { a *= kScale; });
704     std::fill(h.begin() + kFftLengthBy2, h.end(), 0.f);
705 
706     fft_.Fft(&h, &H_[partition_to_constrain_][ch]);
707   }
708 
709   partition_to_constrain_ =
710       partition_to_constrain_ < (current_size_partitions_ - 1)
711           ? partition_to_constrain_ + 1
712           : 0;
713 }
714 
ScaleFilter(float factor)715 void AdaptiveFirFilter::ScaleFilter(float factor) {
716   for (auto& H_p : H_) {
717     for (auto& H_p_ch : H_p) {
718       for (auto& re : H_p_ch.re) {
719         re *= factor;
720       }
721       for (auto& im : H_p_ch.im) {
722         im *= factor;
723       }
724     }
725   }
726 }
727 
728 // Set the filter coefficients.
SetFilter(size_t num_partitions,const std::vector<std::vector<FftData>> & H)729 void AdaptiveFirFilter::SetFilter(size_t num_partitions,
730                                   const std::vector<std::vector<FftData>>& H) {
731   const size_t min_num_partitions =
732       std::min(current_size_partitions_, num_partitions);
733   for (size_t p = 0; p < min_num_partitions; ++p) {
734     RTC_DCHECK_EQ(H_[p].size(), H[p].size());
735     RTC_DCHECK_EQ(num_render_channels_, H_[p].size());
736 
737     for (size_t ch = 0; ch < num_render_channels_; ++ch) {
738       std::copy(H[p][ch].re.begin(), H[p][ch].re.end(), H_[p][ch].re.begin());
739       std::copy(H[p][ch].im.begin(), H[p][ch].im.end(), H_[p][ch].im.begin());
740     }
741   }
742 }
743 
744 }  // namespace webrtc
745