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.

838 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 "bitstream.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
  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. * Apply generic gain control.
  375. *
  376. * @param v_out output vector
  377. * @param v_in gain-controlled vector
  378. * @param v_ref vector to control gain of
  379. *
  380. * FIXME: If v_ref is a zero vector, it energy is zero
  381. * and the behavior of the gain control is
  382. * undefined in the specs.
  383. *
  384. * TIA/EIA/IS-733 2.4.8.3-2/3/4/5, 2.4.8.6
  385. */
  386. static void apply_gain_ctrl(float *v_out, const float *v_ref,
  387. const float *v_in)
  388. {
  389. int i, j, len;
  390. float scalefactor;
  391. for(i=0, j=0; i<4; i++)
  392. {
  393. scalefactor = ff_dot_productf(v_in + j, v_in + j, 40);
  394. if(scalefactor)
  395. scalefactor = sqrt(ff_dot_productf(v_ref + j, v_ref + j, 40)
  396. / scalefactor);
  397. else
  398. ff_log_missing_feature(NULL, "Zero energy for gain control", 1);
  399. for(len=j+40; j<len; j++)
  400. v_out[j] = scalefactor * v_in[j];
  401. }
  402. }
  403. /**
  404. * Apply filter in pitch-subframe steps.
  405. *
  406. * @param memory buffer for the previous state of the filter
  407. * - must be able to contain 303 elements
  408. * - the 143 first elements are from the previous state
  409. * - the next 160 are for output
  410. * @param v_in input filter vector
  411. * @param gain per-subframe gain array, each element is between 0.0 and 2.0
  412. * @param lag per-subframe lag array, each element is
  413. * - between 16 and 143 if its corresponding pfrac is 0,
  414. * - between 16 and 139 otherwise
  415. * @param pfrac per-subframe boolean array, 1 if the lag is fractional, 0
  416. * otherwise
  417. *
  418. * @return filter output vector
  419. */
  420. static const float *do_pitchfilter(float memory[303], const float v_in[160],
  421. const float gain[4], const uint8_t *lag,
  422. const uint8_t pfrac[4])
  423. {
  424. int i, j;
  425. float *v_lag, *v_out;
  426. const float *v_len;
  427. v_out = memory + 143; // Output vector starts at memory[143].
  428. for(i=0; i<4; i++)
  429. {
  430. if(gain[i])
  431. {
  432. v_lag = memory + 143 + 40 * i - lag[i];
  433. for(v_len=v_in+40; v_in<v_len; v_in++)
  434. {
  435. if(pfrac[i]) // If it is a fractional lag...
  436. {
  437. for(j=0, *v_out=0.; j<4; j++)
  438. *v_out += qcelp_hammsinc_table[j] * (v_lag[j-4] + v_lag[3-j]);
  439. }else
  440. *v_out = *v_lag;
  441. *v_out = *v_in + gain[i] * *v_out;
  442. v_lag++;
  443. v_out++;
  444. }
  445. }else
  446. {
  447. memcpy(v_out, v_in, 40 * sizeof(float));
  448. v_in += 40;
  449. v_out += 40;
  450. }
  451. }
  452. memmove(memory, memory + 160, 143 * sizeof(float));
  453. return memory + 143;
  454. }
  455. /**
  456. * Apply pitch synthesis filter and pitch prefilter to the scaled codebook vector.
  457. * TIA/EIA/IS-733 2.4.5.2, 2.4.8.7.2
  458. *
  459. * @param q the context
  460. * @param cdn_vector the scaled codebook vector
  461. */
  462. static void apply_pitch_filters(QCELPContext *q, float *cdn_vector)
  463. {
  464. int i;
  465. const float *v_synthesis_filtered, *v_pre_filtered;
  466. if(q->bitrate >= RATE_HALF ||
  467. q->bitrate == SILENCE ||
  468. (q->bitrate == I_F_Q && (q->prev_bitrate >= RATE_HALF)))
  469. {
  470. if(q->bitrate >= RATE_HALF)
  471. {
  472. // Compute gain & lag for the whole frame.
  473. for(i=0; i<4; i++)
  474. {
  475. q->pitch_gain[i] = q->frame.plag[i] ? (q->frame.pgain[i] + 1) * 0.25 : 0.0;
  476. q->pitch_lag[i] = q->frame.plag[i] + 16;
  477. }
  478. }else
  479. {
  480. float max_pitch_gain;
  481. if (q->bitrate == I_F_Q)
  482. {
  483. if (q->erasure_count < 3)
  484. max_pitch_gain = 0.9 - 0.3 * (q->erasure_count - 1);
  485. else
  486. max_pitch_gain = 0.0;
  487. }else
  488. {
  489. assert(q->bitrate == SILENCE);
  490. max_pitch_gain = 1.0;
  491. }
  492. for(i=0; i<4; i++)
  493. q->pitch_gain[i] = FFMIN(q->pitch_gain[i], max_pitch_gain);
  494. memset(q->frame.pfrac, 0, sizeof(q->frame.pfrac));
  495. }
  496. // pitch synthesis filter
  497. v_synthesis_filtered = do_pitchfilter(q->pitch_synthesis_filter_mem,
  498. cdn_vector, q->pitch_gain,
  499. q->pitch_lag, q->frame.pfrac);
  500. // pitch prefilter update
  501. for(i=0; i<4; i++)
  502. q->pitch_gain[i] = 0.5 * FFMIN(q->pitch_gain[i], 1.0);
  503. v_pre_filtered = do_pitchfilter(q->pitch_pre_filter_mem,
  504. v_synthesis_filtered,
  505. q->pitch_gain, q->pitch_lag,
  506. q->frame.pfrac);
  507. apply_gain_ctrl(cdn_vector, v_synthesis_filtered, v_pre_filtered);
  508. }else
  509. {
  510. memcpy(q->pitch_synthesis_filter_mem, cdn_vector + 17,
  511. 143 * sizeof(float));
  512. memcpy(q->pitch_pre_filter_mem, cdn_vector + 17, 143 * sizeof(float));
  513. memset(q->pitch_gain, 0, sizeof(q->pitch_gain));
  514. memset(q->pitch_lag, 0, sizeof(q->pitch_lag));
  515. }
  516. }
  517. /**
  518. * Reconstructs LPC coefficients from the line spectral pair frequencies
  519. * and performs bandwidth expansion.
  520. *
  521. * @param lspf line spectral pair frequencies
  522. * @param lpc linear predictive coding coefficients
  523. *
  524. * @note: bandwith_expansion_coeff could be precalculated into a table
  525. * but it seems to be slower on x86
  526. *
  527. * TIA/EIA/IS-733 2.4.3.3.5
  528. */
  529. void lspf2lpc(const float *lspf, float *lpc)
  530. {
  531. double lsf[10];
  532. double bandwith_expansion_coeff = QCELP_BANDWITH_EXPANSION_COEFF;
  533. int i;
  534. for (i=0; i<10; i++)
  535. lsf[i] = cos(M_PI * lspf[i]);
  536. ff_celp_lspf2lpc(lsf, lpc);
  537. for (i=0; i<10; i++)
  538. {
  539. lpc[i] *= bandwith_expansion_coeff;
  540. bandwith_expansion_coeff *= QCELP_BANDWITH_EXPANSION_COEFF;
  541. }
  542. }
  543. /**
  544. * Interpolates LSP frequencies and computes LPC coefficients
  545. * for a given bitrate & pitch subframe.
  546. *
  547. * TIA/EIA/IS-733 2.4.3.3.4, 2.4.8.7.2
  548. *
  549. * @param q the context
  550. * @param curr_lspf LSP frequencies vector of the current frame
  551. * @param lpc float vector for the resulting LPC
  552. * @param subframe_num frame number in decoded stream
  553. */
  554. void interpolate_lpc(QCELPContext *q, const float *curr_lspf, float *lpc,
  555. const int subframe_num)
  556. {
  557. float interpolated_lspf[10];
  558. float weight;
  559. if(q->bitrate >= RATE_QUARTER)
  560. weight = 0.25 * (subframe_num + 1);
  561. else if(q->bitrate == RATE_OCTAVE && !subframe_num)
  562. weight = 0.625;
  563. else
  564. weight = 1.0;
  565. if(weight != 1.0)
  566. {
  567. ff_weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf,
  568. weight, 1.0 - weight, 10);
  569. lspf2lpc(interpolated_lspf, lpc);
  570. }else if(q->bitrate >= RATE_QUARTER ||
  571. (q->bitrate == I_F_Q && !subframe_num))
  572. lspf2lpc(curr_lspf, lpc);
  573. else if(q->bitrate == SILENCE && !subframe_num)
  574. lspf2lpc(q->prev_lspf, lpc);
  575. }
  576. static qcelp_packet_rate buf_size2bitrate(const int buf_size)
  577. {
  578. switch(buf_size)
  579. {
  580. case 35: return RATE_FULL;
  581. case 17: return RATE_HALF;
  582. case 8: return RATE_QUARTER;
  583. case 4: return RATE_OCTAVE;
  584. case 1: return SILENCE;
  585. }
  586. return I_F_Q;
  587. }
  588. /**
  589. * Determine the bitrate from the frame size and/or the first byte of the frame.
  590. *
  591. * @param avctx the AV codec context
  592. * @param buf_size length of the buffer
  593. * @param buf the bufffer
  594. *
  595. * @return the bitrate on success,
  596. * I_F_Q if the bitrate cannot be satisfactorily determined
  597. *
  598. * TIA/EIA/IS-733 2.4.8.7.1
  599. */
  600. static qcelp_packet_rate determine_bitrate(AVCodecContext *avctx, const int buf_size,
  601. const uint8_t **buf)
  602. {
  603. qcelp_packet_rate bitrate;
  604. if((bitrate = buf_size2bitrate(buf_size)) >= 0)
  605. {
  606. if(bitrate > **buf)
  607. {
  608. QCELPContext *q = avctx->priv_data;
  609. if (!q->warned_buf_mismatch_bitrate)
  610. {
  611. av_log(avctx, AV_LOG_WARNING,
  612. "Claimed bitrate and buffer size mismatch.\n");
  613. q->warned_buf_mismatch_bitrate = 1;
  614. }
  615. bitrate = **buf;
  616. }else if(bitrate < **buf)
  617. {
  618. av_log(avctx, AV_LOG_ERROR,
  619. "Buffer is too small for the claimed bitrate.\n");
  620. return I_F_Q;
  621. }
  622. (*buf)++;
  623. }else if((bitrate = buf_size2bitrate(buf_size + 1)) >= 0)
  624. {
  625. av_log(avctx, AV_LOG_WARNING,
  626. "Bitrate byte is missing, guessing the bitrate from packet size.\n");
  627. }else
  628. return I_F_Q;
  629. if(bitrate == SILENCE)
  630. {
  631. //FIXME: Remove experimental warning when tested with samples.
  632. ff_log_ask_for_sample(avctx, "'Blank frame handling is experimental.");
  633. }
  634. return bitrate;
  635. }
  636. static void warn_insufficient_frame_quality(AVCodecContext *avctx,
  637. const char *message)
  638. {
  639. av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n", avctx->frame_number,
  640. message);
  641. }
  642. static int qcelp_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  643. const uint8_t *buf, int buf_size)
  644. {
  645. QCELPContext *q = avctx->priv_data;
  646. float *outbuffer = data;
  647. int i;
  648. float quantized_lspf[10], lpc[10];
  649. float gain[16];
  650. float *formant_mem;
  651. if((q->bitrate = determine_bitrate(avctx, buf_size, &buf)) == I_F_Q)
  652. {
  653. warn_insufficient_frame_quality(avctx, "bitrate cannot be determined.");
  654. goto erasure;
  655. }
  656. if(q->bitrate == RATE_OCTAVE &&
  657. (q->first16bits = AV_RB16(buf)) == 0xFFFF)
  658. {
  659. warn_insufficient_frame_quality(avctx, "Bitrate is 1/8 and first 16 bits are on.");
  660. goto erasure;
  661. }
  662. if(q->bitrate > SILENCE)
  663. {
  664. const QCELPBitmap *bitmaps = qcelp_unpacking_bitmaps_per_rate[q->bitrate];
  665. const QCELPBitmap *bitmaps_end = qcelp_unpacking_bitmaps_per_rate[q->bitrate]
  666. + qcelp_unpacking_bitmaps_lengths[q->bitrate];
  667. uint8_t *unpacked_data = (uint8_t *)&q->frame;
  668. init_get_bits(&q->gb, buf, 8*buf_size);
  669. memset(&q->frame, 0, sizeof(QCELPFrame));
  670. for(; bitmaps < bitmaps_end; bitmaps++)
  671. unpacked_data[bitmaps->index] |= get_bits(&q->gb, bitmaps->bitlen) << bitmaps->bitpos;
  672. // Check for erasures/blanks on rates 1, 1/4 and 1/8.
  673. if(q->frame.reserved)
  674. {
  675. warn_insufficient_frame_quality(avctx, "Wrong data in reserved frame area.");
  676. goto erasure;
  677. }
  678. if(q->bitrate == RATE_QUARTER &&
  679. codebook_sanity_check_for_rate_quarter(q->frame.cbgain))
  680. {
  681. warn_insufficient_frame_quality(avctx, "Codebook gain sanity check failed.");
  682. goto erasure;
  683. }
  684. if(q->bitrate >= RATE_HALF)
  685. {
  686. for(i=0; i<4; i++)
  687. {
  688. if(q->frame.pfrac[i] && q->frame.plag[i] >= 124)
  689. {
  690. warn_insufficient_frame_quality(avctx, "Cannot initialize pitch filter.");
  691. goto erasure;
  692. }
  693. }
  694. }
  695. }
  696. decode_gain_and_index(q, gain);
  697. compute_svector(q, gain, outbuffer);
  698. if(decode_lspf(q, quantized_lspf) < 0)
  699. {
  700. warn_insufficient_frame_quality(avctx, "Badly received packets in frame.");
  701. goto erasure;
  702. }
  703. apply_pitch_filters(q, outbuffer);
  704. if(q->bitrate == I_F_Q)
  705. {
  706. erasure:
  707. q->bitrate = I_F_Q;
  708. q->erasure_count++;
  709. decode_gain_and_index(q, gain);
  710. compute_svector(q, gain, outbuffer);
  711. decode_lspf(q, quantized_lspf);
  712. apply_pitch_filters(q, outbuffer);
  713. }else
  714. q->erasure_count = 0;
  715. formant_mem = q->formant_mem + 10;
  716. for(i=0; i<4; i++)
  717. {
  718. interpolate_lpc(q, quantized_lspf, lpc, i);
  719. ff_celp_lp_synthesis_filterf(formant_mem, lpc, outbuffer + i * 40, 40,
  720. 10);
  721. formant_mem += 40;
  722. }
  723. memcpy(q->formant_mem, q->formant_mem + 160, 10 * sizeof(float));
  724. // FIXME: postfilter and final gain control should be here.
  725. // TIA/EIA/IS-733 2.4.8.6
  726. formant_mem = q->formant_mem + 10;
  727. for(i=0; i<160; i++)
  728. *outbuffer++ = av_clipf(*formant_mem++, QCELP_CLIP_LOWER_BOUND,
  729. QCELP_CLIP_UPPER_BOUND);
  730. memcpy(q->prev_lspf, quantized_lspf, sizeof(q->prev_lspf));
  731. q->prev_bitrate = q->bitrate;
  732. *data_size = 160 * sizeof(*outbuffer);
  733. return *data_size;
  734. }
  735. AVCodec qcelp_decoder =
  736. {
  737. .name = "qcelp",
  738. .type = CODEC_TYPE_AUDIO,
  739. .id = CODEC_ID_QCELP,
  740. .init = qcelp_decode_init,
  741. .decode = qcelp_decode_frame,
  742. .priv_data_size = sizeof(QCELPContext),
  743. .long_name = NULL_IF_CONFIG_SMALL("QCELP / PureVoice"),
  744. };