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.

1789 lines
66KB

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