ffmpeg解码h264图像帧:av_parser_parse2()//解码不拷贝数据问题!
char buf[2048*500],*curr_264_nalu_ptr;
AVCodec *avCodec;
AVCodecParserContext *avParserContext;
AVCodecContext *avContext;
AVFrame *avFrame;
uint8_t* avBuf;
//av_log_set_callback( av_log_callback );
avcodec_register_all();
avCodec = avcodec_find_decoder( CODEC_ID_H264 );
avParserContext = av_parser_init( CODEC_ID_H264 );
avContext = avcodec_alloc_context3( avCodec );
avContext->codec_id = CODEC_ID_H264;
avFrame = avcodec_alloc_frame();
int numBytes = avpicture_get_size(PIX_FMT_YUV420P, 720,576);
avBuf = (uint8_t*)malloc(numBytes);
avpicture_fill((AVPicture *)avFrame, avBuf, PIX_FMT_YUV420P,720, 576);
if(avCodec->capabilities & CODEC_CAP_TRUNCATED)
avContext->flags|= CODEC_FLAG_TRUNCATED;
avcodec_open2( avContext, avCodec, NULL);
{
//获取一帧h264图像数据
}
while( curr_264_nalu_size )
{
unsigned char* buf = 0;
int buf_len = 0;
int len = av_parser_parse2(
avParserContext, avContext,
&buf, &buf_len,
curr_264_nalu_ptr , curr_264_nalu_size ,
AV_NOPTS_VALUE, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
curr_264_nalu_ptr += len;
curr_264_nalu_size -= len;
int got_picture=0;
if( buf_len == 0 )
{
AVPacket avpkt = {0};
av_init_packet(&avpkt);
avpkt.data = buf;
avpkt.size = buf_len;
avcodec_decode_video2(avContext, avFrame, &got_picture, &avpkt);
if( got_picture )
{
if (!avFrame->data[0]) return nil;
[self convertFrameToRGB];
return [self imageFromAVPicture:picture width:outputWidth height:outputHeight];
}
}
//release memery
...
return 0;
}
在av_parser_parse2处理后buf为空,buf_len为0,这样处理有问题吗?大神帮帮忙。。。数据是通过网络接收的h264帧(完整的一帧数据,可以转换成一副图像,以前的代码可以解码出图像,但是不支持1080p。。。所以想用ffmpeg解码),