Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 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, (subject to the limitations in the disclaimer below) |
| 5 | are permitted provided that the following conditions 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 Skype Limited, nor the names of specific |
| 12 | contributors, may be used to endorse or promote products derived from |
| 13 | this software without specific prior written permission. |
| 14 | NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED |
| 15 | BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 16 | CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, |
| 17 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 18 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 22 | USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 23 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | ***********************************************************************/ |
| 27 | |
Jean-Marc Valin | 5a48412 | 2011-08-15 10:49:53 -0400 | [diff] [blame] | 28 | #ifdef HAVE_CONFIG_H |
| 29 | #include "config.h" |
| 30 | #endif |
| 31 | |
Jean-Marc Valin | 1c2f563 | 2011-09-16 01:16:53 -0700 | [diff] [blame] | 32 | #include "main.h" |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 33 | |
| 34 | /* Generates excitation for CNG LPC synthesis */ |
Jean-Marc Valin | 4dc0b39 | 2011-08-15 11:24:37 -0400 | [diff] [blame] | 35 | static inline void silk_CNG_exc( |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 36 | opus_int32 residual_Q10[], /* O CNG residual signal Q10 */ |
| 37 | opus_int32 exc_buf_Q10[], /* I Random samples buffer Q10 */ |
| 38 | opus_int32 Gain_Q16, /* I Gain to apply */ |
| 39 | opus_int length, /* I Length */ |
| 40 | opus_int32 *rand_seed /* I/O Seed to random index generator */ |
| 41 | ) |
| 42 | { |
| 43 | opus_int32 seed; |
| 44 | opus_int i, idx, exc_mask; |
| 45 | |
| 46 | exc_mask = CNG_BUF_MASK_MAX; |
| 47 | while( exc_mask > length ) { |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 48 | exc_mask = silk_RSHIFT( exc_mask, 1 ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | seed = *rand_seed; |
| 52 | for( i = 0; i < length; i++ ) { |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 53 | seed = silk_RAND( seed ); |
| 54 | idx = ( opus_int )( silk_RSHIFT( seed, 24 ) & exc_mask ); |
| 55 | silk_assert( idx >= 0 ); |
| 56 | silk_assert( idx <= CNG_BUF_MASK_MAX ); |
| 57 | residual_Q10[ i ] = ( opus_int16 )silk_SAT16( silk_SMULWW( exc_buf_Q10[ idx ], Gain_Q16 ) ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 58 | } |
| 59 | *rand_seed = seed; |
| 60 | } |
| 61 | |
| 62 | void silk_CNG_Reset( |
Koen Vos | acc7a6c | 2011-10-28 19:44:26 -0400 | [diff] [blame^] | 63 | silk_decoder_state *psDec /* I/O Decoder state */ |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 64 | ) |
| 65 | { |
| 66 | opus_int i, NLSF_step_Q15, NLSF_acc_Q15; |
| 67 | |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 68 | NLSF_step_Q15 = silk_DIV32_16( silk_int16_MAX, psDec->LPC_order + 1 ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 69 | NLSF_acc_Q15 = 0; |
| 70 | for( i = 0; i < psDec->LPC_order; i++ ) { |
| 71 | NLSF_acc_Q15 += NLSF_step_Q15; |
| 72 | psDec->sCNG.CNG_smth_NLSF_Q15[ i ] = NLSF_acc_Q15; |
| 73 | } |
| 74 | psDec->sCNG.CNG_smth_Gain_Q16 = 0; |
| 75 | psDec->sCNG.rand_seed = 3176576; |
| 76 | } |
| 77 | |
| 78 | /* Updates CNG estimate, and applies the CNG when packet was lost */ |
| 79 | void silk_CNG( |
Koen Vos | acc7a6c | 2011-10-28 19:44:26 -0400 | [diff] [blame^] | 80 | silk_decoder_state *psDec, /* I/O Decoder state */ |
| 81 | silk_decoder_control *psDecCtrl, /* I/O Decoder control */ |
| 82 | opus_int16 frame[], /* I/O Signal */ |
| 83 | opus_int length /* I Length of residual */ |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 84 | ) |
| 85 | { |
| 86 | opus_int i, j, subfr; |
| 87 | opus_int32 sum_Q6, max_Gain_Q16; |
| 88 | opus_int16 A_Q12[ MAX_LPC_ORDER ]; |
| 89 | opus_int32 CNG_sig_Q10[ MAX_FRAME_LENGTH + MAX_LPC_ORDER ]; |
| 90 | silk_CNG_struct *psCNG = &psDec->sCNG; |
| 91 | |
| 92 | if( psDec->fs_kHz != psCNG->fs_kHz ) { |
| 93 | /* Reset state */ |
| 94 | silk_CNG_Reset( psDec ); |
| 95 | |
| 96 | psCNG->fs_kHz = psDec->fs_kHz; |
| 97 | } |
| 98 | if( psDec->lossCnt == 0 && psDec->prevSignalType == TYPE_NO_VOICE_ACTIVITY ) { |
| 99 | /* Update CNG parameters */ |
| 100 | |
| 101 | /* Smoothing of LSF's */ |
| 102 | for( i = 0; i < psDec->LPC_order; i++ ) { |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 103 | psCNG->CNG_smth_NLSF_Q15[ i ] += silk_SMULWB( psDec->prevNLSF_Q15[ i ] - psCNG->CNG_smth_NLSF_Q15[ i ], CNG_NLSF_SMTH_Q16 ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 104 | } |
| 105 | /* Find the subframe with the highest gain */ |
| 106 | max_Gain_Q16 = 0; |
| 107 | subfr = 0; |
| 108 | for( i = 0; i < psDec->nb_subfr; i++ ) { |
| 109 | if( psDecCtrl->Gains_Q16[ i ] > max_Gain_Q16 ) { |
| 110 | max_Gain_Q16 = psDecCtrl->Gains_Q16[ i ]; |
| 111 | subfr = i; |
| 112 | } |
| 113 | } |
| 114 | /* Update CNG excitation buffer with excitation from this subframe */ |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 115 | silk_memmove( &psCNG->CNG_exc_buf_Q10[ psDec->subfr_length ], psCNG->CNG_exc_buf_Q10, ( psDec->nb_subfr - 1 ) * psDec->subfr_length * sizeof( opus_int32 ) ); |
| 116 | silk_memcpy( psCNG->CNG_exc_buf_Q10, &psDec->exc_Q10[ subfr * psDec->subfr_length ], psDec->subfr_length * sizeof( opus_int32 ) ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 117 | |
| 118 | /* Smooth gains */ |
| 119 | for( i = 0; i < psDec->nb_subfr; i++ ) { |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 120 | psCNG->CNG_smth_Gain_Q16 += silk_SMULWB( psDecCtrl->Gains_Q16[ i ] - psCNG->CNG_smth_Gain_Q16, CNG_GAIN_SMTH_Q16 ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
| 124 | /* Add CNG when packet is lost or during DTX */ |
| 125 | if( psDec->lossCnt ) { |
| 126 | |
| 127 | /* Generate CNG excitation */ |
| 128 | silk_CNG_exc( CNG_sig_Q10 + MAX_LPC_ORDER, psCNG->CNG_exc_buf_Q10, psCNG->CNG_smth_Gain_Q16, length, &psCNG->rand_seed ); |
| 129 | |
| 130 | /* Convert CNG NLSF to filter representation */ |
| 131 | silk_NLSF2A( A_Q12, psCNG->CNG_smth_NLSF_Q15, psDec->LPC_order ); |
| 132 | |
| 133 | /* Generate CNG signal, by synthesis filtering */ |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 134 | silk_memcpy( CNG_sig_Q10, psCNG->CNG_synth_state, MAX_LPC_ORDER * sizeof( opus_int32 ) ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 135 | for( i = 0; i < length; i++ ) { |
| 136 | /* Partially unrolled */ |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 137 | sum_Q6 = silk_SMULWB( CNG_sig_Q10[ MAX_LPC_ORDER + i - 1 ], A_Q12[ 0 ] ); |
| 138 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 2 ], A_Q12[ 1 ] ); |
| 139 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 3 ], A_Q12[ 2 ] ); |
| 140 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 4 ], A_Q12[ 3 ] ); |
| 141 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 5 ], A_Q12[ 4 ] ); |
| 142 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 6 ], A_Q12[ 5 ] ); |
| 143 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 7 ], A_Q12[ 6 ] ); |
| 144 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 8 ], A_Q12[ 7 ] ); |
| 145 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 9 ], A_Q12[ 8 ] ); |
| 146 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 10 ], A_Q12[ 9 ] ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 147 | for( j = 10; j < psDec->LPC_order; j++ ) { |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 148 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - j - 1 ], A_Q12[ j ] ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* Update states */ |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 152 | CNG_sig_Q10[ MAX_LPC_ORDER + i ] = silk_ADD_LSHIFT( CNG_sig_Q10[ MAX_LPC_ORDER + i ], sum_Q6, 4 ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 153 | |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 154 | frame[ i ] = silk_ADD_SAT16( frame[ i ], silk_RSHIFT_ROUND( sum_Q6, 6 ) ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 155 | } |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 156 | silk_memcpy( psCNG->CNG_synth_state, &CNG_sig_Q10[ length ], MAX_LPC_ORDER * sizeof( opus_int32 ) ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 157 | } else { |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 158 | silk_memset( psCNG->CNG_synth_state, 0, psDec->LPC_order * sizeof( opus_int32 ) ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 159 | } |
| 160 | } |