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.

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