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.

1685 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 "bitstream.h"
  23. #include "bytestream.h"
  24. /**
  25. * @file libavcodec/adpcm.c
  26. * ADPCM codecs.
  27. * First version by Francois Revol (revol@free.fr)
  28. * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
  29. * by Mike Melanson (melanson@pcisys.net)
  30. * CD-ROM XA ADPCM codec by BERO
  31. * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
  32. * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
  33. * EA IMA EACS decoder by Peter Ross (pross@xvid.org)
  34. * EA IMA SEAD decoder by Peter Ross (pross@xvid.org)
  35. * EA ADPCM XAS decoder by Peter Ross (pross@xvid.org)
  36. * MAXIS EA ADPCM decoder by Robert Marston (rmarston@gmail.com)
  37. * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
  38. *
  39. * Features and limitations:
  40. *
  41. * Reference documents:
  42. * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
  43. * http://www.geocities.com/SiliconValley/8682/aud3.txt
  44. * http://openquicktime.sourceforge.net/plugins.htm
  45. * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
  46. * http://www.cs.ucla.edu/~leec/mediabench/applications.html
  47. * SoX source code http://home.sprynet.com/~cbagwell/sox.html
  48. *
  49. * CD-ROM XA:
  50. * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html
  51. * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html
  52. * readstr http://www.geocities.co.jp/Playtown/2004/
  53. */
  54. #define BLKSIZE 1024
  55. /* step_table[] and index_table[] are from the ADPCM reference source */
  56. /* This is the index table: */
  57. static const int index_table[16] = {
  58. -1, -1, -1, -1, 2, 4, 6, 8,
  59. -1, -1, -1, -1, 2, 4, 6, 8,
  60. };
  61. /**
  62. * This is the step table. Note that many programs use slight deviations from
  63. * this table, but such deviations are negligible:
  64. */
  65. static const int step_table[89] = {
  66. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  67. 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  68. 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  69. 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  70. 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  71. 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  72. 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  73. 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  74. 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  75. };
  76. /* These are for MS-ADPCM */
  77. /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
  78. static const int AdaptationTable[] = {
  79. 230, 230, 230, 230, 307, 409, 512, 614,
  80. 768, 614, 512, 409, 307, 230, 230, 230
  81. };
  82. static const uint8_t AdaptCoeff1[] = {
  83. 64, 128, 0, 48, 60, 115, 98
  84. };
  85. static const int8_t AdaptCoeff2[] = {
  86. 0, -64, 0, 16, 0, -52, -58
  87. };
  88. /* These are for CD-ROM XA ADPCM */
  89. static const int xa_adpcm_table[5][2] = {
  90. { 0, 0 },
  91. { 60, 0 },
  92. { 115, -52 },
  93. { 98, -55 },
  94. { 122, -60 }
  95. };
  96. static const int ea_adpcm_table[] = {
  97. 0, 240, 460, 392, 0, 0, -208, -220, 0, 1,
  98. 3, 4, 7, 8, 10, 11, 0, -1, -3, -4
  99. };
  100. // padded to zero where table size is less then 16
  101. static const int swf_index_tables[4][16] = {
  102. /*2*/ { -1, 2 },
  103. /*3*/ { -1, -1, 2, 4 },
  104. /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
  105. /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
  106. };
  107. static const int yamaha_indexscale[] = {
  108. 230, 230, 230, 230, 307, 409, 512, 614,
  109. 230, 230, 230, 230, 307, 409, 512, 614
  110. };
  111. static const int yamaha_difflookup[] = {
  112. 1, 3, 5, 7, 9, 11, 13, 15,
  113. -1, -3, -5, -7, -9, -11, -13, -15
  114. };
  115. /* end of tables */
  116. typedef struct ADPCMChannelStatus {
  117. int predictor;
  118. short int step_index;
  119. int step;
  120. /* for encoding */
  121. int prev_sample;
  122. /* MS version */
  123. short sample1;
  124. short sample2;
  125. int coeff1;
  126. int coeff2;
  127. int idelta;
  128. } ADPCMChannelStatus;
  129. typedef struct ADPCMContext {
  130. ADPCMChannelStatus status[6];
  131. } ADPCMContext;
  132. /* XXX: implement encoding */
  133. #if CONFIG_ENCODERS
  134. static av_cold int adpcm_encode_init(AVCodecContext *avctx)
  135. {
  136. if (avctx->channels > 2)
  137. return -1; /* only stereo or mono =) */
  138. if(avctx->trellis && (unsigned)avctx->trellis > 16U){
  139. av_log(avctx, AV_LOG_ERROR, "invalid trellis size\n");
  140. return -1;
  141. }
  142. switch(avctx->codec->id) {
  143. case CODEC_ID_ADPCM_IMA_WAV:
  144. avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
  145. /* and we have 4 bytes per channel overhead */
  146. avctx->block_align = BLKSIZE;
  147. /* seems frame_size isn't taken into account... have to buffer the samples :-( */
  148. break;
  149. case CODEC_ID_ADPCM_IMA_QT:
  150. avctx->frame_size = 64;
  151. avctx->block_align = 34 * avctx->channels;
  152. break;
  153. case CODEC_ID_ADPCM_MS:
  154. avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
  155. /* and we have 7 bytes per channel overhead */
  156. avctx->block_align = BLKSIZE;
  157. break;
  158. case CODEC_ID_ADPCM_YAMAHA:
  159. avctx->frame_size = BLKSIZE * avctx->channels;
  160. avctx->block_align = BLKSIZE;
  161. break;
  162. case CODEC_ID_ADPCM_SWF:
  163. if (avctx->sample_rate != 11025 &&
  164. avctx->sample_rate != 22050 &&
  165. avctx->sample_rate != 44100) {
  166. av_log(avctx, AV_LOG_ERROR, "Sample rate must be 11025, 22050 or 44100\n");
  167. return -1;
  168. }
  169. avctx->frame_size = 512 * (avctx->sample_rate / 11025);
  170. break;
  171. default:
  172. return -1;
  173. break;
  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>0; n--) {
  586. for(i = 0; i < avctx->channels; i++) {
  587. int nibble;
  588. nibble = adpcm_yamaha_compress_sample(&c->status[i], samples[i]);
  589. nibble |= adpcm_yamaha_compress_sample(&c->status[i], samples[i+avctx->channels]) << 4;
  590. *dst++ = nibble;
  591. }
  592. samples += 2 * avctx->channels;
  593. }
  594. break;
  595. default:
  596. return -1;
  597. }
  598. return dst - frame;
  599. }
  600. #endif //CONFIG_ENCODERS
  601. static av_cold int adpcm_decode_init(AVCodecContext * avctx)
  602. {
  603. ADPCMContext *c = avctx->priv_data;
  604. unsigned int max_channels = 2;
  605. switch(avctx->codec->id) {
  606. case CODEC_ID_ADPCM_EA_R1:
  607. case CODEC_ID_ADPCM_EA_R2:
  608. case CODEC_ID_ADPCM_EA_R3:
  609. max_channels = 6;
  610. break;
  611. }
  612. if(avctx->channels > max_channels){
  613. return -1;
  614. }
  615. switch(avctx->codec->id) {
  616. case CODEC_ID_ADPCM_CT:
  617. c->status[0].step = c->status[1].step = 511;
  618. break;
  619. case CODEC_ID_ADPCM_IMA_WS:
  620. if (avctx->extradata && avctx->extradata_size == 2 * 4) {
  621. c->status[0].predictor = AV_RL32(avctx->extradata);
  622. c->status[1].predictor = AV_RL32(avctx->extradata + 4);
  623. }
  624. break;
  625. default:
  626. break;
  627. }
  628. avctx->sample_fmt = SAMPLE_FMT_S16;
  629. return 0;
  630. }
  631. static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
  632. {
  633. int step_index;
  634. int predictor;
  635. int sign, delta, diff, step;
  636. step = step_table[c->step_index];
  637. step_index = c->step_index + index_table[(unsigned)nibble];
  638. if (step_index < 0) step_index = 0;
  639. else if (step_index > 88) step_index = 88;
  640. sign = nibble & 8;
  641. delta = nibble & 7;
  642. /* perform direct multiplication instead of series of jumps proposed by
  643. * the reference ADPCM implementation since modern CPUs can do the mults
  644. * quickly enough */
  645. diff = ((2 * delta + 1) * step) >> shift;
  646. predictor = c->predictor;
  647. if (sign) predictor -= diff;
  648. else predictor += diff;
  649. c->predictor = av_clip_int16(predictor);
  650. c->step_index = step_index;
  651. return (short)c->predictor;
  652. }
  653. static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
  654. {
  655. int predictor;
  656. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;
  657. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  658. c->sample2 = c->sample1;
  659. c->sample1 = av_clip_int16(predictor);
  660. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
  661. if (c->idelta < 16) c->idelta = 16;
  662. return c->sample1;
  663. }
  664. static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
  665. {
  666. int sign, delta, diff;
  667. int new_step;
  668. sign = nibble & 8;
  669. delta = nibble & 7;
  670. /* perform direct multiplication instead of series of jumps proposed by
  671. * the reference ADPCM implementation since modern CPUs can do the mults
  672. * quickly enough */
  673. diff = ((2 * delta + 1) * c->step) >> 3;
  674. /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
  675. c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
  676. c->predictor = av_clip_int16(c->predictor);
  677. /* calculate new step and clamp it to range 511..32767 */
  678. new_step = (AdaptationTable[nibble & 7] * c->step) >> 8;
  679. c->step = av_clip(new_step, 511, 32767);
  680. return (short)c->predictor;
  681. }
  682. static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
  683. {
  684. int sign, delta, diff;
  685. sign = nibble & (1<<(size-1));
  686. delta = nibble & ((1<<(size-1))-1);
  687. diff = delta << (7 + c->step + shift);
  688. /* clamp result */
  689. c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
  690. /* calculate new step */
  691. if (delta >= (2*size - 3) && c->step < 3)
  692. c->step++;
  693. else if (delta == 0 && c->step > 0)
  694. c->step--;
  695. return (short) c->predictor;
  696. }
  697. static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
  698. {
  699. if(!c->step) {
  700. c->predictor = 0;
  701. c->step = 127;
  702. }
  703. c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
  704. c->predictor = av_clip_int16(c->predictor);
  705. c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
  706. c->step = av_clip(c->step, 127, 24567);
  707. return c->predictor;
  708. }
  709. static void xa_decode(short *out, const unsigned char *in,
  710. ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
  711. {
  712. int i, j;
  713. int shift,filter,f0,f1;
  714. int s_1,s_2;
  715. int d,s,t;
  716. for(i=0;i<4;i++) {
  717. shift = 12 - (in[4+i*2] & 15);
  718. filter = in[4+i*2] >> 4;
  719. f0 = xa_adpcm_table[filter][0];
  720. f1 = xa_adpcm_table[filter][1];
  721. s_1 = left->sample1;
  722. s_2 = left->sample2;
  723. for(j=0;j<28;j++) {
  724. d = in[16+i+j*4];
  725. t = (signed char)(d<<4)>>4;
  726. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  727. s_2 = s_1;
  728. s_1 = av_clip_int16(s);
  729. *out = s_1;
  730. out += inc;
  731. }
  732. if (inc==2) { /* stereo */
  733. left->sample1 = s_1;
  734. left->sample2 = s_2;
  735. s_1 = right->sample1;
  736. s_2 = right->sample2;
  737. out = out + 1 - 28*2;
  738. }
  739. shift = 12 - (in[5+i*2] & 15);
  740. filter = in[5+i*2] >> 4;
  741. f0 = xa_adpcm_table[filter][0];
  742. f1 = xa_adpcm_table[filter][1];
  743. for(j=0;j<28;j++) {
  744. d = in[16+i+j*4];
  745. t = (signed char)d >> 4;
  746. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  747. s_2 = s_1;
  748. s_1 = av_clip_int16(s);
  749. *out = s_1;
  750. out += inc;
  751. }
  752. if (inc==2) { /* stereo */
  753. right->sample1 = s_1;
  754. right->sample2 = s_2;
  755. out -= 1;
  756. } else {
  757. left->sample1 = s_1;
  758. left->sample2 = s_2;
  759. }
  760. }
  761. }
  762. /* DK3 ADPCM support macro */
  763. #define DK3_GET_NEXT_NIBBLE() \
  764. if (decode_top_nibble_next) \
  765. { \
  766. nibble = last_byte >> 4; \
  767. decode_top_nibble_next = 0; \
  768. } \
  769. else \
  770. { \
  771. last_byte = *src++; \
  772. if (src >= buf + buf_size) break; \
  773. nibble = last_byte & 0x0F; \
  774. decode_top_nibble_next = 1; \
  775. }
  776. static int adpcm_decode_frame(AVCodecContext *avctx,
  777. void *data, int *data_size,
  778. const uint8_t *buf, int buf_size)
  779. {
  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. samples_in_chunk = AV_RL32(src);
  1063. if (samples_in_chunk >= ((buf_size - 12) * 2)) {
  1064. src += buf_size;
  1065. break;
  1066. }
  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. break;
  1100. case CODEC_ID_ADPCM_EA_MAXIS_XA:
  1101. for(channel = 0; channel < avctx->channels; channel++) {
  1102. for (i=0; i<2; i++)
  1103. coeff[channel][i] = ea_adpcm_table[(*src >> 4) + 4*i];
  1104. shift[channel] = (*src & 0x0F) + 8;
  1105. src++;
  1106. }
  1107. for (count1 = 0; count1 < (buf_size - avctx->channels) / avctx->channels; count1++) {
  1108. for(i = 4; i >= 0; i-=4) { /* Pairwise samples LL RR (st) or LL LL (mono) */
  1109. for(channel = 0; channel < avctx->channels; channel++) {
  1110. int32_t sample = (int32_t)(((*(src+channel) >> i) & 0x0F) << 0x1C) >> shift[channel];
  1111. sample = (sample +
  1112. c->status[channel].sample1 * coeff[channel][0] +
  1113. c->status[channel].sample2 * coeff[channel][1] + 0x80) >> 8;
  1114. c->status[channel].sample2 = c->status[channel].sample1;
  1115. c->status[channel].sample1 = av_clip_int16(sample);
  1116. *samples++ = c->status[channel].sample1;
  1117. }
  1118. }
  1119. src+=avctx->channels;
  1120. }
  1121. break;
  1122. case CODEC_ID_ADPCM_EA_R1:
  1123. case CODEC_ID_ADPCM_EA_R2:
  1124. case CODEC_ID_ADPCM_EA_R3: {
  1125. /* channel numbering
  1126. 2chan: 0=fl, 1=fr
  1127. 4chan: 0=fl, 1=rl, 2=fr, 3=rr
  1128. 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
  1129. const int big_endian = avctx->codec->id == CODEC_ID_ADPCM_EA_R3;
  1130. int32_t previous_sample, current_sample, next_sample;
  1131. int32_t coeff1, coeff2;
  1132. uint8_t shift;
  1133. unsigned int channel;
  1134. uint16_t *samplesC;
  1135. const uint8_t *srcC;
  1136. const uint8_t *src_end = buf + buf_size;
  1137. samples_in_chunk = (big_endian ? bytestream_get_be32(&src)
  1138. : bytestream_get_le32(&src)) / 28;
  1139. if (samples_in_chunk > UINT32_MAX/(28*avctx->channels) ||
  1140. 28*samples_in_chunk*avctx->channels > samples_end-samples) {
  1141. src += buf_size - 4;
  1142. break;
  1143. }
  1144. for (channel=0; channel<avctx->channels; channel++) {
  1145. int32_t offset = (big_endian ? bytestream_get_be32(&src)
  1146. : bytestream_get_le32(&src))
  1147. + (avctx->channels-channel-1) * 4;
  1148. if ((offset < 0) || (offset >= src_end - src - 4)) break;
  1149. srcC = src + offset;
  1150. samplesC = samples + channel;
  1151. if (avctx->codec->id == CODEC_ID_ADPCM_EA_R1) {
  1152. current_sample = (int16_t)bytestream_get_le16(&srcC);
  1153. previous_sample = (int16_t)bytestream_get_le16(&srcC);
  1154. } else {
  1155. current_sample = c->status[channel].predictor;
  1156. previous_sample = c->status[channel].prev_sample;
  1157. }
  1158. for (count1=0; count1<samples_in_chunk; count1++) {
  1159. if (*srcC == 0xEE) { /* only seen in R2 and R3 */
  1160. srcC++;
  1161. if (srcC > src_end - 30*2) break;
  1162. current_sample = (int16_t)bytestream_get_be16(&srcC);
  1163. previous_sample = (int16_t)bytestream_get_be16(&srcC);
  1164. for (count2=0; count2<28; count2++) {
  1165. *samplesC = (int16_t)bytestream_get_be16(&srcC);
  1166. samplesC += avctx->channels;
  1167. }
  1168. } else {
  1169. coeff1 = ea_adpcm_table[ *srcC>>4 ];
  1170. coeff2 = ea_adpcm_table[(*srcC>>4) + 4];
  1171. shift = (*srcC++ & 0x0F) + 8;
  1172. if (srcC > src_end - 14) break;
  1173. for (count2=0; count2<28; count2++) {
  1174. if (count2 & 1)
  1175. next_sample = (int32_t)((*srcC++ & 0x0F) << 28) >> shift;
  1176. else
  1177. next_sample = (int32_t)((*srcC & 0xF0) << 24) >> shift;
  1178. next_sample += (current_sample * coeff1) +
  1179. (previous_sample * coeff2);
  1180. next_sample = av_clip_int16(next_sample >> 8);
  1181. previous_sample = current_sample;
  1182. current_sample = next_sample;
  1183. *samplesC = current_sample;
  1184. samplesC += avctx->channels;
  1185. }
  1186. }
  1187. }
  1188. if (avctx->codec->id != CODEC_ID_ADPCM_EA_R1) {
  1189. c->status[channel].predictor = current_sample;
  1190. c->status[channel].prev_sample = previous_sample;
  1191. }
  1192. }
  1193. src = src + buf_size - (4 + 4*avctx->channels);
  1194. samples += 28 * samples_in_chunk * avctx->channels;
  1195. break;
  1196. }
  1197. case CODEC_ID_ADPCM_EA_XAS:
  1198. if (samples_end-samples < 32*4*avctx->channels
  1199. || buf_size < (4+15)*4*avctx->channels) {
  1200. src += buf_size;
  1201. break;
  1202. }
  1203. for (channel=0; channel<avctx->channels; channel++) {
  1204. int coeff[2][4], shift[4];
  1205. short *s2, *s = &samples[channel];
  1206. for (n=0; n<4; n++, s+=32*avctx->channels) {
  1207. for (i=0; i<2; i++)
  1208. coeff[i][n] = ea_adpcm_table[(src[0]&0x0F)+4*i];
  1209. shift[n] = (src[2]&0x0F) + 8;
  1210. for (s2=s, i=0; i<2; i++, src+=2, s2+=avctx->channels)
  1211. s2[0] = (src[0]&0xF0) + (src[1]<<8);
  1212. }
  1213. for (m=2; m<32; m+=2) {
  1214. s = &samples[m*avctx->channels + channel];
  1215. for (n=0; n<4; n++, src++, s+=32*avctx->channels) {
  1216. for (s2=s, i=0; i<8; i+=4, s2+=avctx->channels) {
  1217. int level = (int32_t)((*src & (0xF0>>i)) << (24+i)) >> shift[n];
  1218. int pred = s2[-1*avctx->channels] * coeff[0][n]
  1219. + s2[-2*avctx->channels] * coeff[1][n];
  1220. s2[0] = av_clip_int16((level + pred + 0x80) >> 8);
  1221. }
  1222. }
  1223. }
  1224. }
  1225. samples += 32*4*avctx->channels;
  1226. break;
  1227. case CODEC_ID_ADPCM_IMA_AMV:
  1228. case CODEC_ID_ADPCM_IMA_SMJPEG:
  1229. c->status[0].predictor = (int16_t)bytestream_get_le16(&src);
  1230. c->status[0].step_index = bytestream_get_le16(&src);
  1231. if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
  1232. src+=4;
  1233. while (src < buf + buf_size) {
  1234. char hi, lo;
  1235. lo = *src & 0x0F;
  1236. hi = *src >> 4;
  1237. if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
  1238. FFSWAP(char, hi, lo);
  1239. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1240. lo, 3);
  1241. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1242. hi, 3);
  1243. src++;
  1244. }
  1245. break;
  1246. case CODEC_ID_ADPCM_CT:
  1247. while (src < buf + buf_size) {
  1248. if (st) {
  1249. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1250. src[0] >> 4);
  1251. *samples++ = adpcm_ct_expand_nibble(&c->status[1],
  1252. src[0] & 0x0F);
  1253. } else {
  1254. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1255. src[0] >> 4);
  1256. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1257. src[0] & 0x0F);
  1258. }
  1259. src++;
  1260. }
  1261. break;
  1262. case CODEC_ID_ADPCM_SBPRO_4:
  1263. case CODEC_ID_ADPCM_SBPRO_3:
  1264. case CODEC_ID_ADPCM_SBPRO_2:
  1265. if (!c->status[0].step_index) {
  1266. /* the first byte is a raw sample */
  1267. *samples++ = 128 * (*src++ - 0x80);
  1268. if (st)
  1269. *samples++ = 128 * (*src++ - 0x80);
  1270. c->status[0].step_index = 1;
  1271. }
  1272. if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
  1273. while (src < buf + buf_size) {
  1274. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1275. src[0] >> 4, 4, 0);
  1276. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1277. src[0] & 0x0F, 4, 0);
  1278. src++;
  1279. }
  1280. } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
  1281. while (src < buf + buf_size && samples + 2 < samples_end) {
  1282. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1283. src[0] >> 5 , 3, 0);
  1284. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1285. (src[0] >> 2) & 0x07, 3, 0);
  1286. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1287. src[0] & 0x03, 2, 0);
  1288. src++;
  1289. }
  1290. } else {
  1291. while (src < buf + buf_size && samples + 3 < samples_end) {
  1292. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1293. src[0] >> 6 , 2, 2);
  1294. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1295. (src[0] >> 4) & 0x03, 2, 2);
  1296. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1297. (src[0] >> 2) & 0x03, 2, 2);
  1298. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1299. src[0] & 0x03, 2, 2);
  1300. src++;
  1301. }
  1302. }
  1303. break;
  1304. case CODEC_ID_ADPCM_SWF:
  1305. {
  1306. GetBitContext gb;
  1307. const int *table;
  1308. int k0, signmask, nb_bits, count;
  1309. int size = buf_size*8;
  1310. init_get_bits(&gb, buf, size);
  1311. //read bits & initial values
  1312. nb_bits = get_bits(&gb, 2)+2;
  1313. //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
  1314. table = swf_index_tables[nb_bits-2];
  1315. k0 = 1 << (nb_bits-2);
  1316. signmask = 1 << (nb_bits-1);
  1317. while (get_bits_count(&gb) <= size - 22*avctx->channels) {
  1318. for (i = 0; i < avctx->channels; i++) {
  1319. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  1320. c->status[i].step_index = get_bits(&gb, 6);
  1321. }
  1322. for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
  1323. int i;
  1324. for (i = 0; i < avctx->channels; i++) {
  1325. // similar to IMA adpcm
  1326. int delta = get_bits(&gb, nb_bits);
  1327. int step = step_table[c->status[i].step_index];
  1328. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  1329. int k = k0;
  1330. do {
  1331. if (delta & k)
  1332. vpdiff += step;
  1333. step >>= 1;
  1334. k >>= 1;
  1335. } while(k);
  1336. vpdiff += step;
  1337. if (delta & signmask)
  1338. c->status[i].predictor -= vpdiff;
  1339. else
  1340. c->status[i].predictor += vpdiff;
  1341. c->status[i].step_index += table[delta & (~signmask)];
  1342. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  1343. c->status[i].predictor = av_clip_int16(c->status[i].predictor);
  1344. *samples++ = c->status[i].predictor;
  1345. if (samples >= samples_end) {
  1346. av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
  1347. return -1;
  1348. }
  1349. }
  1350. }
  1351. }
  1352. src += buf_size;
  1353. break;
  1354. }
  1355. case CODEC_ID_ADPCM_YAMAHA:
  1356. while (src < buf + buf_size) {
  1357. if (st) {
  1358. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1359. src[0] & 0x0F);
  1360. *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
  1361. src[0] >> 4 );
  1362. } else {
  1363. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1364. src[0] & 0x0F);
  1365. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1366. src[0] >> 4 );
  1367. }
  1368. src++;
  1369. }
  1370. break;
  1371. case CODEC_ID_ADPCM_THP:
  1372. {
  1373. int table[2][16];
  1374. unsigned int samplecnt;
  1375. int prev[2][2];
  1376. int ch;
  1377. if (buf_size < 80) {
  1378. av_log(avctx, AV_LOG_ERROR, "frame too small\n");
  1379. return -1;
  1380. }
  1381. src+=4;
  1382. samplecnt = bytestream_get_be32(&src);
  1383. for (i = 0; i < 32; i++)
  1384. table[0][i] = (int16_t)bytestream_get_be16(&src);
  1385. /* Initialize the previous sample. */
  1386. for (i = 0; i < 4; i++)
  1387. prev[0][i] = (int16_t)bytestream_get_be16(&src);
  1388. if (samplecnt >= (samples_end - samples) / (st + 1)) {
  1389. av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
  1390. return -1;
  1391. }
  1392. for (ch = 0; ch <= st; ch++) {
  1393. samples = (unsigned short *) data + ch;
  1394. /* Read in every sample for this channel. */
  1395. for (i = 0; i < samplecnt / 14; i++) {
  1396. int index = (*src >> 4) & 7;
  1397. unsigned int exp = 28 - (*src++ & 15);
  1398. int factor1 = table[ch][index * 2];
  1399. int factor2 = table[ch][index * 2 + 1];
  1400. /* Decode 14 samples. */
  1401. for (n = 0; n < 14; n++) {
  1402. int32_t sampledat;
  1403. if(n&1) sampledat= *src++ <<28;
  1404. else sampledat= (*src&0xF0)<<24;
  1405. sampledat = ((prev[ch][0]*factor1
  1406. + prev[ch][1]*factor2) >> 11) + (sampledat>>exp);
  1407. *samples = av_clip_int16(sampledat);
  1408. prev[ch][1] = prev[ch][0];
  1409. prev[ch][0] = *samples++;
  1410. /* In case of stereo, skip one sample, this sample
  1411. is for the other channel. */
  1412. samples += st;
  1413. }
  1414. }
  1415. }
  1416. /* In the previous loop, in case stereo is used, samples is
  1417. increased exactly one time too often. */
  1418. samples -= st;
  1419. break;
  1420. }
  1421. default:
  1422. return -1;
  1423. }
  1424. *data_size = (uint8_t *)samples - (uint8_t *)data;
  1425. return src - buf;
  1426. }
  1427. #if CONFIG_ENCODERS
  1428. #define ADPCM_ENCODER(id,name,long_name_) \
  1429. AVCodec name ## _encoder = { \
  1430. #name, \
  1431. CODEC_TYPE_AUDIO, \
  1432. id, \
  1433. sizeof(ADPCMContext), \
  1434. adpcm_encode_init, \
  1435. adpcm_encode_frame, \
  1436. adpcm_encode_close, \
  1437. NULL, \
  1438. .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE}, \
  1439. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  1440. };
  1441. #else
  1442. #define ADPCM_ENCODER(id,name,long_name_)
  1443. #endif
  1444. #if CONFIG_DECODERS
  1445. #define ADPCM_DECODER(id,name,long_name_) \
  1446. AVCodec name ## _decoder = { \
  1447. #name, \
  1448. CODEC_TYPE_AUDIO, \
  1449. id, \
  1450. sizeof(ADPCMContext), \
  1451. adpcm_decode_init, \
  1452. NULL, \
  1453. NULL, \
  1454. adpcm_decode_frame, \
  1455. .long_name = NULL_IF_CONFIG_SMALL(long_name_), \
  1456. };
  1457. #else
  1458. #define ADPCM_DECODER(id,name,long_name_)
  1459. #endif
  1460. #define ADPCM_CODEC(id,name,long_name_) \
  1461. ADPCM_ENCODER(id,name,long_name_) ADPCM_DECODER(id,name,long_name_)
  1462. /* Note: Do not forget to add new entries to the Makefile as well. */
  1463. ADPCM_DECODER(CODEC_ID_ADPCM_4XM, adpcm_4xm, "ADPCM 4X Movie");
  1464. ADPCM_DECODER(CODEC_ID_ADPCM_CT, adpcm_ct, "ADPCM Creative Technology");
  1465. ADPCM_DECODER(CODEC_ID_ADPCM_EA, adpcm_ea, "ADPCM Electronic Arts");
  1466. ADPCM_DECODER(CODEC_ID_ADPCM_EA_MAXIS_XA, adpcm_ea_maxis_xa, "ADPCM Electronic Arts Maxis CDROM XA");
  1467. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1, "ADPCM Electronic Arts R1");
  1468. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2, "ADPCM Electronic Arts R2");
  1469. ADPCM_DECODER(CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3, "ADPCM Electronic Arts R3");
  1470. ADPCM_DECODER(CODEC_ID_ADPCM_EA_XAS, adpcm_ea_xas, "ADPCM Electronic Arts XAS");
  1471. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv, "ADPCM IMA AMV");
  1472. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3, "ADPCM IMA Duck DK3");
  1473. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4, "ADPCM IMA Duck DK4");
  1474. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_EACS, adpcm_ima_ea_eacs, "ADPCM IMA Electronic Arts EACS");
  1475. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_EA_SEAD, adpcm_ima_ea_sead, "ADPCM IMA Electronic Arts SEAD");
  1476. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_ISS, adpcm_ima_iss, "ADPCM IMA Funcom ISS");
  1477. ADPCM_CODEC (CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt, "ADPCM IMA QuickTime");
  1478. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg, "ADPCM IMA Loki SDL MJPEG");
  1479. ADPCM_CODEC (CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav, "ADPCM IMA WAV");
  1480. ADPCM_DECODER(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws, "ADPCM IMA Westwood");
  1481. ADPCM_CODEC (CODEC_ID_ADPCM_MS, adpcm_ms, "ADPCM Microsoft");
  1482. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2, "ADPCM Sound Blaster Pro 2-bit");
  1483. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3, "ADPCM Sound Blaster Pro 2.6-bit");
  1484. ADPCM_DECODER(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4, "ADPCM Sound Blaster Pro 4-bit");
  1485. ADPCM_CODEC (CODEC_ID_ADPCM_SWF, adpcm_swf, "ADPCM Shockwave Flash");
  1486. ADPCM_DECODER(CODEC_ID_ADPCM_THP, adpcm_thp, "ADPCM Nintendo Gamecube THP");
  1487. ADPCM_DECODER(CODEC_ID_ADPCM_XA, adpcm_xa, "ADPCM CDROM XA");
  1488. ADPCM_CODEC (CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha, "ADPCM Yamaha");