blob: 6bada59b91a942e4c0a0b5a04dbaa9ae8d447a4e [file] [log] [blame]
Gregory Maxwellae231142011-07-30 08:18:48 -04001/***********************************************************************
2Copyright (c) 2006-2011, Skype Limited. All rights reserved.
3Redistribution and use in source and binary forms, with or without
4modification, (subject to the limitations in the disclaimer below)
5are permitted provided that the following conditions are met:
6- Redistributions of source code must retain the above copyright notice,
7this list of conditions and the following disclaimer.
8- Redistributions in binary form must reproduce the above copyright
9notice, this list of conditions and the following disclaimer in the
10documentation and/or other materials provided with the distribution.
11- Neither the name of Skype Limited, nor the names of specific
12contributors, may be used to endorse or promote products derived from
13this software without specific prior written permission.
14NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
15BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
16CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
17BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
18FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
22USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26***********************************************************************/
27
28#ifndef SILK_STRUCTS_H
29#define SILK_STRUCTS_H
30
Jean-Marc Valin1c2f5632011-09-16 01:16:53 -070031#include "typedef.h"
32#include "SigProc_FIX.h"
33#include "define.h"
Gregory Maxwellae231142011-07-30 08:18:48 -040034#include "entenc.h"
35#include "entdec.h"
36
37#ifdef __cplusplus
38extern "C"
39{
40#endif
41
42/************************************/
43/* Noise shaping quantization state */
44/************************************/
45typedef struct {
Koen Vosacc7a6c2011-10-28 19:44:26 -040046 opus_int16 xq[ 2 * MAX_FRAME_LENGTH ]; /* Buffer for quantized output signal */
Koen Vosa51ebd62011-12-14 11:39:29 -050047 opus_int32 sLTP_shp_Q14[ 2 * MAX_FRAME_LENGTH ];
Koen Vosacc7a6c2011-10-28 19:44:26 -040048 opus_int32 sLPC_Q14[ MAX_SUB_FRAME_LENGTH + NSQ_LPC_BUF_LENGTH ];
49 opus_int32 sAR2_Q14[ MAX_SHAPE_LPC_ORDER ];
Koen Vosa51ebd62011-12-14 11:39:29 -050050 opus_int32 sLF_AR_shp_Q14;
Koen Vosacc7a6c2011-10-28 19:44:26 -040051 opus_int lagPrev;
52 opus_int sLTP_buf_idx;
53 opus_int sLTP_shp_buf_idx;
54 opus_int32 rand_seed;
Koen Vosa51ebd62011-12-14 11:39:29 -050055 opus_int32 prev_gain_Q16;
Koen Vosacc7a6c2011-10-28 19:44:26 -040056 opus_int rewhite_flag;
Gregory Maxwellae231142011-07-30 08:18:48 -040057} silk_nsq_state;
58
59/********************************/
60/* VAD state */
61/********************************/
62typedef struct {
Koen Vosacc7a6c2011-10-28 19:44:26 -040063 opus_int32 AnaState[ 2 ]; /* Analysis filterbank state: 0-8 kHz */
64 opus_int32 AnaState1[ 2 ]; /* Analysis filterbank state: 0-4 kHz */
65 opus_int32 AnaState2[ 2 ]; /* Analysis filterbank state: 0-2 kHz */
66 opus_int32 XnrgSubfr[ VAD_N_BANDS ]; /* Subframe energies */
67 opus_int32 NrgRatioSmth_Q8[ VAD_N_BANDS ]; /* Smoothed energy level in each band */
68 opus_int16 HPstate; /* State of differentiator in the lowest band */
69 opus_int32 NL[ VAD_N_BANDS ]; /* Noise energy level in each band */
70 opus_int32 inv_NL[ VAD_N_BANDS ]; /* Inverse noise energy level in each band */
71 opus_int32 NoiseLevelBias[ VAD_N_BANDS ]; /* Noise level estimator bias/offset */
72 opus_int32 counter; /* Frame counter used in the initial phase */
Gregory Maxwellae231142011-07-30 08:18:48 -040073} silk_VAD_state;
74
75/* Variable cut-off low-pass filter state */
76typedef struct {
77 opus_int32 In_LP_State[ 2 ]; /* Low pass filter state */
78 opus_int32 transition_frame_no; /* Counter which is mapped to a cut-off frequency */
Koen Vosacc7a6c2011-10-28 19:44:26 -040079 opus_int mode; /* Operating mode, <0: switch down, >0: switch up; 0: do nothing */
Gregory Maxwellae231142011-07-30 08:18:48 -040080} silk_LP_state;
81
82/* Structure containing NLSF codebook */
83typedef struct {
84 const opus_int16 nVectors;
85 const opus_int16 order;
86 const opus_int16 quantStepSize_Q16;
87 const opus_int16 invQuantStepSize_Q6;
88 const opus_uint8 *CB1_NLSF_Q8;
89 const opus_uint8 *CB1_iCDF;
90 const opus_uint8 *pred_Q8;
91 const opus_uint8 *ec_sel;
92 const opus_uint8 *ec_iCDF;
93 const opus_uint8 *ec_Rates_Q5;
94 const opus_int16 *deltaMin_Q15;
95} silk_NLSF_CB_struct;
96
97typedef struct {
98 opus_int16 pred_prev_Q13[ 2 ];
99 opus_int16 sMid[ 2 ];
100 opus_int16 sSide[ 2 ];
101 opus_int32 mid_side_amp_Q0[ 4 ];
102 opus_int16 smth_width_Q14;
103 opus_int16 width_prev_Q14;
Jean-Marc Valinb24e5742011-10-11 21:09:14 -0400104 opus_int16 silent_side_len;
Koen Vos4e1ce382011-08-25 13:50:21 -0400105 opus_int8 predIx[ MAX_FRAMES_PER_PACKET ][ 2 ][ 3 ];
106 opus_int8 mid_only_flags[ MAX_FRAMES_PER_PACKET ];
Gregory Maxwellae231142011-07-30 08:18:48 -0400107} stereo_enc_state;
108
109typedef struct {
110 opus_int16 pred_prev_Q13[ 2 ];
111 opus_int16 sMid[ 2 ];
112 opus_int16 sSide[ 2 ];
113} stereo_dec_state;
114
115typedef struct {
116 opus_int8 GainsIndices[ MAX_NB_SUBFR ];
117 opus_int8 LTPIndex[ MAX_NB_SUBFR ];
118 opus_int8 NLSFIndices[ MAX_LPC_ORDER + 1 ];
119 opus_int16 lagIndex;
120 opus_int8 contourIndex;
121 opus_int8 signalType;
122 opus_int8 quantOffsetType;
123 opus_int8 NLSFInterpCoef_Q2;
124 opus_int8 PERIndex;
125 opus_int8 LTP_scaleIndex;
126 opus_int8 Seed;
127} SideInfoIndices;
128
129/********************************/
130/* Encoder state */
131/********************************/
132typedef struct {
Koen Vosacc7a6c2011-10-28 19:44:26 -0400133 opus_int32 In_HP_State[ 2 ]; /* High pass filter state */
134 opus_int32 variable_HP_smth1_Q15; /* State of first smoother */
135 opus_int32 variable_HP_smth2_Q15; /* State of second smoother */
136 silk_LP_state sLP; /* Low pass filter state */
137 silk_VAD_state sVAD; /* Voice activity detector state */
138 silk_nsq_state sNSQ; /* Noise Shape Quantizer State */
139 opus_int16 prev_NLSFq_Q15[ MAX_LPC_ORDER ]; /* Previously quantized NLSF vector */
140 opus_int speech_activity_Q8; /* Speech activity */
141 opus_int allow_bandwidth_switch; /* Flag indicating that switching of internal bandwidth is allowed */
142 opus_int8 LBRRprevLastGainIndex;
143 opus_int8 prevSignalType;
144 opus_int prevLag;
145 opus_int pitch_LPC_win_length;
146 opus_int max_pitch_lag; /* Highest possible pitch lag (samples) */
147 opus_int32 API_fs_Hz; /* API sampling frequency (Hz) */
148 opus_int32 prev_API_fs_Hz; /* Previous API sampling frequency (Hz) */
149 opus_int maxInternal_fs_Hz; /* Maximum internal sampling frequency (Hz) */
150 opus_int minInternal_fs_Hz; /* Minimum internal sampling frequency (Hz) */
151 opus_int desiredInternal_fs_Hz; /* Soft request for internal sampling frequency (Hz) */
152 opus_int fs_kHz; /* Internal sampling frequency (kHz) */
Koen Vosacc7a6c2011-10-28 19:44:26 -0400153 opus_int nb_subfr; /* Number of 5 ms subframes in a frame */
154 opus_int frame_length; /* Frame length (samples) */
155 opus_int subfr_length; /* Subframe length (samples) */
156 opus_int ltp_mem_length; /* Length of LTP memory */
157 opus_int la_pitch; /* Look-ahead for pitch analysis (samples) */
158 opus_int la_shape; /* Look-ahead for noise shape analysis (samples) */
159 opus_int shapeWinLength; /* Window length for noise shape analysis (samples) */
160 opus_int32 TargetRate_bps; /* Target bitrate (bps) */
161 opus_int PacketSize_ms; /* Number of milliseconds to put in each packet */
162 opus_int PacketLoss_perc; /* Packet loss rate measured by farend */
163 opus_int32 frameCounter;
164 opus_int Complexity; /* Complexity setting */
165 opus_int nStatesDelayedDecision; /* Number of states in delayed decision quantization */
166 opus_int useInterpolatedNLSFs; /* Flag for using NLSF interpolation */
167 opus_int shapingLPCOrder; /* Filter order for noise shaping filters */
168 opus_int predictLPCOrder; /* Filter order for prediction filters */
169 opus_int pitchEstimationComplexity; /* Complexity level for pitch estimator */
170 opus_int pitchEstimationLPCOrder; /* Whitening filter order for pitch estimator */
171 opus_int32 pitchEstimationThreshold_Q16; /* Threshold for pitch estimator */
172 opus_int LTPQuantLowComplexity; /* Flag for low complexity LTP quantization */
173 opus_int mu_LTP_Q9; /* Rate-distortion tradeoff in LTP quantization */
174 opus_int NLSF_MSVQ_Survivors; /* Number of survivors in NLSF MSVQ */
175 opus_int first_frame_after_reset; /* Flag for deactivating NLSF interpolation, pitch prediction */
176 opus_int controlled_since_last_payload; /* Flag for ensuring codec_control only runs once per packet */
177 opus_int warping_Q16; /* Warping parameter for warped noise shaping */
178 opus_int useCBR; /* Flag to enable constant bitrate */
179 opus_int prefillFlag; /* Flag to indicate that only buffers are prefilled, no coding */
180 const opus_uint8 *pitch_lag_low_bits_iCDF; /* Pointer to iCDF table for low bits of pitch lag index */
181 const opus_uint8 *pitch_contour_iCDF; /* Pointer to iCDF table for pitch contour index */
182 const silk_NLSF_CB_struct *psNLSF_CB; /* Pointer to NLSF codebook */
183 opus_int input_quality_bands_Q15[ VAD_N_BANDS ];
184 opus_int input_tilt_Q15;
185 opus_int SNR_dB_Q7; /* Quality setting */
Gregory Maxwellae231142011-07-30 08:18:48 -0400186
Koen Vosacc7a6c2011-10-28 19:44:26 -0400187 opus_int8 VAD_flags[ MAX_FRAMES_PER_PACKET ];
188 opus_int8 LBRR_flag;
189 opus_int LBRR_flags[ MAX_FRAMES_PER_PACKET ];
Gregory Maxwellae231142011-07-30 08:18:48 -0400190
Koen Vosacc7a6c2011-10-28 19:44:26 -0400191 SideInfoIndices indices;
192 opus_int8 pulses[ MAX_FRAME_LENGTH ];
Gregory Maxwellae231142011-07-30 08:18:48 -0400193
194 /* Input/output buffering */
Koen Vosacc7a6c2011-10-28 19:44:26 -0400195 opus_int16 inputBuf[ MAX_FRAME_LENGTH + 2 ]; /* Buffer containing input signal */
Koen Vosacc7a6c2011-10-28 19:44:26 -0400196 opus_int inputBufIx;
197 opus_int nFramesPerPacket;
198 opus_int nFramesEncoded; /* Number of frames analyzed in current packet */
Gregory Maxwellae231142011-07-30 08:18:48 -0400199
Koen Vosacc7a6c2011-10-28 19:44:26 -0400200 opus_int nChannelsAPI;
201 opus_int nChannelsInternal;
202 opus_int channelNb;
Gregory Maxwellae231142011-07-30 08:18:48 -0400203
204 /* Parameters For LTP scaling Control */
Koen Vosacc7a6c2011-10-28 19:44:26 -0400205 opus_int frames_since_onset;
Gregory Maxwellae231142011-07-30 08:18:48 -0400206
207 /* Specifically for entropy coding */
Koen Vosacc7a6c2011-10-28 19:44:26 -0400208 opus_int ec_prevSignalType;
209 opus_int16 ec_prevLagIndex;
Gregory Maxwellae231142011-07-30 08:18:48 -0400210
211 silk_resampler_state_struct resampler_state;
212
213 /* DTX */
Koen Vosacc7a6c2011-10-28 19:44:26 -0400214 opus_int useDTX; /* Flag to enable DTX */
215 opus_int inDTX; /* Flag to signal DTX period */
216 opus_int noSpeechCounter; /* Counts concecutive nonactive frames, used by DTX */
Gregory Maxwellae231142011-07-30 08:18:48 -0400217
218 /* Inband Low Bitrate Redundancy (LBRR) data */
Koen Vosacc7a6c2011-10-28 19:44:26 -0400219 opus_int useInBandFEC; /* Saves the API setting for query */
220 opus_int LBRR_enabled; /* Depends on useInBandFRC, bitrate and packet loss rate */
221 opus_int LBRR_GainIncreases; /* Gains increment for coding LBRR frames */
222 SideInfoIndices indices_LBRR[ MAX_FRAMES_PER_PACKET ];
223 opus_int8 pulses_LBRR[ MAX_FRAMES_PER_PACKET ][ MAX_FRAME_LENGTH ];
Gregory Maxwellae231142011-07-30 08:18:48 -0400224} silk_encoder_state;
225
226
227/* Struct for Packet Loss Concealment */
228typedef struct {
Koen Vosacc7a6c2011-10-28 19:44:26 -0400229 opus_int32 pitchL_Q8; /* Pitch lag to use for voiced concealment */
230 opus_int16 LTPCoef_Q14[ LTP_ORDER ]; /* LTP coeficients to use for voiced concealment */
231 opus_int16 prevLPC_Q12[ MAX_LPC_ORDER ];
232 opus_int last_frame_lost; /* Was previous frame lost */
233 opus_int32 rand_seed; /* Seed for unvoiced signal generation */
234 opus_int16 randScale_Q14; /* Scaling of unvoiced random signal */
235 opus_int32 conc_energy;
236 opus_int conc_energy_shift;
237 opus_int16 prevLTP_scale_Q14;
Koen Vos0dbd0ca2011-10-28 21:05:22 -0400238 opus_int32 prevGain_Q16[ 2 ];
Koen Vosacc7a6c2011-10-28 19:44:26 -0400239 opus_int fs_kHz;
Koen Vos0dbd0ca2011-10-28 21:05:22 -0400240 opus_int nb_subfr;
241 opus_int subfr_length;
Gregory Maxwellae231142011-07-30 08:18:48 -0400242} silk_PLC_struct;
243
244/* Struct for CNG */
245typedef struct {
Koen Vosa51ebd62011-12-14 11:39:29 -0500246 opus_int32 CNG_exc_buf_Q14[ MAX_FRAME_LENGTH ];
Koen Vosacc7a6c2011-10-28 19:44:26 -0400247 opus_int16 CNG_smth_NLSF_Q15[ MAX_LPC_ORDER ];
248 opus_int32 CNG_synth_state[ MAX_LPC_ORDER ];
249 opus_int32 CNG_smth_Gain_Q16;
250 opus_int32 rand_seed;
251 opus_int fs_kHz;
Gregory Maxwellae231142011-07-30 08:18:48 -0400252} silk_CNG_struct;
253
254/********************************/
255/* Decoder state */
256/********************************/
257typedef struct {
Koen Vosa51ebd62011-12-14 11:39:29 -0500258 opus_int32 prev_gain_Q16;
259 opus_int32 exc_Q14[ MAX_FRAME_LENGTH ];
Koen Vosacc7a6c2011-10-28 19:44:26 -0400260 opus_int32 sLPC_Q14_buf[ MAX_LPC_ORDER ];
261 opus_int16 outBuf[ MAX_FRAME_LENGTH + 2 * MAX_SUB_FRAME_LENGTH ]; /* Buffer for output signal */
Koen Vosacc7a6c2011-10-28 19:44:26 -0400262 opus_int lagPrev; /* Previous Lag */
263 opus_int8 LastGainIndex; /* Previous gain index */
264 opus_int fs_kHz; /* Sampling frequency in kHz */
265 opus_int32 fs_API_hz; /* API sample frequency (Hz) */
266 opus_int nb_subfr; /* Number of 5 ms subframes in a frame */
267 opus_int frame_length; /* Frame length (samples) */
268 opus_int subfr_length; /* Subframe length (samples) */
269 opus_int ltp_mem_length; /* Length of LTP memory */
270 opus_int LPC_order; /* LPC order */
271 opus_int16 prevNLSF_Q15[ MAX_LPC_ORDER ]; /* Used to interpolate LSFs */
272 opus_int first_frame_after_reset; /* Flag for deactivating NLSF interpolation */
273 const opus_uint8 *pitch_lag_low_bits_iCDF; /* Pointer to iCDF table for low bits of pitch lag index */
274 const opus_uint8 *pitch_contour_iCDF; /* Pointer to iCDF table for pitch contour index */
Gregory Maxwellae231142011-07-30 08:18:48 -0400275
276 /* For buffering payload in case of more frames per packet */
Koen Vosacc7a6c2011-10-28 19:44:26 -0400277 opus_int nFramesDecoded;
278 opus_int nFramesPerPacket;
Gregory Maxwellae231142011-07-30 08:18:48 -0400279
280 /* Specifically for entropy coding */
Koen Vosacc7a6c2011-10-28 19:44:26 -0400281 opus_int ec_prevSignalType;
282 opus_int16 ec_prevLagIndex;
Gregory Maxwellae231142011-07-30 08:18:48 -0400283
Koen Vosacc7a6c2011-10-28 19:44:26 -0400284 opus_int VAD_flags[ MAX_FRAMES_PER_PACKET ];
285 opus_int LBRR_flag;
286 opus_int LBRR_flags[ MAX_FRAMES_PER_PACKET ];
Gregory Maxwellae231142011-07-30 08:18:48 -0400287
288 silk_resampler_state_struct resampler_state;
289
Koen Vosacc7a6c2011-10-28 19:44:26 -0400290 const silk_NLSF_CB_struct *psNLSF_CB; /* Pointer to NLSF codebook */
Gregory Maxwellae231142011-07-30 08:18:48 -0400291
292 /* Quantization indices */
Koen Vosacc7a6c2011-10-28 19:44:26 -0400293 SideInfoIndices indices;
Gregory Maxwellae231142011-07-30 08:18:48 -0400294
295 /* CNG state */
Koen Vosacc7a6c2011-10-28 19:44:26 -0400296 silk_CNG_struct sCNG;
Gregory Maxwellae231142011-07-30 08:18:48 -0400297
298 /* Stuff used for PLC */
Koen Vosacc7a6c2011-10-28 19:44:26 -0400299 opus_int lossCnt;
300 opus_int prevSignalType;
Gregory Maxwellae231142011-07-30 08:18:48 -0400301
302 silk_PLC_struct sPLC;
303
304} silk_decoder_state;
305
306/************************/
307/* Decoder control */
308/************************/
309typedef struct {
Koen Vosacc7a6c2011-10-28 19:44:26 -0400310 /* Prediction and coding parameters */
311 opus_int pitchL[ MAX_NB_SUBFR ];
312 opus_int32 Gains_Q16[ MAX_NB_SUBFR ];
313 /* Holds interpolated and final coefficients, 4-byte aligned */
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700314 silk_DWORD_ALIGN opus_int16 PredCoef_Q12[ 2 ][ MAX_LPC_ORDER ];
Koen Vosacc7a6c2011-10-28 19:44:26 -0400315 opus_int16 LTPCoef_Q14[ LTP_ORDER * MAX_NB_SUBFR ];
316 opus_int LTP_scale_Q14;
Gregory Maxwellae231142011-07-30 08:18:48 -0400317} silk_decoder_control;
318
319
320#ifdef __cplusplus
321}
322#endif
323
324#endif