【图像】数据格式介绍(yuv420sp、yuv420sp、yv12,nv12等)

 

YUV定义:分为三个分量,“Y”表示明亮度(Luminance或Luma),也就是灰度值;而“U”和“V” 表示的则是色度(Chrominance或Chroma),作用是描述影像色彩及饱和度,用于指定像素的颜色。

 

YUV格式:有两大类:planar和packed。

YUV格式通常有两大类:打包(packed)格式和平面(planar)格式。

对于planar的YUV格式,先连续存储所有像素点的Y,紧接着存储所有像素点的U,随后是所有像素点的V。

对于packed的YUV格式,每个像素点的Y,U,V是连续交叉存储的。

 

YUV存储:格式其实与其采样的方式密切相关,主流的采样方式有三 种,YUV4:4:4,YUV4:2:2,YUV4:2:0,关于其详细原理,可以通过网 上其它文章了解,这里我想强调的是如何根据其采样格式来从码流中还原每个像素点的YUV值,因为只有正确地还原了每个像素点的YUV值,才能通过YUV与 RGB的转换公式提取出每个像素点的RGB值,然后显示出来。 

 

YUV特点:也是一种颜色编码方法,它将亮度信息(Y)与色彩信息(UV)分离,没有UV信息一样 可以显示完整的图像,只不过是黑白的,这样的设计很好地解决了彩色电视机与黑白电视的兼容问题。并且,YUV不像RGB那样要求三个独立的视频信号同时传 输,所以用YUV方式传送占用极少的频宽

 

1、YUV444

(1)YUV444p:YYYYYYYYY VVVVVVVVV UUUUUUUU

2、YUV422

(1)YUV422p:YYYYYYYY VVVV UUUU

(2)YUVY:YUYV YUYV YUYV YUYV

(3)UYVY:UYVY UYVY UYVY UYVY

 

3、YUV420

(1)YUV420p:

YV12:YYYYYYYY VV UU

I420:YYYYYYYY UU VV

(2)YUV420sp:

NV12:YYYYYYYY UVUV

NV21:YYYYYYYY VUVU

 

如果看到 YCbCr这种名字其实也是YUV格式 ,其中Y与YUV 中的Y含义一致,Cb , Cr 同样都指色彩,,只是在表示方法上有所不同,Cb、Cr 就是本来理论上的“分量/色差”的标识。C代表分量(是component的缩写)Cr、Cb分别对应r(红)、b(蓝)分量信号,Y除了g(绿)分量信号,还叠加了亮度信号。
 

 

YUV420SP

【图像】数据格式介绍(yuv420sp、yuv420sp、yv12,nv12等)_第1张图片

 

YUV420P

【图像】数据格式介绍(yuv420sp、yuv420sp、yv12,nv12等)_第2张图片

 

yuv420sp中画直线例子

#include 

typedef unsigned char  uInt8;
typedef unsigned short uInt16;
typedef unsigned int uInt32;
typedef char Int8;
typedef short Int16;
typedef int Int32


typedef enum
{
	TYPE_YUV422I_UYVY,
	TYPE_YUV422I_YUYV,
	TYPE_YUV420SP_NV12,
	TYPE_YUV420SP_NV21,
	TYPE_YUV422P,
	TYPE_YUV444I,
	TYPE_YUV444P,
}enYuvType;

typedef enum
{
	YUV_GREEN,
	YUV_RED,
	YUV_BLUE,
	YUV_PURPLE,
	YUV_DARK_GREEN,
	YUV_YELLOW,
	YUV_LIGHT_BLUE,
	YUV_LIGHT_PURPLE,
	YUV_DARK_BLACK,
	YUV_GRAY,
	YUV_WHITE,
	YUV_COLOR_MAX,
}enYuvColorIdx;


static stYuvColor s_color_table[YUV_COLOR_MAX] = {
	{0x00, 0x00, 0x00}, // green
	{0x00, 0x00, 0xff}, // red
	{0x00, 0xff, 0x00},	// blue
	{0x00, 0xff, 0xff},	// purple
	{0xff, 0x00, 0x00}, // dark green
	{0xff, 0x00, 0xff}, // yellow
	{0xff, 0xff, 0x00}, // light blue
	{0xff, 0xff, 0xff}, // light purple
	{0x00, 0x80, 0x80}, // dark black
	{0x80, 0x80, 0x80}, // gray
	{0xff, 0x80, 0x80}, // white
};

typedef struct
{
	uInt8 Y;
	uInt8 U;
	uInt8 V;
}stYuvColor;

