YUV format

YUV is a class of pixel formats used in video applications, including VLC media player.

YUV is actually the name of the color space that is common to all "YUV" pixel formats. Contrary to RGB formats (Red-Green-Blue), YUV colors are represented with one "luminance" component called Y (equivalent to grey scale) and two "chrominance" components, called U (blue projection) and V (red projection) respectively, hence the name.

YUV can also be referred to as YCbCr, although the terms mean slightly different things, they tend to be confused and used interchangeably (including in VLC-related venues).

This page explains the most useful YUV pixel formats in the context of VLC media player. Unless otherwise stated, values for each component is stored in one octet (8 bits).

 

Planar formats

Planar (or sometimes "triplanar") formats use separate matrices for each of the 3 color component. In other words, there is one table of luminance pixel values, and two separate tables for the chrominance components. This is segregated representation in memory of pixels is more convenient for video coding.

YUV 4:2:0 (I420/J420/YV12)

I420

It has the luma "luminance" plane Y first, then the U chroma plane and last the V chroma plane.

The two chroma planes (blue and red projections) are sub-sampled in both the horizontal and vertical dimensions by a factor of 2. That is to say, for a 2x2 square of pixels, there are 4 Y samples but only 1 U sample and 1 V sample.

This format requires 4*8+8+8=48 bits for 4 pixels, so its depth is 12 bits per pixel.

I420 is by far the most common format in VLC. Most video decoders output raw pictures in I420 format.

A graphical illustration: Each letter represents one bit.

  • For a single I420 pixel: YYYYYYYY UU VV
  • For a 50-pixel I420 frame: YYYYYYYY*50 UU*50 VV*50 (or Y*8*50 U*2*50 V*2*50 for short)
  • For an n-pixel I420 frame: Y*8*n U*2*n V*2*n

J420

J420 is exactly like I420, but with a full range ("digital", 0-255) luma (Y) component instead of limited range ("analog", 16-240). The chroma planes are exactly the same as in I420.

YV12

YV12 is exactly like I420, but the order of the U and V planes is reversed. In the name, "YV" refers to the plane order: Y, then V (then U). "12" refers to the pixel depth: 12-bits per pixel as for I420.

YUV 4:2:2 (I422/J422)

I422

Like I420, I422 has one luma plane Y and 2 chroma planes UV.

However, in I422, chroma planes (blue and red projections) are sub-sampled only in the horizontal dimension, still by a factor of 2. Thus, there is the same amount of lines in chroma planes as in the luma plane.

For a 2x2 group of pixels, there are 4 Y samples and 2 U and 2 V samples each. The depth is 4*8+2*8+2*8 = 64 bits per 4 pixels, so 16 bits per pixel.

Some video codecs support I422 (optionally) for higher quality video encoding.

J422

J422 is exactly like I422, but with a full range ("digital", 0-255) luma (Y) component instead of limited range ("analog", 16-240). The chroma planes are exactly the same as in I422.

YUV 4:4:4

I444

As I420 and I422, I444 has one luma plane Y and 2 chroma planes U and V. Chroma planes are not sub-sampled: there are 3 octet values for each pixels. The depth is thus 24 bits per pixel (as with RGB24 or RGB32).

A few video codecs support I444 optionally.

Packed formats

In packed formats, you usually have only one plane, with all the luma and chroma data interleaved. This is similar to RGB pixel formats, only using a different color space.

Packed formats are very popular inside webcams. In hardware, using separate planes is inefficient: several memory accesses are needed for each pixel. Packed formats are easier and thus cheaper to use.

On the other hand, packet formats cannot normally deal with vertical sub-sampling. Otherwise scan lines would have different sizes. So generally, packed formats are horizontally subsampled, especially by a factor of 2 (i.e., YUV 4:2:2).

UYVY

Known as UYVYY422 or UYNV.

In UYVY, the chroma samples are sub-sampled by a factor of 2.

In UYVY, the succession for 2 pixels, starts by U then one luma if the first pixel, then V and then another luma for the second pixel.

YUY2

Known as YUY2YUYVV422 or YUNV.

In YUY2, the chroma samples are sub-sampled by a factor of 2.

In YUY2, the succession for 2 pixels, starts by one luma for the first pixel, then U, then another luma for the second pixel and then V again.

