Our website is made possible by displaying online advertisements to our visitors. Consider supporting us by disable your adblocker or Try ConvertXtoDVD and convert all your movies to DVD. Free trial ! :)

# YUV 4:2:0 to RGB

1. I extracted a raw video frame with "spirit ffms" (bindings for python to the ffmpegsource), and i opened the file in an hex editor to get the YUV values and i dont know how to do it since the h264 specification says that for each component (luma e chrominances) must have from 8 to 14 bits with a total of 1 luma for each pixel + 1 of each U and V every 4 pixels square, and i can get only the Y component because:

-frame resolution = 1920x1080 = 2073600 pixels
-total of bytes of the frame: 2108160

2108160 - 2073600 = 34560 remaining bytes

which means that i have to convert from only the Y component or all three components are in just one byte. I dont think the UV components are in the remaining bytes for two reasons: 1- 34560 * 8 = 276480 bits and 2073600 / 4 = 518400 (2x2 squares), so even if it have just one chrominance component with one bit, for each 2x2 square it would not fit all (squares), and even dividing 276480 / 129600 (16x16 squares) = 2,133.... it doesnt match the calculations.
2- analising the final of the frames in the hex editor i found that they are the same.

All the frames are "I" frames, and i know the frame is right because i converted the video to raw video with ffmpeg and the resultas are the same. Anyone knows anything that can help?
2. Hmmm - reading and don't understand - where is place for RGB from title? ffmpeg is able to output raw RGB so where is hint in Your question?
3. Yes, assuming 8 bit channels you should have a 1920x1080 luma plane followed by two 960x540 chroma planes.

Code:
```2,073,600
+ 518,400
+ 518,400
---------
3,110,400```
You don't have enough data. I suspect your frame isn't a full 1920x1080. Maybe black borders were cropped? How does the luma plane look if you view it as grey scale?
4. Originally Posted by pandy
Hmmm - reading and don't understand - where is place for RGB from title? ffmpeg is able to output raw RGB so where is hint in Your question?
I want to convert from YUV to RGB but i dont know how extract the necessary data from this frame. In ffmpeg i used the command: ffmpeg -i "file" -codec rawvideo "output file" and he gaves me raw video with YUV values, how do you got raw RGB?
5. add -pix_fmt rgb24 to use swscale to convert yuv=>rgb

e.g.

ffmpeg -i input.ext -vcodec rawvideo -pix_fmt rgb24 -an output.rgb
6. Originally Posted by jagabo
Yes, assuming 8 bit channels you should have a 1920x1080 luma plane followed by two 960x540 chroma planes.

Code:
```2,073,600
+ 518,400
+ 518,400
---------
3,110,400```
You don't have enough data. I suspect your frame isn't a full 1920x1080. Maybe black borders were cropped? How does the luma plane look if you view it as grey scale?
Im sure it is full 1920x1080 because the frame is filled blanc. All frames have the same size in bytes, so all the data must be there compressed someway.
7. -pix_fmt rgb24
8. Originally Posted by jagabo
-pix_fmt rgb24
Got it, but i still need know how convert from the original YUV values.
9. Exaclty what are you asking for? The equation for YUV to RGB ?

http://www.fourcc.org/fccyvrgb.php
10. Since your source is high definition you probably need the rec.709 matrix (unless the video is flagged differently).

http://en.wikipedia.org/wiki/YUV#BT.709_and_BT.601
11. Originally Posted by poisondeathray
Exaclty what are you asking for? The equation for YUV to RGB ?

http://www.fourcc.org/fccyvrgb.php
I want to know how to get the YCbCr values from the frame to convert to RGB since the total number of bytes doesnt match the necessary data required.
12. What's the command line you're using? I'll see if I can duplicate the problem.

I made a 10 frame 1920x1080 MP4 file and used the following command line to create a raw YV12 file:

Code:
` ffmpeg -i bn.mp4 -vcodec rawvideo -pix_fmt yuv420p -an output.yuv`
The output file was 31,104,000 bytes, exactly 10 times the size of a single 1920x1080 YV12 frame. I was able to import each plane of the first frame into a paint program as raw binary data to make three greyscale images.

Y:

V:

U:
13. Did you use the ffmpeg command line above ? This is what I got from a single frame raw YUV 4:2:0 source 1920x1080 , and converted to RGB in ffmpeg

raw RGB: 6,220,800 bytes
raw YUV 4:2:0 : 3,110,400 bytes
14. Originally Posted by toshyuki
Originally Posted by poisondeathray
Exaclty what are you asking for? The equation for YUV to RGB ?

http://www.fourcc.org/fccyvrgb.php
I want to know how to get the YCbCr values from the frame to convert to RGB since the total number of bytes doesnt match the necessary data required.

To get individual MB and pixel values from a YCbCr h264 stream, you need a comprehensive stream parser e.g h264visa

But the bytes match with the ffmpeg conversion. Maybe you have a bad binary ?
15. Originally Posted by jagabo
Since your source is high definition you probably need the rec.709 matrix (unless the video is flagged differently).

http://en.wikipedia.org/wiki/YUV#BT.709_and_BT.601
But even if something is wrong and incorrectly signaled - difference will quite subtle and for most normal colors unnoticeable even for hex values (and even for this case error can be calculated) - i still don't understand what is purpose of the author question and how to solve his problem.

Originally Posted by toshyuki
I want to know how to get the YCbCr values from the frame to convert to RGB since the total number of bytes doesnt match the necessary data required.
Perhaps author ask how to upsample data from 4:2:0 to 4:4:4? then how to perform YCbCr>RGB conversion?
But this is only guessing - intention of author question(s) are completely unclear for me.
16. Originally Posted by jagabo
What's the command line you're using? I'll see if I can duplicate the problem.

I made a 10 frame 1920x1080 MP4 file and used the following command line to create a raw YV12 file:

Code:
` ffmpeg -i bn.mp4 -vcodec rawvideo -pix_fmt yuv420p -an output.yuv`
The output file was 31,104,000 bytes, exactly 10 times the size of a single 1920x1080 YV12 frame. I was able to import each plane of the first frame into a paint program as raw binary data to make three greyscale images.

Y:

[Attachment 14176 - Click to enlarge]

V:

[Attachment 14177 - Click to enlarge]

U:

[Attachment 14178 - Click to enlarge]

This solves my question, now i have the YUV components to convert to RGB. Thanks everybody.

Statistics