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.

1688 lines
61KB

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