test01:YUV转RGB

来源:互联网 发布:java线程安全集合类 编辑:程序博客网 时间:2024/05/21 06:49

YUV转RGB实验原理

由电视原理可知,亮度和色差信号的构成如下:

 

Y=0.2990R+0.5870G+0.1140B

 

R-Y=0.7010R-0.5870G-0.1140B

 

B-Y=-0.2990R-0.5870G+0.8860B

 

为了使色差信号的动态范围控制在0.5之间,需要进行归一化,对色差信号引入压缩系数。归一化后的色差信号为:

 

U=-0.1684R-0.3316G+0.5B

 

V=0.5R-0.4187G-0.0813B

易推得YUV到RGB空间的转换公式为:

R=Y+1.4075(V−128)

G=Y−0.3455(U−128)−0.7169(V−128)

B=Y+1.779(U−128)

色度格式

4:2:0格式是指色差信号U,V的取样频率为亮度信号取样频率的四分之一,在水平方向和垂直方向上的取样点数均为Y的一半。

RGB格式的视频按每个像素B、G、R的顺序储存数据;所占空间大小为Width*Height*Frame*3。

YUV格式的视频将整帧图像的Y打包存储,依次再存整帧的U、V,然后再是下一帧的数据;YUV格式按4:2:0取样的视频所占空间大小为Width*Height*Frame*1.5(Width*Height*Frame*+Width*Height*Frame*1/4+Width*Height*Frame*1/4)

YUV文件转换为RGB文件后,由于RGB文件的颜色是8bit量化,则数值不在0-255范围内的颜色会出现溢出。因此需对R,G,B做是否溢出的判断,超过255的就令其显示为255,小于0的就令其显示为0.

实验流程

1为两个文件开辟缓冲区。

2读取yuv文件,并写入。

3通过调用函数YUV2RGB将YUV文件转化为RGB文件。

4将RGB缓存写入文件中。

5释放缓冲区。

关键代码及其分析

main函数

