YUV切割

YUV分割原理是对Y、U、V三个分量分别处理。以下使用I420做例子列举出左右、上下、切块三种方式,原理都一样。

不画图直接上代码。

 

左右切割:

void CutLR(const char* src1, int w, int h)
{
	int w1 = w / 2, w2 = w - w1;
	int h1 = h, h2 = h;
	char* cut_L = new char[w1 * h1 * 3 / 2];    // Left planer
	char* cut_R = new char[w2 * h2 * 3 / 2];    // Right planer
	    
	char* src_y = (char*)src1;
	char* src_u = src_y + w * h;
	char* src_v = src_u + w / 2 * h / 2;

	char* y1 = cut_L;
	char* u1 = cut_L + w1 * h1;
	char* v1 = u1 + w1 / 2 * h1 / 2;
	char* y2 = cut_R;
	char* u2 = cut_R + w2 * h2;
	char* v2 = u2 + w2 / 2 * h2 / 2;

	for (int i = 0; i < h; i++)
	{
		// Left y
		memcpy(y1 + i*w1, src_y + i * w, w1);	
		// Right y
		memcpy(y2 + i*w2, src_y + i*w + w1, w2);
	}

	for (int i = 0; i < h / 2; i++)
	{
		// Left uv
		memcpy(u1 + i*w1 / 2, src_u + i*w / 2, w1 / 2);
		memcpy(v1 + i*w1 / 2, src_v + i*w / 2, w1 / 2);

		// Right uv
		memcpy(u2 + i*w1 / 2, src_u + i*w / 2 + w1/2, w1 / 2);
		memcpy(v2 + i*w1 / 2, src_v + i*w / 2 + w1/2, w1 / 2);
	}
}

上下切割:

void CutUD(const char* src1, int w, int h)
{
	int w1 = w, w2 = w;
	int h1 = h / 2, h2 = h - h1;

	char* cut_U = new char[w1 * h1 * 3 / 2];	// Up planer
	char* cut_D = new char[w2 * h2 * 3 / 2];	// Down planer

	char* src_y = (char*)src1;
	char* src_u = src_y + w * h;
	char* src_v = src_u + w / 2 * h / 2;

	char* y1 = cut_U;
	char* u1 = cut_U + w1 * h1;
	char* v1 = u1 + w1 / 2 * h1 / 2;
	char* y2 = cut_D;
	char* u2 = cut_D + w2 * h2;
	char* v2 = u2 + w2 / 2 * h2 / 2;

	// Up y
	for (int i = 0; i < h1; i++)
	{
		memcpy(y1 + i*w1, src_y + i*w, w1);
	}
	// Up uv
	for (int i = 0; i < h1 / 2; i++)
	{
		memcpy(u1 + i*w1 / 2, src_u + i * w / 2, w1 / 2);
		memcpy(v1 + i*w1 / 2, src_v + i * w / 2, w1 / 2);
	}

	// Down y
	for (int i = 0; i < h2; i++)
	{
		memcpy(y2 + i*w2, src_y + (i + h1)*w, w2);
	}
	// Down uv
	for (int i = 0; i < h2 / 2; i++)
	{
		memcpy(u2 + i*w1 / 2, src_u + (i+h1/2) * w / 2, w1 / 2);
		memcpy(v2 + i*w1 / 2, src_v + (i+h1/2) * w / 2, w1 / 2);
	}
}

切割块:

/*
* src1: source i420
* w: source width
* h: source height
* x,y: cut postion
* dw: destination width
* dh: destination height
*/
void CutBlock(const char* src1, int w, int h, int x, int y, int dw, int dh)
{
	if (x + dw > w || y + dh > h) return;

	char* dst = new char[dw*dh * 3 / 2];    // dst YUV
	char* dst_y = dst;
	char* dst_u = dst + dw * dh;
	char* dst_v = dst_u + dw / 2 * dh / 2;

	char* src_y = (char*)src1;
	char* src_u = src_y + w * h;
	char* src_v = src_u + w / 2 * h / 2;

	for (int i = 0; i < dh; i++)
	{
		memcpy(dst_y + i * dw, src_y + (i + y) * w + x, dw);
	}
	for (int i = 0; i < dh / 2; i++)
	{
		memcpy(dst_u + i * dw / 2, src_u + (i + y / 2) * w / 2 + x/2, dw / 2);
		memcpy(dst_v + i * dw / 2, src_v + (i + y / 2) * w / 2 + x/2, dw / 2);
	}
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UVTools are a set of software utilities to play, convert and analyze YUV or RGB video data in their raw formats. The main features of YUVTools are: It accepts (plays, converts, edits and analyzes) the combination of following formats: YUV in 4:4:4, 4:2:2 or 4:2:0 sample format, RGB in 4:4:4 sampling format; in different component order, like YUV, YVU, UYV, RGB, BGR, etc. in progressive (one single frame) or interlaced (two fields) format; in planar (YYY...UUU...VVV...) or packed (YUV, YUV....) pixel format; in predefined or arbitrary resolutions; Support FOURCC ( refer to: http://www.fourcc.org/). An image preview function to help guess the video format interactively. YUV Player can open multiple player dialogs and play different files in each dialog separately, or play multiple video files in one player dialog one by one sequentially. The detailed format of any opened or generated files will be logged, and the user can directly select any file to play back from the history list. YUV Converter can convert any combination of the format to another format, or convert to (or from) a sequence of BMP files. Other conversion functions include: scaling, join, crop, flip, padding, merge and separation of color components. YUV Analyzer can be used to calculate PSNR between two YUV files, or compare pixel by pixel to check the difference between the corresponding frames of two files, or overlay block type or motion vectors on top of each frame. Another useful feature is to help check the motion between two adjacent frames. YUV Editor can be used to edit the images pixel by pixel, or overlay one YUV image on top of another YUV image file. Convert YUV file to AVI format or vice versa; Setting options include grid display, different YUV to RGB conversion formula, etc. Many of above functions can also be performed in command line mode, which are useful for batch process or scripting.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值