fix 7206.patch

This commit is contained in:
Ward Nakchbandi 2023-03-04 21:03:01 +03:00
parent f960549276
commit 2d7084b869

View File

@ -1,34 +1,14 @@
From 7ac1d462af1be795569e0d5c8f79871901e5d6e0 Mon Sep 17 00:00:00 2001
From: David Rosca <nowrep@gmail.com>
Date: Sun, 28 Aug 2022 10:15:16 +0200
Subject: [PATCH] obs-ffmpeg: Make AMF encoder work on Linux
Only the fallback encoders are available (no texture support).
Requires AMD proprietary Vulkan driver, using different driver
will be detected on startup and the encoders disabled.
---
plugins/obs-ffmpeg/CMakeLists.txt | 3 +
.../obs-ffmpeg/obs-amf-test/CMakeLists.txt | 11 +-
.../obs-amf-test/obs-amf-test-linux.cpp | 140 ++++++++++++++++++
plugins/obs-ffmpeg/obs-ffmpeg.c | 10 +-
plugins/obs-ffmpeg/texture-amf-opts.hpp | 2 +-
plugins/obs-ffmpeg/texture-amf.cpp | 81 +++++++++-
6 files changed, 236 insertions(+), 11 deletions(-)
create mode 100644 plugins/obs-ffmpeg/obs-amf-test/obs-amf-test-linux.cpp
diff --git a/plugins/obs-ffmpeg/CMakeLists.txt b/plugins/obs-ffmpeg/CMakeLists.txt
index 681a057..35cb1ea 100644
--- a/plugins/obs-ffmpeg/CMakeLists.txt
+++ b/plugins/obs-ffmpeg/CMakeLists.txt
@@ -119,11 +119,14 @@ if(OS_WINDOWS)
diff '--color=auto' -ru a/plugins/obs-ffmpeg/CMakeLists.txt b/plugins/obs-ffmpeg/CMakeLists.txt
--- a/plugins/obs-ffmpeg/CMakeLists.txt 2023-02-04 13:17:10.000000000 +0300
+++ b/plugins/obs-ffmpeg/CMakeLists.txt 2023-03-04 21:01:11.132594927 +0300
@@ -119,11 +119,14 @@
obs-ffmpeg.rc)
elseif(OS_POSIX AND NOT OS_MACOS)
+ add_subdirectory(obs-amf-test)
find_package(Libva REQUIRED)
find_package(Libpci REQUIRED)
target_sources(obs-ffmpeg PRIVATE vaapi-utils.c
target_sources(obs-ffmpeg PRIVATE obs-ffmpeg-vaapi.c vaapi-utils.c
vaapi-utils.h)
target_link_libraries(obs-ffmpeg PRIVATE Libva::va Libva::drm LIBPCI::LIBPCI)
+ target_sources(obs-ffmpeg PRIVATE texture-amf.cpp)
@ -36,11 +16,10 @@ index 681a057..35cb1ea 100644
endif()
setup_plugin_target(obs-ffmpeg)
diff --git a/plugins/obs-ffmpeg/obs-amf-test/CMakeLists.txt b/plugins/obs-ffmpeg/obs-amf-test/CMakeLists.txt
index 85347e7..034b4cc 100644
--- a/plugins/obs-ffmpeg/obs-amf-test/CMakeLists.txt
+++ b/plugins/obs-ffmpeg/obs-amf-test/CMakeLists.txt
@@ -3,8 +3,15 @@ project(obs-amf-test)
diff '--color=auto' -ru a/plugins/obs-ffmpeg/obs-amf-test/CMakeLists.txt b/plugins/obs-ffmpeg/obs-amf-test/CMakeLists.txt
--- a/plugins/obs-ffmpeg/obs-amf-test/CMakeLists.txt 2023-02-04 13:17:10.000000000 +0300
+++ b/plugins/obs-ffmpeg/obs-amf-test/CMakeLists.txt 2023-03-04 20:58:48.104742328 +0300
@@ -3,8 +3,15 @@
include_directories(SYSTEM ${CMAKE_SOURCE_DIR}/libobs)
add_executable(obs-amf-test)
@ -58,157 +37,11 @@ index 85347e7..034b4cc 100644
set_target_properties(obs-amf-test PROPERTIES FOLDER "plugins/obs-ffmpeg")
diff --git a/plugins/obs-ffmpeg/obs-amf-test/obs-amf-test-linux.cpp b/plugins/obs-ffmpeg/obs-amf-test/obs-amf-test-linux.cpp
new file mode 100644
index 0000000..018486e
--- /dev/null
+++ b/plugins/obs-ffmpeg/obs-amf-test/obs-amf-test-linux.cpp
@@ -0,0 +1,140 @@
+#include "../external/AMF/include/core/Factory.h"
+#include "../external/AMF/include/core/Trace.h"
+#include "../external/AMF/include/components/VideoEncoderVCE.h"
+#include "../external/AMF/include/components/VideoEncoderHEVC.h"
+#include "../external/AMF/include/components/VideoEncoderAV1.h"
+
+#include <dlfcn.h>
+#include <vulkan/vulkan.hpp>
+
+#include <string>
+#include <map>
+
+using namespace amf;
+
+struct adapter_caps {
+ bool is_amd = false;
+ bool supports_avc = false;
+ bool supports_hevc = false;
+ bool supports_av1 = false;
+};
+
+static AMFFactory *amf_factory = nullptr;
+static std::map<uint32_t, adapter_caps> adapter_info;
+
+static bool has_encoder(AMFContextPtr &amf_context, const wchar_t *encoder_name)
+{
+ AMFComponentPtr encoder;
+ AMF_RESULT res = amf_factory->CreateComponent(amf_context, encoder_name,
+ &encoder);
+ return res == AMF_OK;
+}
+
+static bool get_adapter_caps(uint32_t adapter_idx)
+{
+ if (adapter_idx)
+ return false;
+
+ adapter_caps &caps = adapter_info[adapter_idx];
+
+ AMF_RESULT res;
+ AMFContextPtr amf_context;
+ res = amf_factory->CreateContext(&amf_context);
+ if (res != AMF_OK)
+ return true;
+
+ AMFContext1 *context1 = NULL;
+ res = amf_context->QueryInterface(AMFContext1::IID(),
+ (void **)&context1);
+ if (res != AMF_OK)
+ return false;
+ res = context1->InitVulkan(nullptr);
+ context1->Release();
+ if (res != AMF_OK)
+ return false;
+
+ caps.is_amd = true;
+ caps.supports_avc = has_encoder(amf_context, AMFVideoEncoderVCE_AVC);
+ caps.supports_hevc = has_encoder(amf_context, AMFVideoEncoder_HEVC);
+ caps.supports_av1 = has_encoder(amf_context, AMFVideoEncoder_AV1);
+
+ return true;
+}
+
+int main(void)
+try {
+ AMF_RESULT res;
+ VkResult vkres;
+
+ VkApplicationInfo app_info = {};
+ app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
+ app_info.pApplicationName = "obs-amf-test";
+ app_info.apiVersion = VK_API_VERSION_1_2;
+
+ VkInstanceCreateInfo info = {};
+ info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
+ info.pApplicationInfo = &app_info;
+
+ VkInstance instance;
+ vkres = vkCreateInstance(&info, nullptr, &instance);
+ if (vkres != VK_SUCCESS)
+ throw "Failed to initialize Vulkan";
+
+ uint32_t device_count;
+ vkres = vkEnumeratePhysicalDevices(instance, &device_count, nullptr);
+ if (vkres != VK_SUCCESS || !device_count)
+ throw "Failed to enumerate Vulkan devices";
+
+ VkPhysicalDevice *devices = new VkPhysicalDevice[device_count];
+ vkres = vkEnumeratePhysicalDevices(instance, &device_count, devices);
+ if (vkres != VK_SUCCESS)
+ throw "Failed to enumerate Vulkan devices";
+
+ VkPhysicalDeviceDriverProperties driver_props = {};
+ driver_props.sType =
+ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES;
+ VkPhysicalDeviceProperties2 device_props = {};
+ device_props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
+ device_props.pNext = &driver_props;
+ vkGetPhysicalDeviceProperties2(devices[0], &device_props);
+
+ if (strcmp(driver_props.driverName, "AMD proprietary driver"))
+ throw "Not running AMD proprietary driver";
+
+ vkDestroyInstance(instance, nullptr);
+
+ /* --------------------------------------------------------- */
+ /* try initializing amf, I guess */
+
+ void *amf_module = dlopen(AMF_DLL_NAMEA, RTLD_LAZY);
+ if (!amf_module)
+ throw "Failed to load AMF lib";
+
+ auto init = (AMFInit_Fn)dlsym(amf_module, AMF_INIT_FUNCTION_NAME);
+ if (!init)
+ throw "Failed to get init func";
+
+ res = init(AMF_FULL_VERSION, &amf_factory);
+ if (res != AMF_OK)
+ throw "AMFInit failed";
+
+ uint32_t idx = 0;
+ while (get_adapter_caps(idx++))
+ ;
+
+ for (auto &[idx, caps] : adapter_info) {
+ printf("[%u]\n", idx);
+ printf("is_amd=%s\n", caps.is_amd ? "true" : "false");
+ printf("supports_avc=%s\n",
+ caps.supports_avc ? "true" : "false");
+ printf("supports_hevc=%s\n",
+ caps.supports_hevc ? "true" : "false");
+ printf("supports_av1=%s\n",
+ caps.supports_av1 ? "true" : "false");
+ }
+
+ return 0;
+} catch (const char *text) {
+ printf("[error]\nstring=%s\n", text);
+ return 0;
+}
diff --git a/plugins/obs-ffmpeg/obs-ffmpeg.c b/plugins/obs-ffmpeg/obs-ffmpeg.c
index 1677e08..4bb870a 100644
--- a/plugins/obs-ffmpeg/obs-ffmpeg.c
+++ b/plugins/obs-ffmpeg/obs-ffmpeg.c
@@ -322,6 +322,9 @@ static bool nvenc_supported(bool *out_h264, bool *out_hevc, bool *out_av1)
Only in b/plugins/obs-ffmpeg/obs-amf-test: obs-amf-test-linux.cpp
diff '--color=auto' -ru a/plugins/obs-ffmpeg/obs-ffmpeg.c b/plugins/obs-ffmpeg/obs-ffmpeg.c
--- a/plugins/obs-ffmpeg/obs-ffmpeg.c 2023-02-04 13:17:10.000000000 +0300
+++ b/plugins/obs-ffmpeg/obs-ffmpeg.c 2023-03-04 20:58:48.104742328 +0300
@@ -339,6 +339,9 @@
#ifdef _WIN32
extern void jim_nvenc_load(bool h264, bool hevc, bool av1);
extern void jim_nvenc_unload(void);
@ -218,7 +51,7 @@ index 1677e08..4bb870a 100644
extern void amf_load(void);
extern void amf_unload(void);
#endif
@@ -391,7 +394,7 @@ bool obs_module_load(void)
@@ -408,7 +411,7 @@
#endif
}
@ -227,7 +60,7 @@ index 1677e08..4bb870a 100644
amf_load();
#endif
@@ -409,8 +412,11 @@ void obs_module_unload(void)
@@ -440,8 +443,11 @@
obs_ffmpeg_unload_logging();
#endif
@ -240,23 +73,10 @@ index 1677e08..4bb870a 100644
jim_nvenc_unload();
#endif
}
diff --git a/plugins/obs-ffmpeg/texture-amf-opts.hpp b/plugins/obs-ffmpeg/texture-amf-opts.hpp
index b1c37d2..d28e3f7 100644
--- a/plugins/obs-ffmpeg/texture-amf-opts.hpp
+++ b/plugins/obs-ffmpeg/texture-amf-opts.hpp
@@ -321,7 +321,7 @@ static void amf_apply_opt(amf_base *enc, obs_option *opt)
val = atoi(opt->value);
}
- os_utf8_to_wcs(opt->name, 0, wname, _countof(wname));
+ os_utf8_to_wcs(opt->name, 0, wname, amf_countof(wname));
if (is_bool) {
bool bool_val = (bool)val;
set_amf_property(enc, wname, bool_val);
diff --git a/plugins/obs-ffmpeg/texture-amf.cpp b/plugins/obs-ffmpeg/texture-amf.cpp
index 92e57e0..8c648ce 100644
--- a/plugins/obs-ffmpeg/texture-amf.cpp
+++ b/plugins/obs-ffmpeg/texture-amf.cpp
Only in b/plugins/obs-ffmpeg: obs-ffmpeg.c.orig
diff '--color=auto' -ru a/plugins/obs-ffmpeg/texture-amf.cpp b/plugins/obs-ffmpeg/texture-amf.cpp
--- a/plugins/obs-ffmpeg/texture-amf.cpp 2023-02-04 13:17:10.000000000 +0300
+++ b/plugins/obs-ffmpeg/texture-amf.cpp 2023-03-04 20:58:48.108075702 +0300
@@ -20,6 +20,7 @@
#include "external/AMF/include/core/Factory.h"
#include "external/AMF/include/core/Trace.h"
@ -274,7 +94,7 @@ index 92e57e0..8c648ce 100644
#include <util/platform.h>
#include <util/util.hpp>
#include <util/pipe.h>
@@ -57,8 +60,10 @@ struct amf_error {
@@ -57,8 +60,10 @@
struct handle_tex {
uint32_t handle;
@ -285,7 +105,7 @@ index 92e57e0..8c648ce 100644
};
struct adapter_caps {
@@ -74,7 +79,7 @@ static std::map<uint32_t, adapter_caps> caps;
@@ -74,7 +79,7 @@
static bool h264_supported = false;
static AMFFactory *amf_factory = nullptr;
static AMFTrace *amf_trace = nullptr;
@ -294,7 +114,7 @@ index 92e57e0..8c648ce 100644
static uint64_t amf_version = 0;
/* ========================================================================= */
@@ -122,9 +127,11 @@ struct amf_base {
@@ -122,9 +127,11 @@
virtual void init() = 0;
};
@ -307,7 +127,7 @@ index 92e57e0..8c648ce 100644
struct amf_texencode : amf_base, public AMFSurfaceObserver {
volatile bool destroying = false;
@@ -161,6 +168,7 @@ struct amf_texencode : amf_base, public AMFSurfaceObserver {
@@ -161,6 +168,7 @@
throw amf_error("InitDX11 failed", res);
}
};
@ -315,7 +135,7 @@ index 92e57e0..8c648ce 100644
struct amf_fallback : amf_base, public AMFSurfaceObserver {
volatile bool destroying = false;
@@ -188,9 +196,21 @@ struct amf_fallback : amf_base, public AMFSurfaceObserver {
@@ -188,9 +196,21 @@
void init() override
{
@ -337,7 +157,7 @@ index 92e57e0..8c648ce 100644
}
};
@@ -249,6 +269,7 @@ static void set_amf_property(amf_base *enc, const wchar_t *name, const T &value)
@@ -249,6 +269,7 @@
/* ------------------------------------------------------------------------- */
/* Implementation */
@ -345,7 +165,7 @@ index 92e57e0..8c648ce 100644
static HMODULE get_lib(const char *lib)
{
HMODULE mod = GetModuleHandleA(lib);
@@ -395,6 +416,7 @@ static void get_tex_from_handle(amf_texencode *enc, uint32_t handle,
@@ -395,6 +416,7 @@
*km_out = km.Detach();
*tex_out = tex.Detach();
}
@ -353,7 +173,7 @@ index 92e57e0..8c648ce 100644
static constexpr amf_int64 macroblock_size = 16;
@@ -640,6 +662,7 @@ static void amf_encode_base(amf_base *enc, AMFSurface *amf_surf,
@@ -640,6 +662,7 @@
static bool amf_encode_tex(void *data, uint32_t handle, int64_t pts,
uint64_t lock_key, uint64_t *next_key,
encoder_packet *packet, bool *received_packet)
@ -361,7 +181,7 @@ index 92e57e0..8c648ce 100644
try {
amf_texencode *enc = (amf_texencode *)data;
ID3D11DeviceContext *context = enc->context;
@@ -716,6 +739,18 @@ try {
@@ -716,6 +739,18 @@
*received_packet = false;
return false;
}
@ -380,7 +200,7 @@ index 92e57e0..8c648ce 100644
static buf_t alloc_buf(amf_fallback *enc)
{
@@ -1406,6 +1441,7 @@ static void amf_avc_create_internal(amf_base *enc, obs_data_t *settings)
@@ -1406,6 +1441,7 @@
static void *amf_avc_create_texencode(obs_data_t *settings,
obs_encoder_t *encoder)
@ -388,7 +208,7 @@ index 92e57e0..8c648ce 100644
try {
check_texture_encode_capability(encoder, amf_codec_type::AVC);
@@ -1428,6 +1464,12 @@ try {
@@ -1428,6 +1464,12 @@
blog(LOG_ERROR, "[texture-amf-h264] %s: %s", __FUNCTION__, err);
return obs_encoder_create_rerouted(encoder, "h264_fallback_amf");
}
@ -401,7 +221,7 @@ index 92e57e0..8c648ce 100644
static void *amf_avc_create_fallback(obs_data_t *settings,
obs_encoder_t *encoder)
@@ -1748,6 +1790,7 @@ static void amf_hevc_create_internal(amf_base *enc, obs_data_t *settings)
@@ -1748,6 +1790,7 @@
static void *amf_hevc_create_texencode(obs_data_t *settings,
obs_encoder_t *encoder)
@ -409,7 +229,7 @@ index 92e57e0..8c648ce 100644
try {
check_texture_encode_capability(encoder, amf_codec_type::HEVC);
@@ -1770,6 +1813,12 @@ try {
@@ -1770,6 +1813,12 @@
blog(LOG_ERROR, "[texture-amf-h265] %s: %s", __FUNCTION__, err);
return obs_encoder_create_rerouted(encoder, "h265_fallback_amf");
}
@ -422,7 +242,7 @@ index 92e57e0..8c648ce 100644
static void *amf_hevc_create_fallback(obs_data_t *settings,
obs_encoder_t *encoder)
@@ -2047,6 +2096,7 @@ static void amf_av1_create_internal(amf_base *enc, obs_data_t *settings)
@@ -2047,6 +2096,7 @@
static void *amf_av1_create_texencode(obs_data_t *settings,
obs_encoder_t *encoder)
@ -430,7 +250,7 @@ index 92e57e0..8c648ce 100644
try {
check_texture_encode_capability(encoder, amf_codec_type::AV1);
@@ -2069,6 +2119,12 @@ try {
@@ -2069,6 +2119,12 @@
blog(LOG_ERROR, "[texture-amf-av1] %s: %s", __FUNCTION__, err);
return obs_encoder_create_rerouted(encoder, "av1_fallback_amf");
}
@ -443,7 +263,7 @@ index 92e57e0..8c648ce 100644
static void *amf_av1_create_fallback(obs_data_t *settings,
obs_encoder_t *encoder)
@@ -2159,9 +2215,16 @@ static bool enum_luids(void *param, uint32_t idx, uint64_t luid)
@@ -2159,9 +2215,16 @@
return true;
}
@ -460,7 +280,7 @@ index 92e57e0..8c648ce 100644
HMODULE amf_module_test;
/* Check if the DLL is present before running the more expensive */
@@ -2171,17 +2234,25 @@ try {
@@ -2171,17 +2234,25 @@
if (!amf_module_test)
throw "No AMF library";
FreeLibrary(amf_module_test);
@ -488,7 +308,7 @@ index 92e57e0..8c648ce 100644
os_process_pipe_t *pp = os_process_pipe_create(cmd.str().c_str(), "r");
if (!pp)
throw "Failed to launch the AMF test process I guess";
@@ -2240,12 +2311,12 @@ try {
@@ -2240,12 +2311,12 @@
/* ----------------------------------- */
/* Init AMF */
@ -503,7 +323,7 @@ index 92e57e0..8c648ce 100644
if (!init)
throw "Failed to get AMFInit address";
@@ -2257,7 +2328,7 @@ try {
@@ -2257,7 +2328,7 @@
if (res != AMF_OK)
throw amf_error("GetTrace failed", res);
@ -512,6 +332,15 @@ index 92e57e0..8c648ce 100644
amf_module, AMF_QUERY_VERSION_FUNCTION_NAME);
if (!get_ver)
throw "Failed to get AMFQueryVersion address";
--
2.39.0
diff '--color=auto' -ru a/plugins/obs-ffmpeg/texture-amf-opts.hpp b/plugins/obs-ffmpeg/texture-amf-opts.hpp
--- a/plugins/obs-ffmpeg/texture-amf-opts.hpp 2023-02-04 13:17:10.000000000 +0300
+++ b/plugins/obs-ffmpeg/texture-amf-opts.hpp 2023-03-04 20:58:48.104742328 +0300
@@ -321,7 +321,7 @@
val = atoi(opt->value);
}
- os_utf8_to_wcs(opt->name, 0, wname, _countof(wname));
+ os_utf8_to_wcs(opt->name, 0, wname, amf_countof(wname));
if (is_bool) {
bool bool_val = (bool)val;
set_amf_property(enc, wname, bool_val);