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.

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