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.

850 lines
25KB

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