DecodeVedio.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef DECODEVEDIO_H
  2. #define DECODEVEDIO_H
  3. #include <QObject>
  4. #include <QQueue>
  5. #include <QTimer>
  6. #include <QMutex>
  7. #include <QWaitCondition>
  8. // #include "threadcontroller.h"
  9. extern "C"
  10. {
  11. // #include <libavcodec/avcodec.h>
  12. #include <libavformat/avformat.h>
  13. // #include <libswscale/swscale.h>
  14. // #include <libavutil/imgutils.h>
  15. }
  16. /**
  17. * 使用方式:
  18. * 1. 初始化FFmpeg:initFFmpeg()
  19. * 2. 开启解码线程:startDecodeVedio()
  20. * 3. 获取一帧图像:getOneImage()
  21. * 4. 停止解码线程:stopDecodeVedio()
  22. * 5. 在初始化完成后,未进入第二步的之前,可以获取视频的宽高信息,也可以设置宽高信息
  23. *
  24. */
  25. class DecodeVedio : public QObject
  26. {
  27. Q_OBJECT
  28. public:
  29. explicit DecodeVedio(QThread* thread, QObject* parent = nullptr);
  30. ~DecodeVedio();
  31. // void setFileName(const QString& fileName) { m_fileName = fileName; }
  32. void initFFmpeg(const QString& fileName); /* 初始化函数 */
  33. void startDecodeVedio(); /* 开始解码视频,开始前先设置视频名称 */
  34. void stopDecodeVedio(); /* 停止解码视频,也是停止线程 */
  35. void setCurrentPos(quint64 pos); /* 设置当前播放位置 */
  36. qint64 getCurrentPos(); /* 获取当前播放位置 */
  37. qint64 getDuration(); /* 获取视频时长 */
  38. bool isRunning() { return m_isRunning; } /* 获取解码线程是否运行 */
  39. void wakeUpCondQueueNoEmpty(); /* 唤醒队列非空条件变量 */
  40. QImage* getOneImage(); /* 获取一帧图像 */
  41. QImage* getOneImageUntilHave(); /* 获取一帧图像,直到有图像为止 */
  42. int getFrameCount(); /* 获取帧数 */
  43. int getVideoWidth() const {return m_width;} /* 获取图像宽度 */
  44. int getVideoHeight() const {return m_height;} /* 获取图像高度 */
  45. void setVideoSize(int width,int height); /* 设置图像宽度和高度 */
  46. signals:
  47. void signal_oneImage(); /* 一帧图像信号 */
  48. private:
  49. void freeFFmpeg(); /* 取消ffmpeg初始化函数 */
  50. void decodeVedio(); /* 解码视频的工作函数,这个就是新线程 */
  51. void exitThread(); /* 退出线程 */
  52. private slots:
  53. void do_startDecodeVedio(); /* 开启解码 */
  54. private:
  55. QThread* m_thread = nullptr; /* 解码线程 */
  56. std::atomic_bool m_isRunning = false; /* 解码线程是否运行 */
  57. QTimer m_startThread; /* 开启线程的定时器 */
  58. std::atomic_bool m_initFFmpeg = false; /* ffmpeg初始化标志 */
  59. std::atomic_bool m_threadStopped = false; /* 停止线程 */
  60. std::atomic_bool m_pauseDecode = false; /* 暂停解码 */
  61. QString m_fileName; /* 解码的视频文件名称 */
  62. AVFormatContext *m_pFormatContext = nullptr; /* 格式上下文,贯穿全局 */
  63. AVCodecContext *m_pCodecCtx = nullptr; /* 解码器上下文 */
  64. AVPacket* m_packet = nullptr; /* 存储解码前的数据,一个数据包 */
  65. AVFrame* m_pFrame = nullptr; /* 存储解码后的一帧数据原始视频编码 */
  66. AVFrame* m_pFrameRGB = nullptr; /* 存储解码后的一帧数据,RGB格式 */
  67. struct SwsContext *m_sws_ctx = nullptr; /* 视频转换上下文 */
  68. uint8_t *m_buffer = nullptr; /* 存储解码后的一帧数据,RGB格式 */
  69. int m_videoStream = -1; /* 记录视频流是第几个流 */
  70. int m_width = 0; /* 图像宽度 */
  71. int m_height = 0; /* 图像高度 */
  72. int m_frameCount = 0; /* 帧数 */
  73. qint64 m_duration = 0; /* 视频时长 */
  74. qint64 m_currentPos = 0; /* 当前播放位置 */
  75. qint64 m_startPos = 0; /* 起始位置,后续操作都需要在这各基础上操作 */
  76. // QImage *m_image = nullptr;
  77. int m_queueMaxNum = 30; /* 这里不是环形队列,最大设置为30帧图像 */
  78. QQueue<QImage*> m_queueImage; /* 视频帧队列 */
  79. QMutex m_mutexQueue; /* 队列互斥锁 */
  80. QWaitCondition m_condQueueNoFull; /* 队列不满,可以入队 */
  81. QWaitCondition m_condQueueNoEmpty; /* 队列非空,可以读取 */
  82. };
  83. #endif /* DECODEVEDIO_H */