Moving the SILK fixed-point and float files
[opus.git] / silk / SKP_Silk_structs.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_STRUCTS_H\r
29 #define SKP_SILK_STRUCTS_H\r
30 \r
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34 \r
35 #include "SKP_Silk_typedef.h"\r
36 #include "SKP_Silk_SigProc_FIX.h"\r
37 #include "SKP_Silk_define.h"\r
38 #include "entenc.h"\r
39 #include "entdec.h"\r
40 \r
41 #ifdef __cplusplus\r
42 extern "C"\r
43 {\r
44 #endif\r
45 \r
46 /************************************/\r
47 /* Noise shaping quantization state */\r
48 /************************************/\r
49 typedef struct {\r
50     SKP_int16   xq[           2 * MAX_FRAME_LENGTH ]; /* Buffer for quantized output signal */\r
51     SKP_int32   sLTP_shp_Q10[ 2 * MAX_FRAME_LENGTH ];\r
52     SKP_int32   sLPC_Q14[ MAX_SUB_FRAME_LENGTH + NSQ_LPC_BUF_LENGTH ];\r
53     SKP_int32   sAR2_Q14[ MAX_SHAPE_LPC_ORDER ];\r
54     SKP_int32   sLF_AR_shp_Q12;\r
55     SKP_int     lagPrev;\r
56     SKP_int     sLTP_buf_idx;\r
57     SKP_int     sLTP_shp_buf_idx;\r
58     SKP_int32   rand_seed;\r
59     SKP_int32   prev_inv_gain_Q16;\r
60     SKP_int     rewhite_flag;\r
61 } SKP_Silk_nsq_state;\r
62 \r
63 /********************************/\r
64 /* VAD state                    */\r
65 /********************************/\r
66 typedef struct {\r
67     SKP_int32   AnaState[ 2 ];                  /* Analysis filterbank state: 0-8 kHz                       */\r
68     SKP_int32   AnaState1[ 2 ];                 /* Analysis filterbank state: 0-4 kHz                       */\r
69     SKP_int32   AnaState2[ 2 ];                 /* Analysis filterbank state: 0-2 kHz                       */\r
70     SKP_int32   XnrgSubfr[ VAD_N_BANDS ];       /* Subframe energies                                        */\r
71     SKP_int32   NrgRatioSmth_Q8[ VAD_N_BANDS ]; /* Smoothed energy level in each band                       */\r
72     SKP_int16   HPstate;                        /* State of differentiator in the lowest band               */\r
73     SKP_int32   NL[ VAD_N_BANDS ];              /* Noise energy level in each band                          */\r
74     SKP_int32   inv_NL[ VAD_N_BANDS ];          /* Inverse noise energy level in each band                  */\r
75     SKP_int32   NoiseLevelBias[ VAD_N_BANDS ];  /* Noise level estimator bias/offset                        */\r
76     SKP_int32   counter;                        /* Frame counter used in the initial phase                  */\r
77 } SKP_Silk_VAD_state;\r
78 \r
79 /* Variable cut-off low-pass filter state */\r
80 typedef struct {\r
81     SKP_int32                   In_LP_State[ 2 ];           /* Low pass filter state */\r
82     SKP_int32                   transition_frame_no;        /* Counter which is mapped to a cut-off frequency */\r
83     SKP_int                     mode;                       /* Operating mode, <0: switch down, >0: switch up; 0: do nothing */\r
84 } SKP_Silk_LP_state;\r
85 \r
86 /* Structure containing NLSF codebook */\r
87 typedef struct {\r
88     const SKP_int16             nVectors;\r
89     const SKP_int16             order;\r
90     const SKP_int16             quantStepSize_Q16;\r
91     const SKP_int16             invQuantStepSize_Q6;\r
92     const SKP_uint8             *CB1_NLSF_Q8;\r
93     const SKP_uint8             *CB1_iCDF;\r
94     const SKP_uint8             *pred_Q8;\r
95     const SKP_uint8             *ec_sel;\r
96     const SKP_uint8             *ec_iCDF;\r
97     const SKP_uint8             *ec_Rates_Q5;\r
98     const SKP_int16             *deltaMin_Q15;\r
99 } SKP_Silk_NLSF_CB_struct;\r
100 \r
101 typedef struct {\r
102     SKP_int32                   predictor_prev_Q16;\r
103 } stereo_state;\r
104 \r
105 typedef struct {\r
106     SKP_int8        GainsIndices[ MAX_NB_SUBFR ];\r
107     SKP_int8        LTPIndex[ MAX_NB_SUBFR ];\r
108     SKP_int8        NLSFIndices[ MAX_LPC_ORDER + 1 ];\r
109     SKP_int16       lagIndex;\r
110     SKP_int8        contourIndex;\r
111     SKP_int8        signalType;\r
112     SKP_int8        quantOffsetType;\r
113     SKP_int8        NLSFInterpCoef_Q2;\r
114     SKP_int8        PERIndex;\r
115     SKP_int8        LTP_scaleIndex;\r
116     SKP_int8        Seed;\r
117 } SideInfoIndices;\r
118 \r
119 /********************************/\r
120 /* Encoder state                */\r
121 /********************************/\r
122 typedef struct {\r
123     SKP_int32                       In_HP_State[ 2 ];               /* High pass filter state                                               */\r
124     SKP_int32                       variable_HP_smth1_Q15;          /* State of first smoother                                              */\r
125     SKP_int32                       variable_HP_smth2_Q15;          /* State of second smoother                                             */\r
126     SKP_Silk_LP_state               sLP;                            /* Low pass filter state                                                */\r
127     SKP_Silk_VAD_state              sVAD;                           /* Voice activity detector state                                        */\r
128     SKP_Silk_nsq_state              sNSQ;                           /* Noise Shape Quantizer State                                          */\r
129     SKP_int16                       prev_NLSFq_Q15[ MAX_LPC_ORDER ];/* Previously quantized NLSF vector                                     */\r
130     SKP_int                         speech_activity_Q8;             /* Speech activity                                                      */\r
131     SKP_int                         allow_bandwidth_switch;         /* Flag indicating that switching of internal bandwidth is allowed      */\r
132     SKP_int8                        LBRRprevLastGainIndex;\r
133     SKP_int8                        prevSignalType;\r
134     SKP_int                         prevLag;\r
135     SKP_int                         pitch_LPC_win_length;\r
136     SKP_int                         max_pitch_lag;                  /* Highest possible pitch lag (samples)                                 */\r
137     SKP_int32                       API_fs_Hz;                      /* API sampling frequency (Hz)                                          */\r
138     SKP_int32                       prev_API_fs_Hz;                 /* Previous API sampling frequency (Hz)                                 */\r
139     SKP_int                         maxInternal_fs_Hz;              /* Maximum internal sampling frequency (Hz)                             */\r
140     SKP_int                         minInternal_fs_Hz;              /* Minimum internal sampling frequency (Hz)                             */\r
141     SKP_int                         desiredInternal_fs_Hz;          /* Soft request for internal sampling frequency (Hz)                    */\r
142     SKP_int                         fs_kHz;                         /* Internal sampling frequency (kHz)                                    */\r
143     SKP_int                         nb_subfr;                       /* Number of 5 ms subframes in a frame                                  */\r
144     SKP_int                         frame_length;                   /* Frame length (samples)                                               */\r
145     SKP_int                         subfr_length;                   /* Subframe length (samples)                                            */\r
146     SKP_int                         ltp_mem_length;                 /* Length of LTP memory                                                 */\r
147     SKP_int                         la_pitch;                       /* Look-ahead for pitch analysis (samples)                              */\r
148     SKP_int                         la_shape;                       /* Look-ahead for noise shape analysis (samples)                        */\r
149     SKP_int                         shapeWinLength;                 /* Window length for noise shape analysis (samples)                     */\r
150     SKP_int32                       TargetRate_bps;                 /* Target bitrate (bps)                                                 */\r
151     SKP_int                         PacketSize_ms;                  /* Number of milliseconds to put in each packet                         */\r
152     SKP_int                         PacketLoss_perc;                /* Packet loss rate measured by farend                                  */\r
153     SKP_int32                       frameCounter;\r
154     SKP_int                         Complexity;                     /* Complexity setting: 0-> low; 1-> medium; 2->high                     */\r
155     SKP_int                         nStatesDelayedDecision;         /* Number of states in delayed decision quantization                    */\r
156     SKP_int                         useInterpolatedNLSFs;           /* Flag for using NLSF interpolation                                    */\r
157     SKP_int                         shapingLPCOrder;                /* Filter order for noise shaping filters                               */\r
158     SKP_int                         predictLPCOrder;                /* Filter order for prediction filters                                  */\r
159     SKP_int                         pitchEstimationComplexity;      /* Complexity level for pitch estimator                                 */\r
160     SKP_int                         pitchEstimationLPCOrder;        /* Whitening filter order for pitch estimator                           */\r
161     SKP_int32                       pitchEstimationThreshold_Q16;   /* Threshold for pitch estimator                                        */\r
162     SKP_int                         LTPQuantLowComplexity;          /* Flag for low complexity LTP quantization                             */\r
163     SKP_int                         mu_LTP_Q9;                      /* Rate-distortion tradeoff in LTP quantization                         */\r
164     SKP_int                         NLSF_MSVQ_Survivors;            /* Number of survivors in NLSF MSVQ                                     */\r
165     SKP_int                         first_frame_after_reset;        /* Flag for deactivating NLSF interp. and fluc. reduction after resets  */\r
166     SKP_int                         controlled_since_last_payload;  /* Flag for ensuring codec_control only runs once per packet            */\r
167         SKP_int                         warping_Q16;                    /* Warping parameter for warped noise shaping                           */\r
168     SKP_int                         useCBR;                         /* Flag to enable constant bitrate                                      */\r
169     SKP_int                         prefillFlag;                    /* Flag to indicate that only buffers are prefilled, no coding          */\r
170     const SKP_uint8                 *pitch_lag_low_bits_iCDF;       /* Pointer to iCDF table for low bits of pitch lag index                */\r
171     const SKP_uint8                 *pitch_contour_iCDF;            /* Pointer to iCDF table for pitch contour index                        */\r
172     const SKP_Silk_NLSF_CB_struct   *psNLSF_CB;                     /* Pointer to NLSF codebook                                             */\r
173     SKP_int                         input_quality_bands_Q15[ VAD_N_BANDS ];\r
174     SKP_int                         input_tilt_Q15;\r
175     SKP_int                         SNR_dB_Q7;                      /* Quality setting                                                      */\r
176 \r
177     SKP_int8                        VAD_flags[ MAX_FRAMES_PER_PACKET ];\r
178     SKP_int8                        LBRR_flag;\r
179     SKP_int                         LBRR_flags[ MAX_FRAMES_PER_PACKET ];\r
180 \r
181     SideInfoIndices                 indices;\r
182         SKP_int8                        pulses[ MAX_FRAME_LENGTH ];\r
183 \r
184     /* Input/output buffering */\r
185     SKP_int16                       inputBuf[ MAX_FRAME_LENGTH ];   /* buffer containing input signal                                       */\r
186     SKP_int                         inputBufIx;\r
187     SKP_int                         nFramesPerPacket;\r
188     SKP_int                         nFramesAnalyzed;                /* Number of frames analyzed in current packet                          */\r
189 \r
190     /* Parameters For LTP scaling Control */\r
191     SKP_int                         frames_since_onset;\r
192 \r
193     /* Specifically for entropy coding */\r
194     SKP_int                         ec_prevSignalType;\r
195     SKP_int16                       ec_prevLagIndex;\r
196 \r
197     SKP_Silk_resampler_state_struct resampler_state;\r
198 \r
199     /* DTX */\r
200     SKP_int                         useDTX;                         /* Flag to enable DTX                                                   */\r
201     SKP_int                         inDTX;                          /* Flag to signal DTX period                                            */\r
202     SKP_int                         noSpeechCounter;                /* Counts concecutive nonactive frames, used by DTX                     */\r
203 \r
204     /* Inband Low Bitrate Redundancy (LBRR) data */ \r
205     SKP_int                         useInBandFEC;                   /* Saves the API setting for query                                      */\r
206     SKP_int                         LBRR_enabled;                   /* Depends on useInBandFRC, bitrate and packet loss rate                */\r
207     SKP_int                         LBRR_GainIncreases;             /* Number of shifts to Gains to get LBRR rate Voiced frames             */\r
208     SideInfoIndices                 indices_LBRR[ MAX_FRAMES_PER_PACKET ];\r
209         SKP_int8                        pulses_LBRR[ MAX_FRAMES_PER_PACKET ][ MAX_FRAME_LENGTH ];\r
210 } SKP_Silk_encoder_state;\r
211 \r
212 \r
213 /* Struct for Packet Loss Concealment */\r
214 typedef struct {\r
215     SKP_int32   pitchL_Q8;                      /* Pitch lag to use for voiced concealment                  */\r
216     SKP_int16   LTPCoef_Q14[ LTP_ORDER ];       /* LTP coeficients to use for voiced concealment            */\r
217     SKP_int16   prevLPC_Q12[ MAX_LPC_ORDER ];\r
218     SKP_int     last_frame_lost;                /* Was previous frame lost                                  */\r
219     SKP_int32   rand_seed;                      /* Seed for unvoiced signal generation                      */\r
220     SKP_int16   randScale_Q14;                  /* Scaling of unvoiced random signal                        */\r
221     SKP_int32   conc_energy;\r
222     SKP_int     conc_energy_shift;\r
223     SKP_int16   prevLTP_scale_Q14;\r
224     SKP_int32   prevGain_Q16[ MAX_NB_SUBFR ];\r
225     SKP_int     fs_kHz;\r
226 } SKP_Silk_PLC_struct;\r
227 \r
228 /* Struct for CNG */\r
229 typedef struct {\r
230     SKP_int32   CNG_exc_buf_Q10[ MAX_FRAME_LENGTH ];\r
231     SKP_int16   CNG_smth_NLSF_Q15[ MAX_LPC_ORDER ];\r
232     SKP_int32   CNG_synth_state[ MAX_LPC_ORDER ];\r
233     SKP_int32   CNG_smth_Gain_Q16;\r
234     SKP_int32   rand_seed;\r
235     SKP_int     fs_kHz;\r
236 } SKP_Silk_CNG_struct;\r
237 \r
238 /********************************/\r
239 /* Decoder state                */\r
240 /********************************/\r
241 typedef struct {\r
242     SKP_int32       prev_inv_gain_Q16;\r
243     SKP_int32       sLTP_Q16[ 2 * MAX_FRAME_LENGTH ];\r
244     SKP_int32       sLPC_Q14[ MAX_SUB_FRAME_LENGTH + MAX_LPC_ORDER ];\r
245     SKP_int32       exc_Q10[ MAX_FRAME_LENGTH ];\r
246     SKP_int16       outBuf[ 2 * MAX_FRAME_LENGTH ];             /* Buffer for output signal                                             */\r
247     SKP_int         lagPrev;                                    /* Previous Lag                                                         */\r
248     SKP_int8        LastGainIndex;                              /* Previous gain index                                                  */\r
249     SKP_int         fs_kHz;                                     /* Sampling frequency in kHz                                            */\r
250     SKP_int32       prev_API_sampleRate;                        /* Previous API sample frequency (Hz)                                   */\r
251     SKP_int         nb_subfr;                                   /* Number of 5 ms subframes in a frame                                  */\r
252     SKP_int         frame_length;                               /* Frame length (samples)                                               */\r
253     SKP_int         subfr_length;                               /* Subframe length (samples)                                            */\r
254     SKP_int         ltp_mem_length;                             /* Length of LTP memory                                                 */\r
255     SKP_int         LPC_order;                                  /* LPC order                                                            */\r
256     SKP_int16       prevNLSF_Q15[ MAX_LPC_ORDER ];              /* Used to interpolate LSFs                                             */\r
257     SKP_int         first_frame_after_reset;                    /* Flag for deactivating NLSF interp. and fluc. reduction after resets  */\r
258     const SKP_uint8 *pitch_lag_low_bits_iCDF;                   /* Pointer to iCDF table for low bits of pitch lag index                */\r
259     const SKP_uint8 *pitch_contour_iCDF;                        /* Pointer to iCDF table for pitch contour index                        */\r
260 \r
261     /* For buffering payload in case of more frames per packet */\r
262     SKP_int         nFramesDecoded;\r
263     SKP_int         nFramesPerPacket;\r
264 \r
265     /* Specifically for entropy coding */\r
266     SKP_int         ec_prevSignalType;\r
267     SKP_int16       ec_prevLagIndex;\r
268 \r
269     SKP_int         VAD_flags[ MAX_FRAMES_PER_PACKET ];\r
270     SKP_int         LBRR_flag;\r
271     SKP_int         LBRR_flags[ MAX_FRAMES_PER_PACKET ];\r
272 \r
273     SKP_Silk_resampler_state_struct resampler_state;\r
274 \r
275     const SKP_Silk_NLSF_CB_struct   *psNLSF_CB;                 /* Pointer to NLSF codebook                                             */\r
276 \r
277     /* Quantization indices */\r
278     SideInfoIndices indices;\r
279     \r
280     /* CNG state */\r
281     SKP_Silk_CNG_struct sCNG;\r
282 \r
283     /* Stuff used for PLC */\r
284     SKP_int         lossCnt;\r
285     SKP_int         prevSignalType;\r
286 \r
287     SKP_Silk_PLC_struct sPLC;\r
288 \r
289 } SKP_Silk_decoder_state;\r
290 \r
291 /************************/\r
292 /* Decoder control      */\r
293 /************************/\r
294 typedef struct {\r
295     /* prediction and coding parameters */\r
296     SKP_int             pitchL[ MAX_NB_SUBFR ];\r
297     SKP_int32           Gains_Q16[ MAX_NB_SUBFR ];\r
298     /* holds interpolated and final coefficients, 4-byte aligned */\r
299     SKP_DWORD_ALIGN SKP_int16 PredCoef_Q12[ 2 ][ MAX_LPC_ORDER ];\r
300     SKP_int16           LTPCoef_Q14[ LTP_ORDER * MAX_NB_SUBFR ];\r
301     SKP_int             LTP_scale_Q14;\r
302 } SKP_Silk_decoder_control;\r
303 \r
304 #ifdef __cplusplus\r
305 }\r
306 #endif\r
307 \r
308 #endif\r