1
0
Fork 0

Fix build issue with AviSynth on linux

This commit is contained in:
wangqr 2020-03-06 00:15:07 -05:00
parent 6bd3f4c26b
commit 6cad7c3d6c
3 changed files with 48 additions and 7 deletions

View File

@ -558,9 +558,13 @@ if(WITH_AVISYNTH)
endif()
endif()
if(WITH_AVISYNTH)
target_compile_definitions(Aegisub PRIVATE "WITH_AVISYNTH" "AVS_LINKAGE_DLLIMPORT")
target_compile_definitions(Aegisub PRIVATE "WITH_AVISYNTH")
target_include_directories(Aegisub PRIVATE ${AviSynth_INCLUDE_DIRS})
target_link_libraries(Aegisub Vfw32 ${AviSynth_LIBRARIES})
if(WIN32)
target_link_libraries(Aegisub Vfw32)
endif()
get_filename_component(Avisynth_LIBRARY_NAME "${AviSynth_SHARED_LIBRARY}" NAME CACHE)
target_compile_definitions(Aegisub PRIVATE "AVISYNTH_SO=\"${Avisynth_LIBRARY_NAME}\"")
target_sources(Aegisub PRIVATE src/audio_provider_avs.cpp src/avisynth_wrap.cpp src/video_provider_avs.cpp)
endif()

View File

@ -1,10 +1,11 @@
find_package(PkgConfig QUIET)
pkg_check_modules(PC_AviSynth QUIET AviSynth)
pkg_check_modules(PC_AviSynth QUIET avisynth)
find_path(AviSynth_INCLUDE_DIRS
NAMES avisynth.h
PATH_SUFFIXES avisynth
HINTS ${PC_AviSynth_INCLUDE_DIRS}
)
find_library(AviSynth_LIBRARIES
find_library(AviSynth_SHARED_LIBRARY
NAMES avisynth
PATH_SUFFIXES c_api
HINTS ${PC_AviSynth_LIBRARY_DIRS}
@ -14,7 +15,7 @@ include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(AviSynth
FOUND_VAR AviSynth_FOUND
REQUIRED_VARS
AviSynth_LIBRARIES
AviSynth_SHARED_LIBRARY
AviSynth_INCLUDE_DIRS
VERSION_VAR AviSynth_VERSION
)

View File

@ -40,10 +40,29 @@
#include <mutex>
#ifndef _WIN32
#include <dlfcn.h>
#endif
#ifndef AVISYNTH_SO
// Fallback definition
#ifdef _WIN32
#define AVISYNTH_SO "AviSynth.dll"
#else
#define AVISYNTH_SO "libavisynth.so"
#endif
#endif
const AVS_Linkage* AVS_linkage;
// Allocate storage for and initialise static members
namespace {
int avs_refcount = 0;
#ifdef _WIN32
HINSTANCE hLib = nullptr;
#else
void* hLib = nullptr;
#endif
IScriptEnvironment *env = nullptr;
std::mutex AviSynthMutex;
}
@ -52,12 +71,24 @@ typedef IScriptEnvironment* __stdcall FUNC(int);
AviSynthWrapper::AviSynthWrapper() {
if (!avs_refcount++) {
hLib = LoadLibrary(L"avisynth.dll");
#ifdef _WIN32
#define CONCATENATE(x, y) x ## y
#define _Lstr(x) CONCATENATE(L, x)
hLib = LoadLibraryW(_Lstr(AVISYNTH_SO));
#undef _Lstr
#undef CONCATENATE
#else
hLib = dlopen(AVISYNTH_SO, RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND);
#endif
if (!hLib)
throw AvisynthError("Could not load avisynth.dll");
FUNC *CreateScriptEnv = (FUNC*)GetProcAddress(hLib, "CreateScriptEnvironment");
#ifdef _WIN32
FUNC* CreateScriptEnv = (FUNC*)GetProcAddress(hLib, "CreateScriptEnvironment");
#else
FUNC* CreateScriptEnv = (FUNC*)dlsym(hLib, "CreateScriptEnvironment");
#endif
if (!CreateScriptEnv)
throw AvisynthError("Failed to get address of CreateScriptEnv from avisynth.dll");
@ -69,6 +100,7 @@ AviSynthWrapper::AviSynthWrapper() {
if (!env)
throw AvisynthError("Failed to create a new avisynth script environment. Avisynth is too old?");
AVS_linkage = env->GetAVSLinkage();
// Set memory limit
const int memoryMax = OPT_GET("Provider/Avisynth/Memory Max")->GetInt();
@ -80,7 +112,11 @@ AviSynthWrapper::AviSynthWrapper() {
AviSynthWrapper::~AviSynthWrapper() {
if (!--avs_refcount) {
delete env;
#ifdef _WIN32
FreeLibrary(hLib);
#else
dlclose(hLib);
#endif
}
}