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.

1379 lines
42KB

  1. /*
  2. * G.723.1 compatible decoder
  3. * Copyright (c) 2006 Benjamin Larsson
  4. * Copyright (c) 2010 Mohamed Naufal Basheer
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * G.723.1 compatible decoder
  25. */
  26. #define BITSTREAM_READER_LE
  27. #include "libavutil/channel_layout.h"
  28. #include "libavutil/mem.h"
  29. #include "libavutil/opt.h"
  30. #include "avcodec.h"
  31. #include "get_bits.h"
  32. #include "acelp_vectors.h"
  33. #include "celp_filters.h"
  34. #include "g723_1_data.h"
  35. #include "internal.h"
  36. #define CNG_RANDOM_SEED 12345
  37. /**
  38. * G723.1 frame types
  39. */
  40. enum FrameType {
  41. ACTIVE_FRAME, ///< Active speech
  42. SID_FRAME, ///< Silence Insertion Descriptor frame
  43. UNTRANSMITTED_FRAME
  44. };
  45. enum Rate {
  46. RATE_6300,
  47. RATE_5300
  48. };
  49. /**
  50. * G723.1 unpacked data subframe
  51. */
  52. typedef struct {
  53. int ad_cb_lag; ///< adaptive codebook lag
  54. int ad_cb_gain;
  55. int dirac_train;
  56. int pulse_sign;
  57. int grid_index;
  58. int amp_index;
  59. int pulse_pos;
  60. } G723_1_Subframe;
  61. /**
  62. * Pitch postfilter parameters
  63. */
  64. typedef struct {
  65. int index; ///< postfilter backward/forward lag
  66. int16_t opt_gain; ///< optimal gain
  67. int16_t sc_gain; ///< scaling gain
  68. } PPFParam;
  69. typedef struct g723_1_context {
  70. AVClass *class;
  71. G723_1_Subframe subframe[4];
  72. enum FrameType cur_frame_type;
  73. enum FrameType past_frame_type;
  74. enum Rate cur_rate;
  75. uint8_t lsp_index[LSP_BANDS];
  76. int pitch_lag[2];
  77. int erased_frames;
  78. int16_t prev_lsp[LPC_ORDER];
  79. int16_t sid_lsp[LPC_ORDER];
  80. int16_t prev_excitation[PITCH_MAX];
  81. int16_t excitation[PITCH_MAX + FRAME_LEN + 4];
  82. int16_t synth_mem[LPC_ORDER];
  83. int16_t fir_mem[LPC_ORDER];
  84. int iir_mem[LPC_ORDER];
  85. int random_seed;
  86. int cng_random_seed;
  87. int interp_index;
  88. int interp_gain;
  89. int sid_gain;
  90. int cur_gain;
  91. int reflection_coef;
  92. int pf_gain;
  93. int postfilter;
  94. int16_t audio[FRAME_LEN + LPC_ORDER + PITCH_MAX + 4];
  95. } G723_1_Context;
  96. static av_cold int g723_1_decode_init(AVCodecContext *avctx)
  97. {
  98. G723_1_Context *p = avctx->priv_data;
  99. avctx->channel_layout = AV_CH_LAYOUT_MONO;
  100. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  101. avctx->channels = 1;
  102. avctx->sample_rate = 8000;
  103. p->pf_gain = 1 << 12;
  104. memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(*p->prev_lsp));
  105. memcpy(p->sid_lsp, dc_lsp, LPC_ORDER * sizeof(*p->sid_lsp));
  106. p->cng_random_seed = CNG_RANDOM_SEED;
  107. p->past_frame_type = SID_FRAME;
  108. return 0;
  109. }
  110. /**
  111. * Unpack the frame into parameters.
  112. *
  113. * @param p the context
  114. * @param buf pointer to the input buffer
  115. * @param buf_size size of the input buffer
  116. */
  117. static int unpack_bitstream(G723_1_Context *p, const uint8_t *buf,
  118. int buf_size)
  119. {
  120. GetBitContext gb;
  121. int ad_cb_len;
  122. int temp, info_bits, i;
  123. init_get_bits(&gb, buf, buf_size * 8);
  124. /* Extract frame type and rate info */
  125. info_bits = get_bits(&gb, 2);
  126. if (info_bits == 3) {
  127. p->cur_frame_type = UNTRANSMITTED_FRAME;
  128. return 0;
  129. }
  130. /* Extract 24 bit lsp indices, 8 bit for each band */
  131. p->lsp_index[2] = get_bits(&gb, 8);
  132. p->lsp_index[1] = get_bits(&gb, 8);
  133. p->lsp_index[0] = get_bits(&gb, 8);
  134. if (info_bits == 2) {
  135. p->cur_frame_type = SID_FRAME;
  136. p->subframe[0].amp_index = get_bits(&gb, 6);
  137. return 0;
  138. }
  139. /* Extract the info common to both rates */
  140. p->cur_rate = info_bits ? RATE_5300 : RATE_6300;
  141. p->cur_frame_type = ACTIVE_FRAME;
  142. p->pitch_lag[0] = get_bits(&gb, 7);
  143. if (p->pitch_lag[0] > 123) /* test if forbidden code */
  144. return -1;
  145. p->pitch_lag[0] += PITCH_MIN;
  146. p->subframe[1].ad_cb_lag = get_bits(&gb, 2);
  147. p->pitch_lag[1] = get_bits(&gb, 7);
  148. if (p->pitch_lag[1] > 123)
  149. return -1;
  150. p->pitch_lag[1] += PITCH_MIN;
  151. p->subframe[3].ad_cb_lag = get_bits(&gb, 2);
  152. p->subframe[0].ad_cb_lag = 1;
  153. p->subframe[2].ad_cb_lag = 1;
  154. for (i = 0; i < SUBFRAMES; i++) {
  155. /* Extract combined gain */
  156. temp = get_bits(&gb, 12);
  157. ad_cb_len = 170;
  158. p->subframe[i].dirac_train = 0;
  159. if (p->cur_rate == RATE_6300 && p->pitch_lag[i >> 1] < SUBFRAME_LEN - 2) {
  160. p->subframe[i].dirac_train = temp >> 11;
  161. temp &= 0x7FF;
  162. ad_cb_len = 85;
  163. }
  164. p->subframe[i].ad_cb_gain = FASTDIV(temp, GAIN_LEVELS);
  165. if (p->subframe[i].ad_cb_gain < ad_cb_len) {
  166. p->subframe[i].amp_index = temp - p->subframe[i].ad_cb_gain *
  167. GAIN_LEVELS;
  168. } else {
  169. return -1;
  170. }
  171. }
  172. p->subframe[0].grid_index = get_bits(&gb, 1);
  173. p->subframe[1].grid_index = get_bits(&gb, 1);
  174. p->subframe[2].grid_index = get_bits(&gb, 1);
  175. p->subframe[3].grid_index = get_bits(&gb, 1);
  176. if (p->cur_rate == RATE_6300) {
  177. skip_bits(&gb, 1); /* skip reserved bit */
  178. /* Compute pulse_pos index using the 13-bit combined position index */
  179. temp = get_bits(&gb, 13);
  180. p->subframe[0].pulse_pos = temp / 810;
  181. temp -= p->subframe[0].pulse_pos * 810;
  182. p->subframe[1].pulse_pos = FASTDIV(temp, 90);
  183. temp -= p->subframe[1].pulse_pos * 90;
  184. p->subframe[2].pulse_pos = FASTDIV(temp, 9);
  185. p->subframe[3].pulse_pos = temp - p->subframe[2].pulse_pos * 9;
  186. p->subframe[0].pulse_pos = (p->subframe[0].pulse_pos << 16) +
  187. get_bits(&gb, 16);
  188. p->subframe[1].pulse_pos = (p->subframe[1].pulse_pos << 14) +
  189. get_bits(&gb, 14);
  190. p->subframe[2].pulse_pos = (p->subframe[2].pulse_pos << 16) +
  191. get_bits(&gb, 16);
  192. p->subframe[3].pulse_pos = (p->subframe[3].pulse_pos << 14) +
  193. get_bits(&gb, 14);
  194. p->subframe[0].pulse_sign = get_bits(&gb, 6);
  195. p->subframe[1].pulse_sign = get_bits(&gb, 5);
  196. p->subframe[2].pulse_sign = get_bits(&gb, 6);
  197. p->subframe[3].pulse_sign = get_bits(&gb, 5);
  198. } else { /* 5300 bps */
  199. p->subframe[0].pulse_pos = get_bits(&gb, 12);
  200. p->subframe[1].pulse_pos = get_bits(&gb, 12);
  201. p->subframe[2].pulse_pos = get_bits(&gb, 12);
  202. p->subframe[3].pulse_pos = get_bits(&gb, 12);
  203. p->subframe[0].pulse_sign = get_bits(&gb, 4);
  204. p->subframe[1].pulse_sign = get_bits(&gb, 4);
  205. p->subframe[2].pulse_sign = get_bits(&gb, 4);
  206. p->subframe[3].pulse_sign = get_bits(&gb, 4);
  207. }
  208. return 0;
  209. }
  210. /**
  211. * Bitexact implementation of sqrt(val/2).
  212. */
  213. static int16_t square_root(int val)
  214. {
  215. int16_t res = 0;
  216. int16_t exp = 0x4000;
  217. int i;
  218. for (i = 0; i < 14; i ++) {
  219. int res_exp = res + exp;
  220. if (val >= res_exp * res_exp << 1)
  221. res += exp;
  222. exp >>= 1;
  223. }
  224. return res;
  225. }
  226. /**
  227. * Calculate the number of left-shifts required for normalizing the input.
  228. *
  229. * @param num input number
  230. * @param width width of the input, 16 bits(0) / 32 bits(1)
  231. */
  232. static int normalize_bits(int num, int width)
  233. {
  234. return width - av_log2(num) - 1;
  235. }
  236. /**
  237. * Scale vector contents based on the largest of their absolutes.
  238. */
  239. static int scale_vector(int16_t *dst, const int16_t *vector, int length)
  240. {
  241. int bits, max = 0;
  242. int i;
  243. for (i = 0; i < length; i++)
  244. max |= FFABS(vector[i]);
  245. max = FFMIN(max, 0x7FFF);
  246. bits = normalize_bits(max, 15);
  247. for (i = 0; i < length; i++)
  248. dst[i] = vector[i] << bits >> 3;
  249. return bits - 3;
  250. }
  251. /**
  252. * Perform inverse quantization of LSP frequencies.
  253. *
  254. * @param cur_lsp the current LSP vector
  255. * @param prev_lsp the previous LSP vector
  256. * @param lsp_index VQ indices
  257. * @param bad_frame bad frame flag
  258. */
  259. static void inverse_quant(int16_t *cur_lsp, int16_t *prev_lsp,
  260. uint8_t *lsp_index, int bad_frame)
  261. {
  262. int min_dist, pred;
  263. int i, j, temp, stable;
  264. /* Check for frame erasure */
  265. if (!bad_frame) {
  266. min_dist = 0x100;
  267. pred = 12288;
  268. } else {
  269. min_dist = 0x200;
  270. pred = 23552;
  271. lsp_index[0] = lsp_index[1] = lsp_index[2] = 0;
  272. }
  273. /* Get the VQ table entry corresponding to the transmitted index */
  274. cur_lsp[0] = lsp_band0[lsp_index[0]][0];
  275. cur_lsp[1] = lsp_band0[lsp_index[0]][1];
  276. cur_lsp[2] = lsp_band0[lsp_index[0]][2];
  277. cur_lsp[3] = lsp_band1[lsp_index[1]][0];
  278. cur_lsp[4] = lsp_band1[lsp_index[1]][1];
  279. cur_lsp[5] = lsp_band1[lsp_index[1]][2];
  280. cur_lsp[6] = lsp_band2[lsp_index[2]][0];
  281. cur_lsp[7] = lsp_band2[lsp_index[2]][1];
  282. cur_lsp[8] = lsp_band2[lsp_index[2]][2];
  283. cur_lsp[9] = lsp_band2[lsp_index[2]][3];
  284. /* Add predicted vector & DC component to the previously quantized vector */
  285. for (i = 0; i < LPC_ORDER; i++) {
  286. temp = ((prev_lsp[i] - dc_lsp[i]) * pred + (1 << 14)) >> 15;
  287. cur_lsp[i] += dc_lsp[i] + temp;
  288. }
  289. for (i = 0; i < LPC_ORDER; i++) {
  290. cur_lsp[0] = FFMAX(cur_lsp[0], 0x180);
  291. cur_lsp[LPC_ORDER - 1] = FFMIN(cur_lsp[LPC_ORDER - 1], 0x7e00);
  292. /* Stability check */
  293. for (j = 1; j < LPC_ORDER; j++) {
  294. temp = min_dist + cur_lsp[j - 1] - cur_lsp[j];
  295. if (temp > 0) {
  296. temp >>= 1;
  297. cur_lsp[j - 1] -= temp;
  298. cur_lsp[j] += temp;
  299. }
  300. }
  301. stable = 1;
  302. for (j = 1; j < LPC_ORDER; j++) {
  303. temp = cur_lsp[j - 1] + min_dist - cur_lsp[j] - 4;
  304. if (temp > 0) {
  305. stable = 0;
  306. break;
  307. }
  308. }
  309. if (stable)
  310. break;
  311. }
  312. if (!stable)
  313. memcpy(cur_lsp, prev_lsp, LPC_ORDER * sizeof(*cur_lsp));
  314. }
  315. /**
  316. * Bitexact implementation of 2ab scaled by 1/2^16.
  317. *
  318. * @param a 32 bit multiplicand
  319. * @param b 16 bit multiplier
  320. */
  321. #define MULL2(a, b) \
  322. ((((a) >> 16) * (b) << 1) + (((a) & 0xffff) * (b) >> 15))
  323. /**
  324. * Convert LSP frequencies to LPC coefficients.
  325. *
  326. * @param lpc buffer for LPC coefficients
  327. */
  328. static void lsp2lpc(int16_t *lpc)
  329. {
  330. int f1[LPC_ORDER / 2 + 1];
  331. int f2[LPC_ORDER / 2 + 1];
  332. int i, j;
  333. /* Calculate negative cosine */
  334. for (j = 0; j < LPC_ORDER; j++) {
  335. int index = lpc[j] >> 7;
  336. int offset = lpc[j] & 0x7f;
  337. int temp1 = cos_tab[index] << 16;
  338. int temp2 = (cos_tab[index + 1] - cos_tab[index]) *
  339. ((offset << 8) + 0x80) << 1;
  340. lpc[j] = -(av_sat_dadd32(1 << 15, temp1 + temp2) >> 16);
  341. }
  342. /*
  343. * Compute sum and difference polynomial coefficients
  344. * (bitexact alternative to lsp2poly() in lsp.c)
  345. */
  346. /* Initialize with values in Q28 */
  347. f1[0] = 1 << 28;
  348. f1[1] = (lpc[0] << 14) + (lpc[2] << 14);
  349. f1[2] = lpc[0] * lpc[2] + (2 << 28);
  350. f2[0] = 1 << 28;
  351. f2[1] = (lpc[1] << 14) + (lpc[3] << 14);
  352. f2[2] = lpc[1] * lpc[3] + (2 << 28);
  353. /*
  354. * Calculate and scale the coefficients by 1/2 in
  355. * each iteration for a final scaling factor of Q25
  356. */
  357. for (i = 2; i < LPC_ORDER / 2; i++) {
  358. f1[i + 1] = f1[i - 1] + MULL2(f1[i], lpc[2 * i]);
  359. f2[i + 1] = f2[i - 1] + MULL2(f2[i], lpc[2 * i + 1]);
  360. for (j = i; j >= 2; j--) {
  361. f1[j] = MULL2(f1[j - 1], lpc[2 * i]) +
  362. (f1[j] >> 1) + (f1[j - 2] >> 1);
  363. f2[j] = MULL2(f2[j - 1], lpc[2 * i + 1]) +
  364. (f2[j] >> 1) + (f2[j - 2] >> 1);
  365. }
  366. f1[0] >>= 1;
  367. f2[0] >>= 1;
  368. f1[1] = ((lpc[2 * i] << 16 >> i) + f1[1]) >> 1;
  369. f2[1] = ((lpc[2 * i + 1] << 16 >> i) + f2[1]) >> 1;
  370. }
  371. /* Convert polynomial coefficients to LPC coefficients */
  372. for (i = 0; i < LPC_ORDER / 2; i++) {
  373. int64_t ff1 = f1[i + 1] + f1[i];
  374. int64_t ff2 = f2[i + 1] - f2[i];
  375. lpc[i] = av_clipl_int32(((ff1 + ff2) << 3) + (1 << 15)) >> 16;
  376. lpc[LPC_ORDER - i - 1] = av_clipl_int32(((ff1 - ff2) << 3) +
  377. (1 << 15)) >> 16;
  378. }
  379. }
  380. /**
  381. * Quantize LSP frequencies by interpolation and convert them to
  382. * the corresponding LPC coefficients.
  383. *
  384. * @param lpc buffer for LPC coefficients
  385. * @param cur_lsp the current LSP vector
  386. * @param prev_lsp the previous LSP vector
  387. */
  388. static void lsp_interpolate(int16_t *lpc, int16_t *cur_lsp, int16_t *prev_lsp)
  389. {
  390. int i;
  391. int16_t *lpc_ptr = lpc;
  392. /* cur_lsp * 0.25 + prev_lsp * 0.75 */
  393. ff_acelp_weighted_vector_sum(lpc, cur_lsp, prev_lsp,
  394. 4096, 12288, 1 << 13, 14, LPC_ORDER);
  395. ff_acelp_weighted_vector_sum(lpc + LPC_ORDER, cur_lsp, prev_lsp,
  396. 8192, 8192, 1 << 13, 14, LPC_ORDER);
  397. ff_acelp_weighted_vector_sum(lpc + 2 * LPC_ORDER, cur_lsp, prev_lsp,
  398. 12288, 4096, 1 << 13, 14, LPC_ORDER);
  399. memcpy(lpc + 3 * LPC_ORDER, cur_lsp, LPC_ORDER * sizeof(*lpc));
  400. for (i = 0; i < SUBFRAMES; i++) {
  401. lsp2lpc(lpc_ptr);
  402. lpc_ptr += LPC_ORDER;
  403. }
  404. }
  405. /**
  406. * Generate a train of dirac functions with period as pitch lag.
  407. */
  408. static void gen_dirac_train(int16_t *buf, int pitch_lag)
  409. {
  410. int16_t vector[SUBFRAME_LEN];
  411. int i, j;
  412. memcpy(vector, buf, SUBFRAME_LEN * sizeof(*vector));
  413. for (i = pitch_lag; i < SUBFRAME_LEN; i += pitch_lag) {
  414. for (j = 0; j < SUBFRAME_LEN - i; j++)
  415. buf[i + j] += vector[j];
  416. }
  417. }
  418. /**
  419. * Generate fixed codebook excitation vector.
  420. *
  421. * @param vector decoded excitation vector
  422. * @param subfrm current subframe
  423. * @param cur_rate current bitrate
  424. * @param pitch_lag closed loop pitch lag
  425. * @param index current subframe index
  426. */
  427. static void gen_fcb_excitation(int16_t *vector, G723_1_Subframe *subfrm,
  428. enum Rate cur_rate, int pitch_lag, int index)
  429. {
  430. int temp, i, j;
  431. memset(vector, 0, SUBFRAME_LEN * sizeof(*vector));
  432. if (cur_rate == RATE_6300) {
  433. if (subfrm->pulse_pos >= max_pos[index])
  434. return;
  435. /* Decode amplitudes and positions */
  436. j = PULSE_MAX - pulses[index];
  437. temp = subfrm->pulse_pos;
  438. for (i = 0; i < SUBFRAME_LEN / GRID_SIZE; i++) {
  439. temp -= combinatorial_table[j][i];
  440. if (temp >= 0)
  441. continue;
  442. temp += combinatorial_table[j++][i];
  443. if (subfrm->pulse_sign & (1 << (PULSE_MAX - j))) {
  444. vector[subfrm->grid_index + GRID_SIZE * i] =
  445. -fixed_cb_gain[subfrm->amp_index];
  446. } else {
  447. vector[subfrm->grid_index + GRID_SIZE * i] =
  448. fixed_cb_gain[subfrm->amp_index];
  449. }
  450. if (j == PULSE_MAX)
  451. break;
  452. }
  453. if (subfrm->dirac_train == 1)
  454. gen_dirac_train(vector, pitch_lag);
  455. } else { /* 5300 bps */
  456. int cb_gain = fixed_cb_gain[subfrm->amp_index];
  457. int cb_shift = subfrm->grid_index;
  458. int cb_sign = subfrm->pulse_sign;
  459. int cb_pos = subfrm->pulse_pos;
  460. int offset, beta, lag;
  461. for (i = 0; i < 8; i += 2) {
  462. offset = ((cb_pos & 7) << 3) + cb_shift + i;
  463. vector[offset] = (cb_sign & 1) ? cb_gain : -cb_gain;
  464. cb_pos >>= 3;
  465. cb_sign >>= 1;
  466. }
  467. /* Enhance harmonic components */
  468. lag = pitch_contrib[subfrm->ad_cb_gain << 1] + pitch_lag +
  469. subfrm->ad_cb_lag - 1;
  470. beta = pitch_contrib[(subfrm->ad_cb_gain << 1) + 1];
  471. if (lag < SUBFRAME_LEN - 2) {
  472. for (i = lag; i < SUBFRAME_LEN; i++)
  473. vector[i] += beta * vector[i - lag] >> 15;
  474. }
  475. }
  476. }
  477. /**
  478. * Get delayed contribution from the previous excitation vector.
  479. */
  480. static void get_residual(int16_t *residual, int16_t *prev_excitation, int lag)
  481. {
  482. int offset = PITCH_MAX - PITCH_ORDER / 2 - lag;
  483. int i;
  484. residual[0] = prev_excitation[offset];
  485. residual[1] = prev_excitation[offset + 1];
  486. offset += 2;
  487. for (i = 2; i < SUBFRAME_LEN + PITCH_ORDER - 1; i++)
  488. residual[i] = prev_excitation[offset + (i - 2) % lag];
  489. }
  490. static int dot_product(const int16_t *a, const int16_t *b, int length)
  491. {
  492. int i, sum = 0;
  493. for (i = 0; i < length; i++) {
  494. int prod = a[i] * b[i];
  495. sum = av_sat_dadd32(sum, prod);
  496. }
  497. return sum;
  498. }
  499. /**
  500. * Generate adaptive codebook excitation.
  501. */
  502. static void gen_acb_excitation(int16_t *vector, int16_t *prev_excitation,
  503. int pitch_lag, G723_1_Subframe *subfrm,
  504. enum Rate cur_rate)
  505. {
  506. int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
  507. const int16_t *cb_ptr;
  508. int lag = pitch_lag + subfrm->ad_cb_lag - 1;
  509. int i;
  510. int sum;
  511. get_residual(residual, prev_excitation, lag);
  512. /* Select quantization table */
  513. if (cur_rate == RATE_6300 && pitch_lag < SUBFRAME_LEN - 2)
  514. cb_ptr = adaptive_cb_gain85;
  515. else
  516. cb_ptr = adaptive_cb_gain170;
  517. /* Calculate adaptive vector */
  518. cb_ptr += subfrm->ad_cb_gain * 20;
  519. for (i = 0; i < SUBFRAME_LEN; i++) {
  520. sum = dot_product(residual + i, cb_ptr, PITCH_ORDER);
  521. vector[i] = av_sat_dadd32(1 << 15, sum) >> 16;
  522. }
  523. }
  524. /**
  525. * Estimate maximum auto-correlation around pitch lag.
  526. *
  527. * @param buf buffer with offset applied
  528. * @param offset offset of the excitation vector
  529. * @param ccr_max pointer to the maximum auto-correlation
  530. * @param pitch_lag decoded pitch lag
  531. * @param length length of autocorrelation
  532. * @param dir forward lag(1) / backward lag(-1)
  533. */
  534. static int autocorr_max(const int16_t *buf, int offset, int *ccr_max,
  535. int pitch_lag, int length, int dir)
  536. {
  537. int limit, ccr, lag = 0;
  538. int i;
  539. pitch_lag = FFMIN(PITCH_MAX - 3, pitch_lag);
  540. if (dir > 0)
  541. limit = FFMIN(FRAME_LEN + PITCH_MAX - offset - length, pitch_lag + 3);
  542. else
  543. limit = pitch_lag + 3;
  544. for (i = pitch_lag - 3; i <= limit; i++) {
  545. ccr = dot_product(buf, buf + dir * i, length);
  546. if (ccr > *ccr_max) {
  547. *ccr_max = ccr;
  548. lag = i;
  549. }
  550. }
  551. return lag;
  552. }
  553. /**
  554. * Calculate pitch postfilter optimal and scaling gains.
  555. *
  556. * @param lag pitch postfilter forward/backward lag
  557. * @param ppf pitch postfilter parameters
  558. * @param cur_rate current bitrate
  559. * @param tgt_eng target energy
  560. * @param ccr cross-correlation
  561. * @param res_eng residual energy
  562. */
  563. static void comp_ppf_gains(int lag, PPFParam *ppf, enum Rate cur_rate,
  564. int tgt_eng, int ccr, int res_eng)
  565. {
  566. int pf_residual; /* square of postfiltered residual */
  567. int temp1, temp2;
  568. ppf->index = lag;
  569. temp1 = tgt_eng * res_eng >> 1;
  570. temp2 = ccr * ccr << 1;
  571. if (temp2 > temp1) {
  572. if (ccr >= res_eng) {
  573. ppf->opt_gain = ppf_gain_weight[cur_rate];
  574. } else {
  575. ppf->opt_gain = (ccr << 15) / res_eng *
  576. ppf_gain_weight[cur_rate] >> 15;
  577. }
  578. /* pf_res^2 = tgt_eng + 2*ccr*gain + res_eng*gain^2 */
  579. temp1 = (tgt_eng << 15) + (ccr * ppf->opt_gain << 1);
  580. temp2 = (ppf->opt_gain * ppf->opt_gain >> 15) * res_eng;
  581. pf_residual = av_sat_add32(temp1, temp2 + (1 << 15)) >> 16;
  582. if (tgt_eng >= pf_residual << 1) {
  583. temp1 = 0x7fff;
  584. } else {
  585. temp1 = (tgt_eng << 14) / pf_residual;
  586. }
  587. /* scaling_gain = sqrt(tgt_eng/pf_res^2) */
  588. ppf->sc_gain = square_root(temp1 << 16);
  589. } else {
  590. ppf->opt_gain = 0;
  591. ppf->sc_gain = 0x7fff;
  592. }
  593. ppf->opt_gain = av_clip_int16(ppf->opt_gain * ppf->sc_gain >> 15);
  594. }
  595. /**
  596. * Calculate pitch postfilter parameters.
  597. *
  598. * @param p the context
  599. * @param offset offset of the excitation vector
  600. * @param pitch_lag decoded pitch lag
  601. * @param ppf pitch postfilter parameters
  602. * @param cur_rate current bitrate
  603. */
  604. static void comp_ppf_coeff(G723_1_Context *p, int offset, int pitch_lag,
  605. PPFParam *ppf, enum Rate cur_rate)
  606. {
  607. int16_t scale;
  608. int i;
  609. int temp1, temp2;
  610. /*
  611. * 0 - target energy
  612. * 1 - forward cross-correlation
  613. * 2 - forward residual energy
  614. * 3 - backward cross-correlation
  615. * 4 - backward residual energy
  616. */
  617. int energy[5] = {0, 0, 0, 0, 0};
  618. int16_t *buf = p->audio + LPC_ORDER + offset;
  619. int fwd_lag = autocorr_max(buf, offset, &energy[1], pitch_lag,
  620. SUBFRAME_LEN, 1);
  621. int back_lag = autocorr_max(buf, offset, &energy[3], pitch_lag,
  622. SUBFRAME_LEN, -1);
  623. ppf->index = 0;
  624. ppf->opt_gain = 0;
  625. ppf->sc_gain = 0x7fff;
  626. /* Case 0, Section 3.6 */
  627. if (!back_lag && !fwd_lag)
  628. return;
  629. /* Compute target energy */
  630. energy[0] = dot_product(buf, buf, SUBFRAME_LEN);
  631. /* Compute forward residual energy */
  632. if (fwd_lag)
  633. energy[2] = dot_product(buf + fwd_lag, buf + fwd_lag, SUBFRAME_LEN);
  634. /* Compute backward residual energy */
  635. if (back_lag)
  636. energy[4] = dot_product(buf - back_lag, buf - back_lag, SUBFRAME_LEN);
  637. /* Normalize and shorten */
  638. temp1 = 0;
  639. for (i = 0; i < 5; i++)
  640. temp1 = FFMAX(energy[i], temp1);
  641. scale = normalize_bits(temp1, 31);
  642. for (i = 0; i < 5; i++)
  643. energy[i] = (energy[i] << scale) >> 16;
  644. if (fwd_lag && !back_lag) { /* Case 1 */
  645. comp_ppf_gains(fwd_lag, ppf, cur_rate, energy[0], energy[1],
  646. energy[2]);
  647. } else if (!fwd_lag) { /* Case 2 */
  648. comp_ppf_gains(-back_lag, ppf, cur_rate, energy[0], energy[3],
  649. energy[4]);
  650. } else { /* Case 3 */
  651. /*
  652. * Select the largest of energy[1]^2/energy[2]
  653. * and energy[3]^2/energy[4]
  654. */
  655. temp1 = energy[4] * ((energy[1] * energy[1] + (1 << 14)) >> 15);
  656. temp2 = energy[2] * ((energy[3] * energy[3] + (1 << 14)) >> 15);
  657. if (temp1 >= temp2) {
  658. comp_ppf_gains(fwd_lag, ppf, cur_rate, energy[0], energy[1],
  659. energy[2]);
  660. } else {
  661. comp_ppf_gains(-back_lag, ppf, cur_rate, energy[0], energy[3],
  662. energy[4]);
  663. }
  664. }
  665. }
  666. /**
  667. * Classify frames as voiced/unvoiced.
  668. *
  669. * @param p the context
  670. * @param pitch_lag decoded pitch_lag
  671. * @param exc_eng excitation energy estimation
  672. * @param scale scaling factor of exc_eng
  673. *
  674. * @return residual interpolation index if voiced, 0 otherwise
  675. */
  676. static int comp_interp_index(G723_1_Context *p, int pitch_lag,
  677. int *exc_eng, int *scale)
  678. {
  679. int offset = PITCH_MAX + 2 * SUBFRAME_LEN;
  680. int16_t *buf = p->audio + LPC_ORDER;
  681. int index, ccr, tgt_eng, best_eng, temp;
  682. *scale = scale_vector(buf, p->excitation, FRAME_LEN + PITCH_MAX);
  683. buf += offset;
  684. /* Compute maximum backward cross-correlation */
  685. ccr = 0;
  686. index = autocorr_max(buf, offset, &ccr, pitch_lag, SUBFRAME_LEN * 2, -1);
  687. ccr = av_sat_add32(ccr, 1 << 15) >> 16;
  688. /* Compute target energy */
  689. tgt_eng = dot_product(buf, buf, SUBFRAME_LEN * 2);
  690. *exc_eng = av_sat_add32(tgt_eng, 1 << 15) >> 16;
  691. if (ccr <= 0)
  692. return 0;
  693. /* Compute best energy */
  694. best_eng = dot_product(buf - index, buf - index, SUBFRAME_LEN * 2);
  695. best_eng = av_sat_add32(best_eng, 1 << 15) >> 16;
  696. temp = best_eng * *exc_eng >> 3;
  697. if (temp < ccr * ccr)
  698. return index;
  699. else
  700. return 0;
  701. }
  702. /**
  703. * Peform residual interpolation based on frame classification.
  704. *
  705. * @param buf decoded excitation vector
  706. * @param out output vector
  707. * @param lag decoded pitch lag
  708. * @param gain interpolated gain
  709. * @param rseed seed for random number generator
  710. */
  711. static void residual_interp(int16_t *buf, int16_t *out, int lag,
  712. int gain, int *rseed)
  713. {
  714. int i;
  715. if (lag) { /* Voiced */
  716. int16_t *vector_ptr = buf + PITCH_MAX;
  717. /* Attenuate */
  718. for (i = 0; i < lag; i++)
  719. out[i] = vector_ptr[i - lag] * 3 >> 2;
  720. av_memcpy_backptr((uint8_t*)(out + lag), lag * sizeof(*out),
  721. (FRAME_LEN - lag) * sizeof(*out));
  722. } else { /* Unvoiced */
  723. for (i = 0; i < FRAME_LEN; i++) {
  724. *rseed = *rseed * 521 + 259;
  725. out[i] = gain * *rseed >> 15;
  726. }
  727. memset(buf, 0, (FRAME_LEN + PITCH_MAX) * sizeof(*buf));
  728. }
  729. }
  730. /**
  731. * Perform IIR filtering.
  732. *
  733. * @param fir_coef FIR coefficients
  734. * @param iir_coef IIR coefficients
  735. * @param src source vector
  736. * @param dest destination vector
  737. */
  738. static inline void iir_filter(int16_t *fir_coef, int16_t *iir_coef,
  739. int16_t *src, int *dest)
  740. {
  741. int m, n;
  742. for (m = 0; m < SUBFRAME_LEN; m++) {
  743. int64_t filter = 0;
  744. for (n = 1; n <= LPC_ORDER; n++) {
  745. filter -= fir_coef[n - 1] * src[m - n] -
  746. iir_coef[n - 1] * (dest[m - n] >> 16);
  747. }
  748. dest[m] = av_clipl_int32((src[m] << 16) + (filter << 3) + (1 << 15));
  749. }
  750. }
  751. /**
  752. * Adjust gain of postfiltered signal.
  753. *
  754. * @param p the context
  755. * @param buf postfiltered output vector
  756. * @param energy input energy coefficient
  757. */
  758. static void gain_scale(G723_1_Context *p, int16_t * buf, int energy)
  759. {
  760. int num, denom, gain, bits1, bits2;
  761. int i;
  762. num = energy;
  763. denom = 0;
  764. for (i = 0; i < SUBFRAME_LEN; i++) {
  765. int temp = buf[i] >> 2;
  766. temp *= temp;
  767. denom = av_sat_dadd32(denom, temp);
  768. }
  769. if (num && denom) {
  770. bits1 = normalize_bits(num, 31);
  771. bits2 = normalize_bits(denom, 31);
  772. num = num << bits1 >> 1;
  773. denom <<= bits2;
  774. bits2 = 5 + bits1 - bits2;
  775. bits2 = FFMAX(0, bits2);
  776. gain = (num >> 1) / (denom >> 16);
  777. gain = square_root(gain << 16 >> bits2);
  778. } else {
  779. gain = 1 << 12;
  780. }
  781. for (i = 0; i < SUBFRAME_LEN; i++) {
  782. p->pf_gain = (15 * p->pf_gain + gain + (1 << 3)) >> 4;
  783. buf[i] = av_clip_int16((buf[i] * (p->pf_gain + (p->pf_gain >> 4)) +
  784. (1 << 10)) >> 11);
  785. }
  786. }
  787. /**
  788. * Perform formant filtering.
  789. *
  790. * @param p the context
  791. * @param lpc quantized lpc coefficients
  792. * @param buf input buffer
  793. * @param dst output buffer
  794. */
  795. static void formant_postfilter(G723_1_Context *p, int16_t *lpc,
  796. int16_t *buf, int16_t *dst)
  797. {
  798. int16_t filter_coef[2][LPC_ORDER];
  799. int filter_signal[LPC_ORDER + FRAME_LEN], *signal_ptr;
  800. int i, j, k;
  801. memcpy(buf, p->fir_mem, LPC_ORDER * sizeof(*buf));
  802. memcpy(filter_signal, p->iir_mem, LPC_ORDER * sizeof(*filter_signal));
  803. for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
  804. for (k = 0; k < LPC_ORDER; k++) {
  805. filter_coef[0][k] = (-lpc[k] * postfilter_tbl[0][k] +
  806. (1 << 14)) >> 15;
  807. filter_coef[1][k] = (-lpc[k] * postfilter_tbl[1][k] +
  808. (1 << 14)) >> 15;
  809. }
  810. iir_filter(filter_coef[0], filter_coef[1], buf + i,
  811. filter_signal + i);
  812. lpc += LPC_ORDER;
  813. }
  814. memcpy(p->fir_mem, buf + FRAME_LEN, LPC_ORDER * sizeof(*p->fir_mem));
  815. memcpy(p->iir_mem, filter_signal + FRAME_LEN,
  816. LPC_ORDER * sizeof(*p->iir_mem));
  817. buf += LPC_ORDER;
  818. signal_ptr = filter_signal + LPC_ORDER;
  819. for (i = 0; i < SUBFRAMES; i++) {
  820. int temp;
  821. int auto_corr[2];
  822. int scale, energy;
  823. /* Normalize */
  824. scale = scale_vector(dst, buf, SUBFRAME_LEN);
  825. /* Compute auto correlation coefficients */
  826. auto_corr[0] = dot_product(dst, dst + 1, SUBFRAME_LEN - 1);
  827. auto_corr[1] = dot_product(dst, dst, SUBFRAME_LEN);
  828. /* Compute reflection coefficient */
  829. temp = auto_corr[1] >> 16;
  830. if (temp) {
  831. temp = (auto_corr[0] >> 2) / temp;
  832. }
  833. p->reflection_coef = (3 * p->reflection_coef + temp + 2) >> 2;
  834. temp = -p->reflection_coef >> 1 & ~3;
  835. /* Compensation filter */
  836. for (j = 0; j < SUBFRAME_LEN; j++) {
  837. dst[j] = av_sat_dadd32(signal_ptr[j],
  838. (signal_ptr[j - 1] >> 16) * temp) >> 16;
  839. }
  840. /* Compute normalized signal energy */
  841. temp = 2 * scale + 4;
  842. if (temp < 0) {
  843. energy = av_clipl_int32((int64_t)auto_corr[1] << -temp);
  844. } else
  845. energy = auto_corr[1] >> temp;
  846. gain_scale(p, dst, energy);
  847. buf += SUBFRAME_LEN;
  848. signal_ptr += SUBFRAME_LEN;
  849. dst += SUBFRAME_LEN;
  850. }
  851. }
  852. static int sid_gain_to_lsp_index(int gain)
  853. {
  854. if (gain < 0x10)
  855. return gain << 6;
  856. else if (gain < 0x20)
  857. return gain - 8 << 7;
  858. else
  859. return gain - 20 << 8;
  860. }
  861. static inline int cng_rand(int *state, int base)
  862. {
  863. *state = (*state * 521 + 259) & 0xFFFF;
  864. return (*state & 0x7FFF) * base >> 15;
  865. }
  866. static int estimate_sid_gain(G723_1_Context *p)
  867. {
  868. int i, shift, seg, seg2, t, val, val_add, x, y;
  869. shift = 16 - p->cur_gain * 2;
  870. if (shift > 0)
  871. t = p->sid_gain << shift;
  872. else
  873. t = p->sid_gain >> -shift;
  874. x = t * cng_filt[0] >> 16;
  875. if (x >= cng_bseg[2])
  876. return 0x3F;
  877. if (x >= cng_bseg[1]) {
  878. shift = 4;
  879. seg = 3;
  880. } else {
  881. shift = 3;
  882. seg = (x >= cng_bseg[0]);
  883. }
  884. seg2 = FFMIN(seg, 3);
  885. val = 1 << shift;
  886. val_add = val >> 1;
  887. for (i = 0; i < shift; i++) {
  888. t = seg * 32 + (val << seg2);
  889. t *= t;
  890. if (x >= t)
  891. val += val_add;
  892. else
  893. val -= val_add;
  894. val_add >>= 1;
  895. }
  896. t = seg * 32 + (val << seg2);
  897. y = t * t - x;
  898. if (y <= 0) {
  899. t = seg * 32 + (val + 1 << seg2);
  900. t = t * t - x;
  901. val = (seg2 - 1 << 4) + val;
  902. if (t >= y)
  903. val++;
  904. } else {
  905. t = seg * 32 + (val - 1 << seg2);
  906. t = t * t - x;
  907. val = (seg2 - 1 << 4) + val;
  908. if (t >= y)
  909. val--;
  910. }
  911. return val;
  912. }
  913. static void generate_noise(G723_1_Context *p)
  914. {
  915. int i, j, idx, t;
  916. int off[SUBFRAMES];
  917. int signs[SUBFRAMES / 2 * 11], pos[SUBFRAMES / 2 * 11];
  918. int tmp[SUBFRAME_LEN * 2];
  919. int16_t *vector_ptr;
  920. int64_t sum;
  921. int b0, c, delta, x, shift;
  922. p->pitch_lag[0] = cng_rand(&p->cng_random_seed, 21) + 123;
  923. p->pitch_lag[1] = cng_rand(&p->cng_random_seed, 19) + 123;
  924. for (i = 0; i < SUBFRAMES; i++) {
  925. p->subframe[i].ad_cb_gain = cng_rand(&p->cng_random_seed, 50) + 1;
  926. p->subframe[i].ad_cb_lag = cng_adaptive_cb_lag[i];
  927. }
  928. for (i = 0; i < SUBFRAMES / 2; i++) {
  929. t = cng_rand(&p->cng_random_seed, 1 << 13);
  930. off[i * 2] = t & 1;
  931. off[i * 2 + 1] = ((t >> 1) & 1) + SUBFRAME_LEN;
  932. t >>= 2;
  933. for (j = 0; j < 11; j++) {
  934. signs[i * 11 + j] = (t & 1) * 2 - 1 << 14;
  935. t >>= 1;
  936. }
  937. }
  938. idx = 0;
  939. for (i = 0; i < SUBFRAMES; i++) {
  940. for (j = 0; j < SUBFRAME_LEN / 2; j++)
  941. tmp[j] = j;
  942. t = SUBFRAME_LEN / 2;
  943. for (j = 0; j < pulses[i]; j++, idx++) {
  944. int idx2 = cng_rand(&p->cng_random_seed, t);
  945. pos[idx] = tmp[idx2] * 2 + off[i];
  946. tmp[idx2] = tmp[--t];
  947. }
  948. }
  949. vector_ptr = p->audio + LPC_ORDER;
  950. memcpy(vector_ptr, p->prev_excitation,
  951. PITCH_MAX * sizeof(*p->excitation));
  952. for (i = 0; i < SUBFRAMES; i += 2) {
  953. gen_acb_excitation(vector_ptr, vector_ptr,
  954. p->pitch_lag[i >> 1], &p->subframe[i],
  955. p->cur_rate);
  956. gen_acb_excitation(vector_ptr + SUBFRAME_LEN,
  957. vector_ptr + SUBFRAME_LEN,
  958. p->pitch_lag[i >> 1], &p->subframe[i + 1],
  959. p->cur_rate);
  960. t = 0;
  961. for (j = 0; j < SUBFRAME_LEN * 2; j++)
  962. t |= FFABS(vector_ptr[j]);
  963. t = FFMIN(t, 0x7FFF);
  964. if (!t) {
  965. shift = 0;
  966. } else {
  967. shift = -10 + av_log2(t);
  968. if (shift < -2)
  969. shift = -2;
  970. }
  971. sum = 0;
  972. if (shift < 0) {
  973. for (j = 0; j < SUBFRAME_LEN * 2; j++) {
  974. t = vector_ptr[j] << -shift;
  975. sum += t * t;
  976. tmp[j] = t;
  977. }
  978. } else {
  979. for (j = 0; j < SUBFRAME_LEN * 2; j++) {
  980. t = vector_ptr[j] >> shift;
  981. sum += t * t;
  982. tmp[j] = t;
  983. }
  984. }
  985. b0 = 0;
  986. for (j = 0; j < 11; j++)
  987. b0 += tmp[pos[(i / 2) * 11 + j]] * signs[(i / 2) * 11 + j];
  988. b0 = b0 * 2 * 2979LL + (1 << 29) >> 30; // approximated division by 11
  989. c = p->cur_gain * (p->cur_gain * SUBFRAME_LEN >> 5);
  990. if (shift * 2 + 3 >= 0)
  991. c >>= shift * 2 + 3;
  992. else
  993. c <<= -(shift * 2 + 3);
  994. c = (av_clipl_int32(sum << 1) - c) * 2979LL >> 15;
  995. delta = b0 * b0 * 2 - c;
  996. if (delta <= 0) {
  997. x = -b0;
  998. } else {
  999. delta = square_root(delta);
  1000. x = delta - b0;
  1001. t = delta + b0;
  1002. if (FFABS(t) < FFABS(x))
  1003. x = -t;
  1004. }
  1005. shift++;
  1006. if (shift < 0)
  1007. x >>= -shift;
  1008. else
  1009. x <<= shift;
  1010. x = av_clip(x, -10000, 10000);
  1011. for (j = 0; j < 11; j++) {
  1012. idx = (i / 2) * 11 + j;
  1013. vector_ptr[pos[idx]] = av_clip_int16(vector_ptr[pos[idx]] +
  1014. (x * signs[idx] >> 15));
  1015. }
  1016. /* copy decoded data to serve as a history for the next decoded subframes */
  1017. memcpy(vector_ptr + PITCH_MAX, vector_ptr,
  1018. sizeof(*vector_ptr) * SUBFRAME_LEN * 2);
  1019. vector_ptr += SUBFRAME_LEN * 2;
  1020. }
  1021. /* Save the excitation for the next frame */
  1022. memcpy(p->prev_excitation, p->audio + LPC_ORDER + FRAME_LEN,
  1023. PITCH_MAX * sizeof(*p->excitation));
  1024. }
  1025. static int g723_1_decode_frame(AVCodecContext *avctx, void *data,
  1026. int *got_frame_ptr, AVPacket *avpkt)
  1027. {
  1028. G723_1_Context *p = avctx->priv_data;
  1029. AVFrame *frame = data;
  1030. const uint8_t *buf = avpkt->data;
  1031. int buf_size = avpkt->size;
  1032. int dec_mode = buf[0] & 3;
  1033. PPFParam ppf[SUBFRAMES];
  1034. int16_t cur_lsp[LPC_ORDER];
  1035. int16_t lpc[SUBFRAMES * LPC_ORDER];
  1036. int16_t acb_vector[SUBFRAME_LEN];
  1037. int16_t *out;
  1038. int bad_frame = 0, i, j, ret;
  1039. int16_t *audio = p->audio;
  1040. if (buf_size < frame_size[dec_mode]) {
  1041. if (buf_size)
  1042. av_log(avctx, AV_LOG_WARNING,
  1043. "Expected %d bytes, got %d - skipping packet\n",
  1044. frame_size[dec_mode], buf_size);
  1045. *got_frame_ptr = 0;
  1046. return buf_size;
  1047. }
  1048. if (unpack_bitstream(p, buf, buf_size) < 0) {
  1049. bad_frame = 1;
  1050. if (p->past_frame_type == ACTIVE_FRAME)
  1051. p->cur_frame_type = ACTIVE_FRAME;
  1052. else
  1053. p->cur_frame_type = UNTRANSMITTED_FRAME;
  1054. }
  1055. frame->nb_samples = FRAME_LEN;
  1056. if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
  1057. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  1058. return ret;
  1059. }
  1060. out = (int16_t *)frame->data[0];
  1061. if (p->cur_frame_type == ACTIVE_FRAME) {
  1062. if (!bad_frame)
  1063. p->erased_frames = 0;
  1064. else if (p->erased_frames != 3)
  1065. p->erased_frames++;
  1066. inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, bad_frame);
  1067. lsp_interpolate(lpc, cur_lsp, p->prev_lsp);
  1068. /* Save the lsp_vector for the next frame */
  1069. memcpy(p->prev_lsp, cur_lsp, LPC_ORDER * sizeof(*p->prev_lsp));
  1070. /* Generate the excitation for the frame */
  1071. memcpy(p->excitation, p->prev_excitation,
  1072. PITCH_MAX * sizeof(*p->excitation));
  1073. if (!p->erased_frames) {
  1074. int16_t *vector_ptr = p->excitation + PITCH_MAX;
  1075. /* Update interpolation gain memory */
  1076. p->interp_gain = fixed_cb_gain[(p->subframe[2].amp_index +
  1077. p->subframe[3].amp_index) >> 1];
  1078. for (i = 0; i < SUBFRAMES; i++) {
  1079. gen_fcb_excitation(vector_ptr, &p->subframe[i], p->cur_rate,
  1080. p->pitch_lag[i >> 1], i);
  1081. gen_acb_excitation(acb_vector, &p->excitation[SUBFRAME_LEN * i],
  1082. p->pitch_lag[i >> 1], &p->subframe[i],
  1083. p->cur_rate);
  1084. /* Get the total excitation */
  1085. for (j = 0; j < SUBFRAME_LEN; j++) {
  1086. int v = av_clip_int16(vector_ptr[j] << 1);
  1087. vector_ptr[j] = av_clip_int16(v + acb_vector[j]);
  1088. }
  1089. vector_ptr += SUBFRAME_LEN;
  1090. }
  1091. vector_ptr = p->excitation + PITCH_MAX;
  1092. p->interp_index = comp_interp_index(p, p->pitch_lag[1],
  1093. &p->sid_gain, &p->cur_gain);
  1094. /* Peform pitch postfiltering */
  1095. if (p->postfilter) {
  1096. i = PITCH_MAX;
  1097. for (j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
  1098. comp_ppf_coeff(p, i, p->pitch_lag[j >> 1],
  1099. ppf + j, p->cur_rate);
  1100. for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
  1101. ff_acelp_weighted_vector_sum(p->audio + LPC_ORDER + i,
  1102. vector_ptr + i,
  1103. vector_ptr + i + ppf[j].index,
  1104. ppf[j].sc_gain,
  1105. ppf[j].opt_gain,
  1106. 1 << 14, 15, SUBFRAME_LEN);
  1107. } else {
  1108. audio = vector_ptr - LPC_ORDER;
  1109. }
  1110. /* Save the excitation for the next frame */
  1111. memcpy(p->prev_excitation, p->excitation + FRAME_LEN,
  1112. PITCH_MAX * sizeof(*p->excitation));
  1113. } else {
  1114. p->interp_gain = (p->interp_gain * 3 + 2) >> 2;
  1115. if (p->erased_frames == 3) {
  1116. /* Mute output */
  1117. memset(p->excitation, 0,
  1118. (FRAME_LEN + PITCH_MAX) * sizeof(*p->excitation));
  1119. memset(p->prev_excitation, 0,
  1120. PITCH_MAX * sizeof(*p->excitation));
  1121. memset(frame->data[0], 0,
  1122. (FRAME_LEN + LPC_ORDER) * sizeof(int16_t));
  1123. } else {
  1124. int16_t *buf = p->audio + LPC_ORDER;
  1125. /* Regenerate frame */
  1126. residual_interp(p->excitation, buf, p->interp_index,
  1127. p->interp_gain, &p->random_seed);
  1128. /* Save the excitation for the next frame */
  1129. memcpy(p->prev_excitation, buf + (FRAME_LEN - PITCH_MAX),
  1130. PITCH_MAX * sizeof(*p->excitation));
  1131. }
  1132. }
  1133. p->cng_random_seed = CNG_RANDOM_SEED;
  1134. } else {
  1135. if (p->cur_frame_type == SID_FRAME) {
  1136. p->sid_gain = sid_gain_to_lsp_index(p->subframe[0].amp_index);
  1137. inverse_quant(p->sid_lsp, p->prev_lsp, p->lsp_index, 0);
  1138. } else if (p->past_frame_type == ACTIVE_FRAME) {
  1139. p->sid_gain = estimate_sid_gain(p);
  1140. }
  1141. if (p->past_frame_type == ACTIVE_FRAME)
  1142. p->cur_gain = p->sid_gain;
  1143. else
  1144. p->cur_gain = (p->cur_gain * 7 + p->sid_gain) >> 3;
  1145. generate_noise(p);
  1146. lsp_interpolate(lpc, p->sid_lsp, p->prev_lsp);
  1147. /* Save the lsp_vector for the next frame */
  1148. memcpy(p->prev_lsp, p->sid_lsp, LPC_ORDER * sizeof(*p->prev_lsp));
  1149. }
  1150. p->past_frame_type = p->cur_frame_type;
  1151. memcpy(p->audio, p->synth_mem, LPC_ORDER * sizeof(*p->audio));
  1152. for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
  1153. ff_celp_lp_synthesis_filter(p->audio + i, &lpc[j * LPC_ORDER],
  1154. audio + i, SUBFRAME_LEN, LPC_ORDER,
  1155. 0, 1, 1 << 12);
  1156. memcpy(p->synth_mem, p->audio + FRAME_LEN, LPC_ORDER * sizeof(*p->audio));
  1157. if (p->postfilter) {
  1158. formant_postfilter(p, lpc, p->audio, out);
  1159. } else { // if output is not postfiltered it should be scaled by 2
  1160. for (i = 0; i < FRAME_LEN; i++)
  1161. out[i] = av_clip_int16(p->audio[LPC_ORDER + i] << 1);
  1162. }
  1163. *got_frame_ptr = 1;
  1164. return frame_size[dec_mode];
  1165. }
  1166. #define OFFSET(x) offsetof(G723_1_Context, x)
  1167. #define AD AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  1168. static const AVOption options[] = {
  1169. { "postfilter", "postfilter on/off", OFFSET(postfilter), AV_OPT_TYPE_INT,
  1170. { .i64 = 1 }, 0, 1, AD },
  1171. { NULL }
  1172. };
  1173. static const AVClass g723_1dec_class = {
  1174. .class_name = "G.723.1 decoder",
  1175. .item_name = av_default_item_name,
  1176. .option = options,
  1177. .version = LIBAVUTIL_VERSION_INT,
  1178. };
  1179. AVCodec ff_g723_1_decoder = {
  1180. .name = "g723_1",
  1181. .long_name = NULL_IF_CONFIG_SMALL("G.723.1"),
  1182. .type = AVMEDIA_TYPE_AUDIO,
  1183. .id = AV_CODEC_ID_G723_1,
  1184. .priv_data_size = sizeof(G723_1_Context),
  1185. .init = g723_1_decode_init,
  1186. .decode = g723_1_decode_frame,
  1187. .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
  1188. .priv_class = &g723_1dec_class,
  1189. };