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.

1203 lines
39KB

  1. /*
  2. * G.723.1 compatible encoder
  3. * Copyright (c) Mohamed Naufal <naufal22@gmail.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. * G.723.1 compatible encoder
  24. */
  25. #include <stdint.h>
  26. #include <string.h>
  27. #include "libavutil/channel_layout.h"
  28. #include "libavutil/common.h"
  29. #include "libavutil/mem.h"
  30. #include "libavutil/opt.h"
  31. #include "avcodec.h"
  32. #include "celp_math.h"
  33. #include "g723_1.h"
  34. #include "internal.h"
  35. #define BITSTREAM_WRITER_LE
  36. #include "put_bits.h"
  37. static av_cold int g723_1_encode_init(AVCodecContext *avctx)
  38. {
  39. G723_1_Context *p = avctx->priv_data;
  40. if (avctx->sample_rate != 8000) {
  41. av_log(avctx, AV_LOG_ERROR, "Only 8000Hz sample rate supported\n");
  42. return AVERROR(EINVAL);
  43. }
  44. if (avctx->channels != 1) {
  45. av_log(avctx, AV_LOG_ERROR, "Only mono supported\n");
  46. return AVERROR(EINVAL);
  47. }
  48. if (avctx->bit_rate == 6300) {
  49. p->cur_rate = RATE_6300;
  50. } else if (avctx->bit_rate == 5300) {
  51. av_log(avctx, AV_LOG_ERROR, "Bitrate not supported yet, use 6300\n");
  52. return AVERROR_PATCHWELCOME;
  53. } else {
  54. av_log(avctx, AV_LOG_ERROR, "Bitrate not supported, use 6300\n");
  55. return AVERROR(EINVAL);
  56. }
  57. avctx->frame_size = 240;
  58. memcpy(p->prev_lsp, dc_lsp, LPC_ORDER * sizeof(int16_t));
  59. return 0;
  60. }
  61. /**
  62. * Remove DC component from the input signal.
  63. *
  64. * @param buf input signal
  65. * @param fir zero memory
  66. * @param iir pole memory
  67. */
  68. static void highpass_filter(int16_t *buf, int16_t *fir, int *iir)
  69. {
  70. int i;
  71. for (i = 0; i < FRAME_LEN; i++) {
  72. *iir = (buf[i] << 15) + ((-*fir) << 15) + MULL2(*iir, 0x7f00);
  73. *fir = buf[i];
  74. buf[i] = av_clipl_int32((int64_t) *iir + (1 << 15)) >> 16;
  75. }
  76. }
  77. /**
  78. * Estimate autocorrelation of the input vector.
  79. *
  80. * @param buf input buffer
  81. * @param autocorr autocorrelation coefficients vector
  82. */
  83. static void comp_autocorr(int16_t *buf, int16_t *autocorr)
  84. {
  85. int i, scale, temp;
  86. int16_t vector[LPC_FRAME];
  87. ff_g723_1_scale_vector(vector, buf, LPC_FRAME);
  88. /* Apply the Hamming window */
  89. for (i = 0; i < LPC_FRAME; i++)
  90. vector[i] = (vector[i] * hamming_window[i] + (1 << 14)) >> 15;
  91. /* Compute the first autocorrelation coefficient */
  92. temp = ff_dot_product(vector, vector, LPC_FRAME);
  93. /* Apply a white noise correlation factor of (1025/1024) */
  94. temp += temp >> 10;
  95. /* Normalize */
  96. scale = ff_g723_1_normalize_bits(temp, 31);
  97. autocorr[0] = av_clipl_int32((int64_t) (temp << scale) +
  98. (1 << 15)) >> 16;
  99. /* Compute the remaining coefficients */
  100. if (!autocorr[0]) {
  101. memset(autocorr + 1, 0, LPC_ORDER * sizeof(int16_t));
  102. } else {
  103. for (i = 1; i <= LPC_ORDER; i++) {
  104. temp = ff_dot_product(vector, vector + i, LPC_FRAME - i);
  105. temp = MULL2((temp << scale), binomial_window[i - 1]);
  106. autocorr[i] = av_clipl_int32((int64_t) temp + (1 << 15)) >> 16;
  107. }
  108. }
  109. }
  110. /**
  111. * Use Levinson-Durbin recursion to compute LPC coefficients from
  112. * autocorrelation values.
  113. *
  114. * @param lpc LPC coefficients vector
  115. * @param autocorr autocorrelation coefficients vector
  116. * @param error prediction error
  117. */
  118. static void levinson_durbin(int16_t *lpc, int16_t *autocorr, int16_t error)
  119. {
  120. int16_t vector[LPC_ORDER];
  121. int16_t partial_corr;
  122. int i, j, temp;
  123. memset(lpc, 0, LPC_ORDER * sizeof(int16_t));
  124. for (i = 0; i < LPC_ORDER; i++) {
  125. /* Compute the partial correlation coefficient */
  126. temp = 0;
  127. for (j = 0; j < i; j++)
  128. temp -= lpc[j] * autocorr[i - j - 1];
  129. temp = ((autocorr[i] << 13) + temp) << 3;
  130. if (FFABS(temp) >= (error << 16))
  131. break;
  132. partial_corr = temp / (error << 1);
  133. lpc[i] = av_clipl_int32((int64_t) (partial_corr << 14) +
  134. (1 << 15)) >> 16;
  135. /* Update the prediction error */
  136. temp = MULL2(temp, partial_corr);
  137. error = av_clipl_int32((int64_t) (error << 16) - temp +
  138. (1 << 15)) >> 16;
  139. memcpy(vector, lpc, i * sizeof(int16_t));
  140. for (j = 0; j < i; j++) {
  141. temp = partial_corr * vector[i - j - 1] << 1;
  142. lpc[j] = av_clipl_int32((int64_t) (lpc[j] << 16) - temp +
  143. (1 << 15)) >> 16;
  144. }
  145. }
  146. }
  147. /**
  148. * Calculate LPC coefficients for the current frame.
  149. *
  150. * @param buf current frame
  151. * @param prev_data 2 trailing subframes of the previous frame
  152. * @param lpc LPC coefficients vector
  153. */
  154. static void comp_lpc_coeff(int16_t *buf, int16_t *lpc)
  155. {
  156. int16_t autocorr[(LPC_ORDER + 1) * SUBFRAMES];
  157. int16_t *autocorr_ptr = autocorr;
  158. int16_t *lpc_ptr = lpc;
  159. int i, j;
  160. for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
  161. comp_autocorr(buf + i, autocorr_ptr);
  162. levinson_durbin(lpc_ptr, autocorr_ptr + 1, autocorr_ptr[0]);
  163. lpc_ptr += LPC_ORDER;
  164. autocorr_ptr += LPC_ORDER + 1;
  165. }
  166. }
  167. static void lpc2lsp(int16_t *lpc, int16_t *prev_lsp, int16_t *lsp)
  168. {
  169. int f[LPC_ORDER + 2]; ///< coefficients of the sum and difference
  170. ///< polynomials (F1, F2) ordered as
  171. ///< f1[0], f2[0], ...., f1[5], f2[5]
  172. int max, shift, cur_val, prev_val, count, p;
  173. int i, j;
  174. int64_t temp;
  175. /* Initialize f1[0] and f2[0] to 1 in Q25 */
  176. for (i = 0; i < LPC_ORDER; i++)
  177. lsp[i] = (lpc[i] * bandwidth_expand[i] + (1 << 14)) >> 15;
  178. /* Apply bandwidth expansion on the LPC coefficients */
  179. f[0] = f[1] = 1 << 25;
  180. /* Compute the remaining coefficients */
  181. for (i = 0; i < LPC_ORDER / 2; i++) {
  182. /* f1 */
  183. f[2 * i + 2] = -f[2 * i] - ((lsp[i] + lsp[LPC_ORDER - 1 - i]) << 12);
  184. /* f2 */
  185. f[2 * i + 3] = f[2 * i + 1] - ((lsp[i] - lsp[LPC_ORDER - 1 - i]) << 12);
  186. }
  187. /* Divide f1[5] and f2[5] by 2 for use in polynomial evaluation */
  188. f[LPC_ORDER] >>= 1;
  189. f[LPC_ORDER + 1] >>= 1;
  190. /* Normalize and shorten */
  191. max = FFABS(f[0]);
  192. for (i = 1; i < LPC_ORDER + 2; i++)
  193. max = FFMAX(max, FFABS(f[i]));
  194. shift = ff_g723_1_normalize_bits(max, 31);
  195. for (i = 0; i < LPC_ORDER + 2; i++)
  196. f[i] = av_clipl_int32((int64_t) (f[i] << shift) + (1 << 15)) >> 16;
  197. /**
  198. * Evaluate F1 and F2 at uniform intervals of pi/256 along the
  199. * unit circle and check for zero crossings.
  200. */
  201. p = 0;
  202. temp = 0;
  203. for (i = 0; i <= LPC_ORDER / 2; i++)
  204. temp += f[2 * i] * cos_tab[0];
  205. prev_val = av_clipl_int32(temp << 1);
  206. count = 0;
  207. for (i = 1; i < COS_TBL_SIZE / 2; i++) {
  208. /* Evaluate */
  209. temp = 0;
  210. for (j = 0; j <= LPC_ORDER / 2; j++)
  211. temp += f[LPC_ORDER - 2 * j + p] * cos_tab[i * j % COS_TBL_SIZE];
  212. cur_val = av_clipl_int32(temp << 1);
  213. /* Check for sign change, indicating a zero crossing */
  214. if ((cur_val ^ prev_val) < 0) {
  215. int abs_cur = FFABS(cur_val);
  216. int abs_prev = FFABS(prev_val);
  217. int sum = abs_cur + abs_prev;
  218. shift = ff_g723_1_normalize_bits(sum, 31);
  219. sum <<= shift;
  220. abs_prev = abs_prev << shift >> 8;
  221. lsp[count++] = ((i - 1) << 7) + (abs_prev >> 1) / (sum >> 16);
  222. if (count == LPC_ORDER)
  223. break;
  224. /* Switch between sum and difference polynomials */
  225. p ^= 1;
  226. /* Evaluate */
  227. temp = 0;
  228. for (j = 0; j <= LPC_ORDER / 2; j++)
  229. temp += f[LPC_ORDER - 2 * j + p] *
  230. cos_tab[i * j % COS_TBL_SIZE];
  231. cur_val = av_clipl_int32(temp << 1);
  232. }
  233. prev_val = cur_val;
  234. }
  235. if (count != LPC_ORDER)
  236. memcpy(lsp, prev_lsp, LPC_ORDER * sizeof(int16_t));
  237. }
  238. /**
  239. * Quantize the current LSP subvector.
  240. *
  241. * @param num band number
  242. * @param offset offset of the current subvector in an LPC_ORDER vector
  243. * @param size size of the current subvector
  244. */
  245. #define get_index(num, offset, size) \
  246. { \
  247. int error, max = -1; \
  248. int16_t temp[4]; \
  249. int i, j; \
  250. \
  251. for (i = 0; i < LSP_CB_SIZE; i++) { \
  252. for (j = 0; j < size; j++){ \
  253. temp[j] = (weight[j + (offset)] * lsp_band##num[i][j] + \
  254. (1 << 14)) >> 15; \
  255. } \
  256. error = ff_g723_1_dot_product(lsp + (offset), temp, size) << 1; \
  257. error -= ff_g723_1_dot_product(lsp_band##num[i], temp, size); \
  258. if (error > max) { \
  259. max = error; \
  260. lsp_index[num] = i; \
  261. } \
  262. } \
  263. }
  264. /**
  265. * Vector quantize the LSP frequencies.
  266. *
  267. * @param lsp the current lsp vector
  268. * @param prev_lsp the previous lsp vector
  269. */
  270. static void lsp_quantize(uint8_t *lsp_index, int16_t *lsp, int16_t *prev_lsp)
  271. {
  272. int16_t weight[LPC_ORDER];
  273. int16_t min, max;
  274. int shift, i;
  275. /* Calculate the VQ weighting vector */
  276. weight[0] = (1 << 20) / (lsp[1] - lsp[0]);
  277. weight[LPC_ORDER - 1] = (1 << 20) /
  278. (lsp[LPC_ORDER - 1] - lsp[LPC_ORDER - 2]);
  279. for (i = 1; i < LPC_ORDER - 1; i++) {
  280. min = FFMIN(lsp[i] - lsp[i - 1], lsp[i + 1] - lsp[i]);
  281. if (min > 0x20)
  282. weight[i] = (1 << 20) / min;
  283. else
  284. weight[i] = INT16_MAX;
  285. }
  286. /* Normalize */
  287. max = 0;
  288. for (i = 0; i < LPC_ORDER; i++)
  289. max = FFMAX(weight[i], max);
  290. shift = ff_g723_1_normalize_bits(max, 15);
  291. for (i = 0; i < LPC_ORDER; i++) {
  292. weight[i] <<= shift;
  293. }
  294. /* Compute the VQ target vector */
  295. for (i = 0; i < LPC_ORDER; i++) {
  296. lsp[i] -= dc_lsp[i] +
  297. (((prev_lsp[i] - dc_lsp[i]) * 12288 + (1 << 14)) >> 15);
  298. }
  299. get_index(0, 0, 3);
  300. get_index(1, 3, 3);
  301. get_index(2, 6, 4);
  302. }
  303. /**
  304. * Perform IIR filtering.
  305. *
  306. * @param fir_coef FIR coefficients
  307. * @param iir_coef IIR coefficients
  308. * @param src source vector
  309. * @param dest destination vector
  310. */
  311. static void iir_filter(int16_t *fir_coef, int16_t *iir_coef,
  312. int16_t *src, int16_t *dest)
  313. {
  314. int m, n;
  315. for (m = 0; m < SUBFRAME_LEN; m++) {
  316. int64_t filter = 0;
  317. for (n = 1; n <= LPC_ORDER; n++) {
  318. filter -= fir_coef[n - 1] * src[m - n] -
  319. iir_coef[n - 1] * dest[m - n];
  320. }
  321. dest[m] = av_clipl_int32((src[m] << 16) + (filter << 3) +
  322. (1 << 15)) >> 16;
  323. }
  324. }
  325. /**
  326. * Apply the formant perceptual weighting filter.
  327. *
  328. * @param flt_coef filter coefficients
  329. * @param unq_lpc unquantized lpc vector
  330. */
  331. static void perceptual_filter(G723_1_Context *p, int16_t *flt_coef,
  332. int16_t *unq_lpc, int16_t *buf)
  333. {
  334. int16_t vector[FRAME_LEN + LPC_ORDER];
  335. int i, j, k, l = 0;
  336. memcpy(buf, p->iir_mem, sizeof(int16_t) * LPC_ORDER);
  337. memcpy(vector, p->fir_mem, sizeof(int16_t) * LPC_ORDER);
  338. memcpy(vector + LPC_ORDER, buf + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
  339. for (i = LPC_ORDER, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++) {
  340. for (k = 0; k < LPC_ORDER; k++) {
  341. flt_coef[k + 2 * l] = (unq_lpc[k + l] * percept_flt_tbl[0][k] +
  342. (1 << 14)) >> 15;
  343. flt_coef[k + 2 * l + LPC_ORDER] = (unq_lpc[k + l] *
  344. percept_flt_tbl[1][k] +
  345. (1 << 14)) >> 15;
  346. }
  347. iir_filter(flt_coef + 2 * l, flt_coef + 2 * l + LPC_ORDER,
  348. vector + i, buf + i);
  349. l += LPC_ORDER;
  350. }
  351. memcpy(p->iir_mem, buf + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
  352. memcpy(p->fir_mem, vector + FRAME_LEN, sizeof(int16_t) * LPC_ORDER);
  353. }
  354. /**
  355. * Estimate the open loop pitch period.
  356. *
  357. * @param buf perceptually weighted speech
  358. * @param start estimation is carried out from this position
  359. */
  360. static int estimate_pitch(int16_t *buf, int start)
  361. {
  362. int max_exp = 32;
  363. int max_ccr = 0x4000;
  364. int max_eng = 0x7fff;
  365. int index = PITCH_MIN;
  366. int offset = start - PITCH_MIN + 1;
  367. int ccr, eng, orig_eng, ccr_eng, exp;
  368. int diff, temp;
  369. int i;
  370. orig_eng = ff_dot_product(buf + offset, buf + offset, HALF_FRAME_LEN);
  371. for (i = PITCH_MIN; i <= PITCH_MAX - 3; i++) {
  372. offset--;
  373. /* Update energy and compute correlation */
  374. orig_eng += buf[offset] * buf[offset] -
  375. buf[offset + HALF_FRAME_LEN] * buf[offset + HALF_FRAME_LEN];
  376. ccr = ff_dot_product(buf + start, buf + offset, HALF_FRAME_LEN);
  377. if (ccr <= 0)
  378. continue;
  379. /* Split into mantissa and exponent to maintain precision */
  380. exp = ff_g723_1_normalize_bits(ccr, 31);
  381. ccr = av_clipl_int32((int64_t) (ccr << exp) + (1 << 15)) >> 16;
  382. exp <<= 1;
  383. ccr *= ccr;
  384. temp = ff_g723_1_normalize_bits(ccr, 31);
  385. ccr = ccr << temp >> 16;
  386. exp += temp;
  387. temp = ff_g723_1_normalize_bits(orig_eng, 31);
  388. eng = av_clipl_int32((int64_t) (orig_eng << temp) + (1 << 15)) >> 16;
  389. exp -= temp;
  390. if (ccr >= eng) {
  391. exp--;
  392. ccr >>= 1;
  393. }
  394. if (exp > max_exp)
  395. continue;
  396. if (exp + 1 < max_exp)
  397. goto update;
  398. /* Equalize exponents before comparison */
  399. if (exp + 1 == max_exp)
  400. temp = max_ccr >> 1;
  401. else
  402. temp = max_ccr;
  403. ccr_eng = ccr * max_eng;
  404. diff = ccr_eng - eng * temp;
  405. if (diff > 0 && (i - index < PITCH_MIN || diff > ccr_eng >> 2)) {
  406. update:
  407. index = i;
  408. max_exp = exp;
  409. max_ccr = ccr;
  410. max_eng = eng;
  411. }
  412. }
  413. return index;
  414. }
  415. /**
  416. * Compute harmonic noise filter parameters.
  417. *
  418. * @param buf perceptually weighted speech
  419. * @param pitch_lag open loop pitch period
  420. * @param hf harmonic filter parameters
  421. */
  422. static void comp_harmonic_coeff(int16_t *buf, int16_t pitch_lag, HFParam *hf)
  423. {
  424. int ccr, eng, max_ccr, max_eng;
  425. int exp, max, diff;
  426. int energy[15];
  427. int i, j;
  428. for (i = 0, j = pitch_lag - 3; j <= pitch_lag + 3; i++, j++) {
  429. /* Compute residual energy */
  430. energy[i << 1] = ff_dot_product(buf - j, buf - j, SUBFRAME_LEN);
  431. /* Compute correlation */
  432. energy[(i << 1) + 1] = ff_dot_product(buf, buf - j, SUBFRAME_LEN);
  433. }
  434. /* Compute target energy */
  435. energy[14] = ff_dot_product(buf, buf, SUBFRAME_LEN);
  436. /* Normalize */
  437. max = 0;
  438. for (i = 0; i < 15; i++)
  439. max = FFMAX(max, FFABS(energy[i]));
  440. exp = ff_g723_1_normalize_bits(max, 31);
  441. for (i = 0; i < 15; i++) {
  442. energy[i] = av_clipl_int32((int64_t)(energy[i] << exp) +
  443. (1 << 15)) >> 16;
  444. }
  445. hf->index = -1;
  446. hf->gain = 0;
  447. max_ccr = 1;
  448. max_eng = 0x7fff;
  449. for (i = 0; i <= 6; i++) {
  450. eng = energy[i << 1];
  451. ccr = energy[(i << 1) + 1];
  452. if (ccr <= 0)
  453. continue;
  454. ccr = (ccr * ccr + (1 << 14)) >> 15;
  455. diff = ccr * max_eng - eng * max_ccr;
  456. if (diff > 0) {
  457. max_ccr = ccr;
  458. max_eng = eng;
  459. hf->index = i;
  460. }
  461. }
  462. if (hf->index == -1) {
  463. hf->index = pitch_lag;
  464. return;
  465. }
  466. eng = energy[14] * max_eng;
  467. eng = (eng >> 2) + (eng >> 3);
  468. ccr = energy[(hf->index << 1) + 1] * energy[(hf->index << 1) + 1];
  469. if (eng < ccr) {
  470. eng = energy[(hf->index << 1) + 1];
  471. if (eng >= max_eng)
  472. hf->gain = 0x2800;
  473. else
  474. hf->gain = ((eng << 15) / max_eng * 0x2800 + (1 << 14)) >> 15;
  475. }
  476. hf->index += pitch_lag - 3;
  477. }
  478. /**
  479. * Apply the harmonic noise shaping filter.
  480. *
  481. * @param hf filter parameters
  482. */
  483. static void harmonic_filter(HFParam *hf, const int16_t *src, int16_t *dest)
  484. {
  485. int i;
  486. for (i = 0; i < SUBFRAME_LEN; i++) {
  487. int64_t temp = hf->gain * src[i - hf->index] << 1;
  488. dest[i] = av_clipl_int32((src[i] << 16) - temp + (1 << 15)) >> 16;
  489. }
  490. }
  491. static void harmonic_noise_sub(HFParam *hf, const int16_t *src, int16_t *dest)
  492. {
  493. int i;
  494. for (i = 0; i < SUBFRAME_LEN; i++) {
  495. int64_t temp = hf->gain * src[i - hf->index] << 1;
  496. dest[i] = av_clipl_int32(((dest[i] - src[i]) << 16) + temp +
  497. (1 << 15)) >> 16;
  498. }
  499. }
  500. /**
  501. * Combined synthesis and formant perceptual weighting filer.
  502. *
  503. * @param qnt_lpc quantized lpc coefficients
  504. * @param perf_lpc perceptual filter coefficients
  505. * @param perf_fir perceptual filter fir memory
  506. * @param perf_iir perceptual filter iir memory
  507. * @param scale the filter output will be scaled by 2^scale
  508. */
  509. static void synth_percept_filter(int16_t *qnt_lpc, int16_t *perf_lpc,
  510. int16_t *perf_fir, int16_t *perf_iir,
  511. const int16_t *src, int16_t *dest, int scale)
  512. {
  513. int i, j;
  514. int16_t buf_16[SUBFRAME_LEN + LPC_ORDER];
  515. int64_t buf[SUBFRAME_LEN];
  516. int16_t *bptr_16 = buf_16 + LPC_ORDER;
  517. memcpy(buf_16, perf_fir, sizeof(int16_t) * LPC_ORDER);
  518. memcpy(dest - LPC_ORDER, perf_iir, sizeof(int16_t) * LPC_ORDER);
  519. for (i = 0; i < SUBFRAME_LEN; i++) {
  520. int64_t temp = 0;
  521. for (j = 1; j <= LPC_ORDER; j++)
  522. temp -= qnt_lpc[j - 1] * bptr_16[i - j];
  523. buf[i] = (src[i] << 15) + (temp << 3);
  524. bptr_16[i] = av_clipl_int32(buf[i] + (1 << 15)) >> 16;
  525. }
  526. for (i = 0; i < SUBFRAME_LEN; i++) {
  527. int64_t fir = 0, iir = 0;
  528. for (j = 1; j <= LPC_ORDER; j++) {
  529. fir -= perf_lpc[j - 1] * bptr_16[i - j];
  530. iir += perf_lpc[j + LPC_ORDER - 1] * dest[i - j];
  531. }
  532. dest[i] = av_clipl_int32(((buf[i] + (fir << 3)) << scale) + (iir << 3) +
  533. (1 << 15)) >> 16;
  534. }
  535. memcpy(perf_fir, buf_16 + SUBFRAME_LEN, sizeof(int16_t) * LPC_ORDER);
  536. memcpy(perf_iir, dest + SUBFRAME_LEN - LPC_ORDER,
  537. sizeof(int16_t) * LPC_ORDER);
  538. }
  539. /**
  540. * Compute the adaptive codebook contribution.
  541. *
  542. * @param buf input signal
  543. * @param index the current subframe index
  544. */
  545. static void acb_search(G723_1_Context *p, int16_t *residual,
  546. int16_t *impulse_resp, const int16_t *buf,
  547. int index)
  548. {
  549. int16_t flt_buf[PITCH_ORDER][SUBFRAME_LEN];
  550. const int16_t *cb_tbl = adaptive_cb_gain85;
  551. int ccr_buf[PITCH_ORDER * SUBFRAMES << 2];
  552. int pitch_lag = p->pitch_lag[index >> 1];
  553. int acb_lag = 1;
  554. int acb_gain = 0;
  555. int odd_frame = index & 1;
  556. int iter = 3 + odd_frame;
  557. int count = 0;
  558. int tbl_size = 85;
  559. int i, j, k, l, max;
  560. int64_t temp;
  561. if (!odd_frame) {
  562. if (pitch_lag == PITCH_MIN)
  563. pitch_lag++;
  564. else
  565. pitch_lag = FFMIN(pitch_lag, PITCH_MAX - 5);
  566. }
  567. for (i = 0; i < iter; i++) {
  568. ff_g723_1_get_residual(residual, p->prev_excitation, pitch_lag + i - 1);
  569. for (j = 0; j < SUBFRAME_LEN; j++) {
  570. temp = 0;
  571. for (k = 0; k <= j; k++)
  572. temp += residual[PITCH_ORDER - 1 + k] * impulse_resp[j - k];
  573. flt_buf[PITCH_ORDER - 1][j] = av_clipl_int32((temp << 1) +
  574. (1 << 15)) >> 16;
  575. }
  576. for (j = PITCH_ORDER - 2; j >= 0; j--) {
  577. flt_buf[j][0] = ((residual[j] << 13) + (1 << 14)) >> 15;
  578. for (k = 1; k < SUBFRAME_LEN; k++) {
  579. temp = (flt_buf[j + 1][k - 1] << 15) +
  580. residual[j] * impulse_resp[k];
  581. flt_buf[j][k] = av_clipl_int32((temp << 1) + (1 << 15)) >> 16;
  582. }
  583. }
  584. /* Compute crosscorrelation with the signal */
  585. for (j = 0; j < PITCH_ORDER; j++) {
  586. temp = ff_dot_product(buf, flt_buf[j], SUBFRAME_LEN);
  587. ccr_buf[count++] = av_clipl_int32(temp << 1);
  588. }
  589. /* Compute energies */
  590. for (j = 0; j < PITCH_ORDER; j++) {
  591. ccr_buf[count++] = ff_g723_1_dot_product(flt_buf[j], flt_buf[j],
  592. SUBFRAME_LEN);
  593. }
  594. for (j = 1; j < PITCH_ORDER; j++) {
  595. for (k = 0; k < j; k++) {
  596. temp = ff_dot_product(flt_buf[j], flt_buf[k], SUBFRAME_LEN);
  597. ccr_buf[count++] = av_clipl_int32(temp << 2);
  598. }
  599. }
  600. }
  601. /* Normalize and shorten */
  602. max = 0;
  603. for (i = 0; i < 20 * iter; i++)
  604. max = FFMAX(max, FFABS(ccr_buf[i]));
  605. temp = ff_g723_1_normalize_bits(max, 31);
  606. for (i = 0; i < 20 * iter; i++)
  607. ccr_buf[i] = av_clipl_int32((int64_t) (ccr_buf[i] << temp) +
  608. (1 << 15)) >> 16;
  609. max = 0;
  610. for (i = 0; i < iter; i++) {
  611. /* Select quantization table */
  612. if (!odd_frame && pitch_lag + i - 1 >= SUBFRAME_LEN - 2 ||
  613. odd_frame && pitch_lag >= SUBFRAME_LEN - 2) {
  614. cb_tbl = adaptive_cb_gain170;
  615. tbl_size = 170;
  616. }
  617. for (j = 0, k = 0; j < tbl_size; j++, k += 20) {
  618. temp = 0;
  619. for (l = 0; l < 20; l++)
  620. temp += ccr_buf[20 * i + l] * cb_tbl[k + l];
  621. temp = av_clipl_int32(temp);
  622. if (temp > max) {
  623. max = temp;
  624. acb_gain = j;
  625. acb_lag = i;
  626. }
  627. }
  628. }
  629. if (!odd_frame) {
  630. pitch_lag += acb_lag - 1;
  631. acb_lag = 1;
  632. }
  633. p->pitch_lag[index >> 1] = pitch_lag;
  634. p->subframe[index].ad_cb_lag = acb_lag;
  635. p->subframe[index].ad_cb_gain = acb_gain;
  636. }
  637. /**
  638. * Subtract the adaptive codebook contribution from the input
  639. * to obtain the residual.
  640. *
  641. * @param buf target vector
  642. */
  643. static void sub_acb_contrib(const int16_t *residual, const int16_t *impulse_resp,
  644. int16_t *buf)
  645. {
  646. int i, j;
  647. /* Subtract adaptive CB contribution to obtain the residual */
  648. for (i = 0; i < SUBFRAME_LEN; i++) {
  649. int64_t temp = buf[i] << 14;
  650. for (j = 0; j <= i; j++)
  651. temp -= residual[j] * impulse_resp[i - j];
  652. buf[i] = av_clipl_int32((temp << 2) + (1 << 15)) >> 16;
  653. }
  654. }
  655. /**
  656. * Quantize the residual signal using the fixed codebook (MP-MLQ).
  657. *
  658. * @param optim optimized fixed codebook parameters
  659. * @param buf excitation vector
  660. */
  661. static void get_fcb_param(FCBParam *optim, int16_t *impulse_resp,
  662. int16_t *buf, int pulse_cnt, int pitch_lag)
  663. {
  664. FCBParam param;
  665. int16_t impulse_r[SUBFRAME_LEN];
  666. int16_t temp_corr[SUBFRAME_LEN];
  667. int16_t impulse_corr[SUBFRAME_LEN];
  668. int ccr1[SUBFRAME_LEN];
  669. int ccr2[SUBFRAME_LEN];
  670. int amp, err, max, max_amp_index, min, scale, i, j, k, l;
  671. int64_t temp;
  672. /* Update impulse response */
  673. memcpy(impulse_r, impulse_resp, sizeof(int16_t) * SUBFRAME_LEN);
  674. param.dirac_train = 0;
  675. if (pitch_lag < SUBFRAME_LEN - 2) {
  676. param.dirac_train = 1;
  677. ff_g723_1_gen_dirac_train(impulse_r, pitch_lag);
  678. }
  679. for (i = 0; i < SUBFRAME_LEN; i++)
  680. temp_corr[i] = impulse_r[i] >> 1;
  681. /* Compute impulse response autocorrelation */
  682. temp = ff_g723_1_dot_product(temp_corr, temp_corr, SUBFRAME_LEN);
  683. scale = ff_g723_1_normalize_bits(temp, 31);
  684. impulse_corr[0] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
  685. for (i = 1; i < SUBFRAME_LEN; i++) {
  686. temp = ff_g723_1_dot_product(temp_corr + i, temp_corr,
  687. SUBFRAME_LEN - i);
  688. impulse_corr[i] = av_clipl_int32((temp << scale) + (1 << 15)) >> 16;
  689. }
  690. /* Compute crosscorrelation of impulse response with residual signal */
  691. scale -= 4;
  692. for (i = 0; i < SUBFRAME_LEN; i++) {
  693. temp = ff_g723_1_dot_product(buf + i, impulse_r, SUBFRAME_LEN - i);
  694. if (scale < 0)
  695. ccr1[i] = temp >> -scale;
  696. else
  697. ccr1[i] = av_clipl_int32(temp << scale);
  698. }
  699. /* Search loop */
  700. for (i = 0; i < GRID_SIZE; i++) {
  701. /* Maximize the crosscorrelation */
  702. max = 0;
  703. for (j = i; j < SUBFRAME_LEN; j += GRID_SIZE) {
  704. temp = FFABS(ccr1[j]);
  705. if (temp >= max) {
  706. max = temp;
  707. param.pulse_pos[0] = j;
  708. }
  709. }
  710. /* Quantize the gain (max crosscorrelation/impulse_corr[0]) */
  711. amp = max;
  712. min = 1 << 30;
  713. max_amp_index = GAIN_LEVELS - 2;
  714. for (j = max_amp_index; j >= 2; j--) {
  715. temp = av_clipl_int32((int64_t) fixed_cb_gain[j] *
  716. impulse_corr[0] << 1);
  717. temp = FFABS(temp - amp);
  718. if (temp < min) {
  719. min = temp;
  720. max_amp_index = j;
  721. }
  722. }
  723. max_amp_index--;
  724. /* Select additional gain values */
  725. for (j = 1; j < 5; j++) {
  726. for (k = i; k < SUBFRAME_LEN; k += GRID_SIZE) {
  727. temp_corr[k] = 0;
  728. ccr2[k] = ccr1[k];
  729. }
  730. param.amp_index = max_amp_index + j - 2;
  731. amp = fixed_cb_gain[param.amp_index];
  732. param.pulse_sign[0] = (ccr2[param.pulse_pos[0]] < 0) ? -amp : amp;
  733. temp_corr[param.pulse_pos[0]] = 1;
  734. for (k = 1; k < pulse_cnt; k++) {
  735. max = INT_MIN;
  736. for (l = i; l < SUBFRAME_LEN; l += GRID_SIZE) {
  737. if (temp_corr[l])
  738. continue;
  739. temp = impulse_corr[FFABS(l - param.pulse_pos[k - 1])];
  740. temp = av_clipl_int32((int64_t) temp *
  741. param.pulse_sign[k - 1] << 1);
  742. ccr2[l] -= temp;
  743. temp = FFABS(ccr2[l]);
  744. if (temp > max) {
  745. max = temp;
  746. param.pulse_pos[k] = l;
  747. }
  748. }
  749. param.pulse_sign[k] = (ccr2[param.pulse_pos[k]] < 0) ?
  750. -amp : amp;
  751. temp_corr[param.pulse_pos[k]] = 1;
  752. }
  753. /* Create the error vector */
  754. memset(temp_corr, 0, sizeof(int16_t) * SUBFRAME_LEN);
  755. for (k = 0; k < pulse_cnt; k++)
  756. temp_corr[param.pulse_pos[k]] = param.pulse_sign[k];
  757. for (k = SUBFRAME_LEN - 1; k >= 0; k--) {
  758. temp = 0;
  759. for (l = 0; l <= k; l++) {
  760. int prod = av_clipl_int32((int64_t) temp_corr[l] *
  761. impulse_r[k - l] << 1);
  762. temp = av_clipl_int32(temp + prod);
  763. }
  764. temp_corr[k] = temp << 2 >> 16;
  765. }
  766. /* Compute square of error */
  767. err = 0;
  768. for (k = 0; k < SUBFRAME_LEN; k++) {
  769. int64_t prod;
  770. prod = av_clipl_int32((int64_t) buf[k] * temp_corr[k] << 1);
  771. err = av_clipl_int32(err - prod);
  772. prod = av_clipl_int32((int64_t) temp_corr[k] * temp_corr[k]);
  773. err = av_clipl_int32(err + prod);
  774. }
  775. /* Minimize */
  776. if (err < optim->min_err) {
  777. optim->min_err = err;
  778. optim->grid_index = i;
  779. optim->amp_index = param.amp_index;
  780. optim->dirac_train = param.dirac_train;
  781. for (k = 0; k < pulse_cnt; k++) {
  782. optim->pulse_sign[k] = param.pulse_sign[k];
  783. optim->pulse_pos[k] = param.pulse_pos[k];
  784. }
  785. }
  786. }
  787. }
  788. }
  789. /**
  790. * Encode the pulse position and gain of the current subframe.
  791. *
  792. * @param optim optimized fixed CB parameters
  793. * @param buf excitation vector
  794. */
  795. static void pack_fcb_param(G723_1_Subframe *subfrm, FCBParam *optim,
  796. int16_t *buf, int pulse_cnt)
  797. {
  798. int i, j;
  799. j = PULSE_MAX - pulse_cnt;
  800. subfrm->pulse_sign = 0;
  801. subfrm->pulse_pos = 0;
  802. for (i = 0; i < SUBFRAME_LEN >> 1; i++) {
  803. int val = buf[optim->grid_index + (i << 1)];
  804. if (!val) {
  805. subfrm->pulse_pos += combinatorial_table[j][i];
  806. } else {
  807. subfrm->pulse_sign <<= 1;
  808. if (val < 0)
  809. subfrm->pulse_sign++;
  810. j++;
  811. if (j == PULSE_MAX)
  812. break;
  813. }
  814. }
  815. subfrm->amp_index = optim->amp_index;
  816. subfrm->grid_index = optim->grid_index;
  817. subfrm->dirac_train = optim->dirac_train;
  818. }
  819. /**
  820. * Compute the fixed codebook excitation.
  821. *
  822. * @param buf target vector
  823. * @param impulse_resp impulse response of the combined filter
  824. */
  825. static void fcb_search(G723_1_Context *p, int16_t *impulse_resp,
  826. int16_t *buf, int index)
  827. {
  828. FCBParam optim;
  829. int pulse_cnt = pulses[index];
  830. int i;
  831. optim.min_err = 1 << 30;
  832. get_fcb_param(&optim, impulse_resp, buf, pulse_cnt, SUBFRAME_LEN);
  833. if (p->pitch_lag[index >> 1] < SUBFRAME_LEN - 2) {
  834. get_fcb_param(&optim, impulse_resp, buf, pulse_cnt,
  835. p->pitch_lag[index >> 1]);
  836. }
  837. /* Reconstruct the excitation */
  838. memset(buf, 0, sizeof(int16_t) * SUBFRAME_LEN);
  839. for (i = 0; i < pulse_cnt; i++)
  840. buf[optim.pulse_pos[i]] = optim.pulse_sign[i];
  841. pack_fcb_param(&p->subframe[index], &optim, buf, pulse_cnt);
  842. if (optim.dirac_train)
  843. ff_g723_1_gen_dirac_train(buf, p->pitch_lag[index >> 1]);
  844. }
  845. /**
  846. * Pack the frame parameters into output bitstream.
  847. *
  848. * @param frame output buffer
  849. * @param size size of the buffer
  850. */
  851. static int pack_bitstream(G723_1_Context *p, AVPacket *avpkt)
  852. {
  853. PutBitContext pb;
  854. int info_bits = 0;
  855. int i, temp;
  856. init_put_bits(&pb, avpkt->data, avpkt->size);
  857. put_bits(&pb, 2, info_bits);
  858. put_bits(&pb, 8, p->lsp_index[2]);
  859. put_bits(&pb, 8, p->lsp_index[1]);
  860. put_bits(&pb, 8, p->lsp_index[0]);
  861. put_bits(&pb, 7, p->pitch_lag[0] - PITCH_MIN);
  862. put_bits(&pb, 2, p->subframe[1].ad_cb_lag);
  863. put_bits(&pb, 7, p->pitch_lag[1] - PITCH_MIN);
  864. put_bits(&pb, 2, p->subframe[3].ad_cb_lag);
  865. /* Write 12 bit combined gain */
  866. for (i = 0; i < SUBFRAMES; i++) {
  867. temp = p->subframe[i].ad_cb_gain * GAIN_LEVELS +
  868. p->subframe[i].amp_index;
  869. if (p->cur_rate == RATE_6300)
  870. temp += p->subframe[i].dirac_train << 11;
  871. put_bits(&pb, 12, temp);
  872. }
  873. put_bits(&pb, 1, p->subframe[0].grid_index);
  874. put_bits(&pb, 1, p->subframe[1].grid_index);
  875. put_bits(&pb, 1, p->subframe[2].grid_index);
  876. put_bits(&pb, 1, p->subframe[3].grid_index);
  877. if (p->cur_rate == RATE_6300) {
  878. skip_put_bits(&pb, 1); /* reserved bit */
  879. /* Write 13 bit combined position index */
  880. temp = (p->subframe[0].pulse_pos >> 16) * 810 +
  881. (p->subframe[1].pulse_pos >> 14) * 90 +
  882. (p->subframe[2].pulse_pos >> 16) * 9 +
  883. (p->subframe[3].pulse_pos >> 14);
  884. put_bits(&pb, 13, temp);
  885. put_bits(&pb, 16, p->subframe[0].pulse_pos & 0xffff);
  886. put_bits(&pb, 14, p->subframe[1].pulse_pos & 0x3fff);
  887. put_bits(&pb, 16, p->subframe[2].pulse_pos & 0xffff);
  888. put_bits(&pb, 14, p->subframe[3].pulse_pos & 0x3fff);
  889. put_bits(&pb, 6, p->subframe[0].pulse_sign);
  890. put_bits(&pb, 5, p->subframe[1].pulse_sign);
  891. put_bits(&pb, 6, p->subframe[2].pulse_sign);
  892. put_bits(&pb, 5, p->subframe[3].pulse_sign);
  893. }
  894. flush_put_bits(&pb);
  895. return frame_size[info_bits];
  896. }
  897. static int g723_1_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  898. const AVFrame *frame, int *got_packet_ptr)
  899. {
  900. G723_1_Context *p = avctx->priv_data;
  901. int16_t unq_lpc[LPC_ORDER * SUBFRAMES];
  902. int16_t qnt_lpc[LPC_ORDER * SUBFRAMES];
  903. int16_t cur_lsp[LPC_ORDER];
  904. int16_t weighted_lpc[LPC_ORDER * SUBFRAMES << 1];
  905. int16_t vector[FRAME_LEN + PITCH_MAX];
  906. int offset, ret, i, j;
  907. int16_t *in, *start;
  908. HFParam hf[4];
  909. /* duplicate input */
  910. start = in = av_malloc(frame->nb_samples * sizeof(int16_t));
  911. if (!in)
  912. return AVERROR(ENOMEM);
  913. memcpy(in, frame->data[0], frame->nb_samples * sizeof(int16_t));
  914. highpass_filter(in, &p->hpf_fir_mem, &p->hpf_iir_mem);
  915. memcpy(vector, p->prev_data, HALF_FRAME_LEN * sizeof(int16_t));
  916. memcpy(vector + HALF_FRAME_LEN, in, FRAME_LEN * sizeof(int16_t));
  917. comp_lpc_coeff(vector, unq_lpc);
  918. lpc2lsp(&unq_lpc[LPC_ORDER * 3], p->prev_lsp, cur_lsp);
  919. lsp_quantize(p->lsp_index, cur_lsp, p->prev_lsp);
  920. /* Update memory */
  921. memcpy(vector + LPC_ORDER, p->prev_data + SUBFRAME_LEN,
  922. sizeof(int16_t) * SUBFRAME_LEN);
  923. memcpy(vector + LPC_ORDER + SUBFRAME_LEN, in,
  924. sizeof(int16_t) * (HALF_FRAME_LEN + SUBFRAME_LEN));
  925. memcpy(p->prev_data, in + HALF_FRAME_LEN,
  926. sizeof(int16_t) * HALF_FRAME_LEN);
  927. memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
  928. perceptual_filter(p, weighted_lpc, unq_lpc, vector);
  929. memcpy(in, vector + LPC_ORDER, sizeof(int16_t) * FRAME_LEN);
  930. memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
  931. memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
  932. ff_g723_1_scale_vector(vector, vector, FRAME_LEN + PITCH_MAX);
  933. p->pitch_lag[0] = estimate_pitch(vector, PITCH_MAX);
  934. p->pitch_lag[1] = estimate_pitch(vector, PITCH_MAX + HALF_FRAME_LEN);
  935. for (i = PITCH_MAX, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
  936. comp_harmonic_coeff(vector + i, p->pitch_lag[j >> 1], hf + j);
  937. memcpy(vector, p->prev_weight_sig, sizeof(int16_t) * PITCH_MAX);
  938. memcpy(vector + PITCH_MAX, in, sizeof(int16_t) * FRAME_LEN);
  939. memcpy(p->prev_weight_sig, vector + FRAME_LEN, sizeof(int16_t) * PITCH_MAX);
  940. for (i = 0, j = 0; j < SUBFRAMES; i += SUBFRAME_LEN, j++)
  941. harmonic_filter(hf + j, vector + PITCH_MAX + i, in + i);
  942. ff_g723_1_inverse_quant(cur_lsp, p->prev_lsp, p->lsp_index, 0);
  943. ff_g723_1_lsp_interpolate(qnt_lpc, cur_lsp, p->prev_lsp);
  944. memcpy(p->prev_lsp, cur_lsp, sizeof(int16_t) * LPC_ORDER);
  945. offset = 0;
  946. for (i = 0; i < SUBFRAMES; i++) {
  947. int16_t impulse_resp[SUBFRAME_LEN];
  948. int16_t residual[SUBFRAME_LEN + PITCH_ORDER - 1];
  949. int16_t flt_in[SUBFRAME_LEN];
  950. int16_t zero[LPC_ORDER], fir[LPC_ORDER], iir[LPC_ORDER];
  951. /**
  952. * Compute the combined impulse response of the synthesis filter,
  953. * formant perceptual weighting filter and harmonic noise shaping filter
  954. */
  955. memset(zero, 0, sizeof(int16_t) * LPC_ORDER);
  956. memset(vector, 0, sizeof(int16_t) * PITCH_MAX);
  957. memset(flt_in, 0, sizeof(int16_t) * SUBFRAME_LEN);
  958. flt_in[0] = 1 << 13; /* Unit impulse */
  959. synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
  960. zero, zero, flt_in, vector + PITCH_MAX, 1);
  961. harmonic_filter(hf + i, vector + PITCH_MAX, impulse_resp);
  962. /* Compute the combined zero input response */
  963. flt_in[0] = 0;
  964. memcpy(fir, p->perf_fir_mem, sizeof(int16_t) * LPC_ORDER);
  965. memcpy(iir, p->perf_iir_mem, sizeof(int16_t) * LPC_ORDER);
  966. synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
  967. fir, iir, flt_in, vector + PITCH_MAX, 0);
  968. memcpy(vector, p->harmonic_mem, sizeof(int16_t) * PITCH_MAX);
  969. harmonic_noise_sub(hf + i, vector + PITCH_MAX, in);
  970. acb_search(p, residual, impulse_resp, in, i);
  971. ff_g723_1_gen_acb_excitation(residual, p->prev_excitation,
  972. p->pitch_lag[i >> 1], &p->subframe[i],
  973. RATE_6300);
  974. sub_acb_contrib(residual, impulse_resp, in);
  975. fcb_search(p, impulse_resp, in, i);
  976. /* Reconstruct the excitation */
  977. ff_g723_1_gen_acb_excitation(impulse_resp, p->prev_excitation,
  978. p->pitch_lag[i >> 1], &p->subframe[i],
  979. RATE_6300);
  980. memmove(p->prev_excitation, p->prev_excitation + SUBFRAME_LEN,
  981. sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
  982. for (j = 0; j < SUBFRAME_LEN; j++)
  983. in[j] = av_clip_int16((in[j] << 1) + impulse_resp[j]);
  984. memcpy(p->prev_excitation + PITCH_MAX - SUBFRAME_LEN, in,
  985. sizeof(int16_t) * SUBFRAME_LEN);
  986. /* Update filter memories */
  987. synth_percept_filter(qnt_lpc + offset, weighted_lpc + (offset << 1),
  988. p->perf_fir_mem, p->perf_iir_mem,
  989. in, vector + PITCH_MAX, 0);
  990. memmove(p->harmonic_mem, p->harmonic_mem + SUBFRAME_LEN,
  991. sizeof(int16_t) * (PITCH_MAX - SUBFRAME_LEN));
  992. memcpy(p->harmonic_mem + PITCH_MAX - SUBFRAME_LEN, vector + PITCH_MAX,
  993. sizeof(int16_t) * SUBFRAME_LEN);
  994. in += SUBFRAME_LEN;
  995. offset += LPC_ORDER;
  996. }
  997. av_free(start);
  998. ret = ff_alloc_packet(avpkt, 24);
  999. if (ret < 0)
  1000. return ret;
  1001. *got_packet_ptr = 1;
  1002. return pack_bitstream(p, avpkt);
  1003. }
  1004. AVCodec ff_g723_1_encoder = {
  1005. .name = "g723_1",
  1006. .long_name = NULL_IF_CONFIG_SMALL("G.723.1"),
  1007. .type = AVMEDIA_TYPE_AUDIO,
  1008. .id = AV_CODEC_ID_G723_1,
  1009. .priv_data_size = sizeof(G723_1_Context),
  1010. .init = g723_1_encode_init,
  1011. .encode2 = g723_1_encode_frame,
  1012. .sample_fmts = (const enum AVSampleFormat[]) {
  1013. AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
  1014. },
  1015. };