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.

1689 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 *= avctx->channels; n>0; n--) {
  587. int nibble;
  588. nibble = adpcm_yamaha_compress_sample(&c->status[ 0], *samples++);
  589. nibble |= adpcm_yamaha_compress_sample(&c->status[st], *samples++) << 4;
  590. *dst++ = nibble;
  591. }
  592. break;
  593. default:
  594. return -1;
  595. }
  596. return dst - frame;
  597. }
  598. #endif //CONFIG_ENCODERS
  599. static av_cold int adpcm_decode_init(AVCodecContext * avctx)
  600. {
  601. ADPCMContext *c = avctx->priv_data;
  602. unsigned int max_channels = 2;
  603. switch(avctx->codec->id) {
  604. case CODEC_ID_ADPCM_EA_R1:
  605. case CODEC_ID_ADPCM_EA_R2:
  606. case CODEC_ID_ADPCM_EA_R3:
  607. max_channels = 6;
  608. break;
  609. }
  610. if(avctx->channels > max_channels){
  611. return -1;
  612. }
  613. switch(avctx->codec->id) {
  614. case CODEC_ID_ADPCM_CT:
  615. c->status[0].step = c->status[1].step = 511;
  616. break;
  617. case CODEC_ID_ADPCM_IMA_WS:
  618. if (avctx->extradata && avctx->extradata_size == 2 * 4) {
  619. c->status[0].predictor = AV_RL32(avctx->extradata);
  620. c->status[1].predictor = AV_RL32(avctx->extradata + 4);
  621. }
  622. break;
  623. default:
  624. break;
  625. }
  626. avctx->sample_fmt = SAMPLE_FMT_S16;
  627. return 0;
  628. }
  629. static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
  630. {
  631. int step_index;
  632. int predictor;
  633. int sign, delta, diff, step;
  634. step = step_table[c->step_index];
  635. step_index = c->step_index + index_table[(unsigned)nibble];
  636. if (step_index < 0) step_index = 0;
  637. else if (step_index > 88) step_index = 88;
  638. sign = nibble & 8;
  639. delta = nibble & 7;
  640. /* perform direct multiplication instead of series of jumps proposed by
  641. * the reference ADPCM implementation since modern CPUs can do the mults
  642. * quickly enough */
  643. diff = ((2 * delta + 1) * step) >> shift;
  644. predictor = c->predictor;
  645. if (sign) predictor -= diff;
  646. else predictor += diff;
  647. c->predictor = av_clip_int16(predictor);
  648. c->step_index = step_index;
  649. return (short)c->predictor;
  650. }
  651. static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
  652. {
  653. int predictor;
  654. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
  655. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  656. c->sample2 = c->sample1;
  657. c->sample1 = av_clip_int16(predictor);
  658. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
  659. if (c->idelta < 16) c->idelta = 16;
  660. return c->sample1;
  661. }
  662. static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
  663. {
  664. int sign, delta, diff;
  665. int new_step;
  666. sign = nibble & 8;
  667. delta = nibble & 7;
  668. /* perform direct multiplication instead of series of jumps proposed by
  669. * the reference ADPCM implementation since modern CPUs can do the mults
  670. * quickly enough */
  671. diff = ((2 * delta + 1) * c->step) >> 3;
  672. /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
  673. c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
  674. c->predictor = av_clip_int16(c->predictor);
  675. /* calculate new step and clamp it to range 511..32767 */
  676. new_step = (AdaptationTable[nibble & 7] * c->step) >> 8;
  677. c->step = av_clip(new_step, 511, 32767);
  678. return (short)c->predictor;
  679. }
  680. static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
  681. {
  682. int sign, delta, diff;
  683. sign = nibble & (1<<(size-1));
  684. delta = nibble & ((1<<(size-1))-1);
  685. diff = delta << (7 + c->step + shift);
  686. /* clamp result */
  687. c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
  688. /* calculate new step */
  689. if (delta >= (2*size - 3) && c->step < 3)
  690. c->step++;
  691. else if (delta == 0 && c->step > 0)
  692. c->step--;
  693. return (short) c->predictor;
  694. }
  695. static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
  696. {
  697. if(!c->step) {
  698. c->predictor = 0;
  699. c->step = 127;
  700. }
  701. c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
  702. c->predictor = av_clip_int16(c->predictor);
  703. c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
  704. c->step = av_clip(c->step, 127, 24567);
  705. return c->predictor;
  706. }
  707. static void xa_decode(short *out, const unsigned char *in,
  708. ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
  709. {
  710. int i, j;
  711. int shift,filter,f0,f1;
  712. int s_1,s_2;
  713. int d,s,t;
  714. for(i=0;i<4;i++) {
  715. shift = 12 - (in[4+i*2] & 15);
  716. filter = in[4+i*2] >> 4;
  717. f0 = xa_adpcm_table[filter][0];
  718. f1 = xa_adpcm_table[filter][1];
  719. s_1 = left->sample1;
  720. s_2 = left->sample2;
  721. for(j=0;j<28;j++) {
  722. d = in[16+i+j*4];
  723. t = (signed char)(d<<4)>>4;
  724. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  725. s_2 = s_1;
  726. s_1 = av_clip_int16(s);
  727. *out = s_1;
  728. out += inc;
  729. }
  730. if (inc==2) { /* stereo */
  731. left->sample1 = s_1;
  732. left->sample2 = s_2;
  733. s_1 = right->sample1;
  734. s_2 = right->sample2;
  735. out = out + 1 - 28*2;
  736. }
  737. shift = 12 - (in[5+i*2] & 15);
  738. filter = in[5+i*2] >> 4;
  739. f0 = xa_adpcm_table[filter][0];
  740. f1 = xa_adpcm_table[filter][1];
  741. for(j=0;j<28;j++) {
  742. d = in[16+i+j*4];
  743. t = (signed char)d >> 4;
  744. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  745. s_2 = s_1;
  746. s_1 = av_clip_int16(s);
  747. *out = s_1;
  748. out += inc;
  749. }
  750. if (inc==2) { /* stereo */
  751. right->sample1 = s_1;
  752. right->sample2 = s_2;
  753. out -= 1;
  754. } else {
  755. left->sample1 = s_1;
  756. left->sample2 = s_2;
  757. }
  758. }
  759. }
  760. /* DK3 ADPCM support macro */
  761. #define DK3_GET_NEXT_NIBBLE() \
  762. if (decode_top_nibble_next) \
  763. { \
  764. nibble = last_byte >> 4; \
  765. decode_top_nibble_next = 0; \
  766. } \
  767. else \
  768. { \
  769. last_byte = *src++; \
  770. if (src >= buf + buf_size) break; \
  771. nibble = last_byte & 0x0F; \
  772. decode_top_nibble_next = 1; \
  773. }
  774. static int adpcm_decode_frame(AVCodecContext *avctx,
  775. void *data, int *data_size,
  776. AVPacket *avpkt)
  777. {
  778. const uint8_t *buf = avpkt->data;
  779. int buf_size = avpkt->size;
  780. ADPCMContext *c = avctx->priv_data;
  781. ADPCMChannelStatus *cs;
  782. int n, m, channel, i;
  783. int block_predictor[2];
  784. short *samples;
  785. short *samples_end;
  786. const uint8_t *src;
  787. int st; /* stereo */
  788. /* DK3 ADPCM accounting variables */
  789. unsigned char last_byte = 0;
  790. unsigned char nibble;
  791. int decode_top_nibble_next = 0;
  792. int diff_channel;
  793. /* EA ADPCM state variables */
  794. uint32_t samples_in_chunk;
  795. int32_t previous_left_sample, previous_right_sample;
  796. int32_t current_left_sample, current_right_sample;
  797. int32_t next_left_sample, next_right_sample;
  798. int32_t coeff1l, coeff2l, coeff1r, coeff2r;
  799. uint8_t shift_left, shift_right;
  800. int count1, count2;
  801. int coeff[2][2], shift[2];//used in EA MAXIS ADPCM
  802. if (!buf_size)
  803. return 0;
  804. //should protect all 4bit ADPCM variants
  805. //8 is needed for CODEC_ID_ADPCM_IMA_WAV with 2 channels
  806. //
  807. if(*data_size/4 < buf_size + 8)
  808. return -1;
  809. samples = data;
  810. samples_end= samples + *data_size/2;
  811. *data_size= 0;
  812. src = buf;
  813. st = avctx->channels == 2 ? 1 : 0;
  814. switch(avctx->codec->id) {
  815. case CODEC_ID_ADPCM_IMA_QT:
  816. n = buf_size - 2*avctx->channels;
  817. for (channel = 0; channel < avctx->channels; channel++) {
  818. cs = &(c->status[channel]);
  819. /* (pppppp) (piiiiiii) */
  820. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  821. cs->predictor = (*src++) << 8;
  822. cs->predictor |= (*src & 0x80);
  823. cs->predictor &= 0xFF80;
  824. /* sign extension */
  825. if(cs->predictor & 0x8000)
  826. cs->predictor -= 0x10000;
  827. cs->predictor = av_clip_int16(cs->predictor);
  828. cs->step_index = (*src++) & 0x7F;
  829. if (cs->step_index > 88){
  830. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  831. cs->step_index = 88;
  832. }
  833. cs->step = step_table[cs->step_index];
  834. samples = (short*)data + channel;
  835. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  836. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
  837. samples += avctx->channels;
  838. *samples = adpcm_ima_expand_nibble(cs, src[0] >> 4 , 3);
  839. samples += avctx->channels;
  840. src ++;
  841. }
  842. }
  843. if (st)
  844. samples--;
  845. break;
  846. case CODEC_ID_ADPCM_IMA_WAV:
  847. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  848. buf_size = avctx->block_align;
  849. // samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
  850. for(i=0; i<avctx->channels; i++){
  851. cs = &(c->status[i]);
  852. cs->predictor = *samples++ = (int16_t)bytestream_get_le16(&src);
  853. cs->step_index = *src++;
  854. if (cs->step_index > 88){
  855. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  856. cs->step_index = 88;
  857. }
  858. if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
  859. }
  860. while(src < buf + buf_size){
  861. for(m=0; m<4; m++){
  862. for(i=0; i<=st; i++)
  863. *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);
  864. for(i=0; i<=st; i++)
  865. *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4 , 3);
  866. src++;
  867. }
  868. src += 4*st;
  869. }
  870. break;
  871. case CODEC_ID_ADPCM_4XM:
  872. cs = &(c->status[0]);
  873. c->status[0].predictor= (int16_t)bytestream_get_le16(&src);
  874. if(st){
  875. c->status[1].predictor= (int16_t)bytestream_get_le16(&src);
  876. }
  877. c->status[0].step_index= (int16_t)bytestream_get_le16(&src);
  878. if(st){
  879. c->status[1].step_index= (int16_t)bytestream_get_le16(&src);
  880. }
  881. if (cs->step_index < 0) cs->step_index = 0;
  882. if (cs->step_index > 88) cs->step_index = 88;
  883. m= (buf_size - (src - buf))>>st;
  884. for(i=0; i<m; i++) {
  885. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
  886. if (st)
  887. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
  888. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
  889. if (st)
  890. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
  891. }
  892. src += m<<st;
  893. break;
  894. case CODEC_ID_ADPCM_MS:
  895. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  896. buf_size = avctx->block_align;
  897. n = buf_size - 7 * avctx->channels;
  898. if (n < 0)
  899. return -1;
  900. block_predictor[0] = av_clip(*src++, 0, 6);
  901. block_predictor[1] = 0;
  902. if (st)
  903. block_predictor[1] = av_clip(*src++, 0, 6);
  904. c->status[0].idelta = (int16_t)bytestream_get_le16(&src);
  905. if (st){
  906. c->status[1].idelta = (int16_t)bytestream_get_le16(&src);
  907. }
  908. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  909. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  910. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  911. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  912. c->status[0].sample1 = bytestream_get_le16(&src);
  913. if (st) c->status[1].sample1 = bytestream_get_le16(&src);
  914. c->status[0].sample2 = bytestream_get_le16(&src);
  915. if (st) c->status[1].sample2 = bytestream_get_le16(&src);
  916. *samples++ = c->status[0].sample2;
  917. if (st) *samples++ = c->status[1].sample2;
  918. *samples++ = c->status[0].sample1;
  919. if (st) *samples++ = c->status[1].sample1;
  920. for(;n>0;n--) {
  921. *samples++ = adpcm_ms_expand_nibble(&c->status[0 ], src[0] >> 4 );
  922. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  923. src ++;
  924. }
  925. break;
  926. case CODEC_ID_ADPCM_IMA_DK4:
  927. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  928. buf_size = avctx->block_align;
  929. c->status[0].predictor = (int16_t)bytestream_get_le16(&src);
  930. c->status[0].step_index = *src++;
  931. src++;
  932. *samples++ = c->status[0].predictor;
  933. if (st) {
  934. c->status[1].predictor = (int16_t)bytestream_get_le16(&src);
  935. c->status[1].step_index = *src++;
  936. src++;
  937. *samples++ = c->status[1].predictor;
  938. }
  939. while (src < buf + buf_size) {
  940. /* take care of the top nibble (always left or mono channel) */
  941. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  942. src[0] >> 4, 3);
  943. /* take care of the bottom nibble, which is right sample for
  944. * stereo, or another mono sample */
  945. if (st)
  946. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  947. src[0] & 0x0F, 3);
  948. else
  949. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  950. src[0] & 0x0F, 3);
  951. src++;
  952. }
  953. break;
  954. case CODEC_ID_ADPCM_IMA_DK3:
  955. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  956. buf_size = avctx->block_align;
  957. if(buf_size + 16 > (samples_end - samples)*3/8)
  958. return -1;
  959. c->status[0].predictor = (int16_t)AV_RL16(src + 10);
  960. c->status[1].predictor = (int16_t)AV_RL16(src + 12);
  961. c->status[0].step_index = src[14];
  962. c->status[1].step_index = src[15];
  963. /* sign extend the predictors */
  964. src += 16;
  965. diff_channel = c->status[1].predictor;
  966. /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
  967. * the buffer is consumed */
  968. while (1) {
  969. /* for this algorithm, c->status[0] is the sum channel and
  970. * c->status[1] is the diff channel */
  971. /* process the first predictor of the sum channel */
  972. DK3_GET_NEXT_NIBBLE();
  973. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  974. /* process the diff channel predictor */
  975. DK3_GET_NEXT_NIBBLE();
  976. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  977. /* process the first pair of stereo PCM samples */
  978. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  979. *samples++ = c->status[0].predictor + c->status[1].predictor;
  980. *samples++ = c->status[0].predictor - c->status[1].predictor;
  981. /* process the second predictor of the sum channel */
  982. DK3_GET_NEXT_NIBBLE();
  983. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  984. /* process the second pair of stereo PCM samples */
  985. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  986. *samples++ = c->status[0].predictor + c->status[1].predictor;
  987. *samples++ = c->status[0].predictor - c->status[1].predictor;
  988. }
  989. break;
  990. case CODEC_ID_ADPCM_IMA_ISS:
  991. c->status[0].predictor = (int16_t)AV_RL16(src + 0);
  992. c->status[0].step_index = src[2];
  993. src += 4;
  994. if(st) {
  995. c->status[1].predictor = (int16_t)AV_RL16(src + 0);
  996. c->status[1].step_index = src[2];
  997. src += 4;
  998. }
  999. while (src < buf + buf_size) {
  1000. if (st) {
  1001. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1002. src[0] >> 4 , 3);
  1003. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  1004. src[0] & 0x0F, 3);
  1005. } else {
  1006. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1007. src[0] & 0x0F, 3);
  1008. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1009. src[0] >> 4 , 3);
  1010. }
  1011. src++;
  1012. }
  1013. break;
  1014. case CODEC_ID_ADPCM_IMA_WS:
  1015. /* no per-block initialization; just start decoding the data */
  1016. while (src < buf + buf_size) {
  1017. if (st) {
  1018. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1019. src[0] >> 4 , 3);
  1020. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  1021. src[0] & 0x0F, 3);
  1022. } else {
  1023. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1024. src[0] >> 4 , 3);
  1025. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1026. src[0] & 0x0F, 3);
  1027. }
  1028. src++;
  1029. }
  1030. break;
  1031. case CODEC_ID_ADPCM_XA:
  1032. while (buf_size >= 128) {
  1033. xa_decode(samples, src, &c->status[0], &c->status[1],
  1034. avctx->channels);
  1035. src += 128;
  1036. samples += 28 * 8;
  1037. buf_size -= 128;
  1038. }
  1039. break;
  1040. case CODEC_ID_ADPCM_IMA_EA_EACS:
  1041. samples_in_chunk = bytestream_get_le32(&src) >> (1-st);
  1042. if (samples_in_chunk > buf_size-4-(8<<st)) {
  1043. src += buf_size - 4;
  1044. break;
  1045. }
  1046. for (i=0; i<=st; i++)
  1047. c->status[i].step_index = bytestream_get_le32(&src);
  1048. for (i=0; i<=st; i++)
  1049. c->status[i].predictor = bytestream_get_le32(&src);
  1050. for (; samples_in_chunk; samples_in_chunk--, src++) {
  1051. *samples++ = adpcm_ima_expand_nibble(&c->status[0], *src>>4, 3);
  1052. *samples++ = adpcm_ima_expand_nibble(&c->status[st], *src&0x0F, 3);
  1053. }
  1054. break;
  1055. case CODEC_ID_ADPCM_IMA_EA_SEAD:
  1056. for (; src < buf+buf_size; src++) {
  1057. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] >> 4, 6);
  1058. *samples++ = adpcm_ima_expand_nibble(&c->status[st],src[0]&0x0F, 6);
  1059. }
  1060. break;
  1061. case CODEC_ID_ADPCM_EA:
  1062. if (buf_size < 4 || AV_RL32(src) >= ((buf_size - 12) * 2)) {
  1063. src += buf_size;
  1064. break;
  1065. }
  1066. samples_in_chunk = AV_RL32(src);
  1067. src += 4;
  1068. current_left_sample = (int16_t)bytestream_get_le16(&src);
  1069. previous_left_sample = (int16_t)bytestream_get_le16(&src);
  1070. current_right_sample = (int16_t)bytestream_get_le16(&src);
  1071. previous_right_sample = (int16_t)bytestream_get_le16(&src);
  1072. for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
  1073. coeff1l = ea_adpcm_table[ *src >> 4 ];
  1074. coeff2l = ea_adpcm_table[(*src >> 4 ) + 4];
  1075. coeff1r = ea_adpcm_table[*src & 0x0F];
  1076. coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
  1077. src++;
  1078. shift_left = (*src >> 4 ) + 8;
  1079. shift_right = (*src & 0x0F) + 8;
  1080. src++;
  1081. for (count2 = 0; count2 < 28; count2++) {
  1082. next_left_sample = (int32_t)((*src & 0xF0) << 24) >> shift_left;
  1083. next_right_sample = (int32_t)((*src & 0x0F) << 28) >> shift_right;
  1084. src++;
  1085. next_left_sample = (next_left_sample +
  1086. (current_left_sample * coeff1l) +
  1087. (previous_left_sample * coeff2l) + 0x80) >> 8;
  1088. next_right_sample = (next_right_sample +
  1089. (current_right_sample * coeff1r) +
  1090. (previous_right_sample * coeff2r) + 0x80) >> 8;
  1091. previous_left_sample = current_left_sample;
  1092. current_left_sample = av_clip_int16(next_left_sample);
  1093. previous_right_sample = current_right_sample;
  1094. current_right_sample = av_clip_int16(next_right_sample);
  1095. *samples++ = (unsigned short)current_left_sample;
  1096. *samples++ = (unsigned short)current_right_sample;
  1097. }
  1098. }
  1099. if (src - buf == buf_size - 2)
  1100. src += 2; // Skip terminating 0x0000
  1101. break;
  1102. case CODEC_ID_ADPCM_EA_MAXIS_XA:
  1103. for(channel = 0; channel < avctx->channels; channel++) {
  1104. for (i=0; i<2; i++)
  1105. coeff[channel][i] = ea_adpcm_table[(*src >> 4) + 4*i];
  1106. shift[channel] = (*src & 0x0F) + 8;
  1107. src++;
  1108. }
  1109. for (count1 = 0; count1 < (buf_size - avctx->channels) / avctx->channels; count1++) {
  1110. for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
  1111. for(channel = 0; channel < avctx->channels; channel++) {
  1112. int32_t sample = (int32_t)(((*(src+channel) >> i) & 0x0F) << 0x1C) >> shift[channel];
  1113. sample = (sample +
  1114. c->status[channel].sample1 * coeff[channel][0] +
  1115. c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
  1116. c->status[channel].sample2 = c->status[channel].sample1;
  1117. c->status[channel].sample1 = av_clip_int16(sample);
  1118. *samples++ = c->status[channel].sample1;
  1119. }
  1120. }
  1121. src+=avctx->channels;
  1122. }
  1123. break;
  1124. case CODEC_ID_ADPCM_EA_R1:
  1125. case CODEC_ID_ADPCM_EA_R2:
  1126. case CODEC_ID_ADPCM_EA_R3: {
  1127. /* channel numbering
  1128. 2chan: 0=fl, 1=fr
  1129. 4chan: 0=fl, 1=rl, 2=fr, 3=rr
  1130. 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
  1131. const int big_endian = avctx->codec->id == CODEC_ID_ADPCM_EA_R3;
  1132. int32_t previous_sample, current_sample, next_sample;
  1133. int32_t coeff1, coeff2;
  1134. uint8_t shift;
  1135. unsigned int channel;
  1136. uint16_t *samplesC;
  1137. const uint8_t *srcC;
  1138. const uint8_t *src_end = buf + buf_size;
  1139. samples_in_chunk = (big_endian ? bytestream_get_be32(&src)
  1140. : bytestream_get_le32(&src)) / 28;
  1141. if (samples_in_chunk > UINT32_MAX/(28*avctx->channels) ||
  1142. 28*samples_in_chunk*avctx->channels > samples_end-samples) {
  1143. src += buf_size - 4;
  1144. break;
  1145. }
  1146. for (channel=0; channel<avctx->channels; channel++) {
  1147. int32_t offset = (big_endian ? bytestream_get_be32(&src)
  1148. : bytestream_get_le32(&src))
  1149. + (avctx->channels-channel-1) * 4;
  1150. if ((offset < 0) || (offset >= src_end - src - 4)) break;
  1151. srcC = src + offset;
  1152. samplesC = samples + channel;
  1153. if (avctx->codec->id == CODEC_ID_ADPCM_EA_R1) {
  1154. current_sample = (int16_t)bytestream_get_le16(&srcC);
  1155. previous_sample = (int16_t)bytestream_get_le16(&srcC);
  1156. } else {
  1157. current_sample = c->status[channel].predictor;
  1158. previous_sample = c->status[channel].prev_sample;
  1159. }
  1160. for (count1=0; count1<samples_in_chunk; count1++) {
  1161. if (*srcC == 0xEE) { /* only seen in R2 and R3 */
  1162. srcC++;
  1163. if (srcC > src_end - 30*2) break;
  1164. current_sample = (int16_t)bytestream_get_be16(&srcC);
  1165. previous_sample = (int16_t)bytestream_get_be16(&srcC);
  1166. for (count2=0; count2<28; count2++) {
  1167. *samplesC = (int16_t)bytestream_get_be16(&srcC);
  1168. samplesC += avctx->channels;
  1169. }
  1170. } else {
  1171. coeff1 = ea_adpcm_table[ *srcC>>4 ];
  1172. coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
  1173. shift = (*srcC++ & 0x0F) + 8;
  1174. if (srcC > src_end - 14) break;
  1175. for (count2=0; count2<28; count2++) {
  1176. if (count2 & 1)
  1177. next_sample = (int32_t)((*srcC++ & 0x0F) << 28) >> shift;
  1178. else
  1179. next_sample = (int32_t)((*srcC & 0xF0) << 24) >> shift;
  1180. next_sample += (current_sample * coeff1) +
  1181. (previous_sample * coeff2);
  1182. next_sample = av_clip_int16(next_sample >> 8);
  1183. previous_sample = current_sample;
  1184. current_sample = next_sample;
  1185. *samplesC = current_sample;
  1186. samplesC += avctx->channels;
  1187. }
  1188. }
  1189. }
  1190. if (avctx->codec->id != CODEC_ID_ADPCM_EA_R1) {
  1191. c->status[channel].predictor = current_sample;
  1192. c->status[channel].prev_sample = previous_sample;
  1193. }
  1194. }
  1195. src = src + buf_size - (4 + 4*avctx->channels);
  1196. samples += 28 * samples_in_chunk * avctx->channels;
  1197. break;
  1198. }
  1199. case CODEC_ID_ADPCM_EA_XAS:
  1200. if (samples_end-samples < 32*4*avctx->channels
  1201. || buf_size < (4+15)*4*avctx->channels) {
  1202. src += buf_size;
  1203. break;
  1204. }
  1205. for (channel=0; channel<avctx->channels; channel++) {
  1206. int coeff[2][4], shift[4];
  1207. short *s2, *s = &samples[channel];
  1208. for (n=0; n<4; n++, s+=32*avctx->channels) {
  1209. for (i=0; i<2; i++)
  1210. coeff[i][n] = ea_adpcm_table[(src[0]&0x0F)+4*i];
  1211. shift[n] = (src[2]&0x0F) + 8;
  1212. for (s2=s, i=0; i<2; i++, src+=2, s2+=avctx->channels)
  1213. s2[0] = (src[0]&0xF0) + (src[1]<<8);
  1214. }
  1215. for (m=2; m<32; m+=2) {
  1216. s = &samples[m*avctx->channels + channel];
  1217. for (n=0; n<4; n++, src++, s+=32*avctx->channels) {
  1218. for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
  1219. int level = (int32_t)((*src & (0xF0>>i)) << (24+i)) >> shift[n];
  1220. int pred = s2[-1*avctx->channels] * coeff[0][n]
  1221. + s2[-2*avctx->channels] * coeff[1][n];
  1222. s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
  1223. }
  1224. }
  1225. }
  1226. }
  1227. samples += 32*4*avctx->channels;
  1228. break;
  1229. case CODEC_ID_ADPCM_IMA_AMV:
  1230. case CODEC_ID_ADPCM_IMA_SMJPEG:
  1231. c->status[0].predictor = (int16_t)bytestream_get_le16(&src);
  1232. c->status[0].step_index = bytestream_get_le16(&src);
  1233. if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
  1234. src+=4;
  1235. while (src < buf + buf_size) {
  1236. char hi, lo;
  1237. lo = *src & 0x0F;
  1238. hi = *src >> 4;
  1239. if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
  1240. FFSWAP(char, hi, lo);
  1241. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1242. lo, 3);
  1243. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1244. hi, 3);
  1245. src++;
  1246. }
  1247. break;
  1248. case CODEC_ID_ADPCM_CT:
  1249. while (src < buf + buf_size) {
  1250. if (st) {
  1251. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1252. src[0] >> 4);
  1253. *samples++ = adpcm_ct_expand_nibble(&c->status[1],
  1254. src[0] & 0x0F);
  1255. } else {
  1256. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1257. src[0] >> 4);
  1258. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1259. src[0] & 0x0F);
  1260. }
  1261. src++;
  1262. }
  1263. break;
  1264. case CODEC_ID_ADPCM_SBPRO_4:
  1265. case CODEC_ID_ADPCM_SBPRO_3:
  1266. case CODEC_ID_ADPCM_SBPRO_2:
  1267. if (!c->status[0].step_index) {
  1268. /* the first byte is a raw sample */
  1269. *samples++ = 128 * (*src++ - 0x80);
  1270. if (st)
  1271. *samples++ = 128 * (*src++ - 0x80);
  1272. c->status[0].step_index = 1;
  1273. }
  1274. if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
  1275. while (src < buf + buf_size) {
  1276. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1277. src[0] >> 4, 4, 0);
  1278. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1279. src[0] & 0x0F, 4, 0);
  1280. src++;
  1281. }
  1282. } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
  1283. while (src < buf + buf_size && samples + 2 < samples_end) {
  1284. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1285. src[0] >> 5 , 3, 0);
  1286. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1287. (src[0] >> 2) & 0x07, 3, 0);
  1288. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1289. src[0] & 0x03, 2, 0);
  1290. src++;
  1291. }
  1292. } else {
  1293. while (src < buf + buf_size && samples + 3 < samples_end) {
  1294. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1295. src[0] >> 6 , 2, 2);
  1296. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1297. (src[0] >> 4) & 0x03, 2, 2);
  1298. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1299. (src[0] >> 2) & 0x03, 2, 2);
  1300. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1301. src[0] & 0x03, 2, 2);
  1302. src++;
  1303. }
  1304. }
  1305. break;
  1306. case CODEC_ID_ADPCM_SWF:
  1307. {
  1308. GetBitContext gb;
  1309. const int *table;
  1310. int k0, signmask, nb_bits, count;
  1311. int size = buf_size*8;
  1312. init_get_bits(&gb, buf, size);
  1313. //read bits & initial values
  1314. nb_bits = get_bits(&gb, 2)+2;
  1315. //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
  1316. table = swf_index_tables[nb_bits-2];
  1317. k0 = 1 << (nb_bits-2);
  1318. signmask = 1 << (nb_bits-1);
  1319. while (get_bits_count(&gb) <= size - 22*avctx->channels) {
  1320. for (i = 0; i < avctx->channels; i++) {
  1321. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  1322. c->status[i].step_index = get_bits(&gb, 6);
  1323. }
  1324. for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
  1325. int i;
  1326. for (i = 0; i < avctx->channels; i++) {
  1327. // similar to IMA adpcm
  1328. int delta = get_bits(&gb, nb_bits);
  1329. int step = step_table[c->status[i].step_index];
  1330. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  1331. int k = k0;
  1332. do {
  1333. if (delta & k)
  1334. vpdiff += step;
  1335. step >>= 1;
  1336. k >>= 1;
  1337. } while(k);
  1338. vpdiff += step;
  1339. if (delta & signmask)
  1340. c->status[i].predictor -= vpdiff;
  1341. else
  1342. c->status[i].predictor += vpdiff;
  1343. c->status[i].step_index += table[delta & (~signmask)];
  1344. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  1345. c->status[i].predictor = av_clip_int16(c->status[i].predictor);
  1346. *samples++ = c->status[i].predictor;
  1347. if (samples >= samples_end) {
  1348. av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
  1349. return -1;
  1350. }
  1351. }
  1352. }
  1353. }
  1354. src += buf_size;
  1355. break;
  1356. }
  1357. case CODEC_ID_ADPCM_YAMAHA:
  1358. while (src < buf + buf_size) {
  1359. if (st) {
  1360. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1361. src[0] & 0x0F);
  1362. *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
  1363. src[0] >> 4 );
  1364. } else {
  1365. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1366. src[0] & 0x0F);
  1367. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1368. src[0] >> 4 );
  1369. }
  1370. src++;
  1371. }
  1372. break;
  1373. case CODEC_ID_ADPCM_THP:
  1374. {
  1375. int table[2][16];
  1376. unsigned int samplecnt;
  1377. int prev[2][2];
  1378. int ch;
  1379. if (buf_size < 80) {
  1380. av_log(avctx, AV_LOG_ERROR, "frame too small\n");
  1381. return -1;
  1382. }
  1383. src+=4;
  1384. samplecnt = bytestream_get_be32(&src);
  1385. for (i = 0; i < 32; i++)
  1386. table[0][i] = (int16_t)bytestream_get_be16(&src);
  1387. /* Initialize the previous sample. */
  1388. for (i = 0; i < 4; i++)
  1389. prev[0][i] = (int16_t)bytestream_get_be16(&src);
  1390. if (samplecnt >= (samples_end - samples) / (st + 1)) {
  1391. av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
  1392. return -1;
  1393. }
  1394. for (ch = 0; ch <= st; ch++) {
  1395. samples = (unsigned short *) data + ch;
  1396. /* Read in every sample for this channel. */
  1397. for (i = 0; i < samplecnt / 14; i++) {
  1398. int index = (*src >> 4) & 7;
  1399. unsigned int exp = 28 - (*src++ & 15);
  1400. int factor1 = table[ch][index * 2];
  1401. int factor2 = table[ch][index * 2 + 1];
  1402. /* Decode 14 samples. */
  1403. for (n = 0; n < 14; n++) {
  1404. int32_t sampledat;
  1405. if(n&1) sampledat= *src++ <<28;
  1406. else sampledat= (*src&0xF0)<<24;
  1407. sampledat = ((prev[ch][0]*factor1
  1408. + prev[ch][1]*factor2) >> 11) + (sampledat>>exp);
  1409. *samples = av_clip_int16(sampledat);
  1410. prev[ch][1] = prev[ch][0];
  1411. prev[ch][0] = *samples++;
  1412. /* In case of stereo, skip one sample, this sample
  1413. is for the other channel. */
  1414. samples += st;
  1415. }
  1416. }
  1417. }
  1418. /* In the previous loop, in case stereo is used, samples is
  1419. increased exactly one time too often. */
  1420. samples -= st;
  1421. break;
  1422. }
  1423. default:
  1424. return -1;
  1425. }
  1426. *data_size = (uint8_t *)samples - (uint8_t *)data;
  1427. return src - buf;
  1428. }
  1429. #if CONFIG_ENCODERS
  1430. #define ADPCM_ENCODER(id,name,long_name_) \
  1431. AVCodec name ## _encoder = { \
  1432. #name, \
  1433. CODEC_TYPE_AUDIO, \
  1434. id, \
  1435. sizeof(ADPCMContext), \
  1436. adpcm_encode_init, \
  1437. adpcm_encode_frame, \
  1438. adpcm_encode_close, \
  1439. NULL, \
  1440. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE}, \
  1441. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  1442. };
  1443. #else
  1444. #define ADPCM_ENCODER(id,name,long_name_)
  1445. #endif
  1446. #if CONFIG_DECODERS
  1447. #define ADPCM_DECODER(id,name,long_name_) \
  1448. AVCodec name ## _decoder = { \
  1449. #name, \
  1450. CODEC_TYPE_AUDIO, \
  1451. id, \
  1452. sizeof(ADPCMContext), \
  1453. adpcm_decode_init, \
  1454. NULL, \
  1455. NULL, \
  1456. adpcm_decode_frame, \
  1457. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  1458. };
  1459. #else
  1460. #define ADPCM_DECODER(id,name,long_name_)
  1461. #endif
  1462. #define ADPCM_CODEC(id,name,long_name_) \
  1463. ADPCM_ENCODER(id,name,long_name_) ADPCM_DECODER(id,name,long_name_)
  1464. /* Note: Do not forget to add new entries to the Makefile as well. */
  1465. ADPCM_DECODER(CODEC_ID_ADPCM_4XM, adpcm_4xm, "ADPCM 4X Movie");
  1466. ADPCM_DECODER(CODEC_ID_ADPCM_CT, adpcm_ct, "ADPCM Creative Technology");
  1467. ADPCM_DECODER(CODEC_ID_ADPCM_EA, adpcm_ea, "ADPCM Electronic Arts");
  1468. ADPCM_DECODER(CODEC_ID_ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
  1469. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1, "ADPCM Electronic Arts R1");
  1470. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2, "ADPCM Electronic Arts R2");
  1471. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3, "ADPCM Electronic Arts R3");
  1472. ADPCM_DECODER(CODEC_ID_ADPCM_EA_XAS, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
  1473. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, "ADPCM IMA AMV");
  1474. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
  1475. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
  1476. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
  1477. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
  1478. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_ISS, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
  1479. ADPCM_CODEC (CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
  1480. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
  1481. ADPCM_CODEC (CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
  1482. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws, "ADPCM IMA Westwood");
  1483. ADPCM_CODEC (CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
  1484. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
  1485. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
  1486. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
  1487. ADPCM_CODEC (CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
  1488. ADPCM_DECODER(CODEC_ID_ADPCM_THP, adpcm_thp, "ADPCM Nintendo Gamecube THP");
  1489. ADPCM_DECODER(CODEC_ID_ADPCM_XA, adpcm_xa, "ADPCM CDROM XA");
  1490. ADPCM_CODEC (CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");