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.

1195 lines
39KB

  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift)
  439. {
  440. int sign, delta, diff;
  441. sign = nibble & (1<<(size-1));
  442. delta = nibble & ((1<<(size-1))-1);
  443. diff = delta << (7 + c->step + shift);
  444. if (sign)
  445. c->predictor -= diff;
  446. else
  447. c->predictor += diff;
  448. /* clamp result */
  449. if (c->predictor > 16256)
  450. c->predictor = 16256;
  451. else if (c->predictor < -16384)
  452. c->predictor = -16384;
  453. /* calculate new step */
  454. if (delta >= (2*size - 3) && c->step < 3)
  455. c->step++;
  456. else if (delta == 0 && c->step > 0)
  457. c->step--;
  458. return (short) c->predictor;
  459. }
  460. static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble)
  461. {
  462. if(!c->step) {
  463. c->predictor = 0;
  464. c->step = 127;
  465. }
  466. c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;
  467. CLAMP_TO_SHORT(c->predictor);
  468. c->step = (c->step * yamaha_indexscale[nibble]) >> 8;
  469. c->step = clip(c->step, 127, 24567);
  470. return c->predictor;
  471. }
  472. static void xa_decode(short *out, const unsigned char *in,
  473. ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
  474. {
  475. int i, j;
  476. int shift,filter,f0,f1;
  477. int s_1,s_2;
  478. int d,s,t;
  479. for(i=0;i<4;i++) {
  480. shift = 12 - (in[4+i*2] & 15);
  481. filter = in[4+i*2] >> 4;
  482. f0 = xa_adpcm_table[filter][0];
  483. f1 = xa_adpcm_table[filter][1];
  484. s_1 = left->sample1;
  485. s_2 = left->sample2;
  486. for(j=0;j<28;j++) {
  487. d = in[16+i+j*4];
  488. t = (signed char)(d<<4)>>4;
  489. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  490. CLAMP_TO_SHORT(s);
  491. *out = s;
  492. out += inc;
  493. s_2 = s_1;
  494. s_1 = s;
  495. }
  496. if (inc==2) { /* stereo */
  497. left->sample1 = s_1;
  498. left->sample2 = s_2;
  499. s_1 = right->sample1;
  500. s_2 = right->sample2;
  501. out = out + 1 - 28*2;
  502. }
  503. shift = 12 - (in[5+i*2] & 15);
  504. filter = in[5+i*2] >> 4;
  505. f0 = xa_adpcm_table[filter][0];
  506. f1 = xa_adpcm_table[filter][1];
  507. for(j=0;j<28;j++) {
  508. d = in[16+i+j*4];
  509. t = (signed char)d >> 4;
  510. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  511. CLAMP_TO_SHORT(s);
  512. *out = s;
  513. out += inc;
  514. s_2 = s_1;
  515. s_1 = s;
  516. }
  517. if (inc==2) { /* stereo */
  518. right->sample1 = s_1;
  519. right->sample2 = s_2;
  520. out -= 1;
  521. } else {
  522. left->sample1 = s_1;
  523. left->sample2 = s_2;
  524. }
  525. }
  526. }
  527. /* DK3 ADPCM support macro */
  528. #define DK3_GET_NEXT_NIBBLE() \
  529. if (decode_top_nibble_next) \
  530. { \
  531. nibble = (last_byte >> 4) & 0x0F; \
  532. decode_top_nibble_next = 0; \
  533. } \
  534. else \
  535. { \
  536. last_byte = *src++; \
  537. if (src >= buf + buf_size) break; \
  538. nibble = last_byte & 0x0F; \
  539. decode_top_nibble_next = 1; \
  540. }
  541. static int adpcm_decode_frame(AVCodecContext *avctx,
  542. void *data, int *data_size,
  543. uint8_t *buf, int buf_size)
  544. {
  545. ADPCMContext *c = avctx->priv_data;
  546. ADPCMChannelStatus *cs;
  547. int n, m, channel, i;
  548. int block_predictor[2];
  549. short *samples;
  550. uint8_t *src;
  551. int st; /* stereo */
  552. /* DK3 ADPCM accounting variables */
  553. unsigned char last_byte = 0;
  554. unsigned char nibble;
  555. int decode_top_nibble_next = 0;
  556. int diff_channel;
  557. /* EA ADPCM state variables */
  558. uint32_t samples_in_chunk;
  559. int32_t previous_left_sample, previous_right_sample;
  560. int32_t current_left_sample, current_right_sample;
  561. int32_t next_left_sample, next_right_sample;
  562. int32_t coeff1l, coeff2l, coeff1r, coeff2r;
  563. uint8_t shift_left, shift_right;
  564. int count1, count2;
  565. if (!buf_size)
  566. return 0;
  567. samples = data;
  568. src = buf;
  569. st = avctx->channels == 2 ? 1 : 0;
  570. switch(avctx->codec->id) {
  571. case CODEC_ID_ADPCM_IMA_QT:
  572. n = (buf_size - 2);/* >> 2*avctx->channels;*/
  573. channel = c->channel;
  574. cs = &(c->status[channel]);
  575. /* (pppppp) (piiiiiii) */
  576. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  577. cs->predictor = (*src++) << 8;
  578. cs->predictor |= (*src & 0x80);
  579. cs->predictor &= 0xFF80;
  580. /* sign extension */
  581. if(cs->predictor & 0x8000)
  582. cs->predictor -= 0x10000;
  583. CLAMP_TO_SHORT(cs->predictor);
  584. cs->step_index = (*src++) & 0x7F;
  585. if (cs->step_index > 88) av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  586. if (cs->step_index > 88) cs->step_index = 88;
  587. cs->step = step_table[cs->step_index];
  588. if (st && channel)
  589. samples++;
  590. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  591. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
  592. samples += avctx->channels;
  593. *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
  594. samples += avctx->channels;
  595. src ++;
  596. }
  597. if(st) { /* handle stereo interlacing */
  598. c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
  599. if(channel == 1) { /* wait for the other packet before outputing anything */
  600. return src - buf;
  601. }
  602. }
  603. break;
  604. case CODEC_ID_ADPCM_IMA_WAV:
  605. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  606. buf_size = avctx->block_align;
  607. for(i=0; i<avctx->channels; i++){
  608. cs = &(c->status[i]);
  609. cs->predictor = *src++;
  610. cs->predictor |= (*src++) << 8;
  611. if(cs->predictor & 0x8000)
  612. cs->predictor -= 0x10000;
  613. CLAMP_TO_SHORT(cs->predictor);
  614. // XXX: is this correct ??: *samples++ = cs->predictor;
  615. cs->step_index = *src++;
  616. if (cs->step_index < 0) cs->step_index = 0;
  617. if (cs->step_index > 88) cs->step_index = 88;
  618. if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null !!\n"); /* unused */
  619. }
  620. for(m=4; src < (buf + buf_size);) {
  621. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F, 3);
  622. if (st)
  623. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F, 3);
  624. *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F, 3);
  625. if (st) {
  626. *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F, 3);
  627. if (!--m) {
  628. m=4;
  629. src+=4;
  630. }
  631. }
  632. src++;
  633. }
  634. break;
  635. case CODEC_ID_ADPCM_4XM:
  636. cs = &(c->status[0]);
  637. c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  638. if(st){
  639. c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  640. }
  641. c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  642. if(st){
  643. c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  644. }
  645. if (cs->step_index < 0) cs->step_index = 0;
  646. if (cs->step_index > 88) cs->step_index = 88;
  647. m= (buf_size - (src - buf))>>st;
  648. for(i=0; i<m; i++) {
  649. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
  650. if (st)
  651. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
  652. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
  653. if (st)
  654. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
  655. }
  656. src += m<<st;
  657. break;
  658. case CODEC_ID_ADPCM_MS:
  659. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  660. buf_size = avctx->block_align;
  661. n = buf_size - 7 * avctx->channels;
  662. if (n < 0)
  663. return -1;
  664. block_predictor[0] = clip(*src++, 0, 7);
  665. block_predictor[1] = 0;
  666. if (st)
  667. block_predictor[1] = clip(*src++, 0, 7);
  668. c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  669. src+=2;
  670. if (st){
  671. c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  672. src+=2;
  673. }
  674. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  675. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  676. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  677. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  678. c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  679. src+=2;
  680. if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  681. if (st) src+=2;
  682. c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  683. src+=2;
  684. if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  685. if (st) src+=2;
  686. *samples++ = c->status[0].sample1;
  687. if (st) *samples++ = c->status[1].sample1;
  688. *samples++ = c->status[0].sample2;
  689. if (st) *samples++ = c->status[1].sample2;
  690. for(;n>0;n--) {
  691. *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  692. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  693. src ++;
  694. }
  695. break;
  696. case CODEC_ID_ADPCM_IMA_DK4:
  697. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  698. buf_size = avctx->block_align;
  699. c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
  700. c->status[0].step_index = src[2];
  701. src += 4;
  702. *samples++ = c->status[0].predictor;
  703. if (st) {
  704. c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
  705. c->status[1].step_index = src[2];
  706. src += 4;
  707. *samples++ = c->status[1].predictor;
  708. }
  709. while (src < buf + buf_size) {
  710. /* take care of the top nibble (always left or mono channel) */
  711. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  712. (src[0] >> 4) & 0x0F, 3);
  713. /* take care of the bottom nibble, which is right sample for
  714. * stereo, or another mono sample */
  715. if (st)
  716. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  717. src[0] & 0x0F, 3);
  718. else
  719. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  720. src[0] & 0x0F, 3);
  721. src++;
  722. }
  723. break;
  724. case CODEC_ID_ADPCM_IMA_DK3:
  725. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  726. buf_size = avctx->block_align;
  727. c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
  728. c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
  729. c->status[0].step_index = src[14];
  730. c->status[1].step_index = src[15];
  731. /* sign extend the predictors */
  732. src += 16;
  733. diff_channel = c->status[1].predictor;
  734. /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
  735. * the buffer is consumed */
  736. while (1) {
  737. /* for this algorithm, c->status[0] is the sum channel and
  738. * c->status[1] is the diff channel */
  739. /* process the first predictor of the sum channel */
  740. DK3_GET_NEXT_NIBBLE();
  741. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  742. /* process the diff channel predictor */
  743. DK3_GET_NEXT_NIBBLE();
  744. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  745. /* process the first pair of stereo PCM samples */
  746. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  747. *samples++ = c->status[0].predictor + c->status[1].predictor;
  748. *samples++ = c->status[0].predictor - c->status[1].predictor;
  749. /* process the second predictor of the sum channel */
  750. DK3_GET_NEXT_NIBBLE();
  751. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  752. /* process the second pair of stereo PCM samples */
  753. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  754. *samples++ = c->status[0].predictor + c->status[1].predictor;
  755. *samples++ = c->status[0].predictor - c->status[1].predictor;
  756. }
  757. break;
  758. case CODEC_ID_ADPCM_IMA_WS:
  759. /* no per-block initialization; just start decoding the data */
  760. while (src < buf + buf_size) {
  761. if (st) {
  762. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  763. (src[0] >> 4) & 0x0F, 3);
  764. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  765. src[0] & 0x0F, 3);
  766. } else {
  767. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  768. (src[0] >> 4) & 0x0F, 3);
  769. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  770. src[0] & 0x0F, 3);
  771. }
  772. src++;
  773. }
  774. break;
  775. case CODEC_ID_ADPCM_XA:
  776. c->status[0].sample1 = c->status[0].sample2 =
  777. c->status[1].sample1 = c->status[1].sample2 = 0;
  778. while (buf_size >= 128) {
  779. xa_decode(samples, src, &c->status[0], &c->status[1],
  780. avctx->channels);
  781. src += 128;
  782. samples += 28 * 8;
  783. buf_size -= 128;
  784. }
  785. break;
  786. case CODEC_ID_ADPCM_EA:
  787. samples_in_chunk = LE_32(src);
  788. if (samples_in_chunk >= ((buf_size - 12) * 2)) {
  789. src += buf_size;
  790. break;
  791. }
  792. src += 4;
  793. current_left_sample = (int16_t)LE_16(src);
  794. src += 2;
  795. previous_left_sample = (int16_t)LE_16(src);
  796. src += 2;
  797. current_right_sample = (int16_t)LE_16(src);
  798. src += 2;
  799. previous_right_sample = (int16_t)LE_16(src);
  800. src += 2;
  801. for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
  802. coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
  803. coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
  804. coeff1r = ea_adpcm_table[*src & 0x0F];
  805. coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
  806. src++;
  807. shift_left = ((*src >> 4) & 0x0F) + 8;
  808. shift_right = (*src & 0x0F) + 8;
  809. src++;
  810. for (count2 = 0; count2 < 28; count2++) {
  811. next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
  812. next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
  813. src++;
  814. next_left_sample = (next_left_sample +
  815. (current_left_sample * coeff1l) +
  816. (previous_left_sample * coeff2l) + 0x80) >> 8;
  817. next_right_sample = (next_right_sample +
  818. (current_right_sample * coeff1r) +
  819. (previous_right_sample * coeff2r) + 0x80) >> 8;
  820. CLAMP_TO_SHORT(next_left_sample);
  821. CLAMP_TO_SHORT(next_right_sample);
  822. previous_left_sample = current_left_sample;
  823. current_left_sample = next_left_sample;
  824. previous_right_sample = current_right_sample;
  825. current_right_sample = next_right_sample;
  826. *samples++ = (unsigned short)current_left_sample;
  827. *samples++ = (unsigned short)current_right_sample;
  828. }
  829. }
  830. break;
  831. case CODEC_ID_ADPCM_IMA_SMJPEG:
  832. c->status[0].predictor = *src;
  833. src += 2;
  834. c->status[0].step_index = *src++;
  835. src++; /* skip another byte before getting to the meat */
  836. while (src < buf + buf_size) {
  837. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  838. *src & 0x0F, 3);
  839. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  840. (*src >> 4) & 0x0F, 3);
  841. src++;
  842. }
  843. break;
  844. case CODEC_ID_ADPCM_CT:
  845. while (src < buf + buf_size) {
  846. if (st) {
  847. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  848. (src[0] >> 4) & 0x0F);
  849. *samples++ = adpcm_ct_expand_nibble(&c->status[1],
  850. src[0] & 0x0F);
  851. } else {
  852. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  853. (src[0] >> 4) & 0x0F);
  854. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  855. src[0] & 0x0F);
  856. }
  857. src++;
  858. }
  859. break;
  860. case CODEC_ID_ADPCM_SBPRO_4:
  861. case CODEC_ID_ADPCM_SBPRO_3:
  862. case CODEC_ID_ADPCM_SBPRO_2:
  863. if (!c->status[0].step_index) {
  864. /* the first byte is a raw sample */
  865. *samples++ = 128 * (*src++ - 0x80);
  866. if (st)
  867. *samples++ = 128 * (*src++ - 0x80);
  868. c->status[0].step_index = 1;
  869. }
  870. if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
  871. while (src < buf + buf_size) {
  872. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  873. (src[0] >> 4) & 0x0F, 4, 0);
  874. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  875. src[0] & 0x0F, 4, 0);
  876. src++;
  877. }
  878. } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
  879. while (src < buf + buf_size) {
  880. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  881. (src[0] >> 5) & 0x07, 3, 0);
  882. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  883. (src[0] >> 2) & 0x07, 3, 0);
  884. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  885. src[0] & 0x03, 2, 0);
  886. src++;
  887. }
  888. } else {
  889. while (src < buf + buf_size) {
  890. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  891. (src[0] >> 6) & 0x03, 2, 2);
  892. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  893. (src[0] >> 4) & 0x03, 2, 2);
  894. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  895. (src[0] >> 2) & 0x03, 2, 2);
  896. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  897. src[0] & 0x03, 2, 2);
  898. src++;
  899. }
  900. }
  901. break;
  902. case CODEC_ID_ADPCM_SWF:
  903. {
  904. GetBitContext gb;
  905. const int *table;
  906. int k0, signmask;
  907. int size = buf_size*8;
  908. init_get_bits(&gb, buf, size);
  909. // first frame, read bits & inital values
  910. if (!c->nb_bits)
  911. {
  912. c->nb_bits = get_bits(&gb, 2)+2;
  913. // av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", c->nb_bits);
  914. }
  915. table = swf_index_tables[c->nb_bits-2];
  916. k0 = 1 << (c->nb_bits-2);
  917. signmask = 1 << (c->nb_bits-1);
  918. while (get_bits_count(&gb) <= size)
  919. {
  920. int i;
  921. c->nb_samples++;
  922. // wrap around at every 4096 samples...
  923. if ((c->nb_samples & 0xfff) == 1)
  924. {
  925. for (i = 0; i <= st; i++)
  926. {
  927. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  928. c->status[i].step_index = get_bits(&gb, 6);
  929. }
  930. }
  931. // similar to IMA adpcm
  932. for (i = 0; i <= st; i++)
  933. {
  934. int delta = get_bits(&gb, c->nb_bits);
  935. int step = step_table[c->status[i].step_index];
  936. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  937. int k = k0;
  938. do {
  939. if (delta & k)
  940. vpdiff += step;
  941. step >>= 1;
  942. k >>= 1;
  943. } while(k);
  944. vpdiff += step;
  945. if (delta & signmask)
  946. c->status[i].predictor -= vpdiff;
  947. else
  948. c->status[i].predictor += vpdiff;
  949. c->status[i].step_index += table[delta & (~signmask)];
  950. c->status[i].step_index = clip(c->status[i].step_index, 0, 88);
  951. c->status[i].predictor = clip(c->status[i].predictor, -32768, 32767);
  952. *samples++ = c->status[i].predictor;
  953. }
  954. }
  955. // src += get_bits_count(&gb)*8;
  956. src += size;
  957. break;
  958. }
  959. case CODEC_ID_ADPCM_YAMAHA:
  960. while (src < buf + buf_size) {
  961. if (st) {
  962. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  963. src[0] & 0x0F);
  964. *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
  965. (src[0] >> 4) & 0x0F);
  966. } else {
  967. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  968. src[0] & 0x0F);
  969. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  970. (src[0] >> 4) & 0x0F);
  971. }
  972. src++;
  973. }
  974. break;
  975. default:
  976. return -1;
  977. }
  978. *data_size = (uint8_t *)samples - (uint8_t *)data;
  979. return src - buf;
  980. }
  981. #ifdef CONFIG_ENCODERS
  982. #define ADPCM_ENCODER(id,name) \
  983. AVCodec name ## _encoder = { \
  984. #name, \
  985. CODEC_TYPE_AUDIO, \
  986. id, \
  987. sizeof(ADPCMContext), \
  988. adpcm_encode_init, \
  989. adpcm_encode_frame, \
  990. adpcm_encode_close, \
  991. NULL, \
  992. };
  993. #else
  994. #define ADPCM_ENCODER(id,name)
  995. #endif
  996. #ifdef CONFIG_DECODERS
  997. #define ADPCM_DECODER(id,name) \
  998. AVCodec name ## _decoder = { \
  999. #name, \
  1000. CODEC_TYPE_AUDIO, \
  1001. id, \
  1002. sizeof(ADPCMContext), \
  1003. adpcm_decode_init, \
  1004. NULL, \
  1005. NULL, \
  1006. adpcm_decode_frame, \
  1007. };
  1008. #else
  1009. #define ADPCM_DECODER(id,name)
  1010. #endif
  1011. #define ADPCM_CODEC(id, name) \
  1012. ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
  1013. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
  1014. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
  1015. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
  1016. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
  1017. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
  1018. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
  1019. ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
  1020. ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
  1021. ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
  1022. ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx);
  1023. ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
  1024. ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
  1025. ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);
  1026. ADPCM_CODEC(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);
  1027. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4);
  1028. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3);
  1029. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2);
  1030. #undef ADPCM_CODEC