Four Character Code
本文首发于Daniate的个人网站,文章链接:https://daniate.com/2018/06/22/119.html
Four Character Code
简称FourCC
,是由4个单字节字符构成的代码。
常被用于定义一些音频格式、图像或像素格式。
Apple 平台
在MacTypes.h
头文件中,有以下typedef
:
#if __LP64__
typedef unsigned int UInt32;
#else
typedef unsigned long UInt32;
#endif
typedef UInt32 FourCharCode;
typedef FourCharCode OSType;
可以看到,OSType
、FourCharCode
、UInt32
三个类型是相同的。
在CoreAudio
中,有这样一个typedef
:
/*!
@typedef AudioFormatID
@abstract A four char code indicating the general kind of data in the stream.
*/
typedef UInt32 AudioFormatID;
很明显,AudioFormatID
与FourCharCode
是等同的。
另外,也定义了一些类型为AudioFormatID
的枚举值,比如:
kAudioFormatLinearPCM = 'lpcm'
kAudioFormatMPEG4AAC = 'aac '
kAudioFormatMPEGLayer3 = '.mp3'
可以看出,它们的类型也就是FourCharCode
。
在CoreVideo
中,有一些以kCVPixelFormatType_
开头的枚举值,比如:
kCVPixelFormatType_32BGRA = 'BGRA'
kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange = '420v'
kCVPixelFormatType_420YpCbCr8BiPlanarFullRange = '420f'
它们的类型为OSType
,也即FourCharCode
。
为了将这些FourCC
转换为C字符串
('BGRA'
转换为"BGRA"
),可以使用这样的宏:
#define DGAppleFourCCToString(__FOURCC__) ((char [5])\
{\
(((__FOURCC__) & 0xFF000000) >> 24), \
(((__FOURCC__) & 0x00FF0000) >> 16), \
(((__FOURCC__) & 0x0000FF00) >> 8), \
((__FOURCC__) & 0x000000FF), \
0\
})
Linux 平台
在V4L2
中,具体是在videodev2.h
头文件中,定义了一些以V4L2_PIX_FMT_
开头的宏,比如:
V4L2_PIX_FMT_BGR32
V4L2_PIX_FMT_YUV420
V4L2_PIX_FMT_NV12
这些宏都使用了v4l2_fourcc
这个宏,其定义如下:
/* Four-character-code (FOURCC) */
#define v4l2_fourcc(a, b, c, d)\
((__u32)(a) | ((__u32)(b) << 8) | ((__u32)(c) << 16) | ((__u32)(d) << 24))
这些FourCC
的形式,与Apple
中的形式,是不相同的:一种是((__u32)(a) | ((__u32)(b) << 8) | ((__u32)(c) << 16) | ((__u32)(d) << 24))
,而另一种是'abcd'
。
为了将V4L2
中的FourCC
转换为C字符串
(v4l2_fourcc('B', 'G', 'R', 'A')
转换为"BGRA"
),可以使用这样的宏:
#define DGV4L2FourCC2String(__FOURCC__) ((char [5])\
{\
(__FOURCC__) & 0xFF,\
((__FOURCC__) >> 8) & 0xFF,\
((__FOURCC__) >> 16) & 0xFF,\
((__FOURCC__) >> 24) & 0xFF,\
0\
})
本作品由Daniate采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
评论已关闭