avopenglwidget.h 3.0 KB

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