13

In my project I use OpenH264 codec, which is said to output data in the YUV 4:2:0 planar format. After decoding I get one array with width * height * 1.5 elements, which, when displaying, looks like this image:

http://o3d.googlecode.com/svn/trunk/samples_webgl/assets/shaving_cream.png

Why there are four areas below the main one (which contains Y - responsible for grayscale - elements ), instead of two, like on my second picture? Is that mean that the format is different or am I wrong and my world just collapsed?

I thought that the resoult should have looked like this:

enter image description here

y434y
  • 633
  • 1
  • 8
  • 27
  • 4
    From your image it is not quite clear how you image the U and V samples in linear memory. Planar format means that in linear memory there will be 320x240 U samples and then 320x240 V samples (see http://en.wikipedia.org/wiki/YUV#Y.27UV420p_.28and_Y.27V12_or_YV12.29_to_RGB888_conversion). If your image is a representation of how the image should be stored in memory, then your assumption is wrong since the violet box should be below the yellow one. – Rudolfs Bundulis Jan 07 '15 at 15:02
  • Thanks for the answer, it's also correct. But I found the image on Wikipedia a little bit confusing - maybe it's too small to get the idea. – y434y Jan 07 '15 at 15:59

3 Answers3

20

The accepted answer is totally correct, I'm just adding more information about the conversion to YUV values:

YUV420 Planar

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)];

Reference: https://en.wikipedia.org/wiki/YUV

thiagolr
  • 6,909
  • 6
  • 44
  • 64
10

It is exactly the way you assume. You are just overlooking that there are half as many U and V pixels across the width of the image. So the even numbered lines of U data are on the left, the odd numbered ones on the right. Followed by the V data.

To get it arranged the way you want to look at it, the decoder would have to interleave the U and V data. It doesn't do that. Just don't start looking at the image until you converted to RGB :)

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks:)! especially for the information about odd and even lines. It makes sense if you want to have the planar representation. – y434y Jan 07 '15 at 16:00
5

The diagram from thiagolr is very accurate. An accurate diagram that looks more like yours would be

Y U V layout

The reason yours looks like it has 4 pictures is as explained by Hans Passant.

Mutant Bob
  • 3,121
  • 2
  • 27
  • 52