1 /*
2 * Copyright (c) 2020 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 #include <immintrin.h>
14
15 #include "rtc_base/checks.h"
16
17 namespace webrtc {
18
19 namespace aec3 {
20
21 // Computes and stores the frequency response of the filter.
ComputeFrequencyResponse_Avx2(size_t num_partitions,const std::vector<std::vector<FftData>> & H,std::vector<std::array<float,kFftLengthBy2Plus1>> * H2)22 void ComputeFrequencyResponse_Avx2(
23 size_t num_partitions,
24 const std::vector<std::vector<FftData>>& H,
25 std::vector<std::array<float, kFftLengthBy2Plus1>>* H2) {
26 for (auto& H2_ch : *H2) {
27 H2_ch.fill(0.f);
28 }
29
30 const size_t num_render_channels = H[0].size();
31 RTC_DCHECK_EQ(H.size(), H2->capacity());
32 for (size_t p = 0; p < num_partitions; ++p) {
33 RTC_DCHECK_EQ(kFftLengthBy2Plus1, (*H2)[p].size());
34 auto& H2_p = (*H2)[p];
35 for (size_t ch = 0; ch < num_render_channels; ++ch) {
36 const FftData& H_p_ch = H[p][ch];
37 for (size_t j = 0; j < kFftLengthBy2; j += 8) {
38 __m256 re = _mm256_loadu_ps(&H_p_ch.re[j]);
39 __m256 re2 = _mm256_mul_ps(re, re);
40 __m256 im = _mm256_loadu_ps(&H_p_ch.im[j]);
41 re2 = _mm256_fmadd_ps(im, im, re2);
42 __m256 H2_k_j = _mm256_loadu_ps(&H2_p[j]);
43 H2_k_j = _mm256_max_ps(H2_k_j, re2);
44 _mm256_storeu_ps(&H2_p[j], H2_k_j);
45 }
46 float H2_new = H_p_ch.re[kFftLengthBy2] * H_p_ch.re[kFftLengthBy2] +
47 H_p_ch.im[kFftLengthBy2] * H_p_ch.im[kFftLengthBy2];
48 H2_p[kFftLengthBy2] = std::max(H2_p[kFftLengthBy2], H2_new);
49 }
50 }
51 }
52
53 // Adapts the filter partitions.
AdaptPartitions_Avx2(const RenderBuffer & render_buffer,const FftData & G,size_t num_partitions,std::vector<std::vector<FftData>> * H)54 void AdaptPartitions_Avx2(const RenderBuffer& render_buffer,
55 const FftData& G,
56 size_t num_partitions,
57 std::vector<std::vector<FftData>>* H) {
58 rtc::ArrayView<const std::vector<FftData>> render_buffer_data =
59 render_buffer.GetFftBuffer();
60 const size_t num_render_channels = render_buffer_data[0].size();
61 const size_t lim1 = std::min(
62 render_buffer_data.size() - render_buffer.Position(), num_partitions);
63 const size_t lim2 = num_partitions;
64 constexpr size_t kNumEightBinBands = kFftLengthBy2 / 8;
65
66 size_t X_partition = render_buffer.Position();
67 size_t limit = lim1;
68 size_t p = 0;
69 do {
70 for (; p < limit; ++p, ++X_partition) {
71 for (size_t ch = 0; ch < num_render_channels; ++ch) {
72 FftData& H_p_ch = (*H)[p][ch];
73 const FftData& X = render_buffer_data[X_partition][ch];
74
75 for (size_t k = 0, n = 0; n < kNumEightBinBands; ++n, k += 8) {
76 const __m256 G_re = _mm256_loadu_ps(&G.re[k]);
77 const __m256 G_im = _mm256_loadu_ps(&G.im[k]);
78 const __m256 X_re = _mm256_loadu_ps(&X.re[k]);
79 const __m256 X_im = _mm256_loadu_ps(&X.im[k]);
80 const __m256 H_re = _mm256_loadu_ps(&H_p_ch.re[k]);
81 const __m256 H_im = _mm256_loadu_ps(&H_p_ch.im[k]);
82 const __m256 a = _mm256_mul_ps(X_re, G_re);
83 const __m256 b = _mm256_mul_ps(X_im, G_im);
84 const __m256 c = _mm256_mul_ps(X_re, G_im);
85 const __m256 d = _mm256_mul_ps(X_im, G_re);
86 const __m256 e = _mm256_add_ps(a, b);
87 const __m256 f = _mm256_sub_ps(c, d);
88 const __m256 g = _mm256_add_ps(H_re, e);
89 const __m256 h = _mm256_add_ps(H_im, f);
90 _mm256_storeu_ps(&H_p_ch.re[k], g);
91 _mm256_storeu_ps(&H_p_ch.im[k], h);
92 }
93 }
94 }
95 X_partition = 0;
96 limit = lim2;
97 } while (p < lim2);
98
99 X_partition = render_buffer.Position();
100 limit = lim1;
101 p = 0;
102 do {
103 for (; p < limit; ++p, ++X_partition) {
104 for (size_t ch = 0; ch < num_render_channels; ++ch) {
105 FftData& H_p_ch = (*H)[p][ch];
106 const FftData& X = render_buffer_data[X_partition][ch];
107
108 H_p_ch.re[kFftLengthBy2] += X.re[kFftLengthBy2] * G.re[kFftLengthBy2] +
109 X.im[kFftLengthBy2] * G.im[kFftLengthBy2];
110 H_p_ch.im[kFftLengthBy2] += X.re[kFftLengthBy2] * G.im[kFftLengthBy2] -
111 X.im[kFftLengthBy2] * G.re[kFftLengthBy2];
112 }
113 }
114
115 X_partition = 0;
116 limit = lim2;
117 } while (p < lim2);
118 }
119
120 // Produces the filter output (AVX2 variant).
ApplyFilter_Avx2(const RenderBuffer & render_buffer,size_t num_partitions,const std::vector<std::vector<FftData>> & H,FftData * S)121 void ApplyFilter_Avx2(const RenderBuffer& render_buffer,
122 size_t num_partitions,
123 const std::vector<std::vector<FftData>>& H,
124 FftData* S) {
125 RTC_DCHECK_GE(H.size(), H.size() - 1);
126 S->re.fill(0.f);
127 S->im.fill(0.f);
128
129 rtc::ArrayView<const std::vector<FftData>> render_buffer_data =
130 render_buffer.GetFftBuffer();
131 const size_t num_render_channels = render_buffer_data[0].size();
132 const size_t lim1 = std::min(
133 render_buffer_data.size() - render_buffer.Position(), num_partitions);
134 const size_t lim2 = num_partitions;
135 constexpr size_t kNumEightBinBands = kFftLengthBy2 / 8;
136
137 size_t X_partition = render_buffer.Position();
138 size_t p = 0;
139 size_t limit = lim1;
140 do {
141 for (; p < limit; ++p, ++X_partition) {
142 for (size_t ch = 0; ch < num_render_channels; ++ch) {
143 const FftData& H_p_ch = H[p][ch];
144 const FftData& X = render_buffer_data[X_partition][ch];
145 for (size_t k = 0, n = 0; n < kNumEightBinBands; ++n, k += 8) {
146 const __m256 X_re = _mm256_loadu_ps(&X.re[k]);
147 const __m256 X_im = _mm256_loadu_ps(&X.im[k]);
148 const __m256 H_re = _mm256_loadu_ps(&H_p_ch.re[k]);
149 const __m256 H_im = _mm256_loadu_ps(&H_p_ch.im[k]);
150 const __m256 S_re = _mm256_loadu_ps(&S->re[k]);
151 const __m256 S_im = _mm256_loadu_ps(&S->im[k]);
152 const __m256 a = _mm256_mul_ps(X_re, H_re);
153 const __m256 b = _mm256_mul_ps(X_im, H_im);
154 const __m256 c = _mm256_mul_ps(X_re, H_im);
155 const __m256 d = _mm256_mul_ps(X_im, H_re);
156 const __m256 e = _mm256_sub_ps(a, b);
157 const __m256 f = _mm256_add_ps(c, d);
158 const __m256 g = _mm256_add_ps(S_re, e);
159 const __m256 h = _mm256_add_ps(S_im, f);
160 _mm256_storeu_ps(&S->re[k], g);
161 _mm256_storeu_ps(&S->im[k], h);
162 }
163 }
164 }
165 limit = lim2;
166 X_partition = 0;
167 } while (p < lim2);
168
169 X_partition = render_buffer.Position();
170 p = 0;
171 limit = lim1;
172 do {
173 for (; p < limit; ++p, ++X_partition) {
174 for (size_t ch = 0; ch < num_render_channels; ++ch) {
175 const FftData& H_p_ch = H[p][ch];
176 const FftData& X = render_buffer_data[X_partition][ch];
177 S->re[kFftLengthBy2] += X.re[kFftLengthBy2] * H_p_ch.re[kFftLengthBy2] -
178 X.im[kFftLengthBy2] * H_p_ch.im[kFftLengthBy2];
179 S->im[kFftLengthBy2] += X.re[kFftLengthBy2] * H_p_ch.im[kFftLengthBy2] +
180 X.im[kFftLengthBy2] * H_p_ch.re[kFftLengthBy2];
181 }
182 }
183 limit = lim2;
184 X_partition = 0;
185 } while (p < lim2);
186 }
187
188 } // namespace aec3
189 } // namespace webrtc
190