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.

1703 lines
62KB

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