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.

1583 lines
56KB

  1. /*
  2. * ADPCM codecs
  3. * Copyright (c) 2001-2003 The ffmpeg Project
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avcodec.h"
  22. #include "bitstream.h"
  23. #include "bytestream.h"
  24. /**
  25. * @file adpcm.c
  26. * ADPCM codecs.
  27. * First version by Francois Revol (revol@free.fr)
  28. * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
  29. * by Mike Melanson (melanson@pcisys.net)
  30. * CD-ROM XA ADPCM codec by BERO
  31. * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
  32. * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
  33. * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
  34. * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
  35. * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
  36. *
  37. * Features and limitations:
  38. *
  39. * Reference documents:
  40. * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
  41. * http://www.geocities.com/SiliconValley/8682/aud3.txt
  42. * http://openquicktime.sourceforge.net/plugins.htm
  43. * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
  44. * http://www.cs.ucla.edu/~leec/mediabench/applications.html
  45. * SoX source code http://home.sprynet.com/~cbagwell/sox.html
  46. *
  47. * CD-ROM XA:
  48. * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html
  49. * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html
  50. * readstr http://www.geocities.co.jp/Playtown/2004/
  51. */
  52. #define BLKSIZE 1024
  53. /* step_table[] and index_table[] are from the ADPCM reference source */
  54. /* This is the index table: */
  55. static const int index_table[16] = {
  56. -1, -1, -1, -1, 2, 4, 6, 8,
  57. -1, -1, -1, -1, 2, 4, 6, 8,
  58. };
  59. /**
  60. * This is the step table. Note that many programs use slight deviations from
  61. * this table, but such deviations are negligible:
  62. */
  63. static const int step_table[89] = {
  64. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  65. 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  66. 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  67. 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  68. 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  69. 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  70. 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  71. 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  72. 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  73. };
  74. /* These are for MS-ADPCM */
  75. /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
  76. static const int AdaptationTable[] = {
  77. 230, 230, 230, 230, 307, 409, 512, 614,
  78. 768, 614, 512, 409, 307, 230, 230, 230
  79. };
  80. static const int AdaptCoeff1[] = {
  81. 256, 512, 0, 192, 240, 460, 392
  82. };
  83. static const int AdaptCoeff2[] = {
  84. 0, -256, 0, 64, 0, -208, -232
  85. };
  86. /* These are for CD-ROM XA ADPCM */
  87. static const int xa_adpcm_table[5][2] = {
  88. { 0, 0 },
  89. { 60, 0 },
  90. { 115, -52 },
  91. { 98, -55 },
  92. { 122, -60 }
  93. };
  94. static const int ea_adpcm_table[] = {
  95. 0, 240, 460, 392, 0, 0, -208, -220, 0, 1,
  96. 3, 4, 7, 8, 10, 11, 0, -1, -3, -4
  97. };
  98. static const int ct_adpcm_table[8] = {
  99. 0x00E6, 0x00E6, 0x00E6, 0x00E6,
  100. 0x0133, 0x0199, 0x0200, 0x0266
  101. };
  102. // padded to zero where table size is less then 16
  103. static const int swf_index_tables[4][16] = {
  104. /*2*/ { -1, 2 },
  105. /*3*/ { -1, -1, 2, 4 },
  106. /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
  107. /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
  108. };
  109. static const int yamaha_indexscale[] = {
  110. 230, 230, 230, 230, 307, 409, 512, 614,
  111. 230, 230, 230, 230, 307, 409, 512, 614
  112. };
  113. static const int yamaha_difflookup[] = {
  114. 1, 3, 5, 7, 9, 11, 13, 15,
  115. -1, -3, -5, -7, -9, -11, -13, -15
  116. };
  117. /* end of tables */
  118. typedef struct ADPCMChannelStatus {
  119. int predictor;
  120. short int step_index;
  121. int step;
  122. /* for encoding */
  123. int prev_sample;
  124. /* MS version */
  125. short sample1;
  126. short sample2;
  127. int coeff1;
  128. int coeff2;
  129. int idelta;
  130. } ADPCMChannelStatus;
  131. typedef struct ADPCMContext {
  132. int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
  133. ADPCMChannelStatus status[6];
  134. } ADPCMContext;
  135. /* XXX: implement encoding */
  136. #ifdef CONFIG_ENCODERS
  137. static int adpcm_encode_init(AVCodecContext *avctx)
  138. {
  139. if (avctx->channels > 2)
  140. return -1; /* only stereo or mono =) */
  141. switch(avctx->codec->id) {
  142. case CODEC_ID_ADPCM_IMA_QT:
  143. av_log(avctx, AV_LOG_ERROR, "ADPCM: codec adpcm_ima_qt unsupported for encoding !\n");
  144. avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */
  145. return -1;
  146. break;
  147. case CODEC_ID_ADPCM_IMA_WAV:
  148. avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
  149. /* and we have 4 bytes per channel overhead */
  150. avctx->block_align = BLKSIZE;
  151. /* seems frame_size isn't taken into account... have to buffer the samples :-( */
  152. break;
  153. case CODEC_ID_ADPCM_MS:
  154. avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
  155. /* and we have 7 bytes per channel overhead */
  156. avctx->block_align = BLKSIZE;
  157. break;
  158. case CODEC_ID_ADPCM_YAMAHA:
  159. avctx->frame_size = BLKSIZE * avctx->channels;
  160. avctx->block_align = BLKSIZE;
  161. break;
  162. case CODEC_ID_ADPCM_SWF:
  163. if (avctx->sample_rate != 11025 &&
  164. avctx->sample_rate != 22050 &&
  165. avctx->sample_rate != 44100) {
  166. av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, 22050 or 44100\n");
  167. return -1;
  168. }
  169. avctx->frame_size = 512 * (avctx->sample_rate / 11025);
  170. break;
  171. default:
  172. return -1;
  173. break;
  174. }
  175. avctx->coded_frame= avcodec_alloc_frame();
  176. avctx->coded_frame->key_frame= 1;
  177. return 0;
  178. }
  179. static int adpcm_encode_close(AVCodecContext *avctx)
  180. {
  181. av_freep(&avctx->coded_frame);
  182. return 0;
  183. }
  184. static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
  185. {
  186. int delta = sample - c->prev_sample;
  187. int nibble = FFMIN(7, abs(delta)*4/step_table[c->step_index]) + (delta<0)*8;
  188. c->prev_sample += ((step_table[c->step_index] * yamaha_difflookup[nibble]) / 8);
  189. c->prev_sample = av_clip_int16(c->prev_sample);
  190. c->step_index = av_clip(c->step_index + index_table[nibble], 0, 88);
  191. return nibble;
  192. }
  193. static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
  194. {
  195. int predictor, nibble, bias;
  196. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
  197. nibble= sample - predictor;
  198. if(nibble>=0) bias= c->idelta/2;
  199. else bias=-c->idelta/2;
  200. nibble= (nibble + bias) / c->idelta;
  201. nibble= av_clip(nibble, -8, 7)&0x0F;
  202. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  203. c->sample2 = c->sample1;
  204. c->sample1 = av_clip_int16(predictor);
  205. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
  206. if (c->idelta < 16) c->idelta = 16;
  207. return nibble;
  208. }
  209. static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
  210. {
  211. int nibble, delta;
  212. if(!c->step) {
  213. c->predictor = 0;
  214. c->step = 127;
  215. }
  216. delta = sample - c->predictor;
  217. nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8;
  218. c->predictor += ((c->step * yamaha_difflookup[nibble]) / 8);
  219. c->predictor = av_clip_int16(c->predictor);
  220. c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
  221. c->step = av_clip(c->step, 127, 24567);
  222. return nibble;
  223. }
  224. typedef struct TrellisPath {
  225. int nibble;
  226. int prev;
  227. } TrellisPath;
  228. typedef struct TrellisNode {
  229. uint32_t ssd;
  230. int path;
  231. int sample1;
  232. int sample2;
  233. int step;
  234. } TrellisNode;
  235. static void adpcm_compress_trellis(AVCodecContext *avctx, const short *samples,
  236. uint8_t *dst, ADPCMChannelStatus *c, int n)
  237. {
  238. #define FREEZE_INTERVAL 128
  239. //FIXME 6% faster if frontier is a compile-time constant
  240. const int frontier = 1 << avctx->trellis;
  241. const int stride = avctx->channels;
  242. const int version = avctx->codec->id;
  243. const int max_paths = frontier*FREEZE_INTERVAL;
  244. TrellisPath paths[max_paths], *p;
  245. TrellisNode node_buf[2][frontier];
  246. TrellisNode *nodep_buf[2][frontier];
  247. TrellisNode **nodes = nodep_buf[0]; // nodes[] is always sorted by .ssd
  248. TrellisNode **nodes_next = nodep_buf[1];
  249. int pathn = 0, froze = -1, i, j, k;
  250. assert(!(max_paths&(max_paths-1)));
  251. memset(nodep_buf, 0, sizeof(nodep_buf));
  252. nodes[0] = &node_buf[1][0];
  253. nodes[0]->ssd = 0;
  254. nodes[0]->path = 0;
  255. nodes[0]->step = c->step_index;
  256. nodes[0]->sample1 = c->sample1;
  257. nodes[0]->sample2 = c->sample2;
  258. if((version == CODEC_ID_ADPCM_IMA_WAV) || (version == CODEC_ID_ADPCM_SWF))
  259. nodes[0]->sample1 = c->prev_sample;
  260. if(version == CODEC_ID_ADPCM_MS)
  261. nodes[0]->step = c->idelta;
  262. if(version == CODEC_ID_ADPCM_YAMAHA) {
  263. if(c->step == 0) {
  264. nodes[0]->step = 127;
  265. nodes[0]->sample1 = 0;
  266. } else {
  267. nodes[0]->step = c->step;
  268. nodes[0]->sample1 = c->predictor;
  269. }
  270. }
  271. for(i=0; i<n; i++) {
  272. TrellisNode *t = node_buf[i&1];
  273. TrellisNode **u;
  274. int sample = samples[i*stride];
  275. memset(nodes_next, 0, frontier*sizeof(TrellisNode*));
  276. for(j=0; j<frontier && nodes[j]; j++) {
  277. // higher j have higher ssd already, so they're unlikely to use a suboptimal next sample too
  278. const int range = (j < frontier/2) ? 1 : 0;
  279. const int step = nodes[j]->step;
  280. int nidx;
  281. if(version == CODEC_ID_ADPCM_MS) {
  282. const int predictor = ((nodes[j]->sample1 * c->coeff1) + (nodes[j]->sample2 * c->coeff2)) / 256;
  283. const int div = (sample - predictor) / step;
  284. const int nmin = av_clip(div-range, -8, 6);
  285. const int nmax = av_clip(div+range, -7, 7);
  286. for(nidx=nmin; nidx<=nmax; nidx++) {
  287. const int nibble = nidx & 0xf;
  288. int dec_sample = predictor + nidx * step;
  289. #define STORE_NODE(NAME, STEP_INDEX)\
  290. int d;\
  291. uint32_t ssd;\
  292. dec_sample = av_clip_int16(dec_sample);\
  293. d = sample - dec_sample;\
  294. ssd = nodes[j]->ssd + d*d;\
  295. if(nodes_next[frontier-1] && ssd >= nodes_next[frontier-1]->ssd)\
  296. continue;\
  297. /* Collapse any two states with the same previous sample value. \
  298. * One could also distinguish states by step and by 2nd to last
  299. * sample, but the effects of that are negligible. */\
  300. for(k=0; k<frontier && nodes_next[k]; k++) {\
  301. if(dec_sample == nodes_next[k]->sample1) {\
  302. assert(ssd >= nodes_next[k]->ssd);\
  303. goto next_##NAME;\
  304. }\
  305. }\
  306. for(k=0; k<frontier; k++) {\
  307. if(!nodes_next[k] || ssd < nodes_next[k]->ssd) {\
  308. TrellisNode *u = nodes_next[frontier-1];\
  309. if(!u) {\
  310. assert(pathn < max_paths);\
  311. u = t++;\
  312. u->path = pathn++;\
  313. }\
  314. u->ssd = ssd;\
  315. u->step = STEP_INDEX;\
  316. u->sample2 = nodes[j]->sample1;\
  317. u->sample1 = dec_sample;\
  318. paths[u->path].nibble = nibble;\
  319. paths[u->path].prev = nodes[j]->path;\
  320. memmove(&nodes_next[k+1], &nodes_next[k], (frontier-k-1)*sizeof(TrellisNode*));\
  321. nodes_next[k] = u;\
  322. break;\
  323. }\
  324. }\
  325. next_##NAME:;
  326. STORE_NODE(ms, FFMAX(16, (AdaptationTable[nibble] * step) >> 8));
  327. }
  328. } else if((version == CODEC_ID_ADPCM_IMA_WAV)|| (version == CODEC_ID_ADPCM_SWF)) {
  329. #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
  330. const int predictor = nodes[j]->sample1;\
  331. const int div = (sample - predictor) * 4 / STEP_TABLE;\
  332. int nmin = av_clip(div-range, -7, 6);\
  333. int nmax = av_clip(div+range, -6, 7);\
  334. if(nmin<=0) nmin--; /* distinguish -0 from +0 */\
  335. if(nmax<0) nmax--;\
  336. for(nidx=nmin; nidx<=nmax; nidx++) {\
  337. const int nibble = nidx<0 ? 7-nidx : nidx;\
  338. int dec_sample = predictor + (STEP_TABLE * yamaha_difflookup[nibble]) / 8;\
  339. STORE_NODE(NAME, STEP_INDEX);\
  340. }
  341. LOOP_NODES(ima, step_table[step], av_clip(step + index_table[nibble], 0, 88));
  342. } else { //CODEC_ID_ADPCM_YAMAHA
  343. LOOP_NODES(yamaha, step, av_clip((step * yamaha_indexscale[nibble]) >> 8, 127, 24567));
  344. #undef LOOP_NODES
  345. #undef STORE_NODE
  346. }
  347. }
  348. u = nodes;
  349. nodes = nodes_next;
  350. nodes_next = u;
  351. // prevent overflow
  352. if(nodes[0]->ssd > (1<<28)) {
  353. for(j=1; j<frontier && nodes[j]; j++)
  354. nodes[j]->ssd -= nodes[0]->ssd;
  355. nodes[0]->ssd = 0;
  356. }
  357. // merge old paths to save memory
  358. if(i == froze + FREEZE_INTERVAL) {
  359. p = &paths[nodes[0]->path];
  360. for(k=i; k>froze; k--) {
  361. dst[k] = p->nibble;
  362. p = &paths[p->prev];
  363. }
  364. froze = i;
  365. pathn = 0;
  366. // other nodes might use paths that don't coincide with the frozen one.
  367. // checking which nodes do so is too slow, so just kill them all.
  368. // this also slightly improves quality, but I don't know why.
  369. memset(nodes+1, 0, (frontier-1)*sizeof(TrellisNode*));
  370. }
  371. }
  372. p = &paths[nodes[0]->path];
  373. for(i=n-1; i>froze; i--) {
  374. dst[i] = p->nibble;
  375. p = &paths[p->prev];
  376. }
  377. c->predictor = nodes[0]->sample1;
  378. c->sample1 = nodes[0]->sample1;
  379. c->sample2 = nodes[0]->sample2;
  380. c->step_index = nodes[0]->step;
  381. c->step = nodes[0]->step;
  382. c->idelta = nodes[0]->step;
  383. }
  384. static int adpcm_encode_frame(AVCodecContext *avctx,
  385. unsigned char *frame, int buf_size, void *data)
  386. {
  387. int n, i, st;
  388. short *samples;
  389. unsigned char *dst;
  390. ADPCMContext *c = avctx->priv_data;
  391. dst = frame;
  392. samples = (short *)data;
  393. st= avctx->channels == 2;
  394. /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
  395. switch(avctx->codec->id) {
  396. case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
  397. break;
  398. case CODEC_ID_ADPCM_IMA_WAV:
  399. n = avctx->frame_size / 8;
  400. c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
  401. /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
  402. bytestream_put_le16(&dst, c->status[0].prev_sample);
  403. *dst++ = (unsigned char)c->status[0].step_index;
  404. *dst++ = 0; /* unknown */
  405. samples++;
  406. if (avctx->channels == 2) {
  407. c->status[1].prev_sample = (signed short)samples[1];
  408. /* c->status[1].step_index = 0; */
  409. bytestream_put_le16(&dst, c->status[1].prev_sample);
  410. *dst++ = (unsigned char)c->status[1].step_index;
  411. *dst++ = 0;
  412. samples++;
  413. }
  414. /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
  415. if(avctx->trellis > 0) {
  416. uint8_t buf[2][n*8];
  417. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n*8);
  418. if(avctx->channels == 2)
  419. adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n*8);
  420. for(i=0; i<n; i++) {
  421. *dst++ = buf[0][8*i+0] | (buf[0][8*i+1] << 4);
  422. *dst++ = buf[0][8*i+2] | (buf[0][8*i+3] << 4);
  423. *dst++ = buf[0][8*i+4] | (buf[0][8*i+5] << 4);
  424. *dst++ = buf[0][8*i+6] | (buf[0][8*i+7] << 4);
  425. if (avctx->channels == 2) {
  426. *dst++ = buf[1][8*i+0] | (buf[1][8*i+1] << 4);
  427. *dst++ = buf[1][8*i+2] | (buf[1][8*i+3] << 4);
  428. *dst++ = buf[1][8*i+4] | (buf[1][8*i+5] << 4);
  429. *dst++ = buf[1][8*i+6] | (buf[1][8*i+7] << 4);
  430. }
  431. }
  432. } else
  433. for (; n>0; n--) {
  434. *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]);
  435. *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4;
  436. dst++;
  437. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]);
  438. *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4;
  439. dst++;
  440. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]);
  441. *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4;
  442. dst++;
  443. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]);
  444. *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4;
  445. dst++;
  446. /* right channel */
  447. if (avctx->channels == 2) {
  448. *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
  449. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
  450. dst++;
  451. *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
  452. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
  453. dst++;
  454. *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
  455. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
  456. dst++;
  457. *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
  458. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
  459. dst++;
  460. }
  461. samples += 8 * avctx->channels;
  462. }
  463. break;
  464. case CODEC_ID_ADPCM_SWF:
  465. {
  466. int i;
  467. PutBitContext pb;
  468. init_put_bits(&pb, dst, buf_size*8);
  469. n = avctx->frame_size-1;
  470. //Store AdpcmCodeSize
  471. put_bits(&pb, 2, 2); //Set 4bits flash adpcm format
  472. //Init the encoder state
  473. for(i=0; i<avctx->channels; i++){
  474. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 63); // clip step so it fits 6 bits
  475. put_bits(&pb, 16, samples[i] & 0xFFFF);
  476. put_bits(&pb, 6, c->status[i].step_index);
  477. c->status[i].prev_sample = (signed short)samples[i];
  478. }
  479. if(avctx->trellis > 0) {
  480. uint8_t buf[2][n];
  481. adpcm_compress_trellis(avctx, samples+2, buf[0], &c->status[0], n);
  482. if (avctx->channels == 2)
  483. adpcm_compress_trellis(avctx, samples+3, buf[1], &c->status[1], n);
  484. for(i=0; i<n; i++) {
  485. put_bits(&pb, 4, buf[0][i]);
  486. if (avctx->channels == 2)
  487. put_bits(&pb, 4, buf[1][i]);
  488. }
  489. } else {
  490. for (i=1; i<avctx->frame_size; i++) {
  491. put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels*i]));
  492. if (avctx->channels == 2)
  493. put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1], samples[2*i+1]));
  494. }
  495. }
  496. flush_put_bits(&pb);
  497. dst += put_bits_count(&pb)>>3;
  498. break;
  499. }
  500. case CODEC_ID_ADPCM_MS:
  501. for(i=0; i<avctx->channels; i++){
  502. int predictor=0;
  503. *dst++ = predictor;
  504. c->status[i].coeff1 = AdaptCoeff1[predictor];
  505. c->status[i].coeff2 = AdaptCoeff2[predictor];
  506. }
  507. for(i=0; i<avctx->channels; i++){
  508. if (c->status[i].idelta < 16)
  509. c->status[i].idelta = 16;
  510. bytestream_put_le16(&dst, c->status[i].idelta);
  511. }
  512. for(i=0; i<avctx->channels; i++){
  513. c->status[i].sample1= *samples++;
  514. bytestream_put_le16(&dst, c->status[i].sample1);
  515. }
  516. for(i=0; i<avctx->channels; i++){
  517. c->status[i].sample2= *samples++;
  518. bytestream_put_le16(&dst, c->status[i].sample2);
  519. }
  520. if(avctx->trellis > 0) {
  521. int n = avctx->block_align - 7*avctx->channels;
  522. uint8_t buf[2][n];
  523. if(avctx->channels == 1) {
  524. n *= 2;
  525. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
  526. for(i=0; i<n; i+=2)
  527. *dst++ = (buf[0][i] << 4) | buf[0][i+1];
  528. } else {
  529. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
  530. adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n);
  531. for(i=0; i<n; i++)
  532. *dst++ = (buf[0][i] << 4) | buf[1][i];
  533. }
  534. } else
  535. for(i=7*avctx->channels; i<avctx->block_align; i++) {
  536. int nibble;
  537. nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
  538. nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
  539. *dst++ = nibble;
  540. }
  541. break;
  542. case CODEC_ID_ADPCM_YAMAHA:
  543. n = avctx->frame_size / 2;
  544. if(avctx->trellis > 0) {
  545. uint8_t buf[2][n*2];
  546. n *= 2;
  547. if(avctx->channels == 1) {
  548. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
  549. for(i=0; i<n; i+=2)
  550. *dst++ = buf[0][i] | (buf[0][i+1] << 4);
  551. } else {
  552. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
  553. adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n);
  554. for(i=0; i<n; i++)
  555. *dst++ = buf[0][i] | (buf[1][i] << 4);
  556. }
  557. } else
  558. for (; n>0; n--) {
  559. for(i = 0; i < avctx->channels; i++) {
  560. int nibble;
  561. nibble = adpcm_yamaha_compress_sample(&c->status[i], samples[i]);
  562. nibble |= adpcm_yamaha_compress_sample(&c->status[i], samples[i+avctx->channels]) << 4;
  563. *dst++ = nibble;
  564. }
  565. samples += 2 * avctx->channels;
  566. }
  567. break;
  568. default:
  569. return -1;
  570. }
  571. return dst - frame;
  572. }
  573. #endif //CONFIG_ENCODERS
  574. static int adpcm_decode_init(AVCodecContext * avctx)
  575. {
  576. ADPCMContext *c = avctx->priv_data;
  577. unsigned int max_channels = 2;
  578. switch(avctx->codec->id) {
  579. case CODEC_ID_ADPCM_EA_R1:
  580. case CODEC_ID_ADPCM_EA_R2:
  581. case CODEC_ID_ADPCM_EA_R3:
  582. max_channels = 6;
  583. break;
  584. }
  585. if(avctx->channels > max_channels){
  586. return -1;
  587. }
  588. switch(avctx->codec->id) {
  589. case CODEC_ID_ADPCM_CT:
  590. c->status[0].step = c->status[1].step = 511;
  591. break;
  592. case CODEC_ID_ADPCM_IMA_WS:
  593. if (avctx->extradata && avctx->extradata_size == 2 * 4) {
  594. c->status[0].predictor = AV_RL32(avctx->extradata);
  595. c->status[1].predictor = AV_RL32(avctx->extradata + 4);
  596. }
  597. break;
  598. default:
  599. break;
  600. }
  601. return 0;
  602. }
  603. static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
  604. {
  605. int step_index;
  606. int predictor;
  607. int sign, delta, diff, step;
  608. step = step_table[c->step_index];
  609. step_index = c->step_index + index_table[(unsigned)nibble];
  610. if (step_index < 0) step_index = 0;
  611. else if (step_index > 88) step_index = 88;
  612. sign = nibble & 8;
  613. delta = nibble & 7;
  614. /* perform direct multiplication instead of series of jumps proposed by
  615. * the reference ADPCM implementation since modern CPUs can do the mults
  616. * quickly enough */
  617. diff = ((2 * delta + 1) * step) >> shift;
  618. predictor = c->predictor;
  619. if (sign) predictor -= diff;
  620. else predictor += diff;
  621. c->predictor = av_clip_int16(predictor);
  622. c->step_index = step_index;
  623. return (short)c->predictor;
  624. }
  625. static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
  626. {
  627. int predictor;
  628. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
  629. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  630. c->sample2 = c->sample1;
  631. c->sample1 = av_clip_int16(predictor);
  632. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
  633. if (c->idelta < 16) c->idelta = 16;
  634. return c->sample1;
  635. }
  636. static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
  637. {
  638. int sign, delta, diff;
  639. int new_step;
  640. sign = nibble & 8;
  641. delta = nibble & 7;
  642. /* perform direct multiplication instead of series of jumps proposed by
  643. * the reference ADPCM implementation since modern CPUs can do the mults
  644. * quickly enough */
  645. diff = ((2 * delta + 1) * c->step) >> 3;
  646. /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
  647. c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
  648. c->predictor = av_clip_int16(c->predictor);
  649. /* calculate new step and clamp it to range 511..32767 */
  650. new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
  651. c->step = av_clip(new_step, 511, 32767);
  652. return (short)c->predictor;
  653. }
  654. static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
  655. {
  656. int sign, delta, diff;
  657. sign = nibble & (1<<(size-1));
  658. delta = nibble & ((1<<(size-1))-1);
  659. diff = delta << (7 + c->step + shift);
  660. /* clamp result */
  661. c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
  662. /* calculate new step */
  663. if (delta >= (2*size - 3) && c->step < 3)
  664. c->step++;
  665. else if (delta == 0 && c->step > 0)
  666. c->step--;
  667. return (short) c->predictor;
  668. }
  669. static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
  670. {
  671. if(!c->step) {
  672. c->predictor = 0;
  673. c->step = 127;
  674. }
  675. c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
  676. c->predictor = av_clip_int16(c->predictor);
  677. c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
  678. c->step = av_clip(c->step, 127, 24567);
  679. return c->predictor;
  680. }
  681. static void xa_decode(short *out, const unsigned char *in,
  682. ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
  683. {
  684. int i, j;
  685. int shift,filter,f0,f1;
  686. int s_1,s_2;
  687. int d,s,t;
  688. for(i=0;i<4;i++) {
  689. shift = 12 - (in[4+i*2] & 15);
  690. filter = in[4+i*2] >> 4;
  691. f0 = xa_adpcm_table[filter][0];
  692. f1 = xa_adpcm_table[filter][1];
  693. s_1 = left->sample1;
  694. s_2 = left->sample2;
  695. for(j=0;j<28;j++) {
  696. d = in[16+i+j*4];
  697. t = (signed char)(d<<4)>>4;
  698. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  699. s_2 = s_1;
  700. s_1 = av_clip_int16(s);
  701. *out = s_1;
  702. out += inc;
  703. }
  704. if (inc==2) { /* stereo */
  705. left->sample1 = s_1;
  706. left->sample2 = s_2;
  707. s_1 = right->sample1;
  708. s_2 = right->sample2;
  709. out = out + 1 - 28*2;
  710. }
  711. shift = 12 - (in[5+i*2] & 15);
  712. filter = in[5+i*2] >> 4;
  713. f0 = xa_adpcm_table[filter][0];
  714. f1 = xa_adpcm_table[filter][1];
  715. for(j=0;j<28;j++) {
  716. d = in[16+i+j*4];
  717. t = (signed char)d >> 4;
  718. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  719. s_2 = s_1;
  720. s_1 = av_clip_int16(s);
  721. *out = s_1;
  722. out += inc;
  723. }
  724. if (inc==2) { /* stereo */
  725. right->sample1 = s_1;
  726. right->sample2 = s_2;
  727. out -= 1;
  728. } else {
  729. left->sample1 = s_1;
  730. left->sample2 = s_2;
  731. }
  732. }
  733. }
  734. /* DK3 ADPCM support macro */
  735. #define DK3_GET_NEXT_NIBBLE() \
  736. if (decode_top_nibble_next) \
  737. { \
  738. nibble = (last_byte >> 4) & 0x0F; \
  739. decode_top_nibble_next = 0; \
  740. } \
  741. else \
  742. { \
  743. last_byte = *src++; \
  744. if (src >= buf + buf_size) break; \
  745. nibble = last_byte & 0x0F; \
  746. decode_top_nibble_next = 1; \
  747. }
  748. static int adpcm_decode_frame(AVCodecContext *avctx,
  749. void *data, int *data_size,
  750. uint8_t *buf, int buf_size)
  751. {
  752. ADPCMContext *c = avctx->priv_data;
  753. ADPCMChannelStatus *cs;
  754. int n, m, channel, i;
  755. int block_predictor[2];
  756. short *samples;
  757. short *samples_end;
  758. uint8_t *src;
  759. int st; /* stereo */
  760. /* DK3 ADPCM accounting variables */
  761. unsigned char last_byte = 0;
  762. unsigned char nibble;
  763. int decode_top_nibble_next = 0;
  764. int diff_channel;
  765. /* EA ADPCM state variables */
  766. uint32_t samples_in_chunk;
  767. int32_t previous_left_sample, previous_right_sample;
  768. int32_t current_left_sample, current_right_sample;
  769. int32_t next_left_sample, next_right_sample;
  770. int32_t coeff1l, coeff2l, coeff1r, coeff2r;
  771. uint8_t shift_left, shift_right;
  772. int count1, count2;
  773. if (!buf_size)
  774. return 0;
  775. //should protect all 4bit ADPCM variants
  776. //8 is needed for CODEC_ID_ADPCM_IMA_WAV with 2 channels
  777. //
  778. if(*data_size/4 < buf_size + 8)
  779. return -1;
  780. samples = data;
  781. samples_end= samples + *data_size/2;
  782. *data_size= 0;
  783. src = buf;
  784. st = avctx->channels == 2 ? 1 : 0;
  785. switch(avctx->codec->id) {
  786. case CODEC_ID_ADPCM_IMA_QT:
  787. n = (buf_size - 2);/* >> 2*avctx->channels;*/
  788. channel = c->channel;
  789. cs = &(c->status[channel]);
  790. /* (pppppp) (piiiiiii) */
  791. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  792. cs->predictor = (*src++) << 8;
  793. cs->predictor |= (*src & 0x80);
  794. cs->predictor &= 0xFF80;
  795. /* sign extension */
  796. if(cs->predictor & 0x8000)
  797. cs->predictor -= 0x10000;
  798. cs->predictor = av_clip_int16(cs->predictor);
  799. cs->step_index = (*src++) & 0x7F;
  800. if (cs->step_index > 88){
  801. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  802. cs->step_index = 88;
  803. }
  804. cs->step = step_table[cs->step_index];
  805. if (st && channel)
  806. samples++;
  807. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  808. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
  809. samples += avctx->channels;
  810. *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
  811. samples += avctx->channels;
  812. src ++;
  813. }
  814. if(st) { /* handle stereo interlacing */
  815. c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
  816. if(channel == 1) { /* wait for the other packet before outputing anything */
  817. return src - buf;
  818. }
  819. }
  820. break;
  821. case CODEC_ID_ADPCM_IMA_WAV:
  822. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  823. buf_size = avctx->block_align;
  824. // samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
  825. for(i=0; i<avctx->channels; i++){
  826. cs = &(c->status[i]);
  827. cs->predictor = (int16_t)(src[0] + (src[1]<<8));
  828. src+=2;
  829. // XXX: is this correct ??: *samples++ = cs->predictor;
  830. cs->step_index = *src++;
  831. if (cs->step_index > 88){
  832. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  833. cs->step_index = 88;
  834. }
  835. if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
  836. }
  837. while(src < buf + buf_size){
  838. for(m=0; m<4; m++){
  839. for(i=0; i<=st; i++)
  840. *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);
  841. for(i=0; i<=st; i++)
  842. *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4 , 3);
  843. src++;
  844. }
  845. src += 4*st;
  846. }
  847. break;
  848. case CODEC_ID_ADPCM_4XM:
  849. cs = &(c->status[0]);
  850. c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  851. if(st){
  852. c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  853. }
  854. c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  855. if(st){
  856. c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  857. }
  858. if (cs->step_index < 0) cs->step_index = 0;
  859. if (cs->step_index > 88) cs->step_index = 88;
  860. m= (buf_size - (src - buf))>>st;
  861. for(i=0; i<m; i++) {
  862. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
  863. if (st)
  864. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
  865. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
  866. if (st)
  867. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
  868. }
  869. src += m<<st;
  870. break;
  871. case CODEC_ID_ADPCM_MS:
  872. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  873. buf_size = avctx->block_align;
  874. n = buf_size - 7 * avctx->channels;
  875. if (n < 0)
  876. return -1;
  877. block_predictor[0] = av_clip(*src++, 0, 7);
  878. block_predictor[1] = 0;
  879. if (st)
  880. block_predictor[1] = av_clip(*src++, 0, 7);
  881. c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  882. src+=2;
  883. if (st){
  884. c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  885. src+=2;
  886. }
  887. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  888. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  889. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  890. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  891. c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  892. src+=2;
  893. if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  894. if (st) src+=2;
  895. c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  896. src+=2;
  897. if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  898. if (st) src+=2;
  899. *samples++ = c->status[0].sample1;
  900. if (st) *samples++ = c->status[1].sample1;
  901. *samples++ = c->status[0].sample2;
  902. if (st) *samples++ = c->status[1].sample2;
  903. for(;n>0;n--) {
  904. *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  905. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  906. src ++;
  907. }
  908. break;
  909. case CODEC_ID_ADPCM_IMA_DK4:
  910. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  911. buf_size = avctx->block_align;
  912. c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
  913. c->status[0].step_index = src[2];
  914. src += 4;
  915. *samples++ = c->status[0].predictor;
  916. if (st) {
  917. c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
  918. c->status[1].step_index = src[2];
  919. src += 4;
  920. *samples++ = c->status[1].predictor;
  921. }
  922. while (src < buf + buf_size) {
  923. /* take care of the top nibble (always left or mono channel) */
  924. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  925. (src[0] >> 4) & 0x0F, 3);
  926. /* take care of the bottom nibble, which is right sample for
  927. * stereo, or another mono sample */
  928. if (st)
  929. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  930. src[0] & 0x0F, 3);
  931. else
  932. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  933. src[0] & 0x0F, 3);
  934. src++;
  935. }
  936. break;
  937. case CODEC_ID_ADPCM_IMA_DK3:
  938. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  939. buf_size = avctx->block_align;
  940. if(buf_size + 16 > (samples_end - samples)*3/8)
  941. return -1;
  942. c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
  943. c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
  944. c->status[0].step_index = src[14];
  945. c->status[1].step_index = src[15];
  946. /* sign extend the predictors */
  947. src += 16;
  948. diff_channel = c->status[1].predictor;
  949. /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
  950. * the buffer is consumed */
  951. while (1) {
  952. /* for this algorithm, c->status[0] is the sum channel and
  953. * c->status[1] is the diff channel */
  954. /* process the first predictor of the sum channel */
  955. DK3_GET_NEXT_NIBBLE();
  956. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  957. /* process the diff channel predictor */
  958. DK3_GET_NEXT_NIBBLE();
  959. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  960. /* process the first pair of stereo PCM samples */
  961. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  962. *samples++ = c->status[0].predictor + c->status[1].predictor;
  963. *samples++ = c->status[0].predictor - c->status[1].predictor;
  964. /* process the second predictor of the sum channel */
  965. DK3_GET_NEXT_NIBBLE();
  966. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  967. /* process the second pair of stereo PCM samples */
  968. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  969. *samples++ = c->status[0].predictor + c->status[1].predictor;
  970. *samples++ = c->status[0].predictor - c->status[1].predictor;
  971. }
  972. break;
  973. case CODEC_ID_ADPCM_IMA_WS:
  974. /* no per-block initialization; just start decoding the data */
  975. while (src < buf + buf_size) {
  976. if (st) {
  977. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  978. (src[0] >> 4) & 0x0F, 3);
  979. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  980. src[0] & 0x0F, 3);
  981. } else {
  982. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  983. (src[0] >> 4) & 0x0F, 3);
  984. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  985. src[0] & 0x0F, 3);
  986. }
  987. src++;
  988. }
  989. break;
  990. case CODEC_ID_ADPCM_XA:
  991. c->status[0].sample1 = c->status[0].sample2 =
  992. c->status[1].sample1 = c->status[1].sample2 = 0;
  993. while (buf_size >= 128) {
  994. xa_decode(samples, src, &c->status[0], &c->status[1],
  995. avctx->channels);
  996. src += 128;
  997. samples += 28 * 8;
  998. buf_size -= 128;
  999. }
  1000. break;
  1001. case CODEC_ID_ADPCM_IMA_EA_EACS:
  1002. samples_in_chunk = bytestream_get_le32(&src) >> (1-st);
  1003. if (samples_in_chunk > buf_size-4-(8<<st)) {
  1004. src += buf_size - 4;
  1005. break;
  1006. }
  1007. for (i=0; i<=st; i++)
  1008. c->status[i].step_index = bytestream_get_le32(&src);
  1009. for (i=0; i<=st; i++)
  1010. c->status[i].predictor = bytestream_get_le32(&src);
  1011. for (; samples_in_chunk; samples_in_chunk--, src++) {
  1012. *samples++ = adpcm_ima_expand_nibble(&c->status[0], *src>>4, 3);
  1013. *samples++ = adpcm_ima_expand_nibble(&c->status[st], *src&0x0F, 3);
  1014. }
  1015. break;
  1016. case CODEC_ID_ADPCM_IMA_EA_SEAD:
  1017. for (; src < buf+buf_size; src++) {
  1018. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] >> 4, 6);
  1019. *samples++ = adpcm_ima_expand_nibble(&c->status[st],src[0]&0x0F, 6);
  1020. }
  1021. break;
  1022. case CODEC_ID_ADPCM_EA:
  1023. samples_in_chunk = AV_RL32(src);
  1024. if (samples_in_chunk >= ((buf_size - 12) * 2)) {
  1025. src += buf_size;
  1026. break;
  1027. }
  1028. src += 4;
  1029. current_left_sample = (int16_t)AV_RL16(src);
  1030. src += 2;
  1031. previous_left_sample = (int16_t)AV_RL16(src);
  1032. src += 2;
  1033. current_right_sample = (int16_t)AV_RL16(src);
  1034. src += 2;
  1035. previous_right_sample = (int16_t)AV_RL16(src);
  1036. src += 2;
  1037. for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
  1038. coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
  1039. coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
  1040. coeff1r = ea_adpcm_table[*src & 0x0F];
  1041. coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
  1042. src++;
  1043. shift_left = ((*src >> 4) & 0x0F) + 8;
  1044. shift_right = (*src & 0x0F) + 8;
  1045. src++;
  1046. for (count2 = 0; count2 < 28; count2++) {
  1047. next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
  1048. next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
  1049. src++;
  1050. next_left_sample = (next_left_sample +
  1051. (current_left_sample * coeff1l) +
  1052. (previous_left_sample * coeff2l) + 0x80) >> 8;
  1053. next_right_sample = (next_right_sample +
  1054. (current_right_sample * coeff1r) +
  1055. (previous_right_sample * coeff2r) + 0x80) >> 8;
  1056. previous_left_sample = current_left_sample;
  1057. current_left_sample = av_clip_int16(next_left_sample);
  1058. previous_right_sample = current_right_sample;
  1059. current_right_sample = av_clip_int16(next_right_sample);
  1060. *samples++ = (unsigned short)current_left_sample;
  1061. *samples++ = (unsigned short)current_right_sample;
  1062. }
  1063. }
  1064. break;
  1065. case CODEC_ID_ADPCM_EA_R1:
  1066. case CODEC_ID_ADPCM_EA_R2:
  1067. case CODEC_ID_ADPCM_EA_R3: {
  1068. /* channel numbering
  1069. 2chan: 0=fl, 1=fr
  1070. 4chan: 0=fl, 1=rl, 2=fr, 3=rr
  1071. 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
  1072. const int big_endian = avctx->codec->id == CODEC_ID_ADPCM_EA_R3;
  1073. int32_t previous_sample, current_sample, next_sample;
  1074. int32_t coeff1, coeff2;
  1075. uint8_t shift;
  1076. unsigned int channel;
  1077. uint16_t *samplesC;
  1078. uint8_t *srcC;
  1079. samples_in_chunk = (big_endian ? bytestream_get_be32(&src)
  1080. : bytestream_get_le32(&src)) / 28;
  1081. if (samples_in_chunk > UINT32_MAX/(28*avctx->channels) ||
  1082. 28*samples_in_chunk*avctx->channels > samples_end-samples) {
  1083. src += buf_size - 4;
  1084. break;
  1085. }
  1086. for (channel=0; channel<avctx->channels; channel++) {
  1087. srcC = src + (big_endian ? bytestream_get_be32(&src)
  1088. : bytestream_get_le32(&src))
  1089. + (avctx->channels-channel-1) * 4;
  1090. samplesC = samples + channel;
  1091. if (avctx->codec->id == CODEC_ID_ADPCM_EA_R1) {
  1092. current_sample = (int16_t)bytestream_get_le16(&srcC);
  1093. previous_sample = (int16_t)bytestream_get_le16(&srcC);
  1094. } else {
  1095. current_sample = c->status[channel].predictor;
  1096. previous_sample = c->status[channel].prev_sample;
  1097. }
  1098. for (count1=0; count1<samples_in_chunk; count1++) {
  1099. if (*srcC == 0xEE) { /* only seen in R2 and R3 */
  1100. srcC++;
  1101. current_sample = (int16_t)bytestream_get_be16(&srcC);
  1102. previous_sample = (int16_t)bytestream_get_be16(&srcC);
  1103. for (count2=0; count2<28; count2++) {
  1104. *samplesC = (int16_t)bytestream_get_be16(&srcC);
  1105. samplesC += avctx->channels;
  1106. }
  1107. } else {
  1108. coeff1 = ea_adpcm_table[ (*srcC>>4) & 0x0F ];
  1109. coeff2 = ea_adpcm_table[((*srcC>>4) & 0x0F) + 4];
  1110. shift = (*srcC++ & 0x0F) + 8;
  1111. for (count2=0; count2<28; count2++) {
  1112. if (count2 & 1)
  1113. next_sample = ((*srcC++ & 0x0F) << 28) >> shift;
  1114. else
  1115. next_sample = ((*srcC & 0xF0) << 24) >> shift;
  1116. next_sample += (current_sample * coeff1) +
  1117. (previous_sample * coeff2);
  1118. next_sample = av_clip_int16(next_sample >> 8);
  1119. previous_sample = current_sample;
  1120. current_sample = next_sample;
  1121. *samplesC = current_sample;
  1122. samplesC += avctx->channels;
  1123. }
  1124. }
  1125. }
  1126. if (avctx->codec->id != CODEC_ID_ADPCM_EA_R1) {
  1127. c->status[channel].predictor = current_sample;
  1128. c->status[channel].prev_sample = previous_sample;
  1129. }
  1130. }
  1131. src = src + buf_size - (4 + 4*avctx->channels);
  1132. samples += 28 * samples_in_chunk * avctx->channels;
  1133. break;
  1134. }
  1135. case CODEC_ID_ADPCM_IMA_AMV:
  1136. case CODEC_ID_ADPCM_IMA_SMJPEG:
  1137. c->status[0].predictor = (int16_t)bytestream_get_le16(&src);
  1138. c->status[0].step_index = bytestream_get_le16(&src);
  1139. if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
  1140. src+=4;
  1141. while (src < buf + buf_size) {
  1142. char hi, lo;
  1143. lo = *src & 0x0F;
  1144. hi = (*src >> 4) & 0x0F;
  1145. if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
  1146. FFSWAP(char, hi, lo);
  1147. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1148. lo, 3);
  1149. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1150. hi, 3);
  1151. src++;
  1152. }
  1153. break;
  1154. case CODEC_ID_ADPCM_CT:
  1155. while (src < buf + buf_size) {
  1156. if (st) {
  1157. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1158. (src[0] >> 4) & 0x0F);
  1159. *samples++ = adpcm_ct_expand_nibble(&c->status[1],
  1160. src[0] & 0x0F);
  1161. } else {
  1162. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1163. (src[0] >> 4) & 0x0F);
  1164. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1165. src[0] & 0x0F);
  1166. }
  1167. src++;
  1168. }
  1169. break;
  1170. case CODEC_ID_ADPCM_SBPRO_4:
  1171. case CODEC_ID_ADPCM_SBPRO_3:
  1172. case CODEC_ID_ADPCM_SBPRO_2:
  1173. if (!c->status[0].step_index) {
  1174. /* the first byte is a raw sample */
  1175. *samples++ = 128 * (*src++ - 0x80);
  1176. if (st)
  1177. *samples++ = 128 * (*src++ - 0x80);
  1178. c->status[0].step_index = 1;
  1179. }
  1180. if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
  1181. while (src < buf + buf_size) {
  1182. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1183. (src[0] >> 4) & 0x0F, 4, 0);
  1184. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1185. src[0] & 0x0F, 4, 0);
  1186. src++;
  1187. }
  1188. } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
  1189. while (src < buf + buf_size && samples + 2 < samples_end) {
  1190. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1191. (src[0] >> 5) & 0x07, 3, 0);
  1192. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1193. (src[0] >> 2) & 0x07, 3, 0);
  1194. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1195. src[0] & 0x03, 2, 0);
  1196. src++;
  1197. }
  1198. } else {
  1199. while (src < buf + buf_size && samples + 3 < samples_end) {
  1200. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1201. (src[0] >> 6) & 0x03, 2, 2);
  1202. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1203. (src[0] >> 4) & 0x03, 2, 2);
  1204. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1205. (src[0] >> 2) & 0x03, 2, 2);
  1206. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1207. src[0] & 0x03, 2, 2);
  1208. src++;
  1209. }
  1210. }
  1211. break;
  1212. case CODEC_ID_ADPCM_SWF:
  1213. {
  1214. GetBitContext gb;
  1215. const int *table;
  1216. int k0, signmask, nb_bits, count;
  1217. int size = buf_size*8;
  1218. init_get_bits(&gb, buf, size);
  1219. //read bits & initial values
  1220. nb_bits = get_bits(&gb, 2)+2;
  1221. //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
  1222. table = swf_index_tables[nb_bits-2];
  1223. k0 = 1 << (nb_bits-2);
  1224. signmask = 1 << (nb_bits-1);
  1225. while (get_bits_count(&gb) <= size - 22*avctx->channels) {
  1226. for (i = 0; i < avctx->channels; i++) {
  1227. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  1228. c->status[i].step_index = get_bits(&gb, 6);
  1229. }
  1230. for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
  1231. int i;
  1232. for (i = 0; i < avctx->channels; i++) {
  1233. // similar to IMA adpcm
  1234. int delta = get_bits(&gb, nb_bits);
  1235. int step = step_table[c->status[i].step_index];
  1236. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  1237. int k = k0;
  1238. do {
  1239. if (delta & k)
  1240. vpdiff += step;
  1241. step >>= 1;
  1242. k >>= 1;
  1243. } while(k);
  1244. vpdiff += step;
  1245. if (delta & signmask)
  1246. c->status[i].predictor -= vpdiff;
  1247. else
  1248. c->status[i].predictor += vpdiff;
  1249. c->status[i].step_index += table[delta & (~signmask)];
  1250. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  1251. c->status[i].predictor = av_clip_int16(c->status[i].predictor);
  1252. *samples++ = c->status[i].predictor;
  1253. if (samples >= samples_end) {
  1254. av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
  1255. return -1;
  1256. }
  1257. }
  1258. }
  1259. }
  1260. src += buf_size;
  1261. break;
  1262. }
  1263. case CODEC_ID_ADPCM_YAMAHA:
  1264. while (src < buf + buf_size) {
  1265. if (st) {
  1266. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1267. src[0] & 0x0F);
  1268. *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
  1269. (src[0] >> 4) & 0x0F);
  1270. } else {
  1271. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1272. src[0] & 0x0F);
  1273. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1274. (src[0] >> 4) & 0x0F);
  1275. }
  1276. src++;
  1277. }
  1278. break;
  1279. case CODEC_ID_ADPCM_THP:
  1280. {
  1281. int table[2][16];
  1282. unsigned int samplecnt;
  1283. int prev[2][2];
  1284. int ch;
  1285. if (buf_size < 80) {
  1286. av_log(avctx, AV_LOG_ERROR, "frame too small\n");
  1287. return -1;
  1288. }
  1289. src+=4;
  1290. samplecnt = bytestream_get_be32(&src);
  1291. for (i = 0; i < 32; i++)
  1292. table[0][i] = (int16_t)bytestream_get_be16(&src);
  1293. /* Initialize the previous sample. */
  1294. for (i = 0; i < 4; i++)
  1295. prev[0][i] = (int16_t)bytestream_get_be16(&src);
  1296. if (samplecnt >= (samples_end - samples) / (st + 1)) {
  1297. av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
  1298. return -1;
  1299. }
  1300. for (ch = 0; ch <= st; ch++) {
  1301. samples = (unsigned short *) data + ch;
  1302. /* Read in every sample for this channel. */
  1303. for (i = 0; i < samplecnt / 14; i++) {
  1304. int index = (*src >> 4) & 7;
  1305. unsigned int exp = 28 - (*src++ & 15);
  1306. int factor1 = table[ch][index * 2];
  1307. int factor2 = table[ch][index * 2 + 1];
  1308. /* Decode 14 samples. */
  1309. for (n = 0; n < 14; n++) {
  1310. int32_t sampledat;
  1311. if(n&1) sampledat= *src++ <<28;
  1312. else sampledat= (*src&0xF0)<<24;
  1313. sampledat = ((prev[ch][0]*factor1
  1314. + prev[ch][1]*factor2) >> 11) + (sampledat>>exp);
  1315. *samples = av_clip_int16(sampledat);
  1316. prev[ch][1] = prev[ch][0];
  1317. prev[ch][0] = *samples++;
  1318. /* In case of stereo, skip one sample, this sample
  1319. is for the other channel. */
  1320. samples += st;
  1321. }
  1322. }
  1323. }
  1324. /* In the previous loop, in case stereo is used, samples is
  1325. increased exactly one time too often. */
  1326. samples -= st;
  1327. break;
  1328. }
  1329. default:
  1330. return -1;
  1331. }
  1332. *data_size = (uint8_t *)samples - (uint8_t *)data;
  1333. return src - buf;
  1334. }
  1335. #ifdef CONFIG_ENCODERS
  1336. #define ADPCM_ENCODER(id,name) \
  1337. AVCodec name ## _encoder = { \
  1338. #name, \
  1339. CODEC_TYPE_AUDIO, \
  1340. id, \
  1341. sizeof(ADPCMContext), \
  1342. adpcm_encode_init, \
  1343. adpcm_encode_frame, \
  1344. adpcm_encode_close, \
  1345. NULL, \
  1346. };
  1347. #else
  1348. #define ADPCM_ENCODER(id,name)
  1349. #endif
  1350. #ifdef CONFIG_DECODERS
  1351. #define ADPCM_DECODER(id,name) \
  1352. AVCodec name ## _decoder = { \
  1353. #name, \
  1354. CODEC_TYPE_AUDIO, \
  1355. id, \
  1356. sizeof(ADPCMContext), \
  1357. adpcm_decode_init, \
  1358. NULL, \
  1359. NULL, \
  1360. adpcm_decode_frame, \
  1361. };
  1362. #else
  1363. #define ADPCM_DECODER(id,name)
  1364. #endif
  1365. #define ADPCM_CODEC(id, name) \
  1366. ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
  1367. ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
  1368. ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
  1369. ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
  1370. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv);
  1371. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
  1372. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
  1373. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs);
  1374. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead);
  1375. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
  1376. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
  1377. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
  1378. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
  1379. ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
  1380. ADPCM_CODEC(CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1);
  1381. ADPCM_CODEC(CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2);
  1382. ADPCM_CODEC(CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3);
  1383. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4);
  1384. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3);
  1385. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2);
  1386. ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);
  1387. ADPCM_CODEC(CODEC_ID_ADPCM_THP, adpcm_thp);
  1388. ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
  1389. ADPCM_CODEC(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);
  1390. #undef ADPCM_CODEC