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.

1783 lines
65KB

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