YUV12 and YUV2

Locked
User avatar
mckeed
Joined: Tue May 15, 2001 1:02 pm
Location: Troy, NY
Contact:
Org Profile

YUV12 and YUV2

Post by mckeed » Wed Dec 17, 2003 4:38 pm

I have been doing lots of looking around since I decided to go to avisynth 2.5 and found many filters/plugins for it. I understand what colorspace is and why RGB is different that YUV. I would like someone to explain to me or point me twords some information to explain the differences between YUV12 and YUV2 and why a filter written for one doens't work on another? Is one a higher sampling rate? Also, is there a way to tell right off the bat what colorspace something is in. I want to avoid unnecessary color conversions so if its in RGB I can do cleaning all in RGB vs if it is in YUY12 and i need YUV2 does this conversion hurt the process by degrading video quality. I allways thought DVD's were in YUV2 but with the new avisynth it complains unless i do a converstion to YUV2 colorspace before running plugins that run in YUV2 colorspace where as with the old avisynth everything just worked without me thinking about it. I am trying to get a better understanding of the whole process here. Thank you in advance.
"People can not gain anything without putting forth any effort. That is the absolute Truth" - Dante, Full Metal Alchemist
Image

User avatar
ErMaC
The Man who puts the "E" in READFAG
Joined: Sat Feb 24, 2001 4:39 pm
Location: Irvine, CA
Contact:
Org Profile

Post by ErMaC » Wed Dec 17, 2003 4:52 pm

There's an AVISynth command called Info() which will print out on a video stream all the information about it, including colorspace, so going:
AVISource(blah).Info()
will load that source with the info printed on the stream.

As for the differences in colorspace, there's a few basic things to understand. I'm not going to explain the difference between planar and interleaved colorspaces (since YUY2 is interleaved and YV12 is planar) but just know that there's a difference in the order the way the color information is stored.
Second, YUY2 stores twice as many color samples as YV12.
YUY2 stores color samples every 2 pixels horizontally, so in a line that's 720 pixels wide there are 360 color samples, shared between pairs of pixels. In YV12 colorspace, color samples are shared amongst a block of 4 pixels (in a 2x2 arrangement). This is for progressive MPEG2 though, in interlaced MPEG2, the color samples treat the two fields like different frames, and so the first two pixels of the first line and the first two pixels of the third line share a color sample, and the same goes for the second and fourth lines. Extrapolation to the rest of the frame is left as an exercise to the reader.

This essentially means that filters must be totally rewritten to deal with the two colorspaces, because not only do they store a different number of samples, they store them in a different format (interleaved vs. planar).

As background information, DVD-spec MPEG-1, -2, and -4 streams store YV12 colordata, which is one of the reasons AVISynth implemented it so that you don't need to go through any color conversions if you have YV12 source and destination. So don't be afraid of using YV12 colorspace if that's what your source is in, you aren't losing anything by using it.

trythil
is
Joined: Tue Jul 23, 2002 5:54 am
Status: N͋̀͒̆ͣ͋ͤ̍ͮ͌ͭ̔̊͒ͧ̿
Location: N????????????????
Org Profile

Post by trythil » Wed Dec 17, 2003 5:17 pm

The primary differences between YUY2 and YV12 (also called planar YUV) are twofold:

YUY2 is packed and 4:2:2 subsampled.
YV12 is planar and 4:2:0 subsampled.

The two differences are related to each other, so I'll just elaborate on them simultaneously.

In a packed-pixel format such as YUY2, you store Y', Cb (blue component, also called U), and Cr (red component, also called V) data in the following manner:

Y'0 Cb0 Y'1 Cr0 Y'2 Cb1 Y'3 Cr1 Y'4 Cb2 Y'5 Cr2 ... Y'n Cb(n/2) Y'n+1 Cr(n/2)

In YV12, you store Y'CbCr data in separate arrays, which can be represented as

Y'00 Y'01 Y'02 Y'03
Y'10 Y'11 Y'12 Y'13
...
Y'30 Y'31 Y'32 Y'33
...

Cb00 Cb01
Cb10 Cb11
...

Cr00 Cr01
Cb10 Cb11
...

