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.

120 lines
3.3KB

  1. /*
  2. * gain code, gain pitch and pitch delay decoding
  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 "avcodec.h"
  23. #include "acelp_pitch_delay.h"
  24. #include "acelp_math.h"
  25. int ff_acelp_decode_8bit_to_1st_delay3(int ac_index)
  26. {
  27. ac_index += 58;
  28. if(ac_index > 254)
  29. ac_index = 3 * ac_index - 510;
  30. return ac_index;
  31. }
  32. int ff_acelp_decode_4bit_to_2nd_delay3(
  33. int ac_index,
  34. int pitch_delay_min)
  35. {
  36. if(ac_index < 4)
  37. return 3 * (ac_index + pitch_delay_min);
  38. else if(ac_index < 12)
  39. return 3 * pitch_delay_min + ac_index + 6;
  40. else
  41. return 3 * (ac_index + pitch_delay_min) - 18;
  42. }
  43. int ff_acelp_decode_5_6_bit_to_2nd_delay3(
  44. int ac_index,
  45. int pitch_delay_min)
  46. {
  47. return 3 * pitch_delay_min + ac_index - 2;
  48. }
  49. int ff_acelp_decode_9bit_to_1st_delay6(int ac_index)
  50. {
  51. if(ac_index < 463)
  52. return ac_index + 105;
  53. else
  54. return 6 * (ac_index - 368);
  55. }
  56. int ff_acelp_decode_6bit_to_2nd_delay6(
  57. int ac_index,
  58. int pitch_delay_min)
  59. {
  60. return 6 * pitch_delay_min + ac_index - 3;
  61. }
  62. void ff_acelp_update_past_gain(
  63. int16_t* quant_energy,
  64. int gain_corr_factor,
  65. int log2_ma_pred_order,
  66. int erasure)
  67. {
  68. int i;
  69. int avg_gain=quant_energy[(1 << log2_ma_pred_order) - 1]; // (5.10)
  70. for(i=(1 << log2_ma_pred_order) - 1; i>0; i--)
  71. {
  72. avg_gain += quant_energy[i-1];
  73. quant_energy[i] = quant_energy[i-1];
  74. }
  75. if(erasure)
  76. quant_energy[0] = FFMAX(avg_gain >> log2_ma_pred_order, -10240) - 4096; // -10 and -4 in (5.10)
  77. else
  78. quant_energy[0] = (6165 * ((ff_log2(gain_corr_factor) >> 2) - (13 << 13))) >> 13;
  79. }
  80. int16_t ff_acelp_decode_gain_code(
  81. int gain_corr_factor,
  82. const int16_t* fc_v,
  83. int mr_energy,
  84. const int16_t* quant_energy,
  85. const int16_t* ma_prediction_coeff,
  86. int subframe_size,
  87. int ma_pred_order)
  88. {
  89. int i;
  90. mr_energy <<= 10;
  91. for(i=0; i<ma_pred_order; i++)
  92. mr_energy += quant_energy[i] * ma_prediction_coeff[i];
  93. #ifdef G729_BITEXACT
  94. mr_energy += (((-6165LL * ff_log2(dot_product(fc_v, fc_v, subframe_size, 0))) >> 3) & ~0x3ff);
  95. mr_energy = (5439 * (mr_energy >> 15)) >> 8; // (0.15) = (0.15) * (7.23)
  96. return bidir_sal(
  97. ((ff_exp2(mr_energy & 0x7fff) + 16) >> 5) * (gain_corr_factor >> 1),
  98. (mr_energy >> 15) - 25
  99. );
  100. #else
  101. mr_energy = gain_corr_factor * exp(M_LN10 / (20 << 23) * mr_energy) /
  102. sqrt(dot_product(fc_v, fc_v, subframe_size, 0));
  103. return mr_energy >> 12;
  104. #endif
  105. }