media/libopus/silk/PLC.c

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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 #include "PLC.h"
michael@0 35
michael@0 36 #define NB_ATT 2
michael@0 37 static const opus_int16 HARM_ATT_Q15[NB_ATT] = { 32440, 31130 }; /* 0.99, 0.95 */
michael@0 38 static const opus_int16 PLC_RAND_ATTENUATE_V_Q15[NB_ATT] = { 31130, 26214 }; /* 0.95, 0.8 */
michael@0 39 static const opus_int16 PLC_RAND_ATTENUATE_UV_Q15[NB_ATT] = { 32440, 29491 }; /* 0.99, 0.9 */
michael@0 40
michael@0 41 static OPUS_INLINE void silk_PLC_update(
michael@0 42 silk_decoder_state *psDec, /* I/O Decoder state */
michael@0 43 silk_decoder_control *psDecCtrl /* I/O Decoder control */
michael@0 44 );
michael@0 45
michael@0 46 static OPUS_INLINE void silk_PLC_conceal(
michael@0 47 silk_decoder_state *psDec, /* I/O Decoder state */
michael@0 48 silk_decoder_control *psDecCtrl, /* I/O Decoder control */
michael@0 49 opus_int16 frame[] /* O LPC residual signal */
michael@0 50 );
michael@0 51
michael@0 52
michael@0 53 void silk_PLC_Reset(
michael@0 54 silk_decoder_state *psDec /* I/O Decoder state */
michael@0 55 )
michael@0 56 {
michael@0 57 psDec->sPLC.pitchL_Q8 = silk_LSHIFT( psDec->frame_length, 8 - 1 );
michael@0 58 psDec->sPLC.prevGain_Q16[ 0 ] = SILK_FIX_CONST( 1, 16 );
michael@0 59 psDec->sPLC.prevGain_Q16[ 1 ] = SILK_FIX_CONST( 1, 16 );
michael@0 60 psDec->sPLC.subfr_length = 20;
michael@0 61 psDec->sPLC.nb_subfr = 2;
michael@0 62 }
michael@0 63
michael@0 64 void silk_PLC(
michael@0 65 silk_decoder_state *psDec, /* I/O Decoder state */
michael@0 66 silk_decoder_control *psDecCtrl, /* I/O Decoder control */
michael@0 67 opus_int16 frame[], /* I/O signal */
michael@0 68 opus_int lost /* I Loss flag */
michael@0 69 )
michael@0 70 {
michael@0 71 /* PLC control function */
michael@0 72 if( psDec->fs_kHz != psDec->sPLC.fs_kHz ) {
michael@0 73 silk_PLC_Reset( psDec );
michael@0 74 psDec->sPLC.fs_kHz = psDec->fs_kHz;
michael@0 75 }
michael@0 76
michael@0 77 if( lost ) {
michael@0 78 /****************************/
michael@0 79 /* Generate Signal */
michael@0 80 /****************************/
michael@0 81 silk_PLC_conceal( psDec, psDecCtrl, frame );
michael@0 82
michael@0 83 psDec->lossCnt++;
michael@0 84 } else {
michael@0 85 /****************************/
michael@0 86 /* Update state */
michael@0 87 /****************************/
michael@0 88 silk_PLC_update( psDec, psDecCtrl );
michael@0 89 }
michael@0 90 }
michael@0 91
michael@0 92 /**************************************************/
michael@0 93 /* Update state of PLC */
michael@0 94 /**************************************************/
michael@0 95 static OPUS_INLINE void silk_PLC_update(
michael@0 96 silk_decoder_state *psDec, /* I/O Decoder state */
michael@0 97 silk_decoder_control *psDecCtrl /* I/O Decoder control */
michael@0 98 )
michael@0 99 {
michael@0 100 opus_int32 LTP_Gain_Q14, temp_LTP_Gain_Q14;
michael@0 101 opus_int i, j;
michael@0 102 silk_PLC_struct *psPLC;
michael@0 103
michael@0 104 psPLC = &psDec->sPLC;
michael@0 105
michael@0 106 /* Update parameters used in case of packet loss */
michael@0 107 psDec->prevSignalType = psDec->indices.signalType;
michael@0 108 LTP_Gain_Q14 = 0;
michael@0 109 if( psDec->indices.signalType == TYPE_VOICED ) {
michael@0 110 /* Find the parameters for the last subframe which contains a pitch pulse */
michael@0 111 for( j = 0; j * psDec->subfr_length < psDecCtrl->pitchL[ psDec->nb_subfr - 1 ]; j++ ) {
michael@0 112 if( j == psDec->nb_subfr ) {
michael@0 113 break;
michael@0 114 }
michael@0 115 temp_LTP_Gain_Q14 = 0;
michael@0 116 for( i = 0; i < LTP_ORDER; i++ ) {
michael@0 117 temp_LTP_Gain_Q14 += psDecCtrl->LTPCoef_Q14[ ( psDec->nb_subfr - 1 - j ) * LTP_ORDER + i ];
michael@0 118 }
michael@0 119 if( temp_LTP_Gain_Q14 > LTP_Gain_Q14 ) {
michael@0 120 LTP_Gain_Q14 = temp_LTP_Gain_Q14;
michael@0 121 silk_memcpy( psPLC->LTPCoef_Q14,
michael@0 122 &psDecCtrl->LTPCoef_Q14[ silk_SMULBB( psDec->nb_subfr - 1 - j, LTP_ORDER ) ],
michael@0 123 LTP_ORDER * sizeof( opus_int16 ) );
michael@0 124
michael@0 125 psPLC->pitchL_Q8 = silk_LSHIFT( psDecCtrl->pitchL[ psDec->nb_subfr - 1 - j ], 8 );
michael@0 126 }
michael@0 127 }
michael@0 128
michael@0 129 silk_memset( psPLC->LTPCoef_Q14, 0, LTP_ORDER * sizeof( opus_int16 ) );
michael@0 130 psPLC->LTPCoef_Q14[ LTP_ORDER / 2 ] = LTP_Gain_Q14;
michael@0 131
michael@0 132 /* Limit LT coefs */
michael@0 133 if( LTP_Gain_Q14 < V_PITCH_GAIN_START_MIN_Q14 ) {
michael@0 134 opus_int scale_Q10;
michael@0 135 opus_int32 tmp;
michael@0 136
michael@0 137 tmp = silk_LSHIFT( V_PITCH_GAIN_START_MIN_Q14, 10 );
michael@0 138 scale_Q10 = silk_DIV32( tmp, silk_max( LTP_Gain_Q14, 1 ) );
michael@0 139 for( i = 0; i < LTP_ORDER; i++ ) {
michael@0 140 psPLC->LTPCoef_Q14[ i ] = silk_RSHIFT( silk_SMULBB( psPLC->LTPCoef_Q14[ i ], scale_Q10 ), 10 );
michael@0 141 }
michael@0 142 } else if( LTP_Gain_Q14 > V_PITCH_GAIN_START_MAX_Q14 ) {
michael@0 143 opus_int scale_Q14;
michael@0 144 opus_int32 tmp;
michael@0 145
michael@0 146 tmp = silk_LSHIFT( V_PITCH_GAIN_START_MAX_Q14, 14 );
michael@0 147 scale_Q14 = silk_DIV32( tmp, silk_max( LTP_Gain_Q14, 1 ) );
michael@0 148 for( i = 0; i < LTP_ORDER; i++ ) {
michael@0 149 psPLC->LTPCoef_Q14[ i ] = silk_RSHIFT( silk_SMULBB( psPLC->LTPCoef_Q14[ i ], scale_Q14 ), 14 );
michael@0 150 }
michael@0 151 }
michael@0 152 } else {
michael@0 153 psPLC->pitchL_Q8 = silk_LSHIFT( silk_SMULBB( psDec->fs_kHz, 18 ), 8 );
michael@0 154 silk_memset( psPLC->LTPCoef_Q14, 0, LTP_ORDER * sizeof( opus_int16 ));
michael@0 155 }
michael@0 156
michael@0 157 /* Save LPC coeficients */
michael@0 158 silk_memcpy( psPLC->prevLPC_Q12, psDecCtrl->PredCoef_Q12[ 1 ], psDec->LPC_order * sizeof( opus_int16 ) );
michael@0 159 psPLC->prevLTP_scale_Q14 = psDecCtrl->LTP_scale_Q14;
michael@0 160
michael@0 161 /* Save last two gains */
michael@0 162 silk_memcpy( psPLC->prevGain_Q16, &psDecCtrl->Gains_Q16[ psDec->nb_subfr - 2 ], 2 * sizeof( opus_int32 ) );
michael@0 163
michael@0 164 psPLC->subfr_length = psDec->subfr_length;
michael@0 165 psPLC->nb_subfr = psDec->nb_subfr;
michael@0 166 }
michael@0 167
michael@0 168 static OPUS_INLINE void silk_PLC_conceal(
michael@0 169 silk_decoder_state *psDec, /* I/O Decoder state */
michael@0 170 silk_decoder_control *psDecCtrl, /* I/O Decoder control */
michael@0 171 opus_int16 frame[] /* O LPC residual signal */
michael@0 172 )
michael@0 173 {
michael@0 174 opus_int i, j, k;
michael@0 175 opus_int lag, idx, sLTP_buf_idx, shift1, shift2;
michael@0 176 opus_int32 rand_seed, harm_Gain_Q15, rand_Gain_Q15, inv_gain_Q30;
michael@0 177 opus_int32 energy1, energy2, *rand_ptr, *pred_lag_ptr;
michael@0 178 opus_int32 LPC_pred_Q10, LTP_pred_Q12;
michael@0 179 opus_int16 rand_scale_Q14;
michael@0 180 opus_int16 *B_Q14, *exc_buf_ptr;
michael@0 181 opus_int32 *sLPC_Q14_ptr;
michael@0 182 VARDECL( opus_int16, exc_buf );
michael@0 183 opus_int16 A_Q12[ MAX_LPC_ORDER ];
michael@0 184 VARDECL( opus_int16, sLTP );
michael@0 185 VARDECL( opus_int32, sLTP_Q14 );
michael@0 186 silk_PLC_struct *psPLC = &psDec->sPLC;
michael@0 187 opus_int32 prevGain_Q10[2];
michael@0 188 SAVE_STACK;
michael@0 189
michael@0 190 ALLOC( exc_buf, 2*psPLC->subfr_length, opus_int16 );
michael@0 191 ALLOC( sLTP, psDec->ltp_mem_length, opus_int16 );
michael@0 192 ALLOC( sLTP_Q14, psDec->ltp_mem_length + psDec->frame_length, opus_int32 );
michael@0 193
michael@0 194 prevGain_Q10[0] = silk_RSHIFT( psPLC->prevGain_Q16[ 0 ], 6);
michael@0 195 prevGain_Q10[1] = silk_RSHIFT( psPLC->prevGain_Q16[ 1 ], 6);
michael@0 196
michael@0 197 if( psDec->first_frame_after_reset ) {
michael@0 198 silk_memset( psPLC->prevLPC_Q12, 0, sizeof( psPLC->prevLPC_Q12 ) );
michael@0 199 }
michael@0 200
michael@0 201 /* Find random noise component */
michael@0 202 /* Scale previous excitation signal */
michael@0 203 exc_buf_ptr = exc_buf;
michael@0 204 for( k = 0; k < 2; k++ ) {
michael@0 205 for( i = 0; i < psPLC->subfr_length; i++ ) {
michael@0 206 exc_buf_ptr[ i ] = (opus_int16)silk_SAT16( silk_RSHIFT(
michael@0 207 silk_SMULWW( psDec->exc_Q14[ i + ( k + psPLC->nb_subfr - 2 ) * psPLC->subfr_length ], prevGain_Q10[ k ] ), 8 ) );
michael@0 208 }
michael@0 209 exc_buf_ptr += psPLC->subfr_length;
michael@0 210 }
michael@0 211 /* Find the subframe with lowest energy of the last two and use that as random noise generator */
michael@0 212 silk_sum_sqr_shift( &energy1, &shift1, exc_buf, psPLC->subfr_length );
michael@0 213 silk_sum_sqr_shift( &energy2, &shift2, &exc_buf[ psPLC->subfr_length ], psPLC->subfr_length );
michael@0 214
michael@0 215 if( silk_RSHIFT( energy1, shift2 ) < silk_RSHIFT( energy2, shift1 ) ) {
michael@0 216 /* First sub-frame has lowest energy */
michael@0 217 rand_ptr = &psDec->exc_Q14[ silk_max_int( 0, ( psPLC->nb_subfr - 1 ) * psPLC->subfr_length - RAND_BUF_SIZE ) ];
michael@0 218 } else {
michael@0 219 /* Second sub-frame has lowest energy */
michael@0 220 rand_ptr = &psDec->exc_Q14[ silk_max_int( 0, psPLC->nb_subfr * psPLC->subfr_length - RAND_BUF_SIZE ) ];
michael@0 221 }
michael@0 222
michael@0 223 /* Set up Gain to random noise component */
michael@0 224 B_Q14 = psPLC->LTPCoef_Q14;
michael@0 225 rand_scale_Q14 = psPLC->randScale_Q14;
michael@0 226
michael@0 227 /* Set up attenuation gains */
michael@0 228 harm_Gain_Q15 = HARM_ATT_Q15[ silk_min_int( NB_ATT - 1, psDec->lossCnt ) ];
michael@0 229 if( psDec->prevSignalType == TYPE_VOICED ) {
michael@0 230 rand_Gain_Q15 = PLC_RAND_ATTENUATE_V_Q15[ silk_min_int( NB_ATT - 1, psDec->lossCnt ) ];
michael@0 231 } else {
michael@0 232 rand_Gain_Q15 = PLC_RAND_ATTENUATE_UV_Q15[ silk_min_int( NB_ATT - 1, psDec->lossCnt ) ];
michael@0 233 }
michael@0 234
michael@0 235 /* LPC concealment. Apply BWE to previous LPC */
michael@0 236 silk_bwexpander( psPLC->prevLPC_Q12, psDec->LPC_order, SILK_FIX_CONST( BWE_COEF, 16 ) );
michael@0 237
michael@0 238 /* Preload LPC coeficients to array on stack. Gives small performance gain */
michael@0 239 silk_memcpy( A_Q12, psPLC->prevLPC_Q12, psDec->LPC_order * sizeof( opus_int16 ) );
michael@0 240
michael@0 241 /* First Lost frame */
michael@0 242 if( psDec->lossCnt == 0 ) {
michael@0 243 rand_scale_Q14 = 1 << 14;
michael@0 244
michael@0 245 /* Reduce random noise Gain for voiced frames */
michael@0 246 if( psDec->prevSignalType == TYPE_VOICED ) {
michael@0 247 for( i = 0; i < LTP_ORDER; i++ ) {
michael@0 248 rand_scale_Q14 -= B_Q14[ i ];
michael@0 249 }
michael@0 250 rand_scale_Q14 = silk_max_16( 3277, rand_scale_Q14 ); /* 0.2 */
michael@0 251 rand_scale_Q14 = (opus_int16)silk_RSHIFT( silk_SMULBB( rand_scale_Q14, psPLC->prevLTP_scale_Q14 ), 14 );
michael@0 252 } else {
michael@0 253 /* Reduce random noise for unvoiced frames with high LPC gain */
michael@0 254 opus_int32 invGain_Q30, down_scale_Q30;
michael@0 255
michael@0 256 invGain_Q30 = silk_LPC_inverse_pred_gain( psPLC->prevLPC_Q12, psDec->LPC_order );
michael@0 257
michael@0 258 down_scale_Q30 = silk_min_32( silk_RSHIFT( (opus_int32)1 << 30, LOG2_INV_LPC_GAIN_HIGH_THRES ), invGain_Q30 );
michael@0 259 down_scale_Q30 = silk_max_32( silk_RSHIFT( (opus_int32)1 << 30, LOG2_INV_LPC_GAIN_LOW_THRES ), down_scale_Q30 );
michael@0 260 down_scale_Q30 = silk_LSHIFT( down_scale_Q30, LOG2_INV_LPC_GAIN_HIGH_THRES );
michael@0 261
michael@0 262 rand_Gain_Q15 = silk_RSHIFT( silk_SMULWB( down_scale_Q30, rand_Gain_Q15 ), 14 );
michael@0 263 }
michael@0 264 }
michael@0 265
michael@0 266 rand_seed = psPLC->rand_seed;
michael@0 267 lag = silk_RSHIFT_ROUND( psPLC->pitchL_Q8, 8 );
michael@0 268 sLTP_buf_idx = psDec->ltp_mem_length;
michael@0 269
michael@0 270 /* Rewhiten LTP state */
michael@0 271 idx = psDec->ltp_mem_length - lag - psDec->LPC_order - LTP_ORDER / 2;
michael@0 272 silk_assert( idx > 0 );
michael@0 273 silk_LPC_analysis_filter( &sLTP[ idx ], &psDec->outBuf[ idx ], A_Q12, psDec->ltp_mem_length - idx, psDec->LPC_order );
michael@0 274 /* Scale LTP state */
michael@0 275 inv_gain_Q30 = silk_INVERSE32_varQ( psPLC->prevGain_Q16[ 1 ], 46 );
michael@0 276 inv_gain_Q30 = silk_min( inv_gain_Q30, silk_int32_MAX >> 1 );
michael@0 277 for( i = idx + psDec->LPC_order; i < psDec->ltp_mem_length; i++ ) {
michael@0 278 sLTP_Q14[ i ] = silk_SMULWB( inv_gain_Q30, sLTP[ i ] );
michael@0 279 }
michael@0 280
michael@0 281 /***************************/
michael@0 282 /* LTP synthesis filtering */
michael@0 283 /***************************/
michael@0 284 for( k = 0; k < psDec->nb_subfr; k++ ) {
michael@0 285 /* Set up pointer */
michael@0 286 pred_lag_ptr = &sLTP_Q14[ sLTP_buf_idx - lag + LTP_ORDER / 2 ];
michael@0 287 for( i = 0; i < psDec->subfr_length; i++ ) {
michael@0 288 /* Unrolled loop */
michael@0 289 /* Avoids introducing a bias because silk_SMLAWB() always rounds to -inf */
michael@0 290 LTP_pred_Q12 = 2;
michael@0 291 LTP_pred_Q12 = silk_SMLAWB( LTP_pred_Q12, pred_lag_ptr[ 0 ], B_Q14[ 0 ] );
michael@0 292 LTP_pred_Q12 = silk_SMLAWB( LTP_pred_Q12, pred_lag_ptr[ -1 ], B_Q14[ 1 ] );
michael@0 293 LTP_pred_Q12 = silk_SMLAWB( LTP_pred_Q12, pred_lag_ptr[ -2 ], B_Q14[ 2 ] );
michael@0 294 LTP_pred_Q12 = silk_SMLAWB( LTP_pred_Q12, pred_lag_ptr[ -3 ], B_Q14[ 3 ] );
michael@0 295 LTP_pred_Q12 = silk_SMLAWB( LTP_pred_Q12, pred_lag_ptr[ -4 ], B_Q14[ 4 ] );
michael@0 296 pred_lag_ptr++;
michael@0 297
michael@0 298 /* Generate LPC excitation */
michael@0 299 rand_seed = silk_RAND( rand_seed );
michael@0 300 idx = silk_RSHIFT( rand_seed, 25 ) & RAND_BUF_MASK;
michael@0 301 sLTP_Q14[ sLTP_buf_idx ] = silk_LSHIFT32( silk_SMLAWB( LTP_pred_Q12, rand_ptr[ idx ], rand_scale_Q14 ), 2 );
michael@0 302 sLTP_buf_idx++;
michael@0 303 }
michael@0 304
michael@0 305 /* Gradually reduce LTP gain */
michael@0 306 for( j = 0; j < LTP_ORDER; j++ ) {
michael@0 307 B_Q14[ j ] = silk_RSHIFT( silk_SMULBB( harm_Gain_Q15, B_Q14[ j ] ), 15 );
michael@0 308 }
michael@0 309 /* Gradually reduce excitation gain */
michael@0 310 rand_scale_Q14 = silk_RSHIFT( silk_SMULBB( rand_scale_Q14, rand_Gain_Q15 ), 15 );
michael@0 311
michael@0 312 /* Slowly increase pitch lag */
michael@0 313 psPLC->pitchL_Q8 = silk_SMLAWB( psPLC->pitchL_Q8, psPLC->pitchL_Q8, PITCH_DRIFT_FAC_Q16 );
michael@0 314 psPLC->pitchL_Q8 = silk_min_32( psPLC->pitchL_Q8, silk_LSHIFT( silk_SMULBB( MAX_PITCH_LAG_MS, psDec->fs_kHz ), 8 ) );
michael@0 315 lag = silk_RSHIFT_ROUND( psPLC->pitchL_Q8, 8 );
michael@0 316 }
michael@0 317
michael@0 318 /***************************/
michael@0 319 /* LPC synthesis filtering */
michael@0 320 /***************************/
michael@0 321 sLPC_Q14_ptr = &sLTP_Q14[ psDec->ltp_mem_length - MAX_LPC_ORDER ];
michael@0 322
michael@0 323 /* Copy LPC state */
michael@0 324 silk_memcpy( sLPC_Q14_ptr, psDec->sLPC_Q14_buf, MAX_LPC_ORDER * sizeof( opus_int32 ) );
michael@0 325
michael@0 326 silk_assert( psDec->LPC_order >= 10 ); /* check that unrolling works */
michael@0 327 for( i = 0; i < psDec->frame_length; i++ ) {
michael@0 328 /* partly unrolled */
michael@0 329 /* Avoids introducing a bias because silk_SMLAWB() always rounds to -inf */
michael@0 330 LPC_pred_Q10 = silk_RSHIFT( psDec->LPC_order, 1 );
michael@0 331 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - 1 ], A_Q12[ 0 ] );
michael@0 332 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - 2 ], A_Q12[ 1 ] );
michael@0 333 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - 3 ], A_Q12[ 2 ] );
michael@0 334 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - 4 ], A_Q12[ 3 ] );
michael@0 335 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - 5 ], A_Q12[ 4 ] );
michael@0 336 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - 6 ], A_Q12[ 5 ] );
michael@0 337 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - 7 ], A_Q12[ 6 ] );
michael@0 338 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - 8 ], A_Q12[ 7 ] );
michael@0 339 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - 9 ], A_Q12[ 8 ] );
michael@0 340 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - 10 ], A_Q12[ 9 ] );
michael@0 341 for( j = 10; j < psDec->LPC_order; j++ ) {
michael@0 342 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - j - 1 ], A_Q12[ j ] );
michael@0 343 }
michael@0 344
michael@0 345 /* Add prediction to LPC excitation */
michael@0 346 sLPC_Q14_ptr[ MAX_LPC_ORDER + i ] = silk_ADD_LSHIFT32( sLPC_Q14_ptr[ MAX_LPC_ORDER + i ], LPC_pred_Q10, 4 );
michael@0 347
michael@0 348 /* Scale with Gain */
michael@0 349 frame[ i ] = (opus_int16)silk_SAT16( silk_SAT16( silk_RSHIFT_ROUND( silk_SMULWW( sLPC_Q14_ptr[ MAX_LPC_ORDER + i ], prevGain_Q10[ 1 ] ), 8 ) ) );
michael@0 350 }
michael@0 351
michael@0 352 /* Save LPC state */
michael@0 353 silk_memcpy( psDec->sLPC_Q14_buf, &sLPC_Q14_ptr[ psDec->frame_length ], MAX_LPC_ORDER * sizeof( opus_int32 ) );
michael@0 354
michael@0 355 /**************************************/
michael@0 356 /* Update states */
michael@0 357 /**************************************/
michael@0 358 psPLC->rand_seed = rand_seed;
michael@0 359 psPLC->randScale_Q14 = rand_scale_Q14;
michael@0 360 for( i = 0; i < MAX_NB_SUBFR; i++ ) {
michael@0 361 psDecCtrl->pitchL[ i ] = lag;
michael@0 362 }
michael@0 363 RESTORE_STACK;
michael@0 364 }
michael@0 365
michael@0 366 /* Glues concealed frames with new good received frames */
michael@0 367 void silk_PLC_glue_frames(
michael@0 368 silk_decoder_state *psDec, /* I/O decoder state */
michael@0 369 opus_int16 frame[], /* I/O signal */
michael@0 370 opus_int length /* I length of signal */
michael@0 371 )
michael@0 372 {
michael@0 373 opus_int i, energy_shift;
michael@0 374 opus_int32 energy;
michael@0 375 silk_PLC_struct *psPLC;
michael@0 376 psPLC = &psDec->sPLC;
michael@0 377
michael@0 378 if( psDec->lossCnt ) {
michael@0 379 /* Calculate energy in concealed residual */
michael@0 380 silk_sum_sqr_shift( &psPLC->conc_energy, &psPLC->conc_energy_shift, frame, length );
michael@0 381
michael@0 382 psPLC->last_frame_lost = 1;
michael@0 383 } else {
michael@0 384 if( psDec->sPLC.last_frame_lost ) {
michael@0 385 /* Calculate residual in decoded signal if last frame was lost */
michael@0 386 silk_sum_sqr_shift( &energy, &energy_shift, frame, length );
michael@0 387
michael@0 388 /* Normalize energies */
michael@0 389 if( energy_shift > psPLC->conc_energy_shift ) {
michael@0 390 psPLC->conc_energy = silk_RSHIFT( psPLC->conc_energy, energy_shift - psPLC->conc_energy_shift );
michael@0 391 } else if( energy_shift < psPLC->conc_energy_shift ) {
michael@0 392 energy = silk_RSHIFT( energy, psPLC->conc_energy_shift - energy_shift );
michael@0 393 }
michael@0 394
michael@0 395 /* Fade in the energy difference */
michael@0 396 if( energy > psPLC->conc_energy ) {
michael@0 397 opus_int32 frac_Q24, LZ;
michael@0 398 opus_int32 gain_Q16, slope_Q16;
michael@0 399
michael@0 400 LZ = silk_CLZ32( psPLC->conc_energy );
michael@0 401 LZ = LZ - 1;
michael@0 402 psPLC->conc_energy = silk_LSHIFT( psPLC->conc_energy, LZ );
michael@0 403 energy = silk_RSHIFT( energy, silk_max_32( 24 - LZ, 0 ) );
michael@0 404
michael@0 405 frac_Q24 = silk_DIV32( psPLC->conc_energy, silk_max( energy, 1 ) );
michael@0 406
michael@0 407 gain_Q16 = silk_LSHIFT( silk_SQRT_APPROX( frac_Q24 ), 4 );
michael@0 408 slope_Q16 = silk_DIV32_16( ( (opus_int32)1 << 16 ) - gain_Q16, length );
michael@0 409 /* Make slope 4x steeper to avoid missing onsets after DTX */
michael@0 410 slope_Q16 = silk_LSHIFT( slope_Q16, 2 );
michael@0 411
michael@0 412 for( i = 0; i < length; i++ ) {
michael@0 413 frame[ i ] = silk_SMULWB( gain_Q16, frame[ i ] );
michael@0 414 gain_Q16 += slope_Q16;
michael@0 415 if( gain_Q16 > (opus_int32)1 << 16 ) {
michael@0 416 break;
michael@0 417 }
michael@0 418 }
michael@0 419 }
michael@0 420 }
michael@0 421 psPLC->last_frame_lost = 0;
michael@0 422 }
michael@0 423 }

mercurial