celp_filters.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 shift, int rounder)
  54. {
  55. int i,n;
  56. for (n = 0; n < buffer_length; n++) {
  57. int sum = -rounder, sum1;
  58. for (i = 1; i <= filter_length; i++)
  59. sum += filter_coeffs[i-1] * out[n-i];
  60. sum1 = ((-sum >> 12) + in[n]) >> shift;
  61. sum = av_clip_int16(sum1);
  62. if (stop_on_overflow && sum != sum1)
  63. return 1;
  64. out[n] = sum;
  65. }
  66. return 0;
  67. }
  68. void ff_celp_lp_synthesis_filterf(float *out, const float *filter_coeffs,
  69. const float* in, int buffer_length,
  70. int filter_length)
  71. {
  72. int i,n;
  73. #if 0 // Unoptimized code path for improved readability
  74. for (n = 0; n < buffer_length; n++) {
  75. out[n] = in[n];
  76. for (i = 1; i <= filter_length; i++)
  77. out[n] -= filter_coeffs[i-1] * out[n-i];
  78. }
  79. #else
  80. float out0, out1, out2, out3;
  81. float old_out0, old_out1, old_out2, old_out3;
  82. float a,b,c;
  83. a = filter_coeffs[0];
  84. b = filter_coeffs[1];
  85. c = filter_coeffs[2];
  86. b -= filter_coeffs[0] * filter_coeffs[0];
  87. c -= filter_coeffs[1] * filter_coeffs[0];
  88. c -= filter_coeffs[0] * b;
  89. old_out0 = out[-4];
  90. old_out1 = out[-3];
  91. old_out2 = out[-2];
  92. old_out3 = out[-1];
  93. for (n = 0; n <= buffer_length - 4; n+=4) {
  94. float tmp0,tmp1,tmp2;
  95. float val;
  96. out0 = in[0];
  97. out1 = in[1];
  98. out2 = in[2];
  99. out3 = in[3];
  100. out0 -= filter_coeffs[2] * old_out1;
  101. out1 -= filter_coeffs[2] * old_out2;
  102. out2 -= filter_coeffs[2] * old_out3;
  103. out0 -= filter_coeffs[1] * old_out2;
  104. out1 -= filter_coeffs[1] * old_out3;
  105. out0 -= filter_coeffs[0] * old_out3;
  106. val = filter_coeffs[3];
  107. out0 -= val * old_out0;
  108. out1 -= val * old_out1;
  109. out2 -= val * old_out2;
  110. out3 -= val * old_out3;
  111. for (i = 5; i <= filter_length; i += 2) {
  112. old_out3 = out[-i];
  113. val = filter_coeffs[i-1];
  114. out0 -= val * old_out3;
  115. out1 -= val * old_out0;
  116. out2 -= val * old_out1;
  117. out3 -= val * old_out2;
  118. old_out2 = out[-i-1];
  119. val = filter_coeffs[i];
  120. out0 -= val * old_out2;
  121. out1 -= val * old_out3;
  122. out2 -= val * old_out0;
  123. out3 -= val * old_out1;
  124. FFSWAP(float, old_out0, old_out2);
  125. old_out1 = old_out3;
  126. }
  127. tmp0 = out0;
  128. tmp1 = out1;
  129. tmp2 = out2;
  130. out3 -= a * tmp2;
  131. out2 -= a * tmp1;
  132. out1 -= a * tmp0;
  133. out3 -= b * tmp1;
  134. out2 -= b * tmp0;
  135. out3 -= c * tmp0;
  136. out[0] = out0;
  137. out[1] = out1;
  138. out[2] = out2;
  139. out[3] = out3;
  140. old_out0 = out0;
  141. old_out1 = out1;
  142. old_out2 = out2;
  143. old_out3 = out3;
  144. out += 4;
  145. in += 4;
  146. }
  147. out -= n;
  148. in -= n;
  149. for (; n < buffer_length; n++) {
  150. out[n] = in[n];
  151. for (i = 1; i <= filter_length; i++)
  152. out[n] -= filter_coeffs[i-1] * out[n-i];
  153. }
  154. #endif
  155. }
  156. void ff_celp_lp_zero_synthesis_filterf(float *out, const float *filter_coeffs,
  157. const float *in, int buffer_length,
  158. int filter_length)
  159. {
  160. int i,n;
  161. for (n = 0; n < buffer_length; n++) {
  162. out[n] = in[n];
  163. for (i = 1; i <= filter_length; i++)
  164. out[n] += filter_coeffs[i-1] * in[n-i];
  165. }
  166. }
  167. void ff_celp_filter_init(CELPFContext *c)
  168. {
  169. c->celp_lp_synthesis_filterf = ff_celp_lp_synthesis_filterf;
  170. c->celp_lp_zero_synthesis_filterf = ff_celp_lp_zero_synthesis_filterf;
  171. if(HAVE_MIPSFPU)
  172. ff_celp_filter_init_mips(c);
  173. }