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 |
Jean-Marc Valin | ae00e60 | 2012-04-20 16:31:04 -0400 | [diff] [blame] | 4 | modification, are permitted provided that the following conditions |
| 5 | are met: |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 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. |
Ralph Giles | f2446c2 | 2013-09-16 14:40:04 -0700 | [diff] [blame] | 11 | - Neither the name of Internet Society, IETF or IETF Trust, nor the |
Jean-Marc Valin | ae00e60 | 2012-04-20 16:31:04 -0400 | [diff] [blame] | 12 | names of specific contributors, may be used to endorse or promote |
| 13 | products derived from this software without specific prior written |
| 14 | permission. |
Timothy B. Terriberry | 80ad383 | 2013-05-19 18:00:39 -0700 | [diff] [blame] | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
Jean-Marc Valin | ae00e60 | 2012-04-20 16:31:04 -0400 | [diff] [blame] | 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. |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 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" |
Timothy B. Terriberry | c152d60 | 2013-05-08 10:32:37 -0700 | [diff] [blame] | 33 | #include "stack_alloc.h" |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 34 | |
| 35 | /* Generates excitation for CNG LPC synthesis */ |
Jean-Marc Valin | 4dc0b39 | 2011-08-15 11:24:37 -0400 | [diff] [blame] | 36 | static inline void silk_CNG_exc( |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 37 | opus_int32 residual_Q10[], /* O CNG residual signal Q10 */ |
Koen Vos | a51ebd6 | 2011-12-14 11:39:29 -0500 | [diff] [blame] | 38 | opus_int32 exc_buf_Q14[], /* I Random samples buffer Q10 */ |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 39 | opus_int32 Gain_Q16, /* I Gain to apply */ |
| 40 | opus_int length, /* I Length */ |
| 41 | opus_int32 *rand_seed /* I/O Seed to random index generator */ |
| 42 | ) |
| 43 | { |
| 44 | opus_int32 seed; |
| 45 | opus_int i, idx, exc_mask; |
| 46 | |
| 47 | exc_mask = CNG_BUF_MASK_MAX; |
| 48 | while( exc_mask > length ) { |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 49 | exc_mask = silk_RSHIFT( exc_mask, 1 ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | seed = *rand_seed; |
| 53 | for( i = 0; i < length; i++ ) { |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 54 | seed = silk_RAND( seed ); |
Koen Vos | bbfc9c9 | 2011-12-13 14:50:12 -0500 | [diff] [blame] | 55 | idx = (opus_int)( silk_RSHIFT( seed, 24 ) & exc_mask ); |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 56 | silk_assert( idx >= 0 ); |
| 57 | silk_assert( idx <= CNG_BUF_MASK_MAX ); |
Koen Vos | a51ebd6 | 2011-12-14 11:39:29 -0500 | [diff] [blame] | 58 | residual_Q10[ i ] = (opus_int16)silk_SAT16( silk_SMULWW( exc_buf_Q14[ idx ], Gain_Q16 >> 4 ) ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 59 | } |
| 60 | *rand_seed = seed; |
| 61 | } |
| 62 | |
| 63 | void silk_CNG_Reset( |
Koen Vos | acc7a6c | 2011-10-28 19:44:26 -0400 | [diff] [blame] | 64 | silk_decoder_state *psDec /* I/O Decoder state */ |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 65 | ) |
| 66 | { |
| 67 | opus_int i, NLSF_step_Q15, NLSF_acc_Q15; |
| 68 | |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 69 | 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] | 70 | NLSF_acc_Q15 = 0; |
| 71 | for( i = 0; i < psDec->LPC_order; i++ ) { |
| 72 | NLSF_acc_Q15 += NLSF_step_Q15; |
| 73 | psDec->sCNG.CNG_smth_NLSF_Q15[ i ] = NLSF_acc_Q15; |
| 74 | } |
| 75 | psDec->sCNG.CNG_smth_Gain_Q16 = 0; |
| 76 | psDec->sCNG.rand_seed = 3176576; |
| 77 | } |
| 78 | |
| 79 | /* Updates CNG estimate, and applies the CNG when packet was lost */ |
| 80 | void silk_CNG( |
Koen Vos | acc7a6c | 2011-10-28 19:44:26 -0400 | [diff] [blame] | 81 | silk_decoder_state *psDec, /* I/O Decoder state */ |
| 82 | silk_decoder_control *psDecCtrl, /* I/O Decoder control */ |
| 83 | opus_int16 frame[], /* I/O Signal */ |
| 84 | opus_int length /* I Length of residual */ |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 85 | ) |
| 86 | { |
Koen Vos | bf75c8e | 2011-12-13 14:47:31 -0500 | [diff] [blame] | 87 | opus_int i, subfr; |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 88 | opus_int32 sum_Q6, max_Gain_Q16; |
| 89 | opus_int16 A_Q12[ MAX_LPC_ORDER ]; |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 90 | silk_CNG_struct *psCNG = &psDec->sCNG; |
Timothy B. Terriberry | c152d60 | 2013-05-08 10:32:37 -0700 | [diff] [blame] | 91 | SAVE_STACK; |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 92 | |
| 93 | if( psDec->fs_kHz != psCNG->fs_kHz ) { |
| 94 | /* Reset state */ |
| 95 | silk_CNG_Reset( psDec ); |
| 96 | |
| 97 | psCNG->fs_kHz = psDec->fs_kHz; |
| 98 | } |
| 99 | if( psDec->lossCnt == 0 && psDec->prevSignalType == TYPE_NO_VOICE_ACTIVITY ) { |
| 100 | /* Update CNG parameters */ |
| 101 | |
| 102 | /* Smoothing of LSF's */ |
| 103 | for( i = 0; i < psDec->LPC_order; i++ ) { |
Jean-Marc Valin | 905197d | 2012-03-08 14:09:09 -0500 | [diff] [blame] | 104 | 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 ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 105 | } |
| 106 | /* Find the subframe with the highest gain */ |
| 107 | max_Gain_Q16 = 0; |
| 108 | subfr = 0; |
| 109 | for( i = 0; i < psDec->nb_subfr; i++ ) { |
| 110 | if( psDecCtrl->Gains_Q16[ i ] > max_Gain_Q16 ) { |
| 111 | max_Gain_Q16 = psDecCtrl->Gains_Q16[ i ]; |
| 112 | subfr = i; |
| 113 | } |
| 114 | } |
| 115 | /* Update CNG excitation buffer with excitation from this subframe */ |
Koen Vos | a51ebd6 | 2011-12-14 11:39:29 -0500 | [diff] [blame] | 116 | 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 ) ); |
| 117 | silk_memcpy( psCNG->CNG_exc_buf_Q14, &psDec->exc_Q14[ subfr * psDec->subfr_length ], psDec->subfr_length * sizeof( opus_int32 ) ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 118 | |
| 119 | /* Smooth gains */ |
| 120 | for( i = 0; i < psDec->nb_subfr; i++ ) { |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 121 | 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] | 122 | } |
| 123 | } |
| 124 | |
| 125 | /* Add CNG when packet is lost or during DTX */ |
| 126 | if( psDec->lossCnt ) { |
Timothy B. Terriberry | c152d60 | 2013-05-08 10:32:37 -0700 | [diff] [blame] | 127 | VARDECL( opus_int32, CNG_sig_Q10 ); |
| 128 | |
| 129 | ALLOC( CNG_sig_Q10, length + MAX_LPC_ORDER, opus_int32 ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 130 | |
| 131 | /* Generate CNG excitation */ |
Koen Vos | a51ebd6 | 2011-12-14 11:39:29 -0500 | [diff] [blame] | 132 | silk_CNG_exc( CNG_sig_Q10 + MAX_LPC_ORDER, psCNG->CNG_exc_buf_Q14, psCNG->CNG_smth_Gain_Q16, length, &psCNG->rand_seed ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 133 | |
| 134 | /* Convert CNG NLSF to filter representation */ |
| 135 | silk_NLSF2A( A_Q12, psCNG->CNG_smth_NLSF_Q15, psDec->LPC_order ); |
| 136 | |
| 137 | /* Generate CNG signal, by synthesis filtering */ |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 138 | 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] | 139 | for( i = 0; i < length; i++ ) { |
Koen Vos | bf75c8e | 2011-12-13 14:47:31 -0500 | [diff] [blame] | 140 | silk_assert( psDec->LPC_order == 10 || psDec->LPC_order == 16 ); |
Koen Vos | bbfc9c9 | 2011-12-13 14:50:12 -0500 | [diff] [blame] | 141 | /* Avoids introducing a bias because silk_SMLAWB() always rounds to -inf */ |
| 142 | sum_Q6 = silk_RSHIFT( psDec->LPC_order, 1 ); |
| 143 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 1 ], A_Q12[ 0 ] ); |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 144 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 2 ], A_Q12[ 1 ] ); |
| 145 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 3 ], A_Q12[ 2 ] ); |
| 146 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 4 ], A_Q12[ 3 ] ); |
| 147 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 5 ], A_Q12[ 4 ] ); |
| 148 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 6 ], A_Q12[ 5 ] ); |
| 149 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 7 ], A_Q12[ 6 ] ); |
| 150 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 8 ], A_Q12[ 7 ] ); |
| 151 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 9 ], A_Q12[ 8 ] ); |
| 152 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 10 ], A_Q12[ 9 ] ); |
Koen Vos | bf75c8e | 2011-12-13 14:47:31 -0500 | [diff] [blame] | 153 | if( psDec->LPC_order == 16 ) { |
| 154 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 11 ], A_Q12[ 10 ] ); |
| 155 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 12 ], A_Q12[ 11 ] ); |
| 156 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 13 ], A_Q12[ 12 ] ); |
| 157 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 14 ], A_Q12[ 13 ] ); |
| 158 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 15 ], A_Q12[ 14 ] ); |
| 159 | sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 16 ], A_Q12[ 15 ] ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* Update states */ |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 163 | 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] | 164 | |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 165 | 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] | 166 | } |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 167 | 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] | 168 | } else { |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 169 | 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] | 170 | } |
Timothy B. Terriberry | c152d60 | 2013-05-08 10:32:37 -0700 | [diff] [blame] | 171 | RESTORE_STACK; |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 172 | } |