2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/common_audio/fir_filter.h"
16 #include "webrtc/common_audio/fir_filter_neon.h"
17 #include "webrtc/common_audio/fir_filter_sse.h"
18 #include "webrtc/system_wrappers/interface/cpu_features_wrapper.h"
19 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
23 class FIRFilterC : public FIRFilter {
25 FIRFilterC(const float* coefficients,
26 size_t coefficients_length);
28 virtual void Filter(const float* in, size_t length, float* out) OVERRIDE;
31 size_t coefficients_length_;
33 scoped_ptr<float[]> coefficients_;
34 scoped_ptr<float[]> state_;
37 FIRFilter* FIRFilter::Create(const float* coefficients,
38 size_t coefficients_length,
39 size_t max_input_length) {
40 if (!coefficients || coefficients_length <= 0 || max_input_length <= 0) {
45 FIRFilter* filter = NULL;
46 // If we know the minimum architecture at compile time, avoid CPU detection.
47 #if defined(WEBRTC_ARCH_X86_FAMILY)
50 new FIRFilterSSE2(coefficients, coefficients_length, max_input_length);
52 // x86 CPU detection required.
53 if (WebRtc_GetCPUInfo(kSSE2)) {
55 new FIRFilterSSE2(coefficients, coefficients_length, max_input_length);
57 filter = new FIRFilterC(coefficients, coefficients_length);
60 #elif defined(WEBRTC_ARCH_ARM_V7)
61 #if defined(WEBRTC_ARCH_ARM_NEON)
63 new FIRFilterNEON(coefficients, coefficients_length, max_input_length);
65 // ARM CPU detection required.
66 if (WebRtc_GetCPUFeaturesARM() & kCPUFeatureNEON) {
68 new FIRFilterNEON(coefficients, coefficients_length, max_input_length);
70 filter = new FIRFilterC(coefficients, coefficients_length);
74 filter = new FIRFilterC(coefficients, coefficients_length);
80 FIRFilterC::FIRFilterC(const float* coefficients, size_t coefficients_length)
81 : coefficients_length_(coefficients_length),
82 state_length_(coefficients_length - 1),
83 coefficients_(new float[coefficients_length_]),
84 state_(new float[state_length_]) {
85 for (size_t i = 0; i < coefficients_length_; ++i) {
86 coefficients_[i] = coefficients[coefficients_length_ - i - 1];
88 memset(state_.get(), 0, state_length_ * sizeof(state_[0]));
91 void FIRFilterC::Filter(const float* in, size_t length, float* out) {
94 // Convolves the input signal |in| with the filter kernel |coefficients_|
95 // taking into account the previous state.
96 for (size_t i = 0; i < length; ++i) {
99 for (j = 0; state_length_ > i && j < state_length_ - i; ++j) {
100 out[i] += state_[i + j] * coefficients_[j];
102 for (; j < coefficients_length_; ++j) {
103 out[i] += in[j + i - state_length_] * coefficients_[j];
107 // Update current state.
108 if (length >= state_length_) {
110 state_.get(), &in[length - state_length_], state_length_ * sizeof(*in));
112 memmove(state_.get(),
114 (state_length_ - length) * sizeof(state_[0]));
115 memcpy(&state_[state_length_ - length], in, length * sizeof(*in));
119 } // namespace webrtc