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