aluebs@webrtc.org | 37ca765 | 2014-03-24 10:16:11 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "common_audio/fir_filter_neon.h" |
aluebs@webrtc.org | 37ca765 | 2014-03-24 10:16:11 +0000 | [diff] [blame] | 12 | |
| 13 | #include <arm_neon.h> |
aluebs@webrtc.org | 37ca765 | 2014-03-24 10:16:11 +0000 | [diff] [blame] | 14 | #include <string.h> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
Karl Wiberg | 29e7bee | 2018-03-22 14:11:52 +0100 | [diff] [blame] | 17 | #include "rtc_base/memory/aligned_malloc.h" |
aluebs@webrtc.org | 37ca765 | 2014-03-24 10:16:11 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
Patrik Höglund | f715c53 | 2017-11-17 11:04:15 +0100 | [diff] [blame] | 21 | FIRFilterNEON::~FIRFilterNEON() { |
| 22 | } |
| 23 | |
aluebs@webrtc.org | 37ca765 | 2014-03-24 10:16:11 +0000 | [diff] [blame] | 24 | FIRFilterNEON::FIRFilterNEON(const float* coefficients, |
| 25 | size_t coefficients_length, |
| 26 | size_t max_input_length) |
| 27 | : // Closest higher multiple of four. |
| 28 | coefficients_length_((coefficients_length + 3) & ~0x03), |
| 29 | state_length_(coefficients_length_ - 1), |
| 30 | coefficients_(static_cast<float*>( |
| 31 | AlignedMalloc(sizeof(float) * coefficients_length_, 16))), |
| 32 | state_(static_cast<float*>( |
| 33 | AlignedMalloc(sizeof(float) * (max_input_length + state_length_), |
| 34 | 16))) { |
| 35 | // Add zeros at the end of the coefficients. |
| 36 | size_t padding = coefficients_length_ - coefficients_length; |
| 37 | memset(coefficients_.get(), 0.f, padding * sizeof(coefficients_[0])); |
| 38 | // The coefficients are reversed to compensate for the order in which the |
| 39 | // input samples are acquired (most recent last). |
| 40 | for (size_t i = 0; i < coefficients_length; ++i) { |
| 41 | coefficients_[i + padding] = coefficients[coefficients_length - i - 1]; |
| 42 | } |
| 43 | memset(state_.get(), |
| 44 | 0.f, |
| 45 | (max_input_length + state_length_) * sizeof(state_[0])); |
| 46 | } |
| 47 | |
| 48 | void FIRFilterNEON::Filter(const float* in, size_t length, float* out) { |
kwiberg | b890c95c | 2016-11-29 05:30:40 -0800 | [diff] [blame] | 49 | RTC_DCHECK_GT(length, 0); |
aluebs@webrtc.org | 37ca765 | 2014-03-24 10:16:11 +0000 | [diff] [blame] | 50 | |
| 51 | memcpy(&state_[state_length_], in, length * sizeof(*in)); |
| 52 | |
| 53 | // Convolves the input signal |in| with the filter kernel |coefficients_| |
| 54 | // taking into account the previous state. |
| 55 | for (size_t i = 0; i < length; ++i) { |
| 56 | float* in_ptr = &state_[i]; |
| 57 | float* coef_ptr = coefficients_.get(); |
| 58 | |
| 59 | float32x4_t m_sum = vmovq_n_f32(0); |
| 60 | float32x4_t m_in; |
| 61 | |
| 62 | for (size_t j = 0; j < coefficients_length_; j += 4) { |
| 63 | m_in = vld1q_f32(in_ptr + j); |
| 64 | m_sum = vmlaq_f32(m_sum, m_in, vld1q_f32(coef_ptr + j)); |
| 65 | } |
| 66 | |
| 67 | float32x2_t m_half = vadd_f32(vget_high_f32(m_sum), vget_low_f32(m_sum)); |
| 68 | out[i] = vget_lane_f32(vpadd_f32(m_half, m_half), 0); |
| 69 | } |
| 70 | |
| 71 | // Update current state. |
| 72 | memmove(state_.get(), &state_[length], state_length_ * sizeof(state_[0])); |
| 73 | } |
| 74 | |
| 75 | } // namespace webrtc |