structs.h 21 KB

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