要使用CVPixelBufferCreate
在iOS中创建像素缓冲区,我们需要指定像素格式,但是还有另一个选项kCVPixelBufferPixelFormatTypeKey
可以沿像素缓冲区分配不同的像素格式。
IMO,第一个像素格式确定像素缓冲区的内存布局,例如,kCVPixelFormatType_420YpCbCr8Planar
使缓冲区的内存布局为i420
,但多余的{{1 }}?
我已经搜索了github,并从UCDLive_iOS找到了一个代码段,如下所示。
kCVPixelBufferPixelFormatTypeKey
-(CVPixelBufferRef) convertToNV12:(CVPixelBufferRef)src
{
int width = (int)CVPixelBufferGetWidth(src);
int height = (int)CVPixelBufferGetHeight(src);
NSDictionary* pixelBufferOptions = @{ (NSString*) kCVPixelBufferPixelFormatTypeKey :
@(kCVPixelFormatType_420YpCbCr8Planar),
(NSString*) kCVPixelBufferWidthKey : @(width),
(NSString*) kCVPixelBufferHeightKey : @(height),
(NSString*) kCVPixelBufferOpenGLESCompatibilityKey : @YES,
(NSString*) kCVPixelBufferIOSurfacePropertiesKey : @{}};
CVPixelBufferRef dst;
CVPixelBufferCreate(kCFAllocatorDefault,
width, height,
kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange,
(__bridge CFDictionaryRef)pixelBufferOptions,
&dst);
CVPixelBufferLockBaseAddress(dst, 0);
CVPixelBufferLockBaseAddress(src, 0);
void* pSrcY = CVPixelBufferGetBaseAddressOfPlane(src, 0);
void* pSrcU = CVPixelBufferGetBaseAddressOfPlane(src, 1);
void* pSrcV = CVPixelBufferGetBaseAddressOfPlane(src, 2);
size_t srcStrideY = CVPixelBufferGetBytesPerRowOfPlane(src, 0);
size_t srcStrideU = CVPixelBufferGetBytesPerRowOfPlane(src, 1);
size_t srcStrideV = CVPixelBufferGetBytesPerRowOfPlane(src, 2);
void* pDstY = CVPixelBufferGetBaseAddressOfPlane(dst, 0);
void* pDstUV = CVPixelBufferGetBaseAddressOfPlane(dst, 1);
size_t dstStrideY = CVPixelBufferGetBytesPerRowOfPlane(dst, 0);
size_t dstStrideUV = CVPixelBufferGetBytesPerRowOfPlane(dst, 1);
int ret = libyuv::I420ToNV12((const uint8*)pSrcY, (int)srcStrideY,
(const uint8*)pSrcU, (int)srcStrideU,
(const uint8*)pSrcV, (int)srcStrideV,
(uint8*)pDstY, (int)dstStrideY,
(uint8*)pDstUV, (int)dstStrideUV,
(int)width, (int)height);
if(ret != 0)
{
NSLog(@"I420ToNV12 failed,error is:%d",ret);
CVPixelBufferUnlockBaseAddress(src, 0);
CVPixelBufferUnlockBaseAddress(dst, 0);
CFRelease(dst);
return nil;
}
CVPixelBufferUnlockBaseAddress(src, 0);
CVPixelBufferUnlockBaseAddress(dst, 0);
return dst;
}
与kCVPixelFormatType_420YpCbCr8Planar
不同,但是有什么意义呢?