将CVPixelBufferRef转为Metal 纹理传给unity渲染
- (void)onRemodeVideoRenderArrival:(CVPixelBufferRef)pixelBuffer userID:(NSInteger)userID {
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
{
size_twidth =CVPixelBufferGetWidthOfPlane(pixelBuffer,0);
size_theight =CVPixelBufferGetHeightOfPlane(pixelBuffer,0);
MTLPixelFormatpixelFormat =MTLPixelFormatR8Unorm;// 这里的颜色格式不是RGBA
MTLTextureDescriptor* txDesc = [[MTLTextureDescriptor alloc] init];
txDesc.textureType = MTLTextureType2D;
txDesc.height= height;
txDesc.width= width;
txDesc.depth=1;
txDesc.pixelFormat= pixelFormat;
txDesc.arrayLength=1;
txDesc.mipmapLevelCount=1;
void *buf = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);
size_tbytesPerRow =CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer,0);
_util->yTex= [_util->devicenewTextureWithDescriptor:txDesc];
[_util->yTex replaceRegion:MTLRegionMake2D(0, 0, width, height) mipmapLevel:0 withBytes:buf bytesPerRow:bytesPerRow];
_util->yTexWidth= (int32_t)width;
_util->yTexHeight= (int32_t)height;
// void *yRet = (__bridge void*)(_util->yTex);
// self.yVideoArrivalCallback(_host, yRet, width, height, userID);
}
{
size_twidth =CVPixelBufferGetWidthOfPlane(pixelBuffer,1);
size_theight =CVPixelBufferGetHeightOfPlane(pixelBuffer,1);
MTLPixelFormatpixelFormat =MTLPixelFormatRG8Unorm;// 2-8bit的格式
MTLTextureDescriptor* txDesc = [[MTLTextureDescriptor alloc] init];
txDesc.textureType = MTLTextureType2D;
txDesc.height= height;
txDesc.width= width;
txDesc.depth=1;
txDesc.pixelFormat= pixelFormat;
txDesc.arrayLength=1;
txDesc.mipmapLevelCount=1;
void *buf = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1);
size_tbytesPerRow =CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer,1);
_util->uvTex= [_util->devicenewTextureWithDescriptor:txDesc];
[_util->uvTex replaceRegion:MTLRegionMake2D(0, 0, width, height) mipmapLevel:0 withBytes:buf bytesPerRow:bytesPerRow];
_util->uvTexWidth= (int32_t)width;
_util->uvTexHeight= (int32_t)height;
}
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
_util->frame_updated = true;
}