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.

1555 lines
55KB

  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. * EA ADPCM R1/R2/R3 decoder by Peter Ross (pross@xvid.org)
  33. * THP ADPCM decoder by Marco Gerards (mgerards@xs4all.nl)
  34. *
  35. * Features and limitations:
  36. *
  37. * Reference documents:
  38. * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
  39. * http://www.geocities.com/SiliconValley/8682/aud3.txt
  40. * http://openquicktime.sourceforge.net/plugins.htm
  41. * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
  42. * http://www.cs.ucla.edu/~leec/mediabench/applications.html
  43. * SoX source code http://home.sprynet.com/~cbagwell/sox.html
  44. *
  45. * CD-ROM XA:
  46. * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html
  47. * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html
  48. * readstr http://www.geocities.co.jp/Playtown/2004/
  49. */
  50. #define BLKSIZE 1024
  51. /* step_table[] and index_table[] are from the ADPCM reference source */
  52. /* This is the index table: */
  53. static const int index_table[16] = {
  54. -1, -1, -1, -1, 2, 4, 6, 8,
  55. -1, -1, -1, -1, 2, 4, 6, 8,
  56. };
  57. /**
  58. * This is the step table. Note that many programs use slight deviations from
  59. * this table, but such deviations are negligible:
  60. */
  61. static const int step_table[89] = {
  62. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  63. 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  64. 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  65. 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  66. 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  67. 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  68. 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  69. 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  70. 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  71. };
  72. /* These are for MS-ADPCM */
  73. /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
  74. static const int AdaptationTable[] = {
  75. 230, 230, 230, 230, 307, 409, 512, 614,
  76. 768, 614, 512, 409, 307, 230, 230, 230
  77. };
  78. static const int AdaptCoeff1[] = {
  79. 256, 512, 0, 192, 240, 460, 392
  80. };
  81. static const int AdaptCoeff2[] = {
  82. 0, -256, 0, 64, 0, -208, -232
  83. };
  84. /* These are for CD-ROM XA ADPCM */
  85. static const int xa_adpcm_table[5][2] = {
  86. { 0, 0 },
  87. { 60, 0 },
  88. { 115, -52 },
  89. { 98, -55 },
  90. { 122, -60 }
  91. };
  92. static const int ea_adpcm_table[] = {
  93. 0, 240, 460, 392, 0, 0, -208, -220, 0, 1,
  94. 3, 4, 7, 8, 10, 11, 0, -1, -3, -4
  95. };
  96. static const int ct_adpcm_table[8] = {
  97. 0x00E6, 0x00E6, 0x00E6, 0x00E6,
  98. 0x0133, 0x0199, 0x0200, 0x0266
  99. };
  100. // padded to zero where table size is less then 16
  101. static const int swf_index_tables[4][16] = {
  102. /*2*/ { -1, 2 },
  103. /*3*/ { -1, -1, 2, 4 },
  104. /*4*/ { -1, -1, -1, -1, 2, 4, 6, 8 },
  105. /*5*/ { -1, -1, -1, -1, -1, -1, -1, -1, 1, 2, 4, 6, 8, 10, 13, 16 }
  106. };
  107. static const int yamaha_indexscale[] = {
  108. 230, 230, 230, 230, 307, 409, 512, 614,
  109. 230, 230, 230, 230, 307, 409, 512, 614
  110. };
  111. static const int yamaha_difflookup[] = {
  112. 1, 3, 5, 7, 9, 11, 13, 15,
  113. -1, -3, -5, -7, -9, -11, -13, -15
  114. };
  115. /* end of tables */
  116. typedef struct ADPCMChannelStatus {
  117. int predictor;
  118. short int step_index;
  119. int step;
  120. /* for encoding */
  121. int prev_sample;
  122. /* MS version */
  123. short sample1;
  124. short sample2;
  125. int coeff1;
  126. int coeff2;
  127. int idelta;
  128. } ADPCMChannelStatus;
  129. typedef struct ADPCMContext {
  130. int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
  131. ADPCMChannelStatus status[6];
  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]);
  433. *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4;
  434. dst++;
  435. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]);
  436. *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4;
  437. dst++;
  438. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]);
  439. *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4;
  440. dst++;
  441. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]);
  442. *dst |= adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4;
  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]));
  490. if (avctx->channels == 2)
  491. put_bits(&pb, 4, adpcm_ima_compress_sample(&c->status[1], samples[2*i+1]));
  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. unsigned int max_channels = 2;
  576. switch(avctx->codec->id) {
  577. case CODEC_ID_ADPCM_EA_R1:
  578. case CODEC_ID_ADPCM_EA_R2:
  579. case CODEC_ID_ADPCM_EA_R3:
  580. max_channels = 6;
  581. break;
  582. }
  583. if(avctx->channels > max_channels){
  584. return -1;
  585. }
  586. switch(avctx->codec->id) {
  587. case CODEC_ID_ADPCM_CT:
  588. c->status[0].step = c->status[1].step = 511;
  589. break;
  590. case CODEC_ID_ADPCM_IMA_WS:
  591. if (avctx->extradata && avctx->extradata_size == 2 * 4) {
  592. c->status[0].predictor = AV_RL32(avctx->extradata);
  593. c->status[1].predictor = AV_RL32(avctx->extradata + 4);
  594. }
  595. break;
  596. default:
  597. break;
  598. }
  599. return 0;
  600. }
  601. static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
  602. {
  603. int step_index;
  604. int predictor;
  605. int sign, delta, diff, step;
  606. step = step_table[c->step_index];
  607. step_index = c->step_index + index_table[(unsigned)nibble];
  608. if (step_index < 0) step_index = 0;
  609. else if (step_index > 88) step_index = 88;
  610. sign = nibble & 8;
  611. delta = nibble & 7;
  612. /* perform direct multiplication instead of series of jumps proposed by
  613. * the reference ADPCM implementation since modern CPUs can do the mults
  614. * quickly enough */
  615. diff = ((2 * delta + 1) * step) >> shift;
  616. predictor = c->predictor;
  617. if (sign) predictor -= diff;
  618. else predictor += diff;
  619. c->predictor = av_clip_int16(predictor);
  620. c->step_index = step_index;
  621. return (short)c->predictor;
  622. }
  623. static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
  624. {
  625. int predictor;
  626. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
  627. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  628. c->sample2 = c->sample1;
  629. c->sample1 = av_clip_int16(predictor);
  630. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
  631. if (c->idelta < 16) c->idelta = 16;
  632. return c->sample1;
  633. }
  634. static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
  635. {
  636. int sign, delta, diff;
  637. int new_step;
  638. sign = nibble & 8;
  639. delta = nibble & 7;
  640. /* perform direct multiplication instead of series of jumps proposed by
  641. * the reference ADPCM implementation since modern CPUs can do the mults
  642. * quickly enough */
  643. diff = ((2 * delta + 1) * c->step) >> 3;
  644. /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
  645. c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);
  646. c->predictor = av_clip_int16(c->predictor);
  647. /* calculate new step and clamp it to range 511..32767 */
  648. new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
  649. c->step = av_clip(new_step, 511, 32767);
  650. return (short)c->predictor;
  651. }
  652. static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
  653. {
  654. int sign, delta, diff;
  655. sign = nibble & (1<<(size-1));
  656. delta = nibble & ((1<<(size-1))-1);
  657. diff = delta << (7 + c->step + shift);
  658. /* clamp result */
  659. c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);
  660. /* calculate new step */
  661. if (delta >= (2*size - 3) && c->step < 3)
  662. c->step++;
  663. else if (delta == 0 && c->step > 0)
  664. c->step--;
  665. return (short) c->predictor;
  666. }
  667. static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
  668. {
  669. if(!c->step) {
  670. c->predictor = 0;
  671. c->step = 127;
  672. }
  673. c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
  674. c->predictor = av_clip_int16(c->predictor);
  675. c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
  676. c->step = av_clip(c->step, 127, 24567);
  677. return c->predictor;
  678. }
  679. static void xa_decode(short *out, const unsigned char *in,
  680. ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
  681. {
  682. int i, j;
  683. int shift,filter,f0,f1;
  684. int s_1,s_2;
  685. int d,s,t;
  686. for(i=0;i<4;i++) {
  687. shift = 12 - (in[4+i*2] & 15);
  688. filter = in[4+i*2] >> 4;
  689. f0 = xa_adpcm_table[filter][0];
  690. f1 = xa_adpcm_table[filter][1];
  691. s_1 = left->sample1;
  692. s_2 = left->sample2;
  693. for(j=0;j<28;j++) {
  694. d = in[16+i+j*4];
  695. t = (signed char)(d<<4)>>4;
  696. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  697. s_2 = s_1;
  698. s_1 = av_clip_int16(s);
  699. *out = s_1;
  700. out += inc;
  701. }
  702. if (inc==2) { /* stereo */
  703. left->sample1 = s_1;
  704. left->sample2 = s_2;
  705. s_1 = right->sample1;
  706. s_2 = right->sample2;
  707. out = out + 1 - 28*2;
  708. }
  709. shift = 12 - (in[5+i*2] & 15);
  710. filter = in[5+i*2] >> 4;
  711. f0 = xa_adpcm_table[filter][0];
  712. f1 = xa_adpcm_table[filter][1];
  713. for(j=0;j<28;j++) {
  714. d = in[16+i+j*4];
  715. t = (signed char)d >> 4;
  716. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  717. s_2 = s_1;
  718. s_1 = av_clip_int16(s);
  719. *out = s_1;
  720. out += inc;
  721. }
  722. if (inc==2) { /* stereo */
  723. right->sample1 = s_1;
  724. right->sample2 = s_2;
  725. out -= 1;
  726. } else {
  727. left->sample1 = s_1;
  728. left->sample2 = s_2;
  729. }
  730. }
  731. }
  732. /* DK3 ADPCM support macro */
  733. #define DK3_GET_NEXT_NIBBLE() \
  734. if (decode_top_nibble_next) \
  735. { \
  736. nibble = (last_byte >> 4) & 0x0F; \
  737. decode_top_nibble_next = 0; \
  738. } \
  739. else \
  740. { \
  741. last_byte = *src++; \
  742. if (src >= buf + buf_size) break; \
  743. nibble = last_byte & 0x0F; \
  744. decode_top_nibble_next = 1; \
  745. }
  746. static int adpcm_decode_frame(AVCodecContext *avctx,
  747. void *data, int *data_size,
  748. uint8_t *buf, int buf_size)
  749. {
  750. ADPCMContext *c = avctx->priv_data;
  751. ADPCMChannelStatus *cs;
  752. int n, m, channel, i;
  753. int block_predictor[2];
  754. short *samples;
  755. short *samples_end;
  756. uint8_t *src;
  757. int st; /* stereo */
  758. /* DK3 ADPCM accounting variables */
  759. unsigned char last_byte = 0;
  760. unsigned char nibble;
  761. int decode_top_nibble_next = 0;
  762. int diff_channel;
  763. /* EA ADPCM state variables */
  764. uint32_t samples_in_chunk;
  765. int32_t previous_left_sample, previous_right_sample;
  766. int32_t current_left_sample, current_right_sample;
  767. int32_t next_left_sample, next_right_sample;
  768. int32_t coeff1l, coeff2l, coeff1r, coeff2r;
  769. uint8_t shift_left, shift_right;
  770. int count1, count2;
  771. if (!buf_size)
  772. return 0;
  773. //should protect all 4bit ADPCM variants
  774. //8 is needed for CODEC_ID_ADPCM_IMA_WAV with 2 channels
  775. //
  776. if(*data_size/4 < buf_size + 8)
  777. return -1;
  778. samples = data;
  779. samples_end= samples + *data_size/2;
  780. *data_size= 0;
  781. src = buf;
  782. st = avctx->channels == 2 ? 1 : 0;
  783. switch(avctx->codec->id) {
  784. case CODEC_ID_ADPCM_IMA_QT:
  785. n = (buf_size - 2);/* >> 2*avctx->channels;*/
  786. channel = c->channel;
  787. cs = &(c->status[channel]);
  788. /* (pppppp) (piiiiiii) */
  789. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  790. cs->predictor = (*src++) << 8;
  791. cs->predictor |= (*src & 0x80);
  792. cs->predictor &= 0xFF80;
  793. /* sign extension */
  794. if(cs->predictor & 0x8000)
  795. cs->predictor -= 0x10000;
  796. cs->predictor = av_clip_int16(cs->predictor);
  797. cs->step_index = (*src++) & 0x7F;
  798. if (cs->step_index > 88){
  799. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  800. cs->step_index = 88;
  801. }
  802. cs->step = step_table[cs->step_index];
  803. if (st && channel)
  804. samples++;
  805. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  806. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
  807. samples += avctx->channels;
  808. *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
  809. samples += avctx->channels;
  810. src ++;
  811. }
  812. if(st) { /* handle stereo interlacing */
  813. c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
  814. if(channel == 1) { /* wait for the other packet before outputing anything */
  815. return src - buf;
  816. }
  817. }
  818. break;
  819. case CODEC_ID_ADPCM_IMA_WAV:
  820. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  821. buf_size = avctx->block_align;
  822. // samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
  823. for(i=0; i<avctx->channels; i++){
  824. cs = &(c->status[i]);
  825. cs->predictor = (int16_t)(src[0] + (src[1]<<8));
  826. src+=2;
  827. // XXX: is this correct ??: *samples++ = cs->predictor;
  828. cs->step_index = *src++;
  829. if (cs->step_index > 88){
  830. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  831. cs->step_index = 88;
  832. }
  833. if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
  834. }
  835. while(src < buf + buf_size){
  836. for(m=0; m<4; m++){
  837. for(i=0; i<=st; i++)
  838. *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);
  839. for(i=0; i<=st; i++)
  840. *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4 , 3);
  841. src++;
  842. }
  843. src += 4*st;
  844. }
  845. break;
  846. case CODEC_ID_ADPCM_4XM:
  847. cs = &(c->status[0]);
  848. c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  849. if(st){
  850. c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  851. }
  852. c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  853. if(st){
  854. c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  855. }
  856. if (cs->step_index < 0) cs->step_index = 0;
  857. if (cs->step_index > 88) cs->step_index = 88;
  858. m= (buf_size - (src - buf))>>st;
  859. for(i=0; i<m; i++) {
  860. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
  861. if (st)
  862. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
  863. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
  864. if (st)
  865. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
  866. }
  867. src += m<<st;
  868. break;
  869. case CODEC_ID_ADPCM_MS:
  870. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  871. buf_size = avctx->block_align;
  872. n = buf_size - 7 * avctx->channels;
  873. if (n < 0)
  874. return -1;
  875. block_predictor[0] = av_clip(*src++, 0, 7);
  876. block_predictor[1] = 0;
  877. if (st)
  878. block_predictor[1] = av_clip(*src++, 0, 7);
  879. c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  880. src+=2;
  881. if (st){
  882. c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  883. src+=2;
  884. }
  885. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  886. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  887. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  888. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  889. c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  890. src+=2;
  891. if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  892. if (st) src+=2;
  893. c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  894. src+=2;
  895. if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  896. if (st) src+=2;
  897. *samples++ = c->status[0].sample1;
  898. if (st) *samples++ = c->status[1].sample1;
  899. *samples++ = c->status[0].sample2;
  900. if (st) *samples++ = c->status[1].sample2;
  901. for(;n>0;n--) {
  902. *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  903. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  904. src ++;
  905. }
  906. break;
  907. case CODEC_ID_ADPCM_IMA_DK4:
  908. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  909. buf_size = avctx->block_align;
  910. c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
  911. c->status[0].step_index = src[2];
  912. src += 4;
  913. *samples++ = c->status[0].predictor;
  914. if (st) {
  915. c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
  916. c->status[1].step_index = src[2];
  917. src += 4;
  918. *samples++ = c->status[1].predictor;
  919. }
  920. while (src < buf + buf_size) {
  921. /* take care of the top nibble (always left or mono channel) */
  922. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  923. (src[0] >> 4) & 0x0F, 3);
  924. /* take care of the bottom nibble, which is right sample for
  925. * stereo, or another mono sample */
  926. if (st)
  927. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  928. src[0] & 0x0F, 3);
  929. else
  930. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  931. src[0] & 0x0F, 3);
  932. src++;
  933. }
  934. break;
  935. case CODEC_ID_ADPCM_IMA_DK3:
  936. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  937. buf_size = avctx->block_align;
  938. if(buf_size + 16 > (samples_end - samples)*3/8)
  939. return -1;
  940. c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
  941. c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
  942. c->status[0].step_index = src[14];
  943. c->status[1].step_index = src[15];
  944. /* sign extend the predictors */
  945. src += 16;
  946. diff_channel = c->status[1].predictor;
  947. /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
  948. * the buffer is consumed */
  949. while (1) {
  950. /* for this algorithm, c->status[0] is the sum channel and
  951. * c->status[1] is the diff channel */
  952. /* process the first predictor of the sum channel */
  953. DK3_GET_NEXT_NIBBLE();
  954. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  955. /* process the diff channel predictor */
  956. DK3_GET_NEXT_NIBBLE();
  957. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  958. /* process the first pair of stereo PCM samples */
  959. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  960. *samples++ = c->status[0].predictor + c->status[1].predictor;
  961. *samples++ = c->status[0].predictor - c->status[1].predictor;
  962. /* process the second predictor of the sum channel */
  963. DK3_GET_NEXT_NIBBLE();
  964. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  965. /* process the second pair of stereo PCM samples */
  966. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  967. *samples++ = c->status[0].predictor + c->status[1].predictor;
  968. *samples++ = c->status[0].predictor - c->status[1].predictor;
  969. }
  970. break;
  971. case CODEC_ID_ADPCM_IMA_WS:
  972. /* no per-block initialization; just start decoding the data */
  973. while (src < buf + buf_size) {
  974. if (st) {
  975. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  976. (src[0] >> 4) & 0x0F, 3);
  977. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  978. src[0] & 0x0F, 3);
  979. } else {
  980. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  981. (src[0] >> 4) & 0x0F, 3);
  982. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  983. src[0] & 0x0F, 3);
  984. }
  985. src++;
  986. }
  987. break;
  988. case CODEC_ID_ADPCM_XA:
  989. c->status[0].sample1 = c->status[0].sample2 =
  990. c->status[1].sample1 = c->status[1].sample2 = 0;
  991. while (buf_size >= 128) {
  992. xa_decode(samples, src, &c->status[0], &c->status[1],
  993. avctx->channels);
  994. src += 128;
  995. samples += 28 * 8;
  996. buf_size -= 128;
  997. }
  998. break;
  999. case CODEC_ID_ADPCM_EA:
  1000. samples_in_chunk = AV_RL32(src);
  1001. if (samples_in_chunk >= ((buf_size - 12) * 2)) {
  1002. src += buf_size;
  1003. break;
  1004. }
  1005. src += 4;
  1006. current_left_sample = (int16_t)AV_RL16(src);
  1007. src += 2;
  1008. previous_left_sample = (int16_t)AV_RL16(src);
  1009. src += 2;
  1010. current_right_sample = (int16_t)AV_RL16(src);
  1011. src += 2;
  1012. previous_right_sample = (int16_t)AV_RL16(src);
  1013. src += 2;
  1014. for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
  1015. coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
  1016. coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
  1017. coeff1r = ea_adpcm_table[*src & 0x0F];
  1018. coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
  1019. src++;
  1020. shift_left = ((*src >> 4) & 0x0F) + 8;
  1021. shift_right = (*src & 0x0F) + 8;
  1022. src++;
  1023. for (count2 = 0; count2 < 28; count2++) {
  1024. next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
  1025. next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
  1026. src++;
  1027. next_left_sample = (next_left_sample +
  1028. (current_left_sample * coeff1l) +
  1029. (previous_left_sample * coeff2l) + 0x80) >> 8;
  1030. next_right_sample = (next_right_sample +
  1031. (current_right_sample * coeff1r) +
  1032. (previous_right_sample * coeff2r) + 0x80) >> 8;
  1033. previous_left_sample = current_left_sample;
  1034. current_left_sample = av_clip_int16(next_left_sample);
  1035. previous_right_sample = current_right_sample;
  1036. current_right_sample = av_clip_int16(next_right_sample);
  1037. *samples++ = (unsigned short)current_left_sample;
  1038. *samples++ = (unsigned short)current_right_sample;
  1039. }
  1040. }
  1041. break;
  1042. case CODEC_ID_ADPCM_EA_R1:
  1043. case CODEC_ID_ADPCM_EA_R2:
  1044. case CODEC_ID_ADPCM_EA_R3: {
  1045. /* channel numbering
  1046. 2chan: 0=fl, 1=fr
  1047. 4chan: 0=fl, 1=rl, 2=fr, 3=rr
  1048. 6chan: 0=fl, 1=c, 2=fr, 3=rl, 4=rr, 5=sub */
  1049. const int big_endian = avctx->codec->id == CODEC_ID_ADPCM_EA_R3;
  1050. int32_t previous_sample, current_sample, next_sample;
  1051. int32_t coeff1, coeff2;
  1052. uint8_t shift;
  1053. unsigned int channel;
  1054. uint16_t *samplesC;
  1055. uint8_t *srcC;
  1056. samples_in_chunk = (big_endian ? bytestream_get_be32(&src)
  1057. : bytestream_get_le32(&src)) / 28;
  1058. if (samples_in_chunk > UINT32_MAX/(28*avctx->channels) ||
  1059. 28*samples_in_chunk*avctx->channels > samples_end-samples) {
  1060. src += buf_size - 4;
  1061. break;
  1062. }
  1063. for (channel=0; channel<avctx->channels; channel++) {
  1064. srcC = src + (big_endian ? bytestream_get_be32(&src)
  1065. : bytestream_get_le32(&src))
  1066. + (avctx->channels-channel-1) * 4;
  1067. samplesC = samples + channel;
  1068. if (avctx->codec->id == CODEC_ID_ADPCM_EA_R1) {
  1069. current_sample = (int16_t)bytestream_get_le16(&srcC);
  1070. previous_sample = (int16_t)bytestream_get_le16(&srcC);
  1071. } else {
  1072. current_sample = c->status[channel].predictor;
  1073. previous_sample = c->status[channel].prev_sample;
  1074. }
  1075. for (count1=0; count1<samples_in_chunk; count1++) {
  1076. if (*srcC == 0xEE) { /* only seen in R2 and R3 */
  1077. srcC++;
  1078. current_sample = (int16_t)bytestream_get_be16(&srcC);
  1079. previous_sample = (int16_t)bytestream_get_be16(&srcC);
  1080. for (count2=0; count2<28; count2++) {
  1081. *samplesC = (int16_t)bytestream_get_be16(&srcC);
  1082. samplesC += avctx->channels;
  1083. }
  1084. } else {
  1085. coeff1 = ea_adpcm_table[ (*srcC>>4) & 0x0F ];
  1086. coeff2 = ea_adpcm_table[((*srcC>>4) & 0x0F) + 4];
  1087. shift = (*srcC++ & 0x0F) + 8;
  1088. for (count2=0; count2<28; count2++) {
  1089. if (count2 & 1)
  1090. next_sample = ((*srcC++ & 0x0F) << 28) >> shift;
  1091. else
  1092. next_sample = ((*srcC & 0xF0) << 24) >> shift;
  1093. next_sample += (current_sample * coeff1) +
  1094. (previous_sample * coeff2);
  1095. next_sample = av_clip_int16(next_sample >> 8);
  1096. previous_sample = current_sample;
  1097. current_sample = next_sample;
  1098. *samplesC = current_sample;
  1099. samplesC += avctx->channels;
  1100. }
  1101. }
  1102. }
  1103. if (avctx->codec->id != CODEC_ID_ADPCM_EA_R1) {
  1104. c->status[channel].predictor = current_sample;
  1105. c->status[channel].prev_sample = previous_sample;
  1106. }
  1107. }
  1108. src = src + buf_size - (4 + 4*avctx->channels);
  1109. samples += 28 * samples_in_chunk * avctx->channels;
  1110. break;
  1111. }
  1112. case CODEC_ID_ADPCM_IMA_AMV:
  1113. case CODEC_ID_ADPCM_IMA_SMJPEG:
  1114. c->status[0].predictor = (int16_t)bytestream_get_le16(&src);
  1115. c->status[0].step_index = bytestream_get_le16(&src);
  1116. if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
  1117. src+=4;
  1118. while (src < buf + buf_size) {
  1119. char hi, lo;
  1120. lo = *src & 0x0F;
  1121. hi = (*src >> 4) & 0x0F;
  1122. if (avctx->codec->id == CODEC_ID_ADPCM_IMA_AMV)
  1123. FFSWAP(char, hi, lo);
  1124. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1125. lo, 3);
  1126. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  1127. hi, 3);
  1128. src++;
  1129. }
  1130. break;
  1131. case CODEC_ID_ADPCM_CT:
  1132. while (src < buf + buf_size) {
  1133. if (st) {
  1134. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1135. (src[0] >> 4) & 0x0F);
  1136. *samples++ = adpcm_ct_expand_nibble(&c->status[1],
  1137. src[0] & 0x0F);
  1138. } else {
  1139. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1140. (src[0] >> 4) & 0x0F);
  1141. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  1142. src[0] & 0x0F);
  1143. }
  1144. src++;
  1145. }
  1146. break;
  1147. case CODEC_ID_ADPCM_SBPRO_4:
  1148. case CODEC_ID_ADPCM_SBPRO_3:
  1149. case CODEC_ID_ADPCM_SBPRO_2:
  1150. if (!c->status[0].step_index) {
  1151. /* the first byte is a raw sample */
  1152. *samples++ = 128 * (*src++ - 0x80);
  1153. if (st)
  1154. *samples++ = 128 * (*src++ - 0x80);
  1155. c->status[0].step_index = 1;
  1156. }
  1157. if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
  1158. while (src < buf + buf_size) {
  1159. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1160. (src[0] >> 4) & 0x0F, 4, 0);
  1161. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1162. src[0] & 0x0F, 4, 0);
  1163. src++;
  1164. }
  1165. } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
  1166. while (src < buf + buf_size && samples + 2 < samples_end) {
  1167. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1168. (src[0] >> 5) & 0x07, 3, 0);
  1169. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1170. (src[0] >> 2) & 0x07, 3, 0);
  1171. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1172. src[0] & 0x03, 2, 0);
  1173. src++;
  1174. }
  1175. } else {
  1176. while (src < buf + buf_size && samples + 3 < samples_end) {
  1177. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1178. (src[0] >> 6) & 0x03, 2, 2);
  1179. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1180. (src[0] >> 4) & 0x03, 2, 2);
  1181. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  1182. (src[0] >> 2) & 0x03, 2, 2);
  1183. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  1184. src[0] & 0x03, 2, 2);
  1185. src++;
  1186. }
  1187. }
  1188. break;
  1189. case CODEC_ID_ADPCM_SWF:
  1190. {
  1191. GetBitContext gb;
  1192. const int *table;
  1193. int k0, signmask, nb_bits, count;
  1194. int size = buf_size*8;
  1195. init_get_bits(&gb, buf, size);
  1196. //read bits & initial values
  1197. nb_bits = get_bits(&gb, 2)+2;
  1198. //av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", nb_bits);
  1199. table = swf_index_tables[nb_bits-2];
  1200. k0 = 1 << (nb_bits-2);
  1201. signmask = 1 << (nb_bits-1);
  1202. while (get_bits_count(&gb) <= size - 22*avctx->channels) {
  1203. for (i = 0; i < avctx->channels; i++) {
  1204. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  1205. c->status[i].step_index = get_bits(&gb, 6);
  1206. }
  1207. for (count = 0; get_bits_count(&gb) <= size - nb_bits*avctx->channels && count < 4095; count++) {
  1208. int i;
  1209. for (i = 0; i < avctx->channels; i++) {
  1210. // similar to IMA adpcm
  1211. int delta = get_bits(&gb, nb_bits);
  1212. int step = step_table[c->status[i].step_index];
  1213. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  1214. int k = k0;
  1215. do {
  1216. if (delta & k)
  1217. vpdiff += step;
  1218. step >>= 1;
  1219. k >>= 1;
  1220. } while(k);
  1221. vpdiff += step;
  1222. if (delta & signmask)
  1223. c->status[i].predictor -= vpdiff;
  1224. else
  1225. c->status[i].predictor += vpdiff;
  1226. c->status[i].step_index += table[delta & (~signmask)];
  1227. c->status[i].step_index = av_clip(c->status[i].step_index, 0, 88);
  1228. c->status[i].predictor = av_clip_int16(c->status[i].predictor);
  1229. *samples++ = c->status[i].predictor;
  1230. if (samples >= samples_end) {
  1231. av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
  1232. return -1;
  1233. }
  1234. }
  1235. }
  1236. }
  1237. src += buf_size;
  1238. break;
  1239. }
  1240. case CODEC_ID_ADPCM_YAMAHA:
  1241. while (src < buf + buf_size) {
  1242. if (st) {
  1243. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1244. src[0] & 0x0F);
  1245. *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
  1246. (src[0] >> 4) & 0x0F);
  1247. } else {
  1248. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1249. src[0] & 0x0F);
  1250. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  1251. (src[0] >> 4) & 0x0F);
  1252. }
  1253. src++;
  1254. }
  1255. break;
  1256. case CODEC_ID_ADPCM_THP:
  1257. {
  1258. int table[2][16];
  1259. unsigned int samplecnt;
  1260. int prev[2][2];
  1261. int ch;
  1262. if (buf_size < 80) {
  1263. av_log(avctx, AV_LOG_ERROR, "frame too small\n");
  1264. return -1;
  1265. }
  1266. src+=4;
  1267. samplecnt = bytestream_get_be32(&src);
  1268. for (i = 0; i < 32; i++)
  1269. table[0][i] = (int16_t)bytestream_get_be16(&src);
  1270. /* Initialize the previous sample. */
  1271. for (i = 0; i < 4; i++)
  1272. prev[0][i] = (int16_t)bytestream_get_be16(&src);
  1273. if (samplecnt >= (samples_end - samples) / (st + 1)) {
  1274. av_log(avctx, AV_LOG_ERROR, "allocated output buffer is too small\n");
  1275. return -1;
  1276. }
  1277. for (ch = 0; ch <= st; ch++) {
  1278. samples = (unsigned short *) data + ch;
  1279. /* Read in every sample for this channel. */
  1280. for (i = 0; i < samplecnt / 14; i++) {
  1281. int index = (*src >> 4) & 7;
  1282. unsigned int exp = 28 - (*src++ & 15);
  1283. int factor1 = table[ch][index * 2];
  1284. int factor2 = table[ch][index * 2 + 1];
  1285. /* Decode 14 samples. */
  1286. for (n = 0; n < 14; n++) {
  1287. int32_t sampledat;
  1288. if(n&1) sampledat= *src++ <<28;
  1289. else sampledat= (*src&0xF0)<<24;
  1290. sampledat = ((prev[ch][0]*factor1
  1291. + prev[ch][1]*factor2) >> 11) + (sampledat>>exp);
  1292. *samples = av_clip_int16(sampledat);
  1293. prev[ch][1] = prev[ch][0];
  1294. prev[ch][0] = *samples++;
  1295. /* In case of stereo, skip one sample, this sample
  1296. is for the other channel. */
  1297. samples += st;
  1298. }
  1299. }
  1300. }
  1301. /* In the previous loop, in case stereo is used, samples is
  1302. increased exactly one time too often. */
  1303. samples -= st;
  1304. break;
  1305. }
  1306. default:
  1307. return -1;
  1308. }
  1309. *data_size = (uint8_t *)samples - (uint8_t *)data;
  1310. return src - buf;
  1311. }
  1312. #ifdef CONFIG_ENCODERS
  1313. #define ADPCM_ENCODER(id,name) \
  1314. AVCodec name ## _encoder = { \
  1315. #name, \
  1316. CODEC_TYPE_AUDIO, \
  1317. id, \
  1318. sizeof(ADPCMContext), \
  1319. adpcm_encode_init, \
  1320. adpcm_encode_frame, \
  1321. adpcm_encode_close, \
  1322. NULL, \
  1323. };
  1324. #else
  1325. #define ADPCM_ENCODER(id,name)
  1326. #endif
  1327. #ifdef CONFIG_DECODERS
  1328. #define ADPCM_DECODER(id,name) \
  1329. AVCodec name ## _decoder = { \
  1330. #name, \
  1331. CODEC_TYPE_AUDIO, \
  1332. id, \
  1333. sizeof(ADPCMContext), \
  1334. adpcm_decode_init, \
  1335. NULL, \
  1336. NULL, \
  1337. adpcm_decode_frame, \
  1338. };
  1339. #else
  1340. #define ADPCM_DECODER(id,name)
  1341. #endif
  1342. #define ADPCM_CODEC(id, name) \
  1343. ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
  1344. ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
  1345. ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
  1346. ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
  1347. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_AMV, adpcm_ima_amv);
  1348. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
  1349. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
  1350. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
  1351. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
  1352. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
  1353. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
  1354. ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
  1355. ADPCM_CODEC(CODEC_ID_ADPCM_EA_R1, adpcm_ea_r1);
  1356. ADPCM_CODEC(CODEC_ID_ADPCM_EA_R2, adpcm_ea_r2);
  1357. ADPCM_CODEC(CODEC_ID_ADPCM_EA_R3, adpcm_ea_r3);
  1358. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4);
  1359. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3);
  1360. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2);
  1361. ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);
  1362. ADPCM_CODEC(CODEC_ID_ADPCM_THP, adpcm_thp);
  1363. ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
  1364. ADPCM_CODEC(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);
  1365. #undef ADPCM_CODEC