Update SILK code using the CELT range coder
[opus.git] / src_common / 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 #include "entenc.h"\r
35 #include "entdec.h"\r
36 \r
37 #ifdef __cplusplus\r
38 extern "C"\r
39 {\r
40 #endif\r
41 \r
42 \r
43 /************************************/\r
44 /* Noise shaping quantization state */\r
45 /************************************/\r
46 typedef struct {\r
47     SKP_int16   xq[           2 * MAX_FRAME_LENGTH ]; /* Buffer for quantized output signal */\r
48     SKP_int32   sLTP_shp_Q10[ 2 * MAX_FRAME_LENGTH ];\r
49     SKP_int32   sLPC_Q14[ MAX_FRAME_LENGTH / MAX_NB_SUBFR + MAX_LPC_ORDER ];\r
50     SKP_int32   sAR2_Q14[ MAX_SHAPE_LPC_ORDER ];\r
51     SKP_int32   sLF_AR_shp_Q12;\r
52     SKP_int     lagPrev;\r
53     SKP_int     sLTP_buf_idx;\r
54     SKP_int     sLTP_shp_buf_idx;\r
55     SKP_int32   rand_seed;\r
56     SKP_int32   prev_inv_gain_Q16;\r
57     SKP_int     rewhite_flag;\r
58 } SKP_Silk_nsq_state; /* FIX*/\r
59 \r
60 /* Struct for Low BitRate Redundant (LBRR) information */\r
61 typedef struct {\r
62     SKP_uint8   payload[ MAX_ARITHM_BYTES ];    \r
63     SKP_int     nBytes;                         /* Number of bytes in payload                               */\r
64     SKP_int     usage;                          /* Tells how the payload should be used as FEC              */\r
65 } SKP_SILK_LBRR_struct;\r
66 \r
67 /********************************/\r
68 /* VAD state                    */\r
69 /********************************/\r
70 typedef struct {\r
71     SKP_int32   AnaState[ 2 ];                  /* Analysis filterbank state: 0-8 kHz                       */\r
72     SKP_int32   AnaState1[ 2 ];                 /* Analysis filterbank state: 0-4 kHz                       */\r
73     SKP_int32   AnaState2[ 2 ];                 /* Analysis filterbank state: 0-2 kHz                       */\r
74     SKP_int32   XnrgSubfr[ VAD_N_BANDS ];       /* Subframe energies                                        */\r
75     SKP_int32   NrgRatioSmth_Q8[ VAD_N_BANDS ]; /* Smoothed energy level in each band                       */\r
76     SKP_int16   HPstate;                        /* State of differentiator in the lowest band               */\r
77     SKP_int32   NL[ VAD_N_BANDS ];              /* Noise energy level in each band                          */\r
78     SKP_int32   inv_NL[ VAD_N_BANDS ];          /* Inverse noise energy level in each band                  */\r
79     SKP_int32   NoiseLevelBias[ VAD_N_BANDS ];  /* Noise level estimator bias/offset                        */\r
80     SKP_int32   counter;                        /* Frame counter used in the initial phase                  */\r
81 } SKP_Silk_VAD_state;\r
82 \r
83 /*******************************/\r
84 /* Range encoder/decoder state */\r
85 /*******************************/\r
86 typedef struct {\r
87     SKP_int32   bufferLength;\r
88     SKP_int32   bufferIx;\r
89     SKP_uint32  base_Q32;\r
90     SKP_uint32  range_Q16;\r
91     SKP_int32   error;\r
92     SKP_uint8   buffer[ MAX_ARITHM_BYTES ];     /* Buffer containing payload                                */\r
93     /* The CELT entropy encoder + decoder */\r
94     ec_enc      range_enc_celt_state;\r
95     ec_dec      range_dec_celt_state;\r
96 } SKP_Silk_range_coder_state;\r
97 \r
98 /* Input frequency range detection struct */\r
99 typedef struct {\r
100     SKP_int32                   S_HP_8_kHz[ NB_SOS ][ 2 ];  /* HP filter State */\r
101     SKP_int32                   ConsecSmplsAboveThres;\r
102     SKP_int32                   ActiveSpeech_ms;            /* Accumulated time with active speech */\r
103     SKP_int                     SWB_detected;               /* Flag to indicate SWB input */\r
104     SKP_int                     WB_detected;                /* Flag to indicate WB input */\r
105 } SKP_Silk_detect_SWB_state;\r
106 \r
107 #if SWITCH_TRANSITION_FILTERING\r
108 /* Variable cut-off low-pass filter state */\r
109 typedef struct {\r
110     SKP_int32                   In_LP_State[ 2 ];           /* Low pass filter state */\r
111     SKP_int32                   transition_frame_no;        /* Counter which is mapped to a cut-off frequency */\r
112     SKP_int                     mode;                       /* Operating mode, 0: switch down, 1: switch up */\r
113 } SKP_Silk_LP_state;\r
114 #endif\r
115 \r
116 /* Structure for one stage of MSVQ */\r
117 typedef struct {\r
118     const SKP_int32             nVectors;\r
119     const SKP_int16             *CB_NLSF_Q15;\r
120     const SKP_int16             *Rates_Q5;\r
121 } SKP_Silk_NLSF_CBS;\r
122 \r
123 /* Structure containing NLSF MSVQ codebook */\r
124 typedef struct {\r
125     const SKP_int32             nStages;\r
126 \r
127     /* Fields for (de)quantizing */\r
128     const SKP_Silk_NLSF_CBS     *CBStages;\r
129     const SKP_int               *NDeltaMin_Q15;\r
130 \r
131     /* Fields for arithmetic (de)coding */\r
132     const SKP_uint16            *CDF;\r
133     const SKP_uint16 * const    *StartPtr;\r
134     const SKP_int               *MiddleIx;\r
135 } SKP_Silk_NLSF_CB_struct;\r
136 \r
137 /********************************/\r
138 /* Encoder state                */\r
139 /********************************/\r
140 typedef struct {\r
141     SKP_Silk_range_coder_state      sRC;                            /* Range coder state                                                    */\r
142     SKP_Silk_range_coder_state      sRC_LBRR;                       /* Range coder state (for low bitrate redundancy)                       */\r
143 #if HIGH_PASS_INPUT\r
144     SKP_int32                       In_HP_State[ 2 ];               /* High pass filter state                                               */\r
145 #endif\r
146 #if SWITCH_TRANSITION_FILTERING\r
147     SKP_Silk_LP_state               sLP;                            /* Low pass filter state */\r
148 #endif\r
149     SKP_Silk_VAD_state              sVAD;                           /* Voice activity detector state                                        */\r
150 \r
151     SKP_int                         LBRRprevLastGainIndex;\r
152     SKP_int                         prev_sigtype;\r
153     SKP_int                         typeOffsetPrev;                 /* Previous signal type and quantization offset                         */\r
154     SKP_int                         prevLag;\r
155     SKP_int                         prev_lagIndex;\r
156     SKP_int32                       API_fs_Hz;                      /* API sampling frequency (Hz)                                          */\r
157     SKP_int32                       prev_API_fs_Hz;                 /* Previous API sampling frequency (Hz)                                 */\r
158     SKP_int                         maxInternal_fs_kHz;             /* Maximum internal sampling frequency (kHz)                            */\r
159     SKP_int                         fs_kHz;                         /* Internal sampling frequency (kHz)                                    */\r
160     SKP_int                         fs_kHz_changed;                 /* Did we switch yet?                                                   */\r
161     SKP_int                         nb_subfr;                       /* Number of 5 ms subframes in a frame                                  */\r
162     SKP_int                         frame_length;                   /* Frame length (samples)                                               */\r
163     SKP_int                         subfr_length;                   /* Subframe length (samples)                                            */\r
164     SKP_int                         ltp_mem_length;                 /* Length of LTP memory                                                 */\r
165     SKP_int                         la_pitch;                       /* Look-ahead for pitch analysis (samples)                              */\r
166     SKP_int                         la_shape;                       /* Look-ahead for noise shape analysis (samples)                        */\r
167     SKP_int32                       TargetRate_bps;                 /* Target bitrate (bps)                                                 */\r
168     SKP_int                         PacketSize_ms;                  /* Number of milliseconds to put in each packet                         */\r
169     SKP_int                         PacketLoss_perc;                /* Packet loss rate measured by farend                                  */\r
170     SKP_int32                       frameCounter;\r
171     SKP_int                         Complexity;                     /* Complexity setting: 0-> low; 1-> medium; 2->high                     */\r
172     SKP_int                         nStatesDelayedDecision;         /* Number of states in delayed decision quantization                    */\r
173     SKP_int                         useInterpolatedNLSFs;           /* Flag for using NLSF interpolation                                    */\r
174     SKP_int                         shapingLPCOrder;                /* Filter order for noise shaping filters                               */\r
175     SKP_int                         predictLPCOrder;                /* Filter order for prediction filters                                  */\r
176     SKP_int                         pitchEstimationComplexity;      /* Complexity level for pitch estimator                                 */\r
177     SKP_int                         pitchEstimationLPCOrder;        /* Whitening filter order for pitch estimator                           */\r
178     SKP_int                         LTPQuantLowComplexity;          /* Flag for low complexity LTP quantization                             */\r
179     SKP_int                         NLSF_MSVQ_Survivors;            /* Number of survivors in NLSF MSVQ                                     */\r
180     SKP_int                         first_frame_after_reset;        /* Flag for deactivating NLSF interp. and fluc. reduction after resets  */\r
181 \r
182     /* Input/output buffering */\r
183     SKP_int16                       inputBuf[ MAX_FRAME_LENGTH ];   /* buffer containin input signal                                        */\r
184     SKP_int                         inputBufIx;\r
185     SKP_int                         nFramesInPayloadBuf;            /* number of frames sitting in outputBuf                                */\r
186     SKP_int                         nBytesInPayloadBuf;             /* number of bytes sitting in outputBuf                                 */\r
187 \r
188     /* Parameters For LTP scaling Control */\r
189     SKP_int                         frames_since_onset;\r
190 \r
191     const SKP_Silk_NLSF_CB_struct   *psNLSF_CB[ 2 ];                /* Pointers to voiced/unvoiced NLSF codebooks */\r
192 \r
193     /* Struct for Inband LBRR */ \r
194     SKP_SILK_LBRR_struct            LBRR_buffer[ MAX_LBRR_DELAY ];\r
195     SKP_int                         oldest_LBRR_idx;\r
196     SKP_int                         useInBandFEC;                   /* Saves the API setting for query                                      */\r
197     SKP_int                         LBRR_enabled;\r
198     SKP_int                         LBRR_GainIncreases;             /* Number of shifts to Gains to get LBRR rate Voiced frames             */\r
199 \r
200     /* Bitrate control */\r
201     SKP_int32                       bitrateDiff;                    /* Accumulated diff. between the target bitrate and the switch bitrates */\r
202     SKP_int32                       bitrate_threshold_up;           /* Threshold for switching to a higher internal sample frequency        */\r
203     SKP_int32                       bitrate_threshold_down;         /* Threshold for switching to a lower internal sample frequency         */\r
204 \r
205     SKP_Silk_resampler_state_struct  resampler_state;\r
206 \r
207     /* DTX */\r
208     SKP_int                         noSpeechCounter;                /* Counts concecutive nonactive frames, used by DTX                     */\r
209     SKP_int                         useDTX;                         /* Flag to enable DTX                                                   */\r
210     SKP_int                         inDTX;                          /* Flag to signal DTX period                                            */\r
211     SKP_int                         vadFlag;                        /* Flag to indicate Voice Activity                                      */\r
212 \r
213     /* Struct for detecting SWB input */\r
214     SKP_Silk_detect_SWB_state       sSWBdetect;\r
215 \r
216 \r
217     /* Buffers */\r
218         SKP_int8                        q[ MAX_FRAME_LENGTH * MAX_FRAMES_PER_PACKET ];      /* pulse signal buffer */\r
219     SKP_int8                        q_LBRR[ MAX_FRAME_LENGTH * MAX_FRAMES_PER_PACKET ]; /* pulse signal buffer */\r
220     SKP_int                         sigtype[ MAX_FRAMES_PER_PACKET ];\r
221     SKP_int                         QuantOffsetType[ MAX_FRAMES_PER_PACKET ];\r
222 } SKP_Silk_encoder_state;\r
223 \r
224 \r
225 /************************/\r
226 /* Encoder control      */\r
227 /************************/\r
228 typedef struct {\r
229     /* Quantization indices */\r
230     SKP_int     lagIndex;\r
231     SKP_int     contourIndex;\r
232     SKP_int     PERIndex;\r
233     SKP_int     LTPIndex[ MAX_NB_SUBFR ];\r
234     SKP_int     NLSFIndices[ NLSF_MSVQ_MAX_CB_STAGES ];  /* NLSF path of quantized LSF vector   */\r
235     SKP_int     NLSFInterpCoef_Q2;\r
236     SKP_int     GainsIndices[ MAX_NB_SUBFR ];\r
237         SKP_int     warping_Q16;\r
238     SKP_int32   Seed;\r
239     SKP_int     LTP_scaleIndex;\r
240     SKP_int     RateLevelIndex;\r
241     SKP_int     QuantOffsetType;\r
242     SKP_int     sigtype;\r
243 \r
244     /* Prediction and coding parameters */\r
245     SKP_int     pitchL[ MAX_NB_SUBFR ];\r
246 \r
247     SKP_int     LBRR_usage;                     /* Low bitrate redundancy usage                             */\r
248 } SKP_Silk_encoder_control;\r
249 \r
250 /* Struct for Packet Loss Concealment */\r
251 typedef struct {\r
252     SKP_int     pitchL[ MAX_NB_SUBFR ];         /* Pitch lag per 5 ms for last 20 ms                        */\r
253     SKP_int32   pitchL_Q8;                      /* Pitch lag to use for voiced concealment                  */\r
254     SKP_int16   LTPCoef_Q14[ LTP_ORDER ];       /* LTP coeficients to use for voiced concealment            */\r
255     SKP_int16   prevLPC_Q12[ MAX_LPC_ORDER ];\r
256     SKP_int     last_frame_lost;                /* Was previous frame lost                                  */\r
257     SKP_int32   rand_seed;                      /* Seed for unvoiced signal generation                      */\r
258     SKP_int16   randScale_Q14;                  /* Scaling of unvoiced random signal                        */\r
259     SKP_int32   conc_energy;\r
260     SKP_int     conc_energy_shift;\r
261     SKP_int16   prevLTP_scale_Q14;\r
262     SKP_int32   prevGain_Q16[ MAX_NB_SUBFR ];\r
263     SKP_int     fs_kHz;\r
264 } SKP_Silk_PLC_struct;\r
265 \r
266 /* Struct for CNG */\r
267 typedef struct {\r
268     SKP_int32   CNG_exc_buf_Q10[ MAX_FRAME_LENGTH ];\r
269     SKP_int     CNG_smth_NLSF_Q15[ MAX_LPC_ORDER ];\r
270     SKP_int32   CNG_synth_state[ MAX_LPC_ORDER ];\r
271     SKP_int32   CNG_smth_Gain_Q16;\r
272     SKP_int32   rand_seed;\r
273     SKP_int     fs_kHz;\r
274 } SKP_Silk_CNG_struct;\r
275 \r
276 /********************************/\r
277 /* Decoder state                */\r
278 /********************************/\r
279 typedef struct {\r
280     SKP_Silk_range_coder_state  sRC;                            /* Range coder state                                                    */\r
281     SKP_int32       prev_inv_gain_Q16;\r
282     SKP_int32       sLTP_Q16[ 2 * MAX_FRAME_LENGTH ];\r
283     SKP_int32       sLPC_Q14[ MAX_FRAME_LENGTH / MAX_NB_SUBFR + MAX_LPC_ORDER ];\r
284     SKP_int32       exc_Q10[ MAX_FRAME_LENGTH ];\r
285     SKP_int32       res_Q10[ MAX_FRAME_LENGTH ];\r
286     SKP_int16       outBuf[ 2 * MAX_FRAME_LENGTH ];             /* Buffer for output signal                                             */\r
287     SKP_int         sLTP_buf_idx;                               /* LTP_buf_index                                                        */\r
288     SKP_int         lagPrev;                                    /* Previous Lag                                                         */\r
289     SKP_int         LastGainIndex;                              /* Previous gain index                                                  */\r
290     SKP_int         LastGainIndex_EnhLayer;                     /* Previous gain index                                                  */\r
291     SKP_int         typeOffsetPrev;                             /* Previous signal type and quantization offset                         */\r
292     SKP_int32       HPState[ DEC_HP_ORDER ];                    /* HP filter state                                                      */\r
293     const SKP_int16 *HP_A;                                      /* HP filter AR coefficients                                            */\r
294     const SKP_int16 *HP_B;                                      /* HP filter MA coefficients                                            */\r
295     SKP_int         fs_kHz;                                     /* Sampling frequency in kHz                                            */\r
296     SKP_int32       prev_API_sampleRate;                        /* Previous API sample frequency (Hz)                                   */\r
297     SKP_int         nb_subfr;                                   /* Number of 5 ms subframes in a frame                                  */\r
298     SKP_int         frame_length;                               /* Frame length (samples)                                               */\r
299     SKP_int         subfr_length;                               /* Subframe length (samples)                                            */\r
300     SKP_int         ltp_mem_length;                             /* Length of LTP memory                                                 */\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_Silk_resampler_state_struct  resampler_state;\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 ][ MAX_NB_SUBFR ];\r
319     SKP_int         GainsIndices_EnhLayer[ MAX_FRAMES_PER_PACKET ][ MAX_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 ][ MAX_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_int         lossCnt;\r
340     SKP_int         prev_sigtype;                               /* Previous sigtype                                                     */\r
341 \r
342     SKP_Silk_PLC_struct sPLC;\r
343 \r
344 } SKP_Silk_decoder_state;\r
345 \r
346 /************************/\r
347 /* Decoder control      */\r
348 /************************/\r
349 typedef struct {\r
350     /* prediction and coding parameters */\r
351     SKP_int             pitchL[ MAX_NB_SUBFR ];\r
352     SKP_int32           Gains_Q16[ MAX_NB_SUBFR ];\r
353     SKP_int32           Seed;\r
354     /* holds interpolated and final coefficients, 4-byte aligned */\r
355     SKP_array_of_int16_4_byte_aligned( PredCoef_Q12[ 2 ], MAX_LPC_ORDER );\r
356     SKP_int16           LTPCoef_Q14[ LTP_ORDER * MAX_NB_SUBFR ];\r
357     SKP_int             LTP_scale_Q14;\r
358 \r
359     /* quantization indices */\r
360     SKP_int             PERIndex;\r
361     SKP_int             RateLevelIndex;\r
362     SKP_int             QuantOffsetType;\r
363     SKP_int             sigtype;\r
364     SKP_int             NLSFInterpCoef_Q2;\r
365 } SKP_Silk_decoder_control;\r
366 \r
367 #ifdef __cplusplus\r
368 }\r
369 #endif\r
370 \r
371 #endif\r