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