Thursday, June 25, 2009

Back To Basics : YUV to RGB conversion (Color Space)

I know there are like hundreds of books and tutorials available which explain color space. But I also know that neither of those tutorials stop a new author to explain it again in different manner, more explanatory in more easy way, so nor Me.

I remember my first interview in video domain. I can still hear that voice.
"Ok tell me what will be the color if Y 128 and Cb and Cr are 0", I said "Gray"
"if Y is 0 and Cb and Cr are at 128 then color is ", I was not sure what to say....
"Ok all are in 128, so what will be color", I said "White" (It was my guess)

I must say that interviewer taught me a lot, At that time I was mostly working on understanding of H.264 standard. That was a hell work for me. So I was learning how to implement those algorithms but for basics of video thing I was just beginner.

ok, lets start the tutorial now.......

Color models are conversion formats from one model to another. There are RGB, YCbCr, YUV etc. RGB in formations comes though the capturing devices like cameras or scanners. RGB color space is generally used in computer graphics. And combination of these three colors will generate other colors like white, black, yellow, cyan etc.But in real world processing RGB model is not the efficient one. If each color represents with 8 bits/pixel than for RGB we need 24 bits/pixels. If we wish to modify intensity or color of one pixel, we have to read all three components, process it and then store back, NOT good in real time with processing wise as well as memory requirement wise. To solve that problem there are other color space/model which store the pixel information in intensity and color format, like using luma and two color difference which can be converted to RGB or from RGB to that formats. Most common is YUV format.

The basic equations for YUV are :
Y = 0.299R + 0.587G + 0.114B
U = 0.492 (B-Y)
U = 0.887 (R-Y)

and YUV to RGB are :
R = Y + 1.14V
G = Y - 0.395U - 0.581V
B = Y + 2.032U


RGB ranges 0 to 255 while Y has a range of 0 to 255, U ranges 0 to +/- 112 and V ranges 0 to +/-157. But these equations are generally scaled for better implementation in NTSC or PAL digital codec. And for 8 bit data YUV and RGB data are saturated between 0 to 255.

YCbCr is scaled and offset version of YUV model, where Y ranges 16-235, Cb and Cr ranges 16-240. But actually all saturated to 0-255 levels. Here are the equations:

Y = 0.257R + 0.504G + 0.098B + 16
Cb = -0.148R - 0.291G + 0.439B + 128
Cr = 0.439R - 0.368G - 0.071B +128

and YCbCr to RGB conversion equations are :
R = 1.164 (Y-16) + 1.596 (Cr-128)
G = 1.164 (Y-16) - 0.813 (Cr-128) - 0.391 (Cb - 128)
B = 1.164 (Y-16) + 2.018 (Cb- 128)

As per my interview Q & A I have to consider here to this YCbCr to RGB conversion. If you analyze properly YUV to RGB and YCbCR to RGB equations are almost the same. I simplified the equation for better to remember way for human being like me and specially should not be used in computer for implementation. (computer dont give interview ;))

Here is my approximate way for YCbCr to RGB conversion:

R = Y+ 1.5 (Cr-128)
G = Y - 0.8(Cr-128)
B = Y+ 2.0 (Cb-128)

Here is the table for color we will get with different value of YCbCr.

You dont have remember this table, just use above approximate equation and RGB Color Cube (shown below), you will have the answer in sec.



By the way, after these tutorial, here are my answers for previous questions.
1) what will be the color if Y 128 and Cb and Cr are 0 ? GREEN
2) If Y is 0 and Cb and Cr are at 128, what will be the color ? BLACK
3) All are in 128, so color will be ? WHITE

And this time 100% sure... :)

Sunday, June 14, 2009

Google Wave: A new way of Browsing

How many times you felt like you need multiple browsers to open various communication formats like 'email', 'chat', 'docs', 'blogs', 'youtube' etc. etc. while discussing with only one friend. You also might have thought Why I cant share all my docs in one place , so that everybody like my friend or team can see it and can discuss everything here at one browser in real time. Many times it happens to me like my friend tell me check this page or video or doc, he gives me link, I copy the link and open another browser and check it, sometimes link work and sometimes doesn't. Even if it works, he has to wait for me to finish reading it and come back to start discussion. Now while discussion we need to switch between the doc window and chat window multiple times, really frustrating........

Thanks to google that after few more months wait, this problem is going to resolved like forever. Google has launched the new open source product called 'Google Wave' . "Google Wave" is equal parts conversation and document, where people can communicate and work together with richly formatted text, photos, videos, maps, and more.



