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.

736 lines
25KB

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