videodecode.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /******************************************************************************
  2. * @文件名 videodecode.h
  3. * @功能 视频解码类,在这个类中调用ffmpeg打开视频进行解码
  4. *
  5. * @开发者 mhf
  6. * @邮箱 1603291350@qq.com
  7. * @时间 2022/09/15
  8. * @备注
  9. *****************************************************************************/
  10. #ifndef VIDEODECODE_H
  11. #define VIDEODECODE_H
  12. #include <QString>
  13. #include <QSize>
  14. #include <qlist.h>
  15. struct AVFormatContext;
  16. struct AVCodecContext;
  17. struct AVRational;
  18. struct AVPacket;
  19. struct AVFrame;
  20. struct AVCodec;
  21. struct SwsContext;
  22. struct AVBufferRef;
  23. class QImage;
  24. class VideoDecode
  25. {
  26. public:
  27. VideoDecode();
  28. ~VideoDecode();
  29. bool open(const QString& url = QString()); // 打开媒体文件,或者流媒体rtmp、strp、http
  30. QImage read(); // 读取视频图像
  31. void close(); // 关闭
  32. bool isEnd(); // 是否读取完成
  33. const qint64& pts(); // 获取当前帧显示时间
  34. void setHWDecoder(bool flag); // 是否使用硬件解码器
  35. bool isHWDecoder();
  36. private:
  37. void initFFmpeg(); // 初始化ffmpeg库(整个程序中只需加载一次)
  38. void initHWDecoder(const AVCodec* codec); // 初始化硬件解码器
  39. bool initObject(); // 初始化对象
  40. bool dataCopy(); // 硬件解码完成需要将数据从GPU复制到CPU
  41. void showError(int err); // 显示ffmpeg执行错误时的错误信息
  42. qreal rationalToDouble(AVRational* rational); // 将AVRational转换为double
  43. void clear(); // 清空读取缓冲
  44. void free(); // 释放
  45. private:
  46. AVFormatContext* m_formatContext = nullptr; // 解封装上下文
  47. AVCodecContext* m_codecContext = nullptr; // 解码器上下文
  48. SwsContext* m_swsContext = nullptr; // 图像转换上下文
  49. AVPacket* m_packet = nullptr; // 数据包
  50. AVFrame* m_frame = nullptr; // 解码后的视频帧
  51. AVFrame* m_frameHW = nullptr; // 硬件解码后的视频帧
  52. int m_videoIndex = 0; // 视频流索引
  53. qint64 m_totalTime = 0; // 视频总时长
  54. qint64 m_totalFrames = 0; // 视频总帧数
  55. qint64 m_obtainFrames = 0; // 视频当前获取到的帧数
  56. qint64 m_pts = 0; // 图像帧的显示时间
  57. qreal m_frameRate = 0; // 视频帧率
  58. QSize m_size; // 视频分辨率大小
  59. char* m_error = nullptr; // 保存异常信息
  60. bool m_end = false; // 视频读取完成
  61. uchar* m_buffer = nullptr;
  62. QList<int> m_HWDeviceTypes; // 保存当前环境支持的硬件解码器
  63. AVBufferRef* hw_device_ctx = nullptr; // 对数据缓冲区的引用
  64. bool m_HWDecoder = false; // 记录是否使用硬件解码
  65. };
  66. #endif // VIDEODECODE_H