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.

1843 lines
67KB

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