Initial Skype commit taken from FreeSwitch, which got it from the IETF draft.
[opus.git] / src / SKP_Silk_structs.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_STRUCTS_H\r
29 #define SKP_SILK_STRUCTS_H\r
30 \r
31 #include "SKP_Silk_typedef.h"\r
32 #include "SKP_Silk_SigProc_FIX.h"\r
33 #include "SKP_Silk_define.h"\r
34 \r
35 #ifdef __cplusplus\r
36 extern "C"\r
37 {\r
38 #endif\r
39 \r
40 \r
41 /************************************/\r
42 /* Noise shaping quantization state */\r
43 /************************************/\r
44 typedef struct {\r
45     SKP_int16   xq[           2 * MAX_FRAME_LENGTH ]; /* Buffer for quantized output signal */\r
46     SKP_int32   sLTP_shp_Q10[ 2 * MAX_FRAME_LENGTH ];\r
47     SKP_int32   sLPC_Q14[ MAX_FRAME_LENGTH / NB_SUBFR + MAX_LPC_ORDER ];\r
48     SKP_int32   sLF_AR_shp_Q12;\r
49     SKP_int     lagPrev;\r
50     SKP_int     sLTP_buf_idx;\r
51     SKP_int     sLTP_shp_buf_idx;\r
52     SKP_int32   rand_seed;\r
53     SKP_int32   prev_inv_gain_Q16;\r
54     SKP_int     rewhite_flag;\r
55 } SKP_Silk_nsq_state; /* FIX*/\r
56 \r
57 /* Struct for Low BitRate Redundant (LBRR) information */\r
58 typedef struct {\r
59     SKP_uint8   payload[ MAX_ARITHM_BYTES ];    \r
60     SKP_int     nBytes;                         /* Number of bytes in payload                               */\r
61     SKP_int     usage;                          /* Tells how the payload should be used as FEC              */\r
62 } SKP_SILK_LBRR_struct;\r
63 \r
64 /********************************/\r
65 /* VAD state                    */\r
66 /********************************/\r
67 typedef struct {\r
68     SKP_int32   AnaState[ 2 ];                  /* Analysis filterbank state: 0-8 kHz                       */\r
69     SKP_int32   AnaState1[ 2 ];                 /* Analysis filterbank state: 0-4 kHz                       */\r
70     SKP_int32   AnaState2[ 2 ];                 /* Analysis filterbank state: 0-2 kHz                       */\r
71     SKP_int32   XnrgSubfr[ VAD_N_BANDS ];       /* Subframe energies                                        */\r
72     SKP_int32   NrgRatioSmth_Q8[ VAD_N_BANDS ]; /* Smoothed energy level in each band                       */\r
73     SKP_int16   HPstate;                        /* State of differentiator in the lowest band               */\r
74     SKP_int32   NL[ VAD_N_BANDS ];              /* Noise energy level in each band                          */\r
75     SKP_int32   inv_NL[ VAD_N_BANDS ];          /* Inverse noise energy level in each band                  */\r
76     SKP_int32   NoiseLevelBias[ VAD_N_BANDS ];  /* Noise level estimator bias/offset                        */\r
77     SKP_int32   counter;                        /* Frame counter used in the initial phase                  */\r
78 } SKP_Silk_VAD_state;\r
79 \r
80 /*******************************/\r
81 /* Range encoder/decoder state */\r
82 /*******************************/\r
83 typedef struct {\r
84     SKP_int32   bufferLength;\r
85     SKP_int32   bufferIx;\r
86     SKP_uint32  base_Q32;\r
87     SKP_uint32  range_Q16;\r
88     SKP_int32   error;\r
89     SKP_uint8   buffer[ MAX_ARITHM_BYTES ];     /* Buffer containing payload                                */\r
90 } SKP_Silk_range_coder_state;\r
91 \r
92 /* Input frequency range detection struct */\r
93 typedef struct {\r
94     SKP_int32                   S_HP_8_kHz[ NB_SOS ][ 2 ];  /* HP filter State */\r
95     SKP_int32                   ConsecSmplsAboveThres;\r
96     SKP_int32                   ActiveSpeech_ms;            /* Accumulated time with active speech */\r
97     SKP_int                     SWB_detected;               /* Flag to indicate SWB input */\r
98     SKP_int                     WB_detected;                /* Flag to indicate WB input */\r
99 } SKP_Silk_detect_SWB_state;\r
100 \r
101 #if SWITCH_TRANSITION_FILTERING\r
102 /* Variable cut-off low-pass filter state */\r
103 typedef struct {\r
104     SKP_int32                   In_LP_State[ 2 ];           /* Low pass filter state */\r
105     SKP_int32                   transition_frame_no;        /* Counter which is mapped to a cut-off frequency */\r
106     SKP_int                     mode;                       /* Operating mode, 0: switch down, 1: switch up */\r
107 } SKP_Silk_LP_state;\r
108 #endif\r
109 \r
110 /* Structure for one stage of MSVQ */\r
111 typedef struct {\r
112     const SKP_int32             nVectors;\r
113     const SKP_int16             *CB_NLSF_Q15;\r
114     const SKP_int16             *Rates_Q5;\r
115 } SKP_Silk_NLSF_CBS;\r
116 \r
117 /* Structure containing NLSF MSVQ codebook */\r
118 typedef struct {\r
119     const SKP_int32             nStages;\r
120 \r
121     /* Fields for (de)quantizing */\r
122     const SKP_Silk_NLSF_CBS     *CBStages;\r
123     const SKP_int               *NDeltaMin_Q15;\r
124 \r
125     /* Fields for arithmetic (de)coding */\r
126     const SKP_uint16            *CDF;\r
127     const SKP_uint16 * const    *StartPtr;\r
128     const SKP_int               *MiddleIx;\r
129 } SKP_Silk_NLSF_CB_struct;\r
130 \r
131 /********************************/\r
132 /* Encoder state                */\r
133 /********************************/\r
134 typedef struct {\r
135     SKP_Silk_range_coder_state      sRC;                            /* Range coder state                                                    */\r
136     SKP_Silk_range_coder_state      sRC_LBRR;                       /* Range coder state (for low bitrate redundancy)                       */\r
137 #if HIGH_PASS_INPUT\r
138     SKP_int32                       In_HP_State[ 2 ];               /* High pass filter state                                               */\r
139 #endif\r
140 #if SWITCH_TRANSITION_FILTERING\r
141     SKP_Silk_LP_state               sLP;                            /* Low pass filter state */\r
142 #endif\r
143     SKP_Silk_VAD_state              sVAD;                           /* Voice activity detector state                                        */\r
144 \r
145     SKP_int                         LBRRprevLastGainIndex;\r
146     SKP_int                         prev_sigtype;\r
147     SKP_int                         typeOffsetPrev;                 /* Previous signal type and quantization offset                         */\r
148     SKP_int                         prevLag;\r
149     SKP_int                         prev_lagIndex;\r
150     SKP_int                         fs_kHz;                         /* Sampling frequency (kHz)                                             */\r
151     SKP_int                         fs_kHz_changed;                 /* Did we switch yet?                                                   */\r
152     SKP_int                         frame_length;                   /* Frame length (samples)                                               */\r
153     SKP_int                         subfr_length;                   /* Subframe length (samples)                                            */\r
154     SKP_int                         la_pitch;                       /* Look-ahead for pitch analysis (samples)                              */\r
155     SKP_int                         la_shape;                       /* Look-ahead for noise shape analysis (samples)                        */\r
156     SKP_int32                       TargetRate_bps;                 /* Target bitrate (bps)                                                 */\r
157     SKP_int                         PacketSize_ms;                  /* Number of milliseconds to put in each packet                         */\r
158     SKP_int                         PacketLoss_perc;                /* Packet loss rate measured by farend                                  */\r
159     SKP_int32                       frameCounter;\r
160     SKP_int                         Complexity;                     /* Complexity setting: 0-> low; 1-> medium; 2->high                     */\r
161     SKP_int                         nStatesDelayedDecision;         /* Number of states in delayed decision quantization                    */\r
162     SKP_int                         useInterpolatedNLSFs;           /* Flag for using NLSF interpolation                                    */\r
163     SKP_int                         shapingLPCOrder;                /* Filter order for noise shaping filters                               */\r
164     SKP_int                         predictLPCOrder;                /* Filter order for prediction filters                                  */\r
165     SKP_int                         pitchEstimationComplexity;      /* Complexity level for pitch estimator                                 */\r
166     SKP_int                         pitchEstimationLPCOrder;        /* Whitening filter order for pitch estimator                           */\r
167     SKP_int                         LTPQuantLowComplexity;          /* Flag for low complexity LTP quantization                             */\r
168     SKP_int                         NLSF_MSVQ_Survivors;            /* Number of survivors in NLSF MSVQ                                     */\r
169     SKP_int                         first_frame_after_reset;        /* Flag for deactivating NLSF interp. and fluc. reduction after resets  */\r
170 \r
171     /* Input/output buffering */\r
172     SKP_int16                       inputBuf[ MAX_FRAME_LENGTH ];   /* buffer containin input signal                                        */\r
173     SKP_int                         inputBufIx;\r
174     SKP_int                         nFramesInPayloadBuf;            /* number of frames sitting in outputBuf                                */\r
175     SKP_int                         nBytesInPayloadBuf;             /* number of bytes sitting in outputBuf                                 */\r
176 \r
177     /* Parameters For LTP scaling Control */\r
178     SKP_int                         frames_since_onset;\r
179 \r
180     const SKP_Silk_NLSF_CB_struct   *psNLSF_CB[ 2 ];                /* Pointers to voiced/unvoiced NLSF codebooks */\r
181 \r
182     /* Struct for Inband LBRR */ \r
183     SKP_SILK_LBRR_struct            LBRR_buffer[ MAX_LBRR_DELAY ];\r
184     SKP_int                         oldest_LBRR_idx;\r
185     SKP_int                         LBRR_enabled;\r
186     SKP_int                         LBRR_GainIncreases;             /* Number of shifts to Gains to get LBRR rate Voiced frames             */\r
187 \r
188     /* Bitrate control */\r
189     SKP_int32                       bitrateDiff;                    /* accumulated diff. between the target bitrate and the SWB/WB limits   */\r
190 \r
191 #if LOW_COMPLEXITY_ONLY\r
192     /* state for downsampling from 24 to 16 kHz in low complexity mode */\r
193     SKP_int16                       resample24To16state[ SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ];\r
194 #else\r
195     SKP_int32                       resample24To16state[ 11 ];      /* state for downsampling from 24 to 16 kHz                             */\r
196 #endif\r
197     SKP_int32                       resample24To12state[ 6 ];       /* state for downsampling from 24 to 12 kHz                             */\r
198     SKP_int32                       resample24To8state[ 7 ];        /* state for downsampling from 24 to  8 kHz                             */\r
199     SKP_int32                       resample16To12state[ 15 ];      /* state for downsampling from 16 to 12 kHz                             */\r
200     SKP_int32                       resample16To8state[ 6 ];        /* state for downsampling from 16 to  8 kHz                             */\r
201 #if LOW_COMPLEXITY_ONLY\r
202     /* state for downsampling from 12 to 8 kHz in low complexity mode */\r
203     SKP_int16                       resample12To8state[ SigProc_Resample_2_3_coarse_NUM_FIR_COEFS - 1 ];    \r
204 #else\r
205     SKP_int32                       resample12To8state[ 11 ];       /* state for downsampling from 12 to  8 kHz                             */\r
206 #endif\r
207 \r
208     /* DTX */\r
209     SKP_int                         noSpeechCounter;                /* Counts concecutive nonactive frames, used by DTX                     */\r
210     SKP_int                         useDTX;                         /* Flag to enable DTX                                                   */\r
211     SKP_int                         inDTX;                          /* Flag to signal DTX period                                            */\r
212     SKP_int                         vadFlag;                        /* Flag to indicate Voice Activity                                      */\r
213 \r
214     /* Struct for detecting SWB input */\r
215     SKP_Silk_detect_SWB_state       sSWBdetect;\r
216 \r
217 \r
218     /********************************************/\r
219     /* Buffers etc used by the new bitstream V4 */\r
220     /********************************************/\r
221     SKP_int                         q[ MAX_FRAME_LENGTH * MAX_FRAMES_PER_PACKET ];      /* pulse signal buffer */\r
222     SKP_int                         q_LBRR[ MAX_FRAME_LENGTH * MAX_FRAMES_PER_PACKET ]; /* pulse signal buffer */\r
223     SKP_int                         sigtype[ MAX_FRAMES_PER_PACKET ];\r
224     SKP_int                         QuantOffsetType[ MAX_FRAMES_PER_PACKET ];\r
225     SKP_int                         extension_layer;                                    /* Add extension layer      */\r
226     SKP_int                         bitstream_v;                                        /* Holds bitstream version  */\r
227 } SKP_Silk_encoder_state;\r
228 \r
229 \r
230 /************************/\r
231 /* Encoder control      */\r
232 /************************/\r
233 typedef struct {\r
234     /* Quantization indices */\r
235     SKP_int     lagIndex;\r
236     SKP_int     contourIndex;\r
237     SKP_int     PERIndex;\r
238     SKP_int     LTPIndex[ NB_SUBFR ];\r
239     SKP_int     NLSFIndices[ NLSF_MSVQ_MAX_CB_STAGES ];  /* NLSF path of quantized LSF vector   */\r
240     SKP_int     NLSFInterpCoef_Q2;\r
241     SKP_int     GainsIndices[ NB_SUBFR ];\r
242     SKP_int32   Seed;\r
243     SKP_int     LTP_scaleIndex;\r
244     SKP_int     RateLevelIndex;\r
245     SKP_int     QuantOffsetType;\r
246     SKP_int     sigtype;\r
247 \r
248     /* Prediction and coding parameters */\r
249     SKP_int     pitchL[ NB_SUBFR ];\r
250 \r
251     SKP_int     LBRR_usage;                     /* Low bitrate redundancy usage                             */\r
252 } SKP_Silk_encoder_control;\r
253 \r
254 /* Struct for Packet Loss Concealment */\r
255 typedef struct {\r
256     SKP_int32   pitchL_Q8;                      /* Pitch lag to use for voiced concealment                  */\r
257     SKP_int16   LTPCoef_Q14[ LTP_ORDER ];       /* LTP coeficients to use for voiced concealment            */\r
258     SKP_int16   prevLPC_Q12[ MAX_LPC_ORDER ];\r
259     SKP_int     last_frame_lost;                /* Was previous frame lost                                  */\r
260     SKP_int32   rand_seed;                      /* Seed for unvoiced signal generation                      */\r
261     SKP_int16   randScale_Q14;                  /* Scaling of unvoiced random signal                        */\r
262     SKP_int32   conc_energy;\r
263     SKP_int     conc_energy_shift;\r
264     SKP_int16   prevLTP_scale_Q14;\r
265     SKP_int32   prevGain_Q16[ NB_SUBFR ];\r
266     SKP_int     fs_kHz;\r
267 } SKP_Silk_PLC_struct;\r
268 \r
269 /* Struct for CNG */\r
270 typedef struct {\r
271     SKP_int32   CNG_exc_buf_Q10[ MAX_FRAME_LENGTH ];\r
272     SKP_int     CNG_smth_NLSF_Q15[ MAX_LPC_ORDER ];\r
273     SKP_int32   CNG_synth_state[ MAX_LPC_ORDER ];\r
274     SKP_int32   CNG_smth_Gain_Q16;\r
275     SKP_int32   rand_seed;\r
276     SKP_int     fs_kHz;\r
277 } SKP_Silk_CNG_struct;\r
278 \r
279 /********************************/\r
280 /* Decoder state                */\r
281 /********************************/\r
282 typedef struct {\r
283     SKP_Silk_range_coder_state  sRC;                            /* Range coder state                                                    */\r
284     SKP_int32       prev_inv_gain_Q16;\r
285     SKP_int32       sLTP_Q16[ 2 * MAX_FRAME_LENGTH ];\r
286     SKP_int32       sLPC_Q14[ MAX_FRAME_LENGTH / NB_SUBFR + MAX_LPC_ORDER ];\r
287     SKP_int32       exc_Q10[ MAX_FRAME_LENGTH ];\r
288     SKP_int32       res_Q10[ MAX_FRAME_LENGTH ];\r
289     SKP_int16       outBuf[ 2 * MAX_FRAME_LENGTH ];             /* Buffer for output signal                                             */\r
290     SKP_int         sLTP_buf_idx;                               /* LTP_buf_index                                                        */\r
291     SKP_int         lagPrev;                                    /* Previous Lag                                                         */\r
292     SKP_int         LastGainIndex;                              /* Previous gain index                                                  */\r
293     SKP_int         LastGainIndex_EnhLayer;                     /* Previous gain index                                                  */\r
294     SKP_int         typeOffsetPrev;                             /* Previous signal type and quantization offset                         */\r
295     SKP_int32       HPState[ DEC_HP_ORDER ];                    /* HP filter state                                                      */\r
296     const SKP_int16 *HP_A;                                      /* HP filter AR coefficients                                            */\r
297     const SKP_int16 *HP_B;                                      /* HP filter MA coefficients                                            */\r
298     SKP_int         fs_kHz;                                     /* Sampling frequency in kHz                                            */\r
299     SKP_int         frame_length;                               /* Frame length (samples)                                               */\r
300     SKP_int         subfr_length;                               /* Subframe length (samples)                                            */\r
301     SKP_int         LPC_order;                                  /* LPC order                                                            */\r
302     SKP_int         prevNLSF_Q15[ MAX_LPC_ORDER ];              /* Used to interpolate LSFs                                             */\r
303     SKP_int         first_frame_after_reset;                    /* Flag for deactivating NLSF interp. and fluc. reduction after resets  */\r
304 \r
305     /* For buffering payload in case of more frames per packet */\r
306     SKP_int         nBytesLeft;\r
307     SKP_int         nFramesDecoded;\r
308     SKP_int         nFramesInPacket;\r
309     SKP_int         moreInternalDecoderFrames;\r
310     SKP_int         FrameTermination;\r
311 \r
312     SKP_int32       resampleState[ 15 ];\r
313 \r
314     const SKP_Silk_NLSF_CB_struct *psNLSF_CB[ 2 ];      /* Pointers to voiced/unvoiced NLSF codebooks */\r
315 \r
316     SKP_int         sigtype[               MAX_FRAMES_PER_PACKET ];\r
317     SKP_int         QuantOffsetType[       MAX_FRAMES_PER_PACKET ];\r
318     SKP_int         GainsIndices[          MAX_FRAMES_PER_PACKET ][ NB_SUBFR ];\r
319     SKP_int         GainsIndices_EnhLayer[ MAX_FRAMES_PER_PACKET ][ NB_SUBFR ];\r
320     SKP_int         NLSFIndices[           MAX_FRAMES_PER_PACKET ][ NLSF_MSVQ_MAX_CB_STAGES ];\r
321     SKP_int         NLSFInterpCoef_Q2[     MAX_FRAMES_PER_PACKET ];\r
322     SKP_int         lagIndex[              MAX_FRAMES_PER_PACKET ];\r
323     SKP_int         contourIndex[          MAX_FRAMES_PER_PACKET ];\r
324     SKP_int         PERIndex[              MAX_FRAMES_PER_PACKET ];\r
325     SKP_int         LTPIndex[              MAX_FRAMES_PER_PACKET ][ NB_SUBFR ];\r
326     SKP_int         LTP_scaleIndex[        MAX_FRAMES_PER_PACKET ];\r
327     SKP_int         Seed[                  MAX_FRAMES_PER_PACKET ];\r
328     SKP_int         vadFlagBuf[            MAX_FRAMES_PER_PACKET ];\r
329     \r
330     /* Parameters used to investigate if inband FEC is used */\r
331     SKP_int         vadFlag;\r
332     SKP_int         no_FEC_counter;                             /* Counts number of frames wo inband FEC                                */\r
333     SKP_int         inband_FEC_offset;                          /* 0: no FEC, 1: FEC with 1 packet offset, 2: FEC w 2 packets offset    */ \r
334 \r
335     /* CNG state */\r
336     SKP_Silk_CNG_struct sCNG;\r
337 \r
338     /* Stuff used for PLC */\r
339     SKP_Silk_PLC_struct sPLC;\r
340     SKP_int         lossCnt;\r
341     SKP_int         prev_sigtype;                               /* Previous sigtype                                                     */\r
342 #ifdef USE_INTERPOLATION_PLC\r
343     SKP_int16       prevQuant[ MAX_FRAME_LENGTH ];\r
344     SKP_int         prevPitchL[ NB_SUBFR ];                     /* Previous Lags used                                                   */\r
345     SKP_int16       prevLTPCoef_Q14[ NB_SUBFR * LTP_ORDER ];    /* Previous LTCoefs used                                                */\r
346     SKP_int16       prevAR_Q12[ MAX_LPC_ORDER ];\r
347     SKP_int         interpolDistance;                           /* Number of frames between old and new recieved packet                 */\r
348 #endif\r
349 \r
350 \r
351 \r
352 \r
353     SKP_int                 bitstream_v;                        /* Holds bitstream version                                              */\r
354 } SKP_Silk_decoder_state;\r
355 \r
356 /************************/\r
357 /* Decoder control      */\r
358 /************************/\r
359 typedef struct {\r
360     /* prediction and coding parameters */\r
361     SKP_int             pitchL[ NB_SUBFR ];\r
362     SKP_int32           Gains_Q16[ NB_SUBFR ];\r
363     SKP_int32           Seed;\r
364     /* holds interpolated and final coefficients, 4-byte aligned */\r
365     SKP_array_of_int16_4_byte_aligned( PredCoef_Q12[ 2 ], MAX_LPC_ORDER );\r
366     SKP_int16           LTPCoef_Q14[ LTP_ORDER * NB_SUBFR ];\r
367     SKP_int             LTP_scale_Q14;\r
368 \r
369     /* quantization indices */\r
370     SKP_int             PERIndex;\r
371     SKP_int             RateLevelIndex;\r
372     SKP_int             QuantOffsetType;\r
373     SKP_int             sigtype;\r
374     SKP_int             NLSFInterpCoef_Q2;\r
375 } SKP_Silk_decoder_control;\r
376 \r
377 #ifdef __cplusplus\r
378 }\r
379 #endif\r
380 \r
381 #endif\r