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.

125 lines
3.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. #include <inttypes.h>
  23. #include "avcodec.h"
  24. #include "acelp_filters.h"
  25. #define FRAC_BITS 13
  26. #include "mathops.h"
  27. void ff_acelp_convolve_circ(
  28. int16_t* fc_out,
  29. const int16_t* fc_in,
  30. const int16_t* filter,
  31. int subframe_size)
  32. {
  33. int i, k;
  34. memset(fc_out, 0, subframe_size * sizeof(int16_t));
  35. /* Since there are few pulses over an entire subframe (i.e. almost
  36. all fc_in[i] are zero) it is faster to swap two loops and process
  37. non-zero samples only. In the case of G.729D the buffer contains
  38. two non-zero samples before the call to ff_acelp_enhance_harmonics
  39. and, due to pitch_delay being bounded by [20; 143], a maximum
  40. of four non-zero samples for a total of 40 after the call. */
  41. for(i=0; i<subframe_size; i++)
  42. {
  43. if(fc_in[i])
  44. {
  45. for(k=0; k<i; k++)
  46. fc_out[k] += (fc_in[i] * filter[subframe_size + k - i]) >> 15;
  47. for(k=i; k<subframe_size; k++)
  48. fc_out[k] += (fc_in[i] * filter[k - i]) >> 15;
  49. }
  50. }
  51. }
  52. int ff_acelp_lp_synthesis_filter(
  53. int16_t *out,
  54. const int16_t* filter_coeffs,
  55. const int16_t* in,
  56. int buffer_length,
  57. int filter_length,
  58. int stop_on_overflow)
  59. {
  60. int i,n;
  61. for(n=0; n<buffer_length; n++)
  62. {
  63. int sum = 0x800;
  64. for(i=1; i<filter_length; i++)
  65. sum -= filter_coeffs[i] * out[n-i];
  66. sum = (sum >> 12) + in[n];
  67. /* Check for overflow */
  68. if(sum + 0x8000 > 0xFFFFU)
  69. {
  70. if(stop_on_overflow)
  71. return 1;
  72. sum = (sum >> 31) ^ 32767;
  73. }
  74. out[n] = sum;
  75. }
  76. return 0;
  77. }
  78. void ff_acelp_weighted_filter(
  79. int16_t *out,
  80. const int16_t* in,
  81. const int16_t *weight_pow,
  82. int filter_length)
  83. {
  84. int n;
  85. for(n=0; n<filter_length; n++)
  86. out[n] = (in[n] * weight_pow[n] + 0x4000) >> 15; /* (3.12) = (0.15) * (3.12) with rounding */
  87. }
  88. void ff_acelp_high_pass_filter(
  89. int16_t* out,
  90. int hpf_f[2],
  91. const int16_t* in,
  92. int length)
  93. {
  94. int i;
  95. int tmp;
  96. for(i=0; i<length; i++)
  97. {
  98. tmp = MULL(hpf_f[0], 15836); /* (14.13) = (13.13) * (1.13) */
  99. tmp += MULL(hpf_f[1], -7667); /* (13.13) = (13.13) * (0.13) */
  100. tmp += 7699 * (in[i] - 2*in[i-1] + in[i-2]); /* (14.13) = (0.13) * (14.0) */
  101. /* Multiplication by 2 with rounding can cause short type
  102. overflow, thus clipping is required. */
  103. out[i] = av_clip_int16((tmp + 0x800) >> 12); /* (15.0) = 2 * (13.13) = (14.13) */
  104. hpf_f[1] = hpf_f[0];
  105. hpf_f[0] = tmp;
  106. }
  107. }