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