利用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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • APP开发防套路秘籍!

    APP开发防套路秘籍!在互联网软件开发行业混迹多年,深知这个行业的水有多深。就拿APP开发来说,市场上APP开发外包公司实在太多了,大中小都应有尽有,稍不留神,就很容易被“不正规”的公司给套路了。为此,整理了一份“三要一不”防套路秘籍,一起来学习下吧!1.要整体外包大多数企业,想要开发一款APP,都会首选外包这种方式。而外包又有两种形式,即整体外包和半外包。顾名思义,整体外包就是将UI、前端、后台都交给一个外包公司…

    2022年5月18日
    31
  • 跳出循环语句

    跳出循环语句跳出循环语句

    2022年6月4日
    30
  • 支付风控模型

    支付风控模型支付风控数据仓库建设 。支付风控涉及到多方面的内容,包括反洗钱、反欺诈、客户风险等级分类管理等。其中最核心的功能在于对实时交易进行风险评估,或者说是欺诈检测。如果这个交易的风险太高,则会执行拦截。由于反欺诈检测是在交易时实时进行的,在要求不能误拦截的同时,还有用户体验上的要求,即不能占用太多时间,一般要求风控操作必须控制在100ms以内,对于交易量大的业务,10ms甚至更低的性能要求都是必须的。

    2022年4月29日
    87
  • robots书写说明:

    robots书写说明:

    2021年10月9日
    57
  • RabbitMQ消费消息坑:failed to convert serialized Message content

    RabbitMQ消费消息坑:failed to convert serialized Message content文章目录一、问题描述二、解决方案方案一:共同使用一个对象方案二:消息JSON序列化(推荐)2.1.生产者发送消息JSON序列化2.2.消费者接收消息JSON反序列化三、测试一、问题描述2022-05-0314:01:40.630WARN16876—[ntContainer#0-2]s.a.r.l.ConditionalRejectingErrorHandler:ExecutionofRabbitmessagelistenerfailed.org.springfram

    2022年6月16日
    633
  • Python图像处理基本操作[通俗易懂]

    Python图像处理基本操作[通俗易懂]在Python中进行图像处理可以使用的库有很多,本文主要介绍下面三个:OpenCV、PIL、skimage。其中,OpenCV是图像处理中最强大的一个库,它的源代码是由C\C++写成的,所以原版的OpenCV可以与C、C++无缝结合。Python版的OpenCV主要依赖于cv2这个包来实现。Python里面自带一个PIL(pythonimageslibrary),但这个库现在已经停止更新了,所以使用Pillow,它是由PIL发展而来的

    2022年10月14日
    2

发表回复

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

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