Notice that the number of chrominance samples (the Cb and Cr) do not equal the number of luminance samples (Y') in either case. This is because there does not exist a chrominance + luminance sample for every pixel.

With YUY2 4:2:2, you have a 2:1 chrominance sampling reduction horizontally, and no reduction vertically.

With YV12 4:2:0, you have a 2:1 chrominance sampling reduction in both vertical and horizontal directions.

Using our running example, we can lay YUY2 on a 2-D grid (say, a video image) like so:
Image

(These images are ripped off of MSDN, so go there if they don't display)
So every scanline has two chrominance samples, and for every two luminance samples you have going horizontally, you have one chrominance sample (a chrominance sample is CbCr, not just Cb or just Cr).

The planar YV12 representation would look like

Image


This is a standard that's actually used in MPEG-2, which is why ripped DVDs will be in this colorspace, and not YUY2.

Filters written for YUY2 data won't work for YV12 data because of these fundamental differences.

trythil
is
Joined: Tue Jul 23, 2002 5:54 am
Status: N͋̀͒̆ͣ͋ͤ̍ͮ͌ͭ̔̊͒ͧ̿
Location: N????????????????
Org Profile

Post by trythil » Wed Dec 17, 2003 5:19 pm

Addition:

The Xs in the grid represent luminance samples; the Os chrominance samples.


And...damnit ErMaC, you beat me to it.

You're probably more correct than me, too :P

User avatar
ErMaC
The Man who puts the "E" in READFAG
Joined: Sat Feb 24, 2001 4:39 pm
Location: Irvine, CA
Contact:
Org Profile

Post by ErMaC » Wed Dec 17, 2003 5:28 pm

I'm certainly less technical and wordy. Jesus, tryth :shock:

User avatar
mckeed
Joined: Tue May 15, 2001 1:02 pm
Location: Troy, NY
Contact:
Org Profile

Post by mckeed » Wed Dec 17, 2003 5:33 pm

I just get lucky with a double explanation trythil :)

So the low down is stay in YUV12 if possible. So converting to YUV2 i essentially sample more so i'm not really loosing quality, just sampling more of the same data.
"People can not gain anything without putting forth any effort. That is the absolute Truth" - Dante, Full Metal Alchemist
Image

trythil
is
Joined: Tue Jul 23, 2002 5:54 am
Status: N͋̀͒̆ͣ͋ͤ̍ͮ͌ͭ̔̊͒ͧ̿
Location: N????????????????
Org Profile

Post by trythil » Wed Dec 17, 2003 7:11 pm

mckeed wrote:I just get lucky with a double explanation trythil :)

So the low down is stay in YUV12 if possible. So converting to YUV2 i essentially sample more so i'm not really loosing quality, just sampling more of the same data.
No, you're not sampling more of the same data, because there's nothing more to sample.

Since YV12 is 4:2:0 subsampled and YUY2 is 4:2:2 subsampled, this means that you have to interpolate between the scanlines if you want to convert between the two colorspaces. Good interpolation algorithms can minimize the effect, but you still have to fill in the missing values.

User avatar
mckeed
Joined: Tue May 15, 2001 1:02 pm
Location: Troy, NY
Contact:
Org Profile

Post by mckeed » Wed Dec 17, 2003 7:19 pm

ok....so YUV12 to YUV2 is a bad idea.
"People can not gain anything without putting forth any effort. That is the absolute Truth" - Dante, Full Metal Alchemist
Image

User avatar
Tab.
Joined: Tue May 13, 2003 10:36 pm
Status: SLP
Location: gayville
Org Profile

Post by Tab. » Wed Dec 17, 2003 9:36 pm

It's not so much the YV12 to YUY2 part, but the inevitable downsampling back to YV12 when you encode. All modern lossy codecs I can think of of (not just the mpeg codecs, as Ermac said) use it, and most good lossless codecs too.
Hm.. I did a series of conversion examples once to prove a point to Hatter. I'll have to see if those are still around.

User avatar
Tab.
Joined: Tue May 13, 2003 10:36 pm
Status: SLP
Location: gayville
Org Profile

Post by Tab. » Wed Dec 17, 2003 10:07 pm


Locked

Return to “Video & Audio Help”