sipr16k.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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/mathematics.h"
  26. #include "lsp.h"
  27. #include "celp_math.h"
  28. #include "acelp_vectors.h"
  29. #include "acelp_pitch_delay.h"
  30. #include "acelp_filters.h"
  31. #include "celp_filters.h"
  32. #include "sipr16kdata.h"
  33. /**
  34. * Convert an lsf vector into an lsp vector.
  35. *
  36. * @param lsf input lsf vector
  37. * @param lsp output lsp vector
  38. */
  39. static void lsf2lsp(const float *lsf, double *lsp)
  40. {
  41. int i;
  42. for (i = 0; i < LP_FILTER_ORDER_16k; i++)
  43. lsp[i] = cosf(lsf[i]);
  44. }
  45. static void dequant(float *out, const int *idx, const float *cbs[])
  46. {
  47. int i;
  48. for (i = 0; i < 4; i++)
  49. memcpy(out + 3*i, cbs[i] + 3*idx[i], 3*sizeof(float));
  50. memcpy(out + 12, cbs[4] + 4*idx[4], 4*sizeof(float));
  51. }
  52. static void lsf_decode_fp_16k(float* lsf_history, float* isp_new,
  53. const int* parm, int ma_pred)
  54. {
  55. int i;
  56. float isp_q[LP_FILTER_ORDER_16k];
  57. dequant(isp_q, parm, lsf_codebooks_16k);
  58. for (i = 0; i < LP_FILTER_ORDER_16k; i++) {
  59. isp_new[i] = (1 - qu[ma_pred]) * isp_q[i]
  60. + qu[ma_pred] * lsf_history[i]
  61. + mean_lsf_16k[i];
  62. }
  63. memcpy(lsf_history, isp_q, LP_FILTER_ORDER_16k * sizeof(float));
  64. }
  65. static int dec_delay3_1st(int index)
  66. {
  67. if (index < 390) {
  68. return index + 88;
  69. } else
  70. return 3 * index - 690;
  71. }
  72. static int dec_delay3_2nd(int index, int pit_min, int pit_max,
  73. int pitch_lag_prev)
  74. {
  75. if (index < 62) {
  76. int pitch_delay_min = av_clip(pitch_lag_prev - 10,
  77. pit_min, pit_max - 19);
  78. return 3 * pitch_delay_min + index - 2;
  79. } else
  80. return 3 * pitch_lag_prev;
  81. }
  82. static void postfilter(float* synth, float* iir_mem, float* filt_mem[2],
  83. float* mem_preemph)
  84. {
  85. float buf[30 + LP_FILTER_ORDER_16k];
  86. float *tmpbuf = buf + LP_FILTER_ORDER_16k;
  87. float s;
  88. int i;
  89. for (i = 0; i < LP_FILTER_ORDER_16k; i++)
  90. filt_mem[0][i] = iir_mem[i] * ff_pow_0_5[i];
  91. memcpy(tmpbuf - LP_FILTER_ORDER_16k, mem_preemph,
  92. LP_FILTER_ORDER_16k*sizeof(*buf));
  93. ff_celp_lp_synthesis_filterf(tmpbuf, filt_mem[1], synth, 30,
  94. LP_FILTER_ORDER_16k);
  95. memcpy(synth - LP_FILTER_ORDER_16k, mem_preemph,
  96. LP_FILTER_ORDER_16k * sizeof(*synth));
  97. ff_celp_lp_synthesis_filterf(synth, filt_mem[0], synth, 2*L_SUBFR_16k,
  98. LP_FILTER_ORDER_16k);
  99. memcpy(mem_preemph, synth + 2*L_SUBFR_16k - LP_FILTER_ORDER_16k,
  100. LP_FILTER_ORDER_16k * sizeof(*synth));
  101. FFSWAP(float *, filt_mem[0], filt_mem[1]);
  102. for (i = 0, s = 0; i < 30; i++, s += 1.0/30)
  103. synth[i] = tmpbuf[i] + s * (synth[i] - tmpbuf[i]);
  104. }
  105. /**
  106. * Floating point version of ff_acelp_lp_decode().
  107. */
  108. static void acelp_lp_decodef(float *lp_1st, float *lp_2nd,
  109. const double *lsp_2nd, const double *lsp_prev)
  110. {
  111. double lsp_1st[LP_FILTER_ORDER_16k];
  112. int i;
  113. /* LSP values for first subframe (3.2.5 of G.729, Equation 24) */
  114. for (i = 0; i < LP_FILTER_ORDER_16k; i++)
  115. lsp_1st[i] = (lsp_2nd[i] + lsp_prev[i]) * 0.5;
  116. ff_acelp_lspd2lpc(lsp_1st, lp_1st, LP_FILTER_ORDER_16k >> 1);
  117. /* LSP values for second subframe (3.2.5 of G.729) */
  118. ff_acelp_lspd2lpc(lsp_2nd, lp_2nd, LP_FILTER_ORDER_16k >> 1);
  119. }
  120. /**
  121. * Floating point version of ff_acelp_decode_gain_code().
  122. */
  123. static float acelp_decode_gain_codef(float gain_corr_factor, const float *fc_v,
  124. float mr_energy, const float *quant_energy,
  125. const float *ma_prediction_coeff,
  126. int subframe_size, int ma_pred_order)
  127. {
  128. mr_energy +=
  129. ff_dot_productf(quant_energy, ma_prediction_coeff, ma_pred_order);
  130. mr_energy = gain_corr_factor * exp(M_LN10 / 20. * mr_energy) /
  131. sqrt((0.01 + ff_dot_productf(fc_v, fc_v, subframe_size)));
  132. return mr_energy;
  133. }
  134. #define DIVIDE_BY_3(x) ((x) * 10923 >> 15)
  135. void ff_sipr_decode_frame_16k(SiprContext *ctx, SiprParameters *params,
  136. float *out_data)
  137. {
  138. int frame_size = SUBFRAME_COUNT_16k * L_SUBFR_16k;
  139. float *synth = ctx->synth_buf + LP_FILTER_ORDER_16k;
  140. float lsf_new[LP_FILTER_ORDER_16k];
  141. double lsp_new[LP_FILTER_ORDER_16k];
  142. float Az[2][LP_FILTER_ORDER_16k];
  143. float fixed_vector[L_SUBFR_16k];
  144. float pitch_fac, gain_code;
  145. int i;
  146. int pitch_delay_3x;
  147. float *excitation = ctx->excitation + 292;
  148. lsf_decode_fp_16k(ctx->lsf_history, lsf_new, params->vq_indexes,
  149. params->ma_pred_switch);
  150. ff_set_min_dist_lsf(lsf_new, LSFQ_DIFF_MIN / 2, LP_FILTER_ORDER_16k);
  151. lsf2lsp(lsf_new, lsp_new);
  152. acelp_lp_decodef(Az[0], Az[1], lsp_new, ctx->lsp_history_16k);
  153. memcpy(ctx->lsp_history_16k, lsp_new, LP_FILTER_ORDER_16k * sizeof(double));
  154. memcpy(synth - LP_FILTER_ORDER_16k, ctx->synth,
  155. LP_FILTER_ORDER_16k * sizeof(*synth));
  156. for (i = 0; i < SUBFRAME_COUNT_16k; i++) {
  157. int i_subfr = i * L_SUBFR_16k;
  158. AMRFixed f;
  159. float gain_corr_factor;
  160. int pitch_delay_int;
  161. int pitch_delay_frac;
  162. if (!i) {
  163. pitch_delay_3x = dec_delay3_1st(params->pitch_delay[i]);
  164. } else
  165. pitch_delay_3x = dec_delay3_2nd(params->pitch_delay[i],
  166. PITCH_MIN, PITCH_MAX,
  167. ctx->pitch_lag_prev);
  168. pitch_fac = gain_pitch_cb_16k[params->gp_index[i]];
  169. f.pitch_fac = FFMIN(pitch_fac, 1.0);
  170. f.pitch_lag = DIVIDE_BY_3(pitch_delay_3x+1);
  171. ctx->pitch_lag_prev = f.pitch_lag;
  172. pitch_delay_int = DIVIDE_BY_3(pitch_delay_3x + 2);
  173. pitch_delay_frac = pitch_delay_3x + 2 - 3*pitch_delay_int;
  174. ff_acelp_interpolatef(&excitation[i_subfr],
  175. &excitation[i_subfr] - pitch_delay_int + 1,
  176. sinc_win, 3, pitch_delay_frac + 1,
  177. LP_FILTER_ORDER, L_SUBFR_16k);
  178. memset(fixed_vector, 0, sizeof(fixed_vector));
  179. ff_decode_10_pulses_35bits(params->fc_indexes[i], &f,
  180. ff_fc_4pulses_8bits_tracks_13, 5, 4);
  181. ff_set_fixed_vector(fixed_vector, &f, 1.0, L_SUBFR_16k);
  182. gain_corr_factor = gain_cb_16k[params->gc_index[i]];
  183. gain_code = gain_corr_factor *
  184. acelp_decode_gain_codef(sqrt(L_SUBFR_16k), fixed_vector,
  185. 19.0 - 15.0/(0.05*M_LN10/M_LN2),
  186. pred_16k, ctx->energy_history,
  187. L_SUBFR_16k, 2);
  188. ctx->energy_history[1] = ctx->energy_history[0];
  189. ctx->energy_history[0] = 20.0 * log10f(gain_corr_factor);
  190. ff_weighted_vector_sumf(&excitation[i_subfr], &excitation[i_subfr],
  191. fixed_vector, pitch_fac,
  192. gain_code, L_SUBFR_16k);
  193. ff_celp_lp_synthesis_filterf(synth + i_subfr, Az[i],
  194. &excitation[i_subfr], L_SUBFR_16k,
  195. LP_FILTER_ORDER_16k);
  196. }
  197. memcpy(ctx->synth, synth + frame_size - LP_FILTER_ORDER_16k,
  198. LP_FILTER_ORDER_16k * sizeof(*synth));
  199. memmove(ctx->excitation, ctx->excitation + 2 * L_SUBFR_16k,
  200. (L_INTERPOL+PITCH_MAX) * sizeof(float));
  201. postfilter(synth, ctx->iir_mem, ctx->filt_mem, ctx->mem_preemph);
  202. memcpy(ctx->iir_mem, Az[1], LP_FILTER_ORDER_16k * sizeof(float));
  203. ctx->dsp.vector_clipf(out_data, synth, -1, 32767./(1<<15), frame_size);
  204. }
  205. void ff_sipr_init_16k(SiprContext *ctx)
  206. {
  207. int i;
  208. for (i = 0; i < LP_FILTER_ORDER_16k; i++)
  209. ctx->lsp_history_16k[i] = cos((i + 1) * M_PI/(LP_FILTER_ORDER_16k + 1));
  210. ctx->filt_mem[0] = ctx->filt_buf[0];
  211. ctx->filt_mem[1] = ctx->filt_buf[1];
  212. ctx->pitch_lag_prev = 180;
  213. }