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.

921 lines
29KB

  1. /*
  2. * Enhanced Variable Rate Codec, Service Option 3 decoder
  3. * Copyright (c) 2013 Paul B Mahol
  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. * Enhanced Variable Rate Codec, Service Option 3 decoder
  24. * @author Paul B Mahol
  25. */
  26. #include "libavutil/mathematics.h"
  27. #include "avcodec.h"
  28. #include "internal.h"
  29. #include "get_bits.h"
  30. #include "evrcdata.h"
  31. #include "acelp_vectors.h"
  32. #include "lsp.h"
  33. #define MIN_LSP_SEP (0.05 / (2.0 * M_PI))
  34. #define MIN_DELAY 20
  35. #define MAX_DELAY 120
  36. #define NB_SUBFRAMES 3
  37. #define SUBFRAME_SIZE 54
  38. #define FILTER_ORDER 10
  39. #define ACB_SIZE 128
  40. typedef enum {
  41. RATE_ERRS = -1,
  42. SILENCE,
  43. RATE_QUANT,
  44. RATE_QUARTER,
  45. RATE_HALF,
  46. RATE_FULL,
  47. } evrc_packet_rate;
  48. /**
  49. * EVRC-A unpacked data frame
  50. */
  51. typedef struct EVRCAFrame {
  52. uint8_t lpc_flag; ///< spectral change indicator
  53. uint16_t lsp[4]; ///< index into LSP codebook
  54. uint8_t pitch_delay; ///< pitch delay for entire frame
  55. uint8_t delay_diff; ///< delay difference for entire frame
  56. uint8_t acb_gain[3]; ///< adaptive codebook gain
  57. uint16_t fcb_shape[3][4]; ///< fixed codebook shape
  58. uint8_t fcb_gain[3]; ///< fixed codebook gain index
  59. uint8_t energy_gain; ///< frame energy gain index
  60. uint8_t tty; ///< tty baud rate bit
  61. } EVRCAFrame;
  62. typedef struct EVRCContext {
  63. AVFrame avframe;
  64. GetBitContext gb;
  65. evrc_packet_rate bitrate;
  66. evrc_packet_rate last_valid_bitrate;
  67. EVRCAFrame frame;
  68. float lspf[FILTER_ORDER];
  69. float prev_lspf[FILTER_ORDER];
  70. float synthesis[FILTER_ORDER];
  71. float postfilter_fir[FILTER_ORDER];
  72. float postfilter_iir[FILTER_ORDER];
  73. float postfilter_residual[ACB_SIZE + SUBFRAME_SIZE];
  74. float pitch_delay;
  75. float prev_pitch_delay;
  76. float avg_acb_gain; ///< average adaptive codebook gain
  77. float avg_fcb_gain; ///< average fixed codebook gain
  78. float pitch[ACB_SIZE + FILTER_ORDER + SUBFRAME_SIZE];
  79. float pitch_back[ACB_SIZE];
  80. float interpolation_coeffs[136];
  81. float energy_vector[NB_SUBFRAMES];
  82. float fade_scale;
  83. float last;
  84. uint8_t prev_energy_gain;
  85. uint8_t prev_error_flag;
  86. uint8_t warned_buf_mismatch_bitrate;
  87. } EVRCContext;
  88. /**
  89. * Frame unpacking for RATE_FULL, RATE_HALF and RATE_QUANT
  90. *
  91. * @param e the context
  92. *
  93. * TIA/IS-127 Table 4.21-1
  94. */
  95. static void unpack_frame(EVRCContext *e)
  96. {
  97. EVRCAFrame *frame = &e->frame;
  98. GetBitContext *gb = &e->gb;
  99. switch (e->bitrate) {
  100. case RATE_FULL:
  101. frame->lpc_flag = get_bits1(gb);
  102. frame->lsp[0] = get_bits(gb, 6);
  103. frame->lsp[1] = get_bits(gb, 6);
  104. frame->lsp[2] = get_bits(gb, 9);
  105. frame->lsp[3] = get_bits(gb, 7);
  106. frame->pitch_delay = get_bits(gb, 7);
  107. frame->delay_diff = get_bits(gb, 5);
  108. frame->acb_gain[0] = get_bits(gb, 3);
  109. frame->fcb_shape[0][0] = get_bits(gb, 8);
  110. frame->fcb_shape[0][1] = get_bits(gb, 8);
  111. frame->fcb_shape[0][2] = get_bits(gb, 8);
  112. frame->fcb_shape[0][3] = get_bits(gb, 11);
  113. frame->fcb_gain[0] = get_bits(gb, 5);
  114. frame->acb_gain[1] = get_bits(gb, 3);
  115. frame->fcb_shape[1][0] = get_bits(gb, 8);
  116. frame->fcb_shape[1][1] = get_bits(gb, 8);
  117. frame->fcb_shape[1][2] = get_bits(gb, 8);
  118. frame->fcb_shape[1][3] = get_bits(gb, 11);
  119. frame->fcb_gain [1] = get_bits(gb, 5);
  120. frame->acb_gain [2] = get_bits(gb, 3);
  121. frame->fcb_shape[2][0] = get_bits(gb, 8);
  122. frame->fcb_shape[2][1] = get_bits(gb, 8);
  123. frame->fcb_shape[2][2] = get_bits(gb, 8);
  124. frame->fcb_shape[2][3] = get_bits(gb, 11);
  125. frame->fcb_gain [2] = get_bits(gb, 5);
  126. frame->tty = get_bits1(gb);
  127. break;
  128. case RATE_HALF:
  129. frame->lsp [0] = get_bits(gb, 7);
  130. frame->lsp [1] = get_bits(gb, 7);
  131. frame->lsp [2] = get_bits(gb, 8);
  132. frame->pitch_delay = get_bits(gb, 7);
  133. frame->acb_gain [0] = get_bits(gb, 3);
  134. frame->fcb_shape[0][0] = get_bits(gb, 10);
  135. frame->fcb_gain [0] = get_bits(gb, 4);
  136. frame->acb_gain [1] = get_bits(gb, 3);
  137. frame->fcb_shape[1][0] = get_bits(gb, 10);
  138. frame->fcb_gain [1] = get_bits(gb, 4);
  139. frame->acb_gain [2] = get_bits(gb, 3);
  140. frame->fcb_shape[2][0] = get_bits(gb, 10);
  141. frame->fcb_gain [2] = get_bits(gb, 4);
  142. break;
  143. case RATE_QUANT:
  144. frame->lsp [0] = get_bits(gb, 4);
  145. frame->lsp [1] = get_bits(gb, 4);
  146. frame->energy_gain = get_bits(gb, 8);
  147. break;
  148. }
  149. }
  150. static evrc_packet_rate buf_size2bitrate(const int buf_size)
  151. {
  152. switch (buf_size) {
  153. case 23: return RATE_FULL;
  154. case 11: return RATE_HALF;
  155. case 6: return RATE_QUARTER;
  156. case 3: return RATE_QUANT;
  157. case 1: return SILENCE;
  158. }
  159. return RATE_ERRS;
  160. }
  161. /**
  162. * Determine the bitrate from the frame size and/or the first byte of the frame.
  163. *
  164. * @param avctx the AV codec context
  165. * @param buf_size length of the buffer
  166. * @param buf the bufffer
  167. *
  168. * @return the bitrate on success,
  169. * RATE_ERRS if the bitrate cannot be satisfactorily determined
  170. */
  171. static evrc_packet_rate determine_bitrate(AVCodecContext *avctx,
  172. int *buf_size,
  173. const uint8_t **buf)
  174. {
  175. evrc_packet_rate bitrate;
  176. if ((bitrate = buf_size2bitrate(*buf_size)) >= 0) {
  177. if (bitrate > **buf) {
  178. EVRCContext *e = avctx->priv_data;
  179. if (!e->warned_buf_mismatch_bitrate) {
  180. av_log(avctx, AV_LOG_WARNING,
  181. "Claimed bitrate and buffer size mismatch.\n");
  182. e->warned_buf_mismatch_bitrate = 1;
  183. }
  184. bitrate = **buf;
  185. } else if (bitrate < **buf) {
  186. av_log(avctx, AV_LOG_ERROR,
  187. "Buffer is too small for the claimed bitrate.\n");
  188. return RATE_ERRS;
  189. }
  190. (*buf)++;
  191. *buf_size -= 1;
  192. } else if ((bitrate = buf_size2bitrate(*buf_size + 1)) >= 0) {
  193. av_log(avctx, AV_LOG_DEBUG,
  194. "Bitrate byte is missing, guessing the bitrate from packet size.\n");
  195. } else
  196. return RATE_ERRS;
  197. return bitrate;
  198. }
  199. static void warn_insufficient_frame_quality(AVCodecContext *avctx,
  200. const char *message)
  201. {
  202. av_log(avctx, AV_LOG_WARNING, "Frame #%d, %s\n",
  203. avctx->frame_number, message);
  204. }
  205. /**
  206. * Initialize the speech codec according to the specification.
  207. *
  208. * TIA/IS-127 5.2
  209. */
  210. static av_cold int evrc_decode_init(AVCodecContext *avctx)
  211. {
  212. EVRCContext *e = avctx->priv_data;
  213. int i, n, idx = 0;
  214. float denom = 2.0 / (2.0 * 8.0 + 1.0);
  215. avcodec_get_frame_defaults(&e->avframe);
  216. avctx->coded_frame = &e->avframe;
  217. avctx->channels = 1;
  218. avctx->channel_layout = AV_CH_LAYOUT_MONO;
  219. avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
  220. for (i = 0; i < FILTER_ORDER; i++) {
  221. e->prev_lspf[i] = (i + 1) * 0.048;
  222. e->synthesis[i] = 0.0;
  223. }
  224. for (i = 0; i < ACB_SIZE; i++)
  225. e->pitch[i] = e->pitch_back[i] = 0.0;
  226. e->last_valid_bitrate = RATE_QUANT;
  227. e->prev_pitch_delay = 40.0;
  228. e->fade_scale = 1.0;
  229. e->prev_error_flag = 0;
  230. e->avg_acb_gain = e->avg_fcb_gain = 0.0;
  231. for (i = 0; i < 8; i++) {
  232. float tt = ((float)i - 8.0 / 2.0) / 8.0;
  233. for (n = -8; n <= 8; n++, idx++) {
  234. float arg1 = M_PI * 0.9 * (tt - n);
  235. float arg2 = M_PI * (tt - n);
  236. e->interpolation_coeffs[idx] = 0.9;
  237. if (arg1)
  238. e->interpolation_coeffs[idx] *= (0.54 + 0.46 * cos(arg2 * denom)) *
  239. sin(arg1) / arg1;
  240. }
  241. }
  242. return 0;
  243. }
  244. /**
  245. * Decode the 10 vector quantized line spectral pair frequencies from the LSP
  246. * transmission codes of any bitrate and check for badly received packets.
  247. *
  248. * @param e the context
  249. *
  250. * @return 0 on success, -1 if the packet is badly received
  251. *
  252. * TIA/IS-127 5.2.1, 5.7.1
  253. */
  254. static int decode_lspf(EVRCContext *e)
  255. {
  256. const float **codebooks = evrc_lspq_codebooks[e->bitrate];
  257. int i, j, k = 0;
  258. for (i = 0; i < evrc_lspq_nb_codebooks[e->bitrate]; i++) {
  259. int row_size = evrc_lspq_codebooks_row_sizes[e->bitrate][i];
  260. const float *codebook = codebooks[i];
  261. for (j = 0; j < row_size; j++)
  262. e->lspf[k++] = codebook[e->frame.lsp[i] * row_size + j];
  263. }
  264. // check for monotonic LSPs
  265. for (i = 1; i < FILTER_ORDER; i++)
  266. if (e->lspf[i] <= e->lspf[i - 1])
  267. return -1;
  268. // check for minimum separation of LSPs at the splits
  269. for (i = 0, k = 0; i < evrc_lspq_nb_codebooks[e->bitrate] - 1; i++) {
  270. k += evrc_lspq_codebooks_row_sizes[e->bitrate][i];
  271. if (e->lspf[k] - e->lspf[k - 1] <= MIN_LSP_SEP)
  272. return -1;
  273. }
  274. return 0;
  275. }
  276. /*
  277. * Interpolation of LSP parameters.
  278. *
  279. * TIA/IS-127 5.2.3.1, 5.7.3.2
  280. */
  281. static void interpolate_lsp(float *ilsp, const float *lsp,
  282. const float *prev, int index)
  283. {
  284. static const float lsp_interpolation_factors[] = { 0.1667, 0.5, 0.8333 };
  285. ff_weighted_vector_sumf(ilsp, prev, lsp,
  286. 1.0 - lsp_interpolation_factors[index],
  287. lsp_interpolation_factors[index], FILTER_ORDER);
  288. }
  289. /*
  290. * Reconstruction of the delay contour.
  291. *
  292. * TIA/IS-127 5.2.2.3.2
  293. */
  294. static void interpolate_delay(float *dst, float current, float prev, int index)
  295. {
  296. static const float d_interpolation_factors[] = { 0, 0.3313, 0.6625, 1, 1 };
  297. dst[0] = (1.0 - d_interpolation_factors[index ]) * prev
  298. + d_interpolation_factors[index ] * current;
  299. dst[1] = (1.0 - d_interpolation_factors[index + 1]) * prev
  300. + d_interpolation_factors[index + 1] * current;
  301. dst[2] = (1.0 - d_interpolation_factors[index + 2]) * prev
  302. + d_interpolation_factors[index + 2] * current;
  303. }
  304. /*
  305. * Convert the quantized, interpolated line spectral frequencies,
  306. * to prediction coefficients.
  307. *
  308. * TIA/IS-127 5.2.3.2, 4.7.2.2
  309. */
  310. static void decode_predictor_coeffs(const float *ilspf, float *ilpc)
  311. {
  312. double lsp[FILTER_ORDER];
  313. float a[FILTER_ORDER / 2 + 1], b[FILTER_ORDER / 2 + 1];
  314. float a1[FILTER_ORDER / 2] = { 0 };
  315. float a2[FILTER_ORDER / 2] = { 0 };
  316. float b1[FILTER_ORDER / 2] = { 0 };
  317. float b2[FILTER_ORDER / 2] = { 0 };
  318. int i, k;
  319. ff_acelp_lsf2lspd(lsp, ilspf, FILTER_ORDER);
  320. for (k = 0; k <= FILTER_ORDER; k++) {
  321. a[0] = k < 2 ? 0.25 : 0;
  322. b[0] = k < 2 ? k < 1 ? 0.25 : -0.25 : 0;
  323. for (i = 0; i < FILTER_ORDER / 2; i++) {
  324. a[i + 1] = a[i] - 2 * lsp[i * 2 ] * a1[i] + a2[i];
  325. b[i + 1] = b[i] - 2 * lsp[i * 2 + 1] * b1[i] + b2[i];
  326. a2[i] = a1[i];
  327. a1[i] = a[i];
  328. b2[i] = b1[i];
  329. b1[i] = b[i];
  330. }
  331. if (k)
  332. ilpc[k - 1] = 2.0 * (a[FILTER_ORDER / 2] + b[FILTER_ORDER / 2]);
  333. }
  334. }
  335. static void bl_intrp(EVRCContext *e, float *ex, float delay)
  336. {
  337. float *f;
  338. int offset, i, coef_idx;
  339. int16_t t;
  340. offset = lrintf(fabs(delay));
  341. t = (offset - delay + 0.5) * 8.0 + 0.5;
  342. if (t == 8) {
  343. t = 0;
  344. offset--;
  345. }
  346. f = ex - offset - 8;
  347. coef_idx = t * (2 * 8 + 1);
  348. ex[0] = 0.0;
  349. for (i = 0; i < 2 * 8 + 1; i++)
  350. ex[0] += e->interpolation_coeffs[coef_idx + i] * f[i];
  351. }
  352. /*
  353. * Adaptive codebook excitation.
  354. *
  355. * TIA/IS-127 5.2.2.3.3, 4.12.5.2
  356. */
  357. static void acb_excitation(EVRCContext *e, float *excitation, float gain,
  358. const float delay[3], int length)
  359. {
  360. float denom, locdelay, dpr, invl;
  361. int i;
  362. invl = 1.0 / ((float) length);
  363. dpr = length;
  364. /* first at-most extra samples */
  365. denom = (delay[1] - delay[0]) * invl;
  366. for (i = 0; i < dpr; i++) {
  367. locdelay = delay[0] + i * denom;
  368. bl_intrp(e, excitation + i, locdelay);
  369. }
  370. denom = (delay[2] - delay[1]) * invl;
  371. /* interpolation */
  372. for (i = dpr; i < dpr + 10; i++) {
  373. locdelay = delay[1] + (i - dpr) * denom;
  374. bl_intrp(e, excitation + i, locdelay);
  375. }
  376. for (i = 0; i < length; i++)
  377. excitation[i] *= gain;
  378. }
  379. static void decode_8_pulses_35bits(const uint16_t *fixed_index, float *cod)
  380. {
  381. int i, pos1, pos2, offset;
  382. offset = (fixed_index[3] >> 9) & 3;
  383. for (i = 0; i < 3; i++) {
  384. pos1 = ((fixed_index[i] & 0x7f) / 11) * 5 + ((i + offset) % 5);
  385. pos2 = ((fixed_index[i] & 0x7f) % 11) * 5 + ((i + offset) % 5);
  386. cod[pos1] = (fixed_index[i] & 0x80) ? -1.0 : 1.0;
  387. if (pos2 < pos1)
  388. cod[pos2] = -cod[pos1];
  389. else
  390. cod[pos2] += cod[pos1];
  391. }
  392. pos1 = ((fixed_index[3] & 0x7f) / 11) * 5 + ((3 + offset) % 5);
  393. pos2 = ((fixed_index[3] & 0x7f) % 11) * 5 + ((4 + offset) % 5);
  394. cod[pos1] = (fixed_index[3] & 0x100) ? -1.0 : 1.0;
  395. cod[pos2] = (fixed_index[3] & 0x80 ) ? -1.0 : 1.0;
  396. }
  397. static void decode_3_pulses_10bits(uint16_t fixed_index, float *cod)
  398. {
  399. float sign;
  400. int pos;
  401. sign = (fixed_index & 0x200) ? -1.0 : 1.0;
  402. pos = ((fixed_index & 0x7) * 7) + 4;
  403. cod[pos] += sign;
  404. pos = (((fixed_index >> 3) & 0x7) * 7) + 2;
  405. cod[pos] -= sign;
  406. pos = (((fixed_index >> 6) & 0x7) * 7);
  407. cod[pos] += sign;
  408. }
  409. /*
  410. * Reconstruction of ACELP fixed codebook excitation for full and half rate.
  411. *
  412. * TIA/IS-127 5.2.3.7
  413. */
  414. static void fcb_excitation(EVRCContext *e, const uint16_t *codebook,
  415. float *excitation, float pitch_gain,
  416. int pitch_lag, int subframe_size)
  417. {
  418. int i;
  419. if (e->bitrate == RATE_FULL)
  420. decode_8_pulses_35bits(codebook, excitation);
  421. else
  422. decode_3_pulses_10bits(*codebook, excitation);
  423. pitch_gain = av_clipf(pitch_gain, 0.2, 0.9);
  424. for (i = pitch_lag; i < subframe_size; i++)
  425. excitation[i] += pitch_gain * excitation[i - pitch_lag];
  426. }
  427. /**
  428. * Synthesis of the decoder output signal.
  429. *
  430. * param[in] in input signal
  431. * param[in] filter_coeffs LPC coefficients
  432. * param[in/out] memory synthesis filter memory
  433. * param buffer_length amount of data to process
  434. * param[out] samples output samples
  435. *
  436. * TIA/IS-127 5.2.3.15, 5.7.3.4
  437. */
  438. static void synthesis_filter(const float *in, const float *filter_coeffs,
  439. float *memory, int buffer_length, float *samples)
  440. {
  441. int i, j;
  442. for (i = 0; i < buffer_length; i++) {
  443. samples[i] = in[i];
  444. for (j = FILTER_ORDER - 1; j > 0; j--) {
  445. samples[i] -= filter_coeffs[j] * memory[j];
  446. memory[j] = memory[j - 1];
  447. }
  448. samples[i] -= filter_coeffs[0] * memory[0];
  449. memory[0] = samples[i];
  450. }
  451. }
  452. static void bandwidth_expansion(float *coeff, const float *inbuf, float gamma)
  453. {
  454. double fac = gamma;
  455. int i;
  456. for (i = 0; i < FILTER_ORDER; i++) {
  457. coeff[i] = inbuf[i] * fac;
  458. fac *= gamma;
  459. }
  460. }
  461. static void residual_filter(float *output, const float *input,
  462. const float *coef, float *memory, int length)
  463. {
  464. float sum;
  465. int i, j;
  466. for (i = 0; i < length; i++) {
  467. sum = input[i];
  468. for (j = FILTER_ORDER - 1; j > 0; j--) {
  469. sum += coef[j] * memory[j];
  470. memory[j] = memory[j - 1];
  471. }
  472. sum += coef[0] * memory[0];
  473. memory[0] = input[i];
  474. output[i] = sum;
  475. }
  476. }
  477. /*
  478. * TIA/IS-127 Table 5.9.1-1.
  479. */
  480. static const struct PfCoeff {
  481. float tilt;
  482. float ltgain;
  483. float p1;
  484. float p2;
  485. } postfilter_coeffs[5] = {
  486. { 0.0 , 0.0 , 0.0 , 0.0 },
  487. { 0.0 , 0.0 , 0.57, 0.57 },
  488. { 0.0 , 0.0 , 0.0 , 0.0 },
  489. { 0.35, 0.50, 0.50, 0.75 },
  490. { 0.20, 0.50, 0.57, 0.75 },
  491. };
  492. /*
  493. * Adaptive postfilter.
  494. *
  495. * TIA/IS-127 5.9
  496. */
  497. static void postfilter(EVRCContext *e, float *in, const float *coeff,
  498. float *out, int idx, const struct PfCoeff *pfc,
  499. int length)
  500. {
  501. float wcoef1[FILTER_ORDER], wcoef2[FILTER_ORDER],
  502. scratch[SUBFRAME_SIZE], temp[SUBFRAME_SIZE],
  503. mem[SUBFRAME_SIZE];
  504. float sum1 = 0.0, sum2 = 0.0, gamma, gain;
  505. float tilt = pfc->tilt;
  506. int i, n, best;
  507. bandwidth_expansion(wcoef1, coeff, pfc->p1);
  508. bandwidth_expansion(wcoef2, coeff, pfc->p2);
  509. /* Tilt compensation filter, TIA/IS-127 5.9.1 */
  510. for (i = 0; i < length - 1; i++)
  511. sum2 += in[i] * in[i + 1];
  512. if (sum2 < 0.0)
  513. tilt = 0.0;
  514. for (i = 0; i < length; i++) {
  515. scratch[i] = in[i] - tilt * e->last;
  516. e->last = in[i];
  517. }
  518. /* Short term residual filter, TIA/IS-127 5.9.2 */
  519. residual_filter(&e->postfilter_residual[ACB_SIZE], scratch, wcoef1, e->postfilter_fir, length);
  520. /* Long term postfilter */
  521. best = idx;
  522. for (i = FFMIN(MIN_DELAY, idx - 3); i <= FFMAX(MAX_DELAY, idx + 3); i++) {
  523. for (n = ACB_SIZE, sum2 = 0; n < ACB_SIZE + length; n++)
  524. sum2 += e->postfilter_residual[n] * e->postfilter_residual[n - i];
  525. if (sum2 > sum1) {
  526. sum1 = sum2;
  527. best = i;
  528. }
  529. }
  530. for (i = ACB_SIZE, sum1 = 0; i < ACB_SIZE + length; i++)
  531. sum1 += e->postfilter_residual[i - best] * e->postfilter_residual[i - best];
  532. for (i = ACB_SIZE, sum2 = 0; i < ACB_SIZE + length; i++)
  533. sum2 += e->postfilter_residual[i] * e->postfilter_residual[i - best];
  534. if (sum2 * sum1 == 0 || e->bitrate == RATE_QUANT) {
  535. memcpy(temp, e->postfilter_residual + ACB_SIZE, length * sizeof(float));
  536. } else {
  537. gamma = sum2 / sum1;
  538. if (gamma < 0.5)
  539. memcpy(temp, e->postfilter_residual + ACB_SIZE, length * sizeof(float));
  540. else {
  541. gamma = FFMIN(gamma, 1.0);
  542. for (i = 0; i < length; i++) {
  543. temp[i] = e->postfilter_residual[ACB_SIZE + i] + gamma *
  544. pfc->ltgain * e->postfilter_residual[ACB_SIZE + i - best];
  545. }
  546. }
  547. }
  548. memcpy(scratch, temp, length * sizeof(float));
  549. memcpy(mem, e->postfilter_iir, FILTER_ORDER * sizeof(float));
  550. synthesis_filter(scratch, wcoef2, mem, length, scratch);
  551. /* Gain computation, TIA/IS-127 5.9.4-2 */
  552. for (i = 0, sum1 = 0, sum2 = 0; i < length; i++) {
  553. sum1 += in[i] * in[i];
  554. sum2 += scratch[i] * scratch[i];
  555. }
  556. gain = sum2 ? sqrt(sum1 / sum2) : 1.0;
  557. for (i = 0; i < length; i++)
  558. temp[i] *= gain;
  559. /* Short term postfilter */
  560. synthesis_filter(temp, wcoef2, e->postfilter_iir, length, out);
  561. memcpy(e->postfilter_residual,
  562. e->postfilter_residual + length, ACB_SIZE * sizeof(float));
  563. }
  564. static void frame_erasure(EVRCContext *e, float *samples)
  565. {
  566. float ilspf[FILTER_ORDER], ilpc[FILTER_ORDER], idelay[NB_SUBFRAMES],
  567. tmp[SUBFRAME_SIZE + 6], f;
  568. int i, j;
  569. for (i = 0; i < FILTER_ORDER; i++) {
  570. if (e->bitrate != RATE_QUANT)
  571. e->lspf[i] = e->prev_lspf[i] * 0.875 + 0.125 * (i + 1) * 0.048;
  572. else
  573. e->lspf[i] = e->prev_lspf[i];
  574. }
  575. if (e->prev_error_flag)
  576. e->avg_acb_gain *= 0.75;
  577. if (e->bitrate == RATE_FULL)
  578. memcpy(e->pitch_back, e->pitch, ACB_SIZE * sizeof(float));
  579. if (e->last_valid_bitrate == RATE_QUANT)
  580. e->bitrate = RATE_QUANT;
  581. else
  582. e->bitrate = RATE_FULL;
  583. if (e->bitrate == RATE_FULL || e->bitrate == RATE_HALF) {
  584. e->pitch_delay = e->prev_pitch_delay;
  585. } else {
  586. float sum = 0;
  587. idelay[0] = idelay[1] = idelay[2] = MIN_DELAY;
  588. for (i = 0; i < NB_SUBFRAMES; i++)
  589. sum += evrc_energy_quant[e->prev_energy_gain][i];
  590. sum /= (float) NB_SUBFRAMES;
  591. sum = pow(10, sum);
  592. for (i = 0; i < NB_SUBFRAMES; i++)
  593. e->energy_vector[i] = sum;
  594. }
  595. if (fabs(e->pitch_delay - e->prev_pitch_delay) > 15)
  596. e->prev_pitch_delay = e->pitch_delay;
  597. for (i = 0; i < NB_SUBFRAMES; i++) {
  598. int subframe_size = subframe_sizes[i];
  599. int pitch_lag;
  600. interpolate_lsp(ilspf, e->lspf, e->prev_lspf, i);
  601. if (e->bitrate != RATE_QUANT) {
  602. if (e->avg_acb_gain < 0.3) {
  603. idelay[0] = estimation_delay[i];
  604. idelay[1] = estimation_delay[i + 1];
  605. idelay[2] = estimation_delay[i + 2];
  606. } else {
  607. interpolate_delay(idelay, e->pitch_delay, e->prev_pitch_delay, i);
  608. }
  609. }
  610. pitch_lag = lrintf((idelay[1] + idelay[0]) / 2.0);
  611. decode_predictor_coeffs(ilspf, ilpc);
  612. if (e->bitrate != RATE_QUANT) {
  613. acb_excitation(e, e->pitch + ACB_SIZE,
  614. e->avg_acb_gain, idelay, subframe_size);
  615. for (j = 0; j < subframe_size; j++)
  616. e->pitch[ACB_SIZE + j] *= e->fade_scale;
  617. e->fade_scale = FFMAX(e->fade_scale - 0.05, 0.0);
  618. } else {
  619. for (j = 0; j < subframe_size; j++)
  620. e->pitch[ACB_SIZE + j] = e->energy_vector[i];
  621. }
  622. memcpy(e->pitch, e->pitch + subframe_size, ACB_SIZE * sizeof(float));
  623. if (e->bitrate != RATE_QUANT && e->avg_acb_gain < 0.4) {
  624. f = 0.1 * e->avg_fcb_gain;
  625. for (j = 0; j < subframe_size; j++)
  626. e->pitch[ACB_SIZE + j] += f;
  627. } else if (e->bitrate == RATE_QUANT) {
  628. for (j = 0; j < subframe_size; j++)
  629. e->pitch[ACB_SIZE + j] = e->energy_vector[i];
  630. }
  631. synthesis_filter(e->pitch + ACB_SIZE, ilpc,
  632. e->synthesis, subframe_size, tmp);
  633. postfilter(e, tmp, ilpc, samples, pitch_lag,
  634. &postfilter_coeffs[e->bitrate], subframe_size);
  635. samples += subframe_size;
  636. }
  637. }
  638. static int evrc_decode_frame(AVCodecContext *avctx, void *data,
  639. int *got_frame_ptr, AVPacket *avpkt)
  640. {
  641. const uint8_t *buf = avpkt->data;
  642. EVRCContext *e = avctx->priv_data;
  643. int buf_size = avpkt->size;
  644. float ilspf[FILTER_ORDER], ilpc[FILTER_ORDER], idelay[NB_SUBFRAMES];
  645. float *samples;
  646. int i, j, ret, error_flag = 0;
  647. e->avframe.nb_samples = 160;
  648. if ((ret = ff_get_buffer(avctx, &e->avframe)) < 0)
  649. return ret;
  650. samples = (float *)e->avframe.data[0];
  651. if ((e->bitrate = determine_bitrate(avctx, &buf_size, &buf)) == RATE_ERRS) {
  652. warn_insufficient_frame_quality(avctx, "bitrate cannot be determined.");
  653. goto erasure;
  654. }
  655. if (e->bitrate <= SILENCE || e->bitrate == RATE_QUARTER)
  656. goto erasure;
  657. if (e->bitrate == RATE_QUANT && e->last_valid_bitrate == RATE_FULL
  658. && !e->prev_error_flag)
  659. goto erasure;
  660. init_get_bits(&e->gb, buf, 8 * buf_size);
  661. memset(&e->frame, 0, sizeof(EVRCAFrame));
  662. unpack_frame(e);
  663. if (e->bitrate != RATE_QUANT) {
  664. uint8_t *p = (uint8_t *) &e->frame;
  665. for (i = 0; i < sizeof(EVRCAFrame); i++) {
  666. if (p[i])
  667. break;
  668. }
  669. if (i == sizeof(EVRCAFrame))
  670. goto erasure;
  671. } else if (e->frame.lsp[0] == e->frame.lsp[1] == 0xf &&
  672. e->frame.energy_gain == 0xff) {
  673. goto erasure;
  674. }
  675. if (decode_lspf(e) < 0)
  676. goto erasure;
  677. if (e->bitrate == RATE_FULL || e->bitrate == RATE_HALF) {
  678. /* Pitch delay parameter checking as per TIA/IS-127 5.1.5.1 */
  679. if (e->frame.pitch_delay > MAX_DELAY - MIN_DELAY)
  680. goto erasure;
  681. e->pitch_delay = e->frame.pitch_delay + MIN_DELAY;
  682. /* Delay diff parameter checking as per TIA/IS-127 5.1.5.2 */
  683. if (e->frame.delay_diff) {
  684. int p = e->pitch_delay - e->frame.delay_diff + 16;
  685. if (p < MIN_DELAY || p > MAX_DELAY)
  686. goto erasure;
  687. }
  688. /* Delay contour reconstruction as per TIA/IS-127 5.2.2.2 */
  689. if (e->frame.delay_diff &&
  690. e->bitrate == RATE_FULL && e->prev_error_flag) {
  691. float delay;
  692. memcpy(e->pitch, e->pitch_back, ACB_SIZE * sizeof(float));
  693. delay = e->prev_pitch_delay;
  694. e->prev_pitch_delay = delay - e->frame.delay_diff + 16.0;
  695. if (fabs(e->pitch_delay - delay) > 15)
  696. delay = e->pitch_delay;
  697. for (i = 0; i < NB_SUBFRAMES; i++) {
  698. int subframe_size = subframe_sizes[i];
  699. interpolate_delay(idelay, delay, e->prev_pitch_delay, i);
  700. acb_excitation(e, e->pitch + ACB_SIZE, e->avg_acb_gain, idelay, subframe_size);
  701. memcpy(e->pitch, e->pitch + subframe_size, ACB_SIZE * sizeof(float));
  702. }
  703. }
  704. /* Smoothing of the decoded delay as per TIA/IS-127 5.2.2.5 */
  705. if (fabs(e->pitch_delay - e->prev_pitch_delay) > 15)
  706. e->prev_pitch_delay = e->pitch_delay;
  707. e->avg_acb_gain = e->avg_fcb_gain = 0.0;
  708. } else {
  709. idelay[0] = idelay[1] = idelay[2] = MIN_DELAY;
  710. /* Decode frame energy vectors as per TIA/IS-127 5.7.2 */
  711. for (i = 0; i < NB_SUBFRAMES; i++)
  712. e->energy_vector[i] = pow(10, evrc_energy_quant[e->frame.energy_gain][i]);
  713. e->prev_energy_gain = e->frame.energy_gain;
  714. }
  715. for (i = 0; i < NB_SUBFRAMES; i++) {
  716. float tmp[SUBFRAME_SIZE + 6] = { 0 };
  717. int subframe_size = subframe_sizes[i];
  718. int pitch_lag;
  719. interpolate_lsp(ilspf, e->lspf, e->prev_lspf, i);
  720. if (e->bitrate != RATE_QUANT)
  721. interpolate_delay(idelay, e->pitch_delay, e->prev_pitch_delay, i);
  722. pitch_lag = lrintf((idelay[1] + idelay[0]) / 2.0);
  723. decode_predictor_coeffs(ilspf, ilpc);
  724. /* Bandwidth expansion as per TIA/IS-127 5.2.3.3 */
  725. if (e->frame.lpc_flag && e->prev_error_flag)
  726. bandwidth_expansion(ilpc, ilpc, 0.75);
  727. if (e->bitrate != RATE_QUANT) {
  728. float acb_sum, f;
  729. f = exp((e->bitrate == RATE_HALF ? 0.5 : 0.25)
  730. * (e->frame.fcb_gain[i] + 1));
  731. acb_sum = pitch_gain_vq[e->frame.acb_gain[i]];
  732. e->avg_acb_gain += acb_sum / NB_SUBFRAMES;
  733. e->avg_fcb_gain += f / NB_SUBFRAMES;
  734. acb_excitation(e, e->pitch + ACB_SIZE,
  735. acb_sum, idelay, subframe_size);
  736. fcb_excitation(e, e->frame.fcb_shape[i], tmp,
  737. acb_sum, pitch_lag, subframe_size);
  738. /* Total excitation generation as per TIA/IS-127 5.2.3.9 */
  739. for (j = 0; j < subframe_size; j++)
  740. e->pitch[ACB_SIZE + j] += f * tmp[j];
  741. e->fade_scale = FFMIN(e->fade_scale + 0.2, 1.0);
  742. } else {
  743. for (j = 0; j < subframe_size; j++)
  744. e->pitch[ACB_SIZE + j] = e->energy_vector[i];
  745. }
  746. memcpy(e->pitch, e->pitch + subframe_size, ACB_SIZE * sizeof(float));
  747. synthesis_filter(e->pitch + ACB_SIZE, ilpc,
  748. e->synthesis, subframe_size, tmp);
  749. postfilter(e, tmp, ilpc, samples, pitch_lag,
  750. &postfilter_coeffs[e->bitrate], subframe_size);
  751. samples += subframe_size;
  752. }
  753. if (error_flag) {
  754. erasure:
  755. error_flag = 1;
  756. av_log(avctx, AV_LOG_WARNING, "frame erasure\n");
  757. frame_erasure(e, samples);
  758. }
  759. memcpy(e->prev_lspf, e->lspf, sizeof(e->prev_lspf));
  760. e->prev_error_flag = error_flag;
  761. e->last_valid_bitrate = e->bitrate;
  762. if (e->bitrate != RATE_QUANT)
  763. e->prev_pitch_delay = e->pitch_delay;
  764. samples = (float *)e->avframe.data[0];
  765. for (i = 0; i < 160; i++)
  766. samples[i] /= 32768;
  767. *got_frame_ptr = 1;
  768. *(AVFrame *)data = e->avframe;
  769. return avpkt->size;
  770. }
  771. AVCodec ff_evrc_decoder = {
  772. .name = "evrc",
  773. .type = AVMEDIA_TYPE_AUDIO,
  774. .id = AV_CODEC_ID_EVRC,
  775. .init = evrc_decode_init,
  776. .decode = evrc_decode_frame,
  777. .capabilities = CODEC_CAP_DR1,
  778. .priv_data_size = sizeof(EVRCContext),
  779. .long_name = NULL_IF_CONFIG_SMALL("EVRC (Enhanced Variable Rate Codec)"),
  780. };