CMVideoFormatDescriptionCreateFromH264ParameterSets in Swift

The CoreMedia/Video Toolbox API uses a lot of pointers which in Swift is confusing me!

The SPS, PPS data has come from my h264 stream and I’m simply trying to create a VFD for it.

I have tried the following and expected it to work, but I get a -12710 error (kCMFormatDescriptionError_InvalidParameter = -12710).

Here’s my playground.. What I’m I doing wrong??

// 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)

Source: ios

Leave a Reply