find_LPC_FLP.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 "define.h"
  31. #include "main_FLP.h"
  32. #include "tuning_parameters.h"
  33. /* LPC analysis */
  34. void silk_find_LPC_FLP(
  35. silk_encoder_state *psEncC, /* I/O Encoder state */
  36. opus_int16 NLSF_Q15[], /* O NLSFs */
  37. const silk_float x[], /* I Input signal */
  38. const silk_float minInvGain /* I Inverse of max prediction gain */
  39. )
  40. {
  41. opus_int k, subfr_length;
  42. silk_float a[ MAX_LPC_ORDER ];
  43. /* Used only for NLSF interpolation */
  44. silk_float res_nrg, res_nrg_2nd, res_nrg_interp;
  45. opus_int16 NLSF0_Q15[ MAX_LPC_ORDER ];
  46. silk_float a_tmp[ MAX_LPC_ORDER ];
  47. silk_float LPC_res[ MAX_FRAME_LENGTH + MAX_NB_SUBFR * MAX_LPC_ORDER ];
  48. subfr_length = psEncC->subfr_length + psEncC->predictLPCOrder;
  49. /* Default: No interpolation */
  50. psEncC->indices.NLSFInterpCoef_Q2 = 4;
  51. /* Burg AR analysis for the full frame */
  52. res_nrg = silk_burg_modified_FLP( a, x, minInvGain, subfr_length, psEncC->nb_subfr, psEncC->predictLPCOrder );
  53. if( psEncC->useInterpolatedNLSFs && !psEncC->first_frame_after_reset && psEncC->nb_subfr == MAX_NB_SUBFR ) {
  54. /* Optimal solution for last 10 ms; subtract residual energy here, as that's easier than */
  55. /* adding it to the residual energy of the first 10 ms in each iteration of the search below */
  56. res_nrg -= silk_burg_modified_FLP( a_tmp, x + ( MAX_NB_SUBFR / 2 ) * subfr_length, minInvGain, subfr_length, MAX_NB_SUBFR / 2, psEncC->predictLPCOrder );
  57. /* Convert to NLSFs */
  58. silk_A2NLSF_FLP( NLSF_Q15, a_tmp, psEncC->predictLPCOrder );
  59. /* Search over interpolation indices to find the one with lowest residual energy */
  60. res_nrg_2nd = silk_float_MAX;
  61. for( k = 3; k >= 0; k-- ) {
  62. /* Interpolate NLSFs for first half */
  63. silk_interpolate( NLSF0_Q15, psEncC->prev_NLSFq_Q15, NLSF_Q15, k, psEncC->predictLPCOrder );
  64. /* Convert to LPC for residual energy evaluation */
  65. silk_NLSF2A_FLP( a_tmp, NLSF0_Q15, psEncC->predictLPCOrder );
  66. /* Calculate residual energy with LSF interpolation */
  67. silk_LPC_analysis_filter_FLP( LPC_res, a_tmp, x, 2 * subfr_length, psEncC->predictLPCOrder );
  68. res_nrg_interp = (silk_float)(
  69. silk_energy_FLP( LPC_res + psEncC->predictLPCOrder, subfr_length - psEncC->predictLPCOrder ) +
  70. silk_energy_FLP( LPC_res + psEncC->predictLPCOrder + subfr_length, subfr_length - psEncC->predictLPCOrder ) );
  71. /* Determine whether current interpolated NLSFs are best so far */
  72. if( res_nrg_interp < res_nrg ) {
  73. /* Interpolation has lower residual energy */
  74. res_nrg = res_nrg_interp;
  75. psEncC->indices.NLSFInterpCoef_Q2 = (opus_int8)k;
  76. } else if( res_nrg_interp > res_nrg_2nd ) {
  77. /* No reason to continue iterating - residual energies will continue to climb */
  78. break;
  79. }
  80. res_nrg_2nd = res_nrg_interp;
  81. }
  82. }
  83. if( psEncC->indices.NLSFInterpCoef_Q2 == 4 ) {
  84. /* NLSF interpolation is currently inactive, calculate NLSFs from full frame AR coefficients */
  85. silk_A2NLSF_FLP( NLSF_Q15, a, psEncC->predictLPCOrder );
  86. }
  87. silk_assert( psEncC->indices.NLSFInterpCoef_Q2 == 4 ||
  88. ( psEncC->useInterpolatedNLSFs && !psEncC->first_frame_after_reset && psEncC->nb_subfr == MAX_NB_SUBFR ) );
  89. }