Skip to content

Instantly share code, notes, and snippets.

@SpencerKaiser
Last active May 25, 2021 13:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SpencerKaiser/3908ab1c661dfe87fa87bf4f22f638dc to your computer and use it in GitHub Desktop.
Save SpencerKaiser/3908ab1c661dfe87fa87bf4f22f638dc to your computer and use it in GitHub Desktop.
Swift implementation of a DJI-provided sample to convert vc_pixelbuffer_fastupload to a usable CVPixelBuffer
func createPixelBuffer(fromFrame frame: VideoFrameYUV) -> CVPixelBuffer? {
var initialPixelBuffer: CVPixelBuffer?
let _: CVReturn = CVPixelBufferCreate(kCFAllocatorDefault, Int(frame.width), Int(frame.height), kCVPixelFormatType_420YpCbCr8Planar, nil, &initialPixelBuffer)
guard let pixelBuffer = initialPixelBuffer,
CVPixelBufferLockBaseAddress(pixelBuffer, []) == kCVReturnSuccess
else {
return nil
}
let yPlaneWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer, 0)
let yPlaneHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer, 0)
let uPlaneWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer, 1)
let uPlaneHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer, 1)
let vPlaneWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer, 2)
let vPlaneHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer, 2)
let yDestination = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0)
memcpy(yDestination, frame.luma, yPlaneWidth * yPlaneHeight)
let uDestination = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1)
memcpy(uDestination, frame.chromaB, uPlaneWidth * uPlaneHeight)
let vDestination = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 2)
memcpy(vDestination, frame.chromaR, vPlaneWidth * vPlaneHeight)
CVPixelBufferUnlockBaseAddress(pixelBuffer, [])
return pixelBuffer
}
override func viewDidLoad() {
super.viewDidLoad()
registerApp()
}
// MARK: Registering app via the DJI SDK
func registerApp() {
DJISDKManager.registerApp(with: self)
DJIVideoPreviewer.instance()?.registFrameProcessor(self)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment