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.

535 lines
18KB

  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_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
  269. {
  270. int predictor;
  271. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
  272. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  273. CLAMP_TO_SHORT(predictor);
  274. c->sample2 = c->sample1;
  275. c->sample1 = predictor;
  276. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) / 256;
  277. if (c->idelta < 16) c->idelta = 16;
  278. return (short)predictor;
  279. }
  280. static int adpcm_decode_frame(AVCodecContext *avctx,
  281. void *data, int *data_size,
  282. uint8_t *buf, int buf_size)
  283. {
  284. ADPCMContext *c = avctx->priv_data;
  285. ADPCMChannelStatus *cs;
  286. int n, m, channel;
  287. int block_predictor[2];
  288. short *samples;
  289. uint8_t *src;
  290. int st; /* stereo */
  291. samples = data;
  292. src = buf;
  293. st = avctx->channels == 2;
  294. switch(avctx->codec->id) {
  295. case CODEC_ID_ADPCM_IMA_QT:
  296. n = (buf_size - 2);/* >> 2*avctx->channels;*/
  297. channel = c->channel;
  298. cs = &(c->status[channel]);
  299. /* (pppppp) (piiiiiii) */
  300. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  301. cs->predictor = (*src++) << 8;
  302. cs->predictor |= (*src & 0x80);
  303. cs->predictor &= 0xFF80;
  304. /* sign extension */
  305. if(cs->predictor & 0x8000)
  306. cs->predictor -= 0x10000;
  307. CLAMP_TO_SHORT(cs->predictor);
  308. cs->step_index = (*src++) & 0x7F;
  309. if (cs->step_index > 88) fprintf(stderr, "ERROR: step_index = %i\n", cs->step_index);
  310. if (cs->step_index > 88) cs->step_index = 88;
  311. cs->step = step_table[cs->step_index];
  312. if (st && channel)
  313. samples++;
  314. *samples++ = cs->predictor;
  315. samples += st;
  316. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  317. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F);
  318. samples += avctx->channels;
  319. *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F);
  320. samples += avctx->channels;
  321. src ++;
  322. }
  323. if(st) { /* handle stereo interlacing */
  324. c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
  325. if(channel == 0) { /* wait for the other packet before outputing anything */
  326. *data_size = 0;
  327. return src - buf;
  328. }
  329. }
  330. break;
  331. case CODEC_ID_ADPCM_IMA_WAV:
  332. if (buf_size > BLKSIZE) {
  333. if (avctx->block_align != 0)
  334. buf_size = avctx->block_align;
  335. else
  336. buf_size = BLKSIZE;
  337. }
  338. // XXX: do as per-channel loop
  339. cs = &(c->status[0]);
  340. cs->predictor = (*src++) & 0x0FF;
  341. cs->predictor |= ((*src++) << 8) & 0x0FF00;
  342. if(cs->predictor & 0x8000)
  343. cs->predictor -= 0x10000;
  344. CLAMP_TO_SHORT(cs->predictor);
  345. // XXX: is this correct ??: *samples++ = cs->predictor;
  346. cs->step_index = *src++;
  347. if (cs->step_index < 0) cs->step_index = 0;
  348. if (cs->step_index > 88) cs->step_index = 88;
  349. if (*src++) fprintf(stderr, "unused byte should be null !!\n"); /* unused */
  350. if (st) {
  351. cs = &(c->status[1]);
  352. cs->predictor = (*src++) & 0x0FF;
  353. cs->predictor |= ((*src++) << 8) & 0x0FF00;
  354. if(cs->predictor & 0x8000)
  355. cs->predictor -= 0x10000;
  356. CLAMP_TO_SHORT(cs->predictor);
  357. // XXX: is this correct ??: *samples++ = cs->predictor;
  358. cs->step_index = *src++;
  359. if (cs->step_index < 0) cs->step_index = 0;
  360. if (cs->step_index > 88) cs->step_index = 88;
  361. src++; /* if != 0 -> out-of-sync */
  362. }
  363. for(m=4; src < (buf + buf_size);) {
  364. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F);
  365. if (st)
  366. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F);
  367. *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  368. if (st) {
  369. *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F);
  370. if (!--m) {
  371. m=4;
  372. src+=4;
  373. }
  374. }
  375. src++;
  376. }
  377. break;
  378. case CODEC_ID_ADPCM_MS:
  379. if (buf_size > BLKSIZE) {
  380. if (avctx->block_align != 0)
  381. buf_size = avctx->block_align;
  382. else
  383. buf_size = BLKSIZE;
  384. }
  385. n = buf_size - 7 * avctx->channels;
  386. if (n < 0)
  387. return -1;
  388. block_predictor[0] = (*src++); /* should be bound */
  389. block_predictor[0] = (block_predictor[0] < 0)?(0):((block_predictor[0] > 7)?(7):(block_predictor[0]));
  390. block_predictor[1] = 0;
  391. if (st)
  392. block_predictor[1] = (*src++);
  393. block_predictor[1] = (block_predictor[1] < 0)?(0):((block_predictor[1] > 7)?(7):(block_predictor[1]));
  394. c->status[0].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  395. if (c->status[0].idelta & 0x08000)
  396. c->status[0].idelta -= 0x10000;
  397. src+=2;
  398. if (st)
  399. c->status[1].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  400. if (st && c->status[1].idelta & 0x08000)
  401. c->status[1].idelta |= 0xFFFF0000;
  402. if (st)
  403. src+=2;
  404. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  405. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  406. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  407. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  408. c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  409. src+=2;
  410. if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  411. if (st) src+=2;
  412. c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  413. src+=2;
  414. if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  415. if (st) src+=2;
  416. *samples++ = c->status[0].sample1;
  417. if (st) *samples++ = c->status[1].sample1;
  418. *samples++ = c->status[0].sample2;
  419. if (st) *samples++ = c->status[1].sample2;
  420. for(;n>0;n--) {
  421. *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  422. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  423. src ++;
  424. }
  425. break;
  426. default:
  427. *data_size = 0;
  428. return -1;
  429. }
  430. *data_size = (uint8_t *)samples - (uint8_t *)data;
  431. return src - buf;
  432. }
  433. #define ADPCM_CODEC(id, name) \
  434. AVCodec name ## _encoder = { \
  435. #name, \
  436. CODEC_TYPE_AUDIO, \
  437. id, \
  438. sizeof(ADPCMContext), \
  439. adpcm_encode_init, \
  440. adpcm_encode_frame, \
  441. adpcm_encode_close, \
  442. NULL, \
  443. }; \
  444. AVCodec name ## _decoder = { \
  445. #name, \
  446. CODEC_TYPE_AUDIO, \
  447. id, \
  448. sizeof(ADPCMContext), \
  449. adpcm_decode_init, \
  450. NULL, \
  451. NULL, \
  452. adpcm_decode_frame, \
  453. };
  454. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
  455. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
  456. ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
  457. #undef ADPCM_CODEC