⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adpcm.c

📁 tcpmp播放器的flv插件
💻 C
📖 第 1 页 / 共 3 页
字号:
            c->status[i].coeff2 = AdaptCoeff2[predictor];        }        for(i=0; i<avctx->channels; i++){            if (c->status[i].idelta < 16)                 c->status[i].idelta = 16;                        *dst++ = c->status[i].idelta & 0xFF;            *dst++ = c->status[i].idelta >> 8;        }        for(i=0; i<avctx->channels; i++){            c->status[i].sample1= *samples++;            *dst++ = c->status[i].sample1 & 0xFF;            *dst++ = c->status[i].sample1 >> 8;        }        for(i=0; i<avctx->channels; i++){            c->status[i].sample2= *samples++;            *dst++ = c->status[i].sample2 & 0xFF;            *dst++ = c->status[i].sample2 >> 8;        }        for(i=7*avctx->channels; i<avctx->block_align; i++) {            int nibble;            nibble = adpcm_ms_compress_sample(&c->status[ 0], *samples++)<<4;            nibble|= adpcm_ms_compress_sample(&c->status[st], *samples++);            *dst++ = nibble;        }        break;    case CODEC_ID_ADPCM_YAMAHA:        n = avctx->frame_size / 2;        for (; n>0; n--) {            for(i = 0; i < avctx->channels; i++) {                int nibble;                nibble  = adpcm_yamaha_compress_sample(&c->status[i], samples[i]);                nibble |= adpcm_yamaha_compress_sample(&c->status[i], samples[i+avctx->channels]) << 4;                *dst++ = nibble;            }            samples += 2 * avctx->channels;        }        break;    default:        return -1;    }    return dst - frame;}#endif //CONFIG_ENCODERSstatic int adpcm_decode_init(AVCodecContext * avctx){    ADPCMContext *c = avctx->priv_data;    c->channel = 0;    c->status[0].predictor = c->status[1].predictor = 0;    c->status[0].step_index = c->status[1].step_index = 0;    c->status[0].step = c->status[1].step = 0;    switch(avctx->codec->id) {    case CODEC_ID_ADPCM_CT:	c->status[0].step = c->status[1].step = 511;	break;    default:        break;    }    return 0;}static inline short adpcm_ima_expand_nibble(ADPCMChannelStatus *c, char nibble, int shift){    int step_index;    int predictor;    int sign, delta, diff, step;    step = step_table[c->step_index];    step_index = c->step_index + index_table[(unsigned)nibble];    if (step_index < 0) step_index = 0;    else if (step_index > 88) step_index = 88;    sign = nibble & 8;    delta = nibble & 7;    /* perform direct multiplication instead of series of jumps proposed by     * the reference ADPCM implementation since modern CPUs can do the mults     * quickly enough */    diff = ((2 * delta + 1) * step) >> shift;    predictor = c->predictor;    if (sign) predictor -= diff;    else predictor += diff;    CLAMP_TO_SHORT(predictor);    c->predictor = predictor;    c->step_index = step_index;    return (short)predictor;}static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble){    int predictor;    predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 256;    predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;    CLAMP_TO_SHORT(predictor);    c->sample2 = c->sample1;    c->sample1 = predictor;    c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;    if (c->idelta < 16) c->idelta = 16;    return (short)predictor;}static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble){    int predictor;    int sign, delta, diff;    int new_step;    sign = nibble & 8;    delta = nibble & 7;    /* perform direct multiplication instead of series of jumps proposed by     * the reference ADPCM implementation since modern CPUs can do the mults     * quickly enough */    diff = ((2 * delta + 1) * c->step) >> 3;    predictor = c->predictor;    /* predictor update is not so trivial: predictor is multiplied on 254/256 before updating */    if(sign)	predictor = ((predictor * 254) >> 8) - diff;    else    	predictor = ((predictor * 254) >> 8) + diff;    /* calculate new step and clamp it to range 511..32767 */    new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;    c->step = new_step;    if(c->step < 511)	c->step = 511;    if(c->step > 32767)	c->step = 32767;    CLAMP_TO_SHORT(predictor);    c->predictor = predictor;    return (short)predictor;}static inline short adpcm_yamaha_expand_nibble(ADPCMChannelStatus *c, unsigned char nibble){    if(!c->step) {        c->predictor = 0;        c->step = 127;    }    c->predictor += (c->step * yamaha_difflookup[nibble]) / 8;    CLAMP_TO_SHORT(c->predictor);    c->step = (c->step * yamaha_indexscale[nibble]) >> 8;    c->step = clip(c->step, 127, 24567);    return c->predictor;}static void xa_decode(short *out, const unsigned char *in,     ADPCMChannelStatus *left, ADPCMChannelStatus *right, int inc){    int i, j;    int shift,filter,f0,f1;    int s_1,s_2;    int d,s,t;    for(i=0;i<4;i++) {        shift  = 12 - (in[4+i*2] & 15);        filter = in[4+i*2] >> 4;        f0 = xa_adpcm_table[filter][0];        f1 = xa_adpcm_table[filter][1];        s_1 = left->sample1;        s_2 = left->sample2;        for(j=0;j<28;j++) {            d = in[16+i+j*4];            t = (signed char)(d<<4)>>4;            s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);            CLAMP_TO_SHORT(s);            *out = s;            out += inc;            s_2 = s_1;            s_1 = s;        }        if (inc==2) { /* stereo */            left->sample1 = s_1;            left->sample2 = s_2;            s_1 = right->sample1;            s_2 = right->sample2;            out = out + 1 - 28*2;        }        shift  = 12 - (in[5+i*2] & 15);        filter = in[5+i*2] >> 4;        f0 = xa_adpcm_table[filter][0];        f1 = xa_adpcm_table[filter][1];        for(j=0;j<28;j++) {            d = in[16+i+j*4];            t = (signed char)d >> 4;            s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);            CLAMP_TO_SHORT(s);            *out = s;            out += inc;            s_2 = s_1;            s_1 = s;        }        if (inc==2) { /* stereo */            right->sample1 = s_1;            right->sample2 = s_2;            out -= 1;        } else {            left->sample1 = s_1;            left->sample2 = s_2;        }    }}/* DK3 ADPCM support macro */#define DK3_GET_NEXT_NIBBLE() \    if (decode_top_nibble_next) \    { \        nibble = (last_byte >> 4) & 0x0F; \        decode_top_nibble_next = 0; \    } \    else \    { \        last_byte = *src++; \        if (src >= buf + buf_size) break; \        nibble = last_byte & 0x0F; \        decode_top_nibble_next = 1; \    }static int adpcm_decode_frame(AVCodecContext *avctx,			    void *data, int *data_size,			    uint8_t *buf, int buf_size){    ADPCMContext *c = avctx->priv_data;    ADPCMChannelStatus *cs;    int n, m, channel, i;    int block_predictor[2];    short *samples;    uint8_t *src;    int st; /* stereo */    /* DK3 ADPCM accounting variables */    unsigned char last_byte = 0;    unsigned char nibble;    int decode_top_nibble_next = 0;    int diff_channel;    /* EA ADPCM state variables */    uint32_t samples_in_chunk;    int32_t previous_left_sample, previous_right_sample;    int32_t current_left_sample, current_right_sample;    int32_t next_left_sample, next_right_sample;    int32_t coeff1l, coeff2l, coeff1r, coeff2r;    uint8_t shift_left, shift_right;    int count1, count2;    if (!buf_size)        return 0;    samples = data;    src = buf;    st = avctx->channels == 2;    switch(avctx->codec->id) {    case CODEC_ID_ADPCM_IMA_QT:        n = (buf_size - 2);/* >> 2*avctx->channels;*/        channel = c->channel;        cs = &(c->status[channel]);        /* (pppppp) (piiiiiii) */        /* Bits 15-7 are the _top_ 9 bits of the 16-bit initial predictor value */        cs->predictor = (*src++) << 8;        cs->predictor |= (*src & 0x80);        cs->predictor &= 0xFF80;        /* sign extension */        if(cs->predictor & 0x8000)            cs->predictor -= 0x10000;        CLAMP_TO_SHORT(cs->predictor);        cs->step_index = (*src++) & 0x7F;        if (cs->step_index > 88) av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);        if (cs->step_index > 88) cs->step_index = 88;        cs->step = step_table[cs->step_index];        if (st && channel)            samples++;        for(m=32; n>0 && m>0; n--, m--) { /* in QuickTime, IMA is encoded by chuncks of 34 bytes (=64 samples) */            *samples = adpcm_ima_expand_nibble(cs, src[0] & 0x0F, 3);            samples += avctx->channels;            *samples = adpcm_ima_expand_nibble(cs, (src[0] >> 4) & 0x0F, 3);            samples += avctx->channels;            src ++;        }        if(st) { /* handle stereo interlacing */            c->channel = (channel + 1) % 2; /* we get one packet for left, then one for right data */            if(channel == 1) { /* wait for the other packet before outputing anything */                return src - buf;            }        }        break;    case CODEC_ID_ADPCM_IMA_WAV:        if (avctx->block_align != 0 && buf_size > avctx->block_align)            buf_size = avctx->block_align;        for(i=0; i<avctx->channels; i++){            cs = &(c->status[i]);            cs->predictor = *src++;            cs->predictor |= (*src++) << 8;            if(cs->predictor & 0x8000)                cs->predictor -= 0x10000;            CLAMP_TO_SHORT(cs->predictor);	// XXX: is this correct ??: *samples++ = cs->predictor;            cs->step_index = *src++;            if (cs->step_index < 0) cs->step_index = 0;            if (cs->step_index > 88) cs->step_index = 88;            if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null !!\n"); /* unused */        }        for(m=4; src < (buf + buf_size);) {	    *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[0] & 0x0F, 3);            if (st)                *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[4] & 0x0F, 3);            *samples++ = adpcm_ima_expand_nibble(&c->status[0], (src[0] >> 4) & 0x0F, 3);	    if (st) {                *samples++ = adpcm_ima_expand_nibble(&c->status[1], (src[4] >> 4) & 0x0F, 3);		if (!--m) {		    m=4;		    src+=4;		}	    }	    src++;	}        break;    case CODEC_ID_ADPCM_4XM:        cs = &(c->status[0]);        c->status[0].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;        if(st){            c->status[1].predictor= (int16_t)(src[0] + (src[1]<<8)); src+=2;        }        c->status[0].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;        if(st){            c->status[1].step_index= (int16_t)(src[0] + (src[1]<<8)); src+=2;        }        if (cs->step_index < 0) cs->step_index = 0;        if (cs->step_index > 88) cs->step_index = 88;        m= (buf_size - (src - buf))>>st;        for(i=0; i<m; i++) {	    *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] & 0x0F, 4);            if (st)                *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] & 0x0F, 4);            *samples++ = adpcm_ima_expand_nibble(&c->status[0], src[i] >> 4, 4);	    if (st)                *samples++ = adpcm_ima_expand_nibble(&c->status[1], src[i+m] >> 4, 4);	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -