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.

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