You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
6.5KB

  1. /*
  2. * various filters for ACELP-based codecs
  3. *
  4. * Copyright (c) 2008 Vladimir Voroshilov
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #ifndef FFMPEG_ACELP_FILTERS_H
  23. #define FFMPEG_ACELP_FILTERS_H
  24. #include <stdint.h>
  25. /**
  26. * low-pass FIR (Finite Impulse Response) filter coefficients
  27. *
  28. * A similar filter is named b30 in G.729.
  29. *
  30. * G.729 specification says:
  31. * b30 is based on Hamming windowed sinc functions, truncated at +/-29 and
  32. * padded with zeros at +/-30 b30[30]=0.
  33. * The filter has a cut-off frequency (-3 dB) at 3600 Hz in the oversampled
  34. * domain.
  35. *
  36. * After some analysis, I found this approximation:
  37. *
  38. * PI * x
  39. * Hamm(x,N) = 0.53836-0.46164*cos(--------)
  40. * N-1
  41. * ---
  42. * 2
  43. *
  44. * PI * x
  45. * Hamm'(x,k) = Hamm(x - k, 2*k+1) = 0.53836 + 0.46164*cos(--------)
  46. * k
  47. *
  48. * sin(PI * x)
  49. * Sinc(x) = ----------- (normalized sinc function)
  50. * PI * x
  51. *
  52. * h(t,B) = 2 * B * Sinc(2 * B * t) (impulse response of sinc low-pass filter)
  53. *
  54. * b(k,B, n) = Hamm'(n, k) * h(n, B)
  55. *
  56. *
  57. * 3600
  58. * B = ----
  59. * 8000
  60. *
  61. * 3600 - cut-off frequency
  62. * 8000 - sampling rate
  63. * k - filter order
  64. *
  65. * ff_acelp_interp_filter[6*i+j] = b(10, 3600/8000, i+j/6)
  66. *
  67. * The filter assumes the following order of fractions (X - integer delay):
  68. *
  69. * 1/3 precision: X 1/3 2/3 X 1/3 2/3 X
  70. * 1/6 precision: X 1/6 2/6 3/6 4/6 5/6 X 1/6 2/6 3/6 4/6 5/6 X
  71. *
  72. * The filter can be used for 1/3 precision, too, by
  73. * passing 2*pitch_delay_frac as third parameter to the interpolation routine.
  74. *
  75. */
  76. extern const int16_t ff_acelp_interp_filter[61];
  77. /**
  78. * \brief Generic interpolation routine
  79. * \param out [out] buffer for interpolated data
  80. * \param in input data
  81. * \param filter_coeffs interpolation filter coefficients (0.15)
  82. * \param precision filter is able to interpolate with 1/precision precision of pitch delay
  83. * \param pitch_delay_frac pitch delay, fractional part [0..precision-1]
  84. * \param filter_length filter length
  85. * \param length length of speech data to process
  86. *
  87. * filter_coeffs contains coefficients of the positive half of the symmetric
  88. * interpolation filter. filter_coeffs[0] should the central (unpaired) coefficient.
  89. * See ff_acelp_interp_filter fot example.
  90. *
  91. */
  92. void ff_acelp_interpolate(
  93. int16_t* out,
  94. const int16_t* in,
  95. const int16_t* filter_coeffs,
  96. int precision,
  97. int pitch_delay_frac,
  98. int filter_length,
  99. int length);
  100. /**
  101. * \brief Circularly convolve fixed vector with a phase dispersion impulse
  102. * response filter (D.6.2 of G.729 and 6.1.5 of AMR).
  103. * \param fc_out vector with filter applied
  104. * \param fc_in source vector
  105. * \param filter phase filter coefficients
  106. *
  107. * fc_out[n] = sum(i,0,len-1){ fc_in[i] * filter[(len + n - i)%len] }
  108. *
  109. * \note fc_in and fc_out should not overlap!
  110. */
  111. void ff_acelp_convolve_circ(
  112. int16_t* fc_out,
  113. const int16_t* fc_in,
  114. const int16_t* filter,
  115. int subframe_size);
  116. /**
  117. * \brief LP synthesis filter
  118. * \param out [out] pointer to output buffer
  119. * \param filter_coeffs filter coefficients (-0x8000 <= (3.12) < 0x8000)
  120. * \param in input signal
  121. * \param buffer_length amount of data to process
  122. * \param filter_length filter length (11 for 10th order LP filter)
  123. * \param stop_on_overflow 1 - return immediately if overflow occurs
  124. * 0 - ignore overflows
  125. *
  126. * \return 1 if overflow occurred, 0 - otherwise
  127. *
  128. * \note Output buffer must contain 10 samples of past
  129. * speech data before pointer.
  130. *
  131. * Routine applies 1/A(z) filter to given speech data.
  132. */
  133. int ff_acelp_lp_synthesis_filter(
  134. int16_t *out,
  135. const int16_t* filter_coeffs,
  136. const int16_t* in,
  137. int buffer_length,
  138. int filter_length,
  139. int stop_on_overflow);
  140. /**
  141. * \brief Calculates coefficients of weighted A(z/weight) filter.
  142. * \param out [out] weighted A(z/weight) result
  143. * filter (-0x8000 <= (3.12) < 0x8000)
  144. * \param in source filter (-0x8000 <= (3.12) < 0x8000)
  145. * \param weight_pow array containing weight^i (-0x8000 <= (0.15) < 0x8000)
  146. * \param filter_length filter length (11 for 10th order LP filter)
  147. *
  148. * out[i]=weight_pow[i]*in[i] , i=0..9
  149. */
  150. void ff_acelp_weighted_filter(
  151. int16_t *out,
  152. const int16_t* in,
  153. const int16_t *weight_pow,
  154. int filter_length);
  155. /**
  156. * \brief high-pass filtering and upscaling (4.2.5 of G.729)
  157. * \param out [out] output buffer for filtered speech data
  158. * \param hpf_f [in/out] past filtered data from previous (2 items long)
  159. * frames (-0x20000000 <= (14.13) < 0x20000000)
  160. * \param in speech data to process
  161. * \param length input data size
  162. *
  163. * out[i] = 0.93980581 * in[i] - 1.8795834 * in[i-1] + 0.93980581 * in[i-2] +
  164. * 1.9330735 * out[i-1] - 0.93589199 * out[i-2]
  165. *
  166. * The filter has a cut-off frequency of 100Hz
  167. *
  168. * \note Two items before the top of the out buffer must contain two items from the
  169. * tail of the previous subframe.
  170. *
  171. * \remark It is safe to pass the same array in in and out parameters.
  172. *
  173. * \remark AMR uses mostly the same filter (cut-off frequency 60Hz, same formula,
  174. * but constants differs in 5th sign after comma). Fortunately in
  175. * fixed-point all coefficients are the same as in G.729. Thus this
  176. * routine can be used for the fixed-point AMR decoder, too.
  177. */
  178. void ff_acelp_high_pass_filter(
  179. int16_t* out,
  180. int hpf_f[2],
  181. const int16_t* in,
  182. int length);
  183. #endif /* FFMPEG_ACELP_FILTERS_H */