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.

832 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. const static 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. fprintf(stderr, "ADPCM: codec admcp_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. fprintf(stderr, "ADPCM: codec admcp_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[0];
  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) fprintf(stderr, "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. *samples++ = cs->predictor;
  423. samples += st;
  424. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  425. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F);
  426. samples += avctx->channels;
  427. *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F);
  428. samples += avctx->channels;
  429. src ++;
  430. }
  431. if(st) { /* handle stereo interlacing */
  432. c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
  433. if(channel == 0) { /* wait for the other packet before outputing anything */
  434. *data_size = 0;
  435. return src - buf;
  436. }
  437. }
  438. break;
  439. case CODEC_ID_ADPCM_IMA_WAV:
  440. if (buf_size > BLKSIZE) {
  441. if (avctx->block_align != 0)
  442. buf_size = avctx->block_align;
  443. else
  444. buf_size = BLKSIZE;
  445. }
  446. // XXX: do as per-channel loop
  447. cs = &(c->status[0]);
  448. cs->predictor = (*src++) & 0x0FF;
  449. cs->predictor |= ((*src++) << 8) & 0x0FF00;
  450. if(cs->predictor & 0x8000)
  451. cs->predictor -= 0x10000;
  452. CLAMP_TO_SHORT(cs->predictor);
  453. // XXX: is this correct ??: *samples++ = cs->predictor;
  454. cs->step_index = *src++;
  455. if (cs->step_index < 0) cs->step_index = 0;
  456. if (cs->step_index > 88) cs->step_index = 88;
  457. if (*src++) fprintf(stderr, "unused byte should be null !!\n"); /* unused */
  458. if (st) {
  459. cs = &(c->status[1]);
  460. cs->predictor = (*src++) & 0x0FF;
  461. cs->predictor |= ((*src++) << 8) & 0x0FF00;
  462. if(cs->predictor & 0x8000)
  463. cs->predictor -= 0x10000;
  464. CLAMP_TO_SHORT(cs->predictor);
  465. // XXX: is this correct ??: *samples++ = cs->predictor;
  466. cs->step_index = *src++;
  467. if (cs->step_index < 0) cs->step_index = 0;
  468. if (cs->step_index > 88) cs->step_index = 88;
  469. src++; /* if != 0 -> out-of-sync */
  470. }
  471. for(m=4; src < (buf + buf_size);) {
  472. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F);
  473. if (st)
  474. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F);
  475. *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  476. if (st) {
  477. *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F);
  478. if (!--m) {
  479. m=4;
  480. src+=4;
  481. }
  482. }
  483. src++;
  484. }
  485. break;
  486. case CODEC_ID_ADPCM_4XM:
  487. cs = &(c->status[0]);
  488. c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  489. if(st){
  490. c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  491. }
  492. c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  493. if(st){
  494. c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  495. }
  496. // if (cs->step_index < 0) cs->step_index = 0;
  497. // if (cs->step_index > 88) cs->step_index = 88;
  498. m= (buf_size - (src - buf))>>st;
  499. //printf("%d %d %d %d\n", st, m, c->status[0].predictor, c->status[0].step_index);
  500. //FIXME / XXX decode chanels individual & interleave samples
  501. for(i=0; i<m; i++) {
  502. *samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] & 0x0F);
  503. if (st)
  504. *samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] & 0x0F);
  505. *samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] >> 4);
  506. if (st)
  507. *samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] >> 4);
  508. }
  509. src += m<<st;
  510. break;
  511. case CODEC_ID_ADPCM_MS:
  512. if (buf_size > BLKSIZE) {
  513. if (avctx->block_align != 0)
  514. buf_size = avctx->block_align;
  515. else
  516. buf_size = BLKSIZE;
  517. }
  518. n = buf_size - 7 * avctx->channels;
  519. if (n < 0)
  520. return -1;
  521. block_predictor[0] = (*src++); /* should be bound */
  522. block_predictor[0] = (block_predictor[0] < 0)?(0):((block_predictor[0] > 7)?(7):(block_predictor[0]));
  523. block_predictor[1] = 0;
  524. if (st)
  525. block_predictor[1] = (*src++);
  526. block_predictor[1] = (block_predictor[1] < 0)?(0):((block_predictor[1] > 7)?(7):(block_predictor[1]));
  527. c->status[0].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  528. if (c->status[0].idelta & 0x08000)
  529. c->status[0].idelta -= 0x10000;
  530. src+=2;
  531. if (st)
  532. c->status[1].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  533. if (st && c->status[1].idelta & 0x08000)
  534. c->status[1].idelta |= 0xFFFF0000;
  535. if (st)
  536. src+=2;
  537. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  538. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  539. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  540. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  541. c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  542. src+=2;
  543. if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  544. if (st) src+=2;
  545. c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  546. src+=2;
  547. if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  548. if (st) src+=2;
  549. *samples++ = c->status[0].sample1;
  550. if (st) *samples++ = c->status[1].sample1;
  551. *samples++ = c->status[0].sample2;
  552. if (st) *samples++ = c->status[1].sample2;
  553. for(;n>0;n--) {
  554. *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  555. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  556. src ++;
  557. }
  558. break;
  559. case CODEC_ID_ADPCM_IMA_DK4:
  560. if (buf_size > BLKSIZE) {
  561. if (avctx->block_align != 0)
  562. buf_size = avctx->block_align;
  563. else
  564. buf_size = BLKSIZE;
  565. }
  566. c->status[0].predictor = (src[0] | (src[1] << 8));
  567. c->status[0].step_index = src[2];
  568. src += 4;
  569. if(c->status[0].predictor & 0x8000)
  570. c->status[0].predictor -= 0x10000;
  571. *samples++ = c->status[0].predictor;
  572. if (st) {
  573. c->status[1].predictor = (src[0] | (src[1] << 8));
  574. c->status[1].step_index = src[2];
  575. src += 4;
  576. if(c->status[1].predictor & 0x8000)
  577. c->status[1].predictor -= 0x10000;
  578. *samples++ = c->status[1].predictor;
  579. }
  580. while (src < buf + buf_size) {
  581. /* take care of the top nibble (always left or mono channel) */
  582. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  583. (src[0] >> 4) & 0x0F);
  584. /* take care of the bottom nibble, which is right sample for
  585. * stereo, or another mono sample */
  586. if (st)
  587. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  588. src[0] & 0x0F);
  589. else
  590. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  591. src[0] & 0x0F);
  592. src++;
  593. }
  594. break;
  595. case CODEC_ID_ADPCM_IMA_DK3:
  596. if (buf_size > BLKSIZE) {
  597. if (avctx->block_align != 0)
  598. buf_size = avctx->block_align;
  599. else
  600. buf_size = BLKSIZE;
  601. }
  602. c->status[0].predictor = (src[10] | (src[11] << 8));
  603. c->status[1].predictor = (src[12] | (src[13] << 8));
  604. c->status[0].step_index = src[14];
  605. c->status[1].step_index = src[15];
  606. /* sign extend the predictors */
  607. if(c->status[0].predictor & 0x8000)
  608. c->status[0].predictor -= 0x10000;
  609. if(c->status[1].predictor & 0x8000)
  610. c->status[1].predictor -= 0x10000;
  611. src += 16;
  612. diff_channel = c->status[1].predictor;
  613. /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
  614. * the buffer is consumed */
  615. while (1) {
  616. /* for this algorithm, c->status[0] is the sum channel and
  617. * c->status[1] is the diff channel */
  618. /* process the first predictor of the sum channel */
  619. DK3_GET_NEXT_NIBBLE();
  620. adpcm_ima_expand_nibble(&c->status[0], nibble);
  621. /* process the diff channel predictor */
  622. DK3_GET_NEXT_NIBBLE();
  623. adpcm_ima_expand_nibble(&c->status[1], nibble);
  624. /* process the first pair of stereo PCM samples */
  625. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  626. *samples++ = c->status[0].predictor + c->status[1].predictor;
  627. *samples++ = c->status[0].predictor - c->status[1].predictor;
  628. /* process the second predictor of the sum channel */
  629. DK3_GET_NEXT_NIBBLE();
  630. adpcm_ima_expand_nibble(&c->status[0], nibble);
  631. /* process the second pair of stereo PCM samples */
  632. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  633. *samples++ = c->status[0].predictor + c->status[1].predictor;
  634. *samples++ = c->status[0].predictor - c->status[1].predictor;
  635. }
  636. break;
  637. case CODEC_ID_ADPCM_IMA_WS:
  638. /* no per-block initialization; just start decoding the data */
  639. while (src < buf + buf_size) {
  640. if (st) {
  641. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  642. (src[0] >> 4) & 0x0F);
  643. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  644. src[0] & 0x0F);
  645. } else {
  646. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  647. (src[0] >> 4) & 0x0F);
  648. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  649. src[0] & 0x0F);
  650. }
  651. src++;
  652. }
  653. break;
  654. case CODEC_ID_ADPCM_XA:
  655. c->status[0].sample1 = c->status[0].sample2 =
  656. c->status[1].sample1 = c->status[1].sample2 = 0;
  657. while (buf_size >= 128) {
  658. xa_decode(samples, src, &c->status[0], &c->status[1],
  659. avctx->channels);
  660. src += 128;
  661. samples += 28 * 8;
  662. buf_size -= 128;
  663. }
  664. break;
  665. default:
  666. *data_size = 0;
  667. return -1;
  668. }
  669. *data_size = (uint8_t *)samples - (uint8_t *)data;
  670. return src - buf;
  671. }
  672. #ifdef CONFIG_ENCODERS
  673. #define ADPCM_ENCODER(id,name) \
  674. AVCodec name ## _encoder = { \
  675. #name, \
  676. CODEC_TYPE_AUDIO, \
  677. id, \
  678. sizeof(ADPCMContext), \
  679. adpcm_encode_init, \
  680. adpcm_encode_frame, \
  681. adpcm_encode_close, \
  682. NULL, \
  683. };
  684. #else
  685. #define ADPCM_ENCODER(id,name)
  686. #endif
  687. #ifdef CONFIG_DECODERS
  688. #define ADPCM_DECODER(id,name) \
  689. AVCodec name ## _decoder = { \
  690. #name, \
  691. CODEC_TYPE_AUDIO, \
  692. id, \
  693. sizeof(ADPCMContext), \
  694. adpcm_decode_init, \
  695. NULL, \
  696. NULL, \
  697. adpcm_decode_frame, \
  698. };
  699. #else
  700. #define ADPCM_DECODER(id,name)
  701. #endif
  702. #define ADPCM_CODEC(id, name) \
  703. ADPCM_ENCODER(id,name) ADPCM_DECODER(id,name)
  704. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
  705. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
  706. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
  707. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
  708. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
  709. ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
  710. ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
  711. ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
  712. ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx);
  713. #undef ADPCM_CODEC