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.

902 lines
30KB

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