当我试图将IMA ADPCM解码为16位有符号PCM时,我得到了半垃圾PCM
我尝试在WAV文件(22050采样/秒,4位/采样,36(!)byte(?)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条答案
按热度按时间pkwftd7m1#
这是因为在WAV中,ADPCM不存储为编码声音的连续流。相反,它被存储为块,其中块的每个前4字节是初始ADPCM解码器状态。
例如:WAV.align=36;//块大小为36字节,块结构为--|当前(2字节)|步骤索引(2字节)|数据(WAV.align-4(36-4=32)字节)|--