Chris@69: /*********************************************************************** Chris@69: Copyright (c) 2006-2011, Skype Limited. All rights reserved. Chris@69: Redistribution and use in source and binary forms, with or without Chris@69: modification, are permitted provided that the following conditions Chris@69: are met: Chris@69: - Redistributions of source code must retain the above copyright notice, Chris@69: this list of conditions and the following disclaimer. Chris@69: - Redistributions in binary form must reproduce the above copyright Chris@69: notice, this list of conditions and the following disclaimer in the Chris@69: documentation and/or other materials provided with the distribution. Chris@69: - Neither the name of Internet Society, IETF or IETF Trust, nor the Chris@69: names of specific contributors, may be used to endorse or promote Chris@69: products derived from this software without specific prior written Chris@69: permission. Chris@69: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" Chris@69: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE Chris@69: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE Chris@69: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE Chris@69: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR Chris@69: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF Chris@69: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS Chris@69: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN Chris@69: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) Chris@69: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE Chris@69: POSSIBILITY OF SUCH DAMAGE. Chris@69: ***********************************************************************/ Chris@69: Chris@69: #ifdef HAVE_CONFIG_H Chris@69: #include "config.h" Chris@69: #endif Chris@69: Chris@69: #include "main.h" Chris@69: #include "stack_alloc.h" Chris@69: Chris@69: /**********************************************************/ Chris@69: /* Core decoder. Performs inverse NSQ operation LTP + LPC */ Chris@69: /**********************************************************/ Chris@69: void silk_decode_core( Chris@69: silk_decoder_state *psDec, /* I/O Decoder state */ Chris@69: silk_decoder_control *psDecCtrl, /* I Decoder control */ Chris@69: opus_int16 xq[], /* O Decoded speech */ Chris@69: const opus_int16 pulses[ MAX_FRAME_LENGTH ], /* I Pulse signal */ Chris@69: int arch /* I Run-time architecture */ Chris@69: ) Chris@69: { Chris@69: opus_int i, k, lag = 0, start_idx, sLTP_buf_idx, NLSF_interpolation_flag, signalType; Chris@69: opus_int16 *A_Q12, *B_Q14, *pxq, A_Q12_tmp[ MAX_LPC_ORDER ]; Chris@69: VARDECL( opus_int16, sLTP ); Chris@69: VARDECL( opus_int32, sLTP_Q15 ); Chris@69: opus_int32 LTP_pred_Q13, LPC_pred_Q10, Gain_Q10, inv_gain_Q31, gain_adj_Q16, rand_seed, offset_Q10; Chris@69: opus_int32 *pred_lag_ptr, *pexc_Q14, *pres_Q14; Chris@69: VARDECL( opus_int32, res_Q14 ); Chris@69: VARDECL( opus_int32, sLPC_Q14 ); Chris@69: SAVE_STACK; Chris@69: Chris@69: silk_assert( psDec->prev_gain_Q16 != 0 ); Chris@69: Chris@69: ALLOC( sLTP, psDec->ltp_mem_length, opus_int16 ); Chris@69: ALLOC( sLTP_Q15, psDec->ltp_mem_length + psDec->frame_length, opus_int32 ); Chris@69: ALLOC( res_Q14, psDec->subfr_length, opus_int32 ); Chris@69: ALLOC( sLPC_Q14, psDec->subfr_length + MAX_LPC_ORDER, opus_int32 ); Chris@69: Chris@69: offset_Q10 = silk_Quantization_Offsets_Q10[ psDec->indices.signalType >> 1 ][ psDec->indices.quantOffsetType ]; Chris@69: Chris@69: if( psDec->indices.NLSFInterpCoef_Q2 < 1 << 2 ) { Chris@69: NLSF_interpolation_flag = 1; Chris@69: } else { Chris@69: NLSF_interpolation_flag = 0; Chris@69: } Chris@69: Chris@69: /* Decode excitation */ Chris@69: rand_seed = psDec->indices.Seed; Chris@69: for( i = 0; i < psDec->frame_length; i++ ) { Chris@69: rand_seed = silk_RAND( rand_seed ); Chris@69: psDec->exc_Q14[ i ] = silk_LSHIFT( (opus_int32)pulses[ i ], 14 ); Chris@69: if( psDec->exc_Q14[ i ] > 0 ) { Chris@69: psDec->exc_Q14[ i ] -= QUANT_LEVEL_ADJUST_Q10 << 4; Chris@69: } else Chris@69: if( psDec->exc_Q14[ i ] < 0 ) { Chris@69: psDec->exc_Q14[ i ] += QUANT_LEVEL_ADJUST_Q10 << 4; Chris@69: } Chris@69: psDec->exc_Q14[ i ] += offset_Q10 << 4; Chris@69: if( rand_seed < 0 ) { Chris@69: psDec->exc_Q14[ i ] = -psDec->exc_Q14[ i ]; Chris@69: } Chris@69: Chris@69: rand_seed = silk_ADD32_ovflw( rand_seed, pulses[ i ] ); Chris@69: } Chris@69: Chris@69: /* Copy LPC state */ Chris@69: silk_memcpy( sLPC_Q14, psDec->sLPC_Q14_buf, MAX_LPC_ORDER * sizeof( opus_int32 ) ); Chris@69: Chris@69: pexc_Q14 = psDec->exc_Q14; Chris@69: pxq = xq; Chris@69: sLTP_buf_idx = psDec->ltp_mem_length; Chris@69: /* Loop over subframes */ Chris@69: for( k = 0; k < psDec->nb_subfr; k++ ) { Chris@69: pres_Q14 = res_Q14; Chris@69: A_Q12 = psDecCtrl->PredCoef_Q12[ k >> 1 ]; Chris@69: Chris@69: /* Preload LPC coeficients to array on stack. Gives small performance gain */ Chris@69: silk_memcpy( A_Q12_tmp, A_Q12, psDec->LPC_order * sizeof( opus_int16 ) ); Chris@69: B_Q14 = &psDecCtrl->LTPCoef_Q14[ k * LTP_ORDER ]; Chris@69: signalType = psDec->indices.signalType; Chris@69: Chris@69: Gain_Q10 = silk_RSHIFT( psDecCtrl->Gains_Q16[ k ], 6 ); Chris@69: inv_gain_Q31 = silk_INVERSE32_varQ( psDecCtrl->Gains_Q16[ k ], 47 ); Chris@69: Chris@69: /* Calculate gain adjustment factor */ Chris@69: if( psDecCtrl->Gains_Q16[ k ] != psDec->prev_gain_Q16 ) { Chris@69: gain_adj_Q16 = silk_DIV32_varQ( psDec->prev_gain_Q16, psDecCtrl->Gains_Q16[ k ], 16 ); Chris@69: Chris@69: /* Scale short term state */ Chris@69: for( i = 0; i < MAX_LPC_ORDER; i++ ) { Chris@69: sLPC_Q14[ i ] = silk_SMULWW( gain_adj_Q16, sLPC_Q14[ i ] ); Chris@69: } Chris@69: } else { Chris@69: gain_adj_Q16 = (opus_int32)1 << 16; Chris@69: } Chris@69: Chris@69: /* Save inv_gain */ Chris@69: silk_assert( inv_gain_Q31 != 0 ); Chris@69: psDec->prev_gain_Q16 = psDecCtrl->Gains_Q16[ k ]; Chris@69: Chris@69: /* Avoid abrupt transition from voiced PLC to unvoiced normal decoding */ Chris@69: if( psDec->lossCnt && psDec->prevSignalType == TYPE_VOICED && Chris@69: psDec->indices.signalType != TYPE_VOICED && k < MAX_NB_SUBFR/2 ) { Chris@69: Chris@69: silk_memset( B_Q14, 0, LTP_ORDER * sizeof( opus_int16 ) ); Chris@69: B_Q14[ LTP_ORDER/2 ] = SILK_FIX_CONST( 0.25, 14 ); Chris@69: Chris@69: signalType = TYPE_VOICED; Chris@69: psDecCtrl->pitchL[ k ] = psDec->lagPrev; Chris@69: } Chris@69: Chris@69: if( signalType == TYPE_VOICED ) { Chris@69: /* Voiced */ Chris@69: lag = psDecCtrl->pitchL[ k ]; Chris@69: Chris@69: /* Re-whitening */ Chris@69: if( k == 0 || ( k == 2 && NLSF_interpolation_flag ) ) { Chris@69: /* Rewhiten with new A coefs */ Chris@69: start_idx = psDec->ltp_mem_length - lag - psDec->LPC_order - LTP_ORDER / 2; Chris@69: celt_assert( start_idx > 0 ); Chris@69: Chris@69: if( k == 2 ) { Chris@69: silk_memcpy( &psDec->outBuf[ psDec->ltp_mem_length ], xq, 2 * psDec->subfr_length * sizeof( opus_int16 ) ); Chris@69: } Chris@69: Chris@69: silk_LPC_analysis_filter( &sLTP[ start_idx ], &psDec->outBuf[ start_idx + k * psDec->subfr_length ], Chris@69: A_Q12, psDec->ltp_mem_length - start_idx, psDec->LPC_order, arch ); Chris@69: Chris@69: /* After rewhitening the LTP state is unscaled */ Chris@69: if( k == 0 ) { Chris@69: /* Do LTP downscaling to reduce inter-packet dependency */ Chris@69: inv_gain_Q31 = silk_LSHIFT( silk_SMULWB( inv_gain_Q31, psDecCtrl->LTP_scale_Q14 ), 2 ); Chris@69: } Chris@69: for( i = 0; i < lag + LTP_ORDER/2; i++ ) { Chris@69: sLTP_Q15[ sLTP_buf_idx - i - 1 ] = silk_SMULWB( inv_gain_Q31, sLTP[ psDec->ltp_mem_length - i - 1 ] ); Chris@69: } Chris@69: } else { Chris@69: /* Update LTP state when Gain changes */ Chris@69: if( gain_adj_Q16 != (opus_int32)1 << 16 ) { Chris@69: for( i = 0; i < lag + LTP_ORDER/2; i++ ) { Chris@69: sLTP_Q15[ sLTP_buf_idx - i - 1 ] = silk_SMULWW( gain_adj_Q16, sLTP_Q15[ sLTP_buf_idx - i - 1 ] ); Chris@69: } Chris@69: } Chris@69: } Chris@69: } Chris@69: Chris@69: /* Long-term prediction */ Chris@69: if( signalType == TYPE_VOICED ) { Chris@69: /* Set up pointer */ Chris@69: pred_lag_ptr = &sLTP_Q15[ sLTP_buf_idx - lag + LTP_ORDER / 2 ]; Chris@69: for( i = 0; i < psDec->subfr_length; i++ ) { Chris@69: /* Unrolled loop */ Chris@69: /* Avoids introducing a bias because silk_SMLAWB() always rounds to -inf */ Chris@69: LTP_pred_Q13 = 2; Chris@69: LTP_pred_Q13 = silk_SMLAWB( LTP_pred_Q13, pred_lag_ptr[ 0 ], B_Q14[ 0 ] ); Chris@69: LTP_pred_Q13 = silk_SMLAWB( LTP_pred_Q13, pred_lag_ptr[ -1 ], B_Q14[ 1 ] ); Chris@69: LTP_pred_Q13 = silk_SMLAWB( LTP_pred_Q13, pred_lag_ptr[ -2 ], B_Q14[ 2 ] ); Chris@69: LTP_pred_Q13 = silk_SMLAWB( LTP_pred_Q13, pred_lag_ptr[ -3 ], B_Q14[ 3 ] ); Chris@69: LTP_pred_Q13 = silk_SMLAWB( LTP_pred_Q13, pred_lag_ptr[ -4 ], B_Q14[ 4 ] ); Chris@69: pred_lag_ptr++; Chris@69: Chris@69: /* Generate LPC excitation */ Chris@69: pres_Q14[ i ] = silk_ADD_LSHIFT32( pexc_Q14[ i ], LTP_pred_Q13, 1 ); Chris@69: Chris@69: /* Update states */ Chris@69: sLTP_Q15[ sLTP_buf_idx ] = silk_LSHIFT( pres_Q14[ i ], 1 ); Chris@69: sLTP_buf_idx++; Chris@69: } Chris@69: } else { Chris@69: pres_Q14 = pexc_Q14; Chris@69: } Chris@69: Chris@69: for( i = 0; i < psDec->subfr_length; i++ ) { Chris@69: /* Short-term prediction */ Chris@69: celt_assert( psDec->LPC_order == 10 || psDec->LPC_order == 16 ); Chris@69: /* Avoids introducing a bias because silk_SMLAWB() always rounds to -inf */ Chris@69: LPC_pred_Q10 = silk_RSHIFT( psDec->LPC_order, 1 ); Chris@69: LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 1 ], A_Q12_tmp[ 0 ] ); Chris@69: LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 2 ], A_Q12_tmp[ 1 ] ); Chris@69: LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 3 ], A_Q12_tmp[ 2 ] ); Chris@69: LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 4 ], A_Q12_tmp[ 3 ] ); Chris@69: LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 5 ], A_Q12_tmp[ 4 ] ); Chris@69: LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 6 ], A_Q12_tmp[ 5 ] ); Chris@69: LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 7 ], A_Q12_tmp[ 6 ] ); Chris@69: LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 8 ], A_Q12_tmp[ 7 ] ); Chris@69: LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 9 ], A_Q12_tmp[ 8 ] ); Chris@69: LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 10 ], A_Q12_tmp[ 9 ] ); Chris@69: if( psDec->LPC_order == 16 ) { Chris@69: LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 11 ], A_Q12_tmp[ 10 ] ); Chris@69: LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 12 ], A_Q12_tmp[ 11 ] ); Chris@69: LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 13 ], A_Q12_tmp[ 12 ] ); Chris@69: LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 14 ], A_Q12_tmp[ 13 ] ); Chris@69: LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 15 ], A_Q12_tmp[ 14 ] ); Chris@69: LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 16 ], A_Q12_tmp[ 15 ] ); Chris@69: } Chris@69: Chris@69: /* Add prediction to LPC excitation */ Chris@69: sLPC_Q14[ MAX_LPC_ORDER + i ] = silk_ADD_SAT32( pres_Q14[ i ], silk_LSHIFT_SAT32( LPC_pred_Q10, 4 ) ); Chris@69: Chris@69: /* Scale with gain */ Chris@69: pxq[ i ] = (opus_int16)silk_SAT16( silk_RSHIFT_ROUND( silk_SMULWW( sLPC_Q14[ MAX_LPC_ORDER + i ], Gain_Q10 ), 8 ) ); Chris@69: } Chris@69: Chris@69: /* Update LPC filter state */ Chris@69: silk_memcpy( sLPC_Q14, &sLPC_Q14[ psDec->subfr_length ], MAX_LPC_ORDER * sizeof( opus_int32 ) ); Chris@69: pexc_Q14 += psDec->subfr_length; Chris@69: pxq += psDec->subfr_length; Chris@69: } Chris@69: Chris@69: /* Save LPC state */ Chris@69: silk_memcpy( psDec->sLPC_Q14_buf, sLPC_Q14, MAX_LPC_ORDER * sizeof( opus_int32 ) ); Chris@69: RESTORE_STACK; Chris@69: }