opengl_video_renderer.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #ifndef AV_PLAYER_OPENGL_VIDEO_RENDERER_H
  2. #define AV_PLAYER_OPENGL_VIDEO_RENDERER_H
  3. #include <QOpenGLWidget>
  4. #include <QOpenGLFunctions>
  5. #include <QOpenGLTexture>
  6. #include <QOpenGLShaderProgram>
  7. #include <QOpenGLBuffer>
  8. #include <QOpenGLVertexArrayObject>
  9. #include <QtOpenGL/qgl.h>
  10. #include <QMutex>
  11. #include <QTimer>
  12. #include <memory>
  13. #include <atomic>
  14. extern "C" {
  15. #include <libavutil/frame.h>
  16. #include <libavutil/pixfmt.h>
  17. #include <libswscale/swscale.h>
  18. }
  19. #include "../base/media_common.h"
  20. namespace av {
  21. namespace player {
  22. /**
  23. * OpenGL视频渲染器类
  24. * 使用QOpenGLWidget进行硬件加速的视频渲染
  25. * 支持YUV420P、NV12等常见格式
  26. */
  27. class OpenGLVideoRenderer : public QOpenGLWidget, protected QOpenGLFunctions
  28. {
  29. Q_OBJECT
  30. public:
  31. explicit OpenGLVideoRenderer(QWidget* parent = nullptr);
  32. ~OpenGLVideoRenderer();
  33. /**
  34. * 初始化视频渲染器
  35. * @param width 视频宽度
  36. * @param height 视频高度
  37. * @param pixelFormat 像素格式
  38. * @param fps 视频帧率(默认25fps)
  39. * @return 是否成功
  40. */
  41. bool initialize(int width, int height, AVPixelFormat pixelFormat, double fps = 25.0);
  42. /**
  43. * 渲染视频帧
  44. * @param frame 视频帧
  45. * @return 是否成功
  46. */
  47. bool renderFrame(const AVFramePtr& frame);
  48. /**
  49. * 清空显示
  50. */
  51. void clear();
  52. /**
  53. * 设置保持宽高比
  54. * @param keepAspectRatio 是否保持宽高比
  55. */
  56. void setKeepAspectRatio(bool keepAspectRatio);
  57. /**
  58. * 获取是否保持宽高比
  59. */
  60. bool getKeepAspectRatio() const;
  61. /**
  62. * 设置背景颜色
  63. * @param color 背景颜色
  64. */
  65. void setBackgroundColor(const QColor& color);
  66. /**
  67. * 获取视频尺寸
  68. */
  69. QSize getVideoSize() const;
  70. /**
  71. * 获取显示尺寸
  72. */
  73. QSize getDisplaySize() const;
  74. /**
  75. * 是否已初始化
  76. */
  77. bool isInitialized() const;
  78. /**
  79. * 设置渲染质量
  80. * @param quality 质量级别 (0-1)
  81. */
  82. void setRenderQuality(float quality);
  83. /**
  84. * 启用/禁用垂直同步
  85. * @param enable 是否启用
  86. */
  87. void setVSync(bool enable);
  88. protected:
  89. // OpenGL相关
  90. void initializeGL() override;
  91. void paintGL() override;
  92. void resizeGL(int width, int height) override;
  93. // 事件处理
  94. void resizeEvent(QResizeEvent* event) override;
  95. private slots:
  96. void updateDisplay();
  97. private:
  98. /**
  99. * 初始化OpenGL资源
  100. */
  101. bool initializeOpenGLResources();
  102. /**
  103. * 清理OpenGL资源
  104. */
  105. void cleanupOpenGLResources();
  106. /**
  107. * 初始化着色器程序
  108. */
  109. bool initializeShaders();
  110. /**
  111. * 初始化顶点数据
  112. */
  113. bool initializeVertexData();
  114. /**
  115. * 创建纹理
  116. */
  117. bool createTextures();
  118. /**
  119. * 更新纹理数据
  120. */
  121. void updateTextures(const AVFramePtr& frame);
  122. /**
  123. * 计算显示矩形
  124. */
  125. QRectF calculateDisplayRect() const;
  126. /**
  127. * 设置投影矩阵
  128. */
  129. void setupProjectionMatrix();
  130. /**
  131. * 渲染当前帧
  132. */
  133. void renderCurrentFrame();
  134. /**
  135. * 检查OpenGL错误
  136. */
  137. bool checkGLError(const QString& operation);
  138. private:
  139. // 视频参数
  140. int m_videoWidth;
  141. int m_videoHeight;
  142. AVPixelFormat m_inputFormat;
  143. double m_fps;
  144. // OpenGL资源
  145. std::unique_ptr<QOpenGLShaderProgram> m_shaderProgram;
  146. std::unique_ptr<QOpenGLBuffer> m_vertexBuffer;
  147. std::unique_ptr<QOpenGLBuffer> m_indexBuffer;
  148. std::unique_ptr<QOpenGLVertexArrayObject> m_vao;
  149. // 纹理 - 参考yuvopenglwidget.cpp
  150. GLuint m_textureY;
  151. GLuint m_textureU;
  152. GLuint m_textureV;
  153. // 图像转换器
  154. SwsContext* m_swsContext;
  155. uint8_t* m_yuvBuffer;
  156. int m_yuvBufferSize;
  157. // 显示相关
  158. QColor m_backgroundColor;
  159. bool m_keepAspectRatio;
  160. float m_renderQuality;
  161. bool m_vSyncEnabled;
  162. // 状态
  163. std::atomic<bool> m_initialized;
  164. std::atomic<bool> m_glInitialized;
  165. std::atomic<bool> m_hasFrame;
  166. // 更新定时器
  167. QTimer* m_updateTimer;
  168. // 线程安全
  169. mutable QMutex m_mutex;
  170. // 当前帧数据
  171. struct FrameData {
  172. int width;
  173. int height;
  174. std::vector<uint8_t> yData;
  175. std::vector<uint8_t> uData;
  176. std::vector<uint8_t> vData;
  177. };
  178. FrameData m_currentFrame;
  179. };
  180. } // namespace player
  181. } // namespace av
  182. #endif // AV_PLAYER_OPENGL_VIDEO_RENDERER_H