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.

220 lines
5.8KB

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