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.

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