## Re: [xine-devel] Converting yuv to rgb

 Re: [xine-devel] Converting yuv to rgb From: Marcel Janssen - 2007-07-29 22:31:37 ```On Sunday 29 July 2007, Bob van Loosen wrote: > R = 1.164(Y - 16) + 1.596(V - 128)*/ > G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) > B = 1.164(Y - 16) + 2.018(U - 128) Note that these are only correct for RGB of the video, not for the LED's. If you would like the LED's to show the same colour as your video, than you'd need an extra transformation as mentioned earlier. The basic principle will still work without the transformations, but colours probably look different. > Next the colors are increased, this looks a lot better since the average > color of a given picture is usually a combination > of red, green and blue and this can look a lot like gray. > > average = (R + G + B / 3; (R + G + B)/ 3 > There is only one problem with this setup, when there is a scene which > is dark gray or has some white pixels and a lot of black pixels, > the lights turn dark green, I think the problem is in the YUV to RGB > translation and is amplified by the multiplication. How do the lights look like if you take a gray video and increase it's intensity slowly ? The light colour should stay the same than. If the colour shifts (which I expect) there are a few causes : 1) white point not correct of the LEDs. I think you like the LED's the have the exact same white point as your screen. 2) the problem of using YUV to RGB conversion and using this RGB for your LED's although it should first be transformed from one RGB space to another. This is probably the reason why you think the YUV to RGB conversion is wrong (it basically is :-)). You should transform the RGBscreen to RGBleds by a 3x3 matrix or better transform YUV directly to the LED RGB space. 3) gamma of your LEDs+controller. Since it's PWM, it should be linear I think. Since you don't have a colorimeter, you can try to match your LED white point to your screen (showing white of course). Than decrease your screen intensity and make sure the LEDs match over the whole range. You could make a lookup table or try to calculate a matrix from the resulting RGB values for each grey level. In case you live near Heerlen, you can borrow a colorimeter from me. Regards, Marcel ```

 [xine-devel] Converting yuv to rgb From: Bob van Loosen - 2007-06-21 18:22:59 ```Hi, I'm implementing DIY ambilight in xine in video_out.c, right now I'm reading the yuv values in the video_frame_t structure and converting them with some calculations I found on the net but it isn't very accurate. I would like to use xine's yuv2rgb funtions in yuv2rgb.c. However I'm having a hard time figuring out how to use these functions, I hope someone can give me a nudge in the right direction. Cheers, Bob. ```
 Re: [xine-devel] Converting yuv to rgb From: Marcel Janssen - 2007-07-28 13:13:41 ```Hello Bob, > I'm implementing DIY ambilight in xine in video_out.c, right now I'm > reading the yuv values in the video_frame_t structure and converting > them with some calculations I found on the net but it isn't very accurate. Nice idea :-) What do you mean by not accurate ? I assume you use RGB LED's for you lighting. You will need to calibrate the system, meaning that you need to know the colour point of the primaries of both your screen and your LED's and their white point. Than you can do a transformation (best in XYZ colour space using a 3x3 matrix I think) and finally back to RGB for your LED's. You will also need to involve the gamma curves in the transformation. In case you have a colorimeter, you could measure the primaries and gamma. In case you don't have, you may just get the supplier data on those primary colours (will be less accurate) Check http://www.brucelindbloom.com for additional math and the earlier mentioned transformations. regards, Marcel ```
 Re: [xine-devel] Converting yuv to rgb From: Bob van Loosen - 2007-07-29 18:51:33 ```Hi Marcel, > What do you mean by not accurate ? > The algorithm I'm currently using calculates the average Y, U and V pixels from a given area of the screen, from those YUV values the RGB values are calculated using the following formula: R = 1.164(Y - 16) + 1.596(V - 128)*/ G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) B = 1.164(Y - 16) + 2.018(U - 128) Or in C: R = rintf((1.164 * ((float)(ambiY[0] - 16))) + (1.596 * ((float)(ambiV[0] - 128)))); G = rintf((1.164 * ((float)(ambiY[0] - 16))) - (0.813 * ((float)(ambiV[0] - 128))) - (0.391 * ((float)(ambiU[0] - 128)))); B = rintf((1.164 * ((float)(ambiY[0] - 16))) + (2.018 * ((float)(ambiU[0] - 128)))); Next the colors are increased, this looks a lot better since the average color of a given picture is usually a combination of red, green and blue and this can look a lot like gray. average = (R + G + B / 3; R = ((10 - color) * average + R * color) / 10; G = ((10 - color) * average + G * color) / 10; B = ((10 - color) * average + B * color) / 10; Now there is only one problem left, in dark scenes the RGB values are pretty low, so the lights aren't really doing anything, to fix this the color with the highest value is multiplied so it is always 255 (with a maximum multiplication of 10) and the other two values are multiplied with the same value. float max_multiply = 10.0; int highest = 0; if(R > highest) highest = R; if(G > highest) highest = G; if(B > highest) highest = B; if (highest != 0) { float multiply = 255.0 / (float)highest; if (multiply > max_multiply) multiply = max_multiply; R = (int)((float)R * multiply); G = (int)((float)G * multiply); B = (int)((float)B * multiply); } There is only one problem with this setup, when there is a scene which is dark gray or has some white pixels and a lot of black pixels, the lights turn dark green, I think the problem is in the YUV to RGB translation and is amplified by the multiplication. > I assume you use RGB LED's for you lighting. You will need to calibrate the > system, meaning that you need to know the colour point of the primaries of > both your screen and your LED's and their white point. Than you can do a > transformation (best in XYZ colour space using a 3x3 matrix I think) and > finally back to RGB for your LED's. You will also need to involve the gamma > curves in the transformation. > I'm using 80 red leds, 60 green leds and 60 blue leds. The red leds need 2 volts and the green and blue leds need 3,8 volts. The current through the leds is 50 mA. They're hooked up to a six channel pwm controller made with a 20 mhz PIC16F84A which is connected to the serial port. > In case you have a colorimeter, you could measure the primaries and gamma. In > case you don't have, you may just get the supplier data on those primary > colours (will be less accurate) > Check http://www.brucelindbloom.com for additional math and the earlier > mentioned transformations. I don't have a colorimeter, but controlling the leds with pwm makes them pretty linear. Bob. ```
 Re: [xine-devel] Converting yuv to rgb From: Marcel Janssen - 2007-07-29 22:31:37 ```On Sunday 29 July 2007, Bob van Loosen wrote: > R = 1.164(Y - 16) + 1.596(V - 128)*/ > G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) > B = 1.164(Y - 16) + 2.018(U - 128) Note that these are only correct for RGB of the video, not for the LED's. If you would like the LED's to show the same colour as your video, than you'd need an extra transformation as mentioned earlier. The basic principle will still work without the transformations, but colours probably look different. > Next the colors are increased, this looks a lot better since the average > color of a given picture is usually a combination > of red, green and blue and this can look a lot like gray. > > average = (R + G + B / 3; (R + G + B)/ 3 > There is only one problem with this setup, when there is a scene which > is dark gray or has some white pixels and a lot of black pixels, > the lights turn dark green, I think the problem is in the YUV to RGB > translation and is amplified by the multiplication. How do the lights look like if you take a gray video and increase it's intensity slowly ? The light colour should stay the same than. If the colour shifts (which I expect) there are a few causes : 1) white point not correct of the LEDs. I think you like the LED's the have the exact same white point as your screen. 2) the problem of using YUV to RGB conversion and using this RGB for your LED's although it should first be transformed from one RGB space to another. This is probably the reason why you think the YUV to RGB conversion is wrong (it basically is :-)). You should transform the RGBscreen to RGBleds by a 3x3 matrix or better transform YUV directly to the LED RGB space. 3) gamma of your LEDs+controller. Since it's PWM, it should be linear I think. Since you don't have a colorimeter, you can try to match your LED white point to your screen (showing white of course). Than decrease your screen intensity and make sure the LEDs match over the whole range. You could make a lookup table or try to calculate a matrix from the resulting RGB values for each grey level. In case you live near Heerlen, you can borrow a colorimeter from me. Regards, Marcel ```
 Re: [xine-devel] Converting yuv to rgb From: Matthias Hopf - 2007-08-01 10:00:41 ```On Jul 29, 07 20:51:27 +0200, Bob van Loosen wrote: > > finally back to RGB for your LED's. You will also need to involve the gamma > > curves in the transformation. > > > I'm using 80 red leds, 60 green leds and 60 blue leds. The red leds need > 2 volts and the green and blue leds need 3,8 volts. > The current through the leds is 50 mA. > They're hooked up to a six channel pwm controller made with a 20 mhz > PIC16F84A which is connected to the serial port. So you have linear output, while the CRT has a gamma of approx. 2.3 (which is resembled in the electronics of LCDs as well). So you still have to apply gamma (or inverse gamma, depending on how you see it). > I don't have a colorimeter, but controlling the leds with pwm makes them > pretty linear. Actually almost exactly linear. But the human eye's sensitivity isn't linear at all... HTH Matthias -- Matthias Hopf __ __ __ Maxfeldstr. 5 / 90409 Nuernberg (_ | | (_ |__ mat@... Phone +49-911-74053-715 __) |_| __) |__ R & D http://www.mshopf.de ```
 Re: [xine-devel] Converting yuv to rgb From: Bob van Loosen - 2007-08-01 16:02:35 ```Matthias Hopf wrote: > On Jul 29, 07 20:51:27 +0200, Bob van Loosen wrote: > >>> finally back to RGB for your LED's. You will also need to involve the gamma >>> curves in the transformation. >>> >>> >> I'm using 80 red leds, 60 green leds and 60 blue leds. The red leds need >> 2 volts and the green and blue leds need 3,8 volts. >> The current through the leds is 50 mA. >> They're hooked up to a six channel pwm controller made with a 20 mhz >> PIC16F84A which is connected to the serial port. >> > > So you have linear output, while the CRT has a gamma of approx. 2.3 > (which is resembled in the electronics of LCDs as well). So you still > have to apply gamma (or inverse gamma, depending on how you see it). > I read this on Wikipedia: In PC video, images are encoded with a gamma of about 0.45 and decoded with a gamma of 2.2; in Mac systems, the corresponding typical values are 0.55 and 1.8. So after the YUV to RGB conversion, I have to apply a gamma of 2.2 to get linear output, is this correct? This still leaves the problem of a proper YUV to RGB conversion, because I still have the problem of the dark gray showing up as green. I would really like to use xine's own yuv2rgb functions, however I can't figure out how to use them. I could implement the ambilight in one of the video output modules, since the conversion is already done there. Greetings, Bob. ```
 Re: [xine-devel] Converting yuv to rgb From: Marcel Janssen - 2007-08-02 06:01:25 ```On Wednesday 01 August 2007, Bob van Loosen wrote: > So after the YUV to RGB conversion, I have to apply a gamma of 2.2 to > get linear output, is this correct? Your screen is not linear with respect to RGB (grey) levels. The screen shows a gamma curve of 2.2 when you apply linear grey scales. This means that when a picture is shown which has been encode with a gamma of 0.45 it will show correctly on your screen. In other words, if you direct the output from the video to your LEDs, you should apply a gamma of 2.2 on the LED curves. The trick is to make both respond equal. This also should be applied to colour (see below). This gamma correction should have quite a large impact on your system already I think. > This still leaves the problem of a proper YUV to RGB conversion, > because I still have the problem of the dark gray showing up as green. > I would really like to use xine's own yuv2rgb functions, however I can't > figure out how to use them. > I could implement the ambilight in one of the video output modules, > since the conversion is already done there. This conversion is fine but it's not the complete conversion you should do I believe. When transforming YUV to RGB, you get RGB values of the video (the actual movie or image). This does not represent the R,G,B colour of your screen or LED's but just the intensity levels of these colours. They are only correct if the screen white point is as was intended by the video material. For this purpose many people like to calibrate their monitor/TV/projector so that the colour in the video is shown correctly on the screen (and not blueish or greenish). R,G and B of the screen and LED's are also different in colour. You can see that by showing full a colour on your screen and look at what should be the same colour for your LED's. I think you will see some difference. Also, if you have 2 monitors and put them next to each other you will see notable differences in colour (often even when they are same brand and model). This means that to get equal colour you should calibrate them to be equal. If you take the CIE colour triangle for example (http://en.wikipedia.org/wiki/CIE_1931_color_space) and mark 3 dots for R,G and B for your screen and mark 3 different dots for your LED's you will find 2 triangles within this CIE chromaticity diagram. Within these 2 triangles you can match another triangle, which basically marks the area in which both your LED's and screen can produce equal colours. Ideally you would transform both your screen and your LED's to the new found triangle. Doing that means that colour points will shift a little to white and therefore become a little less saturated (that's the penalty). Getting back at the YUV conversion, the RGB values from this routine will produce a different white colour on your screen and your LEDs if they are not calibrated but are correct for the video material. RGB needs to be compensated for your screen and your LEDs. To transform, you will need a matrix. This matrix is usually calculated using CIE 1931 x,y (or XYZ) from the R,G and B points and their white point (XYZ). The catch is that you should know these x,y points. Since you can't measure them, you can try to find the values in the specifications. For most monitors you can find the values in the monitor profile, for your TV maybe in the TV specification. For the LED's you may find them from the spec as well. Note that these values are not entirely correct (mostly the spec contains a range). There is some good reading material on this matter on the web. Look for white balancing for example and you will find quite a lot of articles on it. Some info that may be a good read : http://www.brucelindbloom.com/index.html?ColorCheckerRGB.html http://en.wikipedia.org/wiki/White_balance regards, Marcel ```
 Re: [xine-devel] Converting yuv to rgb From: Simon Truss - 2007-08-02 17:27:07 ```Bob van Loosen wrote: > Matthias Hopf wrote: >> On Jul 29, 07 20:51:27 +0200, Bob van Loosen wrote: >> >>>> finally back to RGB for your LED's. You will also need to involve the gamma >>>> curves in the transformation. >>>> >>>> >>> I'm using 80 red leds, 60 green leds and 60 blue leds. The red leds need >>> 2 volts and the green and blue leds need 3,8 volts. >>> The current through the leds is 50 mA. >>> They're hooked up to a six channel pwm controller made with a 20 mhz >>> PIC16F84A which is connected to the serial port. >>> >> So you have linear output, while the CRT has a gamma of approx. 2.3 >> (which is resembled in the electronics of LCDs as well). So you still >> have to apply gamma (or inverse gamma, depending on how you see it). >> > I read this on Wikipedia: > > In PC video, images are encoded with a gamma of about 0.45 and decoded > with a gamma of 2.2; > in Mac systems, the corresponding typical values are 0.55 and 1.8. > > So after the YUV to RGB conversion, I have to apply a gamma of 2.2 to > get linear output, is this correct? > > This still leaves the problem of a proper YUV to RGB conversion, > because I still have the problem of the dark gray showing up as green. > I would really like to use xine's own yuv2rgb functions, however I can't > figure out how to use them. > I could implement the ambilight in one of the video output modules, > since the conversion is already done there. Marcel gave a good reply and I think only one important thing was missing. broadcast SDTV uses ITU REC601 standard, HDTV should use ITU REC709. That is the program material is transmitted with the assumption that the equipment being used at the receiver end uses these standards and thus will produce the correct output. These standards should provide you with the details you need to calibrate your system to. The sites listed elsewhere in the thread cover all the conversion you will need to perform. There are a number of cheap kits that can assist in colour calibration. They tend to use a cheap sensor that responds to white light and then you place colour filters in front of it to measure RGB, this should do well enough for black level and gamma. I personally would not trust them for anything else. Professional camera shops often have equipment for hire. A good colourimeter should be quite cheap to hire for a few days. just be sure you have calibration software first and you know how to use it. be aware also that some meters need switching to a different mode or just plain don't work with projection systems. I would expect that colour LEDs have a small number of narrow wavelengths that make up the colours rather than the broader spread found in natural light. I also recommend Bruce Lindblooms web site as others have mentioned. It is also worth checking out the Colour FAQ and Gamma FAQ on Charles Poynton's web page. http://www.poynton.com/ Simon ```
 Re: [xine-devel] Converting yuv to rgb From: Simon Truss - 2007-08-02 16:33:37 ```Bob van Loosen wrote: > Matthias Hopf wrote: >> On Jul 29, 07 20:51:27 +0200, Bob van Loosen wrote: >> >>>> finally back to RGB for your LED's. You will also need to involve the gamma >>>> curves in the transformation. >>>> >>>> >>> I'm using 80 red leds, 60 green leds and 60 blue leds. The red leds need >>> 2 volts and the green and blue leds need 3,8 volts. >>> The current through the leds is 50 mA. >>> They're hooked up to a six channel pwm controller made with a 20 mhz >>> PIC16F84A which is connected to the serial port. >>> >> So you have linear output, while the CRT has a gamma of approx. 2.3 >> (which is resembled in the electronics of LCDs as well). So you still >> have to apply gamma (or inverse gamma, depending on how you see it). >> > I read this on Wikipedia: > > In PC video, images are encoded with a gamma of about 0.45 and decoded > with a gamma of 2.2; > in Mac systems, the corresponding typical values are 0.55 and 1.8. > > So after the YUV to RGB conversion, I have to apply a gamma of 2.2 to > get linear output, is this correct? > > This still leaves the problem of a proper YUV to RGB conversion, > because I still have the problem of the dark gray showing up as green. > I would really like to use xine's own yuv2rgb functions, however I can't > figure out how to use them. > I could implement the ambilight in one of the video output modules, > since the conversion is already done there. Marcel gave a good reply and I think only one important thing was missing. broadcast SDTV uses ITU REC601 standard, HDTV should use ITU REC709. That is the program material is transmitted with the assumption that the equipment being used at the receiver end uses these standards and thus will produce the correct output. These standards should provide you with the details you need to calibrate your system to. The sites listed elsewhere in the thread cover all the conversion you will need to perform. There are a number of cheap kits that can assist in colour calibration. They tend to use a cheap sensor that responds to white light and then you place colour filters in front of it to measure RGB, this should do well enough for black level and gamma. I personally would not trust them for anything else. Professional camera shops often have equipment for hire. A good colourimeter should be quite cheap to hire for a few days. just be sure you have calibration software first and you know how to use it. be aware also that some meters need switching to a different mode or just plain don't work with projection systems. I would expect that colour LEDs have a small number of narrow wavelengths that make up the colours rather than the broader spread found in natural light. I also recommend Bruce Lindblooms web site as others have mentioned. It is also worth checking out the Colour FAQ and Gamma FAQ on Charles Poynton's web page. http://www.poynton.com/ Simon ```