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.

719 lines
24KB

  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. *
  27. * Features and limitations:
  28. *
  29. * Reference documents:
  30. * http://www.pcisys.net/~melanson/codecs/simpleaudio.html
  31. * http://www.geocities.com/SiliconValley/8682/aud3.txt
  32. * http://openquicktime.sourceforge.net/plugins.htm
  33. * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
  34. * http://www.cs.ucla.edu/~leec/mediabench/applications.html
  35. * SoX source code http://home.sprynet.com/~cbagwell/sox.html
  36. */
  37. #define BLKSIZE 1024
  38. #define CLAMP_TO_SHORT(value) \
  39. if (value > 32767) \
  40. value = 32767; \
  41. else if (value < -32768) \
  42. value = -32768; \
  43. /* step_table[] and index_table[] are from the ADPCM reference source */
  44. /* This is the index table: */
  45. static const int index_table[16] = {
  46. -1, -1, -1, -1, 2, 4, 6, 8,
  47. -1, -1, -1, -1, 2, 4, 6, 8,
  48. };
  49. /**
  50. * This is the step table. Note that many programs use slight deviations from
  51. * this table, but such deviations are negligible:
  52. */
  53. static const int step_table[89] = {
  54. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  55. 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  56. 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  57. 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  58. 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  59. 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  60. 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  61. 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  62. 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  63. };
  64. /* Those are for MS-ADPCM */
  65. /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
  66. static const int AdaptationTable[] = {
  67. 230, 230, 230, 230, 307, 409, 512, 614,
  68. 768, 614, 512, 409, 307, 230, 230, 230
  69. };
  70. static const int AdaptCoeff1[] = {
  71. 256, 512, 0, 192, 240, 460, 392
  72. };
  73. static const int AdaptCoeff2[] = {
  74. 0, -256, 0, 64, 0, -208, -232
  75. };
  76. /* end of tables */
  77. typedef struct ADPCMChannelStatus {
  78. int predictor;
  79. short int step_index;
  80. int step;
  81. /* for encoding */
  82. int prev_sample;
  83. /* MS version */
  84. short sample1;
  85. short sample2;
  86. int coeff1;
  87. int coeff2;
  88. int idelta;
  89. } ADPCMChannelStatus;
  90. typedef struct ADPCMContext {
  91. int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
  92. ADPCMChannelStatus status[2];
  93. short sample_buffer[32]; /* hold left samples while waiting for right samples */
  94. } ADPCMContext;
  95. /* XXX: implement encoding */
  96. static int adpcm_encode_init(AVCodecContext *avctx)
  97. {
  98. if (avctx->channels > 2)
  99. return -1; /* only stereo or mono =) */
  100. switch(avctx->codec->id) {
  101. case CODEC_ID_ADPCM_IMA_QT:
  102. fprintf(stderr, "ADPCM: codec admcp_ima_qt unsupported for encoding !\n");
  103. avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */
  104. return -1;
  105. break;
  106. case CODEC_ID_ADPCM_IMA_WAV:
  107. avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
  108. /* and we have 4 bytes per channel overhead */
  109. avctx->block_align = BLKSIZE;
  110. /* seems frame_size isn't taken into account... have to buffer the samples :-( */
  111. break;
  112. case CODEC_ID_ADPCM_MS:
  113. fprintf(stderr, "ADPCM: codec admcp_ms unsupported for encoding !\n");
  114. return -1;
  115. break;
  116. default:
  117. return -1;
  118. break;
  119. }
  120. avctx->coded_frame= avcodec_alloc_frame();
  121. avctx->coded_frame->key_frame= 1;
  122. return 0;
  123. }
  124. static int adpcm_encode_close(AVCodecContext *avctx)
  125. {
  126. av_freep(&avctx->coded_frame);
  127. return 0;
  128. }
  129. static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
  130. {
  131. int step_index;
  132. unsigned char nibble;
  133. int sign = 0; /* sign bit of the nibble (MSB) */
  134. int delta, predicted_delta;
  135. delta = sample - c->prev_sample;
  136. if (delta < 0) {
  137. sign = 1;
  138. delta = -delta;
  139. }
  140. step_index = c->step_index;
  141. /* nibble = 4 * delta / step_table[step_index]; */
  142. nibble = (delta << 2) / step_table[step_index];
  143. if (nibble > 7)
  144. nibble = 7;
  145. step_index += index_table[nibble];
  146. if (step_index < 0)
  147. step_index = 0;
  148. if (step_index > 88)
  149. step_index = 88;
  150. /* what the decoder will find */
  151. predicted_delta = ((step_table[step_index] * nibble) / 4) + (step_table[step_index] / 8);
  152. if (sign)
  153. c->prev_sample -= predicted_delta;
  154. else
  155. c->prev_sample += predicted_delta;
  156. CLAMP_TO_SHORT(c->prev_sample);
  157. nibble += sign << 3; /* sign * 8 */
  158. /* save back */
  159. c->step_index = step_index;
  160. return nibble;
  161. }
  162. static int adpcm_encode_frame(AVCodecContext *avctx,
  163. unsigned char *frame, int buf_size, void *data)
  164. {
  165. int n;
  166. short *samples;
  167. unsigned char *dst;
  168. ADPCMContext *c = avctx->priv_data;
  169. dst = frame;
  170. samples = (short *)data;
  171. /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
  172. switch(avctx->codec->id) {
  173. case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
  174. break;
  175. case CODEC_ID_ADPCM_IMA_WAV:
  176. n = avctx->frame_size / 8;
  177. c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
  178. /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
  179. *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
  180. *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
  181. *dst++ = (unsigned char)c->status[0].step_index;
  182. *dst++ = 0; /* unknown */
  183. samples++;
  184. if (avctx->channels == 2) {
  185. c->status[1].prev_sample = (signed short)samples[0];
  186. /* c->status[1].step_index = 0; */
  187. *dst++ = (c->status[1].prev_sample) & 0xFF;
  188. *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
  189. *dst++ = (unsigned char)c->status[1].step_index;
  190. *dst++ = 0;
  191. samples++;
  192. }
  193. /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
  194. for (; n>0; n--) {
  195. *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F;
  196. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0;
  197. dst++;
  198. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F;
  199. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0;
  200. dst++;
  201. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F;
  202. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0;
  203. dst++;
  204. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F;
  205. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0;
  206. dst++;
  207. /* right channel */
  208. if (avctx->channels == 2) {
  209. *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
  210. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
  211. dst++;
  212. *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
  213. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
  214. dst++;
  215. *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
  216. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
  217. dst++;
  218. *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
  219. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
  220. dst++;
  221. }
  222. samples += 8 * avctx->channels;
  223. }
  224. break;
  225. default:
  226. return -1;
  227. }
  228. return dst - frame;
  229. }
  230. static int adpcm_decode_init(AVCodecContext * avctx)
  231. {
  232. ADPCMContext *c = avctx->priv_data;
  233. c->channel = 0;
  234. c->status[0].predictor = c->status[1].predictor = 0;
  235. c->status[0].step_index = c->status[1].step_index = 0;
  236. c->status[0].step = c->status[1].step = 0;
  237. switch(avctx->codec->id) {
  238. default:
  239. break;
  240. }
  241. return 0;
  242. }
  243. static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble)
  244. {
  245. int step_index;
  246. int predictor;
  247. int sign, delta, diff, step;
  248. step = step_table[c->step_index];
  249. step_index = c->step_index + index_table[(unsigned)nibble];
  250. if (step_index < 0) step_index = 0;
  251. else if (step_index > 88) step_index = 88;
  252. sign = nibble & 8;
  253. delta = nibble & 7;
  254. /* perform direct multiplication instead of series of jumps proposed by
  255. * the reference ADPCM implementation since modern CPUs can do the mults
  256. * quickly enough */
  257. diff = ((2 * delta + 1) * step) >> 3;
  258. predictor = c->predictor;
  259. if (sign) predictor -= diff;
  260. else predictor += diff;
  261. CLAMP_TO_SHORT(predictor);
  262. c->predictor = predictor;
  263. c->step_index = step_index;
  264. return (short)predictor;
  265. }
  266. static inline short adpcm_4xa_expand_nibble(ADPCMChannelStatus *c, char nibble)
  267. {
  268. int step_index;
  269. int predictor;
  270. int sign, delta, diff, step;
  271. step = step_table[c->step_index];
  272. step_index = c->step_index + index_table[(unsigned)nibble];
  273. if (step_index < 0) step_index = 0;
  274. else if (step_index > 88) step_index = 88;
  275. sign = nibble & 8;
  276. delta = nibble & 7;
  277. diff = (delta*step + (step>>1))>>3; // difference to code above
  278. predictor = c->predictor;
  279. if (sign) predictor -= diff;
  280. else predictor += diff;
  281. CLAMP_TO_SHORT(predictor);
  282. c->predictor = predictor;
  283. c->step_index = step_index;
  284. return (short)predictor;
  285. }
  286. static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
  287. {
  288. int predictor;
  289. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
  290. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  291. CLAMP_TO_SHORT(predictor);
  292. c->sample2 = c->sample1;
  293. c->sample1 = predictor;
  294. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) / 256;
  295. if (c->idelta < 16) c->idelta = 16;
  296. return (short)predictor;
  297. }
  298. /* DK3 ADPCM support macro */
  299. #define DK3_GET_NEXT_NIBBLE() \
  300. if (decode_top_nibble_next) \
  301. { \
  302. nibble = (last_byte >> 4) & 0x0F; \
  303. decode_top_nibble_next = 0; \
  304. } \
  305. else \
  306. { \
  307. last_byte = *src++; \
  308. if (src >= buf + buf_size) break; \
  309. nibble = last_byte & 0x0F; \
  310. decode_top_nibble_next = 1; \
  311. }
  312. static int adpcm_decode_frame(AVCodecContext *avctx,
  313. void *data, int *data_size,
  314. uint8_t *buf, int buf_size)
  315. {
  316. ADPCMContext *c = avctx->priv_data;
  317. ADPCMChannelStatus *cs;
  318. int n, m, channel, i;
  319. int block_predictor[2];
  320. short *samples;
  321. uint8_t *src;
  322. int st; /* stereo */
  323. /* DK3 ADPCM accounting variables */
  324. unsigned char last_byte = 0;
  325. unsigned char nibble;
  326. int decode_top_nibble_next = 0;
  327. int diff_channel;
  328. samples = data;
  329. src = buf;
  330. st = avctx->channels == 2;
  331. switch(avctx->codec->id) {
  332. case CODEC_ID_ADPCM_IMA_QT:
  333. n = (buf_size - 2);/* >> 2*avctx->channels;*/
  334. channel = c->channel;
  335. cs = &(c->status[channel]);
  336. /* (pppppp) (piiiiiii) */
  337. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  338. cs->predictor = (*src++) << 8;
  339. cs->predictor |= (*src & 0x80);
  340. cs->predictor &= 0xFF80;
  341. /* sign extension */
  342. if(cs->predictor & 0x8000)
  343. cs->predictor -= 0x10000;
  344. CLAMP_TO_SHORT(cs->predictor);
  345. cs->step_index = (*src++) & 0x7F;
  346. if (cs->step_index > 88) fprintf(stderr, "ERROR: step_index = %i\n", cs->step_index);
  347. if (cs->step_index > 88) cs->step_index = 88;
  348. cs->step = step_table[cs->step_index];
  349. if (st && channel)
  350. samples++;
  351. *samples++ = cs->predictor;
  352. samples += st;
  353. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  354. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F);
  355. samples += avctx->channels;
  356. *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F);
  357. samples += avctx->channels;
  358. src ++;
  359. }
  360. if(st) { /* handle stereo interlacing */
  361. c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
  362. if(channel == 0) { /* wait for the other packet before outputing anything */
  363. *data_size = 0;
  364. return src - buf;
  365. }
  366. }
  367. break;
  368. case CODEC_ID_ADPCM_IMA_WAV:
  369. if (buf_size > BLKSIZE) {
  370. if (avctx->block_align != 0)
  371. buf_size = avctx->block_align;
  372. else
  373. buf_size = BLKSIZE;
  374. }
  375. // XXX: do as per-channel loop
  376. cs = &(c->status[0]);
  377. cs->predictor = (*src++) & 0x0FF;
  378. cs->predictor |= ((*src++) << 8) & 0x0FF00;
  379. if(cs->predictor & 0x8000)
  380. cs->predictor -= 0x10000;
  381. CLAMP_TO_SHORT(cs->predictor);
  382. // XXX: is this correct ??: *samples++ = cs->predictor;
  383. cs->step_index = *src++;
  384. if (cs->step_index < 0) cs->step_index = 0;
  385. if (cs->step_index > 88) cs->step_index = 88;
  386. if (*src++) fprintf(stderr, "unused byte should be null !!\n"); /* unused */
  387. if (st) {
  388. cs = &(c->status[1]);
  389. cs->predictor = (*src++) & 0x0FF;
  390. cs->predictor |= ((*src++) << 8) & 0x0FF00;
  391. if(cs->predictor & 0x8000)
  392. cs->predictor -= 0x10000;
  393. CLAMP_TO_SHORT(cs->predictor);
  394. // XXX: is this correct ??: *samples++ = cs->predictor;
  395. cs->step_index = *src++;
  396. if (cs->step_index < 0) cs->step_index = 0;
  397. if (cs->step_index > 88) cs->step_index = 88;
  398. src++; /* if != 0 -> out-of-sync */
  399. }
  400. for(m=4; src < (buf + buf_size);) {
  401. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F);
  402. if (st)
  403. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F);
  404. *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  405. if (st) {
  406. *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F);
  407. if (!--m) {
  408. m=4;
  409. src+=4;
  410. }
  411. }
  412. src++;
  413. }
  414. break;
  415. case CODEC_ID_ADPCM_4XM:
  416. cs = &(c->status[0]);
  417. c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  418. if(st){
  419. c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  420. }
  421. c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  422. if(st){
  423. c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  424. }
  425. // if (cs->step_index < 0) cs->step_index = 0;
  426. // if (cs->step_index > 88) cs->step_index = 88;
  427. m= (buf_size - (src - buf))>>st;
  428. //printf("%d %d %d %d\n", st, m, c->status[0].predictor, c->status[0].step_index);
  429. //FIXME / XXX decode chanels individual & interleave samples
  430. for(i=0; i<m; i++) {
  431. *samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] & 0x0F);
  432. if (st)
  433. *samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] & 0x0F);
  434. *samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] >> 4);
  435. if (st)
  436. *samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] >> 4);
  437. }
  438. src += m<<st;
  439. break;
  440. case CODEC_ID_ADPCM_MS:
  441. if (buf_size > BLKSIZE) {
  442. if (avctx->block_align != 0)
  443. buf_size = avctx->block_align;
  444. else
  445. buf_size = BLKSIZE;
  446. }
  447. n = buf_size - 7 * avctx->channels;
  448. if (n < 0)
  449. return -1;
  450. block_predictor[0] = (*src++); /* should be bound */
  451. block_predictor[0] = (block_predictor[0] < 0)?(0):((block_predictor[0] > 7)?(7):(block_predictor[0]));
  452. block_predictor[1] = 0;
  453. if (st)
  454. block_predictor[1] = (*src++);
  455. block_predictor[1] = (block_predictor[1] < 0)?(0):((block_predictor[1] > 7)?(7):(block_predictor[1]));
  456. c->status[0].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  457. if (c->status[0].idelta & 0x08000)
  458. c->status[0].idelta -= 0x10000;
  459. src+=2;
  460. if (st)
  461. c->status[1].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  462. if (st && c->status[1].idelta & 0x08000)
  463. c->status[1].idelta |= 0xFFFF0000;
  464. if (st)
  465. src+=2;
  466. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  467. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  468. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  469. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  470. c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  471. src+=2;
  472. if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  473. if (st) src+=2;
  474. c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  475. src+=2;
  476. if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  477. if (st) src+=2;
  478. *samples++ = c->status[0].sample1;
  479. if (st) *samples++ = c->status[1].sample1;
  480. *samples++ = c->status[0].sample2;
  481. if (st) *samples++ = c->status[1].sample2;
  482. for(;n>0;n--) {
  483. *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  484. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  485. src ++;
  486. }
  487. break;
  488. case CODEC_ID_ADPCM_IMA_DK4:
  489. if (buf_size > BLKSIZE) {
  490. if (avctx->block_align != 0)
  491. buf_size = avctx->block_align;
  492. else
  493. buf_size = BLKSIZE;
  494. }
  495. c->status[0].predictor = (src[0] | (src[1] << 8));
  496. c->status[0].step_index = src[2];
  497. src += 4;
  498. if(c->status[0].predictor & 0x8000)
  499. c->status[0].predictor -= 0x10000;
  500. *samples++ = c->status[0].predictor;
  501. if (st) {
  502. c->status[1].predictor = (src[0] | (src[1] << 8));
  503. c->status[1].step_index = src[2];
  504. src += 4;
  505. if(c->status[1].predictor & 0x8000)
  506. c->status[1].predictor -= 0x10000;
  507. *samples++ = c->status[1].predictor;
  508. }
  509. while (src < buf + buf_size) {
  510. /* take care of the top nibble (always left or mono channel) */
  511. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  512. (src[0] >> 4) & 0x0F);
  513. /* take care of the bottom nibble, which is right sample for
  514. * stereo, or another mono sample */
  515. if (st)
  516. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  517. src[0] & 0x0F);
  518. else
  519. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  520. src[0] & 0x0F);
  521. src++;
  522. }
  523. break;
  524. case CODEC_ID_ADPCM_IMA_DK3:
  525. if (buf_size > BLKSIZE) {
  526. if (avctx->block_align != 0)
  527. buf_size = avctx->block_align;
  528. else
  529. buf_size = BLKSIZE;
  530. }
  531. c->status[0].predictor = (src[10] | (src[11] << 8));
  532. c->status[1].predictor = (src[12] | (src[13] << 8));
  533. c->status[0].step_index = src[14];
  534. c->status[1].step_index = src[15];
  535. /* sign extend the predictors */
  536. if(c->status[0].predictor & 0x8000)
  537. c->status[0].predictor -= 0x10000;
  538. if(c->status[1].predictor & 0x8000)
  539. c->status[1].predictor -= 0x10000;
  540. src += 16;
  541. diff_channel = c->status[1].predictor;
  542. /* the DK3_GET_NEXT_NIBBLE macro issues the break statement when
  543. * the buffer is consumed */
  544. while (1) {
  545. /* for this algorithm, c->status[0] is the sum channel and
  546. * c->status[1] is the diff channel */
  547. /* process the first predictor of the sum channel */
  548. DK3_GET_NEXT_NIBBLE();
  549. adpcm_ima_expand_nibble(&c->status[0], nibble);
  550. /* process the diff channel predictor */
  551. DK3_GET_NEXT_NIBBLE();
  552. adpcm_ima_expand_nibble(&c->status[1], nibble);
  553. /* process the first pair of stereo PCM samples */
  554. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  555. *samples++ = c->status[0].predictor + c->status[1].predictor;
  556. *samples++ = c->status[0].predictor - c->status[1].predictor;
  557. /* process the second predictor of the sum channel */
  558. DK3_GET_NEXT_NIBBLE();
  559. adpcm_ima_expand_nibble(&c->status[0], nibble);
  560. /* process the second pair of stereo PCM samples */
  561. diff_channel = (diff_channel + c->status[1].predictor) / 2;
  562. *samples++ = c->status[0].predictor + c->status[1].predictor;
  563. *samples++ = c->status[0].predictor - c->status[1].predictor;
  564. }
  565. break;
  566. case CODEC_ID_ADPCM_IMA_WS:
  567. /* no per-block initialization; just start decoding the data */
  568. while (src < buf + buf_size) {
  569. if (st) {
  570. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  571. (src[0] >> 4) & 0x0F);
  572. *samples++ = adpcm_ima_expand_nibble(&c->status[1],
  573. src[0] & 0x0F);
  574. } else {
  575. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  576. (src[0] >> 4) & 0x0F);
  577. *samples++ = adpcm_ima_expand_nibble(&c->status[0],
  578. src[0] & 0x0F);
  579. }
  580. src++;
  581. }
  582. break;
  583. default:
  584. *data_size = 0;
  585. return -1;
  586. }
  587. *data_size = (uint8_t *)samples - (uint8_t *)data;
  588. return src - buf;
  589. }
  590. #define ADPCM_CODEC(id, name) \
  591. AVCodec name ## _encoder = { \
  592. #name, \
  593. CODEC_TYPE_AUDIO, \
  594. id, \
  595. sizeof(ADPCMContext), \
  596. adpcm_encode_init, \
  597. adpcm_encode_frame, \
  598. adpcm_encode_close, \
  599. NULL, \
  600. }; \
  601. AVCodec name ## _decoder = { \
  602. #name, \
  603. CODEC_TYPE_AUDIO, \
  604. id, \
  605. sizeof(ADPCMContext), \
  606. adpcm_decode_init, \
  607. NULL, \
  608. NULL, \
  609. adpcm_decode_frame, \
  610. };
  611. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
  612. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
  613. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK3, adpcm_ima_dk3);
  614. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_DK4, adpcm_ima_dk4);
  615. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WS, adpcm_ima_ws);
  616. ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
  617. ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
  618. #undef ADPCM_CODEC