Log in

View Full Version : YUV <-> RGB matrix in AVISynth


skaven65536
8th July 2005, 14:59
Hello!
Now I am working on video application, which will support .avi, .yuv and .avs files as input files.
To test it's work with .avs I've created such script:
script.avs

clip1 = Avisource("bus.avi")
clip1 = clip1.ConvertToYUY2()
return clip1

(I've also tried ConvertToYV12).
As I understand, there is no any losses in Y component during YUV->YUY2.
bus.avi - is an uncompressed RGB24 sequence.
After it I've add a code in my application, which will open these two files (bus.avi and script.avs), convert frames from "bus.avi" to YUV colorspace and perfom an Y-PSNR convertion between them (correspondent frames from these two sequences).
In theory, the result must be near 100 :), but it was only about 30 :(
Before it I've used such rgb<->yuv matrix

y = (0.299 * r + 0.587 * g + 0.114 * b);
u = ((-0.147) * r -0.289 * g + 0.436 * b);
v = (0.615 * r -0.515 * g -0.100 * b);

r = (y + 1.14 * v);
g = (y - 0.395 * u - 0.581 * v);
b = (y + 2.032 * u) ;

I know, that it is also a variety of this matrix, when you add 128 to u and v to make them positive, but it doesn't affect on Y component.

After it I've tried matrix from fourcc.org site (http://www.fourcc.org/fccyvrgb.php).
The PSNR was about 60, it is really better :), but I think that it is too much for just a round-up mistake.

So, the question is: what RGB<->AVS matrix is used in AVISynth?

Thanks.

IanB
8th July 2005, 15:55
The default avisynth YUV colour spaces have a Y range of [16..235] and a UV range of [16..240] (or 128+/-112). The RGB range is [0..255].

The default matrix is Rec.601 y = (0.299*r + 0.587*g + 0.114*b) with 0..255 to 16..235 scaling.

There is the option, Matrix="rec709", to use Rec.709 coefficients, y = (0.2125*r + 0.7154*g + 0.0721*b)

In the new rev 2.5.6 there is the added option to use PC levels scaling (full range 0..255 to 0..255). Matrix="PC.601" or "PC.709"

IanB

Wilbert
8th July 2005, 16:04
Hello!
Now I am working on video application, which will support .avi, .yuv and .avs files as input files.
To test it's work with .avs I've created such script:
script.avs

clip1 = Avisource("bus.avi")
clip1 = clip1.ConvertToYUY2()
return clip1

(I've also tried ConvertToYV12).
As I understand, there is no any losses in Y component during YUV->YUY2.
Depends. If i understand it correctly you are comparing YUY2 [16,235] from AviSynth (note: the script above scales the luma range to [16,235]) with YUY2 from your progam (with luma range [0,255]). Other than that, the color converions are the same.

For a meaningful conversion i suggest you try the script

Avisource("bus.avi")
ConvertToYUY2(matrix="pc.601")

which doesn't scale the luma range.

After it I've tried matrix from fourcc.org site (http://www.fourcc.org/fccyvrgb.php).
The PSNR was about 60, it is really better, but I think that it is too much for just a round-up mistake.
Ok, that one scales the luma range to [16,235]. These coefficients are the same ones that avs is using by default.

Then i don't know what's going on ....

edit: too late :)

edit2: your u conversion is wrong. The coefficients should sum up to one. I will post the right ones when i'm home.

Wilbert
8th July 2005, 18:13
Correct conversion:

Y = 0.299 * R + 0.587 * G + 0.114 * B
U = - 0.1687 * R - 0.3313 * G + 0.500 * B
V = 0.5000 * R - 0.4187 * G - 0.0813 * B

reference:
http://www.avisynth.org/ColorConversions