int read_buffer(void* opaque, uint8_t* buf, int buf_size)
{
buf=NULL;
buf_size=0;
memcpy(buf, buffer_aac_la, nRecEcho);
return nRecEcho;
}
unsigned char* aviobuffer = (unsigned char*)av_malloc(nRecEcho);
AVIOContext* avio = avio_alloc_context(aviobuffer, nRecEcho, 0, NULL, read_buffer, NULL, NULL);
avio_alloc_context(aviobuffer, nRecEcho, 0, NULL, read_buffer, NULL, NULL);
原型:AVIOContext *avio_alloc_context(
unsigned char *buffer,
int buffer_size,
int write_flag,
void *opaque,
int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
int64_t (*seek)(void *opaque, int64_t offset, int whence))
unsigned char *iobuffer = NULL;
iobuffer = (unsigned char *)av_malloc(40000);
if (iobuffer == NULL)
{
printf("iobuffer av_malloc failed.\n");
return -1;
}
AVIOContext *avio = avio_alloc_context(iobuffer, 40000, 0, this, fill_iobuffer, NULL, NULL);
if (avio == NULL)
{
printf(" avio_alloc_context failed.\n");
return -1;
}
/*.........*/
if (avformat_open_input(&pFormatCtx, NULL, NULL, NULL) < 0) //打开输入文件
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
while (av_read_frame(pFormatCtx, pkt) >= 0)
{
time++;
if (pkt->stream_index == index_audio)
{
if (avcodec_decode_audio4(pCodecCtx, pFrame, &get_frame, pkt) < 0)
{
printf("Decode Error!\n");
return -1;
}
if (get_frame)
{
//printf("Decoded frame index: %d\n", frame_cnt); //一个循环解码了多少帧
swr_convert(aud_convert_ctx, &out_buffer, 2 * 22050, (const uint8_t**)pFrame->data, pFrame->nb_samples);//1024
//printf("pFrame->nb_samples=%d\n",pFrame->nb_samples);1024
memcpy(buffer_pcm_s16, out_buffer, 2048);
fwrite(out_buffer, 1, 2048, file_s16);
//frame_cnt++;
}
printf("解码成功:%d\n", time);
}
av_free_packet(pkt);
}
swr_convert(aud_convert_ctx, &out_buffer, 2 * 22050, (const uint8_t**)pFrame->data, pFrame->nb_samples);
// 当 inLen < m_avframe->nb_samples时,会产生噪音噪音
ffmpeg代码:swr_convert 音频重采样产生噪音的原因记录_yangops的博客-CSDN博客
int ret = swr_convert(m_swr_ctx,
m_avframe->extended_data, // out:重采样数据存储缓存区
m_avframe->nb_samples, // out:重采样样本数
(const uint8_t**)m_pcmPointer, // in:原始音频数据缓冲区
inLen); // in:原始音频样本数
swr_alloc_set_opts(swrContext,codecCtx->channel_layout, AV_SAMPLE_FMT_S16, codecCtx->sample_rate,codecCtx->channels, codecCtx->sample_fmt, codecCtx->sample_rate, 0, 0);
int swr_convert(struct SwrContext *s,
uint8_t **out,
int out_count,
const uint8_t **in ,
int in_count);
第一个参数是SwrContext 结构体,第二个参数是输出数据保存的地方,第三个参数是out的大小,第四个参数是输入数据就是要待转换的数据,第五个参数是输入数据采样的个数。
FFmpeg(11)-基于FFmpeg进行音频重采样(swr_init(), swr_convert())_weixin_34216107的博客-CSDN博客
针对每一帧音频的处理。把一帧帧的音频作相应的重采样
int swr_convert(struct SwrContext *s, uint8_t **out, int out_count, const uint8_t **in, int in_count);
参数1:音频重采样的上下文
参数2:输出的指针。传递的输出的数组
参数3:输出的样本数量,不是字节数。单通道的样本数量。
参数4:输入的数组,AVFrame解码出来的DATA
参数5:输入的单通道的样本数量。