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.

956 lines
32KB

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