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.

1728 lines
63KB

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