Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennyty/bcd298b5cb9c4d5d3b8a344e4d282b0c to your computer and use it in GitHub Desktop.
Save bennyty/bcd298b5cb9c4d5d3b8a344e4d282b0c to your computer and use it in GitHub Desktop.
let duration = CMTime( value:10, timescale:600, flags: CMTimeFlags(rawValue: 0), epoch:0)
let presentationTimeStamp = CMTime( value:0, timescale:600, flags:CMTimeFlags(rawValue: 0), epoch:0)
var timingInfo = CMSampleTimingInfo( duration:duration, presentationTimeStamp:presentationTimeStamp, decodeTimeStamp:kCMTimeInvalid )
// let potentialBlockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer)
// var imageBufferCopy = UnsafeMutablePointer<CVImageBuffer>.alloc(1)
guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
fatalError("Sample Buffer did not contain a pixel buffer")
}
// Get pixel buffer info
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
let bufferWidth = CVPixelBufferGetWidth(pixelBuffer);
let bufferHeight = CVPixelBufferGetHeight(pixelBuffer);
let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer);
let baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer);
// Copy the pixel buffer
var pixelBufferCopy: CVPixelBuffer?
let status = CVPixelBufferCreate(kCFAllocatorDefault, bufferWidth, bufferHeight, CVPixelBufferGetPixelFormatType(pixelBuffer), nil, &pixelBufferCopy);
guard status == 0 else {
fatalError("Failed to create CVPixelBuffer for cloning. Error code: \(status)")
}
CVPixelBufferLockBaseAddress(pixelBufferCopy!, 0);
let copyBaseAddress = CVPixelBufferGetBaseAddress(pixelBufferCopy!);
memcpy(copyBaseAddress, baseAddress, bufferHeight * bytesPerRow);
// let copy = NSData(bytes: baseAddress, length: bufferHeight * bytesPerRow)
// memcpy(copyBaseAddress, copy.bytes, bufferHeight * bytesPerRow);
print(pixelBuffer)
print("hi")
print(pixelBufferCopy)
let formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer)
// let formatDescription = CMFormatDescription()
var output: CMSampleBuffer?
// let outputPointer = UnsafeMutablePointer<CMSampleBuffer?>.alloc(1)
let err: OSStatus
// var sampleSize: Int = CMBlockBufferGetDataLength(blockBuffer)
err = CMSampleBufferCreateForImageBuffer(kCFAllocatorDefault, pixelBufferCopy!, true, nil, nil, formatDescription!, &timingInfo, &output)
// err = CMSampleBufferCreateima( kCFAllocatorDefault, blockBuffer, true, nil, nil, formatDescription, 1, 1, &timingInfo, 1, &sampleSize, &output )
guard err == 0 else {
fatalError("Error creating sample buffer for copying data from camera. Error code: \(err)")
}
@fukemy
Copy link

fukemy commented Jun 8, 2023

Hi, did you hardcode the timingInfo value?

@bennyty
Copy link
Author

bennyty commented Jun 8, 2023

@fukemy I asked on StackOverflow and received this very helpful answer: https://stackoverflow.com/a/38362579

Note that this was done nearly 7 years ago so iOS may have changed since then.

@fukemy
Copy link

fukemy commented Jun 9, 2023

your solution is the best i have found, other is using CIContext.render() method. It's took much CPU. Thanks a lot anyway

@fukemy
Copy link

fukemy commented Jun 9, 2023

I noted that Copy the pixel buffer will make slower, just make a create and do not copy any would be better

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment