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.

1841 lines
66KB

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