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.

795 lines
24KB

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