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.

208 lines
5.5KB

  1. /*
  2. * various filters for ACELP-based codecs
  3. *
  4. * Copyright (c) 2008 Vladimir Voroshilov
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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. #if 0 // Unoptimized code path for improved readability
  76. for (n = 0; n < buffer_length; n++) {
  77. out[n] = in[n];
  78. for (i = 1; i <= filter_length; i++)
  79. out[n] -= filter_coeffs[i-1] * out[n-i];
  80. }
  81. #else
  82. float out0, out1, out2, out3;
  83. float old_out0, old_out1, old_out2, old_out3;
  84. float a,b,c;
  85. a = filter_coeffs[0];
  86. b = filter_coeffs[1];
  87. c = filter_coeffs[2];
  88. b -= filter_coeffs[0] * filter_coeffs[0];
  89. c -= filter_coeffs[1] * filter_coeffs[0];
  90. c -= filter_coeffs[0] * b;
  91. old_out0 = out[-4];
  92. old_out1 = out[-3];
  93. old_out2 = out[-2];
  94. old_out3 = out[-1];
  95. for (n = 0; n <= buffer_length - 4; n+=4) {
  96. float tmp0,tmp1,tmp2;
  97. float val;
  98. out0 = in[0];
  99. out1 = in[1];
  100. out2 = in[2];
  101. out3 = in[3];
  102. out0 -= filter_coeffs[2] * old_out1;
  103. out1 -= filter_coeffs[2] * old_out2;
  104. out2 -= filter_coeffs[2] * old_out3;
  105. out0 -= filter_coeffs[1] * old_out2;
  106. out1 -= filter_coeffs[1] * old_out3;
  107. out0 -= filter_coeffs[0] * old_out3;
  108. val = filter_coeffs[3];
  109. out0 -= val * old_out0;
  110. out1 -= val * old_out1;
  111. out2 -= val * old_out2;
  112. out3 -= val * old_out3;
  113. for (i = 5; i <= filter_length; i += 2) {
  114. old_out3 = out[-i];
  115. val = filter_coeffs[i-1];
  116. out0 -= val * old_out3;
  117. out1 -= val * old_out0;
  118. out2 -= val * old_out1;
  119. out3 -= val * old_out2;
  120. old_out2 = out[-i-1];
  121. val = filter_coeffs[i];
  122. out0 -= val * old_out2;
  123. out1 -= val * old_out3;
  124. out2 -= val * old_out0;
  125. out3 -= val * old_out1;
  126. FFSWAP(float, old_out0, old_out2);
  127. old_out1 = old_out3;
  128. }
  129. tmp0 = out0;
  130. tmp1 = out1;
  131. tmp2 = out2;
  132. out3 -= a * tmp2;
  133. out2 -= a * tmp1;
  134. out1 -= a * tmp0;
  135. out3 -= b * tmp1;
  136. out2 -= b * tmp0;
  137. out3 -= c * tmp0;
  138. out[0] = out0;
  139. out[1] = out1;
  140. out[2] = out2;
  141. out[3] = out3;
  142. old_out0 = out0;
  143. old_out1 = out1;
  144. old_out2 = out2;
  145. old_out3 = out3;
  146. out += 4;
  147. in += 4;
  148. }
  149. out -= n;
  150. in -= n;
  151. for (; n < buffer_length; n++) {
  152. out[n] = in[n];
  153. for (i = 1; i <= filter_length; i++)
  154. out[n] -= filter_coeffs[i-1] * out[n-i];
  155. }
  156. #endif
  157. }
  158. void ff_celp_lp_zero_synthesis_filterf(float *out, const float *filter_coeffs,
  159. const float *in, int buffer_length,
  160. int filter_length)
  161. {
  162. int i,n;
  163. for (n = 0; n < buffer_length; n++) {
  164. out[n] = in[n];
  165. for (i = 1; i <= filter_length; i++)
  166. out[n] += filter_coeffs[i-1] * in[n-i];
  167. }
  168. }