NRRGBCamTextureYUV.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System;
  12. using UnityEngine;
  13. /// <summary> Create a YUV camera texture. </summary>
  14. public class NRRGBCamTextureYUV : CameraModelView
  15. {
  16. /// <summary> The on update. </summary>
  17. public Action<YUVTextureFrame> OnUpdate;
  18. /// <summary> A yuv texture frame. </summary>
  19. public struct YUVTextureFrame
  20. {
  21. /// <summary> The time stamp. </summary>
  22. public UInt64 timeStamp;
  23. /// <summary> The gain </summary>
  24. public UInt32 gain;
  25. /// <summary> The exposureTime </summary>
  26. public UInt32 exposureTime;
  27. /// <summary> The texture y coordinate. </summary>
  28. public Texture2D textureY;
  29. /// <summary> The texture u. </summary>
  30. public Texture2D textureU;
  31. /// <summary> The texture v. </summary>
  32. public Texture2D textureV;
  33. /// <summary> The buffer. </summary>
  34. public byte[] YBuf;
  35. /// <summary> The buffer. </summary>
  36. public byte[] UBuf;
  37. /// <summary> The buffer. </summary>
  38. public byte[] VBuf;
  39. }
  40. /// <summary> Information describing the frame. </summary>
  41. private YUVTextureFrame m_FrameData;
  42. /// <summary> Creates the tex. </summary>
  43. private void CreateTex()
  44. {
  45. if (m_FrameData.textureY == null)
  46. {
  47. m_FrameData = new YUVTextureFrame();
  48. m_FrameData.textureY = new Texture2D(Width, Height, TextureFormat.Alpha8, false);
  49. m_FrameData.textureU = new Texture2D(Width / 2, Height / 2, TextureFormat.Alpha8, false);
  50. m_FrameData.textureV = new Texture2D(Width / 2, Height / 2, TextureFormat.Alpha8, false);
  51. }
  52. }
  53. /// <summary> Gets the texture. </summary>
  54. /// <returns> The texture. </returns>
  55. public YUVTextureFrame GetTexture()
  56. {
  57. if (m_FrameData.textureY == null)
  58. {
  59. CreateTex();
  60. }
  61. return m_FrameData;
  62. }
  63. /// <summary> Default constructor. </summary>
  64. public NRRGBCamTextureYUV() : base(CameraImageFormat.YUV_420_888)
  65. {
  66. CreateTex();
  67. }
  68. /// <summary> Load raw texture data. </summary>
  69. /// <param name="frame"> .</param>
  70. protected override void OnRawDataUpdate(FrameRawData frame)
  71. {
  72. LoadYUVTexture(frame);
  73. OnUpdate?.Invoke(m_FrameData);
  74. }
  75. /// <summary> Loads yuv texture. </summary>
  76. /// <param name="frame"> The frame.</param>
  77. private void LoadYUVTexture(FrameRawData frame)
  78. {
  79. if (frame.data == null || frame.data.Length == 0)
  80. {
  81. NRDebugger.Error("[NRRGBCamTextureYUV] LoadYUVTexture error: frame is null");
  82. return;
  83. }
  84. int size = frame.data.Length;
  85. if (m_FrameData.YBuf == null)
  86. {
  87. m_FrameData.YBuf = new byte[size * 2 / 3];
  88. m_FrameData.UBuf = new byte[size / 6];
  89. m_FrameData.VBuf = new byte[size / 6];
  90. }
  91. if (m_FrameData.textureY == null)
  92. {
  93. CreateTex();
  94. }
  95. m_FrameData.timeStamp = frame.timeStamp;
  96. m_FrameData.gain = frame.gain;
  97. m_FrameData.exposureTime = frame.exposureTime;
  98. Array.Copy(frame.data, 0, m_FrameData.YBuf, 0, m_FrameData.YBuf.Length);
  99. Array.Copy(frame.data, m_FrameData.YBuf.Length, m_FrameData.UBuf, 0, m_FrameData.UBuf.Length);
  100. Array.Copy(frame.data, m_FrameData.YBuf.Length + m_FrameData.UBuf.Length, m_FrameData.VBuf, 0, m_FrameData.VBuf.Length);
  101. m_FrameData.textureY.LoadRawTextureData(m_FrameData.YBuf);
  102. m_FrameData.textureU.LoadRawTextureData(m_FrameData.UBuf);
  103. m_FrameData.textureV.LoadRawTextureData(m_FrameData.VBuf);
  104. m_FrameData.textureY.Apply();
  105. m_FrameData.textureU.Apply();
  106. m_FrameData.textureV.Apply();
  107. }
  108. /// <summary> On texture stopped. </summary>
  109. protected override void OnStopped()
  110. {
  111. base.OnStopped();
  112. GameObject.Destroy(m_FrameData.textureY);
  113. GameObject.Destroy(m_FrameData.textureU);
  114. GameObject.Destroy(m_FrameData.textureV);
  115. m_FrameData.textureY = null;
  116. m_FrameData.textureU = null;
  117. m_FrameData.textureV = null;
  118. m_FrameData.YBuf = null;
  119. m_FrameData.UBuf = null;
  120. m_FrameData.VBuf = null;
  121. }
  122. }
  123. }