YUV420、YUV422、RGB24转换

//平面YUV422转平面RGB24
static void YUV422p_to_RGB24(unsigned char *yuv422[3], unsigned char *rgb24, int width, int height)
{
 int R,G,B,Y,U,V;
 int x,y;
 int nWidth = width>>1; //色度信号宽度
 for (y=0;y<height;y++)
 {
  for (x=0;x<width;x++)
  {
   Y = *(yuv422[0] + y*width + x);
   U = *(yuv422[1] + y*nWidth + (x>>1));
   V = *(yuv422[2] + y*nWidth + (x>>1));
   R = Y + 1.402*(V-128);
   G = Y - 0.34414*(U-128) - 0.71414*(V-128);
   B = Y + 1.772*(U-128);
   
   //防止越界
   if (R>255)R=255;
   if (R<0)R=0;
   if (G>255)G=255;
   if (G<0)G=0;
   if (B>255)B=255;
   if (B<0)B=0;
   
   *(rgb24 + ((height-y-1)*width + x)*3) = B;
   *(rgb24 + ((height-y-1)*width + x)*3 + 1) = G;
   *(rgb24 + ((height-y-1)*width + x)*3 + 2) = R;  
  }
 }
}

//平面YUV420转平面YUV422
static void YUV420p_to_YUV422p(unsigned char *yuv420[3], unsigned char *yuv422, int width, int height)  
{
 int x, y;
 //亮度信号Y复制
 int Ylen = width*height;
 memcpy(yuv422, yuv420[0], Ylen);
 //色度信号U复制
 unsigned char *pU422 = yuv422 + Ylen; //指向U的位置
 int Uwidth = width>>1; //422色度信号U宽度
 int Uheight = height>>1; //422色度信号U高度 
 for (y = 0; y < Uheight; y++) 
 {         
  memcpy(pU422 + y*width,          yuv420[1] + y*Uwidth, Uwidth);
  memcpy(pU422 + y*width + Uwidth, yuv420[1] + y*Uwidth, Uwidth);
 }
 //色度信号V复制
 unsigned char *pV422 = yuv422 + Ylen + (Ylen>>1); //指向V的位置
 int Vwidth = Uwidth; //422色度信号V宽度
 int Vheight = Uheight; //422色度信号U宽度
 for (y = 0; y < Vheight; y++) 
 {  
  memcpy(pV422 + y*width,          yuv420[2] + y*Vwidth, Vwidth);
  memcpy(pV422 + y*width + Vwidth, yuv420[2] + y*Vwidth, Vwidth);
 } 
}

//平面YUV420转RGB24
static void YUV420p_to_RGB24(unsigned char *yuv420[3], unsigned char *rgb24, int width, int height) 
{
//  int begin = GetTickCount();
 int R,G,B,Y,U,V;
 int x,y;
 int nWidth = width>>1; //色度信号宽度
 for (y=0;y<height;y++)
 {
  for (x=0;x<width;x++)
  {
   Y = *(yuv420[0] + y*width + x);
   U = *(yuv420[1] + ((y>>1)*nWidth) + (x>>1));
   V = *(yuv420[2] + ((y>>1)*nWidth) + (x>>1));
   R = Y + 1.402*(V-128);
   G = Y - 0.34414*(U-128) - 0.71414*(V-128);
   B = Y + 1.772*(U-128);

   //防止越界
   if (R>255)R=255;
   if (R<0)R=0;
   if (G>255)G=255;
   if (G<0)G=0;
   if (B>255)B=255;
   if (B<0)B=0;
   
   *(rgb24 + ((height-y-1)*width + x)*3) = B;
   *(rgb24 + ((height-y-1)*width + x)*3 + 1) = G;
   *(rgb24 + ((height-y-1)*width + x)*3 + 2) = R;
//    *(rgb24 + (y*width + x)*3) = B;
//    *(rgb24 + (y*width + x)*3 + 1) = G;
//    *(rgb24 + (y*width + x)*3 + 2) = R;   
  }
 }
}


static void yuv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
                              int width, int height)
{
    const uint8_t *p, *p1;
    uint8_t *cr, *cb, *lum1, *cr1, *cb1;
    uint8_t *lum;
    int w;

    p1 = src->data[0];
    lum1 = dst->data[0];
    cb1 = dst->data[1];
    cr1 = dst->data[2];

    for(;height >= 1; height -= 2) {
        p = p1;
        lum = lum1;
        cb = cb1;
        cr = cr1;
        for(w = width; w >= 2; w -= 2) {
            lum[0] = p[0];
            cb[0] = p[1];
            lum[1] = p[2];
            cr[0] = p[3];
            p += 4;
            lum += 2;
            cb++;
            cr++;
        }
        if (w) {
            lum[0] = p[0];
            cb[0] = p[1];
            cr[0] = p[3];
            cb++;
            cr++;
        }
        p1 += src->linesize[0];
        lum1 += dst->linesize[0];
        if (height>1) {
            p = p1;
            lum = lum1;
            for(w = width; w >= 2; w -= 2) {
                do{lum[0] = p[0];
                lum[1] = p[2];
                p += 4;
                lum += 2;}while(0);
            }
            if (w) {
                lum[0] = p[0];
            }
            p1 += src->linesize[0];
            lum1 += dst->linesize[0];
        }
        cb1 += dst->linesize[1];
        cr1 += dst->linesize[2];
    }
}






你可能感兴趣的:(YUV420、YUV422、RGB24转换)