Four Character Code

Four Character Code

Four Character Code简称FourCC,是由4个单字节字符构成的代码。

常被用于定义一些音频格式、图像或像素格式。

Apple 平台

MacTypes.h头文件中,有以下typedef

1
2
3
4
5
6
7
8
#if __LP64__
typedef unsigned int UInt32;
#else
typedef unsigned long UInt32;
#endif

typedef UInt32 FourCharCode;
typedef FourCharCode OSType;

可以看到,OSTypeFourCharCodeUInt32三个类型是相同的。

CoreAudio中,有这样一个typedef

1
2
3
4
5
/*!
@typedef AudioFormatID
@abstract A four char code indicating the general kind of data in the stream.
*/
typedef UInt32 AudioFormatID;

很明显,AudioFormatIDFourCharCode是等同的。

另外,也定义了一些类型为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"),可以使用这样的宏:

1
2
3
4
5
6
7
8
#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这个宏,其定义如下:

1
2
3
/*  Four-character-code (FOURCC) */
#define v4l2_fourcc(a, b, c, d)\
((__u32)(a) | ((__u32)(b) << 8) | ((__u32)(c) << 16) | ((__u32)(d) << 24))

V4L2-FourCC.png

这些FourCC的形式,与Apple中的形式,是不相同的:一种是((__u32)(a) | ((__u32)(b) << 8) | ((__u32)(c) << 16) | ((__u32)(d) << 24)),而另一种是'abcd'

为了将V4L2中的FourCC转换为C字符串v4l2_fourcc('B', 'G', 'R', 'A')转换为"BGRA"),可以使用这样的宏:

1
2
3
4
5
6
7
8
#define DGV4L2FourCC2String(__FOURCC__) ((char [5])\
{\
(__FOURCC__) & 0xFF,\
((__FOURCC__) >> 8) & 0xFF,\
((__FOURCC__) >> 16) & 0xFF,\
((__FOURCC__) >> 24) & 0xFF,\
0\
})

Four Character Code
https://daniate.github.io/2018/06/22/Four Character Code/
作者
Daniate
发布于
2018年6月22日
许可协议