comparison adpcm.c @ 2296:2b75dff01118 libavcodec

Creative ADPCM decoder, format 0x200, courtesy of Konstantin Shishkov
author melanson
date Tue, 12 Oct 2004 12:47:49 +0000
parents 141a9539e270
children 9f17dd9b80c6
comparison
equal deleted inserted replaced
2295:c8f35aaeb857 2296:2b75dff01118
99 }; 99 };
100 100
101 static int ea_adpcm_table[] = { 101 static int ea_adpcm_table[] = {
102 0, 240, 460, 392, 0, 0, -208, -220, 0, 1, 102 0, 240, 460, 392, 0, 0, -208, -220, 0, 1,
103 3, 4, 7, 8, 10, 11, 0, -1, -3, -4 103 3, 4, 7, 8, 10, 11, 0, -1, -3, -4
104 };
105
106 static int ct_adpcm_table[8] = {
107 0x00E6, 0x00E6, 0x00E6, 0x00E6,
108 0x0133, 0x0199, 0x0200, 0x0266
104 }; 109 };
105 110
106 /* end of tables */ 111 /* end of tables */
107 112
108 typedef struct ADPCMChannelStatus { 113 typedef struct ADPCMChannelStatus {
359 c->status[0].predictor = c->status[1].predictor = 0; 364 c->status[0].predictor = c->status[1].predictor = 0;
360 c->status[0].step_index = c->status[1].step_index = 0; 365 c->status[0].step_index = c->status[1].step_index = 0;
361 c->status[0].step = c->status[1].step = 0; 366 c->status[0].step = c->status[1].step = 0;
362 367
363 switch(avctx->codec->id) { 368 switch(avctx->codec->id) {
369 case CODEC_ID_ADPCM_CT:
370 c->status[0].step = c->status[1].step = 511;
371 break;
364 default: 372 default:
365 break; 373 break;
366 } 374 }
367 return 0; 375 return 0;
368 } 376 }
406 c->sample2 = c->sample1; 414 c->sample2 = c->sample1;
407 c->sample1 = predictor; 415 c->sample1 = predictor;
408 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8; 416 c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;
409 if (c->idelta < 16) c->idelta = 16; 417 if (c->idelta < 16) c->idelta = 16;
410 418
419 return (short)predictor;
420 }
421
422 static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble)
423 {
424 int predictor;
425 int sign, delta, diff;
426 int new_step;
427
428 sign = nibble & 8;
429 delta = nibble & 7;
430 /* perform direct multiplication instead of series of jumps proposed by
431 * the reference ADPCM implementation since modern CPUs can do the mults
432 * quickly enough */
433 diff = ((2 * delta + 1) * c->step) >> 3;
434 predictor = c->predictor;
435 /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */
436 if(sign)
437 predictor = ((predictor * 254) >> 8) - diff;
438 else
439 predictor = ((predictor * 254) >> 8) + diff;
440 /* calculate new step and clamp it to range 511..32767 */
441 new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;
442 c->step = new_step;
443 if(c->step < 511)
444 c->step = 511;
445 if(c->step > 32767)
446 c->step = 32767;
447
448 CLAMP_TO_SHORT(predictor);
449 c->predictor = predictor;
411 return (short)predictor; 450 return (short)predictor;
412 } 451 }
413 452
414 static void xa_decode(short *out, const unsigned char *in, 453 static void xa_decode(short *out, const unsigned char *in,
415 ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc) 454 ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
838 *samples++ = adpcm_ima_expand_nibble(&c->status[0], 877 *samples++ = adpcm_ima_expand_nibble(&c->status[0],
839 (*src >> 4) & 0x0F, 3); 878 (*src >> 4) & 0x0F, 3);
840 src++; 879 src++;
841 } 880 }
842 break; 881 break;
882 case CODEC_ID_ADPCM_CT:
883 while (src < buf + buf_size) {
884 if (st) {
885 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
886 (src[0] >> 4) & 0x0F);
887 *samples++ = adpcm_ct_expand_nibble(&c->status[1],
888 src[0] & 0x0F);
889 } else {
890 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
891 (src[0] >> 4) & 0x0F);
892 *samples++ = adpcm_ct_expand_nibble(&c->status[0],
893 src[0] & 0x0F);
894 }
895 src++;
896 }
897 break;
843 default: 898 default:
844 return -1; 899 return -1;
845 } 900 }
846 *data_size = (uint8_t *)samples - (uint8_t *)data; 901 *data_size = (uint8_t *)samples - (uint8_t *)data;
847 return src - buf; 902 return src - buf;
893 ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms); 948 ADPCM_CODEC(CODEC_ID_ADPCM_MS, adpcm_ms);
894 ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm); 949 ADPCM_CODEC(CODEC_ID_ADPCM_4XM, adpcm_4xm);
895 ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa); 950 ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
896 ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx); 951 ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx);
897 ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea); 952 ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
953 ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
898 954
899 #undef ADPCM_CODEC 955 #undef ADPCM_CODEC