vic.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <array>
  4. extern "C" {
  5. #if defined(__GNUC__) || defined(__clang__)
  6. #pragma GCC diagnostic push
  7. #pragma GCC diagnostic ignored "-Wconversion"
  8. #endif
  9. #include <libswscale/swscale.h>
  10. #if defined(__GNUC__) || defined(__clang__)
  11. #pragma GCC diagnostic pop
  12. #endif
  13. }
  14. #include "common/assert.h"
  15. #include "common/bit_field.h"
  16. #include "common/logging/log.h"
  17. #include "video_core/engines/maxwell_3d.h"
  18. #include "video_core/host1x/host1x.h"
  19. #include "video_core/host1x/nvdec.h"
  20. #include "video_core/host1x/vic.h"
  21. #include "video_core/memory_manager.h"
  22. #include "video_core/textures/decoders.h"
  23. namespace Tegra {
  24. namespace Host1x {
  25. namespace {
  26. enum class VideoPixelFormat : u64_le {
  27. RGBA8 = 0x1f,
  28. BGRA8 = 0x20,
  29. RGBX8 = 0x23,
  30. YUV420 = 0x44,
  31. };
  32. } // Anonymous namespace
  33. union VicConfig {
  34. u64_le raw{};
  35. BitField<0, 7, VideoPixelFormat> pixel_format;
  36. BitField<7, 2, u64_le> chroma_loc_horiz;
  37. BitField<9, 2, u64_le> chroma_loc_vert;
  38. BitField<11, 4, u64_le> block_linear_kind;
  39. BitField<15, 4, u64_le> block_linear_height_log2;
  40. BitField<32, 14, u64_le> surface_width_minus1;
  41. BitField<46, 14, u64_le> surface_height_minus1;
  42. };
  43. Vic::Vic(Host1x& host1x_, std::shared_ptr<Nvdec> nvdec_processor_)
  44. : host1x(host1x_),
  45. nvdec_processor(std::move(nvdec_processor_)), converted_frame_buffer{nullptr, av_free} {}
  46. Vic::~Vic() = default;
  47. void Vic::ProcessMethod(Method method, u32 argument) {
  48. LOG_DEBUG(HW_GPU, "Vic method 0x{:X}", static_cast<u32>(method));
  49. const u64 arg = static_cast<u64>(argument) << 8;
  50. switch (method) {
  51. case Method::Execute:
  52. Execute();
  53. break;
  54. case Method::SetConfigStructOffset:
  55. config_struct_address = arg;
  56. break;
  57. case Method::SetOutputSurfaceLumaOffset:
  58. output_surface_luma_address = arg;
  59. break;
  60. case Method::SetOutputSurfaceChromaOffset:
  61. output_surface_chroma_address = arg;
  62. break;
  63. default:
  64. break;
  65. }
  66. }
  67. void Vic::Execute() {
  68. if (output_surface_luma_address == 0) {
  69. LOG_ERROR(Service_NVDRV, "VIC Luma address not set.");
  70. return;
  71. }
  72. const VicConfig config{host1x.MemoryManager().Read<u64>(config_struct_address + 0x20)};
  73. auto frame = nvdec_processor->GetFrame();
  74. if (!frame) {
  75. return;
  76. }
  77. const u64 surface_width = config.surface_width_minus1 + 1;
  78. const u64 surface_height = config.surface_height_minus1 + 1;
  79. if (static_cast<u64>(frame->GetWidth()) != surface_width ||
  80. static_cast<u64>(frame->GetHeight()) != surface_height) {
  81. // TODO: Properly support multiple video streams with differing frame dimensions
  82. LOG_WARNING(Service_NVDRV, "Frame dimensions {}x{} don't match surface dimensions {}x{}",
  83. frame->GetWidth(), frame->GetHeight(), surface_width, surface_height);
  84. }
  85. switch (config.pixel_format) {
  86. case VideoPixelFormat::RGBA8:
  87. case VideoPixelFormat::BGRA8:
  88. case VideoPixelFormat::RGBX8:
  89. WriteRGBFrame(std::move(frame), config);
  90. break;
  91. case VideoPixelFormat::YUV420:
  92. WriteYUVFrame(std::move(frame), config);
  93. break;
  94. default:
  95. UNIMPLEMENTED_MSG("Unknown video pixel format {:X}", config.pixel_format.Value());
  96. break;
  97. }
  98. }
  99. void Vic::WriteRGBFrame(std::unique_ptr<FFmpeg::Frame> frame, const VicConfig& config) {
  100. LOG_TRACE(Service_NVDRV, "Writing RGB Frame");
  101. const auto frame_width = frame->GetWidth();
  102. const auto frame_height = frame->GetHeight();
  103. const auto frame_format = frame->GetPixelFormat();
  104. if (!scaler_ctx || frame_width != scaler_width || frame_height != scaler_height) {
  105. const AVPixelFormat target_format = [pixel_format = config.pixel_format]() {
  106. switch (pixel_format) {
  107. case VideoPixelFormat::RGBA8:
  108. return AV_PIX_FMT_RGBA;
  109. case VideoPixelFormat::BGRA8:
  110. return AV_PIX_FMT_BGRA;
  111. case VideoPixelFormat::RGBX8:
  112. return AV_PIX_FMT_RGB0;
  113. default:
  114. return AV_PIX_FMT_RGBA;
  115. }
  116. }();
  117. sws_freeContext(scaler_ctx);
  118. // Frames are decoded into either YUV420 or NV12 formats. Convert to desired RGB format
  119. scaler_ctx = sws_getContext(frame_width, frame_height, frame_format, frame_width,
  120. frame_height, target_format, 0, nullptr, nullptr, nullptr);
  121. scaler_width = frame_width;
  122. scaler_height = frame_height;
  123. converted_frame_buffer.reset();
  124. }
  125. if (!converted_frame_buffer) {
  126. const size_t frame_size = frame_width * frame_height * 4;
  127. converted_frame_buffer = AVMallocPtr{static_cast<u8*>(av_malloc(frame_size)), av_free};
  128. }
  129. const std::array<int, 4> converted_stride{frame_width * 4, frame_height * 4, 0, 0};
  130. u8* const converted_frame_buf_addr{converted_frame_buffer.get()};
  131. sws_scale(scaler_ctx, frame->GetPlanes(), frame->GetStrides(), 0, frame_height,
  132. &converted_frame_buf_addr, converted_stride.data());
  133. // Use the minimum of surface/frame dimensions to avoid buffer overflow.
  134. const u32 surface_width = static_cast<u32>(config.surface_width_minus1) + 1;
  135. const u32 surface_height = static_cast<u32>(config.surface_height_minus1) + 1;
  136. const u32 width = std::min(surface_width, static_cast<u32>(frame_width));
  137. const u32 height = std::min(surface_height, static_cast<u32>(frame_height));
  138. const u32 blk_kind = static_cast<u32>(config.block_linear_kind);
  139. if (blk_kind != 0) {
  140. // swizzle pitch linear to block linear
  141. const u32 block_height = static_cast<u32>(config.block_linear_height_log2);
  142. const auto size = Texture::CalculateSize(true, 4, width, height, 1, block_height, 0);
  143. luma_buffer.resize_destructive(size);
  144. std::span<const u8> frame_buff(converted_frame_buf_addr, 4 * width * height);
  145. Texture::SwizzleSubrect(luma_buffer, frame_buff, 4, width, height, 1, 0, 0, width, height,
  146. block_height, 0, width * 4);
  147. host1x.MemoryManager().WriteBlock(output_surface_luma_address, luma_buffer.data(), size);
  148. } else {
  149. // send pitch linear frame
  150. const size_t linear_size = width * height * 4;
  151. host1x.MemoryManager().WriteBlock(output_surface_luma_address, converted_frame_buf_addr,
  152. linear_size);
  153. }
  154. }
  155. void Vic::WriteYUVFrame(std::unique_ptr<FFmpeg::Frame> frame, const VicConfig& config) {
  156. LOG_TRACE(Service_NVDRV, "Writing YUV420 Frame");
  157. const std::size_t surface_width = config.surface_width_minus1 + 1;
  158. const std::size_t surface_height = config.surface_height_minus1 + 1;
  159. const std::size_t aligned_width = (surface_width + 0xff) & ~0xffUL;
  160. // Use the minimum of surface/frame dimensions to avoid buffer overflow.
  161. const auto frame_width = std::min(surface_width, static_cast<size_t>(frame->GetWidth()));
  162. const auto frame_height = std::min(surface_height, static_cast<size_t>(frame->GetHeight()));
  163. const auto stride = static_cast<size_t>(frame->GetStride(0));
  164. luma_buffer.resize_destructive(aligned_width * surface_height);
  165. chroma_buffer.resize_destructive(aligned_width * surface_height / 2);
  166. // Populate luma buffer
  167. const u8* luma_src = frame->GetData(0);
  168. for (std::size_t y = 0; y < frame_height; ++y) {
  169. const std::size_t src = y * stride;
  170. const std::size_t dst = y * aligned_width;
  171. std::memcpy(luma_buffer.data() + dst, luma_src + src, frame_width);
  172. }
  173. host1x.MemoryManager().WriteBlock(output_surface_luma_address, luma_buffer.data(),
  174. luma_buffer.size());
  175. // Chroma
  176. const std::size_t half_height = frame_height / 2;
  177. const auto half_stride = static_cast<size_t>(frame->GetStride(1));
  178. switch (frame->GetPixelFormat()) {
  179. case AV_PIX_FMT_YUV420P: {
  180. // Frame from FFmpeg software
  181. // Populate chroma buffer from both channels with interleaving.
  182. const std::size_t half_width = frame_width / 2;
  183. u8* chroma_buffer_data = chroma_buffer.data();
  184. const u8* chroma_b_src = frame->GetData(1);
  185. const u8* chroma_r_src = frame->GetData(2);
  186. for (std::size_t y = 0; y < half_height; ++y) {
  187. const std::size_t src = y * half_stride;
  188. const std::size_t dst = y * aligned_width;
  189. for (std::size_t x = 0; x < half_width; ++x) {
  190. chroma_buffer_data[dst + x * 2] = chroma_b_src[src + x];
  191. chroma_buffer_data[dst + x * 2 + 1] = chroma_r_src[src + x];
  192. }
  193. }
  194. break;
  195. }
  196. case AV_PIX_FMT_NV12: {
  197. // Frame from VA-API hardware
  198. // This is already interleaved so just copy
  199. const u8* chroma_src = frame->GetData(1);
  200. for (std::size_t y = 0; y < half_height; ++y) {
  201. const std::size_t src = y * stride;
  202. const std::size_t dst = y * aligned_width;
  203. std::memcpy(chroma_buffer.data() + dst, chroma_src + src, frame_width);
  204. }
  205. break;
  206. }
  207. default:
  208. ASSERT(false);
  209. break;
  210. }
  211. host1x.MemoryManager().WriteBlock(output_surface_chroma_address, chroma_buffer.data(),
  212. chroma_buffer.size());
  213. }
  214. } // namespace Host1x
  215. } // namespace Tegra