LPC_inv_pred_gain.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "SigProc_FIX.h"
  31. #define QA 24
  32. #define A_LIMIT SILK_FIX_CONST( 0.99975, QA )
  33. #define MUL32_FRAC_Q(a32, b32, Q) ((opus_int32)(silk_RSHIFT_ROUND64(silk_SMULL(a32, b32), Q)))
  34. /* Compute inverse of LPC prediction gain, and */
  35. /* test if LPC coefficients are stable (all poles within unit circle) */
  36. static opus_int32 LPC_inverse_pred_gain_QA( /* O Returns inverse prediction gain in energy domain, Q30 */
  37. opus_int32 A_QA[ 2 ][ SILK_MAX_ORDER_LPC ], /* I Prediction coefficients */
  38. const opus_int order /* I Prediction order */
  39. )
  40. {
  41. opus_int k, n, mult2Q;
  42. opus_int32 invGain_Q30, rc_Q31, rc_mult1_Q30, rc_mult2, tmp_QA;
  43. opus_int32 *Aold_QA, *Anew_QA;
  44. Anew_QA = A_QA[ order & 1 ];
  45. invGain_Q30 = (opus_int32)1 << 30;
  46. for( k = order - 1; k > 0; k-- ) {
  47. /* Check for stability */
  48. if( ( Anew_QA[ k ] > A_LIMIT ) || ( Anew_QA[ k ] < -A_LIMIT ) ) {
  49. return 0;
  50. }
  51. /* Set RC equal to negated AR coef */
  52. rc_Q31 = -silk_LSHIFT( Anew_QA[ k ], 31 - QA );
  53. /* rc_mult1_Q30 range: [ 1 : 2^30 ] */
  54. rc_mult1_Q30 = ( (opus_int32)1 << 30 ) - silk_SMMUL( rc_Q31, rc_Q31 );
  55. silk_assert( rc_mult1_Q30 > ( 1 << 15 ) ); /* reduce A_LIMIT if fails */
  56. silk_assert( rc_mult1_Q30 <= ( 1 << 30 ) );
  57. /* rc_mult2 range: [ 2^30 : silk_int32_MAX ] */
  58. mult2Q = 32 - silk_CLZ32( silk_abs( rc_mult1_Q30 ) );
  59. rc_mult2 = silk_INVERSE32_varQ( rc_mult1_Q30, mult2Q + 30 );
  60. /* Update inverse gain */
  61. /* invGain_Q30 range: [ 0 : 2^30 ] */
  62. invGain_Q30 = silk_LSHIFT( silk_SMMUL( invGain_Q30, rc_mult1_Q30 ), 2 );
  63. silk_assert( invGain_Q30 >= 0 );
  64. silk_assert( invGain_Q30 <= ( 1 << 30 ) );
  65. /* Swap pointers */
  66. Aold_QA = Anew_QA;
  67. Anew_QA = A_QA[ k & 1 ];
  68. /* Update AR coefficient */
  69. for( n = 0; n < k; n++ ) {
  70. tmp_QA = Aold_QA[ n ] - MUL32_FRAC_Q( Aold_QA[ k - n - 1 ], rc_Q31, 31 );
  71. Anew_QA[ n ] = MUL32_FRAC_Q( tmp_QA, rc_mult2 , mult2Q );
  72. }
  73. }
  74. /* Check for stability */
  75. if( ( Anew_QA[ 0 ] > A_LIMIT ) || ( Anew_QA[ 0 ] < -A_LIMIT ) ) {
  76. return 0;
  77. }
  78. /* Set RC equal to negated AR coef */
  79. rc_Q31 = -silk_LSHIFT( Anew_QA[ 0 ], 31 - QA );
  80. /* Range: [ 1 : 2^30 ] */
  81. rc_mult1_Q30 = ( (opus_int32)1 << 30 ) - silk_SMMUL( rc_Q31, rc_Q31 );
  82. /* Update inverse gain */
  83. /* Range: [ 0 : 2^30 ] */
  84. invGain_Q30 = silk_LSHIFT( silk_SMMUL( invGain_Q30, rc_mult1_Q30 ), 2 );
  85. silk_assert( invGain_Q30 >= 0 );
  86. silk_assert( invGain_Q30 <= 1<<30 );
  87. return invGain_Q30;
  88. }
  89. /* For input in Q12 domain */
  90. opus_int32 silk_LPC_inverse_pred_gain( /* O Returns inverse prediction gain in energy domain, Q30 */
  91. const opus_int16 *A_Q12, /* I Prediction coefficients, Q12 [order] */
  92. const opus_int order /* I Prediction order */
  93. )
  94. {
  95. opus_int k;
  96. opus_int32 Atmp_QA[ 2 ][ SILK_MAX_ORDER_LPC ];
  97. opus_int32 *Anew_QA;
  98. opus_int32 DC_resp = 0;
  99. Anew_QA = Atmp_QA[ order & 1 ];
  100. /* Increase Q domain of the AR coefficients */
  101. for( k = 0; k < order; k++ ) {
  102. DC_resp += (opus_int32)A_Q12[ k ];
  103. Anew_QA[ k ] = silk_LSHIFT32( (opus_int32)A_Q12[ k ], QA - 12 );
  104. }
  105. /* If the DC is unstable, we don't even need to do the full calculations */
  106. if( DC_resp >= 4096 ) {
  107. return 0;
  108. }
  109. return LPC_inverse_pred_gain_QA( Atmp_QA, order );
  110. }
  111. #ifdef FIXED_POINT
  112. /* For input in Q24 domain */
  113. opus_int32 silk_LPC_inverse_pred_gain_Q24( /* O Returns inverse prediction gain in energy domain, Q30 */
  114. const opus_int32 *A_Q24, /* I Prediction coefficients [order] */
  115. const opus_int order /* I Prediction order */
  116. )
  117. {
  118. opus_int k;
  119. opus_int32 Atmp_QA[ 2 ][ SILK_MAX_ORDER_LPC ];
  120. opus_int32 *Anew_QA;
  121. Anew_QA = Atmp_QA[ order & 1 ];
  122. /* Increase Q domain of the AR coefficients */
  123. for( k = 0; k < order; k++ ) {
  124. Anew_QA[ k ] = silk_RSHIFT32( A_Q24[ k ], 24 - QA );
  125. }
  126. return LPC_inverse_pred_gain_QA( Atmp_QA, order );
  127. }
  128. #endif