opengl_video_widget.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #pragma once
  2. #include <QMutex>
  3. #include <QOpenGLFunctions>
  4. #include <QOpenGLShaderProgram>
  5. #include <QOpenGLTexture>
  6. #include <QOpenGLWidget>
  7. #include "basic/frame.h" // 添加对 AVFrame 的支持
  8. // 定义 VideoFrame 结构体
  9. struct VideoFrame {
  10. unsigned char* data;
  11. int width;
  12. int height;
  13. int format;
  14. };
  15. class OpenGLVideoWidget : public QOpenGLWidget, protected QOpenGLFunctions
  16. {
  17. Q_OBJECT
  18. public:
  19. explicit OpenGLVideoWidget(QWidget* parent = nullptr);
  20. ~OpenGLVideoWidget();
  21. // 添加与 VideoRender 类似的接口
  22. bool Open(unsigned int width, unsigned int height);
  23. void Close();
  24. bool Render(AVFrame* frame);
  25. // 原有接口
  26. void updateFrame(const VideoFrame& frame);
  27. void clearFrame();
  28. // 添加从 AVFrame 转换的方法
  29. bool convertFromAVFrame(AVFrame* frame);
  30. void setKeepAspectRatio(bool keep) { m_keepAspectRatio = keep; update(); }
  31. bool keepAspectRatio() const { return m_keepAspectRatio; }
  32. protected:
  33. void initializeGL() override;
  34. void paintGL() override;
  35. void resizeGL(int width, int height) override;
  36. private:
  37. QOpenGLShaderProgram* m_program;
  38. GLuint m_textureId; // 使用原生OpenGL纹理ID
  39. // 存储原始帧数据
  40. unsigned char* m_frameData;
  41. int m_frameWidth;
  42. int m_frameHeight;
  43. int m_frameFormat; // 可以用来表示像素格式
  44. QMutex m_mutex;
  45. bool m_frameUpdated;
  46. bool m_initialized;
  47. // 顶点和纹理坐标
  48. GLfloat m_vertices[8];
  49. GLfloat m_texCoords[8];
  50. bool m_keepAspectRatio = true;
  51. };