codec.h 1.9 KB

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