Squashed commit of the following:
[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\r
32 #include "config.h"\r
33 #endif\r
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                   pred_prev_Q13[ 2 ];\r
103     SKP_int16                   sMid[ 2 ];\r
104     SKP_int16                   sSide[ 2 ];\r
105 } stereo_state;\r
106 \r
107 typedef struct {\r
108     SKP_int8        GainsIndices[ MAX_NB_SUBFR ];\r
109     SKP_int8        LTPIndex[ MAX_NB_SUBFR ];\r
110     SKP_int8        NLSFIndices[ MAX_LPC_ORDER + 1 ];\r
111     SKP_int16       lagIndex;\r
112     SKP_int8        contourIndex;\r
113     SKP_int8        signalType;\r
114     SKP_int8        quantOffsetType;\r
115     SKP_int8        NLSFInterpCoef_Q2;\r
116     SKP_int8        PERIndex;\r
117     SKP_int8        LTP_scaleIndex;\r
118     SKP_int8        Seed;\r
119 } SideInfoIndices;\r
120 \r
121 /********************************/\r
122 /* Encoder state                */\r
123 /********************************/\r
124 typedef struct {\r
125     SKP_int32                       In_HP_State[ 2 ];               /* High pass filter state                                               */\r
126     SKP_int32                       variable_HP_smth1_Q15;          /* State of first smoother                                              */\r
127     SKP_int32                       variable_HP_smth2_Q15;          /* State of second smoother                                             */\r
128     SKP_Silk_LP_state               sLP;                            /* Low pass filter state                                                */\r
129     SKP_Silk_VAD_state              sVAD;                           /* Voice activity detector state                                        */\r
130     SKP_Silk_nsq_state              sNSQ;                           /* Noise Shape Quantizer State                                          */\r
131     SKP_int16                       prev_NLSFq_Q15[ MAX_LPC_ORDER ];/* Previously quantized NLSF vector                                     */\r
132     SKP_int                         speech_activity_Q8;             /* Speech activity                                                      */\r
133     SKP_int                         allow_bandwidth_switch;         /* Flag indicating that switching of internal bandwidth is allowed      */\r
134     SKP_int8                        LBRRprevLastGainIndex;\r
135     SKP_int8                        prevSignalType;\r
136     SKP_int                         prevLag;\r
137     SKP_int                         pitch_LPC_win_length;\r
138     SKP_int                         max_pitch_lag;                  /* Highest possible pitch lag (samples)                                 */\r
139     SKP_int32                       API_fs_Hz;                      /* API sampling frequency (Hz)                                          */\r
140     SKP_int32                       prev_API_fs_Hz;                 /* Previous API sampling frequency (Hz)                                 */\r
141     SKP_int                         maxInternal_fs_Hz;              /* Maximum internal sampling frequency (Hz)                             */\r
142     SKP_int                         minInternal_fs_Hz;              /* Minimum internal sampling frequency (Hz)                             */\r
143     SKP_int                         desiredInternal_fs_Hz;          /* Soft request for internal sampling frequency (Hz)                    */\r
144     SKP_int                         fs_kHz;                         /* Internal sampling frequency (kHz)                                    */\r
145     SKP_int                         nb_subfr;                       /* Number of 5 ms subframes in a frame                                  */\r
146     SKP_int                         frame_length;                   /* Frame length (samples)                                               */\r
147     SKP_int                         subfr_length;                   /* Subframe length (samples)                                            */\r
148     SKP_int                         ltp_mem_length;                 /* Length of LTP memory                                                 */\r
149     SKP_int                         la_pitch;                       /* Look-ahead for pitch analysis (samples)                              */\r
150     SKP_int                         la_shape;                       /* Look-ahead for noise shape analysis (samples)                        */\r
151     SKP_int                         shapeWinLength;                 /* Window length for noise shape analysis (samples)                     */\r
152     SKP_int32                       TargetRate_bps;                 /* Target bitrate (bps)                                                 */\r
153     SKP_int                         PacketSize_ms;                  /* Number of milliseconds to put in each packet                         */\r
154     SKP_int                         PacketLoss_perc;                /* Packet loss rate measured by farend                                  */\r
155     SKP_int32                       frameCounter;\r
156     SKP_int                         Complexity;                     /* Complexity setting: 0-> low; 1-> medium; 2->high                     */\r
157     SKP_int                         nStatesDelayedDecision;         /* Number of states in delayed decision quantization                    */\r
158     SKP_int                         useInterpolatedNLSFs;           /* Flag for using NLSF interpolation                                    */\r
159     SKP_int                         shapingLPCOrder;                /* Filter order for noise shaping filters                               */\r
160     SKP_int                         predictLPCOrder;                /* Filter order for prediction filters                                  */\r
161     SKP_int                         pitchEstimationComplexity;      /* Complexity level for pitch estimator                                 */\r
162     SKP_int                         pitchEstimationLPCOrder;        /* Whitening filter order for pitch estimator                           */\r
163     SKP_int32                       pitchEstimationThreshold_Q16;   /* Threshold for pitch estimator                                        */\r
164     SKP_int                         LTPQuantLowComplexity;          /* Flag for low complexity LTP quantization                             */\r
165     SKP_int                         mu_LTP_Q9;                      /* Rate-distortion tradeoff in LTP quantization                         */\r
166     SKP_int                         NLSF_MSVQ_Survivors;            /* Number of survivors in NLSF MSVQ                                     */\r
167     SKP_int                         first_frame_after_reset;        /* Flag for deactivating NLSF interp. and fluc. reduction after resets  */\r
168     SKP_int                         controlled_since_last_payload;  /* Flag for ensuring codec_control only runs once per packet            */\r
169         SKP_int                         warping_Q16;                    /* Warping parameter for warped noise shaping                           */\r
170     SKP_int                         useCBR;                         /* Flag to enable constant bitrate                                      */\r
171     SKP_int                         prefillFlag;                    /* Flag to indicate that only buffers are prefilled, no coding          */\r
172     const SKP_uint8                 *pitch_lag_low_bits_iCDF;       /* Pointer to iCDF table for low bits of pitch lag index                */\r
173     const SKP_uint8                 *pitch_contour_iCDF;            /* Pointer to iCDF table for pitch contour index                        */\r
174     const SKP_Silk_NLSF_CB_struct   *psNLSF_CB;                     /* Pointer to NLSF codebook                                             */\r
175     SKP_int                         input_quality_bands_Q15[ VAD_N_BANDS ];\r
176     SKP_int                         input_tilt_Q15;\r
177     SKP_int                         SNR_dB_Q7;                      /* Quality setting                                                      */\r
178 \r
179     SKP_int8                        VAD_flags[ MAX_FRAMES_PER_PACKET ];\r
180     SKP_int8                        LBRR_flag;\r
181     SKP_int                         LBRR_flags[ MAX_FRAMES_PER_PACKET ];\r
182 \r
183     SideInfoIndices                 indices;\r
184         SKP_int8                        pulses[ MAX_FRAME_LENGTH ];\r
185 \r
186     /* Input/output buffering */\r
187     SKP_int16                       inputBuf[ MAX_FRAME_LENGTH ];   /* buffer containing input signal                                       */\r
188     SKP_int                         inputBufIx;\r
189     SKP_int                         nFramesPerPacket;\r
190     SKP_int                         nFramesAnalyzed;                /* Number of frames analyzed in current packet                          */\r
191 \r
192     SKP_int                         nChannels;\r
193     SKP_int                         channelNb;\r
194 \r
195     /* Parameters For LTP scaling Control */\r
196     SKP_int                         frames_since_onset;\r
197 \r
198     /* Specifically for entropy coding */\r
199     SKP_int                         ec_prevSignalType;\r
200     SKP_int16                       ec_prevLagIndex;\r
201 \r
202     SKP_Silk_resampler_state_struct resampler_state;\r
203 \r
204     /* DTX */\r
205     SKP_int                         useDTX;                         /* Flag to enable DTX                                                   */\r
206     SKP_int                         inDTX;                          /* Flag to signal DTX period                                            */\r
207     SKP_int                         noSpeechCounter;                /* Counts concecutive nonactive frames, used by DTX                     */\r
208 \r
209     /* Inband Low Bitrate Redundancy (LBRR) data */ \r
210     SKP_int                         useInBandFEC;                   /* Saves the API setting for query                                      */\r
211     SKP_int                         LBRR_enabled;                   /* Depends on useInBandFRC, bitrate and packet loss rate                */\r
212     SKP_int                         LBRR_GainIncreases;             /* Number of shifts to Gains to get LBRR rate Voiced frames             */\r
213     SideInfoIndices                 indices_LBRR[ MAX_FRAMES_PER_PACKET ];\r
214         SKP_int8                        pulses_LBRR[ MAX_FRAMES_PER_PACKET ][ MAX_FRAME_LENGTH ];\r
215 } SKP_Silk_encoder_state;\r
216 \r
217 \r
218 /* Struct for Packet Loss Concealment */\r
219 typedef struct {\r
220     SKP_int32   pitchL_Q8;                      /* Pitch lag to use for voiced concealment                  */\r
221     SKP_int16   LTPCoef_Q14[ LTP_ORDER ];       /* LTP coeficients to use for voiced concealment            */\r
222     SKP_int16   prevLPC_Q12[ MAX_LPC_ORDER ];\r
223     SKP_int     last_frame_lost;                /* Was previous frame lost                                  */\r
224     SKP_int32   rand_seed;                      /* Seed for unvoiced signal generation                      */\r
225     SKP_int16   randScale_Q14;                  /* Scaling of unvoiced random signal                        */\r
226     SKP_int32   conc_energy;\r
227     SKP_int     conc_energy_shift;\r
228     SKP_int16   prevLTP_scale_Q14;\r
229     SKP_int32   prevGain_Q16[ MAX_NB_SUBFR ];\r
230     SKP_int     fs_kHz;\r
231 } SKP_Silk_PLC_struct;\r
232 \r
233 /* Struct for CNG */\r
234 typedef struct {\r
235     SKP_int32   CNG_exc_buf_Q10[ MAX_FRAME_LENGTH ];\r
236     SKP_int16   CNG_smth_NLSF_Q15[ MAX_LPC_ORDER ];\r
237     SKP_int32   CNG_synth_state[ MAX_LPC_ORDER ];\r
238     SKP_int32   CNG_smth_Gain_Q16;\r
239     SKP_int32   rand_seed;\r
240     SKP_int     fs_kHz;\r
241 } SKP_Silk_CNG_struct;\r
242 \r
243 /********************************/\r
244 /* Decoder state                */\r
245 /********************************/\r
246 typedef struct {\r
247     SKP_int32       prev_inv_gain_Q16;\r
248     SKP_int32       sLTP_Q16[ 2 * MAX_FRAME_LENGTH ];\r
249     SKP_int32       sLPC_Q14[ MAX_SUB_FRAME_LENGTH + MAX_LPC_ORDER ];\r
250     SKP_int32       exc_Q10[ MAX_FRAME_LENGTH ];\r
251     SKP_int16       outBuf[ 2 * MAX_FRAME_LENGTH ];             /* Buffer for output signal                                             */\r
252     SKP_int         lagPrev;                                    /* Previous Lag                                                         */\r
253     SKP_int8        LastGainIndex;                              /* Previous gain index                                                  */\r
254     SKP_int         fs_kHz;                                     /* Sampling frequency in kHz                                            */\r
255     SKP_int32       prev_API_sampleRate;                        /* Previous API sample frequency (Hz)                                   */\r
256     SKP_int         nb_subfr;                                   /* Number of 5 ms subframes in a frame                                  */\r
257     SKP_int         frame_length;                               /* Frame length (samples)                                               */\r
258     SKP_int         subfr_length;                               /* Subframe length (samples)                                            */\r
259     SKP_int         ltp_mem_length;                             /* Length of LTP memory                                                 */\r
260     SKP_int         LPC_order;                                  /* LPC order                                                            */\r
261     SKP_int16       prevNLSF_Q15[ MAX_LPC_ORDER ];              /* Used to interpolate LSFs                                             */\r
262     SKP_int         first_frame_after_reset;                    /* Flag for deactivating NLSF interp. and fluc. reduction after resets  */\r
263     const SKP_uint8 *pitch_lag_low_bits_iCDF;                   /* Pointer to iCDF table for low bits of pitch lag index                */\r
264     const SKP_uint8 *pitch_contour_iCDF;                        /* Pointer to iCDF table for pitch contour index                        */\r
265 \r
266     /* For buffering payload in case of more frames per packet */\r
267     SKP_int         nFramesDecoded;\r
268     SKP_int         nFramesPerPacket;\r
269 \r
270     /* Specifically for entropy coding */\r
271     SKP_int         ec_prevSignalType;\r
272     SKP_int16       ec_prevLagIndex;\r
273 \r
274     SKP_int         VAD_flags[ MAX_FRAMES_PER_PACKET ];\r
275     SKP_int         LBRR_flag;\r
276     SKP_int         LBRR_flags[ MAX_FRAMES_PER_PACKET ];\r
277 \r
278     SKP_Silk_resampler_state_struct resampler_state;\r
279 \r
280     const SKP_Silk_NLSF_CB_struct   *psNLSF_CB;                 /* Pointer to NLSF codebook                                             */\r
281 \r
282     /* Quantization indices */\r
283     SideInfoIndices indices;\r
284     \r
285     /* CNG state */\r
286     SKP_Silk_CNG_struct sCNG;\r
287 \r
288     /* Stuff used for PLC */\r
289     SKP_int         lossCnt;\r
290     SKP_int         prevSignalType;\r
291 \r
292     SKP_Silk_PLC_struct sPLC;\r
293 \r
294 } SKP_Silk_decoder_state;\r
295 \r
296 /************************/\r
297 /* Decoder control      */\r
298 /************************/\r
299 typedef struct {\r
300     /* prediction and coding parameters */\r
301     SKP_int             pitchL[ MAX_NB_SUBFR ];\r
302     SKP_int32           Gains_Q16[ MAX_NB_SUBFR ];\r
303     /* holds interpolated and final coefficients, 4-byte aligned */\r
304     SKP_DWORD_ALIGN SKP_int16 PredCoef_Q12[ 2 ][ MAX_LPC_ORDER ];\r
305     SKP_int16           LTPCoef_Q14[ LTP_ORDER * MAX_NB_SUBFR ];\r
306     SKP_int             LTP_scale_Q14;\r
307 } SKP_Silk_decoder_control;\r
308 \r
309 \r
310 #ifdef __cplusplus\r
311 }\r
312 #endif\r
313 \r
314 #endif\r