DJI VideoFrameYUV to CIImage conversion

时光毁灭记忆、已成空白 提交于 2021-01-29 07:10:45

问题


I have problem with VideoFrameYUV to CIImage conversion. I have tried HW accelerated method with:

CVPixelBufferRef pixelBuffer = frame->cv_pixelbuffer_fastupload;
CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];

but CIImage is always NULL.

I have tried SW way:

CVPixelBufferRef pixelBuffer = NULL;
CVReturn resulst = CVPixelBufferCreate(kCFAllocatorDefault,
                                       frame-> width,
                                       frame -> height,
                                       kCVPixelFormatType_420YpCbCr8Planar,
                                       NULL,
                                       &pixelBuffer);
if (kCVReturnSuccess != CVPixelBufferLockBaseAddress(pixelBuffer, 0) || pixelBuffer == NULL) {
    return;
}

long yPlaneWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer, 0);
long yPlaneHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer,0);
long uPlaneWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer, 1);
long uPlaneHeight = CVPixelBufferGetHeightOfPlane(pixelBuffer, 1);
long vPlaneWidth = CVPixelBufferGetWidthOfPlane(pixelBuffer, 2);
long vPlaneHeight =  CVPixelBufferGetHeightOfPlane(pixelBuffer, 2);
uint8_t* yDestination = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);
memcpy(yDestination, frame->luma, yPlaneWidth * yPlaneHeight);
uint8_t* uDestination = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1);
memcpy(uDestination, frame->chromaB, uPlaneWidth * uPlaneHeight);
uint8_t* vDestination = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 2);
memcpy(vDestination, frame->chromaR, vPlaneWidth * vPlaneHeight);
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);

[self log:[NSString stringWithFormat:@"Frame width: %@", @(frame-> width).stringValue]];

if(pixelBuffer == NULL) {
    [self log:@"PixelBuffer is NULL"];
    return;
}

if(CVPixelBufferGetPlaneCount(pixelBuffer) == 3) {
    [self log:@"y-cr-cb"];
}

if(CVPixelBufferGetPlaneCount(pixelBuffer) == 2) {
    [self log:@"y-cr-cb-bi"];
}

CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];

if(ciImage != NULL) {
    [self log:@"CIImage exist"];
} else {
    [self log:@"CIImage doesnt exist"];
}

Output is:

Frame width: 1280
y-cr-cb
CIImage doesnt exist
Frame width: 1280
y-cr-cb
CIImage doesnt exist
....

What am I doing wrong? Thanks.

来源:https://stackoverflow.com/questions/63227358/dji-videoframeyuv-to-ciimage-conversion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!