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.

1122 lines
36KB

  1. /*
  2. * ADPCM codecs
  3. * Copyright (c) 2001-2003 The ffmpeg Project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avcodec.h"
  20. #include "bitstream.h"
  21. /**
  22. * @file adpcm.c
  23. * ADPCM codecs.
  24. * First version by Francois Revol (revol@free.fr)
  25. * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
  26. * by Mike Melanson (melanson@pcisys.net)
  27. * CD-ROM XA ADPCM codec by BERO
  28. * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
  29. *
  30. * Features and limitations:
  31. *
  32. * Reference documents:
  33. * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
  34. * http://www.geocities.com/SiliconValley/8682/aud3.txt
  35. * http://openquicktime.sourceforge.net/plugins.htm
  36. * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
  37. * http://www.cs.ucla.edu/~leec/mediabench/applications.html
  38. * SoX source code http://home.sprynet.com/~cbagwell/sox.html
  39. *
  40. * CD-ROM XA:
  41. * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html
  42. * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html
  43. * readstr http://www.geocities.co.jp/Playtown/2004/
  44. */
  45. #define BLKSIZE 1024
  46. #define CLAMP_TO_SHORT(value) \
  47. if (value > 32767) \
  48. value = 32767; \
  49. else if (value < -32768) \
  50. value = -32768; \
  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[2];
  132. short sample_buffer[32]; /* hold left samples while waiting for right samples */
  133. /* SWF only */
  134. int nb_bits;
  135. int nb_samples;
  136. } ADPCMContext;
  137. /* XXX: implement encoding */
  138. #ifdef CONFIG_ENCODERS
  139. static int adpcm_encode_init(AVCodecContext *avctx)
  140. {
  141. if (avctx->channels > 2)
  142. return -1; /* only stereo or mono =) */
  143. switch(avctx->codec->id) {
  144. case CODEC_ID_ADPCM_IMA_QT:
  145. av_log(avctx, AV_LOG_ERROR, "ADPCM: codec adpcm_ima_qt unsupported for encoding !\n");
  146. avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */
  147. return -1;
  148. break;
  149. case CODEC_ID_ADPCM_IMA_WAV:
  150. avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
  151. /* and we have 4 bytes per channel overhead */
  152. avctx->block_align = BLKSIZE;
  153. /* seems frame_size isn't taken into account... have to buffer the samples :-( */
  154. break;
  155. case CODEC_ID_ADPCM_MS:
  156. avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
  157. /* and we have 7 bytes per channel overhead */
  158. avctx->block_align = BLKSIZE;
  159. break;
  160. case CODEC_ID_ADPCM_YAMAHA:
  161. avctx->frame_size = BLKSIZE * avctx->channels;
  162. avctx->block_align = BLKSIZE;
  163. break;
  164. default:
  165. return -1;
  166. break;
  167. }
  168. avctx->coded_frame= avcodec_alloc_frame();
  169. avctx->coded_frame->key_frame= 1;
  170. return 0;
  171. }
  172. static int adpcm_encode_close(AVCodecContext *avctx)
  173. {
  174. av_freep(&avctx->coded_frame);
  175. return 0;
  176. }
  177. static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
  178. {
  179. int step_index;
  180. unsigned char nibble;
  181. int sign = 0; /* sign bit of the nibble (MSB) */
  182. int delta, predicted_delta;
  183. delta = sample - c->prev_sample;
  184. if (delta < 0) {
  185. sign = 1;
  186. delta = -delta;
  187. }
  188. step_index = c->step_index;
  189. /* nibble = 4 * delta / step_table[step_index]; */
  190. nibble = (delta << 2) / step_table[step_index];
  191. if (nibble > 7)
  192. nibble = 7;
  193. step_index += index_table[nibble];
  194. if (step_index < 0)
  195. step_index = 0;
  196. if (step_index > 88)
  197. step_index = 88;
  198. /* what the decoder will find */
  199. predicted_delta = ((step_table[step_index] * nibble) / 4) + (step_table[step_index] / 8);
  200. if (sign)
  201. c->prev_sample -= predicted_delta;
  202. else
  203. c->prev_sample += predicted_delta;
  204. CLAMP_TO_SHORT(c->prev_sample);
  205. nibble += sign << 3; /* sign * 8 */
  206. /* save back */
  207. c->step_index = step_index;
  208. return nibble;
  209. }
  210. static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
  211. {
  212. int predictor, nibble, bias;
  213. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
  214. nibble= sample - predictor;
  215. if(nibble>=0) bias= c->idelta/2;
  216. else bias=-c->idelta/2;
  217. nibble= (nibble + bias) / c->idelta;
  218. nibble= clip(nibble, -8, 7)&0x0F;
  219. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  220. CLAMP_TO_SHORT(predictor);
  221. c->sample2 = c->sample1;
  222. c->sample1 = predictor;
  223. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
  224. if (c->idelta < 16) c->idelta = 16;
  225. return nibble;
  226. }
  227. static inline unsigned char adpcm_yamaha_compress_sample(ADPCMChannelStatus *c, short sample)
  228. {
  229. int i1 = 0, j1;
  230. if(!c->step) {
  231. c->predictor = 0;
  232. c->step = 127;
  233. }
  234. j1 = sample - c->predictor;
  235. j1 = (j1 * 8) / c->step;
  236. i1 = abs(j1) / 2;
  237. if (i1 > 7)
  238. i1 = 7;
  239. if (j1 < 0)
  240. i1 += 8;
  241. c->predictor = c->predictor + ((c->step * yamaha_difflookup[i1]) / 8);
  242. CLAMP_TO_SHORT(c->predictor);
  243. c->step = (c->step * yamaha_indexscale[i1]) >> 8;
  244. c->step = clip(c->step, 127, 24567);
  245. return i1;
  246. }
  247. static int adpcm_encode_frame(AVCodecContext *avctx,
  248. unsigned char *frame, int buf_size, void *data)
  249. {
  250. int n, i, st;
  251. short *samples;
  252. unsigned char *dst;
  253. ADPCMContext *c = avctx->priv_data;
  254. dst = frame;
  255. samples = (short *)data;
  256. st= avctx->channels == 2;
  257. /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
  258. switch(avctx->codec->id) {
  259. case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
  260. break;
  261. case CODEC_ID_ADPCM_IMA_WAV:
  262. n = avctx->frame_size / 8;
  263. c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
  264. /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
  265. *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
  266. *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
  267. *dst++ = (unsigned char)c->status[0].step_index;
  268. *dst++ = 0; /* unknown */
  269. samples++;
  270. if (avctx->channels == 2) {
  271. c->status[1].prev_sample = (signed short)samples[1];
  272. /* c->status[1].step_index = 0; */
  273. *dst++ = (c->status[1].prev_sample) & 0xFF;
  274. *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
  275. *dst++ = (unsigned char)c->status[1].step_index;
  276. *dst++ = 0;
  277. samples++;
  278. }
  279. /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
  280. for (; n>0; n--) {
  281. *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F;
  282. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0;
  283. dst++;
  284. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F;
  285. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0;
  286. dst++;
  287. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F;
  288. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0;
  289. dst++;
  290. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F;
  291. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0;
  292. dst++;
  293. /* right channel */
  294. if (avctx->channels == 2) {
  295. *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
  296. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
  297. dst++;
  298. *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
  299. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
  300. dst++;
  301. *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
  302. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
  303. dst++;
  304. *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
  305. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
  306. dst++;
  307. }
  308. samples += 8 * avctx->channels;
  309. }
  310. break;
  311. case CODEC_ID_ADPCM_MS:
  312. for(i=0; i<avctx->channels; i++){
  313. int predictor=0;
  314. *dst++ = predictor;
  315. c->status[i].coeff1 = AdaptCoeff1[predictor];
  316. c->status[i].coeff2 = AdaptCoeff2[predictor];
  317. }
  318. for(i=0; i<avctx->channels; i++){
  319. if (c->status[i].idelta < 16)
  320. c->status[i].idelta = 16;
  321. *dst++ = c->status[i].idelta & 0xFF;
  322. *dst++ = c->status[i].idelta >> 8;
  323. }
  324. for(i=0; i<avctx->channels; i++){
  325. c->status[i].sample1= *samples++;
  326. *dst++ = c->status[i].sample1 & 0xFF;
  327. *dst++ = c->status[i].sample1 >> 8;
  328. }
  329. for(i=0; i<avctx->channels; i++){
  330. c->status[i].sample2= *samples++;
  331. *dst++ = c->status[i].sample2 & 0xFF;
  332. *dst++ = c->status[i].sample2 >> 8;
  333. }
  334. for(i=7*avctx->channels; i<avctx->block_align; i++) {
  335. int nibble;
  336. nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
  337. nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
  338. *dst++ = nibble;
  339. }
  340. break;
  341. case CODEC_ID_ADPCM_YAMAHA:
  342. n = avctx->frame_size / 2;
  343. for (; n>0; n--) {
  344. for(i = 0; i < avctx->channels; i++) {
  345. int nibble;
  346. nibble = adpcm_yamaha_compress_sample(&c->status[i], samples[i]);
  347. nibble |= adpcm_yamaha_compress_sample(&c->status[i], samples[i+avctx->channels]) << 4;
  348. *dst++ = nibble;
  349. }
  350. samples += 2 * avctx->channels;
  351. }
  352. break;
  353. default:
  354. return -1;
  355. }
  356. return dst - frame;
  357. }
  358. #endif //CONFIG_ENCODERS
  359. static int adpcm_decode_init(AVCodecContext * avctx)
  360. {
  361. ADPCMContext *c = avctx->priv_data;
  362. c->channel = 0;
  363. c->status[0].predictor = c->status[1].predictor = 0;
  364. c->status[0].step_index = c->status[1].step_index = 0;
  365. c->status[0].step = c->status[1].step = 0;
  366. switch(avctx->codec->id) {
  367. case CODEC_ID_ADPCM_CT:
  368. c->status[0].step = c->status[1].step = 511;
  369. break;
  370. default:
  371. break;
  372. }
  373. return 0;
  374. }
  375. static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
  376. {
  377. int step_index;
  378. int predictor;
  379. int sign, delta, diff, step;
  380. step = step_table[c->step_index];
  381. step_index = c->step_index + index_table[(unsigned)nibble];
  382. if (step_index < 0) step_index = 0;
  383. else if (step_index > 88) step_index = 88;
  384. sign = nibble & 8;
  385. delta = nibble & 7;
  386. /* perform direct multiplication instead of series of jumps proposed by
  387. * the reference ADPCM implementation since modern CPUs can do the mults
  388. * quickly enough */
  389. diff = ((2 * delta + 1) * step) >> shift;
  390. predictor = c->predictor;
  391. if (sign) predictor -= diff;
  392. else predictor += diff;
  393. CLAMP_TO_SHORT(predictor);
  394. c->predictor = predictor;
  395. c->step_index = step_index;
  396. return (short)predictor;
  397. }
  398. static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
  399. {
  400. int predictor;
  401. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
  402. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  403. CLAMP_TO_SHORT(predictor);
  404. c->sample2 = c->sample1;
  405. c->sample1 = predictor;
  406. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
  407. if (c->idelta < 16) c->idelta = 16;
  408. return (short)predictor;
  409. }
  410. static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
  411. {
  412. int predictor;
  413. int sign, delta, diff;
  414. int new_step;
  415. sign = nibble & 8;
  416. delta = nibble & 7;
  417. /* perform direct multiplication instead of series of jumps proposed by
  418. * the reference ADPCM implementation since modern CPUs can do the mults
  419. * quickly enough */
  420. diff = ((2 * delta + 1) * c->step) >> 3;
  421. predictor = c->predictor;
  422. /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
  423. if(sign)
  424. predictor = ((predictor * 254) >> 8) - diff;
  425. else
  426. predictor = ((predictor * 254) >> 8) + diff;
  427. /* calculate new step and clamp it to range 511..32767 */
  428. new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
  429. c->step = new_step;
  430. if(c->step < 511)
  431. c->step = 511;
  432. if(c->step > 32767)
  433. c->step = 32767;
  434. CLAMP_TO_SHORT(predictor);
  435. c->predictor = predictor;
  436. return (short)predictor;
  437. }
  438. static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
  439. {
  440. if(!c->step) {
  441. c->predictor = 0;
  442. c->step = 127;
  443. }
  444. c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
  445. CLAMP_TO_SHORT(c->predictor);
  446. c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
  447. c->step = clip(c->step, 127, 24567);
  448. return c->predictor;
  449. }
  450. static void xa_decode(short *out, const unsigned char *in,
  451. ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
  452. {
  453. int i, j;
  454. int shift,filter,f0,f1;
  455. int s_1,s_2;
  456. int d,s,t;
  457. for(i=0;i<4;i++) {
  458. shift = 12 - (in[4+i*2] & 15);
  459. filter = in[4+i*2] >> 4;
  460. f0 = xa_adpcm_table[filter][0];
  461. f1 = xa_adpcm_table[filter][1];
  462. s_1 = left->sample1;
  463. s_2 = left->sample2;
  464. for(j=0;j<28;j++) {
  465. d = in[16+i+j*4];
  466. t = (signed char)(d<<4)>>4;
  467. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  468. CLAMP_TO_SHORT(s);
  469. *out = s;
  470. out += inc;
  471. s_2 = s_1;
  472. s_1 = s;
  473. }
  474. if (inc==2) { /* stereo */
  475. left->sample1 = s_1;
  476. left->sample2 = s_2;
  477. s_1 = right->sample1;
  478. s_2 = right->sample2;
  479. out = out + 1 - 28*2;
  480. }
  481. shift = 12 - (in[5+i*2] & 15);
  482. filter = in[5+i*2] >> 4;
  483. f0 = xa_adpcm_table[filter][0];
  484. f1 = xa_adpcm_table[filter][1];
  485. for(j=0;j<28;j++) {
  486. d = in[16+i+j*4];
  487. t = (signed char)d >> 4;
  488. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  489. CLAMP_TO_SHORT(s);
  490. *out = s;
  491. out += inc;
  492. s_2 = s_1;
  493. s_1 = s;
  494. }
  495. if (inc==2) { /* stereo */
  496. right->sample1 = s_1;
  497. right->sample2 = s_2;
  498. out -= 1;
  499. } else {
  500. left->sample1 = s_1;
  501. left->sample2 = s_2;
  502. }
  503. }
  504. }
  505. /* DK3 ADPCM support macro */
  506. #define DK3_GET_NEXT_NIBBLE() \
  507. if (decode_top_nibble_next) \
  508. { \
  509. nibble = (last_byte >> 4) & 0x0F; \
  510. decode_top_nibble_next = 0; \
  511. } \
  512. else \
  513. { \
  514. last_byte = *src++; \
  515. if (src >= buf + buf_size) break; \
  516. nibble = last_byte & 0x0F; \
  517. decode_top_nibble_next = 1; \
  518. }
  519. static int adpcm_decode_frame(AVCodecContext *avctx,
  520. void *data, int *data_size,
  521. uint8_t *buf, int buf_size)
  522. {
  523. ADPCMContext *c = avctx->priv_data;
  524. ADPCMChannelStatus *cs;
  525. int n, m, channel, i;
  526. int block_predictor[2];
  527. short *samples;
  528. uint8_t *src;
  529. int st; /* stereo */
  530. /* DK3 ADPCM accounting variables */
  531. unsigned char last_byte = 0;
  532. unsigned char nibble;
  533. int decode_top_nibble_next = 0;
  534. int diff_channel;
  535. /* EA ADPCM state variables */
  536. uint32_t samples_in_chunk;
  537. int32_t previous_left_sample, previous_right_sample;
  538. int32_t current_left_sample, current_right_sample;
  539. int32_t next_left_sample, next_right_sample;
  540. int32_t coeff1l, coeff2l, coeff1r, coeff2r;
  541. uint8_t shift_left, shift_right;
  542. int count1, count2;
  543. if (!buf_size)
  544. return 0;
  545. samples = data;
  546. src = buf;
  547. st = avctx->channels == 2;
  548. switch(avctx->codec->id) {
  549. case CODEC_ID_ADPCM_IMA_QT:
  550. n = (buf_size - 2);/* >> 2*avctx->channels;*/
  551. channel = c->channel;
  552. cs = &(c->status[channel]);
  553. /* (pppppp) (piiiiiii) */
  554. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  555. cs->predictor = (*src++) << 8;
  556. cs->predictor |= (*src & 0x80);
  557. cs->predictor &= 0xFF80;
  558. /* sign extension */
  559. if(cs->predictor & 0x8000)
  560. cs->predictor -= 0x10000;
  561. CLAMP_TO_SHORT(cs->predictor);
  562. cs->step_index = (*src++) & 0x7F;
  563. if (cs->step_index > 88) av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  564. if (cs->step_index > 88) cs->step_index = 88;
  565. cs->step = step_table[cs->step_index];
  566. if (st && channel)
  567. samples++;
  568. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  569. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
  570. samples += avctx->channels;
  571. *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
  572. samples += avctx->channels;
  573. src ++;
  574. }
  575. if(st) { /* handle stereo interlacing */
  576. c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
  577. if(channel == 1) { /* wait for the other packet before outputing anything */
  578. return src - buf;
  579. }
  580. }
  581. break;
  582. case CODEC_ID_ADPCM_IMA_WAV:
  583. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  584. buf_size = avctx->block_align;
  585. for(i=0; i<avctx->channels; i++){
  586. cs = &(c->status[i]);
  587. cs->predictor = *src++;
  588. cs->predictor |= (*src++) << 8;
  589. if(cs->predictor & 0x8000)
  590. cs->predictor -= 0x10000;
  591. CLAMP_TO_SHORT(cs->predictor);
  592. // XXX: is this correct ??: *samples++ = cs->predictor;
  593. cs->step_index = *src++;
  594. if (cs->step_index < 0) cs->step_index = 0;
  595. if (cs->step_index > 88) cs->step_index = 88;
  596. if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null !!\n"); /* unused */
  597. }
  598. for(m=4; src < (buf + buf_size);) {
  599. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F, 3);
  600. if (st)
  601. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F, 3);
  602. *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F, 3);
  603. if (st) {
  604. *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F, 3);
  605. if (!--m) {
  606. m=4;
  607. src+=4;
  608. }
  609. }
  610. src++;
  611. }
  612. break;
  613. case CODEC_ID_ADPCM_4XM:
  614. cs = &(c->status[0]);
  615. c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  616. if(st){
  617. c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  618. }
  619. c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  620. if(st){
  621. c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  622. }
  623. if (cs->step_index < 0) cs->step_index = 0;
  624. if (cs->step_index > 88) cs->step_index = 88;
  625. m= (buf_size - (src - buf))>>st;
  626. for(i=0; i<m; i++) {
  627. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
  628. if (st)
  629. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
  630. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
  631. if (st)
  632. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
  633. }
  634. src += m<<st;
  635. break;
  636. case CODEC_ID_ADPCM_MS:
  637. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  638. buf_size = avctx->block_align;
  639. n = buf_size - 7 * avctx->channels;
  640. if (n < 0)
  641. return -1;
  642. block_predictor[0] = clip(*src++, 0, 7);
  643. block_predictor[1] = 0;
  644. if (st)
  645. block_predictor[1] = clip(*src++, 0, 7);
  646. c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  647. src+=2;
  648. if (st){
  649. c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  650. src+=2;
  651. }
  652. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  653. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  654. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  655. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  656. c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  657. src+=2;
  658. if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  659. if (st) src+=2;
  660. c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  661. src+=2;
  662. if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  663. if (st) src+=2;
  664. *samples++ = c->status[0].sample1;
  665. if (st) *samples++ = c->status[1].sample1;
  666. *samples++ = c->status[0].sample2;
  667. if (st) *samples++ = c->status[1].sample2;
  668. for(;n>0;n--) {
  669. *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  670. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  671. src ++;
  672. }
  673. break;
  674. case CODEC_ID_ADPCM_IMA_DK4:
  675. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  676. buf_size = avctx->block_align;
  677. c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
  678. c->status[0].step_index = src[2];
  679. src += 4;
  680. *samples++ = c->status[0].predictor;
  681. if (st) {
  682. c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
  683. c->status[1].step_index = src[2];
  684. src += 4;
  685. *samples++ = c->status[1].predictor;
  686. }
  687. while (src < buf + buf_size) {
  688. /* take care of the top nibble (always left or mono channel) */
  689. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  690. (src[0] >> 4) & 0x0F, 3);
  691. /* take care of the bottom nibble, which is right sample for
  692. * stereo, or another mono sample */
  693. if (st)
  694. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  695. src[0] & 0x0F, 3);
  696. else
  697. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  698. src[0] & 0x0F, 3);
  699. src++;
  700. }
  701. break;
  702. case CODEC_ID_ADPCM_IMA_DK3:
  703. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  704. buf_size = avctx->block_align;
  705. c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
  706. c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
  707. c->status[0].step_index = src[14];
  708. c->status[1].step_index = src[15];
  709. /* sign extend the predictors */
  710. src += 16;
  711. diff_channel = c->status[1].predictor;
  712. /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
  713. * the buffer is consumed */
  714. while (1) {
  715. /* for this algorithm, c->status[0] is the sum channel and
  716. * c->status[1] is the diff channel */
  717. /* process the first predictor of the sum channel */
  718. DK3_GET_NEXT_NIBBLE();
  719. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  720. /* process the diff channel predictor */
  721. DK3_GET_NEXT_NIBBLE();
  722. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  723. /* process the first pair of stereo PCM samples */
  724. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  725. *samples++ = c->status[0].predictor + c->status[1].predictor;
  726. *samples++ = c->status[0].predictor - c->status[1].predictor;
  727. /* process the second predictor of the sum channel */
  728. DK3_GET_NEXT_NIBBLE();
  729. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  730. /* process the second pair of stereo PCM samples */
  731. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  732. *samples++ = c->status[0].predictor + c->status[1].predictor;
  733. *samples++ = c->status[0].predictor - c->status[1].predictor;
  734. }
  735. break;
  736. case CODEC_ID_ADPCM_IMA_WS:
  737. /* no per-block initialization; just start decoding the data */
  738. while (src < buf + buf_size) {
  739. if (st) {
  740. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  741. (src[0] >> 4) & 0x0F, 3);
  742. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  743. src[0] & 0x0F, 3);
  744. } else {
  745. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  746. (src[0] >> 4) & 0x0F, 3);
  747. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  748. src[0] & 0x0F, 3);
  749. }
  750. src++;
  751. }
  752. break;
  753. case CODEC_ID_ADPCM_XA:
  754. c->status[0].sample1 = c->status[0].sample2 =
  755. c->status[1].sample1 = c->status[1].sample2 = 0;
  756. while (buf_size >= 128) {
  757. xa_decode(samples, src, &c->status[0], &c->status[1],
  758. avctx->channels);
  759. src += 128;
  760. samples += 28 * 8;
  761. buf_size -= 128;
  762. }
  763. break;
  764. case CODEC_ID_ADPCM_EA:
  765. samples_in_chunk = LE_32(src);
  766. if (samples_in_chunk >= ((buf_size - 12) * 2)) {
  767. src += buf_size;
  768. break;
  769. }
  770. src += 4;
  771. current_left_sample = (int16_t)LE_16(src);
  772. src += 2;
  773. previous_left_sample = (int16_t)LE_16(src);
  774. src += 2;
  775. current_right_sample = (int16_t)LE_16(src);
  776. src += 2;
  777. previous_right_sample = (int16_t)LE_16(src);
  778. src += 2;
  779. for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
  780. coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
  781. coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
  782. coeff1r = ea_adpcm_table[*src & 0x0F];
  783. coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
  784. src++;
  785. shift_left = ((*src >> 4) & 0x0F) + 8;
  786. shift_right = (*src & 0x0F) + 8;
  787. src++;
  788. for (count2 = 0; count2 < 28; count2++) {
  789. next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
  790. next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
  791. src++;
  792. next_left_sample = (next_left_sample +
  793. (current_left_sample * coeff1l) +
  794. (previous_left_sample * coeff2l) + 0x80) >> 8;
  795. next_right_sample = (next_right_sample +
  796. (current_right_sample * coeff1r) +
  797. (previous_right_sample * coeff2r) + 0x80) >> 8;
  798. CLAMP_TO_SHORT(next_left_sample);
  799. CLAMP_TO_SHORT(next_right_sample);
  800. previous_left_sample = current_left_sample;
  801. current_left_sample = next_left_sample;
  802. previous_right_sample = current_right_sample;
  803. current_right_sample = next_right_sample;
  804. *samples++ = (unsigned short)current_left_sample;
  805. *samples++ = (unsigned short)current_right_sample;
  806. }
  807. }
  808. break;
  809. case CODEC_ID_ADPCM_IMA_SMJPEG:
  810. c->status[0].predictor = *src;
  811. src += 2;
  812. c->status[0].step_index = *src++;
  813. src++; /* skip another byte before getting to the meat */
  814. while (src < buf + buf_size) {
  815. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  816. *src & 0x0F, 3);
  817. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  818. (*src >> 4) & 0x0F, 3);
  819. src++;
  820. }
  821. break;
  822. case CODEC_ID_ADPCM_CT:
  823. while (src < buf + buf_size) {
  824. if (st) {
  825. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  826. (src[0] >> 4) & 0x0F);
  827. *samples++ = adpcm_ct_expand_nibble(&c->status[1],
  828. src[0] & 0x0F);
  829. } else {
  830. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  831. (src[0] >> 4) & 0x0F);
  832. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  833. src[0] & 0x0F);
  834. }
  835. src++;
  836. }
  837. break;
  838. case CODEC_ID_ADPCM_SWF:
  839. {
  840. GetBitContext gb;
  841. const int *table;
  842. int k0, signmask;
  843. int size = buf_size*8;
  844. init_get_bits(&gb, buf, size);
  845. // first frame, read bits & inital values
  846. if (!c->nb_bits)
  847. {
  848. c->nb_bits = get_bits(&gb, 2)+2;
  849. // av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", c->nb_bits);
  850. }
  851. table = swf_index_tables[c->nb_bits-2];
  852. k0 = 1 << (c->nb_bits-2);
  853. signmask = 1 << (c->nb_bits-1);
  854. while (get_bits_count(&gb) <= size)
  855. {
  856. int i;
  857. c->nb_samples++;
  858. // wrap around at every 4096 samples...
  859. if ((c->nb_samples & 0xfff) == 1)
  860. {
  861. for (i = 0; i <= st; i++)
  862. {
  863. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  864. c->status[i].step_index = get_bits(&gb, 6);
  865. }
  866. }
  867. // similar to IMA adpcm
  868. for (i = 0; i <= st; i++)
  869. {
  870. int delta = get_bits(&gb, c->nb_bits);
  871. int step = step_table[c->status[i].step_index];
  872. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  873. int k = k0;
  874. do {
  875. if (delta & k)
  876. vpdiff += step;
  877. step >>= 1;
  878. k >>= 1;
  879. } while(k);
  880. vpdiff += step;
  881. if (delta & signmask)
  882. c->status[i].predictor -= vpdiff;
  883. else
  884. c->status[i].predictor += vpdiff;
  885. c->status[i].step_index += table[delta & (~signmask)];
  886. c->status[i].step_index = clip(c->status[i].step_index, 0, 88);
  887. c->status[i].predictor = clip(c->status[i].predictor, -32768, 32767);
  888. *samples++ = c->status[i].predictor;
  889. }
  890. }
  891. // src += get_bits_count(&gb)*8;
  892. src += size;
  893. break;
  894. }
  895. case CODEC_ID_ADPCM_YAMAHA:
  896. while (src < buf + buf_size) {
  897. if (st) {
  898. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  899. src[0] & 0x0F);
  900. *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
  901. (src[0] >> 4) & 0x0F);
  902. } else {
  903. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  904. src[0] & 0x0F);
  905. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  906. (src[0] >> 4) & 0x0F);
  907. }
  908. src++;
  909. }
  910. break;
  911. default:
  912. return -1;
  913. }
  914. *data_size = (uint8_t *)samples - (uint8_t *)data;
  915. return src - buf;
  916. }
  917. #ifdef CONFIG_ENCODERS
  918. #define ADPCM_ENCODER(id,name) \
  919. AVCodec name ## _encoder = { \
  920. #name, \
  921. CODEC_TYPE_AUDIO, \
  922. id, \
  923. sizeof(ADPCMContext), \
  924. adpcm_encode_init, \
  925. adpcm_encode_frame, \
  926. adpcm_encode_close, \
  927. NULL, \
  928. };
  929. #else
  930. #define ADPCM_ENCODER(id,name)
  931. #endif
  932. #ifdef CONFIG_DECODERS
  933. #define ADPCM_DECODER(id,name) \
  934. AVCodec name ## _decoder = { \
  935. #name, \
  936. CODEC_TYPE_AUDIO, \
  937. id, \
  938. sizeof(ADPCMContext), \
  939. adpcm_decode_init, \
  940. NULL, \
  941. NULL, \
  942. adpcm_decode_frame, \
  943. };
  944. #else
  945. #define ADPCM_DECODER(id,name)
  946. #endif
  947. #define ADPCM_CODEC(id, name) \
  948. ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
  949. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
  950. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
  951. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
  952. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
  953. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
  954. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
  955. ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
  956. ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
  957. ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
  958. ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx);
  959. ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
  960. ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
  961. ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);
  962. ADPCM_CODEC(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);
  963. #undef ADPCM_CODEC