codec_video_decoder.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #pragma once
  2. #include "codec_abstract_codec.h"
  3. #include <vector>
  4. #include <memory>
  5. #include <mutex>
  6. #include <chrono>
  7. namespace av {
  8. namespace codec {
  9. // 视频解码器参数
  10. struct VideoDecoderParams : public CodecParams {
  11. std::string codecName; // 解码器名称 (如 "h264", "hevc", "vp9")
  12. int width = 0; // 视频宽度(可选,用于验证)
  13. int height = 0; // 视频高度(可选,用于验证)
  14. AVPixelFormat pixelFormat = AV_PIX_FMT_YUV420P; // 输出像素格式
  15. bool hardwareAccel = true; // 是否启用硬件加速
  16. int threadCount = 0; // 解码线程数(0为自动)
  17. bool lowLatency = false; // 低延迟模式
  18. VideoDecoderParams() {
  19. type = MediaType::VIDEO;
  20. }
  21. };
  22. // 视频解码器类
  23. class VideoDecoder : public AbstractDecoder {
  24. public:
  25. VideoDecoder();
  26. ~VideoDecoder() override;
  27. // 基础接口实现
  28. ErrorCode open(const CodecParams& params) override;
  29. void close() override;
  30. ErrorCode flush() override;
  31. ErrorCode reset() override;
  32. // 初始化方法
  33. ErrorCode initialize(const CodecParams& params);
  34. // 从流参数设置解码器
  35. ErrorCode setStreamParameters(const AVCodecParameters* codecpar);
  36. // 解码接口
  37. ErrorCode decode(const AVPacketPtr& packet, std::vector<AVFramePtr>& frames) override;
  38. ErrorCode finishDecode(std::vector<AVFramePtr>& frames) override;
  39. // 状态查询
  40. bool isHardwareDecoder() const { return isHardwareDecoder_; }
  41. std::string getDecoderName() const;
  42. // 统计信息
  43. struct DecoderStats {
  44. uint64_t decodedFrames = 0;
  45. uint64_t droppedFrames = 0;
  46. uint64_t errorCount = 0;
  47. double avgDecodeTime = 0.0;
  48. uint64_t totalBytes = 0;
  49. };
  50. DecoderStats getStats() const;
  51. void resetStats();
  52. // 静态工具方法
  53. static std::vector<std::string> getSupportedDecoders();
  54. static bool isHardwareDecoder(const std::string& codecName);
  55. static std::string getRecommendedDecoder(const std::string& codecName = "");
  56. // 解码器工厂
  57. class VideoDecoderFactory {
  58. public:
  59. static std::unique_ptr<VideoDecoder> create(const std::string& codecName = "");
  60. static std::unique_ptr<VideoDecoder> createBest(bool preferHardware = true);
  61. };
  62. protected:
  63. bool validateParams(const CodecParams& params) override;
  64. private:
  65. // 内部实现方法
  66. ErrorCode initDecoder();
  67. ErrorCode setupDecoderParams();
  68. ErrorCode setupHardwareAcceleration();
  69. ErrorCode setupHardwareFrameContext();
  70. // 流参数
  71. const AVCodecParameters* streamCodecpar_ = nullptr;
  72. ErrorCode decodeFrame(const AVPacketPtr& packet, std::vector<AVFramePtr>& frames);
  73. ErrorCode receiveFrames(std::vector<AVFramePtr>& frames);
  74. AVFramePtr convertFrame(const AVFramePtr& frame);
  75. AVFramePtr transferFromHardware(AVFramePtr hwFrame);
  76. // 硬件加速相关
  77. AVHWDeviceType getHardwareDeviceType() const;
  78. AVPixelFormat getHardwarePixelFormat() const;
  79. // 统计相关
  80. void updateStats(bool success, double decodeTime, size_t dataSize);
  81. // 静态方法实现
  82. static void findUsableDecoders();
  83. private:
  84. VideoDecoderParams videoParams_;
  85. // FFmpeg 对象
  86. const AVCodec* codec_ = nullptr;
  87. AVCodecContextPtr codecCtx_;
  88. AVBufferRef* hwDeviceCtx_ = nullptr;
  89. AVFramePtr hwFrame_;
  90. // 硬件加速相关
  91. bool isHardwareDecoder_ = false;
  92. // 线程安全
  93. mutable std::mutex decodeMutex_;
  94. // 统计信息
  95. mutable std::mutex statsMutex_;
  96. DecoderStats stats_;
  97. std::chrono::high_resolution_clock::time_point lastStatsUpdate_;
  98. // 静态成员
  99. static std::vector<std::string> supportedDecoders_;
  100. static std::once_flag decodersInitFlag_;
  101. // 支持的解码器列表
  102. static constexpr const char* HARDWARE_DECODERS[] = {
  103. "h264_cuvid", // NVIDIA CUDA
  104. "hevc_cuvid", // NVIDIA CUDA HEVC
  105. "h264_qsv", // Intel Quick Sync Video
  106. "hevc_qsv", // Intel Quick Sync Video HEVC
  107. "h264_d3d11va", // Direct3D 11 Video Acceleration
  108. "hevc_d3d11va", // Direct3D 11 Video Acceleration HEVC
  109. "h264_videotoolbox", // Apple VideoToolbox
  110. "hevc_videotoolbox", // Apple VideoToolbox HEVC
  111. "vp9_cuvid", // NVIDIA CUDA VP9
  112. "av1_cuvid", // NVIDIA CUDA AV1
  113. nullptr
  114. };
  115. static constexpr const char* SOFTWARE_DECODERS[] = {
  116. "h264", // H.264/AVC
  117. "hevc", // H.265/HEVC
  118. "vp8", // VP8
  119. "vp9", // VP9
  120. "av1", // AV1
  121. "mpeg2video", // MPEG-2
  122. "mpeg4", // MPEG-4
  123. "mjpeg", // Motion JPEG
  124. nullptr
  125. };
  126. };
  127. } // namespace codec
  128. } // namespace av