首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >yuv422转rgb(422sp转420p)

yuv422转rgb(422sp转420p)

作者头像
全栈程序员站长
发布2022-07-25 20:26:58
发布2022-07-25 20:26:58
8710
举报

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

YUV420转RGB888

yuv420的数据存储方式是planar,就是在一帧中先存y分量,存完y存u,接着v分量。而在yuv420中有y分量widthheight byte,uv分量各是widthheight1/4,一帧中总的数据是widthheight3/2(widthheight12/8).所以当时认为,yuv分量代入转换公式的话,uv分量是不是少了。其实不然,因为是每四个y分量共用一个u分量一个v分量,但也不是[YiYi+1Yi+2Yi+3]共用[Ui],[Vi],因为是一个22的窗口内的Y分量共用一个uv,所以[YiYi+1Yi+wYi+w+1],其中i是偶数。uv分量由于是分别按水平方向和垂直方向2:1采样,所以[U(i/2*w/2+j/2)],V分量同理。

代码语言:javascript
复制
#include <iostream>
#include <stdio.h>
#include<fstream>

using namespace std;

bool yuv420ToRgb(char *yuv, int w, int h, char *rgb)
{ 
   
	unsigned char *y = new unsigned char[w*h];
	unsigned char *u = new unsigned char[w*h / 4];
	unsigned char *v = new unsigned char[w*h / 4];

	memcpy(y, yuv, w*h);
	memcpy(u, yuv + w * h, w*h / 4);
	memcpy(v, yuv + w * h * 5 / 4, w*h / 4);

	for (int i = 0; i < h; i++)
	{ 
   
		for (int j = 0; j < w; j++)
		{ 
   
			rgb[i*w*3 + 3*j] = 1.164*(y[i*w+j] - 16) + 1.596*(v[i / 4 * w + j / 2] - 128);//R

			rgb[i*w*3 + 3*j+1] = 1.164*(y[i*w + j] - 16) - 0.392*(u[i / 4*w+j/2] - 128) - 0.813*(v[i / 4 * w + j / 2] - 128);//G

			rgb[i*w*3 + 3*j+2] = 1.164*(y[i*w + j] - 16) + 2.017*(u[i / 4 * w + j / 2] - 128);  //B
		}
	}
	free(y);
	free(u);
	free(v);
	return true;
}


int main(int argc, char* argv[])
{ 
   
	FILE *yuv, *out;	
	int len = 832 * 480 * 3 / 2;
	char  *yuvbuff = ( char *)malloc(len);
	char  *rgbbuff = ( char *)malloc(len*2);
	char  *buff = (char *)malloc(len * 2);
	yuv = fopen("D://movie player//BasketballDrill_832x480_50.yuv", "rb");
	out= fopen("D://movie player//rgb888.txt", "wb+");

	ifstream fl("D://movie player//BasketballDrill_832x480_50.yuv", ios::binary);
	fl.seekg(0, ios::end);
	int size = fl.tellg();
	int inFrameNum = (width*height * 3 / 2);
	int frameNum = size / inFrameNum;

	for (int i = 0; i <frameNum; i++) { 
   
		fread(yuvbuff, 1, width*height * 3/2, yuv);
		yuv420ToRgb(yuvbuff, width, height, rgbbuff);
	    fwrite(rgbbuff, 1, width*height * 3 ,out);
	}
		return 0;
}

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档