michael@0: /*********************************************************************** michael@0: Copyright (c) 2006-2011, Skype Limited. All rights reserved. michael@0: Redistribution and use in source and binary forms, with or without michael@0: modification, are permitted provided that the following conditions michael@0: are met: michael@0: - Redistributions of source code must retain the above copyright notice, michael@0: this list of conditions and the following disclaimer. michael@0: - Redistributions in binary form must reproduce the above copyright michael@0: notice, this list of conditions and the following disclaimer in the michael@0: documentation and/or other materials provided with the distribution. michael@0: - Neither the name of Internet Society, IETF or IETF Trust, nor the michael@0: names of specific contributors, may be used to endorse or promote michael@0: products derived from this software without specific prior written michael@0: permission. michael@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" michael@0: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE michael@0: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR michael@0: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF michael@0: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS michael@0: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN michael@0: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE michael@0: POSSIBILITY OF SUCH DAMAGE. michael@0: ***********************************************************************/ michael@0: michael@0: #ifdef HAVE_CONFIG_H michael@0: #include "config.h" michael@0: #endif michael@0: michael@0: #include "main.h" michael@0: #include "stack_alloc.h" michael@0: michael@0: /* Generates excitation for CNG LPC synthesis */ michael@0: static OPUS_INLINE void silk_CNG_exc( michael@0: opus_int32 residual_Q10[], /* O CNG residual signal Q10 */ michael@0: opus_int32 exc_buf_Q14[], /* I Random samples buffer Q10 */ michael@0: opus_int32 Gain_Q16, /* I Gain to apply */ michael@0: opus_int length, /* I Length */ michael@0: opus_int32 *rand_seed /* I/O Seed to random index generator */ michael@0: ) michael@0: { michael@0: opus_int32 seed; michael@0: opus_int i, idx, exc_mask; michael@0: michael@0: exc_mask = CNG_BUF_MASK_MAX; michael@0: while( exc_mask > length ) { michael@0: exc_mask = silk_RSHIFT( exc_mask, 1 ); michael@0: } michael@0: michael@0: seed = *rand_seed; michael@0: for( i = 0; i < length; i++ ) { michael@0: seed = silk_RAND( seed ); michael@0: idx = (opus_int)( silk_RSHIFT( seed, 24 ) & exc_mask ); michael@0: silk_assert( idx >= 0 ); michael@0: silk_assert( idx <= CNG_BUF_MASK_MAX ); michael@0: residual_Q10[ i ] = (opus_int16)silk_SAT16( silk_SMULWW( exc_buf_Q14[ idx ], Gain_Q16 >> 4 ) ); michael@0: } michael@0: *rand_seed = seed; michael@0: } michael@0: michael@0: void silk_CNG_Reset( michael@0: silk_decoder_state *psDec /* I/O Decoder state */ michael@0: ) michael@0: { michael@0: opus_int i, NLSF_step_Q15, NLSF_acc_Q15; michael@0: michael@0: NLSF_step_Q15 = silk_DIV32_16( silk_int16_MAX, psDec->LPC_order + 1 ); michael@0: NLSF_acc_Q15 = 0; michael@0: for( i = 0; i < psDec->LPC_order; i++ ) { michael@0: NLSF_acc_Q15 += NLSF_step_Q15; michael@0: psDec->sCNG.CNG_smth_NLSF_Q15[ i ] = NLSF_acc_Q15; michael@0: } michael@0: psDec->sCNG.CNG_smth_Gain_Q16 = 0; michael@0: psDec->sCNG.rand_seed = 3176576; michael@0: } michael@0: michael@0: /* Updates CNG estimate, and applies the CNG when packet was lost */ michael@0: void silk_CNG( michael@0: silk_decoder_state *psDec, /* I/O Decoder state */ michael@0: silk_decoder_control *psDecCtrl, /* I/O Decoder control */ michael@0: opus_int16 frame[], /* I/O Signal */ michael@0: opus_int length /* I Length of residual */ michael@0: ) michael@0: { michael@0: opus_int i, subfr; michael@0: opus_int32 sum_Q6, max_Gain_Q16; michael@0: opus_int16 A_Q12[ MAX_LPC_ORDER ]; michael@0: silk_CNG_struct *psCNG = &psDec->sCNG; michael@0: SAVE_STACK; michael@0: michael@0: if( psDec->fs_kHz != psCNG->fs_kHz ) { michael@0: /* Reset state */ michael@0: silk_CNG_Reset( psDec ); michael@0: michael@0: psCNG->fs_kHz = psDec->fs_kHz; michael@0: } michael@0: if( psDec->lossCnt == 0 && psDec->prevSignalType == TYPE_NO_VOICE_ACTIVITY ) { michael@0: /* Update CNG parameters */ michael@0: michael@0: /* Smoothing of LSF's */ michael@0: for( i = 0; i < psDec->LPC_order; i++ ) { michael@0: psCNG->CNG_smth_NLSF_Q15[ i ] += silk_SMULWB( (opus_int32)psDec->prevNLSF_Q15[ i ] - (opus_int32)psCNG->CNG_smth_NLSF_Q15[ i ], CNG_NLSF_SMTH_Q16 ); michael@0: } michael@0: /* Find the subframe with the highest gain */ michael@0: max_Gain_Q16 = 0; michael@0: subfr = 0; michael@0: for( i = 0; i < psDec->nb_subfr; i++ ) { michael@0: if( psDecCtrl->Gains_Q16[ i ] > max_Gain_Q16 ) { michael@0: max_Gain_Q16 = psDecCtrl->Gains_Q16[ i ]; michael@0: subfr = i; michael@0: } michael@0: } michael@0: /* Update CNG excitation buffer with excitation from this subframe */ michael@0: silk_memmove( &psCNG->CNG_exc_buf_Q14[ psDec->subfr_length ], psCNG->CNG_exc_buf_Q14, ( psDec->nb_subfr - 1 ) * psDec->subfr_length * sizeof( opus_int32 ) ); michael@0: silk_memcpy( psCNG->CNG_exc_buf_Q14, &psDec->exc_Q14[ subfr * psDec->subfr_length ], psDec->subfr_length * sizeof( opus_int32 ) ); michael@0: michael@0: /* Smooth gains */ michael@0: for( i = 0; i < psDec->nb_subfr; i++ ) { michael@0: psCNG->CNG_smth_Gain_Q16 += silk_SMULWB( psDecCtrl->Gains_Q16[ i ] - psCNG->CNG_smth_Gain_Q16, CNG_GAIN_SMTH_Q16 ); michael@0: } michael@0: } michael@0: michael@0: /* Add CNG when packet is lost or during DTX */ michael@0: if( psDec->lossCnt ) { michael@0: VARDECL( opus_int32, CNG_sig_Q10 ); michael@0: michael@0: ALLOC( CNG_sig_Q10, length + MAX_LPC_ORDER, opus_int32 ); michael@0: michael@0: /* Generate CNG excitation */ michael@0: silk_CNG_exc( CNG_sig_Q10 + MAX_LPC_ORDER, psCNG->CNG_exc_buf_Q14, psCNG->CNG_smth_Gain_Q16, length, &psCNG->rand_seed ); michael@0: michael@0: /* Convert CNG NLSF to filter representation */ michael@0: silk_NLSF2A( A_Q12, psCNG->CNG_smth_NLSF_Q15, psDec->LPC_order ); michael@0: michael@0: /* Generate CNG signal, by synthesis filtering */ michael@0: silk_memcpy( CNG_sig_Q10, psCNG->CNG_synth_state, MAX_LPC_ORDER * sizeof( opus_int32 ) ); michael@0: for( i = 0; i < length; i++ ) { michael@0: silk_assert( psDec->LPC_order == 10 || psDec->LPC_order == 16 ); michael@0: /* Avoids introducing a bias because silk_SMLAWB() always rounds to -inf */ michael@0: sum_Q6 = silk_RSHIFT( psDec->LPC_order, 1 ); michael@0: sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 1 ], A_Q12[ 0 ] ); michael@0: sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 2 ], A_Q12[ 1 ] ); michael@0: sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 3 ], A_Q12[ 2 ] ); michael@0: sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 4 ], A_Q12[ 3 ] ); michael@0: sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 5 ], A_Q12[ 4 ] ); michael@0: sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 6 ], A_Q12[ 5 ] ); michael@0: sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 7 ], A_Q12[ 6 ] ); michael@0: sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 8 ], A_Q12[ 7 ] ); michael@0: sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 9 ], A_Q12[ 8 ] ); michael@0: sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 10 ], A_Q12[ 9 ] ); michael@0: if( psDec->LPC_order == 16 ) { michael@0: sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 11 ], A_Q12[ 10 ] ); michael@0: sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 12 ], A_Q12[ 11 ] ); michael@0: sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 13 ], A_Q12[ 12 ] ); michael@0: sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 14 ], A_Q12[ 13 ] ); michael@0: sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 15 ], A_Q12[ 14 ] ); michael@0: sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 16 ], A_Q12[ 15 ] ); michael@0: } michael@0: michael@0: /* Update states */ michael@0: CNG_sig_Q10[ MAX_LPC_ORDER + i ] = silk_ADD_LSHIFT( CNG_sig_Q10[ MAX_LPC_ORDER + i ], sum_Q6, 4 ); michael@0: michael@0: frame[ i ] = silk_ADD_SAT16( frame[ i ], silk_RSHIFT_ROUND( sum_Q6, 6 ) ); michael@0: } michael@0: silk_memcpy( psCNG->CNG_synth_state, &CNG_sig_Q10[ length ], MAX_LPC_ORDER * sizeof( opus_int32 ) ); michael@0: } else { michael@0: silk_memset( psCNG->CNG_synth_state, 0, psDec->LPC_order * sizeof( opus_int32 ) ); michael@0: } michael@0: RESTORE_STACK; michael@0: }