ffmpeg 实时解码H.264 只能解码出来I帧,P帧解不出来

wangjijian 发布于 2013/08/09 17:24
阅读 5K+
收藏 0

求高手,指点啊。代码如下,pFrame是厂家给的回调函数的传递的视频数据,定义如下。

enum MEDIA_PACK_TYPE
{
	FILE_HEAD =	0,	    		// 文件头
	VIDEO_I_FRAME = 1,			// 视频I帧
	VIDEO_B_FRAME =	2,			// 视频B帧
	VIDEO_P_FRAME = 3,			// 视频P帧
	VIDEO_BP_FRAME = 4,			// 视频BP帧
	VIDEO_BBP_FRAME	= 5,		// 视频B帧B帧P帧
	VIDEO_J_FRAME = 6,			// 图片帧
	AUDIO_PACKET = 10,			// 音频包
};

typedef struct
{
	int		nPacketType;				// 包类型,见MEDIA_PACK_TYPE
	char*	pPacketBuffer;				// 缓存区地址
	unsigned int	dwPacketSize;				// 包的大小

	// 绝对时标
	int		nYear;						// 时标:年		
	int		nMonth;						// 时标:月
	int		nDay;						// 时标:日
	int		nHour;						// 时标:时
	int		nMinute;					// 时标:分
	int		nSecond;					// 时标:秒
	unsigned int 	dwTimeStamp;					// 相对时标低位,单位为毫秒
	unsigned int	dwTimeStampHigh;        //相对时标高位,单位为毫秒
	unsigned int   dwFrameNum;             //帧序号
	unsigned int   dwFrameRate;            //帧率
	unsigned short uWidth;              //图像宽度
	unsigned short uHeight;             //图像高度
	unsigned int       Reserved[6];            //保留
} PACKET_INFO_EX;

 

static bool bFirst = true;
	static AVCodecContext * pCodecCtx = NULL;
	static AVFrame * pavFrame = NULL;
	static AVCodec* pCodec = NULL;
	static size_t count = 0;
	if (bFirst)
	{
		bFirst = false;

		av_register_all();
		pCodec = avcodec_find_decoder(AV_CODEC_ID_H264);
		if (pCodec)
		{
			pCodecCtx = avcodec_alloc_context3(pCodec);
			pCodecCtx->time_base.num = 1;
			pCodecCtx->time_base.den = 25;
			pCodecCtx->bit_rate = 0;
			pCodecCtx->frame_number = 1;
			pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
			pCodecCtx->width = 704;
			pCodecCtx->height = 576;

			if (avcodec_open2(pCodecCtx, pCodec, NULL) >= 0)
			{
				pavFrame = avcodec_alloc_frame();
			}
		}
		int nGot = 0;
		AVPacket avpacket;
		av_init_packet(&avpacket);
		uint8_t inputbuf[204800 + FF_INPUT_BUFFER_PADDING_SIZE];
		memcpy_s(inputbuf, sizeof(inputbuf), pFrame->pPacketBuffer, pFrame->dwPacketSize);
		avpacket.size = pFrame->dwPacketSize;
		avpacket.data = inputbuf;

		avcodec_decode_video2(pCodecCtx, pavFrame, &nGot, &avpacket);
		if (pFrame->nPacketType == 1)
		{
			printf("I frame!\n");
		}
		printf("%d\n", ++count);
		if (nGot)
		{
			printf("Got a picture!\n");
		}
	}

 

加载中
0
wangjijian
wangjijian

问题已解决。

代码有点小问题,更正的如下:

static bool bFirst = true;
	static AVCodecContext * pCodecCtx = NULL;
	static AVFrame * pavFrame = NULL;
	static AVCodec* pCodec = NULL;
	static size_t count = 0;
	if (bFirst)
	{
		bFirst = false;

		av_register_all();
		pCodec = avcodec_find_decoder(AV_CODEC_ID_H264);
		if (pCodec)
		{
			pCodecCtx = avcodec_alloc_context3(pCodec);
			pCodecCtx->time_base.num = 1;
			pCodecCtx->time_base.den = 25;
			pCodecCtx->bit_rate = 0;
			pCodecCtx->frame_number = 1;
			pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
			pCodecCtx->width = 704;
			pCodecCtx->height = 576;

			if (avcodec_open2(pCodecCtx, pCodec, NULL) >= 0)
			{
				pavFrame = avcodec_alloc_frame();
			}
		}
	}
		int nGot = 0;
		AVPacket avpacket;
		av_init_packet(&avpacket);
		uint8_t inputbuf[204800 + FF_INPUT_BUFFER_PADDING_SIZE];
		memcpy_s(inputbuf, sizeof(inputbuf), pFrame->pPacketBuffer, pFrame->dwPacketSize);
		avpacket.size = pFrame->dwPacketSize;
		avpacket.data = inputbuf;

		avcodec_decode_video2(pCodecCtx, pavFrame, &nGot, &avpacket);
		if (pFrame->nPacketType == 1)
		{
			printf("I frame!\n");
		}
		printf("%d\n", ++count);
		if (nGot)
		{
			printf("Got a picture!\n");
		}
	
	return 0;

 

0
l
lingshaohu

大哥,你是用英码dvr的sdk吧?我现在在用,也遇到解码的问题。。。能发您的解码部分的完整源码给我吗····412386275@qq.com            。谢谢~

0
l
lingshaohu

大哥,解码后的是rgb格式的吗?

返回顶部
顶部