tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

signal_processing_library.h (59985B)


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