YUV 像素格式

http://blog.csdn.net/skdev/article/details/6583028

1简介

YUV是种颜色编码方法,是彩色电视为了兼容黑白电视而发展起来的。在现代彩色电视系统中,通常采用三管彩色摄影机或彩色CCD摄影机进行取像,然后把取得的彩色图像信号经分色、分别放大校正后得到RGB,再经过矩阵变换电路得到亮度信号Y和两个色差信号R-Y(即U)、B-Y(即V),然后对这三个信号分别进行编码。Y就是所谓的流明(luminance),表示光的浓度且为非线性,使用伽马修正(gamma correction)编码处理,而CB和CR则为蓝色和红色的浓度偏移量成份。

 

2 YUV采样格式

  (1) YUV 4:4:4

  YUV三个信道的抽样率相同,因此在生成的图像里,每个象素的三个分量信息完整(每个分量通常8比特),经过8比特量化之后,未经压缩的每个像素占用3个字节。

  下面的四个像素为: [Y0 U0 V0] [Y1 U1 V1] [Y2 U2 V2] [Y3 U3 V3]

  存放的码流为: Y0 U0 V0 Y1 U1 V1 Y2 U2 V2 Y3 U3 V3

  (2) YUV 4:2:2

  每个色差信道的抽样率是亮度信道的一半,所以水平方向的色度抽样率只是4:4:4的一半。对非压缩的8比特量化的图像来说,每个由两个水平方向相邻的像素组成的宏像素需要占用4字节内存。

  下面的四个像素为:[Y0 U0 V0] [Y1 U1 V1] [Y2 U2 V2] [Y3 U3 V3]

  存放的码流为:Y0 U0 Y1 V1 Y2 U2 Y3 V3

  映射出像素点为:[Y0 U0 V1] [Y1 U0 V1] [Y2 U2 V3] [Y3 U2 V3]

  (3) YUV 4:1:1

  4:1:1的色度抽样,是在水平方向上对色度进行4:1抽样。对于低端用户和消费类产品这仍然是可以接受的。对非压缩的8比特量化的视频来说,每个由4个水平方向相邻的像素组成的宏像素需要占用6字节内存。

  下面的四个像素为: [Y0 U0 V0] [Y1 U1 V1] [Y2 U2 V2] [Y3 U3 V3]

  存放的码流为: Y0 U0 Y1 Y2 V2 Y3

  映射出像素点为:[Y0 U0 V2] [Y1 U0 V2] [Y2 U0 V2] [Y3 U0 V2]

  (4)YUV4:2:0

  4:2:0并不意味着只有Y,Cb而没有Cr分量。它指得是对每行扫描线来说,只有一种色度分量以2:1的抽样率存储。相邻的扫描行存储不同的色度分量,也就是说,如果一行是4:2:0的话,下一行就是4:0:2,再下一行是4:2:0...以此类推。对每个色度分量来说,水平方向和竖直方向的抽样率都是2:1,所以可以说色度的抽样率是4:1。

  下面八个像素为:[Y0 U0 V0] [Y1 U1 V1] [Y2 U2 V2] [Y3 U3 V3]

   [Y5 U5 V5] [Y6 U6 V6] [Y7U7 V7] [Y8 U8 V8]

  存放的码流为:Y0 U0 Y1 Y2 U2 Y3 Y5 V5 Y6 Y7 V7 Y8

  映射出的像素点为:[Y0 U0 V5] [Y1 U0 V5] [Y2 U2 V7] [Y3 U2 V7]

   [Y5 U0 V5] [Y6 U0 V5] [Y7U2 V7] [Y8 U2 V7]

 3 YUV 存储格式

YUV格式分为packed格式和planar格式,packed格式会将YUV数据按顺序放进数组里,planar则分将YUV的数据分为三部分放进数组里,即先放Y部分的数据,再放U,接着是V的数据。

3.1 Packed YUV Formats

Label

Bits per pixel

Description

AYUV

32

Combined YUV and alpha

CLJR

8

Cirrus Logic format with 4 pixels packed into a u_int32. A form of YUV 4:1:1 wiht less than 8 bits per Y, U and V sample.

cyuv

16

Essentially a copy of UYVY except that the sense of the height is reversed - the image is upside down with respect to the UYVY version.

