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.

1392 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. /* 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 = av_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= av_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 = av_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 = av_clip(div-range, -8, 6);
  281. const int nmax = av_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 = av_clip(div-range, -7, 6);\
  329. int nmax = av_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], av_clip(step + index_table[nibble], 0, 88));
  338. } else { //CODEC_ID_ADPCM_YAMAHA
  339. LOOP_NODES(yamaha, step, av_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. if(avctx->channels > 2U){
  543. return -1;
  544. }
  545. c->channel = 0;
  546. c->status[0].predictor = c->status[1].predictor = 0;
  547. c->status[0].step_index = c->status[1].step_index = 0;
  548. c->status[0].step = c->status[1].step = 0;
  549. switch(avctx->codec->id) {
  550. case CODEC_ID_ADPCM_CT:
  551. c->status[0].step = c->status[1].step = 511;
  552. break;
  553. default:
  554. break;
  555. }
  556. return 0;
  557. }
  558. static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
  559. {
  560. int step_index;
  561. int predictor;
  562. int sign, delta, diff, step;
  563. step = step_table[c->step_index];
  564. step_index = c->step_index + index_table[(unsigned)nibble];
  565. if (step_index < 0) step_index = 0;
  566. else if (step_index > 88) step_index = 88;
  567. sign = nibble & 8;
  568. delta = nibble & 7;
  569. /* perform direct multiplication instead of series of jumps proposed by
  570. * the reference ADPCM implementation since modern CPUs can do the mults
  571. * quickly enough */
  572. diff = ((2 * delta + 1) * step) >> shift;
  573. predictor = c->predictor;
  574. if (sign) predictor -= diff;
  575. else predictor += diff;
  576. CLAMP_TO_SHORT(predictor);
  577. c->predictor = predictor;
  578. c->step_index = step_index;
  579. return (short)predictor;
  580. }
  581. static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
  582. {
  583. int predictor;
  584. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
  585. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  586. CLAMP_TO_SHORT(predictor);
  587. c->sample2 = c->sample1;
  588. c->sample1 = predictor;
  589. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
  590. if (c->idelta < 16) c->idelta = 16;
  591. return (short)predictor;
  592. }
  593. static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
  594. {
  595. int predictor;
  596. int sign, delta, diff;
  597. int new_step;
  598. sign = nibble & 8;
  599. delta = nibble & 7;
  600. /* perform direct multiplication instead of series of jumps proposed by
  601. * the reference ADPCM implementation since modern CPUs can do the mults
  602. * quickly enough */
  603. diff = ((2 * delta + 1) * c->step) >> 3;
  604. predictor = c->predictor;
  605. /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
  606. if(sign)
  607. predictor = ((predictor * 254) >> 8) - diff;
  608. else
  609. predictor = ((predictor * 254) >> 8) + diff;
  610. /* calculate new step and clamp it to range 511..32767 */
  611. new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
  612. c->step = new_step;
  613. if(c->step < 511)
  614. c->step = 511;
  615. if(c->step > 32767)
  616. c->step = 32767;
  617. CLAMP_TO_SHORT(predictor);
  618. c->predictor = predictor;
  619. return (short)predictor;
  620. }
  621. static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
  622. {
  623. int sign, delta, diff;
  624. sign = nibble & (1<<(size-1));
  625. delta = nibble & ((1<<(size-1))-1);
  626. diff = delta << (7 + c->step + shift);
  627. if (sign)
  628. c->predictor -= diff;
  629. else
  630. c->predictor += diff;
  631. /* clamp result */
  632. if (c->predictor > 16256)
  633. c->predictor = 16256;
  634. else if (c->predictor < -16384)
  635. c->predictor = -16384;
  636. /* calculate new step */
  637. if (delta >= (2*size - 3) && c->step < 3)
  638. c->step++;
  639. else if (delta == 0 && c->step > 0)
  640. c->step--;
  641. return (short) c->predictor;
  642. }
  643. static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
  644. {
  645. if(!c->step) {
  646. c->predictor = 0;
  647. c->step = 127;
  648. }
  649. c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
  650. CLAMP_TO_SHORT(c->predictor);
  651. c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
  652. c->step = av_clip(c->step, 127, 24567);
  653. return c->predictor;
  654. }
  655. static void xa_decode(short *out, const unsigned char *in,
  656. ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
  657. {
  658. int i, j;
  659. int shift,filter,f0,f1;
  660. int s_1,s_2;
  661. int d,s,t;
  662. for(i=0;i<4;i++) {
  663. shift = 12 - (in[4+i*2] & 15);
  664. filter = in[4+i*2] >> 4;
  665. f0 = xa_adpcm_table[filter][0];
  666. f1 = xa_adpcm_table[filter][1];
  667. s_1 = left->sample1;
  668. s_2 = left->sample2;
  669. for(j=0;j<28;j++) {
  670. d = in[16+i+j*4];
  671. t = (signed char)(d<<4)>>4;
  672. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  673. CLAMP_TO_SHORT(s);
  674. *out = s;
  675. out += inc;
  676. s_2 = s_1;
  677. s_1 = s;
  678. }
  679. if (inc==2) { /* stereo */
  680. left->sample1 = s_1;
  681. left->sample2 = s_2;
  682. s_1 = right->sample1;
  683. s_2 = right->sample2;
  684. out = out + 1 - 28*2;
  685. }
  686. shift = 12 - (in[5+i*2] & 15);
  687. filter = in[5+i*2] >> 4;
  688. f0 = xa_adpcm_table[filter][0];
  689. f1 = xa_adpcm_table[filter][1];
  690. for(j=0;j<28;j++) {
  691. d = in[16+i+j*4];
  692. t = (signed char)d >> 4;
  693. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  694. CLAMP_TO_SHORT(s);
  695. *out = s;
  696. out += inc;
  697. s_2 = s_1;
  698. s_1 = s;
  699. }
  700. if (inc==2) { /* stereo */
  701. right->sample1 = s_1;
  702. right->sample2 = s_2;
  703. out -= 1;
  704. } else {
  705. left->sample1 = s_1;
  706. left->sample2 = s_2;
  707. }
  708. }
  709. }
  710. /* DK3 ADPCM support macro */
  711. #define DK3_GET_NEXT_NIBBLE() \
  712. if (decode_top_nibble_next) \
  713. { \
  714. nibble = (last_byte >> 4) & 0x0F; \
  715. decode_top_nibble_next = 0; \
  716. } \
  717. else \
  718. { \
  719. last_byte = *src++; \
  720. if (src >= buf + buf_size) break; \
  721. nibble = last_byte & 0x0F; \
  722. decode_top_nibble_next = 1; \
  723. }
  724. static int adpcm_decode_frame(AVCodecContext *avctx,
  725. void *data, int *data_size,
  726. uint8_t *buf, int buf_size)
  727. {
  728. ADPCMContext *c = avctx->priv_data;
  729. ADPCMChannelStatus *cs;
  730. int n, m, channel, i;
  731. int block_predictor[2];
  732. short *samples;
  733. short *samples_end;
  734. uint8_t *src;
  735. int st; /* stereo */
  736. /* DK3 ADPCM accounting variables */
  737. unsigned char last_byte = 0;
  738. unsigned char nibble;
  739. int decode_top_nibble_next = 0;
  740. int diff_channel;
  741. /* EA ADPCM state variables */
  742. uint32_t samples_in_chunk;
  743. int32_t previous_left_sample, previous_right_sample;
  744. int32_t current_left_sample, current_right_sample;
  745. int32_t next_left_sample, next_right_sample;
  746. int32_t coeff1l, coeff2l, coeff1r, coeff2r;
  747. uint8_t shift_left, shift_right;
  748. int count1, count2;
  749. if (!buf_size)
  750. return 0;
  751. //should protect all 4bit ADPCM variants
  752. //8 is needed for CODEC_ID_ADPCM_IMA_WAV with 2 channels
  753. //
  754. if(*data_size/4 < buf_size + 8)
  755. return -1;
  756. samples = data;
  757. samples_end= samples + *data_size/2;
  758. *data_size= 0;
  759. src = buf;
  760. st = avctx->channels == 2 ? 1 : 0;
  761. switch(avctx->codec->id) {
  762. case CODEC_ID_ADPCM_IMA_QT:
  763. n = (buf_size - 2);/* >> 2*avctx->channels;*/
  764. channel = c->channel;
  765. cs = &(c->status[channel]);
  766. /* (pppppp) (piiiiiii) */
  767. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  768. cs->predictor = (*src++) << 8;
  769. cs->predictor |= (*src & 0x80);
  770. cs->predictor &= 0xFF80;
  771. /* sign extension */
  772. if(cs->predictor & 0x8000)
  773. cs->predictor -= 0x10000;
  774. CLAMP_TO_SHORT(cs->predictor);
  775. cs->step_index = (*src++) & 0x7F;
  776. if (cs->step_index > 88){
  777. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  778. cs->step_index = 88;
  779. }
  780. cs->step = step_table[cs->step_index];
  781. if (st && channel)
  782. samples++;
  783. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  784. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
  785. samples += avctx->channels;
  786. *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
  787. samples += avctx->channels;
  788. src ++;
  789. }
  790. if(st) { /* handle stereo interlacing */
  791. c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
  792. if(channel == 1) { /* wait for the other packet before outputing anything */
  793. return src - buf;
  794. }
  795. }
  796. break;
  797. case CODEC_ID_ADPCM_IMA_WAV:
  798. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  799. buf_size = avctx->block_align;
  800. // samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
  801. for(i=0; i<avctx->channels; i++){
  802. cs = &(c->status[i]);
  803. cs->predictor = (int16_t)(src[0] + (src[1]<<8));
  804. src+=2;
  805. // XXX: is this correct ??: *samples++ = cs->predictor;
  806. cs->step_index = *src++;
  807. if (cs->step_index > 88){
  808. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  809. cs->step_index = 88;
  810. }
  811. if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
  812. }
  813. while(src < buf + buf_size){
  814. for(m=0; m<4; m++){
  815. for(i=0; i<=st; i++)
  816. *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);
  817. for(i=0; i<=st; i++)
  818. *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4 , 3);
  819. src++;
  820. }
  821. src += 4*st;
  822. }
  823. break;
  824. case CODEC_ID_ADPCM_4XM:
  825. cs = &(c->status[0]);
  826. c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  827. if(st){
  828. c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  829. }
  830. c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  831. if(st){
  832. c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  833. }
  834. if (cs->step_index < 0) cs->step_index = 0;
  835. if (cs->step_index > 88) cs->step_index = 88;
  836. m= (buf_size - (src - buf))>>st;
  837. for(i=0; i<m; i++) {
  838. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
  839. if (st)
  840. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
  841. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
  842. if (st)
  843. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
  844. }
  845. src += m<<st;
  846. break;
  847. case CODEC_ID_ADPCM_MS:
  848. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  849. buf_size = avctx->block_align;
  850. n = buf_size - 7 * avctx->channels;
  851. if (n < 0)
  852. return -1;
  853. block_predictor[0] = av_clip(*src++, 0, 7);
  854. block_predictor[1] = 0;
  855. if (st)
  856. block_predictor[1] = av_clip(*src++, 0, 7);
  857. c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  858. src+=2;
  859. if (st){
  860. c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  861. src+=2;
  862. }
  863. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  864. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  865. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  866. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  867. c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  868. src+=2;
  869. if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  870. if (st) src+=2;
  871. c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  872. src+=2;
  873. if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  874. if (st) src+=2;
  875. *samples++ = c->status[0].sample1;
  876. if (st) *samples++ = c->status[1].sample1;
  877. *samples++ = c->status[0].sample2;
  878. if (st) *samples++ = c->status[1].sample2;
  879. for(;n>0;n--) {
  880. *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  881. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  882. src ++;
  883. }
  884. break;
  885. case CODEC_ID_ADPCM_IMA_DK4:
  886. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  887. buf_size = avctx->block_align;
  888. c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
  889. c->status[0].step_index = src[2];
  890. src += 4;
  891. *samples++ = c->status[0].predictor;
  892. if (st) {
  893. c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
  894. c->status[1].step_index = src[2];
  895. src += 4;
  896. *samples++ = c->status[1].predictor;
  897. }
  898. while (src < buf + buf_size) {
  899. /* take care of the top nibble (always left or mono channel) */
  900. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  901. (src[0] >> 4) & 0x0F, 3);
  902. /* take care of the bottom nibble, which is right sample for
  903. * stereo, or another mono sample */
  904. if (st)
  905. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  906. src[0] & 0x0F, 3);
  907. else
  908. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  909. src[0] & 0x0F, 3);
  910. src++;
  911. }
  912. break;
  913. case CODEC_ID_ADPCM_IMA_DK3:
  914. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  915. buf_size = avctx->block_align;
  916. if(buf_size + 16 > (samples_end - samples)*3/8)
  917. return -1;
  918. c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
  919. c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
  920. c->status[0].step_index = src[14];
  921. c->status[1].step_index = src[15];
  922. /* sign extend the predictors */
  923. src += 16;
  924. diff_channel = c->status[1].predictor;
  925. /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
  926. * the buffer is consumed */
  927. while (1) {
  928. /* for this algorithm, c->status[0] is the sum channel and
  929. * c->status[1] is the diff channel */
  930. /* process the first predictor of the sum channel */
  931. DK3_GET_NEXT_NIBBLE();
  932. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  933. /* process the diff channel predictor */
  934. DK3_GET_NEXT_NIBBLE();
  935. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  936. /* process the first pair of stereo PCM samples */
  937. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  938. *samples++ = c->status[0].predictor + c->status[1].predictor;
  939. *samples++ = c->status[0].predictor - c->status[1].predictor;
  940. /* process the second predictor of the sum channel */
  941. DK3_GET_NEXT_NIBBLE();
  942. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  943. /* process the second pair of stereo PCM samples */
  944. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  945. *samples++ = c->status[0].predictor + c->status[1].predictor;
  946. *samples++ = c->status[0].predictor - c->status[1].predictor;
  947. }
  948. break;
  949. case CODEC_ID_ADPCM_IMA_WS:
  950. /* no per-block initialization; just start decoding the data */
  951. while (src < buf + buf_size) {
  952. if (st) {
  953. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  954. (src[0] >> 4) & 0x0F, 3);
  955. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  956. src[0] & 0x0F, 3);
  957. } else {
  958. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  959. (src[0] >> 4) & 0x0F, 3);
  960. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  961. src[0] & 0x0F, 3);
  962. }
  963. src++;
  964. }
  965. break;
  966. case CODEC_ID_ADPCM_XA:
  967. c->status[0].sample1 = c->status[0].sample2 =
  968. c->status[1].sample1 = c->status[1].sample2 = 0;
  969. while (buf_size >= 128) {
  970. xa_decode(samples, src, &c->status[0], &c->status[1],
  971. avctx->channels);
  972. src += 128;
  973. samples += 28 * 8;
  974. buf_size -= 128;
  975. }
  976. break;
  977. case CODEC_ID_ADPCM_EA:
  978. samples_in_chunk = AV_RL32(src);
  979. if (samples_in_chunk >= ((buf_size - 12) * 2)) {
  980. src += buf_size;
  981. break;
  982. }
  983. src += 4;
  984. current_left_sample = (int16_t)AV_RL16(src);
  985. src += 2;
  986. previous_left_sample = (int16_t)AV_RL16(src);
  987. src += 2;
  988. current_right_sample = (int16_t)AV_RL16(src);
  989. src += 2;
  990. previous_right_sample = (int16_t)AV_RL16(src);
  991. src += 2;
  992. for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
  993. coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
  994. coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
  995. coeff1r = ea_adpcm_table[*src & 0x0F];
  996. coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
  997. src++;
  998. shift_left = ((*src >> 4) & 0x0F) + 8;
  999. shift_right = (*src & 0x0F) + 8;
  1000. src++;
  1001. for (count2 = 0; count2 < 28; count2++) {
  1002. next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
  1003. next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
  1004. src++;
  1005. next_left_sample = (next_left_sample +
  1006. (current_left_sample * coeff1l) +
  1007. (previous_left_sample * coeff2l) + 0x80) >> 8;
  1008. next_right_sample = (next_right_sample +
  1009. (current_right_sample * coeff1r) +
  1010. (previous_right_sample * coeff2r) + 0x80) >> 8;
  1011. CLAMP_TO_SHORT(next_left_sample);
  1012. CLAMP_TO_SHORT(next_right_sample);
  1013. previous_left_sample = current_left_sample;
  1014. current_left_sample = next_left_sample;
  1015. previous_right_sample = current_right_sample;
  1016. current_right_sample = next_right_sample;
  1017. *samples++ = (unsigned short)current_left_sample;
  1018. *samples++ = (unsigned short)current_right_sample;
  1019. }
  1020. }
  1021. break;
  1022. case CODEC_ID_ADPCM_IMA_SMJPEG:
  1023. c->status[0].predictor = *src;
  1024. src += 2;
  1025. c->status[0].step_index = *src++;
  1026. src++; /* skip another byte before getting to the meat */
  1027. while (src < buf + buf_size) {
  1028. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1029. *src & 0x0F, 3);
  1030. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1031. (*src >> 4) & 0x0F, 3);
  1032. src++;
  1033. }
  1034. break;
  1035. case CODEC_ID_ADPCM_CT:
  1036. while (src < buf + buf_size) {
  1037. if (st) {
  1038. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1039. (src[0] >> 4) & 0x0F);
  1040. *samples++ = adpcm_ct_expand_nibble(&c->status[1],
  1041. src[0] & 0x0F);
  1042. } else {
  1043. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1044. (src[0] >> 4) & 0x0F);
  1045. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1046. src[0] & 0x0F);
  1047. }
  1048. src++;
  1049. }
  1050. break;
  1051. case CODEC_ID_ADPCM_SBPRO_4:
  1052. case CODEC_ID_ADPCM_SBPRO_3:
  1053. case CODEC_ID_ADPCM_SBPRO_2:
  1054. if (!c->status[0].step_index) {
  1055. /* the first byte is a raw sample */
  1056. *samples++ = 128 * (*src++ - 0x80);
  1057. if (st)
  1058. *samples++ = 128 * (*src++ - 0x80);
  1059. c->status[0].step_index = 1;
  1060. }
  1061. if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
  1062. while (src < buf + buf_size) {
  1063. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1064. (src[0] >> 4) & 0x0F, 4, 0);
  1065. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1066. src[0] & 0x0F, 4, 0);
  1067. src++;
  1068. }
  1069. } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
  1070. while (src < buf + buf_size && samples + 2 < samples_end) {
  1071. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1072. (src[0] >> 5) & 0x07, 3, 0);
  1073. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1074. (src[0] >> 2) & 0x07, 3, 0);
  1075. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1076. src[0] & 0x03, 2, 0);
  1077. src++;
  1078. }
  1079. } else {
  1080. while (src < buf + buf_size && samples + 3 < samples_end) {
  1081. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1082. (src[0] >> 6) & 0x03, 2, 2);
  1083. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1084. (src[0] >> 4) & 0x03, 2, 2);
  1085. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1086. (src[0] >> 2) & 0x03, 2, 2);
  1087. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1088. src[0] & 0x03, 2, 2);
  1089. src++;
  1090. }
  1091. }
  1092. break;
  1093. case CODEC_ID_ADPCM_SWF:
  1094. {
  1095. GetBitContext gb;
  1096. const int *table;
  1097. int k0, signmask;
  1098. int size = buf_size*8;
  1099. init_get_bits(&gb, buf, size);
  1100. //FIXME the following return -1 may be removed only after
  1101. //1. correctly spliting the stream into packets at demuxer or parser level
  1102. //2. checking array bounds when writing
  1103. //3. moving the global nb_bits header into extradata
  1104. return -1;
  1105. // first frame, read bits & inital values
  1106. if (!c->nb_bits)
  1107. {
  1108. c->nb_bits = get_bits(&gb, 2)+2;
  1109. // av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", c->nb_bits);
  1110. }
  1111. table = swf_index_tables[c->nb_bits-2];
  1112. k0 = 1 << (c->nb_bits-2);
  1113. signmask = 1 << (c->nb_bits-1);
  1114. while (get_bits_count(&gb) <= size)
  1115. {
  1116. int i;
  1117. c->nb_samples++;
  1118. // wrap around at every 4096 samples...
  1119. if ((c->nb_samples & 0xfff) == 1)
  1120. {
  1121. for (i = 0; i <= st; i++)
  1122. {
  1123. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  1124. c->status[i].step_index = get_bits(&gb, 6);
  1125. }
  1126. }
  1127. // similar to IMA adpcm
  1128. for (i = 0; i <= st; i++)
  1129. {
  1130. int delta = get_bits(&gb, c->nb_bits);
  1131. int step = step_table[c->status[i].step_index];
  1132. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  1133. int k = k0;
  1134. do {
  1135. if (delta & k)
  1136. vpdiff += step;
  1137. step >>= 1;
  1138. k >>= 1;
  1139. } while(k);
  1140. vpdiff += step;
  1141. if (delta & signmask)
  1142. c->status[i].predictor -= vpdiff;
  1143. else
  1144. c->status[i].predictor += vpdiff;
  1145. c->status[i].step_index += table[delta & (~signmask)];
  1146. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  1147. c->status[i].predictor = av_clip(c->status[i].predictor, -32768, 32767);
  1148. *samples++ = c->status[i].predictor;
  1149. }
  1150. }
  1151. // src += get_bits_count(&gb)*8;
  1152. src += size;
  1153. break;
  1154. }
  1155. case CODEC_ID_ADPCM_YAMAHA:
  1156. while (src < buf + buf_size) {
  1157. if (st) {
  1158. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1159. src[0] & 0x0F);
  1160. *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
  1161. (src[0] >> 4) & 0x0F);
  1162. } else {
  1163. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1164. src[0] & 0x0F);
  1165. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1166. (src[0] >> 4) & 0x0F);
  1167. }
  1168. src++;
  1169. }
  1170. break;
  1171. default:
  1172. return -1;
  1173. }
  1174. *data_size = (uint8_t *)samples - (uint8_t *)data;
  1175. return src - buf;
  1176. }
  1177. #ifdef CONFIG_ENCODERS
  1178. #define ADPCM_ENCODER(id,name) \
  1179. AVCodec name ## _encoder = { \
  1180. #name, \
  1181. CODEC_TYPE_AUDIO, \
  1182. id, \
  1183. sizeof(ADPCMContext), \
  1184. adpcm_encode_init, \
  1185. adpcm_encode_frame, \
  1186. adpcm_encode_close, \
  1187. NULL, \
  1188. };
  1189. #else
  1190. #define ADPCM_ENCODER(id,name)
  1191. #endif
  1192. #ifdef CONFIG_DECODERS
  1193. #define ADPCM_DECODER(id,name) \
  1194. AVCodec name ## _decoder = { \
  1195. #name, \
  1196. CODEC_TYPE_AUDIO, \
  1197. id, \
  1198. sizeof(ADPCMContext), \
  1199. adpcm_decode_init, \
  1200. NULL, \
  1201. NULL, \
  1202. adpcm_decode_frame, \
  1203. };
  1204. #else
  1205. #define ADPCM_DECODER(id,name)
  1206. #endif
  1207. #define ADPCM_CODEC(id, name) \
  1208. ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
  1209. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
  1210. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
  1211. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
  1212. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
  1213. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
  1214. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
  1215. ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
  1216. ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
  1217. ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
  1218. ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
  1219. ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
  1220. ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);
  1221. ADPCM_CODEC(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);
  1222. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4);
  1223. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3);
  1224. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2);
  1225. #undef ADPCM_CODEC