PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/jni/silk/src/SKP_Silk_find_LTP_FIX.c

https://github.com/rbochet/batphone
C | 234 lines | 157 code | 33 blank | 44 comment | 22 complexity | b5b26ba5d31ff85df21c9e990592ee1e MD5 | raw file
  1. /***********************************************************************
  2. Copyright (c) 2006-2010, Skype Limited. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, (subject to the limitations in the disclaimer below)
  5. are permitted provided that the following conditions 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 Skype Limited, nor the names of specific
  12. contributors, may be used to endorse or promote products derived from
  13. this software without specific prior written permission.
  14. NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
  15. BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  16. CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  17. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  18. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  19. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  22. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  23. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. ***********************************************************************/
  27. #include "SKP_Silk_main_FIX.h"
  28. void SKP_Silk_fit_LTP(
  29. SKP_int32 LTP_coefs_Q16[ LTP_ORDER ],
  30. SKP_int16 LTP_coefs_Q14[ LTP_ORDER ]
  31. );
  32. void SKP_Silk_find_LTP_FIX(
  33. SKP_int16 b_Q14[ NB_SUBFR * LTP_ORDER ], /* O LTP coefs */
  34. SKP_int32 WLTP[ NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* O Weight for LTP quantization */
  35. SKP_int *LTPredCodGain_Q7, /* O LTP coding gain */
  36. const SKP_int16 r_first[], /* I residual signal after LPC signal + state for first 10 ms */
  37. const SKP_int16 r_last[], /* I residual signal after LPC signal + state for last 10 ms */
  38. const SKP_int lag[ NB_SUBFR ], /* I LTP lags */
  39. const SKP_int32 Wght_Q15[ NB_SUBFR ], /* I weights */
  40. const SKP_int subfr_length, /* I subframe length */
  41. const SKP_int mem_offset, /* I number of samples in LTP memory */
  42. SKP_int corr_rshifts[ NB_SUBFR ] /* O right shifts applied to correlations */
  43. )
  44. {
  45. SKP_int i, k, lshift;
  46. const SKP_int16 *r_ptr, *lag_ptr;
  47. SKP_int16 *b_Q14_ptr;
  48. SKP_int32 regu;
  49. SKP_int32 *WLTP_ptr;
  50. SKP_int32 b_Q16[ LTP_ORDER ], delta_b_Q14[ LTP_ORDER ], d_Q14[ NB_SUBFR ], nrg[ NB_SUBFR ], g_Q26;
  51. SKP_int32 w[ NB_SUBFR ], WLTP_max, max_abs_d_Q14, max_w_bits;
  52. SKP_int32 temp32, denom32;
  53. SKP_int extra_shifts;
  54. SKP_int rr_shifts, maxRshifts, maxRshifts_wxtra, LZs;
  55. SKP_int32 LPC_res_nrg, LPC_LTP_res_nrg, div_Q16;
  56. SKP_int32 Rr[ LTP_ORDER ], rr[ NB_SUBFR ];
  57. SKP_int32 wd, m_Q12;
  58. b_Q14_ptr = b_Q14;
  59. WLTP_ptr = WLTP;
  60. r_ptr = &r_first[ mem_offset ];
  61. for( k = 0; k < NB_SUBFR; k++ ) {
  62. if( k == ( NB_SUBFR >> 1 ) ) { /* shift residual for last 10 ms */
  63. r_ptr = &r_last[ mem_offset ];
  64. }
  65. lag_ptr = r_ptr - ( lag[ k ] + LTP_ORDER / 2 );
  66. SKP_Silk_sum_sqr_shift( &rr[ k ], &rr_shifts, r_ptr, subfr_length ); /* rr[ k ] in Q( -rr_shifts ) */
  67. /* Assure headroom */
  68. LZs = SKP_Silk_CLZ32( rr[k] );
  69. if( LZs < LTP_CORRS_HEAD_ROOM ) {
  70. rr[ k ] = SKP_RSHIFT_ROUND( rr[ k ], LTP_CORRS_HEAD_ROOM - LZs );
  71. rr_shifts += (LTP_CORRS_HEAD_ROOM - LZs);
  72. }
  73. corr_rshifts[ k ] = rr_shifts;
  74. SKP_Silk_corrMatrix_FIX( lag_ptr, subfr_length, LTP_ORDER, WLTP_ptr, &corr_rshifts[ k ] ); /* WLTP_fix_ptr in Q( -corr_rshifts[ k ] ) */
  75. /* The correlation vector always have lower max abs value than rr and/or RR so head room is assured */
  76. SKP_Silk_corrVector_FIX( lag_ptr, r_ptr, subfr_length, LTP_ORDER, Rr, corr_rshifts[ k ] ); /* Rr_fix_ptr in Q( -corr_rshifts[ k ] ) */
  77. if( corr_rshifts[ k ] > rr_shifts ) {
  78. rr[ k ] = SKP_RSHIFT( rr[ k ], corr_rshifts[ k ] - rr_shifts ); /* rr[ k ] in Q( -corr_rshifts[ k ] ) */
  79. }
  80. SKP_assert( rr[ k ] >= 0 );
  81. regu = SKP_SMULWB( rr[ k ] + 1, LTP_DAMPING_Q16 );
  82. SKP_Silk_regularize_correlations_FIX( WLTP_ptr, &rr[k], regu, LTP_ORDER );
  83. SKP_Silk_solve_LDL_FIX( WLTP_ptr, LTP_ORDER, Rr, b_Q16 ); /* WLTP_fix_ptr and Rr_fix_ptr both in Q(-corr_rshifts[k]) */
  84. /* Limit and store in Q14 */
  85. SKP_Silk_fit_LTP( b_Q16, b_Q14_ptr );
  86. /* Calculate residual energy */
  87. nrg[ k ] = SKP_Silk_residual_energy16_covar_FIX( b_Q14_ptr, WLTP_ptr, Rr, rr[ k ], LTP_ORDER, 14 ); /* nrg_fix in Q( -corr_rshifts[ k ] ) */
  88. /* temp = Wght[ k ] / ( nrg[ k ] * Wght[ k ] + 0.01f * subfr_length ); */
  89. extra_shifts = SKP_min_int( corr_rshifts[ k ], LTP_CORRS_HEAD_ROOM );
  90. denom32 = SKP_LSHIFT_SAT32( SKP_SMULWB( nrg[ k ], Wght_Q15[ k ] ), 1 + extra_shifts ) + /* Q( -corr_rshifts[ k ] + extra_shifts ) */
  91. SKP_RSHIFT( SKP_SMULWB( subfr_length, 655 ), corr_rshifts[ k ] - extra_shifts ); /* Q( -corr_rshifts[ k ] + extra_shifts ) */
  92. denom32 = SKP_max( denom32, 1 );
  93. SKP_assert( ((SKP_int64)Wght_Q15[ k ] << 16 ) < SKP_int32_MAX ); /* Wght always < 0.5 in Q0 */
  94. temp32 = SKP_DIV32( SKP_LSHIFT( ( SKP_int32 )Wght_Q15[ k ], 16 ), denom32 ); /* Q( 15 + 16 + corr_rshifts[k] - extra_shifts ) */
  95. temp32 = SKP_RSHIFT( temp32, 31 + corr_rshifts[ k ] - extra_shifts - 26 ); /* Q26 */
  96. /* Limit temp such that the below scaling never wraps around */
  97. WLTP_max = 0;
  98. for( i = 0; i < LTP_ORDER * LTP_ORDER; i++ ) {
  99. WLTP_max = SKP_max( WLTP_ptr[ i ], WLTP_max );
  100. }
  101. lshift = SKP_Silk_CLZ32( WLTP_max ) - 1 - 3; /* keep 3 bits free for vq_nearest_neighbor_fix */
  102. SKP_assert( 26 - 18 + lshift >= 0 );
  103. if( 26 - 18 + lshift < 31 ) {
  104. temp32 = SKP_min_32( temp32, SKP_LSHIFT( ( SKP_int32 )1, 26 - 18 + lshift ) );
  105. }
  106. SKP_Silk_scale_vector32_Q26_lshift_18( WLTP_ptr, temp32, LTP_ORDER * LTP_ORDER ); /* WLTP_ptr in Q( 18 - corr_rshifts[ k ] ) */
  107. w[ k ] = matrix_ptr( WLTP_ptr, ( LTP_ORDER >> 1 ), ( LTP_ORDER >> 1 ), LTP_ORDER ); /* w in Q( 18 - corr_rshifts[ k ] ) */
  108. SKP_assert( w[k] >= 0 );
  109. r_ptr += subfr_length;
  110. b_Q14_ptr += LTP_ORDER;
  111. WLTP_ptr += LTP_ORDER * LTP_ORDER;
  112. }
  113. maxRshifts = 0;
  114. for( k = 0; k < NB_SUBFR; k++ ) {
  115. maxRshifts = SKP_max_int( corr_rshifts[ k ], maxRshifts );
  116. }
  117. /* compute LTP coding gain */
  118. if( LTPredCodGain_Q7 != NULL ) {
  119. LPC_LTP_res_nrg = 0;
  120. LPC_res_nrg = 0;
  121. SKP_assert( LTP_CORRS_HEAD_ROOM >= 2 ); /* Check that no overflow will happen when adding */
  122. for( k = 0; k < NB_SUBFR; k++ ) {
  123. LPC_res_nrg = SKP_ADD32( LPC_res_nrg, SKP_RSHIFT( SKP_ADD32( SKP_SMULWB( rr[ k ], Wght_Q15[ k ] ), 1 ), 1 + ( maxRshifts - corr_rshifts[ k ] ) ) ); /* Q( -maxRshifts ) */
  124. LPC_LTP_res_nrg = SKP_ADD32( LPC_LTP_res_nrg, SKP_RSHIFT( SKP_ADD32( SKP_SMULWB( nrg[ k ], Wght_Q15[ k ] ), 1 ), 1 + ( maxRshifts - corr_rshifts[ k ] ) ) ); /* Q( -maxRshifts ) */
  125. }
  126. LPC_LTP_res_nrg = SKP_max( LPC_LTP_res_nrg, 1 ); /* avoid division by zero */
  127. div_Q16 = SKP_DIV32_varQ( LPC_res_nrg, LPC_LTP_res_nrg, 16 );
  128. *LTPredCodGain_Q7 = ( SKP_int )SKP_SMULBB( 3, SKP_Silk_lin2log( div_Q16 ) - ( 16 << 7 ) );
  129. SKP_assert( *LTPredCodGain_Q7 == ( SKP_int )SKP_SAT16( SKP_MUL( 3, SKP_Silk_lin2log( div_Q16 ) - ( 16 << 7 ) ) ) );
  130. }
  131. /* smoothing */
  132. /* d = sum( B, 1 ); */
  133. b_Q14_ptr = b_Q14;
  134. for( k = 0; k < NB_SUBFR; k++ ) {
  135. d_Q14[ k ] = 0;
  136. for( i = 0; i < LTP_ORDER; i++ ) {
  137. d_Q14[ k ] += b_Q14_ptr[ i ];
  138. }
  139. b_Q14_ptr += LTP_ORDER;
  140. }
  141. /* m = ( w * d' ) / ( sum( w ) + 1e-3 ); */
  142. /* Find maximum absolute value of d_Q14 and the bits used by w in Q0 */
  143. max_abs_d_Q14 = 0;
  144. max_w_bits = 0;
  145. for( k = 0; k < NB_SUBFR; k++ ) {
  146. max_abs_d_Q14 = SKP_max_32( max_abs_d_Q14, SKP_abs( d_Q14[ k ] ) );
  147. /* w[ k ] is in Q( 18 - corr_rshifts[ k ] ) */
  148. /* Find bits needed in Q( 18 - maxRshifts ) */
  149. max_w_bits = SKP_max_32( max_w_bits, 32 - SKP_Silk_CLZ32( w[ k ] ) + corr_rshifts[ k ] - maxRshifts );
  150. }
  151. /* max_abs_d_Q14 = (5 << 15); worst case, i.e. LTP_ORDER * -SKP_int16_MIN */
  152. SKP_assert( max_abs_d_Q14 <= ( 5 << 15 ) );
  153. /* How many bits is needed for w*d' in Q( 18 - maxRshifts ) in the worst case, of all d_Q14's being equal to max_abs_d_Q14 */
  154. extra_shifts = max_w_bits + 32 - SKP_Silk_CLZ32( max_abs_d_Q14 ) - 14;
  155. /* Subtract what we got available; bits in output var plus maxRshifts */
  156. extra_shifts -= ( 32 - 1 - 2 + maxRshifts ); /* Keep sign bit free as well as 2 bits for accumulation */
  157. extra_shifts = SKP_max_int( extra_shifts, 0 );
  158. maxRshifts_wxtra = maxRshifts + extra_shifts;
  159. temp32 = SKP_RSHIFT( 262, maxRshifts + extra_shifts ) + 1; /* 1e-3f in Q( 18 - (maxRshifts + extra_shifts) ) */
  160. wd = 0;
  161. for( k = 0; k < NB_SUBFR; k++ ) {
  162. /* w has at least 2 bits of headroom so no overflow should happen */
  163. temp32 = SKP_ADD32( temp32, SKP_RSHIFT( w[ k ], maxRshifts_wxtra - corr_rshifts[ k ] ) ); /* Q( 18 - maxRshifts_wxtra ) */
  164. wd = SKP_ADD32( wd, SKP_LSHIFT( SKP_SMULWW( SKP_RSHIFT( w[ k ], maxRshifts_wxtra - corr_rshifts[ k ] ), d_Q14[ k ] ), 2 ) ); /* Q( 18 - maxRshifts_wxtra ) */
  165. }
  166. m_Q12 = SKP_DIV32_varQ( wd, temp32, 12 );
  167. b_Q14_ptr = b_Q14;
  168. for( k = 0; k < NB_SUBFR; k++ ) {
  169. /* w_fix[ k ] from Q( 18 - corr_rshifts[ k ] ) to Q( 16 ) */
  170. if( 2 - corr_rshifts[k] > 0 ) {
  171. temp32 = SKP_RSHIFT( w[ k ], 2 - corr_rshifts[ k ] );
  172. } else {
  173. temp32 = SKP_LSHIFT_SAT32( w[ k ], corr_rshifts[ k ] - 2 );
  174. }
  175. g_Q26 = SKP_MUL(
  176. SKP_DIV32(
  177. LTP_SMOOTHING_Q26,
  178. SKP_RSHIFT( LTP_SMOOTHING_Q26, 10 ) + temp32 ), /* Q10 */
  179. SKP_LSHIFT_SAT32( SKP_SUB_SAT32( ( SKP_int32 )m_Q12, SKP_RSHIFT( d_Q14[ k ], 2 ) ), 4 ) ); /* Q16 */
  180. temp32 = 0;
  181. for( i = 0; i < LTP_ORDER; i++ ) {
  182. delta_b_Q14[ i ] = SKP_max_16( b_Q14_ptr[ i ], 1638 ); /* 1638_Q14 = 0.1_Q0 */
  183. temp32 += delta_b_Q14[ i ]; /* Q14 */
  184. }
  185. temp32 = SKP_DIV32( g_Q26, temp32 ); /* Q14->Q12 */
  186. for( i = 0; i < LTP_ORDER; i++ ) {
  187. b_Q14_ptr[ i ] = SKP_LIMIT( ( SKP_int32 )b_Q14_ptr[ i ] + SKP_SMULWB( SKP_LSHIFT_SAT32( temp32, 4 ), delta_b_Q14[ i ] ), -16000, 28000 );
  188. }
  189. b_Q14_ptr += LTP_ORDER;
  190. }
  191. }
  192. void SKP_Silk_fit_LTP(
  193. SKP_int32 LTP_coefs_Q16[ LTP_ORDER ],
  194. SKP_int16 LTP_coefs_Q14[ LTP_ORDER ]
  195. )
  196. {
  197. SKP_int i;
  198. for( i = 0; i < LTP_ORDER; i++ ) {
  199. LTP_coefs_Q14[ i ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT_ROUND( LTP_coefs_Q16[ i ], 2 ) );
  200. }
  201. }