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.

1369 lines
47KB

  1. /*
  2. * ADPCM codecs
  3. * Copyright (c) 2001-2003 The ffmpeg Project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "avcodec.h"
  20. #include "bitstream.h"
  21. /**
  22. * @file adpcm.c
  23. * ADPCM codecs.
  24. * First version by Francois Revol (revol@free.fr)
  25. * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
  26. * by Mike Melanson (melanson@pcisys.net)
  27. * CD-ROM XA ADPCM codec by BERO
  28. * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
  29. *
  30. * Features and limitations:
  31. *
  32. * Reference documents:
  33. * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
  34. * http://www.geocities.com/SiliconValley/8682/aud3.txt
  35. * http://openquicktime.sourceforge.net/plugins.htm
  36. * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
  37. * http://www.cs.ucla.edu/~leec/mediabench/applications.html
  38. * SoX source code http://home.sprynet.com/~cbagwell/sox.html
  39. *
  40. * CD-ROM XA:
  41. * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html
  42. * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html
  43. * readstr http://www.geocities.co.jp/Playtown/2004/
  44. */
  45. #define BLKSIZE 1024
  46. #define CLAMP_TO_SHORT(value) \
  47. if (value > 32767) \
  48. value = 32767; \
  49. else if (value < -32768) \
  50. value = -32768; \
  51. /* step_table[] and index_table[] are from the ADPCM reference source */
  52. /* This is the index table: */
  53. static const int index_table[16] = {
  54. -1, -1, -1, -1, 2, 4, 6, 8,
  55. -1, -1, -1, -1, 2, 4, 6, 8,
  56. };
  57. /**
  58. * This is the step table. Note that many programs use slight deviations from
  59. * this table, but such deviations are negligible:
  60. */
  61. static const int step_table[89] = {
  62. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  63. 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  64. 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  65. 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  66. 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  67. 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  68. 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  69. 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  70. 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  71. };
  72. /* These are for MS-ADPCM */
  73. /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
  74. static const int AdaptationTable[] = {
  75. 230, 230, 230, 230, 307, 409, 512, 614,
  76. 768, 614, 512, 409, 307, 230, 230, 230
  77. };
  78. static const int AdaptCoeff1[] = {
  79. 256, 512, 0, 192, 240, 460, 392
  80. };
  81. static const int AdaptCoeff2[] = {
  82. 0, -256, 0, 64, 0, -208, -232
  83. };
  84. /* These are for CD-ROM XA ADPCM */
  85. static const int xa_adpcm_table[5][2] = {
  86. { 0, 0 },
  87. { 60, 0 },
  88. { 115, -52 },
  89. { 98, -55 },
  90. { 122, -60 }
  91. };
  92. static const int ea_adpcm_table[] = {
  93. 0, 240, 460, 392, 0, 0, -208, -220, 0, 1,
  94. 3, 4, 7, 8, 10, 11, 0, -1, -3, -4
  95. };
  96. static const int ct_adpcm_table[8] = {
  97. 0x00E6, 0x00E6, 0x00E6, 0x00E6,
  98. 0x0133, 0x0199, 0x0200, 0x0266
  99. };
  100. // padded to zero where table size is less then 16
  101. static const int swf_index_tables[4][16] = {
  102. /*2*/ { -1, 2 },
  103. /*3*/ { -1, -1, 2, 4 },
  104. /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
  105. /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
  106. };
  107. static const int yamaha_indexscale[] = {
  108. 230, 230, 230, 230, 307, 409, 512, 614,
  109. 230, 230, 230, 230, 307, 409, 512, 614
  110. };
  111. static const int yamaha_difflookup[] = {
  112. 1, 3, 5, 7, 9, 11, 13, 15,
  113. -1, -3, -5, -7, -9, -11, -13, -15
  114. };
  115. /* end of tables */
  116. typedef struct ADPCMChannelStatus {
  117. int predictor;
  118. short int step_index;
  119. int step;
  120. /* for encoding */
  121. int prev_sample;
  122. /* MS version */
  123. short sample1;
  124. short sample2;
  125. int coeff1;
  126. int coeff2;
  127. int idelta;
  128. } ADPCMChannelStatus;
  129. typedef struct ADPCMContext {
  130. int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
  131. ADPCMChannelStatus status[2];
  132. short sample_buffer[32]; /* hold left samples while waiting for right samples */
  133. /* SWF only */
  134. int nb_bits;
  135. int nb_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 = 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= 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 = 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 = clip(div-range, -8, 6);
  279. const int nmax = 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 = clip(div-range, -7, 6);\
  327. int nmax = 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], clip(step + index_table[nibble], 0, 88));
  336. } else { //CODEC_ID_ADPCM_YAMAHA
  337. LOOP_NODES(yamaha, step, 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. c->channel = 0;
  541. c->status[0].predictor = c->status[1].predictor = 0;
  542. c->status[0].step_index = c->status[1].step_index = 0;
  543. c->status[0].step = c->status[1].step = 0;
  544. switch(avctx->codec->id) {
  545. case CODEC_ID_ADPCM_CT:
  546. c->status[0].step = c->status[1].step = 511;
  547. break;
  548. default:
  549. break;
  550. }
  551. return 0;
  552. }
  553. static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
  554. {
  555. int step_index;
  556. int predictor;
  557. int sign, delta, diff, step;
  558. step = step_table[c->step_index];
  559. step_index = c->step_index + index_table[(unsigned)nibble];
  560. if (step_index < 0) step_index = 0;
  561. else if (step_index > 88) step_index = 88;
  562. sign = nibble & 8;
  563. delta = nibble & 7;
  564. /* perform direct multiplication instead of series of jumps proposed by
  565. * the reference ADPCM implementation since modern CPUs can do the mults
  566. * quickly enough */
  567. diff = ((2 * delta + 1) * step) >> shift;
  568. predictor = c->predictor;
  569. if (sign) predictor -= diff;
  570. else predictor += diff;
  571. CLAMP_TO_SHORT(predictor);
  572. c->predictor = predictor;
  573. c->step_index = step_index;
  574. return (short)predictor;
  575. }
  576. static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
  577. {
  578. int predictor;
  579. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
  580. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  581. CLAMP_TO_SHORT(predictor);
  582. c->sample2 = c->sample1;
  583. c->sample1 = predictor;
  584. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
  585. if (c->idelta < 16) c->idelta = 16;
  586. return (short)predictor;
  587. }
  588. static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
  589. {
  590. int predictor;
  591. int sign, delta, diff;
  592. int new_step;
  593. sign = nibble & 8;
  594. delta = nibble & 7;
  595. /* perform direct multiplication instead of series of jumps proposed by
  596. * the reference ADPCM implementation since modern CPUs can do the mults
  597. * quickly enough */
  598. diff = ((2 * delta + 1) * c->step) >> 3;
  599. predictor = c->predictor;
  600. /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
  601. if(sign)
  602. predictor = ((predictor * 254) >> 8) - diff;
  603. else
  604. predictor = ((predictor * 254) >> 8) + diff;
  605. /* calculate new step and clamp it to range 511..32767 */
  606. new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
  607. c->step = new_step;
  608. if(c->step < 511)
  609. c->step = 511;
  610. if(c->step > 32767)
  611. c->step = 32767;
  612. CLAMP_TO_SHORT(predictor);
  613. c->predictor = predictor;
  614. return (short)predictor;
  615. }
  616. static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
  617. {
  618. int sign, delta, diff;
  619. sign = nibble & (1<<(size-1));
  620. delta = nibble & ((1<<(size-1))-1);
  621. diff = delta << (7 + c->step + shift);
  622. if (sign)
  623. c->predictor -= diff;
  624. else
  625. c->predictor += diff;
  626. /* clamp result */
  627. if (c->predictor > 16256)
  628. c->predictor = 16256;
  629. else if (c->predictor < -16384)
  630. c->predictor = -16384;
  631. /* calculate new step */
  632. if (delta >= (2*size - 3) && c->step < 3)
  633. c->step++;
  634. else if (delta == 0 && c->step > 0)
  635. c->step--;
  636. return (short) c->predictor;
  637. }
  638. static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
  639. {
  640. if(!c->step) {
  641. c->predictor = 0;
  642. c->step = 127;
  643. }
  644. c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
  645. CLAMP_TO_SHORT(c->predictor);
  646. c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
  647. c->step = clip(c->step, 127, 24567);
  648. return c->predictor;
  649. }
  650. static void xa_decode(short *out, const unsigned char *in,
  651. ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
  652. {
  653. int i, j;
  654. int shift,filter,f0,f1;
  655. int s_1,s_2;
  656. int d,s,t;
  657. for(i=0;i<4;i++) {
  658. shift = 12 - (in[4+i*2] & 15);
  659. filter = in[4+i*2] >> 4;
  660. f0 = xa_adpcm_table[filter][0];
  661. f1 = xa_adpcm_table[filter][1];
  662. s_1 = left->sample1;
  663. s_2 = left->sample2;
  664. for(j=0;j<28;j++) {
  665. d = in[16+i+j*4];
  666. t = (signed char)(d<<4)>>4;
  667. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  668. CLAMP_TO_SHORT(s);
  669. *out = s;
  670. out += inc;
  671. s_2 = s_1;
  672. s_1 = s;
  673. }
  674. if (inc==2) { /* stereo */
  675. left->sample1 = s_1;
  676. left->sample2 = s_2;
  677. s_1 = right->sample1;
  678. s_2 = right->sample2;
  679. out = out + 1 - 28*2;
  680. }
  681. shift = 12 - (in[5+i*2] & 15);
  682. filter = in[5+i*2] >> 4;
  683. f0 = xa_adpcm_table[filter][0];
  684. f1 = xa_adpcm_table[filter][1];
  685. for(j=0;j<28;j++) {
  686. d = in[16+i+j*4];
  687. t = (signed char)d >> 4;
  688. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  689. CLAMP_TO_SHORT(s);
  690. *out = s;
  691. out += inc;
  692. s_2 = s_1;
  693. s_1 = s;
  694. }
  695. if (inc==2) { /* stereo */
  696. right->sample1 = s_1;
  697. right->sample2 = s_2;
  698. out -= 1;
  699. } else {
  700. left->sample1 = s_1;
  701. left->sample2 = s_2;
  702. }
  703. }
  704. }
  705. /* DK3 ADPCM support macro */
  706. #define DK3_GET_NEXT_NIBBLE() \
  707. if (decode_top_nibble_next) \
  708. { \
  709. nibble = (last_byte >> 4) & 0x0F; \
  710. decode_top_nibble_next = 0; \
  711. } \
  712. else \
  713. { \
  714. last_byte = *src++; \
  715. if (src >= buf + buf_size) break; \
  716. nibble = last_byte & 0x0F; \
  717. decode_top_nibble_next = 1; \
  718. }
  719. static int adpcm_decode_frame(AVCodecContext *avctx,
  720. void *data, int *data_size,
  721. uint8_t *buf, int buf_size)
  722. {
  723. ADPCMContext *c = avctx->priv_data;
  724. ADPCMChannelStatus *cs;
  725. int n, m, channel, i;
  726. int block_predictor[2];
  727. short *samples;
  728. uint8_t *src;
  729. int st; /* stereo */
  730. /* DK3 ADPCM accounting variables */
  731. unsigned char last_byte = 0;
  732. unsigned char nibble;
  733. int decode_top_nibble_next = 0;
  734. int diff_channel;
  735. /* EA ADPCM state variables */
  736. uint32_t samples_in_chunk;
  737. int32_t previous_left_sample, previous_right_sample;
  738. int32_t current_left_sample, current_right_sample;
  739. int32_t next_left_sample, next_right_sample;
  740. int32_t coeff1l, coeff2l, coeff1r, coeff2r;
  741. uint8_t shift_left, shift_right;
  742. int count1, count2;
  743. if (!buf_size)
  744. return 0;
  745. samples = data;
  746. src = buf;
  747. st = avctx->channels == 2 ? 1 : 0;
  748. switch(avctx->codec->id) {
  749. case CODEC_ID_ADPCM_IMA_QT:
  750. n = (buf_size - 2);/* >> 2*avctx->channels;*/
  751. channel = c->channel;
  752. cs = &(c->status[channel]);
  753. /* (pppppp) (piiiiiii) */
  754. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  755. cs->predictor = (*src++) << 8;
  756. cs->predictor |= (*src & 0x80);
  757. cs->predictor &= 0xFF80;
  758. /* sign extension */
  759. if(cs->predictor & 0x8000)
  760. cs->predictor -= 0x10000;
  761. CLAMP_TO_SHORT(cs->predictor);
  762. cs->step_index = (*src++) & 0x7F;
  763. if (cs->step_index > 88){
  764. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  765. cs->step_index = 88;
  766. }
  767. cs->step = step_table[cs->step_index];
  768. if (st && channel)
  769. samples++;
  770. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  771. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
  772. samples += avctx->channels;
  773. *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
  774. samples += avctx->channels;
  775. src ++;
  776. }
  777. if(st) { /* handle stereo interlacing */
  778. c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
  779. if(channel == 1) { /* wait for the other packet before outputing anything */
  780. return src - buf;
  781. }
  782. }
  783. break;
  784. case CODEC_ID_ADPCM_IMA_WAV:
  785. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  786. buf_size = avctx->block_align;
  787. // samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
  788. for(i=0; i<avctx->channels; i++){
  789. cs = &(c->status[i]);
  790. cs->predictor = (int16_t)(src[0] + (src[1]<<8));
  791. src+=2;
  792. // XXX: is this correct ??: *samples++ = cs->predictor;
  793. cs->step_index = *src++;
  794. if (cs->step_index > 88){
  795. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  796. cs->step_index = 88;
  797. }
  798. if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
  799. }
  800. while(src < buf + buf_size){
  801. for(m=0; m<4; m++){
  802. for(i=0; i<=st; i++)
  803. *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);
  804. for(i=0; i<=st; i++)
  805. *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4 , 3);
  806. src++;
  807. }
  808. src += 4*st;
  809. }
  810. break;
  811. case CODEC_ID_ADPCM_4XM:
  812. cs = &(c->status[0]);
  813. c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  814. if(st){
  815. c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  816. }
  817. c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  818. if(st){
  819. c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  820. }
  821. if (cs->step_index < 0) cs->step_index = 0;
  822. if (cs->step_index > 88) cs->step_index = 88;
  823. m= (buf_size - (src - buf))>>st;
  824. for(i=0; i<m; i++) {
  825. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
  826. if (st)
  827. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
  828. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
  829. if (st)
  830. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
  831. }
  832. src += m<<st;
  833. break;
  834. case CODEC_ID_ADPCM_MS:
  835. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  836. buf_size = avctx->block_align;
  837. n = buf_size - 7 * avctx->channels;
  838. if (n < 0)
  839. return -1;
  840. block_predictor[0] = clip(*src++, 0, 7);
  841. block_predictor[1] = 0;
  842. if (st)
  843. block_predictor[1] = clip(*src++, 0, 7);
  844. c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  845. src+=2;
  846. if (st){
  847. c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  848. src+=2;
  849. }
  850. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  851. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  852. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  853. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  854. c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  855. src+=2;
  856. if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  857. if (st) src+=2;
  858. c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  859. src+=2;
  860. if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  861. if (st) src+=2;
  862. *samples++ = c->status[0].sample1;
  863. if (st) *samples++ = c->status[1].sample1;
  864. *samples++ = c->status[0].sample2;
  865. if (st) *samples++ = c->status[1].sample2;
  866. for(;n>0;n--) {
  867. *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  868. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  869. src ++;
  870. }
  871. break;
  872. case CODEC_ID_ADPCM_IMA_DK4:
  873. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  874. buf_size = avctx->block_align;
  875. c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
  876. c->status[0].step_index = src[2];
  877. src += 4;
  878. *samples++ = c->status[0].predictor;
  879. if (st) {
  880. c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
  881. c->status[1].step_index = src[2];
  882. src += 4;
  883. *samples++ = c->status[1].predictor;
  884. }
  885. while (src < buf + buf_size) {
  886. /* take care of the top nibble (always left or mono channel) */
  887. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  888. (src[0] >> 4) & 0x0F, 3);
  889. /* take care of the bottom nibble, which is right sample for
  890. * stereo, or another mono sample */
  891. if (st)
  892. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  893. src[0] & 0x0F, 3);
  894. else
  895. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  896. src[0] & 0x0F, 3);
  897. src++;
  898. }
  899. break;
  900. case CODEC_ID_ADPCM_IMA_DK3:
  901. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  902. buf_size = avctx->block_align;
  903. c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
  904. c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
  905. c->status[0].step_index = src[14];
  906. c->status[1].step_index = src[15];
  907. /* sign extend the predictors */
  908. src += 16;
  909. diff_channel = c->status[1].predictor;
  910. /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
  911. * the buffer is consumed */
  912. while (1) {
  913. /* for this algorithm, c->status[0] is the sum channel and
  914. * c->status[1] is the diff channel */
  915. /* process the first predictor of the sum channel */
  916. DK3_GET_NEXT_NIBBLE();
  917. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  918. /* process the diff channel predictor */
  919. DK3_GET_NEXT_NIBBLE();
  920. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  921. /* process the first pair of stereo PCM samples */
  922. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  923. *samples++ = c->status[0].predictor + c->status[1].predictor;
  924. *samples++ = c->status[0].predictor - c->status[1].predictor;
  925. /* process the second predictor of the sum channel */
  926. DK3_GET_NEXT_NIBBLE();
  927. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  928. /* process the second pair of stereo PCM samples */
  929. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  930. *samples++ = c->status[0].predictor + c->status[1].predictor;
  931. *samples++ = c->status[0].predictor - c->status[1].predictor;
  932. }
  933. break;
  934. case CODEC_ID_ADPCM_IMA_WS:
  935. /* no per-block initialization; just start decoding the data */
  936. while (src < buf + buf_size) {
  937. if (st) {
  938. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  939. (src[0] >> 4) & 0x0F, 3);
  940. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  941. src[0] & 0x0F, 3);
  942. } else {
  943. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  944. (src[0] >> 4) & 0x0F, 3);
  945. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  946. src[0] & 0x0F, 3);
  947. }
  948. src++;
  949. }
  950. break;
  951. case CODEC_ID_ADPCM_XA:
  952. c->status[0].sample1 = c->status[0].sample2 =
  953. c->status[1].sample1 = c->status[1].sample2 = 0;
  954. while (buf_size >= 128) {
  955. xa_decode(samples, src, &c->status[0], &c->status[1],
  956. avctx->channels);
  957. src += 128;
  958. samples += 28 * 8;
  959. buf_size -= 128;
  960. }
  961. break;
  962. case CODEC_ID_ADPCM_EA:
  963. samples_in_chunk = LE_32(src);
  964. if (samples_in_chunk >= ((buf_size - 12) * 2)) {
  965. src += buf_size;
  966. break;
  967. }
  968. src += 4;
  969. current_left_sample = (int16_t)LE_16(src);
  970. src += 2;
  971. previous_left_sample = (int16_t)LE_16(src);
  972. src += 2;
  973. current_right_sample = (int16_t)LE_16(src);
  974. src += 2;
  975. previous_right_sample = (int16_t)LE_16(src);
  976. src += 2;
  977. for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
  978. coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
  979. coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
  980. coeff1r = ea_adpcm_table[*src & 0x0F];
  981. coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
  982. src++;
  983. shift_left = ((*src >> 4) & 0x0F) + 8;
  984. shift_right = (*src & 0x0F) + 8;
  985. src++;
  986. for (count2 = 0; count2 < 28; count2++) {
  987. next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
  988. next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
  989. src++;
  990. next_left_sample = (next_left_sample +
  991. (current_left_sample * coeff1l) +
  992. (previous_left_sample * coeff2l) + 0x80) >> 8;
  993. next_right_sample = (next_right_sample +
  994. (current_right_sample * coeff1r) +
  995. (previous_right_sample * coeff2r) + 0x80) >> 8;
  996. CLAMP_TO_SHORT(next_left_sample);
  997. CLAMP_TO_SHORT(next_right_sample);
  998. previous_left_sample = current_left_sample;
  999. current_left_sample = next_left_sample;
  1000. previous_right_sample = current_right_sample;
  1001. current_right_sample = next_right_sample;
  1002. *samples++ = (unsigned short)current_left_sample;
  1003. *samples++ = (unsigned short)current_right_sample;
  1004. }
  1005. }
  1006. break;
  1007. case CODEC_ID_ADPCM_IMA_SMJPEG:
  1008. c->status[0].predictor = *src;
  1009. src += 2;
  1010. c->status[0].step_index = *src++;
  1011. src++; /* skip another byte before getting to the meat */
  1012. while (src < buf + buf_size) {
  1013. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1014. *src & 0x0F, 3);
  1015. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1016. (*src >> 4) & 0x0F, 3);
  1017. src++;
  1018. }
  1019. break;
  1020. case CODEC_ID_ADPCM_CT:
  1021. while (src < buf + buf_size) {
  1022. if (st) {
  1023. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1024. (src[0] >> 4) & 0x0F);
  1025. *samples++ = adpcm_ct_expand_nibble(&c->status[1],
  1026. src[0] & 0x0F);
  1027. } else {
  1028. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1029. (src[0] >> 4) & 0x0F);
  1030. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1031. src[0] & 0x0F);
  1032. }
  1033. src++;
  1034. }
  1035. break;
  1036. case CODEC_ID_ADPCM_SBPRO_4:
  1037. case CODEC_ID_ADPCM_SBPRO_3:
  1038. case CODEC_ID_ADPCM_SBPRO_2:
  1039. if (!c->status[0].step_index) {
  1040. /* the first byte is a raw sample */
  1041. *samples++ = 128 * (*src++ - 0x80);
  1042. if (st)
  1043. *samples++ = 128 * (*src++ - 0x80);
  1044. c->status[0].step_index = 1;
  1045. }
  1046. if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
  1047. while (src < buf + buf_size) {
  1048. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1049. (src[0] >> 4) & 0x0F, 4, 0);
  1050. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1051. src[0] & 0x0F, 4, 0);
  1052. src++;
  1053. }
  1054. } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
  1055. while (src < buf + buf_size) {
  1056. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1057. (src[0] >> 5) & 0x07, 3, 0);
  1058. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1059. (src[0] >> 2) & 0x07, 3, 0);
  1060. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1061. src[0] & 0x03, 2, 0);
  1062. src++;
  1063. }
  1064. } else {
  1065. while (src < buf + buf_size) {
  1066. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1067. (src[0] >> 6) & 0x03, 2, 2);
  1068. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1069. (src[0] >> 4) & 0x03, 2, 2);
  1070. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1071. (src[0] >> 2) & 0x03, 2, 2);
  1072. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1073. src[0] & 0x03, 2, 2);
  1074. src++;
  1075. }
  1076. }
  1077. break;
  1078. case CODEC_ID_ADPCM_SWF:
  1079. {
  1080. GetBitContext gb;
  1081. const int *table;
  1082. int k0, signmask;
  1083. int size = buf_size*8;
  1084. init_get_bits(&gb, buf, size);
  1085. // first frame, read bits & inital values
  1086. if (!c->nb_bits)
  1087. {
  1088. c->nb_bits = get_bits(&gb, 2)+2;
  1089. // av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", c->nb_bits);
  1090. }
  1091. table = swf_index_tables[c->nb_bits-2];
  1092. k0 = 1 << (c->nb_bits-2);
  1093. signmask = 1 << (c->nb_bits-1);
  1094. while (get_bits_count(&gb) <= size)
  1095. {
  1096. int i;
  1097. c->nb_samples++;
  1098. // wrap around at every 4096 samples...
  1099. if ((c->nb_samples & 0xfff) == 1)
  1100. {
  1101. for (i = 0; i <= st; i++)
  1102. {
  1103. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  1104. c->status[i].step_index = get_bits(&gb, 6);
  1105. }
  1106. }
  1107. // similar to IMA adpcm
  1108. for (i = 0; i <= st; i++)
  1109. {
  1110. int delta = get_bits(&gb, c->nb_bits);
  1111. int step = step_table[c->status[i].step_index];
  1112. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  1113. int k = k0;
  1114. do {
  1115. if (delta & k)
  1116. vpdiff += step;
  1117. step >>= 1;
  1118. k >>= 1;
  1119. } while(k);
  1120. vpdiff += step;
  1121. if (delta & signmask)
  1122. c->status[i].predictor -= vpdiff;
  1123. else
  1124. c->status[i].predictor += vpdiff;
  1125. c->status[i].step_index += table[delta & (~signmask)];
  1126. c->status[i].step_index = clip(c->status[i].step_index, 0, 88);
  1127. c->status[i].predictor = clip(c->status[i].predictor, -32768, 32767);
  1128. *samples++ = c->status[i].predictor;
  1129. }
  1130. }
  1131. // src += get_bits_count(&gb)*8;
  1132. src += size;
  1133. break;
  1134. }
  1135. case CODEC_ID_ADPCM_YAMAHA:
  1136. while (src < buf + buf_size) {
  1137. if (st) {
  1138. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1139. src[0] & 0x0F);
  1140. *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
  1141. (src[0] >> 4) & 0x0F);
  1142. } else {
  1143. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1144. src[0] & 0x0F);
  1145. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1146. (src[0] >> 4) & 0x0F);
  1147. }
  1148. src++;
  1149. }
  1150. break;
  1151. default:
  1152. return -1;
  1153. }
  1154. *data_size = (uint8_t *)samples - (uint8_t *)data;
  1155. return src - buf;
  1156. }
  1157. #ifdef CONFIG_ENCODERS
  1158. #define ADPCM_ENCODER(id,name) \
  1159. AVCodec name ## _encoder = { \
  1160. #name, \
  1161. CODEC_TYPE_AUDIO, \
  1162. id, \
  1163. sizeof(ADPCMContext), \
  1164. adpcm_encode_init, \
  1165. adpcm_encode_frame, \
  1166. adpcm_encode_close, \
  1167. NULL, \
  1168. };
  1169. #else
  1170. #define ADPCM_ENCODER(id,name)
  1171. #endif
  1172. #ifdef CONFIG_DECODERS
  1173. #define ADPCM_DECODER(id,name) \
  1174. AVCodec name ## _decoder = { \
  1175. #name, \
  1176. CODEC_TYPE_AUDIO, \
  1177. id, \
  1178. sizeof(ADPCMContext), \
  1179. adpcm_decode_init, \
  1180. NULL, \
  1181. NULL, \
  1182. adpcm_decode_frame, \
  1183. };
  1184. #else
  1185. #define ADPCM_DECODER(id,name)
  1186. #endif
  1187. #define ADPCM_CODEC(id, name) \
  1188. ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
  1189. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
  1190. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
  1191. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
  1192. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
  1193. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
  1194. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
  1195. ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
  1196. ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
  1197. ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
  1198. ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
  1199. ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
  1200. ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);
  1201. ADPCM_CODEC(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);
  1202. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4);
  1203. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3);
  1204. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2);
  1205. #undef ADPCM_CODEC