Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func getUIImageFromFrame(frame: RTCVideoFrame) -> UIImage?
- {
- let size = Int(frame.width * frame.height)
- let argb : [Int] = [size]
- if frame.rotation.rawValue == 90 || frame.rotation.rawValue == -270
- {
- return nil
- }
- else if frame.rotation.rawValue == 180 || frame.rotation.rawValue == -180
- {
- return nil
- }
- else if frame.rotation.rawValue == 270 || frame.rotation.rawValue == -90
- {
- return nil
- }
- else
- {
- print("check- rotation else")
- let result = convertYuvToArgbRot0(outputArgb: argb, frame: frame)
- // Create UIImage from ARGB pixel data.
- return nil
- }
- }
- func copyByteBuffer(dst: [UInt8]?, src: UnsafePointer<UInt8>) -> [UInt8]
- {
- // Create a new byte array if necessary.
- var out: [UInt8]?
- if dst == nil || dst?.capacity != [src.pointee].capacity
- {
- out = [UInt8([src.pointee].capacity)]
- }
- else
- {
- out = dst
- }
- // Copy the ByteBuffer's contents to the byte array.
- out?.append(src.pointee)
- return out!
- }
- func convertYuvToArgbRot0(outputArgb: [Int], frame: RTCVideoFrame) -> [Int]
- {
- var outputArgbToReturn = outputArgb
- // Calculate the size of the frame
- let size = Int(frame.width * frame.height)
- var yPlane: [UInt8]?
- var uPlane: [UInt8]?
- var vPlane: [UInt8]?
- yPlane = copyByteBuffer(dst: yPlane,src: frame.buffer.toI420().dataY)
- vPlane = copyByteBuffer(dst: vPlane,src: frame.buffer.toI420().dataV)
- uPlane = copyByteBuffer(dst: uPlane,src: frame.buffer.toI420().dataU)
- // Each U/V cell is overlaid on a 2x2 block of Y cells.
- // Loop through the size of the U/V planes, and manage the 2x2 Y block on each iteration.
- var u, v : UInt8?
- var y1, y2, y3, y4 : UInt8?
- var p1, p2, p3, p4 : Int?
- var rowOffset : Int = 0 // Y and RGB array position is offset by an extra row width each iteration, since they're handled as 2x2 sections.
- let uvSize : Int = size / 4
- let uvWidth : Int = Int(frame.width) / 2
- for i in 0...uvSize
- {
- // At the end of each row, increment the Y/RGB row offset by an extra frame width
- if ( i != 0 && ( i % ( uvWidth ) ) == 0 )
- {
- rowOffset += Int(frame.width)
- }
- // Calculate the 2x2 grid indices
- p1 = rowOffset + ( i * 2 )
- p2 = p1! + 1
- p3 = p1! + Int(frame.width)
- p4 = p3! + 1
- // Get the U and V values from the source.
- u = uPlane![i] & 0xff
- v = vPlane![i] & 0xff
- u = u! - 128
- v = v! - 128
- // Get the Y values for the matching 2x2 pixel block
- y1 = yPlane![p1!] & 0xff
- y2 = yPlane![p2!] & 0xff
- y3 = yPlane![p3!] & 0xff
- y4 = yPlane![p4!] & 0xff
- // Convert each YUV pixel to RGB
- outputArgbToReturn[p1!] = convertYuvToArgb(y: y1!,u: u!,v: v!)
- outputArgbToReturn[p2!] = convertYuvToArgb(y: y2!,u: u!,v: v!)
- outputArgbToReturn[p3!] = convertYuvToArgb(y: y3!,u: u!,v: v!)
- outputArgbToReturn[p4!] = convertYuvToArgb(y: y4!,u: u!,v: v!)
- }
- return outputArgbToReturn
- }
- func convertYuvToArgb(y: UInt8,u :UInt8,v: UInt8) -> Int
- {
- var r, g, b: Int?
- // Convert YUV to RGB
- r = Int(Double(y) + 1.402 * Double(v))
- g = Int(Double(y) - 0.344 * Double(u) + 0.714 * Double(v))
- b = Int(Double(y) + 1.772 * Double(u))
- // Clamp RGB values to [0,255]
- r = ( r! > 255 ) ? 255 : ( r! < 0 ) ? 0 : r
- g = ( g! > 255 ) ? 255 : ( g! < 0 ) ? 0 : g
- b = ( b! > 255 ) ? 255 : ( b! < 0 ) ? 0 : b
- // Shift the RGB values into position in the final ARGB pixel
- return 0xff000000 | (b!<<16) | (g!<<8) | r!
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement