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.

861 lines
29KB

  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. /**
  21. * @file adpcm.c
  22. * ADPCM codecs.
  23. * First version by Francois Revol (revol@free.fr)
  24. * Fringe ADPCM codecs (e.g., DK3, DK4, Westwood)
  25. * by Mike Melanson (melanson@pcisys.net)
  26. * CD-ROM XA ADPCM codec by BERO
  27. * EA ADPCM decoder by Robin Kay (komadori@myrealbox.com)
  28. *
  29. * Features and limitations:
  30. *
  31. * Reference documents:
  32. * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
  33. * http://www.geocities.com/SiliconValley/8682/aud3.txt
  34. * http://openquicktime.sourceforge.net/plugins.htm
  35. * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
  36. * http://www.cs.ucla.edu/~leec/mediabench/applications.html
  37. * SoX source code http://home.sprynet.com/~cbagwell/sox.html
  38. *
  39. * CD-ROM XA:
  40. * http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html
  41. * vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html
  42. * readstr http://www.geocities.co.jp/Playtown/2004/
  43. */
  44. #define BLKSIZE 1024
  45. #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
  46. #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
  47. #define LE_32(x) ((((uint8_t*)(x))[3] << 24) | \
  48. (((uint8_t*)(x))[2] << 16) | \
  49. (((uint8_t*)(x))[1] << 8) | \
  50. ((uint8_t*)(x))[0])
  51. #define CLAMP_TO_SHORT(value) \
  52. if (value > 32767) \
  53. value = 32767; \
  54. else if (value < -32768) \
  55. value = -32768; \
  56. /* step_table[] and index_table[] are from the ADPCM reference source */
  57. /* This is the index table: */
  58. static const int index_table[16] = {
  59. -1, -1, -1, -1, 2, 4, 6, 8,
  60. -1, -1, -1, -1, 2, 4, 6, 8,
  61. };
  62. /**
  63. * This is the step table. Note that many programs use slight deviations from
  64. * this table, but such deviations are negligible:
  65. */
  66. static const int step_table[89] = {
  67. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  68. 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  69. 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  70. 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  71. 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  72. 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  73. 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  74. 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  75. 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  76. };
  77. /* These are for MS-ADPCM */
  78. /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
  79. static const int AdaptationTable[] = {
  80. 230, 230, 230, 230, 307, 409, 512, 614,
  81. 768, 614, 512, 409, 307, 230, 230, 230
  82. };
  83. static const int AdaptCoeff1[] = {
  84. 256, 512, 0, 192, 240, 460, 392
  85. };
  86. static const int AdaptCoeff2[] = {
  87. 0, -256, 0, 64, 0, -208, -232
  88. };
  89. /* These are for CD-ROM XA ADPCM */
  90. static const int xa_adpcm_table[5][2] = {
  91. { 0, 0 },
  92. { 60, 0 },
  93. { 115, -52 },
  94. { 98, -55 },
  95. { 122, -60 }
  96. };
  97. static int ea_adpcm_table[] = {
  98. 0, 240, 460, 392, 0, 0, -208, -220, 0, 1,
  99. 3, 4, 7, 8, 10, 11, 0, -1, -3, -4
  100. };
  101. /* end of tables */
  102. typedef struct ADPCMChannelStatus {
  103. int predictor;
  104. short int step_index;
  105. int step;
  106. /* for encoding */
  107. int prev_sample;
  108. /* MS version */
  109. short sample1;
  110. short sample2;
  111. int coeff1;
  112. int coeff2;
  113. int idelta;
  114. } ADPCMChannelStatus;
  115. typedef struct ADPCMContext {
  116. int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
  117. ADPCMChannelStatus status[2];
  118. short sample_buffer[32]; /* hold left samples while waiting for right samples */
  119. } ADPCMContext;
  120. /* XXX: implement encoding */
  121. #ifdef CONFIG_ENCODERS
  122. static int adpcm_encode_init(AVCodecContext *avctx)
  123. {
  124. if (avctx->channels > 2)
  125. return -1; /* only stereo or mono =) */
  126. switch(avctx->codec->id) {
  127. case CODEC_ID_ADPCM_IMA_QT:
  128. av_log(avctx, AV_LOG_ERROR, "ADPCM: codec adpcm_ima_qt unsupported for encoding !\n");
  129. avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */
  130. return -1;
  131. break;
  132. case CODEC_ID_ADPCM_IMA_WAV:
  133. avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
  134. /* and we have 4 bytes per channel overhead */
  135. avctx->block_align = BLKSIZE;
  136. /* seems frame_size isn't taken into account... have to buffer the samples :-( */
  137. break;
  138. case CODEC_ID_ADPCM_MS:
  139. av_log(avctx, AV_LOG_ERROR, "ADPCM: codec adpcm_ms unsupported for encoding !\n");
  140. return -1;
  141. break;
  142. default:
  143. return -1;
  144. break;
  145. }
  146. avctx->coded_frame= avcodec_alloc_frame();
  147. avctx->coded_frame->key_frame= 1;
  148. return 0;
  149. }
  150. static int adpcm_encode_close(AVCodecContext *avctx)
  151. {
  152. av_freep(&avctx->coded_frame);
  153. return 0;
  154. }
  155. static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
  156. {
  157. int step_index;
  158. unsigned char nibble;
  159. int sign = 0; /* sign bit of the nibble (MSB) */
  160. int delta, predicted_delta;
  161. delta = sample - c->prev_sample;
  162. if (delta < 0) {
  163. sign = 1;
  164. delta = -delta;
  165. }
  166. step_index = c->step_index;
  167. /* nibble = 4 * delta / step_table[step_index]; */
  168. nibble = (delta << 2) / step_table[step_index];
  169. if (nibble > 7)
  170. nibble = 7;
  171. step_index += index_table[nibble];
  172. if (step_index < 0)
  173. step_index = 0;
  174. if (step_index > 88)
  175. step_index = 88;
  176. /* what the decoder will find */
  177. predicted_delta = ((step_table[step_index] * nibble) / 4) + (step_table[step_index] / 8);
  178. if (sign)
  179. c->prev_sample -= predicted_delta;
  180. else
  181. c->prev_sample += predicted_delta;
  182. CLAMP_TO_SHORT(c->prev_sample);
  183. nibble += sign << 3; /* sign * 8 */
  184. /* save back */
  185. c->step_index = step_index;
  186. return nibble;
  187. }
  188. static int adpcm_encode_frame(AVCodecContext *avctx,
  189. unsigned char *frame, int buf_size, void *data)
  190. {
  191. int n;
  192. short *samples;
  193. unsigned char *dst;
  194. ADPCMContext *c = avctx->priv_data;
  195. dst = frame;
  196. samples = (short *)data;
  197. /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
  198. switch(avctx->codec->id) {
  199. case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
  200. break;
  201. case CODEC_ID_ADPCM_IMA_WAV:
  202. n = avctx->frame_size / 8;
  203. c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
  204. /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
  205. *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
  206. *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
  207. *dst++ = (unsigned char)c->status[0].step_index;
  208. *dst++ = 0; /* unknown */
  209. samples++;
  210. if (avctx->channels == 2) {
  211. c->status[1].prev_sample = (signed short)samples[1];
  212. /* c->status[1].step_index = 0; */
  213. *dst++ = (c->status[1].prev_sample) & 0xFF;
  214. *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
  215. *dst++ = (unsigned char)c->status[1].step_index;
  216. *dst++ = 0;
  217. samples++;
  218. }
  219. /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
  220. for (; n>0; n--) {
  221. *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F;
  222. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0;
  223. dst++;
  224. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F;
  225. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0;
  226. dst++;
  227. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F;
  228. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0;
  229. dst++;
  230. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F;
  231. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0;
  232. dst++;
  233. /* right channel */
  234. if (avctx->channels == 2) {
  235. *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
  236. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
  237. dst++;
  238. *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
  239. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
  240. dst++;
  241. *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
  242. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
  243. dst++;
  244. *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
  245. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
  246. dst++;
  247. }
  248. samples += 8 * avctx->channels;
  249. }
  250. break;
  251. default:
  252. return -1;
  253. }
  254. return dst - frame;
  255. }
  256. #endif //CONFIG_ENCODERS
  257. static int adpcm_decode_init(AVCodecContext * avctx)
  258. {
  259. ADPCMContext *c = avctx->priv_data;
  260. c->channel = 0;
  261. c->status[0].predictor = c->status[1].predictor = 0;
  262. c->status[0].step_index = c->status[1].step_index = 0;
  263. c->status[0].step = c->status[1].step = 0;
  264. switch(avctx->codec->id) {
  265. default:
  266. break;
  267. }
  268. return 0;
  269. }
  270. static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift)
  271. {
  272. int step_index;
  273. int predictor;
  274. int sign, delta, diff, step;
  275. step = step_table[c->step_index];
  276. step_index = c->step_index + index_table[(unsigned)nibble];
  277. if (step_index < 0) step_index = 0;
  278. else if (step_index > 88) step_index = 88;
  279. sign = nibble & 8;
  280. delta = nibble & 7;
  281. /* perform direct multiplication instead of series of jumps proposed by
  282. * the reference ADPCM implementation since modern CPUs can do the mults
  283. * quickly enough */
  284. diff = ((2 * delta + 1) * step) >> shift;
  285. predictor = c->predictor;
  286. if (sign) predictor -= diff;
  287. else predictor += diff;
  288. CLAMP_TO_SHORT(predictor);
  289. c->predictor = predictor;
  290. c->step_index = step_index;
  291. return (short)predictor;
  292. }
  293. static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
  294. {
  295. int predictor;
  296. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
  297. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  298. CLAMP_TO_SHORT(predictor);
  299. c->sample2 = c->sample1;
  300. c->sample1 = predictor;
  301. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) / 256;
  302. if (c->idelta < 16) c->idelta = 16;
  303. return (short)predictor;
  304. }
  305. static void xa_decode(short *out, const unsigned char *in,
  306. ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
  307. {
  308. int i, j;
  309. int shift,filter,f0,f1;
  310. int s_1,s_2;
  311. int d,s,t;
  312. for(i=0;i<4;i++) {
  313. shift = 12 - (in[4+i*2] & 15);
  314. filter = in[4+i*2] >> 4;
  315. f0 = xa_adpcm_table[filter][0];
  316. f1 = xa_adpcm_table[filter][1];
  317. s_1 = left->sample1;
  318. s_2 = left->sample2;
  319. for(j=0;j<28;j++) {
  320. d = in[16+i+j*4];
  321. t = (signed char)(d<<4)>>4;
  322. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  323. CLAMP_TO_SHORT(s);
  324. *out = s;
  325. out += inc;
  326. s_2 = s_1;
  327. s_1 = s;
  328. }
  329. if (inc==2) { /* stereo */
  330. left->sample1 = s_1;
  331. left->sample2 = s_2;
  332. s_1 = right->sample1;
  333. s_2 = right->sample2;
  334. out = out + 1 - 28*2;
  335. }
  336. shift = 12 - (in[5+i*2] & 15);
  337. filter = in[5+i*2] >> 4;
  338. f0 = xa_adpcm_table[filter][0];
  339. f1 = xa_adpcm_table[filter][1];
  340. for(j=0;j<28;j++) {
  341. d = in[16+i+j*4];
  342. t = (signed char)d >> 4;
  343. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  344. CLAMP_TO_SHORT(s);
  345. *out = s;
  346. out += inc;
  347. s_2 = s_1;
  348. s_1 = s;
  349. }
  350. if (inc==2) { /* stereo */
  351. right->sample1 = s_1;
  352. right->sample2 = s_2;
  353. out -= 1;
  354. } else {
  355. left->sample1 = s_1;
  356. left->sample2 = s_2;
  357. }
  358. }
  359. }
  360. /* DK3 ADPCM support macro */
  361. #define DK3_GET_NEXT_NIBBLE() \
  362. if (decode_top_nibble_next) \
  363. { \
  364. nibble = (last_byte >> 4) & 0x0F; \
  365. decode_top_nibble_next = 0; \
  366. } \
  367. else \
  368. { \
  369. last_byte = *src++; \
  370. if (src >= buf + buf_size) break; \
  371. nibble = last_byte & 0x0F; \
  372. decode_top_nibble_next = 1; \
  373. }
  374. static int adpcm_decode_frame(AVCodecContext *avctx,
  375. void *data, int *data_size,
  376. uint8_t *buf, int buf_size)
  377. {
  378. ADPCMContext *c = avctx->priv_data;
  379. ADPCMChannelStatus *cs;
  380. int n, m, channel, i;
  381. int block_predictor[2];
  382. short *samples;
  383. uint8_t *src;
  384. int st; /* stereo */
  385. /* DK3 ADPCM accounting variables */
  386. unsigned char last_byte = 0;
  387. unsigned char nibble;
  388. int decode_top_nibble_next = 0;
  389. int diff_channel;
  390. /* EA ADPCM state variables */
  391. uint32_t samples_in_chunk;
  392. int32_t previous_left_sample, previous_right_sample;
  393. int32_t current_left_sample, current_right_sample;
  394. int32_t next_left_sample, next_right_sample;
  395. int32_t coeff1l, coeff2l, coeff1r, coeff2r;
  396. uint8_t shift_left, shift_right;
  397. int count1, count2;
  398. if (!buf_size)
  399. return 0;
  400. samples = data;
  401. src = buf;
  402. st = avctx->channels == 2;
  403. switch(avctx->codec->id) {
  404. case CODEC_ID_ADPCM_IMA_QT:
  405. n = (buf_size - 2);/* >> 2*avctx->channels;*/
  406. channel = c->channel;
  407. cs = &(c->status[channel]);
  408. /* (pppppp) (piiiiiii) */
  409. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  410. cs->predictor = (*src++) << 8;
  411. cs->predictor |= (*src & 0x80);
  412. cs->predictor &= 0xFF80;
  413. /* sign extension */
  414. if(cs->predictor & 0x8000)
  415. cs->predictor -= 0x10000;
  416. CLAMP_TO_SHORT(cs->predictor);
  417. cs->step_index = (*src++) & 0x7F;
  418. if (cs->step_index > 88) av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  419. if (cs->step_index > 88) cs->step_index = 88;
  420. cs->step = step_table[cs->step_index];
  421. if (st && channel)
  422. samples++;
  423. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  424. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
  425. samples += avctx->channels;
  426. *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
  427. samples += avctx->channels;
  428. src ++;
  429. }
  430. if(st) { /* handle stereo interlacing */
  431. c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
  432. if(channel == 1) { /* wait for the other packet before outputing anything */
  433. *data_size = 0;
  434. return src - buf;
  435. }
  436. }
  437. break;
  438. case CODEC_ID_ADPCM_IMA_WAV:
  439. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  440. buf_size = avctx->block_align;
  441. for(i=0; i<avctx->channels; i++){
  442. cs = &(c->status[i]);
  443. cs->predictor = *src++;
  444. cs->predictor |= (*src++) << 8;
  445. if(cs->predictor & 0x8000)
  446. cs->predictor -= 0x10000;
  447. CLAMP_TO_SHORT(cs->predictor);
  448. // XXX: is this correct ??: *samples++ = cs->predictor;
  449. cs->step_index = *src++;
  450. if (cs->step_index < 0) cs->step_index = 0;
  451. if (cs->step_index > 88) cs->step_index = 88;
  452. if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null !!\n"); /* unused */
  453. }
  454. for(m=4; src < (buf + buf_size);) {
  455. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F, 3);
  456. if (st)
  457. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F, 3);
  458. *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F, 3);
  459. if (st) {
  460. *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F, 3);
  461. if (!--m) {
  462. m=4;
  463. src+=4;
  464. }
  465. }
  466. src++;
  467. }
  468. break;
  469. case CODEC_ID_ADPCM_4XM:
  470. cs = &(c->status[0]);
  471. c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  472. if(st){
  473. c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  474. }
  475. c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  476. if(st){
  477. c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  478. }
  479. if (cs->step_index < 0) cs->step_index = 0;
  480. if (cs->step_index > 88) cs->step_index = 88;
  481. m= (buf_size - (src - buf))>>st;
  482. for(i=0; i<m; i++) {
  483. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
  484. if (st)
  485. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
  486. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
  487. if (st)
  488. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
  489. }
  490. src += m<<st;
  491. break;
  492. case CODEC_ID_ADPCM_MS:
  493. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  494. buf_size = avctx->block_align;
  495. n = buf_size - 7 * avctx->channels;
  496. if (n < 0)
  497. return -1;
  498. block_predictor[0] = (*src++); /* should be bound */
  499. block_predictor[0] = (block_predictor[0] < 0)?(0):((block_predictor[0] > 7)?(7):(block_predictor[0]));
  500. block_predictor[1] = 0;
  501. if (st)
  502. block_predictor[1] = (*src++);
  503. block_predictor[1] = (block_predictor[1] < 0)?(0):((block_predictor[1] > 7)?(7):(block_predictor[1]));
  504. c->status[0].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  505. if (c->status[0].idelta & 0x08000)
  506. c->status[0].idelta -= 0x10000;
  507. src+=2;
  508. if (st)
  509. c->status[1].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  510. if (st && c->status[1].idelta & 0x08000)
  511. c->status[1].idelta |= 0xFFFF0000;
  512. if (st)
  513. src+=2;
  514. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  515. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  516. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  517. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  518. c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  519. src+=2;
  520. if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  521. if (st) src+=2;
  522. c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  523. src+=2;
  524. if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  525. if (st) src+=2;
  526. *samples++ = c->status[0].sample1;
  527. if (st) *samples++ = c->status[1].sample1;
  528. *samples++ = c->status[0].sample2;
  529. if (st) *samples++ = c->status[1].sample2;
  530. for(;n>0;n--) {
  531. *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  532. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  533. src ++;
  534. }
  535. break;
  536. case CODEC_ID_ADPCM_IMA_DK4:
  537. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  538. buf_size = avctx->block_align;
  539. c->status[0].predictor = (src[0] | (src[1] << 8));
  540. c->status[0].step_index = src[2];
  541. src += 4;
  542. if(c->status[0].predictor & 0x8000)
  543. c->status[0].predictor -= 0x10000;
  544. *samples++ = c->status[0].predictor;
  545. if (st) {
  546. c->status[1].predictor = (src[0] | (src[1] << 8));
  547. c->status[1].step_index = src[2];
  548. src += 4;
  549. if(c->status[1].predictor & 0x8000)
  550. c->status[1].predictor -= 0x10000;
  551. *samples++ = c->status[1].predictor;
  552. }
  553. while (src < buf + buf_size) {
  554. /* take care of the top nibble (always left or mono channel) */
  555. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  556. (src[0] >> 4) & 0x0F, 3);
  557. /* take care of the bottom nibble, which is right sample for
  558. * stereo, or another mono sample */
  559. if (st)
  560. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  561. src[0] & 0x0F, 3);
  562. else
  563. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  564. src[0] & 0x0F, 3);
  565. src++;
  566. }
  567. break;
  568. case CODEC_ID_ADPCM_IMA_DK3:
  569. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  570. buf_size = avctx->block_align;
  571. c->status[0].predictor = (src[10] | (src[11] << 8));
  572. c->status[1].predictor = (src[12] | (src[13] << 8));
  573. c->status[0].step_index = src[14];
  574. c->status[1].step_index = src[15];
  575. /* sign extend the predictors */
  576. if(c->status[0].predictor & 0x8000)
  577. c->status[0].predictor -= 0x10000;
  578. if(c->status[1].predictor & 0x8000)
  579. c->status[1].predictor -= 0x10000;
  580. src += 16;
  581. diff_channel = c->status[1].predictor;
  582. /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
  583. * the buffer is consumed */
  584. while (1) {
  585. /* for this algorithm, c->status[0] is the sum channel and
  586. * c->status[1] is the diff channel */
  587. /* process the first predictor of the sum channel */
  588. DK3_GET_NEXT_NIBBLE();
  589. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  590. /* process the diff channel predictor */
  591. DK3_GET_NEXT_NIBBLE();
  592. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  593. /* process the first pair of stereo PCM samples */
  594. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  595. *samples++ = c->status[0].predictor + c->status[1].predictor;
  596. *samples++ = c->status[0].predictor - c->status[1].predictor;
  597. /* process the second predictor of the sum channel */
  598. DK3_GET_NEXT_NIBBLE();
  599. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  600. /* process the second pair of stereo PCM samples */
  601. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  602. *samples++ = c->status[0].predictor + c->status[1].predictor;
  603. *samples++ = c->status[0].predictor - c->status[1].predictor;
  604. }
  605. break;
  606. case CODEC_ID_ADPCM_IMA_WS:
  607. /* no per-block initialization; just start decoding the data */
  608. while (src < buf + buf_size) {
  609. if (st) {
  610. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  611. (src[0] >> 4) & 0x0F, 3);
  612. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  613. src[0] & 0x0F, 3);
  614. } else {
  615. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  616. (src[0] >> 4) & 0x0F, 3);
  617. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  618. src[0] & 0x0F, 3);
  619. }
  620. src++;
  621. }
  622. break;
  623. case CODEC_ID_ADPCM_XA:
  624. c->status[0].sample1 = c->status[0].sample2 =
  625. c->status[1].sample1 = c->status[1].sample2 = 0;
  626. while (buf_size >= 128) {
  627. xa_decode(samples, src, &c->status[0], &c->status[1],
  628. avctx->channels);
  629. src += 128;
  630. samples += 28 * 8;
  631. buf_size -= 128;
  632. }
  633. break;
  634. case CODEC_ID_ADPCM_EA:
  635. samples_in_chunk = LE_32(src);
  636. if (samples_in_chunk >= ((buf_size - 12) * 2)) {
  637. src += buf_size;
  638. break;
  639. }
  640. src += 4;
  641. current_left_sample = (int16_t)LE_16(src);
  642. src += 2;
  643. previous_left_sample = (int16_t)LE_16(src);
  644. src += 2;
  645. current_right_sample = (int16_t)LE_16(src);
  646. src += 2;
  647. previous_right_sample = (int16_t)LE_16(src);
  648. src += 2;
  649. for (count1 = 0; count1 < samples_in_chunk/28;count1++) {
  650. coeff1l = ea_adpcm_table[(*src >> 4) & 0x0F];
  651. coeff2l = ea_adpcm_table[((*src >> 4) & 0x0F) + 4];
  652. coeff1r = ea_adpcm_table[*src & 0x0F];
  653. coeff2r = ea_adpcm_table[(*src & 0x0F) + 4];
  654. src++;
  655. shift_left = ((*src >> 4) & 0x0F) + 8;
  656. shift_right = (*src & 0x0F) + 8;
  657. src++;
  658. for (count2 = 0; count2 < 28; count2++) {
  659. next_left_sample = (((*src & 0xF0) << 24) >> shift_left);
  660. next_right_sample = (((*src & 0x0F) << 28) >> shift_right);
  661. src++;
  662. next_left_sample = (next_left_sample +
  663. (current_left_sample * coeff1l) +
  664. (previous_left_sample * coeff2l) + 0x80) >> 8;
  665. next_right_sample = (next_right_sample +
  666. (current_right_sample * coeff1r) +
  667. (previous_right_sample * coeff2r) + 0x80) >> 8;
  668. CLAMP_TO_SHORT(next_left_sample);
  669. CLAMP_TO_SHORT(next_right_sample);
  670. previous_left_sample = current_left_sample;
  671. current_left_sample = next_left_sample;
  672. previous_right_sample = current_right_sample;
  673. current_right_sample = next_right_sample;
  674. *samples++ = (unsigned short)current_left_sample;
  675. *samples++ = (unsigned short)current_right_sample;
  676. }
  677. }
  678. break;
  679. case CODEC_ID_ADPCM_IMA_SMJPEG:
  680. c->status[0].predictor = *src;
  681. src += 2;
  682. c->status[0].step_index = *src++;
  683. src++; /* skip another byte before getting to the meat */
  684. while (src < buf + buf_size) {
  685. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  686. *src & 0x0F, 3);
  687. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  688. (*src >> 4) & 0x0F, 3);
  689. src++;
  690. }
  691. break;
  692. default:
  693. *data_size = 0;
  694. return -1;
  695. }
  696. *data_size = (uint8_t *)samples - (uint8_t *)data;
  697. return src - buf;
  698. }
  699. #ifdef CONFIG_ENCODERS
  700. #define ADPCM_ENCODER(id,name) \
  701. AVCodec name ## _encoder = { \
  702. #name, \
  703. CODEC_TYPE_AUDIO, \
  704. id, \
  705. sizeof(ADPCMContext), \
  706. adpcm_encode_init, \
  707. adpcm_encode_frame, \
  708. adpcm_encode_close, \
  709. NULL, \
  710. };
  711. #else
  712. #define ADPCM_ENCODER(id,name)
  713. #endif
  714. #ifdef CONFIG_DECODERS
  715. #define ADPCM_DECODER(id,name) \
  716. AVCodec name ## _decoder = { \
  717. #name, \
  718. CODEC_TYPE_AUDIO, \
  719. id, \
  720. sizeof(ADPCMContext), \
  721. adpcm_decode_init, \
  722. NULL, \
  723. NULL, \
  724. adpcm_decode_frame, \
  725. };
  726. #else
  727. #define ADPCM_DECODER(id,name)
  728. #endif
  729. #define ADPCM_CODEC(id, name) \
  730. ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
  731. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
  732. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
  733. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
  734. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
  735. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
  736. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_SMJPEG, adpcm_ima_smjpeg);
  737. ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
  738. ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
  739. ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
  740. ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx);
  741. ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
  742. #undef ADPCM_CODEC