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.

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