Zia
Zia

Reputation: 14722

How do I create a CMVideoFormatDescription object

I get a CVPixelBuffer from ARSessionDelegate.

There is another unchangeable part of my app where I need a CMSampleBuffer object. So I am trying to create a CMSampleBuffer out of CVPixelBuffer.

I am using this method to create CMSampleBuffer:

func CMSampleBufferCreateReadyWithImageBuffer(_ allocator: CFAllocator?, 
                                        _ imageBuffer: CVImageBuffer, 
                                        _ formatDescription: CMVideoFormatDescription, 
                                        _ sampleTiming: UnsafePointer<CMSampleTimingInfo>, 
                                        _ sBufOut: UnsafeMutablePointer<CMSampleBuffer?>) -> OSStatus

This function takes 5 parameters:

  1. CFAllocator - I believe this is not required.
  2. imageBuffer - This is the CVPixelBuffer.
  3. CMVideoFormatDescription - How do I correctly create this?
  4. sampleTiming: I could figure out how to create this using this answer.
  5. sBufOut - Just a pointer to the CMSampleBuffer object I want to create.

Here is my attempt at create a CMVideoFormatDescription:

    let w = CVPixelBufferGetWidth(pixelBuffer)
    let h = CVPixelBufferGetHeight(pixelBuffer)
    var format: CMVideoFormatDescription?
    CMVideoFormatDescriptionCreate(nil, kCMVideoCodecType_HEVC, Int32(w), Int32(h), nil, &format)

I am pretty sure I should not be hardcoding to kCMVideoCodecType_HEVCbut I am not sure how to figure out the codec type.

Upvotes: 1

Views: 1213

Answers (2)

Bogdan Razvan
Bogdan Razvan

Reputation: 1714

var formatDescription: CMVideoFormatDescription?
CMVideoFormatDescriptionCreateForImageBuffer(allocator: nil,
                                             imageBuffer: yourCVPixelBuffer,
                                             formatDescriptionOut: &formatDescription)

Upvotes: 0

NoHalfBits
NoHalfBits

Reputation: 622

Use CMVideoFormatDescriptionCreateForImageBuffer to create the format description directly from the CVPixelBuffer (which is a CVImageBuffer).

Upvotes: 2

Related Questions