GREY

8

Apparently a duplicate of Y800 (and also, presumably, "Y8  ")

IRAW

?

Intel uncompressed YUV. I have no information on this format - can you help?

IUYV

16

Interlaced version of UYVY (line order 0, 2, 4,....,1, 3, 5....) registered by Silviu Brinzei of LEAD Technologies.

IY41

12

Interlaced version of Y41P (line order 0, 2, 4,....,1, 3, 5....) registered by Silviu Brinzei of LEAD Technologies.

IYU1

12

12 bit format used in mode 2 of the IEEE 1394 Digital Camera 1.04 spec. This is equivalent to Y411

IYU2

24

24 bit format used in mode 0 of the IEEE 1394 Digital Camera 1.04 spec

HDYC

16

YUV 4:2:2 (Y sample at every pixel, U and V sampled at every second pixel horizontally on each line). A macropixel contains 2 pixels in 1 u_int32. This is a suplicate of UYVY except that the color components use the BT709 color space (as used in HD video).

UYNV

16

A direct copy of UYVY registered by NVidia to work around problems in some old codecs which did not like hardware which offered more than 2 UYVY surfaces.

UYVP

24?

YCbCr 4:2:2 extended precision 10-bits per component in U0Y0V0Y1 order. Registered by Rich Ehlers of Evans & Sutherland. (Awaiting confirmation of component packing structure)

UYVY

16

YUV 4:2:2 (Y sample at every pixel, U and V sampled at every second pixel horizontally on each line). A macropixel contains 2 pixels in 1 u_int32.

V210

32

10-bit 4:2:2 YCrCb equivalent to the Quicktime format of the same name.

V422

16

I am told that this is an upside down version of UYVY.

V655

16?

16 bit YUV 4:2:2 format registered by Vitec Multimedia. I have no information on the component ordering or packing.

VYUY

?

ATI Packed YUV Data (format unknown but you can get hold of a codec supporting ithere)

Y422

16

Direct copy of UYVY as used by ADS Technologies Pyro WebCam firewire camera.

YUY2

16

YUV 4:2:2 as for UYVY but with different component ordering within the u_int32 macropixel.

YUYV

16

Duplicate of YUY2

YUNV

16

A direct copy of YUY2 registered by NVidia to work around problems in some old codecs which did not like hardware which offered more than 2 YUY2 surfaces.

YVYU

16

YUV 4:2:2 as for UYVY but with different component ordering within the u_int32 macropixel.

Y41P

12

YUV 4:1:1 (Y sample at every pixel, U and V sampled at every fourth pixel horizontally on each line). A macropixel contains 8 pixels in 3 u_int32s.

Y411

12

YUV 4:1:1 with a packed, 6 byte/4 pixel macroblock structure.

Y211

8

Packed YUV format with Y sampled at every second pixel across each line and U and V sampled at every fourth pixel.

Y41T

12

Format as for Y41P but the lsb of each Y component is used to signal pixel transparency .

Y42T

16

Format as for UYVY but the lsb of each Y component is used to signal pixel transparency .

YUVP

24?

YCbCr 4:2:2 extended precision 10-bits per component in Y0U0Y1V0 order. Registered by Rich Ehlers of Evans & Sutherland.

Y800

8

Simple, single Y plane for monochrome images.

Y8

8

Duplicate of Y800 as far as I can see.

Y16

16

16-bit uncompressed greyscale image.

 UYVY存储结构

UYVY 是一种最常用的 YUV 4:2:2 格式,其数据排列如下:

  

3.2 Planar YUV Formats

Label

Bits per pixel

Description

YVU9

9

8 bit Y plane followed by 8 bit 4x4 subsampled V and U planes. Registered by Intel.

YUV9

9?

Registered by Intel., this is the format used internally by Indeo video code

IF09

9.5

As YVU9 but an additional 4x4 subsampled plane is appended containing delta information relative to the last frame. (Bpp is reported as 9)

YV16

16

8 bit Y plane followed by 8 bit 2x1 subsampled V and U planes.

YV12

12

8 bit Y plane followed by 8 bit 2x2 subsampled V and U planes.

I420

12

8 bit Y plane followed by 8 bit 2x2 subsampled U and V planes.

