对PPS和SPS参数进行硬编码时,无法使用VideoToolbox解压缩H.264帧

时间:2018-07-18 02:45:33

标签: swift h.264 codec video-toolbox

我写了一个H.264解码器,效果很好。但是出于性能原因,我试图在PPS和SPS参数中进行硬编码(它们永远不会改变)。

作为参考,它们是以10为底的[UInt8]:

  • SPS [103,66,0,40,244,5,1,236,128]
  • PPS [104,206,9,136]

奇怪的是,当我从接收到的帧中解码PPS和SPS参数并使用它们来创建格式描述和解压缩会话时,它的效果很好;但是当我硬编码它们时,它不起作用,并且在尝试解压缩帧时出现VT错误-12909(错误数据)。据我所知,字节数组是完全相同的。

要对其进行硬编码,我只是这样写了我的createFormatDescription:

func createFormatDescription(sps: [UInt8]?, pps: [UInt8]?) -> Bool {
    self.formatDesc = nil

    let sps = sps ?? [103, 66, 0, 40, 244, 5, 1, 236, 128] as [UInt8]
    let pps = pps ?? [104, 206, 9, 136] as [UInt8]

    let pointerSPS = UnsafePointer<UInt8>(sps)
    let pointerPPS = UnsafePointer<UInt8>(pps)

    let dataParamArray = [pointerSPS, pointerPPS]
    let parameterSetPointers = UnsafePointer<UnsafePointer<UInt8>>(dataParamArray)

    let sizeParamArray = [sps.count, pps.count]
    let parameterSetSizes = UnsafePointer<Int>(sizeParamArray)

    let status = CMVideoFormatDescriptionCreateFromH264ParameterSets(kCFAllocatorDefault, 2, parameterSetPointers, parameterSetSizes, 4, &formatDesc)

    if status == noErr {
        print("===== Format description created")
        return true
    } else {
        return false
    }
}

并在我的init()方法中调用它。除此之外,我没有进行任何更改。帧的解释方式完全相同。

作为参考,这是我的VT回调的样子:

private var callback: VTDecompressionOutputCallback = {(
    decompressionOutputRefCon: UnsafeMutableRawPointer?,
    sourceFrameRefCon: UnsafeMutableRawPointer?,
    status: OSStatus,
    infoFlags: VTDecodeInfoFlags,
    imageBuffer: CVPixelBuffer?,
    presentationTimeStamp: CMTime,
    duration: CMTime) in
    let decoder: VideoFrameDecoder = Unmanaged<VideoFrameDecoder>.fromOpaque(decompressionOutputRefCon!).takeUnretainedValue()
    if imageBuffer != nil && status == noErr {
        decoder.imageDecompressed(image: imageBuffer!)
    } else {
        decoder.delegate?.frameDecodingFailed()
        print("***** FAILED TO DECOMPRESS. VT ERROR \(status)")
    }
}

相当基本。重申一下,我总是收到“ VT ERROR -12909”

0 个答案:

没有答案