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.

308 lines
9.7KB

  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. * Initialize the speech codec according to the specification.
  48. *
  49. * TIA/EIA/IS-733 2.4.9
  50. */
  51. static av_cold int qcelp_decode_init(AVCodecContext *avctx) {
  52. QCELPContext *q = avctx->priv_data;
  53. int i;
  54. avctx->sample_fmt = SAMPLE_FMT_FLT;
  55. for (i = 0; i < 10; i++)
  56. q->prev_lspf[i] = (i + 1) / 11.;
  57. return 0;
  58. }
  59. /**
  60. * Computes the scaled codebook vector Cdn From INDEX and GAIN
  61. * for all rates.
  62. *
  63. * The specification lacks some information here.
  64. *
  65. * TIA/EIA/IS-733 has an omission on the codebook index determination
  66. * formula for RATE_FULL and RATE_HALF frames at section 2.4.8.1.1. It says
  67. * you have to subtract the decoded index parameter from the given scaled
  68. * codebook vector index 'n' to get the desired circular codebook index, but
  69. * it does not mention that you have to clamp 'n' to [0-9] in order to get
  70. * RI-compliant results.
  71. *
  72. * The reason for this mistake seems to be the fact they forgot to mention you
  73. * have to do these calculations per codebook subframe and adjust given
  74. * equation values accordingly.
  75. *
  76. * @param q the context
  77. * @param gain array holding the 4 pitch subframe gain values
  78. * @param cdn_vector array for the generated scaled codebook vector
  79. */
  80. static void compute_svector(const QCELPContext *q,
  81. const float *gain,
  82. float *cdn_vector) {
  83. int i, j, k;
  84. uint16_t cbseed, cindex;
  85. float *rnd, tmp_gain, fir_filter_value;
  86. switch (q->framerate) {
  87. case RATE_FULL:
  88. for (i = 0; i < 16; i++) {
  89. tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
  90. cindex = -q->cindex[i];
  91. for (j = 0; j < 10; j++)
  92. *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cindex++ & 127];
  93. }
  94. break;
  95. case RATE_HALF:
  96. for (i = 0; i < 4; i++) {
  97. tmp_gain = gain[i] * QCELP_RATE_HALF_CODEBOOK_RATIO;
  98. cindex = -q->cindex[i];
  99. for (j = 0; j < 40; j++)
  100. *cdn_vector++ = tmp_gain * qcelp_rate_half_codebook[cindex++ & 127];
  101. }
  102. break;
  103. case RATE_QUARTER:
  104. cbseed = (0x0003 & q->lspv[4])<<14 |
  105. (0x003F & q->lspv[3])<< 8 |
  106. (0x0060 & q->lspv[2])<< 1 |
  107. (0x0007 & q->lspv[1])<< 3 |
  108. (0x0038 & q->lspv[0])>> 3 ;
  109. rnd = q->rnd_fir_filter_mem + 20;
  110. for (i = 0; i < 8; i++) {
  111. tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
  112. for (k = 0; k < 20; k++) {
  113. cbseed = 521 * cbseed + 259;
  114. *rnd = (int16_t)cbseed;
  115. // FIR filter
  116. fir_filter_value = 0.0;
  117. for (j = 0; j < 10; j++)
  118. fir_filter_value += qcelp_rnd_fir_coefs[j ] * (rnd[-j ] + rnd[-20+j]);
  119. fir_filter_value += qcelp_rnd_fir_coefs[10] * rnd[-10];
  120. *cdn_vector++ = tmp_gain * fir_filter_value;
  121. rnd++;
  122. }
  123. }
  124. memcpy(q->rnd_fir_filter_mem, q->rnd_fir_filter_mem + 160, 20 * sizeof(float));
  125. break;
  126. case RATE_OCTAVE:
  127. cbseed = q->first16bits;
  128. for (i = 0; i < 8; i++) {
  129. tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
  130. for (j = 0; j < 20; j++) {
  131. cbseed = 521 * cbseed + 259;
  132. *cdn_vector++ = tmp_gain * (int16_t)cbseed;
  133. }
  134. }
  135. break;
  136. case I_F_Q:
  137. cbseed = -44; // random codebook index
  138. for (i = 0; i < 4; i++) {
  139. tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
  140. for (j = 0; j < 40; j++)
  141. *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cbseed++ & 127];
  142. }
  143. break;
  144. }
  145. }
  146. /**
  147. * Apply generic gain control.
  148. *
  149. * @param v_out output vector
  150. * @param v_in gain-controlled vector
  151. * @param v_ref vector to control gain of
  152. *
  153. * FIXME: If v_ref is a zero vector, it energy is zero
  154. * and the behavior of the gain control is
  155. * undefined in the specs.
  156. *
  157. * TIA/EIA/IS-733 2.4.8.3-2/3/4/5, 2.4.8.6
  158. */
  159. static void apply_gain_ctrl(float *v_out,
  160. const float *v_ref,
  161. const float *v_in) {
  162. int i, j, len;
  163. float scalefactor;
  164. for (i = 0, j = 0; i < 4; i++) {
  165. scalefactor = ff_dot_productf(v_in + j, v_in + j, 40);
  166. if (scalefactor)
  167. scalefactor = sqrt(ff_dot_productf(v_ref + j, v_ref + j, 40) / scalefactor);
  168. else
  169. av_log_missing_feature(NULL, "Zero energy for gain control", 1);
  170. for (len = j + 40; j < len; j++)
  171. v_out[j] = scalefactor * v_in[j];
  172. }
  173. }
  174. /**
  175. * Apply filter in pitch-subframe steps.
  176. *
  177. * @param memory buffer for the previous state of the filter
  178. * - must be able to contain 303 elements
  179. * - the 143 first elements are from the previous state
  180. * - the next 160 are for output
  181. * @param v_in input filter vector
  182. * @param gain per-subframe gain array, each element is between 0.0 and 2.0
  183. * @param lag per-subframe lag array, each element is
  184. * - between 16 and 143 if its corresponding pfrac is 0,
  185. * - between 16 and 139 otherwise
  186. * @param pfrac per-subframe boolean array, 1 if the lag is fractional, 0 otherwise
  187. *
  188. * @return filter output vector
  189. */
  190. static const float *do_pitchfilter(float memory[303],
  191. const float v_in[160],
  192. const float gain[4],
  193. const uint8_t *lag,
  194. const uint8_t pfrac[4]) {
  195. int i, j;
  196. float *v_lag, *v_out;
  197. const float *v_len;
  198. v_out = memory + 143; // Output vector starts at memory[143].
  199. for (i = 0; i < 4; i++)
  200. if (gain[i]) {
  201. v_lag = memory + 143 + 40 * i - lag[i];
  202. for (v_len = v_in + 40; v_in < v_len; v_in++) {
  203. if (pfrac[i]) { // If it is a fractional lag...
  204. for (j = 0, *v_out = 0.; j < 4; j++)
  205. *v_out += qcelp_hammsinc_table[j] * (v_lag[j-4] + v_lag[3-j]);
  206. } else
  207. *v_out = *v_lag;
  208. *v_out = *v_in + gain[i] * *v_out;
  209. v_lag++;
  210. v_out++;
  211. }
  212. } else {
  213. memcpy(v_out, v_in, 40 * sizeof(float));
  214. v_in += 40;
  215. v_out += 40;
  216. }
  217. memmove(memory, memory + 160, 143 * sizeof(float));
  218. return memory + 143;
  219. }
  220. /**
  221. * Interpolates LSP frequencies and computes LPC coefficients
  222. * for a given framerate & pitch subframe.
  223. *
  224. * TIA/EIA/IS-733 2.4.3.3.4
  225. *
  226. * @param q the context
  227. * @param curr_lspf LSP frequencies vector of the current frame
  228. * @param lpc float vector for the resulting LPC
  229. * @param subframe_num frame number in decoded stream
  230. */
  231. void interpolate_lpc(QCELPContext *q,
  232. const float *curr_lspf,
  233. float *lpc,
  234. const int subframe_num) {
  235. float interpolated_lspf[10];
  236. float weight;
  237. if (q->framerate >= RATE_QUARTER) {
  238. weight = 0.25 * (subframe_num + 1);
  239. } else if (q->framerate == RATE_OCTAVE && !subframe_num) {
  240. weight = 0.625;
  241. } else {
  242. weight = 1.0;
  243. }
  244. if (weight != 1.0) {
  245. weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf, weight, 1.0 - weight, 10);
  246. qcelp_lspf2lpc(interpolated_lspf, lpc);
  247. } else if (q->framerate >= RATE_QUARTER || (q->framerate == I_F_Q && !subframe_num))
  248. qcelp_lspf2lpc(curr_lspf, lpc);
  249. }
  250. static int buf_size2framerate(const int buf_size) {
  251. switch (buf_size) {
  252. case 35:
  253. return RATE_FULL;
  254. case 17:
  255. return RATE_HALF;
  256. case 8:
  257. return RATE_QUARTER;
  258. case 4:
  259. return RATE_OCTAVE;
  260. case 1:
  261. return SILENCE;
  262. }
  263. return -1;
  264. }
  265. static void warn_insufficient_frame_quality(AVCodecContext *avctx,
  266. const char *message) {
  267. av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n", avctx->frame_number, message);
  268. }
  269. AVCodec qcelp_decoder =
  270. {
  271. .name = "qcelp",
  272. .type = CODEC_TYPE_AUDIO,
  273. .id = CODEC_ID_QCELP,
  274. .init = qcelp_decode_init,
  275. .decode = qcelp_decode_frame,
  276. .priv_data_size = sizeof(QCELPContext),
  277. .long_name = NULL_IF_CONFIG_SMALL("QCELP / PureVoice"),
  278. };