Initial Skype commit taken from FreeSwitch, which got it from the IETF draft.
[opus.git] / src / SKP_Silk_main_FIX.h
1 /***********************************************************************\r
2 Copyright (c) 2006-2010, 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 <stdlib.h>\r
32 #include "SKP_Silk_SigProc_FIX.h"\r
33 #include "SKP_Silk_structs_FIX.h"\r
34 #include "SKP_Silk_main.h"\r
35 #include "SKP_Silk_define_FIX.h"\r
36 #include "SKP_Silk_PLC.h"\r
37 #define TIC(TAG_NAME)\r
38 #define TOC(TAG_NAME)\r
39 \r
40 #ifndef FORCE_CPP_BUILD\r
41 #ifdef __cplusplus\r
42 extern "C"\r
43 {\r
44 #endif\r
45 #endif\r
46 \r
47 /*********************/\r
48 /* Encoder Functions */\r
49 /*********************/\r
50 \r
51 /* Initializes the Silk encoder state */\r
52 SKP_int SKP_Silk_init_encoder_FIX(\r
53     SKP_Silk_encoder_state_FIX  *psEnc              /* I/O  Pointer to Silk FIX encoder state           */\r
54 );\r
55 \r
56 /* Control the Silk encoder */\r
57 SKP_int SKP_Silk_control_encoder_FIX( \r
58     SKP_Silk_encoder_state_FIX  *psEnc,             /* I/O  Pointer to Silk FIX encoder state               */\r
59     const SKP_int               API_fs_kHz,         /* I    External (API) sampling rate (kHz)              */\r
60     const SKP_int               PacketSize_ms,      /* I    Packet length (ms)                              */\r
61     SKP_int32                   TargetRate_bps,     /* I    Target max bitrate (bps) (used if SNR_dB == 0)  */\r
62     const SKP_int               PacketLoss_perc,    /* I    Packet loss rate (in percent)                   */\r
63     const SKP_int               INBandFec_enabled,  /* I    Enable (1) / disable (0) inband FEC             */\r
64     const SKP_int               DTX_enabled,        /* I    Enable / disable DTX                            */\r
65     const SKP_int               InputFramesize_ms,  /* I    Inputframe in ms                                */\r
66     const SKP_int               Complexity          /* I    Complexity (0->low; 1->medium; 2->high)         */\r
67 );\r
68 \r
69 /* Encoder main function */\r
70 SKP_int SKP_Silk_encode_frame_FIX( \r
71     SKP_Silk_encoder_state_FIX      *psEnc,             /* I/O  Pointer to Silk FIX encoder state           */\r
72     SKP_uint8                       *pCode,             /* O    Pointer to payload                          */\r
73     SKP_int16                       *pnBytesOut,        /* I/O  Pointer to number of payload bytes;         */\r
74                                                         /*      input: max length; output: used             */\r
75     const SKP_int16                 *pIn                /* I    Pointer to input speech frame               */\r
76 );\r
77 \r
78 /* Low BitRate Redundancy encoding functionality. Reuse all parameters but encode with lower bitrate */\r
79 void SKP_Silk_LBRR_encode_FIX(\r
80     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  Pointer to Silk FIX encoder state           */\r
81     SKP_Silk_encoder_control_FIX    *psEncCtrl,     /* I/O  Pointer to Silk FIX encoder control struct  */\r
82     SKP_uint8                       *pCode,         /* O    Pointer to payload                          */\r
83     SKP_int16                       *pnBytesOut,    /* I/O  Pointer to number of payload bytes          */\r
84     SKP_int16                       xfw[]           /* I    Input signal                                */\r
85 );\r
86 \r
87 /* High-pass filter with cutoff frequency adaptation based on pitch lag statistics */\r
88 void SKP_Silk_HP_variable_cutoff_FIX(\r
89     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  Encoder state                               */\r
90     SKP_Silk_encoder_control_FIX    *psEncCtrl,     /* I/O  Encoder control                             */\r
91     SKP_int16                       *out,           /* O    high-pass filtered output signal            */\r
92     const SKP_int16                 *in             /* I    input signal                                */\r
93 );\r
94 \r
95 /****************/\r
96 /* Prefiltering */\r
97 /****************/\r
98 void SKP_Silk_prefilter_FIX(\r
99     SKP_Silk_encoder_state_FIX          *psEnc,         /* I/O  Encoder state                               */\r
100     const SKP_Silk_encoder_control_FIX  *psEncCtrl,     /* I    Encoder control                             */\r
101     SKP_int16                           xw[],           /* O    Weighted signal                             */\r
102     const SKP_int16                     x[]             /* I    Speech signal                               */\r
103 );\r
104 \r
105 /**************************************************************/\r
106 /* Compute noise shaping coefficients and initial gain values */\r
107 /**************************************************************/\r
108 void SKP_Silk_noise_shape_analysis_FIX(\r
109     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  Encoder state                               */\r
110     SKP_Silk_encoder_control_FIX    *psEncCtrl,     /* I/O  Encoder control                             */\r
111     const SKP_int16                 *pitch_res,     /* I    LPC residual from pitch analysis            */\r
112     const SKP_int16                 *x              /* I    Input signal [ 2 * frame_length + la_shape ]*/\r
113 );\r
114 \r
115 /* Processing of gains */\r
116 void SKP_Silk_process_gains_FIX(\r
117     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  Encoder state                               */\r
118     SKP_Silk_encoder_control_FIX    *psEncCtrl      /* I/O  Encoder control                             */\r
119 );\r
120 \r
121 \r
122 /* Control low bitrate redundancy usage */\r
123 void SKP_Silk_LBRR_ctrl_FIX(\r
124     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  encoder state                               */\r
125     SKP_Silk_encoder_control_FIX    *psEncCtrl      /* I/O  encoder control                             */\r
126 );\r
127 \r
128 /* Calculation of LTP state scaling */\r
129 void SKP_Silk_LTP_scale_ctrl_FIX(\r
130     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  encoder state                               */\r
131     SKP_Silk_encoder_control_FIX    *psEncCtrl      /* I/O  encoder control                             */\r
132 );\r
133 \r
134 /**********************************************/\r
135 /* Prediction Analysis                        */\r
136 /**********************************************/\r
137 \r
138 /* Find pitch lags */\r
139 void SKP_Silk_find_pitch_lags_FIX(\r
140     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  encoder state                               */\r
141     SKP_Silk_encoder_control_FIX    *psEncCtrl,     /* I/O  encoder control                             */\r
142     SKP_int16                       res[],          /* O    residual                                    */\r
143     const SKP_int16                 x[]             /* I    Speech signal                               */\r
144 );\r
145 \r
146 void SKP_Silk_find_pred_coefs_FIX(\r
147     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  encoder state                               */\r
148     SKP_Silk_encoder_control_FIX    *psEncCtrl,     /* I/O  encoder control                             */\r
149     const SKP_int16                 res_pitch[]     /* I    Residual from pitch analysis                */\r
150 );\r
151 \r
152 void SKP_Silk_find_LPC_FIX(\r
153     SKP_int             NLSF_Q15[],             /* O    LSFs                                                                        */\r
154     SKP_int             *interpIndex,           /* O    LSF interpolation index, only used for LSF interpolation                    */\r
155     const SKP_int       prev_NLSFq_Q15[],       /* I    previous LSFs, only used for LSF interpolation                              */\r
156     const SKP_int       useInterpolatedLSFs,    /* I    Flag                                                                        */\r
157     const SKP_int       LPC_order,              /* I    LPC order                                                                   */\r
158     const SKP_int16     x[],                    /* I    Input signal                                                                */\r
159     const SKP_int       subfr_length            /* I    Input signal subframe length including preceeding samples                   */\r
160 );\r
161 \r
162 void SKP_Silk_LTP_analysis_filter_FIX(\r
163     SKP_int16       *LTP_res,                           /* O:   LTP residual signal of length NB_SUBFR * ( pre_length + subfr_length )  */\r
164     const SKP_int16 *x,                                 /* I:   Pointer to input signal with at least max( pitchL ) preceeding samples  */\r
165     const SKP_int16 LTPCoef_Q14[ LTP_ORDER * NB_SUBFR ],/* I:   LTP_ORDER LTP coefficients for each NB_SUBFR subframe                   */\r
166     const SKP_int   pitchL[ NB_SUBFR ],                 /* I:   Pitch lag, one for each subframe                                        */\r
167     const SKP_int32 invGains_Qxx[ NB_SUBFR ],           /* I:   Inverse quantization gains, one for each subframe                       */\r
168     const SKP_int   Qxx,                                /* I:   Inverse quantization gains Q domain                                     */\r
169     const SKP_int   subfr_length,                       /* I:   Length of each subframe                                                 */\r
170     const SKP_int   pre_length                          /* I:   Length of the preceeding samples starting at &x[0] for each subframe    */\r
171 );\r
172 \r
173 /* Finds LTP vector from correlations */\r
174 void SKP_Silk_find_LTP_FIX(\r
175     SKP_int16           b_Q14[ NB_SUBFR * LTP_ORDER ],              /* O    LTP coefs                                                   */\r
176     SKP_int32           WLTP[ NB_SUBFR * LTP_ORDER * LTP_ORDER ],   /* O    Weight for LTP quantization                                 */\r
177     SKP_int             *LTPredCodGain_Q7,                          /* O    LTP coding gain                                             */\r
178     const SKP_int16     r_first[],                                  /* I    residual signal after LPC signal + state for first 10 ms    */\r
179     const SKP_int16     r_last[],                                   /* I    residual signal after LPC signal + state for last 10 ms     */\r
180     const SKP_int       lag[ NB_SUBFR ],                            /* I    LTP lags                                                    */\r
181     const SKP_int32     Wght_Q15[ NB_SUBFR ],                       /* I    weights                                                     */\r
182     const SKP_int       subfr_length,                               /* I    subframe length                                             */\r
183     const SKP_int       mem_offset,                                 /* I    number of samples in LTP memory                             */\r
184     SKP_int             corr_rshifts[ NB_SUBFR ]                    /* O    right shifts applied to correlations                        */\r
185 );\r
186 \r
187 /* LTP tap quantizer */\r
188 void SKP_Silk_quant_LTP_gains_FIX(\r
189     SKP_int16               B_Q14[],                /* I/O  (un)quantized LTP gains     */\r
190     SKP_int                 cbk_index[],            /* O    Codebook Index              */\r
191     SKP_int                 *periodicity_index,     /* O    Periodicity Index           */\r
192     const SKP_int32         W_Q18[],                /* I    Error Weights in Q18        */\r
193     SKP_int                 mu_Q8,                  /* I    Mu value (R/D tradeoff)     */\r
194     SKP_int                 lowComplexity           /* I    Flag for low complexity     */\r
195 );\r
196 \r
197 /******************/\r
198 /* NLSF Quantizer */\r
199 /******************/\r
200 \r
201 /* Limit, stabilize, convert and quantize NLSFs.    */ \r
202 void SKP_Silk_process_NLSFs_FIX(\r
203     SKP_Silk_encoder_state_FIX      *psEnc,     /* I/O  encoder state                               */\r
204     SKP_Silk_encoder_control_FIX    *psEncCtrl, /* I/O  encoder control                             */\r
205     SKP_int                         *pNLSF_Q15  /* I/O  Normalized LSFs (quant out) (0 - (2^15-1))  */\r
206 );\r
207 \r
208 /* LSF vector encoder */\r
209 void SKP_Silk_NLSF_MSVQ_encode_FIX(\r
210           SKP_int                   *NLSFIndices,           /* O    Codebook path vector [ CB_STAGES ]      */\r
211           SKP_int                   *pNLSF_Q15,             /* I/O  Quantized NLSF vector [ LPC_ORDER ]     */\r
212     const SKP_Silk_NLSF_CB_struct   *psNLSF_CB,             /* I    Codebook object                         */\r
213     const SKP_int                   *pNLSF_q_Q15_prev,      /* I    Prev. quantized NLSF vector [LPC_ORDER] */\r
214     const SKP_int                   *pW_Q6,                 /* I    NLSF weight vector [ LPC_ORDER ]        */\r
215     const SKP_int                   NLSF_mu_Q15,            /* I    Rate weight for the RD optimization     */\r
216     const SKP_int                   NLSF_mu_fluc_red_Q16,   /* I    Fluctuation reduction error weight      */\r
217     const SKP_int                   NLSF_MSVQ_Survivors,    /* I    Max survivors from each stage           */\r
218     const SKP_int                   LPC_order,              /* I    LPC order                               */\r
219     const SKP_int                   deactivate_fluc_red     /* I    Deactivate fluctuation reduction        */\r
220 );\r
221 \r
222 /* Rate-Distortion calculations for multiple input data vectors */\r
223 void SKP_Silk_NLSF_VQ_rate_distortion_FIX(\r
224     SKP_int32                       *pRD_Q20,           /* O    Rate-distortion values [psNLSF_CBS->nVectors*N] */\r
225     const SKP_Silk_NLSF_CBS         *psNLSF_CBS,        /* I    NLSF codebook stage struct                      */\r
226     const SKP_int                   *in_Q15,            /* I    Input vectors to be quantized                   */\r
227     const SKP_int                   *w_Q6,              /* I    Weight vector                                   */\r
228     const SKP_int32                 *rate_acc_Q5,       /* I    Accumulated rates from previous stage           */\r
229     const SKP_int                   mu_Q15,             /* I    Weight between weighted error and rate          */\r
230     const SKP_int                   N,                  /* I    Number of input vectors to be quantized         */\r
231     const SKP_int                   LPC_order           /* I    LPC order                                       */\r
232 );\r
233 \r
234 /* Compute weighted quantization errors for an LPC_order element input vector, over one codebook stage */\r
235 void SKP_Silk_NLSF_VQ_sum_error_FIX(\r
236     SKP_int32                       *err_Q20,           /* O    Weighted quantization errors  [N*K]         */\r
237     const SKP_int                   *in_Q15,            /* I    Input vectors to be quantized [N*LPC_order] */\r
238     const SKP_int                   *w_Q6,              /* I    Weighting vectors             [N*LPC_order] */\r
239     const SKP_int16                 *pCB_Q15,           /* I    Codebook vectors              [K*LPC_order] */\r
240     const SKP_int                   N,                  /* I    Number of input vectors                     */\r
241     const SKP_int                   K,                  /* I    Number of codebook vectors                  */\r
242     const SKP_int                   LPC_order           /* I    Number of LPCs                              */\r
243 );\r
244 \r
245 /* Entropy constrained MATRIX-weighted VQ, for a single input data vector */\r
246 void SKP_Silk_VQ_WMat_EC_FIX(\r
247     SKP_int                         *ind,               /* O    index of best codebook vector               */\r
248     SKP_int32                       *rate_dist_Q14,     /* O    best weighted quantization error + mu * rate*/\r
249     const SKP_int16                 *in_Q14,            /* I    input vector to be quantized                */\r
250     const SKP_int32                 *W_Q18,             /* I    weighting matrix                            */\r
251     const SKP_int16                 *cb_Q14,            /* I    codebook                                    */\r
252     const SKP_int16                 *cl_Q6,             /* I    code length for each codebook vector        */\r
253     const SKP_int                   mu_Q8,              /* I    tradeoff between weighted error and rate    */\r
254     SKP_int                         L                   /* I    number of vectors in codebook               */\r
255 );\r
256 \r
257 /******************/\r
258 /* Linear Algebra */\r
259 /******************/\r
260 \r
261 /* Calculates correlation matrix X'*X */\r
262 void SKP_Silk_corrMatrix_FIX(\r
263     const SKP_int16                 *x,         /* I    x vector [L + order - 1] used to form data matrix X */\r
264     const SKP_int                   L,          /* I    Length of vectors                                   */\r
265     const SKP_int                   order,      /* I    Max lag for correlation                             */\r
266     SKP_int32                       *XX,        /* O    Pointer to X'*X correlation matrix [ order x order ]*/\r
267     SKP_int                         *rshifts    /* I/O  Right shifts of correlations                        */\r
268 );\r
269 \r
270 /* Calculates correlation vector X'*t */\r
271 void SKP_Silk_corrVector_FIX(\r
272     const SKP_int16                 *x,         /* I    x vector [L + order - 1] used to form data matrix X */\r
273     const SKP_int16                 *t,         /* I    target vector [L]                                   */\r
274     const SKP_int                   L,          /* I    Length of vectors                                   */\r
275     const SKP_int                   order,      /* I    Max lag for correlation                             */\r
276     SKP_int32                       *Xt,        /* O    Pointer to X'*t correlation vector [order]          */\r
277     const SKP_int                   rshifts     /* I    Right shifts of correlations                        */\r
278 );\r
279 \r
280 /* Add noise to matrix diagonal */\r
281 void SKP_Silk_regularize_correlations_FIX(\r
282     SKP_int32                       *XX,                /* I/O  Correlation matrices                        */\r
283     SKP_int32                       *xx,                /* I/O  Correlation values                          */\r
284     SKP_int32                       noise,              /* I    Noise to add                                */\r
285     SKP_int                         D                   /* I    Dimension of XX                             */\r
286 );\r
287 \r
288 /* Solves Ax = b, assuming A is symmetric */\r
289 void SKP_Silk_solve_LDL_FIX(\r
290     SKP_int32                       *A,                 /* I    Pointer to symetric square matrix A         */\r
291     SKP_int                         M,                  /* I    Size of matrix                              */\r
292     const SKP_int32                 *b,                 /* I    Pointer to b vector                         */\r
293     SKP_int32                       *x_Q16              /* O    Pointer to x solution vector                */\r
294 );\r
295 \r
296 /* Residual energy: nrg = wxx - 2 * wXx * c + c' * wXX * c */\r
297 SKP_int32 SKP_Silk_residual_energy16_covar_FIX(\r
298     const SKP_int16                 *c,                 /* I    Prediction vector                           */\r
299     const SKP_int32                 *wXX,               /* I    Correlation matrix                          */\r
300     const SKP_int32                 *wXx,               /* I    Correlation vector                          */\r
301     SKP_int32                       wxx,                /* I    Signal energy                               */\r
302     SKP_int                         D,                  /* I    Dimension                                   */\r
303     SKP_int                         cQ                  /* I    Q value for c vector 0 - 15                 */\r
304 );\r
305 \r
306 /* Calculates residual energies of input subframes where all subframes have LPC_order   */\r
307 /* of preceeding samples                                                                */\r
308 void SKP_Silk_residual_energy_FIX(\r
309           SKP_int32 nrgs[ NB_SUBFR ],           /* O    Residual energy per subframe    */\r
310           SKP_int   nrgsQ[ NB_SUBFR ],          /* O    Q value per subframe            */\r
311     const SKP_int16 x[],                        /* I    Input signal                    */\r
312     const SKP_int16 a_Q12[ 2 ][ MAX_LPC_ORDER ],/* I    AR coefs for each frame half    */\r
313     const SKP_int32 gains_Qx[ NB_SUBFR ],       /* I    Quantization gains in Qx        */\r
314     const SKP_int   Qx,                         /* I    Quantization gains Q value      */\r
315     const SKP_int   subfr_length,               /* I    Subframe length                 */\r
316     const SKP_int   LPC_order                   /* I    LPC order                       */\r
317 );\r
318 \r
319 #ifndef FORCE_CPP_BUILD\r
320 #ifdef __cplusplus\r
321 }\r
322 #endif /* __cplusplus */\r
323 #endif /* FORCE_CPP_BUILD */\r
324 #endif /* SKP_SILK_MAIN_FIX_H */\r