2017-10-04 40 views
0

我實際上使用iPad Pro在iOS10上處理圖像處理。 我已經編寫了小型Swift3圖像處理應用程序來測試圖像處理的速度。 我的解碼器發送每個約33ms(約30 FPS)的新幀,我需要使用iOS的某些CoreImage濾鏡處理而無需額外的緩衝。每個〜爲33ms以下函數將被調用:iOS中的UnsafeMutableRawPointer上的GPU過濾器

func newFrame(_ player: MediaPlayer!, buffer: UnsafeMutableRawPointer!, 
       size: Int32, format_fourcc: UnsafeMutablePointer<Int8>!, 
       width: Int32, height: Int32, bytes_per_row: Int32, 
       pts: Int, will_show: Int32) -> Int32 { 

    if String(cString: format_fourcc) == "BGRA" && will_show == 1 { 
     // START 
     var pixelBuffer: CVPixelBuffer? = nil 
     let ret = CVPixelBufferCreateWithBytes(kCFAllocatorSystemDefault, 
               Int(width), 
               Int(height), 
               kCVPixelFormatType_32BGRA, 
               buffer, 
               Int(bytes_per_row), 
               { (releaseContext: 
                UnsafeMutableRawPointer?, 
                baseAddr: 
                UnsafeRawPointer?) ->() in 
               // Do not need to be used 
               // since created CVPixelBuffer 
               // will be destroyed 
               // in scope of this function 
               // automatically 
               }, 
               buffer, 
               nil, 
               &pixelBuffer) 
     // END_1 

     if ret != kCVReturnSuccess { 
      NSLog("New Frame: Can't create the buffer") 
      return -1 
     } 

     if let pBuff = pixelBuffer { 
      let img = CIImage(cvPixelBuffer: pBuff) 
         .applyingFilter("CIColorInvert", withInputParameters: [:]) 
     } 
     // END_2 
    } 
    return 0 
} 

我需要解決以下問題之一:

  • 複製CIImage IMG原始內存的數據回UnsafeMutableRawPointer緩衝內存。
  • GPU圖像過濾器不知何故應用於CVPixelBuffer pixelBufferUnsafeMutableRawPointer緩衝器直接

//之間的代碼集團START// END_2需要在小於5ms運行。

我所知道的:

  • //之間代碼起始// END_1運行在小於1.3ms。

請幫助你的想法。

最好的問候, 亞歷

回答

0

我找到了暫時的解決方法:

1)在你的視圖中創建CIContext:

imgContext = CIContext(eaglContext: eaglContext!) 

2)使用上下文來繪製過濾CIImage指針的內存:

imgContext.render(img, 
         toBitmap: buffer, 
         rowBytes: Int(bytes_per_row), 
         bounds: CGRect(x: 0, 
            y: 0, 
            width: Int(width), 
            height: Int(height)), 
         format: kCIFormatBGRA8, 
         colorSpace: CGColorSpaceCreateDeviceRGB()) 

該解決方案可以很好地工作,因爲它使用iPad CPU的SIMD指令。但僅用於複製操作的CPU利用率太高〜30%。這30%將被添加到您的程序的CPU使用率。

可能有人有更好的主意讓CIFilter之後如何讓GPU直接寫入UnsafeMutableRawPointer?