#ifndef AV_PLAYER_OPENGL_VIDEO_RENDERER_H #define AV_PLAYER_OPENGL_VIDEO_RENDERER_H #include #include #include #include #include #include #include #include #include #include #include extern "C" { #include #include #include } #include "../base/media_common.h" namespace av { namespace player { /** * OpenGL视频渲染器类 * 使用QOpenGLWidget进行硬件加速的视频渲染 * 支持YUV420P、NV12等常见格式 */ class OpenGLVideoRenderer : public QOpenGLWidget, protected QOpenGLFunctions { Q_OBJECT public: explicit OpenGLVideoRenderer(QWidget* parent = nullptr); ~OpenGLVideoRenderer(); /** * 初始化视频渲染器 * @param width 视频宽度 * @param height 视频高度 * @param pixelFormat 像素格式 * @param fps 视频帧率(默认25fps) * @return 是否成功 */ bool initialize(int width, int height, AVPixelFormat pixelFormat, double fps = 25.0); /** * 渲染视频帧 * @param frame 视频帧 * @return 是否成功 */ bool renderFrame(const AVFramePtr& frame); /** * 清空显示 */ void clear(); /** * 设置保持宽高比 * @param keepAspectRatio 是否保持宽高比 */ void setKeepAspectRatio(bool keepAspectRatio); /** * 获取是否保持宽高比 */ bool getKeepAspectRatio() const; /** * 设置背景颜色 * @param color 背景颜色 */ void setBackgroundColor(const QColor& color); /** * 获取视频尺寸 */ QSize getVideoSize() const; /** * 获取显示尺寸 */ QSize getDisplaySize() const; /** * 是否已初始化 */ bool isInitialized() const; /** * 设置渲染质量 * @param quality 质量级别 (0-1) */ void setRenderQuality(float quality); /** * 启用/禁用垂直同步 * @param enable 是否启用 */ void setVSync(bool enable); protected: // OpenGL相关 void initializeGL() override; void paintGL() override; void resizeGL(int width, int height) override; // 事件处理 void resizeEvent(QResizeEvent* event) override; private slots: void updateDisplay(); private: /** * 初始化OpenGL资源 */ bool initializeOpenGLResources(); /** * 清理OpenGL资源 */ void cleanupOpenGLResources(); /** * 初始化着色器程序 */ bool initializeShaders(); /** * 初始化顶点数据 */ bool initializeVertexData(); /** * 创建纹理 */ bool createTextures(); /** * 更新纹理数据 */ void updateTextures(const AVFramePtr& frame); /** * 计算显示矩形 */ QRectF calculateDisplayRect() const; /** * 设置投影矩阵 */ void setupProjectionMatrix(); /** * 渲染当前帧 */ void renderCurrentFrame(); /** * 检查OpenGL错误 */ bool checkGLError(const QString& operation); private: // 视频参数 int m_videoWidth; int m_videoHeight; AVPixelFormat m_inputFormat; double m_fps; // OpenGL资源 std::unique_ptr m_shaderProgram; std::unique_ptr m_vertexBuffer; std::unique_ptr m_indexBuffer; std::unique_ptr m_vao; // 纹理 - 参考yuvopenglwidget.cpp GLuint m_textureY; GLuint m_textureU; GLuint m_textureV; // 图像转换器 SwsContext* m_swsContext; uint8_t* m_yuvBuffer; int m_yuvBufferSize; // 显示相关 QColor m_backgroundColor; bool m_keepAspectRatio; float m_renderQuality; bool m_vSyncEnabled; // 状态 std::atomic m_initialized; std::atomic m_glInitialized; std::atomic m_hasFrame; // 更新定时器 QTimer* m_updateTimer; // 线程安全 mutable QMutex m_mutex; // 当前帧数据 struct FrameData { int width; int height; std::vector yData; std::vector uData; std::vector vData; }; FrameData m_currentFrame; }; } // namespace player } // namespace av #endif // AV_PLAYER_OPENGL_VIDEO_RENDERER_H