From b38851bb938aa444dbfa330dfdd2269111d9e162 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Wed, 18 Jan 2012 20:08:32 +0000 Subject: [PATCH] Make the avisynth providers store an AvisynthWrapper rather than inherit from it Originally committed to SVN as r6307. --- aegisub/src/audio_provider_avs.cpp | 8 +++-- aegisub/src/audio_provider_avs.h | 6 +++- aegisub/src/avisynth_wrap.cpp | 47 ++++++++++++++---------------- aegisub/src/avisynth_wrap.h | 26 +++-------------- aegisub/src/video_provider_avs.cpp | 9 +++--- aegisub/src/video_provider_avs.h | 4 ++- 6 files changed, 45 insertions(+), 55 deletions(-) diff --git a/aegisub/src/audio_provider_avs.cpp b/aegisub/src/audio_provider_avs.cpp index cbe206cc4..066f95a50 100644 --- a/aegisub/src/audio_provider_avs.cpp +++ b/aegisub/src/audio_provider_avs.cpp @@ -62,12 +62,14 @@ AvisynthAudioProvider::AvisynthAudioProvider(wxString filename) { try { AVSValue script; - wxMutexLocker lock(AviSynthMutex); + wxMutexLocker lock(avs_wrapper.GetMutex()); wxFileName fn(filename); if (!fn.FileExists()) throw agi::FileNotFoundError(STD_STR(filename)); + IScriptEnvironment *env = avs_wrapper.GetEnv(); + // Include if (filename.EndsWith(".avs")) { char *fname = env->SaveString(fn.GetShortPath().mb_str(csConvLocal)); @@ -116,6 +118,8 @@ void AvisynthAudioProvider::LoadFromClip(AVSValue _clip) { VideoInfo vi = _clip.AsClip()->GetVideoInfo(); if (!vi.HasAudio()) throw agi::AudioDataNotFoundError("No audio found.", 0); + IScriptEnvironment *env = avs_wrapper.GetEnv(); + // Convert to one channel char buffer[1024]; strcpy(buffer,lagi_wxString(OPT_GET("Audio/Downmixer")->GetString()).mb_str(csConvLocal)); @@ -174,7 +178,7 @@ void AvisynthAudioProvider::GetAudio(void *buf, int64_t start, int64_t count) co } if (count) { - clip->GetAudio(buf,start,count,env); + clip->GetAudio(buf,start,count,avs_wrapper.GetEnv()); } } #endif diff --git a/aegisub/src/audio_provider_avs.h b/aegisub/src/audio_provider_avs.h index 31836ec87..f88546a85 100644 --- a/aegisub/src/audio_provider_avs.h +++ b/aegisub/src/audio_provider_avs.h @@ -36,6 +36,8 @@ #ifdef WITH_AVISYNTH #include "include/aegisub/audio_provider.h" + +#include "avisynth.h" #include "avisynth_wrap.h" @@ -44,7 +46,9 @@ /// @brief DOCME /// /// DOCME -class AvisynthAudioProvider : public AudioProvider, public AviSynthWrapper { +class AvisynthAudioProvider : public AudioProvider { + AviSynthWrapper avs_wrapper; + /// DOCME wxString filename; diff --git a/aegisub/src/avisynth_wrap.cpp b/aegisub/src/avisynth_wrap.cpp index 56629f016..e12070095 100644 --- a/aegisub/src/avisynth_wrap.cpp +++ b/aegisub/src/avisynth_wrap.cpp @@ -38,30 +38,30 @@ #ifdef WITH_AVISYNTH #include "avisynth_wrap.h" + +#include "avisynth.h" #include "main.h" // Allocate storage for and initialise static members -int AviSynthWrapper::avs_refcount = 0; -HINSTANCE AviSynthWrapper::hLib = NULL; -IScriptEnvironment *AviSynthWrapper::env = NULL; -wxMutex AviSynthWrapper::AviSynthMutex; +namespace { + int avs_refcount = 0; + HINSTANCE hLib = NULL; + IScriptEnvironment *env = NULL; + wxMutex AviSynthMutex; +} +typedef IScriptEnvironment* __stdcall FUNC(int); -/// @brief AviSynth constructor -/// AviSynthWrapper::AviSynthWrapper() { - if (!avs_refcount) { - hLib=LoadLibrary(L"avisynth.dll"); + if (!avs_refcount++) { + hLib = LoadLibrary(L"avisynth.dll"); - if (hLib == NULL) { + if (!hLib) throw wxString("Could not load avisynth.dll"); - } FUNC *CreateScriptEnv = (FUNC*)GetProcAddress(hLib, "CreateScriptEnvironment"); - - if (CreateScriptEnv == NULL) { + if (!CreateScriptEnv) throw wxString("Failed to get address of CreateScriptEnv from avisynth.dll"); - } // Require Avisynth 2.5.6+? if (OPT_GET("Provider/Avisynth/Allow Ancient")->GetBool()) @@ -69,21 +69,16 @@ AviSynthWrapper::AviSynthWrapper() { else env = CreateScriptEnv(AVISYNTH_INTERFACE_VERSION); - if (env == NULL) { + if (!env) throw wxString("Failed to create a new avisynth script environment. Avisynth is too old?"); - } + // Set memory limit const int memoryMax = OPT_GET("Provider/Avisynth/Memory Max")->GetInt(); - if (memoryMax != 0) { + if (memoryMax != 0) env->SetMemoryMax(memoryMax); - } } - - avs_refcount++; } -/// @brief AviSynth destructor -/// AviSynthWrapper::~AviSynthWrapper() { if (!--avs_refcount) { delete env; @@ -91,10 +86,12 @@ AviSynthWrapper::~AviSynthWrapper() { } } -/// @brief Get environment -/// -IScriptEnvironment *AviSynthWrapper::GetEnv() { +wxMutex& AviSynthWrapper::GetMutex() const { + return AviSynthMutex; +} + +IScriptEnvironment *AviSynthWrapper::GetEnv() const { return env; } -#endif +#endif diff --git a/aegisub/src/avisynth_wrap.h b/aegisub/src/avisynth_wrap.h index a47240aff..ff3d6ce2d 100644 --- a/aegisub/src/avisynth_wrap.h +++ b/aegisub/src/avisynth_wrap.h @@ -35,14 +35,8 @@ /// #ifdef WITH_AVISYNTH -#ifndef AGI_PRE -#include -#endif -#include "avisynth.h" - -/// DOCME -typedef IScriptEnvironment* __stdcall FUNC(int); +class IScriptEnvironment; /// DOCME /// @class AviSynthWrapper @@ -50,23 +44,11 @@ typedef IScriptEnvironment* __stdcall FUNC(int); /// /// DOCME class AviSynthWrapper { -private: - - /// DOCME - static int avs_refcount; - - /// DOCME - static HINSTANCE hLib; -protected: - - /// DOCME - static IScriptEnvironment *env; + AviSynthWrapper(AviSynthWrapper const&); public: + wxMutex& GetMutex() const; + IScriptEnvironment *GetEnv() const; - /// DOCME - static wxMutex AviSynthMutex; - - IScriptEnvironment *GetEnv(); AviSynthWrapper(); ~AviSynthWrapper(); }; diff --git a/aegisub/src/video_provider_avs.cpp b/aegisub/src/video_provider_avs.cpp index b3eaf67dc..7e9491d40 100644 --- a/aegisub/src/video_provider_avs.cpp +++ b/aegisub/src/video_provider_avs.cpp @@ -59,7 +59,7 @@ AvisynthVideoProvider::AvisynthVideoProvider(wxString filename) try iframe.flipped = true; iframe.invertChannels = true; - wxMutexLocker lock(AviSynthMutex); + wxMutexLocker lock(avs.GetMutex()); wxFileName fname(filename); if (!fname.FileExists()) @@ -151,7 +151,7 @@ file_exit: if (!script.IsClip() || !script.AsClip()->GetVideoInfo().HasVideo()) throw VideoNotSupported("No usable video found"); - RGB32Video = (env->Invoke("Cache", env->Invoke("ConvertToRGB32", script))).AsClip(); + RGB32Video = (avs.GetEnv()->Invoke("Cache", avs.GetEnv()->Invoke("ConvertToRGB32", script))).AsClip(); vi = RGB32Video->GetVideoInfo(); fps = (double)vi.fps_numerator / vi.fps_denominator; } @@ -164,6 +164,7 @@ AvisynthVideoProvider::~AvisynthVideoProvider() { } AVSValue AvisynthVideoProvider::Open(wxFileName const& fname, wxString const& extension) { + IScriptEnvironment *env = avs.GetEnv(); char *videoFilename = env->SaveString(fname.GetShortPath().mb_str(csConvLocal)); // Avisynth file, just import it @@ -265,9 +266,9 @@ AVSValue AvisynthVideoProvider::Open(wxFileName const& fname, wxString const& ex const AegiVideoFrame AvisynthVideoProvider::GetFrame(int n) { if (n == last_fnum) return iframe; - wxMutexLocker lock(AviSynthMutex); + wxMutexLocker lock(avs.GetMutex()); - PVideoFrame frame = RGB32Video->GetFrame(n,env); + PVideoFrame frame = RGB32Video->GetFrame(n, avs.GetEnv()); iframe.pitch = frame->GetPitch(); iframe.w = frame->GetRowSize() / (vi.BitsPerPixel() / 8); iframe.h = frame->GetHeight(); diff --git a/aegisub/src/video_provider_avs.h b/aegisub/src/video_provider_avs.h index 3fb739250..04abb35d7 100644 --- a/aegisub/src/video_provider_avs.h +++ b/aegisub/src/video_provider_avs.h @@ -35,6 +35,7 @@ /// #ifdef WITH_AVISYNTH +#include "avisynth.h" #include "avisynth_wrap.h" #include "include/aegisub/video_provider.h" @@ -43,7 +44,8 @@ /// @brief DOCME /// /// DOCME -class AvisynthVideoProvider: public VideoProvider, AviSynthWrapper { +class AvisynthVideoProvider: public VideoProvider { + AviSynthWrapper avs; AegiVideoFrame iframe; wxString decoderName; agi::vfr::Framerate fps;