In a simple form Google Wave is nothing but a combination of gmail, gtalk, google docs, blogger, google map, video etc etc. truly saying a combo of all, and even I can say more than that. As it is open source product, so it is also a platform with a rich set of open APIs (google wave API )that allow developers to embed waves in other web services and to build extensions that work inside waves.



Google Wave has been designed by the founders of Where 2 Tech, a start-up acquired by Google to create a cutting-edge mapping service, which later became Google Maps. According to Lars Rasmussen "In Google Wave you create a wave and add people to it. Everyone on your wave can use richly formatted text, photos, gadgets, and even feeds from other sources on the web. They can insert a reply or edit the wave directly. It's concurrent rich-text editing, where you see on your screen nearly instantly what your fellow collaborators are typing in your wave. That means Google Wave is just as well suited for quick messages as for persistent content -- it allows for both collaboration and communication. You can also use "playback" to rewind the wave to see how it evolved."



It includes a rich text editor and other functions like desktop drag-and-drop (which, for example, lets you drag a set of photos right into a wave).



You can add person, chat with them, share videos, discuss about location all in real time.


As "Wave" has google wave extension so it means there are a numbers of applications available that can be used and there will be mutiple users available for interaction like playing online games such as sudoku, scramble, chess etc.


One more interesting thing is you have google map available there, so ask your friends to join for party and give the location in "Wave" using google map, get their reply immediately. Something like this.



Believe me there are a lot many other stuffs too, when you will use it , you will just say "Awesome". To know more about it, check my "Important links" sections at the last of this post.

PS: Google Wave is currently available in a developer preview as the APIs and product continue to evolve. Accounts on the developer sandbox will be given out to people intending to build with the Google Wave APIs prior to the public release.

So if you are developer and want to contribute, go get your "Wave" or wait for release mode for a few months. if you'd like to be notified when we launch Google Wave as a public product, you can sign up at here.

Important links:

1) Google Wave
2) Google Wave Developer Blog
3) Intoducing the Google Wave APIs
4) Google Wave Code Blog
5) About Google Wave
6) Google Wave: A Complete Guide
7) Google Wave Extensions: An Inside Look

Thursday, June 11, 2009

How to include YASM in Visual Studio 2005

Playing with SIMD or assembly is very enjoyable thing, you will love coding here. But believe me actual fun starts when you use on NASM or YASM to write assembly rather than working with IA32 inline (__Asm{...}) assembly development coding method. YASM has included some more features than NASM but the real issue with NASM is the limited debugging formats. Debugger available in NASM are for 'stabs'and 'dwarf2' only. If you work on visual studio than NASM no use. NASM will assemble the code but you can not debug the code. So the best choice is YASM.


YASM can be downloaded from here. YASM supports various output format and supports debug formats 'stabs', 'dwarf2' and 'CodeView (cv)' check this. So it can be included in Visual Studio 2005 for assembly/SIMD code development.

So today we will discuss about how to include YASM assembler in visual studio 2005 project. We will go step by step for better understanding. So here we goes:

1) Download the latest YASM.exe, if donwloaded Yasm executable binary is not named as yasm.exe; we have to rename it yasm.exe.

2) Go to directory "Program Files\Microsoft Visual Studio 8\VC\bin" where all VC related binaries are kept already and put yasm.exe into it.

3) Now you need file named as 'yasm.rules'. To get this, download latest source code of YASM. Actually you can build yasm.exe with this source project also, but I am not concern with that today . Now locate folder "\Mkfiles\vc8" in yasm folder, you will find 'yasm.rules' file there.

4) To use custom build tool of visual studio 2005, copy 'yasm.rules' file to "Program Files\Microsoft Visual Studio 8\VC\VCProjectDefaults".

5) Now simply write your assembly in a file added to your project with file extension '.asm'

6) To assemble a file with Yasm, select the Property Page for the file and the select Yasm Assembler in the Tool dialog entry. Then add following line to 'Misc' section in 'command line' menu of property page :-

Debug mode : yasm -f win32 -g cv8 -o "$(IntDir)\$(InputName)".obj "$(InputPath)"
Release Mode : yasm -f win32 -o "$(IntDir)\$(InputName)".obj "$(InputPath)"

Press Apply and you are done. Now enjoy SIMD/assembly with YASM in Visual studio 2005.
I hope, you will find this article useful.

Useful links:

