opengl_video_widget.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #pragma once
  2. #include <QMutex>
  3. #include <QOpenGLFunctions>
  4. #include <QOpenGLShaderProgram>
  5. #include <QOpenGLTexture>
  6. #include <QOpenGLWidget>
  7. #include <QTimer>
  8. #include <QImage>
  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. bool Render(AVFrame* frame);
  30. // 原有接口
  31. void updateFrame(const VideoFrame& frame);
  32. void clearFrame();
  33. // 添加从 AVFrame 转换的方法
  34. bool convertFromAVFrame(AVFrame* frame);
  35. void setKeepAspectRatio(bool keep) { m_keepAspectRatio = keep; update(); }
  36. bool keepAspectRatio() const { return m_keepAspectRatio; }
  37. // 效果控制接口
  38. void setGray(bool on);
  39. void setThreshold(bool on, float value = 0.5f);
  40. void setBlur(bool on, float radius = 1.0f);
  41. void setReverse(bool on);
  42. void setColorReduce(bool on, int level = 8);
  43. void setGamma(bool on, float gamma = 1.0f);
  44. void setContrastBright(bool on, float contrast = 1.0f, float brightness = 0.0f);
  45. void setMirror(bool on);
  46. void setNoVideoTip(const QString& tip);
  47. void showEndTip(const QString& tip);
  48. protected:
  49. void initializeGL() override;
  50. void paintGL() override;
  51. void resizeGL(int width, int height) override;
  52. private:
  53. QOpenGLShaderProgram* m_program;
  54. GLuint m_textureId; // 使用原生OpenGL纹理ID
  55. // 存储原始帧数据
  56. unsigned char* m_frameData;
  57. int m_frameWidth;
  58. int m_frameHeight;
  59. int m_frameFormat; // 可以用来表示像素格式
  60. QMutex m_mutex;
  61. bool m_frameUpdated;
  62. bool m_initialized;
  63. // 顶点和纹理坐标
  64. GLfloat m_vertices[8];
  65. GLfloat m_texCoords[8];
  66. bool m_keepAspectRatio = true;
  67. // 效果参数
  68. bool m_gray = false;
  69. bool m_threshold = false;
  70. float m_thresholdValue = 0.5f;
  71. bool m_blur = false;
  72. float m_blurRadius = 1.0f;
  73. bool m_reverse = false;
  74. bool m_colorReduce = false;
  75. int m_colorReduceLevel = 8;
  76. bool m_gamma = false;
  77. float m_gammaValue = 1.0f;
  78. bool m_contrastBright = false;
  79. float m_contrast = 1.0f;
  80. float m_brightness = 0.0f;
  81. bool m_mirror = false;
  82. QString m_noVideoTip;
  83. QImage m_tipImage;
  84. GLuint m_tipTexture;
  85. float m_tipAngle;
  86. QTimer* m_tipTimer;
  87. void updateTipTexture();
  88. void drawNoVideoTip3D();
  89. };