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.

161 lines
4.9KB

  1. /*
  2. * gsm 06.10 decoder
  3. * Copyright (c) 2010 Reimar Döffinger <Reimar.Doeffinger@gmx.de>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * GSM decoder
  24. */
  25. #include "get_bits.h"
  26. #include "gsm.h"
  27. #include "gsmdec_data.h"
  28. static const int requant_tab[4][8] = {
  29. { 0 },
  30. { 0, 7 },
  31. { 0, 2, 5, 7 },
  32. { 0, 1, 2, 3, 4, 5, 6, 7 }
  33. };
  34. static void apcm_dequant_add(GetBitContext *gb, int16_t *dst, const int *frame_bits)
  35. {
  36. int i, val;
  37. int maxidx = get_bits(gb, 6);
  38. const int16_t *tab = ff_gsm_dequant_tab[maxidx];
  39. for (i = 0; i < 13; i++) {
  40. val = get_bits(gb, frame_bits[i]);
  41. dst[3*i] += tab[requant_tab[frame_bits[i]][val]];
  42. }
  43. }
  44. static inline int gsm_mult(int a, int b)
  45. {
  46. return (a * b + (1 << 14)) >> 15;
  47. }
  48. static void long_term_synth(int16_t *dst, int lag, int gain_idx)
  49. {
  50. int i;
  51. const int16_t *src = dst - lag;
  52. uint16_t gain = ff_gsm_long_term_gain_tab[gain_idx];
  53. for (i = 0; i < 40; i++)
  54. dst[i] = gsm_mult(gain, src[i]);
  55. }
  56. static inline int decode_log_area(int coded, int factor, int offset)
  57. {
  58. coded <<= 10;
  59. coded -= offset;
  60. return gsm_mult(coded, factor) << 1;
  61. }
  62. static av_noinline int get_rrp(int filtered)
  63. {
  64. int abs = FFABS(filtered);
  65. if (abs < 11059) abs <<= 1;
  66. else if (abs < 20070) abs += 11059;
  67. else abs = (abs >> 2) + 26112;
  68. return filtered < 0 ? -abs : abs;
  69. }
  70. static int filter_value(int in, int rrp[8], int v[9])
  71. {
  72. int i;
  73. for (i = 7; i >= 0; i--) {
  74. in -= gsm_mult(rrp[i], v[i]);
  75. v[i + 1] = v[i] + gsm_mult(rrp[i], in);
  76. }
  77. v[0] = in;
  78. return in;
  79. }
  80. static void short_term_synth(GSMContext *ctx, int16_t *dst, const int16_t *src)
  81. {
  82. int i;
  83. int rrp[8];
  84. int *lar = ctx->lar[ctx->lar_idx];
  85. int *lar_prev = ctx->lar[ctx->lar_idx ^ 1];
  86. for (i = 0; i < 8; i++)
  87. rrp[i] = get_rrp((lar_prev[i] >> 2) + (lar_prev[i] >> 1) + (lar[i] >> 2));
  88. for (i = 0; i < 13; i++)
  89. dst[i] = filter_value(src[i], rrp, ctx->v);
  90. for (i = 0; i < 8; i++)
  91. rrp[i] = get_rrp((lar_prev[i] >> 1) + (lar [i] >> 1));
  92. for (i = 13; i < 27; i++)
  93. dst[i] = filter_value(src[i], rrp, ctx->v);
  94. for (i = 0; i < 8; i++)
  95. rrp[i] = get_rrp((lar_prev[i] >> 2) + (lar [i] >> 1) + (lar[i] >> 2));
  96. for (i = 27; i < 40; i++)
  97. dst[i] = filter_value(src[i], rrp, ctx->v);
  98. for (i = 0; i < 8; i++)
  99. rrp[i] = get_rrp(lar[i]);
  100. for (i = 40; i < 160; i++)
  101. dst[i] = filter_value(src[i], rrp, ctx->v);
  102. ctx->lar_idx ^= 1;
  103. }
  104. static int postprocess(int16_t *data, int msr)
  105. {
  106. int i;
  107. for (i = 0; i < 160; i++) {
  108. msr = av_clip_int16(data[i] + gsm_mult(msr, 28180));
  109. data[i] = av_clip_int16(msr << 1) & ~7;
  110. }
  111. return msr;
  112. }
  113. static int gsm_decode_block(AVCodecContext *avctx, int16_t *samples,
  114. GetBitContext *gb, int mode)
  115. {
  116. GSMContext *ctx = avctx->priv_data;
  117. int i;
  118. int16_t *ref_dst = ctx->ref_buf + 120;
  119. int *lar = ctx->lar[ctx->lar_idx];
  120. lar[0] = decode_log_area(get_bits(gb, 6), 13107, 1 << 15);
  121. lar[1] = decode_log_area(get_bits(gb, 6), 13107, 1 << 15);
  122. lar[2] = decode_log_area(get_bits(gb, 5), 13107, (1 << 14) + 2048*2);
  123. lar[3] = decode_log_area(get_bits(gb, 5), 13107, (1 << 14) - 2560*2);
  124. lar[4] = decode_log_area(get_bits(gb, 4), 19223, (1 << 13) + 94*2);
  125. lar[5] = decode_log_area(get_bits(gb, 4), 17476, (1 << 13) - 1792*2);
  126. lar[6] = decode_log_area(get_bits(gb, 3), 31454, (1 << 12) - 341*2);
  127. lar[7] = decode_log_area(get_bits(gb, 3), 29708, (1 << 12) - 1144*2);
  128. for (i = 0; i < 4; i++) {
  129. int lag = get_bits(gb, 7);
  130. int gain_idx = get_bits(gb, 2);
  131. int offset = get_bits(gb, 2);
  132. lag = av_clip(lag, 40, 120);
  133. long_term_synth(ref_dst, lag, gain_idx);
  134. apcm_dequant_add(gb, ref_dst + offset, ff_gsm_apcm_bits[mode][i]);
  135. ref_dst += 40;
  136. }
  137. memcpy(ctx->ref_buf, ctx->ref_buf + 160, 120 * sizeof(*ctx->ref_buf));
  138. short_term_synth(ctx, samples, ctx->ref_buf + 120);
  139. // for optimal speed this could be merged with short_term_synth,
  140. // not done yet because it is a bit ugly
  141. ctx->msr = postprocess(samples, ctx->msr);
  142. return 0;
  143. }