IYUV

12

Duplicate FOURCC, identical to I420.

NV12

12

8-bit Y plane followed by an interleaved U/V plane with 2x2 subsampling

NV21

12

As NV12 with U and V reversed in the interleaved plane

IMC1

12

As YV12 except the U and V planes each have the same stride as the Y plane

IMC2

12

Similar to IMC1 except that the U and V lines are interleaved at half stride boundaries

IMC3

12

As IMC1 except that U and V are swapped

IMC4

12

As IMC2 except that U and V are swapped

CLPL

12

Format similar to YV12 but including a level of indirection.

Y41B

12?

Weitek format listed as "YUV 4:1:1 planar". I have no other information on this format.

Y42B

16?

Weitek format listed as "YUV 4:2:2 planar". I have no other information on this format.

Y800

8

Simple, single Y plane for monochrome images.

Y8

8

Duplicate of Y800 as far as I can see.

CXY1

12

Awaiting clarification of format.

CXY2

16

Awaiting clarification of format.

Y42B存储结构

YCbCr 4:2:2 的方式存储

 4 YUVRGB的转换关系

yuv422 planar to rgb565
转换公式:
R=Y+1.4075*(V-128)
G=Y-0.3455*(U-128) - 0.7169*(V-128)
B=Y+1.779*(U-128)
为了加快运算速度,采用下面的整形计算法:
u = YUVdata[UPOS] - 128;
v = YUVdata[VPOS] - 128;

rdif = v + ((v * 103) >> 8);
invgdif = ((u * 88) >> 8) +((v * 183) >> 8);
bdif = u +( (u*198) >> 8);

