g722.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * G.722 ADPCM audio encoder/decoder
  3. *
  4. * Copyright (c) CMU 1993 Computer Science, Speech Group
  5. * Chengxiang Lu and Alex Hauptmann
  6. * Copyright (c) 2005 Steve Underwood <steveu at coppice.org>
  7. * Copyright (c) 2009 Kenan Gillet
  8. * Copyright (c) 2010 Martin Storsjo
  9. *
  10. * This file is part of FFmpeg.
  11. *
  12. * FFmpeg is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * FFmpeg is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with FFmpeg; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. /**
  27. * @file
  28. *
  29. * G.722 ADPCM audio codec
  30. *
  31. * This G.722 decoder is a bit-exact implementation of the ITU G.722
  32. * specification for all three specified bitrates - 64000bps, 56000bps
  33. * and 48000bps. It passes the ITU tests.
  34. *
  35. * @note For the 56000bps and 48000bps bitrates, the lowest 1 or 2 bits
  36. * respectively of each byte are ignored.
  37. */
  38. #include "avcodec.h"
  39. #include "mathops.h"
  40. #include "get_bits.h"
  41. #define PREV_SAMPLES_BUF_SIZE 1024
  42. #define FREEZE_INTERVAL 128
  43. typedef struct {
  44. int16_t prev_samples[PREV_SAMPLES_BUF_SIZE]; ///< memory of past decoded samples
  45. int prev_samples_pos; ///< the number of values in prev_samples
  46. /**
  47. * The band[0] and band[1] correspond respectively to the lower band and higher band.
  48. */
  49. struct G722Band {
  50. int16_t s_predictor; ///< predictor output value
  51. int32_t s_zero; ///< previous output signal from zero predictor
  52. int8_t part_reconst_mem[2]; ///< signs of previous partially reconstructed signals
  53. int16_t prev_qtzd_reconst; ///< previous quantized reconstructed signal (internal value, using low_inv_quant4)
  54. int16_t pole_mem[2]; ///< second-order pole section coefficient buffer
  55. int32_t diff_mem[6]; ///< quantizer difference signal memory
  56. int16_t zero_mem[6]; ///< Seventh-order zero section coefficient buffer
  57. int16_t log_factor; ///< delayed 2-logarithmic quantizer factor
  58. int16_t scale_factor; ///< delayed quantizer scale factor
  59. } band[2];
  60. struct TrellisNode {
  61. struct G722Band state;
  62. uint32_t ssd;
  63. int path;
  64. } *node_buf[2], **nodep_buf[2];
  65. struct TrellisPath {
  66. int value;
  67. int prev;
  68. } *paths[2];
  69. } G722Context;
  70. static const int8_t sign_lookup[2] = { -1, 1 };
  71. static const int16_t inv_log2_table[32] = {
  72. 2048, 2093, 2139, 2186, 2233, 2282, 2332, 2383,
  73. 2435, 2489, 2543, 2599, 2656, 2714, 2774, 2834,
  74. 2896, 2960, 3025, 3091, 3158, 3228, 3298, 3371,
  75. 3444, 3520, 3597, 3676, 3756, 3838, 3922, 4008
  76. };
  77. static const int16_t high_log_factor_step[2] = { 798, -214 };
  78. static const int16_t high_inv_quant[4] = { -926, -202, 926, 202 };
  79. /**
  80. * low_log_factor_step[index] == wl[rl42[index]]
  81. */
  82. static const int16_t low_log_factor_step[16] = {
  83. -60, 3042, 1198, 538, 334, 172, 58, -30,
  84. 3042, 1198, 538, 334, 172, 58, -30, -60
  85. };
  86. static const int16_t low_inv_quant4[16] = {
  87. 0, -2557, -1612, -1121, -786, -530, -323, -150,
  88. 2557, 1612, 1121, 786, 530, 323, 150, 0
  89. };
  90. /**
  91. * quadrature mirror filter (QMF) coefficients
  92. *
  93. * ITU-T G.722 Table 11
  94. */
  95. static const int16_t qmf_coeffs[12] = {
  96. 3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11,
  97. };
  98. /**
  99. * adaptive predictor
  100. *
  101. * @param cur_diff the dequantized and scaled delta calculated from the
  102. * current codeword
  103. */
  104. static void do_adaptive_prediction(struct G722Band *band, const int cur_diff)
  105. {
  106. int sg[2], limit, i, cur_qtzd_reconst;
  107. const int cur_part_reconst = band->s_zero + cur_diff < 0;
  108. sg[0] = sign_lookup[cur_part_reconst != band->part_reconst_mem[0]];
  109. sg[1] = sign_lookup[cur_part_reconst == band->part_reconst_mem[1]];
  110. band->part_reconst_mem[1] = band->part_reconst_mem[0];
  111. band->part_reconst_mem[0] = cur_part_reconst;
  112. band->pole_mem[1] = av_clip((sg[0] * av_clip(band->pole_mem[0], -8191, 8191) >> 5) +
  113. (sg[1] << 7) + (band->pole_mem[1] * 127 >> 7), -12288, 12288);
  114. limit = 15360 - band->pole_mem[1];
  115. band->pole_mem[0] = av_clip(-192 * sg[0] + (band->pole_mem[0] * 255 >> 8), -limit, limit);
  116. if (cur_diff) {
  117. for (i = 0; i < 6; i++)
  118. band->zero_mem[i] = ((band->zero_mem[i]*255) >> 8) +
  119. ((band->diff_mem[i]^cur_diff) < 0 ? -128 : 128);
  120. } else
  121. for (i = 0; i < 6; i++)
  122. band->zero_mem[i] = (band->zero_mem[i]*255) >> 8;
  123. for (i = 5; i > 0; i--)
  124. band->diff_mem[i] = band->diff_mem[i-1];
  125. band->diff_mem[0] = av_clip_int16(cur_diff << 1);
  126. band->s_zero = 0;
  127. for (i = 5; i >= 0; i--)
  128. band->s_zero += (band->zero_mem[i]*band->diff_mem[i]) >> 15;
  129. cur_qtzd_reconst = av_clip_int16((band->s_predictor + cur_diff) << 1);
  130. band->s_predictor = av_clip_int16(band->s_zero +
  131. (band->pole_mem[0] * cur_qtzd_reconst >> 15) +
  132. (band->pole_mem[1] * band->prev_qtzd_reconst >> 15));
  133. band->prev_qtzd_reconst = cur_qtzd_reconst;
  134. }
  135. static int inline linear_scale_factor(const int log_factor)
  136. {
  137. const int wd1 = inv_log2_table[(log_factor >> 6) & 31];
  138. const int shift = log_factor >> 11;
  139. return shift < 0 ? wd1 >> -shift : wd1 << shift;
  140. }
  141. static void update_low_predictor(struct G722Band *band, const int ilow)
  142. {
  143. do_adaptive_prediction(band,
  144. band->scale_factor * low_inv_quant4[ilow] >> 10);
  145. // quantizer adaptation
  146. band->log_factor = av_clip((band->log_factor * 127 >> 7) +
  147. low_log_factor_step[ilow], 0, 18432);
  148. band->scale_factor = linear_scale_factor(band->log_factor - (8 << 11));
  149. }
  150. static void update_high_predictor(struct G722Band *band, const int dhigh,
  151. const int ihigh)
  152. {
  153. do_adaptive_prediction(band, dhigh);
  154. // quantizer adaptation
  155. band->log_factor = av_clip((band->log_factor * 127 >> 7) +
  156. high_log_factor_step[ihigh&1], 0, 22528);
  157. band->scale_factor = linear_scale_factor(band->log_factor - (10 << 11));
  158. }
  159. static void apply_qmf(const int16_t *prev_samples, int *xout1, int *xout2)
  160. {
  161. int i;
  162. *xout1 = 0;
  163. *xout2 = 0;
  164. for (i = 0; i < 12; i++) {
  165. MAC16(*xout2, prev_samples[2*i ], qmf_coeffs[i ]);
  166. MAC16(*xout1, prev_samples[2*i+1], qmf_coeffs[11-i]);
  167. }
  168. }
  169. static av_cold int g722_init(AVCodecContext * avctx)
  170. {
  171. G722Context *c = avctx->priv_data;
  172. if (avctx->channels != 1) {
  173. av_log(avctx, AV_LOG_ERROR, "Only mono tracks are allowed.\n");
  174. return AVERROR_INVALIDDATA;
  175. }
  176. avctx->sample_fmt = AV_SAMPLE_FMT_S16;
  177. switch (avctx->bits_per_coded_sample) {
  178. case 8:
  179. case 7:
  180. case 6:
  181. break;
  182. default:
  183. av_log(avctx, AV_LOG_WARNING, "Unsupported bits_per_coded_sample [%d], "
  184. "assuming 8\n",
  185. avctx->bits_per_coded_sample);
  186. case 0:
  187. avctx->bits_per_coded_sample = 8;
  188. break;
  189. }
  190. c->band[0].scale_factor = 8;
  191. c->band[1].scale_factor = 2;
  192. c->prev_samples_pos = 22;
  193. if (avctx->lowres)
  194. avctx->sample_rate /= 2;
  195. if (avctx->trellis) {
  196. int frontier = 1 << avctx->trellis;
  197. int max_paths = frontier * FREEZE_INTERVAL;
  198. int i;
  199. for (i = 0; i < 2; i++) {
  200. c->paths[i] = av_mallocz(max_paths * sizeof(**c->paths));
  201. c->node_buf[i] = av_mallocz(2 * frontier * sizeof(**c->node_buf));
  202. c->nodep_buf[i] = av_mallocz(2 * frontier * sizeof(**c->nodep_buf));
  203. }
  204. }
  205. return 0;
  206. }
  207. static av_cold int g722_close(AVCodecContext *avctx)
  208. {
  209. G722Context *c = avctx->priv_data;
  210. int i;
  211. for (i = 0; i < 2; i++) {
  212. av_freep(&c->paths[i]);
  213. av_freep(&c->node_buf[i]);
  214. av_freep(&c->nodep_buf[i]);
  215. }
  216. return 0;
  217. }
  218. #if CONFIG_ADPCM_G722_DECODER
  219. static const int16_t low_inv_quant5[32] = {
  220. -35, -35, -2919, -2195, -1765, -1458, -1219, -1023,
  221. -858, -714, -587, -473, -370, -276, -190, -110,
  222. 2919, 2195, 1765, 1458, 1219, 1023, 858, 714,
  223. 587, 473, 370, 276, 190, 110, 35, -35
  224. };
  225. static const int16_t low_inv_quant6[64] = {
  226. -17, -17, -17, -17, -3101, -2738, -2376, -2088,
  227. -1873, -1689, -1535, -1399, -1279, -1170, -1072, -982,
  228. -899, -822, -750, -682, -618, -558, -501, -447,
  229. -396, -347, -300, -254, -211, -170, -130, -91,
  230. 3101, 2738, 2376, 2088, 1873, 1689, 1535, 1399,
  231. 1279, 1170, 1072, 982, 899, 822, 750, 682,
  232. 618, 558, 501, 447, 396, 347, 300, 254,
  233. 211, 170, 130, 91, 54, 17, -54, -17
  234. };
  235. static const int16_t *low_inv_quants[3] = { low_inv_quant6, low_inv_quant5,
  236. low_inv_quant4 };
  237. static int g722_decode_frame(AVCodecContext *avctx, void *data,
  238. int *data_size, AVPacket *avpkt)
  239. {
  240. G722Context *c = avctx->priv_data;
  241. int16_t *out_buf = data;
  242. int j, out_len = 0;
  243. const int skip = 8 - avctx->bits_per_coded_sample;
  244. const int16_t *quantizer_table = low_inv_quants[skip];
  245. GetBitContext gb;
  246. init_get_bits(&gb, avpkt->data, avpkt->size * 8);
  247. for (j = 0; j < avpkt->size; j++) {
  248. int ilow, ihigh, rlow;
  249. ihigh = get_bits(&gb, 2);
  250. ilow = get_bits(&gb, 6 - skip);
  251. skip_bits(&gb, skip);
  252. rlow = av_clip((c->band[0].scale_factor * quantizer_table[ilow] >> 10)
  253. + c->band[0].s_predictor, -16384, 16383);
  254. update_low_predictor(&c->band[0], ilow >> (2 - skip));
  255. if (!avctx->lowres) {
  256. const int dhigh = c->band[1].scale_factor *
  257. high_inv_quant[ihigh] >> 10;
  258. const int rhigh = av_clip(dhigh + c->band[1].s_predictor,
  259. -16384, 16383);
  260. int xout1, xout2;
  261. update_high_predictor(&c->band[1], dhigh, ihigh);
  262. c->prev_samples[c->prev_samples_pos++] = rlow + rhigh;
  263. c->prev_samples[c->prev_samples_pos++] = rlow - rhigh;
  264. apply_qmf(c->prev_samples + c->prev_samples_pos - 24,
  265. &xout1, &xout2);
  266. out_buf[out_len++] = av_clip_int16(xout1 >> 12);
  267. out_buf[out_len++] = av_clip_int16(xout2 >> 12);
  268. if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) {
  269. memmove(c->prev_samples,
  270. c->prev_samples + c->prev_samples_pos - 22,
  271. 22 * sizeof(c->prev_samples[0]));
  272. c->prev_samples_pos = 22;
  273. }
  274. } else
  275. out_buf[out_len++] = rlow;
  276. }
  277. *data_size = out_len << 1;
  278. return avpkt->size;
  279. }
  280. AVCodec adpcm_g722_decoder = {
  281. .name = "g722",
  282. .type = AVMEDIA_TYPE_AUDIO,
  283. .id = CODEC_ID_ADPCM_G722,
  284. .priv_data_size = sizeof(G722Context),
  285. .init = g722_init,
  286. .decode = g722_decode_frame,
  287. .long_name = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
  288. .max_lowres = 1,
  289. };
  290. #endif
  291. #if CONFIG_ADPCM_G722_ENCODER
  292. static const int16_t low_quant[33] = {
  293. 35, 72, 110, 150, 190, 233, 276, 323,
  294. 370, 422, 473, 530, 587, 650, 714, 786,
  295. 858, 940, 1023, 1121, 1219, 1339, 1458, 1612,
  296. 1765, 1980, 2195, 2557, 2919
  297. };
  298. static inline void filter_samples(G722Context *c, const int16_t *samples,
  299. int *xlow, int *xhigh)
  300. {
  301. int xout1, xout2;
  302. c->prev_samples[c->prev_samples_pos++] = samples[0];
  303. c->prev_samples[c->prev_samples_pos++] = samples[1];
  304. apply_qmf(c->prev_samples + c->prev_samples_pos - 24, &xout1, &xout2);
  305. *xlow = xout1 + xout2 >> 13;
  306. *xhigh = xout1 - xout2 >> 13;
  307. if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) {
  308. memmove(c->prev_samples,
  309. c->prev_samples + c->prev_samples_pos - 22,
  310. 22 * sizeof(c->prev_samples[0]));
  311. c->prev_samples_pos = 22;
  312. }
  313. }
  314. static inline int encode_high(const struct G722Band *state, int xhigh)
  315. {
  316. int diff = av_clip_int16(xhigh - state->s_predictor);
  317. int pred = 141 * state->scale_factor >> 8;
  318. /* = diff >= 0 ? (diff < pred) + 2 : diff >= -pred */
  319. return ((diff ^ (diff >> (sizeof(diff)*8-1))) < pred) + 2*(diff >= 0);
  320. }
  321. static inline int encode_low(const struct G722Band* state, int xlow)
  322. {
  323. int diff = av_clip_int16(xlow - state->s_predictor);
  324. /* = diff >= 0 ? diff : -(diff + 1) */
  325. int limit = diff ^ (diff >> (sizeof(diff)*8-1));
  326. int i = 0;
  327. limit = limit + 1 << 10;
  328. if (limit > low_quant[8] * state->scale_factor)
  329. i = 9;
  330. while (i < 29 && limit > low_quant[i] * state->scale_factor)
  331. i++;
  332. return (diff < 0 ? (i < 2 ? 63 : 33) : 61) - i;
  333. }
  334. static int g722_encode_trellis(AVCodecContext *avctx,
  335. uint8_t *dst, int buf_size, void *data)
  336. {
  337. G722Context *c = avctx->priv_data;
  338. const int16_t *samples = data;
  339. int i, j, k;
  340. int frontier = 1 << avctx->trellis;
  341. struct TrellisNode **nodes[2];
  342. struct TrellisNode **nodes_next[2];
  343. int pathn[2] = {0, 0}, froze = -1;
  344. struct TrellisPath *p[2];
  345. for (i = 0; i < 2; i++) {
  346. nodes[i] = c->nodep_buf[i];
  347. nodes_next[i] = c->nodep_buf[i] + frontier;
  348. memset(c->nodep_buf[i], 0, 2 * frontier * sizeof(*c->nodep_buf));
  349. nodes[i][0] = c->node_buf[i] + frontier;
  350. nodes[i][0]->ssd = 0;
  351. nodes[i][0]->path = 0;
  352. nodes[i][0]->state = c->band[i];
  353. }
  354. for (i = 0; i < buf_size >> 1; i++) {
  355. int xlow, xhigh;
  356. struct TrellisNode *next[2];
  357. int heap_pos[2] = {0, 0};
  358. for (j = 0; j < 2; j++) {
  359. next[j] = c->node_buf[j] + frontier*(i & 1);
  360. memset(nodes_next[j], 0, frontier * sizeof(**nodes_next));
  361. }
  362. filter_samples(c, &samples[2*i], &xlow, &xhigh);
  363. for (j = 0; j < frontier && nodes[0][j]; j++) {
  364. /* Only k >> 2 affects the future adaptive state, therefore testing
  365. * small steps that don't change k >> 2 is useless, the orignal
  366. * value from encode_low is better than them. Since we step k
  367. * in steps of 4, make sure range is a multiple of 4, so that
  368. * we don't miss the original value from encode_low. */
  369. int range = j < frontier/2 ? 4 : 0;
  370. struct TrellisNode *cur_node = nodes[0][j];
  371. int ilow = encode_low(&cur_node->state, xlow);
  372. for (k = ilow - range; k <= ilow + range && k <= 63; k += 4) {
  373. int decoded, dec_diff, pos;
  374. uint32_t ssd;
  375. struct TrellisNode* node;
  376. if (k < 0)
  377. continue;
  378. decoded = av_clip((cur_node->state.scale_factor *
  379. low_inv_quant6[k] >> 10)
  380. + cur_node->state.s_predictor, -16384, 16383);
  381. dec_diff = xlow - decoded;
  382. #define STORE_NODE(index, UPDATE, VALUE)\
  383. ssd = cur_node->ssd + dec_diff*dec_diff;\
  384. /* Check for wraparound. Using 64 bit ssd counters would \
  385. * be simpler, but is slower on x86 32 bit. */\
  386. if (ssd < cur_node->ssd)\
  387. continue;\
  388. if (heap_pos[index] < frontier) {\
  389. pos = heap_pos[index]++;\
  390. assert(pathn[index] < FREEZE_INTERVAL * frontier);\
  391. node = nodes_next[index][pos] = next[index]++;\
  392. node->path = pathn[index]++;\
  393. } else {\
  394. /* Try to replace one of the leaf nodes with the new \
  395. * one, but not always testing the same leaf position */\
  396. pos = (frontier>>1) + (heap_pos[index] & ((frontier>>1) - 1));\
  397. if (ssd >= nodes_next[index][pos]->ssd)\
  398. continue;\
  399. heap_pos[index]++;\
  400. node = nodes_next[index][pos];\
  401. }\
  402. node->ssd = ssd;\
  403. node->state = cur_node->state;\
  404. UPDATE;\
  405. c->paths[index][node->path].value = VALUE;\
  406. c->paths[index][node->path].prev = cur_node->path;\
  407. /* Sift the newly inserted node up in the heap to restore \
  408. * the heap property */\
  409. while (pos > 0) {\
  410. int parent = (pos - 1) >> 1;\
  411. if (nodes_next[index][parent]->ssd <= ssd)\
  412. break;\
  413. FFSWAP(struct TrellisNode*, nodes_next[index][parent],\
  414. nodes_next[index][pos]);\
  415. pos = parent;\
  416. }
  417. STORE_NODE(0, update_low_predictor(&node->state, k >> 2), k);
  418. }
  419. }
  420. for (j = 0; j < frontier && nodes[1][j]; j++) {
  421. int ihigh;
  422. struct TrellisNode *cur_node = nodes[1][j];
  423. /* We don't try to get any initial guess for ihigh via
  424. * encode_high - since there's only 4 possible values, test
  425. * them all. Testing all of these gives a much, much larger
  426. * gain than testing a larger range around ilow. */
  427. for (ihigh = 0; ihigh < 4; ihigh++) {
  428. int dhigh, decoded, dec_diff, pos;
  429. uint32_t ssd;
  430. struct TrellisNode* node;
  431. dhigh = cur_node->state.scale_factor *
  432. high_inv_quant[ihigh] >> 10;
  433. decoded = av_clip(dhigh + cur_node->state.s_predictor,
  434. -16384, 16383);
  435. dec_diff = xhigh - decoded;
  436. STORE_NODE(1, update_high_predictor(&node->state, dhigh, ihigh), ihigh);
  437. }
  438. }
  439. for (j = 0; j < 2; j++) {
  440. FFSWAP(struct TrellisNode**, nodes[j], nodes_next[j]);
  441. if (nodes[j][0]->ssd > (1 << 16)) {
  442. for (k = 1; k < frontier && nodes[j][k]; k++)
  443. nodes[j][k]->ssd -= nodes[j][0]->ssd;
  444. nodes[j][0]->ssd = 0;
  445. }
  446. }
  447. if (i == froze + FREEZE_INTERVAL) {
  448. p[0] = &c->paths[0][nodes[0][0]->path];
  449. p[1] = &c->paths[1][nodes[1][0]->path];
  450. for (j = i; j > froze; j--) {
  451. dst[j] = p[1]->value << 6 | p[0]->value;
  452. p[0] = &c->paths[0][p[0]->prev];
  453. p[1] = &c->paths[1][p[1]->prev];
  454. }
  455. froze = i;
  456. pathn[0] = pathn[1] = 0;
  457. memset(nodes[0] + 1, 0, (frontier - 1)*sizeof(**nodes));
  458. memset(nodes[1] + 1, 0, (frontier - 1)*sizeof(**nodes));
  459. }
  460. }
  461. p[0] = &c->paths[0][nodes[0][0]->path];
  462. p[1] = &c->paths[1][nodes[1][0]->path];
  463. for (j = i; j > froze; j--) {
  464. dst[j] = p[1]->value << 6 | p[0]->value;
  465. p[0] = &c->paths[0][p[0]->prev];
  466. p[1] = &c->paths[1][p[1]->prev];
  467. }
  468. c->band[0] = nodes[0][0]->state;
  469. c->band[1] = nodes[1][0]->state;
  470. return i;
  471. }
  472. static int g722_encode_frame(AVCodecContext *avctx,
  473. uint8_t *dst, int buf_size, void *data)
  474. {
  475. G722Context *c = avctx->priv_data;
  476. const int16_t *samples = data;
  477. int i;
  478. if (avctx->trellis)
  479. return g722_encode_trellis(avctx, dst, buf_size, data);
  480. for (i = 0; i < buf_size >> 1; i++) {
  481. int xlow, xhigh, ihigh, ilow;
  482. filter_samples(c, &samples[2*i], &xlow, &xhigh);
  483. ihigh = encode_high(&c->band[1], xhigh);
  484. ilow = encode_low(&c->band[0], xlow);
  485. update_high_predictor(&c->band[1], c->band[1].scale_factor *
  486. high_inv_quant[ihigh] >> 10, ihigh);
  487. update_low_predictor(&c->band[0], ilow >> 2);
  488. *dst++ = ihigh << 6 | ilow;
  489. }
  490. return i;
  491. }
  492. AVCodec adpcm_g722_encoder = {
  493. .name = "g722",
  494. .type = AVMEDIA_TYPE_AUDIO,
  495. .id = CODEC_ID_ADPCM_G722,
  496. .priv_data_size = sizeof(G722Context),
  497. .init = g722_init,
  498. .close = g722_close,
  499. .encode = g722_encode_frame,
  500. .long_name = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
  501. .sample_fmts = (enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
  502. };
  503. #endif