diff --git a/ProxyEdit.vcxproj b/ProxyEdit.vcxproj index 65e5622..54c208d 100644 --- a/ProxyEdit.vcxproj +++ b/ProxyEdit.vcxproj @@ -103,6 +103,7 @@ + @@ -117,6 +118,7 @@ + diff --git a/ProxyEdit.vcxproj.filters b/ProxyEdit.vcxproj.filters index dc21eec..771d358 100644 --- a/ProxyEdit.vcxproj.filters +++ b/ProxyEdit.vcxproj.filters @@ -52,6 +52,9 @@ ソース ファイル + + ソース ファイル + ソース ファイル @@ -87,6 +90,9 @@ ヘッダー ファイル + + ヘッダー ファイル + ヘッダー ファイル diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 5f5e556..6a13272 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -5,6 +5,7 @@ #include "MediaDecoder.h" #include "ProxyBuilder.h" #include "ProxyInput.h" +#include "ScanController.h" #include "Settings.h" using namespace pe; @@ -29,10 +30,13 @@ EXTERN_C __declspec(dllexport) void RegisterPlugin(HOST_APP_TABLE* host) { LoadSettings(); StartMediaFoundation(); host->register_input_plugin(ProxyInputTable()); + RegisterScanMenus(host); + StartScanController(); StartBuilder(); } EXTERN_C __declspec(dllexport) void UninitializePlugin() { + StopScanController(); StopBuilder(); ShutdownProxyInput(); StopMediaFoundation(); diff --git a/src/ScanController.cpp b/src/ScanController.cpp new file mode 100644 index 0000000..0b94d04 --- /dev/null +++ b/src/ScanController.cpp @@ -0,0 +1,250 @@ +#include "ScanController.h" + +#include +#include +#include +#include + +#include "HostContext.h" +#include "Log.h" +#include "ProxyBuilder.h" +#include "ProxyFormat.h" +#include "ProxyStore.h" +#include "Settings.h" + +namespace pe { + +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::string ToUtf8(const std::wstring& text) { + if (text.empty()) return std::string(); + int length = WideCharToMultiByte(CP_UTF8, 0, text.c_str(), (int)text.size(), nullptr, 0, nullptr, + nullptr); + std::string result((size_t)length, '\0'); + WideCharToMultiByte(CP_UTF8, 0, text.c_str(), (int)text.size(), result.data(), length, nullptr, + nullptr); + return result; +} + +std::wstring FromUtf8(const char* text) { + if (!text) return std::wstring(); + int length = MultiByteToWideChar(CP_UTF8, 0, text, -1, nullptr, 0); + if (length <= 1) return std::wstring(); + std::wstring result((size_t)(length - 1), L'\0'); + MultiByteToWideChar(CP_UTF8, 0, text, -1, result.data(), length); + return result; +} + +struct Collected { + 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++) { + int frame = 0; + for (int guard = 0; guard < 20000; guard++) { + OBJECT_HANDLE object = edit->find_object(layer, frame); + if (!object) break; + OBJECT_LAYER_FRAME position = edit->get_object_layer_frame(object); + if (edit->count_object_effect(object, kEffect) > 0) { + LPCSTR value = edit->get_object_item_value(object, kEffect, kItem); + std::wstring path = FromUtf8(value); + if (!path.empty()) { + collected->objects.push_back(object); + collected->files.push_back(path); + } + } + if (position.end < frame) break; + frame = position.end + 1; + } + } +} + +struct SwapRequest { + std::vector objects; + std::vector files; + int applied = 0; +}; + +void ApplySwap(void* param, EDIT_SECTION* edit) { + SwapRequest* request = (SwapRequest*)param; + for (size_t index = 0; index < request->objects.size(); index++) { + std::string encoded = ToUtf8(request->files[index]); + if (edit->set_object_item_value(request->objects[index], kEffect, kItem, encoded.c_str())) { + request->applied++; + } + } + if (request->applied > 0) edit->set_edited_state(); +} + +struct MediaQuery { + std::wstring path; + MEDIA_INFO info{}; + bool ok = false; +}; + +void QueryMedia(void* param, EDIT_SECTION* edit) { + MediaQuery* query = (MediaQuery*)param; + query->ok = edit->get_media_info(query->path.c_str(), &query->info, sizeof(query->info)); +} + +bool Eligible(const std::wstring& path, const MEDIA_INFO& info) { + const Settings& settings = CurrentSettings(); + if (info.video_track_num <= 0) return false; + if (info.width >= settings.target_min_width) return true; + if (settings.target_min_mbps > 0 && info.total_time > 0.5) { + SourceKey key; + if (QuerySource(path, key)) { + double mbps = (double)key.size * 8.0 / info.total_time / 1000000.0; + if (mbps >= settings.target_min_mbps) return true; + } + } + return false; +} + +LRESULT CALLBACK MessageProc(HWND window, UINT message, WPARAM first, LPARAM second) { + if (message == kAutomaticScan) { + if (CurrentSettings().enabled) ApplyProxies(); + return 0; + } + return DefWindowProcW(window, message, first, second); +} + +void OnEditMenuApply(EDIT_SECTION*) { + ScanResult result = ApplyProxies(); + Say(L"プロキシへ差し替えました: 対象 %d 件、差し替え %d 件", result.eligible, result.swapped); +} + +void OnEditMenuRestore(EDIT_SECTION*) { + ScanResult result = RestoreOriginals(); + Say(L"元素材へ戻しました: %d 件", result.restored); +} + +} + +bool IsProxyPath(const std::wstring& path) { + if (path.size() < 4) return false; + return _wcsicmp(path.c_str() + path.size() - 4, L".pxy") == 0; +} + +std::wstring SourceOfProxy(const std::wstring& path) { + ProxyReader reader; + if (!reader.Open(path)) return std::wstring(); + std::wstring source = reader.Header().source_path; + reader.Close(); + return source; +} + +ScanResult ApplyProxies() { + ScanResult result; + if (g_scanning.exchange(true)) return result; + Collected collected; + CallReadSection(&collected, CollectObjects); + + SwapRequest request; + std::map resolved; + for (size_t index = 0; index < collected.objects.size(); index++) { + const std::wstring& path = collected.files[index]; + result.examined++; + if (IsProxyPath(path)) continue; + + auto cached = resolved.find(path); + if (cached == resolved.end()) { + MediaQuery query; + query.path = path; + CallReadSection(&query, QueryMedia); + if (!query.ok || !Eligible(path, query.info)) { + resolved[path] = std::wstring(); + result.rejected++; + continue; + } + std::wstring proxy; + if (!RegisterSource(path, proxy)) { + resolved[path] = std::wstring(); + result.rejected++; + continue; + } + resolved[path] = proxy; + cached = resolved.find(path); + } + if (cached->second.empty()) continue; + result.eligible++; + request.objects.push_back(collected.objects[index]); + request.files.push_back(cached->second); + } + + if (!request.objects.empty()) { + CallEditSection(&request, ApplySwap); + result.swapped = request.applied; + } + g_scanning.store(false); + return result; +} + +ScanResult RestoreOriginals() { + ScanResult result; + Collected collected; + CallReadSection(&collected, CollectObjects); + + SwapRequest request; + std::map resolved; + for (size_t index = 0; index < collected.objects.size(); index++) { + const std::wstring& path = collected.files[index]; + if (!IsProxyPath(path)) continue; + auto cached = resolved.find(path); + if (cached == resolved.end()) { + resolved[path] = SourceOfProxy(path); + cached = resolved.find(path); + } + if (cached->second.empty()) continue; + request.objects.push_back(collected.objects[index]); + request.files.push_back(cached->second); + } + if (!request.objects.empty()) { + CallEditSection(&request, ApplySwap); + result.restored = request.applied; + } + return result; +} + +void RequestAutomaticScan() { + if (g_message_window) PostMessageW(g_message_window, kAutomaticScan, 0, 0); +} + +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); +} + +void StopScanController() { + if (g_message_window) { + DestroyWindow(g_message_window); + g_message_window = nullptr; + } +} + +void RegisterScanMenus(HOST_APP_TABLE* host) { + host->register_edit_menu(L"プロキシへ差し替え", OnEditMenuApply); + host->register_edit_menu(L"プロキシから元素材へ戻す", OnEditMenuRestore); +} + +} diff --git a/src/ScanController.h b/src/ScanController.h new file mode 100644 index 0000000..a89b672 --- /dev/null +++ b/src/ScanController.h @@ -0,0 +1,29 @@ +#pragma once + +#include + +#include +#include + +#include "plugin2.h" + +namespace pe { + +struct ScanResult { + int examined = 0; + int eligible = 0; + int swapped = 0; + int restored = 0; + int rejected = 0; +}; + +void RegisterScanMenus(HOST_APP_TABLE* host); +ScanResult ApplyProxies(); +ScanResult RestoreOriginals(); +bool IsProxyPath(const std::wstring& path); +std::wstring SourceOfProxy(const std::wstring& path); +void RequestAutomaticScan(); +void StartScanController(); +void StopScanController(); + +}