CVPixelBufferCreateWithPlanarBytes返回-6661(kCVReturnInvalidArgument)
问题描述:
我正在尝试制作CVPixelBuffer
的深层副本。我收到错误kCVReturnInvalidArgument
或值-661。我已经验证了每个参数的类型和数组的长度,我无法找到我错误编码的内容。我希望有人会发现它。CVPixelBufferCreateWithPlanarBytes返回-6661(kCVReturnInvalidArgument)
下面是代码:
func clonePixelBuffer(pixelBuffer: CVPixelBuffer) -> CVPixelBuffer? {
CVPixelBufferLockBaseAddress(pixelBuffer, 0)
let height = CVPixelBufferGetHeight(pixelBuffer)
let width = CVPixelBufferGetWidth(pixelBuffer)
let numberOfPlanes = CVPixelBufferGetPlaneCount(pixelBuffer)
var planeBaseAddresses = [UnsafeMutablePointer<Void>]()
var planeWidths = [Int]()
var planeHeights = [Int]()
var planeBytesPerRows = [Int]()
for i in 0..<numberOfPlanes {
planeBaseAddresses.append(CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0))
planeWidths.append(CVPixelBufferGetWidthOfPlane(pixelBuffer, i))
planeHeights.append(CVPixelBufferGetHeightOfPlane(pixelBuffer, i))
planeBytesPerRows.append(CVPixelBufferGetHeightOfPlane(pixelBuffer, i))
}
let newPixelBuffer = UnsafeMutablePointer<CVPixelBuffer?>()
let status = CVPixelBufferCreateWithPlanarBytes(nil, width, height, kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, nil, 0, numberOfPlanes, &planeBaseAddresses, &planeWidths, &planeHeights, &planeBytesPerRows, nil, nil, nil, newPixelBuffer)
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0)
if status == noErr { <------ status = -6661
return newPixelBuffer.memory
}
return nil
}
答
不知道这是它,但有一个在bytesPerRows线copypaste错误 - 它说
planeBytesPerRows.append(CVPixelBufferGetHeightOfPlane(pixelBuffer, i))
而应该是
planeBytesPerRows.append(CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, i))
您也可以转发FormatType,而不是对其进行硬编码。
就我所了解的CVPixelBufferRef而言,这实际上不会复制像素缓冲区的数据,只会将一个PixelBuffer中的数据引用到另一个PixelBuffer中。你需要malloc你自己的飞机内存区域,引用该范围内的数据,然后提供一个免费的回调函数free()
,一旦CVPixelBufferRef被销毁,内存就会被释放。
我很惊讶地发现,编译器允许你将* Int *的数组/列表传递给期望'UnsafeMutablePointer'的参数。你确定这是自动和正确转换? –
Codo
是的,这是合法的。它在Using Cocoa和Objective C指南中使用Swift进行了解释。 – Rob