acelp_vectors.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #ifndef AVCODEC_ACELP_VECTORS_H
  23. #define AVCODEC_ACELP_VECTORS_H
  24. #include <stdint.h>
  25. typedef struct ACELPVContext {
  26. /**
  27. * float implementation of weighted sum of two vectors.
  28. * @param[out] out result of addition
  29. * @param in_a first vector
  30. * @param in_b second vector
  31. * @param weight_coeff_a first vector weight coefficient
  32. * @param weight_coeff_a second vector weight coefficient
  33. * @param length vectors length (should be a multiple of two)
  34. *
  35. * @note It is safe to pass the same buffer for out and in_a or in_b.
  36. */
  37. void (*weighted_vector_sumf)(float *out, const float *in_a, const float *in_b,
  38. float weight_coeff_a, float weight_coeff_b,
  39. int length);
  40. }ACELPVContext;
  41. /**
  42. * Initialize ACELPVContext.
  43. */
  44. void ff_acelp_vectors_init(ACELPVContext *c);
  45. void ff_acelp_vectors_init_mips(ACELPVContext *c);
  46. /** Sparse representation for the algebraic codebook (fixed) vector */
  47. typedef struct AMRFixed {
  48. int n;
  49. int x[10];
  50. float y[10];
  51. int no_repeat_mask;
  52. int pitch_lag;
  53. float pitch_fac;
  54. } AMRFixed;
  55. /**
  56. * Track|Pulse| Positions
  57. * -------------------------------------------------------------------------
  58. * 1 | 0 | 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75
  59. * -------------------------------------------------------------------------
  60. * 2 | 1 | 1, 6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61, 66, 71, 76
  61. * -------------------------------------------------------------------------
  62. * 3 | 2 | 2, 7, 12, 17, 22, 27, 32, 37, 42, 47, 52, 57, 62, 67, 72, 77
  63. * -------------------------------------------------------------------------
  64. *
  65. * Table contains only first the pulse indexes.
  66. *
  67. * Used in G.729 @@8k, G.729 @@4.4k, AMR @@7.95k, AMR @@7.40k
  68. */
  69. extern const uint8_t ff_fc_4pulses_8bits_tracks_13[16];
  70. /**
  71. * Track|Pulse| Positions
  72. * -------------------------------------------------------------------------
  73. * 4 | 3 | 3, 8, 13, 18, 23, 28, 33, 38, 43, 48, 53, 58, 63, 68, 73, 78
  74. * | | 4, 9, 14, 19, 24, 29, 34, 39, 44, 49, 54, 59, 64, 69, 74, 79
  75. * -------------------------------------------------------------------------
  76. *
  77. * @remark Track in the table should be read top-to-bottom, left-to-right.
  78. *
  79. * Used in G.729 @@8k, G.729 @@4.4k, AMR @@7.95k, AMR @@7.40k
  80. */
  81. extern const uint8_t ff_fc_4pulses_8bits_track_4[32];
  82. /**
  83. * Track|Pulse| Positions
  84. * -----------------------------------------
  85. * 1 | 0 | 1, 6, 11, 16, 21, 26, 31, 36
  86. * | | 3, 8, 13, 18, 23, 28, 33, 38
  87. * -----------------------------------------
  88. *
  89. * @remark Track in the table should be read top-to-bottom, left-to-right.
  90. *
  91. * @note (EE) Reference G.729D code also uses gray decoding for each
  92. * pulse index before looking up the value in the table.
  93. *
  94. * Used in G.729 @@6.4k (with gray coding), AMR @@5.9k (without gray coding)
  95. */
  96. extern const uint8_t ff_fc_2pulses_9bits_track1[16];
  97. extern const uint8_t ff_fc_2pulses_9bits_track1_gray[16];
  98. /**
  99. * Track|Pulse| Positions
  100. * -----------------------------------------
  101. * 2 | 1 | 0, 7, 14, 20, 27, 34, 1, 21
  102. * | | 2, 9, 15, 22, 29, 35, 6, 26
  103. * | | 4,10, 17, 24, 30, 37, 11, 31
  104. * | | 5,12, 19, 25, 32, 39, 16, 36
  105. * -----------------------------------------
  106. *
  107. * @remark Track in the table should be read top-to-bottom, left-to-right.
  108. *
  109. * @note (EE.1) This table (from the reference code) does not comply with
  110. * the specification.
  111. * The specification contains the following table:
  112. *
  113. * Track|Pulse| Positions
  114. * -----------------------------------------
  115. * 2 | 1 | 0, 5, 10, 15, 20, 25, 30, 35
  116. * | | 1, 6, 11, 16, 21, 26, 31, 36
  117. * | | 2, 7, 12, 17, 22, 27, 32, 37
  118. * | | 4, 9, 14, 19, 24, 29, 34, 39
  119. *
  120. * -----------------------------------------
  121. *
  122. * @note (EE.2) Reference G.729D code also uses gray decoding for each
  123. * pulse index before looking up the value in the table.
  124. *
  125. * Used in G.729 @@6.4k (with gray coding)
  126. */
  127. extern const uint8_t ff_fc_2pulses_9bits_track2_gray[32];
  128. /**
  129. * b60 hamming windowed sinc function coefficients
  130. */
  131. extern const float ff_b60_sinc[61];
  132. /**
  133. * Table of pow(0.7,n)
  134. */
  135. extern const float ff_pow_0_7[10];
  136. /**
  137. * Table of pow(0.75,n)
  138. */
  139. extern const float ff_pow_0_75[10];
  140. /**
  141. * Table of pow(0.55,n)
  142. */
  143. extern const float ff_pow_0_55[10];
  144. /**
  145. * Decode fixed-codebook vector (3.8 and D.5.8 of G.729, 5.7.1 of AMR).
  146. * @param[out] fc_v decoded fixed codebook vector (2.13)
  147. * @param tab1 table used for first pulse_count pulses
  148. * @param tab2 table used for last pulse
  149. * @param pulse_indexes fixed codebook indexes
  150. * @param pulse_signs signs of the excitation pulses (0 bit value
  151. * means negative sign)
  152. * @param bits number of bits per one pulse index
  153. * @param pulse_count number of pulses decoded using first table
  154. * @param bits length of one pulse index in bits
  155. *
  156. * Used in G.729 @@8k, G.729 @@4.4k, G.729 @@6.4k, AMR @@7.95k, AMR @@7.40k
  157. */
  158. void ff_acelp_fc_pulse_per_track(int16_t* fc_v,
  159. const uint8_t *tab1,
  160. const uint8_t *tab2,
  161. int pulse_indexes,
  162. int pulse_signs,
  163. int pulse_count,
  164. int bits);
  165. /**
  166. * Decode the algebraic codebook index to pulse positions and signs and
  167. * construct the algebraic codebook vector for MODE_12k2.
  168. *
  169. * @note: The positions and signs are explicitly coded in MODE_12k2.
  170. *
  171. * @param fixed_index positions of the ten pulses
  172. * @param fixed_sparse pointer to the algebraic codebook vector
  173. * @param gray_decode gray decoding table
  174. * @param half_pulse_count number of couples of pulses
  175. * @param bits length of one pulse index in bits
  176. */
  177. void ff_decode_10_pulses_35bits(const int16_t *fixed_index,
  178. AMRFixed *fixed_sparse,
  179. const uint8_t *gray_decode,
  180. int half_pulse_count, int bits);
  181. /**
  182. * weighted sum of two vectors with rounding.
  183. * @param[out] out result of addition
  184. * @param in_a first vector
  185. * @param in_b second vector
  186. * @param weight_coeff_a first vector weight coefficient
  187. * @param weight_coeff_a second vector weight coefficient
  188. * @param rounder this value will be added to the sum of the two vectors
  189. * @param shift result will be shifted to right by this value
  190. * @param length vectors length
  191. *
  192. * @note It is safe to pass the same buffer for out and in_a or in_b.
  193. *
  194. * out[i] = (in_a[i]*weight_a + in_b[i]*weight_b + rounder) >> shift
  195. */
  196. void ff_acelp_weighted_vector_sum(int16_t* out,
  197. const int16_t *in_a,
  198. const int16_t *in_b,
  199. int16_t weight_coeff_a,
  200. int16_t weight_coeff_b,
  201. int16_t rounder,
  202. int shift,
  203. int length);
  204. /**
  205. * float implementation of weighted sum of two vectors.
  206. * @param[out] out result of addition
  207. * @param in_a first vector
  208. * @param in_b second vector
  209. * @param weight_coeff_a first vector weight coefficient
  210. * @param weight_coeff_a second vector weight coefficient
  211. * @param length vectors length
  212. *
  213. * @note It is safe to pass the same buffer for out and in_a or in_b.
  214. */
  215. void ff_weighted_vector_sumf(float *out, const float *in_a, const float *in_b,
  216. float weight_coeff_a, float weight_coeff_b,
  217. int length);
  218. /**
  219. * Adaptive gain control (as used in AMR postfiltering)
  220. *
  221. * @param out output buffer for filtered speech data
  222. * @param in the input speech buffer (may be the same as out)
  223. * @param speech_energ input energy
  224. * @param size the input buffer size
  225. * @param alpha exponential filter factor
  226. * @param gain_mem a pointer to the filter memory (single float of size)
  227. */
  228. void ff_adaptive_gain_control(float *out, const float *in, float speech_energ,
  229. int size, float alpha, float *gain_mem);
  230. /**
  231. * Set the sum of squares of a signal by scaling
  232. *
  233. * @param out output samples
  234. * @param in input samples
  235. * @param sum_of_squares new sum of squares
  236. * @param n number of samples
  237. *
  238. * @note If the input is zero (or its energy underflows), the output is zero.
  239. * This is the behavior of AGC in the AMR reference decoder. The QCELP
  240. * reference decoder seems to have undefined behavior.
  241. *
  242. * TIA/EIA/IS-733 2.4.8.3-2/3/4/5, 2.4.8.6
  243. * 3GPP TS 26.090 6.1 (6)
  244. */
  245. void ff_scale_vector_to_given_sum_of_squares(float *out, const float *in,
  246. float sum_of_squares, const int n);
  247. /**
  248. * Add fixed vector to an array from a sparse representation
  249. *
  250. * @param out fixed vector with pitch sharpening
  251. * @param in sparse fixed vector
  252. * @param scale number to multiply the fixed vector by
  253. * @param size the output vector size
  254. */
  255. void ff_set_fixed_vector(float *out, const AMRFixed *in, float scale, int size);
  256. /**
  257. * Clear array values set by set_fixed_vector
  258. *
  259. * @param out fixed vector to be cleared
  260. * @param in sparse fixed vector
  261. * @param size the output vector size
  262. */
  263. void ff_clear_fixed_vector(float *out, const AMRFixed *in, int size);
  264. #endif /* AVCODEC_ACELP_VECTORS_H */