From dca152181d7697dc16843a1da26feb2b5102960f Mon Sep 17 00:00:00 2001 From: routersys Date: Wed, 9 Sep 2026 14:55:58 +0900 Subject: [PATCH] =?UTF-8?q?=E8=B5=B0=E6=9F=BB=E3=82=92=E5=B0=82=E7=94=A8?= =?UTF-8?q?=E3=82=B9=E3=83=AC=E3=83=83=E3=83=89=E3=81=A7=E8=A1=8C=E3=81=86?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProxyEdit.vcxproj | 2 +- src/MediaDecoder.cpp | 33 ++------------------ src/MediaDecoder.h | 7 +---- src/ProxyBuilder.cpp | 32 +++++++++++--------- src/ProxyInput.cpp | 2 +- src/ScanController.cpp | 69 ++++++++++++++++++++++++++---------------- src/Settings.cpp | 4 +-- src/Settings.h | 2 +- 8 files changed, 69 insertions(+), 82 deletions(-) diff --git a/ProxyEdit.vcxproj b/ProxyEdit.vcxproj index 6509b62..cd939e1 100644 --- a/ProxyEdit.vcxproj +++ b/ProxyEdit.vcxproj @@ -67,7 +67,7 @@ Windows - user32.lib;gdi32.lib;ole32.lib;shlwapi.lib;windowscodecs.lib;mfplat.lib;mfreadwrite.lib;mfuuid.lib;d3d11.lib;%(AdditionalDependencies) + user32.lib;gdi32.lib;ole32.lib;shlwapi.lib;windowscodecs.lib;mfplat.lib;mfreadwrite.lib;mfuuid.lib;%(AdditionalDependencies) true diff --git a/src/MediaDecoder.cpp b/src/MediaDecoder.cpp index 6433f34..a0e0c25 100644 --- a/src/MediaDecoder.cpp +++ b/src/MediaDecoder.cpp @@ -1,6 +1,5 @@ #include "MediaDecoder.h" -#include #include #include #include @@ -79,34 +78,14 @@ MediaDecoder::~MediaDecoder() { Close(); } -bool MediaDecoder::Open(const std::wstring& path, bool hardware) { +bool MediaDecoder::Open(const std::wstring& path) { Close(); if (!StartMediaFoundation()) return false; std::lock_guard lock(mutex_); - if (hardware) { - UINT flags = D3D11_CREATE_DEVICE_VIDEO_SUPPORT | D3D11_CREATE_DEVICE_BGRA_SUPPORT; - if (SUCCEEDED(D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, flags, nullptr, 0, - D3D11_SDK_VERSION, &device_, nullptr, nullptr)) && - device_) { - ID3D10Multithread* threading = nullptr; - if (SUCCEEDED(device_->QueryInterface(IID_PPV_ARGS(&threading)))) { - threading->SetMultithreadProtected(TRUE); - threading->Release(); - } - if (FAILED(MFCreateDXGIDeviceManager(&token_, &manager_)) || - FAILED(manager_->ResetDevice(device_, token_))) { - if (manager_) { - manager_->Release(); - manager_ = nullptr; - } - } - } - } IMFAttributes* attributes = nullptr; if (FAILED(MFCreateAttributes(&attributes, 4))) return false; attributes->SetUINT32(MF_SOURCE_READER_ENABLE_ADVANCED_VIDEO_PROCESSING, TRUE); - attributes->SetUINT32(MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, hardware ? TRUE : FALSE); - if (manager_) attributes->SetUnknown(MF_SOURCE_READER_D3D_MANAGER, manager_); + attributes->SetUINT32(MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, FALSE); HRESULT result = MFCreateSourceReaderFromURL(path.c_str(), attributes, &reader_); attributes->Release(); if (FAILED(result) || !reader_) return false; @@ -167,14 +146,6 @@ void MediaDecoder::Close() { reader_->Release(); reader_ = nullptr; } - if (manager_) { - manager_->Release(); - manager_ = nullptr; - } - if (device_) { - device_->Release(); - device_ = nullptr; - } next_index_ = -1; } diff --git a/src/MediaDecoder.h b/src/MediaDecoder.h index ffa6827..39406b3 100644 --- a/src/MediaDecoder.h +++ b/src/MediaDecoder.h @@ -8,8 +8,6 @@ #include struct IMFSourceReader; -struct IMFDXGIDeviceManager; -struct ID3D11Device; namespace pe { @@ -19,7 +17,7 @@ void StopMediaFoundation(); class MediaDecoder { public: ~MediaDecoder(); - bool Open(const std::wstring& path, bool hardware); + bool Open(const std::wstring& path); void Close(); bool IsOpen() const; @@ -37,9 +35,6 @@ private: long long FrameToTime(long long index) const; IMFSourceReader* reader_ = nullptr; - IMFDXGIDeviceManager* manager_ = nullptr; - ID3D11Device* device_ = nullptr; - unsigned token_ = 0; int width_ = 0; int height_ = 0; int rate_ = 0; diff --git a/src/ProxyBuilder.cpp b/src/ProxyBuilder.cpp index 5f1bc6f..939a865 100644 --- a/src/ProxyBuilder.cpp +++ b/src/ProxyBuilder.cpp @@ -110,18 +110,28 @@ void MarkChunk(const JobPointer& job, int chunk, unsigned char state) { bool OpenDecoder(const JobPointer& job) { if (job->opened.load()) return true; - if (!job->decoder.Open(job->key.path, true)) { - if (!job->decoder.Open(job->key.path, false)) { - job->failed.store(true); + if (!job->decoder.Open(job->key.path)) { + job->failed.store(true); + { std::lock_guard lock(job->state_lock); job->message = L"この形式は Media Foundation で読み込めません"; - return false; } + Warn(L"元素材を開けませんでした: %s", job->key.path.c_str()); + return false; } job->opened.store(true); return true; } +void FailJob(const JobPointer& job, const wchar_t* reason) { + job->failed.store(true); + { + std::lock_guard lock(job->state_lock); + job->message = reason; + } + Warn(L"生成を中止しました: %s (%s)", reason, job->key.path.c_str()); +} + void ProcessChunk(const JobPointer& job, int chunk) { const ProxyHeader& header = job->header; const int begin = chunk * header.chunk_frames; @@ -136,24 +146,18 @@ void ProcessChunk(const JobPointer& job, int chunk) { for (int frame = begin; frame < end && !g_stop.load(); frame++) { if (job->writer.HasFrame(frame)) continue; if (!job->decoder.ReadFrameYuy2(frame, picture.data(), stride, header.source_height)) { - job->failed.store(true); - std::lock_guard lock(job->state_lock); - job->message = L"復号できないフレームがありました"; + FailJob(job, L"復号できないフレームがありました"); break; } Yuy2ToBgrScaled(picture.data(), header.source_width, header.source_height, stride, reduced.data(), header.proxy_width, header.proxy_height); if (!EncodeJpeg(reduced.data(), header.proxy_width, header.proxy_height, header.proxy_width, header.proxy_height, header.quality, encoded)) { - job->failed.store(true); - std::lock_guard lock(job->state_lock); - job->message = L"プロキシの符号化に失敗しました"; + FailJob(job, L"プロキシの符号化に失敗しました"); break; } if (!job->writer.WriteFrame(frame, encoded.data(), (int)encoded.size())) { - job->failed.store(true); - std::lock_guard lock(job->state_lock); - job->message = L"プロキシを書き込めませんでした"; + FailJob(job, L"プロキシを書き込めませんでした"); break; } produced++; @@ -260,7 +264,7 @@ bool RegisterSource(const std::wstring& source, std::wstring& proxy_path) { job->proxy = ProxyPathFor(key); MediaDecoder probe; - if (!probe.Open(source, true) && !probe.Open(source, false)) return false; + if (!probe.Open(source)) return false; const Settings& settings = CurrentSettings(); int proxy_width = probe.Width() * settings.scale_percent / 100; int proxy_height = probe.Height() * settings.scale_percent / 100; diff --git a/src/ProxyInput.cpp b/src/ProxyInput.cpp index 9525468..c018998 100644 --- a/src/ProxyInput.cpp +++ b/src/ProxyInput.cpp @@ -75,7 +75,7 @@ bool DecodeOriginalFrame(Session* session, int frame, unsigned char* destination if (session->original_failed.load()) return false; std::lock_guard lock(session->original_lock); if (!session->original_ready.load()) { - if (!session->original.Open(session->header.source_path, false)) { + if (!session->original.Open(session->header.source_path)) { session->original_failed.store(true); Warn(L"元素材を読み込めませんでした: %s", session->header.source_path); return false; diff --git a/src/ScanController.cpp b/src/ScanController.cpp index 0b94d04..ebc83e2 100644 --- a/src/ScanController.cpp +++ b/src/ScanController.cpp @@ -2,8 +2,12 @@ #include #include +#include #include +#include +#include #include +#include #include "HostContext.h" #include "Log.h" @@ -18,10 +22,13 @@ namespace { const wchar_t* kEffect = L"動画ファイル"; const wchar_t* kItem = L"ファイル"; -const UINT kAutomaticScan = WM_APP + 71; - -HWND g_message_window = nullptr; std::atomic g_scanning{false}; +std::atomic g_last_scan{0}; +std::atomic g_requested{false}; +std::atomic g_stop{false}; +std::thread g_worker; +std::mutex g_wake_lock; +std::condition_variable g_wake; std::string ToUtf8(const std::wstring& text) { if (text.empty()) return std::string(); @@ -43,16 +50,14 @@ std::wstring FromUtf8(const char* text) { } struct Collected { + int layers = 1; std::vector objects; std::vector files; }; void CollectObjects(void* param, EDIT_SECTION* edit) { Collected* collected = (Collected*)param; - EDIT_INFO info{}; - if (edit->info) info = *edit->info; - const int layers = std::max(info.layer_max + 1, 1); - for (int layer = 0; layer < layers; layer++) { + for (int layer = 0; layer < collected->layers; layer++) { int frame = 0; for (int guard = 0; guard < 20000; guard++) { OBJECT_HANDLE object = edit->find_object(layer, frame); @@ -114,12 +119,25 @@ bool Eligible(const std::wstring& path, const MEDIA_INFO& info) { return false; } -LRESULT CALLBACK MessageProc(HWND window, UINT message, WPARAM first, LPARAM second) { - if (message == kAutomaticScan) { - if (CurrentSettings().enabled) ApplyProxies(); - return 0; +void ScanWorker() { + while (!g_stop.load()) { + { + std::unique_lock lock(g_wake_lock); + g_wake.wait_for(lock, std::chrono::milliseconds(400)); + } + if (g_stop.load()) return; + if (!g_requested.exchange(false)) continue; + if (!CurrentSettings().enabled) continue; + ScanResult result = ApplyProxies(); + g_last_scan.store(GetTickCount64()); + if (result.swapped > 0) { + Say(L"自動でプロキシへ差し替えました: %d 件", result.swapped); + } } - return DefWindowProcW(window, message, first, second); +} + +void OnHostEvent(void*) { + RequestAutomaticScan(); } void OnEditMenuApply(EDIT_SECTION*) { @@ -151,6 +169,7 @@ ScanResult ApplyProxies() { ScanResult result; if (g_scanning.exchange(true)) return result; Collected collected; + collected.layers = std::max(EditInfo().layer_max + 1, 1); CallReadSection(&collected, CollectObjects); SwapRequest request; @@ -196,6 +215,7 @@ ScanResult ApplyProxies() { ScanResult RestoreOriginals() { ScanResult result; Collected collected; + collected.layers = std::max(EditInfo().layer_max + 1, 1); CallReadSection(&collected, CollectObjects); SwapRequest request; @@ -220,31 +240,28 @@ ScanResult RestoreOriginals() { } void RequestAutomaticScan() { - if (g_message_window) PostMessageW(g_message_window, kAutomaticScan, 0, 0); + if (GetTickCount64() - g_last_scan.load() < 1500) return; + g_requested.store(true); + g_wake.notify_all(); } void StartScanController() { - if (g_message_window) return; - WNDCLASSEXW description{}; - description.cbSize = sizeof(description); - description.lpfnWndProc = MessageProc; - description.hInstance = ModuleInstance(); - description.lpszClassName = L"ProxyEditScanWindow"; - RegisterClassExW(&description); - g_message_window = CreateWindowExW(0, L"ProxyEditScanWindow", L"", 0, 0, 0, 0, 0, HWND_MESSAGE, - nullptr, description.hInstance, nullptr); + if (g_worker.joinable()) return; + g_stop.store(false); + g_worker = std::thread(ScanWorker); } void StopScanController() { - if (g_message_window) { - DestroyWindow(g_message_window); - g_message_window = nullptr; - } + g_stop.store(true); + g_wake.notify_all(); + if (g_worker.joinable()) g_worker.join(); } void RegisterScanMenus(HOST_APP_TABLE* host) { host->register_edit_menu(L"プロキシへ差し替え", OnEditMenuApply); host->register_edit_menu(L"プロキシから元素材へ戻す", OnEditMenuRestore); + host->register_event_listener(EVENT_TYPE::UPDATE_OBJECT, nullptr, OnHostEvent); + host->register_event_listener(EVENT_TYPE::CHANGE_EDIT_SCENE, nullptr, OnHostEvent); } } diff --git a/src/Settings.cpp b/src/Settings.cpp index 2103534..a486bf8 100644 --- a/src/Settings.cpp +++ b/src/Settings.cpp @@ -126,7 +126,7 @@ int EffectiveWorkerCount() { unsigned hardware = std::thread::hardware_concurrency(); if (hardware == 0) hardware = 4; int count = (int)(hardware / 2); - return std::clamp(count, 1, 8); + return std::clamp(count, 1, 4); } int ReadAheadFrames(int width, int height) { @@ -134,7 +134,7 @@ int ReadAheadFrames(int width, int height) { long long bytes = (long long)width * height * 2; if (bytes <= 0) return 4; int frames = (int)(CurrentSettings().read_ahead_bytes / bytes); - return std::clamp(frames, 2, 24); + return std::clamp(frames, 2, 8); } } diff --git a/src/Settings.h b/src/Settings.h index 578800e..1e8ed9b 100644 --- a/src/Settings.h +++ b/src/Settings.h @@ -14,7 +14,7 @@ struct Settings { int scale_percent = 50; int quality = 80; int chunk_frames = 240; - int read_ahead_bytes = 96 * 1024 * 1024; + int read_ahead_bytes = 48 * 1024 * 1024; int worker_count = 0; long long capacity_bytes = 20LL * 1024 * 1024 * 1024; std::wstring store_path;