vic.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/texture_cache/surface_params.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}", static_cast<u32>(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. Recieved 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 VideoPixelFormat pixel_format =
  55. static_cast<VideoPixelFormat>(config.pixel_format.Value());
  56. switch (pixel_format) {
  57. case VideoPixelFormat::BGRA8:
  58. case VideoPixelFormat::RGBA8: {
  59. LOG_TRACE(Service_NVDRV, "Writing RGB Frame");
  60. const auto* frame = nvdec_processor->GetFrame();
  61. if (!frame || frame->width == 0 || frame->height == 0) {
  62. return;
  63. }
  64. if (scaler_ctx == nullptr || frame->width != scaler_width ||
  65. frame->height != scaler_height) {
  66. const AVPixelFormat target_format =
  67. (pixel_format == VideoPixelFormat::RGBA8) ? AV_PIX_FMT_RGBA : AV_PIX_FMT_BGRA;
  68. sws_freeContext(scaler_ctx);
  69. scaler_ctx = nullptr;
  70. // FFmpeg returns all frames in YUV420, convert it into expected format
  71. scaler_ctx =
  72. sws_getContext(frame->width, frame->height, AV_PIX_FMT_YUV420P, frame->width,
  73. frame->height, target_format, 0, nullptr, nullptr, nullptr);
  74. scaler_width = frame->width;
  75. scaler_height = frame->height;
  76. }
  77. // Get Converted frame
  78. const std::size_t linear_size = frame->width * frame->height * 4;
  79. using AVMallocPtr = std::unique_ptr<u8, decltype(&av_free)>;
  80. AVMallocPtr converted_frame_buffer{static_cast<u8*>(av_malloc(linear_size)), av_free};
  81. const int converted_stride{frame->width * 4};
  82. u8* const converted_frame_buf_addr{converted_frame_buffer.get()};
  83. sws_scale(scaler_ctx, frame->data, frame->linesize, 0, frame->height,
  84. &converted_frame_buf_addr, &converted_stride);
  85. const u32 blk_kind = static_cast<u32>(config.block_linear_kind);
  86. if (blk_kind != 0) {
  87. // swizzle pitch linear to block linear
  88. const u32 block_height = static_cast<u32>(config.block_linear_height_log2);
  89. const auto size = Tegra::Texture::CalculateSize(true, 4, frame->width, frame->height, 1,
  90. block_height, 0);
  91. std::vector<u8> swizzled_data(size);
  92. Tegra::Texture::CopySwizzledData(frame->width, frame->height, 1, 4, 4,
  93. swizzled_data.data(), converted_frame_buffer.get(),
  94. false, block_height, 0, 1);
  95. gpu.MemoryManager().WriteBlock(output_surface_luma_address, swizzled_data.data(), size);
  96. gpu.Maxwell3D().OnMemoryWrite();
  97. } else {
  98. // send pitch linear frame
  99. gpu.MemoryManager().WriteBlock(output_surface_luma_address, converted_frame_buf_addr,
  100. linear_size);
  101. gpu.Maxwell3D().OnMemoryWrite();
  102. }
  103. break;
  104. }
  105. case VideoPixelFormat::Yuv420: {
  106. LOG_TRACE(Service_NVDRV, "Writing YUV420 Frame");
  107. const auto* frame = nvdec_processor->GetFrame();
  108. if (!frame || frame->width == 0 || frame->height == 0) {
  109. return;
  110. }
  111. const std::size_t surface_width = config.surface_width_minus1 + 1;
  112. const std::size_t surface_height = config.surface_height_minus1 + 1;
  113. const std::size_t half_width = surface_width / 2;
  114. const std::size_t half_height = config.surface_height_minus1 / 2;
  115. const std::size_t aligned_width = (surface_width + 0xff) & ~0xff;
  116. const auto* luma_ptr = frame->data[0];
  117. const auto* chroma_b_ptr = frame->data[1];
  118. const auto* chroma_r_ptr = frame->data[2];
  119. const auto stride = frame->linesize[0];
  120. const auto half_stride = frame->linesize[1];
  121. std::vector<u8> luma_buffer(aligned_width * surface_height);
  122. std::vector<u8> chroma_buffer(aligned_width * half_height);
  123. // Populate luma buffer
  124. for (std::size_t y = 0; y < surface_height - 1; ++y) {
  125. std::size_t src = y * stride;
  126. std::size_t dst = y * aligned_width;
  127. std::size_t size = surface_width;
  128. for (std::size_t offset = 0; offset < size; ++offset) {
  129. luma_buffer[dst + offset] = luma_ptr[src + offset];
  130. }
  131. }
  132. gpu.MemoryManager().WriteBlock(output_surface_luma_address, luma_buffer.data(),
  133. luma_buffer.size());
  134. // Populate chroma buffer from both channels with interleaving.
  135. for (std::size_t y = 0; y < half_height; ++y) {
  136. std::size_t src = y * half_stride;
  137. std::size_t dst = y * aligned_width;
  138. for (std::size_t x = 0; x < half_width; ++x) {
  139. chroma_buffer[dst + x * 2] = chroma_b_ptr[src + x];
  140. chroma_buffer[dst + x * 2 + 1] = chroma_r_ptr[src + x];
  141. }
  142. }
  143. gpu.MemoryManager().WriteBlock(output_surface_chroma_u_address, chroma_buffer.data(),
  144. chroma_buffer.size());
  145. gpu.Maxwell3D().OnMemoryWrite();
  146. break;
  147. }
  148. default:
  149. UNIMPLEMENTED_MSG("Unknown video pixel format {}", config.pixel_format.Value());
  150. break;
  151. }
  152. }
  153. } // namespace Tegra