VideoHelp Forum




+ Reply to Thread
Results 1 to 3 of 3
  1. Member
    Join Date
    Jan 2006
    Location
    Italy
    Search Comp PM
    Hi to all. I'm trying to convert decoded pictures from MPEG files (using libmpeg2) into RGB colospace. I need it because my video output module uses only RGB colorspace.

    Now, I've investigated enought to understand that the most people doesn't know about this conversion.

    Most rielable sources I've found are wikipedia and fourcc.org. On wiki http://en.wikipedia.org/wiki/YUV, while on fourcc.org http://www.fourcc.org/fccyvrgb.php.

    I've used the foucc.org formulas, but my resulting RGB values are out of range of 0-255!!! I've tried to cut-off negative values to 0 and >255 values to 255, but this doesn't works. I've tried also to scale full range values to 0-255, but this also doesn't works!!!

    I'm beginning to think that formulas are wrong.

    It take the most accurate the matrix proposed by wikipedia. So, calcultaing the inverse matrix for converting YUV to RGB, I should obtain the conversion matrix from YUV to RGB, isn't it?

    Well, I've found this page to calculate the inverse matrix of the conversion matrix proposed by wikipedia.og: http://lnx.arrigoamadori.com/CalcoloNumerico/Matrice3x3InC/Matrice3x3InC.htm

    The result was a little different from all sources I've found. But applying this formulas doesn't works so much...

    Anyone can clarify me about?

    P.S.: at the moment I'm not worried about performance. When the conversion will works, I will use a fixed point lookup table to achieve the result using only additions... but I'm going to be crazy about this quest...
    Quote Quote  
  2. Member vhelp's Avatar
    Join Date
    Mar 2001
    Location
    New York
    Search Comp PM
    >> Hi to all. I'm trying to convert decoded pictures from MPEG files
    >> (using libmpeg2) into RGB colospace. I need it because my video output
    >> module uses only RGB colorspace.


    Can you post some code snips as to how you are using this libmpeg2 to
    recieve the image data ??

    From reading this, it seems you are doing the following with this lib:

    ** opening MPEG-2 source files (ie, m2v or mpg)
    ** .. process to obtain image ..
    ** receive YUV data and saving to a file ? or inside a matrix array ?

    Maybe if we see how your source image is obtained, we can go from there


    Coefficients -- 0.299; 0.587; 0.114; ...

    Some people refer to it as 'coefficients'. But its all basically the same
    thing.

    Here is code snip from my YUV tool. For now, inside the current code
    snip, I just use generic terms.. (could change at any time of development)
    My code is very spagetti like. But have a look at what I mean below,
    regarding coefficients.

    In line 0, this is the main one that works for most of my methods of
    dealing with image conversion. But, I do my *own* RGB<->YUV conversions
    using the same formula pairs, so I don't have any issues as you seem to
    be having. But, I have the other coefficients for other conversions as
    well, depending on how I want to 'convert' - RGB->YUV first. The others'
    are lines 1 and 2 for the varialbe valCoef instance:

    Code:
      case valCoef of
        0: begin ituR:= 0.299; ituG:=0.587;  ituB:=0.114;  end;
        1: begin ituR:= 0.257; ituG:=0.504;  ituB:=0.098;  end;
        2: begin ituR:= 0.215; ituG:=0.7154; ituB:=0.0721; end;
      end;
    -vhelp 3748
    Quote Quote  
  3. Member
    Join Date
    Jan 2006
    Location
    Italy
    Search Comp PM
    I played a little with the code, and I found the right conversion formulas:

    #define GETR(y,u,v) ((1.164 * (y - 16)) + (1.596 * ((v) - 128)))
    #define GETG(y,u,v) ((1.164 * (y - 16)) - (0.813 * ((v) - 128)) - (0.391 * ((u) - 128)))
    #define GETB(y,u,v) ((1.164 * (y - 16)) + (2.018 * ((u) - 128)))

    Images seems to be in the right color using these formulas.

    These formulas won't be used, because floating point operations are too expansive, but they works well (that's because I should convert YV12 images for displaying on video; I've already an output module wich display automatically YV12 images using speicfic hardware present on video cards, but now I'm tring to do it manually).

    y, u, v values ranges are [0,255] (at least I think... I've not yet investigated on returned YUV images by libmpeg2, maybe YUV values have restricted ranges).

    Now, what I'm planning is to precalculate floating values, store results in a fixed point notation. These values are indexed in a structure like that:

    Code:
    typedef struct _img_csc_yuv2rgb_table_s {
            gva_fixed_t rgbY[256];                          /**< Red, Green, Blue luminance component. */
            gva_fixed_t rU[256];                            /**< Red U chrominance value. */
            gva_fixed_t rV[256];                            /**< Red V chrominance value. */
            gva_fixed_t gU[256];                            /**< Green U chrominance value. */
            gva_fixed_t gV[256];                            /**< Green V chrominance value. */
            gva_fixed_t bU[256];                            /**< Blue U chrominance value. */
            gva_fixed_t bV[256];                            /**< Blue V chrominance value. */
    } yuv2rgb_table_t;
    
    /* Table initialization code */
    
    for (y = 0; y < 256; y++)
      for (u = 0; u < 256; u++)
        for (v = 0; v < 256; v++) {
          /* initialize coefficient table using formula definition */
        }
    So converting a YV12 pixel should be sufficient adding 2/3 coefficient stored in the tables. The add operation is done on integer values, so it should be faster than using directly floating point operations.

    After 2h...

    This is done. The fixed point operation is good and it works well. But there is a problem: R, G, B components will go out of range [0, 255], so I have to clamp them...
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!