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