更改DJIVideoPreviewer的缓冲区格式

问题描述 投票:0回答:1

现在,我正在创建使用DJI飞机的视频帧并通过张量物体检测模型运行它的应用程序。

我设法让我的应用接收飞机上的镜架。

但是,帧类型为VPFrameTypeYUV420Planer。我想接收VPFrameTypeYUV420SemiPlanar的帧。这是因为我想从框架创建kCVPixelFormatType_420YpCbCr8BiPlanarFullRange cvPixelBuffer。

我尝试如下更改videopreviewr属性。

DJIVideoPreviewer.instance()?.frameOutputType = VPFrameTypeYUV420SemiPlaner

但是,我出错了。

我还尝试从YUV420Planer框架创建kCVPixelFormatType_420YpCbCr8BiPlanarFullRange cvPixelBuffer。但是,我不知道如何将chromaR,chormaB转换为UV。

func createPixelBuffer(fromFrame frame: VideoFrameYUV) -> CVPixelBuffer? {
        var initialPixelBuffer: CVPixelBuffer?
        let _: CVReturn = CVPixelBufferCreate(kCFAllocatorDefault, Int(frame.width), Int(frame.height), kCVPixelFormatType_420YpCbCr8BiPlanarFullRange, nil, &initialPixelBuffer)

        guard let pixelBuffer = initialPixelBuffer,
            CVPixelBufferLockBaseAddress(pixelBuffer, []) == kCVReturnSuccess
            else {
                return nil
        }

        let yPlaneWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer, 0)
        let yPlaneHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer, 0)

        let uvPlaneWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer, 1)
        let uvPlaneHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer, 1)


        let yDestination = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0)
        memcpy(yDestination, frame.luma, yPlaneWidth * yPlaneHeight)

        let uvDestination = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1)

       // let chrB = frame.chromaB.pointee
//        let chrR = frame.chromaR.pointee
        // I don't know how to convert to uv Buffer.

        CVPixelBufferUnlockBaseAddress(pixelBuffer, [])

        return pixelBuffer

是否有解决此问题的好方法?

swift yuv dji-sdk uint8t cvpixelbuffer
1个回答
0
投票
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
    }
© www.soinside.com 2019 - 2024. All rights reserved.