codec_video_decoder.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 decode(const AVPacketPtr& packet, std::vector<AVFramePtr>& frames) override;
  36. ErrorCode finishDecode(std::vector<AVFramePtr>& frames) override;
  37. // 状态查询
  38. bool isHardwareDecoder() const { return isHardwareDecoder_; }
  39. std::string getDecoderName() const;
  40. // 统计信息
  41. struct DecoderStats {
  42. uint64_t decodedFrames = 0;
  43. uint64_t droppedFrames = 0;
  44. uint64_t errorCount = 0;
  45. double avgDecodeTime = 0.0;
  46. uint64_t totalBytes = 0;
  47. };
  48. DecoderStats getStats() const;
  49. void resetStats();
  50. // 静态工具方法
  51. static std::vector<std::string> getSupportedDecoders();
  52. static bool isHardwareDecoder(const std::string& codecName);
  53. static std::string getRecommendedDecoder(const std::string& codecName = "");
  54. // 解码器工厂
  55. class VideoDecoderFactory {
  56. public:
  57. static std::unique_ptr<VideoDecoder> create(const std::string& codecName = "");
  58. static std::unique_ptr<VideoDecoder> createBest(bool preferHardware = true);
  59. };
  60. protected:
  61. bool validateParams(const CodecParams& params) override;
  62. private:
  63. // 内部实现方法
  64. ErrorCode initDecoder();
  65. ErrorCode setupDecoderParams();
  66. ErrorCode setupHardwareAcceleration();
  67. ErrorCode setupHardwareFrameContext();
  68. ErrorCode decodeFrame(const AVPacketPtr& packet, std::vector<AVFramePtr>& frames);
  69. ErrorCode receiveFrames(std::vector<AVFramePtr>& frames);
  70. AVFramePtr convertFrame(const AVFramePtr& frame);
  71. AVFramePtr transferFromHardware(AVFramePtr hwFrame);
  72. // 硬件加速相关
  73. AVHWDeviceType getHardwareDeviceType() const;
  74. AVPixelFormat getHardwarePixelFormat() const;
  75. // 统计相关
  76. void updateStats(bool success, double decodeTime, size_t dataSize);
  77. // 静态方法实现
  78. static void findUsableDecoders();
  79. private:
  80. VideoDecoderParams videoParams_;
  81. // FFmpeg 对象
  82. const AVCodec* codec_ = nullptr;
  83. AVCodecContextPtr codecCtx_;
  84. AVBufferRef* hwDeviceCtx_ = nullptr;
  85. AVFramePtr hwFrame_;
  86. // 硬件加速
  87. bool isHardwareDecoder_ = false;
  88. // 线程安全
  89. mutable std::mutex decodeMutex_;
  90. // 统计信息
  91. mutable std::mutex statsMutex_;
  92. DecoderStats stats_;
  93. std::chrono::high_resolution_clock::time_point lastStatsUpdate_;
  94. // 静态成员
  95. static std::vector<std::string> supportedDecoders_;
  96. static std::once_flag decodersInitFlag_;
  97. // 支持的解码器列表
  98. static constexpr const char* HARDWARE_DECODERS[] = {
  99. "h264_cuvid", // NVIDIA CUDA
  100. "hevc_cuvid", // NVIDIA CUDA HEVC
  101. "h264_qsv", // Intel Quick Sync Video
  102. "hevc_qsv", // Intel Quick Sync Video HEVC
  103. "h264_d3d11va", // Direct3D 11 Video Acceleration
  104. "hevc_d3d11va", // Direct3D 11 Video Acceleration HEVC
  105. "h264_videotoolbox", // Apple VideoToolbox
  106. "hevc_videotoolbox", // Apple VideoToolbox HEVC
  107. "vp9_cuvid", // NVIDIA CUDA VP9
  108. "av1_cuvid", // NVIDIA CUDA AV1
  109. nullptr
  110. };
  111. static constexpr const char* SOFTWARE_DECODERS[] = {
  112. "h264", // H.264/AVC
  113. "hevc", // H.265/HEVC
  114. "vp8", // VP8
  115. "vp9", // VP9
  116. "av1", // AV1
  117. "mpeg2video", // MPEG-2
  118. "mpeg4", // MPEG-4
  119. "mjpeg", // Motion JPEG
  120. nullptr
  121. };
  122. };
  123. } // namespace codec
  124. } // namespace av