1 /*
2  *  Copyright (c) 2011 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 
12 /*
13  * This file contains the function WebRtcSpl_FilterAR().
14  * The description header can be found in signal_processing_library.h
15  *
16  */
17 
18 #include "common_audio/signal_processing/include/signal_processing_library.h"
19 
20 #include "rtc_base/checks.h"
21 
WebRtcSpl_FilterAR(const int16_t * a,size_t a_length,const int16_t * x,size_t x_length,int16_t * state,size_t state_length,int16_t * state_low,size_t state_low_length,int16_t * filtered,int16_t * filtered_low,size_t filtered_low_length)22 size_t WebRtcSpl_FilterAR(const int16_t* a,
23                           size_t a_length,
24                           const int16_t* x,
25                           size_t x_length,
26                           int16_t* state,
27                           size_t state_length,
28                           int16_t* state_low,
29                           size_t state_low_length,
30                           int16_t* filtered,
31                           int16_t* filtered_low,
32                           size_t filtered_low_length)
33 {
34     int64_t o;
35     int32_t oLOW;
36     size_t i, j, stop;
37     const int16_t* x_ptr = &x[0];
38     int16_t* filteredFINAL_ptr = filtered;
39     int16_t* filteredFINAL_LOW_ptr = filtered_low;
40 
41     for (i = 0; i < x_length; i++)
42     {
43         // Calculate filtered[i] and filtered_low[i]
44         const int16_t* a_ptr = &a[1];
45         // The index can become negative, but the arrays will never be indexed
46         // with it when negative. Nevertheless, the index cannot be a size_t
47         // because of this.
48         int filtered_ix = (int)i - 1;
49         int16_t* state_ptr = &state[state_length - 1];
50         int16_t* state_low_ptr = &state_low[state_length - 1];
51 
52         o = (int32_t)(*x_ptr++) * (1 << 12);
53         oLOW = (int32_t)0;
54 
55         stop = (i < a_length) ? i + 1 : a_length;
56         for (j = 1; j < stop; j++)
57         {
58           RTC_DCHECK_GE(filtered_ix, 0);
59           o -= *a_ptr * filtered[filtered_ix];
60           oLOW -= *a_ptr++ * filtered_low[filtered_ix];
61           --filtered_ix;
62         }
63         for (j = i + 1; j < a_length; j++)
64         {
65           o -= *a_ptr * *state_ptr--;
66           oLOW -= *a_ptr++ * *state_low_ptr--;
67         }
68 
69         o += (oLOW >> 12);
70         *filteredFINAL_ptr = (int16_t)((o + (int32_t)2048) >> 12);
71         *filteredFINAL_LOW_ptr++ =
72             (int16_t)(o - ((int32_t)(*filteredFINAL_ptr++) * (1 << 12)));
73     }
74 
75     // Save the filter state
76     if (x_length >= state_length)
77     {
78         WebRtcSpl_CopyFromEndW16(filtered, x_length, a_length - 1, state);
79         WebRtcSpl_CopyFromEndW16(filtered_low, x_length, a_length - 1, state_low);
80     } else
81     {
82         for (i = 0; i < state_length - x_length; i++)
83         {
84             state[i] = state[i + x_length];
85             state_low[i] = state_low[i + x_length];
86         }
87         for (i = 0; i < x_length; i++)
88         {
89             state[state_length - x_length + i] = filtered[i];
90             state_low[state_length - x_length + i] = filtered_low[i];
91         }
92     }
93 
94     return x_length;
95 }
96