burg_modified_FIX.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. #include "define.h"
  32. #include "tuning_parameters.h"
  33. #include "pitch.h"
  34. #define MAX_FRAME_SIZE 384 /* subfr_length * nb_subfr = ( 0.005 * 16000 + 16 ) * 4 = 384 */
  35. #define QA 25
  36. #define N_BITS_HEAD_ROOM 3
  37. #define MIN_RSHIFTS -16
  38. #define MAX_RSHIFTS (32 - QA)
  39. /* Compute reflection coefficients from input signal */
  40. void silk_burg_modified_c(
  41. opus_int32 *res_nrg, /* O Residual energy */
  42. opus_int *res_nrg_Q, /* O Residual energy Q value */
  43. opus_int32 A_Q16[], /* O Prediction coefficients (length order) */
  44. const opus_int16 x[], /* I Input signal, length: nb_subfr * ( D + subfr_length ) */
  45. const opus_int32 minInvGain_Q30, /* I Inverse of max prediction gain */
  46. const opus_int subfr_length, /* I Input signal subframe length (incl. D preceding samples) */
  47. const opus_int nb_subfr, /* I Number of subframes stacked in x */
  48. const opus_int D, /* I Order */
  49. int arch /* I Run-time architecture */
  50. )
  51. {
  52. opus_int k, n, s, lz, rshifts, reached_max_gain;
  53. opus_int32 C0, num, nrg, rc_Q31, invGain_Q30, Atmp_QA, Atmp1, tmp1, tmp2, x1, x2;
  54. const opus_int16 *x_ptr;
  55. opus_int32 C_first_row[ SILK_MAX_ORDER_LPC ];
  56. opus_int32 C_last_row[ SILK_MAX_ORDER_LPC ];
  57. opus_int32 Af_QA[ SILK_MAX_ORDER_LPC ];
  58. opus_int32 CAf[ SILK_MAX_ORDER_LPC + 1 ];
  59. opus_int32 CAb[ SILK_MAX_ORDER_LPC + 1 ];
  60. opus_int32 xcorr[ SILK_MAX_ORDER_LPC ];
  61. opus_int64 C0_64;
  62. celt_assert( subfr_length * nb_subfr <= MAX_FRAME_SIZE );
  63. /* Compute autocorrelations, added over subframes */
  64. C0_64 = silk_inner_prod16_aligned_64( x, x, subfr_length*nb_subfr, arch );
  65. lz = silk_CLZ64(C0_64);
  66. rshifts = 32 + 1 + N_BITS_HEAD_ROOM - lz;
  67. if (rshifts > MAX_RSHIFTS) rshifts = MAX_RSHIFTS;
  68. if (rshifts < MIN_RSHIFTS) rshifts = MIN_RSHIFTS;
  69. if (rshifts > 0) {
  70. C0 = (opus_int32)silk_RSHIFT64(C0_64, rshifts );
  71. } else {
  72. C0 = silk_LSHIFT32((opus_int32)C0_64, -rshifts );
  73. }
  74. CAb[ 0 ] = CAf[ 0 ] = C0 + silk_SMMUL( SILK_FIX_CONST( FIND_LPC_COND_FAC, 32 ), C0 ) + 1; /* Q(-rshifts) */
  75. silk_memset( C_first_row, 0, SILK_MAX_ORDER_LPC * sizeof( opus_int32 ) );
  76. if( rshifts > 0 ) {
  77. for( s = 0; s < nb_subfr; s++ ) {
  78. x_ptr = x + s * subfr_length;
  79. for( n = 1; n < D + 1; n++ ) {
  80. C_first_row[ n - 1 ] += (opus_int32)silk_RSHIFT64(
  81. silk_inner_prod16_aligned_64( x_ptr, x_ptr + n, subfr_length - n, arch ), rshifts );
  82. }
  83. }
  84. } else {
  85. for( s = 0; s < nb_subfr; s++ ) {
  86. int i;
  87. opus_int32 d;
  88. x_ptr = x + s * subfr_length;
  89. celt_pitch_xcorr(x_ptr, x_ptr + 1, xcorr, subfr_length - D, D, arch );
  90. for( n = 1; n < D + 1; n++ ) {
  91. for ( i = n + subfr_length - D, d = 0; i < subfr_length; i++ )
  92. d = MAC16_16( d, x_ptr[ i ], x_ptr[ i - n ] );
  93. xcorr[ n - 1 ] += d;
  94. }
  95. for( n = 1; n < D + 1; n++ ) {
  96. C_first_row[ n - 1 ] += silk_LSHIFT32( xcorr[ n - 1 ], -rshifts );
  97. }
  98. }
  99. }
  100. silk_memcpy( C_last_row, C_first_row, SILK_MAX_ORDER_LPC * sizeof( opus_int32 ) );
  101. /* Initialize */
  102. CAb[ 0 ] = CAf[ 0 ] = C0 + silk_SMMUL( SILK_FIX_CONST( FIND_LPC_COND_FAC, 32 ), C0 ) + 1; /* Q(-rshifts) */
  103. invGain_Q30 = (opus_int32)1 << 30;
  104. reached_max_gain = 0;
  105. for( n = 0; n < D; n++ ) {
  106. /* Update first row of correlation matrix (without first element) */
  107. /* Update last row of correlation matrix (without last element, stored in reversed order) */
  108. /* Update C * Af */
  109. /* Update C * flipud(Af) (stored in reversed order) */
  110. if( rshifts > -2 ) {
  111. for( s = 0; s < nb_subfr; s++ ) {
  112. x_ptr = x + s * subfr_length;
  113. x1 = -silk_LSHIFT32( (opus_int32)x_ptr[ n ], 16 - rshifts ); /* Q(16-rshifts) */
  114. x2 = -silk_LSHIFT32( (opus_int32)x_ptr[ subfr_length - n - 1 ], 16 - rshifts ); /* Q(16-rshifts) */
  115. tmp1 = silk_LSHIFT32( (opus_int32)x_ptr[ n ], QA - 16 ); /* Q(QA-16) */
  116. tmp2 = silk_LSHIFT32( (opus_int32)x_ptr[ subfr_length - n - 1 ], QA - 16 ); /* Q(QA-16) */
  117. for( k = 0; k < n; k++ ) {
  118. C_first_row[ k ] = silk_SMLAWB( C_first_row[ k ], x1, x_ptr[ n - k - 1 ] ); /* Q( -rshifts ) */
  119. C_last_row[ k ] = silk_SMLAWB( C_last_row[ k ], x2, x_ptr[ subfr_length - n + k ] ); /* Q( -rshifts ) */
  120. Atmp_QA = Af_QA[ k ];
  121. tmp1 = silk_SMLAWB( tmp1, Atmp_QA, x_ptr[ n - k - 1 ] ); /* Q(QA-16) */
  122. tmp2 = silk_SMLAWB( tmp2, Atmp_QA, x_ptr[ subfr_length - n + k ] ); /* Q(QA-16) */
  123. }
  124. tmp1 = silk_LSHIFT32( -tmp1, 32 - QA - rshifts ); /* Q(16-rshifts) */
  125. tmp2 = silk_LSHIFT32( -tmp2, 32 - QA - rshifts ); /* Q(16-rshifts) */
  126. for( k = 0; k <= n; k++ ) {
  127. CAf[ k ] = silk_SMLAWB( CAf[ k ], tmp1, x_ptr[ n - k ] ); /* Q( -rshift ) */
  128. CAb[ k ] = silk_SMLAWB( CAb[ k ], tmp2, x_ptr[ subfr_length - n + k - 1 ] ); /* Q( -rshift ) */
  129. }
  130. }
  131. } else {
  132. for( s = 0; s < nb_subfr; s++ ) {
  133. x_ptr = x + s * subfr_length;
  134. x1 = -silk_LSHIFT32( (opus_int32)x_ptr[ n ], -rshifts ); /* Q( -rshifts ) */
  135. x2 = -silk_LSHIFT32( (opus_int32)x_ptr[ subfr_length - n - 1 ], -rshifts ); /* Q( -rshifts ) */
  136. tmp1 = silk_LSHIFT32( (opus_int32)x_ptr[ n ], 17 ); /* Q17 */
  137. tmp2 = silk_LSHIFT32( (opus_int32)x_ptr[ subfr_length - n - 1 ], 17 ); /* Q17 */
  138. for( k = 0; k < n; k++ ) {
  139. C_first_row[ k ] = silk_MLA( C_first_row[ k ], x1, x_ptr[ n - k - 1 ] ); /* Q( -rshifts ) */
  140. C_last_row[ k ] = silk_MLA( C_last_row[ k ], x2, x_ptr[ subfr_length - n + k ] ); /* Q( -rshifts ) */
  141. Atmp1 = silk_RSHIFT_ROUND( Af_QA[ k ], QA - 17 ); /* Q17 */
  142. /* We sometimes have get overflows in the multiplications (even beyond +/- 2^32),
  143. but they cancel each other and the real result seems to always fit in a 32-bit
  144. signed integer. This was determined experimentally, not theoretically (unfortunately). */
  145. tmp1 = silk_MLA_ovflw( tmp1, x_ptr[ n - k - 1 ], Atmp1 ); /* Q17 */
  146. tmp2 = silk_MLA_ovflw( tmp2, x_ptr[ subfr_length - n + k ], Atmp1 ); /* Q17 */
  147. }
  148. tmp1 = -tmp1; /* Q17 */
  149. tmp2 = -tmp2; /* Q17 */
  150. for( k = 0; k <= n; k++ ) {
  151. CAf[ k ] = silk_SMLAWW( CAf[ k ], tmp1,
  152. silk_LSHIFT32( (opus_int32)x_ptr[ n - k ], -rshifts - 1 ) ); /* Q( -rshift ) */
  153. CAb[ k ] = silk_SMLAWW( CAb[ k ], tmp2,
  154. silk_LSHIFT32( (opus_int32)x_ptr[ subfr_length - n + k - 1 ], -rshifts - 1 ) ); /* Q( -rshift ) */
  155. }
  156. }
  157. }
  158. /* Calculate nominator and denominator for the next order reflection (parcor) coefficient */
  159. tmp1 = C_first_row[ n ]; /* Q( -rshifts ) */
  160. tmp2 = C_last_row[ n ]; /* Q( -rshifts ) */
  161. num = 0; /* Q( -rshifts ) */
  162. nrg = silk_ADD32( CAb[ 0 ], CAf[ 0 ] ); /* Q( 1-rshifts ) */
  163. for( k = 0; k < n; k++ ) {
  164. Atmp_QA = Af_QA[ k ];
  165. lz = silk_CLZ32( silk_abs( Atmp_QA ) ) - 1;
  166. lz = silk_min( 32 - QA, lz );
  167. Atmp1 = silk_LSHIFT32( Atmp_QA, lz ); /* Q( QA + lz ) */
  168. tmp1 = silk_ADD_LSHIFT32( tmp1, silk_SMMUL( C_last_row[ n - k - 1 ], Atmp1 ), 32 - QA - lz ); /* Q( -rshifts ) */
  169. tmp2 = silk_ADD_LSHIFT32( tmp2, silk_SMMUL( C_first_row[ n - k - 1 ], Atmp1 ), 32 - QA - lz ); /* Q( -rshifts ) */
  170. num = silk_ADD_LSHIFT32( num, silk_SMMUL( CAb[ n - k ], Atmp1 ), 32 - QA - lz ); /* Q( -rshifts ) */
  171. nrg = silk_ADD_LSHIFT32( nrg, silk_SMMUL( silk_ADD32( CAb[ k + 1 ], CAf[ k + 1 ] ),
  172. Atmp1 ), 32 - QA - lz ); /* Q( 1-rshifts ) */
  173. }
  174. CAf[ n + 1 ] = tmp1; /* Q( -rshifts ) */
  175. CAb[ n + 1 ] = tmp2; /* Q( -rshifts ) */
  176. num = silk_ADD32( num, tmp2 ); /* Q( -rshifts ) */
  177. num = silk_LSHIFT32( -num, 1 ); /* Q( 1-rshifts ) */
  178. /* Calculate the next order reflection (parcor) coefficient */
  179. if( silk_abs( num ) < nrg ) {
  180. rc_Q31 = silk_DIV32_varQ( num, nrg, 31 );
  181. } else {
  182. rc_Q31 = ( num > 0 ) ? silk_int32_MAX : silk_int32_MIN;
  183. }
  184. /* Update inverse prediction gain */
  185. tmp1 = ( (opus_int32)1 << 30 ) - silk_SMMUL( rc_Q31, rc_Q31 );
  186. tmp1 = silk_LSHIFT( silk_SMMUL( invGain_Q30, tmp1 ), 2 );
  187. if( tmp1 <= minInvGain_Q30 ) {
  188. /* Max prediction gain exceeded; set reflection coefficient such that max prediction gain is exactly hit */
  189. tmp2 = ( (opus_int32)1 << 30 ) - silk_DIV32_varQ( minInvGain_Q30, invGain_Q30, 30 ); /* Q30 */
  190. rc_Q31 = silk_SQRT_APPROX( tmp2 ); /* Q15 */
  191. if( rc_Q31 > 0 ) {
  192. /* Newton-Raphson iteration */
  193. rc_Q31 = silk_RSHIFT32( rc_Q31 + silk_DIV32( tmp2, rc_Q31 ), 1 ); /* Q15 */
  194. rc_Q31 = silk_LSHIFT32( rc_Q31, 16 ); /* Q31 */
  195. if( num < 0 ) {
  196. /* Ensure adjusted reflection coefficients has the original sign */
  197. rc_Q31 = -rc_Q31;
  198. }
  199. }
  200. invGain_Q30 = minInvGain_Q30;
  201. reached_max_gain = 1;
  202. } else {
  203. invGain_Q30 = tmp1;
  204. }
  205. /* Update the AR coefficients */
  206. for( k = 0; k < (n + 1) >> 1; k++ ) {
  207. tmp1 = Af_QA[ k ]; /* QA */
  208. tmp2 = Af_QA[ n - k - 1 ]; /* QA */
  209. Af_QA[ k ] = silk_ADD_LSHIFT32( tmp1, silk_SMMUL( tmp2, rc_Q31 ), 1 ); /* QA */
  210. Af_QA[ n - k - 1 ] = silk_ADD_LSHIFT32( tmp2, silk_SMMUL( tmp1, rc_Q31 ), 1 ); /* QA */
  211. }
  212. Af_QA[ n ] = silk_RSHIFT32( rc_Q31, 31 - QA ); /* QA */
  213. if( reached_max_gain ) {
  214. /* Reached max prediction gain; set remaining coefficients to zero and exit loop */
  215. for( k = n + 1; k < D; k++ ) {
  216. Af_QA[ k ] = 0;
  217. }
  218. break;
  219. }
  220. /* Update C * Af and C * Ab */
  221. for( k = 0; k <= n + 1; k++ ) {
  222. tmp1 = CAf[ k ]; /* Q( -rshifts ) */
  223. tmp2 = CAb[ n - k + 1 ]; /* Q( -rshifts ) */
  224. CAf[ k ] = silk_ADD_LSHIFT32( tmp1, silk_SMMUL( tmp2, rc_Q31 ), 1 ); /* Q( -rshifts ) */
  225. CAb[ n - k + 1 ] = silk_ADD_LSHIFT32( tmp2, silk_SMMUL( tmp1, rc_Q31 ), 1 ); /* Q( -rshifts ) */
  226. }
  227. }
  228. if( reached_max_gain ) {
  229. for( k = 0; k < D; k++ ) {
  230. /* Scale coefficients */
  231. A_Q16[ k ] = -silk_RSHIFT_ROUND( Af_QA[ k ], QA - 16 );
  232. }
  233. /* Subtract energy of preceding samples from C0 */
  234. if( rshifts > 0 ) {
  235. for( s = 0; s < nb_subfr; s++ ) {
  236. x_ptr = x + s * subfr_length;
  237. C0 -= (opus_int32)silk_RSHIFT64( silk_inner_prod16_aligned_64( x_ptr, x_ptr, D, arch ), rshifts );
  238. }
  239. } else {
  240. for( s = 0; s < nb_subfr; s++ ) {
  241. x_ptr = x + s * subfr_length;
  242. C0 -= silk_LSHIFT32( silk_inner_prod_aligned( x_ptr, x_ptr, D, arch), -rshifts);
  243. }
  244. }
  245. /* Approximate residual energy */
  246. *res_nrg = silk_LSHIFT( silk_SMMUL( invGain_Q30, C0 ), 2 );
  247. *res_nrg_Q = -rshifts;
  248. } else {
  249. /* Return residual energy */
  250. nrg = CAf[ 0 ]; /* Q( -rshifts ) */
  251. tmp1 = (opus_int32)1 << 16; /* Q16 */
  252. for( k = 0; k < D; k++ ) {
  253. Atmp1 = silk_RSHIFT_ROUND( Af_QA[ k ], QA - 16 ); /* Q16 */
  254. nrg = silk_SMLAWW( nrg, CAf[ k + 1 ], Atmp1 ); /* Q( -rshifts ) */
  255. tmp1 = silk_SMLAWW( tmp1, Atmp1, Atmp1 ); /* Q16 */
  256. A_Q16[ k ] = -Atmp1;
  257. }
  258. *res_nrg = silk_SMLAWW( nrg, silk_SMMUL( SILK_FIX_CONST( FIND_LPC_COND_FAC, 32 ), C0 ), -tmp1 );/* Q( -rshifts ) */
  259. *res_nrg_Q = -rshifts;
  260. }
  261. }