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