我正在iOS上做一个视频播放器项目。
它使用 AVFoundation 从视频文件中提取 CVPixelBuffer ,然后将该缓冲区作为纹理发送到OpenGL。
概念证明代码是受 苹果的示例代码 启发的.AVFoundation在YCbCr颜色空间中提供每个帧,需要将其转换为RGB以在OpenGL中呈现。根据不同的YCbCr标准(例如 国际电联-R BT.709、ITU BT.601 ),这种转换似乎有多个转换矩阵选项。示例代码通过以下代码确定要使用的代码:
CFTypeRef colorAttachments = CVBufferGetAttachment(pixelBuffer,kCVImageBufferYCbCrMatrixKey,NULL);if (colorAttachments == kCVImageBufferYCbCrMatrix_ITU_R_601_4) { _preferredConversion = kColorConversion601;}pixelBuffer= kColorConversion709;}
但是,我使用的是
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
我尝试了两个变换,通过硬编码矩阵和结果(渲染场景)似乎没有区别,这是可以预见的,因为这两个变换矩阵并没有太大的差别。
我的问题:
发布于 2018-01-31 14:44:33
使用
takeUnretainedValue()
将为您提供一个
CFTypeRef
。然后,这需要从
下浇
到
CFString
。例如,代码可能如下所示:
if let colorAttachment = CVBufferGetAttachment(image, kCVImageBufferYCbCrMatrixKey, nil)?.takeUnretainedValue(),
CFGetTypeID(colorAttachment) == CFStringGetTypeID() {
let colorAttachmentString = colorAttachment as! CFString