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