利用ffmpeg将H264解码为RGB

利用ffmpeg将H264解码为RGB由于公司买到了一个不提供解码器的设备,我不得已还要做解码的工作。在网上找了一圈,H264解码比较方便的也就是ffmpeg一系列的函数库了,原本设备中也是用这套函数库解码,但厂家不给提供,没办法,只得自己搞了。

大家好,又见面了,我是你们的朋友全栈君。

由于公司买到了一个不提供解码器的设备,我不得已还要做解码的工作。在网上找了一圈,H264解码比较方便的也就是ffmpeg一系列的函数库了,原本设备中也是用这套函数库解码,但厂家不给提供,没办法,只得自己搞了。

利用H264解码分为几个步骤:

 

注意一点在添加头文件的时候要添加extern “C”,不然会出现错误

extern "C"
{
#include <avcodec.h>
#include <avformat.h>
#include <avutil.h>
#include <swscale.h>
};

 

 

这里申明了几个全局变量

AVCodec         *pCodec = NULL;
AVCodecContext  *pCodecCtx = NULL;
SwsContext      *img_convert_ctx = NULL;
AVFrame         *pFrame = NULL;
AVFrame         *pFrameRGB = NULL;

 

1. 初始化

int H264_Init(void)
{
	/* must be called before using avcodec lib*/
	avcodec_init();
	/* register all the codecs */
	avcodec_register_all();

	/* find the h264 video decoder */
	pCodec = avcodec_find_decoder(CODEC_ID_H264);
	if (!pCodec) {
		fprintf(stderr, "codec not found\n");
	}
	pCodecCtx = avcodec_alloc_context();

	/* open the coderc */
	if (avcodec_open(pCodecCtx, pCodec) < 0) {
		fprintf(stderr, "could not open codec\n");
	}
	// Allocate video frame
	pFrame = avcodec_alloc_frame();
	if(pFrame == NULL)
		return -1;
	// Allocate an AVFrame structure
	pFrameRGB=avcodec_alloc_frame();
	if(pFrameRGB == NULL)
		return -1;


	
	return 0;

}

在最早使用的时候没有使用全局变量,初始化中也就只有init和regisger这两个函数,而这样做的下场是,非关键帧全部无法解码,只有关键帧才有办法解码。

2. 解码

解码的时候avcodec_decode_video函数是进行解码操作,在外部定义outputbuf的大小时,pixes*3,outsize是返回的outputbuf的size,值也是pixes*3。

 

在解码的时候这几句话的意义是将YUV420P的数据倒置。在原先使用中,发现解出来的图像居然是中心旋转图,后面在网上找了些办法,觉得这个比较实用。解码实时是很重要的,图像转化完之后也可以讲RGB图再次转化,那样也能成为一个正的图,但是那样效率就明显低了。

	pFrame->data[0] += pFrame->linesize[0] * (pCodecCtx->height-1);
	pFrame->linesize[0] *= -1;
	pFrame->data[1] += pFrame->linesize[1] * (pCodecCtx->height/2 - 1);;
	pFrame->linesize[1] *= -1;
	pFrame->data[2] += pFrame->linesize[2] * (pCodecCtx->height/2 - 1);;
	pFrame->linesize[2] *= -1;

 

 

