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.

1371 lines
47KB

  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. /* SWF only */
  136. int nb_bits;
  137. int nb_samples;
  138. } ADPCMContext;
  139. /* XXX: implement encoding */
  140. #ifdef CONFIG_ENCODERS
  141. static int adpcm_encode_init(AVCodecContext *avctx)
  142. {
  143. if (avctx->channels > 2)
  144. return -1; /* only stereo or mono =) */
  145. switch(avctx->codec->id) {
  146. case CODEC_ID_ADPCM_IMA_QT:
  147. av_log(avctx, AV_LOG_ERROR, "ADPCM: codec adpcm_ima_qt unsupported for encoding !\n");
  148. avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */
  149. return -1;
  150. break;
  151. case CODEC_ID_ADPCM_IMA_WAV:
  152. avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
  153. /* and we have 4 bytes per channel overhead */
  154. avctx->block_align = BLKSIZE;
  155. /* seems frame_size isn't taken into account... have to buffer the samples :-( */
  156. break;
  157. case CODEC_ID_ADPCM_MS:
  158. avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
  159. /* and we have 7 bytes per channel overhead */
  160. avctx->block_align = BLKSIZE;
  161. break;
  162. case CODEC_ID_ADPCM_YAMAHA:
  163. avctx->frame_size = BLKSIZE * avctx->channels;
  164. avctx->block_align = BLKSIZE;
  165. break;
  166. default:
  167. return -1;
  168. break;
  169. }
  170. avctx->coded_frame= avcodec_alloc_frame();
  171. avctx->coded_frame->key_frame= 1;
  172. return 0;
  173. }
  174. static int adpcm_encode_close(AVCodecContext *avctx)
  175. {
  176. av_freep(&avctx->coded_frame);
  177. return 0;
  178. }
  179. static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
  180. {
  181. int delta = sample - c->prev_sample;
  182. int nibble = FFMIN(7, abs(delta)*4/step_table[c->step_index]) + (delta<0)*8;
  183. c->prev_sample = c->prev_sample + ((step_table[c->step_index] * yamaha_difflookup[nibble]) / 8);
  184. CLAMP_TO_SHORT(c->prev_sample);
  185. c->step_index = clip(c->step_index + index_table[nibble], 0, 88);
  186. return nibble;
  187. }
  188. static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
  189. {
  190. int predictor, nibble, bias;
  191. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
  192. nibble= sample - predictor;
  193. if(nibble>=0) bias= c->idelta/2;
  194. else bias=-c->idelta/2;
  195. nibble= (nibble + bias) / c->idelta;
  196. nibble= clip(nibble, -8, 7)&0x0F;
  197. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  198. CLAMP_TO_SHORT(predictor);
  199. c->sample2 = c->sample1;
  200. c->sample1 = predictor;
  201. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
  202. if (c->idelta < 16) c->idelta = 16;
  203. return nibble;
  204. }
  205. static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
  206. {
  207. int nibble, delta;
  208. if(!c->step) {
  209. c->predictor = 0;
  210. c->step = 127;
  211. }
  212. delta = sample - c->predictor;
  213. nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8;
  214. c->predictor = c->predictor + ((c->step * yamaha_difflookup[nibble]) / 8);
  215. CLAMP_TO_SHORT(c->predictor);
  216. c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
  217. c->step = clip(c->step, 127, 24567);
  218. return nibble;
  219. }
  220. typedef struct TrellisPath {
  221. int nibble;
  222. int prev;
  223. } TrellisPath;
  224. typedef struct TrellisNode {
  225. uint32_t ssd;
  226. int path;
  227. int sample1;
  228. int sample2;
  229. int step;
  230. } TrellisNode;
  231. static void adpcm_compress_trellis(AVCodecContext *avctx, const short *samples,
  232. uint8_t *dst, ADPCMChannelStatus *c, int n)
  233. {
  234. #define FREEZE_INTERVAL 128
  235. //FIXME 6% faster if frontier is a compile-time constant
  236. const int frontier = 1 << avctx->trellis;
  237. const int stride = avctx->channels;
  238. const int version = avctx->codec->id;
  239. const int max_paths = frontier*FREEZE_INTERVAL;
  240. TrellisPath paths[max_paths], *p;
  241. TrellisNode node_buf[2][frontier];
  242. TrellisNode *nodep_buf[2][frontier];
  243. TrellisNode **nodes = nodep_buf[0]; // nodes[] is always sorted by .ssd
  244. TrellisNode **nodes_next = nodep_buf[1];
  245. int pathn = 0, froze = -1, i, j, k;
  246. assert(!(max_paths&(max_paths-1)));
  247. memset(nodep_buf, 0, sizeof(nodep_buf));
  248. nodes[0] = &node_buf[1][0];
  249. nodes[0]->ssd = 0;
  250. nodes[0]->path = 0;
  251. nodes[0]->step = c->step_index;
  252. nodes[0]->sample1 = c->sample1;
  253. nodes[0]->sample2 = c->sample2;
  254. if(version == CODEC_ID_ADPCM_IMA_WAV)
  255. nodes[0]->sample1 = c->prev_sample;
  256. if(version == CODEC_ID_ADPCM_MS)
  257. nodes[0]->step = c->idelta;
  258. if(version == CODEC_ID_ADPCM_YAMAHA) {
  259. if(c->step == 0) {
  260. nodes[0]->step = 127;
  261. nodes[0]->sample1 = 0;
  262. } else {
  263. nodes[0]->step = c->step;
  264. nodes[0]->sample1 = c->predictor;
  265. }
  266. }
  267. for(i=0; i<n; i++) {
  268. TrellisNode *t = node_buf[i&1];
  269. TrellisNode **u;
  270. int sample = samples[i*stride];
  271. memset(nodes_next, 0, frontier*sizeof(TrellisNode*));
  272. for(j=0; j<frontier && nodes[j]; j++) {
  273. // higher j have higher ssd already, so they're unlikely to use a suboptimal next sample too
  274. const int range = (j < frontier/2) ? 1 : 0;
  275. const int step = nodes[j]->step;
  276. int nidx;
  277. if(version == CODEC_ID_ADPCM_MS) {
  278. const int predictor = ((nodes[j]->sample1 * c->coeff1) + (nodes[j]->sample2 * c->coeff2)) / 256;
  279. const int div = (sample - predictor) / step;
  280. const int nmin = clip(div-range, -8, 6);
  281. const int nmax = clip(div+range, -7, 7);
  282. for(nidx=nmin; nidx<=nmax; nidx++) {
  283. const int nibble = nidx & 0xf;
  284. int dec_sample = predictor + nidx * step;
  285. #define STORE_NODE(NAME, STEP_INDEX)\
  286. int d;\
  287. uint32_t ssd;\
  288. CLAMP_TO_SHORT(dec_sample);\
  289. d = sample - dec_sample;\
  290. ssd = nodes[j]->ssd + d*d;\
  291. if(nodes_next[frontier-1] && ssd >= nodes_next[frontier-1]->ssd)\
  292. continue;\
  293. /* Collapse any two states with the same previous sample value. \
  294. * One could also distinguish states by step and by 2nd to last
  295. * sample, but the effects of that are negligible. */\
  296. for(k=0; k<frontier && nodes_next[k]; k++) {\
  297. if(dec_sample == nodes_next[k]->sample1) {\
  298. assert(ssd >= nodes_next[k]->ssd);\
  299. goto next_##NAME;\
  300. }\
  301. }\
  302. for(k=0; k<frontier; k++) {\
  303. if(!nodes_next[k] || ssd < nodes_next[k]->ssd) {\
  304. TrellisNode *u = nodes_next[frontier-1];\
  305. if(!u) {\
  306. assert(pathn < max_paths);\
  307. u = t++;\
  308. u->path = pathn++;\
  309. }\
  310. u->ssd = ssd;\
  311. u->step = STEP_INDEX;\
  312. u->sample2 = nodes[j]->sample1;\
  313. u->sample1 = dec_sample;\
  314. paths[u->path].nibble = nibble;\
  315. paths[u->path].prev = nodes[j]->path;\
  316. memmove(&nodes_next[k+1], &nodes_next[k], (frontier-k-1)*sizeof(TrellisNode*));\
  317. nodes_next[k] = u;\
  318. break;\
  319. }\
  320. }\
  321. next_##NAME:;
  322. STORE_NODE(ms, FFMAX(16, (AdaptationTable[nibble] * step) >> 8));
  323. }
  324. } else if(version == CODEC_ID_ADPCM_IMA_WAV) {
  325. #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
  326. const int predictor = nodes[j]->sample1;\
  327. const int div = (sample - predictor) * 4 / STEP_TABLE;\
  328. int nmin = clip(div-range, -7, 6);\
  329. int nmax = clip(div+range, -6, 7);\
  330. if(nmin<=0) nmin--; /* distinguish -0 from +0 */\
  331. if(nmax<0) nmax--;\
  332. for(nidx=nmin; nidx<=nmax; nidx++) {\
  333. const int nibble = nidx<0 ? 7-nidx : nidx;\
  334. int dec_sample = predictor + (STEP_TABLE * yamaha_difflookup[nibble]) / 8;\
  335. STORE_NODE(NAME, STEP_INDEX);\
  336. }
  337. LOOP_NODES(ima, step_table[step], clip(step + index_table[nibble], 0, 88));
  338. } else { //CODEC_ID_ADPCM_YAMAHA
  339. LOOP_NODES(yamaha, step, clip((step * yamaha_indexscale[nibble]) >> 8, 127, 24567));
  340. #undef LOOP_NODES
  341. #undef STORE_NODE
  342. }
  343. }
  344. u = nodes;
  345. nodes = nodes_next;
  346. nodes_next = u;
  347. // prevent overflow
  348. if(nodes[0]->ssd > (1<<28)) {
  349. for(j=1; j<frontier && nodes[j]; j++)
  350. nodes[j]->ssd -= nodes[0]->ssd;
  351. nodes[0]->ssd = 0;
  352. }
  353. // merge old paths to save memory
  354. if(i == froze + FREEZE_INTERVAL) {
  355. p = &paths[nodes[0]->path];
  356. for(k=i; k>froze; k--) {
  357. dst[k] = p->nibble;
  358. p = &paths[p->prev];
  359. }
  360. froze = i;
  361. pathn = 0;
  362. // other nodes might use paths that don't coincide with the frozen one.
  363. // checking which nodes do so is too slow, so just kill them all.
  364. // this also slightly improves quality, but I don't know why.
  365. memset(nodes+1, 0, (frontier-1)*sizeof(TrellisNode*));
  366. }
  367. }
  368. p = &paths[nodes[0]->path];
  369. for(i=n-1; i>froze; i--) {
  370. dst[i] = p->nibble;
  371. p = &paths[p->prev];
  372. }
  373. c->predictor = nodes[0]->sample1;
  374. c->sample1 = nodes[0]->sample1;
  375. c->sample2 = nodes[0]->sample2;
  376. c->step_index = nodes[0]->step;
  377. c->step = nodes[0]->step;
  378. c->idelta = nodes[0]->step;
  379. }
  380. static int adpcm_encode_frame(AVCodecContext *avctx,
  381. unsigned char *frame, int buf_size, void *data)
  382. {
  383. int n, i, st;
  384. short *samples;
  385. unsigned char *dst;
  386. ADPCMContext *c = avctx->priv_data;
  387. dst = frame;
  388. samples = (short *)data;
  389. st= avctx->channels == 2;
  390. /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
  391. switch(avctx->codec->id) {
  392. case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
  393. break;
  394. case CODEC_ID_ADPCM_IMA_WAV:
  395. n = avctx->frame_size / 8;
  396. c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
  397. /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
  398. *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
  399. *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
  400. *dst++ = (unsigned char)c->status[0].step_index;
  401. *dst++ = 0; /* unknown */
  402. samples++;
  403. if (avctx->channels == 2) {
  404. c->status[1].prev_sample = (signed short)samples[1];
  405. /* c->status[1].step_index = 0; */
  406. *dst++ = (c->status[1].prev_sample) & 0xFF;
  407. *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
  408. *dst++ = (unsigned char)c->status[1].step_index;
  409. *dst++ = 0;
  410. samples++;
  411. }
  412. /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
  413. if(avctx->trellis > 0) {
  414. uint8_t buf[2][n*8];
  415. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n*8);
  416. if(avctx->channels == 2)
  417. adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n*8);
  418. for(i=0; i<n; i++) {
  419. *dst++ = buf[0][8*i+0] | (buf[0][8*i+1] << 4);
  420. *dst++ = buf[0][8*i+2] | (buf[0][8*i+3] << 4);
  421. *dst++ = buf[0][8*i+4] | (buf[0][8*i+5] << 4);
  422. *dst++ = buf[0][8*i+6] | (buf[0][8*i+7] << 4);
  423. if (avctx->channels == 2) {
  424. *dst++ = buf[1][8*i+0] | (buf[1][8*i+1] << 4);
  425. *dst++ = buf[1][8*i+2] | (buf[1][8*i+3] << 4);
  426. *dst++ = buf[1][8*i+4] | (buf[1][8*i+5] << 4);
  427. *dst++ = buf[1][8*i+6] | (buf[1][8*i+7] << 4);
  428. }
  429. }
  430. } else
  431. for (; n>0; n--) {
  432. *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F;
  433. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0;
  434. dst++;
  435. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F;
  436. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0;
  437. dst++;
  438. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F;
  439. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0;
  440. dst++;
  441. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F;
  442. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0;
  443. dst++;
  444. /* right channel */
  445. if (avctx->channels == 2) {
  446. *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
  447. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
  448. dst++;
  449. *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
  450. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
  451. dst++;
  452. *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
  453. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
  454. dst++;
  455. *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
  456. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
  457. dst++;
  458. }
  459. samples += 8 * avctx->channels;
  460. }
  461. break;
  462. case CODEC_ID_ADPCM_MS:
  463. for(i=0; i<avctx->channels; i++){
  464. int predictor=0;
  465. *dst++ = predictor;
  466. c->status[i].coeff1 = AdaptCoeff1[predictor];
  467. c->status[i].coeff2 = AdaptCoeff2[predictor];
  468. }
  469. for(i=0; i<avctx->channels; i++){
  470. if (c->status[i].idelta < 16)
  471. c->status[i].idelta = 16;
  472. *dst++ = c->status[i].idelta & 0xFF;
  473. *dst++ = c->status[i].idelta >> 8;
  474. }
  475. for(i=0; i<avctx->channels; i++){
  476. c->status[i].sample1= *samples++;
  477. *dst++ = c->status[i].sample1 & 0xFF;
  478. *dst++ = c->status[i].sample1 >> 8;
  479. }
  480. for(i=0; i<avctx->channels; i++){
  481. c->status[i].sample2= *samples++;
  482. *dst++ = c->status[i].sample2 & 0xFF;
  483. *dst++ = c->status[i].sample2 >> 8;
  484. }
  485. if(avctx->trellis > 0) {
  486. int n = avctx->block_align - 7*avctx->channels;
  487. uint8_t buf[2][n];
  488. if(avctx->channels == 1) {
  489. n *= 2;
  490. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
  491. for(i=0; i<n; i+=2)
  492. *dst++ = (buf[0][i] << 4) | buf[0][i+1];
  493. } else {
  494. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
  495. adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n);
  496. for(i=0; i<n; i++)
  497. *dst++ = (buf[0][i] << 4) | buf[1][i];
  498. }
  499. } else
  500. for(i=7*avctx->channels; i<avctx->block_align; i++) {
  501. int nibble;
  502. nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
  503. nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
  504. *dst++ = nibble;
  505. }
  506. break;
  507. case CODEC_ID_ADPCM_YAMAHA:
  508. n = avctx->frame_size / 2;
  509. if(avctx->trellis > 0) {
  510. uint8_t buf[2][n*2];
  511. n *= 2;
  512. if(avctx->channels == 1) {
  513. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
  514. for(i=0; i<n; i+=2)
  515. *dst++ = buf[0][i] | (buf[0][i+1] << 4);
  516. } else {
  517. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
  518. adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n);
  519. for(i=0; i<n; i++)
  520. *dst++ = buf[0][i] | (buf[1][i] << 4);
  521. }
  522. } else
  523. for (; n>0; n--) {
  524. for(i = 0; i < avctx->channels; i++) {
  525. int nibble;
  526. nibble = adpcm_yamaha_compress_sample(&c->status[i], samples[i]);
  527. nibble |= adpcm_yamaha_compress_sample(&c->status[i], samples[i+avctx->channels]) << 4;
  528. *dst++ = nibble;
  529. }
  530. samples += 2 * avctx->channels;
  531. }
  532. break;
  533. default:
  534. return -1;
  535. }
  536. return dst - frame;
  537. }
  538. #endif //CONFIG_ENCODERS
  539. static int adpcm_decode_init(AVCodecContext * avctx)
  540. {
  541. ADPCMContext *c = avctx->priv_data;
  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 = 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. uint8_t *src;
  731. int st; /* stereo */
  732. /* DK3 ADPCM accounting variables */
  733. unsigned char last_byte = 0;
  734. unsigned char nibble;
  735. int decode_top_nibble_next = 0;
  736. int diff_channel;
  737. /* EA ADPCM state variables */
  738. uint32_t samples_in_chunk;
  739. int32_t previous_left_sample, previous_right_sample;
  740. int32_t current_left_sample, current_right_sample;
  741. int32_t next_left_sample, next_right_sample;
  742. int32_t coeff1l, coeff2l, coeff1r, coeff2r;
  743. uint8_t shift_left, shift_right;
  744. int count1, count2;
  745. if (!buf_size)
  746. return 0;
  747. samples = data;
  748. src = buf;
  749. st = avctx->channels == 2 ? 1 : 0;
  750. switch(avctx->codec->id) {
  751. case CODEC_ID_ADPCM_IMA_QT:
  752. n = (buf_size - 2);/* >> 2*avctx->channels;*/
  753. channel = c->channel;
  754. cs = &(c->status[channel]);
  755. /* (pppppp) (piiiiiii) */
  756. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  757. cs->predictor = (*src++) << 8;
  758. cs->predictor |= (*src & 0x80);
  759. cs->predictor &= 0xFF80;
  760. /* sign extension */
  761. if(cs->predictor & 0x8000)
  762. cs->predictor -= 0x10000;
  763. CLAMP_TO_SHORT(cs->predictor);
  764. cs->step_index = (*src++) & 0x7F;
  765. if (cs->step_index > 88){
  766. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  767. cs->step_index = 88;
  768. }
  769. cs->step = step_table[cs->step_index];
  770. if (st && channel)
  771. samples++;
  772. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  773. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
  774. samples += avctx->channels;
  775. *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
  776. samples += avctx->channels;
  777. src ++;
  778. }
  779. if(st) { /* handle stereo interlacing */
  780. c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
  781. if(channel == 1) { /* wait for the other packet before outputing anything */
  782. return src - buf;
  783. }
  784. }
  785. break;
  786. case CODEC_ID_ADPCM_IMA_WAV:
  787. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  788. buf_size = avctx->block_align;
  789. // samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
  790. for(i=0; i<avctx->channels; i++){
  791. cs = &(c->status[i]);
  792. cs->predictor = (int16_t)(src[0] + (src[1]<<8));
  793. src+=2;
  794. // XXX: is this correct ??: *samples++ = cs->predictor;
  795. cs->step_index = *src++;
  796. if (cs->step_index > 88){
  797. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  798. cs->step_index = 88;
  799. }
  800. if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
  801. }
  802. while(src < buf + buf_size){
  803. for(m=0; m<4; m++){
  804. for(i=0; i<=st; i++)
  805. *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);
  806. for(i=0; i<=st; i++)
  807. *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4 , 3);
  808. src++;
  809. }
  810. src += 4*st;
  811. }
  812. break;
  813. case CODEC_ID_ADPCM_4XM:
  814. cs = &(c->status[0]);
  815. c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  816. if(st){
  817. c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  818. }
  819. c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  820. if(st){
  821. c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  822. }
  823. if (cs->step_index < 0) cs->step_index = 0;
  824. if (cs->step_index > 88) cs->step_index = 88;
  825. m= (buf_size - (src - buf))>>st;
  826. for(i=0; i<m; i++) {
  827. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
  828. if (st)
  829. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
  830. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
  831. if (st)
  832. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
  833. }
  834. src += m<<st;
  835. break;
  836. case CODEC_ID_ADPCM_MS:
  837. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  838. buf_size = avctx->block_align;
  839. n = buf_size - 7 * avctx->channels;
  840. if (n < 0)
  841. return -1;
  842. block_predictor[0] = clip(*src++, 0, 7);
  843. block_predictor[1] = 0;
  844. if (st)
  845. block_predictor[1] = clip(*src++, 0, 7);
  846. c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  847. src+=2;
  848. if (st){
  849. c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  850. src+=2;
  851. }
  852. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  853. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  854. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  855. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  856. c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  857. src+=2;
  858. if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  859. if (st) src+=2;
  860. c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  861. src+=2;
  862. if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  863. if (st) src+=2;
  864. *samples++ = c->status[0].sample1;
  865. if (st) *samples++ = c->status[1].sample1;
  866. *samples++ = c->status[0].sample2;
  867. if (st) *samples++ = c->status[1].sample2;
  868. for(;n>0;n--) {
  869. *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  870. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  871. src ++;
  872. }
  873. break;
  874. case CODEC_ID_ADPCM_IMA_DK4:
  875. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  876. buf_size = avctx->block_align;
  877. c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
  878. c->status[0].step_index = src[2];
  879. src += 4;
  880. *samples++ = c->status[0].predictor;
  881. if (st) {
  882. c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
  883. c->status[1].step_index = src[2];
  884. src += 4;
  885. *samples++ = c->status[1].predictor;
  886. }
  887. while (src < buf + buf_size) {
  888. /* take care of the top nibble (always left or mono channel) */
  889. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  890. (src[0] >> 4) & 0x0F, 3);
  891. /* take care of the bottom nibble, which is right sample for
  892. * stereo, or another mono sample */
  893. if (st)
  894. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  895. src[0] & 0x0F, 3);
  896. else
  897. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  898. src[0] & 0x0F, 3);
  899. src++;
  900. }
  901. break;
  902. case CODEC_ID_ADPCM_IMA_DK3:
  903. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  904. buf_size = avctx->block_align;
  905. c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
  906. c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
  907. c->status[0].step_index = src[14];
  908. c->status[1].step_index = src[15];
  909. /* sign extend the predictors */
  910. src += 16;
  911. diff_channel = c->status[1].predictor;
  912. /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
  913. * the buffer is consumed */
  914. while (1) {
  915. /* for this algorithm, c->status[0] is the sum channel and
  916. * c->status[1] is the diff channel */
  917. /* process the first predictor of the sum channel */
  918. DK3_GET_NEXT_NIBBLE();
  919. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  920. /* process the diff channel predictor */
  921. DK3_GET_NEXT_NIBBLE();
  922. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  923. /* process the first pair of stereo PCM samples */
  924. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  925. *samples++ = c->status[0].predictor + c->status[1].predictor;
  926. *samples++ = c->status[0].predictor - c->status[1].predictor;
  927. /* process the second predictor of the sum channel */
  928. DK3_GET_NEXT_NIBBLE();
  929. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  930. /* process the second pair of stereo PCM samples */
  931. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  932. *samples++ = c->status[0].predictor + c->status[1].predictor;
  933. *samples++ = c->status[0].predictor - c->status[1].predictor;
  934. }
  935. break;
  936. case CODEC_ID_ADPCM_IMA_WS:
  937. /* no per-block initialization; just start decoding the data */
  938. while (src < buf + buf_size) {
  939. if (st) {
  940. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  941. (src[0] >> 4) & 0x0F, 3);
  942. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  943. src[0] & 0x0F, 3);
  944. } else {
  945. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  946. (src[0] >> 4) & 0x0F, 3);
  947. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  948. src[0] & 0x0F, 3);
  949. }
  950. src++;
  951. }
  952. break;
  953. case CODEC_ID_ADPCM_XA:
  954. c->status[0].sample1 = c->status[0].sample2 =
  955. c->status[1].sample1 = c->status[1].sample2 = 0;
  956. while (buf_size >= 128) {
  957. xa_decode(samples, src, &c->status[0], &c->status[1],
  958. avctx->channels);
  959. src += 128;
  960. samples += 28 * 8;
  961. buf_size -= 128;
  962. }
  963. break;
  964. case CODEC_ID_ADPCM_EA:
  965. samples_in_chunk = LE_32(src);
  966. if (samples_in_chunk >= ((buf_size - 12) * 2)) {
  967. src += buf_size;
  968. break;
  969. }
  970. src += 4;
  971. current_left_sample = (int16_t)LE_16(src);
  972. src += 2;
  973. previous_left_sample = (int16_t)LE_16(src);
  974. src += 2;
  975. current_right_sample = (int16_t)LE_16(src);
  976. src += 2;
  977. previous_right_sample = (int16_t)LE_16(src);
  978. src += 2;
  979. for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
  980. coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
  981. coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
  982. coeff1r = ea_adpcm_table[*src & 0x0F];
  983. coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
  984. src++;
  985. shift_left = ((*src >> 4) & 0x0F) + 8;
  986. shift_right = (*src & 0x0F) + 8;
  987. src++;
  988. for (count2 = 0; count2 < 28; count2++) {
  989. next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
  990. next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
  991. src++;
  992. next_left_sample = (next_left_sample +
  993. (current_left_sample * coeff1l) +
  994. (previous_left_sample * coeff2l) + 0x80) >> 8;
  995. next_right_sample = (next_right_sample +
  996. (current_right_sample * coeff1r) +
  997. (previous_right_sample * coeff2r) + 0x80) >> 8;
  998. CLAMP_TO_SHORT(next_left_sample);
  999. CLAMP_TO_SHORT(next_right_sample);
  1000. previous_left_sample = current_left_sample;
  1001. current_left_sample = next_left_sample;
  1002. previous_right_sample = current_right_sample;
  1003. current_right_sample = next_right_sample;
  1004. *samples++ = (unsigned short)current_left_sample;
  1005. *samples++ = (unsigned short)current_right_sample;
  1006. }
  1007. }
  1008. break;
  1009. case CODEC_ID_ADPCM_IMA_SMJPEG:
  1010. c->status[0].predictor = *src;
  1011. src += 2;
  1012. c->status[0].step_index = *src++;
  1013. src++; /* skip another byte before getting to the meat */
  1014. while (src < buf + buf_size) {
  1015. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1016. *src & 0x0F, 3);
  1017. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1018. (*src >> 4) & 0x0F, 3);
  1019. src++;
  1020. }
  1021. break;
  1022. case CODEC_ID_ADPCM_CT:
  1023. while (src < buf + buf_size) {
  1024. if (st) {
  1025. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1026. (src[0] >> 4) & 0x0F);
  1027. *samples++ = adpcm_ct_expand_nibble(&c->status[1],
  1028. src[0] & 0x0F);
  1029. } else {
  1030. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1031. (src[0] >> 4) & 0x0F);
  1032. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1033. src[0] & 0x0F);
  1034. }
  1035. src++;
  1036. }
  1037. break;
  1038. case CODEC_ID_ADPCM_SBPRO_4:
  1039. case CODEC_ID_ADPCM_SBPRO_3:
  1040. case CODEC_ID_ADPCM_SBPRO_2:
  1041. if (!c->status[0].step_index) {
  1042. /* the first byte is a raw sample */
  1043. *samples++ = 128 * (*src++ - 0x80);
  1044. if (st)
  1045. *samples++ = 128 * (*src++ - 0x80);
  1046. c->status[0].step_index = 1;
  1047. }
  1048. if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
  1049. while (src < buf + buf_size) {
  1050. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1051. (src[0] >> 4) & 0x0F, 4, 0);
  1052. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1053. src[0] & 0x0F, 4, 0);
  1054. src++;
  1055. }
  1056. } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
  1057. while (src < buf + buf_size) {
  1058. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1059. (src[0] >> 5) & 0x07, 3, 0);
  1060. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1061. (src[0] >> 2) & 0x07, 3, 0);
  1062. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1063. src[0] & 0x03, 2, 0);
  1064. src++;
  1065. }
  1066. } else {
  1067. while (src < buf + buf_size) {
  1068. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1069. (src[0] >> 6) & 0x03, 2, 2);
  1070. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1071. (src[0] >> 4) & 0x03, 2, 2);
  1072. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1073. (src[0] >> 2) & 0x03, 2, 2);
  1074. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1075. src[0] & 0x03, 2, 2);
  1076. src++;
  1077. }
  1078. }
  1079. break;
  1080. case CODEC_ID_ADPCM_SWF:
  1081. {
  1082. GetBitContext gb;
  1083. const int *table;
  1084. int k0, signmask;
  1085. int size = buf_size*8;
  1086. init_get_bits(&gb, buf, size);
  1087. // first frame, read bits & inital values
  1088. if (!c->nb_bits)
  1089. {
  1090. c->nb_bits = get_bits(&gb, 2)+2;
  1091. // av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", c->nb_bits);
  1092. }
  1093. table = swf_index_tables[c->nb_bits-2];
  1094. k0 = 1 << (c->nb_bits-2);
  1095. signmask = 1 << (c->nb_bits-1);
  1096. while (get_bits_count(&gb) <= size)
  1097. {
  1098. int i;
  1099. c->nb_samples++;
  1100. // wrap around at every 4096 samples...
  1101. if ((c->nb_samples & 0xfff) == 1)
  1102. {
  1103. for (i = 0; i <= st; i++)
  1104. {
  1105. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  1106. c->status[i].step_index = get_bits(&gb, 6);
  1107. }
  1108. }
  1109. // similar to IMA adpcm
  1110. for (i = 0; i <= st; i++)
  1111. {
  1112. int delta = get_bits(&gb, c->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 = clip(c->status[i].step_index, 0, 88);
  1129. c->status[i].predictor = clip(c->status[i].predictor, -32768, 32767);
  1130. *samples++ = c->status[i].predictor;
  1131. }
  1132. }
  1133. // src += get_bits_count(&gb)*8;
  1134. src += size;
  1135. break;
  1136. }
  1137. case CODEC_ID_ADPCM_YAMAHA:
  1138. while (src < buf + buf_size) {
  1139. if (st) {
  1140. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1141. src[0] & 0x0F);
  1142. *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
  1143. (src[0] >> 4) & 0x0F);
  1144. } else {
  1145. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1146. src[0] & 0x0F);
  1147. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1148. (src[0] >> 4) & 0x0F);
  1149. }
  1150. src++;
  1151. }
  1152. break;
  1153. default:
  1154. return -1;
  1155. }
  1156. *data_size = (uint8_t *)samples - (uint8_t *)data;
  1157. return src - buf;
  1158. }
  1159. #ifdef CONFIG_ENCODERS
  1160. #define ADPCM_ENCODER(id,name) \
  1161. AVCodec name ## _encoder = { \
  1162. #name, \
  1163. CODEC_TYPE_AUDIO, \
  1164. id, \
  1165. sizeof(ADPCMContext), \
  1166. adpcm_encode_init, \
  1167. adpcm_encode_frame, \
  1168. adpcm_encode_close, \
  1169. NULL, \
  1170. };
  1171. #else
  1172. #define ADPCM_ENCODER(id,name)
  1173. #endif
  1174. #ifdef CONFIG_DECODERS
  1175. #define ADPCM_DECODER(id,name) \
  1176. AVCodec name ## _decoder = { \
  1177. #name, \
  1178. CODEC_TYPE_AUDIO, \
  1179. id, \
  1180. sizeof(ADPCMContext), \
  1181. adpcm_decode_init, \
  1182. NULL, \
  1183. NULL, \
  1184. adpcm_decode_frame, \
  1185. };
  1186. #else
  1187. #define ADPCM_DECODER(id,name)
  1188. #endif
  1189. #define ADPCM_CODEC(id, name) \
  1190. ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
  1191. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
  1192. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
  1193. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
  1194. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
  1195. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
  1196. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
  1197. ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
  1198. ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
  1199. ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
  1200. ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
  1201. ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
  1202. ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);
  1203. ADPCM_CODEC(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);
  1204. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4);
  1205. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3);
  1206. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2);
  1207. #undef ADPCM_CODEC