在GPU Cocoa上应用CIFilter

3

苹果文档给出了一个将 CIFilter 应用到 AVAsset 的示例:

let filter = CIFilter(name: "CIGaussianBlur")!
let composition = AVVideoComposition(asset: asset, applyingCIFiltersWithHandler: { request in

   // Clamp to avoid blurring transparent pixels at the image edges
   let source = request.sourceImage.clampingToExtent()
   filter.setValue(source, forKey: kCIInputImageKey)

   // Vary filter parameters based on video timing
   let seconds = CMTimeGetSeconds(request.compositionTime)
   filter.setValue(seconds * 10.0, forKey: kCIInputRadiusKey)

   // Crop the blurred output to the bounds of the original image
  let output = filter.outputImage!.cropping(to: request.sourceImage.extent)

   // Provide the filter output to the composition
   request.finish(with: output, context: nil)
})

这在某些视频上效果很好(使用AAC编解码器的视频似乎更具性能),而在其他视频上,CPU使用率飙升,视频处理从未完成。是否有一种方法将其移至GPU以加速处理/减少CPU占用?我在iOS中看到这个问题,但OS X上没有CIContext contextWithEAGLContext:。我对AVFoundation /视频处理不熟悉,在OS X上是否有相应的功能?
注意:我不希望实时进行此操作,我只想使用GPU应用滤镜并将文件导出到文件系统。
1个回答

3

相比之下,macOS使用contextWithCGLContext来处理OpenGL:

+ (CIContext *)contextWithCGLContext:(CGLContextObj)cglctx
                         pixelFormat:(nullable CGLPixelFormatObj)pixelFormat
                          colorSpace:(nullable CGColorSpaceRef)colorSpace
                             options:(nullable NSDictionary<NSString*,id> *)options;

或者如果您更喜欢Metal,可以使用contextWithMTLDevice:

+ (CIContext *)contextWithMTLDevice:(id<MTLDevice>)device;

我正在尝试在iPhone上的实时相机视频中实现相同的效果。但不幸的是,Metal的性能非常慢。如果您能帮忙,请查看以下链接:https://stackoverflow.com/questions/53898780/how-to-apply-a-vignette-cifilter-to-a-live-camera-feed-in-ios - nr5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接