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

📄 adpcm.c

📁 ffmpeg移植到symbian的全部源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
                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 av_cold int adpcm_decode_init(AVCodecContext * avctx){    ADPCMContext *c = avctx->priv_data;    unsigned int max_channels = 2;    switch(avctx->codec->id) {    case CODEC_ID_ADPCM_EA_R1:    case CODEC_ID_ADPCM_EA_R2:    case CODEC_ID_ADPCM_EA_R3:        max_channels = 6;        break;    }    if(avctx->channels > max_channels){        return -1;    }    switch(avctx->codec->id) {    case CODEC_ID_ADPCM_CT:        c->status[0].step = c->status[1].step = 511;        break;    case CODEC_ID_ADPCM_IMA_WS:        if (avctx->extradata && avctx->extradata_size == 2 * 4) {            c->status[0].predictor = AV_RL32(avctx->extradata);            c->status[1].predictor = AV_RL32(avctx->extradata + 4);        }        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;    c->predictor = av_clip_int16(predictor);    c->step_index = step_index;    return (short)c->predictor;}static inline short adpcm_ms_expand_nibble(ADPCMChannelStatus *c, char nibble){    int predictor;    predictor = (((c->sample1) * (c->coeff1)) + ((c->sample2) * (c->coeff2))) / 64;    predictor += (signed)((nibble & 0x08)?(nibble - 0x10):(nibble)) * c->idelta;    c->sample2 = c->sample1;    c->sample1 = av_clip_int16(predictor);    c->idelta = (AdaptationTable[(int)nibble] * c->idelta) >> 8;    if (c->idelta < 16) c->idelta = 16;    return c->sample1;}static inline short adpcm_ct_expand_nibble(ADPCMChannelStatus *c, char nibble){    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 update is not so trivial: predictor is multiplied on 254/256 before updating */    c->predictor = ((c->predictor * 254) >> 8) + (sign ? -diff : diff);    c->predictor = av_clip_int16(c->predictor);    /* calculate new step and clamp it to range 511..32767 */    new_step = (ct_adpcm_table[nibble & 7] * c->step) >> 8;    c->step = av_clip(new_step, 511, 32767);    return (short)c->predictor;}static inline short adpcm_sbpro_expand_nibble(ADPCMChannelStatus *c, char nibble, int size, int shift){    int sign, delta, diff;    sign = nibble & (1<<(size-1));    delta = nibble & ((1<<(size-1))-1);    diff = delta << (7 + c->step + shift);    /* clamp result */    c->predictor = av_clip(c->predictor + (sign ? -diff : diff), -16384,16256);    /* calculate new step */    if (delta >= (2*size - 3) && c->step < 3)        c->step++;    else if (delta == 0 && c->step > 0)        c->step--;    return (short) c->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;    c->predictor = av_clip_int16(c->predictor);    c->step = (c->step * yamaha_indexscale[nibble]) >> 8;    c->step = av_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);            s_2 = s_1;            s_1 = av_clip_int16(s);            *out = s_1;            out += inc;        }        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);            s_2 = s_1;            s_1 = av_clip_int16(s);            *out = s_1;            out += inc;        }        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; \        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,                            const uint8_t *buf, int buf_size){    ADPCMContext *c = avctx->priv_data;    ADPCMChannelStatus *cs;    int n, m, channel, i;    int block_predictor[2];    short *samples;    short *samples_end;    const 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;    int coeff[2][2], shift[2];//used in EA MAXIS ADPCM    if (!buf_size)        return 0;    //should protect all 4bit ADPCM variants    //8 is needed for CODEC_ID_ADPCM_IMA_WAV with 2 channels    //    if(*data_size/4 < buf_size + 8)        return -1;    samples = data;    samples_end= samples + *data_size/2;    *data_size= 0;    src = buf;    st = avctx->channels == 2 ? 1 : 0;    switch(avctx->codec->id) {    case CODEC_ID_ADPCM_IMA_QT:        n = buf_size - 2*avctx->channels;        for (channel = 0; channel < avctx->channels; 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;            cs->predictor = av_clip_int16(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);                cs->step_index = 88;            }            cs->step = step_table[cs->step_index];            samples = (short*)data + channel;            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  , 3);                samples += avctx->channels;                src ++;            }        }        if (st)            samples--;        break;    case CODEC_ID_ADPCM_IMA_WAV:        if (avctx->block_align != 0 && buf_size > avctx->block_align)            buf_size = avctx->block_align;//        samples_per_block= (block_align-4*chanels)*8 / (bits_per_sample * chanels) + 1;        for(i=0; i<avctx->channels; i++){            cs = &(c->status[i]);            cs->predictor = *samples++ = (int16_t)bytestream_get_le16(&src);            cs->step_index = *src++;            if (cs->step_index > 88){                av_log(avctx, AV_LOG_ERROR, "ERROR: step_index = %i\n", cs->step_index);                cs->step_index = 88;            }            if (*src++) av_log(avctx, AV_LOG_ERROR, "unused byte should be null but is %d!!\n", src[-1]); /* unused */        }        while(src < buf + buf_size){            for(m=0; m<4; m++){                for(i=0; i<=st; i++)                    *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] & 0x0F, 3);                for(i=0; i<=st; i++)                    *samples++ = adpcm_ima_expand_nibble(&c->status[i], src[4*i] >> 4  , 3);                src++;            }            src += 4*st;        }        break;    case CODEC_ID_ADPCM_4XM:        cs = &(c->status[0]);        c->status[0].predictor= (int16_t)bytestream_get_le16(&src);        if(st){            c->status[1].predictor= (int16_t)bytestream_get_le16(&src);        }        c->status[0].step_index= (int16_t)bytestream_get_le16(&src);        if(st){            c->status[1].step_index= (int16_t)bytestream_get_le16(&src);        }        if (cs->step_index < 0) cs->step_index = 0;

⌨️ 快捷键说明

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