我收到了来自系统API的autofill
,其中包含CMSampleBufferRef
不是CVPixelBufferRef
(线性像素)的RGBA
。缓冲区包含平面像素(例如420f
又名kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
又名yCbCr
又名YUV
)。
我想修改对此视频数据进行一些操作,然后再将其发送到VideoToolkit
以编码为h264
(绘制一些文字,覆盖徽标,旋转图像等),但我希望它能够高效,实时。 Buuuut平面图像数据看起来很混乱 - 有色度平面和亮度平面,它们的大小不同......在字节级别上使用它似乎很多工作。
我可以使用CGContextRef
并且只是在像素顶部绘制,但是从我可以收集它只支持RGBA像素。有关如何使用尽可能少的数据复制执行此操作的任何建议,但尽可能少的代码行?
答案 0 :(得分:3)
CGBitmapContextRef
只能绘制成32ARGB
之类的内容,正确无误。这意味着您需要创建ARGB
(或RGBA
)缓冲区,然后找到一种快速将YUV
像素转移到此ARGB
曲面上的方法。此配方包括使用CoreImage
,通过池自制CVPixelBufferRef
,引用自制像素缓冲区的CGBitmapContextRef
,然后重新创建类似于输入缓冲区的CMSampleBufferRef
,但参考输出像素。换句话说,
CIImage
。CVPixelBufferPool
。你不想实时创建没有池的CVPixelBuffer
:如果你的制作人太快,你的内存就会耗尽;当你不能重复使用缓冲区时,你会分割你的RAM;这是一个浪费周期。CIContext
。它不包含外部状态,但是文档说在每一帧上重新创建它都非常昂贵。CMVideoFormatDescriptionRef
这是一个示例实现,我选择32ARGB作为图像格式,因为CGBitmapContext
和CoreVideo
喜欢在iOS上使用的东西:
{
CGPixelBufferPoolRef *_pool;
CGSize _poolBufferDimensions;
}
- (void)_processSampleBuffer:(CMSampleBufferRef)inputBuffer
{
// 1. Input data
CVPixelBufferRef inputPixels = CMSampleBufferGetImageBuffer(inputBuffer);
CIImage *inputImage = [CIImage imageWithCVPixelBuffer:inputPixels];
// 2. Create a new pool if the old pool doesn't have the right format.
CGSize bufferDimensions = {CVPixelBufferGetWidth(inputPixels), CVPixelBufferGetHeight(inputPixels)};
if(!_pool || !CGSizeEqualToSize(bufferDimensions, _poolBufferDimensions)) {
if(_pool) {
CFRelease(_pool);
}
OSStatus ok0 = CVPixelBufferPoolCreate(NULL,
NULL, // pool attrs
(__bridge CFDictionaryRef)(@{
(id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32ARGB),
(id)kCVPixelBufferWidthKey: @(bufferDimensions.width),
(id)kCVPixelBufferHeightKey: @(bufferDimensions.height),
}), // buffer attrs
&_pool
);
_poolBufferDimensions = bufferDimensions;
assert(ok0 == noErr);
}
// 4. Create pixel buffer
CVPixelBufferRef outputPixels;
OSStatus ok1 = CVPixelBufferPoolCreatePixelBufferWithAuxAttributes(NULL,
_pool,
(__bridge CFDictionaryRef)@{
// Opt to fail buffer creation in case of slow buffer consumption
// rather than to exhaust all memory.
(__bridge id)kCVPixelBufferPoolAllocationThresholdKey: @20
}, // aux attributes
&outputPixels
);
if(ok1 == kCVReturnWouldExceedAllocationThreshold) {
// Dropping frame because consumer is too slow
return;
}
assert(ok1 == noErr);
// 5, 6. Graphics context to draw in
CGColorSpaceRef deviceColors = CGColorSpaceCreateDeviceRGB();
OSStatus ok2 = CVPixelBufferLockBaseAddress(outputPixels, 0);
assert(ok2 == noErr);
CGContextRef cg = CGBitmapContextCreate(
CVPixelBufferGetBaseAddress(outputPixels), // bytes
CVPixelBufferGetWidth(inputPixels), CVPixelBufferGetHeight(inputPixels), // dimensions
8, // bits per component
CVPixelBufferGetBytesPerRow(outputPixels), // bytes per row
deviceColors, // color space
kCGImageAlphaPremultipliedFirst // bitmap info
);
CFRelease(deviceColors);
assert(cg != NULL);
// 7
[_imageContext render:inputImage toCVPixelBuffer:outputPixels];
// 8. DRAW
CGContextSetRGBFillColor(cg, 0.5, 0, 0, 1);
CGContextSetTextDrawingMode(cg, kCGTextFill);
NSAttributedString *text = [[NSAttributedString alloc] initWithString:@"Hello world" attributes:NULL];
CTLineRef line = CTLineCreateWithAttributedString((__bridge CFAttributedStringRef)text);
CTLineDraw(line, cg);
CFRelease(line);
// 9. Unlock and stop drawing
CFRelease(cg);
CVPixelBufferUnlockBaseAddress(outputPixels, 0);
// 10. Timings
CMSampleTimingInfo timingInfo;
OSStatus ok4 = CMSampleBufferGetSampleTimingInfo(inputBuffer, 0, &timingInfo);
assert(ok4 == noErr);
// 11. VIdeo format
CMVideoFormatDescriptionRef videoFormat;
OSStatus ok5 = CMVideoFormatDescriptionCreateForImageBuffer(NULL, outputPixels, &videoFormat);
assert(ok5 == noErr);
// 12. Output sample buffer
CMSampleBufferRef outputBuffer;
OSStatus ok3 = CMSampleBufferCreateForImageBuffer(NULL, // allocator
outputPixels, // image buffer
YES, // data ready
NULL, // make ready callback
NULL, // make ready refcon
videoFormat,
&timingInfo, // timing info
&outputBuffer // out
);
assert(ok3 == noErr);
[_consumer consumeSampleBuffer:outputBuffer];
CFRelease(outputPixels);
CFRelease(videoFormat);
CFRelease(outputBuffer);
}