CNG.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. /* Generates excitation for CNG LPC synthesis */
  33. static OPUS_INLINE void silk_CNG_exc(
  34. opus_int32 exc_Q14[], /* O CNG excitation signal Q10 */
  35. opus_int32 exc_buf_Q14[], /* I Random samples buffer Q10 */
  36. opus_int length, /* I Length */
  37. opus_int32 *rand_seed /* I/O Seed to random index generator */
  38. )
  39. {
  40. opus_int32 seed;
  41. opus_int i, idx, exc_mask;
  42. exc_mask = CNG_BUF_MASK_MAX;
  43. while( exc_mask > length ) {
  44. exc_mask = silk_RSHIFT( exc_mask, 1 );
  45. }
  46. seed = *rand_seed;
  47. for( i = 0; i < length; i++ ) {
  48. seed = silk_RAND( seed );
  49. idx = (opus_int)( silk_RSHIFT( seed, 24 ) & exc_mask );
  50. silk_assert( idx >= 0 );
  51. silk_assert( idx <= CNG_BUF_MASK_MAX );
  52. exc_Q14[ i ] = exc_buf_Q14[ idx ];
  53. }
  54. *rand_seed = seed;
  55. }
  56. void silk_CNG_Reset(
  57. silk_decoder_state *psDec /* I/O Decoder state */
  58. )
  59. {
  60. opus_int i, NLSF_step_Q15, NLSF_acc_Q15;
  61. NLSF_step_Q15 = silk_DIV32_16( silk_int16_MAX, psDec->LPC_order + 1 );
  62. NLSF_acc_Q15 = 0;
  63. for( i = 0; i < psDec->LPC_order; i++ ) {
  64. NLSF_acc_Q15 += NLSF_step_Q15;
  65. psDec->sCNG.CNG_smth_NLSF_Q15[ i ] = NLSF_acc_Q15;
  66. }
  67. psDec->sCNG.CNG_smth_Gain_Q16 = 0;
  68. psDec->sCNG.rand_seed = 3176576;
  69. }
  70. /* Updates CNG estimate, and applies the CNG when packet was lost */
  71. void silk_CNG(
  72. silk_decoder_state *psDec, /* I/O Decoder state */
  73. silk_decoder_control *psDecCtrl, /* I/O Decoder control */
  74. opus_int16 frame[], /* I/O Signal */
  75. opus_int length /* I Length of residual */
  76. )
  77. {
  78. opus_int i, subfr;
  79. opus_int32 LPC_pred_Q10, max_Gain_Q16, gain_Q16, gain_Q10;
  80. opus_int16 A_Q12[ MAX_LPC_ORDER ];
  81. silk_CNG_struct *psCNG = &psDec->sCNG;
  82. SAVE_STACK;
  83. if( psDec->fs_kHz != psCNG->fs_kHz ) {
  84. /* Reset state */
  85. silk_CNG_Reset( psDec );
  86. psCNG->fs_kHz = psDec->fs_kHz;
  87. }
  88. if( psDec->lossCnt == 0 && psDec->prevSignalType == TYPE_NO_VOICE_ACTIVITY ) {
  89. /* Update CNG parameters */
  90. /* Smoothing of LSF's */
  91. for( i = 0; i < psDec->LPC_order; i++ ) {
  92. psCNG->CNG_smth_NLSF_Q15[ i ] += silk_SMULWB( (opus_int32)psDec->prevNLSF_Q15[ i ] - (opus_int32)psCNG->CNG_smth_NLSF_Q15[ i ], CNG_NLSF_SMTH_Q16 );
  93. }
  94. /* Find the subframe with the highest gain */
  95. max_Gain_Q16 = 0;
  96. subfr = 0;
  97. for( i = 0; i < psDec->nb_subfr; i++ ) {
  98. if( psDecCtrl->Gains_Q16[ i ] > max_Gain_Q16 ) {
  99. max_Gain_Q16 = psDecCtrl->Gains_Q16[ i ];
  100. subfr = i;
  101. }
  102. }
  103. /* Update CNG excitation buffer with excitation from this subframe */
  104. silk_memmove( &psCNG->CNG_exc_buf_Q14[ psDec->subfr_length ], psCNG->CNG_exc_buf_Q14, ( psDec->nb_subfr - 1 ) * psDec->subfr_length * sizeof( opus_int32 ) );
  105. silk_memcpy( psCNG->CNG_exc_buf_Q14, &psDec->exc_Q14[ subfr * psDec->subfr_length ], psDec->subfr_length * sizeof( opus_int32 ) );
  106. /* Smooth gains */
  107. for( i = 0; i < psDec->nb_subfr; i++ ) {
  108. psCNG->CNG_smth_Gain_Q16 += silk_SMULWB( psDecCtrl->Gains_Q16[ i ] - psCNG->CNG_smth_Gain_Q16, CNG_GAIN_SMTH_Q16 );
  109. /* If the smoothed gain is 3 dB greater than this subframe's gain, use this subframe's gain to adapt faster. */
  110. if( silk_SMULWW( psCNG->CNG_smth_Gain_Q16, CNG_GAIN_SMTH_THRESHOLD_Q16 ) > psDecCtrl->Gains_Q16[ i ] ) {
  111. psCNG->CNG_smth_Gain_Q16 = psDecCtrl->Gains_Q16[ i ];
  112. }
  113. }
  114. }
  115. /* Add CNG when packet is lost or during DTX */
  116. if( psDec->lossCnt ) {
  117. VARDECL( opus_int32, CNG_sig_Q14 );
  118. ALLOC( CNG_sig_Q14, length + MAX_LPC_ORDER, opus_int32 );
  119. /* Generate CNG excitation */
  120. gain_Q16 = silk_SMULWW( psDec->sPLC.randScale_Q14, psDec->sPLC.prevGain_Q16[1] );
  121. if( gain_Q16 >= (1 << 21) || psCNG->CNG_smth_Gain_Q16 > (1 << 23) ) {
  122. gain_Q16 = silk_SMULTT( gain_Q16, gain_Q16 );
  123. gain_Q16 = silk_SUB_LSHIFT32(silk_SMULTT( psCNG->CNG_smth_Gain_Q16, psCNG->CNG_smth_Gain_Q16 ), gain_Q16, 5 );
  124. gain_Q16 = silk_LSHIFT32( silk_SQRT_APPROX( gain_Q16 ), 16 );
  125. } else {
  126. gain_Q16 = silk_SMULWW( gain_Q16, gain_Q16 );
  127. gain_Q16 = silk_SUB_LSHIFT32(silk_SMULWW( psCNG->CNG_smth_Gain_Q16, psCNG->CNG_smth_Gain_Q16 ), gain_Q16, 5 );
  128. gain_Q16 = silk_LSHIFT32( silk_SQRT_APPROX( gain_Q16 ), 8 );
  129. }
  130. gain_Q10 = silk_RSHIFT( gain_Q16, 6 );
  131. silk_CNG_exc( CNG_sig_Q14 + MAX_LPC_ORDER, psCNG->CNG_exc_buf_Q14, length, &psCNG->rand_seed );
  132. /* Convert CNG NLSF to filter representation */
  133. silk_NLSF2A( A_Q12, psCNG->CNG_smth_NLSF_Q15, psDec->LPC_order, psDec->arch );
  134. /* Generate CNG signal, by synthesis filtering */
  135. silk_memcpy( CNG_sig_Q14, psCNG->CNG_synth_state, MAX_LPC_ORDER * sizeof( opus_int32 ) );
  136. celt_assert( psDec->LPC_order == 10 || psDec->LPC_order == 16 );
  137. for( i = 0; i < length; i++ ) {
  138. /* Avoids introducing a bias because silk_SMLAWB() always rounds to -inf */
  139. LPC_pred_Q10 = silk_RSHIFT( psDec->LPC_order, 1 );
  140. LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, CNG_sig_Q14[ MAX_LPC_ORDER + i - 1 ], A_Q12[ 0 ] );
  141. LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, CNG_sig_Q14[ MAX_LPC_ORDER + i - 2 ], A_Q12[ 1 ] );
  142. LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, CNG_sig_Q14[ MAX_LPC_ORDER + i - 3 ], A_Q12[ 2 ] );
  143. LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, CNG_sig_Q14[ MAX_LPC_ORDER + i - 4 ], A_Q12[ 3 ] );
  144. LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, CNG_sig_Q14[ MAX_LPC_ORDER + i - 5 ], A_Q12[ 4 ] );
  145. LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, CNG_sig_Q14[ MAX_LPC_ORDER + i - 6 ], A_Q12[ 5 ] );
  146. LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, CNG_sig_Q14[ MAX_LPC_ORDER + i - 7 ], A_Q12[ 6 ] );
  147. LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, CNG_sig_Q14[ MAX_LPC_ORDER + i - 8 ], A_Q12[ 7 ] );
  148. LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, CNG_sig_Q14[ MAX_LPC_ORDER + i - 9 ], A_Q12[ 8 ] );
  149. LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, CNG_sig_Q14[ MAX_LPC_ORDER + i - 10 ], A_Q12[ 9 ] );
  150. if( psDec->LPC_order == 16 ) {
  151. LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, CNG_sig_Q14[ MAX_LPC_ORDER + i - 11 ], A_Q12[ 10 ] );
  152. LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, CNG_sig_Q14[ MAX_LPC_ORDER + i - 12 ], A_Q12[ 11 ] );
  153. LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, CNG_sig_Q14[ MAX_LPC_ORDER + i - 13 ], A_Q12[ 12 ] );
  154. LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, CNG_sig_Q14[ MAX_LPC_ORDER + i - 14 ], A_Q12[ 13 ] );
  155. LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, CNG_sig_Q14[ MAX_LPC_ORDER + i - 15 ], A_Q12[ 14 ] );
  156. LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, CNG_sig_Q14[ MAX_LPC_ORDER + i - 16 ], A_Q12[ 15 ] );
  157. }
  158. /* Update states */
  159. CNG_sig_Q14[ MAX_LPC_ORDER + i ] = silk_ADD_SAT32( CNG_sig_Q14[ MAX_LPC_ORDER + i ], silk_LSHIFT_SAT32( LPC_pred_Q10, 4 ) );
  160. /* Scale with Gain and add to input signal */
  161. frame[ i ] = (opus_int16)silk_ADD_SAT16( frame[ i ], silk_SAT16( silk_RSHIFT_ROUND( silk_SMULWW( CNG_sig_Q14[ MAX_LPC_ORDER + i ], gain_Q10 ), 8 ) ) );
  162. }
  163. silk_memcpy( psCNG->CNG_synth_state, &CNG_sig_Q14[ length ], MAX_LPC_ORDER * sizeof( opus_int32 ) );
  164. } else {
  165. silk_memset( psCNG->CNG_synth_state, 0, psDec->LPC_order * sizeof( opus_int32 ) );
  166. }
  167. RESTORE_STACK;
  168. }