Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. func getUIImageFromFrame(frame: RTCVideoFrame) -> UIImage?
  2. {
  3. let size = Int(frame.width * frame.height)
  4. let argb : [Int] = [size]
  5.  
  6. if frame.rotation.rawValue == 90 || frame.rotation.rawValue == -270
  7. {
  8. return nil
  9. }
  10. else if frame.rotation.rawValue == 180 || frame.rotation.rawValue == -180
  11. {
  12. return nil
  13. }
  14. else if frame.rotation.rawValue == 270 || frame.rotation.rawValue == -90
  15. {
  16. return nil
  17. }
  18. else
  19. {
  20. print("check- rotation else")
  21.  
  22. let result = convertYuvToArgbRot0(outputArgb: argb, frame: frame)
  23.  
  24. // Create UIImage from ARGB pixel data.
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. return nil
  32. }
  33. }
  34.  
  35. func copyByteBuffer(dst: [UInt8]?, src: UnsafePointer<UInt8>) -> [UInt8]
  36. {
  37. // Create a new byte array if necessary.
  38. var out: [UInt8]?
  39.  
  40. if dst == nil || dst?.capacity != [src.pointee].capacity
  41. {
  42. out = [UInt8([src.pointee].capacity)]
  43. }
  44. else
  45. {
  46. out = dst
  47. }
  48.  
  49. // Copy the ByteBuffer's contents to the byte array.
  50. out?.append(src.pointee)
  51.  
  52. return out!
  53. }
  54.  
  55. func convertYuvToArgbRot0(outputArgb: [Int], frame: RTCVideoFrame) -> [Int]
  56. {
  57. var outputArgbToReturn = outputArgb
  58.  
  59. // Calculate the size of the frame
  60. let size = Int(frame.width * frame.height)
  61.  
  62. var yPlane: [UInt8]?
  63. var uPlane: [UInt8]?
  64. var vPlane: [UInt8]?
  65.  
  66. yPlane = copyByteBuffer(dst: yPlane,src: frame.buffer.toI420().dataY)
  67. vPlane = copyByteBuffer(dst: vPlane,src: frame.buffer.toI420().dataV)
  68. uPlane = copyByteBuffer(dst: uPlane,src: frame.buffer.toI420().dataU)
  69.  
  70. // Each U/V cell is overlaid on a 2x2 block of Y cells.
  71. // Loop through the size of the U/V planes, and manage the 2x2 Y block on each iteration.
  72. var u, v : UInt8?
  73. var y1, y2, y3, y4 : UInt8?
  74. var p1, p2, p3, p4 : Int?
  75. 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.
  76.  
  77. let uvSize : Int = size / 4
  78. let uvWidth : Int = Int(frame.width) / 2
  79.  
  80. for i in 0...uvSize
  81. {
  82. // At the end of each row, increment the Y/RGB row offset by an extra frame width
  83. if ( i != 0 && ( i % ( uvWidth ) ) == 0 )
  84. {
  85. rowOffset += Int(frame.width)
  86. }
  87.  
  88. // Calculate the 2x2 grid indices
  89. p1 = rowOffset + ( i * 2 )
  90. p2 = p1! + 1
  91. p3 = p1! + Int(frame.width)
  92. p4 = p3! + 1
  93.  
  94. // Get the U and V values from the source.
  95. u = uPlane![i] & 0xff
  96. v = vPlane![i] & 0xff
  97. u = u! - 128
  98. v = v! - 128
  99.  
  100. // Get the Y values for the matching 2x2 pixel block
  101. y1 = yPlane![p1!] & 0xff
  102. y2 = yPlane![p2!] & 0xff
  103. y3 = yPlane![p3!] & 0xff
  104. y4 = yPlane![p4!] & 0xff
  105.  
  106. // Convert each YUV pixel to RGB
  107. outputArgbToReturn[p1!] = convertYuvToArgb(y: y1!,u: u!,v: v!)
  108. outputArgbToReturn[p2!] = convertYuvToArgb(y: y2!,u: u!,v: v!)
  109. outputArgbToReturn[p3!] = convertYuvToArgb(y: y3!,u: u!,v: v!)
  110. outputArgbToReturn[p4!] = convertYuvToArgb(y: y4!,u: u!,v: v!)
  111. }
  112.  
  113. return outputArgbToReturn
  114. }
  115.  
  116. func convertYuvToArgb(y: UInt8,u :UInt8,v: UInt8) -> Int
  117. {
  118. var r, g, b: Int?
  119.  
  120. // Convert YUV to RGB
  121. r = Int(Double(y) + 1.402 * Double(v))
  122. g = Int(Double(y) - 0.344 * Double(u) + 0.714 * Double(v))
  123. b = Int(Double(y) + 1.772 * Double(u))
  124.  
  125. // Clamp RGB values to [0,255]
  126. r = ( r! > 255 ) ? 255 : ( r! < 0 ) ? 0 : r
  127. g = ( g! > 255 ) ? 255 : ( g! < 0 ) ? 0 : g
  128. b = ( b! > 255 ) ? 255 : ( b! < 0 ) ? 0 : b
  129.  
  130. // Shift the RGB values into position in the final ARGB pixel
  131. return 0xff000000 | (b!<<16) | (g!<<8) | r!
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement