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 28 #ifndef SILK_MAIN_H 29 #define SILK_MAIN_H 30 31 #include "SigProc_FIX.h" 32 #include "define.h" 33 #include "structs.h" 34 #include "tables.h" 35 #include "PLC.h" 36 #include "control.h" 37 #include "debug.h" 38 #include "entenc.h" 39 #include "entdec.h" 40 41 /* Convert Left/Right stereo signal to adaptive Mid/Side representation */ 42 void silk_stereo_LR_to_MS( 43 stereo_enc_state *state, /* I/O State */ 44 opus_int16 x1[], /* I/O Left input signal, becomes mid signal */ 45 opus_int16 x2[], /* I/O Right input signal, becomes side signal */ 46 opus_int8 ix[ 2 ][ 3 ], /* O Quantization indices */ 47 opus_int8 *mid_only_flag, /* O Flag: only mid signal coded */ 48 opus_int32 mid_side_rates_bps[], /* O Bitrates for mid and side signals */ 49 opus_int32 total_rate_bps, /* I Total bitrate */ 50 opus_int prev_speech_act_Q8, /* I Speech activity level in previous frame */ 51 opus_int toMono, /* I Last frame before a stereo->mono transition */ 52 opus_int fs_kHz, /* I Sample rate (kHz) */ 53 opus_int frame_length /* I Number of samples */ 54 ); 55 56 /* Convert adaptive Mid/Side representation to Left/Right stereo signal */ 57 void silk_stereo_MS_to_LR( 58 stereo_dec_state *state, /* I/O State */ 59 opus_int16 x1[], /* I/O Left input signal, becomes mid signal */ 60 opus_int16 x2[], /* I/O Right input signal, becomes side signal */ 61 const opus_int32 pred_Q13[], /* I Predictors */ 62 opus_int fs_kHz, /* I Samples rate (kHz) */ 63 opus_int frame_length /* I Number of samples */ 64 ); 65 66 /* Find least-squares prediction gain for one signal based on another and quantize it */ 67 opus_int32 silk_stereo_find_predictor( /* O Returns predictor in Q13 */ 68 opus_int32 *ratio_Q14, /* O Ratio of residual and mid energies */ 69 const opus_int16 x[], /* I Basis signal */ 70 const opus_int16 y[], /* I Target signal */ 71 opus_int32 mid_res_amp_Q0[], /* I/O Smoothed mid, residual norms */ 72 opus_int length, /* I Number of samples */ 73 opus_int smooth_coef_Q16 /* I Smoothing coefficient */ 74 ); 75 76 /* Quantize mid/side predictors */ 77 void silk_stereo_quant_pred( 78 opus_int32 pred_Q13[], /* I/O Predictors (out: quantized) */ 79 opus_int8 ix[ 2 ][ 3 ] /* O Quantization indices */ 80 ); 81 82 /* Entropy code the mid/side quantization indices */ 83 void silk_stereo_encode_pred( 84 ec_enc *psRangeEnc, /* I/O Compressor data structure */ 85 opus_int8 ix[ 2 ][ 3 ] /* I Quantization indices */ 86 ); 87 88 /* Entropy code the mid-only flag */ 89 void silk_stereo_encode_mid_only( 90 ec_enc *psRangeEnc, /* I/O Compressor data structure */ 91 opus_int8 mid_only_flag 92 ); 93 94 /* Decode mid/side predictors */ 95 void silk_stereo_decode_pred( 96 ec_dec *psRangeDec, /* I/O Compressor data structure */ 97 opus_int32 pred_Q13[] /* O Predictors */ 98 ); 99 100 /* Decode mid-only flag */ 101 void silk_stereo_decode_mid_only( 102 ec_dec *psRangeDec, /* I/O Compressor data structure */ 103 opus_int *decode_only_mid /* O Flag that only mid channel has been coded */ 104 ); 105 106 /* Encodes signs of excitation */ 107 void silk_encode_signs( 108 ec_enc *psRangeEnc, /* I/O Compressor data structure */ 109 const opus_int8 pulses[], /* I pulse signal */ 110 opus_int length, /* I length of input */ 111 const opus_int signalType, /* I Signal type */ 112 const opus_int quantOffsetType, /* I Quantization offset type */ 113 const opus_int sum_pulses[ MAX_NB_SHELL_BLOCKS ] /* I Sum of absolute pulses per block */ 114 ); 115 116 /* Decodes signs of excitation */ 117 void silk_decode_signs( 118 ec_dec *psRangeDec, /* I/O Compressor data structure */ 119 opus_int pulses[], /* I/O pulse signal */ 120 opus_int length, /* I length of input */ 121 const opus_int signalType, /* I Signal type */ 122 const opus_int quantOffsetType, /* I Quantization offset type */ 123 const opus_int sum_pulses[ MAX_NB_SHELL_BLOCKS ] /* I Sum of absolute pulses per block */ 124 ); 125 126 /* Check encoder control struct */ 127 opus_int check_control_input( 128 silk_EncControlStruct *encControl /* I Control structure */ 129 ); 130 131 /* Control internal sampling rate */ 132 opus_int silk_control_audio_bandwidth( 133 silk_encoder_state *psEncC, /* I/O Pointer to Silk encoder state */ 134 silk_EncControlStruct *encControl /* I Control structure */ 135 ); 136 137 /* Control SNR of redidual quantizer */ 138 opus_int silk_control_SNR( 139 silk_encoder_state *psEncC, /* I/O Pointer to Silk encoder state */ 140 opus_int32 TargetRate_bps /* I Target max bitrate (bps) */ 141 ); 142 143 /***************/ 144 /* Shell coder */ 145 /***************/ 146 147 /* Encode quantization indices of excitation */ 148 void silk_encode_pulses( 149 ec_enc *psRangeEnc, /* I/O compressor data structure */ 150 const opus_int signalType, /* I Signal type */ 151 const opus_int quantOffsetType, /* I quantOffsetType */ 152 opus_int8 pulses[], /* I quantization indices */ 153 const opus_int frame_length /* I Frame length */ 154 ); 155 156 /* Shell encoder, operates on one shell code frame of 16 pulses */ 157 void silk_shell_encoder( 158 ec_enc *psRangeEnc, /* I/O compressor data structure */ 159 const opus_int *pulses0 /* I data: nonnegative pulse amplitudes */ 160 ); 161 162 /* Shell decoder, operates on one shell code frame of 16 pulses */ 163 void silk_shell_decoder( 164 opus_int *pulses0, /* O data: nonnegative pulse amplitudes */ 165 ec_dec *psRangeDec, /* I/O Compressor data structure */ 166 const opus_int pulses4 /* I number of pulses per pulse-subframe */ 167 ); 168 169 /* Gain scalar quantization with hysteresis, uniform on log scale */ 170 void silk_gains_quant( 171 opus_int8 ind[ MAX_NB_SUBFR ], /* O gain indices */ 172 opus_int32 gain_Q16[ MAX_NB_SUBFR ], /* I/O gains (quantized out) */ 173 opus_int8 *prev_ind, /* I/O last index in previous frame */ 174 const opus_int conditional, /* I first gain is delta coded if 1 */ 175 const opus_int nb_subfr /* I number of subframes */ 176 ); 177 178 /* Gains scalar dequantization, uniform on log scale */ 179 void silk_gains_dequant( 180 opus_int32 gain_Q16[ MAX_NB_SUBFR ], /* O quantized gains */ 181 const opus_int8 ind[ MAX_NB_SUBFR ], /* I gain indices */ 182 opus_int8 *prev_ind, /* I/O last index in previous frame */ 183 const opus_int conditional, /* I first gain is delta coded if 1 */ 184 const opus_int nb_subfr /* I number of subframes */ 185 ); 186 187 /* Compute unique identifier of gain indices vector */ 188 opus_int32 silk_gains_ID( /* O returns unique identifier of gains */ 189 const opus_int8 ind[ MAX_NB_SUBFR ], /* I gain indices */ 190 const opus_int nb_subfr /* I number of subframes */ 191 ); 192 193 /* Interpolate two vectors */ 194 void silk_interpolate( 195 opus_int16 xi[ MAX_LPC_ORDER ], /* O interpolated vector */ 196 const opus_int16 x0[ MAX_LPC_ORDER ], /* I first vector */ 197 const opus_int16 x1[ MAX_LPC_ORDER ], /* I second vector */ 198 const opus_int ifact_Q2, /* I interp. factor, weight on 2nd vector */ 199 const opus_int d /* I number of parameters */ 200 ); 201 202 /* LTP tap quantizer */ 203 void silk_quant_LTP_gains( 204 opus_int16 B_Q14[ MAX_NB_SUBFR * LTP_ORDER ], /* I/O (un)quantized LTP gains */ 205 opus_int8 cbk_index[ MAX_NB_SUBFR ], /* O Codebook Index */ 206 opus_int8 *periodicity_index, /* O Periodicity Index */ 207 opus_int32 *sum_gain_dB_Q7, /* I/O Cumulative max prediction gain */ 208 const opus_int32 W_Q18[ MAX_NB_SUBFR*LTP_ORDER*LTP_ORDER ], /* I Error Weights in Q18 */ 209 opus_int mu_Q9, /* I Mu value (R/D tradeoff) */ 210 opus_int lowComplexity, /* I Flag for low complexity */ 211 const opus_int nb_subfr /* I number of subframes */ 212 ); 213 214 /* Entropy constrained matrix-weighted VQ, for a single input data vector */ 215 void silk_VQ_WMat_EC( 216 opus_int8 *ind, /* O index of best codebook vector */ 217 opus_int32 *rate_dist_Q14, /* O best weighted quant error + mu * rate */ 218 opus_int *gain_Q7, /* O sum of absolute LTP coefficients */ 219 const opus_int16 *in_Q14, /* I input vector to be quantized */ 220 const opus_int32 *W_Q18, /* I weighting matrix */ 221 const opus_int8 *cb_Q7, /* I codebook */ 222 const opus_uint8 *cb_gain_Q7, /* I codebook effective gain */ 223 const opus_uint8 *cl_Q5, /* I code length for each codebook vector */ 224 const opus_int mu_Q9, /* I tradeoff betw. weighted error and rate */ 225 const opus_int32 max_gain_Q7, /* I maximum sum of absolute LTP coefficients */ 226 opus_int L /* I number of vectors in codebook */ 227 ); 228 229 /************************************/ 230 /* Noise shaping quantization (NSQ) */ 231 /************************************/ 232 void silk_NSQ( 233 const silk_encoder_state *psEncC, /* I/O Encoder State */ 234 silk_nsq_state *NSQ, /* I/O NSQ state */ 235 SideInfoIndices *psIndices, /* I/O Quantization Indices */ 236 const opus_int32 x_Q3[], /* I Prefiltered input signal */ 237 opus_int8 pulses[], /* O Quantized pulse signal */ 238 const opus_int16 PredCoef_Q12[ 2 * MAX_LPC_ORDER ], /* I Short term prediction coefs */ 239 const opus_int16 LTPCoef_Q14[ LTP_ORDER * MAX_NB_SUBFR ], /* I Long term prediction coefs */ 240 const opus_int16 AR2_Q13[ MAX_NB_SUBFR * MAX_SHAPE_LPC_ORDER ], /* I Noise shaping coefs */ 241 const opus_int HarmShapeGain_Q14[ MAX_NB_SUBFR ], /* I Long term shaping coefs */ 242 const opus_int Tilt_Q14[ MAX_NB_SUBFR ], /* I Spectral tilt */ 243 const opus_int32 LF_shp_Q14[ MAX_NB_SUBFR ], /* I Low frequency shaping coefs */ 244 const opus_int32 Gains_Q16[ MAX_NB_SUBFR ], /* I Quantization step sizes */ 245 const opus_int pitchL[ MAX_NB_SUBFR ], /* I Pitch lags */ 246 const opus_int Lambda_Q10, /* I Rate/distortion tradeoff */ 247 const opus_int LTP_scale_Q14 /* I LTP state scaling */ 248 ); 249 250 /* Noise shaping using delayed decision */ 251 void silk_NSQ_del_dec( 252 const silk_encoder_state *psEncC, /* I/O Encoder State */ 253 silk_nsq_state *NSQ, /* I/O NSQ state */ 254 SideInfoIndices *psIndices, /* I/O Quantization Indices */ 255 const opus_int32 x_Q3[], /* I Prefiltered input signal */ 256 opus_int8 pulses[], /* O Quantized pulse signal */ 257 const opus_int16 PredCoef_Q12[ 2 * MAX_LPC_ORDER ], /* I Short term prediction coefs */ 258 const opus_int16 LTPCoef_Q14[ LTP_ORDER * MAX_NB_SUBFR ], /* I Long term prediction coefs */ 259 const opus_int16 AR2_Q13[ MAX_NB_SUBFR * MAX_SHAPE_LPC_ORDER ], /* I Noise shaping coefs */ 260 const opus_int HarmShapeGain_Q14[ MAX_NB_SUBFR ], /* I Long term shaping coefs */ 261 const opus_int Tilt_Q14[ MAX_NB_SUBFR ], /* I Spectral tilt */ 262 const opus_int32 LF_shp_Q14[ MAX_NB_SUBFR ], /* I Low frequency shaping coefs */ 263 const opus_int32 Gains_Q16[ MAX_NB_SUBFR ], /* I Quantization step sizes */ 264 const opus_int pitchL[ MAX_NB_SUBFR ], /* I Pitch lags */ 265 const opus_int Lambda_Q10, /* I Rate/distortion tradeoff */ 266 const opus_int LTP_scale_Q14 /* I LTP state scaling */ 267 ); 268 269 /************/ 270 /* Silk VAD */ 271 /************/ 272 /* Initialize the Silk VAD */ 273 opus_int silk_VAD_Init( /* O Return value, 0 if success */ 274 silk_VAD_state *psSilk_VAD /* I/O Pointer to Silk VAD state */ 275 ); 276 277 /* Get speech activity level in Q8 */ 278 opus_int silk_VAD_GetSA_Q8( /* O Return value, 0 if success */ 279 silk_encoder_state *psEncC, /* I/O Encoder state */ 280 const opus_int16 pIn[] /* I PCM input */ 281 ); 282 283 /* Low-pass filter with variable cutoff frequency based on */ 284 /* piece-wise linear interpolation between elliptic filters */ 285 /* Start by setting transition_frame_no = 1; */ 286 void silk_LP_variable_cutoff( 287 silk_LP_state *psLP, /* I/O LP filter state */ 288 opus_int16 *frame, /* I/O Low-pass filtered output signal */ 289 const opus_int frame_length /* I Frame length */ 290 ); 291 292 /******************/ 293 /* NLSF Quantizer */ 294 /******************/ 295 /* Limit, stabilize, convert and quantize NLSFs */ 296 void silk_process_NLSFs( 297 silk_encoder_state *psEncC, /* I/O Encoder state */ 298 opus_int16 PredCoef_Q12[ 2 ][ MAX_LPC_ORDER ], /* O Prediction coefficients */ 299 opus_int16 pNLSF_Q15[ MAX_LPC_ORDER ], /* I/O Normalized LSFs (quant out) (0 - (2^15-1)) */ 300 const opus_int16 prev_NLSFq_Q15[ MAX_LPC_ORDER ] /* I Previous Normalized LSFs (0 - (2^15-1)) */ 301 ); 302 303 opus_int32 silk_NLSF_encode( /* O Returns RD value in Q25 */ 304 opus_int8 *NLSFIndices, /* I Codebook path vector [ LPC_ORDER + 1 ] */ 305 opus_int16 *pNLSF_Q15, /* I/O Quantized NLSF vector [ LPC_ORDER ] */ 306 const silk_NLSF_CB_struct *psNLSF_CB, /* I Codebook object */ 307 const opus_int16 *pW_QW, /* I NLSF weight vector [ LPC_ORDER ] */ 308 const opus_int NLSF_mu_Q20, /* I Rate weight for the RD optimization */ 309 const opus_int nSurvivors, /* I Max survivors after first stage */ 310 const opus_int signalType /* I Signal type: 0/1/2 */ 311 ); 312 313 /* Compute quantization errors for an LPC_order element input vector for a VQ codebook */ 314 void silk_NLSF_VQ( 315 opus_int32 err_Q26[], /* O Quantization errors [K] */ 316 const opus_int16 in_Q15[], /* I Input vectors to be quantized [LPC_order] */ 317 const opus_uint8 pCB_Q8[], /* I Codebook vectors [K*LPC_order] */ 318 const opus_int K, /* I Number of codebook vectors */ 319 const opus_int LPC_order /* I Number of LPCs */ 320 ); 321 322 /* Delayed-decision quantizer for NLSF residuals */ 323 opus_int32 silk_NLSF_del_dec_quant( /* O Returns RD value in Q25 */ 324 opus_int8 indices[], /* O Quantization indices [ order ] */ 325 const opus_int16 x_Q10[], /* I Input [ order ] */ 326 const opus_int16 w_Q5[], /* I Weights [ order ] */ 327 const opus_uint8 pred_coef_Q8[], /* I Backward predictor coefs [ order ] */ 328 const opus_int16 ec_ix[], /* I Indices to entropy coding tables [ order ] */ 329 const opus_uint8 ec_rates_Q5[], /* I Rates [] */ 330 const opus_int quant_step_size_Q16, /* I Quantization step size */ 331 const opus_int16 inv_quant_step_size_Q6, /* I Inverse quantization step size */ 332 const opus_int32 mu_Q20, /* I R/D tradeoff */ 333 const opus_int16 order /* I Number of input values */ 334 ); 335 336 /* Unpack predictor values and indices for entropy coding tables */ 337 void silk_NLSF_unpack( 338 opus_int16 ec_ix[], /* O Indices to entropy tables [ LPC_ORDER ] */ 339 opus_uint8 pred_Q8[], /* O LSF predictor [ LPC_ORDER ] */ 340 const silk_NLSF_CB_struct *psNLSF_CB, /* I Codebook object */ 341 const opus_int CB1_index /* I Index of vector in first LSF codebook */ 342 ); 343 344 /***********************/ 345 /* NLSF vector decoder */ 346 /***********************/ 347 void silk_NLSF_decode( 348 opus_int16 *pNLSF_Q15, /* O Quantized NLSF vector [ LPC_ORDER ] */ 349 opus_int8 *NLSFIndices, /* I Codebook path vector [ LPC_ORDER + 1 ] */ 350 const silk_NLSF_CB_struct *psNLSF_CB /* I Codebook object */ 351 ); 352 353 /****************************************************/ 354 /* Decoder Functions */ 355 /****************************************************/ 356 opus_int silk_init_decoder( 357 silk_decoder_state *psDec /* I/O Decoder state pointer */ 358 ); 359 360 /* Set decoder sampling rate */ 361 opus_int silk_decoder_set_fs( 362 silk_decoder_state *psDec, /* I/O Decoder state pointer */ 363 opus_int fs_kHz, /* I Sampling frequency (kHz) */ 364 opus_int32 fs_API_Hz /* I API Sampling frequency (Hz) */ 365 ); 366 367 /****************/ 368 /* Decode frame */ 369 /****************/ 370 opus_int silk_decode_frame( 371 silk_decoder_state *psDec, /* I/O Pointer to Silk decoder state */ 372 ec_dec *psRangeDec, /* I/O Compressor data structure */ 373 opus_int16 pOut[], /* O Pointer to output speech frame */ 374 opus_int32 *pN, /* O Pointer to size of output frame */ 375 opus_int lostFlag, /* I 0: no loss, 1 loss, 2 decode fec */ 376 opus_int condCoding /* I The type of conditional coding to use */ 377 ); 378 379 /* Decode indices from bitstream */ 380 void silk_decode_indices( 381 silk_decoder_state *psDec, /* I/O State */ 382 ec_dec *psRangeDec, /* I/O Compressor data structure */ 383 opus_int FrameIndex, /* I Frame number */ 384 opus_int decode_LBRR, /* I Flag indicating LBRR data is being decoded */ 385 opus_int condCoding /* I The type of conditional coding to use */ 386 ); 387 388 /* Decode parameters from payload */ 389 void silk_decode_parameters( 390 silk_decoder_state *psDec, /* I/O State */ 391 silk_decoder_control *psDecCtrl, /* I/O Decoder control */ 392 opus_int condCoding /* I The type of conditional coding to use */ 393 ); 394 395 /* Core decoder. Performs inverse NSQ operation LTP + LPC */ 396 void silk_decode_core( 397 silk_decoder_state *psDec, /* I/O Decoder state */ 398 silk_decoder_control *psDecCtrl, /* I Decoder control */ 399 opus_int16 xq[], /* O Decoded speech */ 400 const opus_int pulses[ MAX_FRAME_LENGTH ] /* I Pulse signal */ 401 ); 402 403 /* Decode quantization indices of excitation (Shell coding) */ 404 void silk_decode_pulses( 405 ec_dec *psRangeDec, /* I/O Compressor data structure */ 406 opus_int pulses[], /* O Excitation signal */ 407 const opus_int signalType, /* I Sigtype */ 408 const opus_int quantOffsetType, /* I quantOffsetType */ 409 const opus_int frame_length /* I Frame length */ 410 ); 411 412 /******************/ 413 /* CNG */ 414 /******************/ 415 416 /* Reset CNG */ 417 void silk_CNG_Reset( 418 silk_decoder_state *psDec /* I/O Decoder state */ 419 ); 420 421 /* Updates CNG estimate, and applies the CNG when packet was lost */ 422 void silk_CNG( 423 silk_decoder_state *psDec, /* I/O Decoder state */ 424 silk_decoder_control *psDecCtrl, /* I/O Decoder control */ 425 opus_int16 frame[], /* I/O Signal */ 426 opus_int length /* I Length of residual */ 427 ); 428 429 /* Encoding of various parameters */ 430 void silk_encode_indices( 431 silk_encoder_state *psEncC, /* I/O Encoder state */ 432 ec_enc *psRangeEnc, /* I/O Compressor data structure */ 433 opus_int FrameIndex, /* I Frame number */ 434 opus_int encode_LBRR, /* I Flag indicating LBRR data is being encoded */ 435 opus_int condCoding /* I The type of conditional coding to use */ 436 ); 437 438 #endif 439