opengl_video_renderer.h 4.5 KB

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