2016-10-21 2 views
1

Apple-docs dieses Beispiel geben für eine CIFilter zu einem AVAsset Anwendung:Anwendung CIFilter auf der GPU Cocoa

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) 
}) 

Dies funktioniert gut auf einige Videos (es scheint, mit denen viel mehr performant zu sein mit AAC-Codec) und Bei anderen nimmt die CPU-Nutzung zu und das Video wird nie fertig bearbeitet. Gibt es eine Möglichkeit, dies auf die GPU zu verschieben, um Dinge zu beschleunigen/nicht so viel von der CPU zu binden? Ich sah this question für iOS aber CIContext contextWithEAGLContext: ist nicht verfügbar auf OS X. Ich bin neu in AVFoundation/Video-Verarbeitung, gibt es eine Entsprechung auf OS X?

Hinweis: Ich suche nicht in Echtzeit, ich möchte einfach den Filter anwenden und die Datei mit der GPU in das Dateisystem exportieren.

Antwort

1

macOS hat stattdessen contextWithCGLContext für OpenGL:

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

oder contextWithMTLDevice: für Metall, wenn Sie es vorziehen, dass:

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