avcodec_decode_audio2返回值为-1?

来源:互联网 发布:java web接口开发demo 编辑:程序博客网 时间:2024/05/17 01:00

我用ffmpeg4.0版本代替旧版本3.2时,解码时发现avcodec_decode_audio2返回值总为-1,我程序中代码如下:

         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;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 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;}#if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)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);}#endif


 

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);}#endifint 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_close(pCodecCtx);av_close_input_file(pFormatCtx);return TRUE;}

原创粉丝点击