AVCodec *m_DecodeCodec;AVCodecContext *m_DecodeC; int CMyAudio::InitDecode()
{
m_DecodeCodec = avcodec_find_decoder(CODEC_ID_ADPCM_IMA_WAV); if(!m_DecodeCodec)
{
TRACE("查找解码器失败\n"); return 1;
} m_DecodeC = avcodec_alloc_context(); m_DecodeC->codec_type = CODEC_TYPE_AUDIO;
m_DecodeC->sample_rate = 44100;
m_DecodeC->channels = 2;
m_DecodeC->bit_rate = 64000;
m_DecodeC->sample_fmt = SAMPLE_FMT_S16; if(avcodec_open(m_DecodeC,m_DecodeCodec) < 0)
{
TRACE("打开解码器失败\n"); return 1;
} return 0;
}int CMyAudio::EncodeAudio(char *chSourceData,int nSourceLen,char *chTargetData,int &nTargetLen)
{
int frame_size, out_size, outbuf_size; short *samples; uint8_t *outbuf; frame_size = m_EncodeC->frame_size; samples = (short*)malloc(nSourceLen); outbuf_size = 10000; outbuf = (uint8_t*)malloc(outbuf_size); memcpy(samples,chSourceData,nSourceLen);
while(1)
{
out_size = avcodec_encode_audio(m_EncodeC, outbuf, outbuf_size, samples); if(out_size > 0)
break;
} nTargetLen = out_size; memcpy(chTargetData,outbuf,out_size); free(outbuf); free(samples); return 0;
}aac的音频流 这样 解码成 pcm音频流 行吗
{
m_DecodeCodec = avcodec_find_decoder(CODEC_ID_ADPCM_IMA_WAV); if(!m_DecodeCodec)
{
TRACE("查找解码器失败\n"); return 1;
} m_DecodeC = avcodec_alloc_context(); m_DecodeC->codec_type = CODEC_TYPE_AUDIO;
m_DecodeC->sample_rate = 44100;
m_DecodeC->channels = 2;
m_DecodeC->bit_rate = 64000;
m_DecodeC->sample_fmt = SAMPLE_FMT_S16; if(avcodec_open(m_DecodeC,m_DecodeCodec) < 0)
{
TRACE("打开解码器失败\n"); return 1;
} return 0;
}int CMyAudio::EncodeAudio(char *chSourceData,int nSourceLen,char *chTargetData,int &nTargetLen)
{
int frame_size, out_size, outbuf_size; short *samples; uint8_t *outbuf; frame_size = m_EncodeC->frame_size; samples = (short*)malloc(nSourceLen); outbuf_size = 10000; outbuf = (uint8_t*)malloc(outbuf_size); memcpy(samples,chSourceData,nSourceLen);
while(1)
{
out_size = avcodec_encode_audio(m_EncodeC, outbuf, outbuf_size, samples); if(out_size > 0)
break;
} nTargetLen = out_size; memcpy(chTargetData,outbuf,out_size); free(outbuf); free(samples); return 0;
}aac的音频流 这样 解码成 pcm音频流 行吗
AVCodecContext *m_EncodeC;m_EncodeC = avcodec_alloc_context();m_EncodeC->codec_type = CODEC_TYPE_AUDIO;
m_EncodeC->codec_id = CODEC_ID_AAC;
m_EncodeC->block_align = 0;
m_EncodeC->sample_rate = 44100;
m_EncodeC->channels = 2;
//m_EncodeC->bit_rate = 64000;
m_EncodeC->sample_fmt = SAMPLE_FMT_S16;
m_EncodeC->profile = FF_PROFILE_AAC_MAIN;
//m_EncodeC->bits_per_sample = 8;/*m_EncodeC->time_base.num= 1;
m_EncodeC->time_base.den= 11025;*/
解码:
AVCodecContext *m_DecodeC;m_DecodeC = avcodec_alloc_context();m_DecodeC->codec_type = CODEC_TYPE_AUDIO;
m_DecodeC->sample_rate = /*11025*/44100;
m_DecodeC->channels = 2;
//m_DecodeC->bit_rate = 64000;
//m_DecodeC->sample_fmt = SAMPLE_FMT_S16;
m_DecodeC->bits_per_sample = 8;
//m_DecodeC->profile = FF_PROFILE_AAC_MAIN;
现在听起来就是“吱吱”的杂音,应该是哪个参数没有设或者设错了。有谁知道吗
m_EncodeCodec = avcodec_find_encoder(CODEC_ID_AAC);m_EncodeC = avcodec_alloc_context(); m_EncodeC->codec_type = CODEC_TYPE_AUDIO;
m_EncodeC->codec_id = CODEC_ID_AAC;
//m_EncodeC->block_align = 0;
m_EncodeC->sample_rate = 44100;
m_EncodeC->channels = 2;
m_EncodeC->bit_rate = 64000;
m_EncodeC->sample_fmt = SAMPLE_FMT_U8;
m_EncodeC->profile = FF_PROFILE_AAC_MAIN;
解码:
m_DecodeCodec = avcodec_find_decoder(CODEC_ID_PCM_U8);m_DecodeC = avcodec_alloc_context(); m_DecodeC->codec_type = CODEC_TYPE_AUDIO;
m_DecodeC->codec_id = CODEC_ID_PCM_U8;
m_DecodeC->sample_rate = 11025;
m_DecodeC->channels = 2;
m_DecodeC->bits_per_sample = 8;采集音频的 WAVEFORMATEX m_wfxInput;
m_wfxInput.wFormatTag = WAVE_FORMAT_PCM;
m_wfxInput.nSamplesPerSec = 11025;
m_wfxInput.wBitsPerSample = 8;
m_wfxInput.nChannels = 2;
m_wfxInput.nBlockAlign = m_wfxInput.nChannels*m_wfxInput.wBitsPerSample/8; //2
m_wfxInput.nAvgBytesPerSec = m_wfxInput.nBlockAlign*m_wfxInput.nSamplesPerSec; //22050
现在编码压缩的比例是10多:1 解码出来的却是2:1
你还可以用faad,一个纯粹的AAC decode
我就是根据这个写的,现在编码后,压缩比达到1:11 声音效果也很好。 可是,解码方面,压缩比却是1:4。肯定是错的,改了许多参数,都这样,还有的出错。m_DecodeCodec = avcodec_find_decoder(/*CODEC_ID_PCM_S8*/CODEC_ID_ADPCM_IMA_WAV); if(!m_DecodeCodec)
{
TRACE("查找解码器失败\n"); return 1;
} m_DecodeC = avcodec_alloc_context(); m_DecodeC->codec_type = CODEC_TYPE_AUDIO;
m_DecodeC->codec_id = CODEC_ID_ADPCM_IMA_WAV;
m_DecodeC->sample_rate = 11025;
m_DecodeC->channels = 2;
m_DecodeC->bits_per_sample = 16;
if(avcodec_open(m_DecodeC,m_DecodeCodec) < 0)
{
TRACE("打开解码器失败\n"); return 1;
}采集的参数是
m_wfxInput.wFormatTag = WAVE_FORMAT_PCM;
m_wfxInput.nSamplesPerSec = 11025;
m_wfxInput.wBitsPerSample = 16;
m_wfxInput.nChannels = 2;
m_wfxInput.nBlockAlign = m_wfxInput.nChannels*m_wfxInput.wBitsPerSample/8; //2
m_wfxInput.nAvgBytesPerSec = m_wfxInput.nBlockAlign*m_wfxInput.nSamplesPerSec; //22050
你那有FAC的库和头文件吗 如果有,想问你讨一个 呵呵
我刚试了一下,把AAC编解码改成MP3,结果也是一样的。编码可以,但是解码还是出问题。。