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.

452 lines
14KB

  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. {
  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. * Decodes the 10 quantized LSP frequencies from the LSPV/LSP
  61. * transmission codes of any bitrate and checks for badly received packets.
  62. *
  63. * @param q the context
  64. * @param lspf line spectral pair frequencies
  65. *
  66. * @return 0 on success, -1 if the packet is badly received
  67. *
  68. * TIA/EIA/IS-733 2.4.3.2.6.2-2, 2.4.8.7.3
  69. */
  70. static int decode_lspf(QCELPContext *q, float *lspf)
  71. {
  72. int i;
  73. float tmp_lspf;
  74. if(q->bitrate == RATE_OCTAVE || q->bitrate == I_F_Q)
  75. {
  76. float smooth;
  77. const float *predictors = (q->prev_bitrate != RATE_OCTAVE &&
  78. q->prev_bitrate != I_F_Q ? q->prev_lspf
  79. : q->predictor_lspf);
  80. if(q->bitrate == RATE_OCTAVE)
  81. {
  82. q->octave_count++;
  83. for(i=0; i<10; i++)
  84. {
  85. q->predictor_lspf[i] =
  86. lspf[i] = (q->lspv[i] ? QCELP_LSP_SPREAD_FACTOR
  87. : -QCELP_LSP_SPREAD_FACTOR)
  88. + predictors[i] * QCELP_LSP_OCTAVE_PREDICTOR
  89. + (i + 1) * ((1 - QCELP_LSP_OCTAVE_PREDICTOR)/11);
  90. }
  91. smooth = (q->octave_count < 10 ? .875 : 0.1);
  92. }else
  93. {
  94. float erasure_coeff = QCELP_LSP_OCTAVE_PREDICTOR;
  95. assert(q->bitrate == I_F_Q);
  96. if(q->erasure_count > 1)
  97. erasure_coeff *= (q->erasure_count < 4 ? 0.9 : 0.7);
  98. for(i=0; i<10; i++)
  99. {
  100. q->predictor_lspf[i] =
  101. lspf[i] = (i + 1) * ( 1 - erasure_coeff)/11
  102. + erasure_coeff * predictors[i];
  103. }
  104. smooth = 0.125;
  105. }
  106. // Check the stability of the LSP frequencies.
  107. lspf[0] = FFMAX(lspf[0], QCELP_LSP_SPREAD_FACTOR);
  108. for(i=1; i<10; i++)
  109. lspf[i] = FFMAX(lspf[i], (lspf[i-1] + QCELP_LSP_SPREAD_FACTOR));
  110. lspf[9] = FFMIN(lspf[9], (1.0 - QCELP_LSP_SPREAD_FACTOR));
  111. for(i=9; i>0; i--)
  112. lspf[i-1] = FFMIN(lspf[i-1], (lspf[i] - QCELP_LSP_SPREAD_FACTOR));
  113. // Low-pass filter the LSP frequencies.
  114. weighted_vector_sumf(lspf, lspf, q->prev_lspf, smooth, 1.0-smooth, 10);
  115. }else
  116. {
  117. q->octave_count = 0;
  118. tmp_lspf = 0.;
  119. for(i=0; i<5 ; i++)
  120. {
  121. lspf[2*i+0] = tmp_lspf += qcelp_lspvq[i][q->lspv[i]][0] * 0.0001;
  122. lspf[2*i+1] = tmp_lspf += qcelp_lspvq[i][q->lspv[i]][1] * 0.0001;
  123. }
  124. // Check for badly received packets.
  125. if(q->bitrate == RATE_QUARTER)
  126. {
  127. if(lspf[9] <= .70 || lspf[9] >= .97)
  128. return -1;
  129. for(i=3; i<10; i++)
  130. if(fabs(lspf[i] - lspf[i-2]) < .08)
  131. return -1;
  132. }else
  133. {
  134. if(lspf[9] <= .66 || lspf[9] >= .985)
  135. return -1;
  136. for(i=4; i<10; i++)
  137. if (fabs(lspf[i] - lspf[i-4]) < .0931)
  138. return -1;
  139. }
  140. }
  141. return 0;
  142. }
  143. /**
  144. * If the received packet is Rate 1/4 a further sanity check is made of the
  145. * codebook gain.
  146. *
  147. * @param cbgain the unpacked cbgain array
  148. * @return -1 if the sanity check fails, 0 otherwise
  149. *
  150. * TIA/EIA/IS-733 2.4.8.7.3
  151. */
  152. static int codebook_sanity_check_for_rate_quarter(const uint8_t *cbgain)
  153. {
  154. int i, prev_diff=0;
  155. for(i=1; i<5; i++)
  156. {
  157. int diff = cbgain[i] - cbgain[i-1];
  158. if(FFABS(diff) > 10)
  159. return -1;
  160. else if(FFABS(diff - prev_diff) > 12)
  161. return -1;
  162. prev_diff = diff;
  163. }
  164. return 0;
  165. }
  166. /**
  167. * Computes the scaled codebook vector Cdn From INDEX and GAIN
  168. * for all rates.
  169. *
  170. * The specification lacks some information here.
  171. *
  172. * TIA/EIA/IS-733 has an omission on the codebook index determination
  173. * formula for RATE_FULL and RATE_HALF frames at section 2.4.8.1.1. It says
  174. * you have to subtract the decoded index parameter from the given scaled
  175. * codebook vector index 'n' to get the desired circular codebook index, but
  176. * it does not mention that you have to clamp 'n' to [0-9] in order to get
  177. * RI-compliant results.
  178. *
  179. * The reason for this mistake seems to be the fact they forgot to mention you
  180. * have to do these calculations per codebook subframe and adjust given
  181. * equation values accordingly.
  182. *
  183. * @param q the context
  184. * @param gain array holding the 4 pitch subframe gain values
  185. * @param cdn_vector array for the generated scaled codebook vector
  186. */
  187. static void compute_svector(const QCELPContext *q, const float *gain,
  188. float *cdn_vector)
  189. {
  190. int i, j, k;
  191. uint16_t cbseed, cindex;
  192. float *rnd, tmp_gain, fir_filter_value;
  193. switch(q->bitrate)
  194. {
  195. case RATE_FULL:
  196. for(i=0; i<16; i++)
  197. {
  198. tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
  199. cindex = -q->cindex[i];
  200. for(j=0; j<10; j++)
  201. *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cindex++ & 127];
  202. }
  203. break;
  204. case RATE_HALF:
  205. for(i=0; i<4; i++)
  206. {
  207. tmp_gain = gain[i] * QCELP_RATE_HALF_CODEBOOK_RATIO;
  208. cindex = -q->cindex[i];
  209. for (j = 0; j < 40; j++)
  210. *cdn_vector++ = tmp_gain * qcelp_rate_half_codebook[cindex++ & 127];
  211. }
  212. break;
  213. case RATE_QUARTER:
  214. cbseed = (0x0003 & q->lspv[4])<<14 |
  215. (0x003F & q->lspv[3])<< 8 |
  216. (0x0060 & q->lspv[2])<< 1 |
  217. (0x0007 & q->lspv[1])<< 3 |
  218. (0x0038 & q->lspv[0])>> 3 ;
  219. rnd = q->rnd_fir_filter_mem + 20;
  220. for(i=0; i<8; i++)
  221. {
  222. tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
  223. for(k=0; k<20; k++)
  224. {
  225. cbseed = 521 * cbseed + 259;
  226. *rnd = (int16_t)cbseed;
  227. // FIR filter
  228. fir_filter_value = 0.0;
  229. for(j=0; j<10; j++)
  230. fir_filter_value += qcelp_rnd_fir_coefs[j ]
  231. * (rnd[-j ] + rnd[-20+j]);
  232. fir_filter_value += qcelp_rnd_fir_coefs[10] * rnd[-10];
  233. *cdn_vector++ = tmp_gain * fir_filter_value;
  234. rnd++;
  235. }
  236. }
  237. memcpy(q->rnd_fir_filter_mem, q->rnd_fir_filter_mem + 160, 20 * sizeof(float));
  238. break;
  239. case RATE_OCTAVE:
  240. cbseed = q->first16bits;
  241. for(i=0; i<8; i++)
  242. {
  243. tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
  244. for(j=0; j<20; j++)
  245. {
  246. cbseed = 521 * cbseed + 259;
  247. *cdn_vector++ = tmp_gain * (int16_t)cbseed;
  248. }
  249. }
  250. break;
  251. case I_F_Q:
  252. cbseed = -44; // random codebook index
  253. for(i=0; i<4; i++)
  254. {
  255. tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
  256. for(j=0; j<40; j++)
  257. *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cbseed++ & 127];
  258. }
  259. break;
  260. }
  261. }
  262. /**
  263. * Apply generic gain control.
  264. *
  265. * @param v_out output vector
  266. * @param v_in gain-controlled vector
  267. * @param v_ref vector to control gain of
  268. *
  269. * FIXME: If v_ref is a zero vector, it energy is zero
  270. * and the behavior of the gain control is
  271. * undefined in the specs.
  272. *
  273. * TIA/EIA/IS-733 2.4.8.3-2/3/4/5, 2.4.8.6
  274. */
  275. static void apply_gain_ctrl(float *v_out, const float *v_ref,
  276. const float *v_in)
  277. {
  278. int i, j, len;
  279. float scalefactor;
  280. for(i=0, j=0; i<4; i++)
  281. {
  282. scalefactor = ff_dot_productf(v_in + j, v_in + j, 40);
  283. if(scalefactor)
  284. scalefactor = sqrt(ff_dot_productf(v_ref + j, v_ref + j, 40)
  285. / scalefactor);
  286. else
  287. av_log_missing_feature(NULL, "Zero energy for gain control", 1);
  288. for(len=j+40; j<len; j++)
  289. v_out[j] = scalefactor * v_in[j];
  290. }
  291. }
  292. /**
  293. * Apply filter in pitch-subframe steps.
  294. *
  295. * @param memory buffer for the previous state of the filter
  296. * - must be able to contain 303 elements
  297. * - the 143 first elements are from the previous state
  298. * - the next 160 are for output
  299. * @param v_in input filter vector
  300. * @param gain per-subframe gain array, each element is between 0.0 and 2.0
  301. * @param lag per-subframe lag array, each element is
  302. * - between 16 and 143 if its corresponding pfrac is 0,
  303. * - between 16 and 139 otherwise
  304. * @param pfrac per-subframe boolean array, 1 if the lag is fractional, 0
  305. * otherwise
  306. *
  307. * @return filter output vector
  308. */
  309. static const float *do_pitchfilter(float memory[303], const float v_in[160],
  310. const float gain[4], const uint8_t *lag,
  311. const uint8_t pfrac[4])
  312. {
  313. int i, j;
  314. float *v_lag, *v_out;
  315. const float *v_len;
  316. v_out = memory + 143; // Output vector starts at memory[143].
  317. for(i=0; i<4; i++)
  318. {
  319. if(gain[i])
  320. {
  321. v_lag = memory + 143 + 40 * i - lag[i];
  322. for(v_len=v_in+40; v_in<v_len; v_in++)
  323. {
  324. if(pfrac[i]) // If it is a fractional lag...
  325. {
  326. for(j=0, *v_out=0.; j<4; j++)
  327. *v_out += qcelp_hammsinc_table[j] * (v_lag[j-4] + v_lag[3-j]);
  328. }else
  329. *v_out = *v_lag;
  330. *v_out = *v_in + gain[i] * *v_out;
  331. v_lag++;
  332. v_out++;
  333. }
  334. }else
  335. {
  336. memcpy(v_out, v_in, 40 * sizeof(float));
  337. v_in += 40;
  338. v_out += 40;
  339. }
  340. }
  341. memmove(memory, memory + 160, 143 * sizeof(float));
  342. return memory + 143;
  343. }
  344. /**
  345. * Interpolates LSP frequencies and computes LPC coefficients
  346. * for a given bitrate & pitch subframe.
  347. *
  348. * TIA/EIA/IS-733 2.4.3.3.4
  349. *
  350. * @param q the context
  351. * @param curr_lspf LSP frequencies vector of the current frame
  352. * @param lpc float vector for the resulting LPC
  353. * @param subframe_num frame number in decoded stream
  354. */
  355. void interpolate_lpc(QCELPContext *q, const float *curr_lspf, float *lpc,
  356. const int subframe_num)
  357. {
  358. float interpolated_lspf[10];
  359. float weight;
  360. if(q->bitrate >= RATE_QUARTER)
  361. weight = 0.25 * (subframe_num + 1);
  362. else if(q->bitrate == RATE_OCTAVE && !subframe_num)
  363. weight = 0.625;
  364. else
  365. weight = 1.0;
  366. if(weight != 1.0)
  367. {
  368. weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf,
  369. weight, 1.0 - weight, 10);
  370. qcelp_lspf2lpc(interpolated_lspf, lpc);
  371. }else if(q->bitrate >= RATE_QUARTER || (q->bitrate == I_F_Q && !subframe_num))
  372. qcelp_lspf2lpc(curr_lspf, lpc);
  373. }
  374. static int buf_size2bitrate(const int buf_size)
  375. {
  376. switch(buf_size)
  377. {
  378. case 35:
  379. return RATE_FULL;
  380. case 17:
  381. return RATE_HALF;
  382. case 8:
  383. return RATE_QUARTER;
  384. case 4:
  385. return RATE_OCTAVE;
  386. case 1:
  387. return SILENCE;
  388. }
  389. return -1;
  390. }
  391. static void warn_insufficient_frame_quality(AVCodecContext *avctx,
  392. const char *message)
  393. {
  394. av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n", avctx->frame_number,
  395. message);
  396. }
  397. AVCodec qcelp_decoder =
  398. {
  399. .name = "qcelp",
  400. .type = CODEC_TYPE_AUDIO,
  401. .id = CODEC_ID_QCELP,
  402. .init = qcelp_decode_init,
  403. .decode = qcelp_decode_frame,
  404. .priv_data_size = sizeof(QCELPContext),
  405. .long_name = NULL_IF_CONFIG_SMALL("QCELP / PureVoice"),
  406. };