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.

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