Last active
August 27, 2019 06:46
-
-
Save wotjd/a6ed9ace5d72cd53ed62ddafd340735a to your computer and use it in GitHub Desktop.
get h264/hevc parameter set from cmsamplebuffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: ¶meterSetPointer, parameterSetSizeOut: ¶meterSetLength, 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