annotate src/opus-1.3/silk/PLC.c @ 77:4edcd14160a5 pa_catalina

Duplicate for patch testing
author Chris Cannam
date Wed, 30 Oct 2019 11:25:10 +0000
parents 7aeed7906520
children
rev   line source
Chris@69 1 /***********************************************************************
Chris@69 2 Copyright (c) 2006-2011, Skype Limited. All rights reserved.
Chris@69 3 Redistribution and use in source and binary forms, with or without
Chris@69 4 modification, are permitted provided that the following conditions
Chris@69 5 are met:
Chris@69 6 - Redistributions of source code must retain the above copyright notice,
Chris@69 7 this list of conditions and the following disclaimer.
Chris@69 8 - Redistributions in binary form must reproduce the above copyright
Chris@69 9 notice, this list of conditions and the following disclaimer in the
Chris@69 10 documentation and/or other materials provided with the distribution.
Chris@69 11 - Neither the name of Internet Society, IETF or IETF Trust, nor the
Chris@69 12 names of specific contributors, may be used to endorse or promote
Chris@69 13 products derived from this software without specific prior written
Chris@69 14 permission.
Chris@69 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Chris@69 16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Chris@69 17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Chris@69 18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Chris@69 19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Chris@69 20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Chris@69 21 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Chris@69 22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Chris@69 23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Chris@69 24 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Chris@69 25 POSSIBILITY OF SUCH DAMAGE.
Chris@69 26 ***********************************************************************/
Chris@69 27
Chris@69 28 #ifdef HAVE_CONFIG_H
Chris@69 29 #include "config.h"
Chris@69 30 #endif
Chris@69 31
Chris@69 32 #include "main.h"
Chris@69 33 #include "stack_alloc.h"
Chris@69 34 #include "PLC.h"
Chris@69 35
Chris@69 36 #define NB_ATT 2
Chris@69 37 static const opus_int16 HARM_ATT_Q15[NB_ATT] = { 32440, 31130 }; /* 0.99, 0.95 */
Chris@69 38 static const opus_int16 PLC_RAND_ATTENUATE_V_Q15[NB_ATT] = { 31130, 26214 }; /* 0.95, 0.8 */
Chris@69 39 static const opus_int16 PLC_RAND_ATTENUATE_UV_Q15[NB_ATT] = { 32440, 29491 }; /* 0.99, 0.9 */
Chris@69 40
Chris@69 41 static OPUS_INLINE void silk_PLC_update(
Chris@69 42 silk_decoder_state *psDec, /* I/O Decoder state */
Chris@69 43 silk_decoder_control *psDecCtrl /* I/O Decoder control */
Chris@69 44 );
Chris@69 45
Chris@69 46 static OPUS_INLINE void silk_PLC_conceal(
Chris@69 47 silk_decoder_state *psDec, /* I/O Decoder state */
Chris@69 48 silk_decoder_control *psDecCtrl, /* I/O Decoder control */
Chris@69 49 opus_int16 frame[], /* O LPC residual signal */
Chris@69 50 int arch /* I Run-time architecture */
Chris@69 51 );
Chris@69 52
Chris@69 53
Chris@69 54 void silk_PLC_Reset(
Chris@69 55 silk_decoder_state *psDec /* I/O Decoder state */
Chris@69 56 )
Chris@69 57 {
Chris@69 58 psDec->sPLC.pitchL_Q8 = silk_LSHIFT( psDec->frame_length, 8 - 1 );
Chris@69 59 psDec->sPLC.prevGain_Q16[ 0 ] = SILK_FIX_CONST( 1, 16 );
Chris@69 60 psDec->sPLC.prevGain_Q16[ 1 ] = SILK_FIX_CONST( 1, 16 );
Chris@69 61 psDec->sPLC.subfr_length = 20;
Chris@69 62 psDec->sPLC.nb_subfr = 2;
Chris@69 63 }
Chris@69 64
Chris@69 65 void silk_PLC(
Chris@69 66 silk_decoder_state *psDec, /* I/O Decoder state */
Chris@69 67 silk_decoder_control *psDecCtrl, /* I/O Decoder control */
Chris@69 68 opus_int16 frame[], /* I/O signal */
Chris@69 69 opus_int lost, /* I Loss flag */
Chris@69 70 int arch /* I Run-time architecture */
Chris@69 71 )
Chris@69 72 {
Chris@69 73 /* PLC control function */
Chris@69 74 if( psDec->fs_kHz != psDec->sPLC.fs_kHz ) {
Chris@69 75 silk_PLC_Reset( psDec );
Chris@69 76 psDec->sPLC.fs_kHz = psDec->fs_kHz;
Chris@69 77 }
Chris@69 78
Chris@69 79 if( lost ) {
Chris@69 80 /****************************/
Chris@69 81 /* Generate Signal */
Chris@69 82 /****************************/
Chris@69 83 silk_PLC_conceal( psDec, psDecCtrl, frame, arch );
Chris@69 84
Chris@69 85 psDec->lossCnt++;
Chris@69 86 } else {
Chris@69 87 /****************************/
Chris@69 88 /* Update state */
Chris@69 89 /****************************/
Chris@69 90 silk_PLC_update( psDec, psDecCtrl );
Chris@69 91 }
Chris@69 92 }
Chris@69 93
Chris@69 94 /**************************************************/
Chris@69 95 /* Update state of PLC */
Chris@69 96 /**************************************************/
Chris@69 97 static OPUS_INLINE void silk_PLC_update(
Chris@69 98 silk_decoder_state *psDec, /* I/O Decoder state */
Chris@69 99 silk_decoder_control *psDecCtrl /* I/O Decoder control */
Chris@69 100 )
Chris@69 101 {
Chris@69 102 opus_int32 LTP_Gain_Q14, temp_LTP_Gain_Q14;
Chris@69 103 opus_int i, j;
Chris@69 104 silk_PLC_struct *psPLC;
Chris@69 105
Chris@69 106 psPLC = &psDec->sPLC;
Chris@69 107
Chris@69 108 /* Update parameters used in case of packet loss */
Chris@69 109 psDec->prevSignalType = psDec->indices.signalType;
Chris@69 110 LTP_Gain_Q14 = 0;
Chris@69 111 if( psDec->indices.signalType == TYPE_VOICED ) {
Chris@69 112 /* Find the parameters for the last subframe which contains a pitch pulse */
Chris@69 113 for( j = 0; j * psDec->subfr_length < psDecCtrl->pitchL[ psDec->nb_subfr - 1 ]; j++ ) {
Chris@69 114 if( j == psDec->nb_subfr ) {
Chris@69 115 break;
Chris@69 116 }
Chris@69 117 temp_LTP_Gain_Q14 = 0;
Chris@69 118 for( i = 0; i < LTP_ORDER; i++ ) {
Chris@69 119 temp_LTP_Gain_Q14 += psDecCtrl->LTPCoef_Q14[ ( psDec->nb_subfr - 1 - j ) * LTP_ORDER + i ];
Chris@69 120 }
Chris@69 121 if( temp_LTP_Gain_Q14 > LTP_Gain_Q14 ) {
Chris@69 122 LTP_Gain_Q14 = temp_LTP_Gain_Q14;
Chris@69 123 silk_memcpy( psPLC->LTPCoef_Q14,
Chris@69 124 &psDecCtrl->LTPCoef_Q14[ silk_SMULBB( psDec->nb_subfr - 1 - j, LTP_ORDER ) ],
Chris@69 125 LTP_ORDER * sizeof( opus_int16 ) );
Chris@69 126
Chris@69 127 psPLC->pitchL_Q8 = silk_LSHIFT( psDecCtrl->pitchL[ psDec->nb_subfr - 1 - j ], 8 );
Chris@69 128 }
Chris@69 129 }
Chris@69 130
Chris@69 131 silk_memset( psPLC->LTPCoef_Q14, 0, LTP_ORDER * sizeof( opus_int16 ) );
Chris@69 132 psPLC->LTPCoef_Q14[ LTP_ORDER / 2 ] = LTP_Gain_Q14;
Chris@69 133
Chris@69 134 /* Limit LT coefs */
Chris@69 135 if( LTP_Gain_Q14 < V_PITCH_GAIN_START_MIN_Q14 ) {
Chris@69 136 opus_int scale_Q10;
Chris@69 137 opus_int32 tmp;
Chris@69 138
Chris@69 139 tmp = silk_LSHIFT( V_PITCH_GAIN_START_MIN_Q14, 10 );
Chris@69 140 scale_Q10 = silk_DIV32( tmp, silk_max( LTP_Gain_Q14, 1 ) );
Chris@69 141 for( i = 0; i < LTP_ORDER; i++ ) {
Chris@69 142 psPLC->LTPCoef_Q14[ i ] = silk_RSHIFT( silk_SMULBB( psPLC->LTPCoef_Q14[ i ], scale_Q10 ), 10 );
Chris@69 143 }
Chris@69 144 } else if( LTP_Gain_Q14 > V_PITCH_GAIN_START_MAX_Q14 ) {
Chris@69 145 opus_int scale_Q14;
Chris@69 146 opus_int32 tmp;
Chris@69 147
Chris@69 148 tmp = silk_LSHIFT( V_PITCH_GAIN_START_MAX_Q14, 14 );
Chris@69 149 scale_Q14 = silk_DIV32( tmp, silk_max( LTP_Gain_Q14, 1 ) );
Chris@69 150 for( i = 0; i < LTP_ORDER; i++ ) {
Chris@69 151 psPLC->LTPCoef_Q14[ i ] = silk_RSHIFT( silk_SMULBB( psPLC->LTPCoef_Q14[ i ], scale_Q14 ), 14 );
Chris@69 152 }
Chris@69 153 }
Chris@69 154 } else {
Chris@69 155 psPLC->pitchL_Q8 = silk_LSHIFT( silk_SMULBB( psDec->fs_kHz, 18 ), 8 );
Chris@69 156 silk_memset( psPLC->LTPCoef_Q14, 0, LTP_ORDER * sizeof( opus_int16 ));
Chris@69 157 }
Chris@69 158
Chris@69 159 /* Save LPC coeficients */
Chris@69 160 silk_memcpy( psPLC->prevLPC_Q12, psDecCtrl->PredCoef_Q12[ 1 ], psDec->LPC_order * sizeof( opus_int16 ) );
Chris@69 161 psPLC->prevLTP_scale_Q14 = psDecCtrl->LTP_scale_Q14;
Chris@69 162
Chris@69 163 /* Save last two gains */
Chris@69 164 silk_memcpy( psPLC->prevGain_Q16, &psDecCtrl->Gains_Q16[ psDec->nb_subfr - 2 ], 2 * sizeof( opus_int32 ) );
Chris@69 165
Chris@69 166 psPLC->subfr_length = psDec->subfr_length;
Chris@69 167 psPLC->nb_subfr = psDec->nb_subfr;
Chris@69 168 }
Chris@69 169
Chris@69 170 static OPUS_INLINE void silk_PLC_energy(opus_int32 *energy1, opus_int *shift1, opus_int32 *energy2, opus_int *shift2,
Chris@69 171 const opus_int32 *exc_Q14, const opus_int32 *prevGain_Q10, int subfr_length, int nb_subfr)
Chris@69 172 {
Chris@69 173 int i, k;
Chris@69 174 VARDECL( opus_int16, exc_buf );
Chris@69 175 opus_int16 *exc_buf_ptr;
Chris@69 176 SAVE_STACK;
Chris@69 177 ALLOC( exc_buf, 2*subfr_length, opus_int16 );
Chris@69 178 /* Find random noise component */
Chris@69 179 /* Scale previous excitation signal */
Chris@69 180 exc_buf_ptr = exc_buf;
Chris@69 181 for( k = 0; k < 2; k++ ) {
Chris@69 182 for( i = 0; i < subfr_length; i++ ) {
Chris@69 183 exc_buf_ptr[ i ] = (opus_int16)silk_SAT16( silk_RSHIFT(
Chris@69 184 silk_SMULWW( exc_Q14[ i + ( k + nb_subfr - 2 ) * subfr_length ], prevGain_Q10[ k ] ), 8 ) );
Chris@69 185 }
Chris@69 186 exc_buf_ptr += subfr_length;
Chris@69 187 }
Chris@69 188 /* Find the subframe with lowest energy of the last two and use that as random noise generator */
Chris@69 189 silk_sum_sqr_shift( energy1, shift1, exc_buf, subfr_length );
Chris@69 190 silk_sum_sqr_shift( energy2, shift2, &exc_buf[ subfr_length ], subfr_length );
Chris@69 191 RESTORE_STACK;
Chris@69 192 }
Chris@69 193
Chris@69 194 static OPUS_INLINE void silk_PLC_conceal(
Chris@69 195 silk_decoder_state *psDec, /* I/O Decoder state */
Chris@69 196 silk_decoder_control *psDecCtrl, /* I/O Decoder control */
Chris@69 197 opus_int16 frame[], /* O LPC residual signal */
Chris@69 198 int arch /* I Run-time architecture */
Chris@69 199 )
Chris@69 200 {
Chris@69 201 opus_int i, j, k;
Chris@69 202 opus_int lag, idx, sLTP_buf_idx, shift1, shift2;
Chris@69 203 opus_int32 rand_seed, harm_Gain_Q15, rand_Gain_Q15, inv_gain_Q30;
Chris@69 204 opus_int32 energy1, energy2, *rand_ptr, *pred_lag_ptr;
Chris@69 205 opus_int32 LPC_pred_Q10, LTP_pred_Q12;
Chris@69 206 opus_int16 rand_scale_Q14;
Chris@69 207 opus_int16 *B_Q14;
Chris@69 208 opus_int32 *sLPC_Q14_ptr;
Chris@69 209 opus_int16 A_Q12[ MAX_LPC_ORDER ];
Chris@69 210 #ifdef SMALL_FOOTPRINT
Chris@69 211 opus_int16 *sLTP;
Chris@69 212 #else
Chris@69 213 VARDECL( opus_int16, sLTP );
Chris@69 214 #endif
Chris@69 215 VARDECL( opus_int32, sLTP_Q14 );
Chris@69 216 silk_PLC_struct *psPLC = &psDec->sPLC;
Chris@69 217 opus_int32 prevGain_Q10[2];
Chris@69 218 SAVE_STACK;
Chris@69 219
Chris@69 220 ALLOC( sLTP_Q14, psDec->ltp_mem_length + psDec->frame_length, opus_int32 );
Chris@69 221 #ifdef SMALL_FOOTPRINT
Chris@69 222 /* Ugly hack that breaks aliasing rules to save stack: put sLTP at the very end of sLTP_Q14. */
Chris@69 223 sLTP = ((opus_int16*)&sLTP_Q14[psDec->ltp_mem_length + psDec->frame_length])-psDec->ltp_mem_length;
Chris@69 224 #else
Chris@69 225 ALLOC( sLTP, psDec->ltp_mem_length, opus_int16 );
Chris@69 226 #endif
Chris@69 227
Chris@69 228 prevGain_Q10[0] = silk_RSHIFT( psPLC->prevGain_Q16[ 0 ], 6);
Chris@69 229 prevGain_Q10[1] = silk_RSHIFT( psPLC->prevGain_Q16[ 1 ], 6);
Chris@69 230
Chris@69 231 if( psDec->first_frame_after_reset ) {
Chris@69 232 silk_memset( psPLC->prevLPC_Q12, 0, sizeof( psPLC->prevLPC_Q12 ) );
Chris@69 233 }
Chris@69 234
Chris@69 235 silk_PLC_energy(&energy1, &shift1, &energy2, &shift2, psDec->exc_Q14, prevGain_Q10, psDec->subfr_length, psDec->nb_subfr);
Chris@69 236
Chris@69 237 if( silk_RSHIFT( energy1, shift2 ) < silk_RSHIFT( energy2, shift1 ) ) {
Chris@69 238 /* First sub-frame has lowest energy */
Chris@69 239 rand_ptr = &psDec->exc_Q14[ silk_max_int( 0, ( psPLC->nb_subfr - 1 ) * psPLC->subfr_length - RAND_BUF_SIZE ) ];
Chris@69 240 } else {
Chris@69 241 /* Second sub-frame has lowest energy */
Chris@69 242 rand_ptr = &psDec->exc_Q14[ silk_max_int( 0, psPLC->nb_subfr * psPLC->subfr_length - RAND_BUF_SIZE ) ];
Chris@69 243 }
Chris@69 244
Chris@69 245 /* Set up Gain to random noise component */
Chris@69 246 B_Q14 = psPLC->LTPCoef_Q14;
Chris@69 247 rand_scale_Q14 = psPLC->randScale_Q14;
Chris@69 248
Chris@69 249 /* Set up attenuation gains */
Chris@69 250 harm_Gain_Q15 = HARM_ATT_Q15[ silk_min_int( NB_ATT - 1, psDec->lossCnt ) ];
Chris@69 251 if( psDec->prevSignalType == TYPE_VOICED ) {
Chris@69 252 rand_Gain_Q15 = PLC_RAND_ATTENUATE_V_Q15[ silk_min_int( NB_ATT - 1, psDec->lossCnt ) ];
Chris@69 253 } else {
Chris@69 254 rand_Gain_Q15 = PLC_RAND_ATTENUATE_UV_Q15[ silk_min_int( NB_ATT - 1, psDec->lossCnt ) ];
Chris@69 255 }
Chris@69 256
Chris@69 257 /* LPC concealment. Apply BWE to previous LPC */
Chris@69 258 silk_bwexpander( psPLC->prevLPC_Q12, psDec->LPC_order, SILK_FIX_CONST( BWE_COEF, 16 ) );
Chris@69 259
Chris@69 260 /* Preload LPC coeficients to array on stack. Gives small performance gain */
Chris@69 261 silk_memcpy( A_Q12, psPLC->prevLPC_Q12, psDec->LPC_order * sizeof( opus_int16 ) );
Chris@69 262
Chris@69 263 /* First Lost frame */
Chris@69 264 if( psDec->lossCnt == 0 ) {
Chris@69 265 rand_scale_Q14 = 1 << 14;
Chris@69 266
Chris@69 267 /* Reduce random noise Gain for voiced frames */
Chris@69 268 if( psDec->prevSignalType == TYPE_VOICED ) {
Chris@69 269 for( i = 0; i < LTP_ORDER; i++ ) {
Chris@69 270 rand_scale_Q14 -= B_Q14[ i ];
Chris@69 271 }
Chris@69 272 rand_scale_Q14 = silk_max_16( 3277, rand_scale_Q14 ); /* 0.2 */
Chris@69 273 rand_scale_Q14 = (opus_int16)silk_RSHIFT( silk_SMULBB( rand_scale_Q14, psPLC->prevLTP_scale_Q14 ), 14 );
Chris@69 274 } else {
Chris@69 275 /* Reduce random noise for unvoiced frames with high LPC gain */
Chris@69 276 opus_int32 invGain_Q30, down_scale_Q30;
Chris@69 277
Chris@69 278 invGain_Q30 = silk_LPC_inverse_pred_gain( psPLC->prevLPC_Q12, psDec->LPC_order, arch );
Chris@69 279
Chris@69 280 down_scale_Q30 = silk_min_32( silk_RSHIFT( (opus_int32)1 << 30, LOG2_INV_LPC_GAIN_HIGH_THRES ), invGain_Q30 );
Chris@69 281 down_scale_Q30 = silk_max_32( silk_RSHIFT( (opus_int32)1 << 30, LOG2_INV_LPC_GAIN_LOW_THRES ), down_scale_Q30 );
Chris@69 282 down_scale_Q30 = silk_LSHIFT( down_scale_Q30, LOG2_INV_LPC_GAIN_HIGH_THRES );
Chris@69 283
Chris@69 284 rand_Gain_Q15 = silk_RSHIFT( silk_SMULWB( down_scale_Q30, rand_Gain_Q15 ), 14 );
Chris@69 285 }
Chris@69 286 }
Chris@69 287
Chris@69 288 rand_seed = psPLC->rand_seed;
Chris@69 289 lag = silk_RSHIFT_ROUND( psPLC->pitchL_Q8, 8 );
Chris@69 290 sLTP_buf_idx = psDec->ltp_mem_length;
Chris@69 291
Chris@69 292 /* Rewhiten LTP state */
Chris@69 293 idx = psDec->ltp_mem_length - lag - psDec->LPC_order - LTP_ORDER / 2;
Chris@69 294 celt_assert( idx > 0 );
Chris@69 295 silk_LPC_analysis_filter( &sLTP[ idx ], &psDec->outBuf[ idx ], A_Q12, psDec->ltp_mem_length - idx, psDec->LPC_order, arch );
Chris@69 296 /* Scale LTP state */
Chris@69 297 inv_gain_Q30 = silk_INVERSE32_varQ( psPLC->prevGain_Q16[ 1 ], 46 );
Chris@69 298 inv_gain_Q30 = silk_min( inv_gain_Q30, silk_int32_MAX >> 1 );
Chris@69 299 for( i = idx + psDec->LPC_order; i < psDec->ltp_mem_length; i++ ) {
Chris@69 300 sLTP_Q14[ i ] = silk_SMULWB( inv_gain_Q30, sLTP[ i ] );
Chris@69 301 }
Chris@69 302
Chris@69 303 /***************************/
Chris@69 304 /* LTP synthesis filtering */
Chris@69 305 /***************************/
Chris@69 306 for( k = 0; k < psDec->nb_subfr; k++ ) {
Chris@69 307 /* Set up pointer */
Chris@69 308 pred_lag_ptr = &sLTP_Q14[ sLTP_buf_idx - lag + LTP_ORDER / 2 ];
Chris@69 309 for( i = 0; i < psDec->subfr_length; i++ ) {
Chris@69 310 /* Unrolled loop */
Chris@69 311 /* Avoids introducing a bias because silk_SMLAWB() always rounds to -inf */
Chris@69 312 LTP_pred_Q12 = 2;
Chris@69 313 LTP_pred_Q12 = silk_SMLAWB( LTP_pred_Q12, pred_lag_ptr[ 0 ], B_Q14[ 0 ] );
Chris@69 314 LTP_pred_Q12 = silk_SMLAWB( LTP_pred_Q12, pred_lag_ptr[ -1 ], B_Q14[ 1 ] );
Chris@69 315 LTP_pred_Q12 = silk_SMLAWB( LTP_pred_Q12, pred_lag_ptr[ -2 ], B_Q14[ 2 ] );
Chris@69 316 LTP_pred_Q12 = silk_SMLAWB( LTP_pred_Q12, pred_lag_ptr[ -3 ], B_Q14[ 3 ] );
Chris@69 317 LTP_pred_Q12 = silk_SMLAWB( LTP_pred_Q12, pred_lag_ptr[ -4 ], B_Q14[ 4 ] );
Chris@69 318 pred_lag_ptr++;
Chris@69 319
Chris@69 320 /* Generate LPC excitation */
Chris@69 321 rand_seed = silk_RAND( rand_seed );
Chris@69 322 idx = silk_RSHIFT( rand_seed, 25 ) & RAND_BUF_MASK;
Chris@69 323 sLTP_Q14[ sLTP_buf_idx ] = silk_LSHIFT32( silk_SMLAWB( LTP_pred_Q12, rand_ptr[ idx ], rand_scale_Q14 ), 2 );
Chris@69 324 sLTP_buf_idx++;
Chris@69 325 }
Chris@69 326
Chris@69 327 /* Gradually reduce LTP gain */
Chris@69 328 for( j = 0; j < LTP_ORDER; j++ ) {
Chris@69 329 B_Q14[ j ] = silk_RSHIFT( silk_SMULBB( harm_Gain_Q15, B_Q14[ j ] ), 15 );
Chris@69 330 }
Chris@69 331 if ( psDec->indices.signalType != TYPE_NO_VOICE_ACTIVITY ) {
Chris@69 332 /* Gradually reduce excitation gain */
Chris@69 333 rand_scale_Q14 = silk_RSHIFT( silk_SMULBB( rand_scale_Q14, rand_Gain_Q15 ), 15 );
Chris@69 334 }
Chris@69 335
Chris@69 336 /* Slowly increase pitch lag */
Chris@69 337 psPLC->pitchL_Q8 = silk_SMLAWB( psPLC->pitchL_Q8, psPLC->pitchL_Q8, PITCH_DRIFT_FAC_Q16 );
Chris@69 338 psPLC->pitchL_Q8 = silk_min_32( psPLC->pitchL_Q8, silk_LSHIFT( silk_SMULBB( MAX_PITCH_LAG_MS, psDec->fs_kHz ), 8 ) );
Chris@69 339 lag = silk_RSHIFT_ROUND( psPLC->pitchL_Q8, 8 );
Chris@69 340 }
Chris@69 341
Chris@69 342 /***************************/
Chris@69 343 /* LPC synthesis filtering */
Chris@69 344 /***************************/
Chris@69 345 sLPC_Q14_ptr = &sLTP_Q14[ psDec->ltp_mem_length - MAX_LPC_ORDER ];
Chris@69 346
Chris@69 347 /* Copy LPC state */
Chris@69 348 silk_memcpy( sLPC_Q14_ptr, psDec->sLPC_Q14_buf, MAX_LPC_ORDER * sizeof( opus_int32 ) );
Chris@69 349
Chris@69 350 celt_assert( psDec->LPC_order >= 10 ); /* check that unrolling works */
Chris@69 351 for( i = 0; i < psDec->frame_length; i++ ) {
Chris@69 352 /* partly unrolled */
Chris@69 353 /* Avoids introducing a bias because silk_SMLAWB() always rounds to -inf */
Chris@69 354 LPC_pred_Q10 = silk_RSHIFT( psDec->LPC_order, 1 );
Chris@69 355 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - 1 ], A_Q12[ 0 ] );
Chris@69 356 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - 2 ], A_Q12[ 1 ] );
Chris@69 357 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - 3 ], A_Q12[ 2 ] );
Chris@69 358 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - 4 ], A_Q12[ 3 ] );
Chris@69 359 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - 5 ], A_Q12[ 4 ] );
Chris@69 360 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - 6 ], A_Q12[ 5 ] );
Chris@69 361 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - 7 ], A_Q12[ 6 ] );
Chris@69 362 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - 8 ], A_Q12[ 7 ] );
Chris@69 363 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - 9 ], A_Q12[ 8 ] );
Chris@69 364 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - 10 ], A_Q12[ 9 ] );
Chris@69 365 for( j = 10; j < psDec->LPC_order; j++ ) {
Chris@69 366 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14_ptr[ MAX_LPC_ORDER + i - j - 1 ], A_Q12[ j ] );
Chris@69 367 }
Chris@69 368
Chris@69 369 /* Add prediction to LPC excitation */
Chris@69 370 sLPC_Q14_ptr[ MAX_LPC_ORDER + i ] = silk_ADD_SAT32( sLPC_Q14_ptr[ MAX_LPC_ORDER + i ],
Chris@69 371 silk_LSHIFT_SAT32( LPC_pred_Q10, 4 ));
Chris@69 372
Chris@69 373 /* Scale with Gain */
Chris@69 374 frame[ i ] = (opus_int16)silk_SAT16( silk_SAT16( silk_RSHIFT_ROUND( silk_SMULWW( sLPC_Q14_ptr[ MAX_LPC_ORDER + i ], prevGain_Q10[ 1 ] ), 8 ) ) );
Chris@69 375 }
Chris@69 376
Chris@69 377 /* Save LPC state */
Chris@69 378 silk_memcpy( psDec->sLPC_Q14_buf, &sLPC_Q14_ptr[ psDec->frame_length ], MAX_LPC_ORDER * sizeof( opus_int32 ) );
Chris@69 379
Chris@69 380 /**************************************/
Chris@69 381 /* Update states */
Chris@69 382 /**************************************/
Chris@69 383 psPLC->rand_seed = rand_seed;
Chris@69 384 psPLC->randScale_Q14 = rand_scale_Q14;
Chris@69 385 for( i = 0; i < MAX_NB_SUBFR; i++ ) {
Chris@69 386 psDecCtrl->pitchL[ i ] = lag;
Chris@69 387 }
Chris@69 388 RESTORE_STACK;
Chris@69 389 }
Chris@69 390
Chris@69 391 /* Glues concealed frames with new good received frames */
Chris@69 392 void silk_PLC_glue_frames(
Chris@69 393 silk_decoder_state *psDec, /* I/O decoder state */
Chris@69 394 opus_int16 frame[], /* I/O signal */
Chris@69 395 opus_int length /* I length of signal */
Chris@69 396 )
Chris@69 397 {
Chris@69 398 opus_int i, energy_shift;
Chris@69 399 opus_int32 energy;
Chris@69 400 silk_PLC_struct *psPLC;
Chris@69 401 psPLC = &psDec->sPLC;
Chris@69 402
Chris@69 403 if( psDec->lossCnt ) {
Chris@69 404 /* Calculate energy in concealed residual */
Chris@69 405 silk_sum_sqr_shift( &psPLC->conc_energy, &psPLC->conc_energy_shift, frame, length );
Chris@69 406
Chris@69 407 psPLC->last_frame_lost = 1;
Chris@69 408 } else {
Chris@69 409 if( psDec->sPLC.last_frame_lost ) {
Chris@69 410 /* Calculate residual in decoded signal if last frame was lost */
Chris@69 411 silk_sum_sqr_shift( &energy, &energy_shift, frame, length );
Chris@69 412
Chris@69 413 /* Normalize energies */
Chris@69 414 if( energy_shift > psPLC->conc_energy_shift ) {
Chris@69 415 psPLC->conc_energy = silk_RSHIFT( psPLC->conc_energy, energy_shift - psPLC->conc_energy_shift );
Chris@69 416 } else if( energy_shift < psPLC->conc_energy_shift ) {
Chris@69 417 energy = silk_RSHIFT( energy, psPLC->conc_energy_shift - energy_shift );
Chris@69 418 }
Chris@69 419
Chris@69 420 /* Fade in the energy difference */
Chris@69 421 if( energy > psPLC->conc_energy ) {
Chris@69 422 opus_int32 frac_Q24, LZ;
Chris@69 423 opus_int32 gain_Q16, slope_Q16;
Chris@69 424
Chris@69 425 LZ = silk_CLZ32( psPLC->conc_energy );
Chris@69 426 LZ = LZ - 1;
Chris@69 427 psPLC->conc_energy = silk_LSHIFT( psPLC->conc_energy, LZ );
Chris@69 428 energy = silk_RSHIFT( energy, silk_max_32( 24 - LZ, 0 ) );
Chris@69 429
Chris@69 430 frac_Q24 = silk_DIV32( psPLC->conc_energy, silk_max( energy, 1 ) );
Chris@69 431
Chris@69 432 gain_Q16 = silk_LSHIFT( silk_SQRT_APPROX( frac_Q24 ), 4 );
Chris@69 433 slope_Q16 = silk_DIV32_16( ( (opus_int32)1 << 16 ) - gain_Q16, length );
Chris@69 434 /* Make slope 4x steeper to avoid missing onsets after DTX */
Chris@69 435 slope_Q16 = silk_LSHIFT( slope_Q16, 2 );
Chris@69 436
Chris@69 437 for( i = 0; i < length; i++ ) {
Chris@69 438 frame[ i ] = silk_SMULWB( gain_Q16, frame[ i ] );
Chris@69 439 gain_Q16 += slope_Q16;
Chris@69 440 if( gain_Q16 > (opus_int32)1 << 16 ) {
Chris@69 441 break;
Chris@69 442 }
Chris@69 443 }
Chris@69 444 }
Chris@69 445 }
Chris@69 446 psPLC->last_frame_lost = 0;
Chris@69 447 }
Chris@69 448 }