typedef struct
{
	uInt16 x;
	uInt16 y;
}stPoint;

static void yuv_setPixel(
	uInt8* YBuff,
	uInt8* UVBuff,
	enYuvType yuvType,
	uInt16 width,
	uInt16 height,
	stPoint draw_point,
	enYuvColorIdx clrIdx)
{
	switch(yuvType)
	{
		case TYPE_YUV420SP_NV12:
		case TYPE_YUV420SP_NV21:
		{
			/*
				YY YY
				YY YY
				UV UV
			*/
			uInt32 y_offset = draw_point.y * width + draw_point.x;
			uInt32 u_offset = 0, v_offset = 0;
			YBuff[y_offset] = s_color_table[clrIdx].Y;
			#if 0
			Int32 x_flag = 1, y_flag = 1;
			if(draw_point.y % 2 == 0) {
				YBuff[y_offset + width] = s_color_table[clrIdx].Y;
				y_flag = 1;
			}
			else {
				YBuff[y_offset - width] = s_color_table[clrIdx].Y;
				y_flag = -1;
			}
 
			if(draw_point.x % 2 == 0) {
				YBuff[y_offset + 1] = s_color_table[clrIdx].Y;
				x_flag = 1;
			}
			else {
				YBuff[y_offset - 1] = s_color_table[clrIdx].Y;
				x_flag = -1;
			}
			YBuff[y_offset + width * y_flag + 1 * x_flag] = s_color_table[clrIdx].Y;
			#endif
			
			if(yuvType == TYPE_YUV420SP_NV12) {
				u_offset = (draw_point.y / 2) * width + draw_point.x / 2 * 2;
				v_offset = u_offset + 1;
			}
			else {
				v_offset = (draw_point.y / 2) * width + draw_point.x / 2 * 2;
				u_offset = v_offset + 1;
			}
			//UVBuff[u_offset] = s_color_table[clrIdx].U;
			//UVBuff[v_offset] = s_color_table[clrIdx].V;
			//printf("[%d, %d]: y_offset = %d, u_offset = %d, v_offset = %d\n",
			//	draw_point.x, draw_point.y, y_offset, u_offset, v_offset);
		}break;
	}
}
 

static void yuv_drawline(uInt8 *pBuf, uInt16 width, uInt16 height, point *startPoint, point *endPoint, type yuvType, enYuvColorIdx color, uInt16 lineWidth)
{
	if(!pBuf) {printf("null buf\n");return;}
 
	uInt8 *YBuff = NULL, *UVBuff = NULL;
	uInt16 x0 = startPoint->x, y0 = startPoint->y;
	uInt16 x1 = endPoint->x, y1 = endPoint->y;
 
	if(lineWidth == 0) lineWidth = 1;
	x0 = (x0 >= width) ? (x0 - lineWidth) : x0;
	x1 = (x1 >= width) ? (x1 - lineWidth) : x1;
	y0 = (y0 >= height) ? (y0 - lineWidth) : y0;
	y1 = (y1 >= height) ? (y1 - lineWidth) : y1;
 
	uInt16 dx = (x0 > x1) ? (x0 - x1) : (x1 - x0);
	uInt16 dy = (y0 > y1) ? (y0 - y1) : (y1 - y0);
 
	Int16 xstep = (x0 < x1) ? 1 : -1;
	Int16 ystep = (y0 < y1) ? 1 : -1;
	Int16 nstep = 0, eps = 0;
 
	stPoint draw_point;
	draw_point.x = x0;
	draw_point.y = y0;
 
	switch(yuvType)
	{
		case TYPE_YUV420SP_NV12:
		case TYPE_YUV420SP_NV21:
		{
			YBuff = pBuf;
			UVBuff = pBuf + width * height;
		}break;
		default:
			return;
	}
 
	// Bresenham算法画线
	if(dx > dy){
		while(nstep <= dx) {
			yuv_setPixel(YBuff, UVBuff, yuvType, width, height, draw_point, color);
			eps += dy;
			if( (eps << 1) >= dx ) {
				draw_point.y += ystep;
				eps -= dx;
			}
			draw_point.x += xstep;
			nstep++;
		}
	}else {
		while(nstep <= dy){	
			yuv_setPixel(YBuff, UVBuff, yuvType, width, height, draw_point, color);
			eps += dx;
			if( (eps << 1) >= dy ) {
				draw_point.x += xstep;
				eps -= dy;
			}
			draw_point.y += ystep;
			nstep++;
		}
	}
}

 

你可能感兴趣的:(音视频)