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.

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