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.

771 lines
25KB

  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, int shift)
  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) >> shift;
  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_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
  283. {
  284. int predictor;
  285. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
  286. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  287. CLAMP_TO_SHORT(predictor);
  288. c->sample2 = c->sample1;
  289. c->sample1 = predictor;
  290. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) / 256;
  291. if (c->idelta < 16) c->idelta = 16;
  292. return (short)predictor;
  293. }
  294. static void xa_decode(short *out, const unsigned char *in,
  295. ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
  296. {
  297. int i, j;
  298. int shift,filter,f0,f1;
  299. int s_1,s_2;
  300. int d,s,t;
  301. for(i=0;i<4;i++) {
  302. shift = 12 - (in[4+i*2] & 15);
  303. filter = in[4+i*2] >> 4;
  304. f0 = xa_adpcm_table[filter][0];
  305. f1 = xa_adpcm_table[filter][1];
  306. s_1 = left->sample1;
  307. s_2 = left->sample2;
  308. for(j=0;j<28;j++) {
  309. d = in[16+i+j*4];
  310. t = (signed char)(d<<4)>>4;
  311. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  312. CLAMP_TO_SHORT(s);
  313. *out = s;
  314. out += inc;
  315. s_2 = s_1;
  316. s_1 = s;
  317. }
  318. if (inc==2) { /* stereo */
  319. left->sample1 = s_1;
  320. left->sample2 = s_2;
  321. s_1 = right->sample1;
  322. s_2 = right->sample2;
  323. out = out + 1 - 28*2;
  324. }
  325. shift = 12 - (in[5+i*2] & 15);
  326. filter = in[5+i*2] >> 4;
  327. f0 = xa_adpcm_table[filter][0];
  328. f1 = xa_adpcm_table[filter][1];
  329. for(j=0;j<28;j++) {
  330. d = in[16+i+j*4];
  331. t = (signed char)d >> 4;
  332. s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
  333. CLAMP_TO_SHORT(s);
  334. *out = s;
  335. out += inc;
  336. s_2 = s_1;
  337. s_1 = s;
  338. }
  339. if (inc==2) { /* stereo */
  340. right->sample1 = s_1;
  341. right->sample2 = s_2;
  342. out -= 1;
  343. } else {
  344. left->sample1 = s_1;
  345. left->sample2 = s_2;
  346. }
  347. }
  348. }
  349. /* DK3 ADPCM support macro */
  350. #define DK3_GET_NEXT_NIBBLE() \
  351. if (decode_top_nibble_next) \
  352. { \
  353. nibble = (last_byte >> 4) & 0x0F; \
  354. decode_top_nibble_next = 0; \
  355. } \
  356. else \
  357. { \
  358. last_byte = *src++; \
  359. if (src >= buf + buf_size) break; \
  360. nibble = last_byte & 0x0F; \
  361. decode_top_nibble_next = 1; \
  362. }
  363. static int adpcm_decode_frame(AVCodecContext *avctx,
  364. void *data, int *data_size,
  365. uint8_t *buf, int buf_size)
  366. {
  367. ADPCMContext *c = avctx->priv_data;
  368. ADPCMChannelStatus *cs;
  369. int n, m, channel, i;
  370. int block_predictor[2];
  371. short *samples;
  372. uint8_t *src;
  373. int st; /* stereo */
  374. /* DK3 ADPCM accounting variables */
  375. unsigned char last_byte = 0;
  376. unsigned char nibble;
  377. int decode_top_nibble_next = 0;
  378. int diff_channel;
  379. samples = data;
  380. src = buf;
  381. st = avctx->channels == 2;
  382. switch(avctx->codec->id) {
  383. case CODEC_ID_ADPCM_IMA_QT:
  384. n = (buf_size - 2);/* >> 2*avctx->channels;*/
  385. channel = c->channel;
  386. cs = &(c->status[channel]);
  387. /* (pppppp) (piiiiiii) */
  388. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  389. cs->predictor = (*src++) << 8;
  390. cs->predictor |= (*src & 0x80);
  391. cs->predictor &= 0xFF80;
  392. /* sign extension */
  393. if(cs->predictor & 0x8000)
  394. cs->predictor -= 0x10000;
  395. CLAMP_TO_SHORT(cs->predictor);
  396. cs->step_index = (*src++) & 0x7F;
  397. if (cs->step_index > 88) av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  398. if (cs->step_index > 88) cs->step_index = 88;
  399. cs->step = step_table[cs->step_index];
  400. if (st && channel)
  401. samples++;
  402. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  403. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
  404. samples += avctx->channels;
  405. *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
  406. samples += avctx->channels;
  407. src ++;
  408. }
  409. if(st) { /* handle stereo interlacing */
  410. c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
  411. if(channel == 1) { /* wait for the other packet before outputing anything */
  412. *data_size = 0;
  413. return src - buf;
  414. }
  415. }
  416. break;
  417. case CODEC_ID_ADPCM_IMA_WAV:
  418. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  419. buf_size = avctx->block_align;
  420. for(i=0; i<avctx->channels; i++){
  421. cs = &(c->status[i]);
  422. cs->predictor = *src++;
  423. cs->predictor |= (*src++) << 8;
  424. if(cs->predictor & 0x8000)
  425. cs->predictor -= 0x10000;
  426. CLAMP_TO_SHORT(cs->predictor);
  427. // XXX: is this correct ??: *samples++ = cs->predictor;
  428. cs->step_index = *src++;
  429. if (cs->step_index < 0) cs->step_index = 0;
  430. if (cs->step_index > 88) cs->step_index = 88;
  431. if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null !!\n"); /* unused */
  432. }
  433. for(m=4; src < (buf + buf_size);) {
  434. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F, 3);
  435. if (st)
  436. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F, 3);
  437. *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F, 3);
  438. if (st) {
  439. *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F, 3);
  440. if (!--m) {
  441. m=4;
  442. src+=4;
  443. }
  444. }
  445. src++;
  446. }
  447. break;
  448. case CODEC_ID_ADPCM_4XM:
  449. cs = &(c->status[0]);
  450. c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  451. if(st){
  452. c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  453. }
  454. c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  455. if(st){
  456. c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  457. }
  458. if (cs->step_index < 0) cs->step_index = 0;
  459. if (cs->step_index > 88) cs->step_index = 88;
  460. m= (buf_size - (src - buf))>>st;
  461. for(i=0; i<m; i++) {
  462. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
  463. if (st)
  464. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
  465. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
  466. if (st)
  467. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
  468. }
  469. src += m<<st;
  470. break;
  471. case CODEC_ID_ADPCM_MS:
  472. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  473. buf_size = avctx->block_align;
  474. n = buf_size - 7 * avctx->channels;
  475. if (n < 0)
  476. return -1;
  477. block_predictor[0] = (*src++); /* should be bound */
  478. block_predictor[0] = (block_predictor[0] < 0)?(0):((block_predictor[0] > 7)?(7):(block_predictor[0]));
  479. block_predictor[1] = 0;
  480. if (st)
  481. block_predictor[1] = (*src++);
  482. block_predictor[1] = (block_predictor[1] < 0)?(0):((block_predictor[1] > 7)?(7):(block_predictor[1]));
  483. c->status[0].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  484. if (c->status[0].idelta & 0x08000)
  485. c->status[0].idelta -= 0x10000;
  486. src+=2;
  487. if (st)
  488. c->status[1].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  489. if (st && c->status[1].idelta & 0x08000)
  490. c->status[1].idelta |= 0xFFFF0000;
  491. if (st)
  492. src+=2;
  493. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  494. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  495. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  496. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  497. c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  498. src+=2;
  499. if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  500. if (st) src+=2;
  501. c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  502. src+=2;
  503. if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  504. if (st) src+=2;
  505. *samples++ = c->status[0].sample1;
  506. if (st) *samples++ = c->status[1].sample1;
  507. *samples++ = c->status[0].sample2;
  508. if (st) *samples++ = c->status[1].sample2;
  509. for(;n>0;n--) {
  510. *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  511. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  512. src ++;
  513. }
  514. break;
  515. case CODEC_ID_ADPCM_IMA_DK4:
  516. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  517. buf_size = avctx->block_align;
  518. c->status[0].predictor = (src[0] | (src[1] << 8));
  519. c->status[0].step_index = src[2];
  520. src += 4;
  521. if(c->status[0].predictor & 0x8000)
  522. c->status[0].predictor -= 0x10000;
  523. *samples++ = c->status[0].predictor;
  524. if (st) {
  525. c->status[1].predictor = (src[0] | (src[1] << 8));
  526. c->status[1].step_index = src[2];
  527. src += 4;
  528. if(c->status[1].predictor & 0x8000)
  529. c->status[1].predictor -= 0x10000;
  530. *samples++ = c->status[1].predictor;
  531. }
  532. while (src < buf + buf_size) {
  533. /* take care of the top nibble (always left or mono channel) */
  534. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  535. (src[0] >> 4) & 0x0F, 3);
  536. /* take care of the bottom nibble, which is right sample for
  537. * stereo, or another mono sample */
  538. if (st)
  539. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  540. src[0] & 0x0F, 3);
  541. else
  542. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  543. src[0] & 0x0F, 3);
  544. src++;
  545. }
  546. break;
  547. case CODEC_ID_ADPCM_IMA_DK3:
  548. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  549. buf_size = avctx->block_align;
  550. c->status[0].predictor = (src[10] | (src[11] << 8));
  551. c->status[1].predictor = (src[12] | (src[13] << 8));
  552. c->status[0].step_index = src[14];
  553. c->status[1].step_index = src[15];
  554. /* sign extend the predictors */
  555. if(c->status[0].predictor & 0x8000)
  556. c->status[0].predictor -= 0x10000;
  557. if(c->status[1].predictor & 0x8000)
  558. c->status[1].predictor -= 0x10000;
  559. src += 16;
  560. diff_channel = c->status[1].predictor;
  561. /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
  562. * the buffer is consumed */
  563. while (1) {
  564. /* for this algorithm, c->status[0] is the sum channel and
  565. * c->status[1] is the diff channel */
  566. /* process the first predictor of the sum channel */
  567. DK3_GET_NEXT_NIBBLE();
  568. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  569. /* process the diff channel predictor */
  570. DK3_GET_NEXT_NIBBLE();
  571. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  572. /* process the first pair of stereo PCM samples */
  573. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  574. *samples++ = c->status[0].predictor + c->status[1].predictor;
  575. *samples++ = c->status[0].predictor - c->status[1].predictor;
  576. /* process the second predictor of the sum channel */
  577. DK3_GET_NEXT_NIBBLE();
  578. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  579. /* process the second pair of stereo PCM samples */
  580. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  581. *samples++ = c->status[0].predictor + c->status[1].predictor;
  582. *samples++ = c->status[0].predictor - c->status[1].predictor;
  583. }
  584. break;
  585. case CODEC_ID_ADPCM_IMA_WS:
  586. /* no per-block initialization; just start decoding the data */
  587. while (src < buf + buf_size) {
  588. if (st) {
  589. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  590. (src[0] >> 4) & 0x0F, 3);
  591. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  592. src[0] & 0x0F, 3);
  593. } else {
  594. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  595. (src[0] >> 4) & 0x0F, 3);
  596. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  597. src[0] & 0x0F, 3);
  598. }
  599. src++;
  600. }
  601. break;
  602. case CODEC_ID_ADPCM_XA:
  603. c->status[0].sample1 = c->status[0].sample2 =
  604. c->status[1].sample1 = c->status[1].sample2 = 0;
  605. while (buf_size >= 128) {
  606. xa_decode(samples, src, &c->status[0], &c->status[1],
  607. avctx->channels);
  608. src += 128;
  609. samples += 28 * 8;
  610. buf_size -= 128;
  611. }
  612. break;
  613. default:
  614. *data_size = 0;
  615. return -1;
  616. }
  617. *data_size = (uint8_t *)samples - (uint8_t *)data;
  618. return src - buf;
  619. }
  620. #ifdef CONFIG_ENCODERS
  621. #define ADPCM_ENCODER(id,name) \
  622. AVCodec name ## _encoder = { \
  623. #name, \
  624. CODEC_TYPE_AUDIO, \
  625. id, \
  626. sizeof(ADPCMContext), \
  627. adpcm_encode_init, \
  628. adpcm_encode_frame, \
  629. adpcm_encode_close, \
  630. NULL, \
  631. };
  632. #else
  633. #define ADPCM_ENCODER(id,name)
  634. #endif
  635. #ifdef CONFIG_DECODERS
  636. #define ADPCM_DECODER(id,name) \
  637. AVCodec name ## _decoder = { \
  638. #name, \
  639. CODEC_TYPE_AUDIO, \
  640. id, \
  641. sizeof(ADPCMContext), \
  642. adpcm_decode_init, \
  643. NULL, \
  644. NULL, \
  645. adpcm_decode_frame, \
  646. };
  647. #else
  648. #define ADPCM_DECODER(id,name)
  649. #endif
  650. #define ADPCM_CODEC(id, name) \
  651. ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
  652. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
  653. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
  654. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
  655. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
  656. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
  657. ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
  658. ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
  659. ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
  660. ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx);
  661. #undef ADPCM_CODEC