Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / common_audio / fir_filter.cc
1 /*
2  *  Copyright (c) 2014 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 "webrtc/common_audio/fir_filter.h"
12
13 #include <assert.h>
14 #include <string.h>
15
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"
20
21 namespace webrtc {
22
23 class FIRFilterC : public FIRFilter {
24  public:
25   FIRFilterC(const float* coefficients,
26              size_t coefficients_length);
27
28   virtual void Filter(const float* in, size_t length, float* out) OVERRIDE;
29
30  private:
31   size_t coefficients_length_;
32   size_t state_length_;
33   scoped_ptr<float[]> coefficients_;
34   scoped_ptr<float[]> state_;
35 };
36
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) {
41     assert(false);
42     return NULL;
43   }
44
45   FIRFilter* filter = NULL;
46 // If we know the minimum architecture at compile time, avoid CPU detection.
47 #if defined(WEBRTC_ARCH_X86_FAMILY)
48 #if defined(__SSE2__)
49   filter =
50       new FIRFilterSSE2(coefficients, coefficients_length, max_input_length);
51 #else
52   // x86 CPU detection required.
53   if (WebRtc_GetCPUInfo(kSSE2)) {
54     filter =
55         new FIRFilterSSE2(coefficients, coefficients_length, max_input_length);
56   } else {
57     filter = new FIRFilterC(coefficients, coefficients_length);
58   }
59 #endif
60 #elif defined(WEBRTC_ARCH_ARM_V7)
61 #if defined(WEBRTC_ARCH_ARM_NEON)
62   filter =
63       new FIRFilterNEON(coefficients, coefficients_length, max_input_length);
64 #else
65   // ARM CPU detection required.
66   if (WebRtc_GetCPUFeaturesARM() & kCPUFeatureNEON) {
67     filter =
68         new FIRFilterNEON(coefficients, coefficients_length, max_input_length);
69   } else {
70     filter = new FIRFilterC(coefficients, coefficients_length);
71   }
72 #endif
73 #else
74   filter = new FIRFilterC(coefficients, coefficients_length);
75 #endif
76
77   return filter;
78 }
79
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];
87   }
88   memset(state_.get(), 0, state_length_ * sizeof(state_[0]));
89 }
90
91 void FIRFilterC::Filter(const float* in, size_t length, float* out) {
92   assert(length > 0);
93
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) {
97     out[i] = 0.f;
98     size_t j;
99     for (j = 0; state_length_ > i && j < state_length_ - i; ++j) {
100       out[i] += state_[i + j] * coefficients_[j];
101     }
102     for (; j < coefficients_length_; ++j) {
103       out[i] += in[j + i - state_length_] * coefficients_[j];
104     }
105   }
106
107   // Update current state.
108   if (length >= state_length_) {
109     memcpy(
110         state_.get(), &in[length - state_length_], state_length_ * sizeof(*in));
111   } else {
112     memmove(state_.get(),
113             &state_[length],
114             (state_length_ - length) * sizeof(state_[0]));
115     memcpy(&state_[state_length_ - length], in, length * sizeof(*in));
116   }
117 }
118
119 }  // namespace webrtc