意外的输出将CVPixelBuffer转换为MTLTexture

时间:2019-06-13 08:31:44

标签: ios swift avfoundation metal

我正在使用AVAssetReader从AVAsset提取SampleBuffers。我使用下面的代码片段将每个CMSampleBuffer转换为MTLTexture。但是,我得到了CVPixelBuffer的期望,但是当我尝试对其进行转换时却得到了unexpected output。预期输出为this ..

我已经尝试调试准确的宽度和高度,我尝试使用发生相同问题的其他视频,尝试创建不同的textureCache。同样的问题。

    func convertToMTLTexture(sampleBuffer: CMSampleBuffer?) -> MTLTexture? {


        if let textureCache = textureCache,
            let sampleBuffer = sampleBuffer,
            let imageBuffer:CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) {

            let width = CVPixelBufferGetWidth(imageBuffer)
            let height = CVPixelBufferGetHeight(imageBuffer)

            var texture: CVMetalTexture?
            CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, textureCache,
                                                      imageBuffer, nil, .bgra8Unorm, width, height, 0, &texture)

            if let texture = texture {
                return CVMetalTextureGetTexture(texture)
            }

        }
        return nil
    }

0 个答案:

没有答案