blob: f668841be63581131bacf9b2a0d02274adf528f1 [file] [log] [blame]
aluebs@webrtc.org37ca7652014-03-24 10:16:11 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "common_audio/fir_filter_neon.h"
aluebs@webrtc.org37ca7652014-03-24 10:16:11 +000012
13#include <arm_neon.h>
aluebs@webrtc.org37ca7652014-03-24 10:16:11 +000014#include <string.h>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
Karl Wiberg29e7bee2018-03-22 14:11:52 +010017#include "rtc_base/memory/aligned_malloc.h"
aluebs@webrtc.org37ca7652014-03-24 10:16:11 +000018
19namespace webrtc {
20
Yves Gerey665174f2018-06-19 15:03:05 +020021FIRFilterNEON::~FIRFilterNEON() {}
Patrik Höglundf715c532017-11-17 11:04:15 +010022
aluebs@webrtc.org37ca7652014-03-24 10:16:11 +000023FIRFilterNEON::FIRFilterNEON(const float* coefficients,
24 size_t coefficients_length,
25 size_t max_input_length)
26 : // Closest higher multiple of four.
27 coefficients_length_((coefficients_length + 3) & ~0x03),
28 state_length_(coefficients_length_ - 1),
29 coefficients_(static_cast<float*>(
30 AlignedMalloc(sizeof(float) * coefficients_length_, 16))),
31 state_(static_cast<float*>(
32 AlignedMalloc(sizeof(float) * (max_input_length + state_length_),
33 16))) {
34 // Add zeros at the end of the coefficients.
35 size_t padding = coefficients_length_ - coefficients_length;
36 memset(coefficients_.get(), 0.f, padding * sizeof(coefficients_[0]));
37 // The coefficients are reversed to compensate for the order in which the
38 // input samples are acquired (most recent last).
39 for (size_t i = 0; i < coefficients_length; ++i) {
40 coefficients_[i + padding] = coefficients[coefficients_length - i - 1];
41 }
Yves Gerey665174f2018-06-19 15:03:05 +020042 memset(state_.get(), 0.f,
aluebs@webrtc.org37ca7652014-03-24 10:16:11 +000043 (max_input_length + state_length_) * sizeof(state_[0]));
44}
45
46void FIRFilterNEON::Filter(const float* in, size_t length, float* out) {
kwibergb890c95c2016-11-29 05:30:40 -080047 RTC_DCHECK_GT(length, 0);
aluebs@webrtc.org37ca7652014-03-24 10:16:11 +000048
49 memcpy(&state_[state_length_], in, length * sizeof(*in));
50
51 // Convolves the input signal |in| with the filter kernel |coefficients_|
52 // taking into account the previous state.
53 for (size_t i = 0; i < length; ++i) {
54 float* in_ptr = &state_[i];
55 float* coef_ptr = coefficients_.get();
56
57 float32x4_t m_sum = vmovq_n_f32(0);
58 float32x4_t m_in;
59
60 for (size_t j = 0; j < coefficients_length_; j += 4) {
Yves Gerey665174f2018-06-19 15:03:05 +020061 m_in = vld1q_f32(in_ptr + j);
62 m_sum = vmlaq_f32(m_sum, m_in, vld1q_f32(coef_ptr + j));
aluebs@webrtc.org37ca7652014-03-24 10:16:11 +000063 }
64
65 float32x2_t m_half = vadd_f32(vget_high_f32(m_sum), vget_low_f32(m_sum));
66 out[i] = vget_lane_f32(vpadd_f32(m_half, m_half), 0);
67 }
68
69 // Update current state.
70 memmove(state_.get(), &state_[length], state_length_ * sizeof(state_[0]));
71}
72
73} // namespace webrtc