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.

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