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.

1445 lines
50KB

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