celp_filters.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "celp_filters.h"
  25. void ff_celp_convolve_circ(
  26. int16_t* fc_out,
  27. const int16_t* fc_in,
  28. const int16_t* filter,
  29. int len)
  30. {
  31. int i, k;
  32. memset(fc_out, 0, len * sizeof(int16_t));
  33. /* Since there are few pulses over an entire subframe (i.e. almost
  34. all fc_in[i] are zero) it is faster to loop over fc_in first. */
  35. for(i=0; i<len; i++)
  36. {
  37. if(fc_in[i])
  38. {
  39. for(k=0; k<i; k++)
  40. fc_out[k] += (fc_in[i] * filter[len + k - i]) >> 15;
  41. for(k=i; k<len; k++)
  42. fc_out[k] += (fc_in[i] * filter[ k - i]) >> 15;
  43. }
  44. }
  45. }
  46. int ff_celp_lp_synthesis_filter(
  47. int16_t *out,
  48. const int16_t* filter_coeffs,
  49. const int16_t* in,
  50. int buffer_length,
  51. int filter_length,
  52. int stop_on_overflow,
  53. int rounder)
  54. {
  55. int i,n;
  56. // These two lines are to avoid a -1 subtraction in the main loop
  57. filter_length++;
  58. filter_coeffs--;
  59. for(n=0; n<buffer_length; n++)
  60. {
  61. int sum = rounder;
  62. for(i=1; i<filter_length; i++)
  63. sum -= filter_coeffs[i] * out[n-i];
  64. sum = (sum >> 12) + in[n];
  65. if(sum + 0x8000 > 0xFFFFU)
  66. {
  67. if(stop_on_overflow)
  68. return 1;
  69. sum = (sum >> 31) ^ 32767;
  70. }
  71. out[n] = sum;
  72. }
  73. return 0;
  74. }
  75. void ff_celp_lp_synthesis_filterf(
  76. float *out,
  77. const float* filter_coeffs,
  78. const float* in,
  79. int buffer_length,
  80. int filter_length)
  81. {
  82. int i,n;
  83. // These two lines are to avoid a -1 subtraction in the main loop
  84. filter_length++;
  85. filter_coeffs--;
  86. for(n=0; n<buffer_length; n++)
  87. {
  88. out[n] = in[n];
  89. for(i=1; i<filter_length; i++)
  90. out[n] -= filter_coeffs[i] * out[n-i];
  91. }
  92. }