Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /*********************************************************************** |
michael@0 | 2 | Copyright (c) 2006-2011, Skype Limited. All rights reserved. |
michael@0 | 3 | Redistribution and use in source and binary forms, with or without |
michael@0 | 4 | modification, are permitted provided that the following conditions |
michael@0 | 5 | are met: |
michael@0 | 6 | - Redistributions of source code must retain the above copyright notice, |
michael@0 | 7 | this list of conditions and the following disclaimer. |
michael@0 | 8 | - Redistributions in binary form must reproduce the above copyright |
michael@0 | 9 | notice, this list of conditions and the following disclaimer in the |
michael@0 | 10 | documentation and/or other materials provided with the distribution. |
michael@0 | 11 | - Neither the name of Internet Society, IETF or IETF Trust, nor the |
michael@0 | 12 | names of specific contributors, may be used to endorse or promote |
michael@0 | 13 | products derived from this software without specific prior written |
michael@0 | 14 | permission. |
michael@0 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
michael@0 | 16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
michael@0 | 17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
michael@0 | 18 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
michael@0 | 19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
michael@0 | 20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
michael@0 | 21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
michael@0 | 22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
michael@0 | 23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
michael@0 | 24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
michael@0 | 25 | POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 26 | ***********************************************************************/ |
michael@0 | 27 | |
michael@0 | 28 | #ifdef HAVE_CONFIG_H |
michael@0 | 29 | #include "config.h" |
michael@0 | 30 | #endif |
michael@0 | 31 | |
michael@0 | 32 | #include "main.h" |
michael@0 | 33 | #include "stack_alloc.h" |
michael@0 | 34 | |
michael@0 | 35 | /**********************************************************/ |
michael@0 | 36 | /* Core decoder. Performs inverse NSQ operation LTP + LPC */ |
michael@0 | 37 | /**********************************************************/ |
michael@0 | 38 | void silk_decode_core( |
michael@0 | 39 | silk_decoder_state *psDec, /* I/O Decoder state */ |
michael@0 | 40 | silk_decoder_control *psDecCtrl, /* I Decoder control */ |
michael@0 | 41 | opus_int16 xq[], /* O Decoded speech */ |
michael@0 | 42 | const opus_int pulses[ MAX_FRAME_LENGTH ] /* I Pulse signal */ |
michael@0 | 43 | ) |
michael@0 | 44 | { |
michael@0 | 45 | opus_int i, k, lag = 0, start_idx, sLTP_buf_idx, NLSF_interpolation_flag, signalType; |
michael@0 | 46 | opus_int16 *A_Q12, *B_Q14, *pxq, A_Q12_tmp[ MAX_LPC_ORDER ]; |
michael@0 | 47 | VARDECL( opus_int16, sLTP ); |
michael@0 | 48 | VARDECL( opus_int32, sLTP_Q15 ); |
michael@0 | 49 | opus_int32 LTP_pred_Q13, LPC_pred_Q10, Gain_Q10, inv_gain_Q31, gain_adj_Q16, rand_seed, offset_Q10; |
michael@0 | 50 | opus_int32 *pred_lag_ptr, *pexc_Q14, *pres_Q14; |
michael@0 | 51 | VARDECL( opus_int32, res_Q14 ); |
michael@0 | 52 | VARDECL( opus_int32, sLPC_Q14 ); |
michael@0 | 53 | SAVE_STACK; |
michael@0 | 54 | |
michael@0 | 55 | silk_assert( psDec->prev_gain_Q16 != 0 ); |
michael@0 | 56 | |
michael@0 | 57 | ALLOC( sLTP, psDec->ltp_mem_length, opus_int16 ); |
michael@0 | 58 | ALLOC( sLTP_Q15, psDec->ltp_mem_length + psDec->frame_length, opus_int32 ); |
michael@0 | 59 | ALLOC( res_Q14, psDec->subfr_length, opus_int32 ); |
michael@0 | 60 | ALLOC( sLPC_Q14, psDec->subfr_length + MAX_LPC_ORDER, opus_int32 ); |
michael@0 | 61 | |
michael@0 | 62 | offset_Q10 = silk_Quantization_Offsets_Q10[ psDec->indices.signalType >> 1 ][ psDec->indices.quantOffsetType ]; |
michael@0 | 63 | |
michael@0 | 64 | if( psDec->indices.NLSFInterpCoef_Q2 < 1 << 2 ) { |
michael@0 | 65 | NLSF_interpolation_flag = 1; |
michael@0 | 66 | } else { |
michael@0 | 67 | NLSF_interpolation_flag = 0; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | /* Decode excitation */ |
michael@0 | 71 | rand_seed = psDec->indices.Seed; |
michael@0 | 72 | for( i = 0; i < psDec->frame_length; i++ ) { |
michael@0 | 73 | rand_seed = silk_RAND( rand_seed ); |
michael@0 | 74 | psDec->exc_Q14[ i ] = silk_LSHIFT( (opus_int32)pulses[ i ], 14 ); |
michael@0 | 75 | if( psDec->exc_Q14[ i ] > 0 ) { |
michael@0 | 76 | psDec->exc_Q14[ i ] -= QUANT_LEVEL_ADJUST_Q10 << 4; |
michael@0 | 77 | } else |
michael@0 | 78 | if( psDec->exc_Q14[ i ] < 0 ) { |
michael@0 | 79 | psDec->exc_Q14[ i ] += QUANT_LEVEL_ADJUST_Q10 << 4; |
michael@0 | 80 | } |
michael@0 | 81 | psDec->exc_Q14[ i ] += offset_Q10 << 4; |
michael@0 | 82 | if( rand_seed < 0 ) { |
michael@0 | 83 | psDec->exc_Q14[ i ] = -psDec->exc_Q14[ i ]; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | rand_seed = silk_ADD32_ovflw( rand_seed, pulses[ i ] ); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | /* Copy LPC state */ |
michael@0 | 90 | silk_memcpy( sLPC_Q14, psDec->sLPC_Q14_buf, MAX_LPC_ORDER * sizeof( opus_int32 ) ); |
michael@0 | 91 | |
michael@0 | 92 | pexc_Q14 = psDec->exc_Q14; |
michael@0 | 93 | pxq = xq; |
michael@0 | 94 | sLTP_buf_idx = psDec->ltp_mem_length; |
michael@0 | 95 | /* Loop over subframes */ |
michael@0 | 96 | for( k = 0; k < psDec->nb_subfr; k++ ) { |
michael@0 | 97 | pres_Q14 = res_Q14; |
michael@0 | 98 | A_Q12 = psDecCtrl->PredCoef_Q12[ k >> 1 ]; |
michael@0 | 99 | |
michael@0 | 100 | /* Preload LPC coeficients to array on stack. Gives small performance gain */ |
michael@0 | 101 | silk_memcpy( A_Q12_tmp, A_Q12, psDec->LPC_order * sizeof( opus_int16 ) ); |
michael@0 | 102 | B_Q14 = &psDecCtrl->LTPCoef_Q14[ k * LTP_ORDER ]; |
michael@0 | 103 | signalType = psDec->indices.signalType; |
michael@0 | 104 | |
michael@0 | 105 | Gain_Q10 = silk_RSHIFT( psDecCtrl->Gains_Q16[ k ], 6 ); |
michael@0 | 106 | inv_gain_Q31 = silk_INVERSE32_varQ( psDecCtrl->Gains_Q16[ k ], 47 ); |
michael@0 | 107 | |
michael@0 | 108 | /* Calculate gain adjustment factor */ |
michael@0 | 109 | if( psDecCtrl->Gains_Q16[ k ] != psDec->prev_gain_Q16 ) { |
michael@0 | 110 | gain_adj_Q16 = silk_DIV32_varQ( psDec->prev_gain_Q16, psDecCtrl->Gains_Q16[ k ], 16 ); |
michael@0 | 111 | |
michael@0 | 112 | /* Scale short term state */ |
michael@0 | 113 | for( i = 0; i < MAX_LPC_ORDER; i++ ) { |
michael@0 | 114 | sLPC_Q14[ i ] = silk_SMULWW( gain_adj_Q16, sLPC_Q14[ i ] ); |
michael@0 | 115 | } |
michael@0 | 116 | } else { |
michael@0 | 117 | gain_adj_Q16 = (opus_int32)1 << 16; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | /* Save inv_gain */ |
michael@0 | 121 | silk_assert( inv_gain_Q31 != 0 ); |
michael@0 | 122 | psDec->prev_gain_Q16 = psDecCtrl->Gains_Q16[ k ]; |
michael@0 | 123 | |
michael@0 | 124 | /* Avoid abrupt transition from voiced PLC to unvoiced normal decoding */ |
michael@0 | 125 | if( psDec->lossCnt && psDec->prevSignalType == TYPE_VOICED && |
michael@0 | 126 | psDec->indices.signalType != TYPE_VOICED && k < MAX_NB_SUBFR/2 ) { |
michael@0 | 127 | |
michael@0 | 128 | silk_memset( B_Q14, 0, LTP_ORDER * sizeof( opus_int16 ) ); |
michael@0 | 129 | B_Q14[ LTP_ORDER/2 ] = SILK_FIX_CONST( 0.25, 14 ); |
michael@0 | 130 | |
michael@0 | 131 | signalType = TYPE_VOICED; |
michael@0 | 132 | psDecCtrl->pitchL[ k ] = psDec->lagPrev; |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | if( signalType == TYPE_VOICED ) { |
michael@0 | 136 | /* Voiced */ |
michael@0 | 137 | lag = psDecCtrl->pitchL[ k ]; |
michael@0 | 138 | |
michael@0 | 139 | /* Re-whitening */ |
michael@0 | 140 | if( k == 0 || ( k == 2 && NLSF_interpolation_flag ) ) { |
michael@0 | 141 | /* Rewhiten with new A coefs */ |
michael@0 | 142 | start_idx = psDec->ltp_mem_length - lag - psDec->LPC_order - LTP_ORDER / 2; |
michael@0 | 143 | silk_assert( start_idx > 0 ); |
michael@0 | 144 | |
michael@0 | 145 | if( k == 2 ) { |
michael@0 | 146 | silk_memcpy( &psDec->outBuf[ psDec->ltp_mem_length ], xq, 2 * psDec->subfr_length * sizeof( opus_int16 ) ); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | silk_LPC_analysis_filter( &sLTP[ start_idx ], &psDec->outBuf[ start_idx + k * psDec->subfr_length ], |
michael@0 | 150 | A_Q12, psDec->ltp_mem_length - start_idx, psDec->LPC_order ); |
michael@0 | 151 | |
michael@0 | 152 | /* After rewhitening the LTP state is unscaled */ |
michael@0 | 153 | if( k == 0 ) { |
michael@0 | 154 | /* Do LTP downscaling to reduce inter-packet dependency */ |
michael@0 | 155 | inv_gain_Q31 = silk_LSHIFT( silk_SMULWB( inv_gain_Q31, psDecCtrl->LTP_scale_Q14 ), 2 ); |
michael@0 | 156 | } |
michael@0 | 157 | for( i = 0; i < lag + LTP_ORDER/2; i++ ) { |
michael@0 | 158 | sLTP_Q15[ sLTP_buf_idx - i - 1 ] = silk_SMULWB( inv_gain_Q31, sLTP[ psDec->ltp_mem_length - i - 1 ] ); |
michael@0 | 159 | } |
michael@0 | 160 | } else { |
michael@0 | 161 | /* Update LTP state when Gain changes */ |
michael@0 | 162 | if( gain_adj_Q16 != (opus_int32)1 << 16 ) { |
michael@0 | 163 | for( i = 0; i < lag + LTP_ORDER/2; i++ ) { |
michael@0 | 164 | sLTP_Q15[ sLTP_buf_idx - i - 1 ] = silk_SMULWW( gain_adj_Q16, sLTP_Q15[ sLTP_buf_idx - i - 1 ] ); |
michael@0 | 165 | } |
michael@0 | 166 | } |
michael@0 | 167 | } |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | /* Long-term prediction */ |
michael@0 | 171 | if( signalType == TYPE_VOICED ) { |
michael@0 | 172 | /* Set up pointer */ |
michael@0 | 173 | pred_lag_ptr = &sLTP_Q15[ sLTP_buf_idx - lag + LTP_ORDER / 2 ]; |
michael@0 | 174 | for( i = 0; i < psDec->subfr_length; i++ ) { |
michael@0 | 175 | /* Unrolled loop */ |
michael@0 | 176 | /* Avoids introducing a bias because silk_SMLAWB() always rounds to -inf */ |
michael@0 | 177 | LTP_pred_Q13 = 2; |
michael@0 | 178 | LTP_pred_Q13 = silk_SMLAWB( LTP_pred_Q13, pred_lag_ptr[ 0 ], B_Q14[ 0 ] ); |
michael@0 | 179 | LTP_pred_Q13 = silk_SMLAWB( LTP_pred_Q13, pred_lag_ptr[ -1 ], B_Q14[ 1 ] ); |
michael@0 | 180 | LTP_pred_Q13 = silk_SMLAWB( LTP_pred_Q13, pred_lag_ptr[ -2 ], B_Q14[ 2 ] ); |
michael@0 | 181 | LTP_pred_Q13 = silk_SMLAWB( LTP_pred_Q13, pred_lag_ptr[ -3 ], B_Q14[ 3 ] ); |
michael@0 | 182 | LTP_pred_Q13 = silk_SMLAWB( LTP_pred_Q13, pred_lag_ptr[ -4 ], B_Q14[ 4 ] ); |
michael@0 | 183 | pred_lag_ptr++; |
michael@0 | 184 | |
michael@0 | 185 | /* Generate LPC excitation */ |
michael@0 | 186 | pres_Q14[ i ] = silk_ADD_LSHIFT32( pexc_Q14[ i ], LTP_pred_Q13, 1 ); |
michael@0 | 187 | |
michael@0 | 188 | /* Update states */ |
michael@0 | 189 | sLTP_Q15[ sLTP_buf_idx ] = silk_LSHIFT( pres_Q14[ i ], 1 ); |
michael@0 | 190 | sLTP_buf_idx++; |
michael@0 | 191 | } |
michael@0 | 192 | } else { |
michael@0 | 193 | pres_Q14 = pexc_Q14; |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | for( i = 0; i < psDec->subfr_length; i++ ) { |
michael@0 | 197 | /* Short-term prediction */ |
michael@0 | 198 | silk_assert( psDec->LPC_order == 10 || psDec->LPC_order == 16 ); |
michael@0 | 199 | /* Avoids introducing a bias because silk_SMLAWB() always rounds to -inf */ |
michael@0 | 200 | LPC_pred_Q10 = silk_RSHIFT( psDec->LPC_order, 1 ); |
michael@0 | 201 | LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 1 ], A_Q12_tmp[ 0 ] ); |
michael@0 | 202 | LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 2 ], A_Q12_tmp[ 1 ] ); |
michael@0 | 203 | LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 3 ], A_Q12_tmp[ 2 ] ); |
michael@0 | 204 | LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 4 ], A_Q12_tmp[ 3 ] ); |
michael@0 | 205 | LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 5 ], A_Q12_tmp[ 4 ] ); |
michael@0 | 206 | LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 6 ], A_Q12_tmp[ 5 ] ); |
michael@0 | 207 | LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 7 ], A_Q12_tmp[ 6 ] ); |
michael@0 | 208 | LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 8 ], A_Q12_tmp[ 7 ] ); |
michael@0 | 209 | LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 9 ], A_Q12_tmp[ 8 ] ); |
michael@0 | 210 | LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 10 ], A_Q12_tmp[ 9 ] ); |
michael@0 | 211 | if( psDec->LPC_order == 16 ) { |
michael@0 | 212 | LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 11 ], A_Q12_tmp[ 10 ] ); |
michael@0 | 213 | LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 12 ], A_Q12_tmp[ 11 ] ); |
michael@0 | 214 | LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 13 ], A_Q12_tmp[ 12 ] ); |
michael@0 | 215 | LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 14 ], A_Q12_tmp[ 13 ] ); |
michael@0 | 216 | LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 15 ], A_Q12_tmp[ 14 ] ); |
michael@0 | 217 | LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 16 ], A_Q12_tmp[ 15 ] ); |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | /* Add prediction to LPC excitation */ |
michael@0 | 221 | sLPC_Q14[ MAX_LPC_ORDER + i ] = silk_ADD_LSHIFT32( pres_Q14[ i ], LPC_pred_Q10, 4 ); |
michael@0 | 222 | |
michael@0 | 223 | /* Scale with gain */ |
michael@0 | 224 | pxq[ i ] = (opus_int16)silk_SAT16( silk_RSHIFT_ROUND( silk_SMULWW( sLPC_Q14[ MAX_LPC_ORDER + i ], Gain_Q10 ), 8 ) ); |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | /* DEBUG_STORE_DATA( dec.pcm, pxq, psDec->subfr_length * sizeof( opus_int16 ) ) */ |
michael@0 | 228 | |
michael@0 | 229 | /* Update LPC filter state */ |
michael@0 | 230 | silk_memcpy( sLPC_Q14, &sLPC_Q14[ psDec->subfr_length ], MAX_LPC_ORDER * sizeof( opus_int32 ) ); |
michael@0 | 231 | pexc_Q14 += psDec->subfr_length; |
michael@0 | 232 | pxq += psDec->subfr_length; |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | /* Save LPC state */ |
michael@0 | 236 | silk_memcpy( psDec->sLPC_Q14_buf, sLPC_Q14, MAX_LPC_ORDER * sizeof( opus_int32 ) ); |
michael@0 | 237 | RESTORE_STACK; |
michael@0 | 238 | } |