Last active
May 25, 2021 13:53
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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