video_play_thread.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #ifndef AVPLAYER2_VIDEO_PLAY_THREAD_H
  2. #define AVPLAYER2_VIDEO_PLAY_THREAD_H
  3. #pragma once
  4. #include "ThreadBase.h"
  5. #include <QDebug>
  6. #include <QImage>
  7. #include <QRegularExpression>
  8. #include "packets_sync.h"
  9. #define PRINT_VIDEO_BUFFER_INFO 0
  10. typedef struct Video_Resample
  11. {
  12. AVFrame* pFrameRGB{nullptr};
  13. uint8_t* buffer_RGB{nullptr};
  14. struct SwsContext* sws_ctx{nullptr};
  15. } Video_Resample;
  16. class VideoPlayThread : public ThreadBase
  17. {
  18. public:
  19. explicit VideoPlayThread(VideoState* pState = nullptr);
  20. ~VideoPlayThread();
  21. bool init_resample_param(AVCodecContext* pVideo, bool bHardware = false);
  22. void stop() override;
  23. void setOnFrameReady(std::function<void(AVFrame*)> cb) { m_onFrameReady = std::move(cb); }
  24. void setOnSubtitleReady(std::function<void(const QString&)> cb) { m_onSubtitleReady = std::move(cb); }
  25. protected:
  26. void run() override;
  27. private:
  28. void video_refresh(VideoState* is, double* remaining_time);
  29. void video_image_display(VideoState* is);
  30. void video_display(VideoState* is);
  31. void final_resample_param();
  32. inline int compute_mod(int a, int b) { return a < 0 ? (a % b + b) : (a % b); }
  33. void parse_subtitle_ass(const QString& text);
  34. private:
  35. VideoState* m_pState{nullptr};
  36. Video_Resample m_Resample;
  37. const static QRegularExpression m_assFilter;
  38. const static QRegularExpression m_assNewLineReplacer;
  39. std::function<void(AVFrame*)> m_onFrameReady;
  40. std::function<void(const QString&)> m_onSubtitleReady;
  41. };
  42. #endif // AVPLAYER2_VIDEO_PLAY_THREAD_H