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.

856 lines
26KB

  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 libavcodec/qcelpdec.c
  23. * QCELP decoder
  24. * @author Reynaldo H. Verdejo Pinochet
  25. * @remark FFmpeg merging spearheaded by Kenan Gillet
  26. * @remark Development mentored by Benjamin Larson
  27. */
  28. #include <stddef.h>
  29. #include "avcodec.h"
  30. #include "internal.h"
  31. #include "get_bits.h"
  32. #include "qcelpdata.h"
  33. #include "celp_math.h"
  34. #include "celp_filters.h"
  35. #include "acelp_vectors.h"
  36. #undef NDEBUG
  37. #include <assert.h>
  38. typedef enum
  39. {
  40. I_F_Q = -1, /*!< insufficient frame quality */
  41. SILENCE,
  42. RATE_OCTAVE,
  43. RATE_QUARTER,
  44. RATE_HALF,
  45. RATE_FULL
  46. } qcelp_packet_rate;
  47. typedef struct
  48. {
  49. GetBitContext gb;
  50. qcelp_packet_rate bitrate;
  51. QCELPFrame frame; /*!< unpacked data frame */
  52. uint8_t erasure_count;
  53. uint8_t octave_count; /*!< count the consecutive RATE_OCTAVE frames */
  54. float prev_lspf[10];
  55. float predictor_lspf[10];/*!< LSP predictor for RATE_OCTAVE and I_F_Q */
  56. float pitch_synthesis_filter_mem[303];
  57. float pitch_pre_filter_mem[303];
  58. float rnd_fir_filter_mem[180];
  59. float formant_mem[170];
  60. float last_codebook_gain;
  61. int prev_g1[2];
  62. int prev_bitrate;
  63. float pitch_gain[4];
  64. uint8_t pitch_lag[4];
  65. uint16_t first16bits;
  66. uint8_t warned_buf_mismatch_bitrate;
  67. } QCELPContext;
  68. /**
  69. * Reconstructs LPC coefficients from the line spectral pair frequencies.
  70. *
  71. * TIA/EIA/IS-733 2.4.3.3.5
  72. */
  73. void ff_celp_lspf2lpc(const double *lspf, float *lpc);
  74. /**
  75. * Initialize the speech codec according to the specification.
  76. *
  77. * TIA/EIA/IS-733 2.4.9
  78. */
  79. static av_cold int qcelp_decode_init(AVCodecContext *avctx)
  80. {
  81. QCELPContext *q = avctx->priv_data;
  82. int i;
  83. avctx->sample_fmt = SAMPLE_FMT_FLT;
  84. for(i=0; i<10; i++)
  85. q->prev_lspf[i] = (i+1)/11.;
  86. return 0;
  87. }
  88. /**
  89. * Decodes the 10 quantized LSP frequencies from the LSPV/LSP
  90. * transmission codes of any bitrate and checks for badly received packets.
  91. *
  92. * @param q the context
  93. * @param lspf line spectral pair frequencies
  94. *
  95. * @return 0 on success, -1 if the packet is badly received
  96. *
  97. * TIA/EIA/IS-733 2.4.3.2.6.2-2, 2.4.8.7.3
  98. */
  99. static int decode_lspf(QCELPContext *q, float *lspf)
  100. {
  101. int i;
  102. float tmp_lspf, smooth, erasure_coeff;
  103. const float *predictors;
  104. if(q->bitrate == RATE_OCTAVE || q->bitrate == I_F_Q)
  105. {
  106. predictors = (q->prev_bitrate != RATE_OCTAVE &&
  107. q->prev_bitrate != I_F_Q ?
  108. q->prev_lspf : q->predictor_lspf);
  109. if(q->bitrate == RATE_OCTAVE)
  110. {
  111. q->octave_count++;
  112. for(i=0; i<10; i++)
  113. {
  114. q->predictor_lspf[i] =
  115. lspf[i] = (q->frame.lspv[i] ? QCELP_LSP_SPREAD_FACTOR
  116. : -QCELP_LSP_SPREAD_FACTOR)
  117. + predictors[i] * QCELP_LSP_OCTAVE_PREDICTOR
  118. + (i + 1) * ((1 - QCELP_LSP_OCTAVE_PREDICTOR)/11);
  119. }
  120. smooth = (q->octave_count < 10 ? .875 : 0.1);
  121. }else
  122. {
  123. erasure_coeff = QCELP_LSP_OCTAVE_PREDICTOR;
  124. assert(q->bitrate == I_F_Q);
  125. if(q->erasure_count > 1)
  126. erasure_coeff *= (q->erasure_count < 4 ? 0.9 : 0.7);
  127. for(i=0; i<10; i++)
  128. {
  129. q->predictor_lspf[i] =
  130. lspf[i] = (i + 1) * ( 1 - erasure_coeff)/11
  131. + erasure_coeff * predictors[i];
  132. }
  133. smooth = 0.125;
  134. }
  135. // Check the stability of the LSP frequencies.
  136. lspf[0] = FFMAX(lspf[0], QCELP_LSP_SPREAD_FACTOR);
  137. for(i=1; i<10; i++)
  138. lspf[i] = FFMAX(lspf[i], (lspf[i-1] + QCELP_LSP_SPREAD_FACTOR));
  139. lspf[9] = FFMIN(lspf[9], (1.0 - QCELP_LSP_SPREAD_FACTOR));
  140. for(i=9; i>0; i--)
  141. lspf[i-1] = FFMIN(lspf[i-1], (lspf[i] - QCELP_LSP_SPREAD_FACTOR));
  142. // Low-pass filter the LSP frequencies.
  143. ff_weighted_vector_sumf(lspf, lspf, q->prev_lspf, smooth, 1.0-smooth, 10);
  144. }else
  145. {
  146. q->octave_count = 0;
  147. tmp_lspf = 0.;
  148. for(i=0; i<5 ; i++)
  149. {
  150. lspf[2*i+0] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][0] * 0.0001;
  151. lspf[2*i+1] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][1] * 0.0001;
  152. }
  153. // Check for badly received packets.
  154. if(q->bitrate == RATE_QUARTER)
  155. {
  156. if(lspf[9] <= .70 || lspf[9] >= .97)
  157. return -1;
  158. for(i=3; i<10; i++)
  159. if(fabs(lspf[i] - lspf[i-2]) < .08)
  160. return -1;
  161. }else
  162. {
  163. if(lspf[9] <= .66 || lspf[9] >= .985)
  164. return -1;
  165. for(i=4; i<10; i++)
  166. if (fabs(lspf[i] - lspf[i-4]) < .0931)
  167. return -1;
  168. }
  169. }
  170. return 0;
  171. }
  172. /**
  173. * Converts codebook transmission codes to GAIN and INDEX.
  174. *
  175. * @param q the context
  176. * @param gain array holding the decoded gain
  177. *
  178. * TIA/EIA/IS-733 2.4.6.2
  179. */
  180. static void decode_gain_and_index(QCELPContext *q,
  181. float *gain) {
  182. int i, subframes_count, g1[16];
  183. float slope;
  184. if(q->bitrate >= RATE_QUARTER)
  185. {
  186. switch(q->bitrate)
  187. {
  188. case RATE_FULL: subframes_count = 16; break;
  189. case RATE_HALF: subframes_count = 4; break;
  190. default: subframes_count = 5;
  191. }
  192. for(i=0; i<subframes_count; i++)
  193. {
  194. g1[i] = 4 * q->frame.cbgain[i];
  195. if(q->bitrate == RATE_FULL && !((i+1) & 3))
  196. {
  197. g1[i] += av_clip((g1[i-1] + g1[i-2] + g1[i-3]) / 3 - 6, 0, 32);
  198. }
  199. gain[i] = qcelp_g12ga[g1[i]];
  200. if(q->frame.cbsign[i])
  201. {
  202. gain[i] = -gain[i];
  203. q->frame.cindex[i] = (q->frame.cindex[i]-89) & 127;
  204. }
  205. }
  206. q->prev_g1[0] = g1[i-2];
  207. q->prev_g1[1] = g1[i-1];
  208. q->last_codebook_gain = qcelp_g12ga[g1[i-1]];
  209. if(q->bitrate == RATE_QUARTER)
  210. {
  211. // Provide smoothing of the unvoiced excitation energy.
  212. gain[7] = gain[4];
  213. gain[6] = 0.4*gain[3] + 0.6*gain[4];
  214. gain[5] = gain[3];
  215. gain[4] = 0.8*gain[2] + 0.2*gain[3];
  216. gain[3] = 0.2*gain[1] + 0.8*gain[2];
  217. gain[2] = gain[1];
  218. gain[1] = 0.6*gain[0] + 0.4*gain[1];
  219. }
  220. }else if (q->bitrate != SILENCE)
  221. {
  222. if(q->bitrate == RATE_OCTAVE)
  223. {
  224. g1[0] = 2 * q->frame.cbgain[0]
  225. + av_clip((q->prev_g1[0] + q->prev_g1[1]) / 2 - 5, 0, 54);
  226. subframes_count = 8;
  227. }else
  228. {
  229. assert(q->bitrate == I_F_Q);
  230. g1[0] = q->prev_g1[1];
  231. switch(q->erasure_count)
  232. {
  233. case 1 : break;
  234. case 2 : g1[0] -= 1; break;
  235. case 3 : g1[0] -= 2; break;
  236. default: g1[0] -= 6;
  237. }
  238. if(g1[0] < 0)
  239. g1[0] = 0;
  240. subframes_count = 4;
  241. }
  242. // This interpolation is done to produce smoother background noise.
  243. slope = 0.5*(qcelp_g12ga[g1[0]] - q->last_codebook_gain) / subframes_count;
  244. for(i=1; i<=subframes_count; i++)
  245. gain[i-1] = q->last_codebook_gain + slope * i;
  246. q->last_codebook_gain = gain[i-2];
  247. q->prev_g1[0] = q->prev_g1[1];
  248. q->prev_g1[1] = g1[0];
  249. }
  250. }
  251. /**
  252. * If the received packet is Rate 1/4 a further sanity check is made of the
  253. * codebook gain.
  254. *
  255. * @param cbgain the unpacked cbgain array
  256. * @return -1 if the sanity check fails, 0 otherwise
  257. *
  258. * TIA/EIA/IS-733 2.4.8.7.3
  259. */
  260. static int codebook_sanity_check_for_rate_quarter(const uint8_t *cbgain)
  261. {
  262. int i, diff, prev_diff=0;
  263. for(i=1; i<5; i++)
  264. {
  265. diff = cbgain[i] - cbgain[i-1];
  266. if(FFABS(diff) > 10)
  267. return -1;
  268. else if(FFABS(diff - prev_diff) > 12)
  269. return -1;
  270. prev_diff = diff;
  271. }
  272. return 0;
  273. }
  274. /**
  275. * Computes the scaled codebook vector Cdn From INDEX and GAIN
  276. * for all rates.
  277. *
  278. * The specification lacks some information here.
  279. *
  280. * TIA/EIA/IS-733 has an omission on the codebook index determination
  281. * formula for RATE_FULL and RATE_HALF frames at section 2.4.8.1.1. It says
  282. * you have to subtract the decoded index parameter from the given scaled
  283. * codebook vector index 'n' to get the desired circular codebook index, but
  284. * it does not mention that you have to clamp 'n' to [0-9] in order to get
  285. * RI-compliant results.
  286. *
  287. * The reason for this mistake seems to be the fact they forgot to mention you
  288. * have to do these calculations per codebook subframe and adjust given
  289. * equation values accordingly.
  290. *
  291. * @param q the context
  292. * @param gain array holding the 4 pitch subframe gain values
  293. * @param cdn_vector array for the generated scaled codebook vector
  294. */
  295. static void compute_svector(QCELPContext *q, const float *gain,
  296. float *cdn_vector)
  297. {
  298. int i, j, k;
  299. uint16_t cbseed, cindex;
  300. float *rnd, tmp_gain, fir_filter_value;
  301. switch(q->bitrate)
  302. {
  303. case RATE_FULL:
  304. for(i=0; i<16; i++)
  305. {
  306. tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
  307. cindex = -q->frame.cindex[i];
  308. for(j=0; j<10; j++)
  309. *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cindex++ & 127];
  310. }
  311. break;
  312. case RATE_HALF:
  313. for(i=0; i<4; i++)
  314. {
  315. tmp_gain = gain[i] * QCELP_RATE_HALF_CODEBOOK_RATIO;
  316. cindex = -q->frame.cindex[i];
  317. for (j = 0; j < 40; j++)
  318. *cdn_vector++ = tmp_gain * qcelp_rate_half_codebook[cindex++ & 127];
  319. }
  320. break;
  321. case RATE_QUARTER:
  322. cbseed = (0x0003 & q->frame.lspv[4])<<14 |
  323. (0x003F & q->frame.lspv[3])<< 8 |
  324. (0x0060 & q->frame.lspv[2])<< 1 |
  325. (0x0007 & q->frame.lspv[1])<< 3 |
  326. (0x0038 & q->frame.lspv[0])>> 3 ;
  327. rnd = q->rnd_fir_filter_mem + 20;
  328. for(i=0; i<8; i++)
  329. {
  330. tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
  331. for(k=0; k<20; k++)
  332. {
  333. cbseed = 521 * cbseed + 259;
  334. *rnd = (int16_t)cbseed;
  335. // FIR filter
  336. fir_filter_value = 0.0;
  337. for(j=0; j<10; j++)
  338. fir_filter_value += qcelp_rnd_fir_coefs[j ]
  339. * (rnd[-j ] + rnd[-20+j]);
  340. fir_filter_value += qcelp_rnd_fir_coefs[10] * rnd[-10];
  341. *cdn_vector++ = tmp_gain * fir_filter_value;
  342. rnd++;
  343. }
  344. }
  345. memcpy(q->rnd_fir_filter_mem, q->rnd_fir_filter_mem + 160, 20 * sizeof(float));
  346. break;
  347. case RATE_OCTAVE:
  348. cbseed = q->first16bits;
  349. for(i=0; i<8; i++)
  350. {
  351. tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
  352. for(j=0; j<20; j++)
  353. {
  354. cbseed = 521 * cbseed + 259;
  355. *cdn_vector++ = tmp_gain * (int16_t)cbseed;
  356. }
  357. }
  358. break;
  359. case I_F_Q:
  360. cbseed = -44; // random codebook index
  361. for(i=0; i<4; i++)
  362. {
  363. tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
  364. for(j=0; j<40; j++)
  365. *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cbseed++ & 127];
  366. }
  367. break;
  368. case SILENCE:
  369. memset(cdn_vector, 0, 160 * sizeof(float));
  370. break;
  371. }
  372. }
  373. /**
  374. * Compute the gain control
  375. *
  376. * @param v_in gain-controlled vector
  377. * @param v_ref vector to control gain of
  378. *
  379. * @return gain control
  380. *
  381. * FIXME: If v_ref is a zero vector, it energy is zero
  382. * and the behavior of the gain control is
  383. * undefined in the specs.
  384. *
  385. * TIA/EIA/IS-733 2.4.8.3-2/3/4/5, 2.4.8.6
  386. */
  387. static float compute_gain_ctrl(const float *v_ref, const float *v_in, const int len)
  388. {
  389. float scalefactor = ff_dot_productf(v_in, v_in, len);
  390. if(scalefactor)
  391. scalefactor = sqrt(ff_dot_productf(v_ref, v_ref, len) / scalefactor);
  392. else
  393. ff_log_missing_feature(NULL, "Zero energy for gain control", 1);
  394. return scalefactor;
  395. }
  396. /**
  397. * Apply generic gain control.
  398. *
  399. * @param v_out output vector
  400. * @param v_in gain-controlled vector
  401. * @param v_ref vector to control gain of
  402. *
  403. * TIA/EIA/IS-733 2.4.8.3, 2.4.8.6
  404. */
  405. static void apply_gain_ctrl(float *v_out, const float *v_ref,
  406. const float *v_in)
  407. {
  408. int i, j, len;
  409. float scalefactor;
  410. for(i=0, j=0; i<4; i++)
  411. {
  412. scalefactor = compute_gain_ctrl(v_ref + j, v_in + j, 40);
  413. for(len=j+40; j<len; j++)
  414. v_out[j] = scalefactor * v_in[j];
  415. }
  416. }
  417. /**
  418. * Apply filter in pitch-subframe steps.
  419. *
  420. * @param memory buffer for the previous state of the filter
  421. * - must be able to contain 303 elements
  422. * - the 143 first elements are from the previous state
  423. * - the next 160 are for output
  424. * @param v_in input filter vector
  425. * @param gain per-subframe gain array, each element is between 0.0 and 2.0
  426. * @param lag per-subframe lag array, each element is
  427. * - between 16 and 143 if its corresponding pfrac is 0,
  428. * - between 16 and 139 otherwise
  429. * @param pfrac per-subframe boolean array, 1 if the lag is fractional, 0
  430. * otherwise
  431. *
  432. * @return filter output vector
  433. */
  434. static const float *do_pitchfilter(float memory[303], const float v_in[160],
  435. const float gain[4], const uint8_t *lag,
  436. const uint8_t pfrac[4])
  437. {
  438. int i, j;
  439. float *v_lag, *v_out;
  440. const float *v_len;
  441. v_out = memory + 143; // Output vector starts at memory[143].
  442. for(i=0; i<4; i++)
  443. {
  444. if(gain[i])
  445. {
  446. v_lag = memory + 143 + 40 * i - lag[i];
  447. for(v_len=v_in+40; v_in<v_len; v_in++)
  448. {
  449. if(pfrac[i]) // If it is a fractional lag...
  450. {
  451. for(j=0, *v_out=0.; j<4; j++)
  452. *v_out += qcelp_hammsinc_table[j] * (v_lag[j-4] + v_lag[3-j]);
  453. }else
  454. *v_out = *v_lag;
  455. *v_out = *v_in + gain[i] * *v_out;
  456. v_lag++;
  457. v_out++;
  458. }
  459. }else
  460. {
  461. memcpy(v_out, v_in, 40 * sizeof(float));
  462. v_in += 40;
  463. v_out += 40;
  464. }
  465. }
  466. memmove(memory, memory + 160, 143 * sizeof(float));
  467. return memory + 143;
  468. }
  469. /**
  470. * Apply pitch synthesis filter and pitch prefilter to the scaled codebook vector.
  471. * TIA/EIA/IS-733 2.4.5.2, 2.4.8.7.2
  472. *
  473. * @param q the context
  474. * @param cdn_vector the scaled codebook vector
  475. */
  476. static void apply_pitch_filters(QCELPContext *q, float *cdn_vector)
  477. {
  478. int i;
  479. const float *v_synthesis_filtered, *v_pre_filtered;
  480. if(q->bitrate >= RATE_HALF ||
  481. q->bitrate == SILENCE ||
  482. (q->bitrate == I_F_Q && (q->prev_bitrate >= RATE_HALF)))
  483. {
  484. if(q->bitrate >= RATE_HALF)
  485. {
  486. // Compute gain & lag for the whole frame.
  487. for(i=0; i<4; i++)
  488. {
  489. q->pitch_gain[i] = q->frame.plag[i] ? (q->frame.pgain[i] + 1) * 0.25 : 0.0;
  490. q->pitch_lag[i] = q->frame.plag[i] + 16;
  491. }
  492. }else
  493. {
  494. float max_pitch_gain;
  495. if (q->bitrate == I_F_Q)
  496. {
  497. if (q->erasure_count < 3)
  498. max_pitch_gain = 0.9 - 0.3 * (q->erasure_count - 1);
  499. else
  500. max_pitch_gain = 0.0;
  501. }else
  502. {
  503. assert(q->bitrate == SILENCE);
  504. max_pitch_gain = 1.0;
  505. }
  506. for(i=0; i<4; i++)
  507. q->pitch_gain[i] = FFMIN(q->pitch_gain[i], max_pitch_gain);
  508. memset(q->frame.pfrac, 0, sizeof(q->frame.pfrac));
  509. }
  510. // pitch synthesis filter
  511. v_synthesis_filtered = do_pitchfilter(q->pitch_synthesis_filter_mem,
  512. cdn_vector, q->pitch_gain,
  513. q->pitch_lag, q->frame.pfrac);
  514. // pitch prefilter update
  515. for(i=0; i<4; i++)
  516. q->pitch_gain[i] = 0.5 * FFMIN(q->pitch_gain[i], 1.0);
  517. v_pre_filtered = do_pitchfilter(q->pitch_pre_filter_mem,
  518. v_synthesis_filtered,
  519. q->pitch_gain, q->pitch_lag,
  520. q->frame.pfrac);
  521. apply_gain_ctrl(cdn_vector, v_synthesis_filtered, v_pre_filtered);
  522. }else
  523. {
  524. memcpy(q->pitch_synthesis_filter_mem, cdn_vector + 17,
  525. 143 * sizeof(float));
  526. memcpy(q->pitch_pre_filter_mem, cdn_vector + 17, 143 * sizeof(float));
  527. memset(q->pitch_gain, 0, sizeof(q->pitch_gain));
  528. memset(q->pitch_lag, 0, sizeof(q->pitch_lag));
  529. }
  530. }
  531. /**
  532. * Reconstructs LPC coefficients from the line spectral pair frequencies
  533. * and performs bandwidth expansion.
  534. *
  535. * @param lspf line spectral pair frequencies
  536. * @param lpc linear predictive coding coefficients
  537. *
  538. * @note: bandwith_expansion_coeff could be precalculated into a table
  539. * but it seems to be slower on x86
  540. *
  541. * TIA/EIA/IS-733 2.4.3.3.5
  542. */
  543. static void lspf2lpc(const float *lspf, float *lpc)
  544. {
  545. double lsf[10];
  546. double bandwith_expansion_coeff = QCELP_BANDWITH_EXPANSION_COEFF;
  547. int i;
  548. for (i=0; i<10; i++)
  549. lsf[i] = cos(M_PI * lspf[i]);
  550. ff_celp_lspf2lpc(lsf, lpc);
  551. for (i=0; i<10; i++)
  552. {
  553. lpc[i] *= bandwith_expansion_coeff;
  554. bandwith_expansion_coeff *= QCELP_BANDWITH_EXPANSION_COEFF;
  555. }
  556. }
  557. /**
  558. * Interpolates LSP frequencies and computes LPC coefficients
  559. * for a given bitrate & pitch subframe.
  560. *
  561. * TIA/EIA/IS-733 2.4.3.3.4, 2.4.8.7.2
  562. *
  563. * @param q the context
  564. * @param curr_lspf LSP frequencies vector of the current frame
  565. * @param lpc float vector for the resulting LPC
  566. * @param subframe_num frame number in decoded stream
  567. */
  568. void interpolate_lpc(QCELPContext *q, const float *curr_lspf, float *lpc,
  569. const int subframe_num)
  570. {
  571. float interpolated_lspf[10];
  572. float weight;
  573. if(q->bitrate >= RATE_QUARTER)
  574. weight = 0.25 * (subframe_num + 1);
  575. else if(q->bitrate == RATE_OCTAVE && !subframe_num)
  576. weight = 0.625;
  577. else
  578. weight = 1.0;
  579. if(weight != 1.0)
  580. {
  581. ff_weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf,
  582. weight, 1.0 - weight, 10);
  583. lspf2lpc(interpolated_lspf, lpc);
  584. }else if(q->bitrate >= RATE_QUARTER ||
  585. (q->bitrate == I_F_Q && !subframe_num))
  586. lspf2lpc(curr_lspf, lpc);
  587. else if(q->bitrate == SILENCE && !subframe_num)
  588. lspf2lpc(q->prev_lspf, lpc);
  589. }
  590. static qcelp_packet_rate buf_size2bitrate(const int buf_size)
  591. {
  592. switch(buf_size)
  593. {
  594. case 35: return RATE_FULL;
  595. case 17: return RATE_HALF;
  596. case 8: return RATE_QUARTER;
  597. case 4: return RATE_OCTAVE;
  598. case 1: return SILENCE;
  599. }
  600. return I_F_Q;
  601. }
  602. /**
  603. * Determine the bitrate from the frame size and/or the first byte of the frame.
  604. *
  605. * @param avctx the AV codec context
  606. * @param buf_size length of the buffer
  607. * @param buf the bufffer
  608. *
  609. * @return the bitrate on success,
  610. * I_F_Q if the bitrate cannot be satisfactorily determined
  611. *
  612. * TIA/EIA/IS-733 2.4.8.7.1
  613. */
  614. static qcelp_packet_rate determine_bitrate(AVCodecContext *avctx, const int buf_size,
  615. const uint8_t **buf)
  616. {
  617. qcelp_packet_rate bitrate;
  618. if((bitrate = buf_size2bitrate(buf_size)) >= 0)
  619. {
  620. if(bitrate > **buf)
  621. {
  622. QCELPContext *q = avctx->priv_data;
  623. if (!q->warned_buf_mismatch_bitrate)
  624. {
  625. av_log(avctx, AV_LOG_WARNING,
  626. "Claimed bitrate and buffer size mismatch.\n");
  627. q->warned_buf_mismatch_bitrate = 1;
  628. }
  629. bitrate = **buf;
  630. }else if(bitrate < **buf)
  631. {
  632. av_log(avctx, AV_LOG_ERROR,
  633. "Buffer is too small for the claimed bitrate.\n");
  634. return I_F_Q;
  635. }
  636. (*buf)++;
  637. }else if((bitrate = buf_size2bitrate(buf_size + 1)) >= 0)
  638. {
  639. av_log(avctx, AV_LOG_WARNING,
  640. "Bitrate byte is missing, guessing the bitrate from packet size.\n");
  641. }else
  642. return I_F_Q;
  643. if(bitrate == SILENCE)
  644. {
  645. //FIXME: Remove experimental warning when tested with samples.
  646. ff_log_ask_for_sample(avctx, "'Blank frame handling is experimental.");
  647. }
  648. return bitrate;
  649. }
  650. static void warn_insufficient_frame_quality(AVCodecContext *avctx,
  651. const char *message)
  652. {
  653. av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n", avctx->frame_number,
  654. message);
  655. }
  656. static int qcelp_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  657. AVPacket *avpkt)
  658. {
  659. const uint8_t *buf = avpkt->data;
  660. int buf_size = avpkt->size;
  661. QCELPContext *q = avctx->priv_data;
  662. float *outbuffer = data;
  663. int i;
  664. float quantized_lspf[10], lpc[10];
  665. float gain[16];
  666. float *formant_mem;
  667. if((q->bitrate = determine_bitrate(avctx, buf_size, &buf)) == I_F_Q)
  668. {
  669. warn_insufficient_frame_quality(avctx, "bitrate cannot be determined.");
  670. goto erasure;
  671. }
  672. if(q->bitrate == RATE_OCTAVE &&
  673. (q->first16bits = AV_RB16(buf)) == 0xFFFF)
  674. {
  675. warn_insufficient_frame_quality(avctx, "Bitrate is 1/8 and first 16 bits are on.");
  676. goto erasure;
  677. }
  678. if(q->bitrate > SILENCE)
  679. {
  680. const QCELPBitmap *bitmaps = qcelp_unpacking_bitmaps_per_rate[q->bitrate];
  681. const QCELPBitmap *bitmaps_end = qcelp_unpacking_bitmaps_per_rate[q->bitrate]
  682. + qcelp_unpacking_bitmaps_lengths[q->bitrate];
  683. uint8_t *unpacked_data = (uint8_t *)&q->frame;
  684. init_get_bits(&q->gb, buf, 8*buf_size);
  685. memset(&q->frame, 0, sizeof(QCELPFrame));
  686. for(; bitmaps < bitmaps_end; bitmaps++)
  687. unpacked_data[bitmaps->index] |= get_bits(&q->gb, bitmaps->bitlen) << bitmaps->bitpos;
  688. // Check for erasures/blanks on rates 1, 1/4 and 1/8.
  689. if(q->frame.reserved)
  690. {
  691. warn_insufficient_frame_quality(avctx, "Wrong data in reserved frame area.");
  692. goto erasure;
  693. }
  694. if(q->bitrate == RATE_QUARTER &&
  695. codebook_sanity_check_for_rate_quarter(q->frame.cbgain))
  696. {
  697. warn_insufficient_frame_quality(avctx, "Codebook gain sanity check failed.");
  698. goto erasure;
  699. }
  700. if(q->bitrate >= RATE_HALF)
  701. {
  702. for(i=0; i<4; i++)
  703. {
  704. if(q->frame.pfrac[i] && q->frame.plag[i] >= 124)
  705. {
  706. warn_insufficient_frame_quality(avctx, "Cannot initialize pitch filter.");
  707. goto erasure;
  708. }
  709. }
  710. }
  711. }
  712. decode_gain_and_index(q, gain);
  713. compute_svector(q, gain, outbuffer);
  714. if(decode_lspf(q, quantized_lspf) < 0)
  715. {
  716. warn_insufficient_frame_quality(avctx, "Badly received packets in frame.");
  717. goto erasure;
  718. }
  719. apply_pitch_filters(q, outbuffer);
  720. if(q->bitrate == I_F_Q)
  721. {
  722. erasure:
  723. q->bitrate = I_F_Q;
  724. q->erasure_count++;
  725. decode_gain_and_index(q, gain);
  726. compute_svector(q, gain, outbuffer);
  727. decode_lspf(q, quantized_lspf);
  728. apply_pitch_filters(q, outbuffer);
  729. }else
  730. q->erasure_count = 0;
  731. formant_mem = q->formant_mem + 10;
  732. for(i=0; i<4; i++)
  733. {
  734. interpolate_lpc(q, quantized_lspf, lpc, i);
  735. ff_celp_lp_synthesis_filterf(formant_mem, lpc, outbuffer + i * 40, 40,
  736. 10);
  737. formant_mem += 40;
  738. }
  739. memcpy(q->formant_mem, q->formant_mem + 160, 10 * sizeof(float));
  740. // FIXME: postfilter and final gain control should be here.
  741. // TIA/EIA/IS-733 2.4.8.6
  742. formant_mem = q->formant_mem + 10;
  743. for(i=0; i<160; i++)
  744. *outbuffer++ = av_clipf(*formant_mem++, QCELP_CLIP_LOWER_BOUND,
  745. QCELP_CLIP_UPPER_BOUND);
  746. memcpy(q->prev_lspf, quantized_lspf, sizeof(q->prev_lspf));
  747. q->prev_bitrate = q->bitrate;
  748. *data_size = 160 * sizeof(*outbuffer);
  749. return *data_size;
  750. }
  751. AVCodec qcelp_decoder =
  752. {
  753. .name = "qcelp",
  754. .type = CODEC_TYPE_AUDIO,
  755. .id = CODEC_ID_QCELP,
  756. .init = qcelp_decode_init,
  757. .decode = qcelp_decode_frame,
  758. .priv_data_size = sizeof(QCELPContext),
  759. .long_name = NULL_IF_CONFIG_SMALL("QCELP / PureVoice"),
  760. };