PLC.c 20 KB

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