diff --git a/ProxyEdit.vcxproj b/ProxyEdit.vcxproj index 234019d..85c054e 100644 --- a/ProxyEdit.vcxproj +++ b/ProxyEdit.vcxproj @@ -100,6 +100,7 @@ + @@ -112,6 +113,7 @@ + diff --git a/ProxyEdit.vcxproj.filters b/ProxyEdit.vcxproj.filters index bf6c66e..5681d8c 100644 --- a/ProxyEdit.vcxproj.filters +++ b/ProxyEdit.vcxproj.filters @@ -43,6 +43,9 @@ ソース ファイル + + ソース ファイル + ソース ファイル @@ -72,6 +75,9 @@ ヘッダー ファイル + + ヘッダー ファイル + ヘッダー ファイル diff --git a/src/Plugin.cpp b/src/Plugin.cpp index 8caa9d3..5f5e556 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -2,6 +2,10 @@ #include "HostContext.h" #include "Log.h" +#include "MediaDecoder.h" +#include "ProxyBuilder.h" +#include "ProxyInput.h" +#include "Settings.h" using namespace pe; @@ -22,9 +26,16 @@ EXTERN_C __declspec(dllexport) bool InitializePlugin(DWORD) { return true; } EXTERN_C __declspec(dllexport) void RegisterPlugin(HOST_APP_TABLE* host) { SetEditHandle(host->create_edit_handle()); SetHostWindow(Edit()->get_host_app_window()); + LoadSettings(); + StartMediaFoundation(); + host->register_input_plugin(ProxyInputTable()); + StartBuilder(); } EXTERN_C __declspec(dllexport) void UninitializePlugin() { + StopBuilder(); + ShutdownProxyInput(); + StopMediaFoundation(); } BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID) { diff --git a/src/ProxyBuilder.cpp b/src/ProxyBuilder.cpp index fab15a9..5f1bc6f 100644 --- a/src/ProxyBuilder.cpp +++ b/src/ProxyBuilder.cpp @@ -280,6 +280,7 @@ bool RegisterSource(const std::wstring& source, std::wstring& proxy_path) { header.chunk_frames = settings.chunk_frames; header.source_size = key.size; header.source_time = key.time; + wcsncpy_s(header.source_path, key.path.c_str(), _TRUNCATE); probe.Close(); EnsureStoreDirectory(); @@ -287,6 +288,7 @@ bool RegisterSource(const std::wstring& source, std::wstring& proxy_path) { if (job->writer.OpenExisting(job->proxy)) { const ProxyHeader& existing = job->writer.Header(); if (SameSource(key, existing.source_size, existing.source_time) && + _wcsicmp(existing.source_path, key.path.c_str()) == 0 && existing.proxy_width == header.proxy_width && existing.proxy_height == header.proxy_height && existing.frame_count == header.frame_count && existing.quality == header.quality && existing.chunk_frames == header.chunk_frames) { diff --git a/src/ProxyFormat.h b/src/ProxyFormat.h index 372857f..f2f3e48 100644 --- a/src/ProxyFormat.h +++ b/src/ProxyFormat.h @@ -26,6 +26,7 @@ struct ProxyHeader { int64_t source_time; int64_t data_offset; int32_t reserved[14]; + wchar_t source_path[1024]; }; struct ProxyIndexEntry { diff --git a/src/ProxyInput.cpp b/src/ProxyInput.cpp new file mode 100644 index 0000000..344a613 --- /dev/null +++ b/src/ProxyInput.cpp @@ -0,0 +1,259 @@ +#include "ProxyInput.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ColorConvert.h" +#include "HostContext.h" +#include "JpegCodec.h" +#include "Log.h" +#include "MediaDecoder.h" +#include "ProxyBuilder.h" +#include "ProxyFormat.h" +#include "ProxyStore.h" +#include "Settings.h" +#include "input2.h" + +namespace pe { + +namespace { + +struct Frame { + std::vector data; +}; + +using FramePointer = std::shared_ptr; + +struct Session { + ProxyReader reader; + ProxyHeader header{}; + BITMAPINFOHEADER format{}; + size_t bytes = 0; + int stride = 0; + + MediaDecoder original; + std::mutex original_lock; + std::atomic original_ready{false}; + std::atomic original_failed{false}; + + std::mutex cache_lock; + std::condition_variable wake; + std::map cache; + std::set inflight; + std::deque queue; + std::vector workers; + bool stop = false; + int ahead = 0; + int last_frame = -100000; + int sequential = 0; +}; + +std::atomic g_sessions{0}; + +bool DecodeProxyFrame(Session* session, int frame, unsigned char* destination) { + std::vector encoded; + if (!session->reader.ReadFrame(frame, encoded)) return false; + return DecodeJpegToYuy2(encoded.data(), encoded.size(), session->header.proxy_width, + session->header.proxy_height, destination, session->header.source_width, + session->header.source_height, session->stride); +} + +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)) { + session->original_failed.store(true); + Warn(L"元素材を読み込めませんでした: %s", session->header.source_path); + return false; + } + session->original_ready.store(true); + } + return session->original.ReadFrameYuy2(frame, destination, session->stride, + session->header.source_height); +} + +bool ProduceFrame(Session* session, int frame, unsigned char* destination) { + if (Exporting()) { + if (DecodeOriginalFrame(session, frame, destination)) return true; + if (DecodeProxyFrame(session, frame, destination)) return true; + FillBlackYuy2(destination, session->header.source_width, session->header.source_height, + session->stride); + return true; + } + if (DecodeProxyFrame(session, frame, destination)) return true; + if (DecodeOriginalFrame(session, frame, destination)) return true; + FillBlackYuy2(destination, session->header.source_width, session->header.source_height, + session->stride); + return true; +} + +void SessionWorker(Session* session) { + for (;;) { + int frame = -1; + { + std::unique_lock lock(session->cache_lock); + session->wake.wait(lock, [session] { return session->stop || !session->queue.empty(); }); + if (session->stop) return; + frame = session->queue.front(); + session->queue.pop_front(); + if (session->cache.count(frame) || session->inflight.count(frame)) continue; + session->inflight.insert(frame); + } + FramePointer produced = std::make_shared(); + produced->data.resize(session->bytes); + ProduceFrame(session, frame, produced->data.data()); + { + std::lock_guard lock(session->cache_lock); + session->inflight.erase(frame); + session->cache[frame] = produced; + } + } +} + +INPUT_HANDLE OnOpen(LPCWSTR file) { + ProxyReader probe; + if (!probe.Open(file)) return nullptr; + Session* session = new Session(); + session->header = probe.Header(); + probe.Close(); + if (!session->reader.Open(file)) { + delete session; + return nullptr; + } + session->stride = session->header.source_width * 2; + session->bytes = (size_t)session->stride * session->header.source_height; + session->ahead = ReadAheadFrames(session->header.source_width, session->header.source_height); + int count = std::clamp(EffectiveWorkerCount() / 2, 1, 3); + for (int index = 0; index < count; index++) { + session->workers.emplace_back(SessionWorker, session); + } + g_sessions.fetch_add(1); + return (INPUT_HANDLE)session; +} + +bool OnClose(INPUT_HANDLE handle) { + Session* session = (Session*)handle; + if (!session) return true; + { + std::lock_guard lock(session->cache_lock); + session->stop = true; + } + session->wake.notify_all(); + for (std::thread& worker : session->workers) { + if (worker.joinable()) worker.join(); + } + session->reader.Close(); + session->original.Close(); + delete session; + g_sessions.fetch_sub(1); + return true; +} + +bool OnInfoGet(INPUT_HANDLE handle, INPUT_INFO* info) { + Session* session = (Session*)handle; + if (!session || !info) return false; + session->format.biSize = sizeof(BITMAPINFOHEADER); + session->format.biWidth = session->header.source_width; + session->format.biHeight = session->header.source_height; + session->format.biPlanes = 1; + session->format.biBitCount = 16; + session->format.biCompression = MAKEFOURCC('Y', 'U', 'Y', '2'); + session->format.biSizeImage = (DWORD)session->bytes; + info->flag = INPUT_INFO::FLAG_VIDEO; + info->rate = session->header.rate; + info->scale = session->header.scale; + info->n = session->header.frame_count; + info->format = &session->format; + info->format_size = sizeof(BITMAPINFOHEADER); + info->audio_n = 0; + info->audio_format = nullptr; + info->audio_format_size = 0; + return true; +} + +int OnReadVideo(INPUT_HANDLE handle, int frame, void* buffer) { + Session* session = (Session*)handle; + if (!session || !buffer) return 0; + if (frame < 0) frame = 0; + if (frame >= session->header.frame_count) frame = session->header.frame_count - 1; + + FocusSource(session->header.source_path, frame); + + FramePointer hit; + { + std::lock_guard lock(session->cache_lock); + if (frame == session->last_frame + 1) { + if (session->sequential < 1000) session->sequential++; + } else if (frame != session->last_frame) { + session->sequential = 0; + } + session->last_frame = frame; + + auto found = session->cache.find(frame); + if (found != session->cache.end()) hit = found->second; + + for (auto entry = session->cache.begin(); entry != session->cache.end();) { + if (entry->first < frame - 1 || entry->first > frame + session->ahead + 1) { + entry = session->cache.erase(entry); + } else { + ++entry; + } + } + session->queue.clear(); + if (session->sequential >= 2 && !Exporting()) { + for (int step = 1; step <= session->ahead; step++) { + int next = frame + step; + if (next >= session->header.frame_count) break; + if (session->cache.count(next) || session->inflight.count(next)) continue; + session->queue.push_back(next); + } + } + } + session->wake.notify_all(); + + if (hit) { + memcpy(buffer, hit->data.data(), session->bytes); + return (int)session->bytes; + } + ProduceFrame(session, frame, (unsigned char*)buffer); + return (int)session->bytes; +} + +int OnReadAudio(INPUT_HANDLE, int, int, void*) { + return 0; +} + +INPUT_PLUGIN_TABLE g_table = { + INPUT_PLUGIN_TABLE::FLAG_VIDEO | INPUT_PLUGIN_TABLE::FLAG_CONCURRENT, + L"プロキシ編集", + L"プロキシ編集 (*.pxy)\0*.pxy\0", + L"プロキシ編集 version 1.0.0", + OnOpen, + OnClose, + OnInfoGet, + OnReadVideo, + OnReadAudio, + nullptr, + nullptr, + nullptr, +}; + +} + +INPUT_PLUGIN_TABLE* ProxyInputTable() { + return &g_table; +} + +void ShutdownProxyInput() { +} + +} diff --git a/src/ProxyInput.h b/src/ProxyInput.h new file mode 100644 index 0000000..c3c8809 --- /dev/null +++ b/src/ProxyInput.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +#include "plugin2.h" + +namespace pe { + +INPUT_PLUGIN_TABLE* ProxyInputTable(); +void ShutdownProxyInput(); + +}