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.

1040 lines
33KB

  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. /* end of tables */
  108. typedef struct ADPCMChannelStatus {
  109. int predictor;
  110. short int step_index;
  111. int step;
  112. /* for encoding */
  113. int prev_sample;
  114. /* MS version */
  115. short sample1;
  116. short sample2;
  117. int coeff1;
  118. int coeff2;
  119. int idelta;
  120. } ADPCMChannelStatus;
  121. typedef struct ADPCMContext {
  122. int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
  123. ADPCMChannelStatus status[2];
  124. short sample_buffer[32]; /* hold left samples while waiting for right samples */
  125. /* SWF only */
  126. int nb_bits;
  127. int nb_samples;
  128. } ADPCMContext;
  129. /* XXX: implement encoding */
  130. #ifdef CONFIG_ENCODERS
  131. static int adpcm_encode_init(AVCodecContext *avctx)
  132. {
  133. if (avctx->channels > 2)
  134. return -1; /* only stereo or mono =) */
  135. switch(avctx->codec->id) {
  136. case CODEC_ID_ADPCM_IMA_QT:
  137. av_log(avctx, AV_LOG_ERROR, "ADPCM: codec adpcm_ima_qt unsupported for encoding !\n");
  138. avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */
  139. return -1;
  140. break;
  141. case CODEC_ID_ADPCM_IMA_WAV:
  142. avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
  143. /* and we have 4 bytes per channel overhead */
  144. avctx->block_align = BLKSIZE;
  145. /* seems frame_size isn't taken into account... have to buffer the samples :-( */
  146. break;
  147. case CODEC_ID_ADPCM_MS:
  148. avctx->frame_size = (BLKSIZE - 7 * avctx->channels) * 2 / avctx->channels + 2; /* each 16 bits sample gives one nibble */
  149. /* and we have 7 bytes per channel overhead */
  150. avctx->block_align = BLKSIZE;
  151. break;
  152. default:
  153. return -1;
  154. break;
  155. }
  156. avctx->coded_frame= avcodec_alloc_frame();
  157. avctx->coded_frame->key_frame= 1;
  158. return 0;
  159. }
  160. static int adpcm_encode_close(AVCodecContext *avctx)
  161. {
  162. av_freep(&avctx->coded_frame);
  163. return 0;
  164. }
  165. static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
  166. {
  167. int step_index;
  168. unsigned char nibble;
  169. int sign = 0; /* sign bit of the nibble (MSB) */
  170. int delta, predicted_delta;
  171. delta = sample - c->prev_sample;
  172. if (delta < 0) {
  173. sign = 1;
  174. delta = -delta;
  175. }
  176. step_index = c->step_index;
  177. /* nibble = 4 * delta / step_table[step_index]; */
  178. nibble = (delta << 2) / step_table[step_index];
  179. if (nibble > 7)
  180. nibble = 7;
  181. step_index += index_table[nibble];
  182. if (step_index < 0)
  183. step_index = 0;
  184. if (step_index > 88)
  185. step_index = 88;
  186. /* what the decoder will find */
  187. predicted_delta = ((step_table[step_index] * nibble) / 4) + (step_table[step_index] / 8);
  188. if (sign)
  189. c->prev_sample -= predicted_delta;
  190. else
  191. c->prev_sample += predicted_delta;
  192. CLAMP_TO_SHORT(c->prev_sample);
  193. nibble += sign << 3; /* sign * 8 */
  194. /* save back */
  195. c->step_index = step_index;
  196. return nibble;
  197. }
  198. static inline unsigned char adpcm_ms_compress_sample(ADPCMChannelStatus *c, short sample)
  199. {
  200. int predictor, nibble, bias;
  201. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
  202. nibble= sample - predictor;
  203. if(nibble>=0) bias= c->idelta/2;
  204. else bias=-c->idelta/2;
  205. nibble= (nibble + bias) / c->idelta;
  206. nibble= clip(nibble, -8, 7)&0x0F;
  207. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  208. CLAMP_TO_SHORT(predictor);
  209. c->sample2 = c->sample1;
  210. c->sample1 = predictor;
  211. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
  212. if (c->idelta < 16) c->idelta = 16;
  213. return nibble;
  214. }
  215. static int adpcm_encode_frame(AVCodecContext *avctx,
  216. unsigned char *frame, int buf_size, void *data)
  217. {
  218. int n, i, st;
  219. short *samples;
  220. unsigned char *dst;
  221. ADPCMContext *c = avctx->priv_data;
  222. dst = frame;
  223. samples = (short *)data;
  224. st= avctx->channels == 2;
  225. /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
  226. switch(avctx->codec->id) {
  227. case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
  228. break;
  229. case CODEC_ID_ADPCM_IMA_WAV:
  230. n = avctx->frame_size / 8;
  231. c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
  232. /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
  233. *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
  234. *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
  235. *dst++ = (unsigned char)c->status[0].step_index;
  236. *dst++ = 0; /* unknown */
  237. samples++;
  238. if (avctx->channels == 2) {
  239. c->status[1].prev_sample = (signed short)samples[1];
  240. /* c->status[1].step_index = 0; */
  241. *dst++ = (c->status[1].prev_sample) & 0xFF;
  242. *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
  243. *dst++ = (unsigned char)c->status[1].step_index;
  244. *dst++ = 0;
  245. samples++;
  246. }
  247. /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
  248. for (; n>0; n--) {
  249. *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F;
  250. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0;
  251. dst++;
  252. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F;
  253. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0;
  254. dst++;
  255. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F;
  256. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0;
  257. dst++;
  258. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F;
  259. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0;
  260. dst++;
  261. /* right channel */
  262. if (avctx->channels == 2) {
  263. *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
  264. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
  265. dst++;
  266. *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
  267. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
  268. dst++;
  269. *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
  270. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
  271. dst++;
  272. *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
  273. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
  274. dst++;
  275. }
  276. samples += 8 * avctx->channels;
  277. }
  278. break;
  279. case CODEC_ID_ADPCM_MS:
  280. for(i=0; i<avctx->channels; i++){
  281. int predictor=0;
  282. *dst++ = predictor;
  283. c->status[i].coeff1 = AdaptCoeff1[predictor];
  284. c->status[i].coeff2 = AdaptCoeff2[predictor];
  285. }
  286. for(i=0; i<avctx->channels; i++){
  287. if (c->status[i].idelta < 16)
  288. c->status[i].idelta = 16;
  289. *dst++ = c->status[i].idelta & 0xFF;
  290. *dst++ = c->status[i].idelta >> 8;
  291. }
  292. for(i=0; i<avctx->channels; i++){
  293. c->status[i].sample1= *samples++;
  294. *dst++ = c->status[i].sample1 & 0xFF;
  295. *dst++ = c->status[i].sample1 >> 8;
  296. }
  297. for(i=0; i<avctx->channels; i++){
  298. c->status[i].sample2= *samples++;
  299. *dst++ = c->status[i].sample2 & 0xFF;
  300. *dst++ = c->status[i].sample2 >> 8;
  301. }
  302. for(i=7*avctx->channels; i<avctx->block_align; i++) {
  303. int nibble;
  304. nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;
  305. nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);
  306. *dst++ = nibble;
  307. }
  308. break;
  309. default:
  310. return -1;
  311. }
  312. return dst - frame;
  313. }
  314. #endif //CONFIG_ENCODERS
  315. static int adpcm_decode_init(AVCodecContext * avctx)
  316. {
  317. ADPCMContext *c = avctx->priv_data;
  318. c->channel = 0;
  319. c->status[0].predictor = c->status[1].predictor = 0;
  320. c->status[0].step_index = c->status[1].step_index = 0;
  321. c->status[0].step = c->status[1].step = 0;
  322. switch(avctx->codec->id) {
  323. case CODEC_ID_ADPCM_CT:
  324. c->status[0].step = c->status[1].step = 511;
  325. break;
  326. default:
  327. break;
  328. }
  329. return 0;
  330. }
  331. static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
  332. {
  333. int step_index;
  334. int predictor;
  335. int sign, delta, diff, step;
  336. step = step_table[c->step_index];
  337. step_index = c->step_index + index_table[(unsigned)nibble];
  338. if (step_index < 0) step_index = 0;
  339. else if (step_index > 88) step_index = 88;
  340. sign = nibble & 8;
  341. delta = nibble & 7;
  342. /* perform direct multiplication instead of series of jumps proposed by
  343. * the reference ADPCM implementation since modern CPUs can do the mults
  344. * quickly enough */
  345. diff = ((2 * delta + 1) * step) >> shift;
  346. predictor = c->predictor;
  347. if (sign) predictor -= diff;
  348. else predictor += diff;
  349. CLAMP_TO_SHORT(predictor);
  350. c->predictor = predictor;
  351. c->step_index = step_index;
  352. return (short)predictor;
  353. }
  354. static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
  355. {
  356. int predictor;
  357. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
  358. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  359. CLAMP_TO_SHORT(predictor);
  360. c->sample2 = c->sample1;
  361. c->sample1 = predictor;
  362. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
  363. if (c->idelta < 16) c->idelta = 16;
  364. return (short)predictor;
  365. }
  366. static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
  367. {
  368. int predictor;
  369. int sign, delta, diff;
  370. int new_step;
  371. sign = nibble & 8;
  372. delta = nibble & 7;
  373. /* perform direct multiplication instead of series of jumps proposed by
  374. * the reference ADPCM implementation since modern CPUs can do the mults
  375. * quickly enough */
  376. diff = ((2 * delta + 1) * c->step) >> 3;
  377. predictor = c->predictor;
  378. /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
  379. if(sign)
  380. predictor = ((predictor * 254) >> 8) - diff;
  381. else
  382. predictor = ((predictor * 254) >> 8) + diff;
  383. /* calculate new step and clamp it to range 511..32767 */
  384. new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
  385. c->step = new_step;
  386. if(c->step < 511)
  387. c->step = 511;
  388. if(c->step > 32767)
  389. c->step = 32767;
  390. CLAMP_TO_SHORT(predictor);
  391. c->predictor = predictor;
  392. return (short)predictor;
  393. }
  394. static void xa_decode(short *out, const unsigned char *in,
  395. ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
  396. {
  397. int i, j;
  398. int shift,filter,f0,f1;
  399. int s_1,s_2;
  400. int d,s,t;
  401. for(i=0;i<4;i++) {
  402. shift = 12 - (in[4+i*2] & 15);
  403. filter = in[4+i*2] >> 4;
  404. f0 = xa_adpcm_table[filter][0];
  405. f1 = xa_adpcm_table[filter][1];
  406. s_1 = left->sample1;
  407. s_2 = left->sample2;
  408. for(j=0;j<28;j++) {
  409. d = in[16+i+j*4];
  410. t = (signed char)(d<<4)>>4;
  411. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  412. CLAMP_TO_SHORT(s);
  413. *out = s;
  414. out += inc;
  415. s_2 = s_1;
  416. s_1 = s;
  417. }
  418. if (inc==2) { /* stereo */
  419. left->sample1 = s_1;
  420. left->sample2 = s_2;
  421. s_1 = right->sample1;
  422. s_2 = right->sample2;
  423. out = out + 1 - 28*2;
  424. }
  425. shift = 12 - (in[5+i*2] & 15);
  426. filter = in[5+i*2] >> 4;
  427. f0 = xa_adpcm_table[filter][0];
  428. f1 = xa_adpcm_table[filter][1];
  429. for(j=0;j<28;j++) {
  430. d = in[16+i+j*4];
  431. t = (signed char)d >> 4;
  432. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  433. CLAMP_TO_SHORT(s);
  434. *out = s;
  435. out += inc;
  436. s_2 = s_1;
  437. s_1 = s;
  438. }
  439. if (inc==2) { /* stereo */
  440. right->sample1 = s_1;
  441. right->sample2 = s_2;
  442. out -= 1;
  443. } else {
  444. left->sample1 = s_1;
  445. left->sample2 = s_2;
  446. }
  447. }
  448. }
  449. /* DK3 ADPCM support macro */
  450. #define DK3_GET_NEXT_NIBBLE() \
  451. if (decode_top_nibble_next) \
  452. { \
  453. nibble = (last_byte >> 4) & 0x0F; \
  454. decode_top_nibble_next = 0; \
  455. } \
  456. else \
  457. { \
  458. last_byte = *src++; \
  459. if (src >= buf + buf_size) break; \
  460. nibble = last_byte & 0x0F; \
  461. decode_top_nibble_next = 1; \
  462. }
  463. static int adpcm_decode_frame(AVCodecContext *avctx,
  464. void *data, int *data_size,
  465. uint8_t *buf, int buf_size)
  466. {
  467. ADPCMContext *c = avctx->priv_data;
  468. ADPCMChannelStatus *cs;
  469. int n, m, channel, i;
  470. int block_predictor[2];
  471. short *samples;
  472. uint8_t *src;
  473. int st; /* stereo */
  474. /* DK3 ADPCM accounting variables */
  475. unsigned char last_byte = 0;
  476. unsigned char nibble;
  477. int decode_top_nibble_next = 0;
  478. int diff_channel;
  479. /* EA ADPCM state variables */
  480. uint32_t samples_in_chunk;
  481. int32_t previous_left_sample, previous_right_sample;
  482. int32_t current_left_sample, current_right_sample;
  483. int32_t next_left_sample, next_right_sample;
  484. int32_t coeff1l, coeff2l, coeff1r, coeff2r;
  485. uint8_t shift_left, shift_right;
  486. int count1, count2;
  487. if (!buf_size)
  488. return 0;
  489. samples = data;
  490. src = buf;
  491. st = avctx->channels == 2;
  492. switch(avctx->codec->id) {
  493. case CODEC_ID_ADPCM_IMA_QT:
  494. n = (buf_size - 2);/* >> 2*avctx->channels;*/
  495. channel = c->channel;
  496. cs = &(c->status[channel]);
  497. /* (pppppp) (piiiiiii) */
  498. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  499. cs->predictor = (*src++) << 8;
  500. cs->predictor |= (*src & 0x80);
  501. cs->predictor &= 0xFF80;
  502. /* sign extension */
  503. if(cs->predictor & 0x8000)
  504. cs->predictor -= 0x10000;
  505. CLAMP_TO_SHORT(cs->predictor);
  506. cs->step_index = (*src++) & 0x7F;
  507. if (cs->step_index > 88) av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  508. if (cs->step_index > 88) cs->step_index = 88;
  509. cs->step = step_table[cs->step_index];
  510. if (st && channel)
  511. samples++;
  512. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  513. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
  514. samples += avctx->channels;
  515. *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
  516. samples += avctx->channels;
  517. src ++;
  518. }
  519. if(st) { /* handle stereo interlacing */
  520. c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
  521. if(channel == 1) { /* wait for the other packet before outputing anything */
  522. return src - buf;
  523. }
  524. }
  525. break;
  526. case CODEC_ID_ADPCM_IMA_WAV:
  527. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  528. buf_size = avctx->block_align;
  529. for(i=0; i<avctx->channels; i++){
  530. cs = &(c->status[i]);
  531. cs->predictor = *src++;
  532. cs->predictor |= (*src++) << 8;
  533. if(cs->predictor & 0x8000)
  534. cs->predictor -= 0x10000;
  535. CLAMP_TO_SHORT(cs->predictor);
  536. // XXX: is this correct ??: *samples++ = cs->predictor;
  537. cs->step_index = *src++;
  538. if (cs->step_index < 0) cs->step_index = 0;
  539. if (cs->step_index > 88) cs->step_index = 88;
  540. if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null !!\n"); /* unused */
  541. }
  542. for(m=4; src < (buf + buf_size);) {
  543. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F, 3);
  544. if (st)
  545. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F, 3);
  546. *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F, 3);
  547. if (st) {
  548. *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F, 3);
  549. if (!--m) {
  550. m=4;
  551. src+=4;
  552. }
  553. }
  554. src++;
  555. }
  556. break;
  557. case CODEC_ID_ADPCM_4XM:
  558. cs = &(c->status[0]);
  559. c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  560. if(st){
  561. c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  562. }
  563. c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  564. if(st){
  565. c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  566. }
  567. if (cs->step_index < 0) cs->step_index = 0;
  568. if (cs->step_index > 88) cs->step_index = 88;
  569. m= (buf_size - (src - buf))>>st;
  570. for(i=0; i<m; i++) {
  571. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
  572. if (st)
  573. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
  574. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
  575. if (st)
  576. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
  577. }
  578. src += m<<st;
  579. break;
  580. case CODEC_ID_ADPCM_MS:
  581. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  582. buf_size = avctx->block_align;
  583. n = buf_size - 7 * avctx->channels;
  584. if (n < 0)
  585. return -1;
  586. block_predictor[0] = clip(*src++, 0, 7);
  587. block_predictor[1] = 0;
  588. if (st)
  589. block_predictor[1] = clip(*src++, 0, 7);
  590. c->status[0].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  591. src+=2;
  592. if (st){
  593. c->status[1].idelta = (int16_t)((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  594. src+=2;
  595. }
  596. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  597. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  598. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  599. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  600. c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  601. src+=2;
  602. if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  603. if (st) src+=2;
  604. c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  605. src+=2;
  606. if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  607. if (st) src+=2;
  608. *samples++ = c->status[0].sample1;
  609. if (st) *samples++ = c->status[1].sample1;
  610. *samples++ = c->status[0].sample2;
  611. if (st) *samples++ = c->status[1].sample2;
  612. for(;n>0;n--) {
  613. *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  614. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  615. src ++;
  616. }
  617. break;
  618. case CODEC_ID_ADPCM_IMA_DK4:
  619. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  620. buf_size = avctx->block_align;
  621. c->status[0].predictor = (int16_t)(src[0] | (src[1] << 8));
  622. c->status[0].step_index = src[2];
  623. src += 4;
  624. *samples++ = c->status[0].predictor;
  625. if (st) {
  626. c->status[1].predictor = (int16_t)(src[0] | (src[1] << 8));
  627. c->status[1].step_index = src[2];
  628. src += 4;
  629. *samples++ = c->status[1].predictor;
  630. }
  631. while (src < buf + buf_size) {
  632. /* take care of the top nibble (always left or mono channel) */
  633. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  634. (src[0] >> 4) & 0x0F, 3);
  635. /* take care of the bottom nibble, which is right sample for
  636. * stereo, or another mono sample */
  637. if (st)
  638. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  639. src[0] & 0x0F, 3);
  640. else
  641. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  642. src[0] & 0x0F, 3);
  643. src++;
  644. }
  645. break;
  646. case CODEC_ID_ADPCM_IMA_DK3:
  647. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  648. buf_size = avctx->block_align;
  649. c->status[0].predictor = (int16_t)(src[10] | (src[11] << 8));
  650. c->status[1].predictor = (int16_t)(src[12] | (src[13] << 8));
  651. c->status[0].step_index = src[14];
  652. c->status[1].step_index = src[15];
  653. /* sign extend the predictors */
  654. src += 16;
  655. diff_channel = c->status[1].predictor;
  656. /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
  657. * the buffer is consumed */
  658. while (1) {
  659. /* for this algorithm, c->status[0] is the sum channel and
  660. * c->status[1] is the diff channel */
  661. /* process the first predictor of the sum channel */
  662. DK3_GET_NEXT_NIBBLE();
  663. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  664. /* process the diff channel predictor */
  665. DK3_GET_NEXT_NIBBLE();
  666. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  667. /* process the first pair of stereo PCM samples */
  668. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  669. *samples++ = c->status[0].predictor + c->status[1].predictor;
  670. *samples++ = c->status[0].predictor - c->status[1].predictor;
  671. /* process the second predictor of the sum channel */
  672. DK3_GET_NEXT_NIBBLE();
  673. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  674. /* process the second pair of stereo PCM samples */
  675. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  676. *samples++ = c->status[0].predictor + c->status[1].predictor;
  677. *samples++ = c->status[0].predictor - c->status[1].predictor;
  678. }
  679. break;
  680. case CODEC_ID_ADPCM_IMA_WS:
  681. /* no per-block initialization; just start decoding the data */
  682. while (src < buf + buf_size) {
  683. if (st) {
  684. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  685. (src[0] >> 4) & 0x0F, 3);
  686. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  687. src[0] & 0x0F, 3);
  688. } else {
  689. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  690. (src[0] >> 4) & 0x0F, 3);
  691. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  692. src[0] & 0x0F, 3);
  693. }
  694. src++;
  695. }
  696. break;
  697. case CODEC_ID_ADPCM_XA:
  698. c->status[0].sample1 = c->status[0].sample2 =
  699. c->status[1].sample1 = c->status[1].sample2 = 0;
  700. while (buf_size >= 128) {
  701. xa_decode(samples, src, &c->status[0], &c->status[1],
  702. avctx->channels);
  703. src += 128;
  704. samples += 28 * 8;
  705. buf_size -= 128;
  706. }
  707. break;
  708. case CODEC_ID_ADPCM_EA:
  709. samples_in_chunk = LE_32(src);
  710. if (samples_in_chunk >= ((buf_size - 12) * 2)) {
  711. src += buf_size;
  712. break;
  713. }
  714. src += 4;
  715. current_left_sample = (int16_t)LE_16(src);
  716. src += 2;
  717. previous_left_sample = (int16_t)LE_16(src);
  718. src += 2;
  719. current_right_sample = (int16_t)LE_16(src);
  720. src += 2;
  721. previous_right_sample = (int16_t)LE_16(src);
  722. src += 2;
  723. for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
  724. coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
  725. coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
  726. coeff1r = ea_adpcm_table[*src & 0x0F];
  727. coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
  728. src++;
  729. shift_left = ((*src >> 4) & 0x0F) + 8;
  730. shift_right = (*src & 0x0F) + 8;
  731. src++;
  732. for (count2 = 0; count2 < 28; count2++) {
  733. next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
  734. next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
  735. src++;
  736. next_left_sample = (next_left_sample +
  737. (current_left_sample * coeff1l) +
  738. (previous_left_sample * coeff2l) + 0x80) >> 8;
  739. next_right_sample = (next_right_sample +
  740. (current_right_sample * coeff1r) +
  741. (previous_right_sample * coeff2r) + 0x80) >> 8;
  742. CLAMP_TO_SHORT(next_left_sample);
  743. CLAMP_TO_SHORT(next_right_sample);
  744. previous_left_sample = current_left_sample;
  745. current_left_sample = next_left_sample;
  746. previous_right_sample = current_right_sample;
  747. current_right_sample = next_right_sample;
  748. *samples++ = (unsigned short)current_left_sample;
  749. *samples++ = (unsigned short)current_right_sample;
  750. }
  751. }
  752. break;
  753. case CODEC_ID_ADPCM_IMA_SMJPEG:
  754. c->status[0].predictor = *src;
  755. src += 2;
  756. c->status[0].step_index = *src++;
  757. src++; /* skip another byte before getting to the meat */
  758. while (src < buf + buf_size) {
  759. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  760. *src & 0x0F, 3);
  761. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  762. (*src >> 4) & 0x0F, 3);
  763. src++;
  764. }
  765. break;
  766. case CODEC_ID_ADPCM_CT:
  767. while (src < buf + buf_size) {
  768. if (st) {
  769. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  770. (src[0] >> 4) & 0x0F);
  771. *samples++ = adpcm_ct_expand_nibble(&c->status[1],
  772. src[0] & 0x0F);
  773. } else {
  774. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  775. (src[0] >> 4) & 0x0F);
  776. *samples++ = adpcm_ct_expand_nibble(&c->status[0],
  777. src[0] & 0x0F);
  778. }
  779. src++;
  780. }
  781. break;
  782. case CODEC_ID_ADPCM_SWF:
  783. {
  784. GetBitContext gb;
  785. const int *table;
  786. int k0, signmask;
  787. int size = buf_size*8;
  788. init_get_bits(&gb, buf, size);
  789. // first frame, read bits & inital values
  790. if (!c->nb_bits)
  791. {
  792. c->nb_bits = get_bits(&gb, 2)+2;
  793. // av_log(NULL,AV_LOG_INFO,"nb_bits: %d\n", c->nb_bits);
  794. }
  795. table = swf_index_tables[c->nb_bits-2];
  796. k0 = 1 << (c->nb_bits-2);
  797. signmask = 1 << (c->nb_bits-1);
  798. while (get_bits_count(&gb) <= size)
  799. {
  800. int i;
  801. c->nb_samples++;
  802. // wrap around at every 4096 samples...
  803. if ((c->nb_samples & 0xfff) == 1)
  804. {
  805. for (i = 0; i <= st; i++)
  806. {
  807. *samples++ = c->status[i].predictor = get_sbits(&gb, 16);
  808. c->status[i].step_index = get_bits(&gb, 6);
  809. }
  810. }
  811. // similar to IMA adpcm
  812. for (i = 0; i <= st; i++)
  813. {
  814. int delta = get_bits(&gb, c->nb_bits);
  815. int step = step_table[c->status[i].step_index];
  816. long vpdiff = 0; // vpdiff = (delta+0.5)*step/4
  817. int k = k0;
  818. do {
  819. if (delta & k)
  820. vpdiff += step;
  821. step >>= 1;
  822. k >>= 1;
  823. } while(k);
  824. vpdiff += step;
  825. if (delta & signmask)
  826. c->status[i].predictor -= vpdiff;
  827. else
  828. c->status[i].predictor += vpdiff;
  829. c->status[i].step_index += table[delta & (~signmask)];
  830. c->status[i].step_index = clip(c->status[i].step_index, 0, 88);
  831. c->status[i].predictor = clip(c->status[i].predictor, -32768, 32767);
  832. *samples++ = c->status[i].predictor;
  833. }
  834. }
  835. // src += get_bits_count(&gb)*8;
  836. src += size;
  837. break;
  838. }
  839. default:
  840. return -1;
  841. }
  842. *data_size = (uint8_t *)samples - (uint8_t *)data;
  843. return src - buf;
  844. }
  845. #ifdef CONFIG_ENCODERS
  846. #define ADPCM_ENCODER(id,name) \
  847. AVCodec name ## _encoder = { \
  848. #name, \
  849. CODEC_TYPE_AUDIO, \
  850. id, \
  851. sizeof(ADPCMContext), \
  852. adpcm_encode_init, \
  853. adpcm_encode_frame, \
  854. adpcm_encode_close, \
  855. NULL, \
  856. };
  857. #else
  858. #define ADPCM_ENCODER(id,name)
  859. #endif
  860. #ifdef CONFIG_DECODERS
  861. #define ADPCM_DECODER(id,name) \
  862. AVCodec name ## _decoder = { \
  863. #name, \
  864. CODEC_TYPE_AUDIO, \
  865. id, \
  866. sizeof(ADPCMContext), \
  867. adpcm_decode_init, \
  868. NULL, \
  869. NULL, \
  870. adpcm_decode_frame, \
  871. };
  872. #else
  873. #define ADPCM_DECODER(id,name)
  874. #endif
  875. #define ADPCM_CODEC(id, name) \
  876. ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
  877. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
  878. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
  879. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
  880. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
  881. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
  882. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
  883. ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
  884. ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
  885. ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
  886. ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx);
  887. ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
  888. ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
  889. ADPCM_CODEC(CODEC_ID_ADPCM_SWF, adpcm_swf);
  890. #undef ADPCM_CODEC