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.

591 lines
19KB

  1. /*
  2. * ADPCM codecs
  3. * Copyright (c) 2001 Fabrice Bellard.
  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. *
  25. * Features and limitations:
  26. *
  27. * Reference documents:
  28. * http://www.pcisys.net/~melanson/codecs/adpcm.txt
  29. * http://www.geocities.com/SiliconValley/8682/aud3.txt
  30. * http://openquicktime.sourceforge.net/plugins.htm
  31. * XAnim sources (xa_codec.c) http://www.rasnaimaging.com/people/lapus/download.html
  32. * http://www.cs.ucla.edu/~leec/mediabench/applications.html
  33. * SoX source code http://home.sprynet.com/~cbagwell/sox.html
  34. */
  35. #define BLKSIZE 1024
  36. #define CLAMP_TO_SHORT(value) \
  37. if (value > 32767) \
  38. value = 32767; \
  39. else if (value < -32768) \
  40. value = -32768; \
  41. /* step_table[] and index_table[] are from the ADPCM reference source */
  42. /* This is the index table: */
  43. static const int index_table[16] = {
  44. -1, -1, -1, -1, 2, 4, 6, 8,
  45. -1, -1, -1, -1, 2, 4, 6, 8,
  46. };
  47. /**
  48. * This is the step table. Note that many programs use slight deviations from
  49. * this table, but such deviations are negligible:
  50. */
  51. static const int step_table[89] = {
  52. 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  53. 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  54. 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  55. 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  56. 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  57. 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  58. 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  59. 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  60. 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  61. };
  62. /* Those are for MS-ADPCM */
  63. /* AdaptationTable[], AdaptCoeff1[], and AdaptCoeff2[] are from libsndfile */
  64. static const int AdaptationTable[] = {
  65. 230, 230, 230, 230, 307, 409, 512, 614,
  66. 768, 614, 512, 409, 307, 230, 230, 230
  67. };
  68. static const int AdaptCoeff1[] = {
  69. 256, 512, 0, 192, 240, 460, 392
  70. };
  71. static const int AdaptCoeff2[] = {
  72. 0, -256, 0, 64, 0, -208, -232
  73. };
  74. /* end of tables */
  75. typedef struct ADPCMChannelStatus {
  76. int predictor;
  77. short int step_index;
  78. int step;
  79. /* for encoding */
  80. int prev_sample;
  81. /* MS version */
  82. short sample1;
  83. short sample2;
  84. int coeff1;
  85. int coeff2;
  86. int idelta;
  87. } ADPCMChannelStatus;
  88. typedef struct ADPCMContext {
  89. int channel; /* for stereo MOVs, decode left, then decode right, then tell it's decoded */
  90. ADPCMChannelStatus status[2];
  91. short sample_buffer[32]; /* hold left samples while waiting for right samples */
  92. } ADPCMContext;
  93. /* XXX: implement encoding */
  94. static int adpcm_encode_init(AVCodecContext *avctx)
  95. {
  96. if (avctx->channels > 2)
  97. return -1; /* only stereo or mono =) */
  98. switch(avctx->codec->id) {
  99. case CODEC_ID_ADPCM_IMA_QT:
  100. fprintf(stderr, "ADPCM: codec admcp_ima_qt unsupported for encoding !\n");
  101. avctx->frame_size = 64; /* XXX: can multiple of avctx->channels * 64 (left and right blocks are interleaved) */
  102. return -1;
  103. break;
  104. case CODEC_ID_ADPCM_IMA_WAV:
  105. avctx->frame_size = (BLKSIZE - 4 * avctx->channels) * 8 / (4 * avctx->channels) + 1; /* each 16 bits sample gives one nibble */
  106. /* and we have 4 bytes per channel overhead */
  107. avctx->block_align = BLKSIZE;
  108. /* seems frame_size isn't taken into account... have to buffer the samples :-( */
  109. break;
  110. case CODEC_ID_ADPCM_MS:
  111. fprintf(stderr, "ADPCM: codec admcp_ms unsupported for encoding !\n");
  112. return -1;
  113. break;
  114. default:
  115. return -1;
  116. break;
  117. }
  118. avctx->coded_frame= avcodec_alloc_frame();
  119. avctx->coded_frame->key_frame= 1;
  120. return 0;
  121. }
  122. static int adpcm_encode_close(AVCodecContext *avctx)
  123. {
  124. av_freep(&avctx->coded_frame);
  125. return 0;
  126. }
  127. static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
  128. {
  129. int step_index;
  130. unsigned char nibble;
  131. int sign = 0; /* sign bit of the nibble (MSB) */
  132. int delta, predicted_delta;
  133. delta = sample - c->prev_sample;
  134. if (delta < 0) {
  135. sign = 1;
  136. delta = -delta;
  137. }
  138. step_index = c->step_index;
  139. /* nibble = 4 * delta / step_table[step_index]; */
  140. nibble = (delta << 2) / step_table[step_index];
  141. if (nibble > 7)
  142. nibble = 7;
  143. step_index += index_table[nibble];
  144. if (step_index < 0)
  145. step_index = 0;
  146. if (step_index > 88)
  147. step_index = 88;
  148. /* what the decoder will find */
  149. predicted_delta = ((step_table[step_index] * nibble) / 4) + (step_table[step_index] / 8);
  150. if (sign)
  151. c->prev_sample -= predicted_delta;
  152. else
  153. c->prev_sample += predicted_delta;
  154. CLAMP_TO_SHORT(c->prev_sample);
  155. nibble += sign << 3; /* sign * 8 */
  156. /* save back */
  157. c->step_index = step_index;
  158. return nibble;
  159. }
  160. static int adpcm_encode_frame(AVCodecContext *avctx,
  161. unsigned char *frame, int buf_size, void *data)
  162. {
  163. int n;
  164. short *samples;
  165. unsigned char *dst;
  166. ADPCMContext *c = avctx->priv_data;
  167. dst = frame;
  168. samples = (short *)data;
  169. /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
  170. switch(avctx->codec->id) {
  171. case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
  172. break;
  173. case CODEC_ID_ADPCM_IMA_WAV:
  174. n = avctx->frame_size / 8;
  175. c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
  176. /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
  177. *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
  178. *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
  179. *dst++ = (unsigned char)c->status[0].step_index;
  180. *dst++ = 0; /* unknown */
  181. samples++;
  182. if (avctx->channels == 2) {
  183. c->status[1].prev_sample = (signed short)samples[0];
  184. /* c->status[1].step_index = 0; */
  185. *dst++ = (c->status[1].prev_sample) & 0xFF;
  186. *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
  187. *dst++ = (unsigned char)c->status[1].step_index;
  188. *dst++ = 0;
  189. samples++;
  190. }
  191. /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
  192. for (; n>0; n--) {
  193. *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F;
  194. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0;
  195. dst++;
  196. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F;
  197. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0;
  198. dst++;
  199. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F;
  200. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0;
  201. dst++;
  202. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F;
  203. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0;
  204. dst++;
  205. /* right channel */
  206. if (avctx->channels == 2) {
  207. *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
  208. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
  209. dst++;
  210. *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
  211. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
  212. dst++;
  213. *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
  214. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
  215. dst++;
  216. *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
  217. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
  218. dst++;
  219. }
  220. samples += 8 * avctx->channels;
  221. }
  222. break;
  223. default:
  224. return -1;
  225. }
  226. return dst - frame;
  227. }
  228. static int adpcm_decode_init(AVCodecContext * avctx)
  229. {
  230. ADPCMContext *c = avctx->priv_data;
  231. c->channel = 0;
  232. c->status[0].predictor = c->status[1].predictor = 0;
  233. c->status[0].step_index = c->status[1].step_index = 0;
  234. c->status[0].step = c->status[1].step = 0;
  235. switch(avctx->codec->id) {
  236. default:
  237. break;
  238. }
  239. return 0;
  240. }
  241. static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble)
  242. {
  243. int step_index;
  244. int predictor;
  245. int sign, delta, diff, step;
  246. step = step_table[c->step_index];
  247. step_index = c->step_index + index_table[(unsigned)nibble];
  248. if (step_index < 0) step_index = 0;
  249. else if (step_index > 88) step_index = 88;
  250. sign = nibble & 8;
  251. delta = nibble & 7;
  252. #if 0
  253. diff = step >> 3;
  254. if (delta & 4) diff += step;
  255. if (delta & 2) diff += step >> 1;
  256. if (delta & 1) diff += step >> 2;
  257. #else
  258. diff = ((2 * delta + 1) * step) >> 3; // no jumps
  259. #endif
  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. static int adpcm_decode_frame(AVCodecContext *avctx,
  301. void *data, int *data_size,
  302. uint8_t *buf, int buf_size)
  303. {
  304. ADPCMContext *c = avctx->priv_data;
  305. ADPCMChannelStatus *cs;
  306. int n, m, channel, i;
  307. int block_predictor[2];
  308. short *samples;
  309. uint8_t *src;
  310. int st; /* stereo */
  311. samples = data;
  312. src = buf;
  313. st = avctx->channels == 2;
  314. switch(avctx->codec->id) {
  315. case CODEC_ID_ADPCM_IMA_QT:
  316. n = (buf_size - 2);/* >> 2*avctx->channels;*/
  317. channel = c->channel;
  318. cs = &(c->status[channel]);
  319. /* (pppppp) (piiiiiii) */
  320. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  321. cs->predictor = (*src++) << 8;
  322. cs->predictor |= (*src & 0x80);
  323. cs->predictor &= 0xFF80;
  324. /* sign extension */
  325. if(cs->predictor & 0x8000)
  326. cs->predictor -= 0x10000;
  327. CLAMP_TO_SHORT(cs->predictor);
  328. cs->step_index = (*src++) & 0x7F;
  329. if (cs->step_index > 88) fprintf(stderr, "ERROR: step_index = %i\n", cs->step_index);
  330. if (cs->step_index > 88) cs->step_index = 88;
  331. cs->step = step_table[cs->step_index];
  332. if (st && channel)
  333. samples++;
  334. *samples++ = cs->predictor;
  335. samples += st;
  336. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  337. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F);
  338. samples += avctx->channels;
  339. *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F);
  340. samples += avctx->channels;
  341. src ++;
  342. }
  343. if(st) { /* handle stereo interlacing */
  344. c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
  345. if(channel == 0) { /* wait for the other packet before outputing anything */
  346. *data_size = 0;
  347. return src - buf;
  348. }
  349. }
  350. break;
  351. case CODEC_ID_ADPCM_IMA_WAV:
  352. if (buf_size > BLKSIZE) {
  353. if (avctx->block_align != 0)
  354. buf_size = avctx->block_align;
  355. else
  356. buf_size = BLKSIZE;
  357. }
  358. // XXX: do as per-channel loop
  359. cs = &(c->status[0]);
  360. cs->predictor = (*src++) & 0x0FF;
  361. cs->predictor |= ((*src++) << 8) & 0x0FF00;
  362. if(cs->predictor & 0x8000)
  363. cs->predictor -= 0x10000;
  364. CLAMP_TO_SHORT(cs->predictor);
  365. // XXX: is this correct ??: *samples++ = cs->predictor;
  366. cs->step_index = *src++;
  367. if (cs->step_index < 0) cs->step_index = 0;
  368. if (cs->step_index > 88) cs->step_index = 88;
  369. if (*src++) fprintf(stderr, "unused byte should be null !!\n"); /* unused */
  370. if (st) {
  371. cs = &(c->status[1]);
  372. cs->predictor = (*src++) & 0x0FF;
  373. cs->predictor |= ((*src++) << 8) & 0x0FF00;
  374. if(cs->predictor & 0x8000)
  375. cs->predictor -= 0x10000;
  376. CLAMP_TO_SHORT(cs->predictor);
  377. // XXX: is this correct ??: *samples++ = cs->predictor;
  378. cs->step_index = *src++;
  379. if (cs->step_index < 0) cs->step_index = 0;
  380. if (cs->step_index > 88) cs->step_index = 88;
  381. src++; /* if != 0 -> out-of-sync */
  382. }
  383. for(m=4; src < (buf + buf_size);) {
  384. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F);
  385. if (st)
  386. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F);
  387. *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  388. if (st) {
  389. *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F);
  390. if (!--m) {
  391. m=4;
  392. src+=4;
  393. }
  394. }
  395. src++;
  396. }
  397. break;
  398. case CODEC_ID_ADPCM_4XM:
  399. cs = &(c->status[0]);
  400. c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  401. if(st){
  402. c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  403. }
  404. c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  405. if(st){
  406. c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;
  407. }
  408. // if (cs->step_index < 0) cs->step_index = 0;
  409. // if (cs->step_index > 88) cs->step_index = 88;
  410. m= (buf_size - (src - buf))>>st;
  411. //printf("%d %d %d %d\n", st, m, c->status[0].predictor, c->status[0].step_index);
  412. //FIXME / XXX decode chanels individual & interleave samples
  413. for(i=0; i<m; i++) {
  414. *samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] & 0x0F);
  415. if (st)
  416. *samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] & 0x0F);
  417. *samples++ = adpcm_4xa_expand_nibble(&c->status[0], src[i] >> 4);
  418. if (st)
  419. *samples++ = adpcm_4xa_expand_nibble(&c->status[1], src[i+m] >> 4);
  420. }
  421. src += m<<st;
  422. break;
  423. case CODEC_ID_ADPCM_MS:
  424. if (buf_size > BLKSIZE) {
  425. if (avctx->block_align != 0)
  426. buf_size = avctx->block_align;
  427. else
  428. buf_size = BLKSIZE;
  429. }
  430. n = buf_size - 7 * avctx->channels;
  431. if (n < 0)
  432. return -1;
  433. block_predictor[0] = (*src++); /* should be bound */
  434. block_predictor[0] = (block_predictor[0] < 0)?(0):((block_predictor[0] > 7)?(7):(block_predictor[0]));
  435. block_predictor[1] = 0;
  436. if (st)
  437. block_predictor[1] = (*src++);
  438. block_predictor[1] = (block_predictor[1] < 0)?(0):((block_predictor[1] > 7)?(7):(block_predictor[1]));
  439. c->status[0].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  440. if (c->status[0].idelta & 0x08000)
  441. c->status[0].idelta -= 0x10000;
  442. src+=2;
  443. if (st)
  444. c->status[1].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  445. if (st && c->status[1].idelta & 0x08000)
  446. c->status[1].idelta |= 0xFFFF0000;
  447. if (st)
  448. src+=2;
  449. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  450. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  451. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  452. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  453. c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  454. src+=2;
  455. if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  456. if (st) src+=2;
  457. c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  458. src+=2;
  459. if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  460. if (st) src+=2;
  461. *samples++ = c->status[0].sample1;
  462. if (st) *samples++ = c->status[1].sample1;
  463. *samples++ = c->status[0].sample2;
  464. if (st) *samples++ = c->status[1].sample2;
  465. for(;n>0;n--) {
  466. *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  467. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  468. src ++;
  469. }
  470. break;
  471. default:
  472. *data_size = 0;
  473. return -1;
  474. }
  475. *data_size = (uint8_t *)samples - (uint8_t *)data;
  476. return src - buf;
  477. }
  478. #define ADPCM_CODEC(id, name) \
  479. AVCodec name ## _encoder = { \
  480. #name, \
  481. CODEC_TYPE_AUDIO, \
  482. id, \
  483. sizeof(ADPCMContext), \
  484. adpcm_encode_init, \
  485. adpcm_encode_frame, \
  486. adpcm_encode_close, \
  487. NULL, \
  488. }; \
  489. AVCodec name ## _decoder = { \
  490. #name, \
  491. CODEC_TYPE_AUDIO, \
  492. id, \
  493. sizeof(ADPCMContext), \
  494. adpcm_decode_init, \
  495. NULL, \
  496. NULL, \
  497. adpcm_decode_frame, \
  498. };
  499. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
  500. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
  501. ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
  502. ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
  503. #undef ADPCM_CODEC