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.

273 lines
7.0KB

  1. /*
  2. * adaptive and fixed codebook vector operations 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 "libavutil/common.h"
  24. #include "avcodec.h"
  25. #include "acelp_vectors.h"
  26. #include "celp_math.h"
  27. const uint8_t ff_fc_2pulses_9bits_track1[16] =
  28. {
  29. 1, 3,
  30. 6, 8,
  31. 11, 13,
  32. 16, 18,
  33. 21, 23,
  34. 26, 28,
  35. 31, 33,
  36. 36, 38
  37. };
  38. const uint8_t ff_fc_2pulses_9bits_track1_gray[16] =
  39. {
  40. 1, 3,
  41. 8, 6,
  42. 18, 16,
  43. 11, 13,
  44. 38, 36,
  45. 31, 33,
  46. 21, 23,
  47. 28, 26,
  48. };
  49. const uint8_t ff_fc_2pulses_9bits_track2_gray[32] =
  50. {
  51. 0, 2,
  52. 5, 4,
  53. 12, 10,
  54. 7, 9,
  55. 25, 24,
  56. 20, 22,
  57. 14, 15,
  58. 19, 17,
  59. 36, 31,
  60. 21, 26,
  61. 1, 6,
  62. 16, 11,
  63. 27, 29,
  64. 32, 30,
  65. 39, 37,
  66. 34, 35,
  67. };
  68. const uint8_t ff_fc_4pulses_8bits_tracks_13[16] =
  69. {
  70. 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75,
  71. };
  72. const uint8_t ff_fc_4pulses_8bits_track_4[32] =
  73. {
  74. 3, 4,
  75. 8, 9,
  76. 13, 14,
  77. 18, 19,
  78. 23, 24,
  79. 28, 29,
  80. 33, 34,
  81. 38, 39,
  82. 43, 44,
  83. 48, 49,
  84. 53, 54,
  85. 58, 59,
  86. 63, 64,
  87. 68, 69,
  88. 73, 74,
  89. 78, 79,
  90. };
  91. const float ff_pow_0_7[10] = {
  92. 0.700000, 0.490000, 0.343000, 0.240100, 0.168070,
  93. 0.117649, 0.082354, 0.057648, 0.040354, 0.028248
  94. };
  95. const float ff_pow_0_75[10] = {
  96. 0.750000, 0.562500, 0.421875, 0.316406, 0.237305,
  97. 0.177979, 0.133484, 0.100113, 0.075085, 0.056314
  98. };
  99. const float ff_pow_0_55[10] = {
  100. 0.550000, 0.302500, 0.166375, 0.091506, 0.050328,
  101. 0.027681, 0.015224, 0.008373, 0.004605, 0.002533
  102. };
  103. const float ff_b60_sinc[61] = {
  104. 0.898529 , 0.865051 , 0.769257 , 0.624054 , 0.448639 , 0.265289 ,
  105. 0.0959167 , -0.0412598 , -0.134338 , -0.178986 , -0.178528 , -0.142609 ,
  106. -0.0849304 , -0.0205078 , 0.0369568 , 0.0773926 , 0.0955200 , 0.0912781 ,
  107. 0.0689392 , 0.0357056 , 0. , -0.0305481 , -0.0504150 , -0.0570068 ,
  108. -0.0508423 , -0.0350037 , -0.0141602 , 0.00665283, 0.0230713 , 0.0323486 ,
  109. 0.0335388 , 0.0275879 , 0.0167847 , 0.00411987, -0.00747681, -0.0156860 ,
  110. -0.0193481 , -0.0183716 , -0.0137634 , -0.00704956, 0. , 0.00582886 ,
  111. 0.00939941, 0.0103760 , 0.00903320, 0.00604248, 0.00238037, -0.00109863 ,
  112. -0.00366211, -0.00497437, -0.00503540, -0.00402832, -0.00241089, -0.000579834,
  113. 0.00103760, 0.00222778, 0.00277710, 0.00271606, 0.00213623, 0.00115967 ,
  114. 0.
  115. };
  116. void ff_acelp_fc_pulse_per_track(
  117. int16_t* fc_v,
  118. const uint8_t *tab1,
  119. const uint8_t *tab2,
  120. int pulse_indexes,
  121. int pulse_signs,
  122. int pulse_count,
  123. int bits)
  124. {
  125. int mask = (1 << bits) - 1;
  126. int i;
  127. for(i=0; i<pulse_count; i++)
  128. {
  129. fc_v[i + tab1[pulse_indexes & mask]] +=
  130. (pulse_signs & 1) ? 8191 : -8192; // +/-1 in (2.13)
  131. pulse_indexes >>= bits;
  132. pulse_signs >>= 1;
  133. }
  134. fc_v[tab2[pulse_indexes]] += (pulse_signs & 1) ? 8191 : -8192;
  135. }
  136. void ff_decode_10_pulses_35bits(const int16_t *fixed_index,
  137. AMRFixed *fixed_sparse,
  138. const uint8_t *gray_decode,
  139. int half_pulse_count, int bits)
  140. {
  141. int i;
  142. int mask = (1 << bits) - 1;
  143. fixed_sparse->no_repeat_mask = 0;
  144. fixed_sparse->n = 2 * half_pulse_count;
  145. for (i = 0; i < half_pulse_count; i++) {
  146. const int pos1 = gray_decode[fixed_index[2*i+1] & mask] + i;
  147. const int pos2 = gray_decode[fixed_index[2*i ] & mask] + i;
  148. const float sign = (fixed_index[2*i+1] & (1 << bits)) ? -1.0 : 1.0;
  149. fixed_sparse->x[2*i+1] = pos1;
  150. fixed_sparse->x[2*i ] = pos2;
  151. fixed_sparse->y[2*i+1] = sign;
  152. fixed_sparse->y[2*i ] = pos2 < pos1 ? -sign : sign;
  153. }
  154. }
  155. void ff_acelp_weighted_vector_sum(
  156. int16_t* out,
  157. const int16_t *in_a,
  158. const int16_t *in_b,
  159. int16_t weight_coeff_a,
  160. int16_t weight_coeff_b,
  161. int16_t rounder,
  162. int shift,
  163. int length)
  164. {
  165. int i;
  166. // Clipping required here; breaks OVERFLOW test.
  167. for(i=0; i<length; i++)
  168. out[i] = av_clip_int16((
  169. in_a[i] * weight_coeff_a +
  170. in_b[i] * weight_coeff_b +
  171. rounder) >> shift);
  172. }
  173. void ff_weighted_vector_sumf(float *out, const float *in_a, const float *in_b,
  174. float weight_coeff_a, float weight_coeff_b, int length)
  175. {
  176. int i;
  177. for(i=0; i<length; i++)
  178. out[i] = weight_coeff_a * in_a[i]
  179. + weight_coeff_b * in_b[i];
  180. }
  181. void ff_adaptive_gain_control(float *out, const float *in, float speech_energ,
  182. int size, float alpha, float *gain_mem)
  183. {
  184. int i;
  185. float postfilter_energ = ff_dot_productf(in, in, size);
  186. float gain_scale_factor = 1.0;
  187. float mem = *gain_mem;
  188. if (postfilter_energ)
  189. gain_scale_factor = sqrt(speech_energ / postfilter_energ);
  190. gain_scale_factor *= 1.0 - alpha;
  191. for (i = 0; i < size; i++) {
  192. mem = alpha * mem + gain_scale_factor;
  193. out[i] = in[i] * mem;
  194. }
  195. *gain_mem = mem;
  196. }
  197. void ff_scale_vector_to_given_sum_of_squares(float *out, const float *in,
  198. float sum_of_squares, const int n)
  199. {
  200. int i;
  201. float scalefactor = ff_dot_productf(in, in, n);
  202. if (scalefactor)
  203. scalefactor = sqrt(sum_of_squares / scalefactor);
  204. for (i = 0; i < n; i++)
  205. out[i] = in[i] * scalefactor;
  206. }
  207. void ff_set_fixed_vector(float *out, const AMRFixed *in, float scale, int size)
  208. {
  209. int i;
  210. for (i=0; i < in->n; i++) {
  211. int x = in->x[i], repeats = !((in->no_repeat_mask >> i) & 1);
  212. float y = in->y[i] * scale;
  213. if (in->pitch_lag > 0)
  214. do {
  215. out[x] += y;
  216. y *= in->pitch_fac;
  217. x += in->pitch_lag;
  218. } while (x < size && repeats);
  219. }
  220. }
  221. void ff_clear_fixed_vector(float *out, const AMRFixed *in, int size)
  222. {
  223. int i;
  224. for (i=0; i < in->n; i++) {
  225. int x = in->x[i], repeats = !((in->no_repeat_mask >> i) & 1);
  226. if (in->pitch_lag > 0)
  227. do {
  228. out[x] = 0.0;
  229. x += in->pitch_lag;
  230. } while (x < size && repeats);
  231. }
  232. }
  233. void ff_acelp_vectors_init(ACELPVContext *c)
  234. {
  235. c->weighted_vector_sumf = ff_weighted_vector_sumf;
  236. if(HAVE_MIPSFPU)
  237. ff_acelp_vectors_init_mips(c);
  238. }