1) YASM in Wiki
2) YASM Home page
3) Latest version for download
4) YASM Documentation
5) YASM in VS 2005

Sunday, June 07, 2009

Clip Signed Data To Arbitrary Unsigned Range in SIMD/Assembly

This post is again dedicated to Video domain. But I certainly can say there are various other applications too where we can use it.

Clipping is very simple algorithm as it name indicates, we clip our data to certain range. There will be High value and Low value. If data value is less than Low value, assign data to Low and if data value goes upper than High value assign data to High. something like this:

Clip(Data,Low,High) = if Data is less than Low then data = Low, else if Data is greater than High then data= High , else data = Data ------------(1)

In video domain, after IDCT operation we get signed data for pixel, which should be technically unsigned data type. Here we do clip operation for pixel data, and limit the data between unsigned data type range, and in normal situation pixel bit depth is 8 (i.e unsigned char). So equ. (1) becomes :


Clip(Data,0,255) =if Data is less than 0 then data = 0, else if Data is greater than 255 data= 255, else data = Data ------------------------(2)


But pixel bit-depth is not limited to 8. As I mentioned in my previous post "H264:How to do conversion from 8 bits to 14 bits bit depth support" under the label "Video", H.264 support till 14 bit bit-depth, when you don't want to compromise with quality, go for higher bit -depth. And here pixel data type will be 'unsigned short'. Now we have to modify the 'Clip' function for higher bit-depth. And this time it is not fixed to 8 and not even 14, rather it can vary from 8 to 14 depends upon the YUV input bit-depth for encoder and luma or chroma bit-depth information (bit_depth_luma_minus8 and bit_depth_chroma_minus8) from input H.264 coded video input for decoder. So lets do this clipping in generic form. And remember Low value will be 0 always only High value will change. So equ (2) modified as :

High = (1^Pixel_Bit_Depth)-1
Clip(Data, 0, High) = if Data is less than 0 then data = 0, else if Data is greater than High then data= High , else data = Data ------------------------(3)

There are other optimized ways too for equ.(3), but that's not my concern as of today. so moving ahead for SIMD/assembly (MMX/SSE/SSE2). Now how to achieve the same operation in assembly. Actually if bit-depth is 8 then there is a single instruction available in SIMD as :

packuswb Rx0, Rx0 ;Considered data is in Rx0 (mm/xmm) SIMD register (if pixel type is unsigned char)

or if Rx1 is '0' then

paddusb Rx0, Rx1

if you want to saturate for unsigned short then we have

paddusw Rx0, Rx1

But that is not our case, so we have to go by other way. As we have data type 'unsigned short ' and Max value will be (1^Pixel_Bit_Depth)-1 , So here we goes :

unsigned short High = (1^Pixel_Bit_Depth)-1
unsigned short Range = 0x8000;
unsigned short Low = 0x7FFF - High;
unsigned short MaxHigh = 0xFFFF - High;

movdqu Rx1, Range
movdqu Rx2, Low
movdqu Rx3, MaxHigh
paddw Rx0, Rx1
paddusw Rx0, Rx2 ;Add unsigned saturation with Low
psubusw Rx0, Rx3 ;Subtract unsigned saturation with MaxHigh


(Note above code instructions are for SSE2 but applied for MMX too, also I tried to wrote for one pixel data, to use SIMD advantage properly some data shuffling is required, here my intention was to give idea, not the complete code for cut n paste.)

Enjoy!!!!

Calvin Hobbes Math Atheist


A very good one from calvin and hobbes. I must say, you will enjoy it too.

I am posting it to share with you so that this religion will grow. ...
And FYI I am Math Atheist too.....

Wednesday, June 03, 2009

From Unreality Magzine...

I was just stumbling through various sites and got this article. And felt that I should shares with you. The article is named as :

The 10 Most Visually Stunning Movies of the Last 10 Years

According to Publisher these are the movies which change the way we view movies. These movies are visually unforgettable with their heavily loaded graphics magics.

And from video codec point of view also, the movies listed here are great test vectors for video compression encoder tools. Specially like 300, The Matrix Reloaded, Transformer.


Just check this article and see the movie list .... with some movie snaps...
If you didn't watch those...update your 'must watching movie' list.. ;)

And on this list ... I want to add some more movies like
1) The Lord Of The Rings
2) The Fountain (it is in my 'must watching movie' list, I saw it's trailer and that's awesome )

What's your views say........
Want to add some more movies ...