ShaderYUV420.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #include "ShaderYUV420.h"
  2. #include <QOpenGLWidget>
  3. #include <QOpenGLFunctions_3_3_Core>
  4. #include "spdlog/spdlog.h"
  5. Image_YUV420P::Image_YUV420P(Image_YUV420P&& other): width(other.width), height(other.height),
  6. yData(std::move(other.yData)), uData(std::move(other.uData)), vData(std::move(other.vData))
  7. {}
  8. Image_YUV420P::Image_YUV420P(const Image_YUV420P& other)
  9. : width(other.width), height(other.height),
  10. yData(other.yData), uData(other.uData), vData(other.vData)
  11. {}
  12. Image_YUV420P& Image_YUV420P::operator=(Image_YUV420P&& other)
  13. {
  14. if (this != &other) {
  15. width = other.width;
  16. height = other.height;
  17. yData = std::move(other.yData);
  18. uData = std::move(other.uData);
  19. vData = std::move(other.vData);
  20. }
  21. return *this;
  22. }
  23. Image_YUV420P& Image_YUV420P::operator=(const Image_YUV420P& other)
  24. {
  25. if (this != &other) {
  26. width = other.width;
  27. height = other.height;
  28. yData = other.yData;
  29. uData = other.uData;
  30. vData = other.vData;
  31. }
  32. return *this;
  33. }
  34. bool Image_YUV420P::isValid() const
  35. {
  36. return width > 0 && height > 0 && !yData.isEmpty() && !uData.isEmpty() && !vData.isEmpty();
  37. }
  38. void Image_YUV420P::clear()
  39. {
  40. width = 0;
  41. height = 0;
  42. yData.clear();
  43. uData.clear();
  44. vData.clear();
  45. }
  46. ShaderYUV420::ShaderYUV420()
  47. {
  48. }
  49. ShaderYUV420::~ShaderYUV420()
  50. {
  51. QOpenGLFunctions_3_3_Core *gl = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
  52. if (m_VAO != 0) {
  53. if (gl) {
  54. gl->glDeleteVertexArrays(1, &m_VAO);
  55. gl->glDeleteBuffers(1, &m_VBO);
  56. gl->glDeleteBuffers(1, &m_EBO);
  57. }
  58. }
  59. }
  60. /* 初始化形状 */
  61. GLuint ShaderYUV420::initShape()
  62. {
  63. const int vertexCount = 4; // 顶点数量
  64. const int vertexSize = 8; // 顶点大小
  65. /* 矩形位置,铺满屏幕 */
  66. float vertices [vertexCount * vertexSize] {
  67. /* ----- 位置 -----|-------- 颜色 ----------|----- 纹理坐标 ------ */
  68. -1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, // 左下
  69. 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, // 右下
  70. 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // 右上
  71. -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f // 右上
  72. };
  73. const int indexCount = 6; // 索引数量
  74. /* 顶点顺序 */
  75. uint32_t indices [indexCount] {
  76. 0, 1, 2, // 第一个三角形
  77. 2, 3, 0 // 第二个三角形
  78. };
  79. QOpenGLFunctions_3_3_Core *gl = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
  80. if (!gl) {
  81. SPDLOG_ERROR("Failed to get OpenGL functions");
  82. return 0;
  83. }
  84. /* -------------------------------------------------------------------------------------
  85. * 顶点数组对象
  86. * ------------------------------------------------------------------------------------ */
  87. gl->glGenVertexArrays(1, &m_VAO); // 生成一个VAO,返回的ID存储在VAO1中
  88. gl->glBindVertexArray(m_VAO); // 绑定VAO
  89. /* -------------------------------------------------------------------------------------
  90. * 创建顶点数组缓冲区
  91. * ------------------------------------------------------------------------------------ */
  92. gl->glGenBuffers(1, &m_VBO);
  93. gl->glBindBuffer(GL_ARRAY_BUFFER, m_VBO); // 绑定VBO
  94. gl->glBufferData(GL_ARRAY_BUFFER, vertexCount * vertexSize * sizeof(float), vertices, GL_STATIC_DRAW);
  95. /* -------------------------------------------------------------------------------------
  96. * 创建EBO
  97. * ------------------------------------------------------------------------------------ */
  98. gl->glGenBuffers(1, &m_EBO);
  99. gl->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_EBO);
  100. gl->glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount * sizeof(uint32_t), indices, GL_STATIC_DRAW); // 绑定EBO
  101. /* -------------------------------------------------------------------------------------
  102. * 链接顶点属性
  103. * ------------------------------------------------------------------------------------ */
  104. // 设置顶点属性指针,告诉OpenGL如何解析顶点数据
  105. gl->glVertexAttribPointer(
  106. 0, /* 顶点属性位置,0表示第一个属性 */
  107. 3, /* 每个顶点属性的大小,这里是3,表示x、y、z坐标 */
  108. GL_FLOAT, GL_FALSE, /* 数据类型和是否标准化 */
  109. vertexSize * sizeof(float), /* 步长,表示每个顶点属性之间的间隔 */
  110. (void*)0 /* 偏移量,表示该属性(顶点坐标)从第0个字节开始 */
  111. );
  112. /* 启用顶点属性 */
  113. gl->glEnableVertexAttribArray(0);
  114. // 设置颜色位置,最后一个参数表示从第3个字节开始,倒数第二个参数是两个数据之间起始位置间隔8个字节
  115. gl->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, vertexSize * sizeof(float), (void*)(3*sizeof(float)));
  116. gl->glEnableVertexAttribArray(1);
  117. /* 设置纹理位置 */
  118. gl->glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, vertexSize * sizeof(float), (void*)(6*sizeof(float))); // 纹理坐标位置
  119. gl->glEnableVertexAttribArray(2); // 启用纹理坐标属性
  120. return m_VAO;
  121. }
  122. /* 初始化纹理对象 */
  123. bool ShaderYUV420::initTextures()
  124. {
  125. int unit = 0;
  126. unit = createTexture(m_textureYName, 0);
  127. if(unit < 0) {
  128. SPDLOG_ERROR("Failed to create Y texture");
  129. return false;
  130. }
  131. unit = createTexture(m_textureUName, 1);
  132. if(unit < 0) {
  133. SPDLOG_ERROR("Failed to create U texture");
  134. return false;
  135. }
  136. unit = createTexture(m_textureVName, 2);
  137. if(unit < 0) {
  138. SPDLOG_ERROR("Failed to create V texture");
  139. return false;
  140. }
  141. return true;
  142. }
  143. /* 绘制图形 */
  144. void ShaderYUV420::drawShape()
  145. {
  146. QOpenGLFunctions_3_3_Core *gl = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
  147. /* 需要在使用时手动绑定VAO,绘制图形 */
  148. gl->glBindVertexArray(m_VAO); // 绑定VAO
  149. gl->glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // 绘制四边形
  150. gl->glBindVertexArray(0); // 解绑VAO
  151. }
  152. /* 刷新一帧 */
  153. bool ShaderYUV420::refreshFrameYUV420(const Image_YUV420P& yuvData)
  154. {
  155. if (!yuvData.isValid()) {
  156. SPDLOG_ERROR("Invalid YUV420 data");
  157. return false;
  158. }
  159. QOpenGLTexture *textureY = m_mapTexture.find(0)->second;
  160. QOpenGLTexture *textureU = m_mapTexture.find(1)->second;
  161. QOpenGLTexture *textureV = m_mapTexture.find(2)->second;
  162. if(textureY == nullptr || textureU == nullptr || textureV == nullptr) {
  163. SPDLOG_ERROR("Failed to get YUV textures");
  164. return false;
  165. }
  166. if(yuvData.width != m_lastYUVSize.width() || yuvData.height != m_lastYUVSize.height())
  167. {
  168. /* 如果YUV数据的大小发生变化,则需要重新创建纹理 */
  169. textureY->destroy();
  170. textureU->destroy();
  171. textureV->destroy();
  172. textureY->create();
  173. textureU->create();
  174. textureV->create();
  175. textureY->setSize(yuvData.width, yuvData.height, 1); // 设置Y分量纹理大小
  176. textureY->setFormat(QOpenGLTexture::R8_UNorm); // 设置Y分量纹理格式
  177. textureY->allocateStorage(); // 分配Y分量纹理存储空间
  178. textureU->setSize(yuvData.width / 2, yuvData.height / 2, 1); // 设置U分量纹理大小
  179. textureU->setFormat(QOpenGLTexture::R8_UNorm); // 设置U分量纹理格式
  180. textureU->allocateStorage(); // 分配U分量纹理存储空间
  181. textureV->setSize(yuvData.width / 2, yuvData.height / 2, 1); // 设置V分量纹理大小
  182. textureV->setFormat(QOpenGLTexture::R8_UNorm); // 设置V分量纹理格式
  183. textureV->allocateStorage(); // 分配V分量纹理存储空间
  184. m_lastYUVSize = QSize(yuvData.width, yuvData.height);
  185. }
  186. /* 设置Y分量纹理 */
  187. textureY->setData(QOpenGLTexture::Red, QOpenGLTexture::UInt8, yuvData.yData.constData());
  188. textureY->generateMipMaps(); // 生成多级渐远纹理
  189. /* 设置U分量纹理 */
  190. textureU->setData(QOpenGLTexture::Red, QOpenGLTexture::UInt8, yuvData.uData.constData());
  191. textureU->generateMipMaps(); // 生成多级渐远纹理
  192. /* 设置V分量纹理 */
  193. textureV->setData(QOpenGLTexture::Red, QOpenGLTexture::UInt8, yuvData.vData.constData());
  194. textureV->generateMipMaps(); // 生成多级渐远纹理
  195. printGLerror("ShaderYUV420::refreshFrameYUV420");
  196. return true;
  197. }
  198. /* 刷新一帧 */
  199. bool ShaderYUV420::refreshFrameRGBA(const QImage& image, int textureUnit)
  200. {
  201. m_yuvData.clear(); // 清空YUV数据
  202. convertRGBAToYUV420(image, m_yuvData);
  203. refreshFrameYUV420(m_yuvData);
  204. return true;
  205. }
  206. /* RGBA转换成YUV420 */
  207. bool ShaderYUV420::convertRGBAToYUV420(const QImage& image, Image_YUV420P& yuvData)
  208. {
  209. int width = image.width();
  210. int height = image.height();
  211. const int ySize = yuvData.width * yuvData.height;
  212. const int uvSize = ySize / 4;
  213. QImage image_rgb(width, height, QImage::Format_RGB888);
  214. yuvData.width = width;
  215. yuvData.height = height;
  216. yuvData.yData.resize(ySize);
  217. yuvData.uData.resize(uvSize);
  218. yuvData.vData.resize(uvSize);
  219. for (int y = 0; y < height; ++y) {
  220. for (int x = 0; x < width; ++x) {
  221. QColor color = image.pixelColor(x, y);
  222. int r = color.red();
  223. int g = color.green();
  224. int b = color.blue();
  225. int yIndex = y * width + x;
  226. yuvData.yData[yIndex] = static_cast<unsigned char>((0.257 * r) + (0.504 * g) + (0.098 * b) + 16);
  227. if (y % 2 == 0 && x % 2 == 0) {
  228. int uvIndex = (y / 2) * (width / 2) + (x / 2);
  229. yuvData.uData[uvIndex] = static_cast<unsigned char>((-0.148 * r) - (0.291 * g) + (0.439 * b) + 128);
  230. yuvData.vData[uvIndex] = static_cast<unsigned char>((0.439 * r) - (0.368 * g) - (0.071 * b) + 128);
  231. }
  232. }
  233. }
  234. return true;
  235. }