使用哪个 YCbCr 矩阵?BT.709 或 BT.601

Nan*_*gin 6 avfoundation color-space ios

我正在 iOS 上开发一个视频播放器项目。

它使用AVFoundation从视频文件中提取CVPixelBuffer,然后将该缓冲区作为纹理发送到 OpenGL。

概念验证代码的灵感来自Apple 的示例代码。AVFoundation 提供YCbCr 颜色空间中的每一帧,并且需要将其转换为RGB 以在OpenGL 中渲染。根据不同的 YCbCr 标准(例如ITU-R BT.709、ITU-R BT.601此变换似乎具有多个变换矩阵选项。示例代码通过以下代码确定使用哪一个:

CFTypeRef colorAttachments = CVBufferGetAttachment(pixelBuffer, kCVImageBufferYCbCrMatrixKey, NULL);
if (colorAttachments == kCVImageBufferYCbCrMatrix_ITU_R_601_4) {
    _preferredConversion = kColorConversion601;
}
else {
    _preferredConversion = kColorConversion709;
}
Run Code Online (Sandbox Code Playgroud)

但是,我使用的是 swift 并且返回colorAttachment是类型,Unmanaged<CFTypeRef>而常量kCVImageBufferYCbCrMatrix_ITU_R_601_4是类型,CFString因此它们不能直接相等。我做了一些研究,最后得到了:

CFEqual(colorAttachments, kCVImageBufferYCbCrMatrix_ITU_R_601_4) // returns false
CFEqual(colorAttachments, kCVImageBufferYCbCrMatrix_ITU_R_709_2) // returns false too!!
//-----------------------------------------
CFGetType(colorAttachments) // returns 1
CFStringGetType() // returns 7, note kCVImageBufferYCbCrMatrix_ITU_R_601_4 is of type CFString
// so I still can't check their equality 
// because the retrieved  colorAttachments is not of type CFString at all
Run Code Online (Sandbox Code Playgroud)

我通过对矩阵进行硬编码来逐一尝试了两个变换,结果(渲染场景)似乎与人眼没有区别,这是可以预测的,因为两个变换矩阵差别不大。

我的问题:

  1. 如何确定使用哪种变换?
  2. 如果无法解决 [1.],我可以硬编码其中一个吗?这样做的后果是什么?

Jan*_*egg 2

使用takeUnretainedValue()将为您提供一个CFTypeRef. 然后需要将其向下转换CFString为. 例如,您的代码可能如下所示:

if let colorAttachment = CVBufferGetAttachment(image, kCVImageBufferYCbCrMatrixKey, nil)?.takeUnretainedValue(),
    CFGetTypeID(colorAttachment) == CFStringGetTypeID() {
    let colorAttachmentString = colorAttachment as! CFString
    print(colorAttachmentString)
    print(colorAttachmentString == kCVImageBufferYCbCrMatrix_ITU_R_601_4)
    print(colorAttachmentString == kCVImageBufferYCbCrMatrix_ITU_R_709_2)
}
Run Code Online (Sandbox Code Playgroud)

哪个打印:

ITU_R_601_4
true
false
Run Code Online (Sandbox Code Playgroud)