我正在尝试对CMSampleBuffer
执行深度克隆以存储AVCaptureSession
的输出。我运行函数kCMSampleBufferError_InvalidMediaFormat
时收到错误(OSStatus -12743)
CMSampleBufferCreateForImageBuffer
。我不知道我是如何错配CVImageBuffer
和CMSampleBuffer
格式说明的。谁知道我哪里出错了?她是我的测试代码。
func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
let allocator: CFAllocator = CFAllocatorGetDefault().takeRetainedValue()
func cloneImageBuffer(imageBuffer: CVImageBuffer!) -> CVImageBuffer? {
CVPixelBufferLockBaseAddress(imageBuffer, 0)
let bytesPerRow: size_t = CVPixelBufferGetBytesPerRow(imageBuffer)
let width: size_t = CVPixelBufferGetWidth(imageBuffer)
let height: size_t = CVPixelBufferGetHeight(imageBuffer)
let baseAddress = CVPixelBufferGetBaseAddress(imageBuffer)
let pixelFormatType = CVPixelBufferGetPixelFormatType(imageBuffer)
let data = NSMutableData(bytes: baseAddress, length: bytesPerRow * height)
CVPixelBufferUnlockBaseAddress(imageBuffer, 0)
var clonedImageBuffer: CVPixelBuffer?
let refCon = NSMutableData()
if CVPixelBufferCreateWithBytes(allocator, width, height, pixelFormatType, data.mutableBytes, bytesPerRow, nil, refCon.mutableBytes, nil, &clonedImageBuffer) == noErr {
return clonedImageBuffer
} else {
return nil
}
}
if let oldImageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) {
if let newImageBuffer = cloneImageBuffer(oldImageBuffer) {
if let formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer) {
let dataIsReady = CMSampleBufferDataIsReady(sampleBuffer)
let refCon = NSMutableData()
var timingInfo: CMSampleTimingInfo = kCMTimingInfoInvalid
let timingInfoSuccess = CMSampleBufferGetSampleTimingInfo(sampleBuffer, 0, &timingInfo)
if timingInfoSuccess == noErr {
var newSampleBuffer: CMSampleBuffer?
let success = CMSampleBufferCreateForImageBuffer(allocator, newImageBuffer, dataIsReady, nil, refCon.mutableBytes, formatDescription, &timingInfo, &newSampleBuffer)
if success == noErr {
bufferArray.append(newSampleBuffer!)
} else {
NSLog("Failed to create new image buffer. Error: \(success)")
}
} else {
NSLog("Failed to get timing info. Error: \(timingInfoSuccess)")
}
}
}
}
}
答案 0 :(得分:2)
我能够通过从新创建的图像缓冲区创建格式描述并使用它而不是原始样本缓冲区中的格式描述来解决问题。不幸的是,虽然这解决了这里的问题,但格式描述不匹配并导致问题进一步缩小。
答案 1 :(得分:0)
我最近遇到了同一问题。经过一番调查后,CMVideoFormatDescriptionMatchesImageBuffer()
函数文档给出了一些见解。
此函数使用CMVideoFormatDescriptionGetExtensionKeysCommonWithImageBuffers返回的键将给定格式描述的扩展名与给定图像缓冲区的附件进行比较(如果两个附件中均不存在附件,则两者均不存在)。如果适用,它还会根据CVPixelBufferGetBytesPerRow检查kCMFormatDescriptionExtension_BytesPerRow。
就我而言,我没有复制某些格式描述扩展名作为复制的像素缓冲区的CVBuffer附件。在创建新的CVPixelBufferRef之后运行这段代码对我来说解决了这个问题(Objective-C,但不难转换为Swift)
NSSet *commonKeys = [NSSet setWithArray:(NSArray *)CMVideoFormatDescriptionGetExtensionKeysCommonWithImageBuffers()];
NSDictionary *attachments = (NSDictionary *)CVBufferGetAttachments(originalPixelBuffer, kCVAttachmentMode_ShouldPropagate);
[attachments enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop)
{
if ([commonKeys containsObject:key])
{
CVBufferSetAttachment(pixelBufferCopy, (__bridge CFStringRef)(key), (__bridge CFTypeRef)(obj), kCVAttachmentMode_ShouldPropagate);
}
}];
attachments = (NSDictionary *)CVBufferGetAttachments(originalPixelBuffer, kCVAttachmentMode_ShouldNotPropagate);
[attachments enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop)
{
if ([commonKeys containsObject:key])
{
CVBufferSetAttachment(pixelBufferCopy, (__bridge CFStringRef)(key), (__bridge CFTypeRef)(obj), kCVAttachmentMode_ShouldNotPropagate);
}
}];
答案 2 :(得分:0)
Raymanman的答案的Swift版本。
let commonKeys = NSSet(array: CMVideoFormatDescriptionGetExtensionKeysCommonWithImageBuffers() as! [Any])
let propagatedAttachments = NSDictionary(dictionary: CVBufferGetAttachments(pixelBuffer, .shouldPropagate)!)
propagatedAttachments.enumerateKeysAndObjects { key, obj, stop in
if commonKeys.contains(key) {
CVBufferSetAttachment(outputPixelBuffer, key as! CFString, obj as AnyObject, .shouldPropagate)
}
}
let nonPropagatedAttachments = NSDictionary(dictionary: CVBufferGetAttachments(pixelBuffer, .shouldPropagate)!)
nonPropagatedAttachments.enumerateKeysAndObjects { key, obj, stop in
if commonKeys.contains(key) {
CVBufferSetAttachment(outputPixelBuffer, key as! CFString, obj as AnyObject, .shouldNotPropagate)
}
}