codec.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <memory>
  6. #include <string_view>
  7. #include <queue>
  8. #include "common/common_types.h"
  9. #include "video_core/command_classes/nvdec_common.h"
  10. extern "C" {
  11. #if defined(__GNUC__) || defined(__clang__)
  12. #pragma GCC diagnostic push
  13. #pragma GCC diagnostic ignored "-Wconversion"
  14. #endif
  15. #include <libavcodec/avcodec.h>
  16. #if defined(__GNUC__) || defined(__clang__)
  17. #pragma GCC diagnostic pop
  18. #endif
  19. }
  20. namespace Tegra {
  21. class GPU;
  22. void AVFrameDeleter(AVFrame* ptr);
  23. using AVFramePtr = std::unique_ptr<AVFrame, decltype(&AVFrameDeleter)>;
  24. namespace Decoder {
  25. class H264;
  26. class VP8;
  27. class VP9;
  28. } // namespace Decoder
  29. class Codec {
  30. public:
  31. explicit Codec(GPU& gpu, const NvdecCommon::NvdecRegisters& regs);
  32. ~Codec();
  33. /// Initialize the codec, returning success or failure
  34. void Initialize();
  35. /// Sets NVDEC video stream codec
  36. void SetTargetCodec(NvdecCommon::VideoCodec codec);
  37. /// Call decoders to construct headers, decode AVFrame with ffmpeg
  38. void Decode();
  39. /// Returns next decoded frame
  40. [[nodiscard]] AVFramePtr GetCurrentFrame();
  41. /// Returns the value of current_codec
  42. [[nodiscard]] NvdecCommon::VideoCodec GetCurrentCodec() const;
  43. /// Return name of the current codec
  44. [[nodiscard]] std::string_view GetCurrentCodecName() const;
  45. private:
  46. void InitializeAvCodecContext();
  47. void InitializeGpuDecoder();
  48. bool CreateGpuAvDevice();
  49. bool initialized{};
  50. NvdecCommon::VideoCodec current_codec{NvdecCommon::VideoCodec::None};
  51. AVCodec* av_codec{nullptr};
  52. AVCodecContext* av_codec_ctx{nullptr};
  53. AVBufferRef* av_gpu_decoder{nullptr};
  54. GPU& gpu;
  55. const NvdecCommon::NvdecRegisters& state;
  56. std::unique_ptr<Decoder::H264> h264_decoder;
  57. std::unique_ptr<Decoder::VP8> vp8_decoder;
  58. std::unique_ptr<Decoder::VP9> vp9_decoder;
  59. std::queue<AVFramePtr> av_frames{};
  60. };
  61. } // namespace Tegra