OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/common_audio/fir_filter.h" | 11 #include "webrtc/common_audio/fir_filter.h" |
12 | 12 |
13 #include <assert.h> | |
14 #include <string.h> | 13 #include <string.h> |
15 | 14 |
16 #include <memory> | 15 #include <memory> |
17 | 16 |
| 17 #include "webrtc/base/checks.h" |
18 #include "webrtc/common_audio/fir_filter_neon.h" | 18 #include "webrtc/common_audio/fir_filter_neon.h" |
19 #include "webrtc/common_audio/fir_filter_sse.h" | 19 #include "webrtc/common_audio/fir_filter_sse.h" |
20 #include "webrtc/system_wrappers/include/cpu_features_wrapper.h" | 20 #include "webrtc/system_wrappers/include/cpu_features_wrapper.h" |
21 | 21 |
22 namespace webrtc { | 22 namespace webrtc { |
23 | 23 |
24 class FIRFilterC : public FIRFilter { | 24 class FIRFilterC : public FIRFilter { |
25 public: | 25 public: |
26 FIRFilterC(const float* coefficients, | 26 FIRFilterC(const float* coefficients, |
27 size_t coefficients_length); | 27 size_t coefficients_length); |
28 | 28 |
29 void Filter(const float* in, size_t length, float* out) override; | 29 void Filter(const float* in, size_t length, float* out) override; |
30 | 30 |
31 private: | 31 private: |
32 size_t coefficients_length_; | 32 size_t coefficients_length_; |
33 size_t state_length_; | 33 size_t state_length_; |
34 std::unique_ptr<float[]> coefficients_; | 34 std::unique_ptr<float[]> coefficients_; |
35 std::unique_ptr<float[]> state_; | 35 std::unique_ptr<float[]> state_; |
36 }; | 36 }; |
37 | 37 |
38 FIRFilter* FIRFilter::Create(const float* coefficients, | 38 FIRFilter* FIRFilter::Create(const float* coefficients, |
39 size_t coefficients_length, | 39 size_t coefficients_length, |
40 size_t max_input_length) { | 40 size_t max_input_length) { |
41 if (!coefficients || coefficients_length <= 0 || max_input_length <= 0) { | 41 if (!coefficients || coefficients_length <= 0 || max_input_length <= 0) { |
42 assert(false); | 42 RTC_NOTREACHED(); |
43 return NULL; | 43 return NULL; |
44 } | 44 } |
45 | 45 |
46 FIRFilter* filter = NULL; | 46 FIRFilter* filter = NULL; |
47 // If we know the minimum architecture at compile time, avoid CPU detection. | 47 // If we know the minimum architecture at compile time, avoid CPU detection. |
48 #if defined(WEBRTC_ARCH_X86_FAMILY) | 48 #if defined(WEBRTC_ARCH_X86_FAMILY) |
49 #if defined(__SSE2__) | 49 #if defined(__SSE2__) |
50 filter = | 50 filter = |
51 new FIRFilterSSE2(coefficients, coefficients_length, max_input_length); | 51 new FIRFilterSSE2(coefficients, coefficients_length, max_input_length); |
52 #else | 52 #else |
(...skipping 20 matching lines...) Expand all Loading... |
73 state_length_(coefficients_length - 1), | 73 state_length_(coefficients_length - 1), |
74 coefficients_(new float[coefficients_length_]), | 74 coefficients_(new float[coefficients_length_]), |
75 state_(new float[state_length_]) { | 75 state_(new float[state_length_]) { |
76 for (size_t i = 0; i < coefficients_length_; ++i) { | 76 for (size_t i = 0; i < coefficients_length_; ++i) { |
77 coefficients_[i] = coefficients[coefficients_length_ - i - 1]; | 77 coefficients_[i] = coefficients[coefficients_length_ - i - 1]; |
78 } | 78 } |
79 memset(state_.get(), 0, state_length_ * sizeof(state_[0])); | 79 memset(state_.get(), 0, state_length_ * sizeof(state_[0])); |
80 } | 80 } |
81 | 81 |
82 void FIRFilterC::Filter(const float* in, size_t length, float* out) { | 82 void FIRFilterC::Filter(const float* in, size_t length, float* out) { |
83 assert(length > 0); | 83 RTC_DCHECK_GT(length, 0); |
84 | 84 |
85 // Convolves the input signal |in| with the filter kernel |coefficients_| | 85 // Convolves the input signal |in| with the filter kernel |coefficients_| |
86 // taking into account the previous state. | 86 // taking into account the previous state. |
87 for (size_t i = 0; i < length; ++i) { | 87 for (size_t i = 0; i < length; ++i) { |
88 out[i] = 0.f; | 88 out[i] = 0.f; |
89 size_t j; | 89 size_t j; |
90 for (j = 0; state_length_ > i && j < state_length_ - i; ++j) { | 90 for (j = 0; state_length_ > i && j < state_length_ - i; ++j) { |
91 out[i] += state_[i + j] * coefficients_[j]; | 91 out[i] += state_[i + j] * coefficients_[j]; |
92 } | 92 } |
93 for (; j < coefficients_length_; ++j) { | 93 for (; j < coefficients_length_; ++j) { |
94 out[i] += in[j + i - state_length_] * coefficients_[j]; | 94 out[i] += in[j + i - state_length_] * coefficients_[j]; |
95 } | 95 } |
96 } | 96 } |
97 | 97 |
98 // Update current state. | 98 // Update current state. |
99 if (length >= state_length_) { | 99 if (length >= state_length_) { |
100 memcpy( | 100 memcpy( |
101 state_.get(), &in[length - state_length_], state_length_ * sizeof(*in)); | 101 state_.get(), &in[length - state_length_], state_length_ * sizeof(*in)); |
102 } else { | 102 } else { |
103 memmove(state_.get(), | 103 memmove(state_.get(), |
104 &state_[length], | 104 &state_[length], |
105 (state_length_ - length) * sizeof(state_[0])); | 105 (state_length_ - length) * sizeof(state_[0])); |
106 memcpy(&state_[state_length_ - length], in, length * sizeof(*in)); | 106 memcpy(&state_[state_length_ - length], in, length * sizeof(*in)); |
107 } | 107 } |
108 } | 108 } |
109 | 109 |
110 } // namespace webrtc | 110 } // namespace webrtc |
OLD | NEW |