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.

1656 lines
60KB

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