Renaming the SKP_ prefix to silk_
[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 silk_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     const opus_int                   force_fs_kHz
84 );
85
86 /****************/
87 /* Prefiltering */
88 /****************/
89 void silk_prefilter_FLP(
90     silk_encoder_state_FLP              *psEnc,         /* I/O  Encoder state FLP                       */
91     const silk_encoder_control_FLP      *psEncCtrl,     /* I    Encoder control FLP                     */
92           silk_float                     xw[],           /* O    Weighted signal                         */
93     const silk_float                     x[]             /* I    Speech signal                           */
94 );
95
96 /**************************/
97 /* Noise shaping analysis */
98 /**************************/
99 /* Compute noise shaping coefficients and initial gain values */
100 void silk_noise_shape_analysis_FLP(
101     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
102     silk_encoder_control_FLP        *psEncCtrl,         /* I/O  Encoder control FLP                     */
103     const silk_float                 *pitch_res,         /* I    LPC residual from pitch analysis        */
104     const silk_float                 *x                  /* I    Input signal [frame_length + la_shape]  */
105 );
106
107 /* Autocorrelations for a warped frequency axis */
108 void silk_warped_autocorrelation_FLP(
109           silk_float                 *corr,              /* O    Result [order + 1]                      */
110     const silk_float                 *input,             /* I    Input data to correlate                 */
111     const silk_float                 warping,            /* I    Warping coefficient                     */
112     const opus_int                   length,             /* I    Length of input                         */
113     const opus_int                   order               /* I    Correlation order (even)                */
114 );
115
116 /* Calculation of LTP state scaling */
117 void silk_LTP_scale_ctrl_FLP(
118     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
119     silk_encoder_control_FLP        *psEncCtrl          /* I/O  Encoder control FLP                     */
120 );
121
122 /**********************************************/
123 /* Prediction Analysis                        */
124 /**********************************************/
125 /* Find pitch lags */
126 void silk_find_pitch_lags_FLP(
127     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
128     silk_encoder_control_FLP        *psEncCtrl,         /* I/O  Encoder control FLP                     */
129           silk_float                 res[],              /* O    Residual                                */
130     const silk_float                 x[]                 /* I    Speech signal                           */
131 );
132
133 /* Find LPC and LTP coefficients */
134 void silk_find_pred_coefs_FLP(
135     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
136     silk_encoder_control_FLP        *psEncCtrl,         /* I/O  Encoder control FLP                     */
137     const silk_float                 res_pitch[],        /* I    Residual from pitch analysis            */
138     const silk_float                 x[]                 /* I    Speech signal                           */
139 );
140
141 /* LPC analysis */
142 void silk_find_LPC_FLP(
143           opus_int16                 NLSF_Q15[],             /* O    NLSFs                                   */
144           opus_int8                  *interpIndex,           /* O    NLSF interp. index for NLSF interp.     */
145     const opus_int16                 prev_NLSFq_Q15[],       /* I    Previous NLSFs, for NLSF interpolation  */
146     const opus_int                   useInterpNLSFs,         /* I    Flag                                    */
147     const opus_int                   firstFrameAfterReset,   /* I    Flag                                    */
148     const opus_int                   LPC_order,              /* I    LPC order                               */
149     const silk_float                 x[],                    /* I    Input signal                            */
150     const opus_int                   subfr_length,           /* I    Subframe length incl preceeding samples */
151     const opus_int                   nb_subfr                /* I:   Number of subframes                     */
152 );
153
154 /* LTP analysis */
155 void silk_find_LTP_FLP(
156           silk_float b[ MAX_NB_SUBFR * LTP_ORDER ],          /* O    LTP coefs                               */
157           silk_float WLTP[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* O    Weight for LTP quantization       */
158           silk_float *LTPredCodGain,                         /* O    LTP coding gain                         */
159     const silk_float r_lpc[],                                /* I    LPC residual                            */
160     const opus_int   lag[  MAX_NB_SUBFR ],                   /* I    LTP lags                                */
161     const silk_float Wght[ MAX_NB_SUBFR ],                   /* I    Weights                                 */
162     const opus_int   subfr_length,                           /* I    Subframe length                         */
163     const opus_int   nb_subfr,                               /* I    number of subframes                     */
164     const opus_int   mem_offset                              /* I    Number of samples in LTP memory         */
165 );
166
167 void silk_LTP_analysis_filter_FLP(
168           silk_float         *LTP_res,                   /* O    LTP res MAX_NB_SUBFR*(pre_lgth+subfr_lngth) */
169     const silk_float         *x,                         /* I    Input signal, with preceeding samples       */
170     const silk_float         B[ LTP_ORDER * MAX_NB_SUBFR ],  /* I    LTP coefficients for each subframe      */
171     const opus_int           pitchL[   MAX_NB_SUBFR ],   /* I    Pitch lags                                  */
172     const silk_float         invGains[ MAX_NB_SUBFR ],   /* I    Inverse quantization gains                  */
173     const opus_int           subfr_length,               /* I    Length of each subframe                     */
174     const opus_int           nb_subfr,                   /* I    number of subframes                         */
175     const opus_int           pre_length                  /* I    Preceeding samples for each subframe        */
176 );
177
178 /* Calculates residual energies of input subframes where all subframes have LPC_order   */
179 /* of preceeding samples                                                                */
180 void silk_residual_energy_FLP(
181           silk_float             nrgs[ MAX_NB_SUBFR ],   /* O    Residual energy per subframe            */
182     const silk_float             x[],                    /* I    Input signal                            */
183           silk_float             a[ 2 ][ MAX_LPC_ORDER ],/* I    AR coefs for each frame half            */
184     const silk_float             gains[],                /* I    Quantization gains                      */
185     const opus_int               subfr_length,           /* I    Subframe length                         */
186     const opus_int               nb_subfr,               /* I    number of subframes                     */
187     const opus_int               LPC_order               /* I    LPC order                               */
188 );
189
190 /* 16th order LPC analysis filter */
191 void silk_LPC_analysis_filter_FLP(
192           silk_float                 r_LPC[],            /* O    LPC residual signal                     */
193     const silk_float                 PredCoef[],         /* I    LPC coefficients                        */
194     const silk_float                 s[],                /* I    Input signal                            */
195     const opus_int                   length,             /* I    Length of input signal                  */
196     const opus_int                   Order               /* I    LPC order                               */
197 );
198
199 /* LTP tap quantizer */
200 void silk_quant_LTP_gains_FLP(
201           silk_float B[ MAX_NB_SUBFR * LTP_ORDER ],              /* I/O  (Un-)quantized LTP gains                */
202           opus_int8  cbk_index[ MAX_NB_SUBFR ],                  /* O    Codebook index                          */
203           opus_int8  *periodicity_index,                         /* O    Periodicity index                       */
204     const silk_float W[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ],  /* I    Error weights                           */
205     const opus_int   mu_Q10,                                     /* I    Mu value (R/D tradeoff)                 */
206     const opus_int   lowComplexity,                              /* I    Flag for low complexity                 */
207     const opus_int   nb_subfr                                    /* I    number of subframes                     */
208 );
209
210 /******************/
211 /* NLSF Quantizer */
212 /******************/
213 /* Limit, stabilize, and quantize NLSFs */
214 void silk_process_NLSFs_FLP(
215     silk_encoder_state              *psEncC,                            /* I/O  Encoder state                               */
216     silk_float                       PredCoef[ 2 ][ MAX_LPC_ORDER ],     /* O    Prediction coefficients                     */
217     opus_int16                       NLSF_Q15[      MAX_LPC_ORDER ],     /* I/O  Normalized LSFs (quant out) (0 - (2^15-1))  */
218     const opus_int16                 prev_NLSF_Q15[ MAX_LPC_ORDER ]      /* I    Previous Normalized LSFs (0 - (2^15-1))     */
219 );
220
221 /* Residual energy: nrg = wxx - 2 * wXx * c + c' * wXX * c */
222 silk_float silk_residual_energy_covar_FLP(           /* O    Weighted residual energy                */
223     const silk_float                 *c,                 /* I    Filter coefficients                     */
224           silk_float                 *wXX,               /* I/O  Weighted correlation matrix, reg. out   */
225     const silk_float                 *wXx,               /* I    Weighted correlation vector             */
226     const silk_float                 wxx,                /* I    Weighted correlation value              */
227     const opus_int                   D                   /* I    Dimension                               */
228 );
229
230 /* Entropy constrained MATRIX-weighted VQ, for a single input data vector */
231 void silk_VQ_WMat_EC_FLP(
232           opus_int                   *ind,               /* O    Index of best codebook vector           */
233           silk_float                 *rate_dist,         /* O    Best weighted quant. error + mu * rate  */
234     const silk_float                 *in,                /* I    Input vector to be quantized            */
235     const silk_float                 *W,                 /* I    Weighting matrix                        */
236     const opus_int16                 *cb,                /* I    Codebook                                */
237     const opus_int16                 *cl_Q6,             /* I    Code length for each codebook vector    */
238     const silk_float                 mu,                 /* I    Tradeoff between WSSE and rate          */
239     const opus_int                   L                   /* I    Number of vectors in codebook           */
240 );
241
242 /* Processing of gains */
243 void silk_process_gains_FLP(
244     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
245     silk_encoder_control_FLP        *psEncCtrl          /* I/O  Encoder control FLP                     */
246 );
247
248 /******************/
249 /* Linear Algebra */
250 /******************/
251 /* Calculates correlation matrix X'*X */
252 void silk_corrMatrix_FLP(
253     const silk_float                 *x,                 /* I    x vector [ L+order-1 ] used to create X */
254     const opus_int                   L,                  /* I    Length of vectors                       */
255     const opus_int                   Order,              /* I    Max lag for correlation                 */
256           silk_float                 *XX                 /* O    X'*X correlation matrix [order x order] */
257 );
258
259 /* Calculates correlation vector X'*t */
260 void silk_corrVector_FLP(
261     const silk_float                 *x,                 /* I    x vector [L+order-1] used to create X   */
262     const silk_float                 *t,                 /* I    Target vector [L]                       */
263     const opus_int                   L,                  /* I    Length of vecors                        */
264     const opus_int                   Order,              /* I    Max lag for correlation                 */
265           silk_float                 *Xt                 /* O    X'*t correlation vector [order]         */
266 );
267
268 /* Add noise to matrix diagonal */
269 void silk_regularize_correlations_FLP(
270           silk_float                 *XX,                /* I/O  Correlation matrices                    */
271           silk_float                 *xx,                /* I/O  Correlation values                      */
272     const silk_float                 noise,              /* I    Noise energy to add                     */
273     const opus_int                   D                   /* I    Dimension of XX                         */
274 );
275
276 /* Function to solve linear equation Ax = b, where A is an MxM symmetric matrix */
277 void silk_solve_LDL_FLP(
278           silk_float                 *A,                 /* I/O  Symmetric square matrix, out: reg.      */
279     const opus_int                   M,                  /* I    Size of matrix                          */
280     const silk_float                 *b,                 /* I    Pointer to b vector                     */
281           silk_float                 *x                  /* O    Pointer to x solution vector            */
282 );
283
284 /* Apply sine window to signal vector.                                                                  */
285 /* Window types:                                                                                        */
286 /*  1 -> sine window from 0 to pi/2                                                                     */
287 /*  2 -> sine window from pi/2 to pi                                                                    */
288 void silk_apply_sine_window_FLP(
289           silk_float                 px_win[],           /* O    Pointer to windowed signal              */
290     const silk_float                 px[],               /* I    Pointer to input signal                 */
291     const opus_int                   win_type,           /* I    Selects a window type                   */
292     const opus_int                   length              /* I    Window length, multiple of 4            */
293 );
294
295 /* Wrapper functions. Call flp / fix code */
296
297 /* Convert AR filter coefficients to NLSF parameters */
298 void silk_A2NLSF_FLP(
299           opus_int16                 *NLSF_Q15,          /* O    NLSF vector      [ LPC_order ]          */
300     const silk_float                 *pAR,               /* I    LPC coefficients [ LPC_order ]          */
301     const opus_int                   LPC_order           /* I    LPC order                               */
302 );
303
304 /* Convert NLSF parameters to AR prediction filter coefficients */
305 void silk_NLSF2A_FLP(
306           silk_float                 *pAR,               /* O    LPC coefficients [ LPC_order ]          */
307     const opus_int16                 *NLSF_Q15,          /* I    NLSF vector      [ LPC_order ]          */
308     const opus_int                   LPC_order           /* I    LPC order                               */
309 );
310
311 /****************************************/
312 /* Floating-point Silk NSQ wrapper      */
313 /****************************************/
314 void silk_NSQ_wrapper_FLP(
315     silk_encoder_state_FLP          *psEnc,         /* I/O  Encoder state FLP                           */
316     silk_encoder_control_FLP        *psEncCtrl,     /* I/O  Encoder control FLP                         */
317     SideInfoIndices                 *psIndices,     /* I/O  Quantization indices                        */
318     silk_nsq_state                  *psNSQ,         /* I/O  Noise Shaping Quantzation state             */
319           opus_int8                  pulses[],       /* O    Quantized pulse signal                      */
320     const silk_float                 x[]             /* I    Prefiltered input signal                    */
321 );
322
323 #ifdef __cplusplus
324 }
325 #endif
326
327 #endif