[iOS] Fastest way to render video

Hi.


I'm rendering a video from either one or two source videos from the iOS device camera with (time dependent) transform/clips applied to them and potentially overlaid line drawings. The source videos are intermittently paused & seeks are performed in them.


I'm using an AVAssetWriter with the (simplified) code below being called for each frame. Export for 720p currently takes about 70ms per frame in case of one source video. Profiling tells me the ciContext.createCGImage and CGContextDrawImage calls are basically the only significant ones here.


Is there any way to significantly improve on this performance?


Thanks in advance for any help on the matter!


  // Get a pixel buffer
  var pixelBufferOut: CVPixelBufferRef?
  guard let pool = pixelBufferAdaptor.pixelBufferPool else { return false }
  CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, pool, &pixelBufferOut)
  let pixelBuffer = pixelBufferOut!
  // Access pixel buffer
  CVPixelBufferLockBaseAddress(pixelBuffer, 0)
  let pixelData = CVPixelBufferGetBaseAddress(pixelBuffer)
  let width = CVPixelBufferGetWidth(pixelBuffer)
  let height = CVPixelBufferGetHeight(pixelBuffer)
  let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
  let bitmapInfo: CGBitmapInfo = [ CGBitmapInfo(rawValue: CGImageAlphaInfo.NoneSkipFirst.rawValue), .ByteOrder32Little ]
  // Create CGContext
  let context = CGBitmapContextCreate(pixelData, width, height, 8, bytesPerRow, colorSpace, bitmapInfo.rawValue)!
  UIGraphicsPushContext(context)
  CGContextSetInterpolationQuality(context, .Low)
  // Draw (in some cases there are two source buffers rendered "split screen")
  let ciImage = CIImage(CVPixelBuffer: sourcePixelBuffer)
  let frameImage = ciContext.createCGImage(ciImage, fromRect: ciImage.extent, format: kCIFormatBGRA8, colorSpace: colorSpace)
  CGContextClipToRect(context someRect)
  CGContextConcatCTM(context, someTransform)
  CGContextDrawImage(context, someOtherRect, frameImage)
  // < Some occasional CGPath line drawing here >
  // Cleanup
  UIGraphicsPopContext()
  CVPixelBufferUnlockBaseAddress(pixelBuffer, 0)
  // Append
  let time = CMTime(value: CMTimeValue(frameIndex), timescale: 30)
  return pixelBufferAdaptor.appendPixelBuffer(pixelBuffer, withPresentationTime: time)


I'm currently initializing the source & output pixel buffers with these attributes:


let pixelBufferAttributes: [String:AnyObject] = [
  kCVPixelBufferCGBitmapContextCompatibilityKey as String : true,
  kCVPixelBufferPixelFormatTypeKey as String : UInt(kCVPixelFormatType_32BGRA)
]
[iOS] Fastest way to render video
 
 
Q