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