avcodec_decode_audio2返回值为-1?
来源:互联网 发布:vmware上安装ubuntu 编辑:程序博客网 时间:2024/05/22 14:31
http://blog.csdn.net/cffishappy/article/details/7631424
我用ffmpeg4.0版本代替旧版本3.2时,解码时发现avcodec_decode_audio2返回值总为-1,我程序中代码如下:
int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; //#define AVCODEC_MAX_AUDIO_FRAME_SIZE 2<<20
uint8_t * inbuf = (uint8_t *)malloc(out_size);
FILE* pFileWav;
int fileSize;
pFileWav= fopen("b.pcm","wb+");
while(av_read_frame(pFormatCtx, &packet)>=0)
{
if(packet.stream_index==audioStream) {
pktdata = packet.data;
pktsize = packet.size;
while(pktsize>0)
{
//解码
int len=avcodec_decode_audio2(pCodecCtx,(int16_t *)inbuf,&out_size,pktdata,pktsize);
// int len=avcodec_decode_audio3(pCodecCtx,(int16_t *)inbuf,&out_size,&packet);
if (len<0)
{
printf("Error while decoding.\n");
break;
}
if(out_size>0)
{
fwrite(inbuf,1,out_size,pFileWav);//pcm记录
fflush(pFileWav);
fileSize += out_size;
}
pktsize -= len;
pktdata += len;
}
}
av_free_packet(&packet);
}
后来查了下ffmpeg3.2源代码,发现:
int avcodec_decode_audio(AVCodecContext *avctx, int16_t *samples,
int *frame_size_ptr,
const uint8_t *buf, int buf_size){
*frame_size_ptr= AVCODEC_MAX_AUDIO_FRAME_SIZE;
return avcodec_decode_audio2(avctx, samples, frame_size_ptr, buf, buf_size);
}
int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
int *frame_size_ptr,
const uint8_t *buf, int buf_size)
{
int ret;
if((avctx->codec->capabilities & CODEC_CAP_DELAY) || buf_size){
//FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
return -1;
}
if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
*frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
return -1;
}
ret = avctx->codec->decode(avctx, samples, frame_size_ptr,
buf, buf_size);
avctx->frame_number++;
}else{
ret= 0;
*frame_size_ptr=0;
}
return ret;
}
ffmpeg4.0中代码为:
int attribute_align_arg avcodec_decode_audio2(AVCodecContext *avctx, int16_t *samples,
int *frame_size_ptr,
const uint8_t *buf, int buf_size)
{
AVPacket avpkt;
av_init_packet(&avpkt);
avpkt.data = buf;
avpkt.size = buf_size;
return avcodec_decode_audio3(avctx, samples, frame_size_ptr, &avpkt);
}
#endif
int attribute_align_arg avcodec_decode_audio3(AVCodecContext *avctx, int16_t *samples,
int *frame_size_ptr,
AVPacket *avpkt)
{
int ret;
if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
//FIXME remove the check below _after_ ensuring that all audio check that the available space is enough
if(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE){
av_log(avctx, AV_LOG_ERROR, "buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE\n");
return -1;
}
if(*frame_size_ptr < FF_MIN_BUFFER_SIZE ||
*frame_size_ptr < avctx->channels * avctx->frame_size * sizeof(int16_t)){
av_log(avctx, AV_LOG_ERROR, "buffer %d too small\n", *frame_size_ptr);
return -1;
}
ret = avctx->codec->decode(avctx, samples, frame_size_ptr, avpkt);
avctx->frame_number++;
}else{
ret= 0;
*frame_size_ptr=0;
}
return ret;
}
即每次调用avcodec_decode_audio2时,若(*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE),则返回值为-1,后来我就顺着这个思路,往上找,过不其然,发现了问题之所在,原因是:在intlen=avcodec_decode_audio2(pCodecCtx,(int16_t *)inbuf,&out_size,pktdata,pktsize)处理后,out_size的值变为4608,而不是AVCODEC_MAX_AUDIO_FRAME_SIZE,所以在4.0中,*frame_size_ptr < AVCODEC_MAX_AUDIO_FRAME_SIZE,返回值自然为-1;
找到了问题,解决自然容易,要么在ffmpeg源码处该,要么在客户端,而我选择了后者,主要是方便:
int out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
uint8_t * inbuf = (uint8_t *)malloc(out_size);
FILE* pFileWav;
int fileSize;
pFileWav= fopen("b.pcm","wb+");
while(av_read_frame(pFormatCtx, &packet)>=0)
{
if(packet.stream_index==audioStream) {
pktdata = packet.data;
pktsize = packet.size;
out_size=AVCODEC_MAX_AUDIO_FRAME_SIZE; //加这一句,就OK了
while(pktsize>0)
{
//解码
int len=avcodec_decode_audio2(pCodecCtx,(int16_t *)inbuf,&out_size,pktdata,pktsize);
// int len=avcodec_decode_audio3(pCodecCtx,(int16_t *)inbuf,&out_size,&packet);
if (len<0)
{
printf("Error while decoding.\n");
break;
}
if(out_size>0)
{
fwrite(inbuf,1,out_size,pFileWav);//pcm记录
fflush(pFileWav);
fileSize += out_size;
}
pktsize -= len;
pktdata += len;
}
}
av_free_packet(&packet);
}
- avcodec_decode_audio2返回值为-1?
- avcodec_decode_audio2返回值为-1?
- avcodec_decode_audio2返回值为-1?
- 关于ExecuteNonQuery()返回值为-1
- MFC ADO RecordCount 返回值为-1
- ExecuteNonQuery()返回值为 -1的原因
- 关于scanf()返回值为-1
- findViewById 返回值为空
- 函数返回值为数组
- 返回值为函数指针
- 函数返回值为数组
- C++ 返回值为引用
- C++ 返回值为引用
- 函数返回值为数组
- DWR:返回值为空?
- MyBatis返回值为Map
- 函数返回值为数组
- @RequestMapping返回值为void
- Oracle 高水位线原理解析
- 《项目经验》--通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
- windows服务调用word组件出错的解决方法
- php delimiters 分隔符
- 2013-02-19
- avcodec_decode_audio2返回值为-1?
- 数字图像处理2---图像基础
- net利用office自带的功能将word转成PDF
- 简便添加监听器的方法
- 真正的美剧字幕组翻译高手教你如何学好英语!心得经验之谈啊!
- Hibernate笔记三 主键生成策略总结
- requestLayout()
- 缓存服务器设计与实现(四)
- 好好看看