[html] view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>    
  3. #include <malloc.h>  
  4. #include "yuv2rgb.h"  
  5.   
  6. #define u_int8_t    unsigned __int8  //一个字节  
  7. #define u_int       unsigned __int32  //4个字节  
  8. #define u_int32_t   unsigned __int32  
  9. #define FALSE       false  
  10. #define TRUE        true  
  11. //为了防止不同编译系统的int语法不同  
  12. int main(int argc, char* argv[])  
  13. {  
  14.     /* variables controlable from command line */  
  15.     u_int frameWidth = 352;         /* --width=<uint> */  
  16.     u_int frameHeight = 240;        /* --height=<uint> */  
  17.     bool flip = TRUE;               /* --flip */  
  18.     unsigned int i;  
  19.   
  20.     /* internal variables */  
  21.     char* rgbFileName = NULL;  
  22.     char* yuvFileName = NULL;  
  23.     FILE* rgbFile = NULL;  
  24.     FILE* yuvFile = NULL;  
  25.     u_int8_t* rgbBuf = NULL;  
  26.     u_int8_t* yBuf = NULL;  
  27.     u_int8_t* uBuf = NULL;  
  28.     u_int8_t* vBuf = NULL;  
  29.     u_int32_t videoFramesWritten = 0;  
  30.   
  31.     /* begin process command line */  
  32.     /* point to the specified file names */  
  33.     yuvFileName = argv[1];  
  34.     rgbFileName = argv[2];  
  35.   
  36.     frameWidth = atoi(argv[3]);   //将字符型的256转换成整型  
  37.     frameHeight = atoi(argv[4]);  
  38.   
  39.     /* open the YUV file */  
  40.     yuvFile = fopen(yuvFileName, "rb");  
  41.     if (yuvFile == NULL)  
  42.     {  
  43.         printf("cannot find yuv file\n");  
  44.         exit(1);  
  45.     }  
  46.     else  
  47.     {  
  48.         printf("The input yuv file is %s\n", yuvFileName);  
  49.     }  
  50.   
  51.     /* open the RGB file */  
  52.     rgbFile = fopen(rgbFileName, "wb");  
  53.     if (rgbFile == NULL)  
  54.     {  
  55.         printf("cannot find rgb file\n");  
  56.         exit(1);  
  57.     }  
  58.     else  
  59.     {  
  60.         printf("The output rgb file is %s\n", rgbFileName);  
  61.     }  
  62.   
  63.     /* get an input buffer for a frame */  
  64.     rgbBuf = (u_int8_t*)malloc(frameWidth * frameHeight * 3);  // 向系统申请分配指定(frameWidth * frameHeight * 3)个字节的内存空间,并用指针rgbBuf指向它  
  65.   
  66.     /* get the output buffers for a frame */  
  67.     yBuf = (u_int8_t*)malloc(frameWidth * frameHeight);  
  68.     uBuf = (u_int8_t*)malloc((frameWidth * frameHeight) / 4);  
  69.     vBuf = (u_int8_t*)malloc((frameWidth * frameHeight) / 4);  
  70.   
  71.     if (rgbBuf == NULL || yBuf == NULL || uBuf == NULL || vBuf == NULL) //判断buffer是否已满  
  72.     {  
  73.         printf("no enought memory\n");  
  74.         exit(1);  
  75.     }  
  76.   
  77.     while (fread(yBuf, 1, frameWidth * frameHeight, yuvFile)  
  78.         && fread(uBuf, 1, frameWidth * frameHeight / 4, yuvFile)   
  79.         && fread(vBuf, 1, frameWidth * frameHeight / 4, yuvFile))  
  80.     {  
  81.         //若fread读入成功,则返回十几读取的数据量个数。若视频结束,fread读取失败则返回0,循环结束。  
  82.         if (YUV2RGB(frameWidth, frameHeight, rgbBuf, yBuf, uBuf, vBuf, flip))  
  83.         {  
  84.             printf("error");  
  85.             return 0;  
  86.         }  
  87.         fwrite(rgbBuf, 1, frameWidth * frameHeight * 3, rgbFile);  
  88.   
  89.   
  90.         printf("\r...%d", ++videoFramesWritten);  
  91.     }  
  92.   
  93.     printf("\n%u %ux%u video frames written\n",  
  94.         videoFramesWritten, frameWidth, frameHeight);  
  95.   
  96.   
  97.   
  98.     /* cleanup */  
  99.   
  100.     fclose(rgbFile);  
  101.     fclose(yuvFile);  
  102.   
  103.     if(rgbBuf!=NULL) free(rgbBuf);  
  104.     if (yBuf != NULL)free(yBuf);  
  105.     if (uBuf != NULL)free(uBuf);  
  106.     if (vBuf != NULL)free(vBuf);  
  107.   
  108.     return(0);  
  109. }  

yuv2rgb.cpp

