Crop CMSampleBuffer and process it without converting to CGImage
Asked Answered
A

0

3

I have been following the apple's live stream camera editor code to get the hold of live video editing.

So far so good, but I need a way out to crop a sample buffer into 4 pieces and then process all four with different CIFilters. For instance, If the size of the image is 1000x1000, I want to crop the CMSampleBuffer into 4 images of size 250x250 and then apply unique filter to each, convert it back to CMSammpleBuffer and display on Metal View. Here is the code till which I could crop the CMSampleBuffer in a CGContext but could not convert it back to CMSampleBuffer:

let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!

        CVPixelBufferLockBaseAddress(imageBuffer, .readOnly)

        let baseAddress = CVPixelBufferGetBaseAddress(imageBuffer)
        let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer)
        let cropWidth = 640
        let cropHeight = 640
        let colorSpace = CGColorSpaceCreateDeviceRGB()

        let context = CGContext(data: baseAddress, width: cropWidth, height: cropHeight, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue | CGBitmapInfo.byteOrder32Little.rawValue)


        CVPixelBufferUnlockBaseAddress(imageBuffer, .readOnly)

        // create image
        let cgImage: CGImage = context!.makeImage()!
        let image = UIImage(cgImage: cgImage)

I don't need CGImage, I need either CMSampleBuffer or CVImageBuffer so I can pass it to this function func render(pixelBuffer: CVPixelBuffer) -> CVPixelBuffer? of FilterRenderer class used in the sample code of this link from apple.

Ashil answered 20/2, 2019 at 18:11 Comment(2)
Did you solve itvDeth
Nope. You have to write your own CIFIlter to perform like that which applies 4 filters to 4 parts of the image. Better use metal shaders, as they are fast.Ashil

© 2022 - 2024 — McMap. All rights reserved.