acelp_vectors.c 7.0 KB

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