NLSF2A.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. /* conversion between prediction filter coefficients and LSFs */
  31. /* order should be even */
  32. /* a piecewise linear approximation maps LSF <-> cos(LSF) */
  33. /* therefore the result is not accurate LSFs, but the two */
  34. /* functions are accurate inverses of each other */
  35. #include "SigProc_FIX.h"
  36. #include "tables.h"
  37. #define QA 16
  38. /* helper function for NLSF2A(..) */
  39. static OPUS_INLINE void silk_NLSF2A_find_poly(
  40. opus_int32 *out, /* O intermediate polynomial, QA [dd+1] */
  41. const opus_int32 *cLSF, /* I vector of interleaved 2*cos(LSFs), QA [d] */
  42. opus_int dd /* I polynomial order (= 1/2 * filter order) */
  43. )
  44. {
  45. opus_int k, n;
  46. opus_int32 ftmp;
  47. out[0] = silk_LSHIFT( 1, QA );
  48. out[1] = -cLSF[0];
  49. for( k = 1; k < dd; k++ ) {
  50. ftmp = cLSF[2*k]; /* QA*/
  51. out[k+1] = silk_LSHIFT( out[k-1], 1 ) - (opus_int32)silk_RSHIFT_ROUND64( silk_SMULL( ftmp, out[k] ), QA );
  52. for( n = k; n > 1; n-- ) {
  53. out[n] += out[n-2] - (opus_int32)silk_RSHIFT_ROUND64( silk_SMULL( ftmp, out[n-1] ), QA );
  54. }
  55. out[1] -= ftmp;
  56. }
  57. }
  58. /* compute whitening filter coefficients from normalized line spectral frequencies */
  59. void silk_NLSF2A(
  60. opus_int16 *a_Q12, /* O monic whitening filter coefficients in Q12, [ d ] */
  61. const opus_int16 *NLSF, /* I normalized line spectral frequencies in Q15, [ d ] */
  62. const opus_int d /* I filter order (should be even) */
  63. )
  64. {
  65. /* This ordering was found to maximize quality. It improves numerical accuracy of
  66. silk_NLSF2A_find_poly() compared to "standard" ordering. */
  67. static const unsigned char ordering16[16] = {
  68. 0, 15, 8, 7, 4, 11, 12, 3, 2, 13, 10, 5, 6, 9, 14, 1
  69. };
  70. static const unsigned char ordering10[10] = {
  71. 0, 9, 6, 3, 4, 5, 8, 1, 2, 7
  72. };
  73. const unsigned char *ordering;
  74. opus_int k, i, dd;
  75. opus_int32 cos_LSF_QA[ SILK_MAX_ORDER_LPC ];
  76. opus_int32 P[ SILK_MAX_ORDER_LPC / 2 + 1 ], Q[ SILK_MAX_ORDER_LPC / 2 + 1 ];
  77. opus_int32 Ptmp, Qtmp, f_int, f_frac, cos_val, delta;
  78. opus_int32 a32_QA1[ SILK_MAX_ORDER_LPC ];
  79. opus_int32 maxabs, absval, idx=0, sc_Q16;
  80. silk_assert( LSF_COS_TAB_SZ_FIX == 128 );
  81. silk_assert( d==10||d==16 );
  82. /* convert LSFs to 2*cos(LSF), using piecewise linear curve from table */
  83. ordering = d == 16 ? ordering16 : ordering10;
  84. for( k = 0; k < d; k++ ) {
  85. silk_assert(NLSF[k] >= 0 );
  86. /* f_int on a scale 0-127 (rounded down) */
  87. f_int = silk_RSHIFT( NLSF[k], 15 - 7 );
  88. /* f_frac, range: 0..255 */
  89. f_frac = NLSF[k] - silk_LSHIFT( f_int, 15 - 7 );
  90. silk_assert(f_int >= 0);
  91. silk_assert(f_int < LSF_COS_TAB_SZ_FIX );
  92. /* Read start and end value from table */
  93. cos_val = silk_LSFCosTab_FIX_Q12[ f_int ]; /* Q12 */
  94. delta = silk_LSFCosTab_FIX_Q12[ f_int + 1 ] - cos_val; /* Q12, with a range of 0..200 */
  95. /* Linear interpolation */
  96. cos_LSF_QA[ordering[k]] = silk_RSHIFT_ROUND( silk_LSHIFT( cos_val, 8 ) + silk_MUL( delta, f_frac ), 20 - QA ); /* QA */
  97. }
  98. dd = silk_RSHIFT( d, 1 );
  99. /* generate even and odd polynomials using convolution */
  100. silk_NLSF2A_find_poly( P, &cos_LSF_QA[ 0 ], dd );
  101. silk_NLSF2A_find_poly( Q, &cos_LSF_QA[ 1 ], dd );
  102. /* convert even and odd polynomials to opus_int32 Q12 filter coefs */
  103. for( k = 0; k < dd; k++ ) {
  104. Ptmp = P[ k+1 ] + P[ k ];
  105. Qtmp = Q[ k+1 ] - Q[ k ];
  106. /* the Ptmp and Qtmp values at this stage need to fit in int32 */
  107. a32_QA1[ k ] = -Qtmp - Ptmp; /* QA+1 */
  108. a32_QA1[ d-k-1 ] = Qtmp - Ptmp; /* QA+1 */
  109. }
  110. /* Limit the maximum absolute value of the prediction coefficients, so that they'll fit in int16 */
  111. for( i = 0; i < 10; i++ ) {
  112. /* Find maximum absolute value and its index */
  113. maxabs = 0;
  114. for( k = 0; k < d; k++ ) {
  115. absval = silk_abs( a32_QA1[k] );
  116. if( absval > maxabs ) {
  117. maxabs = absval;
  118. idx = k;
  119. }
  120. }
  121. maxabs = silk_RSHIFT_ROUND( maxabs, QA + 1 - 12 ); /* QA+1 -> Q12 */
  122. if( maxabs > silk_int16_MAX ) {
  123. /* Reduce magnitude of prediction coefficients */
  124. maxabs = silk_min( maxabs, 163838 ); /* ( silk_int32_MAX >> 14 ) + silk_int16_MAX = 163838 */
  125. sc_Q16 = SILK_FIX_CONST( 0.999, 16 ) - silk_DIV32( silk_LSHIFT( maxabs - silk_int16_MAX, 14 ),
  126. silk_RSHIFT32( silk_MUL( maxabs, idx + 1), 2 ) );
  127. silk_bwexpander_32( a32_QA1, d, sc_Q16 );
  128. } else {
  129. break;
  130. }
  131. }
  132. if( i == 10 ) {
  133. /* Reached the last iteration, clip the coefficients */
  134. for( k = 0; k < d; k++ ) {
  135. a_Q12[ k ] = (opus_int16)silk_SAT16( silk_RSHIFT_ROUND( a32_QA1[ k ], QA + 1 - 12 ) ); /* QA+1 -> Q12 */
  136. a32_QA1[ k ] = silk_LSHIFT( (opus_int32)a_Q12[ k ], QA + 1 - 12 );
  137. }
  138. } else {
  139. for( k = 0; k < d; k++ ) {
  140. a_Q12[ k ] = (opus_int16)silk_RSHIFT_ROUND( a32_QA1[ k ], QA + 1 - 12 ); /* QA+1 -> Q12 */
  141. }
  142. }
  143. for( i = 0; i < MAX_LPC_STABILIZE_ITERATIONS; i++ ) {
  144. if( silk_LPC_inverse_pred_gain( a_Q12, d ) < SILK_FIX_CONST( 1.0 / MAX_PREDICTION_POWER_GAIN, 30 ) ) {
  145. /* Prediction coefficients are (too close to) unstable; apply bandwidth expansion */
  146. /* on the unscaled coefficients, convert to Q12 and measure again */
  147. silk_bwexpander_32( a32_QA1, d, 65536 - silk_LSHIFT( 2, i ) );
  148. for( k = 0; k < d; k++ ) {
  149. a_Q12[ k ] = (opus_int16)silk_RSHIFT_ROUND( a32_QA1[ k ], QA + 1 - 12 ); /* QA+1 -> Q12 */
  150. }
  151. } else {
  152. break;
  153. }
  154. }
  155. }