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