summaryrefslogtreecommitdiff
path: root/src/video_core/command_classes
diff options
context:
space:
mode:
Diffstat (limited to 'src/video_core/command_classes')
-rw-r--r--src/video_core/command_classes/codecs/codec.cpp86
1 files changed, 54 insertions, 32 deletions
diff --git a/src/video_core/command_classes/codecs/codec.cpp b/src/video_core/command_classes/codecs/codec.cpp
index 916277811..2a532b883 100644
--- a/src/video_core/command_classes/codecs/codec.cpp
+++ b/src/video_core/command_classes/codecs/codec.cpp
@@ -2,6 +2,8 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <algorithm>
6#include <cstdio>
5#include <fstream> 7#include <fstream>
6#include <vector> 8#include <vector>
7#include "common/assert.h" 9#include "common/assert.h"
@@ -15,12 +17,28 @@
15 17
16extern "C" { 18extern "C" {
17#include <libavutil/opt.h> 19#include <libavutil/opt.h>
20#ifdef LIBVA_FOUND
21// for querying VAAPI driver information
22#include <libavutil/hwcontext_vaapi.h>
23#endif
18} 24}
19 25
20namespace Tegra { 26namespace Tegra {
21namespace { 27namespace {
22constexpr AVPixelFormat PREFERRED_GPU_FMT = AV_PIX_FMT_NV12; 28constexpr AVPixelFormat PREFERRED_GPU_FMT = AV_PIX_FMT_NV12;
23constexpr AVPixelFormat PREFERRED_CPU_FMT = AV_PIX_FMT_YUV420P; 29constexpr AVPixelFormat PREFERRED_CPU_FMT = AV_PIX_FMT_YUV420P;
30constexpr std::array PREFERRED_GPU_DECODERS = {
31 AV_HWDEVICE_TYPE_CUDA,
32#ifdef _WIN32
33 AV_HWDEVICE_TYPE_D3D11VA,
34 AV_HWDEVICE_TYPE_DXVA2,
35#elif defined(__linux__)
36 AV_HWDEVICE_TYPE_VAAPI,
37 AV_HWDEVICE_TYPE_VDPAU,
38#endif
39 // last resort for Linux Flatpak (w/ NVIDIA)
40 AV_HWDEVICE_TYPE_VULKAN,
41};
24 42
25void AVPacketDeleter(AVPacket* ptr) { 43void AVPacketDeleter(AVPacket* ptr) {
26 av_packet_free(&ptr); 44 av_packet_free(&ptr);
@@ -59,46 +77,50 @@ Codec::~Codec() {
59 av_buffer_unref(&av_gpu_decoder); 77 av_buffer_unref(&av_gpu_decoder);
60} 78}
61 79
80// List all the currently available hwcontext in ffmpeg
81static std::vector<AVHWDeviceType> ListSupportedContexts() {
82 std::vector<AVHWDeviceType> contexts{};
83 AVHWDeviceType current_device_type = AV_HWDEVICE_TYPE_NONE;
84 do {
85 current_device_type = av_hwdevice_iterate_types(current_device_type);
86 contexts.push_back(current_device_type);
87 } while (current_device_type != AV_HWDEVICE_TYPE_NONE);
88 return contexts;
89}
90
62bool Codec::CreateGpuAvDevice() { 91bool Codec::CreateGpuAvDevice() {
63#if defined(LIBVA_FOUND)
64 static constexpr std::array<const char*, 3> VAAPI_DRIVERS = {
65 "i915",
66 "iHD",
67 "amdgpu",
68 };
69 AVDictionary* hwdevice_options = nullptr;
70 av_dict_set(&hwdevice_options, "connection_type", "drm", 0);
71 for (const auto& driver : VAAPI_DRIVERS) {
72 av_dict_set(&hwdevice_options, "kernel_driver", driver, 0);
73 const int hwdevice_error = av_hwdevice_ctx_create(&av_gpu_decoder, AV_HWDEVICE_TYPE_VAAPI,
74 nullptr, hwdevice_options, 0);
75 if (hwdevice_error >= 0) {
76 LOG_INFO(Service_NVDRV, "Using VA-API with {}", driver);
77 av_dict_free(&hwdevice_options);
78 av_codec_ctx->pix_fmt = AV_PIX_FMT_VAAPI;
79 return true;
80 }
81 LOG_DEBUG(Service_NVDRV, "VA-API av_hwdevice_ctx_create failed {}", hwdevice_error);
82 }
83 LOG_DEBUG(Service_NVDRV, "VA-API av_hwdevice_ctx_create failed for all drivers");
84 av_dict_free(&hwdevice_options);
85#endif
86 static constexpr auto HW_CONFIG_METHOD = AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX; 92 static constexpr auto HW_CONFIG_METHOD = AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX;
87 static constexpr std::array GPU_DECODER_TYPES{ 93 static const auto supported_contexts = ListSupportedContexts();
88 AV_HWDEVICE_TYPE_CUDA, 94 for (const auto& type : PREFERRED_GPU_DECODERS) {
89#ifdef _WIN32 95 if (std::none_of(supported_contexts.begin(), supported_contexts.end(),
90 AV_HWDEVICE_TYPE_D3D11VA, 96 [&type](const auto& context) { return context == type; })) {
91#else 97 LOG_DEBUG(Service_NVDRV, "{} explicitly unsupported", av_hwdevice_get_type_name(type));
92 AV_HWDEVICE_TYPE_VDPAU, 98 continue;
93#endif 99 }
94 };
95 for (const auto& type : GPU_DECODER_TYPES) {
96 const int hwdevice_res = av_hwdevice_ctx_create(&av_gpu_decoder, type, nullptr, nullptr, 0); 100 const int hwdevice_res = av_hwdevice_ctx_create(&av_gpu_decoder, type, nullptr, nullptr, 0);
97 if (hwdevice_res < 0) { 101 if (hwdevice_res < 0) {
98 LOG_DEBUG(Service_NVDRV, "{} av_hwdevice_ctx_create failed {}", 102 LOG_DEBUG(Service_NVDRV, "{} av_hwdevice_ctx_create failed {}",
99 av_hwdevice_get_type_name(type), hwdevice_res); 103 av_hwdevice_get_type_name(type), hwdevice_res);
100 continue; 104 continue;
101 } 105 }
106#ifdef LIBVA_FOUND
107 if (type == AV_HWDEVICE_TYPE_VAAPI) {
108 // we need to determine if this is an impersonated VAAPI driver
109 AVHWDeviceContext* hwctx =
110 static_cast<AVHWDeviceContext*>(static_cast<void*>(av_gpu_decoder->data));
111 AVVAAPIDeviceContext* vactx = static_cast<AVVAAPIDeviceContext*>(hwctx->hwctx);
112 const char* vendor_name = vaQueryVendorString(vactx->display);
113 if (strstr(vendor_name, "VDPAU backend")) {
114 // VDPAU impersonated VAAPI impl's are super buggy, we need to skip them
115 LOG_DEBUG(Service_NVDRV, "Skipping vdapu impersonated VAAPI driver");
116 continue;
117 } else {
118 // according to some user testing, certain vaapi driver (Intel?) could be buggy
119 // so let's log the driver name which may help the developers/supporters
120 LOG_DEBUG(Service_NVDRV, "Using VAAPI driver: {}", vendor_name);
121 }
122 }
123#endif
102 for (int i = 0;; i++) { 124 for (int i = 0;; i++) {
103 const AVCodecHWConfig* config = avcodec_get_hw_config(av_codec, i); 125 const AVCodecHWConfig* config = avcodec_get_hw_config(av_codec, i);
104 if (!config) { 126 if (!config) {