int H264_2_RGB(unsigned char *inputbuf, int frame_size, unsigned char *outputbuf, unsigned int*outsize)
{
	
	int             decode_size;
	int             numBytes;
	int             av_result;
	uint8_t         *buffer = NULL;

	printf("Video decoding\n");

	av_result = avcodec_decode_video(pCodecCtx, pFrame, &decode_size, inputbuf, frame_size);
	if (av_result < 0)
	{
		fprintf(stderr, "decode failed: inputbuf = 0x%x , input_framesize = %d\n", inputbuf, frame_size);
		return -1;
	}

	// Determine required buffer size and allocate buffer
	numBytes=avpicture_get_size(PIX_FMT_BGR24, pCodecCtx->width,
		pCodecCtx->height);
	buffer = (uint8_t*)malloc(numBytes * sizeof(uint8_t));
	// Assign appropriate parts of buffer to image planes in pFrameRGB
	avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_BGR24,
		pCodecCtx->width, pCodecCtx->height);

	img_convert_ctx = sws_getCachedContext(img_convert_ctx,pCodecCtx->width,pCodecCtx->height,
		//PIX_FMT_YUV420P,pCodecCtx->width,pCodecCtx->height,pCodecCtx->pix_fmt,
		pCodecCtx->pix_fmt,pCodecCtx->width,pCodecCtx->height,PIX_FMT_RGB24 ,
		SWS_X ,NULL,NULL,NULL) ;
	if (img_convert_ctx == NULL) 
	{

		printf("can't init convert context!\n") ;
		return -1;
	}
	pFrame->data[0] += pFrame->linesize[0] * (pCodecCtx->height-1);
	pFrame->linesize[0] *= -1;
	pFrame->data[1] += pFrame->linesize[1] * (pCodecCtx->height/2 - 1);;
	pFrame->linesize[1] *= -1;
	pFrame->data[2] += pFrame->linesize[2] * (pCodecCtx->height/2 - 1);;
	pFrame->linesize[2] *= -1;
	sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize,
		0, 0 - pCodecCtx->width, pFrameRGB->data, pFrameRGB->linesize);
	
	if (decode_size)
	{
		*outsize = pCodecCtx->width * pCodecCtx->height * 3;
		memcpy(outputbuf, pFrameRGB->data[0], *outsize);
	}	


	free(buffer);
	return 0;
}

3. 释放资源

资源的回收。

void H264_Release(void)
{
	avcodec_close(pCodecCtx);
	av_free(pCodecCtx);
	av_free(pFrame);
	av_free(pFrameRGB);
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/149784.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Ant安装及环境配置「建议收藏」

    Ant安装及环境配置「建议收藏」1什么是AntApacheAnt是一个基于Java的生成工具。生成工具在软件开发中用来将源代码和其他输入文件转换为可执行文件的形式(也有可能转换为可安装的产品映像形式)。随着应用程序的生成过程变得更加复杂,确保在每次生成期间都使用精确相同的生成步骤,同时实现尽可能多的自动化,以便及时产生一致的生成版本2、下载、安装、环境变量配置ant下载地址https://download…

    2022年7月24日
    2
  • com组件接口_com组件特点

    com组件接口_com组件特点int main( int argc, char *argv[] ){cout << "Ini

    2022年8月5日
    5
  • Opencv学习笔记(九)光流法

    Opencv学习笔记(九)光流法原创文章,转贴请注明:http://blog.csdn.net/crzy_sparrow/article/details/7407604   本文目录:     一.基于特征点的目标跟踪的一般方法     二.光流法     三.opencv中的光流法函数    四.用类封装基于光流法的目标跟踪方法     五.完整代码     六.参考文献

    2022年7月23日
    9
  • 9款极具创意的HTML5/CSS3进度条动画(免积分下载)「建议收藏」

    9款极具创意的HTML5/CSS3进度条动画(免积分下载)

    2022年1月28日
    50
  • 常用正则表达式—邮箱(Email)

    常用正则表达式—邮箱(Email)本文针对有一点正则基础的同学,如果你对正则一无所知,请移步“正则表达式30分钟入门教程”学习。要验证一个字符串是否为邮箱的话,首先要了解邮箱账号的格式。我尝试过在网上找出一个标准的格式,但是很遗憾我没有找到。我也尝试使用RFC标准来判断邮箱的格式,但是也没有结果。网上些博客说不应该使用RFC标准来验证邮箱是否合法,有兴趣的可以看看“isthisemailval

    2022年4月4日
    609
  • 走进webpack(2)–第三方框架(类库)的引入及抽离

    正文之前,由于这是一个系列的文章,可能第一次看到的看官老爷们会觉得突兀,如果你是webpack新手,我建议你先从前几篇文章看起,如果你对webpack有一些了解,也希望可以在github上下载代码,对

    2022年3月25日
    35

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号