Deep Copy of CMSmapleBuffer or CVImageBuffer

Hi I am currently working on an app which needs to capture a Video and at the same time should be able to take frames to blend them.

The problem I am having is that my frames coming from:

func captureOutput(
     captureOutput: AVCaptureOutput!,
     didOutputSampleBuffer sampleBuffer:
     CMSampleBuffer!,
     fromConnection connection: AVCaptureConnection!
)

will drop after blending about 10-12 frames. I tried blending every 10th frame but it will still drop after 10-12 blended frames.


I know that I should copy the CVImageBuffer to release the imageBuffer which I got using the following:

let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)


I also know that

func CMSampleBufferCreateCopy(
     _ allocator: CFAllocator?,
     _ sbuf: CMSampleBuffer,
     _ sbufCopyOut: UnsafeMutablePointer<CMSampleBuffer?>
) -> OSStatus

only creates shallow copies and won't help releasing the original imageBuffer


So my question is: How can I create a complete deep copy of a CMSmapleBuffer or CVImageBuffer?

I would like to use:

func CMSampleBufferCreate(
     _ allocator: CFAllocator?,
     _ dataBuffer: CMBlockBuffer?,
     _ dataReady: Bool,
     _ makeDataReadyCallback: CMSampleBufferMakeDataReadyCallback?,
     _ makeDataReadyRefcon: UnsafeMutablePointer<Void>,
     _ formatDescription: CMFormatDescription?,
     _ numSamples: CMItemCount,
     _ numSampleTimingEntries: CMItemCount,
     _ sampleTimingArray: UnsafePointer<CMSampleTimingInfo>,
     _ numSampleSizeEntries: CMItemCount,
     _ sampleSizeArray: UnsafePointer<Int>,
     _ sBufOut: UnsafeMutablePointer<CMSampleBuffer?>
) -> OSStatus

But the function is a bit overwhelming and I don't know where to get all the attributes from.

I am sure I can take a lot from the given CMSampleBuffer.


I've been looking for a solution for a couple of days now.

If you need more context please feel free to ask.

I hope somone can help.

Sorry, I haven't been following this forum for a while.

In case you have no solution yet I whipped this for you:


let duration = CMTime( value:10, timescale:600, flags:0, epoch:0)
let presentationTimeStamp = CMTime( value:0, timescale:600, flags:0, epoch:0)
let timingInfo = CMSampleTimingInfo( duration:duration, presentationTimeStamp:presentationTimeStamp, decodeTimeStamp:kCMTimeInvalid )
let timingInfoPtr = UnsafePointer<CMSampleTimingInfo>.alloc(1)
timingInfoPtr.memory = timingInfo
let blockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer)
let sampleSizePtr = UnsafePointer<Int>.alloc(1)
sampleSizePtr.memory = CMBlockBufferGetDataLength(blockBuffer)
let outputPtr = UnsafeMutablePointer<CMSampleBuffer?>.alloc(1)
let err = CMSampleBufferCreate( nil, blockBuffer, true, nil, nil, CMSampleBufferGetFormatDescription(sampleBuffer), 1, 1, timingInfoPtr, 1, sampleSizePtr, outputPtr )


No guaranteees whatsoever!

Deep Copy of CMSmapleBuffer or CVImageBuffer
 
 
Q