参考 demo
typedef struct {
uint8_t *y_frame;
uint8_t *u_frame;
uint8_t *v_frame;
int width;
int height;
int stride_y;
int stride_u;
int stride_v;
} I420Buffer;
+ (CVPixelBufferRef)convertToBGRAPixelBufferWithI420Buffer:(I420Buffer)i420Buffer
{
if (!i420Buffer.y_frame) {
return NULL;
}
//create rgb buffer
NSDictionary *att = @{(NSString *)kCVPixelBufferIOSurfacePropertiesKey : @{}};
CVPixelBufferRef bgraBuffer;
CVPixelBufferCreate(kCFAllocatorDefault,
(size_t)i420Buffer.width,
(size_t)i420Buffer.height,
kCVPixelFormatType_32BGRA,
(__bridge CFDictionaryRef _Nullable)att,
&bgraBuffer);
if (!bgraBuffer) {
//create error
NSLog(@"%s, create error, line = %d", __func__, __LINE__);
return NULL;
}
CVPixelBufferLockBaseAddress(bgraBuffer, 0);
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(bgraBuffer);
int bytesPerRow = (int)CVPixelBufferGetBytesPerRow(bgraBuffer);
int ret = libyuv::I420ToARGB(i420Buffer.y_frame, i420Buffer.stride_y,
i420Buffer.u_frame, i420Buffer.stride_u,
i420Buffer.v_frame, i420Buffer.stride_v,
baseAddress, bytesPerRow,
i420Buffer.width, i420Buffer.height);
assert(ret == 0);
CVPixelBufferUnlockBaseAddress(bgraBuffer, 0);
return (CVPixelBufferRef)CFAutorelease(bgraBuffer);
}
网友评论