3

我正在使用具有以下输出设置的 AVCaptureSession:

NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey; 
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange];
NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
[captureOutput setVideoSettings:videoSettings];

我的 AVCaptureVideoPreviewLayer 显示正常,但我需要的不止这些,因为我没有成功使用 AVCaptureVideoPreviewLayer 获取屏幕截图。因此,在 captureOutput 委托中创建 CGContextRef 时,我正在使用这些设置

uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer); 
size_t height = CVPixelBufferGetHeight(imageBuffer);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, width * 4, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef newImage = CGBitmapContextCreateImage(newContext);

我不再收到“不支持的参数组合”警告,但显示屏只是纯白色。

我应该在更改时添加

NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange];

NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];

一切正常。我的问题是什么?

4

2 回答 2

2

看看下面的代码(它使用 FullVideoRange 代替),它“手动”将双平面视频帧转换为 RGB。

CVPixelBufferLockBaseAddress(imageBuffer, 0);

size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);

uint8_t *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
CVPlanarPixelBufferInfo_YCbCrBiPlanar *bufferInfo = (CVPlanarPixelBufferInfo_YCbCrBiPlanar *)baseAddress;

NSUInteger yOffset = EndianU32_BtoN(bufferInfo->componentInfoY.offset);
NSUInteger yPitch = EndianU32_BtoN(bufferInfo->componentInfoY.rowBytes);

NSUInteger cbCrOffset = EndianU32_BtoN(bufferInfo->componentInfoCbCr.offset);
NSUInteger cbCrPitch = EndianU32_BtoN(bufferInfo->componentInfoCbCr.rowBytes);

uint8_t *rgbBuffer = malloc(width * height * 3);
uint8_t *yBuffer = baseAddress + yOffset;
uint8_t *cbCrBuffer = baseAddress + cbCrOffset;

for(int y = 0; y < height; y++)
{
uint8_t *rgbBufferLine = &rgbBuffer[y * width * 3];
uint8_t *yBufferLine = &yBuffer[y * yPitch];
uint8_t *cbCrBufferLine = &cbCrBuffer[(y >> 1) * cbCrPitch];

for(int x = 0; x < width; x++)
{
    uint8_t y = yBufferLine[x];
    uint8_t cb = cbCrBufferLine[x & ~1];
    uint8_t cr = cbCrBufferLine[x | 1];

    uint8_t *rgbOutput = &rgbBufferLine[x*3];

    // from ITU-R BT.601, rounded to integers
    rgbOutput[0] = (298 * (y - 16) + 409 * cr - 223) >> 8;
    rgbOutput[1] = (298 * (y - 16) + 100 * cb + 208 * cr + 136) >> 8;
    rgbOutput[2] = (298 * (y - 16) + 516 * cb - 277) >> 8;
}
}

以下链接也可能有助于更好地理解这种视频格式: http: //blog.csdn.net/yiheng_l/article/details/3790219#yuvformats_nv12

于 2013-01-09T13:39:07.953 回答
0

看看InvasiveCode 的教程。它展示了如何使用 Accelerate 和 CoreImage 框架来处理 Y 通道

于 2013-06-13T03:00:09.630 回答