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.

1748 lines
64KB

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