sws_scale像素格式转换RGBA转YUV420P

FFmpeg 像素格式和尺寸转换函数

 

双线性过滤(Bilinear_filtering)

代码

test_sws_scale.cpp

#include <iostream>
#include <fstream>

extern "C"
{
#include "libswscale/swscale.h"
}


#pragma comment(lib, "swscale.lib")

#define YUVFILE   "400_300_25.yuv"
#define RGBAFILE  "800_600_25.rgba"

using namespace std;

int main()
{
	ifstream ifs;
	ofstream ofs;
	const int yuv_width = 400;
	const int yuv_height = 300;
	const int rgba_width = 800;
	const int rgba_height = 600;
	unsigned char* yuv[3] = { 0 };
	SwsContext* yuv2rgba = nullptr;
	SwsContext* rgba2yuv = nullptr;
	int yuv_linesize[3] = { yuv_width , yuv_width / 2, yuv_width / 2 };
	unsigned char* rgba = nullptr;
	int rgba_linesize = rgba_width * 4;
	int slice_height = 0;

	yuv[0] = new unsigned char[yuv_width * yuv_height];      // Y
	yuv[1] = new unsigned char[yuv_width * yuv_height / 4];  // U
	yuv[2] = new unsigned char[yuv_width * yuv_height / 4];  // V

	rgba = new unsigned char[rgba_width * rgba_height * 4];

	ifs.open(YUVFILE, ios::in | ios::binary);

	if (!ifs)
	{
		cerr << "open " << YUVFILE << " failed" << endl;

		return -1;
	}

	ofs.open(RGBAFILE, ios::out | ios::binary);

	if (!ofs)
	{
		cerr << "open " << RGBAFILE << " failed" << endl;

		return -1;
	}

	/* YUV420P 转 RGBA */
	for (int i = 0; i < 10; i++)  // 读取10帧数据
	{
		/* 读取一帧的 YUV 数据 */
		ifs.read((char*)yuv[0], yuv_width * yuv_height);
		ifs.read((char*)yuv[1], yuv_width * yuv_height / 4);
		ifs.read((char*)yuv[2], yuv_width * yuv_height / 4);

		/* 文件已经读完 */
		if (ifs.gcount() == 0)
		{
			break;
		}

		/* YUV转RGBA  上下文件创建和获取 */
		yuv2rgba = sws_getCachedContext(
						yuv2rgba,					// 转换上下文,NULL新创建,非NULL判断与现有参数是否一致,
													// 一致直接返回,不一致先清理当前然后再创建
						yuv_width, yuv_height,		// 输入宽高
						AV_PIX_FMT_YUV420P,			// 输入像素格式
						rgba_width, rgba_height,	// 输出的宽高
						AV_PIX_FMT_RGBA,			// 输出的像素格式
						SWS_BILINEAR,				// 选择支持变化的算法,双线性插值
						nullptr, nullptr, nullptr	// 过滤器参数
						);

		if (!yuv2rgba)
		{
			cerr << "sws_getCachedContext failed!" << endl;
			return -1;
		}

		slice_height = sws_scale(
			yuv2rgba,
			yuv,			// 输入数据
			yuv_linesize,	// 输入数据行字节数
			0, 
			yuv_height,		// 输入高度
			&rgba,			// 输出数据
			&rgba_linesize
		);

		ofs.write((char*)rgba, rgba_width * rgba_height * 4);  // 将转换后的 RGBA 数据写入到 rgba 文件中
		cout << slice_height << " " << flush;
	}

	ifs.close();
	ofs.close();

	ifs.open(RGBAFILE, ios::in | ios::binary);

	if (!ifs)
	{
		cerr << "open " << YUVFILE << " failed" << endl;

		return -1;
	}

	/* 将 RGBA 转成 YUV420P */
	for (;;)
	{
		/* 读取一帧的 RGBA 数据 */
		ifs.read((char*)rgba, rgba_width * rgba_height * 4);

		/* 文件已经读完 */
		if (ifs.gcount() == 0)
		{
			break;
		}

		/* RGBA 转 YUV 上下文件创建和获取 */
		rgba2yuv = sws_getCachedContext(
			rgba2yuv,					// 转换上下文,NULL新创建,非NULL判断与现有参数是否一致,
										// 一致直接返回,不一致先清理当前然后再创建
			rgba_width, rgba_height,	// 输入宽高
			AV_PIX_FMT_RGBA,			// 输入像素格式
			yuv_width, yuv_height, 		// 输出的宽高
			AV_PIX_FMT_YUV420P,			// 输出的像素格式
			SWS_BILINEAR,				// 选择支持变化的算法,双线性插值
			nullptr, nullptr, nullptr	// 过滤器参数
		);

		if (!rgba2yuv)
		{
			cerr << "sws_getCachedContext failed!" << endl;
			return -1;
		}

		slice_height = sws_scale(
			rgba2yuv,
			&rgba,			// 输入数据
			&rgba_linesize,	// 输入数据行字节数
			0,
			rgba_height,	// 输入高度
			yuv,			// 输出数据
			yuv_linesize
		);

		cout << "(" << slice_height << ")"  << " " << flush;
	}

	ifs.close();

	delete[] yuv[0];
	delete[] yuv[1];
	delete[] yuv[2];
	delete[] rgba;

	return 0;
}

实现了 YUV420P 和 RGBA 图像不同尺寸不同像素格式之间的转换。

测试结果

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值