SILK update with LBRR and some bugfixes
[opus.git] / src_FIX / SKP_Silk_main_FIX.h
1 /***********************************************************************\r
2 Copyright (c) 2006-2011, Skype Limited. All rights reserved. \r
3 Redistribution and use in source and binary forms, with or without \r
4 modification, (subject to the limitations in the disclaimer below) \r
5 are permitted provided that the following conditions are met:\r
6 - Redistributions of source code must retain the above copyright notice,\r
7 this list of conditions and the following disclaimer.\r
8 - Redistributions in binary form must reproduce the above copyright \r
9 notice, this list of conditions and the following disclaimer in the \r
10 documentation and/or other materials provided with the distribution.\r
11 - Neither the name of Skype Limited, nor the names of specific \r
12 contributors, may be used to endorse or promote products derived from \r
13 this software without specific prior written permission.\r
14 NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED \r
15 BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND \r
16 CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,\r
17 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND \r
18 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE \r
19 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, \r
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF \r
22 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON \r
23 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE \r
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
26 ***********************************************************************/\r
27 \r
28 #ifndef SKP_SILK_MAIN_FIX_H\r
29 #define SKP_SILK_MAIN_FIX_H\r
30 \r
31 #include "SKP_Silk_SigProc_FIX.h"\r
32 #include "SKP_Silk_structs_FIX.h"\r
33 #include "SKP_Silk_main.h"\r
34 #include "SKP_Silk_PLC.h"\r
35 #include "SKP_debug.h"\r
36 #include "entenc.h"\r
37 \r
38 #ifndef FORCE_CPP_BUILD\r
39 #ifdef __cplusplus\r
40 extern "C"\r
41 {\r
42 #endif\r
43 #endif\r
44 \r
45 /*********************/\r
46 /* Encoder Functions */\r
47 /*********************/\r
48 \r
49 /* High-pass filter with cutoff frequency adaptation based on pitch lag statistics */\r
50 void SKP_Silk_HP_variable_cutoff_FIX(\r
51     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  Encoder state                               */\r
52     SKP_Silk_encoder_control_FIX    *psEncCtrl,     /* I/O  Encoder control                             */\r
53     SKP_int16                       *out,           /* O    high-pass filtered output signal            */\r
54     const SKP_int16                 *in             /* I    input signal                                */\r
55 );\r
56 \r
57 /* Encoder main function */\r
58 SKP_int SKP_Silk_encode_frame_FIX( \r
59     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O  Pointer to Silk FIX encoder state       */\r
60     SKP_int32                       *pnBytesOut,        /*   O  Pointer to number of payload bytes;     */\r
61     ec_enc                          *psRangeEnc         /* I/O  compressor data structure               */\r
62 );\r
63 \r
64 /* Low Bitrate Redundancy (LBRR) encoding. Reuse all parameters but encode with lower bitrate           */\r
65 void SKP_Silk_LBRR_encode_FIX(\r
66     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  Pointer to Silk FIX encoder state           */\r
67     SKP_Silk_encoder_control_FIX    *psEncCtrl,     /* I/O  Pointer to Silk FIX encoder control struct  */\r
68     const SKP_int16                 xfw[]           /* I    Input signal                                */\r
69 );\r
70 \r
71 /* Initializes the Silk encoder state */\r
72 SKP_int SKP_Silk_init_encoder_FIX(\r
73     SKP_Silk_encoder_state_FIX  *psEnc              /* I/O  Pointer to Silk FIX encoder state           */\r
74 );\r
75 \r
76 /* Control the Silk encoder */\r
77 SKP_int SKP_Silk_control_encoder_FIX( \r
78     SKP_Silk_encoder_state_FIX  *psEnc,                 /* I/O  Pointer to Silk encoder state           */\r
79     const SKP_int               PacketSize_ms,          /* I    Packet length (ms)                      */\r
80     const SKP_int32             TargetRate_bps,         /* I    Target max bitrate (bps)                */\r
81     const SKP_int               PacketLoss_perc,        /* I    Packet loss rate (in percent)           */\r
82     const SKP_int               Complexity              /* I    Complexity (0-10)                       */\r
83 );\r
84 \r
85 /****************/\r
86 /* Prefiltering */\r
87 /****************/\r
88 void SKP_Silk_prefilter_FIX(\r
89     SKP_Silk_encoder_state_FIX          *psEnc,         /* I/O  Encoder state                               */\r
90     const SKP_Silk_encoder_control_FIX  *psEncCtrl,     /* I    Encoder control                             */\r
91     SKP_int16                           xw[],           /* O    Weighted signal                             */\r
92     const SKP_int16                     x[]             /* I    Speech signal                               */\r
93 );\r
94 \r
95 /**************************/\r
96 /* Noise shaping analysis */\r
97 /**************************/\r
98 /* Compute noise shaping coefficients and initial gain values */\r
99 void SKP_Silk_noise_shape_analysis_FIX(\r
100     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  Encoder state FIX                           */\r
101     SKP_Silk_encoder_control_FIX    *psEncCtrl,     /* I/O  Encoder control FIX                         */\r
102     const SKP_int16                 *pitch_res,     /* I    LPC residual from pitch analysis            */\r
103     const SKP_int16                 *x              /* I    Input signal [ frame_length + la_shape ]    */\r
104 );\r
105 \r
106 /* Autocorrelations for a warped frequency axis */\r
107 void SKP_Silk_warped_autocorrelation_FIX(\r
108           SKP_int32                 *corr,              /* O    Result [order + 1]                      */\r
109           SKP_int                   *scale,             /* O    Scaling of the correlation vector       */\r
110     const SKP_int16                 *input,             /* I    Input data to correlate                 */\r
111     const SKP_int                   warping_Q16,        /* I    Warping coefficient                     */\r
112     const SKP_int                   length,             /* I    Length of input                         */\r
113     const SKP_int                   order               /* I    Correlation order (even)                */\r
114 );\r
115 \r
116 /* Calculation of LTP state scaling */\r
117 void SKP_Silk_LTP_scale_ctrl_FIX(\r
118     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  encoder state                               */\r
119     SKP_Silk_encoder_control_FIX    *psEncCtrl      /* I/O  encoder control                             */\r
120 );\r
121 \r
122 /**********************************************/\r
123 /* Prediction Analysis                        */\r
124 /**********************************************/\r
125 /* Find pitch lags */\r
126 void SKP_Silk_find_pitch_lags_FIX(\r
127     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  encoder state                               */\r
128     SKP_Silk_encoder_control_FIX    *psEncCtrl,     /* I/O  encoder control                             */\r
129     SKP_int16                       res[],          /* O    residual                                    */\r
130     const SKP_int16                 x[]             /* I    Speech signal                               */\r
131 );\r
132 \r
133 /* Find LPC and LTP coefficients */\r
134 void SKP_Silk_find_pred_coefs_FIX(\r
135     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  encoder state                               */\r
136     SKP_Silk_encoder_control_FIX    *psEncCtrl,     /* I/O  encoder control                             */\r
137     const SKP_int16                 res_pitch[],    /* I    Residual from pitch analysis                */\r
138     const SKP_int16                 x[]             /* I    Speech signal                               */\r
139 );\r
140 \r
141 /* LPC analysis */\r
142 void SKP_Silk_find_LPC_FIX(\r
143     SKP_int             NLSF_Q15[],                 /* O    NLSFs                                                                       */\r
144     SKP_int8            *interpIndex,               /* O    NLSF interpolation index, only used for NLSF interpolation                  */\r
145     const SKP_int       prev_NLSFq_Q15[],           /* I    previous NLSFs, only used for NLSF interpolation                            */\r
146     const SKP_int       useInterpolatedNLSFs,       /* I    Flag                                                                        */\r
147     const SKP_int       LPC_order,                  /* I    LPC order                                                                   */\r
148     const SKP_int16     x[],                        /* I    Input signal                                                                */\r
149     const SKP_int       subfr_length,               /* I    Input signal subframe length including preceeding samples                   */\r
150     const SKP_int       nb_subfr                    /* I:   Number of subframes                                                         */\r
151 );\r
152 \r
153 /* LTP analysis */\r
154 void SKP_Silk_find_LTP_FIX(\r
155     SKP_int16           b_Q14[ MAX_NB_SUBFR * LTP_ORDER ],              /* O    LTP coefs                                                   */\r
156     SKP_int32           WLTP[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ],   /* O    Weight for LTP quantization                                 */\r
157     SKP_int             *LTPredCodGain_Q7,                              /* O    LTP coding gain                                             */\r
158     const SKP_int16     r_lpc[],                                        /* I    residual signal after LPC signal + state for first 10 ms    */\r
159     const SKP_int       lag[ MAX_NB_SUBFR ],                            /* I    LTP lags                                                    */\r
160     const SKP_int32     Wght_Q15[ MAX_NB_SUBFR ],                       /* I    weights                                                     */\r
161     const SKP_int       subfr_length,                                   /* I    subframe length                                             */\r
162     const SKP_int       nb_subfr,                                       /* I    number of subframes                                         */\r
163     const SKP_int       mem_offset,                                     /* I    number of samples in LTP memory                             */\r
164     SKP_int             corr_rshifts[ MAX_NB_SUBFR ]                    /* O    right shifts applied to correlations                        */\r
165 );\r
166 \r
167 void SKP_Silk_LTP_analysis_filter_FIX(\r
168     SKP_int16           *LTP_res,                               /* O:   LTP residual signal of length MAX_NB_SUBFR * ( pre_length + subfr_length )  */\r
169     const SKP_int16     *x,                                     /* I:   Pointer to input signal with at least max( pitchL ) preceeding samples      */\r
170     const SKP_int16     LTPCoef_Q14[ LTP_ORDER * MAX_NB_SUBFR ],/* I:   LTP_ORDER LTP coefficients for each MAX_NB_SUBFR subframe                   */\r
171     const SKP_int       pitchL[ MAX_NB_SUBFR ],                 /* I:   Pitch lag, one for each subframe                                            */\r
172     const SKP_int32     invGains_Q16[ MAX_NB_SUBFR ],           /* I:   Inverse quantization gains, one for each subframe                           */\r
173     const SKP_int       subfr_length,                           /* I:   Length of each subframe                                                     */\r
174     const SKP_int       nb_subfr,                               /* I:   Number of subframes                                                         */\r
175     const SKP_int       pre_length                              /* I:   Length of the preceeding samples starting at &x[0] for each subframe        */\r
176 );\r
177 \r
178 /* Calculates residual energies of input subframes where all subframes have LPC_order   */\r
179 /* of preceeding samples                                                                */\r
180 void SKP_Silk_residual_energy_FIX(\r
181           SKP_int32 nrgs[ MAX_NB_SUBFR ],           /* O    Residual energy per subframe    */\r
182           SKP_int   nrgsQ[ MAX_NB_SUBFR ],          /* O    Q value per subframe            */\r
183     const SKP_int16 x[],                            /* I    Input signal                    */\r
184     const SKP_int16 a_Q12[ 2 ][ MAX_LPC_ORDER ],    /* I    AR coefs for each frame half    */\r
185     const SKP_int32 gains[ MAX_NB_SUBFR ],          /* I    Quantization gains              */\r
186     const SKP_int   subfr_length,                   /* I    Subframe length                 */\r
187     const SKP_int   nb_subfr,                       /* I    Number of subframes             */\r
188     const SKP_int   LPC_order                       /* I    LPC order                       */\r
189 );\r
190 \r
191 /******************/\r
192 /* NLSF Quantizer */\r
193 /******************/\r
194 /* Limit, stabilize, convert and quantize NLSFs.    */ \r
195 void SKP_Silk_process_NLSFs_FIX(\r
196     SKP_Silk_encoder_state_FIX      *psEnc,     /* I/O  encoder state                               */\r
197     SKP_Silk_encoder_control_FIX    *psEncCtrl, /* I/O  encoder control                             */\r
198     SKP_int                         *pNLSF_Q15  /* I/O  Normalized LSFs (quant out) (0 - (2^15-1))  */\r
199 );\r
200 \r
201 /* NLSF vector encoder */\r
202 void SKP_Silk_NLSF_MSVQ_encode_FIX(\r
203           SKP_int8                  *NLSFIndices,           /* O    Codebook path vector [ CB_STAGES ]      */\r
204           SKP_int                   *pNLSF_Q15,             /* I/O  Quantized NLSF vector [ LPC_ORDER ]     */\r
205     const SKP_Silk_NLSF_CB_struct   *psNLSF_CB,             /* I    Codebook object                         */\r
206     const SKP_int                   *pNLSF_q_Q15_prev,      /* I    Prev. quantized NLSF vector [LPC_ORDER] */\r
207     const SKP_int                   *pW_Q6,                 /* I    NLSF weight vector [ LPC_ORDER ]        */\r
208     const SKP_int                   NLSF_mu_Q15,            /* I    Rate weight for the RD optimization     */\r
209     const SKP_int                   NLSF_mu_fluc_red_Q16,   /* I    Fluctuation reduction error weight      */\r
210     const SKP_int                   NLSF_MSVQ_Survivors,    /* I    Max survivors from each stage           */\r
211     const SKP_int                   LPC_order,              /* I    LPC order                               */\r
212     const SKP_int                   deactivate_fluc_red     /* I    Deactivate fluctuation reduction        */\r
213 );\r
214 \r
215 /* Rate-Distortion calculations for multiple input data vectors */\r
216 void SKP_Silk_NLSF_VQ_rate_distortion_FIX(\r
217     SKP_int32                       *pRD_Q20,           /* O    Rate-distortion values [psNLSF_CBS->nVectors*N] */\r
218     const SKP_Silk_NLSF_CBS         *psNLSF_CBS,        /* I    NLSF codebook stage struct                      */\r
219     const SKP_int16                 *in_Q15,            /* I    Input vectors to be quantized                   */\r
220     const SKP_int                   *w_Q6,              /* I    Weight vector                                   */\r
221     const SKP_int32                 *rate_acc_Q5,       /* I    Accumulated rates from previous stage           */\r
222     const SKP_int                   mu_Q15,             /* I    Weight between weighted error and rate          */\r
223     const SKP_int                   N,                  /* I    Number of input vectors to be quantized         */\r
224     const SKP_int                   LPC_order           /* I    LPC order                                       */\r
225 );\r
226 \r
227 /* Compute weighted quantization errors for an LPC_order element input vector, over one codebook stage */\r
228 void SKP_Silk_NLSF_VQ_sum_error_FIX(\r
229     SKP_int32                       *err_Q20,           /* O    Weighted quantization errors  [N*K]         */\r
230     const SKP_int16                 *in_Q15,            /* I    Input vectors to be quantized [N*LPC_order] */\r
231     const SKP_int                   *w_Q6,              /* I    Weighting vectors             [N*LPC_order] */\r
232     const SKP_int8                  *pCB_Q9,            /* I    Codebook vectors              [K*LPC_order] */\r
233     const SKP_int                   N,                  /* I    Number of input vectors                     */\r
234     const SKP_int                   K,                  /* I    Number of codebook vectors                  */\r
235     const SKP_int                   LPC_order           /* I    Number of LPCs                              */\r
236 );\r
237 \r
238 /* Residual energy: nrg = wxx - 2 * wXx * c + c' * wXX * c */\r
239 SKP_int32 SKP_Silk_residual_energy16_covar_FIX(\r
240     const SKP_int16                 *c,                 /* I    Prediction vector                           */\r
241     const SKP_int32                 *wXX,               /* I    Correlation matrix                          */\r
242     const SKP_int32                 *wXx,               /* I    Correlation vector                          */\r
243     SKP_int32                       wxx,                /* I    Signal energy                               */\r
244     SKP_int                         D,                  /* I    Dimension                                   */\r
245     SKP_int                         cQ                  /* I    Q value for c vector 0 - 15                 */\r
246 );\r
247 \r
248 /* Processing of gains */\r
249 void SKP_Silk_process_gains_FIX(\r
250     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  Encoder state                               */\r
251     SKP_Silk_encoder_control_FIX    *psEncCtrl      /* I/O  Encoder control                             */\r
252 );\r
253 \r
254 /******************/\r
255 /* Linear Algebra */\r
256 /******************/\r
257 /* Calculates correlation matrix X'*X */\r
258 void SKP_Silk_corrMatrix_FIX(\r
259     const SKP_int16                 *x,         /* I    x vector [L + order - 1] used to form data matrix X */\r
260     const SKP_int                   L,          /* I    Length of vectors                                   */\r
261     const SKP_int                   order,      /* I    Max lag for correlation                             */\r
262     const SKP_int                   head_room,  /* I    Desired headroom                                    */\r
263     SKP_int32                       *XX,        /* O    Pointer to X'*X correlation matrix [ order x order ]*/\r
264     SKP_int                         *rshifts    /* I/O  Right shifts of correlations                        */\r
265 );\r
266 \r
267 /* Calculates correlation vector X'*t */\r
268 void SKP_Silk_corrVector_FIX(\r
269     const SKP_int16                 *x,         /* I    x vector [L + order - 1] used to form data matrix X */\r
270     const SKP_int16                 *t,         /* I    Target vector [L]                                   */\r
271     const SKP_int                   L,          /* I    Length of vectors                                   */\r
272     const SKP_int                   order,      /* I    Max lag for correlation                             */\r
273     SKP_int32                       *Xt,        /* O    Pointer to X'*t correlation vector [order]          */\r
274     const SKP_int                   rshifts     /* I    Right shifts of correlations                        */\r
275 );\r
276 \r
277 /* Add noise to matrix diagonal */\r
278 void SKP_Silk_regularize_correlations_FIX(\r
279     SKP_int32                       *XX,                /* I/O  Correlation matrices                        */\r
280     SKP_int32                       *xx,                /* I/O  Correlation values                          */\r
281     SKP_int32                       noise,              /* I    Noise to add                                */\r
282     SKP_int                         D                   /* I    Dimension of XX                             */\r
283 );\r
284 \r
285 /* Solves Ax = b, assuming A is symmetric */\r
286 void SKP_Silk_solve_LDL_FIX(\r
287     SKP_int32                       *A,                 /* I    Pointer to symetric square matrix A         */\r
288     SKP_int                         M,                  /* I    Size of matrix                              */\r
289     const SKP_int32                 *b,                 /* I    Pointer to b vector                         */\r
290     SKP_int32                       *x_Q16              /* O    Pointer to x solution vector                */\r
291 );\r
292 \r
293 #ifndef FORCE_CPP_BUILD\r
294 #ifdef __cplusplus\r
295 }\r
296 #endif /* __cplusplus */\r
297 #endif /* FORCE_CPP_BUILD */\r
298 #endif /* SKP_SILK_MAIN_FIX_H */\r