YUV格式初探
目录
一、YUV起源
常见的颜色模型中,RGB主要用于电子系统里表达和显示颜色,CMYK印刷四色模式用于彩色印刷,而YUV是被欧洲电视系统所采用的一种颜色编码方法。
使用YUV的优点有两个:
一.YUV主要用于优化彩色视频信号的传输,使其向后兼容老式黑白电视,这一特性用在于电视信号上。
二.YUV是数据总尺寸小于RGB格式(但用YUV444的话,和RGB888一样都是24bits)
彩色,Y分量,V分量,U分量二、YUV的类型
YUV细分的话有Y'UV,YUV,YCbCr,YPbPr等类型,其中YCbCr主要用于数字信号.
YCbCr 是在世界数字组织视频标准研制过程中作为ITU - R BT1601 建议的一部分, 其实是YUV经过Gamma的翻版。其中Y与YUV 中的Y含义一致, Cb , Cr 同样都指色彩, 只是在表示方法上不同而已。在YUV 家族中, YCbCr 是在计算机系统中应用最多的成员, 其应用领域很广泛,JPEG、MPEG,H264均采用此格式。
其中,Cr反映了RGB输入信号红色部分与RGB信号亮度值之间的差异,而Cb反映的是RGB输入信号蓝色部分与RGB信号亮度值之间的差异,此即所谓的色差信号
一般所讲的YUV大多是指YCbCr。以下用YUV指代YCbCr。
三、YUV的采样和存储格式
- 采样
YUV的采样有许多种,常用的有444,422,420,411等。
用三个图来直观地表示采集的方式吧,以黑点表示采样该像素点的Y分量,以空心圆圈表示采用该像素点的UV分量。
YUV常用采样
1.YUV 4:4:4采样,每一个Y对应一组UV分量。
· 4:4:4 Formats, 24 Bits per Pixel
2.YUV 4:2:2采样,每两个Y共用一组UV分量。
· 4:2:2 Formats, 16 Bits per Pixel
3.YUV 4:2:0采样,每四个Y共用一组UV分量。
· 4:2:0 Formats, 12 Bits per Pixel
4:4:4 means no downsampling of the chroma channels.
4:2:2 means 2:1 horizontal downsampling, with no vertical downsampling. Every scan line contains four Y samples for every two U or V samples.
4:2:0 means 2:1 horizontal downsampling, with 2:1 vertical downsampling.
4:1:1 means 4:1 horizontal downsampling, with no vertical downsampling. Every scan line contains four Y samples for every U or V sample.
4:1:1 sampling is less common than other formats, and is not discussed in detail in this article.
- 存储
YUV的存储格式有两大类:packed和planar,还有SemiPlanar。
对于packed的YUV格式,每个像素点的Y,U,V是连续交错存储的。
对于planar的YUV格式,先连续存储所有像素点的Y,紧接着存储所有像素点的U,随后是所有像素点的V。
对于SemiPlanar,则是先连续存储所有像素点的Y,再连续交错U和V。
根据不同的摆放顺序有如下几种常见的:
Packed方式
UYVY422(COLOR_FormatYUV422PackedPlanar)
其他的VYUY422,YUYV422,YVYU422只不过是数据矩阵中Y,U,V顺序差别:
VYUY422: V1 Y1 U1 Y2 V2 Y3 U2 Y4 ...
YUYV422(YUY2): Y1 U1 Y2 V1 Y3 U2 Y4 V2 ...
YVYU422: Y1 V1 Y2 U1 Y3 V2 Y4 U2 …
UYVY422
YUV420 Packet(COLOR_FormatYUV420PackedPlanar) 如下:
YUV420 packet每2X2的Y分量公用一个UV分量,并且将YUV打包到一个平面,如图所示
Planar和SemiPlanar方式
YUV422P(COLOR_FormatYUV422Planar) 如下:
每两个连续的Y分量公用一个UV空间,Y分量空间后面跟U分量平面,然后为V分量平面
YUV422SP(COLOR_FormatYUV422SemiPlanar) 如下:
每两个连续的Y分量公用一个UV空间,前面是Y分量空间,后面是U,V交错分量平面
YUV420P(COLOR_FormatYUV420Planar) 如下:
YUV420PYUV420SP( COLOR_FormatYUV420SemiPlanar) 如下:
YUV420SP其中YUV420P和YUV420SP根据U、V的顺序,又可分出2种格式。
YUV420P:U前V后即YUV420P,也叫I420,
V前U后,叫YV12(YV表示Y后面跟着V,12表示12bit)。
YUV420SP:U前V后叫NV12,
V前U后叫NV21。
数据排列如下:
I420: YYYYYYYY UU VV =>YUV420P
YV12: YYYYYYYY VV UU =>YUV420P
NV12: YYYYYYYY UVUV =>YUV420SP
NV21: YYYYYYYY VUVU =>YUV420SP
四、YUV的相关Enum
OpenNI2的原生像素格式定义如下:
typedef enum
{
// Depth
ONI_PIXEL_FORMAT_DEPTH_1_MM = 100,
ONI_PIXEL_FORMAT_DEPTH_100_UM = 101,
ONI_PIXEL_FORMAT_SHIFT_9_2 = 102,
ONI_PIXEL_FORMAT_SHIFT_9_3 = 103,
// Color
ONI_PIXEL_FORMAT_RGB888 = 200,
ONI_PIXEL_FORMAT_YUV422 = 201,
ONI_PIXEL_FORMAT_GRAY8 = 202,
ONI_PIXEL_FORMAT_GRAY16 = 203,
ONI_PIXEL_FORMAT_JPEG = 204,
ONI_PIXEL_FORMAT_YUYV = 205,
} OniPixelFormat;
AstraSDK的像素格式定义如下:
typedef enum {
ASTRA_PIXEL_FORMAT_UNKNOWN = 0,
ASTRA_PIXEL_FORMAT_DEPTH_MM = 100,
// color layouts
ASTRA_PIXEL_FORMAT_RGB888 = 200,
ASTRA_PIXEL_FORMAT_YUV422 = 201,
ASTRA_PIXEL_FORMAT_YUYV = 202,
ASTRA_PIXEL_FORMAT_RGBA = 203,
ASTRA_PIXEL_FORMAT_GRAY8 = 300,
ASTRA_PIXEL_FORMAT_GRAY16 = 301,
ASTRA_PIXEL_FORMAT_POINT = 400,
} astra_pixel_formats;
LINUX_VIDEODEV2(V4L2)的相关定义如下:
#define V4L2_PIX_FMT_YYUV v4l2_fourcc('Y','Y','U','V') /* 16 YUV 4:2:2 */
#define V4L2_PIX_FMT_YUYV v4l2_fourcc('Y','U','Y','V') /* 16 YUV 4:2:2 */
#define V4L2_PIX_FMT_UYVY v4l2_fourcc('U','Y','V','Y') /* 16 YUV 4:2:2 */
#define V4L2_PIX_FMT_YUV422P v4l2_fourcc('4','2','2','P') /* 16 YVU422 planar */
#define V4L2_PIX_FMT_NV12 v4l2_fourcc('N','V','1','2') /* 12 Y/CbCr 4:2:0 */
#define V4L2_PIX_FMT_NV21 v4l2_fourcc('N','V','2','1') /* 12 Y/CrCb 4:2:0 */
#define V4L2_PIX_FMT_YVU420 v4l2_fourcc('Y','V','1','2') /* 12 YVU 4:2:0 */
#define V4L2_PIX_FMT_YUV420 v4l2_fourcc('Y','U','1','2') /* 12 YUV 4:2:0 */
微软的YUV Video Subtypes相关定义如下:
GUID | Format | Sampling | Packed or planar | Bits per channel |
---|---|---|---|---|
MEDIASUBTYPE_AYUV | AYUV | 4:4:4 | Packed | 8 |
MEDIASUBTYPE_YUY2 | YUY2 | 4:2:2 | Packed | 8 |
MEDIASUBTYPE_UYVY | UYVY | 4:2:2 | Packed | 8 |
MEDIASUBTYPE_IMC1 | IMC1 | 4:2:0 | Planar | 8 |
MEDIASUBTYPE_IMC3 | IMC2 | 4:2:0 | Planar | 8 |
MEDIASUBTYPE_IMC2 | IMC3 | 4:2:0 | Planar | 8 |
MEDIASUBTYPE_IMC4 | IMC4 | 4:2:0 | Planar | 8 |
MEDIASUBTYPE_YV12 | YV12 | 4:2:0 | Planar | 8 |
MEDIASUBTYPE_NV12 | NV12 | 4:2:0 | Planar | 8 |
Android API的MediaCodecInfo.CodecCapabilities如下:
int COLOR_Format12bitRGB444
This constant was deprecated in API level 23. Use COLOR_Format24bitBGR888.
int COLOR_Format16bitARGB1555
This constant was deprecated in API level 23. Use COLOR_Format32bitABGR8888.
int COLOR_Format16bitARGB4444
This constant was deprecated in API level 23. Use COLOR_Format32bitABGR8888.
int COLOR_Format16bitBGR565
This constant was deprecated in API level 23. Use COLOR_Format16bitRGB565.
int COLOR_Format16bitRGB565
16 bits per pixel RGB color format, with 5-bit red & blue and 6-bit green component.
int COLOR_Format18BitBGR666
This constant was deprecated in API level 23. Use COLOR_Format24bitBGR888.
int COLOR_Format18bitARGB1665
This constant was deprecated in API level 23. Use COLOR_Format32bitABGR8888.
int COLOR_Format18bitRGB666
This constant was deprecated in API level 23. Use COLOR_Format24bitBGR888.
int COLOR_Format19bitARGB1666
This constant was deprecated in API level 23. Use COLOR_Format32bitABGR8888.
int COLOR_Format24BitABGR6666
This constant was deprecated in API level 23. Use COLOR_Format32bitABGR8888.
int COLOR_Format24BitARGB6666
This constant was deprecated in API level 23. Use COLOR_Format32bitABGR8888.
int COLOR_Format24bitARGB1887
This constant was deprecated in API level 23. Use COLOR_Format32bitABGR8888.
int COLOR_Format24bitBGR888
24 bits per pixel RGB color format, with 8-bit red, green & blue components.
int COLOR_Format24bitRGB888
This constant was deprecated in API level 23. Use COLOR_Format24bitBGR888 or COLOR_FormatRGBFlexible.
int COLOR_Format25bitARGB1888
This constant was deprecated in API level 23. Use COLOR_Format32bitABGR8888.
int COLOR_Format32bitABGR8888
32 bits per pixel RGBA color format, with 8-bit red, green, blue, and alpha components.
int COLOR_Format32bitARGB8888
This constant was deprecated in API level 23. Use COLOR_Format32bitABGR8888 Or COLOR_FormatRGBAFlexible.
int COLOR_Format32bitBGRA8888
This constant was deprecated in API level 23. Use COLOR_Format32bitABGR8888 Or COLOR_FormatRGBAFlexible.
int COLOR_Format8bitRGB332
This constant was deprecated in API level 23. Use COLOR_Format24bitBGR888.
int COLOR_FormatCbYCrY
This constant was deprecated in API level 23. Use COLOR_FormatYUV422Flexible.
int COLOR_FormatCrYCbY
This constant was deprecated in API level 23. Use COLOR_FormatYUV422Flexible.
int COLOR_FormatL16
16 bits per pixel, little-endian Y color format.
int COLOR_FormatL2
This constant was deprecated in API level 23. Use COLOR_FormatL8.
int COLOR_FormatL24
This constant was deprecated in API level 23. Use COLOR_FormatL16.
int COLOR_FormatL32
This constant was deprecated in API level 23. Use COLOR_FormatL16.
int COLOR_FormatL4
This constant was deprecated in API level 23. Use COLOR_FormatL8.
int COLOR_FormatL8
8 bits per pixel Y color format.
int COLOR_FormatMonochrome
This constant was deprecated in API level 23. Use COLOR_Format24bitBGR888.
int COLOR_FormatRGBAFlexible
Flexible 32 bits per pixel RGBA color format with 8-bit red, green, blue, and alpha components.
int COLOR_FormatRGBFlexible
Flexible 24 bits per pixel RGB color format with 8-bit red, green and blue components.
int COLOR_FormatRawBayer10bit
SMIA 10-bit Bayer format.
int COLOR_FormatRawBayer8bit
SMIA 8-bit Bayer format.
int COLOR_FormatRawBayer8bitcompressed
SMIA 8-bit compressed Bayer format.
int COLOR_FormatSurface
int COLOR_FormatYCbYCr
This constant was deprecated in API level 23. Use COLOR_FormatYUV422Flexible.
int COLOR_FormatYCrYCb
This constant was deprecated in API level 23. Use COLOR_FormatYUV422Flexible.
int COLOR_FormatYUV411PackedPlanar
This constant was deprecated in API level 23. Use COLOR_FormatYUV420Flexible.
int COLOR_FormatYUV411Planar
This constant was deprecated in API level 23. Use COLOR_FormatYUV420Flexible.
int COLOR_FormatYUV420Flexible
Flexible 12 bits per pixel, subsampled YUV color format with 8-bit chroma and luma components.
int COLOR_FormatYUV420PackedPlanar
This constant was deprecated in API level 23. Use COLOR_FormatYUV420Flexible.
int COLOR_FormatYUV420PackedSemiPlanar
This constant was deprecated in API level 23. Use COLOR_FormatYUV420Flexible.
int COLOR_FormatYUV420Planar
This constant was deprecated in API level 23. Use COLOR_FormatYUV420Flexible.
int COLOR_FormatYUV420SemiPlanar
This constant was deprecated in API level 23. Use COLOR_FormatYUV420Flexible.
int COLOR_FormatYUV422Flexible
Flexible 16 bits per pixel, subsampled YUV color format with 8-bit chroma and luma components.
int COLOR_FormatYUV422PackedPlanar
This constant was deprecated in API level 23. Use COLOR_FormatYUV422Flexible.
int COLOR_FormatYUV422PackedSemiPlanar
This constant was deprecated in API level 23. Use COLOR_FormatYUV422Flexible.
int COLOR_FormatYUV422Planar
This constant was deprecated in API level 23. Use COLOR_FormatYUV422Flexible.
int COLOR_FormatYUV422SemiPlanar
This constant was deprecated in API level 23. Use COLOR_FormatYUV422Flexible.
int COLOR_FormatYUV444Flexible
Flexible 24 bits per pixel YUV color format with 8-bit chroma and luma components.
int COLOR_FormatYUV444Interleaved
This constant was deprecated in API level 23. Use COLOR_FormatYUV444Flexible.
int COLOR_QCOM_FormatYUV420SemiPlanar
This constant was deprecated in API level 23. Use COLOR_FormatYUV420Flexible.
int COLOR_TI_FormatYUV420PackedSemiPlanar
This constant was deprecated in API level 23. Use COLOR_FormatYUV420Flexible.
COLOR_FormatYUV420Flexible
这里简单谈谈COLOR_FormatYUV420Flexible,YUV420Flexible并不是一种确定的YUV420格式,而是包含COLOR_FormatYUV411Planar, COLOR_FormatYUV411PackedPlanar, COLOR_FormatYUV420Planar, COLOR_FormatYUV420PackedPlanar, COLOR_FormatYUV420SemiPlanar和COLOR_FormatYUV420PackedSemiPlanar。
在API 21引入YUV420Flexible的同时,它所包含的这些格式都deprecated掉了。
五、YUV与RGB转换
Digital Y′CbCr (8 bits per sample) is derived from analog R'G'B' as follows:
将系数都除以255以后,得到以下公式:
1. <u>RGB 转换成 YUV</u>
2.
3. Y = (0.257 * R) + (0.504 * G) + (0.098 * B) + 16
4.
5. Cr = V = (0.439 * R) - (0.368 * G) - (0.071 * B) + 128
6.
7. Cb = U = -( 0.148 * R) - (0.291 * G) + (0.439 * B) + 128
8.
9. <u>YUV 转换成 RGB</u>
10.
11. B = 1.164(Y - 16) + 2.018(U - 128)
12.
13. G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
14.
15. R = 1.164(Y - 16) + 1.596(V - 128)
这些转换都可以使用SIMD指令集(比如NEON,MMX),来提高性能。
六、参考文献:
https://wiki.videolan.org/YUV/
https://msdn.microsoft.com/en-us/library/aa904813(VS.80).aspx
https://en.wikipedia.org/wiki/YUV
https://en.wikipedia.org/wiki/YCbCr
https://developer.android.com/reference/android/media/MediaCodecInfo.CodecCapabilities.html
http://blog.csdn.net/jumper511/article/details/21719313
图侵删