gpt4 book ai didi

objective-c - 从 CIImage 创建 CVPixelBufferRef 以写入文件

转载 作者:可可西里 更新时间:2023-11-01 06:23:43 25 4
gpt4 key购买 nike

我正在编写自定义电影录制应用程序,并实现了 AVAssetWriter 和 AVAssetWriterInputPixelBufferAdaptor 以将帧写入文件。在 DataOutputDelegate 回调中,我试图将 CIFilter 应用于 sampleBuffer。首先我得到一个 CVPixelBufferRef,然后创建一个 CIImage。然后我应用 CIIFilter 并获取生成的 CIImage:

    CVPixelBufferRef pixelBuffer = (CVPixelBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer);

CIImage *image = [CIImage imageWithCVPixelBuffer:pixelBuffer];
CIFilter *hueAdjust = [CIFilter filterWithName:@"CIHueAdjust"];
[hueAdjust setDefaults];
[hueAdjust setValue: image forKey: @"inputImage"];
[hueAdjust setValue: [NSNumber numberWithFloat: 2.094]
forKey: @"inputAngle"];

CIImage *result = [hueAdjust valueForKey: @"outputImage"];

CVPixelBufferRef newBuffer = //Convert CIImage...

我将如何转换 CIImage 以便我可以:

    [self.filteredImageWriter appendPixelBuffer:newBuffer withPresentationTime:lastSampleTime];

最佳答案

更新的答案

虽然我以前的方法有效,但我能够对其进行调整以简化代码。它似乎也运行得更快。

samplePixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(samplePixelBuffer, 0); // NOT SURE IF NEEDED // NO PERFORMANCE IMPROVEMENTS IF REMOVED

NSDictionary *options = [NSDictionary dictionaryWithObject:(__bridge id)rgbColorSpace forKey:kCIImageColorSpace];
outputImage = [CIImage imageWithCVPixelBuffer:samplePixelBuffer options:options];

//-----------------
// FILTER OUTPUT IMAGE

@autoreleasepool {

outputImage = [self applyEffectToCIImage:outputImage dict:dict];

}
CVPixelBufferUnlockBaseAddress(samplePixelBuffer, 0); // NOT SURE IF NEEDED // NO PERFORMANCE IMPROVEMENTS IF REMOVED

//-----------------
// RENDER OUTPUT IMAGE BACK TO PIXEL BUFFER

[self.filterContext render:outputImage toCVPixelBuffer:samplePixelBuffer bounds:[outputImage extent] colorSpace:CGColorSpaceCreateDeviceRGB()]; // DOES NOT SEEM TO WORK USING rgbColorSpace

上一个答案

我刚刚实现了以下从 CIImage 获取像素缓冲区。确保您的像素格式一致,否则您将遇到颜色问题。 CFDictionaries 也非常重要。 http://allmybrain.com/2011/12/08/rendering-to-a-texture-with-ios-5-texture-cache-api/

CFDictionaryRef empty = CFDictionaryCreate(kCFAllocatorDefault, // EMPTY IOSURFACE DICT
NULL,
NULL,
0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFMutableDictionaryRef attributes = CFDictionaryCreateMutable(kCFAllocatorDefault,
1,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);

CFDictionarySetValue(attributes,kCVPixelBufferIOSurfacePropertiesKey,empty);

CVPixelBufferRef pixelBuffer = NULL;
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault,
outputImage.extent.size.width,
outputImage.extent.size.height,
kCVPixelFormatType_32BGRA,
attributes,
&pixelBuffer);

if (status == kCVReturnSuccess && pixelBuffer != NULL) {

CVPixelBufferLockBaseAddress(pixelBuffer, 0); // NOT SURE IF NEEDED // KEPT JUST IN CASE
[self.filterContext render:outputImage toCVPixelBuffer:pixelBuffer bounds:[outputImage extent] colorSpace:CGColorSpaceCreateDeviceRGB()];
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0); // NOT SURE IF NEEDED // KEPT JUST IN CASE

}

关于objective-c - 从 CIImage 创建 CVPixelBufferRef 以写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9984548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com