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.

855 lines
26KB

  1. /*
  2. * QCELP decoder
  3. * Copyright (c) 2007 Reynaldo H. Verdejo Pinochet
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  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_filters.h"
  36. #include "acelp_vectors.h"
  37. #include "lsp.h"
  38. #undef NDEBUG
  39. #include <assert.h>
  40. typedef enum
  41. {
  42. I_F_Q = -1, /*!< insufficient frame quality */
  43. SILENCE,
  44. RATE_OCTAVE,
  45. RATE_QUARTER,
  46. RATE_HALF,
  47. RATE_FULL
  48. } qcelp_packet_rate;
  49. typedef struct
  50. {
  51. GetBitContext gb;
  52. qcelp_packet_rate bitrate;
  53. QCELPFrame frame; /*!< unpacked data frame */
  54. uint8_t erasure_count;
  55. uint8_t octave_count; /*!< count the consecutive RATE_OCTAVE frames */
  56. float prev_lspf[10];
  57. float predictor_lspf[10];/*!< LSP predictor for RATE_OCTAVE and I_F_Q */
  58. float pitch_synthesis_filter_mem[303];
  59. float pitch_pre_filter_mem[303];
  60. float rnd_fir_filter_mem[180];
  61. float formant_mem[170];
  62. float last_codebook_gain;
  63. int prev_g1[2];
  64. int prev_bitrate;
  65. float pitch_gain[4];
  66. uint8_t pitch_lag[4];
  67. uint16_t first16bits;
  68. uint8_t warned_buf_mismatch_bitrate;
  69. /* postfilter */
  70. float postfilter_synth_mem[10];
  71. float postfilter_agc_mem;
  72. float postfilter_tilt_mem;
  73. } QCELPContext;
  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. * Decode the 10 quantized LSP frequencies from the LSPV/LSP
  90. * transmission codes of any bitrate and check 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. * Convert codebook transmission codes to GAIN and INDEX.
  174. *
  175. * @param q the context
  176. * @param gain array holding the decoded gain
  177. *
  178. * TIA/EIA/IS-733 2.4.6.2
  179. */
  180. static void decode_gain_and_index(QCELPContext *q,
  181. float *gain) {
  182. int i, subframes_count, g1[16];
  183. float slope;
  184. if(q->bitrate >= RATE_QUARTER)
  185. {
  186. switch(q->bitrate)
  187. {
  188. case RATE_FULL: subframes_count = 16; break;
  189. case RATE_HALF: subframes_count = 4; break;
  190. default: subframes_count = 5;
  191. }
  192. for(i=0; i<subframes_count; i++)
  193. {
  194. g1[i] = 4 * q->frame.cbgain[i];
  195. if(q->bitrate == RATE_FULL && !((i+1) & 3))
  196. {
  197. g1[i] += av_clip((g1[i-1] + g1[i-2] + g1[i-3]) / 3 - 6, 0, 32);
  198. }
  199. gain[i] = qcelp_g12ga[g1[i]];
  200. if(q->frame.cbsign[i])
  201. {
  202. gain[i] = -gain[i];
  203. q->frame.cindex[i] = (q->frame.cindex[i]-89) & 127;
  204. }
  205. }
  206. q->prev_g1[0] = g1[i-2];
  207. q->prev_g1[1] = g1[i-1];
  208. q->last_codebook_gain = qcelp_g12ga[g1[i-1]];
  209. if(q->bitrate == RATE_QUARTER)
  210. {
  211. // Provide smoothing of the unvoiced excitation energy.
  212. gain[7] = gain[4];
  213. gain[6] = 0.4*gain[3] + 0.6*gain[4];
  214. gain[5] = gain[3];
  215. gain[4] = 0.8*gain[2] + 0.2*gain[3];
  216. gain[3] = 0.2*gain[1] + 0.8*gain[2];
  217. gain[2] = gain[1];
  218. gain[1] = 0.6*gain[0] + 0.4*gain[1];
  219. }
  220. }else if (q->bitrate != SILENCE)
  221. {
  222. if(q->bitrate == RATE_OCTAVE)
  223. {
  224. g1[0] = 2 * q->frame.cbgain[0]
  225. + av_clip((q->prev_g1[0] + q->prev_g1[1]) / 2 - 5, 0, 54);
  226. subframes_count = 8;
  227. }else
  228. {
  229. assert(q->bitrate == I_F_Q);
  230. g1[0] = q->prev_g1[1];
  231. switch(q->erasure_count)
  232. {
  233. case 1 : break;
  234. case 2 : g1[0] -= 1; break;
  235. case 3 : g1[0] -= 2; break;
  236. default: g1[0] -= 6;
  237. }
  238. if(g1[0] < 0)
  239. g1[0] = 0;
  240. subframes_count = 4;
  241. }
  242. // This interpolation is done to produce smoother background noise.
  243. slope = 0.5*(qcelp_g12ga[g1[0]] - q->last_codebook_gain) / subframes_count;
  244. for(i=1; i<=subframes_count; i++)
  245. gain[i-1] = q->last_codebook_gain + slope * i;
  246. q->last_codebook_gain = gain[i-2];
  247. q->prev_g1[0] = q->prev_g1[1];
  248. q->prev_g1[1] = g1[0];
  249. }
  250. }
  251. /**
  252. * If the received packet is Rate 1/4 a further sanity check is made of the
  253. * codebook gain.
  254. *
  255. * @param cbgain the unpacked cbgain array
  256. * @return -1 if the sanity check fails, 0 otherwise
  257. *
  258. * TIA/EIA/IS-733 2.4.8.7.3
  259. */
  260. static int codebook_sanity_check_for_rate_quarter(const uint8_t *cbgain)
  261. {
  262. int i, diff, prev_diff=0;
  263. for(i=1; i<5; i++)
  264. {
  265. diff = cbgain[i] - cbgain[i-1];
  266. if(FFABS(diff) > 10)
  267. return -1;
  268. else if(FFABS(diff - prev_diff) > 12)
  269. return -1;
  270. prev_diff = diff;
  271. }
  272. return 0;
  273. }
  274. /**
  275. * Compute 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. * TIA/EIA/IS-733 2.4.8.3, 2.4.8.6
  381. */
  382. static void apply_gain_ctrl(float *v_out, const float *v_ref,
  383. const float *v_in)
  384. {
  385. int i;
  386. for (i = 0; i < 160; i += 40)
  387. ff_scale_vector_to_given_sum_of_squares(v_out + i, v_in + i,
  388. ff_dot_productf(v_ref + i,
  389. v_ref + i, 40),
  390. 40);
  391. }
  392. /**
  393. * Apply filter in pitch-subframe steps.
  394. *
  395. * @param memory buffer for the previous state of the filter
  396. * - must be able to contain 303 elements
  397. * - the 143 first elements are from the previous state
  398. * - the next 160 are for output
  399. * @param v_in input filter vector
  400. * @param gain per-subframe gain array, each element is between 0.0 and 2.0
  401. * @param lag per-subframe lag array, each element is
  402. * - between 16 and 143 if its corresponding pfrac is 0,
  403. * - between 16 and 139 otherwise
  404. * @param pfrac per-subframe boolean array, 1 if the lag is fractional, 0
  405. * otherwise
  406. *
  407. * @return filter output vector
  408. */
  409. static const float *do_pitchfilter(float memory[303], const float v_in[160],
  410. const float gain[4], const uint8_t *lag,
  411. const uint8_t pfrac[4])
  412. {
  413. int i, j;
  414. float *v_lag, *v_out;
  415. const float *v_len;
  416. v_out = memory + 143; // Output vector starts at memory[143].
  417. for(i=0; i<4; i++)
  418. {
  419. if(gain[i])
  420. {
  421. v_lag = memory + 143 + 40 * i - lag[i];
  422. for(v_len=v_in+40; v_in<v_len; v_in++)
  423. {
  424. if(pfrac[i]) // If it is a fractional lag...
  425. {
  426. for(j=0, *v_out=0.; j<4; j++)
  427. *v_out += qcelp_hammsinc_table[j] * (v_lag[j-4] + v_lag[3-j]);
  428. }else
  429. *v_out = *v_lag;
  430. *v_out = *v_in + gain[i] * *v_out;
  431. v_lag++;
  432. v_out++;
  433. }
  434. }else
  435. {
  436. memcpy(v_out, v_in, 40 * sizeof(float));
  437. v_in += 40;
  438. v_out += 40;
  439. }
  440. }
  441. memmove(memory, memory + 160, 143 * sizeof(float));
  442. return memory + 143;
  443. }
  444. /**
  445. * Apply pitch synthesis filter and pitch prefilter to the scaled codebook vector.
  446. * TIA/EIA/IS-733 2.4.5.2, 2.4.8.7.2
  447. *
  448. * @param q the context
  449. * @param cdn_vector the scaled codebook vector
  450. */
  451. static void apply_pitch_filters(QCELPContext *q, float *cdn_vector)
  452. {
  453. int i;
  454. const float *v_synthesis_filtered, *v_pre_filtered;
  455. if(q->bitrate >= RATE_HALF ||
  456. q->bitrate == SILENCE ||
  457. (q->bitrate == I_F_Q && (q->prev_bitrate >= RATE_HALF)))
  458. {
  459. if(q->bitrate >= RATE_HALF)
  460. {
  461. // Compute gain & lag for the whole frame.
  462. for(i=0; i<4; i++)
  463. {
  464. q->pitch_gain[i] = q->frame.plag[i] ? (q->frame.pgain[i] + 1) * 0.25 : 0.0;
  465. q->pitch_lag[i] = q->frame.plag[i] + 16;
  466. }
  467. }else
  468. {
  469. float max_pitch_gain;
  470. if (q->bitrate == I_F_Q)
  471. {
  472. if (q->erasure_count < 3)
  473. max_pitch_gain = 0.9 - 0.3 * (q->erasure_count - 1);
  474. else
  475. max_pitch_gain = 0.0;
  476. }else
  477. {
  478. assert(q->bitrate == SILENCE);
  479. max_pitch_gain = 1.0;
  480. }
  481. for(i=0; i<4; i++)
  482. q->pitch_gain[i] = FFMIN(q->pitch_gain[i], max_pitch_gain);
  483. memset(q->frame.pfrac, 0, sizeof(q->frame.pfrac));
  484. }
  485. // pitch synthesis filter
  486. v_synthesis_filtered = do_pitchfilter(q->pitch_synthesis_filter_mem,
  487. cdn_vector, q->pitch_gain,
  488. q->pitch_lag, q->frame.pfrac);
  489. // pitch prefilter update
  490. for(i=0; i<4; i++)
  491. q->pitch_gain[i] = 0.5 * FFMIN(q->pitch_gain[i], 1.0);
  492. v_pre_filtered = do_pitchfilter(q->pitch_pre_filter_mem,
  493. v_synthesis_filtered,
  494. q->pitch_gain, q->pitch_lag,
  495. q->frame.pfrac);
  496. apply_gain_ctrl(cdn_vector, v_synthesis_filtered, v_pre_filtered);
  497. }else
  498. {
  499. memcpy(q->pitch_synthesis_filter_mem, cdn_vector + 17,
  500. 143 * sizeof(float));
  501. memcpy(q->pitch_pre_filter_mem, cdn_vector + 17, 143 * sizeof(float));
  502. memset(q->pitch_gain, 0, sizeof(q->pitch_gain));
  503. memset(q->pitch_lag, 0, sizeof(q->pitch_lag));
  504. }
  505. }
  506. /**
  507. * Reconstruct LPC coefficients from the line spectral pair frequencies
  508. * and perform bandwidth expansion.
  509. *
  510. * @param lspf line spectral pair frequencies
  511. * @param lpc linear predictive coding coefficients
  512. *
  513. * @note: bandwidth_expansion_coeff could be precalculated into a table
  514. * but it seems to be slower on x86
  515. *
  516. * TIA/EIA/IS-733 2.4.3.3.5
  517. */
  518. static void lspf2lpc(const float *lspf, float *lpc)
  519. {
  520. double lsp[10];
  521. double bandwidth_expansion_coeff = QCELP_BANDWIDTH_EXPANSION_COEFF;
  522. int i;
  523. for (i=0; i<10; i++)
  524. lsp[i] = cos(M_PI * lspf[i]);
  525. ff_acelp_lspd2lpc(lsp, lpc, 5);
  526. for (i=0; i<10; i++)
  527. {
  528. lpc[i] *= bandwidth_expansion_coeff;
  529. bandwidth_expansion_coeff *= QCELP_BANDWIDTH_EXPANSION_COEFF;
  530. }
  531. }
  532. /**
  533. * Interpolate LSP frequencies and compute LPC coefficients
  534. * for a given bitrate & pitch subframe.
  535. *
  536. * TIA/EIA/IS-733 2.4.3.3.4, 2.4.8.7.2
  537. *
  538. * @param q the context
  539. * @param curr_lspf LSP frequencies vector of the current frame
  540. * @param lpc float vector for the resulting LPC
  541. * @param subframe_num frame number in decoded stream
  542. */
  543. static void interpolate_lpc(QCELPContext *q, const float *curr_lspf,
  544. float *lpc, const int subframe_num)
  545. {
  546. float interpolated_lspf[10];
  547. float weight;
  548. if(q->bitrate >= RATE_QUARTER)
  549. weight = 0.25 * (subframe_num + 1);
  550. else if(q->bitrate == RATE_OCTAVE && !subframe_num)
  551. weight = 0.625;
  552. else
  553. weight = 1.0;
  554. if(weight != 1.0)
  555. {
  556. ff_weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf,
  557. weight, 1.0 - weight, 10);
  558. lspf2lpc(interpolated_lspf, lpc);
  559. }else if(q->bitrate >= RATE_QUARTER ||
  560. (q->bitrate == I_F_Q && !subframe_num))
  561. lspf2lpc(curr_lspf, lpc);
  562. else if(q->bitrate == SILENCE && !subframe_num)
  563. lspf2lpc(q->prev_lspf, lpc);
  564. }
  565. static qcelp_packet_rate buf_size2bitrate(const int buf_size)
  566. {
  567. switch(buf_size)
  568. {
  569. case 35: return RATE_FULL;
  570. case 17: return RATE_HALF;
  571. case 8: return RATE_QUARTER;
  572. case 4: return RATE_OCTAVE;
  573. case 1: return SILENCE;
  574. }
  575. return I_F_Q;
  576. }
  577. /**
  578. * Determine the bitrate from the frame size and/or the first byte of the frame.
  579. *
  580. * @param avctx the AV codec context
  581. * @param buf_size length of the buffer
  582. * @param buf the bufffer
  583. *
  584. * @return the bitrate on success,
  585. * I_F_Q if the bitrate cannot be satisfactorily determined
  586. *
  587. * TIA/EIA/IS-733 2.4.8.7.1
  588. */
  589. static qcelp_packet_rate determine_bitrate(AVCodecContext *avctx, const int buf_size,
  590. const uint8_t **buf)
  591. {
  592. qcelp_packet_rate bitrate;
  593. if((bitrate = buf_size2bitrate(buf_size)) >= 0)
  594. {
  595. if(bitrate > **buf)
  596. {
  597. QCELPContext *q = avctx->priv_data;
  598. if (!q->warned_buf_mismatch_bitrate)
  599. {
  600. av_log(avctx, AV_LOG_WARNING,
  601. "Claimed bitrate and buffer size mismatch.\n");
  602. q->warned_buf_mismatch_bitrate = 1;
  603. }
  604. bitrate = **buf;
  605. }else if(bitrate < **buf)
  606. {
  607. av_log(avctx, AV_LOG_ERROR,
  608. "Buffer is too small for the claimed bitrate.\n");
  609. return I_F_Q;
  610. }
  611. (*buf)++;
  612. }else if((bitrate = buf_size2bitrate(buf_size + 1)) >= 0)
  613. {
  614. av_log(avctx, AV_LOG_WARNING,
  615. "Bitrate byte is missing, guessing the bitrate from packet size.\n");
  616. }else
  617. return I_F_Q;
  618. if(bitrate == SILENCE)
  619. {
  620. //FIXME: Remove experimental warning when tested with samples.
  621. av_log_ask_for_sample(avctx, "'Blank frame handling is experimental.");
  622. }
  623. return bitrate;
  624. }
  625. static void warn_insufficient_frame_quality(AVCodecContext *avctx,
  626. const char *message)
  627. {
  628. av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n", avctx->frame_number,
  629. message);
  630. }
  631. static void postfilter(QCELPContext *q, float *samples, float *lpc)
  632. {
  633. static const float pow_0_775[10] = {
  634. 0.775000, 0.600625, 0.465484, 0.360750, 0.279582,
  635. 0.216676, 0.167924, 0.130141, 0.100859, 0.078166
  636. }, pow_0_625[10] = {
  637. 0.625000, 0.390625, 0.244141, 0.152588, 0.095367,
  638. 0.059605, 0.037253, 0.023283, 0.014552, 0.009095
  639. };
  640. float lpc_s[10], lpc_p[10], pole_out[170], zero_out[160];
  641. int n;
  642. for (n = 0; n < 10; n++) {
  643. lpc_s[n] = lpc[n] * pow_0_625[n];
  644. lpc_p[n] = lpc[n] * pow_0_775[n];
  645. }
  646. ff_celp_lp_zero_synthesis_filterf(zero_out, lpc_s,
  647. q->formant_mem + 10, 160, 10);
  648. memcpy(pole_out, q->postfilter_synth_mem, sizeof(float) * 10);
  649. ff_celp_lp_synthesis_filterf(pole_out + 10, lpc_p, zero_out, 160, 10);
  650. memcpy(q->postfilter_synth_mem, pole_out + 160, sizeof(float) * 10);
  651. ff_tilt_compensation(&q->postfilter_tilt_mem, 0.3, pole_out + 10, 160);
  652. ff_adaptive_gain_control(samples, pole_out + 10,
  653. ff_dot_productf(q->formant_mem + 10, q->formant_mem + 10, 160),
  654. 160, 0.9375, &q->postfilter_agc_mem);
  655. }
  656. static int qcelp_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  657. AVPacket *avpkt)
  658. {
  659. const uint8_t *buf = avpkt->data;
  660. int buf_size = avpkt->size;
  661. QCELPContext *q = avctx->priv_data;
  662. float *outbuffer = data;
  663. int i;
  664. float quantized_lspf[10], lpc[10];
  665. float gain[16];
  666. float *formant_mem;
  667. if((q->bitrate = determine_bitrate(avctx, buf_size, &buf)) == I_F_Q)
  668. {
  669. warn_insufficient_frame_quality(avctx, "bitrate cannot be determined.");
  670. goto erasure;
  671. }
  672. if(q->bitrate == RATE_OCTAVE &&
  673. (q->first16bits = AV_RB16(buf)) == 0xFFFF)
  674. {
  675. warn_insufficient_frame_quality(avctx, "Bitrate is 1/8 and first 16 bits are on.");
  676. goto erasure;
  677. }
  678. if(q->bitrate > SILENCE)
  679. {
  680. const QCELPBitmap *bitmaps = qcelp_unpacking_bitmaps_per_rate[q->bitrate];
  681. const QCELPBitmap *bitmaps_end = qcelp_unpacking_bitmaps_per_rate[q->bitrate]
  682. + qcelp_unpacking_bitmaps_lengths[q->bitrate];
  683. uint8_t *unpacked_data = (uint8_t *)&q->frame;
  684. init_get_bits(&q->gb, buf, 8*buf_size);
  685. memset(&q->frame, 0, sizeof(QCELPFrame));
  686. for(; bitmaps < bitmaps_end; bitmaps++)
  687. unpacked_data[bitmaps->index] |= get_bits(&q->gb, bitmaps->bitlen) << bitmaps->bitpos;
  688. // Check for erasures/blanks on rates 1, 1/4 and 1/8.
  689. if(q->frame.reserved)
  690. {
  691. warn_insufficient_frame_quality(avctx, "Wrong data in reserved frame area.");
  692. goto erasure;
  693. }
  694. if(q->bitrate == RATE_QUARTER &&
  695. codebook_sanity_check_for_rate_quarter(q->frame.cbgain))
  696. {
  697. warn_insufficient_frame_quality(avctx, "Codebook gain sanity check failed.");
  698. goto erasure;
  699. }
  700. if(q->bitrate >= RATE_HALF)
  701. {
  702. for(i=0; i<4; i++)
  703. {
  704. if(q->frame.pfrac[i] && q->frame.plag[i] >= 124)
  705. {
  706. warn_insufficient_frame_quality(avctx, "Cannot initialize pitch filter.");
  707. goto erasure;
  708. }
  709. }
  710. }
  711. }
  712. decode_gain_and_index(q, gain);
  713. compute_svector(q, gain, outbuffer);
  714. if(decode_lspf(q, quantized_lspf) < 0)
  715. {
  716. warn_insufficient_frame_quality(avctx, "Badly received packets in frame.");
  717. goto erasure;
  718. }
  719. apply_pitch_filters(q, outbuffer);
  720. if(q->bitrate == I_F_Q)
  721. {
  722. erasure:
  723. q->bitrate = I_F_Q;
  724. q->erasure_count++;
  725. decode_gain_and_index(q, gain);
  726. compute_svector(q, gain, outbuffer);
  727. decode_lspf(q, quantized_lspf);
  728. apply_pitch_filters(q, outbuffer);
  729. }else
  730. q->erasure_count = 0;
  731. formant_mem = q->formant_mem + 10;
  732. for(i=0; i<4; i++)
  733. {
  734. interpolate_lpc(q, quantized_lspf, lpc, i);
  735. ff_celp_lp_synthesis_filterf(formant_mem, lpc, outbuffer + i * 40, 40,
  736. 10);
  737. formant_mem += 40;
  738. }
  739. // postfilter, as per TIA/EIA/IS-733 2.4.8.6
  740. postfilter(q, outbuffer, lpc);
  741. memcpy(q->formant_mem, q->formant_mem + 160, 10 * sizeof(float));
  742. memcpy(q->prev_lspf, quantized_lspf, sizeof(q->prev_lspf));
  743. q->prev_bitrate = q->bitrate;
  744. *data_size = 160 * sizeof(*outbuffer);
  745. return *data_size;
  746. }
  747. AVCodec qcelp_decoder =
  748. {
  749. .name = "qcelp",
  750. .type = AVMEDIA_TYPE_AUDIO,
  751. .id = CODEC_ID_QCELP,
  752. .init = qcelp_decode_init,
  753. .decode = qcelp_decode_frame,
  754. .priv_data_size = sizeof(QCELPContext),
  755. .long_name = NULL_IF_CONFIG_SMALL("QCELP / PureVoice"),
  756. };