YUV主要採樣格式理解

來源:互聯網
上載者:User

主要的採樣格式有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).

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.