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. * 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. avctx->coded_frame= avcodec_alloc_frame();
  116. avctx->coded_frame->key_frame= 1;
  117. return 0;
  118. }
  119. static int adpcm_encode_close(AVCodecContext *avctx)
  120. {
  121. av_freep(&avctx->coded_frame);
  122. return 0;
  123. }
  124. static inline unsigned char adpcm_ima_compress_sample(ADPCMChannelStatus *c, short sample)
  125. {
  126. int step_index;
  127. unsigned char nibble;
  128. int sign = 0; /* sign bit of the nibble (MSB) */
  129. int delta, predicted_delta;
  130. delta = sample - c->prev_sample;
  131. if (delta < 0) {
  132. sign = 1;
  133. delta = -delta;
  134. }
  135. step_index = c->step_index;
  136. /* nibble = 4 * delta / step_table[step_index]; */
  137. nibble = (delta << 2) / step_table[step_index];
  138. if (nibble > 7)
  139. nibble = 7;
  140. step_index += index_table[nibble];
  141. if (step_index < 0)
  142. step_index = 0;
  143. if (step_index > 88)
  144. step_index = 88;
  145. /* what the decoder will find */
  146. predicted_delta = ((step_table[step_index] * nibble) / 4) + (step_table[step_index] / 8);
  147. if (sign)
  148. c->prev_sample -= predicted_delta;
  149. else
  150. c->prev_sample += predicted_delta;
  151. CLAMP_TO_SHORT(c->prev_sample);
  152. nibble += sign << 3; /* sign * 8 */
  153. /* save back */
  154. c->step_index = step_index;
  155. return nibble;
  156. }
  157. static int adpcm_encode_frame(AVCodecContext *avctx,
  158. unsigned char *frame, int buf_size, void *data)
  159. {
  160. int n;
  161. short *samples;
  162. unsigned char *dst;
  163. ADPCMContext *c = avctx->priv_data;
  164. dst = frame;
  165. samples = (short *)data;
  166. /* n = (BLKSIZE - 4 * avctx->channels) / (2 * 8 * avctx->channels); */
  167. switch(avctx->codec->id) {
  168. case CODEC_ID_ADPCM_IMA_QT: /* XXX: can't test until we get .mov writer */
  169. break;
  170. case CODEC_ID_ADPCM_IMA_WAV:
  171. n = avctx->frame_size / 8;
  172. c->status[0].prev_sample = (signed short)samples[0]; /* XXX */
  173. /* c->status[0].step_index = 0; *//* XXX: not sure how to init the state machine */
  174. *dst++ = (c->status[0].prev_sample) & 0xFF; /* little endian */
  175. *dst++ = (c->status[0].prev_sample >> 8) & 0xFF;
  176. *dst++ = (unsigned char)c->status[0].step_index;
  177. *dst++ = 0; /* unknown */
  178. samples++;
  179. if (avctx->channels == 2) {
  180. c->status[1].prev_sample = (signed short)samples[0];
  181. /* c->status[1].step_index = 0; */
  182. *dst++ = (c->status[1].prev_sample) & 0xFF;
  183. *dst++ = (c->status[1].prev_sample >> 8) & 0xFF;
  184. *dst++ = (unsigned char)c->status[1].step_index;
  185. *dst++ = 0;
  186. samples++;
  187. }
  188. /* stereo: 4 bytes (8 samples) for left, 4 bytes for right, 4 bytes left, ... */
  189. for (; n>0; n--) {
  190. *dst = adpcm_ima_compress_sample(&c->status[0], samples[0]) & 0x0F;
  191. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels]) << 4) & 0xF0;
  192. dst++;
  193. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 2]) & 0x0F;
  194. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 3]) << 4) & 0xF0;
  195. dst++;
  196. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 4]) & 0x0F;
  197. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 5]) << 4) & 0xF0;
  198. dst++;
  199. *dst = adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 6]) & 0x0F;
  200. *dst |= (adpcm_ima_compress_sample(&c->status[0], samples[avctx->channels * 7]) << 4) & 0xF0;
  201. dst++;
  202. /* right channel */
  203. if (avctx->channels == 2) {
  204. *dst = adpcm_ima_compress_sample(&c->status[1], samples[1]);
  205. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[3]) << 4;
  206. dst++;
  207. *dst = adpcm_ima_compress_sample(&c->status[1], samples[5]);
  208. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[7]) << 4;
  209. dst++;
  210. *dst = adpcm_ima_compress_sample(&c->status[1], samples[9]);
  211. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[11]) << 4;
  212. dst++;
  213. *dst = adpcm_ima_compress_sample(&c->status[1], samples[13]);
  214. *dst |= adpcm_ima_compress_sample(&c->status[1], samples[15]) << 4;
  215. dst++;
  216. }
  217. samples += 8 * avctx->channels;
  218. }
  219. break;
  220. default:
  221. return -1;
  222. }
  223. return dst - frame;
  224. }
  225. static int adpcm_decode_init(AVCodecContext * avctx)
  226. {
  227. ADPCMContext *c = avctx->priv_data;
  228. c->channel = 0;
  229. c->status[0].predictor = c->status[1].predictor = 0;
  230. c->status[0].step_index = c->status[1].step_index = 0;
  231. c->status[0].step = c->status[1].step = 0;
  232. switch(avctx->codec->id) {
  233. default:
  234. break;
  235. }
  236. return 0;
  237. }
  238. static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble)
  239. {
  240. int step_index;
  241. int predictor;
  242. int sign, delta, diff, step;
  243. predictor = c->predictor;
  244. step_index = c->step_index + index_table[(unsigned)nibble];
  245. if (step_index < 0) step_index = 0;
  246. if (step_index > 88) step_index = 88;
  247. step = c->step;
  248. /*
  249. diff = ((signed)((nibble & 0x08)?(nibble | 0xF0):(nibble)) + 0.5) * step / 4;
  250. predictor += diff;
  251. */
  252. sign = nibble & 8;
  253. delta = nibble & 7;
  254. diff = step >> 3;
  255. if (delta & 4) diff += step;
  256. if (delta & 2) diff += step >> 1;
  257. if (delta & 1) diff += step >> 2;
  258. if (sign) predictor -= diff;
  259. else predictor += diff;
  260. CLAMP_TO_SHORT(predictor);
  261. c->predictor = predictor;
  262. c->step_index = step_index;
  263. c->step = step_table[step_index];
  264. return (short)predictor;
  265. }
  266. static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble)
  267. {
  268. int predictor;
  269. predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;
  270. predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;
  271. CLAMP_TO_SHORT(predictor);
  272. c->sample2 = c->sample1;
  273. c->sample1 = predictor;
  274. c->idelta = (AdaptationTable[(int)nibble] * c->idelta) / 256;
  275. if (c->idelta < 16) c->idelta = 16;
  276. return (short)predictor;
  277. }
  278. static int adpcm_decode_frame(AVCodecContext *avctx,
  279. void *data, int *data_size,
  280. UINT8 *buf, int buf_size)
  281. {
  282. ADPCMContext *c = avctx->priv_data;
  283. ADPCMChannelStatus *cs;
  284. int n, m, channel;
  285. int block_predictor[2];
  286. short *samples;
  287. UINT8 *src;
  288. int st; /* stereo */
  289. samples = data;
  290. src = buf;
  291. st = avctx->channels == 2;
  292. switch(avctx->codec->id) {
  293. case CODEC_ID_ADPCM_IMA_QT:
  294. n = (buf_size - 2);/* >> 2*avctx->channels;*/
  295. channel = c->channel;
  296. cs = &(c->status[channel]);
  297. /* (pppppp) (piiiiiii) */
  298. /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */
  299. cs->predictor = (*src++) << 8;
  300. cs->predictor |= (*src & 0x80);
  301. cs->predictor &= 0xFF80;
  302. /* sign extension */
  303. if(cs->predictor & 0x8000)
  304. cs->predictor -= 0x10000;
  305. CLAMP_TO_SHORT(cs->predictor);
  306. cs->step_index = (*src++) & 0x7F;
  307. if (cs->step_index > 88) fprintf(stderr, "ERROR: step_index = %i\n", cs->step_index);
  308. if (cs->step_index > 88) cs->step_index = 88;
  309. cs->step = step_table[cs->step_index];
  310. if (st && channel)
  311. samples++;
  312. *samples++ = cs->predictor;
  313. samples += st;
  314. for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */
  315. *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F);
  316. samples += avctx->channels;
  317. *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F);
  318. samples += avctx->channels;
  319. src ++;
  320. }
  321. if(st) { /* handle stereo interlacing */
  322. c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */
  323. if(channel == 0) { /* wait for the other packet before outputing anything */
  324. *data_size = 0;
  325. return src - buf;
  326. }
  327. }
  328. break;
  329. case CODEC_ID_ADPCM_IMA_WAV:
  330. if (buf_size > BLKSIZE) {
  331. if (avctx->block_align != 0)
  332. buf_size = avctx->block_align;
  333. else
  334. buf_size = BLKSIZE;
  335. }
  336. n = buf_size - 4 * avctx->channels;
  337. cs = &(c->status[0]);
  338. cs->predictor = (*src++) & 0x0FF;
  339. cs->predictor |= ((*src++) << 8) & 0x0FF00;
  340. if(cs->predictor & 0x8000)
  341. cs->predictor -= 0x10000;
  342. CLAMP_TO_SHORT(cs->predictor);
  343. *samples++ = cs->predictor;
  344. cs->step_index = *src++;
  345. if (cs->step_index < 0) cs->step_index = 0;
  346. if (cs->step_index > 88) cs->step_index = 88;
  347. if (*src++) fprintf(stderr, "unused byte should be null !!\n"); /* unused */
  348. if (st) {
  349. cs = &(c->status[1]);
  350. cs->predictor = (*src++) & 0x0FF;
  351. cs->predictor |= ((*src++) << 8) & 0x0FF00;
  352. if(cs->predictor & 0x8000)
  353. cs->predictor -= 0x10000;
  354. CLAMP_TO_SHORT(cs->predictor);
  355. *samples++ = cs->predictor;
  356. cs->step_index = *src++;
  357. if (cs->step_index < 0) cs->step_index = 0;
  358. if (cs->step_index > 88) cs->step_index = 88;
  359. src++; /* unused */
  360. }
  361. cs = &(c->status[0]);
  362. for(m=3; n>0; n--, m--) {
  363. *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F);
  364. if (st)
  365. *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F);
  366. *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  367. if (st)
  368. *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F);
  369. src ++;
  370. if (st && !m) {
  371. m=3;
  372. src+=4;
  373. }
  374. }
  375. break;
  376. case CODEC_ID_ADPCM_MS:
  377. if (buf_size > BLKSIZE) {
  378. if (avctx->block_align != 0)
  379. buf_size = avctx->block_align;
  380. else
  381. buf_size = BLKSIZE;
  382. }
  383. n = buf_size - 7 * avctx->channels;
  384. if (n < 0)
  385. return -1;
  386. block_predictor[0] = (*src++); /* should be bound */
  387. block_predictor[0] = (block_predictor[0] < 0)?(0):((block_predictor[0] > 7)?(7):(block_predictor[0]));
  388. block_predictor[1] = 0;
  389. if (st)
  390. block_predictor[1] = (*src++);
  391. block_predictor[1] = (block_predictor[1] < 0)?(0):((block_predictor[1] > 7)?(7):(block_predictor[1]));
  392. c->status[0].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  393. if (c->status[0].idelta & 0x08000)
  394. c->status[0].idelta -= 0x10000;
  395. src+=2;
  396. if (st)
  397. c->status[1].idelta = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  398. if (st && c->status[1].idelta & 0x08000)
  399. c->status[1].idelta |= 0xFFFF0000;
  400. if (st)
  401. src+=2;
  402. c->status[0].coeff1 = AdaptCoeff1[block_predictor[0]];
  403. c->status[0].coeff2 = AdaptCoeff2[block_predictor[0]];
  404. c->status[1].coeff1 = AdaptCoeff1[block_predictor[1]];
  405. c->status[1].coeff2 = AdaptCoeff2[block_predictor[1]];
  406. c->status[0].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  407. src+=2;
  408. if (st) c->status[1].sample1 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  409. if (st) src+=2;
  410. c->status[0].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  411. src+=2;
  412. if (st) c->status[1].sample2 = ((*src & 0xFF) | ((src[1] << 8) & 0xFF00));
  413. if (st) src+=2;
  414. *samples++ = c->status[0].sample1;
  415. if (st) *samples++ = c->status[1].sample1;
  416. *samples++ = c->status[0].sample2;
  417. if (st) *samples++ = c->status[1].sample2;
  418. for(;n>0;n--) {
  419. *samples++ = adpcm_ms_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F);
  420. *samples++ = adpcm_ms_expand_nibble(&c->status[st], src[0] & 0x0F);
  421. src ++;
  422. }
  423. break;
  424. default:
  425. *data_size = 0;
  426. return -1;
  427. }
  428. *data_size = (UINT8 *)samples - (UINT8 *)data;
  429. return src - buf;
  430. }
  431. #define ADPCM_CODEC(id, name) \
  432. AVCodec name ## _encoder = { \
  433. #name, \
  434. CODEC_TYPE_AUDIO, \
  435. id, \
  436. sizeof(ADPCMContext), \
  437. adpcm_encode_init, \
  438. adpcm_encode_frame, \
  439. adpcm_encode_close, \
  440. NULL, \
  441. }; \
  442. AVCodec name ## _decoder = { \
  443. #name, \
  444. CODEC_TYPE_AUDIO, \
  445. id, \
  446. sizeof(ADPCMContext), \
  447. adpcm_decode_init, \
  448. NULL, \
  449. NULL, \
  450. adpcm_decode_frame, \
  451. };
  452. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_QT, adpcm_ima_qt);
  453. ADPCM_CODEC(CODEC_ID_ADPCM_IMA_WAV, adpcm_ima_wav);
  454. ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
  455. #undef ADPCM_CODEC