//示例为adts的aac流数据(adts数据可以每一包都可以独立解析不需要拼凑)
//解码数据的采样率同解码前的采样率,如果不满足需求,需要对数据进行重采样
#include
int m_fd = -1;
int m_fd2 = -1;
void aac2pcm(){
HANDLE_AACDECODER decoder = aacDecoder_Open(TT_MP4_ADTS , 1);
if (!decoder) {
printf("Failed to open AAC decoder");
return;
}
m_fd = fopen("./send.aac", "rb");
m_fd2 = fopen("./recv.pcm", "wb");
unsigned int size=1024;
unsigned int valid;
unsigned char* data=(unsigned char*)malloc(size);
unsigned int decsize=8* 2048 * sizeof(INT_PCM);
unsigned char* decdata=(unsigned char*)malloc(decsize);
int frameCnt=0;
do{
int len = fread(data, 1,size, m_fd);
valid=len;
AAC_DECODER_ERROR err=aacDecoder_Fill(decoder, &data, &size, &valid);//往aac内部缓存中填数据
if (err != AAC_DEC_OK) {
printf("Failed to fill AAC decoder: %d\n", err);
break;
}
while(1){ //一定要加循环,一次有可能解码不完导致数据丢失
err = aacDecoder_DecodeFrame(decoder, (INT_PCM *)decdata,decsize / sizeof(INT_PCM), 0);
if(err != AAC_DEC_OK){
printf("Failed to DecodeFrame: %d\n", err);
break;
}
//获取解码后码流的信息
CStreamInfo *info = aacDecoder_GetStreamInfo(decoder);
printf("--------------------frameSize:%d ",info->frameSize );//输出解码数据大小
printf("-------------------- sampleRate:%d ", info->sampleRate);//输出解码后采样率
printf("-------------------- numChannels:%d ", info->numChannels);//通道数
printf("-------------------- aacSampleRate:%d ", info->aacSampleRate);//输入aac数据采样率
//int flen = convert_16khz_to_8khz(tmp,decdata,info->frameSize*2); 重采样需要可以到其他链接中获取
fwrite(decdata,info->frameSize*2,1,m_fd2);
frameCnt++;
}
}while(1);
aacDecoder_Close(decoder);
if(data)
free(data);
if(decdata)
free(decdata);
if(m_fd)
fclose(m_fd);
if(m_fd2)
fclose(m_fd2);
}
}