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.

386 lines
13KB

  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. typedef struct {
  43. int16_t prev_samples[PREV_SAMPLES_BUF_SIZE]; ///< memory of past decoded samples
  44. int prev_samples_pos; ///< the number of values in prev_samples
  45. /**
  46. * The band[0] and band[1] correspond respectively to the lower band and higher band.
  47. */
  48. struct G722Band {
  49. int16_t s_predictor; ///< predictor output value
  50. int32_t s_zero; ///< previous output signal from zero predictor
  51. int8_t part_reconst_mem[2]; ///< signs of previous partially reconstructed signals
  52. int16_t prev_qtzd_reconst; ///< previous quantized reconstructed signal (internal value, using low_inv_quant4)
  53. int16_t pole_mem[2]; ///< second-order pole section coefficient buffer
  54. int32_t diff_mem[6]; ///< quantizer difference signal memory
  55. int16_t zero_mem[6]; ///< Seventh-order zero section coefficient buffer
  56. int16_t log_factor; ///< delayed 2-logarithmic quantizer factor
  57. int16_t scale_factor; ///< delayed quantizer scale factor
  58. } band[2];
  59. } G722Context;
  60. static const int8_t sign_lookup[2] = { -1, 1 };
  61. static const int16_t inv_log2_table[32] = {
  62. 2048, 2093, 2139, 2186, 2233, 2282, 2332, 2383,
  63. 2435, 2489, 2543, 2599, 2656, 2714, 2774, 2834,
  64. 2896, 2960, 3025, 3091, 3158, 3228, 3298, 3371,
  65. 3444, 3520, 3597, 3676, 3756, 3838, 3922, 4008
  66. };
  67. static const int16_t high_log_factor_step[2] = { 798, -214 };
  68. static const int16_t high_inv_quant[4] = { -926, -202, 926, 202 };
  69. /**
  70. * low_log_factor_step[index] == wl[rl42[index]]
  71. */
  72. static const int16_t low_log_factor_step[16] = {
  73. -60, 3042, 1198, 538, 334, 172, 58, -30,
  74. 3042, 1198, 538, 334, 172, 58, -30, -60
  75. };
  76. static const int16_t low_inv_quant4[16] = {
  77. 0, -2557, -1612, -1121, -786, -530, -323, -150,
  78. 2557, 1612, 1121, 786, 530, 323, 150, 0
  79. };
  80. /**
  81. * quadrature mirror filter (QMF) coefficients
  82. *
  83. * ITU-T G.722 Table 11
  84. */
  85. static const int16_t qmf_coeffs[12] = {
  86. 3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11,
  87. };
  88. /**
  89. * adaptive predictor
  90. *
  91. * @param cur_diff the dequantized and scaled delta calculated from the
  92. * current codeword
  93. */
  94. static void do_adaptive_prediction(struct G722Band *band, const int cur_diff)
  95. {
  96. int sg[2], limit, i, cur_qtzd_reconst;
  97. const int cur_part_reconst = band->s_zero + cur_diff < 0;
  98. sg[0] = sign_lookup[cur_part_reconst != band->part_reconst_mem[0]];
  99. sg[1] = sign_lookup[cur_part_reconst == band->part_reconst_mem[1]];
  100. band->part_reconst_mem[1] = band->part_reconst_mem[0];
  101. band->part_reconst_mem[0] = cur_part_reconst;
  102. band->pole_mem[1] = av_clip((sg[0] * av_clip(band->pole_mem[0], -8191, 8191) >> 5) +
  103. (sg[1] << 7) + (band->pole_mem[1] * 127 >> 7), -12288, 12288);
  104. limit = 15360 - band->pole_mem[1];
  105. band->pole_mem[0] = av_clip(-192 * sg[0] + (band->pole_mem[0] * 255 >> 8), -limit, limit);
  106. if (cur_diff) {
  107. for (i = 0; i < 6; i++)
  108. band->zero_mem[i] = ((band->zero_mem[i]*255) >> 8) +
  109. ((band->diff_mem[i]^cur_diff) < 0 ? -128 : 128);
  110. } else
  111. for (i = 0; i < 6; i++)
  112. band->zero_mem[i] = (band->zero_mem[i]*255) >> 8;
  113. for (i = 5; i > 0; i--)
  114. band->diff_mem[i] = band->diff_mem[i-1];
  115. band->diff_mem[0] = av_clip_int16(cur_diff << 1);
  116. band->s_zero = 0;
  117. for (i = 5; i >= 0; i--)
  118. band->s_zero += (band->zero_mem[i]*band->diff_mem[i]) >> 15;
  119. cur_qtzd_reconst = av_clip_int16((band->s_predictor + cur_diff) << 1);
  120. band->s_predictor = av_clip_int16(band->s_zero +
  121. (band->pole_mem[0] * cur_qtzd_reconst >> 15) +
  122. (band->pole_mem[1] * band->prev_qtzd_reconst >> 15));
  123. band->prev_qtzd_reconst = cur_qtzd_reconst;
  124. }
  125. static int inline linear_scale_factor(const int log_factor)
  126. {
  127. const int wd1 = inv_log2_table[(log_factor >> 6) & 31];
  128. const int shift = log_factor >> 11;
  129. return shift < 0 ? wd1 >> -shift : wd1 << shift;
  130. }
  131. static void update_low_predictor(struct G722Band *band, const int ilow)
  132. {
  133. do_adaptive_prediction(band,
  134. band->scale_factor * low_inv_quant4[ilow] >> 10);
  135. // quantizer adaptation
  136. band->log_factor = av_clip((band->log_factor * 127 >> 7) +
  137. low_log_factor_step[ilow], 0, 18432);
  138. band->scale_factor = linear_scale_factor(band->log_factor - (8 << 11));
  139. }
  140. static void update_high_predictor(struct G722Band *band, const int dhigh,
  141. const int ihigh)
  142. {
  143. do_adaptive_prediction(band, dhigh);
  144. // quantizer adaptation
  145. band->log_factor = av_clip((band->log_factor * 127 >> 7) +
  146. high_log_factor_step[ihigh&1], 0, 22528);
  147. band->scale_factor = linear_scale_factor(band->log_factor - (10 << 11));
  148. }
  149. static void apply_qmf(const int16_t *prev_samples, int *xout1, int *xout2)
  150. {
  151. int i;
  152. *xout1 = 0;
  153. *xout2 = 0;
  154. for (i = 0; i < 12; i++) {
  155. MAC16(*xout2, prev_samples[2*i ], qmf_coeffs[i ]);
  156. MAC16(*xout1, prev_samples[2*i+1], qmf_coeffs[11-i]);
  157. }
  158. }
  159. static av_cold int g722_init(AVCodecContext * avctx)
  160. {
  161. G722Context *c = avctx->priv_data;
  162. if (avctx->channels != 1) {
  163. av_log(avctx, AV_LOG_ERROR, "Only mono tracks are allowed.\n");
  164. return AVERROR_INVALIDDATA;
  165. }
  166. avctx->sample_fmt = SAMPLE_FMT_S16;
  167. switch (avctx->bits_per_coded_sample) {
  168. case 8:
  169. case 7:
  170. case 6:
  171. break;
  172. default:
  173. av_log(avctx, AV_LOG_WARNING, "Unsupported bits_per_coded_sample [%d], "
  174. "assuming 8\n",
  175. avctx->bits_per_coded_sample);
  176. case 0:
  177. avctx->bits_per_coded_sample = 8;
  178. break;
  179. }
  180. c->band[0].scale_factor = 8;
  181. c->band[1].scale_factor = 2;
  182. c->prev_samples_pos = 22;
  183. if (avctx->lowres)
  184. avctx->sample_rate /= 2;
  185. return 0;
  186. }
  187. #if CONFIG_ADPCM_G722_DECODER
  188. static const int16_t low_inv_quant5[32] = {
  189. -35, -35, -2919, -2195, -1765, -1458, -1219, -1023,
  190. -858, -714, -587, -473, -370, -276, -190, -110,
  191. 2919, 2195, 1765, 1458, 1219, 1023, 858, 714,
  192. 587, 473, 370, 276, 190, 110, 35, -35
  193. };
  194. static const int16_t low_inv_quant6[64] = {
  195. -17, -17, -17, -17, -3101, -2738, -2376, -2088,
  196. -1873, -1689, -1535, -1399, -1279, -1170, -1072, -982,
  197. -899, -822, -750, -682, -618, -558, -501, -447,
  198. -396, -347, -300, -254, -211, -170, -130, -91,
  199. 3101, 2738, 2376, 2088, 1873, 1689, 1535, 1399,
  200. 1279, 1170, 1072, 982, 899, 822, 750, 682,
  201. 618, 558, 501, 447, 396, 347, 300, 254,
  202. 211, 170, 130, 91, 54, 17, -54, -17
  203. };
  204. static const int16_t *low_inv_quants[3] = { low_inv_quant6, low_inv_quant5,
  205. low_inv_quant4 };
  206. static int g722_decode_frame(AVCodecContext *avctx, void *data,
  207. int *data_size, AVPacket *avpkt)
  208. {
  209. G722Context *c = avctx->priv_data;
  210. int16_t *out_buf = data;
  211. int j, out_len = 0;
  212. const int skip = 8 - avctx->bits_per_coded_sample;
  213. const int16_t *quantizer_table = low_inv_quants[skip];
  214. GetBitContext gb;
  215. init_get_bits(&gb, avpkt->data, avpkt->size * 8);
  216. for (j = 0; j < avpkt->size; j++) {
  217. int ilow, ihigh, rlow;
  218. ihigh = get_bits(&gb, 2);
  219. ilow = get_bits(&gb, 6 - skip);
  220. skip_bits(&gb, skip);
  221. rlow = av_clip((c->band[0].scale_factor * quantizer_table[ilow] >> 10)
  222. + c->band[0].s_predictor, -16384, 16383);
  223. update_low_predictor(&c->band[0], ilow >> (2 - skip));
  224. if (!avctx->lowres) {
  225. const int dhigh = c->band[1].scale_factor *
  226. high_inv_quant[ihigh] >> 10;
  227. const int rhigh = av_clip(dhigh + c->band[1].s_predictor,
  228. -16384, 16383);
  229. int xout1, xout2;
  230. update_high_predictor(&c->band[1], dhigh, ihigh);
  231. c->prev_samples[c->prev_samples_pos++] = rlow + rhigh;
  232. c->prev_samples[c->prev_samples_pos++] = rlow - rhigh;
  233. apply_qmf(c->prev_samples + c->prev_samples_pos - 24,
  234. &xout1, &xout2);
  235. out_buf[out_len++] = av_clip_int16(xout1 >> 12);
  236. out_buf[out_len++] = av_clip_int16(xout2 >> 12);
  237. if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) {
  238. memmove(c->prev_samples,
  239. c->prev_samples + c->prev_samples_pos - 22,
  240. 22 * sizeof(c->prev_samples[0]));
  241. c->prev_samples_pos = 22;
  242. }
  243. } else
  244. out_buf[out_len++] = rlow;
  245. }
  246. *data_size = out_len << 1;
  247. return avpkt->size;
  248. }
  249. AVCodec adpcm_g722_decoder = {
  250. .name = "g722",
  251. .type = AVMEDIA_TYPE_AUDIO,
  252. .id = CODEC_ID_ADPCM_G722,
  253. .priv_data_size = sizeof(G722Context),
  254. .init = g722_init,
  255. .decode = g722_decode_frame,
  256. .long_name = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
  257. .max_lowres = 1,
  258. };
  259. #endif
  260. #if CONFIG_ADPCM_G722_ENCODER
  261. static const int16_t low_quant[33] = {
  262. 35, 72, 110, 150, 190, 233, 276, 323,
  263. 370, 422, 473, 530, 587, 650, 714, 786,
  264. 858, 940, 1023, 1121, 1219, 1339, 1458, 1612,
  265. 1765, 1980, 2195, 2557, 2919
  266. };
  267. static inline void filter_samples(G722Context *c, const int16_t *samples,
  268. int *xlow, int *xhigh)
  269. {
  270. int xout1, xout2;
  271. c->prev_samples[c->prev_samples_pos++] = samples[0];
  272. c->prev_samples[c->prev_samples_pos++] = samples[1];
  273. apply_qmf(c->prev_samples + c->prev_samples_pos - 24, &xout1, &xout2);
  274. *xlow = xout1 + xout2 >> 13;
  275. *xhigh = xout1 - xout2 >> 13;
  276. if (c->prev_samples_pos >= PREV_SAMPLES_BUF_SIZE) {
  277. memmove(c->prev_samples,
  278. c->prev_samples + c->prev_samples_pos - 22,
  279. 22 * sizeof(c->prev_samples[0]));
  280. c->prev_samples_pos = 22;
  281. }
  282. }
  283. static inline int encode_high(const struct G722Band *state, int xhigh)
  284. {
  285. int diff = av_clip_int16(xhigh - state->s_predictor);
  286. int pred = 141 * state->scale_factor >> 8;
  287. /* = diff >= 0 ? (diff < pred) + 2 : diff >= -pred */
  288. return ((diff ^ (diff >> (sizeof(diff)*8-1))) < pred) + 2*(diff >= 0);
  289. }
  290. static inline int encode_low(const struct G722Band* state, int xlow)
  291. {
  292. int diff = av_clip_int16(xlow - state->s_predictor);
  293. /* = diff >= 0 ? diff : -(diff + 1) */
  294. int limit = diff ^ (diff >> (sizeof(diff)*8-1));
  295. int i = 0;
  296. limit = limit + 1 << 10;
  297. if (limit > low_quant[8] * state->scale_factor)
  298. i = 9;
  299. while (i < 29 && limit > low_quant[i] * state->scale_factor)
  300. i++;
  301. return (diff < 0 ? (i < 2 ? 63 : 33) : 61) - i;
  302. }
  303. static int g722_encode_frame(AVCodecContext *avctx,
  304. uint8_t *dst, int buf_size, void *data)
  305. {
  306. G722Context *c = avctx->priv_data;
  307. const int16_t *samples = data;
  308. int i;
  309. for (i = 0; i < buf_size >> 1; i++) {
  310. int xlow, xhigh, ihigh, ilow;
  311. filter_samples(c, &samples[2*i], &xlow, &xhigh);
  312. ihigh = encode_high(&c->band[1], xhigh);
  313. ilow = encode_low(&c->band[0], xlow);
  314. update_high_predictor(&c->band[1], c->band[1].scale_factor *
  315. high_inv_quant[ihigh] >> 10, ihigh);
  316. update_low_predictor(&c->band[0], ilow >> 2);
  317. *dst++ = ihigh << 6 | ilow;
  318. }
  319. return i;
  320. }
  321. AVCodec adpcm_g722_encoder = {
  322. .name = "g722",
  323. .type = AVMEDIA_TYPE_AUDIO,
  324. .id = CODEC_ID_ADPCM_G722,
  325. .priv_data_size = sizeof(G722Context),
  326. .init = g722_init,
  327. .encode = g722_encode_frame,
  328. .long_name = NULL_IF_CONFIG_SMALL("G.722 ADPCM"),
  329. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  330. };
  331. #endif