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.

1637 lines
58KB

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