本文檔描述使用AVAssetWriterInputPixelBufferAdaptor簡化Metal渲染結(jié)果從BGRA到y(tǒng)uv420p的轉(zhuǎn)換,適用于寫成MP4或MOV容器跟畅。以前有人用類似的辦法暴力實(shí)現(xiàn)iOS 8以下的H.264硬解,代價(jià)是消耗用戶的閃存寫次數(shù)溶推。
文檔結(jié)構(gòu):
- 初始化AVAssetWriterInput
- 初始化AVAssetWriterInputPixelBufferAdaptor
- CVPixelBuffer寫入AVAssetWriterInputPixelBufferAdaptor
- 討論:指定BT. 709 YUV轉(zhuǎn)RGB矩陣
Metal Camera開發(fā)4:渲染到CVPixelBuffer實(shí)現(xiàn)了CVPixelBuffer存儲渲染結(jié)果徊件,對于攝像頭開發(fā)奸攻,通常希望將渲染結(jié)果編碼成H.264或H.265(iOS 11支持),iOS平臺的H.264編碼器原生支持yuv420sp VideoRange及FullRange虱痕,而Metal渲染到CVPixelBuffer時(shí)像素格式為BGRA睹耐,顯然需要進(jìn)行像素格式轉(zhuǎn)換,比如使用libyuv部翘、Metal 著色器實(shí)現(xiàn)RGBA轉(zhuǎn)yuv或GLSL實(shí)現(xiàn)硝训。對于將數(shù)據(jù)寫到本地的需求,AVFoundation提供了AVAssetWriterInputPixelBufferAdaptor簡化RGB轉(zhuǎn)yuv的步驟新思。GPUImage也使用了這種方式實(shí)現(xiàn)OpenGL ES的渲染結(jié)果編碼成H.264窖梁。下面描述它的編程過程。
AVAssetWriterInputPixelBufferAdaptor必須配合AVAssetWriterInput使用夹囚,不然沒法調(diào)用構(gòu)造函數(shù)(手動(dòng)斜眼.jpg)纵刘。AVAssetWriterInputPixelBufferAdaptor類的頭文件描述了它的功能:
Pixel buffers not in a natively supported format will be converted internally prior to encoding when possible.
對于編碼Metal的渲染結(jié)果,初始化AVAssetWriterInputPixelBufferAdaptor指定sourcePixelBufferAttributes使用kCVPixelBufferPixelFormatTypeKey為kCVPixelFormatType_32BGRA荸哟,之后的像素格式轉(zhuǎn)換都由AVFoundation內(nèi)部實(shí)現(xiàn)假哎,省去了手動(dòng)實(shí)現(xiàn)BGRA轉(zhuǎn)yuv。當(dāng)編碼CoreImage的渲染結(jié)果敲茄,kCVPixelBufferPixelFormatTypeKey對應(yīng)的值為kCVPixelFormatType_32ARGB位谋。總之堰燎,需要和數(shù)據(jù)源對應(yīng)起來掏父,否則視頻會(huì)表現(xiàn)出異常顏色。接下來逐一描述編程步驟秆剪。
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赊淑,網(wǎng)上參考代碼非常豐富,在此不一一描述仅讽。
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是否應(yīng)該在AVAssetWriterInput添加到AVAssetWriter前初始化陶缺?經(jīng)實(shí)驗(yàn),這一操作的順序不影響程序的正常運(yùn)行洁灵,在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渲染的目標(biāo)CVPixelBuffer,即它存儲了最終的渲染結(jié)果徽千,細(xì)節(jié)可參考Metal Camera開發(fā)4:渲染到CVPixelBuffer苫费。
4. 討論:指定BT. 709 YUV轉(zhuǎn)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的測試過程中,發(fā)現(xiàn)已支持kCVImageBufferYCbCrMatrixKey設(shè)置為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讀取生成的視頻信息如下所示。