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

本文详细介绍了如何使用AVAssetWriterInputPixelBufferAdaptor将Metal渲染的BGRA格式转换为yuv420p,并写入MP4或MOV容器。通过设置正确的参数,如像素格式和编码属性,可以避免手动转换,简化iOS上的视频编码流程。同时,讨论了指定BT.709 YUV转RGB矩阵以确保颜色正确。
摘要由CSDN通过智能技术生成

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

文档结构:

初始化AVAssetWriterInput

初始化AVAssetWriterInputPixelBufferAdaptor

CVPixelBuffer写入AVAssetWriterInputPixelBufferAdaptor

讨论:指定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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值