4

I have a requirement to apply filters on the live video and I'm trying to do it in Metal.

But I have encountered problem with converting the MTLTexture into CVPixelBuffer after encoding the filter into destination filter. Reference (https://github.com/oklyc/MetalCameraSample-master-2)

Here are my codes.

if let pixelBuffer = pixelBuffer {
                CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags.init(rawValue: 0))             
                let region = MTLRegionMake2D(0, 0, Int(currentDrawable.layer.drawableSize.width), Int(currentDrawable.layer.drawableSize.height))                    
                let bytesPerPixel = 4;
                let bytesPerRow = CGFloat(bytesPerPixel) * currentDrawable.layer.drawableSize.width

                let tempBuffer = CVPixelBufferGetBaseAddress(pixelBuffer)
                destinationTexture.getBytes(tempBuffer!, bytesPerRow: Int(bytesPerRow), from: region1, mipmapLevel: 0)

                let image = self.imageFromCVPixelBuffer(buffer: pixelBuffer)
                CVPixelBufferUnlockBaseAddress(pixelBuffer, CVPixelBufferLockFlags.init(rawValue: 0))

            }

The method imageFromCVPixelBuffer looks like this.

func imageFromCVPixelBuffer(buffer: CVPixelBuffer) -> UIImage {

    let ciimage = CIImage(cvPixelBuffer: buffer)
    let context = CIContext(options: nil)
    let cgimgage = context.createCGImage(ciimage, from: CGRect(x: 0, y: 0, width: CVPixelBufferGetWidth(buffer), height: CVPixelBufferGetHeight(buffer)))

    let uiimage = UIImage(cgImage: cgimgage!)

    return uiimage
}

Here is the screen shot of the image rendering through metal

enter image description here

Here is the screen shot of the same image converting MTLTexture to CVPixelBuffer.

enter image description here

Converting MTLtexture into CVPixelBuffer is required to write into an AVAssetWriter and then saving it to the Library.

halfer
  • 19,824
  • 17
  • 99
  • 186
Shreesha Kedlaya
  • 340
  • 4
  • 19
  • 1
    Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Jun 30 '17 at 10:37
  • I am also trying to use mtltexture with avassetwriter. Can you share your final code please. – Ömer Karışman Aug 29 '17 at 15:10
  • Converting to UIImage I believe is pretty slow and will give you poor performance if you are making a video. Obviously that's totally fine for a screenshot however. – drewster Jan 09 '18 at 20:11

1 Answers1

7

Don't compute bytesPerRow yourself like that. It's being passed in to Metal to let Metal know how to arrange the rows. You want Metal to arrange them the way CVPixelBuffer expects them. Therefore, you should use CVPixelBufferGetBytesPerRow() to determine the value.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • I believe rows in a CVPixelBuffer are 32-pixel aligned. This leads to issues if you start making assumptions about these buffers not containing any padding bytes at the end of each row. – zzyzy Jul 23 '17 at 16:46
  • Wait, but where are you copying pixels data from mtltexture to cvpixelbuffer? – Dimitrio Jun 20 '18 at 13:12