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