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.

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