【发布时间】:2017-10-03 18:12:23
【问题描述】:
我需要将 YUV 帧转换为从 OTVideoFrame 类获得的 CVPixelBuffer
此类在视频帧中提供一个平面数组,其中包含 y,u,v 帧的三个元素,每个元素的索引为 0,1,2。 p>
@property(非原子,保留)NSPointerArray *planes
视频帧的format
@property(非原子,保留)OTVideoFormat *format
包含框架的宽度、高度、字节数等属性
我需要为收到的OTVideoFrame形式的图像添加过滤器,我已经尝试过这些答案:
这两个链接在 Objective-C 中有解决方案,但我想快速完成。第二个链接中的答案之一是 swift 但缺少有关答案所引用的 YUVFrame 结构的一些信息。
我收到的格式是 NV12
这是我到目前为止一直在尝试做的事情,但我不知道下一步该怎么做:-
/**
* Calcualte the size of each plane from OTVideoFrame.
*
* @param frame The frame to render.
* @return tuple containing three elements for size of each plane
*/
fileprivate func calculatePlaneSize(forFrame frame: OTVideoFrame)
-> (ySize: Int, uSize: Int, vSize: Int){
guard let frameFormat = frame.format
else {
return (0, 0 ,0)
}
let baseSize = Int(frameFormat.imageWidth * frameFormat.imageHeight) * MemoryLayout<GLubyte>.size
return (baseSize, baseSize / 4, baseSize / 4)
}
/**
* Renders a frame to the video renderer.
*
* @param frame The frame to render.
*/
func renderVideoFrame(_ frame: OTVideoFrame) {
let planeSize = calculatePlaneSize(forFrame: frame)
let yPlane = UnsafeMutablePointer<GLubyte>.allocate(capacity: planeSize.ySize)
let uPlane = UnsafeMutablePointer<GLubyte>.allocate(capacity: planeSize.uSize)
let vPlane = UnsafeMutablePointer<GLubyte>.allocate(capacity: planeSize.vSize)
memcpy(yPlane, frame.planes?.pointer(at: 0), planeSize.ySize)
memcpy(uPlane, frame.planes?.pointer(at: 1), planeSize.uSize)
memcpy(vPlane, frame.planes?.pointer(at: 2), planeSize.vSize)
let yStride = frame.format!.bytesPerRow.object(at: 0) as! Int
// multiply chroma strides by 2 as bytesPerRow represents 2x2 subsample
let uStride = frame.format!.bytesPerRow.object(at: 1) as! Int
let vStride = frame.format!.bytesPerRow.object(at: 2) as! Int
let width = frame.format!.imageWidth
let height = frame.format!.imageHeight
var pixelBuffer: CVPixelBuffer? = nil
var err: CVReturn;
err = CVPixelBufferCreate(kCFAllocatorDefault, Int(width), Int(height), kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, nil, &pixelBuffer)
if (err != 0) {
NSLog("Error at CVPixelBufferCreate %d", err)
fatalError()
}
}
从这两个链接的指导下,我尝试创建像素缓冲区,但每次都卡住了,因为此后的 Objective-C 代码转换与我们在 Swift 3 中的转换不同。
【问题讨论】:
标签: ios swift opentok core-video