转:X264中Color Space Converter问题, 附解决图像倒立代码

X264中Color Space Converter问题, 附解决图像倒立代码

X264的编码输入一般以I420格式为主,但是x264也支持了RGB24,RGB32及YUVU,YV12(晕~~

就YUV都整出了这么多大同小异格式来)等格式,当然支持的意思就是x264里
面把这些格式转换成了I420了,具体转换工作(Color Space Converter)在csp.c里完成.

   但是在windows下,RGB的存放顺序是upside down,如果直接使用rgb_to_i420,则图像是
倒立的.因此需要修改,以便图像不再倒立。
   具体修改就是
   #define RGB_TO_I420( name, POS_R, POS_G, POS_B, S_RGB ) /
   static void name( x264_frame_t *frm, x264_image_t *img, /
                  int i_width, int i_height )           /
{                                                       /
    uint8_t *src = img->plane[0]+i_width*(i_height-1)*3;/
    int     i_src= img->i_stride[0];                    /
    int     i_y  = frm->i_stride[0];                    /
    uint8_t *y   = frm->plane[0];                       /
    uint8_t *u   = frm->plane[1];                       /
    uint8_t *v   = frm->plane[2];                       /
                                                        /
    if( img->i_csp & X264_CSP_VFLIP )                   /
    {                                                   /
        src += ( i_height - 1 ) * i_src;                /
        i_src = -i_src;                                 /
    }                                                   /
                                                        /
    for(  ; i_height > 0; i_height -= 2 )               /
    {                                                   /
        uint8_t *ss = src;                              /
        uint8_t *yy = y;                                /
        uint8_t *uu = u;                                /
        uint8_t *vv = v;                                /
        int w;                                          /
                                                        /
        for( w = i_width; w > 0; w -= 2 )               /
        {                                               /
            int cr = 0,cg = 0,cb = 0;                   /
            int r, g, b;                                /
                                                        /
            /* Luma */                                  /
            cr = r = *(ss+POS_R);                       / 
            cg = g = *(ss+POS_G);                       / 
            cb = b = *(ss+POS_B);   /
                                                        /
            yy[0] = Y_ADD + ((Y_R * r + Y_G * g + Y_B * b) >> BITS);    /
                                                       
            cr+= r = *(ss+POS_R-i_src);                 /
            cg+= g = *(ss+POS_G-i_src);                 /
            cb+= b = *(ss+POS_B-i_src);                 /
            yy[i_y] = Y_ADD + ((Y_R * r + Y_G * g + Y_B * b) >> BITS);  /
            yy++;                                       /               
     ss += S_RGB;                                /
                                                        /
            cr+= r = *(ss+POS_R);                       / 
            cg+= g = *(ss+POS_G);                       / 
            cb+= b = *(ss+POS_B);                       /
                                                        /
            yy[0] = Y_ADD + ((Y_R * r + Y_G * g + Y_B * b) >> BITS);    /
                                                        /
            cr+= r = *(ss+POS_R-i_src);                 /
            cg+= g = *(ss+POS_G-i_src);                 / 
            cb+= b = *(ss+POS_B-i_src);                 /
            yy[i_y] = Y_ADD + ((Y_R * r + Y_G * g + Y_B * b) >> BITS);  /
            yy++;                                       /
            ss += S_RGB;      /
                                                        /
            /* Chroma */                                /
            *uu++ = (uint8_t)(U_ADD + ((-U_R * cr - U_G * cg + U_B * cb) >> (B
ITS+2)) ); /
            *vv++ = (uint8_t)(V_ADD + (( V_R * cr - V_G * cg - V_B * cb) >> (B
ITS+2)) ); /
        }                                               /
                                                        /
        src -= 2*i_src;                                 /
        y += 2*frm->i_stride[0];                        /
        u += frm->i_stride[1];                          /
        v += frm->i_stride[2];                          /
    }                                                   /
}
  
   这样就解决了windows下图像倒立的问题,实际上也是一段RGB24 ->YUV420的代码. 供
有需要的人参考.  

你可能感兴趣的:(转:X264中Color Space Converter问题, 附解决图像倒立代码)