Convert all CRLF in the SILK code, tabs to spaces, and trailing
[opus.git] / silk / float / silk_main_FLP.h
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, (subject to the limitations in the disclaimer below)
5 are permitted provided that the following conditions 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 Skype Limited, nor the names of specific
12 contributors, may be used to endorse or promote products derived from
13 this software without specific prior written permission.
14 NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
15 BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
16 CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
17 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
22 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***********************************************************************/
27
28 #ifndef SILK_MAIN_FLP_H
29 #define SILK_MAIN_FLP_H
30
31 #include "silk_SigProc_FLP.h"
32 #include "silk_SigProc_FIX.h"
33 #include "silk_structs_FLP.h"
34 #include "silk_main.h"
35 #include "silk_define.h"
36 #include "silk_debug.h"
37 #include "entenc.h"
38
39 #ifdef __cplusplus
40 extern "C"
41 {
42 #endif
43
44 #define silk_encoder_state_Fxx      silk_encoder_state_FLP
45 #define silk_encode_frame_Fxx       silk_encode_frame_FLP
46
47 /*********************/
48 /* Encoder Functions */
49 /*********************/
50
51 /* High-pass filter with cutoff frequency adaptation based on pitch lag statistics */
52 void silk_HP_variable_cutoff(
53     silk_encoder_state_Fxx          state_Fxx[],        /* I/O  Encoder states                          */
54     const opus_int                   nChannels           /* I    Number of channels                      */
55 );
56
57 /* Encoder main function */
58 opus_int silk_encode_frame_FLP(
59     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
60     opus_int32                       *pnBytesOut,        /*   O  Number of payload bytes;                */
61     ec_enc                          *psRangeEnc         /* I/O  compressor data structure               */
62 );
63
64 /* Low Bitrate Redundancy (LBRR) encoding. Reuse all parameters but encode with lower bitrate           */
65 void silk_LBRR_encode_FLP(
66     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
67     silk_encoder_control_FLP        *psEncCtrl,         /* I/O  Encoder control FLP                     */
68     const SKP_float                 xfw[]               /* I    Input signal                            */
69 );
70
71 /* Initializes the Silk encoder state */
72 opus_int silk_init_encoder(
73     silk_encoder_state_FLP          *psEnc              /* I/O  Encoder state FLP                       */
74 );
75
76 /* Control the Silk encoder */
77 opus_int silk_control_encoder(
78     silk_encoder_state_FLP          *psEnc,             /* I/O  Pointer to Silk encoder state FLP       */
79     silk_EncControlStruct           *encControl,        /* I:   Control structure                       */
80     const opus_int32                 TargetRate_bps,     /* I    Target max bitrate (bps)                */
81     const opus_int                   allow_bw_switch,    /* I    Flag to allow switching audio bandwidth */
82     const opus_int                   channelNb           /* I    Channel number                          */
83 );
84
85 /****************/
86 /* Prefiltering */
87 /****************/
88 void silk_prefilter_FLP(
89     silk_encoder_state_FLP              *psEnc,         /* I/O  Encoder state FLP                       */
90     const silk_encoder_control_FLP      *psEncCtrl,     /* I    Encoder control FLP                     */
91           SKP_float                     xw[],           /* O    Weighted signal                         */
92     const SKP_float                     x[]             /* I    Speech signal                           */
93 );
94
95 /**************************/
96 /* Noise shaping analysis */
97 /**************************/
98 /* Compute noise shaping coefficients and initial gain values */
99 void silk_noise_shape_analysis_FLP(
100     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
101     silk_encoder_control_FLP        *psEncCtrl,         /* I/O  Encoder control FLP                     */
102     const SKP_float                 *pitch_res,         /* I    LPC residual from pitch analysis        */
103     const SKP_float                 *x                  /* I    Input signal [frame_length + la_shape]  */
104 );
105
106 /* Autocorrelations for a warped frequency axis */
107 void silk_warped_autocorrelation_FLP(
108           SKP_float                 *corr,              /* O    Result [order + 1]                      */
109     const SKP_float                 *input,             /* I    Input data to correlate                 */
110     const SKP_float                 warping,            /* I    Warping coefficient                     */
111     const opus_int                   length,             /* I    Length of input                         */
112     const opus_int                   order               /* I    Correlation order (even)                */
113 );
114
115 /* Calculation of LTP state scaling */
116 void silk_LTP_scale_ctrl_FLP(
117     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
118     silk_encoder_control_FLP        *psEncCtrl          /* I/O  Encoder control FLP                     */
119 );
120
121 /**********************************************/
122 /* Prediction Analysis                        */
123 /**********************************************/
124 /* Find pitch lags */
125 void silk_find_pitch_lags_FLP(
126     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
127     silk_encoder_control_FLP        *psEncCtrl,         /* I/O  Encoder control FLP                     */
128           SKP_float                 res[],              /* O    Residual                                */
129     const SKP_float                 x[]                 /* I    Speech signal                           */
130 );
131
132 /* Find LPC and LTP coefficients */
133 void silk_find_pred_coefs_FLP(
134     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
135     silk_encoder_control_FLP        *psEncCtrl,         /* I/O  Encoder control FLP                     */
136     const SKP_float                 res_pitch[],        /* I    Residual from pitch analysis            */
137     const SKP_float                 x[]                 /* I    Speech signal                           */
138 );
139
140 /* LPC analysis */
141 void silk_find_LPC_FLP(
142           opus_int16                 NLSF_Q15[],             /* O    NLSFs                                   */
143           opus_int8                  *interpIndex,           /* O    NLSF interp. index for NLSF interp.     */
144     const opus_int16                 prev_NLSFq_Q15[],       /* I    Previous NLSFs, for NLSF interpolation  */
145     const opus_int                   useInterpNLSFs,         /* I    Flag                                    */
146     const opus_int                   firstFrameAfterReset,   /* I    Flag                                    */
147     const opus_int                   LPC_order,              /* I    LPC order                               */
148     const SKP_float                 x[],                    /* I    Input signal                            */
149     const opus_int                   subfr_length,           /* I    Subframe length incl preceeding samples */
150     const opus_int                   nb_subfr                /* I:   Number of subframes                     */
151 );
152
153 /* LTP analysis */
154 void silk_find_LTP_FLP(
155           SKP_float b[ MAX_NB_SUBFR * LTP_ORDER ],          /* O    LTP coefs                               */
156           SKP_float WLTP[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* O    Weight for LTP quantization       */
157           SKP_float *LTPredCodGain,                         /* O    LTP coding gain                         */
158     const SKP_float r_lpc[],                                /* I    LPC residual                            */
159     const opus_int   lag[  MAX_NB_SUBFR ],                   /* I    LTP lags                                */
160     const SKP_float Wght[ MAX_NB_SUBFR ],                   /* I    Weights                                 */
161     const opus_int   subfr_length,                           /* I    Subframe length                         */
162     const opus_int   nb_subfr,                               /* I    number of subframes                     */
163     const opus_int   mem_offset                              /* I    Number of samples in LTP memory         */
164 );
165
166 void silk_LTP_analysis_filter_FLP(
167           SKP_float         *LTP_res,                   /* O    LTP res MAX_NB_SUBFR*(pre_lgth+subfr_lngth) */
168     const SKP_float         *x,                         /* I    Input signal, with preceeding samples       */
169     const SKP_float         B[ LTP_ORDER * MAX_NB_SUBFR ],  /* I    LTP coefficients for each subframe      */
170     const opus_int           pitchL[   MAX_NB_SUBFR ],   /* I    Pitch lags                                  */
171     const SKP_float         invGains[ MAX_NB_SUBFR ],   /* I    Inverse quantization gains                  */
172     const opus_int           subfr_length,               /* I    Length of each subframe                     */
173     const opus_int           nb_subfr,                   /* I    number of subframes                         */
174     const opus_int           pre_length                  /* I    Preceeding samples for each subframe        */
175 );
176
177 /* Calculates residual energies of input subframes where all subframes have LPC_order   */
178 /* of preceeding samples                                                                */
179 void silk_residual_energy_FLP(
180           SKP_float             nrgs[ MAX_NB_SUBFR ],   /* O    Residual energy per subframe            */
181     const SKP_float             x[],                    /* I    Input signal                            */
182           SKP_float             a[ 2 ][ MAX_LPC_ORDER ],/* I    AR coefs for each frame half            */
183     const SKP_float             gains[],                /* I    Quantization gains                      */
184     const opus_int               subfr_length,           /* I    Subframe length                         */
185     const opus_int               nb_subfr,               /* I    number of subframes                     */
186     const opus_int               LPC_order               /* I    LPC order                               */
187 );
188
189 /* 16th order LPC analysis filter */
190 void silk_LPC_analysis_filter_FLP(
191           SKP_float                 r_LPC[],            /* O    LPC residual signal                     */
192     const SKP_float                 PredCoef[],         /* I    LPC coefficients                        */
193     const SKP_float                 s[],                /* I    Input signal                            */
194     const opus_int                   length,             /* I    Length of input signal                  */
195     const opus_int                   Order               /* I    LPC order                               */
196 );
197
198 /* LTP tap quantizer */
199 void silk_quant_LTP_gains_FLP(
200           SKP_float B[ MAX_NB_SUBFR * LTP_ORDER ],              /* I/O  (Un-)quantized LTP gains                */
201           opus_int8  cbk_index[ MAX_NB_SUBFR ],                  /* O    Codebook index                          */
202           opus_int8  *periodicity_index,                         /* O    Periodicity index                       */
203     const SKP_float W[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ],  /* I    Error weights                           */
204     const opus_int   mu_Q10,                                     /* I    Mu value (R/D tradeoff)                 */
205     const opus_int   lowComplexity,                              /* I    Flag for low complexity                 */
206     const opus_int   nb_subfr                                    /* I    number of subframes                     */
207 );
208
209 /******************/
210 /* NLSF Quantizer */
211 /******************/
212 /* Limit, stabilize, and quantize NLSFs */
213 void silk_process_NLSFs_FLP(
214     silk_encoder_state              *psEncC,                            /* I/O  Encoder state                               */
215     SKP_float                       PredCoef[ 2 ][ MAX_LPC_ORDER ],     /* O    Prediction coefficients                     */
216     opus_int16                       NLSF_Q15[      MAX_LPC_ORDER ],     /* I/O  Normalized LSFs (quant out) (0 - (2^15-1))  */
217     const opus_int16                 prev_NLSF_Q15[ MAX_LPC_ORDER ]      /* I    Previous Normalized LSFs (0 - (2^15-1))     */
218 );
219
220 /* Residual energy: nrg = wxx - 2 * wXx * c + c' * wXX * c */
221 SKP_float silk_residual_energy_covar_FLP(           /* O    Weighted residual energy                */
222     const SKP_float                 *c,                 /* I    Filter coefficients                     */
223           SKP_float                 *wXX,               /* I/O  Weighted correlation matrix, reg. out   */
224     const SKP_float                 *wXx,               /* I    Weighted correlation vector             */
225     const SKP_float                 wxx,                /* I    Weighted correlation value              */
226     const opus_int                   D                   /* I    Dimension                               */
227 );
228
229 /* Entropy constrained MATRIX-weighted VQ, for a single input data vector */
230 void silk_VQ_WMat_EC_FLP(
231           opus_int                   *ind,               /* O    Index of best codebook vector           */
232           SKP_float                 *rate_dist,         /* O    Best weighted quant. error + mu * rate  */
233     const SKP_float                 *in,                /* I    Input vector to be quantized            */
234     const SKP_float                 *W,                 /* I    Weighting matrix                        */
235     const opus_int16                 *cb,                /* I    Codebook                                */
236     const opus_int16                 *cl_Q6,             /* I    Code length for each codebook vector    */
237     const SKP_float                 mu,                 /* I    Tradeoff between WSSE and rate          */
238     const opus_int                   L                   /* I    Number of vectors in codebook           */
239 );
240
241 /* Processing of gains */
242 void silk_process_gains_FLP(
243     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
244     silk_encoder_control_FLP        *psEncCtrl          /* I/O  Encoder control FLP                     */
245 );
246
247 /******************/
248 /* Linear Algebra */
249 /******************/
250 /* Calculates correlation matrix X'*X */
251 void silk_corrMatrix_FLP(
252     const SKP_float                 *x,                 /* I    x vector [ L+order-1 ] used to create X */
253     const opus_int                   L,                  /* I    Length of vectors                       */
254     const opus_int                   Order,              /* I    Max lag for correlation                 */
255           SKP_float                 *XX                 /* O    X'*X correlation matrix [order x order] */
256 );
257
258 /* Calculates correlation vector X'*t */
259 void silk_corrVector_FLP(
260     const SKP_float                 *x,                 /* I    x vector [L+order-1] used to create X   */
261     const SKP_float                 *t,                 /* I    Target vector [L]                       */
262     const opus_int                   L,                  /* I    Length of vecors                        */
263     const opus_int                   Order,              /* I    Max lag for correlation                 */
264           SKP_float                 *Xt                 /* O    X'*t correlation vector [order]         */
265 );
266
267 /* Add noise to matrix diagonal */
268 void silk_regularize_correlations_FLP(
269           SKP_float                 *XX,                /* I/O  Correlation matrices                    */
270           SKP_float                 *xx,                /* I/O  Correlation values                      */
271     const SKP_float                 noise,              /* I    Noise energy to add                     */
272     const opus_int                   D                   /* I    Dimension of XX                         */
273 );
274
275 /* Function to solve linear equation Ax = b, where A is an MxM symmetric matrix */
276 void silk_solve_LDL_FLP(
277           SKP_float                 *A,                 /* I/O  Symmetric square matrix, out: reg.      */
278     const opus_int                   M,                  /* I    Size of matrix                          */
279     const SKP_float                 *b,                 /* I    Pointer to b vector                     */
280           SKP_float                 *x                  /* O    Pointer to x solution vector            */
281 );
282
283 /* Apply sine window to signal vector.                                                                  */
284 /* Window types:                                                                                        */
285 /*  1 -> sine window from 0 to pi/2                                                                     */
286 /*  2 -> sine window from pi/2 to pi                                                                    */
287 void silk_apply_sine_window_FLP(
288           SKP_float                 px_win[],           /* O    Pointer to windowed signal              */
289     const SKP_float                 px[],               /* I    Pointer to input signal                 */
290     const opus_int                   win_type,           /* I    Selects a window type                   */
291     const opus_int                   length              /* I    Window length, multiple of 4            */
292 );
293
294 /* Wrapper functions. Call flp / fix code */
295
296 /* Convert AR filter coefficients to NLSF parameters */
297 void silk_A2NLSF_FLP(
298           opus_int16                 *NLSF_Q15,          /* O    NLSF vector      [ LPC_order ]          */
299     const SKP_float                 *pAR,               /* I    LPC coefficients [ LPC_order ]          */
300     const opus_int                   LPC_order           /* I    LPC order                               */
301 );
302
303 /* Convert NLSF parameters to AR prediction filter coefficients */
304 void silk_NLSF2A_FLP(
305           SKP_float                 *pAR,               /* O    LPC coefficients [ LPC_order ]          */
306     const opus_int16                 *NLSF_Q15,          /* I    NLSF vector      [ LPC_order ]          */
307     const opus_int                   LPC_order           /* I    LPC order                               */
308 );
309
310 /****************************************/
311 /* Floating-point Silk NSQ wrapper      */
312 /****************************************/
313 void silk_NSQ_wrapper_FLP(
314     silk_encoder_state_FLP          *psEnc,         /* I/O  Encoder state FLP                           */
315     silk_encoder_control_FLP        *psEncCtrl,     /* I/O  Encoder control FLP                         */
316     SideInfoIndices                 *psIndices,     /* I/O  Quantization indices                        */
317     silk_nsq_state                  *psNSQ,         /* I/O  Noise Shaping Quantzation state             */
318           opus_int8                  pulses[],       /* O    Quantized pulse signal                      */
319     const SKP_float                 x[]             /* I    Prefiltered input signal                    */
320 );
321
322 #ifdef __cplusplus
323 }
324 #endif
325
326 #endif