LP_variable_cutoff.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /*
  31. Elliptic/Cauer filters designed with 0.1 dB passband ripple,
  32. 80 dB minimum stopband attenuation, and
  33. [0.95 : 0.15 : 0.35] normalized cut off frequencies.
  34. */
  35. #include "main.h"
  36. /* Helper function, interpolates the filter taps */
  37. static OPUS_INLINE void silk_LP_interpolate_filter_taps(
  38. opus_int32 B_Q28[ TRANSITION_NB ],
  39. opus_int32 A_Q28[ TRANSITION_NA ],
  40. const opus_int ind,
  41. const opus_int32 fac_Q16
  42. )
  43. {
  44. opus_int nb, na;
  45. if( ind < TRANSITION_INT_NUM - 1 ) {
  46. if( fac_Q16 > 0 ) {
  47. if( fac_Q16 < 32768 ) { /* fac_Q16 is in range of a 16-bit int */
  48. /* Piece-wise linear interpolation of B and A */
  49. for( nb = 0; nb < TRANSITION_NB; nb++ ) {
  50. B_Q28[ nb ] = silk_SMLAWB(
  51. silk_Transition_LP_B_Q28[ ind ][ nb ],
  52. silk_Transition_LP_B_Q28[ ind + 1 ][ nb ] -
  53. silk_Transition_LP_B_Q28[ ind ][ nb ],
  54. fac_Q16 );
  55. }
  56. for( na = 0; na < TRANSITION_NA; na++ ) {
  57. A_Q28[ na ] = silk_SMLAWB(
  58. silk_Transition_LP_A_Q28[ ind ][ na ],
  59. silk_Transition_LP_A_Q28[ ind + 1 ][ na ] -
  60. silk_Transition_LP_A_Q28[ ind ][ na ],
  61. fac_Q16 );
  62. }
  63. } else { /* ( fac_Q16 - ( 1 << 16 ) ) is in range of a 16-bit int */
  64. silk_assert( fac_Q16 - ( 1 << 16 ) == silk_SAT16( fac_Q16 - ( 1 << 16 ) ) );
  65. /* Piece-wise linear interpolation of B and A */
  66. for( nb = 0; nb < TRANSITION_NB; nb++ ) {
  67. B_Q28[ nb ] = silk_SMLAWB(
  68. silk_Transition_LP_B_Q28[ ind + 1 ][ nb ],
  69. silk_Transition_LP_B_Q28[ ind + 1 ][ nb ] -
  70. silk_Transition_LP_B_Q28[ ind ][ nb ],
  71. fac_Q16 - ( (opus_int32)1 << 16 ) );
  72. }
  73. for( na = 0; na < TRANSITION_NA; na++ ) {
  74. A_Q28[ na ] = silk_SMLAWB(
  75. silk_Transition_LP_A_Q28[ ind + 1 ][ na ],
  76. silk_Transition_LP_A_Q28[ ind + 1 ][ na ] -
  77. silk_Transition_LP_A_Q28[ ind ][ na ],
  78. fac_Q16 - ( (opus_int32)1 << 16 ) );
  79. }
  80. }
  81. } else {
  82. silk_memcpy( B_Q28, silk_Transition_LP_B_Q28[ ind ], TRANSITION_NB * sizeof( opus_int32 ) );
  83. silk_memcpy( A_Q28, silk_Transition_LP_A_Q28[ ind ], TRANSITION_NA * sizeof( opus_int32 ) );
  84. }
  85. } else {
  86. silk_memcpy( B_Q28, silk_Transition_LP_B_Q28[ TRANSITION_INT_NUM - 1 ], TRANSITION_NB * sizeof( opus_int32 ) );
  87. silk_memcpy( A_Q28, silk_Transition_LP_A_Q28[ TRANSITION_INT_NUM - 1 ], TRANSITION_NA * sizeof( opus_int32 ) );
  88. }
  89. }
  90. /* Low-pass filter with variable cutoff frequency based on */
  91. /* piece-wise linear interpolation between elliptic filters */
  92. /* Start by setting psEncC->mode <> 0; */
  93. /* Deactivate by setting psEncC->mode = 0; */
  94. void silk_LP_variable_cutoff(
  95. silk_LP_state *psLP, /* I/O LP filter state */
  96. opus_int16 *frame, /* I/O Low-pass filtered output signal */
  97. const opus_int frame_length /* I Frame length */
  98. )
  99. {
  100. opus_int32 B_Q28[ TRANSITION_NB ], A_Q28[ TRANSITION_NA ], fac_Q16 = 0;
  101. opus_int ind = 0;
  102. silk_assert( psLP->transition_frame_no >= 0 && psLP->transition_frame_no <= TRANSITION_FRAMES );
  103. /* Run filter if needed */
  104. if( psLP->mode != 0 ) {
  105. /* Calculate index and interpolation factor for interpolation */
  106. #if( TRANSITION_INT_STEPS == 64 )
  107. fac_Q16 = silk_LSHIFT( TRANSITION_FRAMES - psLP->transition_frame_no, 16 - 6 );
  108. #else
  109. fac_Q16 = silk_DIV32_16( silk_LSHIFT( TRANSITION_FRAMES - psLP->transition_frame_no, 16 ), TRANSITION_FRAMES );
  110. #endif
  111. ind = silk_RSHIFT( fac_Q16, 16 );
  112. fac_Q16 -= silk_LSHIFT( ind, 16 );
  113. silk_assert( ind >= 0 );
  114. silk_assert( ind < TRANSITION_INT_NUM );
  115. /* Interpolate filter coefficients */
  116. silk_LP_interpolate_filter_taps( B_Q28, A_Q28, ind, fac_Q16 );
  117. /* Update transition frame number for next frame */
  118. psLP->transition_frame_no = silk_LIMIT( psLP->transition_frame_no + psLP->mode, 0, TRANSITION_FRAMES );
  119. /* ARMA low-pass filtering */
  120. silk_assert( TRANSITION_NB == 3 && TRANSITION_NA == 2 );
  121. silk_biquad_alt_stride1( frame, B_Q28, A_Q28, psLP->In_LP_State, frame, frame_length);
  122. }
  123. }