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.

1458 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 += ((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. c->sample2 = c->sample1;
  202. c->sample1 = av_clip_int16(predictor);
  203. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
  204. if (c->idelta < 16) c->idelta = 16;
  205. return nibble;
  206. }
  207. static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
  208. {
  209. int nibble, delta;
  210. if(!c->step) {
  211. c->predictor = 0;
  212. c->step = 127;
  213. }
  214. delta = sample - c->predictor;
  215. nibble = FFMIN(7, abs(delta)*4/c->step) + (delta<0)*8;
  216. c->predictor += ((c->step * yamaha_difflookup[nibble]) / 8);
  217. c->predictor = av_clip_int16(c->predictor);
  218. c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
  219. c->step = av_clip(c->step, 127, 24567);
  220. return nibble;
  221. }
  222. typedef struct TrellisPath {
  223. int nibble;
  224. int prev;
  225. } TrellisPath;
  226. typedef struct TrellisNode {
  227. uint32_t ssd;
  228. int path;
  229. int sample1;
  230. int sample2;
  231. int step;
  232. } TrellisNode;
  233. static void adpcm_compress_trellis(AVCodecContext *avctx, const short *samples,
  234. uint8_t *dst, ADPCMChannelStatus *c, int n)
  235. {
  236. #define FREEZE_INTERVAL 128
  237. //FIXME 6% faster if frontier is a compile-time constant
  238. const int frontier = 1 << avctx->trellis;
  239. const int stride = avctx->channels;
  240. const int version = avctx->codec->id;
  241. const int max_paths = frontier*FREEZE_INTERVAL;
  242. TrellisPath paths[max_paths], *p;
  243. TrellisNode node_buf[2][frontier];
  244. TrellisNode *nodep_buf[2][frontier];
  245. TrellisNode **nodes = nodep_buf[0]; // nodes[] is always sorted by .ssd
  246. TrellisNode **nodes_next = nodep_buf[1];
  247. int pathn = 0, froze = -1, i, j, k;
  248. assert(!(max_paths&(max_paths-1)));
  249. memset(nodep_buf, 0, sizeof(nodep_buf));
  250. nodes[0] = &node_buf[1][0];
  251. nodes[0]->ssd = 0;
  252. nodes[0]->path = 0;
  253. nodes[0]->step = c->step_index;
  254. nodes[0]->sample1 = c->sample1;
  255. nodes[0]->sample2 = c->sample2;
  256. if((version == CODEC_ID_ADPCM_IMA_WAV) || (version == CODEC_ID_ADPCM_SWF))
  257. nodes[0]->sample1 = c->prev_sample;
  258. if(version == CODEC_ID_ADPCM_MS)
  259. nodes[0]->step = c->idelta;
  260. if(version == CODEC_ID_ADPCM_YAMAHA) {
  261. if(c->step == 0) {
  262. nodes[0]->step = 127;
  263. nodes[0]->sample1 = 0;
  264. } else {
  265. nodes[0]->step = c->step;
  266. nodes[0]->sample1 = c->predictor;
  267. }
  268. }
  269. for(i=0; i<n; i++) {
  270. TrellisNode *t = node_buf[i&1];
  271. TrellisNode **u;
  272. int sample = samples[i*stride];
  273. memset(nodes_next, 0, frontier*sizeof(TrellisNode*));
  274. for(j=0; j<frontier && nodes[j]; j++) {
  275. // higher j have higher ssd already, so they're unlikely to use a suboptimal next sample too
  276. const int range = (j < frontier/2) ? 1 : 0;
  277. const int step = nodes[j]->step;
  278. int nidx;
  279. if(version == CODEC_ID_ADPCM_MS) {
  280. const int predictor = ((nodes[j]->sample1 * c->coeff1) + (nodes[j]->sample2 * c->coeff2)) / 256;
  281. const int div = (sample - predictor) / step;
  282. const int nmin = av_clip(div-range, -8, 6);
  283. const int nmax = av_clip(div+range, -7, 7);
  284. for(nidx=nmin; nidx<=nmax; nidx++) {
  285. const int nibble = nidx & 0xf;
  286. int dec_sample = predictor + nidx * step;
  287. #define STORE_NODE(NAME, STEP_INDEX)\
  288. int d;\
  289. uint32_t ssd;\
  290. dec_sample = av_clip_int16(dec_sample);\
  291. d = sample - dec_sample;\
  292. ssd = nodes[j]->ssd + d*d;\
  293. if(nodes_next[frontier-1] && ssd >= nodes_next[frontier-1]->ssd)\
  294. continue;\
  295. /* Collapse any two states with the same previous sample value. \
  296. * One could also distinguish states by step and by 2nd to last
  297. * sample, but the effects of that are negligible. */\
  298. for(k=0; k<frontier && nodes_next[k]; k++) {\
  299. if(dec_sample == nodes_next[k]->sample1) {\
  300. assert(ssd >= nodes_next[k]->ssd);\
  301. goto next_##NAME;\
  302. }\
  303. }\
  304. for(k=0; k<frontier; k++) {\
  305. if(!nodes_next[k] || ssd < nodes_next[k]->ssd) {\
  306. TrellisNode *u = nodes_next[frontier-1];\
  307. if(!u) {\
  308. assert(pathn < max_paths);\
  309. u = t++;\
  310. u->path = pathn++;\
  311. }\
  312. u->ssd = ssd;\
  313. u->step = STEP_INDEX;\
  314. u->sample2 = nodes[j]->sample1;\
  315. u->sample1 = dec_sample;\
  316. paths[u->path].nibble = nibble;\
  317. paths[u->path].prev = nodes[j]->path;\
  318. memmove(&nodes_next[k+1], &nodes_next[k], (frontier-k-1)*sizeof(TrellisNode*));\
  319. nodes_next[k] = u;\
  320. break;\
  321. }\
  322. }\
  323. next_##NAME:;
  324. STORE_NODE(ms, FFMAX(16, (AdaptationTable[nibble] * step) >> 8));
  325. }
  326. } else if((version == CODEC_ID_ADPCM_IMA_WAV)|| (version == CODEC_ID_ADPCM_SWF)) {
  327. #define LOOP_NODES(NAME, STEP_TABLE, STEP_INDEX)\
  328. const int predictor = nodes[j]->sample1;\
  329. const int div = (sample - predictor) * 4 / STEP_TABLE;\
  330. int nmin = av_clip(div-range, -7, 6);\
  331. int nmax = av_clip(div+range, -6, 7);\
  332. if(nmin<=0) nmin--; /* distinguish -0 from +0 */\
  333. if(nmax<0) nmax--;\
  334. for(nidx=nmin; nidx<=nmax; nidx++) {\
  335. const int nibble = nidx<0 ? 7-nidx : nidx;\
  336. int dec_sample = predictor + (STEP_TABLE * yamaha_difflookup[nibble]) / 8;\
  337. STORE_NODE(NAME, STEP_INDEX);\
  338. }
  339. LOOP_NODES(ima, step_table[step], av_clip(step + index_table[nibble], 0, 88));
  340. } else { //CODEC_ID_ADPCM_YAMAHA
  341. LOOP_NODES(yamaha, step, av_clip((step * yamaha_indexscale[nibble]) >> 8, 127, 24567));
  342. #undef LOOP_NODES
  343. #undef STORE_NODE
  344. }
  345. }
  346. u = nodes;
  347. nodes = nodes_next;
  348. nodes_next = u;
  349. // prevent overflow
  350. if(nodes[0]->ssd > (1<<28)) {
  351. for(j=1; j<frontier && nodes[j]; j++)
  352. nodes[j]->ssd -= nodes[0]->ssd;
  353. nodes[0]->ssd = 0;
  354. }
  355. // merge old paths to save memory
  356. if(i == froze + FREEZE_INTERVAL) {
  357. p = &paths[nodes[0]->path];
  358. for(k=i; k>froze; k--) {
  359. dst[k] = p->nibble;
  360. p = &paths[p->prev];
  361. }
  362. froze = i;
  363. pathn = 0;
  364. // other nodes might use paths that don't coincide with the frozen one.
  365. // checking which nodes do so is too slow, so just kill them all.
  366. // this also slightly improves quality, but I don't know why.
  367. memset(nodes+1, 0, (frontier-1)*sizeof(TrellisNode*));
  368. }
  369. }
  370. p = &paths[nodes[0]->path];
  371. for(i=n-1; i>froze; i--) {
  372. dst[i] = p->nibble;
  373. p = &paths[p->prev];
  374. }
  375. c->predictor = nodes[0]->sample1;
  376. c->sample1 = nodes[0]->sample1;
  377. c->sample2 = nodes[0]->sample2;
  378. c->step_index = nodes[0]->step;
  379. c->step = nodes[0]->step;
  380. c->idelta = nodes[0]->step;
  381. }
  382. static int adpcm_encode_frame(AVCodecContext *avctx,
  383. unsigned char *frame, int buf_size, void *data)
  384. {
  385. int n, i, st;
  386. short *samples;
  387. unsigned char *dst;
  388. ADPCMContext *c = avctx->priv_data;
  389. dst = frame;
  390. samples = (short *)data;
  391. st= avctx->channels == 2;
  392. /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
  393. switch(avctx->codec->id) {
  394. case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
  395. break;
  396. case CODEC_ID_ADPCM_IMA_WAV:
  397. n = avctx->frame_size / 8;
  398. c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
  399. /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
  400. bytestream_put_le16(&dst, c->status[0].prev_sample);
  401. *dst++ = (unsigned char)c->status[0].step_index;
  402. *dst++ = 0; /* unknown */
  403. samples++;
  404. if (avctx->channels == 2) {
  405. c->status[1].prev_sample = (signed short)samples[1];
  406. /* c->status[1].step_index = 0; */
  407. bytestream_put_le16(&dst, c->status[1].prev_sample);
  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_SWF:
  463. {
  464. int i;
  465. PutBitContext pb;
  466. init_put_bits(&pb, dst, buf_size*8);
  467. n = avctx->frame_size-1;
  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. if(avctx->trellis > 0) {
  478. uint8_t buf[2][n];
  479. adpcm_compress_trellis(avctx, samples+2, buf[0], &c->status[0], n);
  480. if (avctx->channels == 2)
  481. adpcm_compress_trellis(avctx, samples+3, buf[1], &c->status[1], n);
  482. for(i=0; i<n; i++) {
  483. put_bits(&pb, 4, buf[0][i]);
  484. if (avctx->channels == 2)
  485. put_bits(&pb, 4, buf[1][i]);
  486. }
  487. } else {
  488. for (i=1; i<avctx->frame_size; i++) {
  489. put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels*i]) & 0xF);
  490. if (avctx->channels == 2)
  491. put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1], samples[2*i+1]) & 0xF);
  492. }
  493. }
  494. flush_put_bits(&pb);
  495. dst += put_bits_count(&pb)>>3;
  496. break;
  497. }
  498. case CODEC_ID_ADPCM_MS:
  499. for(i=0; i<avctx->channels; i++){
  500. int predictor=0;
  501. *dst++ = predictor;
  502. c->status[i].coeff1 = AdaptCoeff1[predictor];
  503. c->status[i].coeff2 = AdaptCoeff2[predictor];
  504. }
  505. for(i=0; i<avctx->channels; i++){
  506. if (c->status[i].idelta < 16)
  507. c->status[i].idelta = 16;
  508. bytestream_put_le16(&dst, c->status[i].idelta);
  509. }
  510. for(i=0; i<avctx->channels; i++){
  511. c->status[i].sample1= *samples++;
  512. bytestream_put_le16(&dst, c->status[i].sample1);
  513. }
  514. for(i=0; i<avctx->channels; i++){
  515. c->status[i].sample2= *samples++;
  516. bytestream_put_le16(&dst, c->status[i].sample2);
  517. }
  518. if(avctx->trellis > 0) {
  519. int n = avctx->block_align - 7*avctx->channels;
  520. uint8_t buf[2][n];
  521. if(avctx->channels == 1) {
  522. n *= 2;
  523. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
  524. for(i=0; i<n; i+=2)
  525. *dst++ = (buf[0][i] << 4) | buf[0][i+1];
  526. } else {
  527. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
  528. adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n);
  529. for(i=0; i<n; i++)
  530. *dst++ = (buf[0][i] << 4) | buf[1][i];
  531. }
  532. } else
  533. for(i=7*avctx->channels; i<avctx->block_align; i++) {
  534. int nibble;
  535. nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
  536. nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
  537. *dst++ = nibble;
  538. }
  539. break;
  540. case CODEC_ID_ADPCM_YAMAHA:
  541. n = avctx->frame_size / 2;
  542. if(avctx->trellis > 0) {
  543. uint8_t buf[2][n*2];
  544. n *= 2;
  545. if(avctx->channels == 1) {
  546. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
  547. for(i=0; i<n; i+=2)
  548. *dst++ = buf[0][i] | (buf[0][i+1] << 4);
  549. } else {
  550. adpcm_compress_trellis(avctx, samples, buf[0], &c->status[0], n);
  551. adpcm_compress_trellis(avctx, samples+1, buf[1], &c->status[1], n);
  552. for(i=0; i<n; i++)
  553. *dst++ = buf[0][i] | (buf[1][i] << 4);
  554. }
  555. } else
  556. for (; n>0; n--) {
  557. for(i = 0; i < avctx->channels; i++) {
  558. int nibble;
  559. nibble = adpcm_yamaha_compress_sample(&c->status[i], samples[i]);
  560. nibble |= adpcm_yamaha_compress_sample(&c->status[i], samples[i+avctx->channels]) << 4;
  561. *dst++ = nibble;
  562. }
  563. samples += 2 * avctx->channels;
  564. }
  565. break;
  566. default:
  567. return -1;
  568. }
  569. return dst - frame;
  570. }
  571. #endif //CONFIG_ENCODERS
  572. static int adpcm_decode_init(AVCodecContext * avctx)
  573. {
  574. ADPCMContext *c = avctx->priv_data;
  575. if(avctx->channels > 2U){
  576. return -1;
  577. }
  578. c->channel = 0;
  579. c->status[0].predictor = c->status[1].predictor = 0;
  580. c->status[0].step_index = c->status[1].step_index = 0;
  581. c->status[0].step = c->status[1].step = 0;
  582. switch(avctx->codec->id) {
  583. case CODEC_ID_ADPCM_CT:
  584. c->status[0].step = c->status[1].step = 511;
  585. break;
  586. case CODEC_ID_ADPCM_IMA_WS:
  587. if (avctx->extradata && avctx->extradata_size == 2 * 4) {
  588. c->status[0].predictor = AV_RL32(avctx->extradata);
  589. c->status[1].predictor = AV_RL32(avctx->extradata + 4);
  590. }
  591. break;
  592. default:
  593. break;
  594. }
  595. return 0;
  596. }
  597. static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
  598. {
  599. int step_index;
  600. int predictor;
  601. int sign, delta, diff, step;
  602. step = step_table[c->step_index];
  603. step_index = c->step_index + index_table[(unsigned)nibble];
  604. if (step_index < 0) step_index = 0;
  605. else if (step_index > 88) step_index = 88;
  606. sign = nibble & 8;
  607. delta = nibble & 7;
  608. /* perform direct multiplication instead of series of jumps proposed by
  609. * the reference ADPCM implementation since modern CPUs can do the mults
  610. * quickly enough */
  611. diff = ((2 * delta + 1) * step) >> shift;
  612. predictor = c->predictor;
  613. if (sign) predictor -= diff;
  614. else predictor += diff;
  615. c->predictor = av_clip_int16(predictor);
  616. c->step_index = step_index;
  617. return (short)c->predictor;
  618. }
  619. static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
  620. {
  621. int predictor;
  622. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
  623. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  624. c->sample2 = c->sample1;
  625. c->sample1 = av_clip_int16(predictor);
  626. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
  627. if (c->idelta < 16) c->idelta = 16;
  628. return c->sample1;
  629. }
  630. static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
  631. {
  632. int sign, delta, diff;
  633. int new_step;
  634. sign = nibble & 8;
  635. delta = nibble & 7;
  636. /* perform direct multiplication instead of series of jumps proposed by
  637. * the reference ADPCM implementation since modern CPUs can do the mults
  638. * quickly enough */
  639. diff = ((2 * delta + 1) * c->step) >> 3;
  640. /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
  641. c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
  642. c->predictor = av_clip_int16(c->predictor);
  643. /* calculate new step and clamp it to range 511..32767 */
  644. new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
  645. c->step = av_clip(new_step, 511, 32767);
  646. return (short)c->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. /* clamp result */
  655. c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
  656. /* calculate new step */
  657. if (delta >= (2*size - 3) && c->step < 3)
  658. c->step++;
  659. else if (delta == 0 && c->step > 0)
  660. c->step--;
  661. return (short) c->predictor;
  662. }
  663. static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
  664. {
  665. if(!c->step) {
  666. c->predictor = 0;
  667. c->step = 127;
  668. }
  669. c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
  670. c->predictor = av_clip_int16(c->predictor);
  671. c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
  672. c->step = av_clip(c->step, 127, 24567);
  673. return c->predictor;
  674. }
  675. static void xa_decode(short *out, const unsigned char *in,
  676. ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
  677. {
  678. int i, j;
  679. int shift,filter,f0,f1;
  680. int s_1,s_2;
  681. int d,s,t;
  682. for(i=0;i<4;i++) {
  683. shift = 12 - (in[4+i*2] & 15);
  684. filter = in[4+i*2] >> 4;
  685. f0 = xa_adpcm_table[filter][0];
  686. f1 = xa_adpcm_table[filter][1];
  687. s_1 = left->sample1;
  688. s_2 = left->sample2;
  689. for(j=0;j<28;j++) {
  690. d = in[16+i+j*4];
  691. t = (signed char)(d<<4)>>4;
  692. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  693. s_2 = s_1;
  694. s_1 = av_clip_int16(s);
  695. *out = s_1;
  696. out += inc;
  697. }
  698. if (inc==2) { /* stereo */
  699. left->sample1 = s_1;
  700. left->sample2 = s_2;
  701. s_1 = right->sample1;
  702. s_2 = right->sample2;
  703. out = out + 1 - 28*2;
  704. }
  705. shift = 12 - (in[5+i*2] & 15);
  706. filter = in[5+i*2] >> 4;
  707. f0 = xa_adpcm_table[filter][0];
  708. f1 = xa_adpcm_table[filter][1];
  709. for(j=0;j<28;j++) {
  710. d = in[16+i+j*4];
  711. t = (signed char)d >> 4;
  712. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  713. s_2 = s_1;
  714. s_1 = av_clip_int16(s);
  715. *out = s_1;
  716. out += inc;
  717. }
  718. if (inc==2) { /* stereo */
  719. right->sample1 = s_1;
  720. right->sample2 = s_2;
  721. out -= 1;
  722. } else {
  723. left->sample1 = s_1;
  724. left->sample2 = s_2;
  725. }
  726. }
  727. }
  728. /* DK3 ADPCM support macro */
  729. #define DK3_GET_NEXT_NIBBLE() \
  730. if (decode_top_nibble_next) \
  731. { \
  732. nibble = (last_byte >> 4) & 0x0F; \
  733. decode_top_nibble_next = 0; \
  734. } \
  735. else \
  736. { \
  737. last_byte = *src++; \
  738. if (src >= buf + buf_size) break; \
  739. nibble = last_byte & 0x0F; \
  740. decode_top_nibble_next = 1; \
  741. }
  742. static int adpcm_decode_frame(AVCodecContext *avctx,
  743. void *data, int *data_size,
  744. uint8_t *buf, int buf_size)
  745. {
  746. ADPCMContext *c = avctx->priv_data;
  747. ADPCMChannelStatus *cs;
  748. int n, m, channel, i;
  749. int block_predictor[2];
  750. short *samples;
  751. short *samples_end;
  752. uint8_t *src;
  753. int st; /* stereo */
  754. /* DK3 ADPCM accounting variables */
  755. unsigned char last_byte = 0;
  756. unsigned char nibble;
  757. int decode_top_nibble_next = 0;
  758. int diff_channel;
  759. /* EA ADPCM state variables */
  760. uint32_t samples_in_chunk;
  761. int32_t previous_left_sample, previous_right_sample;
  762. int32_t current_left_sample, current_right_sample;
  763. int32_t next_left_sample, next_right_sample;
  764. int32_t coeff1l, coeff2l, coeff1r, coeff2r;
  765. uint8_t shift_left, shift_right;
  766. int count1, count2;
  767. if (!buf_size)
  768. return 0;
  769. //should protect all 4bit ADPCM variants
  770. //8 is needed for CODEC_ID_ADPCM_IMA_WAV with 2 channels
  771. //
  772. if(*data_size/4 < buf_size + 8)
  773. return -1;
  774. samples = data;
  775. samples_end= samples + *data_size/2;
  776. *data_size= 0;
  777. src = buf;
  778. st = avctx->channels == 2 ? 1 : 0;
  779. switch(avctx->codec->id) {
  780. case CODEC_ID_ADPCM_IMA_QT:
  781. n = (buf_size - 2);/* >> 2*avctx->channels;*/
  782. channel = c->channel;
  783. cs = &(c->status[channel]);
  784. /* (pppppp) (piiiiiii) */
  785. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  786. cs->predictor = (*src++) << 8;
  787. cs->predictor |= (*src & 0x80);
  788. cs->predictor &= 0xFF80;
  789. /* sign extension */
  790. if(cs->predictor & 0x8000)
  791. cs->predictor -= 0x10000;
  792. cs->predictor = av_clip_int16(cs->predictor);
  793. cs->step_index = (*src++) & 0x7F;
  794. if (cs->step_index > 88){
  795. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  796. cs->step_index = 88;
  797. }
  798. cs->step = step_table[cs->step_index];
  799. if (st && channel)
  800. samples++;
  801. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  802. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
  803. samples += avctx->channels;
  804. *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
  805. samples += avctx->channels;
  806. src ++;
  807. }
  808. if(st) { /* handle stereo interlacing */
  809. c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
  810. if(channel == 1) { /* wait for the other packet before outputing anything */
  811. return src - buf;
  812. }
  813. }
  814. break;
  815. case CODEC_ID_ADPCM_IMA_WAV:
  816. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  817. buf_size = avctx->block_align;
  818. // samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
  819. for(i=0; i<avctx->channels; i++){
  820. cs = &(c->status[i]);
  821. cs->predictor = (int16_t)(src[0] + (src[1]<<8));
  822. src+=2;
  823. // XXX: is this correct ??: *samples++ = cs->predictor;
  824. cs->step_index = *src++;
  825. if (cs->step_index > 88){
  826. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  827. cs->step_index = 88;
  828. }
  829. if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
  830. }
  831. while(src < buf + buf_size){
  832. for(m=0; m<4; m++){
  833. for(i=0; i<=st; i++)
  834. *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);
  835. for(i=0; i<=st; i++)
  836. *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4 , 3);
  837. src++;
  838. }
  839. src += 4*st;
  840. }
  841. break;
  842. case CODEC_ID_ADPCM_4XM:
  843. cs = &(c->status[0]);
  844. c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  845. if(st){
  846. c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  847. }
  848. c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  849. if(st){
  850. c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  851. }
  852. if (cs->step_index < 0) cs->step_index = 0;
  853. if (cs->step_index > 88) cs->step_index = 88;
  854. m= (buf_size - (src - buf))>>st;
  855. for(i=0; i<m; i++) {
  856. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
  857. if (st)
  858. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
  859. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
  860. if (st)
  861. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
  862. }
  863. src += m<<st;
  864. break;
  865. case CODEC_ID_ADPCM_MS:
  866. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  867. buf_size = avctx->block_align;
  868. n = buf_size - 7 * avctx->channels;
  869. if (n < 0)
  870. return -1;
  871. block_predictor[0] = av_clip(*src++, 0, 7);
  872. block_predictor[1] = 0;
  873. if (st)
  874. block_predictor[1] = av_clip(*src++, 0, 7);
  875. c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  876. src+=2;
  877. if (st){
  878. c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  879. src+=2;
  880. }
  881. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  882. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  883. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  884. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  885. c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  886. src+=2;
  887. if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  888. if (st) src+=2;
  889. c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  890. src+=2;
  891. if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  892. if (st) src+=2;
  893. *samples++ = c->status[0].sample1;
  894. if (st) *samples++ = c->status[1].sample1;
  895. *samples++ = c->status[0].sample2;
  896. if (st) *samples++ = c->status[1].sample2;
  897. for(;n>0;n--) {
  898. *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  899. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  900. src ++;
  901. }
  902. break;
  903. case CODEC_ID_ADPCM_IMA_DK4:
  904. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  905. buf_size = avctx->block_align;
  906. c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
  907. c->status[0].step_index = src[2];
  908. src += 4;
  909. *samples++ = c->status[0].predictor;
  910. if (st) {
  911. c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
  912. c->status[1].step_index = src[2];
  913. src += 4;
  914. *samples++ = c->status[1].predictor;
  915. }
  916. while (src < buf + buf_size) {
  917. /* take care of the top nibble (always left or mono channel) */
  918. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  919. (src[0] >> 4) & 0x0F, 3);
  920. /* take care of the bottom nibble, which is right sample for
  921. * stereo, or another mono sample */
  922. if (st)
  923. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  924. src[0] & 0x0F, 3);
  925. else
  926. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  927. src[0] & 0x0F, 3);
  928. src++;
  929. }
  930. break;
  931. case CODEC_ID_ADPCM_IMA_DK3:
  932. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  933. buf_size = avctx->block_align;
  934. if(buf_size + 16 > (samples_end - samples)*3/8)
  935. return -1;
  936. c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
  937. c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
  938. c->status[0].step_index = src[14];
  939. c->status[1].step_index = src[15];
  940. /* sign extend the predictors */
  941. src += 16;
  942. diff_channel = c->status[1].predictor;
  943. /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
  944. * the buffer is consumed */
  945. while (1) {
  946. /* for this algorithm, c->status[0] is the sum channel and
  947. * c->status[1] is the diff channel */
  948. /* process the first predictor of the sum channel */
  949. DK3_GET_NEXT_NIBBLE();
  950. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  951. /* process the diff channel predictor */
  952. DK3_GET_NEXT_NIBBLE();
  953. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  954. /* process the first pair of stereo PCM samples */
  955. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  956. *samples++ = c->status[0].predictor + c->status[1].predictor;
  957. *samples++ = c->status[0].predictor - c->status[1].predictor;
  958. /* process the second predictor of the sum channel */
  959. DK3_GET_NEXT_NIBBLE();
  960. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  961. /* process the second pair of stereo PCM samples */
  962. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  963. *samples++ = c->status[0].predictor + c->status[1].predictor;
  964. *samples++ = c->status[0].predictor - c->status[1].predictor;
  965. }
  966. break;
  967. case CODEC_ID_ADPCM_IMA_WS:
  968. /* no per-block initialization; just start decoding the data */
  969. while (src < buf + buf_size) {
  970. if (st) {
  971. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  972. (src[0] >> 4) & 0x0F, 3);
  973. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  974. src[0] & 0x0F, 3);
  975. } else {
  976. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  977. (src[0] >> 4) & 0x0F, 3);
  978. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  979. src[0] & 0x0F, 3);
  980. }
  981. src++;
  982. }
  983. break;
  984. case CODEC_ID_ADPCM_XA:
  985. c->status[0].sample1 = c->status[0].sample2 =
  986. c->status[1].sample1 = c->status[1].sample2 = 0;
  987. while (buf_size >= 128) {
  988. xa_decode(samples, src, &c->status[0], &c->status[1],
  989. avctx->channels);
  990. src += 128;
  991. samples += 28 * 8;
  992. buf_size -= 128;
  993. }
  994. break;
  995. case CODEC_ID_ADPCM_EA:
  996. samples_in_chunk = AV_RL32(src);
  997. if (samples_in_chunk >= ((buf_size - 12) * 2)) {
  998. src += buf_size;
  999. break;
  1000. }
  1001. src += 4;
  1002. current_left_sample = (int16_t)AV_RL16(src);
  1003. src += 2;
  1004. previous_left_sample = (int16_t)AV_RL16(src);
  1005. src += 2;
  1006. current_right_sample = (int16_t)AV_RL16(src);
  1007. src += 2;
  1008. previous_right_sample = (int16_t)AV_RL16(src);
  1009. src += 2;
  1010. for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
  1011. coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
  1012. coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
  1013. coeff1r = ea_adpcm_table[*src & 0x0F];
  1014. coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
  1015. src++;
  1016. shift_left = ((*src >> 4) & 0x0F) + 8;
  1017. shift_right = (*src & 0x0F) + 8;
  1018. src++;
  1019. for (count2 = 0; count2 < 28; count2++) {
  1020. next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
  1021. next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
  1022. src++;
  1023. next_left_sample = (next_left_sample +
  1024. (current_left_sample * coeff1l) +
  1025. (previous_left_sample * coeff2l) + 0x80) >> 8;
  1026. next_right_sample = (next_right_sample +
  1027. (current_right_sample * coeff1r) +
  1028. (previous_right_sample * coeff2r) + 0x80) >> 8;
  1029. previous_left_sample = current_left_sample;
  1030. current_left_sample = av_clip_int16(next_left_sample);
  1031. previous_right_sample = current_right_sample;
  1032. current_right_sample = av_clip_int16(next_right_sample);
  1033. *samples++ = (unsigned short)current_left_sample;
  1034. *samples++ = (unsigned short)current_right_sample;
  1035. }
  1036. }
  1037. break;
  1038. case CODEC_ID_ADPCM_IMA_SMJPEG:
  1039. c->status[0].predictor = *src;
  1040. src += 2;
  1041. c->status[0].step_index = *src++;
  1042. src++; /* skip another byte before getting to the meat */
  1043. while (src < buf + buf_size) {
  1044. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1045. *src & 0x0F, 3);
  1046. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1047. (*src >> 4) & 0x0F, 3);
  1048. src++;
  1049. }
  1050. break;
  1051. case CODEC_ID_ADPCM_CT:
  1052. while (src < buf + buf_size) {
  1053. if (st) {
  1054. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1055. (src[0] >> 4) & 0x0F);
  1056. *samples++ = adpcm_ct_expand_nibble(&c->status[1],
  1057. src[0] & 0x0F);
  1058. } else {
  1059. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1060. (src[0] >> 4) & 0x0F);
  1061. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1062. src[0] & 0x0F);
  1063. }
  1064. src++;
  1065. }
  1066. break;
  1067. case CODEC_ID_ADPCM_SBPRO_4:
  1068. case CODEC_ID_ADPCM_SBPRO_3:
  1069. case CODEC_ID_ADPCM_SBPRO_2:
  1070. if (!c->status[0].step_index) {
  1071. /* the first byte is a raw sample */
  1072. *samples++ = 128 * (*src++ - 0x80);
  1073. if (st)
  1074. *samples++ = 128 * (*src++ - 0x80);
  1075. c->status[0].step_index = 1;
  1076. }
  1077. if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
  1078. while (src < buf + buf_size) {
  1079. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1080. (src[0] >> 4) & 0x0F, 4, 0);
  1081. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1082. src[0] & 0x0F, 4, 0);
  1083. src++;
  1084. }
  1085. } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
  1086. while (src < buf + buf_size && samples + 2 < samples_end) {
  1087. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1088. (src[0] >> 5) & 0x07, 3, 0);
  1089. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1090. (src[0] >> 2) & 0x07, 3, 0);
  1091. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1092. src[0] & 0x03, 2, 0);
  1093. src++;
  1094. }
  1095. } else {
  1096. while (src < buf + buf_size && samples + 3 < samples_end) {
  1097. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1098. (src[0] >> 6) & 0x03, 2, 2);
  1099. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1100. (src[0] >> 4) & 0x03, 2, 2);
  1101. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1102. (src[0] >> 2) & 0x03, 2, 2);
  1103. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1104. src[0] & 0x03, 2, 2);
  1105. src++;
  1106. }
  1107. }
  1108. break;
  1109. case CODEC_ID_ADPCM_SWF:
  1110. {
  1111. GetBitContext gb;
  1112. const int *table;
  1113. int k0, signmask, nb_bits, count;
  1114. int size = buf_size*8;
  1115. init_get_bits(&gb, buf, size);
  1116. //read bits & initial values
  1117. nb_bits = get_bits(&gb, 2)+2;
  1118. //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
  1119. table = swf_index_tables[nb_bits-2];
  1120. k0 = 1 << (nb_bits-2);
  1121. signmask = 1 << (nb_bits-1);
  1122. while (get_bits_count(&gb) <= size - 22*avctx->channels) {
  1123. for (i = 0; i < avctx->channels; i++) {
  1124. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  1125. c->status[i].step_index = get_bits(&gb, 6);
  1126. }
  1127. for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
  1128. int i;
  1129. for (i = 0; i < avctx->channels; i++) {
  1130. // similar to IMA adpcm
  1131. int delta = get_bits(&gb, nb_bits);
  1132. int step = step_table[c->status[i].step_index];
  1133. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  1134. int k = k0;
  1135. do {
  1136. if (delta & k)
  1137. vpdiff += step;
  1138. step >>= 1;
  1139. k >>= 1;
  1140. } while(k);
  1141. vpdiff += step;
  1142. if (delta & signmask)
  1143. c->status[i].predictor -= vpdiff;
  1144. else
  1145. c->status[i].predictor += vpdiff;
  1146. c->status[i].step_index += table[delta & (~signmask)];
  1147. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  1148. c->status[i].predictor = av_clip_int16(c->status[i].predictor);
  1149. *samples++ = c->status[i].predictor;
  1150. if (samples >= samples_end) {
  1151. av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
  1152. return -1;
  1153. }
  1154. }
  1155. }
  1156. }
  1157. src += buf_size;
  1158. break;
  1159. }
  1160. case CODEC_ID_ADPCM_YAMAHA:
  1161. while (src < buf + buf_size) {
  1162. if (st) {
  1163. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1164. src[0] & 0x0F);
  1165. *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
  1166. (src[0] >> 4) & 0x0F);
  1167. } else {
  1168. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1169. src[0] & 0x0F);
  1170. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1171. (src[0] >> 4) & 0x0F);
  1172. }
  1173. src++;
  1174. }
  1175. break;
  1176. case CODEC_ID_ADPCM_THP:
  1177. {
  1178. int table[2][16];
  1179. unsigned int samplecnt;
  1180. int prev[2][2];
  1181. int ch;
  1182. if (buf_size < 80) {
  1183. av_log(avctx, AV_LOG_ERROR, "frame too small\n");
  1184. return -1;
  1185. }
  1186. src+=4;
  1187. samplecnt = bytestream_get_be32(&src);
  1188. for (i = 0; i < 32; i++)
  1189. table[0][i] = (int16_t)bytestream_get_be16(&src);
  1190. /* Initialize the previous sample. */
  1191. for (i = 0; i < 4; i++)
  1192. prev[0][i] = (int16_t)bytestream_get_be16(&src);
  1193. if (samplecnt >= (samples_end - samples) / (st + 1)) {
  1194. av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
  1195. return -1;
  1196. }
  1197. for (ch = 0; ch <= st; ch++) {
  1198. samples = (unsigned short *) data + ch;
  1199. /* Read in every sample for this channel. */
  1200. for (i = 0; i < samplecnt / 14; i++) {
  1201. int index = (*src >> 4) & 7;
  1202. unsigned int exp = 28 - (*src++ & 15);
  1203. int factor1 = table[ch][index * 2];
  1204. int factor2 = table[ch][index * 2 + 1];
  1205. /* Decode 14 samples. */
  1206. for (n = 0; n < 14; n++) {
  1207. int32_t sampledat;
  1208. if(n&1) sampledat= *src++ <<28;
  1209. else sampledat= (*src&0xF0)<<24;
  1210. sampledat = ((prev[ch][0]*factor1
  1211. + prev[ch][1]*factor2) >> 11) + (sampledat>>exp);
  1212. *samples = av_clip_int16(sampledat);
  1213. prev[ch][1] = prev[ch][0];
  1214. prev[ch][0] = *samples++;
  1215. /* In case of stereo, skip one sample, this sample
  1216. is for the other channel. */
  1217. samples += st;
  1218. }
  1219. }
  1220. }
  1221. /* In the previous loop, in case stereo is used, samples is
  1222. increased exactly one time too often. */
  1223. samples -= st;
  1224. break;
  1225. }
  1226. default:
  1227. return -1;
  1228. }
  1229. *data_size = (uint8_t *)samples - (uint8_t *)data;
  1230. return src - buf;
  1231. }
  1232. #ifdef CONFIG_ENCODERS
  1233. #define ADPCM_ENCODER(id,name) \
  1234. AVCodec name ## _encoder = { \
  1235. #name, \
  1236. CODEC_TYPE_AUDIO, \
  1237. id, \
  1238. sizeof(ADPCMContext), \
  1239. adpcm_encode_init, \
  1240. adpcm_encode_frame, \
  1241. adpcm_encode_close, \
  1242. NULL, \
  1243. };
  1244. #else
  1245. #define ADPCM_ENCODER(id,name)
  1246. #endif
  1247. #ifdef CONFIG_DECODERS
  1248. #define ADPCM_DECODER(id,name) \
  1249. AVCodec name ## _decoder = { \
  1250. #name, \
  1251. CODEC_TYPE_AUDIO, \
  1252. id, \
  1253. sizeof(ADPCMContext), \
  1254. adpcm_decode_init, \
  1255. NULL, \
  1256. NULL, \
  1257. adpcm_decode_frame, \
  1258. };
  1259. #else
  1260. #define ADPCM_DECODER(id,name)
  1261. #endif
  1262. #define ADPCM_CODEC(id, name) \
  1263. ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
  1264. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
  1265. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
  1266. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
  1267. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
  1268. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
  1269. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
  1270. ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
  1271. ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
  1272. ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
  1273. ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
  1274. ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
  1275. ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);
  1276. ADPCM_CODEC(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);
  1277. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4);
  1278. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3);
  1279. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2);
  1280. ADPCM_CODEC(CODEC_ID_ADPCM_THP, adpcm_thp);
  1281. #undef ADPCM_CODEC