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