作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
CoreMedia/Video Toolbox API 使用了很多指针,这在 Swift 中让我很困惑!
SPS、PPS 数据来 self 的 h264 流,我只是想为它创建一个 VFD。
我已经尝试了以下方法并希望它能正常工作,但我收到了 -12710 错误 (kCMFormatDescriptionError_InvalidParameter = -12710)。
这是我的 Playground ..我做错了什么??
// uses CoreMedia Framework
import CoreMedia
// sps and pps variables
var spsData: [UInt8] = []
var ppsData: [UInt8] = []
// CMVideoFormatDescriptionCreateFromH264ParameterSets parameters
let parameterSetCount: Int = 2
var parameterSetPointers: UnsafePointer<UnsafePointer<UInt8>> = nil
var parameterSetSizes: UnsafePointer<Int> = nil
let NALUnitHeaderLength: Int32 = 4
var formatDescriptionOut: UnsafeMutablePointer<Unmanaged<CMFormatDescription>?> = nil
// set raw sps and pps data
let rawSPS: [UInt8] = [0x00,0x00,0x00,0x01,0x67,0x64,0x00,0x32,0xAC,0xB4,0x02,0x80,0x2D,0xD2,0xA4,0x00,0x00,0x0F,0xA4,0x00,0x03,0xA9,0x85,0x81,0x00,0x00,0x63,0x2E,0x80,0x01,0x65,0x0E,0xF7,0xBE,0x17,0x84,0x42,0x35]
let rawPPS: [UInt8] = [0x00,0x00,0x00,0x01,0x68,0xEE,0x3C,0xB0]
// extract sps data
spsData = Array(rawSPS[Int(NALUnitHeaderLength)..<rawSPS.count])
// extract pps data
ppsData = Array(rawPPS[Int(NALUnitHeaderLength)..<rawPPS.count])
let dataParamArray = [UnsafePointer<UInt8>(spsData), UnsafePointer<UInt8>(ppsData)]
parameterSetPointers = UnsafePointer(dataParamArray)
let sizeParamArray = [UnsafePointer<Int>(bitPattern: spsData.count), UnsafePointer<Int>(bitPattern: ppsData.count)]
parameterSetSizes = UnsafePointer(sizeParamArray)
// create video format description
let result: OSStatus = CMVideoFormatDescriptionCreateFromH264ParameterSets(kCFAllocatorDefault, parameterSetCount, parameterSetPointers, parameterSetSizes, NALUnitHeaderLength, formatDescriptionOut)
最佳答案
这是一个基于上面原始代码的工作示例。我必须对@ZENUAV 的答案进行额外更改(在代码中注明)才能使其正常工作。使用 Swift 1.2 在 Xcode 6.4 上测试。
注意:如果 h.264 NALU 恰好存储在 NSData
中,则可以使用 bytes
属性值代替 pointerSPS
/pointerPPS
.
// uses CoreMedia Framework
import CoreMedia
// sps and pps variables
var spsByteArray: [UInt8] = []
var ppsByteArray: [UInt8] = []
// CMVideoFormatDescriptionCreateFromH264ParameterSets parameters
let parameterSetCount: Int = 2
var parameterSetPointers: UnsafePointer<UnsafePointer<UInt8>> = nil
var parameterSetSizes: UnsafePointer<Int> = nil
let NALUnitHeaderLength: Int32 = 4
// important change (also, see usage below):
var formatDescriptionOut = UnsafeMutablePointer<Unmanaged<CMFormatDescription>?>.alloc(1)
let rawSPS: [UInt8] = [0x00,0x00,0x00,0x01,0x67,0x64,0x00,0x32,0xAC,0xB4,0x02,0x80,0x2D,0xD2,0xA4,0x00,0x00,0x0F,0xA4,0x00,0x03,0xA9,0x85,0x81,0x00,0x00,0x63,0x2E,0x80,0x01,0x65,0x0E,0xF7,0xBE,0x17,0x84,0x42,0x35]
let rawPPS: [UInt8] = [0x00,0x00,0x00,0x01,0x68,0xEE,0x3C,0xB0]
// extract sps data
spsByteArray = Array(rawSPS[Int(NALUnitHeaderLength)..<rawSPS.count])
// extract pps data
ppsByteArray = Array(rawPPS[Int(NALUnitHeaderLength)..<rawPPS.count])
let pointerSPS = UnsafePointer<UInt8>(spsByteArray)
let pointerPPS = UnsafePointer<UInt8>(ppsByteArray)
var dataParamArray = [pointerSPS, pointerPPS]
parameterSetPointers = UnsafePointer<UnsafePointer<UInt8>>(dataParamArray)
var sizeParamArray = [spsByteArray.count, ppsByteArray.count]
parameterSetSizes = UnsafePointer<Int>(sizeParamArray)
// create video format description
let result: OSStatus = CMVideoFormatDescriptionCreateFromH264ParameterSets(
kCFAllocatorDefault,
parameterSetCount,
parameterSetPointers,
parameterSetSizes,
NALUnitHeaderLength,
formatDescriptionOut // important change
)
关于ios - Swift 中的 CMVideoFormatDescriptionCreateFromH264ParameterSets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32005775/
我是一名优秀的程序员,十分优秀!