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.

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