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.

813 lines
27KB

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