Revision b3bfb299 libavcodec/adpcm.c

View differences:

libavcodec/adpcm.c
103 103
    3, 4, 7, 8, 10, 11, 0, -1, -3, -4
104 104
};
105 105

  
106
static int ct_adpcm_table[8] = {
107
    0x00E6, 0x00E6, 0x00E6, 0x00E6,
108
    0x0133, 0x0199, 0x0200, 0x0266
109
};
110

  
106 111
/* end of tables */
107 112

  
108 113
typedef struct ADPCMChannelStatus {
......
361 366
    c->status[0].step = c->status[1].step = 0;
362 367

  
363 368
    switch(avctx->codec->id) {
369
    case CODEC_ID_ADPCM_CT:
370
	c->status[0].step = c->status[1].step = 511;
371
	break;
364 372
    default:
365 373
        break;
366 374
    }
......
411 419
    return (short)predictor;
412 420
}
413 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;
450
    return (short)predictor;
451
}
452

  
414 453
static void xa_decode(short *out, const unsigned char *in, 
415 454
    ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc)
416 455
{
......
840 879
            src++;
841 880
        }
842 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 898
    default:
844 899
        return -1;
845 900
    }
......
895 950
ADPCM_CODEC(CODEC_ID_ADPCM_XA, adpcm_xa);
896 951
ADPCM_CODEC(CODEC_ID_ADPCM_ADX, adpcm_adx);
897 952
ADPCM_CODEC(CODEC_ID_ADPCM_EA, adpcm_ea);
953
ADPCM_CODEC(CODEC_ID_ADPCM_CT, adpcm_ct);
898 954

  
899 955
#undef ADPCM_CODEC

Also available in: Unified diff