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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_PROCESSING_AEC3_FFT_DATA_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_AEC3_FFT_DATA_H_ |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 13 | |
Niels Möller | a12c42a | 2018-07-25 16:05:48 +0200 | [diff] [blame] | 14 | // Defines WEBRTC_ARCH_X86_FAMILY, used below. |
| 15 | #include "rtc_base/system/arch.h" |
| 16 | |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 17 | #if defined(WEBRTC_ARCH_X86_FAMILY) |
| 18 | #include <emmintrin.h> |
| 19 | #endif |
| 20 | #include <algorithm> |
| 21 | #include <array> |
| 22 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "api/array_view.h" |
| 24 | #include "modules/audio_processing/aec3/aec3_common.h" |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | |
| 28 | // Struct that holds imaginary data produced from 128 point real-valued FFTs. |
| 29 | struct FftData { |
| 30 | // Copies the data in src. |
| 31 | void Assign(const FftData& src) { |
| 32 | std::copy(src.re.begin(), src.re.end(), re.begin()); |
| 33 | std::copy(src.im.begin(), src.im.end(), im.begin()); |
| 34 | im[0] = im[kFftLengthBy2] = 0; |
| 35 | } |
| 36 | |
| 37 | // Clears all the imaginary. |
| 38 | void Clear() { |
| 39 | re.fill(0.f); |
| 40 | im.fill(0.f); |
| 41 | } |
| 42 | |
| 43 | // Computes the power spectrum of the data. |
Zhaoliang Ma | e537e9c | 2020-08-31 10:20:47 +0800 | [diff] [blame] | 44 | void SpectrumAVX2(rtc::ArrayView<float> power_spectrum) const; |
| 45 | |
| 46 | // Computes the power spectrum of the data. |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 47 | void Spectrum(Aec3Optimization optimization, |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 48 | rtc::ArrayView<float> power_spectrum) const { |
| 49 | RTC_DCHECK_EQ(kFftLengthBy2Plus1, power_spectrum.size()); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 50 | switch (optimization) { |
| 51 | #if defined(WEBRTC_ARCH_X86_FAMILY) |
| 52 | case Aec3Optimization::kSse2: { |
| 53 | constexpr int kNumFourBinBands = kFftLengthBy2 / 4; |
| 54 | constexpr int kLimit = kNumFourBinBands * 4; |
| 55 | for (size_t k = 0; k < kLimit; k += 4) { |
| 56 | const __m128 r = _mm_loadu_ps(&re[k]); |
| 57 | const __m128 i = _mm_loadu_ps(&im[k]); |
| 58 | const __m128 ii = _mm_mul_ps(i, i); |
| 59 | const __m128 rr = _mm_mul_ps(r, r); |
| 60 | const __m128 rrii = _mm_add_ps(rr, ii); |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 61 | _mm_storeu_ps(&power_spectrum[k], rrii); |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 62 | } |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 63 | power_spectrum[kFftLengthBy2] = re[kFftLengthBy2] * re[kFftLengthBy2] + |
| 64 | im[kFftLengthBy2] * im[kFftLengthBy2]; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 65 | } break; |
Zhaoliang Ma | e537e9c | 2020-08-31 10:20:47 +0800 | [diff] [blame] | 66 | case Aec3Optimization::kAvx2: |
| 67 | SpectrumAVX2(power_spectrum); |
| 68 | break; |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 69 | #endif |
| 70 | default: |
Per Åhgren | 8ba5861 | 2017-12-01 23:01:44 +0100 | [diff] [blame] | 71 | std::transform(re.begin(), re.end(), im.begin(), power_spectrum.begin(), |
peah | 522d71b | 2017-02-23 05:16:26 -0800 | [diff] [blame] | 72 | [](float a, float b) { return a * a + b * b; }); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Copy the data from an interleaved array. |
| 77 | void CopyFromPackedArray(const std::array<float, kFftLength>& v) { |
| 78 | re[0] = v[0]; |
| 79 | re[kFftLengthBy2] = v[1]; |
| 80 | im[0] = im[kFftLengthBy2] = 0; |
| 81 | for (size_t k = 1, j = 2; k < kFftLengthBy2; ++k) { |
| 82 | re[k] = v[j++]; |
| 83 | im[k] = v[j++]; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Copies the data into an interleaved array. |
| 88 | void CopyToPackedArray(std::array<float, kFftLength>* v) const { |
| 89 | RTC_DCHECK(v); |
| 90 | (*v)[0] = re[0]; |
| 91 | (*v)[1] = re[kFftLengthBy2]; |
| 92 | for (size_t k = 1, j = 2; k < kFftLengthBy2; ++k) { |
| 93 | (*v)[j++] = re[k]; |
| 94 | (*v)[j++] = im[k]; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | std::array<float, kFftLengthBy2Plus1> re; |
| 99 | std::array<float, kFftLengthBy2Plus1> im; |
| 100 | }; |
| 101 | |
| 102 | } // namespace webrtc |
| 103 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 104 | #endif // MODULES_AUDIO_PROCESSING_AEC3_FFT_DATA_H_ |