g726.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * G.726 ADPCM audio codec
  3. * Copyright (c) 2004 Roman Shaposhnik
  4. *
  5. * This is a very straightforward rendition of the G.726
  6. * Section 4 "Computational Details".
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. #include <limits.h>
  25. #include "avcodec.h"
  26. #include "bitstream.h"
  27. /**
  28. * G.726 11bit float.
  29. * G.726 Standard uses rather odd 11bit floating point arithmentic for
  30. * numerous occasions. It's a mistery to me why they did it this way
  31. * instead of simply using 32bit integer arithmetic.
  32. */
  33. typedef struct Float11 {
  34. uint8_t sign; /**< 1bit sign */
  35. uint8_t exp; /**< 4bit exponent */
  36. uint8_t mant; /**< 6bit mantissa */
  37. } Float11;
  38. static inline Float11* i2f(int i, Float11* f)
  39. {
  40. f->sign = (i < 0);
  41. if (f->sign)
  42. i = -i;
  43. f->exp = av_log2_16bit(i) + !!i;
  44. f->mant = i? (i<<6) >> f->exp : 1<<5;
  45. return f;
  46. }
  47. static inline int16_t mult(Float11* f1, Float11* f2)
  48. {
  49. int res, exp;
  50. exp = f1->exp + f2->exp;
  51. res = (((f1->mant * f2->mant) + 0x30) >> 4);
  52. res = exp > 19 ? res << (exp - 19) : res >> (19 - exp);
  53. return (f1->sign ^ f2->sign) ? -res : res;
  54. }
  55. static inline int sgn(int value)
  56. {
  57. return (value < 0) ? -1 : 1;
  58. }
  59. typedef struct G726Tables {
  60. const int* quant; /**< quantization table */
  61. const int16_t* iquant; /**< inverse quantization table */
  62. const int16_t* W; /**< special table #1 ;-) */
  63. const uint8_t* F; /**< special table #2 */
  64. } G726Tables;
  65. typedef struct G726Context {
  66. G726Tables tbls; /**< static tables needed for computation */
  67. Float11 sr[2]; /**< prev. reconstructed samples */
  68. Float11 dq[6]; /**< prev. difference */
  69. int a[2]; /**< second order predictor coeffs */
  70. int b[6]; /**< sixth order predictor coeffs */
  71. int pk[2]; /**< signs of prev. 2 sez + dq */
  72. int ap; /**< scale factor control */
  73. int yu; /**< fast scale factor */
  74. int yl; /**< slow scale factor */
  75. int dms; /**< short average magnitude of F[i] */
  76. int dml; /**< long average magnitude of F[i] */
  77. int td; /**< tone detect */
  78. int se; /**< estimated signal for the next iteration */
  79. int sez; /**< estimated second order prediction */
  80. int y; /**< quantizer scaling factor for the next iteration */
  81. int code_size;
  82. } G726Context;
  83. static const int quant_tbl16[] = /**< 16kbit/s 2bits per sample */
  84. { 260, INT_MAX };
  85. static const int16_t iquant_tbl16[] =
  86. { 116, 365, 365, 116 };
  87. static const int16_t W_tbl16[] =
  88. { -22, 439, 439, -22 };
  89. static const uint8_t F_tbl16[] =
  90. { 0, 7, 7, 0 };
  91. static const int quant_tbl24[] = /**< 24kbit/s 3bits per sample */
  92. { 7, 217, 330, INT_MAX };
  93. static const int16_t iquant_tbl24[] =
  94. { INT16_MIN, 135, 273, 373, 373, 273, 135, INT16_MIN };
  95. static const int16_t W_tbl24[] =
  96. { -4, 30, 137, 582, 582, 137, 30, -4 };
  97. static const uint8_t F_tbl24[] =
  98. { 0, 1, 2, 7, 7, 2, 1, 0 };
  99. static const int quant_tbl32[] = /**< 32kbit/s 4bits per sample */
  100. { -125, 79, 177, 245, 299, 348, 399, INT_MAX };
  101. static const int16_t iquant_tbl32[] =
  102. { INT16_MIN, 4, 135, 213, 273, 323, 373, 425,
  103. 425, 373, 323, 273, 213, 135, 4, INT16_MIN };
  104. static const int16_t W_tbl32[] =
  105. { -12, 18, 41, 64, 112, 198, 355, 1122,
  106. 1122, 355, 198, 112, 64, 41, 18, -12};
  107. static const uint8_t F_tbl32[] =
  108. { 0, 0, 0, 1, 1, 1, 3, 7, 7, 3, 1, 1, 1, 0, 0, 0 };
  109. static const int quant_tbl40[] = /**< 40kbit/s 5bits per sample */
  110. { -122, -16, 67, 138, 197, 249, 297, 338,
  111. 377, 412, 444, 474, 501, 527, 552, INT_MAX };
  112. static const int16_t iquant_tbl40[] =
  113. { INT16_MIN, -66, 28, 104, 169, 224, 274, 318,
  114. 358, 395, 429, 459, 488, 514, 539, 566,
  115. 566, 539, 514, 488, 459, 429, 395, 358,
  116. 318, 274, 224, 169, 104, 28, -66, INT16_MIN };
  117. static const int16_t W_tbl40[] =
  118. { 14, 14, 24, 39, 40, 41, 58, 100,
  119. 141, 179, 219, 280, 358, 440, 529, 696,
  120. 696, 529, 440, 358, 280, 219, 179, 141,
  121. 100, 58, 41, 40, 39, 24, 14, 14 };
  122. static const uint8_t F_tbl40[] =
  123. { 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 6,
  124. 6, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
  125. static const G726Tables G726Tables_pool[] =
  126. {{ quant_tbl16, iquant_tbl16, W_tbl16, F_tbl16 },
  127. { quant_tbl24, iquant_tbl24, W_tbl24, F_tbl24 },
  128. { quant_tbl32, iquant_tbl32, W_tbl32, F_tbl32 },
  129. { quant_tbl40, iquant_tbl40, W_tbl40, F_tbl40 }};
  130. /**
  131. * Para 4.2.2 page 18: Adaptive quantizer.
  132. */
  133. static inline uint8_t quant(G726Context* c, int d)
  134. {
  135. int sign, exp, i, dln;
  136. sign = i = 0;
  137. if (d < 0) {
  138. sign = 1;
  139. d = -d;
  140. }
  141. exp = av_log2_16bit(d);
  142. dln = ((exp<<7) + (((d<<7)>>exp)&0x7f)) - (c->y>>2);
  143. while (c->tbls.quant[i] < INT_MAX && c->tbls.quant[i] < dln)
  144. ++i;
  145. if (sign)
  146. i = ~i;
  147. if (c->code_size != 2 && i == 0) /* I'm not sure this is a good idea */
  148. i = 0xff;
  149. return i;
  150. }
  151. /**
  152. * Para 4.2.3 page 22: Inverse adaptive quantizer.
  153. */
  154. static inline int16_t inverse_quant(G726Context* c, int i)
  155. {
  156. int dql, dex, dqt;
  157. dql = c->tbls.iquant[i] + (c->y >> 2);
  158. dex = (dql>>7) & 0xf; /* 4bit exponent */
  159. dqt = (1<<7) + (dql & 0x7f); /* log2 -> linear */
  160. return (dql < 0) ? 0 : ((dqt<<dex) >> 7);
  161. }
  162. static int16_t g726_decode(G726Context* c, int I)
  163. {
  164. int dq, re_signal, pk0, fa1, i, tr, ylint, ylfrac, thr2, al, dq0;
  165. Float11 f;
  166. int I_sig= I >> (c->code_size - 1);
  167. dq = inverse_quant(c, I);
  168. /* Transition detect */
  169. ylint = (c->yl >> 15);
  170. ylfrac = (c->yl >> 10) & 0x1f;
  171. thr2 = (ylint > 9) ? 0x1f << 10 : (0x20 + ylfrac) << ylint;
  172. tr= (c->td == 1 && dq > ((3*thr2)>>2));
  173. if (I_sig) /* get the sign */
  174. dq = -dq;
  175. re_signal = c->se + dq;
  176. /* Update second order predictor coefficient A2 and A1 */
  177. pk0 = (c->sez + dq) ? sgn(c->sez + dq) : 0;
  178. dq0 = dq ? sgn(dq) : 0;
  179. if (tr) {
  180. c->a[0] = 0;
  181. c->a[1] = 0;
  182. for (i=0; i<6; i++)
  183. c->b[i] = 0;
  184. } else {
  185. /* This is a bit crazy, but it really is +255 not +256 */
  186. fa1 = av_clip((-c->a[0]*c->pk[0]*pk0)>>5, -256, 255);
  187. c->a[1] += 128*pk0*c->pk[1] + fa1 - (c->a[1]>>7);
  188. c->a[1] = av_clip(c->a[1], -12288, 12288);
  189. c->a[0] += 64*3*pk0*c->pk[0] - (c->a[0] >> 8);
  190. c->a[0] = av_clip(c->a[0], -(15360 - c->a[1]), 15360 - c->a[1]);
  191. for (i=0; i<6; i++)
  192. c->b[i] += 128*dq0*sgn(-c->dq[i].sign) - (c->b[i]>>8);
  193. }
  194. /* Update Dq and Sr and Pk */
  195. c->pk[1] = c->pk[0];
  196. c->pk[0] = pk0 ? pk0 : 1;
  197. c->sr[1] = c->sr[0];
  198. i2f(re_signal, &c->sr[0]);
  199. for (i=5; i>0; i--)
  200. c->dq[i] = c->dq[i-1];
  201. i2f(dq, &c->dq[0]);
  202. c->dq[0].sign = I_sig; /* Isn't it crazy ?!?! */
  203. c->td = c->a[1] < -11776;
  204. /* Update Ap */
  205. c->dms += (c->tbls.F[I]<<4) + ((- c->dms) >> 5);
  206. c->dml += (c->tbls.F[I]<<4) + ((- c->dml) >> 7);
  207. if (tr)
  208. c->ap = 256;
  209. else {
  210. c->ap += (-c->ap) >> 4;
  211. if (c->y <= 1535 || c->td || abs((c->dms << 2) - c->dml) >= (c->dml >> 3))
  212. c->ap += 0x20;
  213. }
  214. /* Update Yu and Yl */
  215. c->yu = av_clip(c->y + c->tbls.W[I] + ((-c->y)>>5), 544, 5120);
  216. c->yl += c->yu + ((-c->yl)>>6);
  217. /* Next iteration for Y */
  218. al = (c->ap >= 256) ? 1<<6 : c->ap >> 2;
  219. c->y = (c->yl + (c->yu - (c->yl>>6))*al) >> 6;
  220. /* Next iteration for SE and SEZ */
  221. c->se = 0;
  222. for (i=0; i<6; i++)
  223. c->se += mult(i2f(c->b[i] >> 2, &f), &c->dq[i]);
  224. c->sez = c->se >> 1;
  225. for (i=0; i<2; i++)
  226. c->se += mult(i2f(c->a[i] >> 2, &f), &c->sr[i]);
  227. c->se >>= 1;
  228. return av_clip(re_signal << 2, -0xffff, 0xffff);
  229. }
  230. static av_cold int g726_reset(G726Context* c, int index)
  231. {
  232. int i;
  233. c->tbls = G726Tables_pool[index];
  234. for (i=0; i<2; i++) {
  235. c->sr[i].mant = 1<<5;
  236. c->pk[i] = 1;
  237. }
  238. for (i=0; i<6; i++) {
  239. c->dq[i].mant = 1<<5;
  240. }
  241. c->yu = 544;
  242. c->yl = 34816;
  243. c->y = 544;
  244. return 0;
  245. }
  246. #if CONFIG_ADPCM_G726_ENCODER
  247. static int16_t g726_encode(G726Context* c, int16_t sig)
  248. {
  249. uint8_t i;
  250. i = quant(c, sig/4 - c->se) & ((1<<c->code_size) - 1);
  251. g726_decode(c, i);
  252. return i;
  253. }
  254. #endif
  255. /* Interfacing to the libavcodec */
  256. static av_cold int g726_init(AVCodecContext * avctx)
  257. {
  258. G726Context* c = avctx->priv_data;
  259. unsigned int index;
  260. if (avctx->sample_rate <= 0) {
  261. av_log(avctx, AV_LOG_ERROR, "Samplerate is invalid\n");
  262. return -1;
  263. }
  264. index = (avctx->bit_rate + avctx->sample_rate/2) / avctx->sample_rate - 2;
  265. if (avctx->bit_rate % avctx->sample_rate && avctx->codec->encode) {
  266. av_log(avctx, AV_LOG_ERROR, "Bitrate - Samplerate combination is invalid\n");
  267. return -1;
  268. }
  269. if(avctx->channels != 1){
  270. av_log(avctx, AV_LOG_ERROR, "Only mono is supported\n");
  271. return -1;
  272. }
  273. if(index>3){
  274. av_log(avctx, AV_LOG_ERROR, "Unsupported number of bits %d\n", index+2);
  275. return -1;
  276. }
  277. g726_reset(c, index);
  278. c->code_size = index+2;
  279. avctx->coded_frame = avcodec_alloc_frame();
  280. if (!avctx->coded_frame)
  281. return AVERROR(ENOMEM);
  282. avctx->coded_frame->key_frame = 1;
  283. if (avctx->codec->decode)
  284. avctx->sample_fmt = SAMPLE_FMT_S16;
  285. return 0;
  286. }
  287. static av_cold int g726_close(AVCodecContext *avctx)
  288. {
  289. av_freep(&avctx->coded_frame);
  290. return 0;
  291. }
  292. #if CONFIG_ADPCM_G726_ENCODER
  293. static int g726_encode_frame(AVCodecContext *avctx,
  294. uint8_t *dst, int buf_size, void *data)
  295. {
  296. G726Context *c = avctx->priv_data;
  297. short *samples = data;
  298. PutBitContext pb;
  299. init_put_bits(&pb, dst, 1024*1024);
  300. for (; buf_size; buf_size--)
  301. put_bits(&pb, c->code_size, g726_encode(c, *samples++));
  302. flush_put_bits(&pb);
  303. return put_bits_count(&pb)>>3;
  304. }
  305. #endif
  306. static int g726_decode_frame(AVCodecContext *avctx,
  307. void *data, int *data_size,
  308. const uint8_t *buf, int buf_size)
  309. {
  310. G726Context *c = avctx->priv_data;
  311. short *samples = data;
  312. GetBitContext gb;
  313. init_get_bits(&gb, buf, buf_size * 8);
  314. while (get_bits_count(&gb) + c->code_size <= buf_size*8)
  315. *samples++ = g726_decode(c, get_bits(&gb, c->code_size));
  316. if(buf_size*8 != get_bits_count(&gb))
  317. av_log(avctx, AV_LOG_ERROR, "Frame invalidly split, missing parser?\n");
  318. *data_size = (uint8_t*)samples - (uint8_t*)data;
  319. return buf_size;
  320. }
  321. #if CONFIG_ADPCM_G726_ENCODER
  322. AVCodec adpcm_g726_encoder = {
  323. "g726",
  324. CODEC_TYPE_AUDIO,
  325. CODEC_ID_ADPCM_G726,
  326. sizeof(G726Context),
  327. g726_init,
  328. g726_encode_frame,
  329. g726_close,
  330. NULL,
  331. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
  332. .long_name = NULL_IF_CONFIG_SMALL("G.726 ADPCM"),
  333. };
  334. #endif
  335. AVCodec adpcm_g726_decoder = {
  336. "g726",
  337. CODEC_TYPE_AUDIO,
  338. CODEC_ID_ADPCM_G726,
  339. sizeof(G726Context),
  340. g726_init,
  341. NULL,
  342. g726_close,
  343. g726_decode_frame,
  344. .long_name = NULL_IF_CONFIG_SMALL("G.726 ADPCM"),
  345. };