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.

942 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 "libavutil/opt.h"
  28. #include "avcodec.h"
  29. #include "internal.h"
  30. #include "get_bits.h"
  31. #include "evrcdata.h"
  32. #include "acelp_vectors.h"
  33. #include "lsp.h"
  34. #define MIN_LSP_SEP (0.05 / (2.0 * M_PI))
  35. #define MIN_DELAY 20
  36. #define MAX_DELAY 120
  37. #define NB_SUBFRAMES 3
  38. #define SUBFRAME_SIZE 54
  39. #define FILTER_ORDER 10
  40. #define ACB_SIZE 128
  41. typedef enum {
  42. RATE_ERRS = -1,
  43. SILENCE,
  44. RATE_QUANT,
  45. RATE_QUARTER,
  46. RATE_HALF,
  47. RATE_FULL,
  48. } evrc_packet_rate;
  49. /**
  50. * EVRC-A unpacked data frame
  51. */
  52. typedef struct EVRCAFrame {
  53. uint8_t lpc_flag; ///< spectral change indicator
  54. uint16_t lsp[4]; ///< index into LSP codebook
  55. uint8_t pitch_delay; ///< pitch delay for entire frame
  56. uint8_t delay_diff; ///< delay difference for entire frame
  57. uint8_t acb_gain[3]; ///< adaptive codebook gain
  58. uint16_t fcb_shape[3][4]; ///< fixed codebook shape
  59. uint8_t fcb_gain[3]; ///< fixed codebook gain index
  60. uint8_t energy_gain; ///< frame energy gain index
  61. uint8_t tty; ///< tty baud rate bit
  62. } EVRCAFrame;
  63. typedef struct EVRCContext {
  64. AVClass *class;
  65. int postfilter;
  66. GetBitContext gb;
  67. evrc_packet_rate bitrate;
  68. evrc_packet_rate last_valid_bitrate;
  69. EVRCAFrame frame;
  70. float lspf[FILTER_ORDER];
  71. float prev_lspf[FILTER_ORDER];
  72. float synthesis[FILTER_ORDER];
  73. float postfilter_fir[FILTER_ORDER];
  74. float postfilter_iir[FILTER_ORDER];
  75. float postfilter_residual[ACB_SIZE + SUBFRAME_SIZE];
  76. float pitch_delay;
  77. float prev_pitch_delay;
  78. float avg_acb_gain; ///< average adaptive codebook gain
  79. float avg_fcb_gain; ///< average fixed codebook gain
  80. float pitch[ACB_SIZE + FILTER_ORDER + SUBFRAME_SIZE];
  81. float pitch_back[ACB_SIZE];
  82. float interpolation_coeffs[136];
  83. float energy_vector[NB_SUBFRAMES];
  84. float fade_scale;
  85. float last;
  86. uint8_t prev_energy_gain;
  87. uint8_t prev_error_flag;
  88. uint8_t warned_buf_mismatch_bitrate;
  89. } EVRCContext;
  90. /**
  91. * Frame unpacking for RATE_FULL, RATE_HALF and RATE_QUANT
  92. *
  93. * @param e the context
  94. *
  95. * TIA/IS-127 Table 4.21-1
  96. */
  97. static void unpack_frame(EVRCContext *e)
  98. {
  99. EVRCAFrame *frame = &e->frame;
  100. GetBitContext *gb = &e->gb;
  101. switch (e->bitrate) {
  102. case RATE_FULL:
  103. frame->lpc_flag = get_bits1(gb);
  104. frame->lsp[0] = get_bits(gb, 6);
  105. frame->lsp[1] = get_bits(gb, 6);
  106. frame->lsp[2] = get_bits(gb, 9);
  107. frame->lsp[3] = get_bits(gb, 7);
  108. frame->pitch_delay = get_bits(gb, 7);
  109. frame->delay_diff = get_bits(gb, 5);
  110. frame->acb_gain[0] = get_bits(gb, 3);
  111. frame->fcb_shape[0][0] = get_bits(gb, 8);
  112. frame->fcb_shape[0][1] = get_bits(gb, 8);
  113. frame->fcb_shape[0][2] = get_bits(gb, 8);
  114. frame->fcb_shape[0][3] = get_bits(gb, 11);
  115. frame->fcb_gain[0] = get_bits(gb, 5);
  116. frame->acb_gain[1] = get_bits(gb, 3);
  117. frame->fcb_shape[1][0] = get_bits(gb, 8);
  118. frame->fcb_shape[1][1] = get_bits(gb, 8);
  119. frame->fcb_shape[1][2] = get_bits(gb, 8);
  120. frame->fcb_shape[1][3] = get_bits(gb, 11);
  121. frame->fcb_gain [1] = get_bits(gb, 5);
  122. frame->acb_gain [2] = get_bits(gb, 3);
  123. frame->fcb_shape[2][0] = get_bits(gb, 8);
  124. frame->fcb_shape[2][1] = get_bits(gb, 8);
  125. frame->fcb_shape[2][2] = get_bits(gb, 8);
  126. frame->fcb_shape[2][3] = get_bits(gb, 11);
  127. frame->fcb_gain [2] = get_bits(gb, 5);
  128. frame->tty = get_bits1(gb);
  129. break;
  130. case RATE_HALF:
  131. frame->lsp [0] = get_bits(gb, 7);
  132. frame->lsp [1] = get_bits(gb, 7);
  133. frame->lsp [2] = get_bits(gb, 8);
  134. frame->pitch_delay = get_bits(gb, 7);
  135. frame->acb_gain [0] = get_bits(gb, 3);
  136. frame->fcb_shape[0][0] = get_bits(gb, 10);
  137. frame->fcb_gain [0] = get_bits(gb, 4);
  138. frame->acb_gain [1] = get_bits(gb, 3);
  139. frame->fcb_shape[1][0] = get_bits(gb, 10);
  140. frame->fcb_gain [1] = get_bits(gb, 4);
  141. frame->acb_gain [2] = get_bits(gb, 3);
  142. frame->fcb_shape[2][0] = get_bits(gb, 10);
  143. frame->fcb_gain [2] = get_bits(gb, 4);
  144. break;
  145. case RATE_QUANT:
  146. frame->lsp [0] = get_bits(gb, 4);
  147. frame->lsp [1] = get_bits(gb, 4);
  148. frame->energy_gain = get_bits(gb, 8);
  149. break;
  150. }
  151. }
  152. static evrc_packet_rate buf_size2bitrate(const int buf_size)
  153. {
  154. switch (buf_size) {
  155. case 23: return RATE_FULL;
  156. case 11: return RATE_HALF;
  157. case 6: return RATE_QUARTER;
  158. case 3: return RATE_QUANT;
  159. case 1: return SILENCE;
  160. }
  161. return RATE_ERRS;
  162. }
  163. /**
  164. * Determine the bitrate from the frame size and/or the first byte of the frame.
  165. *
  166. * @param avctx the AV codec context
  167. * @param buf_size length of the buffer
  168. * @param buf the bufffer
  169. *
  170. * @return the bitrate on success,
  171. * RATE_ERRS if the bitrate cannot be satisfactorily determined
  172. */
  173. static evrc_packet_rate determine_bitrate(AVCodecContext *avctx,
  174. int *buf_size,
  175. const uint8_t **buf)
  176. {
  177. evrc_packet_rate bitrate;
  178. if ((bitrate = buf_size2bitrate(*buf_size)) >= 0) {
  179. if (bitrate > **buf) {
  180. EVRCContext *e = avctx->priv_data;
  181. if (!e->warned_buf_mismatch_bitrate) {
  182. av_log(avctx, AV_LOG_WARNING,
  183. "Claimed bitrate and buffer size mismatch.\n");
  184. e->warned_buf_mismatch_bitrate = 1;
  185. }
  186. bitrate = **buf;
  187. } else if (bitrate < **buf) {
  188. av_log(avctx, AV_LOG_ERROR,
  189. "Buffer is too small for the claimed bitrate.\n");
  190. return RATE_ERRS;
  191. }
  192. (*buf)++;
  193. *buf_size -= 1;
  194. } else if ((bitrate = buf_size2bitrate(*buf_size + 1)) >= 0) {
  195. av_log(avctx, AV_LOG_DEBUG,
  196. "Bitrate byte is missing, guessing the bitrate from packet size.\n");
  197. } else
  198. return RATE_ERRS;
  199. return bitrate;
  200. }
  201. static void warn_insufficient_frame_quality(AVCodecContext *avctx,
  202. const char *message)
  203. {
  204. av_log(avctx, AV_LOG_WARNING, "Frame #%d, %s\n",
  205. avctx->frame_number, message);
  206. }
  207. /**
  208. * Initialize the speech codec according to the specification.
  209. *
  210. * TIA/IS-127 5.2
  211. */
  212. static av_cold int evrc_decode_init(AVCodecContext *avctx)
  213. {
  214. EVRCContext *e = avctx->priv_data;
  215. int i, n, idx = 0;
  216. float denom = 2.0 / (2.0 * 8.0 + 1.0);
  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 * const *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(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. memmove(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. memmove(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. AVFrame *frame = data;
  643. EVRCContext *e = avctx->priv_data;
  644. int buf_size = avpkt->size;
  645. float ilspf[FILTER_ORDER], ilpc[FILTER_ORDER], idelay[NB_SUBFRAMES];
  646. float *samples;
  647. int i, j, ret, error_flag = 0;
  648. frame->nb_samples = 160;
  649. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
  650. return ret;
  651. samples = (float *)frame->data[0];
  652. if ((e->bitrate = determine_bitrate(avctx, &buf_size, &buf)) == RATE_ERRS) {
  653. warn_insufficient_frame_quality(avctx, "bitrate cannot be determined.");
  654. goto erasure;
  655. }
  656. if (e->bitrate <= SILENCE || e->bitrate == RATE_QUARTER)
  657. goto erasure;
  658. if (e->bitrate == RATE_QUANT && e->last_valid_bitrate == RATE_FULL
  659. && !e->prev_error_flag)
  660. goto erasure;
  661. if ((ret = init_get_bits8(&e->gb, buf, buf_size)) < 0)
  662. return ret;
  663. memset(&e->frame, 0, sizeof(EVRCAFrame));
  664. unpack_frame(e);
  665. if (e->bitrate != RATE_QUANT) {
  666. uint8_t *p = (uint8_t *) &e->frame;
  667. for (i = 0; i < sizeof(EVRCAFrame); i++) {
  668. if (p[i])
  669. break;
  670. }
  671. if (i == sizeof(EVRCAFrame))
  672. goto erasure;
  673. } else if (e->frame.lsp[0] == 0xf &&
  674. e->frame.lsp[1] == 0xf &&
  675. e->frame.energy_gain == 0xff) {
  676. goto erasure;
  677. }
  678. if (decode_lspf(e) < 0)
  679. goto erasure;
  680. if (e->bitrate == RATE_FULL || e->bitrate == RATE_HALF) {
  681. /* Pitch delay parameter checking as per TIA/IS-127 5.1.5.1 */
  682. if (e->frame.pitch_delay > MAX_DELAY - MIN_DELAY)
  683. goto erasure;
  684. e->pitch_delay = e->frame.pitch_delay + MIN_DELAY;
  685. /* Delay diff parameter checking as per TIA/IS-127 5.1.5.2 */
  686. if (e->frame.delay_diff) {
  687. int p = e->pitch_delay - e->frame.delay_diff + 16;
  688. if (p < MIN_DELAY || p > MAX_DELAY)
  689. goto erasure;
  690. }
  691. /* Delay contour reconstruction as per TIA/IS-127 5.2.2.2 */
  692. if (e->frame.delay_diff &&
  693. e->bitrate == RATE_FULL && e->prev_error_flag) {
  694. float delay;
  695. memcpy(e->pitch, e->pitch_back, ACB_SIZE * sizeof(float));
  696. delay = e->prev_pitch_delay;
  697. e->prev_pitch_delay = delay - e->frame.delay_diff + 16.0;
  698. if (fabs(e->pitch_delay - delay) > 15)
  699. delay = e->pitch_delay;
  700. for (i = 0; i < NB_SUBFRAMES; i++) {
  701. int subframe_size = subframe_sizes[i];
  702. interpolate_delay(idelay, delay, e->prev_pitch_delay, i);
  703. acb_excitation(e, e->pitch + ACB_SIZE, e->avg_acb_gain, idelay, subframe_size);
  704. memmove(e->pitch, e->pitch + subframe_size, ACB_SIZE * sizeof(float));
  705. }
  706. }
  707. /* Smoothing of the decoded delay as per TIA/IS-127 5.2.2.5 */
  708. if (fabs(e->pitch_delay - e->prev_pitch_delay) > 15)
  709. e->prev_pitch_delay = e->pitch_delay;
  710. e->avg_acb_gain = e->avg_fcb_gain = 0.0;
  711. } else {
  712. idelay[0] = idelay[1] = idelay[2] = MIN_DELAY;
  713. /* Decode frame energy vectors as per TIA/IS-127 5.7.2 */
  714. for (i = 0; i < NB_SUBFRAMES; i++)
  715. e->energy_vector[i] = pow(10, evrc_energy_quant[e->frame.energy_gain][i]);
  716. e->prev_energy_gain = e->frame.energy_gain;
  717. }
  718. for (i = 0; i < NB_SUBFRAMES; i++) {
  719. float tmp[SUBFRAME_SIZE + 6] = { 0 };
  720. int subframe_size = subframe_sizes[i];
  721. int pitch_lag;
  722. interpolate_lsp(ilspf, e->lspf, e->prev_lspf, i);
  723. if (e->bitrate != RATE_QUANT)
  724. interpolate_delay(idelay, e->pitch_delay, e->prev_pitch_delay, i);
  725. pitch_lag = lrintf((idelay[1] + idelay[0]) / 2.0);
  726. decode_predictor_coeffs(ilspf, ilpc);
  727. /* Bandwidth expansion as per TIA/IS-127 5.2.3.3 */
  728. if (e->frame.lpc_flag && e->prev_error_flag)
  729. bandwidth_expansion(ilpc, ilpc, 0.75);
  730. if (e->bitrate != RATE_QUANT) {
  731. float acb_sum, f;
  732. f = exp((e->bitrate == RATE_HALF ? 0.5 : 0.25)
  733. * (e->frame.fcb_gain[i] + 1));
  734. acb_sum = pitch_gain_vq[e->frame.acb_gain[i]];
  735. e->avg_acb_gain += acb_sum / NB_SUBFRAMES;
  736. e->avg_fcb_gain += f / NB_SUBFRAMES;
  737. acb_excitation(e, e->pitch + ACB_SIZE,
  738. acb_sum, idelay, subframe_size);
  739. fcb_excitation(e, e->frame.fcb_shape[i], tmp,
  740. acb_sum, pitch_lag, subframe_size);
  741. /* Total excitation generation as per TIA/IS-127 5.2.3.9 */
  742. for (j = 0; j < subframe_size; j++)
  743. e->pitch[ACB_SIZE + j] += f * tmp[j];
  744. e->fade_scale = FFMIN(e->fade_scale + 0.2, 1.0);
  745. } else {
  746. for (j = 0; j < subframe_size; j++)
  747. e->pitch[ACB_SIZE + j] = e->energy_vector[i];
  748. }
  749. memmove(e->pitch, e->pitch + subframe_size, ACB_SIZE * sizeof(float));
  750. synthesis_filter(e->pitch + ACB_SIZE, ilpc,
  751. e->synthesis, subframe_size,
  752. e->postfilter ? tmp : samples);
  753. if (e->postfilter)
  754. postfilter(e, tmp, ilpc, samples, pitch_lag,
  755. &postfilter_coeffs[e->bitrate], subframe_size);
  756. samples += subframe_size;
  757. }
  758. if (error_flag) {
  759. erasure:
  760. error_flag = 1;
  761. av_log(avctx, AV_LOG_WARNING, "frame erasure\n");
  762. frame_erasure(e, samples);
  763. }
  764. memcpy(e->prev_lspf, e->lspf, sizeof(e->prev_lspf));
  765. e->prev_error_flag = error_flag;
  766. e->last_valid_bitrate = e->bitrate;
  767. if (e->bitrate != RATE_QUANT)
  768. e->prev_pitch_delay = e->pitch_delay;
  769. samples = (float *)frame->data[0];
  770. for (i = 0; i < 160; i++)
  771. samples[i] /= 32768;
  772. *got_frame_ptr = 1;
  773. return avpkt->size;
  774. }
  775. #define OFFSET(x) offsetof(EVRCContext, x)
  776. #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  777. static const AVOption options[] = {
  778. { "postfilter", "enable postfilter", OFFSET(postfilter), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AD },
  779. { NULL }
  780. };
  781. static const AVClass evrcdec_class = {
  782. .class_name = "evrc",
  783. .item_name = av_default_item_name,
  784. .option = options,
  785. .version = LIBAVUTIL_VERSION_INT,
  786. };
  787. AVCodec ff_evrc_decoder = {
  788. .name = "evrc",
  789. .long_name = NULL_IF_CONFIG_SMALL("EVRC (Enhanced Variable Rate Codec)"),
  790. .type = AVMEDIA_TYPE_AUDIO,
  791. .id = AV_CODEC_ID_EVRC,
  792. .init = evrc_decode_init,
  793. .decode = evrc_decode_frame,
  794. .capabilities = AV_CODEC_CAP_DR1,
  795. .priv_data_size = sizeof(EVRCContext),
  796. .priv_class = &evrcdec_class,
  797. };