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.

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