From 4907af00e77c28c35c8f3bca9ac237c3120c2ea3 Mon Sep 17 00:00:00 2001 From: Asd-g <65298684+Asd-g@users.noreply.github.com> Date: Sat, 31 Aug 2024 23:38:06 +0300 Subject: [PATCH] Update mlrt_ov version to 1.0.3 Change how runtime files are loaded (now they must be located in `mlrt_ov_rt` folder next to `mlrt_ov.dll`. --- mlrt_ov/CHANGELOG.md | 3 ++ mlrt_ov/README.md | 14 ++----- mlrt_ov/src/mlrt_ov.cpp | 83 +++++++++++++++++++++++++++++++++++++++-- mlrt_ov/src/mlrt_ov.rc | 8 ++-- 4 files changed, 89 insertions(+), 19 deletions(-) diff --git a/mlrt_ov/CHANGELOG.md b/mlrt_ov/CHANGELOG.md index 20c4285..def343e 100644 --- a/mlrt_ov/CHANGELOG.md +++ b/mlrt_ov/CHANGELOG.md @@ -1,3 +1,6 @@ +##### 1.0.3: + Changed how runtime files are loaded (now they must be located in `mlrt_ov_rt` folder next to `mlrt_ov.dll`). + ##### 1.0.2: Fixed undefined behavior with ACP filename. Replaced the depreciated run_on_function with run_on_model. diff --git a/mlrt_ov/README.md b/mlrt_ov/README.md index e46bf6c..5cad0ac 100644 --- a/mlrt_ov/README.md +++ b/mlrt_ov/README.md @@ -1,17 +1,9 @@ ## mlrt_ov -Download the required OpenVINO runtimes (`openvino_dll.7z`) from [Releases](https://github.com/Asd-g/avs-mlrt/releases). +### Runtime files -How to load the above runtimes: -- (Optional) Add the extracted files to PATH. -- Download [LoadDLL.dll](https://forum.doom9.org/showthread.php?t=173259). -- Create the following script (for example `mlrt_ov_loader.avsi`) (it could be placed in the plugins folder for autoloading or be mannually imported): - -``` -LoadDLL("path_to\tbb12.dll") -LoadDLL("path_to\openvino.dll") -LoadPlugin("mlrt_ov.dll") -``` +All runtime files must be in `mlrt_ov_rt` folder that is located in the same folder as `mlrt_ov.dll`.
+They can be downloaded from [Releases](https://github.com/Asd-g/avs-mlrt/releases) (` openvino_dll.7z`). ### Note: diff --git a/mlrt_ov/src/mlrt_ov.cpp b/mlrt_ov/src/mlrt_ov.cpp index 3306af6..ca3b2b8 100644 --- a/mlrt_ov/src/mlrt_ov.cpp +++ b/mlrt_ov/src/mlrt_ov.cpp @@ -16,6 +16,16 @@ #include +#ifdef _WIN32 +#define NOCOMM +#define NOMINMAX +#define WIN32_LEAN_AND_MEAN + +#include + +static std::atomic ref_count{ 0 }; +#endif // _WIN32 + extern std::variant loadONNX( const std::string& path, @@ -188,6 +198,11 @@ struct OVData std::string output_name; std::string err; + +#ifdef _WIN32 + std::string mlrt_ov_path; +#endif // _WIN32 + }; @@ -360,6 +375,19 @@ static void AVSC_CC free_mlrt_ov(AVS_FilterInfo* fi) for (const auto& node : d->nodes) avs_release_clip(node); +#ifdef _WIN32 + if (--ref_count == 0) + { + std::array loaded_dlls{ + GetModuleHandleA((d->mlrt_ov_path + "/mlrt_ov_rt/openvino.dll").c_str()), + GetModuleHandleA((d->mlrt_ov_path + "/mlrt_ov_rt/tbb12.dll").c_str()) + }; + + for (int i{ 0 }; i < 2; ++i) + FreeLibrary(loaded_dlls[i]); + } +#endif // _WIN32 + delete d; } @@ -374,8 +402,37 @@ static AVS_Value AVSC_CC Create_mlrt_ov(AVS_ScriptEnvironment* env, AVS_Value ar { enum { Clips, Network_path, Overlap_w, Overlap_h, Tilesize_w, Tilesize_h, Device, Builtin, Builtindir, Fp16, Config, Path_is_serialization, List_devices, Fp16_blacklist_ops, Dot_path }; + const std::string mlrt_ov_path{ boost::dll::this_line_location().parent_path().generic_string() }; + +#ifdef _WIN32 + if (ref_count == 0) + { + std::array loaded_dlls{ + LoadLibraryA((mlrt_ov_path + "/mlrt_ov_rt/tbb12.dll").c_str()), + LoadLibraryA((mlrt_ov_path + "/mlrt_ov_rt/openvino.dll").c_str()), + }; + if (!loaded_dlls[0]) + { + std::string_view tbb12_path{ ("mlrt_ov: cannot find " + mlrt_ov_path + "/mlrt_ov_rt/tbb12.dll").c_str() }; + return avs_new_value_error(avs_save_string(env, tbb12_path.data(), tbb12_path.size())); + } + if (!loaded_dlls[1]) + { + FreeLibrary(loaded_dlls[0]); + std::string_view openvino_path{ ("mlrt_ov: cannot find " + mlrt_ov_path + "/mlrt_ov_rt/openvino.dll").c_str() }; + return avs_new_value_error(avs_save_string(env, openvino_path.data(), openvino_path.size())); + } + } + + ++ref_count; +#endif // _WIN32 + OVData* d{ new OVData() }; +#ifdef _WIN32 + d->mlrt_ov_path = mlrt_ov_path; +#endif // _WIN32 + AVS_FilterInfo* fi; AVS_Clip* clip{ avs_new_c_filter(env, &fi, *avs_as_array(avs_array_elt(args, Clips)), 1) }; @@ -428,8 +485,7 @@ static AVS_Value AVSC_CC Create_mlrt_ov(AVS_ScriptEnvironment* env, AVS_Value ar d->err = "mlrt_ov: "s + error_message; return avs_new_value_error(d->err.c_str()); - } - }; + } }; if (avs_check_version(env, 10)) return set_error("AviSynth+ version must be r3928 or later."); @@ -508,7 +564,7 @@ static AVS_Value AVSC_CC Create_mlrt_ov(AVS_ScriptEnvironment* env, AVS_Value ar if (builtin) { std::string modeldir{ avs_defined(avs_array_elt(args, Builtindir)) ? (avs_as_string(avs_array_elt(args, Builtindir))) : "models" }; - network_path = boost::dll::this_line_location().parent_path().generic_string() + "/" + modeldir + "/" + network_path; + network_path = mlrt_ov_path + "/" + modeldir + "/" + network_path; } auto result{ loadONNX(network_path, 0, tile_w, tile_h, path_is_serialization) }; @@ -665,6 +721,25 @@ static AVS_Value AVSC_CC Create_mlrt_ov(AVS_ScriptEnvironment* env, AVS_Value ar const char* AVSC_CC avisynth_c_plugin_init(AVS_ScriptEnvironment* env) { - avs_add_function(env, "mlrt_ov", "c+[network_path]s[overlap_w]i[overlap_h]i[tilesize_w]i[tilesize_h]i[device]s[builtin]b[builtindir]s[fp16]b[config]s[path_is_serialization]b[list_devices]b[fp16_blacklist_ops]s*[dot_path]s", Create_mlrt_ov, 0); + const char args[]{ + "c+" + "[network_path]s" + "[overlap_w]i" + "[overlap_h]i" + "[tilesize_w]i" + "[tilesize_h]i" + "[device]s" + "[builtin]b" + "[builtindir]s" + "[fp16]b" + "[config]s" + "[path_is_serialization]b" + "[list_devices]b" + "[fp16_blacklist_ops]s*" + "[dot_path]s" + }; + + avs_add_function(env, "mlrt_ov", args, Create_mlrt_ov, 0); + return "mlrt_ov"; } diff --git a/mlrt_ov/src/mlrt_ov.rc b/mlrt_ov/src/mlrt_ov.rc index d96398e..6d48ffc 100644 --- a/mlrt_ov/src/mlrt_ov.rc +++ b/mlrt_ov/src/mlrt_ov.rc @@ -1,8 +1,8 @@ #include 1 VERSIONINFO -FILEVERSION 1,0,2,0 -PRODUCTVERSION 1,0,2,0 +FILEVERSION 1,0,3,0 +PRODUCTVERSION 1,0,3,0 FILEFLAGSMASK VS_FFI_FILEFLAGSMASK FILETYPE VFT_DLL BEGIN @@ -12,11 +12,11 @@ BEGIN BEGIN VALUE "Comments", "ML filter runtimes." VALUE "FileDescription", "mlrt_ov for AviSynth+." - VALUE "FileVersion", "1.0.2" + VALUE "FileVersion", "1.0.3" VALUE "InternalName", "mlrt_ov" VALUE "OriginalFilename", "mlrt_ov.dll" VALUE "ProductName", "mlrt_ov" - VALUE "ProductVersion", "1.0.2" + VALUE "ProductVersion", "1.0.3" END END BLOCK "VarFileInfo"