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.

1194 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){
  586. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  587. cs->step_index = 88;
  588. }
  589. cs->step = step_table[cs->step_index];
  590. if (st && channel)
  591. samples++;
  592. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  593. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
  594. samples += avctx->channels;
  595. *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
  596. samples += avctx->channels;
  597. src ++;
  598. }
  599. if(st) { /* handle stereo interlacing */
  600. c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
  601. if(channel == 1) { /* wait for the other packet before outputing anything */
  602. return src - buf;
  603. }
  604. }
  605. break;
  606. case CODEC_ID_ADPCM_IMA_WAV:
  607. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  608. buf_size = avctx->block_align;
  609. // samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;
  610. for(i=0; i<avctx->channels; i++){
  611. cs = &(c->status[i]);
  612. cs->predictor = (int16_t)(src[0] + (src[1]<<8));
  613. src+=2;
  614. // XXX: is this correct ??: *samples++ = cs->predictor;
  615. cs->step_index = *src++;
  616. if (cs->step_index > 88){
  617. av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  618. cs->step_index = 88;
  619. }
  620. if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */
  621. }
  622. while(src < buf + buf_size){
  623. for(m=0; m<4; m++){
  624. for(i=0; i<=st; i++)
  625. *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);
  626. for(i=0; i<=st; i++)
  627. *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4 , 3);
  628. src++;
  629. }
  630. src += 4*st;
  631. }
  632. break;
  633. case CODEC_ID_ADPCM_4XM:
  634. cs = &(c->status[0]);
  635. c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  636. if(st){
  637. c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  638. }
  639. c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  640. if(st){
  641. c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  642. }
  643. if (cs->step_index < 0) cs->step_index = 0;
  644. if (cs->step_index > 88) cs->step_index = 88;
  645. m= (buf_size - (src - buf))>>st;
  646. for(i=0; i<m; i++) {
  647. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
  648. if (st)
  649. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
  650. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
  651. if (st)
  652. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
  653. }
  654. src += m<<st;
  655. break;
  656. case CODEC_ID_ADPCM_MS:
  657. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  658. buf_size = avctx->block_align;
  659. n = buf_size - 7 * avctx->channels;
  660. if (n < 0)
  661. return -1;
  662. block_predictor[0] = clip(*src++, 0, 7);
  663. block_predictor[1] = 0;
  664. if (st)
  665. block_predictor[1] = clip(*src++, 0, 7);
  666. c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  667. src+=2;
  668. if (st){
  669. c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  670. src+=2;
  671. }
  672. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  673. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  674. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  675. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  676. c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  677. src+=2;
  678. if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  679. if (st) src+=2;
  680. c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  681. src+=2;
  682. if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  683. if (st) src+=2;
  684. *samples++ = c->status[0].sample1;
  685. if (st) *samples++ = c->status[1].sample1;
  686. *samples++ = c->status[0].sample2;
  687. if (st) *samples++ = c->status[1].sample2;
  688. for(;n>0;n--) {
  689. *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  690. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  691. src ++;
  692. }
  693. break;
  694. case CODEC_ID_ADPCM_IMA_DK4:
  695. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  696. buf_size = avctx->block_align;
  697. c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
  698. c->status[0].step_index = src[2];
  699. src += 4;
  700. *samples++ = c->status[0].predictor;
  701. if (st) {
  702. c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
  703. c->status[1].step_index = src[2];
  704. src += 4;
  705. *samples++ = c->status[1].predictor;
  706. }
  707. while (src < buf + buf_size) {
  708. /* take care of the top nibble (always left or mono channel) */
  709. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  710. (src[0] >> 4) & 0x0F, 3);
  711. /* take care of the bottom nibble, which is right sample for
  712. * stereo, or another mono sample */
  713. if (st)
  714. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  715. src[0] & 0x0F, 3);
  716. else
  717. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  718. src[0] & 0x0F, 3);
  719. src++;
  720. }
  721. break;
  722. case CODEC_ID_ADPCM_IMA_DK3:
  723. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  724. buf_size = avctx->block_align;
  725. c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
  726. c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
  727. c->status[0].step_index = src[14];
  728. c->status[1].step_index = src[15];
  729. /* sign extend the predictors */
  730. src += 16;
  731. diff_channel = c->status[1].predictor;
  732. /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
  733. * the buffer is consumed */
  734. while (1) {
  735. /* for this algorithm, c->status[0] is the sum channel and
  736. * c->status[1] is the diff channel */
  737. /* process the first predictor of the sum channel */
  738. DK3_GET_NEXT_NIBBLE();
  739. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  740. /* process the diff channel predictor */
  741. DK3_GET_NEXT_NIBBLE();
  742. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  743. /* process the first pair of stereo PCM samples */
  744. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  745. *samples++ = c->status[0].predictor + c->status[1].predictor;
  746. *samples++ = c->status[0].predictor - c->status[1].predictor;
  747. /* process the second predictor of the sum channel */
  748. DK3_GET_NEXT_NIBBLE();
  749. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  750. /* process the second pair of stereo PCM samples */
  751. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  752. *samples++ = c->status[0].predictor + c->status[1].predictor;
  753. *samples++ = c->status[0].predictor - c->status[1].predictor;
  754. }
  755. break;
  756. case CODEC_ID_ADPCM_IMA_WS:
  757. /* no per-block initialization; just start decoding the data */
  758. while (src < buf + buf_size) {
  759. if (st) {
  760. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  761. (src[0] >> 4) & 0x0F, 3);
  762. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  763. src[0] & 0x0F, 3);
  764. } else {
  765. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  766. (src[0] >> 4) & 0x0F, 3);
  767. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  768. src[0] & 0x0F, 3);
  769. }
  770. src++;
  771. }
  772. break;
  773. case CODEC_ID_ADPCM_XA:
  774. c->status[0].sample1 = c->status[0].sample2 =
  775. c->status[1].sample1 = c->status[1].sample2 = 0;
  776. while (buf_size >= 128) {
  777. xa_decode(samples, src, &c->status[0], &c->status[1],
  778. avctx->channels);
  779. src += 128;
  780. samples += 28 * 8;
  781. buf_size -= 128;
  782. }
  783. break;
  784. case CODEC_ID_ADPCM_EA:
  785. samples_in_chunk = LE_32(src);
  786. if (samples_in_chunk >= ((buf_size - 12) * 2)) {
  787. src += buf_size;
  788. break;
  789. }
  790. src += 4;
  791. current_left_sample = (int16_t)LE_16(src);
  792. src += 2;
  793. previous_left_sample = (int16_t)LE_16(src);
  794. src += 2;
  795. current_right_sample = (int16_t)LE_16(src);
  796. src += 2;
  797. previous_right_sample = (int16_t)LE_16(src);
  798. src += 2;
  799. for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
  800. coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
  801. coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
  802. coeff1r = ea_adpcm_table[*src & 0x0F];
  803. coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
  804. src++;
  805. shift_left = ((*src >> 4) & 0x0F) + 8;
  806. shift_right = (*src & 0x0F) + 8;
  807. src++;
  808. for (count2 = 0; count2 < 28; count2++) {
  809. next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
  810. next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
  811. src++;
  812. next_left_sample = (next_left_sample +
  813. (current_left_sample * coeff1l) +
  814. (previous_left_sample * coeff2l) + 0x80) >> 8;
  815. next_right_sample = (next_right_sample +
  816. (current_right_sample * coeff1r) +
  817. (previous_right_sample * coeff2r) + 0x80) >> 8;
  818. CLAMP_TO_SHORT(next_left_sample);
  819. CLAMP_TO_SHORT(next_right_sample);
  820. previous_left_sample = current_left_sample;
  821. current_left_sample = next_left_sample;
  822. previous_right_sample = current_right_sample;
  823. current_right_sample = next_right_sample;
  824. *samples++ = (unsigned short)current_left_sample;
  825. *samples++ = (unsigned short)current_right_sample;
  826. }
  827. }
  828. break;
  829. case CODEC_ID_ADPCM_IMA_SMJPEG:
  830. c->status[0].predictor = *src;
  831. src += 2;
  832. c->status[0].step_index = *src++;
  833. src++; /* skip another byte before getting to the meat */
  834. while (src < buf + buf_size) {
  835. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  836. *src & 0x0F, 3);
  837. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  838. (*src >> 4) & 0x0F, 3);
  839. src++;
  840. }
  841. break;
  842. case CODEC_ID_ADPCM_CT:
  843. while (src < buf + buf_size) {
  844. if (st) {
  845. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  846. (src[0] >> 4) & 0x0F);
  847. *samples++ = adpcm_ct_expand_nibble(&c->status[1],
  848. src[0] & 0x0F);
  849. } else {
  850. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  851. (src[0] >> 4) & 0x0F);
  852. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  853. src[0] & 0x0F);
  854. }
  855. src++;
  856. }
  857. break;
  858. case CODEC_ID_ADPCM_SBPRO_4:
  859. case CODEC_ID_ADPCM_SBPRO_3:
  860. case CODEC_ID_ADPCM_SBPRO_2:
  861. if (!c->status[0].step_index) {
  862. /* the first byte is a raw sample */
  863. *samples++ = 128 * (*src++ - 0x80);
  864. if (st)
  865. *samples++ = 128 * (*src++ - 0x80);
  866. c->status[0].step_index = 1;
  867. }
  868. if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_4) {
  869. while (src < buf + buf_size) {
  870. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  871. (src[0] >> 4) & 0x0F, 4, 0);
  872. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  873. src[0] & 0x0F, 4, 0);
  874. src++;
  875. }
  876. } else if (avctx->codec->id == CODEC_ID_ADPCM_SBPRO_3) {
  877. while (src < buf + buf_size) {
  878. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  879. (src[0] >> 5) & 0x07, 3, 0);
  880. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  881. (src[0] >> 2) & 0x07, 3, 0);
  882. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  883. src[0] & 0x03, 2, 0);
  884. src++;
  885. }
  886. } else {
  887. while (src < buf + buf_size) {
  888. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  889. (src[0] >> 6) & 0x03, 2, 2);
  890. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  891. (src[0] >> 4) & 0x03, 2, 2);
  892. *samples++ = adpcm_sbpro_expand_nibble(&c->status[0],
  893. (src[0] >> 2) & 0x03, 2, 2);
  894. *samples++ = adpcm_sbpro_expand_nibble(&c->status[st],
  895. src[0] & 0x03, 2, 2);
  896. src++;
  897. }
  898. }
  899. break;
  900. case CODEC_ID_ADPCM_SWF:
  901. {
  902. GetBitContext gb;
  903. const int *table;
  904. int k0, signmask;
  905. int size = buf_size*8;
  906. init_get_bits(&gb, buf, size);
  907. // first frame, read bits & inital values
  908. if (!c->nb_bits)
  909. {
  910. c->nb_bits = get_bits(&gb, 2)+2;
  911. // av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", c->nb_bits);
  912. }
  913. table = swf_index_tables[c->nb_bits-2];
  914. k0 = 1 << (c->nb_bits-2);
  915. signmask = 1 << (c->nb_bits-1);
  916. while (get_bits_count(&gb) <= size)
  917. {
  918. int i;
  919. c->nb_samples++;
  920. // wrap around at every 4096 samples...
  921. if ((c->nb_samples & 0xfff) == 1)
  922. {
  923. for (i = 0; i <= st; i++)
  924. {
  925. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  926. c->status[i].step_index = get_bits(&gb, 6);
  927. }
  928. }
  929. // similar to IMA adpcm
  930. for (i = 0; i <= st; i++)
  931. {
  932. int delta = get_bits(&gb, c->nb_bits);
  933. int step = step_table[c->status[i].step_index];
  934. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  935. int k = k0;
  936. do {
  937. if (delta & k)
  938. vpdiff += step;
  939. step >>= 1;
  940. k >>= 1;
  941. } while(k);
  942. vpdiff += step;
  943. if (delta & signmask)
  944. c->status[i].predictor -= vpdiff;
  945. else
  946. c->status[i].predictor += vpdiff;
  947. c->status[i].step_index += table[delta & (~signmask)];
  948. c->status[i].step_index = clip(c->status[i].step_index, 0, 88);
  949. c->status[i].predictor = clip(c->status[i].predictor, -32768, 32767);
  950. *samples++ = c->status[i].predictor;
  951. }
  952. }
  953. // src += get_bits_count(&gb)*8;
  954. src += size;
  955. break;
  956. }
  957. case CODEC_ID_ADPCM_YAMAHA:
  958. while (src < buf + buf_size) {
  959. if (st) {
  960. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  961. src[0] & 0x0F);
  962. *samples++ = adpcm_yamaha_expand_nibble(&c->status[1],
  963. (src[0] >> 4) & 0x0F);
  964. } else {
  965. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  966. src[0] & 0x0F);
  967. *samples++ = adpcm_yamaha_expand_nibble(&c->status[0],
  968. (src[0] >> 4) & 0x0F);
  969. }
  970. src++;
  971. }
  972. break;
  973. default:
  974. return -1;
  975. }
  976. *data_size = (uint8_t *)samples - (uint8_t *)data;
  977. return src - buf;
  978. }
  979. #ifdef CONFIG_ENCODERS
  980. #define ADPCM_ENCODER(id,name) \
  981. AVCodec name ## _encoder = { \
  982. #name, \
  983. CODEC_TYPE_AUDIO, \
  984. id, \
  985. sizeof(ADPCMContext), \
  986. adpcm_encode_init, \
  987. adpcm_encode_frame, \
  988. adpcm_encode_close, \
  989. NULL, \
  990. };
  991. #else
  992. #define ADPCM_ENCODER(id,name)
  993. #endif
  994. #ifdef CONFIG_DECODERS
  995. #define ADPCM_DECODER(id,name) \
  996. AVCodec name ## _decoder = { \
  997. #name, \
  998. CODEC_TYPE_AUDIO, \
  999. id, \
  1000. sizeof(ADPCMContext), \
  1001. adpcm_decode_init, \
  1002. NULL, \
  1003. NULL, \
  1004. adpcm_decode_frame, \
  1005. };
  1006. #else
  1007. #define ADPCM_DECODER(id,name)
  1008. #endif
  1009. #define ADPCM_CODEC(id, name) \
  1010. ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
  1011. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
  1012. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
  1013. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
  1014. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
  1015. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
  1016. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
  1017. ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
  1018. ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
  1019. ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
  1020. ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx);
  1021. ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
  1022. ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
  1023. ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);
  1024. ADPCM_CODEC(CODEC_ID_ADPCM_YAMAHA, adpcm_yamaha);
  1025. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_4, adpcm_sbpro_4);
  1026. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_3, adpcm_sbpro_3);
  1027. ADPCM_CODEC(CODEC_ID_ADPCM_SBPRO_2, adpcm_sbpro_2);
  1028. #undef ADPCM_CODEC