Convert all CRLF in the SILK code, tabs to spaces, and trailing
[opus.git] / silk / fixed / silk_main_FIX.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_FIX_H
29 #define SILK_MAIN_FIX_H
30
31 #include "silk_SigProc_FIX.h"
32 #include "silk_structs_FIX.h"
33 #include "silk_control.h"
34 #include "silk_main.h"
35 #include "silk_PLC.h"
36 #include "silk_debug.h"
37 #include "entenc.h"
38
39 #ifndef FORCE_CPP_BUILD
40 #ifdef __cplusplus
41 extern "C"
42 {
43 #endif
44 #endif
45
46 #define silk_encoder_state_Fxx      silk_encoder_state_FIX
47 #define silk_encode_frame_Fxx       silk_encode_frame_FIX
48
49 /*********************/
50 /* Encoder Functions */
51 /*********************/
52
53 /* High-pass filter with cutoff frequency adaptation based on pitch lag statistics */
54 void silk_HP_variable_cutoff(
55     silk_encoder_state_Fxx          state_Fxx[],    /* I/O  Encoder states                              */
56     const opus_int                   nChannels       /* I    Number of channels                          */
57 );
58
59 /* Encoder main function */
60 opus_int silk_encode_frame_FIX(
61     silk_encoder_state_FIX          *psEnc,             /* I/O  Pointer to Silk FIX encoder state       */
62     opus_int32                       *pnBytesOut,        /*   O  Pointer to number of payload bytes;     */
63     ec_enc                          *psRangeEnc         /* I/O  compressor data structure               */
64 );
65
66 /* Low Bitrate Redundancy (LBRR) encoding. Reuse all parameters but encode with lower bitrate           */
67 void silk_LBRR_encode_FIX(
68     silk_encoder_state_FIX          *psEnc,             /* I/O  Pointer to Silk FIX encoder state           */
69     silk_encoder_control_FIX        *psEncCtrl,         /* I/O  Pointer to Silk FIX encoder control struct  */
70     const opus_int16                 xfw[]               /* I    Input signal                                */
71 );
72
73 /* Initializes the Silk encoder state */
74 opus_int silk_init_encoder(
75     silk_encoder_state_FIX          *psEnc              /* I/O  Pointer to Silk FIX encoder state           */
76 );
77
78 /* Control the Silk encoder */
79 opus_int silk_control_encoder(
80     silk_encoder_state_FIX          *psEnc,             /* I/O  Pointer to Silk encoder state           */
81     silk_EncControlStruct           *encControl,        /* I:   Control structure                       */
82     const opus_int32                 TargetRate_bps,     /* I    Target max bitrate (bps)                */
83     const opus_int                   allow_bw_switch,    /* I    Flag to allow switching audio bandwidth */
84     const opus_int                   channelNb           /* I    Channel number                          */
85 );
86
87 /****************/
88 /* Prefiltering */
89 /****************/
90 void silk_prefilter_FIX(
91     silk_encoder_state_FIX              *psEnc,         /* I/O  Encoder state                               */
92     const silk_encoder_control_FIX      *psEncCtrl,     /* I    Encoder control                             */
93     opus_int16                           xw[],           /* O    Weighted signal                             */
94     const opus_int16                     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_FIX(
102     silk_encoder_state_FIX          *psEnc,         /* I/O  Encoder state FIX                           */
103     silk_encoder_control_FIX        *psEncCtrl,     /* I/O  Encoder control FIX                         */
104     const opus_int16                 *pitch_res,     /* I    LPC residual from pitch analysis            */
105     const opus_int16                 *x              /* I    Input signal [ frame_length + la_shape ]    */
106 );
107
108 /* Autocorrelations for a warped frequency axis */
109 void silk_warped_autocorrelation_FIX(
110           opus_int32                 *corr,              /* O    Result [order + 1]                      */
111           opus_int                   *scale,             /* O    Scaling of the correlation vector       */
112     const opus_int16                 *input,             /* I    Input data to correlate                 */
113     const opus_int                   warping_Q16,        /* I    Warping coefficient                     */
114     const opus_int                   length,             /* I    Length of input                         */
115     const opus_int                   order               /* I    Correlation order (even)                */
116 );
117
118 /* Calculation of LTP state scaling */
119 void silk_LTP_scale_ctrl_FIX(
120     silk_encoder_state_FIX          *psEnc,         /* I/O  encoder state                               */
121     silk_encoder_control_FIX        *psEncCtrl      /* I/O  encoder control                             */
122 );
123
124 /**********************************************/
125 /* Prediction Analysis                        */
126 /**********************************************/
127 /* Find pitch lags */
128 void silk_find_pitch_lags_FIX(
129     silk_encoder_state_FIX          *psEnc,         /* I/O  encoder state                               */
130     silk_encoder_control_FIX        *psEncCtrl,     /* I/O  encoder control                             */
131     opus_int16                       res[],          /* O    residual                                    */
132     const opus_int16                 x[]             /* I    Speech signal                               */
133 );
134
135 /* Find LPC and LTP coefficients */
136 void silk_find_pred_coefs_FIX(
137     silk_encoder_state_FIX          *psEnc,         /* I/O  encoder state                               */
138     silk_encoder_control_FIX        *psEncCtrl,     /* I/O  encoder control                             */
139     const opus_int16                 res_pitch[],    /* I    Residual from pitch analysis                */
140     const opus_int16                 x[]             /* I    Speech signal                               */
141 );
142
143 /* LPC analysis */
144 void silk_find_LPC_FIX(
145     opus_int16                       NLSF_Q15[],             /* O    NLSFs                                                           */
146     opus_int8                        *interpIndex,           /* O    NLSF interpolation index, only used for NLSF interpolation      */
147     const opus_int16                 prev_NLSFq_Q15[],       /* I    previous NLSFs, only used for NLSF interpolation                */
148     const opus_int                   useInterpNLSFs,         /* I    Flag                                                            */
149     const opus_int                   firstFrameAfterReset,   /* I    Flag                                                            */
150     const opus_int                   LPC_order,              /* I    LPC order                                                       */
151     const opus_int16                 x[],                    /* I    Input signal                                                    */
152     const opus_int                   subfr_length,           /* I    Input signal subframe length including preceeding samples       */
153     const opus_int                   nb_subfr                /* I:   Number of subframes                                             */
154 );
155
156 /* LTP analysis */
157 void silk_find_LTP_FIX(
158     opus_int16           b_Q14[ MAX_NB_SUBFR * LTP_ORDER ],              /* O    LTP coefs                                                   */
159     opus_int32           WLTP[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ],   /* O    Weight for LTP quantization                                 */
160     opus_int             *LTPredCodGain_Q7,                              /* O    LTP coding gain                                             */
161     const opus_int16     r_lpc[],                                        /* I    residual signal after LPC signal + state for first 10 ms    */
162     const opus_int       lag[ MAX_NB_SUBFR ],                            /* I    LTP lags                                                    */
163     const opus_int32     Wght_Q15[ MAX_NB_SUBFR ],                       /* I    weights                                                     */
164     const opus_int       subfr_length,                                   /* I    subframe length                                             */
165     const opus_int       nb_subfr,                                       /* I    number of subframes                                         */
166     const opus_int       mem_offset,                                     /* I    number of samples in LTP memory                             */
167     opus_int             corr_rshifts[ MAX_NB_SUBFR ]                    /* O    right shifts applied to correlations                        */
168 );
169
170 void silk_LTP_analysis_filter_FIX(
171     opus_int16           *LTP_res,                               /* O:   LTP residual signal of length MAX_NB_SUBFR * ( pre_length + subfr_length )  */
172     const opus_int16     *x,                                     /* I:   Pointer to input signal with at least max( pitchL ) preceeding samples      */
173     const opus_int16     LTPCoef_Q14[ LTP_ORDER * MAX_NB_SUBFR ],/* I:   LTP_ORDER LTP coefficients for each MAX_NB_SUBFR subframe                   */
174     const opus_int       pitchL[ MAX_NB_SUBFR ],                 /* I:   Pitch lag, one for each subframe                                            */
175     const opus_int32     invGains_Q16[ MAX_NB_SUBFR ],           /* I:   Inverse quantization gains, one for each subframe                           */
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:   Length of the preceeding samples starting at &x[0] 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_FIX(
184           opus_int32 nrgs[ MAX_NB_SUBFR ],           /* O    Residual energy per subframe    */
185           opus_int   nrgsQ[ MAX_NB_SUBFR ],          /* O    Q value per subframe            */
186     const opus_int16 x[],                            /* I    Input signal                    */
187           opus_int16 a_Q12[ 2 ][ MAX_LPC_ORDER ],    /* I    AR coefs for each frame half    */
188     const opus_int32 gains[ MAX_NB_SUBFR ],          /* I    Quantization gains              */
189     const opus_int   subfr_length,                   /* I    Subframe length                 */
190     const opus_int   nb_subfr,                       /* I    Number of subframes             */
191     const opus_int   LPC_order                       /* I    LPC order                       */
192 );
193
194 /* Residual energy: nrg = wxx - 2 * wXx * c + c' * wXX * c */
195 opus_int32 silk_residual_energy16_covar_FIX(
196     const opus_int16                 *c,                 /* I    Prediction vector                           */
197     const opus_int32                 *wXX,               /* I    Correlation matrix                          */
198     const opus_int32                 *wXx,               /* I    Correlation vector                          */
199     opus_int32                       wxx,                /* I    Signal energy                               */
200     opus_int                         D,                  /* I    Dimension                                   */
201     opus_int                         cQ                  /* I    Q value for c vector 0 - 15                 */
202 );
203
204 /* Processing of gains */
205 void silk_process_gains_FIX(
206     silk_encoder_state_FIX          *psEnc,         /* I/O  Encoder state                               */
207     silk_encoder_control_FIX        *psEncCtrl      /* I/O  Encoder control                             */
208 );
209
210 /******************/
211 /* Linear Algebra */
212 /******************/
213 /* Calculates correlation matrix X'*X */
214 void silk_corrMatrix_FIX(
215     const opus_int16                 *x,         /* I    x vector [L + order - 1] used to form data matrix X */
216     const opus_int                   L,          /* I    Length of vectors                                   */
217     const opus_int                   order,      /* I    Max lag for correlation                             */
218     const opus_int                   head_room,  /* I    Desired headroom                                    */
219     opus_int32                       *XX,        /* O    Pointer to X'*X correlation matrix [ order x order ]*/
220     opus_int                         *rshifts    /* I/O  Right shifts of correlations                        */
221 );
222
223 /* Calculates correlation vector X'*t */
224 void silk_corrVector_FIX(
225     const opus_int16                 *x,         /* I    x vector [L + order - 1] used to form data matrix X */
226     const opus_int16                 *t,         /* I    Target vector [L]                                   */
227     const opus_int                   L,          /* I    Length of vectors                                   */
228     const opus_int                   order,      /* I    Max lag for correlation                             */
229     opus_int32                       *Xt,        /* O    Pointer to X'*t correlation vector [order]          */
230     const opus_int                   rshifts     /* I    Right shifts of correlations                        */
231 );
232
233 /* Add noise to matrix diagonal */
234 void silk_regularize_correlations_FIX(
235     opus_int32                       *XX,                /* I/O  Correlation matrices                        */
236     opus_int32                       *xx,                /* I/O  Correlation values                          */
237     opus_int32                       noise,              /* I    Noise to add                                */
238     opus_int                         D                   /* I    Dimension of XX                             */
239 );
240
241 /* Solves Ax = b, assuming A is symmetric */
242 void silk_solve_LDL_FIX(
243     opus_int32                       *A,                 /* I    Pointer to symetric square matrix A         */
244     opus_int                         M,                  /* I    Size of matrix                              */
245     const opus_int32                 *b,                 /* I    Pointer to b vector                         */
246     opus_int32                       *x_Q16              /* O    Pointer to x solution vector                */
247 );
248
249 #ifndef FORCE_CPP_BUILD
250 #ifdef __cplusplus
251 }
252 #endif /* __cplusplus */
253 #endif /* FORCE_CPP_BUILD */
254 #endif /* SILK_MAIN_FIX_H */