codec.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstring>
  5. #include <fstream>
  6. #include <vector>
  7. #include "common/assert.h"
  8. #include "video_core/command_classes/codecs/codec.h"
  9. #include "video_core/command_classes/codecs/h264.h"
  10. #include "video_core/command_classes/codecs/vp9.h"
  11. #include "video_core/gpu.h"
  12. #include "video_core/memory_manager.h"
  13. extern "C" {
  14. #include <libavutil/opt.h>
  15. }
  16. namespace Tegra {
  17. void AVFrameDeleter(AVFrame* ptr) {
  18. av_frame_unref(ptr);
  19. av_free(ptr);
  20. }
  21. Codec::Codec(GPU& gpu_)
  22. : gpu(gpu_), h264_decoder(std::make_unique<Decoder::H264>(gpu)),
  23. vp9_decoder(std::make_unique<Decoder::VP9>(gpu)) {}
  24. Codec::~Codec() {
  25. if (!initialized) {
  26. return;
  27. }
  28. // Free libav memory
  29. AVFrame* av_frame{nullptr};
  30. avcodec_send_packet(av_codec_ctx, nullptr);
  31. av_frame = av_frame_alloc();
  32. avcodec_receive_frame(av_codec_ctx, av_frame);
  33. avcodec_flush_buffers(av_codec_ctx);
  34. av_frame_unref(av_frame);
  35. av_free(av_frame);
  36. avcodec_close(av_codec_ctx);
  37. }
  38. void Codec::SetTargetCodec(NvdecCommon::VideoCodec codec) {
  39. LOG_INFO(Service_NVDRV, "NVDEC video codec initialized to {}", codec);
  40. current_codec = codec;
  41. }
  42. void Codec::StateWrite(u32 offset, u64 arguments) {
  43. u8* const state_offset = reinterpret_cast<u8*>(&state) + offset * sizeof(u64);
  44. std::memcpy(state_offset, &arguments, sizeof(u64));
  45. }
  46. void Codec::Decode() {
  47. bool is_first_frame = false;
  48. if (!initialized) {
  49. if (current_codec == NvdecCommon::VideoCodec::H264) {
  50. av_codec = avcodec_find_decoder(AV_CODEC_ID_H264);
  51. } else if (current_codec == NvdecCommon::VideoCodec::Vp9) {
  52. av_codec = avcodec_find_decoder(AV_CODEC_ID_VP9);
  53. } else {
  54. LOG_ERROR(Service_NVDRV, "Unknown video codec {}", current_codec);
  55. return;
  56. }
  57. av_codec_ctx = avcodec_alloc_context3(av_codec);
  58. av_opt_set(av_codec_ctx->priv_data, "tune", "zerolatency", 0);
  59. // TODO(ameerj): libavcodec gpu hw acceleration
  60. const auto av_error = avcodec_open2(av_codec_ctx, av_codec, nullptr);
  61. if (av_error < 0) {
  62. LOG_ERROR(Service_NVDRV, "avcodec_open2() Failed.");
  63. avcodec_close(av_codec_ctx);
  64. return;
  65. }
  66. initialized = true;
  67. is_first_frame = true;
  68. }
  69. bool vp9_hidden_frame = false;
  70. AVPacket packet{};
  71. av_init_packet(&packet);
  72. std::vector<u8> frame_data;
  73. if (current_codec == NvdecCommon::VideoCodec::H264) {
  74. frame_data = h264_decoder->ComposeFrameHeader(state, is_first_frame);
  75. } else if (current_codec == NvdecCommon::VideoCodec::Vp9) {
  76. frame_data = vp9_decoder->ComposeFrameHeader(state);
  77. vp9_hidden_frame = vp9_decoder->WasFrameHidden();
  78. }
  79. packet.data = frame_data.data();
  80. packet.size = static_cast<int>(frame_data.size());
  81. avcodec_send_packet(av_codec_ctx, &packet);
  82. if (!vp9_hidden_frame) {
  83. // Only receive/store visible frames
  84. AVFramePtr frame = AVFramePtr{av_frame_alloc(), AVFrameDeleter};
  85. avcodec_receive_frame(av_codec_ctx, frame.get());
  86. av_frames.push(std::move(frame));
  87. // Limit queue to 10 frames. Workaround for ZLA decode and queue spam
  88. if (av_frames.size() > 10) {
  89. av_frames.pop();
  90. }
  91. }
  92. }
  93. AVFramePtr Codec::GetCurrentFrame() {
  94. // Sometimes VIC will request more frames than have been decoded.
  95. // in this case, return a nullptr and don't overwrite previous frame data
  96. if (av_frames.empty()) {
  97. return AVFramePtr{nullptr, AVFrameDeleter};
  98. }
  99. AVFramePtr frame = std::move(av_frames.front());
  100. av_frames.pop();
  101. return frame;
  102. }
  103. NvdecCommon::VideoCodec Codec::GetCurrentCodec() const {
  104. return current_codec;
  105. }
  106. } // namespace Tegra