opengl_video_widget.h 2.7 KB

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