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.

1708 lines
62KB

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