Created
July 17, 2018 12:44
-
-
Save shalyf/a001492d364954aaa3f6dbc3d04b81a5 to your computer and use it in GitHub Desktop.
CVPixelBuffer转Data,以及Data转CVPixelBuffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func getData(from pixelBuffer: CVPixelBuffer) -> Data { | |
//TODO:可以通过CVPixelBufferGetPlaneCount判断一下pixelBuffer是否有两个Plane | |
CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0)) | |
let pixelFormatType = CVPixelBufferGetPixelFormatType(pixelBuffer) | |
let width = CVPixelBufferGetWidth(pixelBuffer) | |
let height = CVPixelBufferGetHeight(pixelBuffer) | |
var data = Data() | |
let yBaseAddress = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0) | |
let yBytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 0) | |
let yLength = yBytesPerRow * height | |
data.append(Data(bytes: yBaseAddress!, count: yLength)) | |
let cbcrBaseAddress = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1) | |
let cbcrBytesPerRow = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 1) | |
let cbcrLength = cbcrBytesPerRow * height / 2 | |
data.append(Data(bytes: cbcrBaseAddress!, count: cbcrLength)) | |
CVPixelBufferUnlockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0)) | |
return data | |
} | |
func configPixelBufferPool() -> Bool { | |
let attributes: [CFString: Any] = [ | |
kCVPixelBufferPixelFormatTypeKey: pixelFormatType, | |
kCVPixelBufferWidthKey: width, | |
kCVPixelBufferHeightKey: height, | |
kCVPixelBufferBytesPerRowAlignmentKey: yBytesPerRow | |
] | |
let status = CVPixelBufferPoolCreate(kCFAllocatorDefault, nil, attributes as CFDictionary, &pixelBufferPool) | |
return status == kCVReturnSuccess | |
} | |
func getPixelBuffer(from data: Data) -> CVPixelBuffer? { | |
guard data.count == yLength + cbcrLength, | |
let pixelBufferPool = pixelBufferPool else { return nil } | |
var _pixelBuffer: CVPixelBuffer? | |
//用CVPixelBufferPoolCreatePixelBuffer创建CVPixelBuffer会更快一些 | |
let status = CVPixelBufferPoolCreatePixelBuffer(nil, pixelBufferPool, &_pixelBuffer) | |
guard status == kCVReturnSuccess, let pixelBuffer = _pixelBuffer else { | |
print("CVPixelBufferPoolCreatePixelBuffer error") | |
return nil | |
} | |
//把data转换为指针,用指针进行memcpy操作,比用subdata快上百倍 | |
let srcPtr = data.withUnsafeBytes({ UnsafeRawPointer($0) }) | |
CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0)) | |
let yBaseAddress = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0) | |
memcpy(yBaseAddress, srcPtr, yLength) | |
let cbcrBaseAddress = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1) | |
memcpy(cbcrBaseAddress, srcPtr.advanced(by: yLength), cbcrLength) | |
CVPixelBufferUnlockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0)) | |
return pixelBuffer | |
} |
谢谢回复。
我目前需要将 从Broadcast Upload Extension
中获取到的CVPixelBuffer
数据通过socket
传递给主项目,想问下CVPixelBuffer
有什么压缩的好方法么?
Broadcast Upload Extension出的图像好像是yuv420的,再压缩颜色就要失真了,转成data通过socket传给主程序就行了
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
有这些信息才知道怎么把data还原成CVPixelBuffer,我写了一个struct用来存储length等信息,这些信息可以从getData中获取