[csharp] view plain copy
  1. /************************************************************************** 
  2. *                                                                        * 
  3. * This code is developed by Adam Li.  This software is an                * 
  4. * implementation of a part of one or more MPEG-4 Video tools as          * 
  5. * specified in ISO/IEC 14496-2 standard.  Those intending to use this    * 
  6. * software module in hardware or software products are advised that its  * 
  7. * use may infringe existing patents or copyrights, and any such use      * 
  8. * would be at such party's own risk.  The original developer of this     * 
  9. * software module and his/her company, and subsequent editors and their  * 
  10. * companies (including Project Mayo), will have no liability for use of  * 
  11. * this software or modifications or derivatives thereof.                 * 
  12. *                                                                        * 
  13. * Project Mayo gives users of the Codec a license to this software       * 
  14. * module or modifications thereof for use in hardware or software        * 
  15. * products claiming conformance to the MPEG-4 Video Standard as          * 
  16. * described in the Open DivX license.                                    * 
  17. *                                                                        * 
  18. * The complete Open DivX license can be found at                         * 
  19. * http://www.projectmayo.com/opendivx/license.php .                      * 
  20. *                                                                        * 
  21. **************************************************************************/  
  22.   
  23. /************************************************************************** 
  24. * 
  25. *  rgb2yuv.c, 24-bit RGB bitmap to YUV converter 
  26. * 
  27. *  Copyright (C) 2001  Project Mayo 
  28. * 
  29. *  Adam Li 
  30. * 
  31. *  DivX Advance Research Center <darc@projectmayo.com> 
  32. * 
  33. **************************************************************************/  
  34.   
  35. /* This file contains RGB to YUV transformation functions.                */  
  36.  
  37. #include "stdlib.h"  
  38. #include "yuv2rgb.h"  
  39.   
  40. static float YUVRGB14075[256];  
  41. static float YUVRGB03455[256], YUVRGB07169[256];  
  42. static float YUVRGB1779[256];  
  43.   
  44. int YUV2RGB(int width, int height, void *rgb_out, void *y_in, void *u_in, void *v_in, int flip)  
  45. {  
  46.     static int init_done = 0;  
  47.   
  48.     long i, j, size;  
  49.     float r1, g1, b1;  
  50.     unsigned char *r, *g, *b;  
  51.     unsigned char *y, *u, *v;  
  52.     unsigned char *y_buffer, *u_buffer, *v_buffer, *rgb_buffer;  
  53.     unsigned char *sub_u_buf, *sub_v_buf;  
  54.   
  55.     if (init_done == 0)  
  56.     {  
  57.         InitLookupTable();  
  58.         init_done = 1;  
  59.     }  
  60.   
  61.     // 检查width和height能否被2除尽  
  62.     if ((width % 2) || (height % 2)) return 1;  
  63.     size = width*height;  
  64.   
  65.     // allocate memory分配内存  
  66.     y_buffer = (unsigned char *)y_in;  
  67.     u_buffer = (unsigned char *)u_in;  
  68.     v_buffer = (unsigned char *)v_in;  
  69.     rgb_buffer = (unsigned char *)rgb_out;  
  70.     sub_u_buf = (unsigned char *)malloc(size);  
  71.     sub_v_buf = (unsigned char *)malloc(size);  
  72.   
  73.     for (i = 0; i<height; i++)  
  74.     {  
  75.         for (j = 0; j<width; j++)  
  76.         {  
  77.             *(sub_u_buf + i*width + j) = *(u_buffer + (i / 2)*(width / 2) + j / 2);  
  78.             *(sub_v_buf + i*width + j) = *(v_buffer + (i / 2)*(width / 2) + j / 2);  
  79.         }  
  80.     }  
  81.   
  82.     b = rgb_buffer;//RGB格式文件储存时顺序为倒序  
  83.     y = y_buffer;  
  84.     u = sub_u_buf;  
  85.     v = sub_v_buf;  
  86.   
  87.     // convert YUV to RGB      
  88.     for (i = 0; i<height; i++)  
  89.     {  
  90.         for (j = 0; j<width; j++)  
  91.         {  
  92.             g = b + 1;  
  93.             r = b + 2;  
  94.             r1 = *y + YUVRGB14075[*v];  
  95.             g1 = *y - YUVRGB03455[*u] - YUVRGB07169[*v];  
  96.             b1 = *y + YUVRGB1779[*u];  
  97.             *r = (r1>0 ? (r1>255 ? 255 : (unsigned char)r1) : 0);  
  98.             *g = (g1>0 ? (g1>255 ? 255 : (unsigned char)g1) : 0);  
  99.             *b = (b1>0 ? (b1>255 ? 255 : (unsigned char)b1) : 0);  
  100.             b = b + 3;  
  101.             y++;  
  102.             u++;  
  103.             v++;  
  104.         }  
  105.     }  
  106.         if (sub_u_buf!=NULL) free(sub_u_buf);  
  107.         if (sub_v_buf!=NULL) free(sub_v_buf);  
  108.   
  109.   
  110.   
  111.     return 0;  
  112. }  
  113.   
  114.   
  115. void InitLookupTable()  
  116. {  
  117.     int i;  
  118.   
  119.     for (i = 0; i < 256; i++) YUVRGB14075[i] = (float)1.4075 * (i - 128);  
  120.     for (i = 0; i < 256; i++) YUVRGB03455[i] = (float)0.3455 * (i - 128);  
  121.     for (i = 0; i < 256; i++) YUVRGB07169[i] = (float)0.7169 * (i - 128);  
  122.     for (i = 0; i < 256; i++) YUVRGB1779[i] = (float)1.779 * (i - 128);  
  123.   
  124. }  

打开项目属性窗口,通过设置工作目录、命令参数完成主函数的参数输入。


原YUV文件和生成的RGB重新转化成的YUV文件进行对比



实验结果

经过两次转化后的YUV文件与原YUV文件基本一致。