YUV主要采樣格式理解


主要的采樣格式有YCbCr 4:2:0、YCbCr 4:2:2、YCbCr 4:1:1和 YCbCr 4:4:4。其中YCbCr 4:1:1 比較常用,其含義為:每個點保存一個 8bit 的亮度值(也就是Y值), 每 2x2 個點保存一個 Cr 和Cb 值, 圖像在肉眼中的感覺不會起太大的變化。所以, 原來用 RGB(R,G,B 都是 8bit unsigned) 模型, 4 個點需要 8x3=24 bites(如下圖第一個圖). 而現在僅需要 8+(8/4)+(8/4)=12bites, 平均每個點占12bites(如下圖第二個圖)。這樣就把圖像的數據壓縮了一半。

上邊僅給出了理論上的示例,在實際數據存儲中是有可能是不同的,下面給出幾種具體的存儲形式:

(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字節內存(亮度2個字節,兩個色度各1個字節)。

下面的四個像素為: [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字節內存(亮度4個字節,兩個色度各1個字節)。

下面的四個像素為: [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。對非壓縮的8比特量化的視頻來說,每個由2x2個2行2列相鄰的像素組成的宏像素需要占用6字節內存(亮度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]

Y'UV420p (and Y'V12 or YV12)

Y'UV420p is a planar format, meaning that the Y', U, and V values are grouped together instead of interspersed. The reason for this is that by grouping the U and V values together, the image becomes much more compressible. When given an array of an image in the Y'UV420p format, all the Y' values come first, followed by all the U values, followed finally by all the V values.

The Y'V12 format is essentially the same as Y'UV420p, but it has the U and V data reversed: the Y' values are followed by the V values, with the U values last. As long as care is taken to extract U and V values from the proper locations, both Y'UV420p and Y'V12 can be processed using the same algorithm.

As with most Y'UV formats, there are as many Y' values as there are pixels. Where X equals the height multiplied by the width, the first X indices in the array are Y' values that correspond to each individual pixel. However, there are only one fourth as many U and V values. The U and V values correspond to each 2 by 2 block of the image, meaning each U and V entry applies to four pixels. After the Y' values, the next X/4 indices are the U values for each 2 by 2 block, and the next X/4 indices after that are the V values that also apply to each 2 by 2 block.

Translating Y'UV420p to RGB is a more involved process compared to the previous formats. Lookup of the Y', U and V values can be done using the following method:

size.total = size.width * size.height;

y = yuv[position.y * size.width + position.x];

u = yuv[(position.y / 2) * (size.width / 2) + (position.x / 2) + size.total];

v = yuv[(position.y / 2) * (size.width / 2) + (position.x / 2) + size.total + (size.total / 4)];

rgb = Y'UV444toRGB888(y, u, v);

Here "/" is Div not division.

As shown in the above image, the Y', U and V components in Y'UV420 are encoded separately in sequential blocks. A Y' value is stored for every pixel, followed by a U value for each 2×2 square block of pixels, and finally a V value for each 2×2 block. Corresponding Y', U and V values are shown using the same color in the diagram above. Read line-by-line as a byte stream from a device, the Y' block would be found at position 0, the U block at position x×y (6×4 = 24 in this example) and the V block at position x×y + (x×y)/4 (here, 6×4 + (6×4)/4 = 30).

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM