Skip to content

Instantly share code, notes, and snippets.

@wotjd
Last active August 27, 2019 06:46
Show Gist options
  • Save wotjd/a6ed9ace5d72cd53ed62ddafd340735a to your computer and use it in GitHub Desktop.
Save wotjd/a6ed9ace5d72cd53ed62ddafd340735a to your computer and use it in GitHub Desktop.
get h264/hevc parameter set from cmsamplebuffer
import AVFoundation
// Get H264/HEVC Parameter Set
private func getParameterSet(_ sampleBuffer: CMSampleBuffer) -> Data {
var parameterSet = Data()
let codecStartCode = [UInt8](arrayLiteral: 0x00, 0x00, 0x00, 0x01)
parameterSet.append(contentsOf: codecStartCode)
let description = CMSampleBufferGetFormatDescription(sampleBuffer)!
var numParams = 0
CMVideoFormatDescriptionGetH264ParameterSetAtIndex(description, parameterSetIndex: 0, parameterSetPointerOut: nil, parameterSetSizeOut: nil, parameterSetCountOut: &numParams, nalUnitHeaderLengthOut: nil)
//CMVideoFormatDescriptionGetHEVCParameterSetAtIndex(description, parameterSetIndex: 0, parameterSetPointerOut: nil, parameterSetSizeOut: nil, parameterSetCountOut: &numParams, nalUnitHeaderLengthOut: nil)
// in H264 Stream, index 0 == sps, 1 == pps
// in HEVC Stream, index 0 == vps, 1 == sps, 2 == pps
for index in 0 ..< numParams {
var parameterSetPointer: UnsafePointer<UInt8>!
var parameterSetLength = 0
CMVideoFormatDescriptionGetH264ParameterSetAtIndex(description, parameterSetIndex: index, parameterSetPointerOut: &parameterSetPointer, parameterSetSizeOut: &parameterSetLength, parameterSetCountOut: nil, nalUnitHeaderLengthOut: nil)
parameterSet.append(contentsOf: codecStartCode)
parameterSet.append(parameterSetPointer!, count: parameterSetLength)
}
return parameterSet
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment