annotate src/opus-1.3/silk/PLC.c @ 169:223a55898ab9 tip default

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