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 <stdint.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 
98 #ifdef __cplusplus
99 extern "C" {
100 #endif
101 
102 #define WEBRTC_SPL_MEMCPY_W16(v1, v2, length) \
103   memcpy(v1, v2, (length) * sizeof(int16_t))
104 
105 // inline functions:
106 #include "spl_inl.h"
107 
108 // Initialize SPL. Currently it contains only function pointer initialization.
109 // If the underlying platform is known to be ARM-Neon (WEBRTC_ARCH_ARM_NEON
110 // defined), the pointers will be assigned to code optimized for Neon; otherwise
111 // if run-time Neon detection (WEBRTC_DETECT_ARM_NEON) is enabled, the pointers
112 // will be assigned to either Neon code or generic C code; otherwise, generic C
113 // code will be assigned.
114 // Note that this function MUST be called in any application that uses SPL
115 // functions.
116 void WebRtcSpl_Init();
117 
118 int16_t WebRtcSpl_GetScalingSquare(int16_t* in_vector,
119                                    int in_vector_length,
120                                    int times);
121 
122 // Copy and set operations. Implementation in copy_set_operations.c.
123 // Descriptions at bottom of file.
124 void WebRtcSpl_MemSetW16(int16_t* vector,
125                          int16_t set_value,
126                          int vector_length);
127 void WebRtcSpl_MemSetW32(int32_t* vector,
128                          int32_t set_value,
129                          int vector_length);
130 void WebRtcSpl_MemCpyReversedOrder(int16_t* out_vector,
131                                    int16_t* in_vector,
132                                    int vector_length);
133 void WebRtcSpl_CopyFromEndW16(const int16_t* in_vector,
134                               int in_vector_length,
135                               int samples,
136                               int16_t* out_vector);
137 void WebRtcSpl_ZerosArrayW16(int16_t* vector,
138                              int vector_length);
139 void WebRtcSpl_ZerosArrayW32(int32_t* vector,
140                              int vector_length);
141 // End: Copy and set operations.
142 
143 
144 // Minimum and maximum operation functions and their pointers.
145 // Implementation in min_max_operations.c.
146 
147 // Returns the largest absolute value in a signed 16-bit vector.
148 //
149 // Input:
150 //      - vector : 16-bit input vector.
151 //      - length : Number of samples in vector.
152 //
153 // Return value  : Maximum absolute value in vector;
154 //                 or -1, if (vector == NULL || length <= 0).
155 typedef int16_t (*MaxAbsValueW16)(const int16_t* vector, int length);
156 extern MaxAbsValueW16 WebRtcSpl_MaxAbsValueW16;
157 int16_t WebRtcSpl_MaxAbsValueW16C(const int16_t* vector, int length);
158 #if (defined WEBRTC_DETECT_ARM_NEON) || (defined WEBRTC_ARCH_ARM_NEON)
159 int16_t WebRtcSpl_MaxAbsValueW16Neon(const int16_t* vector, int length);
160 #endif
161 #if defined(MIPS32_LE)
162 int16_t WebRtcSpl_MaxAbsValueW16_mips(const int16_t* vector, int length);
163 #endif
164 
165 // Returns the largest absolute value in a signed 32-bit vector.
166 //
167 // Input:
168 //      - vector : 32-bit input vector.
169 //      - length : Number of samples in vector.
170 //
171 // Return value  : Maximum absolute value in vector;
172 //                 or -1, if (vector == NULL || length <= 0).
173 typedef int32_t (*MaxAbsValueW32)(const int32_t* vector, int length);
174 extern MaxAbsValueW32 WebRtcSpl_MaxAbsValueW32;
175 int32_t WebRtcSpl_MaxAbsValueW32C(const int32_t* vector, int length);
176 #if (defined WEBRTC_DETECT_ARM_NEON) || (defined WEBRTC_ARCH_ARM_NEON)
177 int32_t WebRtcSpl_MaxAbsValueW32Neon(const int32_t* vector, int length);
178 #endif
179 #if defined(MIPS_DSP_R1_LE)
180 int32_t WebRtcSpl_MaxAbsValueW32_mips(const int32_t* vector, int length);
181 #endif
182 
183 // Returns the maximum value of a 16-bit vector.
184 //
185 // Input:
186 //      - vector : 16-bit input vector.
187 //      - length : Number of samples in vector.
188 //
189 // Return value  : Maximum sample value in |vector|.
190 //                 If (vector == NULL || length <= 0) WEBRTC_SPL_WORD16_MIN
191 //                 is returned. Note that WEBRTC_SPL_WORD16_MIN is a feasible
192 //                 value and we can't catch errors purely based on it.
193 typedef int16_t (*MaxValueW16)(const int16_t* vector, int length);
194 extern MaxValueW16 WebRtcSpl_MaxValueW16;
195 int16_t WebRtcSpl_MaxValueW16C(const int16_t* vector, int length);
196 #if (defined WEBRTC_DETECT_ARM_NEON) || (defined WEBRTC_ARCH_ARM_NEON)
197 int16_t WebRtcSpl_MaxValueW16Neon(const int16_t* vector, int length);
198 #endif
199 #if defined(MIPS32_LE)
200 int16_t WebRtcSpl_MaxValueW16_mips(const int16_t* vector, int length);
201 #endif
202 
203 // Returns the maximum value of a 32-bit vector.
204 //
205 // Input:
206 //      - vector : 32-bit input vector.
207 //      - length : Number of samples in vector.
208 //
209 // Return value  : Maximum sample value in |vector|.
210 //                 If (vector == NULL || length <= 0) WEBRTC_SPL_WORD32_MIN
211 //                 is returned. Note that WEBRTC_SPL_WORD32_MIN is a feasible
212 //                 value and we can't catch errors purely based on it.
213 typedef int32_t (*MaxValueW32)(const int32_t* vector, int length);
214 extern MaxValueW32 WebRtcSpl_MaxValueW32;
215 int32_t WebRtcSpl_MaxValueW32C(const int32_t* vector, int length);
216 #if (defined WEBRTC_DETECT_ARM_NEON) || (defined WEBRTC_ARCH_ARM_NEON)
217 int32_t WebRtcSpl_MaxValueW32Neon(const int32_t* vector, int length);
218 #endif
219 #if defined(MIPS32_LE)
220 int32_t WebRtcSpl_MaxValueW32_mips(const int32_t* vector, int length);
221 #endif
222 
223 // Returns the minimum value of a 16-bit vector.
224 //
225 // Input:
226 //      - vector : 16-bit input vector.
227 //      - length : Number of samples in vector.
228 //
229 // Return value  : Minimum sample value in |vector|.
230 //                 If (vector == NULL || length <= 0) WEBRTC_SPL_WORD16_MAX
231 //                 is returned. Note that WEBRTC_SPL_WORD16_MAX is a feasible
232 //                 value and we can't catch errors purely based on it.
233 typedef int16_t (*MinValueW16)(const int16_t* vector, int length);
234 extern MinValueW16 WebRtcSpl_MinValueW16;
235 int16_t WebRtcSpl_MinValueW16C(const int16_t* vector, int length);
236 #if (defined WEBRTC_DETECT_ARM_NEON) || (defined WEBRTC_ARCH_ARM_NEON)
237 int16_t WebRtcSpl_MinValueW16Neon(const int16_t* vector, int length);
238 #endif
239 #if defined(MIPS32_LE)
240 int16_t WebRtcSpl_MinValueW16_mips(const int16_t* vector, int length);
241 #endif
242 
243 // Returns the minimum value of a 32-bit vector.
244 //
245 // Input:
246 //      - vector : 32-bit input vector.
247 //      - length : Number of samples in vector.
248 //
249 // Return value  : Minimum sample value in |vector|.
250 //                 If (vector == NULL || length <= 0) WEBRTC_SPL_WORD32_MAX
251 //                 is returned. Note that WEBRTC_SPL_WORD32_MAX is a feasible
252 //                 value and we can't catch errors purely based on it.
253 typedef int32_t (*MinValueW32)(const int32_t* vector, int length);
254 extern MinValueW32 WebRtcSpl_MinValueW32;
255 int32_t WebRtcSpl_MinValueW32C(const int32_t* vector, int length);
256 #if (defined WEBRTC_DETECT_ARM_NEON) || (defined WEBRTC_ARCH_ARM_NEON)
257 int32_t WebRtcSpl_MinValueW32Neon(const int32_t* vector, int length);
258 #endif
259 #if defined(MIPS32_LE)
260 int32_t WebRtcSpl_MinValueW32_mips(const int32_t* vector, int length);
261 #endif
262 
263 // Returns the vector index to the largest absolute value of a 16-bit vector.
264 //
265 // Input:
266 //      - vector : 16-bit input vector.
267 //      - length : Number of samples in vector.
268 //
269 // Return value  : Index to the maximum absolute value in vector, or -1,
270 //                 if (vector == NULL || length <= 0).
271 //                 If there are multiple equal maxima, return the index of the
272 //                 first. -32768 will always have precedence over 32767 (despite
273 //                 -32768 presenting an int16 absolute value of 32767);
274 int WebRtcSpl_MaxAbsIndexW16(const int16_t* vector, int length);
275 
276 // Returns the vector index to the maximum sample value of a 16-bit vector.
277 //
278 // Input:
279 //      - vector : 16-bit input vector.
280 //      - length : Number of samples in vector.
281 //
282 // Return value  : Index to the maximum value in vector (if multiple
283 //                 indexes have the maximum, return the first);
284 //                 or -1, if (vector == NULL || length <= 0).
285 int WebRtcSpl_MaxIndexW16(const int16_t* vector, int length);
286 
287 // Returns the vector index to the maximum sample value of a 32-bit vector.
288 //
289 // Input:
290 //      - vector : 32-bit input vector.
291 //      - length : Number of samples in vector.
292 //
293 // Return value  : Index to the maximum value in vector (if multiple
294 //                 indexes have the maximum, return the first);
295 //                 or -1, if (vector == NULL || length <= 0).
296 int WebRtcSpl_MaxIndexW32(const int32_t* vector, int length);
297 
298 // Returns the vector index to the minimum sample value of a 16-bit vector.
299 //
300 // Input:
301 //      - vector : 16-bit input vector.
302 //      - length : Number of samples in vector.
303 //
304 // Return value  : Index to the mimimum value in vector  (if multiple
305 //                 indexes have the minimum, return the first);
306 //                 or -1, if (vector == NULL || length <= 0).
307 int WebRtcSpl_MinIndexW16(const int16_t* vector, int length);
308 
309 // Returns the vector index to the minimum sample value of a 32-bit vector.
310 //
311 // Input:
312 //      - vector : 32-bit input vector.
313 //      - length : Number of samples in vector.
314 //
315 // Return value  : Index to the mimimum value in vector  (if multiple
316 //                 indexes have the minimum, return the first);
317 //                 or -1, if (vector == NULL || length <= 0).
318 int WebRtcSpl_MinIndexW32(const int32_t* vector, int length);
319 
320 // End: Minimum and maximum operations.
321 
322 
323 // Vector scaling operations. Implementation in vector_scaling_operations.c.
324 // Description at bottom of file.
325 void WebRtcSpl_VectorBitShiftW16(int16_t* out_vector,
326                                  int16_t vector_length,
327                                  const int16_t* in_vector,
328                                  int16_t right_shifts);
329 void WebRtcSpl_VectorBitShiftW32(int32_t* out_vector,
330                                  int16_t vector_length,
331                                  const int32_t* in_vector,
332                                  int16_t right_shifts);
333 void WebRtcSpl_VectorBitShiftW32ToW16(int16_t* out_vector,
334                                       int vector_length,
335                                       const int32_t* in_vector,
336                                       int right_shifts);
337 void WebRtcSpl_ScaleVector(const int16_t* in_vector,
338                            int16_t* out_vector,
339                            int16_t gain,
340                            int16_t vector_length,
341                            int16_t right_shifts);
342 void WebRtcSpl_ScaleVectorWithSat(const int16_t* in_vector,
343                                   int16_t* out_vector,
344                                   int16_t gain,
345                                   int16_t vector_length,
346                                   int16_t right_shifts);
347 void WebRtcSpl_ScaleAndAddVectors(const int16_t* in_vector1,
348                                   int16_t gain1, int right_shifts1,
349                                   const int16_t* in_vector2,
350                                   int16_t gain2, int right_shifts2,
351                                   int16_t* out_vector,
352                                   int vector_length);
353 
354 // The functions (with related pointer) perform the vector operation:
355 //   out_vector[k] = ((scale1 * in_vector1[k]) + (scale2 * in_vector2[k])
356 //        + round_value) >> right_shifts,
357 //   where  round_value = (1 << right_shifts) >> 1.
358 //
359 // Input:
360 //      - in_vector1       : Input vector 1
361 //      - in_vector1_scale : Gain to be used for vector 1
362 //      - in_vector2       : Input vector 2
363 //      - in_vector2_scale : Gain to be used for vector 2
364 //      - right_shifts     : Number of right bit shifts to be applied
365 //      - length           : Number of elements in the input vectors
366 //
367 // Output:
368 //      - out_vector       : Output vector
369 // Return value            : 0 if OK, -1 if (in_vector1 == NULL
370 //                           || in_vector2 == NULL || out_vector == NULL
371 //                           || length <= 0 || right_shift < 0).
372 typedef int (*ScaleAndAddVectorsWithRound)(const int16_t* in_vector1,
373                                            int16_t in_vector1_scale,
374                                            const int16_t* in_vector2,
375                                            int16_t in_vector2_scale,
376                                            int right_shifts,
377                                            int16_t* out_vector,
378                                            int length);
379 extern ScaleAndAddVectorsWithRound WebRtcSpl_ScaleAndAddVectorsWithRound;
380 int WebRtcSpl_ScaleAndAddVectorsWithRoundC(const int16_t* in_vector1,
381                                            int16_t in_vector1_scale,
382                                            const int16_t* in_vector2,
383                                            int16_t in_vector2_scale,
384                                            int right_shifts,
385                                            int16_t* out_vector,
386                                            int length);
387 #if (defined WEBRTC_DETECT_ARM_NEON) || (defined WEBRTC_ARCH_ARM_NEON)
388 int WebRtcSpl_ScaleAndAddVectorsWithRoundNeon(const int16_t* in_vector1,
389                                               int16_t in_vector1_scale,
390                                               const int16_t* in_vector2,
391                                               int16_t in_vector2_scale,
392                                               int right_shifts,
393                                               int16_t* out_vector,
394                                               int length);
395 #endif
396 #if defined(MIPS_DSP_R1_LE)
397 int WebRtcSpl_ScaleAndAddVectorsWithRound_mips(const int16_t* in_vector1,
398                                                int16_t in_vector1_scale,
399                                                const int16_t* in_vector2,
400                                                int16_t in_vector2_scale,
401                                                int right_shifts,
402                                                int16_t* out_vector,
403                                                int length);
404 #endif
405 // End: Vector scaling operations.
406 
407 // iLBC specific functions. Implementations in ilbc_specific_functions.c.
408 // Description at bottom of file.
409 void WebRtcSpl_ReverseOrderMultArrayElements(int16_t* out_vector,
410                                              const int16_t* in_vector,
411                                              const int16_t* window,
412                                              int16_t vector_length,
413                                              int16_t right_shifts);
414 void WebRtcSpl_ElementwiseVectorMult(int16_t* out_vector,
415                                      const int16_t* in_vector,
416                                      const int16_t* window,
417                                      int16_t vector_length,
418                                      int16_t right_shifts);
419 void WebRtcSpl_AddVectorsAndShift(int16_t* out_vector,
420                                   const int16_t* in_vector1,
421                                   const int16_t* in_vector2,
422                                   int16_t vector_length,
423                                   int16_t right_shifts);
424 void WebRtcSpl_AddAffineVectorToVector(int16_t* out_vector,
425                                        int16_t* in_vector,
426                                        int16_t gain,
427                                        int32_t add_constant,
428                                        int16_t right_shifts,
429                                        int vector_length);
430 void WebRtcSpl_AffineTransformVector(int16_t* out_vector,
431                                      int16_t* in_vector,
432                                      int16_t gain,
433                                      int32_t add_constant,
434                                      int16_t right_shifts,
435                                      int vector_length);
436 // End: iLBC specific functions.
437 
438 // Signal processing operations.
439 
440 // A 32-bit fix-point implementation of auto-correlation computation
441 //
442 // Input:
443 //      - in_vector        : Vector to calculate autocorrelation upon
444 //      - in_vector_length : Length (in samples) of |vector|
445 //      - order            : The order up to which the autocorrelation should be
446 //                           calculated
447 //
448 // Output:
449 //      - result           : auto-correlation values (values should be seen
450 //                           relative to each other since the absolute values
451 //                           might have been down shifted to avoid overflow)
452 //
453 //      - scale            : The number of left shifts required to obtain the
454 //                           auto-correlation in Q0
455 //
456 // Return value            :
457 //      - -1, if |order| > |in_vector_length|;
458 //      - Number of samples in |result|, i.e. (order+1), otherwise.
459 int WebRtcSpl_AutoCorrelation(const int16_t* in_vector,
460                               int in_vector_length,
461                               int order,
462                               int32_t* result,
463                               int* scale);
464 
465 // A 32-bit fix-point implementation of the Levinson-Durbin algorithm that
466 // does NOT use the 64 bit class
467 //
468 // Input:
469 //      - auto_corr : Vector with autocorrelation values of length >=
470 //                    |use_order|+1
471 //      - use_order : The LPC filter order (support up to order 20)
472 //
473 // Output:
474 //      - lpc_coef  : lpc_coef[0..use_order] LPC coefficients in Q12
475 //      - refl_coef : refl_coef[0...use_order-1]| Reflection coefficients in
476 //                    Q15
477 //
478 // Return value     : 1 for stable 0 for unstable
479 int16_t WebRtcSpl_LevinsonDurbin(int32_t* auto_corr,
480                                  int16_t* lpc_coef,
481                                  int16_t* refl_coef,
482                                  int16_t order);
483 
484 // Converts reflection coefficients |refl_coef| to LPC coefficients |lpc_coef|.
485 // This version is a 16 bit operation.
486 //
487 // NOTE: The 16 bit refl_coef -> lpc_coef conversion might result in a
488 // "slightly unstable" filter (i.e., a pole just outside the unit circle) in
489 // "rare" cases even if the reflection coefficients are stable.
490 //
491 // Input:
492 //      - refl_coef : Reflection coefficients in Q15 that should be converted
493 //                    to LPC coefficients
494 //      - use_order : Number of coefficients in |refl_coef|
495 //
496 // Output:
497 //      - lpc_coef  : LPC coefficients in Q12
498 void WebRtcSpl_ReflCoefToLpc(const int16_t* refl_coef,
499                              int use_order,
500                              int16_t* lpc_coef);
501 
502 // Converts LPC coefficients |lpc_coef| to reflection coefficients |refl_coef|.
503 // This version is a 16 bit operation.
504 // The conversion is implemented by the step-down algorithm.
505 //
506 // Input:
507 //      - lpc_coef  : LPC coefficients in Q12, that should be converted to
508 //                    reflection coefficients
509 //      - use_order : Number of coefficients in |lpc_coef|
510 //
511 // Output:
512 //      - refl_coef : Reflection coefficients in Q15.
513 void WebRtcSpl_LpcToReflCoef(int16_t* lpc_coef,
514                              int use_order,
515                              int16_t* refl_coef);
516 
517 // Calculates reflection coefficients (16 bit) from auto-correlation values
518 //
519 // Input:
520 //      - auto_corr : Auto-correlation values
521 //      - use_order : Number of coefficients wanted be calculated
522 //
523 // Output:
524 //      - refl_coef : Reflection coefficients in Q15.
525 void WebRtcSpl_AutoCorrToReflCoef(const int32_t* auto_corr,
526                                   int use_order,
527                                   int16_t* refl_coef);
528 
529 // The functions (with related pointer) calculate the cross-correlation between
530 // two sequences |seq1| and |seq2|.
531 // |seq1| is fixed and |seq2| slides as the pointer is increased with the
532 // amount |step_seq2|. Note the arguments should obey the relationship:
533 // |dim_seq| - 1 + |step_seq2| * (|dim_cross_correlation| - 1) <
534 //      buffer size of |seq2|
535 //
536 // Input:
537 //      - seq1           : First sequence (fixed throughout the correlation)
538 //      - seq2           : Second sequence (slides |step_vector2| for each
539 //                            new correlation)
540 //      - dim_seq        : Number of samples to use in the cross-correlation
541 //      - dim_cross_correlation : Number of cross-correlations to calculate (the
542 //                            start position for |vector2| is updated for each
543 //                            new one)
544 //      - right_shifts   : Number of right bit shifts to use. This will
545 //                            become the output Q-domain.
546 //      - step_seq2      : How many (positive or negative) steps the
547 //                            |vector2| pointer should be updated for each new
548 //                            cross-correlation value.
549 //
550 // Output:
551 //      - cross_correlation : The cross-correlation in Q(-right_shifts)
552 typedef void (*CrossCorrelation)(int32_t* cross_correlation,
553                                  const int16_t* seq1,
554                                  const int16_t* seq2,
555                                  int16_t dim_seq,
556                                  int16_t dim_cross_correlation,
557                                  int16_t right_shifts,
558                                  int16_t step_seq2);
559 extern CrossCorrelation WebRtcSpl_CrossCorrelation;
560 void WebRtcSpl_CrossCorrelationC(int32_t* cross_correlation,
561                                  const int16_t* seq1,
562                                  const int16_t* seq2,
563                                  int16_t dim_seq,
564                                  int16_t dim_cross_correlation,
565                                  int16_t right_shifts,
566                                  int16_t step_seq2);
567 #if (defined WEBRTC_DETECT_ARM_NEON) || (defined WEBRTC_ARCH_ARM_NEON)
568 void WebRtcSpl_CrossCorrelationNeon(int32_t* cross_correlation,
569                                     const int16_t* seq1,
570                                     const int16_t* seq2,
571                                     int16_t dim_seq,
572                                     int16_t dim_cross_correlation,
573                                     int16_t right_shifts,
574                                     int16_t step_seq2);
575 #endif
576 #if defined(MIPS32_LE)
577 void WebRtcSpl_CrossCorrelation_mips(int32_t* cross_correlation,
578                                      const int16_t* seq1,
579                                      const int16_t* seq2,
580                                      int16_t dim_seq,
581                                      int16_t dim_cross_correlation,
582                                      int16_t right_shifts,
583                                      int16_t step_seq2);
584 #endif
585 
586 
587 
588 // Creates (the first half of) a Hanning window. Size must be at least 1 and
589 // at most 512.
590 //
591 // Input:
592 //      - size      : Length of the requested Hanning window (1 to 512)
593 //
594 // Output:
595 //      - window    : Hanning vector in Q14.
596 void WebRtcSpl_GetHanningWindow(int16_t* window, int16_t size);
597 
598 // Calculates y[k] = sqrt(1 - x[k]^2) for each element of the input vector
599 // |in_vector|. Input and output values are in Q15.
600 //
601 // Inputs:
602 //      - in_vector     : Values to calculate sqrt(1 - x^2) of
603 //      - vector_length : Length of vector |in_vector|
604 //
605 // Output:
606 //      - out_vector    : Output values in Q15
607 void WebRtcSpl_SqrtOfOneMinusXSquared(int16_t* in_vector,
608                                       int vector_length,
609                                       int16_t* out_vector);
610 // End: Signal processing operations.
611 
612 // Randomization functions. Implementations collected in
613 // randomization_functions.c and descriptions at bottom of this file.
614 int16_t WebRtcSpl_RandU(uint32_t* seed);
615 int16_t WebRtcSpl_RandN(uint32_t* seed);
616 int16_t WebRtcSpl_RandUArray(int16_t* vector,
617                              int16_t vector_length,
618                              uint32_t* seed);
619 // End: Randomization functions.
620 
621 // Math functions
622 int32_t WebRtcSpl_Sqrt(int32_t value);
623 int32_t WebRtcSpl_SqrtFloor(int32_t value);
624 
625 // Divisions. Implementations collected in division_operations.c and
626 // descriptions at bottom of this file.
627 uint32_t WebRtcSpl_DivU32U16(uint32_t num, uint16_t den);
628 int32_t WebRtcSpl_DivW32W16(int32_t num, int16_t den);
629 int16_t WebRtcSpl_DivW32W16ResW16(int32_t num, int16_t den);
630 int32_t WebRtcSpl_DivResultInQ31(int32_t num, int32_t den);
631 int32_t WebRtcSpl_DivW32HiLow(int32_t num, int16_t den_hi, int16_t den_low);
632 // End: Divisions.
633 
634 int32_t WebRtcSpl_Energy(int16_t* vector, int vector_length, int* scale_factor);
635 
636 // Calculates the dot product between two (int16_t) vectors.
637 //
638 // Input:
639 //      - vector1       : Vector 1
640 //      - vector2       : Vector 2
641 //      - vector_length : Number of samples used in the dot product
642 //      - scaling       : The number of right bit shifts to apply on each term
643 //                        during calculation to avoid overflow, i.e., the
644 //                        output will be in Q(-|scaling|)
645 //
646 // Return value         : The dot product in Q(-scaling)
647 int32_t WebRtcSpl_DotProductWithScale(const int16_t* vector1,
648                                       const int16_t* vector2,
649                                       int length,
650                                       int scaling);
651 
652 // Filter operations.
653 int WebRtcSpl_FilterAR(const int16_t* ar_coef,
654                        int ar_coef_length,
655                        const int16_t* in_vector,
656                        int in_vector_length,
657                        int16_t* filter_state,
658                        int filter_state_length,
659                        int16_t* filter_state_low,
660                        int filter_state_low_length,
661                        int16_t* out_vector,
662                        int16_t* out_vector_low,
663                        int out_vector_low_length);
664 
665 void WebRtcSpl_FilterMAFastQ12(int16_t* in_vector,
666                                int16_t* out_vector,
667                                int16_t* ma_coef,
668                                int16_t ma_coef_length,
669                                int16_t vector_length);
670 
671 // Performs a AR filtering on a vector in Q12
672 // Input:
673 //      - data_in            : Input samples
674 //      - data_out           : State information in positions
675 //                               data_out[-order] .. data_out[-1]
676 //      - coefficients       : Filter coefficients (in Q12)
677 //      - coefficients_length: Number of coefficients (order+1)
678 //      - data_length        : Number of samples to be filtered
679 // Output:
680 //      - data_out           : Filtered samples
681 void WebRtcSpl_FilterARFastQ12(const int16_t* data_in,
682                                int16_t* data_out,
683                                const int16_t* __restrict coefficients,
684                                int coefficients_length,
685                                int data_length);
686 
687 // The functions (with related pointer) perform a MA down sampling filter
688 // on a vector.
689 // Input:
690 //      - data_in            : Input samples (state in positions
691 //                               data_in[-order] .. data_in[-1])
692 //      - data_in_length     : Number of samples in |data_in| to be filtered.
693 //                               This must be at least
694 //                               |delay| + |factor|*(|out_vector_length|-1) + 1)
695 //      - data_out_length    : Number of down sampled samples desired
696 //      - coefficients       : Filter coefficients (in Q12)
697 //      - coefficients_length: Number of coefficients (order+1)
698 //      - factor             : Decimation factor
699 //      - delay              : Delay of filter (compensated for in out_vector)
700 // Output:
701 //      - data_out           : Filtered samples
702 // Return value              : 0 if OK, -1 if |in_vector| is too short
703 typedef int (*DownsampleFast)(const int16_t* data_in,
704                               int data_in_length,
705                               int16_t* data_out,
706                               int data_out_length,
707                               const int16_t* __restrict coefficients,
708                               int coefficients_length,
709                               int factor,
710                               int delay);
711 extern DownsampleFast WebRtcSpl_DownsampleFast;
712 int WebRtcSpl_DownsampleFastC(const int16_t* data_in,
713                               int data_in_length,
714                               int16_t* data_out,
715                               int data_out_length,
716                               const int16_t* __restrict coefficients,
717                               int coefficients_length,
718                               int factor,
719                               int delay);
720 #if (defined WEBRTC_DETECT_ARM_NEON) || (defined WEBRTC_ARCH_ARM_NEON)
721 int WebRtcSpl_DownsampleFastNeon(const int16_t* data_in,
722                                  int data_in_length,
723                                  int16_t* data_out,
724                                  int data_out_length,
725                                  const int16_t* __restrict coefficients,
726                                  int coefficients_length,
727                                  int factor,
728                                  int delay);
729 #endif
730 #if defined(MIPS32_LE)
731 int WebRtcSpl_DownsampleFast_mips(const int16_t* data_in,
732                                   int data_in_length,
733                                   int16_t* data_out,
734                                   int data_out_length,
735                                   const int16_t* __restrict coefficients,
736                                   int coefficients_length,
737                                   int factor,
738                                   int delay);
739 #endif
740 
741 // End: Filter operations.
742 
743 // FFT operations
744 
745 int WebRtcSpl_ComplexFFT(int16_t vector[], int stages, int mode);
746 int WebRtcSpl_ComplexIFFT(int16_t vector[], int stages, int mode);
747 
748 // Treat a 16-bit complex data buffer |complex_data| as an array of 32-bit
749 // values, and swap elements whose indexes are bit-reverses of each other.
750 //
751 // Input:
752 //      - complex_data  : Complex data buffer containing 2^|stages| real
753 //                        elements interleaved with 2^|stages| imaginary
754 //                        elements: [Re Im Re Im Re Im....]
755 //      - stages        : Number of FFT stages. Must be at least 3 and at most
756 //                        10, since the table WebRtcSpl_kSinTable1024[] is 1024
757 //                        elements long.
758 //
759 // Output:
760 //      - complex_data  : The complex data buffer.
761 
762 void WebRtcSpl_ComplexBitReverse(int16_t* __restrict complex_data, int stages);
763 
764 // End: FFT operations
765 
766 /************************************************************
767  *
768  * RESAMPLING FUNCTIONS AND THEIR STRUCTS ARE DEFINED BELOW
769  *
770  ************************************************************/
771 
772 /*******************************************************************
773  * resample.c
774  *
775  * Includes the following resampling combinations
776  * 22 kHz -> 16 kHz
777  * 16 kHz -> 22 kHz
778  * 22 kHz ->  8 kHz
779  *  8 kHz -> 22 kHz
780  *
781  ******************************************************************/
782 
783 // state structure for 22 -> 16 resampler
784 typedef struct {
785   int32_t S_22_44[8];
786   int32_t S_44_32[8];
787   int32_t S_32_16[8];
788 } WebRtcSpl_State22khzTo16khz;
789 
790 void WebRtcSpl_Resample22khzTo16khz(const int16_t* in,
791                                     int16_t* out,
792                                     WebRtcSpl_State22khzTo16khz* state,
793                                     int32_t* tmpmem);
794 
795 void WebRtcSpl_ResetResample22khzTo16khz(WebRtcSpl_State22khzTo16khz* state);
796 
797 // state structure for 16 -> 22 resampler
798 typedef struct {
799   int32_t S_16_32[8];
800   int32_t S_32_22[8];
801 } WebRtcSpl_State16khzTo22khz;
802 
803 void WebRtcSpl_Resample16khzTo22khz(const int16_t* in,
804                                     int16_t* out,
805                                     WebRtcSpl_State16khzTo22khz* state,
806                                     int32_t* tmpmem);
807 
808 void WebRtcSpl_ResetResample16khzTo22khz(WebRtcSpl_State16khzTo22khz* state);
809 
810 // state structure for 22 -> 8 resampler
811 typedef struct {
812   int32_t S_22_22[16];
813   int32_t S_22_16[8];
814   int32_t S_16_8[8];
815 } WebRtcSpl_State22khzTo8khz;
816 
817 void WebRtcSpl_Resample22khzTo8khz(const int16_t* in, int16_t* out,
818                                    WebRtcSpl_State22khzTo8khz* state,
819                                    int32_t* tmpmem);
820 
821 void WebRtcSpl_ResetResample22khzTo8khz(WebRtcSpl_State22khzTo8khz* state);
822 
823 // state structure for 8 -> 22 resampler
824 typedef struct {
825   int32_t S_8_16[8];
826   int32_t S_16_11[8];
827   int32_t S_11_22[8];
828 } WebRtcSpl_State8khzTo22khz;
829 
830 void WebRtcSpl_Resample8khzTo22khz(const int16_t* in, int16_t* out,
831                                    WebRtcSpl_State8khzTo22khz* state,
832                                    int32_t* tmpmem);
833 
834 void WebRtcSpl_ResetResample8khzTo22khz(WebRtcSpl_State8khzTo22khz* state);
835 
836 /*******************************************************************
837  * resample_fractional.c
838  * Functions for internal use in the other resample functions
839  *
840  * Includes the following resampling combinations
841  * 48 kHz -> 32 kHz
842  * 32 kHz -> 24 kHz
843  * 44 kHz -> 32 kHz
844  *
845  ******************************************************************/
846 
847 void WebRtcSpl_Resample48khzTo32khz(const int32_t* In, int32_t* Out,
848                                     int32_t K);
849 
850 void WebRtcSpl_Resample32khzTo24khz(const int32_t* In, int32_t* Out,
851                                     int32_t K);
852 
853 void WebRtcSpl_Resample44khzTo32khz(const int32_t* In, int32_t* Out,
854                                     int32_t K);
855 
856 /*******************************************************************
857  * resample_48khz.c
858  *
859  * Includes the following resampling combinations
860  * 48 kHz -> 16 kHz
861  * 16 kHz -> 48 kHz
862  * 48 kHz ->  8 kHz
863  *  8 kHz -> 48 kHz
864  *
865  ******************************************************************/
866 
867 typedef struct {
868   int32_t S_48_48[16];
869   int32_t S_48_32[8];
870   int32_t S_32_16[8];
871 } WebRtcSpl_State48khzTo16khz;
872 
873 void WebRtcSpl_Resample48khzTo16khz(const int16_t* in, int16_t* out,
874                                     WebRtcSpl_State48khzTo16khz* state,
875                                     int32_t* tmpmem);
876 
877 void WebRtcSpl_ResetResample48khzTo16khz(WebRtcSpl_State48khzTo16khz* state);
878 
879 typedef struct {
880   int32_t S_16_32[8];
881   int32_t S_32_24[8];
882   int32_t S_24_48[8];
883 } WebRtcSpl_State16khzTo48khz;
884 
885 void WebRtcSpl_Resample16khzTo48khz(const int16_t* in, int16_t* out,
886                                     WebRtcSpl_State16khzTo48khz* state,
887                                     int32_t* tmpmem);
888 
889 void WebRtcSpl_ResetResample16khzTo48khz(WebRtcSpl_State16khzTo48khz* state);
890 
891 typedef struct {
892   int32_t S_48_24[8];
893   int32_t S_24_24[16];
894   int32_t S_24_16[8];
895   int32_t S_16_8[8];
896 } WebRtcSpl_State48khzTo8khz;
897 
898 void WebRtcSpl_Resample48khzTo8khz(const int16_t* in, int16_t* out,
899                                    WebRtcSpl_State48khzTo8khz* state,
900                                    int32_t* tmpmem);
901 
902 void WebRtcSpl_ResetResample48khzTo8khz(WebRtcSpl_State48khzTo8khz* state);
903 
904 typedef struct {
905   int32_t S_8_16[8];
906   int32_t S_16_12[8];
907   int32_t S_12_24[8];
908   int32_t S_24_48[8];
909 } WebRtcSpl_State8khzTo48khz;
910 
911 void WebRtcSpl_Resample8khzTo48khz(const int16_t* in, int16_t* out,
912                                    WebRtcSpl_State8khzTo48khz* state,
913                                    int32_t* tmpmem);
914 
915 void WebRtcSpl_ResetResample8khzTo48khz(WebRtcSpl_State8khzTo48khz* state);
916 
917 /*******************************************************************
918  * resample_by_2.c
919  *
920  * Includes down and up sampling by a factor of two.
921  *
922  ******************************************************************/
923 
924 void WebRtcSpl_DownsampleBy2(const int16_t* in, int len,
925                              int16_t* out, int32_t* filtState);
926 
927 void WebRtcSpl_UpsampleBy2(const int16_t* in, int len,
928                            int16_t* out, int32_t* filtState);
929 
930 /************************************************************
931  * END OF RESAMPLING FUNCTIONS
932  ************************************************************/
933 void WebRtcSpl_AnalysisQMF(const int16_t* in_data,
934                            int in_data_length,
935                            int16_t* low_band,
936                            int16_t* high_band,
937                            int32_t* filter_state1,
938                            int32_t* filter_state2);
939 void WebRtcSpl_SynthesisQMF(const int16_t* low_band,
940                             const int16_t* high_band,
941                             int band_length,
942                             int16_t* out_data,
943                             int32_t* filter_state1,
944                             int32_t* filter_state2);
945 
946 
947 /* Float functions.
948  */
949 void FloatS16ToS16(const float* src, size_t size, int16_t* dest);
950 void S16ToFloatS16(const int16_t* src, size_t size, float* dest);
951 void FloatToS16(const float* src, size_t size, int16_t* dest);
952 void S16ToFloat(const int16_t* src, size_t size, float* dest);
953 void FloatToFloatS16(const float* src, size_t size, float* dest);
954 void FloatS16ToFloat(const float* src, size_t size, float* dest);
955 
956 typedef struct {
957   int16_t y[4];
958   int16_t x[2];
959   const int16_t* ba;
960 } FilterState;
961 
962 void init_highpass_filter(FilterState *hpf, uint32_t fs);
963 int highpass_filter(FilterState* hpf, int16_t* data, int length);
964 
965 #ifdef __cplusplus
966 }
967 #endif
968 
969 
970 #endif
971