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.

774 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. if (!buf_size)
  380. return 0;
  381. samples = data;
  382. src = buf;
  383. st = avctx->channels == 2;
  384. switch(avctx->codec->id) {
  385. case CODEC_ID_ADPCM_IMA_QT:
  386. n = (buf_size - 2);/* >> 2*avctx->channels;*/
  387. channel = c->channel;
  388. cs = &(c->status[channel]);
  389. /* (pppppp) (piiiiiii) */
  390. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  391. cs->predictor = (*src++) << 8;
  392. cs->predictor |= (*src & 0x80);
  393. cs->predictor &= 0xFF80;
  394. /* sign extension */
  395. if(cs->predictor & 0x8000)
  396. cs->predictor -= 0x10000;
  397. CLAMP_TO_SHORT(cs->predictor);
  398. cs->step_index = (*src++) & 0x7F;
  399. if (cs->step_index > 88) av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);
  400. if (cs->step_index > 88) cs->step_index = 88;
  401. cs->step = step_table[cs->step_index];
  402. if (st && channel)
  403. samples++;
  404. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  405. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);
  406. samples += avctx->channels;
  407. *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);
  408. samples += avctx->channels;
  409. src ++;
  410. }
  411. if(st) { /* handle stereo interlacing */
  412. c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
  413. if(channel == 1) { /* wait for the other packet before outputing anything */
  414. *data_size = 0;
  415. return src - buf;
  416. }
  417. }
  418. break;
  419. case CODEC_ID_ADPCM_IMA_WAV:
  420. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  421. buf_size = avctx->block_align;
  422. for(i=0; i<avctx->channels; i++){
  423. cs = &(c->status[i]);
  424. cs->predictor = *src++;
  425. cs->predictor |= (*src++) << 8;
  426. if(cs->predictor & 0x8000)
  427. cs->predictor -= 0x10000;
  428. CLAMP_TO_SHORT(cs->predictor);
  429. // XXX: is this correct ??: *samples++ = cs->predictor;
  430. cs->step_index = *src++;
  431. if (cs->step_index < 0) cs->step_index = 0;
  432. if (cs->step_index > 88) cs->step_index = 88;
  433. if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null !!\n"); /* unused */
  434. }
  435. for(m=4; src < (buf + buf_size);) {
  436. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F, 3);
  437. if (st)
  438. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F, 3);
  439. *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F, 3);
  440. if (st) {
  441. *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F, 3);
  442. if (!--m) {
  443. m=4;
  444. src+=4;
  445. }
  446. }
  447. src++;
  448. }
  449. break;
  450. case CODEC_ID_ADPCM_4XM:
  451. cs = &(c->status[0]);
  452. c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  453. if(st){
  454. c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  455. }
  456. c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  457. if(st){
  458. c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  459. }
  460. if (cs->step_index < 0) cs->step_index = 0;
  461. if (cs->step_index > 88) cs->step_index = 88;
  462. m= (buf_size - (src - buf))>>st;
  463. for(i=0; i<m; i++) {
  464. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);
  465. if (st)
  466. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);
  467. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);
  468. if (st)
  469. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);
  470. }
  471. src += m<<st;
  472. break;
  473. case CODEC_ID_ADPCM_MS:
  474. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  475. buf_size = avctx->block_align;
  476. n = buf_size - 7 * avctx->channels;
  477. if (n < 0)
  478. return -1;
  479. block_predictor[0] = (*src++); /* should be bound */
  480. block_predictor[0] = (block_predictor[0] < 0)?(0):((block_predictor[0] > 7)?(7):(block_predictor[0]));
  481. block_predictor[1] = 0;
  482. if (st)
  483. block_predictor[1] = (*src++);
  484. block_predictor[1] = (block_predictor[1] < 0)?(0):((block_predictor[1] > 7)?(7):(block_predictor[1]));
  485. c->status[0].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  486. if (c->status[0].idelta & 0x08000)
  487. c->status[0].idelta -= 0x10000;
  488. src+=2;
  489. if (st)
  490. c->status[1].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  491. if (st && c->status[1].idelta & 0x08000)
  492. c->status[1].idelta |= 0xFFFF0000;
  493. if (st)
  494. src+=2;
  495. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  496. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  497. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  498. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  499. c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  500. src+=2;
  501. if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  502. if (st) src+=2;
  503. c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  504. src+=2;
  505. if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  506. if (st) src+=2;
  507. *samples++ = c->status[0].sample1;
  508. if (st) *samples++ = c->status[1].sample1;
  509. *samples++ = c->status[0].sample2;
  510. if (st) *samples++ = c->status[1].sample2;
  511. for(;n>0;n--) {
  512. *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  513. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  514. src ++;
  515. }
  516. break;
  517. case CODEC_ID_ADPCM_IMA_DK4:
  518. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  519. buf_size = avctx->block_align;
  520. c->status[0].predictor = (src[0] | (src[1] << 8));
  521. c->status[0].step_index = src[2];
  522. src += 4;
  523. if(c->status[0].predictor & 0x8000)
  524. c->status[0].predictor -= 0x10000;
  525. *samples++ = c->status[0].predictor;
  526. if (st) {
  527. c->status[1].predictor = (src[0] | (src[1] << 8));
  528. c->status[1].step_index = src[2];
  529. src += 4;
  530. if(c->status[1].predictor & 0x8000)
  531. c->status[1].predictor -= 0x10000;
  532. *samples++ = c->status[1].predictor;
  533. }
  534. while (src < buf + buf_size) {
  535. /* take care of the top nibble (always left or mono channel) */
  536. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  537. (src[0] >> 4) & 0x0F, 3);
  538. /* take care of the bottom nibble, which is right sample for
  539. * stereo, or another mono sample */
  540. if (st)
  541. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  542. src[0] & 0x0F, 3);
  543. else
  544. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  545. src[0] & 0x0F, 3);
  546. src++;
  547. }
  548. break;
  549. case CODEC_ID_ADPCM_IMA_DK3:
  550. if (avctx->block_align != 0 && buf_size > avctx->block_align)
  551. buf_size = avctx->block_align;
  552. c->status[0].predictor = (src[10] | (src[11] << 8));
  553. c->status[1].predictor = (src[12] | (src[13] << 8));
  554. c->status[0].step_index = src[14];
  555. c->status[1].step_index = src[15];
  556. /* sign extend the predictors */
  557. if(c->status[0].predictor & 0x8000)
  558. c->status[0].predictor -= 0x10000;
  559. if(c->status[1].predictor & 0x8000)
  560. c->status[1].predictor -= 0x10000;
  561. src += 16;
  562. diff_channel = c->status[1].predictor;
  563. /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
  564. * the buffer is consumed */
  565. while (1) {
  566. /* for this algorithm, c->status[0] is the sum channel and
  567. * c->status[1] is the diff channel */
  568. /* process the first predictor of the sum channel */
  569. DK3_GET_NEXT_NIBBLE();
  570. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  571. /* process the diff channel predictor */
  572. DK3_GET_NEXT_NIBBLE();
  573. adpcm_ima_expand_nibble(&c->status[1], nibble, 3);
  574. /* process the first pair of stereo PCM samples */
  575. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  576. *samples++ = c->status[0].predictor + c->status[1].predictor;
  577. *samples++ = c->status[0].predictor - c->status[1].predictor;
  578. /* process the second predictor of the sum channel */
  579. DK3_GET_NEXT_NIBBLE();
  580. adpcm_ima_expand_nibble(&c->status[0], nibble, 3);
  581. /* process the second pair of stereo PCM samples */
  582. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  583. *samples++ = c->status[0].predictor + c->status[1].predictor;
  584. *samples++ = c->status[0].predictor - c->status[1].predictor;
  585. }
  586. break;
  587. case CODEC_ID_ADPCM_IMA_WS:
  588. /* no per-block initialization; just start decoding the data */
  589. while (src < buf + buf_size) {
  590. if (st) {
  591. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  592. (src[0] >> 4) & 0x0F, 3);
  593. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  594. src[0] & 0x0F, 3);
  595. } else {
  596. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  597. (src[0] >> 4) & 0x0F, 3);
  598. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  599. src[0] & 0x0F, 3);
  600. }
  601. src++;
  602. }
  603. break;
  604. case CODEC_ID_ADPCM_XA:
  605. c->status[0].sample1 = c->status[0].sample2 =
  606. c->status[1].sample1 = c->status[1].sample2 = 0;
  607. while (buf_size >= 128) {
  608. xa_decode(samples, src, &c->status[0], &c->status[1],
  609. avctx->channels);
  610. src += 128;
  611. samples += 28 * 8;
  612. buf_size -= 128;
  613. }
  614. break;
  615. default:
  616. *data_size = 0;
  617. return -1;
  618. }
  619. *data_size = (uint8_t *)samples - (uint8_t *)data;
  620. return src - buf;
  621. }
  622. #ifdef CONFIG_ENCODERS
  623. #define ADPCM_ENCODER(id,name) \
  624. AVCodec name ## _encoder = { \
  625. #name, \
  626. CODEC_TYPE_AUDIO, \
  627. id, \
  628. sizeof(ADPCMContext), \
  629. adpcm_encode_init, \
  630. adpcm_encode_frame, \
  631. adpcm_encode_close, \
  632. NULL, \
  633. };
  634. #else
  635. #define ADPCM_ENCODER(id,name)
  636. #endif
  637. #ifdef CONFIG_DECODERS
  638. #define ADPCM_DECODER(id,name) \
  639. AVCodec name ## _decoder = { \
  640. #name, \
  641. CODEC_TYPE_AUDIO, \
  642. id, \
  643. sizeof(ADPCMContext), \
  644. adpcm_decode_init, \
  645. NULL, \
  646. NULL, \
  647. adpcm_decode_frame, \
  648. };
  649. #else
  650. #define ADPCM_DECODER(id,name)
  651. #endif
  652. #define ADPCM_CODEC(id, name) \
  653. ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
  654. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
  655. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
  656. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
  657. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
  658. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
  659. ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
  660. ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
  661. ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
  662. ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx);
  663. #undef ADPCM_CODEC