Metal Camera开发5:AVAssetWriterInputPixelBufferAdaptor实现Metal渲染结果从BGRA到yuv420p的快速转换

本文档描述使用AVAssetWriterInputPixelBufferAdaptor简化Metal渲染结果从BGRA到yuv420p的转换,适用于写成MP4或MOV容器。以前有人用类似的办法暴力实现iOS 8以下的H.264硬解,代价是消耗用户的闪存写次数。

文档结构:

  1. 初始化AVAssetWriterInput
  2. 初始化AVAssetWriterInputPixelBufferAdaptor
  3. CVPixelBuffer写入AVAssetWriterInputPixelBufferAdaptor
  4. 讨论:指定BT. 709 YUV转RGB矩阵

Metal Camera开发4:渲染到CVPixelBuffer实现了CVPixelBuffer存储渲染结果,对于摄像头开发,通常希望将渲染结果编码成H.264或H.265(iOS 11支持),iOS平台的H.264编码器原生支持yuv420sp VideoRange及FullRange,而Metal渲染到CVPixelBuffer时像素格式为BGRA,显然需要进行像素格式转换,比如使用libyuv、Metal 着色器实现RGBA转yuv或GLSL实现。对于将数据写到本地的需求,AVFoundation提供了AVAssetWriterInputPixelBufferAdaptor简化RGB转yuv的步骤。GPUImage也使用了这种方式实现OpenGL ES的渲染结果编码成H.264。下面描述它的编程过程。

AVAssetWriterInputPixelBufferAdaptor必须配合AVAssetWriterInput使用,不然没法调用构造函数(手动斜眼.jpg)。AVAssetWriterInputPixelBufferAdaptor类的头文件描述了它的功能:

Pixel buffers not in a natively supported format will be converted internally prior to encoding when possible.

对于编码Metal的渲染结果,初始化AVAssetWriterInputPixelBufferAdaptor指定sourcePixelBufferAttributes使用kCVPixelBufferPixelFormatTypeKey为kCVPixelFormatType_32BGRA,之后的像素格式转换都由AVFoundation内部实现,省去了手动实现BGRA转yuv。当编码CoreImage的渲染结果,kCVPixelBufferPixelFormatTypeKey对应的值为kCVPixelFormatType_32ARGB。总之,需要和数据源对应起来,否则视频会表现出异常颜色。接下来逐一描述编程步骤。

1. 初始化AVAssetWriterInput

let videoCompressionProperties = [
    AVVideoAverageBitRateKey: 1080 * 1920 * 15.15
]

let videoSettings: [String : Any] = [
        AVVideoCodecKey: AVVideoCodecH264,
        AVVideoWidthKey: 1080,
        AVVideoHeightKey: 1920,
        AVVideoCompressionPropertiesKey: videoCompressionProperties
]

self.videoWriterInput = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: videoSettings)

AVAssetWriterInput还需添加到AVAssetWriter,网上参考代码非常丰富,在此不一一描述。

2. 初始化AVAssetWriterInputPixelBufferAdaptor

var videoWriterInputPixelBufferAdaptor: AVAssetWriterInputPixelBufferAdaptor?
//-------
let sourcePixelBufferAttributes = [
        kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA,
        kCVPixelBufferWidthKey as String: 1080,
        kCVPixelBufferHeightKey as String: 1920
]

self.videoWriterInputPixelBufferAdaptor = AVAssetWriterInputPixelBufferAdaptor(
        assetWriterInput: self.videoWriterInput!,
        sourcePixelBufferAttributes: sourcePixelBufferAttributes
)

AVAssetWriterInputPixelBufferAdaptor是否应该在AVAssetWriterInput添加到AVAssetWriter前初始化?经实验,这一操作的顺序不影响程序的正常运行,在AVAssetWriter.startWriting()前初始化即可。

3. CVPixelBuffer写入AVAssetWriterInputPixelBufferAdaptor

self.videoWriterInputPixelBufferAdaptor!.assetWriterInput.isReadyForMoreMediaData {
    let whetherPixelBufferAppendedtoAdaptor = self.videoWriterInputPixelBufferAdaptor!.append(renderPixelBuffer!, withPresentationTime: presentationTime)
    if whetherPixelBufferAppendedtoAdaptor {
        print("adaptor appended CVPixelBuffer successfully")
    } else {
        print("adaptor appended CVPixelBuffer  failed")
    }
}

renderPixelBuffer为Metal渲染的目标CVPixelBuffer,即它存储了最终的渲染结果,细节可参考Metal Camera开发4:渲染到CVPixelBuffer

4. 讨论:指定BT. 709 YUV转RGB矩阵

GPUImageMovieWriter类的createDataFBO方法有这么一段注释:

/* AVAssetWriter will use BT.601 conversion matrix for RGB to YCbCr conversion
 * regardless of the kCVImageBufferYCbCrMatrixKey value.
 * Tagging the resulting video file as BT.601, is the best option right now.
 * Creating a proper BT.709 video is not possible at the moment.
 */

在iOS 10的测试过程中,发现已支持kCVImageBufferYCbCrMatrixKey设置为kCVImageBufferTransferFunction_ITU_R_709_2。参考代码如下。

CVBufferSetAttachment(pixelBuffer!, kCVImageBufferColorPrimariesKey, kCVImageBufferColorPrimaries_ITU_R_709_2, CVAttachmentMode.shouldPropagate)
CVBufferSetAttachment(pixelBuffer!, kCVImageBufferYCbCrMatrixKey, kCVImageBufferTransferFunction_ITU_R_709_2, CVAttachmentMode.shouldPropagate)
CVBufferSetAttachment(pixelBuffer!, kCVImageBufferTransferFunctionKey, kCVImageBufferTransferFunction_ITU_R_709_2, CVAttachmentMode.shouldPropagate)

FFmpeg读取生成的视频信息如下所示。

AVAssetWriter已支持ITU_R_709
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 在保证视频图像质量的前提下,HEVC通过增加一定的计算复杂度,可以实现码流在H.264/AVC的基础上降低50%。...
    加刘景长阅读 8,293评论 0 6
  • 由于H.264等压缩算法都是在YUV的颜色空间上进行的,所有在进行压缩前,首先要进行颜色空间的转换。如果摄像头采集...
    眷卿三世阅读 13,795评论 2 6
  • 本文档通过Metal compute shader对摄像头当前捕获的画面进行简单的Gamma校正,绘制到屏幕(MT...
    熊皮皮阅读 5,988评论 5 11
  • “你会像爱自己那样爱她吗?” “不会。” “原因。” “因为机器不可能爱我,我更不可能爱她。” 对于人工智能,你想...
    青_浦阅读 250评论 0 1
  • 01 几天前听到交广的一个栏目,讨论的话题就是什么不能将就。 有一位听众是这么说的,第一不能将就的是老婆,第二不能...
    青叶11阅读 623评论 0 1

友情链接更多精彩内容