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.

1603 lines
57KB

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