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.

1463 lines
51KB

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