xref: /aosp_15_r20/external/libopus/silk/float/SigProc_FLP.h (revision a58d3d2adb790c104798cd88c8a3aff4fa8b82cc)
1 /***********************************************************************
2 Copyright (c) 2006-2011, Skype Limited. All rights reserved.
3 Redistribution and use in source and binary forms, with or without
4 modification, are permitted provided that the following conditions
5 are met:
6 - Redistributions of source code must retain the above copyright notice,
7 this list of conditions and the following disclaimer.
8 - Redistributions in binary form must reproduce the above copyright
9 notice, this list of conditions and the following disclaimer in the
10 documentation and/or other materials provided with the distribution.
11 - Neither the name of Internet Society, IETF or IETF Trust, nor the
12 names of specific contributors, may be used to endorse or promote
13 products derived from this software without specific prior written
14 permission.
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 POSSIBILITY OF SUCH DAMAGE.
26 ***********************************************************************/
27 
28 #ifndef SILK_SIGPROC_FLP_H
29 #define SILK_SIGPROC_FLP_H
30 
31 #include "SigProc_FIX.h"
32 #include "float_cast.h"
33 #include "main.h"
34 #include <math.h>
35 
36 #ifdef  __cplusplus
37 extern "C"
38 {
39 #endif
40 
41 /********************************************************************/
42 /*                    SIGNAL PROCESSING FUNCTIONS                   */
43 /********************************************************************/
44 
45 /* Chirp (bw expand) LP AR filter */
46 void silk_bwexpander_FLP(
47     silk_float          *ar,                /* I/O  AR filter to be expanded (without leading 1)                */
48     const opus_int      d,                  /* I    length of ar                                                */
49     const silk_float    chirp               /* I    chirp factor (typically in range (0..1) )                   */
50 );
51 
52 /* compute inverse of LPC prediction gain, and                          */
53 /* test if LPC coefficients are stable (all poles within unit circle)   */
54 /* this code is based on silk_FLP_a2k()                                 */
55 silk_float silk_LPC_inverse_pred_gain_FLP(  /* O    return inverse prediction gain, energy domain               */
56     const silk_float    *A,                 /* I    prediction coefficients [order]                             */
57     opus_int32          order               /* I    prediction order                                            */
58 );
59 
60 silk_float silk_schur_FLP(                  /* O    returns residual energy                                     */
61     silk_float          refl_coef[],        /* O    reflection coefficients (length order)                      */
62     const silk_float    auto_corr[],        /* I    autocorrelation sequence (length order+1)                   */
63     opus_int            order               /* I    order                                                       */
64 );
65 
66 void silk_k2a_FLP(
67     silk_float          *A,                 /* O     prediction coefficients [order]                            */
68     const silk_float    *rc,                /* I     reflection coefficients [order]                            */
69     opus_int32          order               /* I     prediction order                                           */
70 );
71 
72 /* compute autocorrelation */
73 void silk_autocorrelation_FLP(
74     silk_float          *results,           /* O    result (length correlationCount)                            */
75     const silk_float    *inputData,         /* I    input data to correlate                                     */
76     opus_int            inputDataSize,      /* I    length of input                                             */
77     opus_int            correlationCount,    /* I    number of correlation taps to compute                       */
78     int                 arch
79 );
80 
81 opus_int silk_pitch_analysis_core_FLP(      /* O    Voicing estimate: 0 voiced, 1 unvoiced                      */
82     const silk_float    *frame,             /* I    Signal of length PE_FRAME_LENGTH_MS*Fs_kHz                  */
83     opus_int            *pitch_out,         /* O    Pitch lag values [nb_subfr]                                 */
84     opus_int16          *lagIndex,          /* O    Lag Index                                                   */
85     opus_int8           *contourIndex,      /* O    Pitch contour Index                                         */
86     silk_float          *LTPCorr,           /* I/O  Normalized correlation; input: value from previous frame    */
87     opus_int            prevLag,            /* I    Last lag of previous frame; set to zero is unvoiced         */
88     const silk_float    search_thres1,      /* I    First stage threshold for lag candidates 0 - 1              */
89     const silk_float    search_thres2,      /* I    Final threshold for lag candidates 0 - 1                    */
90     const opus_int      Fs_kHz,             /* I    sample frequency (kHz)                                      */
91     const opus_int      complexity,         /* I    Complexity setting, 0-2, where 2 is highest                 */
92     const opus_int      nb_subfr,           /* I    Number of 5 ms subframes                                    */
93     int                 arch                /* I    Run-time architecture                                       */
94 );
95 
96 void silk_insertion_sort_decreasing_FLP(
97     silk_float          *a,                 /* I/O  Unsorted / Sorted vector                                    */
98     opus_int            *idx,               /* O    Index vector for the sorted elements                        */
99     const opus_int      L,                  /* I    Vector length                                               */
100     const opus_int      K                   /* I    Number of correctly sorted positions                        */
101 );
102 
103 /* Compute reflection coefficients from input signal */
104 silk_float silk_burg_modified_FLP(          /* O    returns residual energy                                     */
105     silk_float          A[],                /* O    prediction coefficients (length order)                      */
106     const silk_float    x[],                /* I    input signal, length: nb_subfr*(D+L_sub)                    */
107     const silk_float    minInvGain,         /* I    minimum inverse prediction gain                             */
108     const opus_int      subfr_length,       /* I    input signal subframe length (incl. D preceding samples)    */
109     const opus_int      nb_subfr,           /* I    number of subframes stacked in x                            */
110     const opus_int      D,                  /* I    order                                                       */
111     int                 arch
112 );
113 
114 /* multiply a vector by a constant */
115 void silk_scale_vector_FLP(
116     silk_float          *data1,
117     silk_float          gain,
118     opus_int            dataSize
119 );
120 
121 /* copy and multiply a vector by a constant */
122 void silk_scale_copy_vector_FLP(
123     silk_float          *data_out,
124     const silk_float    *data_in,
125     silk_float          gain,
126     opus_int            dataSize
127 );
128 
129 /* inner product of two silk_float arrays, with result as double */
130 double silk_inner_product_FLP_c(
131     const silk_float    *data1,
132     const silk_float    *data2,
133     opus_int            dataSize
134 );
135 
136 #ifndef OVERRIDE_inner_product_FLP
137 #define silk_inner_product_FLP(data1, data2, dataSize, arch) ((void)arch,silk_inner_product_FLP_c(data1, data2, dataSize))
138 #endif
139 
140 
141 /* sum of squares of a silk_float array, with result as double */
142 double silk_energy_FLP(
143     const silk_float    *data,
144     opus_int            dataSize
145 );
146 
147 /********************************************************************/
148 /*                                MACROS                            */
149 /********************************************************************/
150 
151 #define PI              (3.1415926536f)
152 
153 #define silk_min_float( a, b )                  (((a) < (b)) ? (a) :  (b))
154 #define silk_max_float( a, b )                  (((a) > (b)) ? (a) :  (b))
155 #define silk_abs_float( a )                     ((silk_float)fabs(a))
156 
157 /* sigmoid function */
silk_sigmoid(silk_float x)158 static OPUS_INLINE silk_float silk_sigmoid( silk_float x )
159 {
160     return (silk_float)(1.0 / (1.0 + exp(-x)));
161 }
162 
163 /* floating-point to integer conversion (rounding) */
silk_float2int(silk_float x)164 static OPUS_INLINE opus_int32 silk_float2int( silk_float x )
165 {
166     return (opus_int32)float2int( x );
167 }
168 
169 /* floating-point to integer conversion (rounding) */
silk_float2short_array(opus_int16 * out,const silk_float * in,opus_int32 length)170 static OPUS_INLINE void silk_float2short_array(
171     opus_int16       *out,
172     const silk_float *in,
173     opus_int32       length
174 )
175 {
176     opus_int32 k;
177     for( k = length - 1; k >= 0; k-- ) {
178         out[k] = silk_SAT16( (opus_int32)float2int( in[k] ) );
179     }
180 }
181 
182 /* integer to floating-point conversion */
silk_short2float_array(silk_float * out,const opus_int16 * in,opus_int32 length)183 static OPUS_INLINE void silk_short2float_array(
184     silk_float       *out,
185     const opus_int16 *in,
186     opus_int32       length
187 )
188 {
189     opus_int32 k;
190     for( k = length - 1; k >= 0; k-- ) {
191         out[k] = (silk_float)in[k];
192     }
193 }
194 
195 /* using log2() helps the fixed-point conversion */
silk_log2(double x)196 static OPUS_INLINE silk_float silk_log2( double x )
197 {
198     return ( silk_float )( 3.32192809488736 * log10( x ) );
199 }
200 
201 #ifdef  __cplusplus
202 }
203 #endif
204 
205 #endif /* SILK_SIGPROC_FLP_H */
206