Conversion between YUY2 and UYVY is achieved by swapping bytes pairwise.

YVYU

YVYU is just like YUY2, but with U and V order reversed.

VYUV

TBD.

Semi-planar

Semi planar formats have two planes instead of three, one plane for luminance, and one plane for both chrominance components. They are also sometimes referred to as biplanar formats also.

NV12/NV21

NV12

Related to I420, NV12 has one luma "luminance" plane Y and one plane with U and V values interleaved.

In NV12, chroma planes (blue and red) are subsampled in both the horizontal and vertical dimensions by a factor of 2.

For a 2x2 group of pixels, you have 4 Y samples and 1 U and 1 V sample.

It can be helpful to think of NV12 as I420 with the U and V planes interleaved.

Here is a graphical representation of NV12. Each letter represents one bit:

  • For 1 NV12 pixel: YYYYYYYY UVUV
  • For a 2-pixel NV12 frame: YYYYYYYYYYYYYYYY UVUVUVUV
  • For a 50-pixel NV12 frame: Y*8*50 (UV)*2*50
  • For a n-pixel NV12 frame: Y*8*n (UV)*2*n

NV21

NV21 is like NV12, but with U and V order reversed: it starts with V.

Other NV formats

Following the same pattern as NV12/NV21, there are NV16/NV61 (4:2:2 sub-sampling) and NV24/NV42 (4:4:4 sampling) formats. They are mostly used in some funky cheap camera circuitry and not supported in VLC (as of VLC version 2.0).

YUV422是一种常见的像素格式,属于YUV格式类别中的一种。YUV是一种颜色编码系统,常用于视频和图像的处理,它将亮度(Y)和色度(U、V)信息分离,以达到减少数据量的目的。 ### 定义 YUV422格式中,每两个Y(亮度)分量共用一组U(Cb,蓝色色度)和V(Cr,红色色度)分量。也就是说,在水平方向上,每两个像素点共享相同的色度信息,这样可以在一定程度上减少色度数据的存储量,同时保持较好的图像质量。YUV422P是YUV422的一种平面模式,它并非将YUV数据交错存储,而是先存放所有的Y分量,接着存储所有的U(Cb)分量,最后存储所有的V(Cr)分量 [^3]。 ### 应用场景 在本地摄像头设备采集数据时,默认采集回来的数据格式可能就是YUV422。不过,显示数据那边默认通常是YUV420格式。如果要直接使用YUV422格式进行显示,则需要更改绘制代码,并且存储方面也需要做特殊处理。因此,有时会考虑从源头进行格式转换,将YUV422转换为YUV420格式。此外,YUV422格式在视频采集、处理和传输等领域也较为常见,它在保证一定图像质量的前提下,能够相对减少数据量,提高处理和传输效率 [^1]。 ### 处理方法 在对YUV422格式的数据进行处理时,若需要将其转换为其他格式,例如YUV420格式,可以使用像sws_scale这样的工具。sws_scale是FFmpeg库中的一个函数,它可以方便地实现各种格式之间的转换。由于FFmpeg采集时通常需要将非YUV420格式的数据转换为YUV420格式,所以使用sws_scale来处理YUV422到YUV420的转换是比较合适的 [^1]。 ### 示例代码(使用FFmpeg的sws_scale进行转换) ```c #include <libswscale/swscale.h> #include <libavutil/imgutils.h> // 初始化SwsContext SwsContext *sws_ctx = sws_getContext(src_width, src_height, AV_PIX_FMT_YUV422P, dst_width, dst_height, AV_PIX_FMT_YUV420P, SWS_BILINEAR, NULL, NULL, NULL); // 分配源和目标图像数据的内存 uint8_t *src_data[4]; int src_linesize[4]; av_image_alloc(src_data, src_linesize, src_width, src_height, AV_PIX_FMT_YUV422P, 1); uint8_t *dst_data[4]; int dst_linesize[4]; av_image_alloc(dst_data, dst_linesize, dst_width, dst_height, AV_PIX_FMT_YUV420P, 1); // 进行格式转换 sws_scale(sws_ctx, (const uint8_t * const *)src_data, src_linesize, 0, src_height, dst_data, dst_linesize); // 释放资源 sws_freeContext(sws_ctx); av_freep(&src_data[0]); av_freep(&dst_data[0]); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值