Hi, I'm trying to replace portraitMatte with a custom pixel buffer I created.
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
var finalPortraitMatte: AVPortraitEffectsMatte?
if let portraitMatte = photo.portraitEffectsMatte {
let mattingImage = portraitMatte.mattingImage
var mattingCIImage = CIImage(cvPixelBuffer: mattingImage)
// this should be kCVPixelFormatType_OneComponent8
let matteImageType = CVPixelBufferGetPixelFormatType(mattingImage)
if let destMatteBuffer = processMatteImage(ciImage: mattingCIImage, formatType: matteImageType){
do {
finalPortraitMatte = try portraitMatte.replacingPortraitEffectsMatte(with: destMatteBuffer)
} catch {
// I get an error here
print("replacing matte error", error)
}
}
}
}
func processMatteImage(ciImage: CIImage, formatType: OSType) -> CVPixelBuffer? {
var ciMatteImage = ciImage
var destPixelBuffer: CVPixelBuffer?
let width = Int(ciMatteImage.extent.width)
let height = Int(ciMatteImage.extent.height)
let attrs = [kCVPixelBufferColorPrimariesKey: kCVImageBufferColorPrimaries_ITU_R_709_2,
kCVPixelBufferTransferFunctionKey: kCVImageBufferTransferFunction_Linear] as [CFString: Any]
CVPixelBufferCreate(kCFAllocatorDefault, width, height, formatType, attrs as CFDictionary, &destPixelBuffer)
if self.cameraDevicePosition == .front {
ciMatteImage = ciMatteImage.transformed(by: CGAffineTransform(a: -1, b: 0, c: 0, d: 1, tx: CGFloat(width), ty: 0))
}
if let pixelBuffer = destPixelBuffer {
CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))
CIContext().render(ciMatteImage, to pixelBuffer)
CVPixelBufferUnlockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))
return pixelBuffer
}
return nil
}
When I try to replace matte pixel buffer, I get an error
Error Domain=AVFoundationErrorDomain Code=-11864 "Format Unsupported" UserInfo={NSLocalizedDescription=Format Unsupported, NSLocalizedFailureResion=The format of this content is unsupported.}
But according to https://developer.apple.com/documentation/avfoundation/avportraiteffectsmatte/2976124-replacingportraiteffectsmatte,
PortraitMatte Buffer's pixel format is kCVPixelFormatType_OneComponent8, which is the same format above the code.
I also added some attributes according to that link.
My device is iPhone XS and OS version is 12.1.4
I can't figure out why "format unsupported" error occurs. Is there anything I missed?
Thanks in advance.