vic.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <array>
  5. #include "common/assert.h"
  6. #include "video_core/command_classes/nvdec.h"
  7. #include "video_core/command_classes/vic.h"
  8. #include "video_core/engines/maxwell_3d.h"
  9. #include "video_core/gpu.h"
  10. #include "video_core/memory_manager.h"
  11. #include "video_core/textures/decoders.h"
  12. extern "C" {
  13. #include <libswscale/swscale.h>
  14. }
  15. namespace Tegra {
  16. Vic::Vic(GPU& gpu_, std::shared_ptr<Nvdec> nvdec_processor_)
  17. : gpu(gpu_), nvdec_processor(std::move(nvdec_processor_)) {}
  18. Vic::~Vic() = default;
  19. void Vic::VicStateWrite(u32 offset, u32 arguments) {
  20. u8* const state_offset = reinterpret_cast<u8*>(&vic_state) + offset * sizeof(u32);
  21. std::memcpy(state_offset, &arguments, sizeof(u32));
  22. }
  23. void Vic::ProcessMethod(Method method, const std::vector<u32>& arguments) {
  24. LOG_DEBUG(HW_GPU, "Vic method 0x{:X}", method);
  25. VicStateWrite(static_cast<u32>(method), arguments[0]);
  26. const u64 arg = static_cast<u64>(arguments[0]) << 8;
  27. switch (method) {
  28. case Method::Execute:
  29. Execute();
  30. break;
  31. case Method::SetConfigStructOffset:
  32. config_struct_address = arg;
  33. break;
  34. case Method::SetOutputSurfaceLumaOffset:
  35. output_surface_luma_address = arg;
  36. break;
  37. case Method::SetOutputSurfaceChromaUOffset:
  38. output_surface_chroma_u_address = arg;
  39. break;
  40. case Method::SetOutputSurfaceChromaVOffset:
  41. output_surface_chroma_v_address = arg;
  42. break;
  43. default:
  44. break;
  45. }
  46. }
  47. void Vic::Execute() {
  48. if (output_surface_luma_address == 0) {
  49. LOG_ERROR(Service_NVDRV, "VIC Luma address not set. Received 0x{:X}",
  50. vic_state.output_surface.luma_offset);
  51. return;
  52. }
  53. const VicConfig config{gpu.MemoryManager().Read<u64>(config_struct_address + 0x20)};
  54. const AVFramePtr frame_ptr = nvdec_processor->GetFrame();
  55. const auto* frame = frame_ptr.get();
  56. if (!frame || frame->width == 0 || frame->height == 0) {
  57. return;
  58. }
  59. const VideoPixelFormat pixel_format =
  60. static_cast<VideoPixelFormat>(config.pixel_format.Value());
  61. switch (pixel_format) {
  62. case VideoPixelFormat::BGRA8:
  63. case VideoPixelFormat::RGBA8: {
  64. LOG_TRACE(Service_NVDRV, "Writing RGB Frame");
  65. if (scaler_ctx == nullptr || frame->width != scaler_width ||
  66. frame->height != scaler_height) {
  67. const AVPixelFormat target_format =
  68. (pixel_format == VideoPixelFormat::RGBA8) ? AV_PIX_FMT_RGBA : AV_PIX_FMT_BGRA;
  69. sws_freeContext(scaler_ctx);
  70. scaler_ctx = nullptr;
  71. // FFmpeg returns all frames in YUV420, convert it into expected format
  72. scaler_ctx =
  73. sws_getContext(frame->width, frame->height, AV_PIX_FMT_YUV420P, frame->width,
  74. frame->height, target_format, 0, nullptr, nullptr, nullptr);
  75. scaler_width = frame->width;
  76. scaler_height = frame->height;
  77. }
  78. // Get Converted frame
  79. const std::size_t linear_size = frame->width * frame->height * 4;
  80. using AVMallocPtr = std::unique_ptr<u8, decltype(&av_free)>;
  81. AVMallocPtr converted_frame_buffer{static_cast<u8*>(av_malloc(linear_size)), av_free};
  82. const int converted_stride{frame->width * 4};
  83. u8* const converted_frame_buf_addr{converted_frame_buffer.get()};
  84. sws_scale(scaler_ctx, frame->data, frame->linesize, 0, frame->height,
  85. &converted_frame_buf_addr, &converted_stride);
  86. const u32 blk_kind = static_cast<u32>(config.block_linear_kind);
  87. if (blk_kind != 0) {
  88. // swizzle pitch linear to block linear
  89. const u32 block_height = static_cast<u32>(config.block_linear_height_log2);
  90. const auto size = Tegra::Texture::CalculateSize(true, 4, frame->width, frame->height, 1,
  91. block_height, 0);
  92. std::vector<u8> swizzled_data(size);
  93. Tegra::Texture::SwizzleSubrect(frame->width, frame->height, frame->width * 4,
  94. frame->width, 4, swizzled_data.data(),
  95. converted_frame_buffer.get(), block_height, 0, 0);
  96. gpu.MemoryManager().WriteBlock(output_surface_luma_address, swizzled_data.data(), size);
  97. } else {
  98. // send pitch linear frame
  99. gpu.MemoryManager().WriteBlock(output_surface_luma_address, converted_frame_buf_addr,
  100. linear_size);
  101. }
  102. break;
  103. }
  104. case VideoPixelFormat::Yuv420: {
  105. LOG_TRACE(Service_NVDRV, "Writing YUV420 Frame");
  106. const std::size_t surface_width = config.surface_width_minus1 + 1;
  107. const std::size_t surface_height = config.surface_height_minus1 + 1;
  108. const std::size_t half_width = surface_width / 2;
  109. const std::size_t half_height = config.surface_height_minus1 / 2;
  110. const std::size_t aligned_width = (surface_width + 0xff) & ~0xff;
  111. const auto* luma_ptr = frame->data[0];
  112. const auto* chroma_b_ptr = frame->data[1];
  113. const auto* chroma_r_ptr = frame->data[2];
  114. const auto stride = frame->linesize[0];
  115. const auto half_stride = frame->linesize[1];
  116. std::vector<u8> luma_buffer(aligned_width * surface_height);
  117. std::vector<u8> chroma_buffer(aligned_width * half_height);
  118. // Populate luma buffer
  119. for (std::size_t y = 0; y < surface_height - 1; ++y) {
  120. std::size_t src = y * stride;
  121. std::size_t dst = y * aligned_width;
  122. std::size_t size = surface_width;
  123. for (std::size_t offset = 0; offset < size; ++offset) {
  124. luma_buffer[dst + offset] = luma_ptr[src + offset];
  125. }
  126. }
  127. gpu.MemoryManager().WriteBlock(output_surface_luma_address, luma_buffer.data(),
  128. luma_buffer.size());
  129. // Populate chroma buffer from both channels with interleaving.
  130. for (std::size_t y = 0; y < half_height; ++y) {
  131. std::size_t src = y * half_stride;
  132. std::size_t dst = y * aligned_width;
  133. for (std::size_t x = 0; x < half_width; ++x) {
  134. chroma_buffer[dst + x * 2] = chroma_b_ptr[src + x];
  135. chroma_buffer[dst + x * 2 + 1] = chroma_r_ptr[src + x];
  136. }
  137. }
  138. gpu.MemoryManager().WriteBlock(output_surface_chroma_u_address, chroma_buffer.data(),
  139. chroma_buffer.size());
  140. break;
  141. }
  142. default:
  143. UNIMPLEMENTED_MSG("Unknown video pixel format {}", config.pixel_format.Value());
  144. break;
  145. }
  146. }
  147. } // namespace Tegra