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.

203 lines
5.3KB

  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(int16_t* fc_out, const int16_t* fc_in,
  26. const int16_t* filter, int len)
  27. {
  28. int i, k;
  29. memset(fc_out, 0, len * sizeof(int16_t));
  30. /* Since there are few pulses over an entire subframe (i.e. almost
  31. all fc_in[i] are zero) it is faster to loop over fc_in first. */
  32. for (i = 0; i < len; i++) {
  33. if (fc_in[i]) {
  34. for (k = 0; k < i; k++)
  35. fc_out[k] += (fc_in[i] * filter[len + k - i]) >> 15;
  36. for (k = i; k < len; k++)
  37. fc_out[k] += (fc_in[i] * filter[ k - i]) >> 15;
  38. }
  39. }
  40. }
  41. void ff_celp_circ_addf(float *out, const float *in,
  42. const float *lagged, int lag, float fac, int n)
  43. {
  44. int k;
  45. for (k = 0; k < lag; k++)
  46. out[k] = in[k] + fac * lagged[n + k - lag];
  47. for (; k < n; k++)
  48. out[k] = in[k] + fac * lagged[ k - lag];
  49. }
  50. int ff_celp_lp_synthesis_filter(int16_t *out, const int16_t *filter_coeffs,
  51. const int16_t *in, int buffer_length,
  52. int filter_length, int stop_on_overflow,
  53. int rounder)
  54. {
  55. int i,n;
  56. for (n = 0; n < buffer_length; n++) {
  57. int sum = rounder;
  58. for (i = 1; i <= filter_length; i++)
  59. sum -= filter_coeffs[i-1] * out[n-i];
  60. sum = (sum >> 12) + in[n];
  61. if (sum + 0x8000 > 0xFFFFU) {
  62. if (stop_on_overflow)
  63. return 1;
  64. sum = (sum >> 31) ^ 32767;
  65. }
  66. out[n] = sum;
  67. }
  68. return 0;
  69. }
  70. void ff_celp_lp_synthesis_filterf(float *out, const float *filter_coeffs,
  71. const float* in, int buffer_length,
  72. int filter_length)
  73. {
  74. int i,n;
  75. float out0, out1, out2, out3;
  76. float old_out0, old_out1, old_out2, old_out3;
  77. float a,b,c;
  78. a = filter_coeffs[0];
  79. b = filter_coeffs[1];
  80. c = filter_coeffs[2];
  81. b -= filter_coeffs[0] * filter_coeffs[0];
  82. c -= filter_coeffs[1] * filter_coeffs[0];
  83. c -= filter_coeffs[0] * b;
  84. old_out0 = out[-4];
  85. old_out1 = out[-3];
  86. old_out2 = out[-2];
  87. old_out3 = out[-1];
  88. for (n = 0; n <= buffer_length - 4; n+=4) {
  89. float tmp0,tmp1,tmp2,tmp3;
  90. float val;
  91. out0 = in[0];
  92. out1 = in[1];
  93. out2 = in[2];
  94. out3 = in[3];
  95. out0 -= filter_coeffs[2] * old_out1;
  96. out1 -= filter_coeffs[2] * old_out2;
  97. out2 -= filter_coeffs[2] * old_out3;
  98. out0 -= filter_coeffs[1] * old_out2;
  99. out1 -= filter_coeffs[1] * old_out3;
  100. out0 -= filter_coeffs[0] * old_out3;
  101. val = filter_coeffs[3];
  102. out0 -= val * old_out0;
  103. out1 -= val * old_out1;
  104. out2 -= val * old_out2;
  105. out3 -= val * old_out3;
  106. old_out3 = out[-5];
  107. for (i = 5; i <= filter_length; i += 2) {
  108. val = filter_coeffs[i-1];
  109. out0 -= val * old_out3;
  110. out1 -= val * old_out0;
  111. out2 -= val * old_out1;
  112. out3 -= val * old_out2;
  113. old_out2 = out[-i-1];
  114. val = filter_coeffs[i];
  115. out0 -= val * old_out2;
  116. out1 -= val * old_out3;
  117. out2 -= val * old_out0;
  118. out3 -= val * old_out1;
  119. FFSWAP(float, old_out0, old_out2);
  120. old_out1 = old_out3;
  121. old_out3 = out[-i-2];
  122. }
  123. tmp0 = out0;
  124. tmp1 = out1;
  125. tmp2 = out2;
  126. tmp3 = out3;
  127. out3 -= a * tmp2;
  128. out2 -= a * tmp1;
  129. out1 -= a * tmp0;
  130. out3 -= b * tmp1;
  131. out2 -= b * tmp0;
  132. out3 -= c * tmp0;
  133. out[0] = out0;
  134. out[1] = out1;
  135. out[2] = out2;
  136. out[3] = out3;
  137. old_out0 = out0;
  138. old_out1 = out1;
  139. old_out2 = out2;
  140. old_out3 = out3;
  141. out += 4;
  142. in += 4;
  143. }
  144. out -= n;
  145. in -= n;
  146. for (; n < buffer_length; n++) {
  147. out[n] = in[n];
  148. for (i = 1; i <= filter_length; i++)
  149. out[n] -= filter_coeffs[i-1] * out[n-i];
  150. }
  151. }
  152. void ff_celp_lp_zero_synthesis_filterf(float *out, const float *filter_coeffs,
  153. const float *in, int buffer_length,
  154. int filter_length)
  155. {
  156. int i,n;
  157. for (n = 0; n < buffer_length; n++) {
  158. out[n] = in[n];
  159. for (i = 1; i <= filter_length; i++)
  160. out[n] += filter_coeffs[i-1] * in[n-i];
  161. }
  162. }