Orr Hsiao
Orr Hsiao

Reputation: 1

How to convert RTCVideoFrame to YUV in webrtc iOS

This is my code, the converted yuv rendering is sometimes normal, sometimes not normal, is there any problem?

    func convertRTCVideoFrameToYUV(frame: RTCVideoFrame) -> (yData: Data, uData: Data, vData: Data) {
        
        let i420Buffer = frame.buffer.toI420()
        
        let width = Int(i420Buffer.width)
        let height = Int(i420Buffer.height)
        
        let ySize = Int(width * height)
        
//        let uvWidth = (width + 1) / 2
//        let uvHeight = (height + 1) / 2
        
//        let uSize = Int(uvWidth * uvHeight)
//        let vSize = Int(uvWidth * uvHeight)
        let uSize = Int((width / 2) * (height / 2))
        let vSize = Int((width / 2) * (height / 2))
        
        var yData = Data(count: ySize)
        var uData = Data(count: uSize)
        var vData = Data(count: vSize)
        
        yData.withUnsafeMutableBytes { yBuffer in
            guard let yPtr = yBuffer.baseAddress?.assumingMemoryBound(to: UInt8.self) else { return }
            let yPlane = i420Buffer.dataY
            memcpy(yPtr, yPlane, ySize)
        }
        
        uData.withUnsafeMutableBytes { uBuffer in
            guard let uPtr = uBuffer.baseAddress?.assumingMemoryBound(to: UInt8.self) else { return }
            let uPlane = i420Buffer.dataU
            memcpy(uPtr, uPlane, uSize)
        }
        
        vData.withUnsafeMutableBytes { vBuffer in
            guard let vPtr = vBuffer.baseAddress?.assumingMemoryBound(to: UInt8.self) else { return }
            let vPlane = i420Buffer.dataV
            memcpy(vPtr, vPlane, vSize)
        }
        
//        adjustForRotation(&yData, &uData, &vData, width, height, frame.rotation)

        return (yData, uData, vData)
    }

I have tried many conversion methods, but can not find the problem of rendering flower screen

Upvotes: 0

Views: 41

Answers (0)

Related Questions