vic.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. extern "C" {
  6. #if defined(__GNUC__) || defined(__clang__)
  7. #pragma GCC diagnostic push
  8. #pragma GCC diagnostic ignored "-Wconversion"
  9. #endif
  10. #include <libswscale/swscale.h>
  11. #if defined(__GNUC__) || defined(__clang__)
  12. #pragma GCC diagnostic pop
  13. #endif
  14. }
  15. #include "common/assert.h"
  16. #include "common/logging/log.h"
  17. #include "video_core/command_classes/nvdec.h"
  18. #include "video_core/command_classes/vic.h"
  19. #include "video_core/engines/maxwell_3d.h"
  20. #include "video_core/gpu.h"
  21. #include "video_core/memory_manager.h"
  22. #include "video_core/textures/decoders.h"
  23. namespace Tegra {
  24. Vic::Vic(GPU& gpu_, std::shared_ptr<Nvdec> nvdec_processor_)
  25. : gpu(gpu_),
  26. nvdec_processor(std::move(nvdec_processor_)), converted_frame_buffer{nullptr, av_free} {}
  27. Vic::~Vic() = default;
  28. void Vic::ProcessMethod(Method method, u32 argument) {
  29. LOG_DEBUG(HW_GPU, "Vic method 0x{:X}", static_cast<u32>(method));
  30. const u64 arg = static_cast<u64>(argument) << 8;
  31. switch (method) {
  32. case Method::Execute:
  33. Execute();
  34. break;
  35. case Method::SetConfigStructOffset:
  36. config_struct_address = arg;
  37. break;
  38. case Method::SetOutputSurfaceLumaOffset:
  39. output_surface_luma_address = arg;
  40. break;
  41. case Method::SetOutputSurfaceChromaOffset:
  42. output_surface_chroma_address = arg;
  43. break;
  44. default:
  45. break;
  46. }
  47. }
  48. void Vic::Execute() {
  49. if (output_surface_luma_address == 0) {
  50. LOG_ERROR(Service_NVDRV, "VIC Luma address not set.");
  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) {
  57. return;
  58. }
  59. const auto pixel_format = static_cast<VideoPixelFormat>(config.pixel_format.Value());
  60. switch (pixel_format) {
  61. case VideoPixelFormat::BGRA8:
  62. case VideoPixelFormat::RGBA8: {
  63. LOG_TRACE(Service_NVDRV, "Writing RGB Frame");
  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. // Frames are decoded into either YUV420 or NV12 formats. Convert to desired format
  71. scaler_ctx = sws_getContext(frame->width, frame->height,
  72. static_cast<AVPixelFormat>(frame->format), 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 u32 width = static_cast<u32>(frame->width);
  79. const u32 height = static_cast<u32>(frame->height);
  80. const std::size_t linear_size = width * height * 4;
  81. // Only allocate frame_buffer once per stream, as the size is not expected to change
  82. if (!converted_frame_buffer) {
  83. converted_frame_buffer = AVMallocPtr{static_cast<u8*>(av_malloc(linear_size)), av_free};
  84. }
  85. const std::array<int, 4> converted_stride{frame->width * 4, frame->height * 4, 0, 0};
  86. u8* const converted_frame_buf_addr{converted_frame_buffer.get()};
  87. sws_scale(scaler_ctx, frame->data, frame->linesize, 0, frame->height,
  88. &converted_frame_buf_addr, converted_stride.data());
  89. const u32 blk_kind = static_cast<u32>(config.block_linear_kind);
  90. if (blk_kind != 0) {
  91. // swizzle pitch linear to block linear
  92. const u32 block_height = static_cast<u32>(config.block_linear_height_log2);
  93. const auto size =
  94. Tegra::Texture::CalculateSize(true, 4, width, height, 1, block_height, 0);
  95. luma_buffer.resize(size);
  96. Tegra::Texture::SwizzleSubrect(width, height, width * 4, width, 4, luma_buffer.data(),
  97. converted_frame_buffer.get(), block_height, 0, 0);
  98. gpu.MemoryManager().WriteBlock(output_surface_luma_address, luma_buffer.data(), size);
  99. } else {
  100. // send pitch linear frame
  101. gpu.MemoryManager().WriteBlock(output_surface_luma_address, converted_frame_buf_addr,
  102. linear_size);
  103. }
  104. break;
  105. }
  106. case VideoPixelFormat::Yuv420: {
  107. LOG_TRACE(Service_NVDRV, "Writing YUV420 Frame");
  108. const std::size_t surface_width = config.surface_width_minus1 + 1;
  109. const std::size_t surface_height = config.surface_height_minus1 + 1;
  110. const auto frame_width = std::min(surface_width, static_cast<size_t>(frame->width));
  111. const auto frame_height = std::min(surface_height, static_cast<size_t>(frame->height));
  112. const std::size_t aligned_width = (surface_width + 0xff) & ~0xffUL;
  113. const auto stride = static_cast<size_t>(frame->linesize[0]);
  114. luma_buffer.resize(aligned_width * surface_height);
  115. chroma_buffer.resize(aligned_width * surface_height / 2);
  116. // Populate luma buffer
  117. const u8* luma_src = frame->data[0];
  118. for (std::size_t y = 0; y < frame_height; ++y) {
  119. const std::size_t src = y * stride;
  120. const std::size_t dst = y * aligned_width;
  121. for (std::size_t x = 0; x < frame_width; ++x) {
  122. luma_buffer[dst + x] = luma_src[src + x];
  123. }
  124. }
  125. gpu.MemoryManager().WriteBlock(output_surface_luma_address, luma_buffer.data(),
  126. luma_buffer.size());
  127. // Chroma
  128. const std::size_t half_height = frame_height / 2;
  129. const auto half_stride = static_cast<size_t>(frame->linesize[1]);
  130. switch (frame->format) {
  131. case AV_PIX_FMT_YUV420P: {
  132. // Frame from FFmpeg software
  133. // Populate chroma buffer from both channels with interleaving.
  134. const std::size_t half_width = frame_width / 2;
  135. const u8* chroma_b_src = frame->data[1];
  136. const u8* chroma_r_src = frame->data[2];
  137. for (std::size_t y = 0; y < half_height; ++y) {
  138. const std::size_t src = y * half_stride;
  139. const std::size_t dst = y * aligned_width;
  140. for (std::size_t x = 0; x < half_width; ++x) {
  141. chroma_buffer[dst + x * 2] = chroma_b_src[src + x];
  142. chroma_buffer[dst + x * 2 + 1] = chroma_r_src[src + x];
  143. }
  144. }
  145. break;
  146. }
  147. case AV_PIX_FMT_NV12: {
  148. // Frame from VA-API hardware
  149. // This is already interleaved so just copy
  150. const u8* chroma_src = frame->data[1];
  151. for (std::size_t y = 0; y < half_height; ++y) {
  152. const std::size_t src = y * stride;
  153. const std::size_t dst = y * aligned_width;
  154. for (std::size_t x = 0; x < frame_width; ++x) {
  155. chroma_buffer[dst + x] = chroma_src[src + x];
  156. }
  157. }
  158. break;
  159. }
  160. default:
  161. UNREACHABLE();
  162. break;
  163. }
  164. gpu.MemoryManager().WriteBlock(output_surface_chroma_address, chroma_buffer.data(),
  165. chroma_buffer.size());
  166. break;
  167. }
  168. default:
  169. UNIMPLEMENTED_MSG("Unknown video pixel format {}", config.pixel_format.Value());
  170. break;
  171. }
  172. }
  173. } // namespace Tegra