C中的ADPCM解码

时间:2019-03-31 15:46:50

标签: c wav adpcm

当我尝试将IMA ADPCM解码为16位带符号的PCM时,我的PCM一半装满了

我尝试对WAV文件中的IMA ADPCM进行解码和编码(22050个样本/秒,4位/样本,36(!)个字节(?)align(?!),1个通道)。

int16_t decodeImaAdpcmSampleUIS(struct AdpcmState* state, const uint8_t sample){
int diff;
int8_t si=state->stepindex;
int step=i_step_table[si];
int cur=state->current;

diff=step>>3;
if( sample&0x04 ) diff += step;
if( sample&0x02 ) diff += step>>1;
if( sample&0x01 ) diff += step>>2;
if( sample&0x08 ){
cur -= diff;
if(cur<-32768)
    cur=-32768;
}else{
cur += diff;
if(cur>32767)
    cur=32767;
}
//predictor: cur, state.current
//step_index: si, stepindex
si+=ima_index_table[sample&0b111];
if(si < 0)
    si = 0;
if(si > 88)
    si = 88;
state->stepindex=si;

return state->current=cur;
}

void decodeImaAdpcm(uint8_t* src, int16_t* dst, size_t srcLen){
    struct AdpcmState state={0,0};

    for(size_t i=0; i<srcLen; i++){
        *(dst++)=decodeImaAdpcmSampleUIS(&state, (*src)&0xf);
        *(dst++)=decodeImaAdpcmSampleUIS(&state, (*src)>>4);
        src++;
    }
}

带有WAV文件的完整项目: https://drive.google.com/open?id=1xuxwXj3Y_QhPDWhrQY7nmz8ycBE1cgyL

转换为PCM WAV的ADPCM WAV文件包含一些垃圾,但是ffmpeg正常将其转换。目前尚未实现PCM WAV到ADPCM WAV的转换。

1 个答案:

答案 0 :(得分:0)

发生这种情况是因为WAV 包含 PLAIN IMA ADPCM。块的每个前4个字节均为ADPCM状态。

例如:WAV.align = 36; //块大小为36个字节,块结构为-|当前(16位)|步长索引(16个字节)| data(WAV.align-4(36-4 = 32)个字节)|-