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.

1470 lines
51KB

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