vic.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. const AVFramePtr frame_ptr = nvdec_processor->GetFrame();
  74. const auto* frame = frame_ptr.get();
  75. if (!frame) {
  76. return;
  77. }
  78. const u64 surface_width = config.surface_width_minus1 + 1;
  79. const u64 surface_height = config.surface_height_minus1 + 1;
  80. if (static_cast<u64>(frame->width) != surface_width ||
  81. static_cast<u64>(frame->height) != surface_height) {
  82. // TODO: Properly support multiple video streams with differing frame dimensions
  83. LOG_WARNING(Service_NVDRV, "Frame dimensions {}x{} don't match surface dimensions {}x{}",
  84. frame->width, frame->height, surface_width, surface_height);
  85. }
  86. switch (config.pixel_format) {
  87. case VideoPixelFormat::RGBA8:
  88. case VideoPixelFormat::BGRA8:
  89. case VideoPixelFormat::RGBX8:
  90. WriteRGBFrame(frame, config);
  91. break;
  92. case VideoPixelFormat::YUV420:
  93. WriteYUVFrame(frame, config);
  94. break;
  95. default:
  96. UNIMPLEMENTED_MSG("Unknown video pixel format {:X}", config.pixel_format.Value());
  97. break;
  98. }
  99. }
  100. void Vic::WriteRGBFrame(const AVFrame* frame, const VicConfig& config) {
  101. LOG_TRACE(Service_NVDRV, "Writing RGB Frame");
  102. if (!scaler_ctx || frame->width != scaler_width || frame->height != scaler_height) {
  103. const AVPixelFormat target_format = [pixel_format = config.pixel_format]() {
  104. switch (pixel_format) {
  105. case VideoPixelFormat::RGBA8:
  106. return AV_PIX_FMT_RGBA;
  107. case VideoPixelFormat::BGRA8:
  108. return AV_PIX_FMT_BGRA;
  109. case VideoPixelFormat::RGBX8:
  110. return AV_PIX_FMT_RGB0;
  111. default:
  112. return AV_PIX_FMT_RGBA;
  113. }
  114. }();
  115. sws_freeContext(scaler_ctx);
  116. // Frames are decoded into either YUV420 or NV12 formats. Convert to desired RGB format
  117. scaler_ctx = sws_getContext(frame->width, frame->height,
  118. static_cast<AVPixelFormat>(frame->format), frame->width,
  119. frame->height, target_format, 0, nullptr, nullptr, nullptr);
  120. scaler_width = frame->width;
  121. scaler_height = frame->height;
  122. converted_frame_buffer.reset();
  123. }
  124. if (!converted_frame_buffer) {
  125. const size_t frame_size = frame->width * frame->height * 4;
  126. converted_frame_buffer = AVMallocPtr{static_cast<u8*>(av_malloc(frame_size)), av_free};
  127. }
  128. const std::array<int, 4> converted_stride{frame->width * 4, frame->height * 4, 0, 0};
  129. u8* const converted_frame_buf_addr{converted_frame_buffer.get()};
  130. sws_scale(scaler_ctx, frame->data, frame->linesize, 0, frame->height, &converted_frame_buf_addr,
  131. converted_stride.data());
  132. // Use the minimum of surface/frame dimensions to avoid buffer overflow.
  133. const u32 surface_width = static_cast<u32>(config.surface_width_minus1) + 1;
  134. const u32 surface_height = static_cast<u32>(config.surface_height_minus1) + 1;
  135. const u32 width = std::min(surface_width, static_cast<u32>(frame->width));
  136. const u32 height = std::min(surface_height, static_cast<u32>(frame->height));
  137. const u32 blk_kind = static_cast<u32>(config.block_linear_kind);
  138. if (blk_kind != 0) {
  139. // swizzle pitch linear to block linear
  140. const u32 block_height = static_cast<u32>(config.block_linear_height_log2);
  141. const auto size = Texture::CalculateSize(true, 4, width, height, 1, block_height, 0);
  142. luma_buffer.resize_destructive(size);
  143. std::span<const u8> frame_buff(converted_frame_buf_addr, 4 * width * height);
  144. Texture::SwizzleSubrect(luma_buffer, frame_buff, 4, width, height, 1, 0, 0, width, height,
  145. block_height, 0, width * 4);
  146. host1x.MemoryManager().WriteBlock(output_surface_luma_address, luma_buffer.data(), size);
  147. } else {
  148. // send pitch linear frame
  149. const size_t linear_size = width * height * 4;
  150. host1x.MemoryManager().WriteBlock(output_surface_luma_address, converted_frame_buf_addr,
  151. linear_size);
  152. }
  153. }
  154. void Vic::WriteYUVFrame(const AVFrame* frame, const VicConfig& config) {
  155. LOG_TRACE(Service_NVDRV, "Writing YUV420 Frame");
  156. const std::size_t surface_width = config.surface_width_minus1 + 1;
  157. const std::size_t surface_height = config.surface_height_minus1 + 1;
  158. const std::size_t aligned_width = (surface_width + 0xff) & ~0xffUL;
  159. // Use the minimum of surface/frame dimensions to avoid buffer overflow.
  160. const auto frame_width = std::min(surface_width, static_cast<size_t>(frame->width));
  161. const auto frame_height = std::min(surface_height, static_cast<size_t>(frame->height));
  162. const auto stride = static_cast<size_t>(frame->linesize[0]);
  163. luma_buffer.resize_destructive(aligned_width * surface_height);
  164. chroma_buffer.resize_destructive(aligned_width * surface_height / 2);
  165. // Populate luma buffer
  166. const u8* luma_src = frame->data[0];
  167. for (std::size_t y = 0; y < frame_height; ++y) {
  168. const std::size_t src = y * stride;
  169. const std::size_t dst = y * aligned_width;
  170. for (std::size_t x = 0; x < frame_width; ++x) {
  171. luma_buffer[dst + x] = luma_src[src + x];
  172. }
  173. }
  174. host1x.MemoryManager().WriteBlock(output_surface_luma_address, luma_buffer.data(),
  175. luma_buffer.size());
  176. // Chroma
  177. const std::size_t half_height = frame_height / 2;
  178. const auto half_stride = static_cast<size_t>(frame->linesize[1]);
  179. switch (frame->format) {
  180. case AV_PIX_FMT_YUV420P: {
  181. // Frame from FFmpeg software
  182. // Populate chroma buffer from both channels with interleaving.
  183. const std::size_t half_width = frame_width / 2;
  184. const u8* chroma_b_src = frame->data[1];
  185. const u8* chroma_r_src = frame->data[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[dst + x * 2] = chroma_b_src[src + x];
  191. chroma_buffer[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->data[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. for (std::size_t x = 0; x < frame_width; ++x) {
  204. chroma_buffer[dst + x] = chroma_src[src + x];
  205. }
  206. }
  207. break;
  208. }
  209. default:
  210. ASSERT(false);
  211. break;
  212. }
  213. host1x.MemoryManager().WriteBlock(output_surface_chroma_address, chroma_buffer.data(),
  214. chroma_buffer.size());
  215. }
  216. } // namespace Host1x
  217. } // namespace Tegra