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.

184 lines
5.2KB

  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 "acelp_filters.h"
  25. #define FRAC_BITS 13
  26. #include "mathops.h"
  27. const int16_t ff_acelp_interp_filter[61] =
  28. { /* (0.15) */
  29. 29443, 28346, 25207, 20449, 14701, 8693,
  30. 3143, -1352, -4402, -5865, -5850, -4673,
  31. -2783, -672, 1211, 2536, 3130, 2991,
  32. 2259, 1170, 0, -1001, -1652, -1868,
  33. -1666, -1147, -464, 218, 756, 1060,
  34. 1099, 904, 550, 135, -245, -514,
  35. -634, -602, -451, -231, 0, 191,
  36. 308, 340, 296, 198, 78, -36,
  37. -120, -163, -165, -132, -79, -19,
  38. 34, 73, 91, 89, 70, 38,
  39. 0,
  40. };
  41. void ff_acelp_interpolate(
  42. int16_t* out,
  43. const int16_t* in,
  44. const int16_t* filter_coeffs,
  45. int precision,
  46. int pitch_delay_frac,
  47. int filter_length,
  48. int length)
  49. {
  50. int n, i;
  51. assert(pitch_delay_frac >= 0 && pitch_delay_frac < precision);
  52. for(n=0; n<length; n++)
  53. {
  54. int idx = 0;
  55. int v = 0x4000;
  56. for(i=0; i<filter_length;)
  57. {
  58. /* The reference G.729 and AMR fixed point code performs clipping after
  59. each of the two following accumulations.
  60. Since clipping affects only the synthetic OVERFLOW test without
  61. causing an int type overflow, it was moved outside the loop. */
  62. /* R(x):=ac_v[-k+x]
  63. v += R(n-i)*ff_acelp_interp_filter(t+6i)
  64. v += R(n+i+1)*ff_acelp_interp_filter(6-t+6i) */
  65. v += in[n + i] * filter_coeffs[idx + pitch_delay_frac];
  66. idx += precision;
  67. i++;
  68. v += in[n - i] * filter_coeffs[idx - pitch_delay_frac];
  69. }
  70. out[n] = av_clip_int16(v >> 15);
  71. }
  72. }
  73. void ff_acelp_convolve_circ(
  74. int16_t* fc_out,
  75. const int16_t* fc_in,
  76. const int16_t* filter,
  77. int subframe_size)
  78. {
  79. int i, k;
  80. memset(fc_out, 0, subframe_size * sizeof(int16_t));
  81. /* Since there are few pulses over an entire subframe (i.e. almost
  82. all fc_in[i] are zero) it is faster to swap two loops and process
  83. non-zero samples only. In the case of G.729D the buffer contains
  84. two non-zero samples before the call to ff_acelp_enhance_harmonics
  85. and, due to pitch_delay being bounded by [20; 143], a maximum
  86. of four non-zero samples for a total of 40 after the call. */
  87. for(i=0; i<subframe_size; i++)
  88. {
  89. if(fc_in[i])
  90. {
  91. for(k=0; k<i; k++)
  92. fc_out[k] += (fc_in[i] * filter[subframe_size + k - i]) >> 15;
  93. for(k=i; k<subframe_size; k++)
  94. fc_out[k] += (fc_in[i] * filter[k - i]) >> 15;
  95. }
  96. }
  97. }
  98. int ff_acelp_lp_synthesis_filter(
  99. int16_t *out,
  100. const int16_t* filter_coeffs,
  101. const int16_t* in,
  102. int buffer_length,
  103. int filter_length,
  104. int stop_on_overflow,
  105. int rounder)
  106. {
  107. int i,n;
  108. // These two lines are to avoid a -1 subtraction in the main loop
  109. filter_length++;
  110. filter_coeffs--;
  111. for(n=0; n<buffer_length; n++)
  112. {
  113. int sum = rounder;
  114. for(i=1; i<filter_length; i++)
  115. sum -= filter_coeffs[i] * out[n-i];
  116. sum = (sum >> 12) + in[n];
  117. /* Check for overflow */
  118. if(sum + 0x8000 > 0xFFFFU)
  119. {
  120. if(stop_on_overflow)
  121. return 1;
  122. sum = (sum >> 31) ^ 32767;
  123. }
  124. out[n] = sum;
  125. }
  126. return 0;
  127. }
  128. void ff_acelp_weighted_filter(
  129. int16_t *out,
  130. const int16_t* in,
  131. const int16_t *weight_pow,
  132. int filter_length)
  133. {
  134. int n;
  135. for(n=0; n<filter_length; n++)
  136. out[n] = (in[n] * weight_pow[n] + 0x4000) >> 15; /* (3.12) = (0.15) * (3.12) with rounding */
  137. }
  138. void ff_acelp_high_pass_filter(
  139. int16_t* out,
  140. int hpf_f[2],
  141. const int16_t* in,
  142. int length)
  143. {
  144. int i;
  145. int tmp;
  146. for(i=0; i<length; i++)
  147. {
  148. tmp = MULL(hpf_f[0], 15836); /* (14.13) = (13.13) * (1.13) */
  149. tmp += MULL(hpf_f[1], -7667); /* (13.13) = (13.13) * (0.13) */
  150. tmp += 7699 * (in[i] - 2*in[i-1] + in[i-2]); /* (14.13) = (0.13) * (14.0) */
  151. /* Multiplication by 2 with rounding can cause short type
  152. overflow, thus clipping is required. */
  153. out[i] = av_clip_int16((tmp + 0x800) >> 12); /* (15.0) = 2 * (13.13) = (14.13) */
  154. hpf_f[1] = hpf_f[0];
  155. hpf_f[0] = tmp;
  156. }
  157. }