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.

126 lines
3.8KB

  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. static int buf_size2framerate(const int buf_size) {
  93. switch (buf_size) {
  94. case 35:
  95. return RATE_FULL;
  96. case 17:
  97. return RATE_HALF;
  98. case 8:
  99. return RATE_QUARTER;
  100. case 4:
  101. return RATE_OCTAVE;
  102. case 1:
  103. return SILENCE;
  104. }
  105. return -1;
  106. }
  107. static void warn_insufficient_frame_quality(AVCodecContext *avctx,
  108. const char *message) {
  109. av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n", avctx->frame_number, message);
  110. }