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.

1373 lines
48KB

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