在 Swift returns 中创建 CMSampleBuffer 的副本 OSStatus -12743(媒体格式无效)
Creating copy of CMSampleBuffer in Swift returns OSStatus -12743 (Invalid Media Format)
我正在尝试执行 CMSampleBuffer
的深度克隆以存储 AVCaptureSession
的输出。当我 运行 函数 CMSampleBufferCreateForImageBuffer
时,我收到错误 kCMSampleBufferError_InvalidMediaFormat
(OSStatus -12743)
。我不明白我如何不匹配 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)")
}
}
}
}
}
我能够通过在新创建的图像缓冲区中创建格式描述并使用它来代替原始样本缓冲区中的格式描述来解决问题。不幸的是,虽然这解决了这里的问题,但格式描述不匹配并进一步导致问题。
我最近遇到了同样的问题。经过一些调查,CMVideoFormatDescriptionMatchesImageBuffer()
函数文档给出了一些见解。
This function uses the keys returned by CMVideoFormatDescriptionGetExtensionKeysCommonWithImageBuffers to compares the extensions of the given format description to the attachments of the given image buffer (if an attachment is absent in either it must be absent in both). It also checks kCMFormatDescriptionExtension_BytesPerRow against CVPixelBufferGetBytesPerRow, if applicable.
在我的例子中,我没有复制一些格式描述扩展作为复制像素缓冲区的 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);
}
}];
雷曼曼的答案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)
}
}
我正在尝试执行 CMSampleBuffer
的深度克隆以存储 AVCaptureSession
的输出。当我 运行 函数 CMSampleBufferCreateForImageBuffer
时,我收到错误 kCMSampleBufferError_InvalidMediaFormat
(OSStatus -12743)
。我不明白我如何不匹配 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)")
}
}
}
}
}
我能够通过在新创建的图像缓冲区中创建格式描述并使用它来代替原始样本缓冲区中的格式描述来解决问题。不幸的是,虽然这解决了这里的问题,但格式描述不匹配并进一步导致问题。
我最近遇到了同样的问题。经过一些调查,CMVideoFormatDescriptionMatchesImageBuffer()
函数文档给出了一些见解。
This function uses the keys returned by CMVideoFormatDescriptionGetExtensionKeysCommonWithImageBuffers to compares the extensions of the given format description to the attachments of the given image buffer (if an attachment is absent in either it must be absent in both). It also checks kCMFormatDescriptionExtension_BytesPerRow against CVPixelBufferGetBytesPerRow, if applicable.
在我的例子中,我没有复制一些格式描述扩展作为复制像素缓冲区的 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);
}
}];
雷曼曼的答案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)
}
}