wrappers_FLP.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_FLP.h"
  31. /* Wrappers. Calls flp / fix code */
  32. /* Convert AR filter coefficients to NLSF parameters */
  33. void silk_A2NLSF_FLP(
  34. opus_int16 *NLSF_Q15, /* O NLSF vector [ LPC_order ] */
  35. const silk_float *pAR, /* I LPC coefficients [ LPC_order ] */
  36. const opus_int LPC_order /* I LPC order */
  37. )
  38. {
  39. opus_int i;
  40. opus_int32 a_fix_Q16[ MAX_LPC_ORDER ];
  41. for( i = 0; i < LPC_order; i++ ) {
  42. a_fix_Q16[ i ] = silk_float2int( pAR[ i ] * 65536.0f );
  43. }
  44. silk_A2NLSF( NLSF_Q15, a_fix_Q16, LPC_order );
  45. }
  46. /* Convert LSF parameters to AR prediction filter coefficients */
  47. void silk_NLSF2A_FLP(
  48. silk_float *pAR, /* O LPC coefficients [ LPC_order ] */
  49. const opus_int16 *NLSF_Q15, /* I NLSF vector [ LPC_order ] */
  50. const opus_int LPC_order, /* I LPC order */
  51. int arch /* I Run-time architecture */
  52. )
  53. {
  54. opus_int i;
  55. opus_int16 a_fix_Q12[ MAX_LPC_ORDER ];
  56. silk_NLSF2A( a_fix_Q12, NLSF_Q15, LPC_order, arch );
  57. for( i = 0; i < LPC_order; i++ ) {
  58. pAR[ i ] = ( silk_float )a_fix_Q12[ i ] * ( 1.0f / 4096.0f );
  59. }
  60. }
  61. /******************************************/
  62. /* Floating-point NLSF processing wrapper */
  63. /******************************************/
  64. void silk_process_NLSFs_FLP(
  65. silk_encoder_state *psEncC, /* I/O Encoder state */
  66. silk_float PredCoef[ 2 ][ MAX_LPC_ORDER ], /* O Prediction coefficients */
  67. opus_int16 NLSF_Q15[ MAX_LPC_ORDER ], /* I/O Normalized LSFs (quant out) (0 - (2^15-1)) */
  68. const opus_int16 prev_NLSF_Q15[ MAX_LPC_ORDER ] /* I Previous Normalized LSFs (0 - (2^15-1)) */
  69. )
  70. {
  71. opus_int i, j;
  72. opus_int16 PredCoef_Q12[ 2 ][ MAX_LPC_ORDER ];
  73. silk_process_NLSFs( psEncC, PredCoef_Q12, NLSF_Q15, prev_NLSF_Q15);
  74. for( j = 0; j < 2; j++ ) {
  75. for( i = 0; i < psEncC->predictLPCOrder; i++ ) {
  76. PredCoef[ j ][ i ] = ( silk_float )PredCoef_Q12[ j ][ i ] * ( 1.0f / 4096.0f );
  77. }
  78. }
  79. }
  80. /****************************************/
  81. /* Floating-point Silk NSQ wrapper */
  82. /****************************************/
  83. void silk_NSQ_wrapper_FLP(
  84. silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */
  85. silk_encoder_control_FLP *psEncCtrl, /* I/O Encoder control FLP */
  86. SideInfoIndices *psIndices, /* I/O Quantization indices */
  87. silk_nsq_state *psNSQ, /* I/O Noise Shaping Quantzation state */
  88. opus_int8 pulses[], /* O Quantized pulse signal */
  89. const silk_float x[] /* I Prefiltered input signal */
  90. )
  91. {
  92. opus_int i, j;
  93. opus_int16 x16[ MAX_FRAME_LENGTH ];
  94. opus_int32 Gains_Q16[ MAX_NB_SUBFR ];
  95. silk_DWORD_ALIGN opus_int16 PredCoef_Q12[ 2 ][ MAX_LPC_ORDER ];
  96. opus_int16 LTPCoef_Q14[ LTP_ORDER * MAX_NB_SUBFR ];
  97. opus_int LTP_scale_Q14;
  98. /* Noise shaping parameters */
  99. opus_int16 AR_Q13[ MAX_NB_SUBFR * MAX_SHAPE_LPC_ORDER ];
  100. opus_int32 LF_shp_Q14[ MAX_NB_SUBFR ]; /* Packs two int16 coefficients per int32 value */
  101. opus_int Lambda_Q10;
  102. opus_int Tilt_Q14[ MAX_NB_SUBFR ];
  103. opus_int HarmShapeGain_Q14[ MAX_NB_SUBFR ];
  104. /* Convert control struct to fix control struct */
  105. /* Noise shape parameters */
  106. for( i = 0; i < psEnc->sCmn.nb_subfr; i++ ) {
  107. for( j = 0; j < psEnc->sCmn.shapingLPCOrder; j++ ) {
  108. AR_Q13[ i * MAX_SHAPE_LPC_ORDER + j ] = silk_float2int( psEncCtrl->AR[ i * MAX_SHAPE_LPC_ORDER + j ] * 8192.0f );
  109. }
  110. }
  111. for( i = 0; i < psEnc->sCmn.nb_subfr; i++ ) {
  112. LF_shp_Q14[ i ] = silk_LSHIFT32( silk_float2int( psEncCtrl->LF_AR_shp[ i ] * 16384.0f ), 16 ) |
  113. (opus_uint16)silk_float2int( psEncCtrl->LF_MA_shp[ i ] * 16384.0f );
  114. Tilt_Q14[ i ] = (opus_int)silk_float2int( psEncCtrl->Tilt[ i ] * 16384.0f );
  115. HarmShapeGain_Q14[ i ] = (opus_int)silk_float2int( psEncCtrl->HarmShapeGain[ i ] * 16384.0f );
  116. }
  117. Lambda_Q10 = ( opus_int )silk_float2int( psEncCtrl->Lambda * 1024.0f );
  118. /* prediction and coding parameters */
  119. for( i = 0; i < psEnc->sCmn.nb_subfr * LTP_ORDER; i++ ) {
  120. LTPCoef_Q14[ i ] = (opus_int16)silk_float2int( psEncCtrl->LTPCoef[ i ] * 16384.0f );
  121. }
  122. for( j = 0; j < 2; j++ ) {
  123. for( i = 0; i < psEnc->sCmn.predictLPCOrder; i++ ) {
  124. PredCoef_Q12[ j ][ i ] = (opus_int16)silk_float2int( psEncCtrl->PredCoef[ j ][ i ] * 4096.0f );
  125. }
  126. }
  127. for( i = 0; i < psEnc->sCmn.nb_subfr; i++ ) {
  128. Gains_Q16[ i ] = silk_float2int( psEncCtrl->Gains[ i ] * 65536.0f );
  129. silk_assert( Gains_Q16[ i ] > 0 );
  130. }
  131. if( psIndices->signalType == TYPE_VOICED ) {
  132. LTP_scale_Q14 = silk_LTPScales_table_Q14[ psIndices->LTP_scaleIndex ];
  133. } else {
  134. LTP_scale_Q14 = 0;
  135. }
  136. /* Convert input to fix */
  137. for( i = 0; i < psEnc->sCmn.frame_length; i++ ) {
  138. x16[ i ] = silk_float2int( x[ i ] );
  139. }
  140. /* Call NSQ */
  141. if( psEnc->sCmn.nStatesDelayedDecision > 1 || psEnc->sCmn.warping_Q16 > 0 ) {
  142. silk_NSQ_del_dec( &psEnc->sCmn, psNSQ, psIndices, x16, pulses, PredCoef_Q12[ 0 ], LTPCoef_Q14,
  143. AR_Q13, HarmShapeGain_Q14, Tilt_Q14, LF_shp_Q14, Gains_Q16, psEncCtrl->pitchL, Lambda_Q10, LTP_scale_Q14, psEnc->sCmn.arch );
  144. } else {
  145. silk_NSQ( &psEnc->sCmn, psNSQ, psIndices, x16, pulses, PredCoef_Q12[ 0 ], LTPCoef_Q14,
  146. AR_Q13, HarmShapeGain_Q14, Tilt_Q14, LF_shp_Q14, Gains_Q16, psEncCtrl->pitchL, Lambda_Q10, LTP_scale_Q14, psEnc->sCmn.arch );
  147. }
  148. }
  149. /***********************************************/
  150. /* Floating-point Silk LTP quantiation wrapper */
  151. /***********************************************/
  152. void silk_quant_LTP_gains_FLP(
  153. silk_float B[ MAX_NB_SUBFR * LTP_ORDER ], /* O Quantized LTP gains */
  154. opus_int8 cbk_index[ MAX_NB_SUBFR ], /* O Codebook index */
  155. opus_int8 *periodicity_index, /* O Periodicity index */
  156. opus_int32 *sum_log_gain_Q7, /* I/O Cumulative max prediction gain */
  157. silk_float *pred_gain_dB, /* O LTP prediction gain */
  158. const silk_float XX[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* I Correlation matrix */
  159. const silk_float xX[ MAX_NB_SUBFR * LTP_ORDER ], /* I Correlation vector */
  160. const opus_int subfr_len, /* I Number of samples per subframe */
  161. const opus_int nb_subfr, /* I Number of subframes */
  162. int arch /* I Run-time architecture */
  163. )
  164. {
  165. opus_int i, pred_gain_dB_Q7;
  166. opus_int16 B_Q14[ MAX_NB_SUBFR * LTP_ORDER ];
  167. opus_int32 XX_Q17[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ];
  168. opus_int32 xX_Q17[ MAX_NB_SUBFR * LTP_ORDER ];
  169. for( i = 0; i < nb_subfr * LTP_ORDER * LTP_ORDER; i++ ) {
  170. XX_Q17[ i ] = (opus_int32)silk_float2int( XX[ i ] * 131072.0f );
  171. }
  172. for( i = 0; i < nb_subfr * LTP_ORDER; i++ ) {
  173. xX_Q17[ i ] = (opus_int32)silk_float2int( xX[ i ] * 131072.0f );
  174. }
  175. silk_quant_LTP_gains( B_Q14, cbk_index, periodicity_index, sum_log_gain_Q7, &pred_gain_dB_Q7, XX_Q17, xX_Q17, subfr_len, nb_subfr, arch );
  176. for( i = 0; i < nb_subfr * LTP_ORDER; i++ ) {
  177. B[ i ] = (silk_float)B_Q14[ i ] * ( 1.0f / 16384.0f );
  178. }
  179. *pred_gain_dB = (silk_float)pred_gain_dB_Q7 * ( 1.0f / 128.0f );
  180. }