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.

282 lines
9.0KB

  1. /*
  2. * SIPR decoder for the 16k mode
  3. *
  4. * Copyright (c) 2008 Vladimir Voroshilov
  5. * Copyright (c) 2009 Vitor Sessak
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <math.h>
  24. #include "sipr.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/mathematics.h"
  27. #include "lsp.h"
  28. #include "celp_math.h"
  29. #include "acelp_vectors.h"
  30. #include "acelp_pitch_delay.h"
  31. #include "acelp_filters.h"
  32. #include "celp_filters.h"
  33. #include "sipr16kdata.h"
  34. /**
  35. * Convert an lsf vector into an lsp vector.
  36. *
  37. * @param lsf input lsf vector
  38. * @param lsp output lsp vector
  39. */
  40. static void lsf2lsp(const float *lsf, double *lsp)
  41. {
  42. int i;
  43. for (i = 0; i < LP_FILTER_ORDER_16k; i++)
  44. lsp[i] = cosf(lsf[i]);
  45. }
  46. static void dequant(float *out, const int *idx, const float *cbs[])
  47. {
  48. int i;
  49. for (i = 0; i < 4; i++)
  50. memcpy(out + 3*i, cbs[i] + 3*idx[i], 3*sizeof(float));
  51. memcpy(out + 12, cbs[4] + 4*idx[4], 4*sizeof(float));
  52. }
  53. static void lsf_decode_fp_16k(float* lsf_history, float* isp_new,
  54. const int* parm, int ma_pred)
  55. {
  56. int i;
  57. float isp_q[LP_FILTER_ORDER_16k];
  58. dequant(isp_q, parm, lsf_codebooks_16k);
  59. for (i = 0; i < LP_FILTER_ORDER_16k; i++) {
  60. isp_new[i] = (1 - qu[ma_pred]) * isp_q[i]
  61. + qu[ma_pred] * lsf_history[i]
  62. + mean_lsf_16k[i];
  63. }
  64. memcpy(lsf_history, isp_q, LP_FILTER_ORDER_16k * sizeof(float));
  65. }
  66. static int dec_delay3_1st(int index)
  67. {
  68. if (index < 390) {
  69. return index + 88;
  70. } else
  71. return 3 * index - 690;
  72. }
  73. static int dec_delay3_2nd(int index, int pit_min, int pit_max,
  74. int pitch_lag_prev)
  75. {
  76. if (index < 62) {
  77. int pitch_delay_min = av_clip(pitch_lag_prev - 10,
  78. pit_min, pit_max - 19);
  79. return 3 * pitch_delay_min + index - 2;
  80. } else
  81. return 3 * pitch_lag_prev;
  82. }
  83. static void postfilter(float *out_data, float* synth, float* iir_mem,
  84. float* filt_mem[2], float* mem_preemph)
  85. {
  86. float buf[30 + LP_FILTER_ORDER_16k];
  87. float *tmpbuf = buf + LP_FILTER_ORDER_16k;
  88. float s;
  89. int i;
  90. for (i = 0; i < LP_FILTER_ORDER_16k; i++)
  91. filt_mem[0][i] = iir_mem[i] * ff_pow_0_5[i];
  92. memcpy(tmpbuf - LP_FILTER_ORDER_16k, mem_preemph,
  93. LP_FILTER_ORDER_16k*sizeof(*buf));
  94. ff_celp_lp_synthesis_filterf(tmpbuf, filt_mem[1], synth, 30,
  95. LP_FILTER_ORDER_16k);
  96. memcpy(synth - LP_FILTER_ORDER_16k, mem_preemph,
  97. LP_FILTER_ORDER_16k * sizeof(*synth));
  98. ff_celp_lp_synthesis_filterf(synth, filt_mem[0], synth, 30,
  99. LP_FILTER_ORDER_16k);
  100. memcpy(out_data + 30 - LP_FILTER_ORDER_16k,
  101. synth + 30 - LP_FILTER_ORDER_16k,
  102. LP_FILTER_ORDER_16k * sizeof(*synth));
  103. ff_celp_lp_synthesis_filterf(out_data + 30, filt_mem[0],
  104. synth + 30, 2 * L_SUBFR_16k - 30,
  105. LP_FILTER_ORDER_16k);
  106. memcpy(mem_preemph, out_data + 2*L_SUBFR_16k - LP_FILTER_ORDER_16k,
  107. LP_FILTER_ORDER_16k * sizeof(*synth));
  108. FFSWAP(float *, filt_mem[0], filt_mem[1]);
  109. for (i = 0, s = 0; i < 30; i++, s += 1.0/30)
  110. out_data[i] = tmpbuf[i] + s * (synth[i] - tmpbuf[i]);
  111. }
  112. /**
  113. * Floating point version of ff_acelp_lp_decode().
  114. */
  115. static void acelp_lp_decodef(float *lp_1st, float *lp_2nd,
  116. const double *lsp_2nd, const double *lsp_prev)
  117. {
  118. double lsp_1st[LP_FILTER_ORDER_16k];
  119. int i;
  120. /* LSP values for first subframe (3.2.5 of G.729, Equation 24) */
  121. for (i = 0; i < LP_FILTER_ORDER_16k; i++)
  122. lsp_1st[i] = (lsp_2nd[i] + lsp_prev[i]) * 0.5;
  123. ff_acelp_lspd2lpc(lsp_1st, lp_1st, LP_FILTER_ORDER_16k >> 1);
  124. /* LSP values for second subframe (3.2.5 of G.729) */
  125. ff_acelp_lspd2lpc(lsp_2nd, lp_2nd, LP_FILTER_ORDER_16k >> 1);
  126. }
  127. /**
  128. * Floating point version of ff_acelp_decode_gain_code().
  129. */
  130. static float acelp_decode_gain_codef(float gain_corr_factor, const float *fc_v,
  131. float mr_energy, const float *quant_energy,
  132. const float *ma_prediction_coeff,
  133. int subframe_size, int ma_pred_order)
  134. {
  135. mr_energy +=
  136. ff_dot_productf(quant_energy, ma_prediction_coeff, ma_pred_order);
  137. mr_energy = gain_corr_factor * exp(M_LN10 / 20. * mr_energy) /
  138. sqrt((0.01 + ff_dot_productf(fc_v, fc_v, subframe_size)));
  139. return mr_energy;
  140. }
  141. #define DIVIDE_BY_3(x) ((x) * 10923 >> 15)
  142. void ff_sipr_decode_frame_16k(SiprContext *ctx, SiprParameters *params,
  143. float *out_data)
  144. {
  145. int frame_size = SUBFRAME_COUNT_16k * L_SUBFR_16k;
  146. float *synth = ctx->synth_buf + LP_FILTER_ORDER_16k;
  147. float lsf_new[LP_FILTER_ORDER_16k];
  148. double lsp_new[LP_FILTER_ORDER_16k];
  149. float Az[2][LP_FILTER_ORDER_16k];
  150. float fixed_vector[L_SUBFR_16k];
  151. float pitch_fac, gain_code;
  152. int i;
  153. int pitch_delay_3x;
  154. float *excitation = ctx->excitation + 292;
  155. lsf_decode_fp_16k(ctx->lsf_history, lsf_new, params->vq_indexes,
  156. params->ma_pred_switch);
  157. ff_set_min_dist_lsf(lsf_new, LSFQ_DIFF_MIN / 2, LP_FILTER_ORDER_16k);
  158. lsf2lsp(lsf_new, lsp_new);
  159. acelp_lp_decodef(Az[0], Az[1], lsp_new, ctx->lsp_history_16k);
  160. memcpy(ctx->lsp_history_16k, lsp_new, LP_FILTER_ORDER_16k * sizeof(double));
  161. memcpy(synth - LP_FILTER_ORDER_16k, ctx->synth,
  162. LP_FILTER_ORDER_16k * sizeof(*synth));
  163. for (i = 0; i < SUBFRAME_COUNT_16k; i++) {
  164. int i_subfr = i * L_SUBFR_16k;
  165. AMRFixed f;
  166. float gain_corr_factor;
  167. int pitch_delay_int;
  168. int pitch_delay_frac;
  169. if (!i) {
  170. pitch_delay_3x = dec_delay3_1st(params->pitch_delay[i]);
  171. } else
  172. pitch_delay_3x = dec_delay3_2nd(params->pitch_delay[i],
  173. PITCH_MIN, PITCH_MAX,
  174. ctx->pitch_lag_prev);
  175. pitch_fac = gain_pitch_cb_16k[params->gp_index[i]];
  176. f.pitch_fac = FFMIN(pitch_fac, 1.0);
  177. f.pitch_lag = DIVIDE_BY_3(pitch_delay_3x+1);
  178. ctx->pitch_lag_prev = f.pitch_lag;
  179. pitch_delay_int = DIVIDE_BY_3(pitch_delay_3x + 2);
  180. pitch_delay_frac = pitch_delay_3x + 2 - 3*pitch_delay_int;
  181. ff_acelp_interpolatef(&excitation[i_subfr],
  182. &excitation[i_subfr] - pitch_delay_int + 1,
  183. sinc_win, 3, pitch_delay_frac + 1,
  184. LP_FILTER_ORDER, L_SUBFR_16k);
  185. memset(fixed_vector, 0, sizeof(fixed_vector));
  186. ff_decode_10_pulses_35bits(params->fc_indexes[i], &f,
  187. ff_fc_4pulses_8bits_tracks_13, 5, 4);
  188. ff_set_fixed_vector(fixed_vector, &f, 1.0, L_SUBFR_16k);
  189. gain_corr_factor = gain_cb_16k[params->gc_index[i]];
  190. gain_code = gain_corr_factor *
  191. acelp_decode_gain_codef(sqrt(L_SUBFR_16k), fixed_vector,
  192. 19.0 - 15.0/(0.05*M_LN10/M_LN2),
  193. pred_16k, ctx->energy_history,
  194. L_SUBFR_16k, 2);
  195. ctx->energy_history[1] = ctx->energy_history[0];
  196. ctx->energy_history[0] = 20.0 * log10f(gain_corr_factor);
  197. ff_weighted_vector_sumf(&excitation[i_subfr], &excitation[i_subfr],
  198. fixed_vector, pitch_fac,
  199. gain_code, L_SUBFR_16k);
  200. ff_celp_lp_synthesis_filterf(synth + i_subfr, Az[i],
  201. &excitation[i_subfr], L_SUBFR_16k,
  202. LP_FILTER_ORDER_16k);
  203. }
  204. memcpy(ctx->synth, synth + frame_size - LP_FILTER_ORDER_16k,
  205. LP_FILTER_ORDER_16k * sizeof(*synth));
  206. memmove(ctx->excitation, ctx->excitation + 2 * L_SUBFR_16k,
  207. (L_INTERPOL+PITCH_MAX) * sizeof(float));
  208. postfilter(out_data, synth, ctx->iir_mem, ctx->filt_mem, ctx->mem_preemph);
  209. memcpy(ctx->iir_mem, Az[1], LP_FILTER_ORDER_16k * sizeof(float));
  210. }
  211. void ff_sipr_init_16k(SiprContext *ctx)
  212. {
  213. int i;
  214. for (i = 0; i < LP_FILTER_ORDER_16k; i++)
  215. ctx->lsp_history_16k[i] = cos((i + 1) * M_PI/(LP_FILTER_ORDER_16k + 1));
  216. ctx->filt_mem[0] = ctx->filt_buf[0];
  217. ctx->filt_mem[1] = ctx->filt_buf[1];
  218. ctx->pitch_lag_prev = 180;
  219. }