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.

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