Decoding frames with VTDecompressionSessionDecodeFrame fails with 12909 error

Viewed 297

I'm trying to decode CMSampleBuffer so I can analyze their pixel data. I keep getting error 12909 when I call VTDecompressionSessionDecodeFrame. This is all very new to me - any ideas where might be the problem?

Here's my code:

func decode(sampleBuffer: CMSampleBuffer) {
    let imageBufferAttributes: CFDictionary? = [kCVPixelBufferOpenGLESCompatibilityKey: true,
                                                kCVPixelBufferPixelFormatTypeKey:kCVPixelFormatType_32BGRA] as CFDictionary
    let formatDes = sampleBuffer.formatDescription!
    VTDecompressionSessionCreate(allocator: kCFAllocatorDefault,
                                 formatDescription: formatDes,
                                 decoderSpecification: nil,
                                 imageBufferAttributes: imageBufferAttributes,
                                 outputCallback: nil,
                                 decompressionSessionOut: &session)


    let flags: VTDecodeFrameFlags = []
    var flagOut: VTDecodeInfoFlags = []
    let canAccept = VTDecompressionSessionCanAcceptFormatDescription(session!,
                                                                     formatDescription: formatDes)
    print("Can accept: \(canAccept)") // true
    VTDecompressionSessionDecodeFrame(session!,
                                      sampleBuffer: sampleBuffer,
                                      flags: flags,
                                      infoFlagsOut: &flagOut)
    { status, infoFlags,imageBuffer, _ , _ in
        guard let imageBuffer = imageBuffer else {
            print("Error decoding. No image buffer. \(status)") // 12909
            return
        }
    }
}
1 Answers

I my application I created the CMSampleBuffer from a CMBlockBuffer. The CMBlockBuffer was created from a byte array extracted from a h.264 stream. When creating the CMBlockBuffer I wrote a false array size into the NALU header. That was causing the -12909 (Bad Data) error in my case.

Related