1 /*
2  *  Copyright (c) 2012 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 header file includes all of the fix point signal processing library (SPL) function
14  * descriptions and declarations.
15  * For specific function calls, see bottom of file.
16  */
17 
18 #ifndef WEBRTC_SPL_SIGNAL_PROCESSING_LIBRARY_H_
19 #define WEBRTC_SPL_SIGNAL_PROCESSING_LIBRARY_H_
20 
21 #include <string.h>
22 #include "typedefs.h"
23 
24 #ifdef ARM_WINM
25 #include <Armintr.h> // intrinsic file for windows mobile
26 #endif
27 
28 // Macros specific for the fixed point implementation
29 #define WEBRTC_SPL_WORD16_MAX       32767
30 #define WEBRTC_SPL_WORD16_MIN       -32768
31 #define WEBRTC_SPL_WORD32_MAX       (WebRtc_Word32)0x7fffffff
32 #define WEBRTC_SPL_WORD32_MIN       (WebRtc_Word32)0x80000000
33 #define WEBRTC_SPL_MAX_LPC_ORDER    14
34 #define WEBRTC_SPL_MAX_SEED_USED    0x80000000L
35 #define WEBRTC_SPL_MIN(A, B)        (A < B ? A : B) // Get min value
36 #define WEBRTC_SPL_MAX(A, B)        (A > B ? A : B) // Get max value
37 #define WEBRTC_SPL_ABS_W16(a) \
38     (((WebRtc_Word16)a >= 0) ? ((WebRtc_Word16)a) : -((WebRtc_Word16)a))
39 #define WEBRTC_SPL_ABS_W32(a) \
40     (((WebRtc_Word32)a >= 0) ? ((WebRtc_Word32)a) : -((WebRtc_Word32)a))
41 
42 #if (defined WEBRTC_TARGET_PC)||(defined __TARGET_XSCALE)
43 #define WEBRTC_SPL_GET_BYTE(a, nr)  (((WebRtc_Word8 *)a)[nr])
44 #define WEBRTC_SPL_SET_BYTE(d_ptr, val, index) \
45     (((WebRtc_Word8 *)d_ptr)[index] = (val))
46 #elif defined WEBRTC_BIG_ENDIAN
47 #define WEBRTC_SPL_GET_BYTE(a, nr) \
48     ((((WebRtc_Word16 *)a)[nr >> 1]) >> (((nr + 1) & 0x1) * 8) & 0x00ff)
49 #define WEBRTC_SPL_SET_BYTE(d_ptr, val, index) \
50     ((WebRtc_Word16 *)d_ptr)[index >> 1] = \
51     ((((WebRtc_Word16 *)d_ptr)[index >> 1]) \
52     & (0x00ff << (8 * ((index) & 0x1)))) | (val << (8 * ((index + 1) & 0x1)))
53 #else
54 #define WEBRTC_SPL_GET_BYTE(a,nr) \
55     ((((WebRtc_Word16 *)(a))[(nr) >> 1]) >> (((nr) & 0x1) * 8) & 0x00ff)
56 #define WEBRTC_SPL_SET_BYTE(d_ptr, val, index) \
57     ((WebRtc_Word16 *)(d_ptr))[(index) >> 1] = \
58     ((((WebRtc_Word16 *)(d_ptr))[(index) >> 1]) \
59     & (0x00ff << (8 * (((index) + 1) & 0x1)))) | \
60     ((val) << (8 * ((index) & 0x1)))
61 #endif
62 
63 #define WEBRTC_SPL_MUL(a, b) \
64     ((WebRtc_Word32) ((WebRtc_Word32)(a) * (WebRtc_Word32)(b)))
65 #define WEBRTC_SPL_UMUL(a, b) \
66     ((WebRtc_UWord32) ((WebRtc_UWord32)(a) * (WebRtc_UWord32)(b)))
67 #define WEBRTC_SPL_UMUL_RSFT16(a, b) \
68     ((WebRtc_UWord32) ((WebRtc_UWord32)(a) * (WebRtc_UWord32)(b)) >> 16)
69 #define WEBRTC_SPL_UMUL_16_16(a, b) \
70     ((WebRtc_UWord32) (WebRtc_UWord16)(a) * (WebRtc_UWord16)(b))
71 #define WEBRTC_SPL_UMUL_16_16_RSFT16(a, b) \
72     (((WebRtc_UWord32) (WebRtc_UWord16)(a) * (WebRtc_UWord16)(b)) >> 16)
73 #define WEBRTC_SPL_UMUL_32_16(a, b) \
74     ((WebRtc_UWord32) ((WebRtc_UWord32)(a) * (WebRtc_UWord16)(b)))
75 #define WEBRTC_SPL_UMUL_32_16_RSFT16(a, b) \
76     ((WebRtc_UWord32) ((WebRtc_UWord32)(a) * (WebRtc_UWord16)(b)) >> 16)
77 #define WEBRTC_SPL_MUL_16_U16(a, b) \
78     ((WebRtc_Word32)(WebRtc_Word16)(a) * (WebRtc_UWord16)(b))
79 #define WEBRTC_SPL_DIV(a, b) \
80     ((WebRtc_Word32) ((WebRtc_Word32)(a) / (WebRtc_Word32)(b)))
81 #define WEBRTC_SPL_UDIV(a, b) \
82     ((WebRtc_UWord32) ((WebRtc_UWord32)(a) / (WebRtc_UWord32)(b)))
83 
84 #ifndef WEBRTC_ARCH_ARM_V7A
85 // For ARMv7 platforms, these are inline functions in spl_inl_armv7.h
86 #define WEBRTC_SPL_MUL_16_16(a, b) \
87     ((WebRtc_Word32) (((WebRtc_Word16)(a)) * ((WebRtc_Word16)(b))))
88 #define WEBRTC_SPL_MUL_16_32_RSFT16(a, b) \
89     (WEBRTC_SPL_MUL_16_16(a, b >> 16) \
90      + ((WEBRTC_SPL_MUL_16_16(a, (b & 0xffff) >> 1) + 0x4000) >> 15))
91 #define WEBRTC_SPL_MUL_32_32_RSFT32(a32a, a32b, b32) \
92     ((WebRtc_Word32)(WEBRTC_SPL_MUL_16_32_RSFT16(a32a, b32) \
93     + (WEBRTC_SPL_MUL_16_32_RSFT16(a32b, b32) >> 16)))
94 #define WEBRTC_SPL_MUL_32_32_RSFT32BI(a32, b32) \
95     ((WebRtc_Word32)(WEBRTC_SPL_MUL_16_32_RSFT16(( \
96     (WebRtc_Word16)(a32 >> 16)), b32) + \
97     (WEBRTC_SPL_MUL_16_32_RSFT16(( \
98     (WebRtc_Word16)((a32 & 0x0000FFFF) >> 1)), b32) >> 15)))
99 #endif
100 
101 #define WEBRTC_SPL_MUL_16_32_RSFT11(a, b) \
102     ((WEBRTC_SPL_MUL_16_16(a, (b) >> 16) << 5) \
103     + (((WEBRTC_SPL_MUL_16_U16(a, (WebRtc_UWord16)(b)) >> 1) + 0x0200) >> 10))
104 #define WEBRTC_SPL_MUL_16_32_RSFT14(a, b) \
105     ((WEBRTC_SPL_MUL_16_16(a, (b) >> 16) << 2) \
106     + (((WEBRTC_SPL_MUL_16_U16(a, (WebRtc_UWord16)(b)) >> 1) + 0x1000) >> 13))
107 #define WEBRTC_SPL_MUL_16_32_RSFT15(a, b) \
108     ((WEBRTC_SPL_MUL_16_16(a, (b) >> 16) << 1) \
109     + (((WEBRTC_SPL_MUL_16_U16(a, (WebRtc_UWord16)(b)) >> 1) + 0x2000) >> 14))
110 
111 #ifdef ARM_WINM
112 #define WEBRTC_SPL_MUL_16_16(a, b) \
113     _SmulLo_SW_SL((WebRtc_Word16)(a), (WebRtc_Word16)(b))
114 #endif
115 
116 #define WEBRTC_SPL_MUL_16_16_RSFT(a, b, c) \
117     (WEBRTC_SPL_MUL_16_16(a, b) >> (c))
118 
119 #define WEBRTC_SPL_MUL_16_16_RSFT_WITH_ROUND(a, b, c) \
120     ((WEBRTC_SPL_MUL_16_16(a, b) + ((WebRtc_Word32) \
121                                   (((WebRtc_Word32)1) << ((c) - 1)))) >> (c))
122 #define WEBRTC_SPL_MUL_16_16_RSFT_WITH_FIXROUND(a, b) \
123     ((WEBRTC_SPL_MUL_16_16(a, b) + ((WebRtc_Word32) (1 << 14))) >> 15)
124 
125 // C + the 32 most significant bits of A * B
126 #define WEBRTC_SPL_SCALEDIFF32(A, B, C) \
127     (C + (B >> 16) * A + (((WebRtc_UWord32)(0x0000FFFF & B) * A) >> 16))
128 
129 #define WEBRTC_SPL_ADD_SAT_W32(a, b)    WebRtcSpl_AddSatW32(a, b)
130 #define WEBRTC_SPL_SAT(a, b, c)         (b > a ? a : b < c ? c : b)
131 #define WEBRTC_SPL_MUL_32_16(a, b)      ((a) * (b))
132 
133 #define WEBRTC_SPL_SUB_SAT_W32(a, b)    WebRtcSpl_SubSatW32(a, b)
134 #define WEBRTC_SPL_ADD_SAT_W16(a, b)    WebRtcSpl_AddSatW16(a, b)
135 #define WEBRTC_SPL_SUB_SAT_W16(a, b)    WebRtcSpl_SubSatW16(a, b)
136 
137 // We cannot do casting here due to signed/unsigned problem
138 #define WEBRTC_SPL_IS_NEG(a)            ((a) & 0x80000000)
139 // Shifting with negative numbers allowed
140 // Positive means left shift
141 #define WEBRTC_SPL_SHIFT_W16(x, c) \
142     (((c) >= 0) ? ((x) << (c)) : ((x) >> (-(c))))
143 #define WEBRTC_SPL_SHIFT_W32(x, c) \
144     (((c) >= 0) ? ((x) << (c)) : ((x) >> (-(c))))
145 
146 // Shifting with negative numbers not allowed
147 // We cannot do casting here due to signed/unsigned problem
148 #define WEBRTC_SPL_RSHIFT_W16(x, c)     ((x) >> (c))
149 #define WEBRTC_SPL_LSHIFT_W16(x, c)     ((x) << (c))
150 #define WEBRTC_SPL_RSHIFT_W32(x, c)     ((x) >> (c))
151 #define WEBRTC_SPL_LSHIFT_W32(x, c)     ((x) << (c))
152 
153 #define WEBRTC_SPL_RSHIFT_U16(x, c)     ((WebRtc_UWord16)(x) >> (c))
154 #define WEBRTC_SPL_LSHIFT_U16(x, c)     ((WebRtc_UWord16)(x) << (c))
155 #define WEBRTC_SPL_RSHIFT_U32(x, c)     ((WebRtc_UWord32)(x) >> (c))
156 #define WEBRTC_SPL_LSHIFT_U32(x, c)     ((WebRtc_UWord32)(x) << (c))
157 
158 #define WEBRTC_SPL_VNEW(t, n)           (t *) malloc (sizeof (t) * (n))
159 #define WEBRTC_SPL_FREE                 free
160 
161 #define WEBRTC_SPL_RAND(a) \
162     ((WebRtc_Word16)(WEBRTC_SPL_MUL_16_16_RSFT((a), 18816, 7) & 0x00007fff))
163 
164 #ifdef __cplusplus
165 extern "C"
166 {
167 #endif
168 
169 #define WEBRTC_SPL_MEMCPY_W8(v1, v2, length) \
170    memcpy(v1, v2, (length) * sizeof(char))
171 #define WEBRTC_SPL_MEMCPY_W16(v1, v2, length) \
172    memcpy(v1, v2, (length) * sizeof(WebRtc_Word16))
173 
174 #define WEBRTC_SPL_MEMMOVE_W16(v1, v2, length) \
175    memmove(v1, v2, (length) * sizeof(WebRtc_Word16))
176 
177 // inline functions:
178 #include "spl_inl.h"
179 
180 // Get SPL Version
181 WebRtc_Word16 WebRtcSpl_get_version(char* version,
182                                     WebRtc_Word16 length_in_bytes);
183 
184 int WebRtcSpl_GetScalingSquare(WebRtc_Word16* in_vector,
185                                int in_vector_length,
186                                int times);
187 
188 // Copy and set operations. Implementation in copy_set_operations.c.
189 // Descriptions at bottom of file.
190 void WebRtcSpl_MemSetW16(WebRtc_Word16* vector,
191                          WebRtc_Word16 set_value,
192                          int vector_length);
193 void WebRtcSpl_MemSetW32(WebRtc_Word32* vector,
194                          WebRtc_Word32 set_value,
195                          int vector_length);
196 void WebRtcSpl_MemCpyReversedOrder(WebRtc_Word16* out_vector,
197                                    WebRtc_Word16* in_vector,
198                                    int vector_length);
199 WebRtc_Word16 WebRtcSpl_CopyFromEndW16(G_CONST WebRtc_Word16* in_vector,
200                                        WebRtc_Word16 in_vector_length,
201                                        WebRtc_Word16 samples,
202                                        WebRtc_Word16* out_vector);
203 WebRtc_Word16 WebRtcSpl_ZerosArrayW16(WebRtc_Word16* vector,
204                                       WebRtc_Word16 vector_length);
205 WebRtc_Word16 WebRtcSpl_ZerosArrayW32(WebRtc_Word32* vector,
206                                       WebRtc_Word16 vector_length);
207 WebRtc_Word16 WebRtcSpl_OnesArrayW16(WebRtc_Word16* vector,
208                                      WebRtc_Word16 vector_length);
209 WebRtc_Word16 WebRtcSpl_OnesArrayW32(WebRtc_Word32* vector,
210                                      WebRtc_Word16 vector_length);
211 // End: Copy and set operations.
212 
213 // Minimum and maximum operations. Implementation in min_max_operations.c.
214 // Descriptions at bottom of file.
215 WebRtc_Word16 WebRtcSpl_MaxAbsValueW16(const WebRtc_Word16* vector,
216                                        WebRtc_Word16 length);
217 WebRtc_Word32 WebRtcSpl_MaxAbsValueW32(G_CONST WebRtc_Word32* vector,
218                                        WebRtc_Word16 length);
219 WebRtc_Word16 WebRtcSpl_MinValueW16(G_CONST WebRtc_Word16* vector,
220                                     WebRtc_Word16 length);
221 WebRtc_Word32 WebRtcSpl_MinValueW32(G_CONST WebRtc_Word32* vector,
222                                     WebRtc_Word16 length);
223 WebRtc_Word16 WebRtcSpl_MaxValueW16(G_CONST WebRtc_Word16* vector,
224                                     WebRtc_Word16 length);
225 
226 WebRtc_Word16 WebRtcSpl_MaxAbsIndexW16(G_CONST WebRtc_Word16* vector,
227                                        WebRtc_Word16 length);
228 WebRtc_Word32 WebRtcSpl_MaxValueW32(G_CONST WebRtc_Word32* vector,
229                                     WebRtc_Word16 length);
230 WebRtc_Word16 WebRtcSpl_MinIndexW16(G_CONST WebRtc_Word16* vector,
231                                     WebRtc_Word16 length);
232 WebRtc_Word16 WebRtcSpl_MinIndexW32(G_CONST WebRtc_Word32* vector,
233                                     WebRtc_Word16 length);
234 WebRtc_Word16 WebRtcSpl_MaxIndexW16(G_CONST WebRtc_Word16* vector,
235                                     WebRtc_Word16 length);
236 WebRtc_Word16 WebRtcSpl_MaxIndexW32(G_CONST WebRtc_Word32* vector,
237                                     WebRtc_Word16 length);
238 // End: Minimum and maximum operations.
239 
240 // Vector scaling operations. Implementation in vector_scaling_operations.c.
241 // Description at bottom of file.
242 void WebRtcSpl_VectorBitShiftW16(WebRtc_Word16* out_vector,
243                                  WebRtc_Word16 vector_length,
244                                  G_CONST WebRtc_Word16* in_vector,
245                                  WebRtc_Word16 right_shifts);
246 void WebRtcSpl_VectorBitShiftW32(WebRtc_Word32* out_vector,
247                                  WebRtc_Word16 vector_length,
248                                  G_CONST WebRtc_Word32* in_vector,
249                                  WebRtc_Word16 right_shifts);
250 void WebRtcSpl_VectorBitShiftW32ToW16(WebRtc_Word16* out_vector,
251                                       WebRtc_Word16 vector_length,
252                                       G_CONST WebRtc_Word32* in_vector,
253                                       WebRtc_Word16 right_shifts);
254 
255 void WebRtcSpl_ScaleVector(G_CONST WebRtc_Word16* in_vector,
256                            WebRtc_Word16* out_vector,
257                            WebRtc_Word16 gain,
258                            WebRtc_Word16 vector_length,
259                            WebRtc_Word16 right_shifts);
260 void WebRtcSpl_ScaleVectorWithSat(G_CONST WebRtc_Word16* in_vector,
261                                   WebRtc_Word16* out_vector,
262                                   WebRtc_Word16 gain,
263                                   WebRtc_Word16 vector_length,
264                                   WebRtc_Word16 right_shifts);
265 void WebRtcSpl_ScaleAndAddVectors(G_CONST WebRtc_Word16* in_vector1,
266                                   WebRtc_Word16 gain1, int right_shifts1,
267                                   G_CONST WebRtc_Word16* in_vector2,
268                                   WebRtc_Word16 gain2, int right_shifts2,
269                                   WebRtc_Word16* out_vector,
270                                   int vector_length);
271 // End: Vector scaling operations.
272 
273 // iLBC specific functions. Implementations in ilbc_specific_functions.c.
274 // Description at bottom of file.
275 void WebRtcSpl_ScaleAndAddVectorsWithRound(WebRtc_Word16* in_vector1,
276                                            WebRtc_Word16 scale1,
277                                            WebRtc_Word16* in_vector2,
278                                            WebRtc_Word16 scale2,
279                                            WebRtc_Word16 right_shifts,
280                                            WebRtc_Word16* out_vector,
281                                            WebRtc_Word16 vector_length);
282 void WebRtcSpl_ReverseOrderMultArrayElements(WebRtc_Word16* out_vector,
283                                              G_CONST WebRtc_Word16* in_vector,
284                                              G_CONST WebRtc_Word16* window,
285                                              WebRtc_Word16 vector_length,
286                                              WebRtc_Word16 right_shifts);
287 void WebRtcSpl_ElementwiseVectorMult(WebRtc_Word16* out_vector,
288                                      G_CONST WebRtc_Word16* in_vector,
289                                      G_CONST WebRtc_Word16* window,
290                                      WebRtc_Word16 vector_length,
291                                      WebRtc_Word16 right_shifts);
292 void WebRtcSpl_AddVectorsAndShift(WebRtc_Word16* out_vector,
293                                   G_CONST WebRtc_Word16* in_vector1,
294                                   G_CONST WebRtc_Word16* in_vector2,
295                                   WebRtc_Word16 vector_length,
296                                   WebRtc_Word16 right_shifts);
297 void WebRtcSpl_AddAffineVectorToVector(WebRtc_Word16* out_vector,
298                                        WebRtc_Word16* in_vector,
299                                        WebRtc_Word16 gain,
300                                        WebRtc_Word32 add_constant,
301                                        WebRtc_Word16 right_shifts,
302                                        int vector_length);
303 void WebRtcSpl_AffineTransformVector(WebRtc_Word16* out_vector,
304                                      WebRtc_Word16* in_vector,
305                                      WebRtc_Word16 gain,
306                                      WebRtc_Word32 add_constant,
307                                      WebRtc_Word16 right_shifts,
308                                      int vector_length);
309 // End: iLBC specific functions.
310 
311 // Signal processing operations. Descriptions at bottom of this file.
312 int WebRtcSpl_AutoCorrelation(G_CONST WebRtc_Word16* vector,
313                               int vector_length, int order,
314                               WebRtc_Word32* result_vector,
315                               int* scale);
316 WebRtc_Word16 WebRtcSpl_LevinsonDurbin(WebRtc_Word32* auto_corr,
317                                        WebRtc_Word16* lpc_coef,
318                                        WebRtc_Word16* refl_coef,
319                                        WebRtc_Word16 order);
320 void WebRtcSpl_ReflCoefToLpc(G_CONST WebRtc_Word16* refl_coef,
321                              int use_order,
322                              WebRtc_Word16* lpc_coef);
323 void WebRtcSpl_LpcToReflCoef(WebRtc_Word16* lpc_coef,
324                              int use_order,
325                              WebRtc_Word16* refl_coef);
326 void WebRtcSpl_AutoCorrToReflCoef(G_CONST WebRtc_Word32* auto_corr,
327                                   int use_order,
328                                   WebRtc_Word16* refl_coef);
329 void WebRtcSpl_CrossCorrelation(WebRtc_Word32* cross_corr,
330                                 WebRtc_Word16* vector1,
331                                 WebRtc_Word16* vector2,
332                                 WebRtc_Word16 dim_vector,
333                                 WebRtc_Word16 dim_cross_corr,
334                                 WebRtc_Word16 right_shifts,
335                                 WebRtc_Word16 step_vector2);
336 void WebRtcSpl_GetHanningWindow(WebRtc_Word16* window, WebRtc_Word16 size);
337 void WebRtcSpl_SqrtOfOneMinusXSquared(WebRtc_Word16* in_vector,
338                                       int vector_length,
339                                       WebRtc_Word16* out_vector);
340 // End: Signal processing operations.
341 
342 // Randomization functions. Implementations collected in randomization_functions.c and
343 // descriptions at bottom of this file.
344 WebRtc_UWord32 WebRtcSpl_IncreaseSeed(WebRtc_UWord32* seed);
345 WebRtc_Word16 WebRtcSpl_RandU(WebRtc_UWord32* seed);
346 WebRtc_Word16 WebRtcSpl_RandN(WebRtc_UWord32* seed);
347 WebRtc_Word16 WebRtcSpl_RandUArray(WebRtc_Word16* vector,
348                                    WebRtc_Word16 vector_length,
349                                    WebRtc_UWord32* seed);
350 // End: Randomization functions.
351 
352 // Math functions
353 WebRtc_Word32 WebRtcSpl_Sqrt(WebRtc_Word32 value);
354 WebRtc_Word32 WebRtcSpl_SqrtFloor(WebRtc_Word32 value);
355 
356 // Divisions. Implementations collected in division_operations.c and
357 // descriptions at bottom of this file.
358 WebRtc_UWord32 WebRtcSpl_DivU32U16(WebRtc_UWord32 num, WebRtc_UWord16 den);
359 WebRtc_Word32 WebRtcSpl_DivW32W16(WebRtc_Word32 num, WebRtc_Word16 den);
360 WebRtc_Word16 WebRtcSpl_DivW32W16ResW16(WebRtc_Word32 num, WebRtc_Word16 den);
361 WebRtc_Word32 WebRtcSpl_DivResultInQ31(WebRtc_Word32 num, WebRtc_Word32 den);
362 WebRtc_Word32 WebRtcSpl_DivW32HiLow(WebRtc_Word32 num, WebRtc_Word16 den_hi,
363                                     WebRtc_Word16 den_low);
364 // End: Divisions.
365 
366 WebRtc_Word32 WebRtcSpl_Energy(WebRtc_Word16* vector,
367                                int vector_length,
368                                int* scale_factor);
369 
370 WebRtc_Word32 WebRtcSpl_DotProductWithScale(WebRtc_Word16* vector1,
371                                             WebRtc_Word16* vector2,
372                                             int vector_length,
373                                             int scaling);
374 
375 // Filter operations.
376 int WebRtcSpl_FilterAR(G_CONST WebRtc_Word16* ar_coef, int ar_coef_length,
377                        G_CONST WebRtc_Word16* in_vector, int in_vector_length,
378                        WebRtc_Word16* filter_state, int filter_state_length,
379                        WebRtc_Word16* filter_state_low,
380                        int filter_state_low_length, WebRtc_Word16* out_vector,
381                        WebRtc_Word16* out_vector_low, int out_vector_low_length);
382 
383 void WebRtcSpl_FilterMAFastQ12(WebRtc_Word16* in_vector,
384                                WebRtc_Word16* out_vector,
385                                WebRtc_Word16* ma_coef,
386                                WebRtc_Word16 ma_coef_length,
387                                WebRtc_Word16 vector_length);
388 
389 // Performs a AR filtering on a vector in Q12
390 // Input:
391 //      - data_in            : Input samples
392 //      - data_out           : State information in positions
393 //                               data_out[-order] .. data_out[-1]
394 //      - coefficients       : Filter coefficients (in Q12)
395 //      - coefficients_length: Number of coefficients (order+1)
396 //      - data_length        : Number of samples to be filtered
397 // Output:
398 //      - data_out           : Filtered samples
399 void WebRtcSpl_FilterARFastQ12(const int16_t* data_in,
400                                int16_t* data_out,
401                                const int16_t* __restrict coefficients,
402                                int coefficients_length,
403                                int data_length);
404 
405 // Performs a MA down sampling filter on a vector
406 // Input:
407 //      - data_in            : Input samples (state in positions
408 //                               data_in[-order] .. data_in[-1])
409 //      - data_in_length     : Number of samples in |data_in| to be filtered.
410 //                               This must be at least
411 //                               |delay| + |factor|*(|out_vector_length|-1) + 1)
412 //      - data_out_length    : Number of down sampled samples desired
413 //      - coefficients       : Filter coefficients (in Q12)
414 //      - coefficients_length: Number of coefficients (order+1)
415 //      - factor             : Decimation factor
416 //      - delay              : Delay of filter (compensated for in out_vector)
417 // Output:
418 //      - data_out           : Filtered samples
419 // Return value              : 0 if OK, -1 if |in_vector| is too short
420 int WebRtcSpl_DownsampleFast(const int16_t* data_in,
421                              int data_in_length,
422                              int16_t* data_out,
423                              int data_out_length,
424                              const int16_t* __restrict coefficients,
425                              int coefficients_length,
426                              int factor,
427                              int delay);
428 
429 // End: Filter operations.
430 
431 // FFT operations
432 
433 int WebRtcSpl_ComplexFFT(WebRtc_Word16 vector[], int stages, int mode);
434 int WebRtcSpl_ComplexIFFT(WebRtc_Word16 vector[], int stages, int mode);
435 
436 // Treat a 16-bit complex data buffer |complex_data| as an array of 32-bit
437 // values, and swap elements whose indexes are bit-reverses of each other.
438 //
439 // Input:
440 //      - complex_data  : Complex data buffer containing 2^|stages| real
441 //                        elements interleaved with 2^|stages| imaginary
442 //                        elements: [Re Im Re Im Re Im....]
443 //      - stages        : Number of FFT stages. Must be at least 3 and at most
444 //                        10, since the table WebRtcSpl_kSinTable1024[] is 1024
445 //                        elements long.
446 //
447 // Output:
448 //      - complex_data  : The complex data buffer.
449 
450 void WebRtcSpl_ComplexBitReverse(int16_t* __restrict complex_data, int stages);
451 
452 // End: FFT operations
453 
454 /************************************************************
455  *
456  * RESAMPLING FUNCTIONS AND THEIR STRUCTS ARE DEFINED BELOW
457  *
458  ************************************************************/
459 
460 /*******************************************************************
461  * resample.c
462  *
463  * Includes the following resampling combinations
464  * 22 kHz -> 16 kHz
465  * 16 kHz -> 22 kHz
466  * 22 kHz ->  8 kHz
467  *  8 kHz -> 22 kHz
468  *
469  ******************************************************************/
470 
471 // state structure for 22 -> 16 resampler
472 typedef struct
473 {
474     WebRtc_Word32 S_22_44[8];
475     WebRtc_Word32 S_44_32[8];
476     WebRtc_Word32 S_32_16[8];
477 } WebRtcSpl_State22khzTo16khz;
478 
479 void WebRtcSpl_Resample22khzTo16khz(const WebRtc_Word16* in,
480                                     WebRtc_Word16* out,
481                                     WebRtcSpl_State22khzTo16khz* state,
482                                     WebRtc_Word32* tmpmem);
483 
484 void WebRtcSpl_ResetResample22khzTo16khz(WebRtcSpl_State22khzTo16khz* state);
485 
486 // state structure for 16 -> 22 resampler
487 typedef struct
488 {
489     WebRtc_Word32 S_16_32[8];
490     WebRtc_Word32 S_32_22[8];
491 } WebRtcSpl_State16khzTo22khz;
492 
493 void WebRtcSpl_Resample16khzTo22khz(const WebRtc_Word16* in,
494                                     WebRtc_Word16* out,
495                                     WebRtcSpl_State16khzTo22khz* state,
496                                     WebRtc_Word32* tmpmem);
497 
498 void WebRtcSpl_ResetResample16khzTo22khz(WebRtcSpl_State16khzTo22khz* state);
499 
500 // state structure for 22 -> 8 resampler
501 typedef struct
502 {
503     WebRtc_Word32 S_22_22[16];
504     WebRtc_Word32 S_22_16[8];
505     WebRtc_Word32 S_16_8[8];
506 } WebRtcSpl_State22khzTo8khz;
507 
508 void WebRtcSpl_Resample22khzTo8khz(const WebRtc_Word16* in, WebRtc_Word16* out,
509                                    WebRtcSpl_State22khzTo8khz* state,
510                                    WebRtc_Word32* tmpmem);
511 
512 void WebRtcSpl_ResetResample22khzTo8khz(WebRtcSpl_State22khzTo8khz* state);
513 
514 // state structure for 8 -> 22 resampler
515 typedef struct
516 {
517     WebRtc_Word32 S_8_16[8];
518     WebRtc_Word32 S_16_11[8];
519     WebRtc_Word32 S_11_22[8];
520 } WebRtcSpl_State8khzTo22khz;
521 
522 void WebRtcSpl_Resample8khzTo22khz(const WebRtc_Word16* in, WebRtc_Word16* out,
523                                    WebRtcSpl_State8khzTo22khz* state,
524                                    WebRtc_Word32* tmpmem);
525 
526 void WebRtcSpl_ResetResample8khzTo22khz(WebRtcSpl_State8khzTo22khz* state);
527 
528 /*******************************************************************
529  * resample_fractional.c
530  * Functions for internal use in the other resample functions
531  *
532  * Includes the following resampling combinations
533  * 48 kHz -> 32 kHz
534  * 32 kHz -> 24 kHz
535  * 44 kHz -> 32 kHz
536  *
537  ******************************************************************/
538 
539 void WebRtcSpl_Resample48khzTo32khz(const WebRtc_Word32* In, WebRtc_Word32* Out,
540                                     const WebRtc_Word32 K);
541 
542 void WebRtcSpl_Resample32khzTo24khz(const WebRtc_Word32* In, WebRtc_Word32* Out,
543                                     const WebRtc_Word32 K);
544 
545 void WebRtcSpl_Resample44khzTo32khz(const WebRtc_Word32* In, WebRtc_Word32* Out,
546                                     const WebRtc_Word32 K);
547 
548 /*******************************************************************
549  * resample_48khz.c
550  *
551  * Includes the following resampling combinations
552  * 48 kHz -> 16 kHz
553  * 16 kHz -> 48 kHz
554  * 48 kHz ->  8 kHz
555  *  8 kHz -> 48 kHz
556  *
557  ******************************************************************/
558 
559 typedef struct
560 {
561     WebRtc_Word32 S_48_48[16];
562     WebRtc_Word32 S_48_32[8];
563     WebRtc_Word32 S_32_16[8];
564 } WebRtcSpl_State48khzTo16khz;
565 
566 void WebRtcSpl_Resample48khzTo16khz(const WebRtc_Word16* in, WebRtc_Word16* out,
567                                     WebRtcSpl_State48khzTo16khz* state,
568                                     WebRtc_Word32* tmpmem);
569 
570 void WebRtcSpl_ResetResample48khzTo16khz(WebRtcSpl_State48khzTo16khz* state);
571 
572 typedef struct
573 {
574     WebRtc_Word32 S_16_32[8];
575     WebRtc_Word32 S_32_24[8];
576     WebRtc_Word32 S_24_48[8];
577 } WebRtcSpl_State16khzTo48khz;
578 
579 void WebRtcSpl_Resample16khzTo48khz(const WebRtc_Word16* in, WebRtc_Word16* out,
580                                     WebRtcSpl_State16khzTo48khz* state,
581                                     WebRtc_Word32* tmpmem);
582 
583 void WebRtcSpl_ResetResample16khzTo48khz(WebRtcSpl_State16khzTo48khz* state);
584 
585 typedef struct
586 {
587     WebRtc_Word32 S_48_24[8];
588     WebRtc_Word32 S_24_24[16];
589     WebRtc_Word32 S_24_16[8];
590     WebRtc_Word32 S_16_8[8];
591 } WebRtcSpl_State48khzTo8khz;
592 
593 void WebRtcSpl_Resample48khzTo8khz(const WebRtc_Word16* in, WebRtc_Word16* out,
594                                    WebRtcSpl_State48khzTo8khz* state,
595                                    WebRtc_Word32* tmpmem);
596 
597 void WebRtcSpl_ResetResample48khzTo8khz(WebRtcSpl_State48khzTo8khz* state);
598 
599 typedef struct
600 {
601     WebRtc_Word32 S_8_16[8];
602     WebRtc_Word32 S_16_12[8];
603     WebRtc_Word32 S_12_24[8];
604     WebRtc_Word32 S_24_48[8];
605 } WebRtcSpl_State8khzTo48khz;
606 
607 void WebRtcSpl_Resample8khzTo48khz(const WebRtc_Word16* in, WebRtc_Word16* out,
608                                    WebRtcSpl_State8khzTo48khz* state,
609                                    WebRtc_Word32* tmpmem);
610 
611 void WebRtcSpl_ResetResample8khzTo48khz(WebRtcSpl_State8khzTo48khz* state);
612 
613 /*******************************************************************
614  * resample_by_2.c
615  *
616  * Includes down and up sampling by a factor of two.
617  *
618  ******************************************************************/
619 
620 void WebRtcSpl_DownsampleBy2(const WebRtc_Word16* in, const WebRtc_Word16 len,
621                              WebRtc_Word16* out, WebRtc_Word32* filtState);
622 
623 void WebRtcSpl_UpsampleBy2(const WebRtc_Word16* in, WebRtc_Word16 len, WebRtc_Word16* out,
624                            WebRtc_Word32* filtState);
625 
626 /************************************************************
627  * END OF RESAMPLING FUNCTIONS
628  ************************************************************/
629 void WebRtcSpl_AnalysisQMF(const WebRtc_Word16* in_data,
630                            WebRtc_Word16* low_band,
631                            WebRtc_Word16* high_band,
632                            WebRtc_Word32* filter_state1,
633                            WebRtc_Word32* filter_state2);
634 void WebRtcSpl_SynthesisQMF(const WebRtc_Word16* low_band,
635                             const WebRtc_Word16* high_band,
636                             WebRtc_Word16* out_data,
637                             WebRtc_Word32* filter_state1,
638                             WebRtc_Word32* filter_state2);
639 
640 #ifdef __cplusplus
641 }
642 #endif // __cplusplus
643 #endif // WEBRTC_SPL_SIGNAL_PROCESSING_LIBRARY_H_
644 
645 //
646 // WebRtcSpl_AddSatW16(...)
647 // WebRtcSpl_AddSatW32(...)
648 //
649 // Returns the result of a saturated 16-bit, respectively 32-bit, addition of
650 // the numbers specified by the |var1| and |var2| parameters.
651 //
652 // Input:
653 //      - var1      : Input variable 1
654 //      - var2      : Input variable 2
655 //
656 // Return value     : Added and saturated value
657 //
658 
659 //
660 // WebRtcSpl_SubSatW16(...)
661 // WebRtcSpl_SubSatW32(...)
662 //
663 // Returns the result of a saturated 16-bit, respectively 32-bit, subtraction
664 // of the numbers specified by the |var1| and |var2| parameters.
665 //
666 // Input:
667 //      - var1      : Input variable 1
668 //      - var2      : Input variable 2
669 //
670 // Returned value   : Subtracted and saturated value
671 //
672 
673 //
674 // WebRtcSpl_GetSizeInBits(...)
675 //
676 // Returns the # of bits that are needed at the most to represent the number
677 // specified by the |value| parameter.
678 //
679 // Input:
680 //      - value     : Input value
681 //
682 // Return value     : Number of bits needed to represent |value|
683 //
684 
685 //
686 // WebRtcSpl_NormW32(...)
687 //
688 // Norm returns the # of left shifts required to 32-bit normalize the 32-bit
689 // signed number specified by the |value| parameter.
690 //
691 // Input:
692 //      - value     : Input value
693 //
694 // Return value     : Number of bit shifts needed to 32-bit normalize |value|
695 //
696 
697 //
698 // WebRtcSpl_NormW16(...)
699 //
700 // Norm returns the # of left shifts required to 16-bit normalize the 16-bit
701 // signed number specified by the |value| parameter.
702 //
703 // Input:
704 //      - value     : Input value
705 //
706 // Return value     : Number of bit shifts needed to 32-bit normalize |value|
707 //
708 
709 //
710 // WebRtcSpl_NormU32(...)
711 //
712 // Norm returns the # of left shifts required to 32-bit normalize the unsigned
713 // 32-bit number specified by the |value| parameter.
714 //
715 // Input:
716 //      - value     : Input value
717 //
718 // Return value     : Number of bit shifts needed to 32-bit normalize |value|
719 //
720 
721 //
722 // WebRtcSpl_GetScalingSquare(...)
723 //
724 // Returns the # of bits required to scale the samples specified in the
725 // |in_vector| parameter so that, if the squares of the samples are added the
726 // # of times specified by the |times| parameter, the 32-bit addition will not
727 // overflow (result in WebRtc_Word32).
728 //
729 // Input:
730 //      - in_vector         : Input vector to check scaling on
731 //      - in_vector_length  : Samples in |in_vector|
732 //      - times             : Number of additions to be performed
733 //
734 // Return value             : Number of right bit shifts needed to avoid
735 //                            overflow in the addition calculation
736 //
737 
738 //
739 // WebRtcSpl_MemSetW16(...)
740 //
741 // Sets all the values in the WebRtc_Word16 vector |vector| of length
742 // |vector_length| to the specified value |set_value|
743 //
744 // Input:
745 //      - vector        : Pointer to the WebRtc_Word16 vector
746 //      - set_value     : Value specified
747 //      - vector_length : Length of vector
748 //
749 
750 //
751 // WebRtcSpl_MemSetW32(...)
752 //
753 // Sets all the values in the WebRtc_Word32 vector |vector| of length
754 // |vector_length| to the specified value |set_value|
755 //
756 // Input:
757 //      - vector        : Pointer to the WebRtc_Word16 vector
758 //      - set_value     : Value specified
759 //      - vector_length : Length of vector
760 //
761 
762 //
763 // WebRtcSpl_MemCpyReversedOrder(...)
764 //
765 // Copies all the values from the source WebRtc_Word16 vector |in_vector| to a
766 // destination WebRtc_Word16 vector |out_vector|. It is done in reversed order,
767 // meaning that the first sample of |in_vector| is copied to the last sample of
768 // the |out_vector|. The procedure continues until the last sample of
769 // |in_vector| has been copied to the first sample of |out_vector|. This
770 // creates a reversed vector. Used in e.g. prediction in iLBC.
771 //
772 // Input:
773 //      - in_vector     : Pointer to the first sample in a WebRtc_Word16 vector
774 //                        of length |length|
775 //      - vector_length : Number of elements to copy
776 //
777 // Output:
778 //      - out_vector    : Pointer to the last sample in a WebRtc_Word16 vector
779 //                        of length |length|
780 //
781 
782 //
783 // WebRtcSpl_CopyFromEndW16(...)
784 //
785 // Copies the rightmost |samples| of |in_vector| (of length |in_vector_length|)
786 // to the vector |out_vector|.
787 //
788 // Input:
789 //      - in_vector         : Input vector
790 //      - in_vector_length  : Number of samples in |in_vector|
791 //      - samples           : Number of samples to extract (from right side)
792 //                            from |in_vector|
793 //
794 // Output:
795 //      - out_vector        : Vector with the requested samples
796 //
797 // Return value             : Number of copied samples in |out_vector|
798 //
799 
800 //
801 // WebRtcSpl_ZerosArrayW16(...)
802 // WebRtcSpl_ZerosArrayW32(...)
803 //
804 // Inserts the value "zero" in all positions of a w16 and a w32 vector
805 // respectively.
806 //
807 // Input:
808 //      - vector_length : Number of samples in vector
809 //
810 // Output:
811 //      - vector        : Vector containing all zeros
812 //
813 // Return value         : Number of samples in vector
814 //
815 
816 //
817 // WebRtcSpl_OnesArrayW16(...)
818 // WebRtcSpl_OnesArrayW32(...)
819 //
820 // Inserts the value "one" in all positions of a w16 and a w32 vector
821 // respectively.
822 //
823 // Input:
824 //      - vector_length : Number of samples in vector
825 //
826 // Output:
827 //      - vector        : Vector containing all ones
828 //
829 // Return value         : Number of samples in vector
830 //
831 
832 //
833 // WebRtcSpl_MinValueW16(...)
834 // WebRtcSpl_MinValueW32(...)
835 //
836 // Returns the minimum value of a vector
837 //
838 // Input:
839 //      - vector        : Input vector
840 //      - vector_length : Number of samples in vector
841 //
842 // Return value         : Minimum sample value in vector
843 //
844 
845 //
846 // WebRtcSpl_MaxValueW16(...)
847 // WebRtcSpl_MaxValueW32(...)
848 //
849 // Returns the maximum value of a vector
850 //
851 // Input:
852 //      - vector        : Input vector
853 //      - vector_length : Number of samples in vector
854 //
855 // Return value         : Maximum sample value in vector
856 //
857 
858 //
859 // WebRtcSpl_MaxAbsValueW16(...)
860 // WebRtcSpl_MaxAbsValueW32(...)
861 //
862 // Returns the largest absolute value of a vector
863 //
864 // Input:
865 //      - vector        : Input vector
866 //      - vector_length : Number of samples in vector
867 //
868 // Return value         : Maximum absolute value in vector
869 //
870 
871 //
872 // WebRtcSpl_MaxAbsIndexW16(...)
873 //
874 // Returns the vector index to the largest absolute value of a vector
875 //
876 // Input:
877 //      - vector        : Input vector
878 //      - vector_length : Number of samples in vector
879 //
880 // Return value         : Index to maximum absolute value in vector
881 //
882 
883 //
884 // WebRtcSpl_MinIndexW16(...)
885 // WebRtcSpl_MinIndexW32(...)
886 //
887 // Returns the vector index to the minimum sample value of a vector
888 //
889 // Input:
890 //      - vector        : Input vector
891 //      - vector_length : Number of samples in vector
892 //
893 // Return value         : Index to minimum sample value in vector
894 //
895 
896 //
897 // WebRtcSpl_MaxIndexW16(...)
898 // WebRtcSpl_MaxIndexW32(...)
899 //
900 // Returns the vector index to the maximum sample value of a vector
901 //
902 // Input:
903 //      - vector        : Input vector
904 //      - vector_length : Number of samples in vector
905 //
906 // Return value         : Index to maximum sample value in vector
907 //
908 
909 //
910 // WebRtcSpl_VectorBitShiftW16(...)
911 // WebRtcSpl_VectorBitShiftW32(...)
912 //
913 // Bit shifts all the values in a vector up or downwards. Different calls for
914 // WebRtc_Word16 and WebRtc_Word32 vectors respectively.
915 //
916 // Input:
917 //      - vector_length : Length of vector
918 //      - in_vector     : Pointer to the vector that should be bit shifted
919 //      - right_shifts  : Number of right bit shifts (negative value gives left
920 //                        shifts)
921 //
922 // Output:
923 //      - out_vector    : Pointer to the result vector (can be the same as
924 //                        |in_vector|)
925 //
926 
927 //
928 // WebRtcSpl_VectorBitShiftW32ToW16(...)
929 //
930 // Bit shifts all the values in a WebRtc_Word32 vector up or downwards and
931 // stores the result as a WebRtc_Word16 vector
932 //
933 // Input:
934 //      - vector_length : Length of vector
935 //      - in_vector     : Pointer to the vector that should be bit shifted
936 //      - right_shifts  : Number of right bit shifts (negative value gives left
937 //                        shifts)
938 //
939 // Output:
940 //      - out_vector    : Pointer to the result vector (can be the same as
941 //                        |in_vector|)
942 //
943 
944 //
945 // WebRtcSpl_ScaleVector(...)
946 //
947 // Performs the vector operation:
948 //  out_vector[k] = (gain*in_vector[k])>>right_shifts
949 //
950 // Input:
951 //      - in_vector     : Input vector
952 //      - gain          : Scaling gain
953 //      - vector_length : Elements in the |in_vector|
954 //      - right_shifts  : Number of right bit shifts applied
955 //
956 // Output:
957 //      - out_vector    : Output vector (can be the same as |in_vector|)
958 //
959 
960 //
961 // WebRtcSpl_ScaleVectorWithSat(...)
962 //
963 // Performs the vector operation:
964 //  out_vector[k] = SATURATE( (gain*in_vector[k])>>right_shifts )
965 //
966 // Input:
967 //      - in_vector     : Input vector
968 //      - gain          : Scaling gain
969 //      - vector_length : Elements in the |in_vector|
970 //      - right_shifts  : Number of right bit shifts applied
971 //
972 // Output:
973 //      - out_vector    : Output vector (can be the same as |in_vector|)
974 //
975 
976 //
977 // WebRtcSpl_ScaleAndAddVectors(...)
978 //
979 // Performs the vector operation:
980 //  out_vector[k] = (gain1*in_vector1[k])>>right_shifts1
981 //                  + (gain2*in_vector2[k])>>right_shifts2
982 //
983 // Input:
984 //      - in_vector1    : Input vector 1
985 //      - gain1         : Gain to be used for vector 1
986 //      - right_shifts1 : Right bit shift to be used for vector 1
987 //      - in_vector2    : Input vector 2
988 //      - gain2         : Gain to be used for vector 2
989 //      - right_shifts2 : Right bit shift to be used for vector 2
990 //      - vector_length : Elements in the input vectors
991 //
992 // Output:
993 //      - out_vector    : Output vector
994 //
995 
996 //
997 // WebRtcSpl_ScaleAndAddVectorsWithRound(...)
998 //
999 // Performs the vector operation:
1000 //
1001 //  out_vector[k] = ((scale1*in_vector1[k]) + (scale2*in_vector2[k])
1002 //                      + round_value) >> right_shifts
1003 //
1004 //      where:
1005 //
1006 //  round_value = (1<<right_shifts)>>1
1007 //
1008 // Input:
1009 //      - in_vector1    : Input vector 1
1010 //      - scale1        : Gain to be used for vector 1
1011 //      - in_vector2    : Input vector 2
1012 //      - scale2        : Gain to be used for vector 2
1013 //      - right_shifts  : Number of right bit shifts to be applied
1014 //      - vector_length : Number of elements in the input vectors
1015 //
1016 // Output:
1017 //      - out_vector    : Output vector
1018 //
1019 
1020 //
1021 // WebRtcSpl_ReverseOrderMultArrayElements(...)
1022 //
1023 // Performs the vector operation:
1024 //  out_vector[n] = (in_vector[n]*window[-n])>>right_shifts
1025 //
1026 // Input:
1027 //      - in_vector     : Input vector
1028 //      - window        : Window vector (should be reversed). The pointer
1029 //                        should be set to the last value in the vector
1030 //      - right_shifts  : Number of right bit shift to be applied after the
1031 //                        multiplication
1032 //      - vector_length : Number of elements in |in_vector|
1033 //
1034 // Output:
1035 //      - out_vector    : Output vector (can be same as |in_vector|)
1036 //
1037 
1038 //
1039 // WebRtcSpl_ElementwiseVectorMult(...)
1040 //
1041 // Performs the vector operation:
1042 //  out_vector[n] = (in_vector[n]*window[n])>>right_shifts
1043 //
1044 // Input:
1045 //      - in_vector     : Input vector
1046 //      - window        : Window vector.
1047 //      - right_shifts  : Number of right bit shift to be applied after the
1048 //                        multiplication
1049 //      - vector_length : Number of elements in |in_vector|
1050 //
1051 // Output:
1052 //      - out_vector    : Output vector (can be same as |in_vector|)
1053 //
1054 
1055 //
1056 // WebRtcSpl_AddVectorsAndShift(...)
1057 //
1058 // Performs the vector operation:
1059 //  out_vector[k] = (in_vector1[k] + in_vector2[k])>>right_shifts
1060 //
1061 // Input:
1062 //      - in_vector1    : Input vector 1
1063 //      - in_vector2    : Input vector 2
1064 //      - right_shifts  : Number of right bit shift to be applied after the
1065 //                        multiplication
1066 //      - vector_length : Number of elements in |in_vector1| and |in_vector2|
1067 //
1068 // Output:
1069 //      - out_vector    : Output vector (can be same as |in_vector1|)
1070 //
1071 
1072 //
1073 // WebRtcSpl_AddAffineVectorToVector(...)
1074 //
1075 // Adds an affine transformed vector to another vector |out_vector|, i.e,
1076 // performs
1077 //  out_vector[k] += (in_vector[k]*gain+add_constant)>>right_shifts
1078 //
1079 // Input:
1080 //      - in_vector     : Input vector
1081 //      - gain          : Gain value, used to multiply the in vector with
1082 //      - add_constant  : Constant value to add (usually 1<<(right_shifts-1),
1083 //                        but others can be used as well
1084 //      - right_shifts  : Number of right bit shifts (0-16)
1085 //      - vector_length : Number of samples in |in_vector| and |out_vector|
1086 //
1087 // Output:
1088 //      - out_vector    : Vector with the output
1089 //
1090 
1091 //
1092 // WebRtcSpl_AffineTransformVector(...)
1093 //
1094 // Affine transforms a vector, i.e, performs
1095 //  out_vector[k] = (in_vector[k]*gain+add_constant)>>right_shifts
1096 //
1097 // Input:
1098 //      - in_vector     : Input vector
1099 //      - gain          : Gain value, used to multiply the in vector with
1100 //      - add_constant  : Constant value to add (usually 1<<(right_shifts-1),
1101 //                        but others can be used as well
1102 //      - right_shifts  : Number of right bit shifts (0-16)
1103 //      - vector_length : Number of samples in |in_vector| and |out_vector|
1104 //
1105 // Output:
1106 //      - out_vector    : Vector with the output
1107 //
1108 
1109 //
1110 // WebRtcSpl_AutoCorrelation(...)
1111 //
1112 // A 32-bit fix-point implementation of auto-correlation computation
1113 //
1114 // Input:
1115 //      - vector        : Vector to calculate autocorrelation upon
1116 //      - vector_length : Length (in samples) of |vector|
1117 //      - order         : The order up to which the autocorrelation should be
1118 //                        calculated
1119 //
1120 // Output:
1121 //      - result_vector : auto-correlation values (values should be seen
1122 //                        relative to each other since the absolute values
1123 //                        might have been down shifted to avoid overflow)
1124 //
1125 //      - scale         : The number of left shifts required to obtain the
1126 //                        auto-correlation in Q0
1127 //
1128 // Return value         : Number of samples in |result_vector|, i.e., (order+1)
1129 //
1130 
1131 //
1132 // WebRtcSpl_LevinsonDurbin(...)
1133 //
1134 // A 32-bit fix-point implementation of the Levinson-Durbin algorithm that
1135 // does NOT use the 64 bit class
1136 //
1137 // Input:
1138 //      - auto_corr : Vector with autocorrelation values of length >=
1139 //                    |use_order|+1
1140 //      - use_order : The LPC filter order (support up to order 20)
1141 //
1142 // Output:
1143 //      - lpc_coef  : lpc_coef[0..use_order] LPC coefficients in Q12
1144 //      - refl_coef : refl_coef[0...use_order-1]| Reflection coefficients in
1145 //                    Q15
1146 //
1147 // Return value     : 1 for stable 0 for unstable
1148 //
1149 
1150 //
1151 // WebRtcSpl_ReflCoefToLpc(...)
1152 //
1153 // Converts reflection coefficients |refl_coef| to LPC coefficients |lpc_coef|.
1154 // This version is a 16 bit operation.
1155 //
1156 // NOTE: The 16 bit refl_coef -> lpc_coef conversion might result in a
1157 // "slightly unstable" filter (i.e., a pole just outside the unit circle) in
1158 // "rare" cases even if the reflection coefficients are stable.
1159 //
1160 // Input:
1161 //      - refl_coef : Reflection coefficients in Q15 that should be converted
1162 //                    to LPC coefficients
1163 //      - use_order : Number of coefficients in |refl_coef|
1164 //
1165 // Output:
1166 //      - lpc_coef  : LPC coefficients in Q12
1167 //
1168 
1169 //
1170 // WebRtcSpl_LpcToReflCoef(...)
1171 //
1172 // Converts LPC coefficients |lpc_coef| to reflection coefficients |refl_coef|.
1173 // This version is a 16 bit operation.
1174 // The conversion is implemented by the step-down algorithm.
1175 //
1176 // Input:
1177 //      - lpc_coef  : LPC coefficients in Q12, that should be converted to
1178 //                    reflection coefficients
1179 //      - use_order : Number of coefficients in |lpc_coef|
1180 //
1181 // Output:
1182 //      - refl_coef : Reflection coefficients in Q15.
1183 //
1184 
1185 //
1186 // WebRtcSpl_AutoCorrToReflCoef(...)
1187 //
1188 // Calculates reflection coefficients (16 bit) from auto-correlation values
1189 //
1190 // Input:
1191 //      - auto_corr : Auto-correlation values
1192 //      - use_order : Number of coefficients wanted be calculated
1193 //
1194 // Output:
1195 //      - refl_coef : Reflection coefficients in Q15.
1196 //
1197 
1198 //
1199 // WebRtcSpl_CrossCorrelation(...)
1200 //
1201 // Calculates the cross-correlation between two sequences |vector1| and
1202 // |vector2|. |vector1| is fixed and |vector2| slides as the pointer is
1203 // increased with the amount |step_vector2|
1204 //
1205 // Input:
1206 //      - vector1           : First sequence (fixed throughout the correlation)
1207 //      - vector2           : Second sequence (slides |step_vector2| for each
1208 //                            new correlation)
1209 //      - dim_vector        : Number of samples to use in the cross-correlation
1210 //      - dim_cross_corr    : Number of cross-correlations to calculate (the
1211 //                            start position for |vector2| is updated for each
1212 //                            new one)
1213 //      - right_shifts      : Number of right bit shifts to use. This will
1214 //                            become the output Q-domain.
1215 //      - step_vector2      : How many (positive or negative) steps the
1216 //                            |vector2| pointer should be updated for each new
1217 //                            cross-correlation value.
1218 //
1219 // Output:
1220 //      - cross_corr        : The cross-correlation in Q(-right_shifts)
1221 //
1222 
1223 //
1224 // WebRtcSpl_GetHanningWindow(...)
1225 //
1226 // Creates (the first half of) a Hanning window. Size must be at least 1 and
1227 // at most 512.
1228 //
1229 // Input:
1230 //      - size      : Length of the requested Hanning window (1 to 512)
1231 //
1232 // Output:
1233 //      - window    : Hanning vector in Q14.
1234 //
1235 
1236 //
1237 // WebRtcSpl_SqrtOfOneMinusXSquared(...)
1238 //
1239 // Calculates y[k] = sqrt(1 - x[k]^2) for each element of the input vector
1240 // |in_vector|. Input and output values are in Q15.
1241 //
1242 // Inputs:
1243 //      - in_vector     : Values to calculate sqrt(1 - x^2) of
1244 //      - vector_length : Length of vector |in_vector|
1245 //
1246 // Output:
1247 //      - out_vector    : Output values in Q15
1248 //
1249 
1250 //
1251 // WebRtcSpl_IncreaseSeed(...)
1252 //
1253 // Increases the seed (and returns the new value)
1254 //
1255 // Input:
1256 //      - seed      : Seed for random calculation
1257 //
1258 // Output:
1259 //      - seed      : Updated seed value
1260 //
1261 // Return value     : The new seed value
1262 //
1263 
1264 //
1265 // WebRtcSpl_RandU(...)
1266 //
1267 // Produces a uniformly distributed value in the WebRtc_Word16 range
1268 //
1269 // Input:
1270 //      - seed      : Seed for random calculation
1271 //
1272 // Output:
1273 //      - seed      : Updated seed value
1274 //
1275 // Return value     : Uniformly distributed value in the range
1276 //                    [Word16_MIN...Word16_MAX]
1277 //
1278 
1279 //
1280 // WebRtcSpl_RandN(...)
1281 //
1282 // Produces a normal distributed value in the WebRtc_Word16 range
1283 //
1284 // Input:
1285 //      - seed      : Seed for random calculation
1286 //
1287 // Output:
1288 //      - seed      : Updated seed value
1289 //
1290 // Return value     : N(0,1) value in the Q13 domain
1291 //
1292 
1293 //
1294 // WebRtcSpl_RandUArray(...)
1295 //
1296 // Produces a uniformly distributed vector with elements in the WebRtc_Word16
1297 // range
1298 //
1299 // Input:
1300 //      - vector_length : Samples wanted in the vector
1301 //      - seed          : Seed for random calculation
1302 //
1303 // Output:
1304 //      - vector        : Vector with the uniform values
1305 //      - seed          : Updated seed value
1306 //
1307 // Return value         : Number of samples in vector, i.e., |vector_length|
1308 //
1309 
1310 //
1311 // WebRtcSpl_Sqrt(...)
1312 //
1313 // Returns the square root of the input value |value|. The precision of this
1314 // function is integer precision, i.e., sqrt(8) gives 2 as answer.
1315 // If |value| is a negative number then 0 is returned.
1316 //
1317 // Algorithm:
1318 //
1319 // A sixth order Taylor Series expansion is used here to compute the square
1320 // root of a number y^0.5 = (1+x)^0.5
1321 // where
1322 // x = y-1
1323 //   = 1+(x/2)-0.5*((x/2)^2+0.5*((x/2)^3-0.625*((x/2)^4+0.875*((x/2)^5)
1324 // 0.5 <= x < 1
1325 //
1326 // Input:
1327 //      - value     : Value to calculate sqrt of
1328 //
1329 // Return value     : Result of the sqrt calculation
1330 //
1331 
1332 //
1333 // WebRtcSpl_SqrtFloor(...)
1334 //
1335 // Returns the square root of the input value |value|. The precision of this
1336 // function is rounding down integer precision, i.e., sqrt(8) gives 2 as answer.
1337 // If |value| is a negative number then 0 is returned.
1338 //
1339 // Algorithm:
1340 //
1341 // An iterative 4 cylce/bit routine
1342 //
1343 // Input:
1344 //      - value     : Value to calculate sqrt of
1345 //
1346 // Return value     : Result of the sqrt calculation
1347 //
1348 
1349 //
1350 // WebRtcSpl_DivU32U16(...)
1351 //
1352 // Divides a WebRtc_UWord32 |num| by a WebRtc_UWord16 |den|.
1353 //
1354 // If |den|==0, (WebRtc_UWord32)0xFFFFFFFF is returned.
1355 //
1356 // Input:
1357 //      - num       : Numerator
1358 //      - den       : Denominator
1359 //
1360 // Return value     : Result of the division (as a WebRtc_UWord32), i.e., the
1361 //                    integer part of num/den.
1362 //
1363 
1364 //
1365 // WebRtcSpl_DivW32W16(...)
1366 //
1367 // Divides a WebRtc_Word32 |num| by a WebRtc_Word16 |den|.
1368 //
1369 // If |den|==0, (WebRtc_Word32)0x7FFFFFFF is returned.
1370 //
1371 // Input:
1372 //      - num       : Numerator
1373 //      - den       : Denominator
1374 //
1375 // Return value     : Result of the division (as a WebRtc_Word32), i.e., the
1376 //                    integer part of num/den.
1377 //
1378 
1379 //
1380 // WebRtcSpl_DivW32W16ResW16(...)
1381 //
1382 // Divides a WebRtc_Word32 |num| by a WebRtc_Word16 |den|, assuming that the
1383 // result is less than 32768, otherwise an unpredictable result will occur.
1384 //
1385 // If |den|==0, (WebRtc_Word16)0x7FFF is returned.
1386 //
1387 // Input:
1388 //      - num       : Numerator
1389 //      - den       : Denominator
1390 //
1391 // Return value     : Result of the division (as a WebRtc_Word16), i.e., the
1392 //                    integer part of num/den.
1393 //
1394 
1395 //
1396 // WebRtcSpl_DivResultInQ31(...)
1397 //
1398 // Divides a WebRtc_Word32 |num| by a WebRtc_Word16 |den|, assuming that the
1399 // absolute value of the denominator is larger than the numerator, otherwise
1400 // an unpredictable result will occur.
1401 //
1402 // Input:
1403 //      - num       : Numerator
1404 //      - den       : Denominator
1405 //
1406 // Return value     : Result of the division in Q31.
1407 //
1408 
1409 //
1410 // WebRtcSpl_DivW32HiLow(...)
1411 //
1412 // Divides a WebRtc_Word32 |num| by a denominator in hi, low format. The
1413 // absolute value of the denominator has to be larger (or equal to) the
1414 // numerator.
1415 //
1416 // Input:
1417 //      - num       : Numerator
1418 //      - den_hi    : High part of denominator
1419 //      - den_low   : Low part of denominator
1420 //
1421 // Return value     : Divided value in Q31
1422 //
1423 
1424 //
1425 // WebRtcSpl_Energy(...)
1426 //
1427 // Calculates the energy of a vector
1428 //
1429 // Input:
1430 //      - vector        : Vector which the energy should be calculated on
1431 //      - vector_length : Number of samples in vector
1432 //
1433 // Output:
1434 //      - scale_factor  : Number of left bit shifts needed to get the physical
1435 //                        energy value, i.e, to get the Q0 value
1436 //
1437 // Return value         : Energy value in Q(-|scale_factor|)
1438 //
1439 
1440 //
1441 // WebRtcSpl_FilterAR(...)
1442 //
1443 // Performs a 32-bit AR filtering on a vector in Q12
1444 //
1445 // Input:
1446 //  - ar_coef                   : AR-coefficient vector (values in Q12),
1447 //                                ar_coef[0] must be 4096.
1448 //  - ar_coef_length            : Number of coefficients in |ar_coef|.
1449 //  - in_vector                 : Vector to be filtered.
1450 //  - in_vector_length          : Number of samples in |in_vector|.
1451 //  - filter_state              : Current state (higher part) of the filter.
1452 //  - filter_state_length       : Length (in samples) of |filter_state|.
1453 //  - filter_state_low          : Current state (lower part) of the filter.
1454 //  - filter_state_low_length   : Length (in samples) of |filter_state_low|.
1455 //  - out_vector_low_length     : Maximum length (in samples) of
1456 //                                |out_vector_low|.
1457 //
1458 // Output:
1459 //  - filter_state              : Updated state (upper part) vector.
1460 //  - filter_state_low          : Updated state (lower part) vector.
1461 //  - out_vector                : Vector containing the upper part of the
1462 //                                filtered values.
1463 //  - out_vector_low            : Vector containing the lower part of the
1464 //                                filtered values.
1465 //
1466 // Return value                 : Number of samples in the |out_vector|.
1467 //
1468 
1469 //
1470 // WebRtcSpl_FilterMAFastQ12(...)
1471 //
1472 // Performs a MA filtering on a vector in Q12
1473 //
1474 // Input:
1475 //      - in_vector         : Input samples (state in positions
1476 //                            in_vector[-order] .. in_vector[-1])
1477 //      - ma_coef           : Filter coefficients (in Q12)
1478 //      - ma_coef_length    : Number of B coefficients (order+1)
1479 //      - vector_length     : Number of samples to be filtered
1480 //
1481 // Output:
1482 //      - out_vector        : Filtered samples
1483 //
1484 
1485 
1486 //
1487 // WebRtcSpl_DotProductWithScale(...)
1488 //
1489 // Calculates the dot product between two (WebRtc_Word16) vectors
1490 //
1491 // Input:
1492 //      - vector1       : Vector 1
1493 //      - vector2       : Vector 2
1494 //      - vector_length : Number of samples used in the dot product
1495 //      - scaling       : The number of right bit shifts to apply on each term
1496 //                        during calculation to avoid overflow, i.e., the
1497 //                        output will be in Q(-|scaling|)
1498 //
1499 // Return value         : The dot product in Q(-scaling)
1500 //
1501 
1502 //
1503 // WebRtcSpl_ComplexIFFT(...)
1504 //
1505 // Complex Inverse FFT
1506 //
1507 // Computes an inverse complex 2^|stages|-point FFT on the input vector, which
1508 // is in bit-reversed order. The original content of the vector is destroyed in
1509 // the process, since the input is overwritten by the output, normal-ordered,
1510 // FFT vector. With X as the input complex vector, y as the output complex
1511 // vector and with M = 2^|stages|, the following is computed:
1512 //
1513 //        M-1
1514 // y(k) = sum[X(i)*[cos(2*pi*i*k/M) + j*sin(2*pi*i*k/M)]]
1515 //        i=0
1516 //
1517 // The implementations are optimized for speed, not for code size. It uses the
1518 // decimation-in-time algorithm with radix-2 butterfly technique.
1519 //
1520 // Input:
1521 //      - vector    : In pointer to complex vector containing 2^|stages|
1522 //                    real elements interleaved with 2^|stages| imaginary
1523 //                    elements.
1524 //                    [ReImReImReIm....]
1525 //                    The elements are in Q(-scale) domain, see more on Return
1526 //                    Value below.
1527 //
1528 //      - stages    : Number of FFT stages. Must be at least 3 and at most 10,
1529 //                    since the table WebRtcSpl_kSinTable1024[] is 1024
1530 //                    elements long.
1531 //
1532 //      - mode      : This parameter gives the user to choose how the FFT
1533 //                    should work.
1534 //                    mode==0: Low-complexity and Low-accuracy mode
1535 //                    mode==1: High-complexity and High-accuracy mode
1536 //
1537 // Output:
1538 //      - vector    : Out pointer to the FFT vector (the same as input).
1539 //
1540 // Return Value     : The scale value that tells the number of left bit shifts
1541 //                    that the elements in the |vector| should be shifted with
1542 //                    in order to get Q0 values, i.e. the physically correct
1543 //                    values. The scale parameter is always 0 or positive,
1544 //                    except if N>1024 (|stages|>10), which returns a scale
1545 //                    value of -1, indicating error.
1546 //
1547 
1548 //
1549 // WebRtcSpl_ComplexFFT(...)
1550 //
1551 // Complex FFT
1552 //
1553 // Computes a complex 2^|stages|-point FFT on the input vector, which is in
1554 // bit-reversed order. The original content of the vector is destroyed in
1555 // the process, since the input is overwritten by the output, normal-ordered,
1556 // FFT vector. With x as the input complex vector, Y as the output complex
1557 // vector and with M = 2^|stages|, the following is computed:
1558 //
1559 //              M-1
1560 // Y(k) = 1/M * sum[x(i)*[cos(2*pi*i*k/M) + j*sin(2*pi*i*k/M)]]
1561 //              i=0
1562 //
1563 // The implementations are optimized for speed, not for code size. It uses the
1564 // decimation-in-time algorithm with radix-2 butterfly technique.
1565 //
1566 // This routine prevents overflow by scaling by 2 before each FFT stage. This is
1567 // a fixed scaling, for proper normalization - there will be log2(n) passes, so
1568 // this results in an overall factor of 1/n, distributed to maximize arithmetic
1569 // accuracy.
1570 //
1571 // Input:
1572 //      - vector    : In pointer to complex vector containing 2^|stages| real
1573 //                    elements interleaved with 2^|stages| imaginary elements.
1574 //                    [ReImReImReIm....]
1575 //                    The output is in the Q0 domain.
1576 //
1577 //      - stages    : Number of FFT stages. Must be at least 3 and at most 10,
1578 //                    since the table WebRtcSpl_kSinTable1024[] is 1024
1579 //                    elements long.
1580 //
1581 //      - mode      : This parameter gives the user to choose how the FFT
1582 //                    should work.
1583 //                    mode==0: Low-complexity and Low-accuracy mode
1584 //                    mode==1: High-complexity and High-accuracy mode
1585 //
1586 // Output:
1587 //      - vector    : The output FFT vector is in the Q0 domain.
1588 //
1589 // Return value     : The scale parameter is always 0, except if N>1024,
1590 //                    which returns a scale value of -1, indicating error.
1591 //
1592 
1593 //
1594 // WebRtcSpl_AnalysisQMF(...)
1595 //
1596 // Splits a 0-2*F Hz signal into two sub bands: 0-F Hz and F-2*F Hz. The
1597 // current version has F = 8000, therefore, a super-wideband audio signal is
1598 // split to lower-band 0-8 kHz and upper-band 8-16 kHz.
1599 //
1600 // Input:
1601 //      - in_data       : Wide band speech signal, 320 samples (10 ms)
1602 //
1603 // Input & Output:
1604 //      - filter_state1 : Filter state for first All-pass filter
1605 //      - filter_state2 : Filter state for second All-pass filter
1606 //
1607 // Output:
1608 //      - low_band      : Lower-band signal 0-8 kHz band, 160 samples (10 ms)
1609 //      - high_band     : Upper-band signal 8-16 kHz band (flipped in frequency
1610 //                        domain), 160 samples (10 ms)
1611 //
1612 
1613 //
1614 // WebRtcSpl_SynthesisQMF(...)
1615 //
1616 // Combines the two sub bands (0-F and F-2*F Hz) into a signal of 0-2*F
1617 // Hz, (current version has F = 8000 Hz). So the filter combines lower-band
1618 // (0-8 kHz) and upper-band (8-16 kHz) channels to obtain super-wideband 0-16
1619 // kHz audio.
1620 //
1621 // Input:
1622 //      - low_band      : The signal with the 0-8 kHz band, 160 samples (10 ms)
1623 //      - high_band     : The signal with the 8-16 kHz band, 160 samples (10 ms)
1624 //
1625 // Input & Output:
1626 //      - filter_state1 : Filter state for first All-pass filter
1627 //      - filter_state2 : Filter state for second All-pass filter
1628 //
1629 // Output:
1630 //      - out_data      : Super-wideband speech signal, 0-16 kHz
1631 //
1632 
1633 // WebRtc_Word16 WebRtcSpl_SatW32ToW16(...)
1634 //
1635 // This function saturates a 32-bit word into a 16-bit word.
1636 //
1637 // Input:
1638 //      - value32   : The value of a 32-bit word.
1639 //
1640 // Output:
1641 //      - out16     : the saturated 16-bit word.
1642 //
1643 
1644 // int32_t WebRtc_MulAccumW16(...)
1645 //
1646 // This function multiply a 16-bit word by a 16-bit word, and accumulate this
1647 // value to a 32-bit integer.
1648 //
1649 // Input:
1650 //      - a    : The value of the first 16-bit word.
1651 //      - b    : The value of the second 16-bit word.
1652 //      - c    : The value of an 32-bit integer.
1653 //
1654 // Return Value: The value of a * b + c.
1655 //
1656 
1657 // WebRtc_Word16 WebRtcSpl_get_version(...)
1658 //
1659 // This function gives the version string of the Signal Processing Library.
1660 //
1661 // Input:
1662 //      - length_in_bytes   : The size of Allocated space (in Bytes) where
1663 //                            the version number is written to (in string format).
1664 //
1665 // Output:
1666 //      - version           : Pointer to a buffer where the version number is written to.
1667 //
1668