Eliminate some unreachable cases from the cwrs code and fixup the
[opus.git] / silk / silk_structs.h
1 /***********************************************************************
2 Copyright (c) 2006-2011, Skype Limited. All rights reserved.
3 Redistribution and use in source and binary forms, with or without
4 modification, (subject to the limitations in the disclaimer below)
5 are permitted provided that the following conditions are met:
6 - Redistributions of source code must retain the above copyright notice,
7 this list of conditions and the following disclaimer.
8 - Redistributions in binary form must reproduce the above copyright
9 notice, this list of conditions and the following disclaimer in the
10 documentation and/or other materials provided with the distribution.
11 - Neither the name of Skype Limited, nor the names of specific
12 contributors, may be used to endorse or promote products derived from
13 this software without specific prior written permission.
14 NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
15 BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
16 CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
17 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
22 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***********************************************************************/
27
28 #ifndef SILK_STRUCTS_H
29 #define SILK_STRUCTS_H
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include "silk_typedef.h"
36 #include "silk_SigProc_FIX.h"
37 #include "silk_define.h"
38 #include "entenc.h"
39 #include "entdec.h"
40
41 #ifdef __cplusplus
42 extern "C"
43 {
44 #endif
45
46 /************************************/
47 /* Noise shaping quantization state */
48 /************************************/
49 typedef struct {
50     opus_int16   xq[           2 * MAX_FRAME_LENGTH ]; /* Buffer for quantized output signal */
51     opus_int32   sLTP_shp_Q10[ 2 * MAX_FRAME_LENGTH ];
52     opus_int32   sLPC_Q14[ MAX_SUB_FRAME_LENGTH + NSQ_LPC_BUF_LENGTH ];
53     opus_int32   sAR2_Q14[ MAX_SHAPE_LPC_ORDER ];
54     opus_int32   sLF_AR_shp_Q12;
55     opus_int     lagPrev;
56     opus_int     sLTP_buf_idx;
57     opus_int     sLTP_shp_buf_idx;
58     opus_int32   rand_seed;
59     opus_int32   prev_inv_gain_Q16;
60     opus_int     rewhite_flag;
61 } silk_nsq_state;
62
63 /********************************/
64 /* VAD state                    */
65 /********************************/
66 typedef struct {
67     opus_int32   AnaState[ 2 ];                  /* Analysis filterbank state: 0-8 kHz                       */
68     opus_int32   AnaState1[ 2 ];                 /* Analysis filterbank state: 0-4 kHz                       */
69     opus_int32   AnaState2[ 2 ];                 /* Analysis filterbank state: 0-2 kHz                       */
70     opus_int32   XnrgSubfr[ VAD_N_BANDS ];       /* Subframe energies                                        */
71     opus_int32   NrgRatioSmth_Q8[ VAD_N_BANDS ]; /* Smoothed energy level in each band                       */
72     opus_int16   HPstate;                        /* State of differentiator in the lowest band               */
73     opus_int32   NL[ VAD_N_BANDS ];              /* Noise energy level in each band                          */
74     opus_int32   inv_NL[ VAD_N_BANDS ];          /* Inverse noise energy level in each band                  */
75     opus_int32   NoiseLevelBias[ VAD_N_BANDS ];  /* Noise level estimator bias/offset                        */
76     opus_int32   counter;                        /* Frame counter used in the initial phase                  */
77 } silk_VAD_state;
78
79 /* Variable cut-off low-pass filter state */
80 typedef struct {
81     opus_int32                   In_LP_State[ 2 ];           /* Low pass filter state */
82     opus_int32                   transition_frame_no;        /* Counter which is mapped to a cut-off frequency */
83     opus_int                     mode;                       /* Operating mode, <0: switch down, >0: switch up; 0: do nothing */
84 } silk_LP_state;
85
86 /* Structure containing NLSF codebook */
87 typedef struct {
88     const opus_int16             nVectors;
89     const opus_int16             order;
90     const opus_int16             quantStepSize_Q16;
91     const opus_int16             invQuantStepSize_Q6;
92     const opus_uint8             *CB1_NLSF_Q8;
93     const opus_uint8             *CB1_iCDF;
94     const opus_uint8             *pred_Q8;
95     const opus_uint8             *ec_sel;
96     const opus_uint8             *ec_iCDF;
97     const opus_uint8             *ec_Rates_Q5;
98     const opus_int16             *deltaMin_Q15;
99 } silk_NLSF_CB_struct;
100
101 typedef struct {
102     opus_int16                   pred_prev_Q13[ 2 ];
103     opus_int16                   sMid[ 2 ];
104     opus_int16                   sSide[ 2 ];
105     opus_int32                   mid_side_amp_Q0[ 4 ];
106     opus_int16                   smth_width_Q14;
107     opus_int16                   width_prev_Q14;
108     opus_int8                    ix[ MAX_FRAMES_PER_PACKET ][ 2 ][ 4 ];
109 } stereo_enc_state;
110
111 typedef struct {
112     opus_int16                   pred_prev_Q13[ 2 ];
113     opus_int16                   sMid[ 2 ];
114     opus_int16                   sSide[ 2 ];
115 } stereo_dec_state;
116
117 typedef struct {
118     opus_int8                    GainsIndices[ MAX_NB_SUBFR ];
119     opus_int8                    LTPIndex[ MAX_NB_SUBFR ];
120     opus_int8                    NLSFIndices[ MAX_LPC_ORDER + 1 ];
121     opus_int16                   lagIndex;
122     opus_int8                    contourIndex;
123     opus_int8                    signalType;
124     opus_int8                    quantOffsetType;
125     opus_int8                    NLSFInterpCoef_Q2;
126     opus_int8                    PERIndex;
127     opus_int8                    LTP_scaleIndex;
128     opus_int8                    Seed;
129 } SideInfoIndices;
130
131 /********************************/
132 /* Encoder state                */
133 /********************************/
134 typedef struct {
135     opus_int32                       In_HP_State[ 2 ];               /* High pass filter state                                               */
136     opus_int32                       variable_HP_smth1_Q15;          /* State of first smoother                                              */
137     opus_int32                       variable_HP_smth2_Q15;          /* State of second smoother                                             */
138     opus_int                         HP_cutoff_Hz;                   /* Fixed cutoff frequency (if zero: adaptive)                           */
139     silk_LP_state                   sLP;                            /* Low pass filter state                                                */
140     silk_VAD_state                  sVAD;                           /* Voice activity detector state                                        */
141     silk_nsq_state                  sNSQ;                           /* Noise Shape Quantizer State                                          */
142     opus_int16                       prev_NLSFq_Q15[ MAX_LPC_ORDER ];/* Previously quantized NLSF vector                                     */
143     opus_int                         speech_activity_Q8;             /* Speech activity                                                      */
144     opus_int                         allow_bandwidth_switch;         /* Flag indicating that switching of internal bandwidth is allowed      */
145     opus_int8                        LBRRprevLastGainIndex;
146     opus_int8                        prevSignalType;
147     opus_int                         prevLag;
148     opus_int                         pitch_LPC_win_length;
149     opus_int                         max_pitch_lag;                  /* Highest possible pitch lag (samples)                                 */
150     opus_int32                       API_fs_Hz;                      /* API sampling frequency (Hz)                                          */
151     opus_int32                       prev_API_fs_Hz;                 /* Previous API sampling frequency (Hz)                                 */
152     opus_int                         maxInternal_fs_Hz;              /* Maximum internal sampling frequency (Hz)                             */
153     opus_int                         minInternal_fs_Hz;              /* Minimum internal sampling frequency (Hz)                             */
154     opus_int                         desiredInternal_fs_Hz;          /* Soft request for internal sampling frequency (Hz)                    */
155     opus_int                         fs_kHz;                         /* Internal sampling frequency (kHz)                                    */
156     opus_int                         nb_subfr;                       /* Number of 5 ms subframes in a frame                                  */
157     opus_int                         frame_length;                   /* Frame length (samples)                                               */
158     opus_int                         subfr_length;                   /* Subframe length (samples)                                            */
159     opus_int                         ltp_mem_length;                 /* Length of LTP memory                                                 */
160     opus_int                         la_pitch;                       /* Look-ahead for pitch analysis (samples)                              */
161     opus_int                         la_shape;                       /* Look-ahead for noise shape analysis (samples)                        */
162     opus_int                         shapeWinLength;                 /* Window length for noise shape analysis (samples)                     */
163     opus_int32                       TargetRate_bps;                 /* Target bitrate (bps)                                                 */
164     opus_int                         PacketSize_ms;                  /* Number of milliseconds to put in each packet                         */
165     opus_int                         PacketLoss_perc;                /* Packet loss rate measured by farend                                  */
166     opus_int32                       frameCounter;
167     opus_int                         Complexity;                     /* Complexity setting                                                   */
168     opus_int                         nStatesDelayedDecision;         /* Number of states in delayed decision quantization                    */
169     opus_int                         useInterpolatedNLSFs;           /* Flag for using NLSF interpolation                                    */
170     opus_int                         shapingLPCOrder;                /* Filter order for noise shaping filters                               */
171     opus_int                         predictLPCOrder;                /* Filter order for prediction filters                                  */
172     opus_int                         pitchEstimationComplexity;      /* Complexity level for pitch estimator                                 */
173     opus_int                         pitchEstimationLPCOrder;        /* Whitening filter order for pitch estimator                           */
174     opus_int32                       pitchEstimationThreshold_Q16;   /* Threshold for pitch estimator                                        */
175     opus_int                         LTPQuantLowComplexity;          /* Flag for low complexity LTP quantization                             */
176     opus_int                         mu_LTP_Q9;                      /* Rate-distortion tradeoff in LTP quantization                         */
177     opus_int                         NLSF_MSVQ_Survivors;            /* Number of survivors in NLSF MSVQ                                     */
178     opus_int                         first_frame_after_reset;        /* Flag for deactivating NLSF interp. and fluc. reduction after resets  */
179     opus_int                         controlled_since_last_payload;  /* Flag for ensuring codec_control only runs once per packet            */
180     opus_int                         warping_Q16;                    /* Warping parameter for warped noise shaping                           */
181     opus_int                         useCBR;                         /* Flag to enable constant bitrate                                      */
182     opus_int                         prefillFlag;                    /* Flag to indicate that only buffers are prefilled, no coding          */
183     const opus_uint8                 *pitch_lag_low_bits_iCDF;       /* Pointer to iCDF table for low bits of pitch lag index                */
184     const opus_uint8                 *pitch_contour_iCDF;            /* Pointer to iCDF table for pitch contour index                        */
185     const silk_NLSF_CB_struct       *psNLSF_CB;                     /* Pointer to NLSF codebook                                             */
186     opus_int                         input_quality_bands_Q15[ VAD_N_BANDS ];
187     opus_int                         input_tilt_Q15;
188     opus_int                         SNR_dB_Q7;                      /* Quality setting                                                      */
189
190     opus_int8                        VAD_flags[ MAX_FRAMES_PER_PACKET ];
191     opus_int8                        LBRR_flag;
192     opus_int                         LBRR_flags[ MAX_FRAMES_PER_PACKET ];
193
194     SideInfoIndices                 indices;
195     opus_int8                        pulses[ MAX_FRAME_LENGTH ];
196
197     /* Input/output buffering */
198     opus_int16                       inputBuf__[ MAX_FRAME_LENGTH + 2 ]; /* Buffer containing input signal                                   */
199     opus_int16                       *inputBuf;                      /* Points to second element of above buffer                             */
200     opus_int                         inputBufIx;
201     opus_int                         nFramesPerPacket;
202     opus_int                         nFramesEncoded;                 /* Number of frames analyzed in current packet                          */
203
204     opus_int                         nChannelsAPI;
205     opus_int                         nChannelsInternal;
206     opus_int                         channelNb;
207
208     /* Parameters For LTP scaling Control */
209     opus_int                         frames_since_onset;
210
211     /* Specifically for entropy coding */
212     opus_int                         ec_prevSignalType;
213     opus_int16                       ec_prevLagIndex;
214
215     silk_resampler_state_struct resampler_state;
216
217     /* DTX */
218     opus_int                         useDTX;                         /* Flag to enable DTX                                                   */
219     opus_int                         inDTX;                          /* Flag to signal DTX period                                            */
220     opus_int                         noSpeechCounter;                /* Counts concecutive nonactive frames, used by DTX                     */
221
222     /* Inband Low Bitrate Redundancy (LBRR) data */
223     opus_int                         useInBandFEC;                   /* Saves the API setting for query                                      */
224     opus_int                         LBRR_enabled;                   /* Depends on useInBandFRC, bitrate and packet loss rate                */
225     opus_int                         LBRR_GainIncreases;             /* Gains increment for coding LBRR frames                               */
226     SideInfoIndices                 indices_LBRR[ MAX_FRAMES_PER_PACKET ];
227     opus_int8                        pulses_LBRR[ MAX_FRAMES_PER_PACKET ][ MAX_FRAME_LENGTH ];
228 } silk_encoder_state;
229
230
231 /* Struct for Packet Loss Concealment */
232 typedef struct {
233     opus_int32   pitchL_Q8;                      /* Pitch lag to use for voiced concealment                  */
234     opus_int16   LTPCoef_Q14[ LTP_ORDER ];       /* LTP coeficients to use for voiced concealment            */
235     opus_int16   prevLPC_Q12[ MAX_LPC_ORDER ];
236     opus_int     last_frame_lost;                /* Was previous frame lost                                  */
237     opus_int32   rand_seed;                      /* Seed for unvoiced signal generation                      */
238     opus_int16   randScale_Q14;                  /* Scaling of unvoiced random signal                        */
239     opus_int32   conc_energy;
240     opus_int     conc_energy_shift;
241     opus_int16   prevLTP_scale_Q14;
242     opus_int32   prevGain_Q16[ MAX_NB_SUBFR ];
243     opus_int     fs_kHz;
244 } silk_PLC_struct;
245
246 /* Struct for CNG */
247 typedef struct {
248     opus_int32   CNG_exc_buf_Q10[ MAX_FRAME_LENGTH ];
249     opus_int16   CNG_smth_NLSF_Q15[ MAX_LPC_ORDER ];
250     opus_int32   CNG_synth_state[ MAX_LPC_ORDER ];
251     opus_int32   CNG_smth_Gain_Q16;
252     opus_int32   rand_seed;
253     opus_int     fs_kHz;
254 } silk_CNG_struct;
255
256 /********************************/
257 /* Decoder state                */
258 /********************************/
259 typedef struct {
260     opus_int32       prev_inv_gain_Q16;
261     opus_int32       sLTP_Q16[ 2 * MAX_FRAME_LENGTH ];
262     opus_int32       sLPC_Q14[ MAX_SUB_FRAME_LENGTH + MAX_LPC_ORDER ];
263     opus_int32       exc_Q10[ MAX_FRAME_LENGTH ];
264     opus_int16       outBuf[ 2 * MAX_FRAME_LENGTH ];             /* Buffer for output signal                                             */
265     opus_int         lagPrev;                                    /* Previous Lag                                                         */
266     opus_int8        LastGainIndex;                              /* Previous gain index                                                  */
267     opus_int         fs_kHz;                                     /* Sampling frequency in kHz                                            */
268     opus_int32       prev_API_sampleRate;                        /* Previous API sample frequency (Hz)                                   */
269     opus_int         nb_subfr;                                   /* Number of 5 ms subframes in a frame                                  */
270     opus_int         frame_length;                               /* Frame length (samples)                                               */
271     opus_int         subfr_length;                               /* Subframe length (samples)                                            */
272     opus_int         ltp_mem_length;                             /* Length of LTP memory                                                 */
273     opus_int         LPC_order;                                  /* LPC order                                                            */
274     opus_int16       prevNLSF_Q15[ MAX_LPC_ORDER ];              /* Used to interpolate LSFs                                             */
275     opus_int         first_frame_after_reset;                    /* Flag for deactivating NLSF interp. and fluc. reduction after resets  */
276     const opus_uint8 *pitch_lag_low_bits_iCDF;                   /* Pointer to iCDF table for low bits of pitch lag index                */
277     const opus_uint8 *pitch_contour_iCDF;                        /* Pointer to iCDF table for pitch contour index                        */
278
279     /* For buffering payload in case of more frames per packet */
280     opus_int         nFramesDecoded;
281     opus_int         nFramesPerPacket;
282
283     /* Specifically for entropy coding */
284     opus_int         ec_prevSignalType;
285     opus_int16       ec_prevLagIndex;
286
287     opus_int         VAD_flags[ MAX_FRAMES_PER_PACKET ];
288     opus_int         LBRR_flag;
289     opus_int         LBRR_flags[ MAX_FRAMES_PER_PACKET ];
290
291     silk_resampler_state_struct resampler_state;
292
293     const silk_NLSF_CB_struct   *psNLSF_CB;                 /* Pointer to NLSF codebook                                             */
294
295     /* Quantization indices */
296     SideInfoIndices indices;
297
298     /* CNG state */
299     silk_CNG_struct sCNG;
300
301     /* Stuff used for PLC */
302     opus_int         lossCnt;
303     opus_int         prevSignalType;
304
305     silk_PLC_struct sPLC;
306
307 } silk_decoder_state;
308
309 /************************/
310 /* Decoder control      */
311 /************************/
312 typedef struct {
313     /* prediction and coding parameters */
314     opus_int             pitchL[ MAX_NB_SUBFR ];
315     opus_int32           Gains_Q16[ MAX_NB_SUBFR ];
316     /* holds interpolated and final coefficients, 4-byte aligned */
317     SKP_DWORD_ALIGN opus_int16 PredCoef_Q12[ 2 ][ MAX_LPC_ORDER ];
318     opus_int16           LTPCoef_Q14[ LTP_ORDER * MAX_NB_SUBFR ];
319     opus_int             LTP_scale_Q14;
320 } silk_decoder_control;
321
322
323 #ifdef __cplusplus
324 }
325 #endif
326
327 #endif