r = YUVdata[YPOS] + rdif;
g = YUVdata[YPOS] - invgdif;
b = YUVdata[YPOS] + bdif;
r=r>255?:255:(r<0:?0:r);
g=g>255?:255:(g<0:?0:g);
b=b>255?:255:(b<0:?0:b);
以上得到的是rgb888的数据,再将rgb888转为rgb555
RGBdata[1] =( (r & 0xF8) | ( g >> 5) );
RGBdata[0] =( ((g & 0x1C) << 3) | ( b >> 3) );
YUV422 Planar  FORMAT size:
size=width*height*2;
YSIZE = size/2;
USIZE = size/4;
VSIZE = size/4;
YPOS=0;
UPOS=YPOS + size/2;
VPOS=UPOS + size/4;


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
b - c + d + e / f; } else if (op1 == '*' && op2 == '*' && op3 == '+YUV420P是一种常见的YUV像素格式,用于存储和处理视频数据。它是一种') { result = a * b * c + d + e + f; } else if (op1 == '*' && op2平面格式,即Y、U、V三个分量分别存储在不同的内存空间中,而 == '*' && op3 == '-') { result = a * b * c + d + e - f; } else if (op不是交错存储YUV420P格式像素数据按照以下顺序存储: - Y分量:1 == '*' && op2 == '*' && op3 == '*') { result = a * b * c + d + e * f; } else if (op1 == '*' && op2 == '*' && op3 == '/') { result = a * b * c按行存储,每行占用width个字节,共占用width * height个字节。 - U分量: + d + e / f; } else if (op1 == '*' && op2 == '/' && op3 == '+') { 按行存储,每行占用width / 2个字节,共占用width * height / 4个字节 result = a * b / c + d + e + f; } else if (op1 == '*' && op2 == '/' &&。 - V分量:按行存储,每行占用width / 2个字节,共占用width * height op3 == '-') { result = a * b / c + d + e - f; } else if (op1 == '*' / 4个字节。 因此,YUV420P格式的总像素数为width * height * 3 / && op2 == '/' && op3 == '*') { result = a * b / c + d + e * f; } else if (op1 == '*' && op2 == '/' && op3 == '/') { result = a * b / c + d +2,其中Y分量占用了2/3的空间,U和V分量各占用1/6的空间。 下面是一个简单的示例程序,读取一张YUV420P格式的图像文件,并将其 e / f; } else if (op1 == '/' && op2 == '+' && op3 == '+') { result = a / b + c + d + e + f; } else if (op1 == '/' && op2 == '+' && op3 ==显示在屏幕上。 ``` #include <SDL.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { SDL_Init(SDL_INIT_VIDEO); SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32 '-') { result = a / b + c + d + e - f; } else if (op1 == '/' && op2, SDL_SWSURFACE); // 打开YUV420P格式的图像文件 FILE* fp = fopen("example == '+' && op3 == '*') { result = a / b + c + d + e * f; } else if (.yuv", "rb"); if (!fp) { printf("Failed to open file.\n"); return -1; op1 == '/' && op2 == '+' && op3 == '/') { result = a / b + c + d + e / f } // 分配内存存储像素数据 int width = 640, height = 480; int y_size; } else if (op1 == '/' && op2 == '-' && op3 == '+') { result = a / b - = width * height; int uv_size = width * height / 4; unsigned char* y_data = (unsigned char*)malloc c + d + e + f; } else if (op1 == '/' && op2 == '-' && op3 == '-') { (y_size); unsigned char* u_data = (unsigned char*)malloc(uv_size); unsigned char* v_data = (unsigned char result = a / b - c + d + e - f; } else if (op1 == '/' && op2 == '-' &&*)malloc(uv_size); // 读取像素数据 fread(y_data, 1, y_size, fp); op3 == '*') { result = a / b - c + d + e * f; } else if (op1 == fread(u_data, 1, uv_size, fp); fread(v_data, 1, uv_size, fp); // 创建SDL '/' && op2 == '-' && op3 == '/') { result = a / b - c + d + e / f; }表面存储像素数据 SDL_Surface* image = SDL_CreateRGBSurfaceFrom(y_data, width, height, 8, else if (op1 == '/' && op2 == '*' && op3 == '+') { result = a / b * c + d width, 0, 0, 0, 0); SDL_Surface* u_surface = SDL_CreateRGBSurfaceFrom(u_data, + e + f; } else if (op1 == '/' && op2 == '*' && op3 == '-') { result = a width / 2, height / 2, 8, width / 2, 0, 0, 0, 0 / b * c + d + e - f; } else if (op1 == '/' && op2 == '*' && op3 ==); SDL_Surface* v_surface = SDL_CreateRGBSurfaceFrom(v_data, width / 2, height / 2, 8, '*') { result = a / b * c + d + e * f; } else if (op1 == '/' && op width / 2, 0, 0, 0, 0); // 创建SDL表面格式转换器 2 == '*' && op3 == '/') { result = a / b * c + d + e / f; } else if ( SDL_PixelFormat* format = SDL_AllocFormat(SDL_PIXELFORMAT_YV12); SDL_Surface* yuv_surface = SDL_CreateRGBop1 == '/' && op2 == '/' && op3 == '+') { result = a / b / c + d + e +Surface(0, width, height, 12, format->Rmask, format->Gmask, format->Bmask, format->Am f; } else if (op1 == '/' && op2 == '/' && op3 == '-') { result = a / b /ask); SDL_FreeFormat(format); // 转换YUV420P格式到YV12格式 SDL_Rect src_rect = c + d + e - f; } else if (op1 == '/' && op2 == '/' && op3 == '*') { { 0, 0, width, height }; SDL_Rect dst_rect = { 0, 0, width, height }; result = a / b / c + d + e * f; } else if (op1 == '/' && op2 == '/' SDL_BlitSurface(image, &src_rect, yuv_surface, &dst_rect); src_rect.w = dst_rect.w = width / && op3 == '/') { result = a / b / c + d + e / f; } // 输出表达式 2; src_rect.h = dst_rect.h = height / 2; SDL_BlitSurface(v_surface, &src_rect,和结果 printf("%d %c %d %c %d %c %d %c %d %c %d = % yuv_surface, &dst_rect); src_rect.x = dst_rect.x = width / 2; SDL_BlitSurface(u_surfaced\n", a, op1, b, op2, c, op3, d, op2, e, op3, f, &src_rect, yuv_surface, &dst_rect); // 创建SDL纹理 SDL_Renderer* renderer = SDL_CreateRenderer, result); return 0; } ``` 这个程序会随机生成 6 个操作数和 3 个(screen, -1, 0); SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, yuv_surface); // 渲染运算符,并计算出结果。注意,为了简化代码,我只列举了一部分情况,实际上一共有 $4^3=64$ 种情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值