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.

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