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.

263 lines
8.0KB

  1. /*
  2. * G.723.1 compatible decoder
  3. * Copyright (c) 2006 Benjamin Larsson
  4. * Copyright (c) 2010 Mohamed Naufal Basheer
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <stdint.h>
  23. #include "libavutil/common.h"
  24. #include "acelp_vectors.h"
  25. #include "avcodec.h"
  26. #include "celp_math.h"
  27. #include "g723_1.h"
  28. int ff_g723_1_scale_vector(int16_t *dst, const int16_t *vector, int length)
  29. {
  30. int bits, max = 0;
  31. int i;
  32. for (i = 0; i < length; i++)
  33. max |= FFABS(vector[i]);
  34. max = FFMIN(max, 0x7FFF);
  35. bits = ff_g723_1_normalize_bits(max, 15);
  36. for (i = 0; i < length; i++)
  37. dst[i] = vector[i] << bits >> 3;
  38. return bits - 3;
  39. }
  40. int ff_g723_1_normalize_bits(int num, int width)
  41. {
  42. return width - av_log2(num) - 1;
  43. }
  44. int ff_g723_1_dot_product(const int16_t *a, const int16_t *b, int length)
  45. {
  46. int sum = ff_dot_product(a, b, length);
  47. return av_sat_add32(sum, sum);
  48. }
  49. void ff_g723_1_get_residual(int16_t *residual, int16_t *prev_excitation,
  50. int lag)
  51. {
  52. int offset = PITCH_MAX - PITCH_ORDER / 2 - lag;
  53. int i;
  54. residual[0] = prev_excitation[offset];
  55. residual[1] = prev_excitation[offset + 1];
  56. offset += 2;
  57. for (i = 2; i < SUBFRAME_LEN + PITCH_ORDER - 1; i++)
  58. residual[i] = prev_excitation[offset + (i - 2) % lag];
  59. }
  60. void ff_g723_1_gen_dirac_train(int16_t *buf, int pitch_lag)
  61. {
  62. int16_t vector[SUBFRAME_LEN];
  63. int i, j;
  64. memcpy(vector, buf, SUBFRAME_LEN * sizeof(*vector));
  65. for (i = pitch_lag; i < SUBFRAME_LEN; i += pitch_lag) {
  66. for (j = 0; j < SUBFRAME_LEN - i; j++)
  67. buf[i + j] += vector[j];
  68. }
  69. }
  70. void ff_g723_1_gen_acb_excitation(int16_t *vector, int16_t *prev_excitation,
  71. int pitch_lag, G723_1_Subframe *subfrm,
  72. enum Rate cur_rate)
  73. {
  74. int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
  75. const int16_t *cb_ptr;
  76. int lag = pitch_lag + subfrm->ad_cb_lag - 1;
  77. int i;
  78. int sum;
  79. ff_g723_1_get_residual(residual, prev_excitation, lag);
  80. /* Select quantization table */
  81. if (cur_rate == RATE_6300 && pitch_lag < SUBFRAME_LEN - 2)
  82. cb_ptr = adaptive_cb_gain85;
  83. else
  84. cb_ptr = adaptive_cb_gain170;
  85. /* Calculate adaptive vector */
  86. cb_ptr += subfrm->ad_cb_gain * 20;
  87. for (i = 0; i < SUBFRAME_LEN; i++) {
  88. sum = ff_g723_1_dot_product(residual + i, cb_ptr, PITCH_ORDER);
  89. vector[i] = av_sat_dadd32(1 << 15, sum) >> 16;
  90. }
  91. }
  92. /**
  93. * Convert LSP frequencies to LPC coefficients.
  94. *
  95. * @param lpc buffer for LPC coefficients
  96. */
  97. static void lsp2lpc(int16_t *lpc)
  98. {
  99. int f1[LPC_ORDER / 2 + 1];
  100. int f2[LPC_ORDER / 2 + 1];
  101. int i, j;
  102. /* Calculate negative cosine */
  103. for (j = 0; j < LPC_ORDER; j++) {
  104. int index = (lpc[j] >> 7) & 0x1FF;
  105. int offset = lpc[j] & 0x7f;
  106. int temp1 = cos_tab[index] << 16;
  107. int temp2 = (cos_tab[index + 1] - cos_tab[index]) *
  108. ((offset << 8) + 0x80) << 1;
  109. lpc[j] = -(av_sat_dadd32(1 << 15, temp1 + temp2) >> 16);
  110. }
  111. /*
  112. * Compute sum and difference polynomial coefficients
  113. * (bitexact alternative to lsp2poly() in lsp.c)
  114. */
  115. /* Initialize with values in Q28 */
  116. f1[0] = 1 << 28;
  117. f1[1] = (lpc[0] << 14) + (lpc[2] << 14);
  118. f1[2] = lpc[0] * lpc[2] + (2 << 28);
  119. f2[0] = 1 << 28;
  120. f2[1] = (lpc[1] << 14) + (lpc[3] << 14);
  121. f2[2] = lpc[1] * lpc[3] + (2 << 28);
  122. /*
  123. * Calculate and scale the coefficients by 1/2 in
  124. * each iteration for a final scaling factor of Q25
  125. */
  126. for (i = 2; i < LPC_ORDER / 2; i++) {
  127. f1[i + 1] = f1[i - 1] + MULL2(f1[i], lpc[2 * i]);
  128. f2[i + 1] = f2[i - 1] + MULL2(f2[i], lpc[2 * i + 1]);
  129. for (j = i; j >= 2; j--) {
  130. f1[j] = MULL2(f1[j - 1], lpc[2 * i]) +
  131. (f1[j] >> 1) + (f1[j - 2] >> 1);
  132. f2[j] = MULL2(f2[j - 1], lpc[2 * i + 1]) +
  133. (f2[j] >> 1) + (f2[j - 2] >> 1);
  134. }
  135. f1[0] >>= 1;
  136. f2[0] >>= 1;
  137. f1[1] = ((lpc[2 * i] << 16 >> i) + f1[1]) >> 1;
  138. f2[1] = ((lpc[2 * i + 1] << 16 >> i) + f2[1]) >> 1;
  139. }
  140. /* Convert polynomial coefficients to LPC coefficients */
  141. for (i = 0; i < LPC_ORDER / 2; i++) {
  142. int64_t ff1 = f1[i + 1] + f1[i];
  143. int64_t ff2 = f2[i + 1] - f2[i];
  144. lpc[i] = av_clipl_int32(((ff1 + ff2) << 3) +
  145. (1 << 15)) >> 16;
  146. lpc[LPC_ORDER - i - 1] = av_clipl_int32(((ff1 - ff2) << 3) +
  147. (1 << 15)) >> 16;
  148. }
  149. }
  150. void ff_g723_1_lsp_interpolate(int16_t *lpc, int16_t *cur_lsp,
  151. int16_t *prev_lsp)
  152. {
  153. int i;
  154. int16_t *lpc_ptr = lpc;
  155. /* cur_lsp * 0.25 + prev_lsp * 0.75 */
  156. ff_acelp_weighted_vector_sum(lpc, cur_lsp, prev_lsp,
  157. 4096, 12288, 1 << 13, 14, LPC_ORDER);
  158. ff_acelp_weighted_vector_sum(lpc + LPC_ORDER, cur_lsp, prev_lsp,
  159. 8192, 8192, 1 << 13, 14, LPC_ORDER);
  160. ff_acelp_weighted_vector_sum(lpc + 2 * LPC_ORDER, cur_lsp, prev_lsp,
  161. 12288, 4096, 1 << 13, 14, LPC_ORDER);
  162. memcpy(lpc + 3 * LPC_ORDER, cur_lsp, LPC_ORDER * sizeof(*lpc));
  163. for (i = 0; i < SUBFRAMES; i++) {
  164. lsp2lpc(lpc_ptr);
  165. lpc_ptr += LPC_ORDER;
  166. }
  167. }
  168. void ff_g723_1_inverse_quant(int16_t *cur_lsp, int16_t *prev_lsp,
  169. uint8_t *lsp_index, int bad_frame)
  170. {
  171. int min_dist, pred;
  172. int i, j, temp, stable;
  173. /* Check for frame erasure */
  174. if (!bad_frame) {
  175. min_dist = 0x100;
  176. pred = 12288;
  177. } else {
  178. min_dist = 0x200;
  179. pred = 23552;
  180. lsp_index[0] = lsp_index[1] = lsp_index[2] = 0;
  181. }
  182. /* Get the VQ table entry corresponding to the transmitted index */
  183. cur_lsp[0] = lsp_band0[lsp_index[0]][0];
  184. cur_lsp[1] = lsp_band0[lsp_index[0]][1];
  185. cur_lsp[2] = lsp_band0[lsp_index[0]][2];
  186. cur_lsp[3] = lsp_band1[lsp_index[1]][0];
  187. cur_lsp[4] = lsp_band1[lsp_index[1]][1];
  188. cur_lsp[5] = lsp_band1[lsp_index[1]][2];
  189. cur_lsp[6] = lsp_band2[lsp_index[2]][0];
  190. cur_lsp[7] = lsp_band2[lsp_index[2]][1];
  191. cur_lsp[8] = lsp_band2[lsp_index[2]][2];
  192. cur_lsp[9] = lsp_band2[lsp_index[2]][3];
  193. /* Add predicted vector & DC component to the previously quantized vector */
  194. for (i = 0; i < LPC_ORDER; i++) {
  195. temp = ((prev_lsp[i] - dc_lsp[i]) * pred + (1 << 14)) >> 15;
  196. cur_lsp[i] += dc_lsp[i] + temp;
  197. }
  198. for (i = 0; i < LPC_ORDER; i++) {
  199. cur_lsp[0] = FFMAX(cur_lsp[0], 0x180);
  200. cur_lsp[LPC_ORDER - 1] = FFMIN(cur_lsp[LPC_ORDER - 1], 0x7e00);
  201. /* Stability check */
  202. for (j = 1; j < LPC_ORDER; j++) {
  203. temp = min_dist + cur_lsp[j - 1] - cur_lsp[j];
  204. if (temp > 0) {
  205. temp >>= 1;
  206. cur_lsp[j - 1] -= temp;
  207. cur_lsp[j] += temp;
  208. }
  209. }
  210. stable = 1;
  211. for (j = 1; j < LPC_ORDER; j++) {
  212. temp = cur_lsp[j - 1] + min_dist - cur_lsp[j] - 4;
  213. if (temp > 0) {
  214. stable = 0;
  215. break;
  216. }
  217. }
  218. if (stable)
  219. break;
  220. }
  221. if (!stable)
  222. memcpy(cur_lsp, prev_lsp, LPC_ORDER * sizeof(*cur_lsp));
  223. }