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.

170 lines
5.2KB

  1. /*
  2. * QCELP decoder
  3. * Copyright (c) 2007 Reynaldo H. Verdejo Pinochet
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file qcelpdec.c
  23. * QCELP decoder
  24. * @author Reynaldo H. Verdejo Pinochet
  25. */
  26. #include <stddef.h>
  27. #include "avcodec.h"
  28. #include "bitstream.h"
  29. #include "qcelp.h"
  30. #include "qcelpdata.h"
  31. #include "celp_math.h"
  32. #include "celp_filters.h"
  33. #undef NDEBUG
  34. #include <assert.h>
  35. static void weighted_vector_sumf(float *out,
  36. const float *in_a,
  37. const float *in_b,
  38. float weight_coeff_a,
  39. float weight_coeff_b,
  40. int length) {
  41. int i;
  42. for (i = 0; i < length; i++)
  43. out[i] = weight_coeff_a * in_a[i]
  44. + weight_coeff_b * in_b[i];
  45. }
  46. /**
  47. * Apply filter in pitch-subframe steps.
  48. *
  49. * @param memory buffer for the previous state of the filter
  50. * - must be able to contain 303 elements
  51. * - the 143 first elements are from the previous state
  52. * - the next 160 are for output
  53. * @param v_in input filter vector
  54. * @param gain per-subframe gain array, each element is between 0.0 and 2.0
  55. * @param lag per-subframe lag array, each element is
  56. * - between 16 and 143 if its corresponding pfrac is 0,
  57. * - between 16 and 139 otherwise
  58. * @param pfrac per-subframe boolean array, 1 if the lag is fractional, 0 otherwise
  59. *
  60. * @return filter output vector
  61. */
  62. static const float *do_pitchfilter(float memory[303],
  63. const float v_in[160],
  64. const float gain[4],
  65. const uint8_t *lag,
  66. const uint8_t pfrac[4]) {
  67. int i, j;
  68. float *v_lag, *v_out;
  69. const float *v_len;
  70. v_out = memory + 143; // Output vector starts at memory[143].
  71. for (i = 0; i < 4; i++)
  72. if (gain[i]) {
  73. v_lag = memory + 143 + 40 * i - lag[i];
  74. for (v_len = v_in + 40; v_in < v_len; v_in++) {
  75. if (pfrac[i]) { // If it is a fractional lag...
  76. for (j = 0, *v_out = 0.; j < 4; j++)
  77. *v_out += qcelp_hammsinc_table[j] * (v_lag[j-4] + v_lag[3-j]);
  78. } else
  79. *v_out = *v_lag;
  80. *v_out = *v_in + gain[i] * *v_out;
  81. v_lag++;
  82. v_out++;
  83. }
  84. } else {
  85. memcpy(v_out, v_in, 40 * sizeof(float));
  86. v_in += 40;
  87. v_out += 40;
  88. }
  89. memmove(memory, memory + 160, 143 * sizeof(float));
  90. return memory + 143;
  91. }
  92. /**
  93. * Interpolates LSP frequencies and computes LPC coefficients
  94. * for a given framerate & pitch subframe.
  95. *
  96. * TIA/EIA/IS-733 2.4.3.3.4
  97. *
  98. * @param q the context
  99. * @param curr_lspf LSP frequencies vector of the current frame
  100. * @param lpc float vector for the resulting LPC
  101. * @param subframe_num frame number in decoded stream
  102. */
  103. void interpolate_lpc(QCELPContext *q,
  104. const float *curr_lspf,
  105. float *lpc,
  106. const int subframe_num) {
  107. float interpolated_lspf[10];
  108. float weight;
  109. if (q->framerate >= RATE_QUARTER) {
  110. weight = 0.25 * (subframe_num + 1);
  111. } else if (q->framerate == RATE_OCTAVE && !subframe_num) {
  112. weight = 0.625;
  113. } else {
  114. weight = 1.0;
  115. }
  116. if (weight != 1.0) {
  117. weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf, weight, 1.0 - weight, 10);
  118. lspf2lpc(q, interpolated_lspf, lpc);
  119. } else if (q->framerate >= RATE_QUARTER || (q->framerate == I_F_Q && !subframe_num))
  120. lspf2lpc(q, curr_lspf, lpc);
  121. }
  122. static int buf_size2framerate(const int buf_size) {
  123. switch (buf_size) {
  124. case 35:
  125. return RATE_FULL;
  126. case 17:
  127. return RATE_HALF;
  128. case 8:
  129. return RATE_QUARTER;
  130. case 4:
  131. return RATE_OCTAVE;
  132. case 1:
  133. return SILENCE;
  134. }
  135. return -1;
  136. }
  137. static void warn_insufficient_frame_quality(AVCodecContext *avctx,
  138. const char *message) {
  139. av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n", avctx->frame_number, message);
  140. }
  141. AVCodec qcelp_decoder =
  142. {
  143. .name = "qcelp",
  144. .type = CODEC_TYPE_AUDIO,
  145. .id = CODEC_ID_QCELP,
  146. .init = qcelp_decode_init,
  147. .decode = qcelp_decode_frame,
  148. .priv_data_size = sizeof(QCELPContext),
  149. .long_name = NULL_IF_CONFIG_SMALL("QCELP / PureVoice"),
  150. };