opengl_video_widget.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #pragma once
  2. #include <QMutex>
  3. #include <QOpenGLFunctions>
  4. #include <QOpenGLShaderProgram>
  5. #include <QOpenGLTexture>
  6. #include <QOpenGLWidget>
  7. extern "C" {
  8. #include <libavutil/frame.h>
  9. }
  10. class OpenGLVideoWidget : public QOpenGLWidget, protected QOpenGLFunctions
  11. {
  12. Q_OBJECT
  13. public:
  14. // 定义 VideoFrame 结构体
  15. struct VideoFrame
  16. {
  17. unsigned char* data;
  18. int width;
  19. int height;
  20. int format;
  21. };
  22. explicit OpenGLVideoWidget(QWidget* parent = nullptr);
  23. ~OpenGLVideoWidget();
  24. // 添加与 VideoRender 类似的接口
  25. bool Open(unsigned int width, unsigned int height);
  26. void Close();
  27. bool Render(AVFrame* frame);
  28. // 原有接口
  29. void updateFrame(const VideoFrame& frame);
  30. void clearFrame();
  31. // 添加从 AVFrame 转换的方法
  32. bool convertFromAVFrame(AVFrame* frame);
  33. void setKeepAspectRatio(bool keep) { m_keepAspectRatio = keep; update(); }
  34. bool keepAspectRatio() const { return m_keepAspectRatio; }
  35. // 效果控制接口
  36. void setGray(bool on);
  37. void setThreshold(bool on, float value = 0.5f);
  38. void setBlur(bool on, float radius = 1.0f);
  39. void setReverse(bool on);
  40. void setColorReduce(bool on, int level = 8);
  41. void setGamma(bool on, float gamma = 1.0f);
  42. void setContrastBright(bool on, float contrast = 1.0f, float brightness = 0.0f);
  43. void setMirror(bool on);
  44. protected:
  45. void initializeGL() override;
  46. void paintGL() override;
  47. void resizeGL(int width, int height) override;
  48. private:
  49. QOpenGLShaderProgram* m_program;
  50. GLuint m_textureId; // 使用原生OpenGL纹理ID
  51. // 存储原始帧数据
  52. unsigned char* m_frameData;
  53. int m_frameWidth;
  54. int m_frameHeight;
  55. int m_frameFormat; // 可以用来表示像素格式
  56. QMutex m_mutex;
  57. bool m_frameUpdated;
  58. bool m_initialized;
  59. // 顶点和纹理坐标
  60. GLfloat m_vertices[8];
  61. GLfloat m_texCoords[8];
  62. bool m_keepAspectRatio = true;
  63. // 效果参数
  64. bool m_gray = false;
  65. bool m_threshold = false;
  66. float m_thresholdValue = 0.5f;
  67. bool m_blur = false;
  68. float m_blurRadius = 1.0f;
  69. bool m_reverse = false;
  70. bool m_colorReduce = false;
  71. int m_colorReduceLevel = 8;
  72. bool m_gamma = false;
  73. float m_gammaValue = 1.0f;
  74. bool m_contrastBright = false;
  75. float m_contrast = 1.0f;
  76. float m_brightness = 0.0f;
  77. bool m_mirror = false;
  78. };