codec.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "common/assert.h"
  7. #include "video_core/command_classes/codecs/codec.h"
  8. #include "video_core/command_classes/codecs/h264.h"
  9. #include "video_core/command_classes/codecs/vp9.h"
  10. #include "video_core/gpu.h"
  11. #include "video_core/memory_manager.h"
  12. extern "C" {
  13. #include <libavutil/opt.h>
  14. }
  15. namespace Tegra {
  16. Codec::Codec(GPU& gpu_)
  17. : gpu(gpu_), h264_decoder(std::make_unique<Decoder::H264>(gpu)),
  18. vp9_decoder(std::make_unique<Decoder::VP9>(gpu)) {}
  19. Codec::~Codec() {
  20. if (!initialized) {
  21. return;
  22. }
  23. // Free libav memory
  24. avcodec_send_packet(av_codec_ctx, nullptr);
  25. avcodec_receive_frame(av_codec_ctx, av_frame);
  26. avcodec_flush_buffers(av_codec_ctx);
  27. av_frame_unref(av_frame);
  28. av_free(av_frame);
  29. avcodec_close(av_codec_ctx);
  30. }
  31. void Codec::SetTargetCodec(NvdecCommon::VideoCodec codec) {
  32. LOG_INFO(Service_NVDRV, "NVDEC video codec initialized to {}", static_cast<u32>(codec));
  33. current_codec = codec;
  34. }
  35. void Codec::StateWrite(u32 offset, u64 arguments) {
  36. u8* const state_offset = reinterpret_cast<u8*>(&state) + offset * sizeof(u64);
  37. std::memcpy(state_offset, &arguments, sizeof(u64));
  38. }
  39. void Codec::Decode() {
  40. bool is_first_frame = false;
  41. if (!initialized) {
  42. if (current_codec == NvdecCommon::VideoCodec::H264) {
  43. av_codec = avcodec_find_decoder(AV_CODEC_ID_H264);
  44. } else if (current_codec == NvdecCommon::VideoCodec::Vp9) {
  45. av_codec = avcodec_find_decoder(AV_CODEC_ID_VP9);
  46. } else {
  47. LOG_ERROR(Service_NVDRV, "Unknown video codec {}", static_cast<u32>(current_codec));
  48. return;
  49. }
  50. av_codec_ctx = avcodec_alloc_context3(av_codec);
  51. av_frame = av_frame_alloc();
  52. av_opt_set(av_codec_ctx->priv_data, "tune", "zerolatency", 0);
  53. // TODO(ameerj): libavcodec gpu hw acceleration
  54. const auto av_error = avcodec_open2(av_codec_ctx, av_codec, nullptr);
  55. if (av_error < 0) {
  56. LOG_ERROR(Service_NVDRV, "avcodec_open2() Failed.");
  57. av_frame_unref(av_frame);
  58. av_free(av_frame);
  59. avcodec_close(av_codec_ctx);
  60. return;
  61. }
  62. initialized = true;
  63. is_first_frame = true;
  64. }
  65. bool vp9_hidden_frame = false;
  66. AVPacket packet{};
  67. av_init_packet(&packet);
  68. std::vector<u8> frame_data;
  69. if (current_codec == NvdecCommon::VideoCodec::H264) {
  70. frame_data = h264_decoder->ComposeFrameHeader(state, is_first_frame);
  71. } else if (current_codec == NvdecCommon::VideoCodec::Vp9) {
  72. frame_data = vp9_decoder->ComposeFrameHeader(state);
  73. vp9_hidden_frame = vp9_decoder->WasFrameHidden();
  74. }
  75. packet.data = frame_data.data();
  76. packet.size = static_cast<int>(frame_data.size());
  77. avcodec_send_packet(av_codec_ctx, &packet);
  78. if (!vp9_hidden_frame) {
  79. // Only receive/store visible frames
  80. avcodec_receive_frame(av_codec_ctx, av_frame);
  81. }
  82. }
  83. AVFrame* Codec::GetCurrentFrame() {
  84. return av_frame;
  85. }
  86. const AVFrame* Codec::GetCurrentFrame() const {
  87. return av_frame;
  88. }
  89. NvdecCommon::VideoCodec Codec::GetCurrentCodec() const {
  90. return current_codec;
  91. }
  92. } // namespace Tegra