走査を専用スレッドで行うようにした

このコミットが含まれているのは:
routersys committed 2026-09-09 14:55:58 +09:00
1 親 f10c747423
コミット dca152181d
8ファイル変更
+69 -82

検索条件に一致するファイルはありません

+1 -1
ファイルの表示
@@ -67,7 +67,7 @@
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<AdditionalDependencies>user32.lib;gdi32.lib;ole32.lib;shlwapi.lib;windowscodecs.lib;mfplat.lib;mfreadwrite.lib;mfuuid.lib;d3d11.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>user32.lib;gdi32.lib;ole32.lib;shlwapi.lib;windowscodecs.lib;mfplat.lib;mfreadwrite.lib;mfuuid.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
+2 -31
ファイルの表示
@@ -1,6 +1,5 @@
#include "MediaDecoder.h"
#include <d3d11.h>
#include <mfapi.h>
#include <mferror.h>
#include <mfidl.h>
@@ -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<std::mutex> 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;
}
+1 -6
ファイルの表示
@@ -8,8 +8,6 @@
#include <vector>
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;
+18 -14
ファイルの表示
@@ -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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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;
+1 -1
ファイルの表示
@@ -75,7 +75,7 @@ bool DecodeOriginalFrame(Session* session, int frame, unsigned char* destination
if (session->original_failed.load()) return false;
std::lock_guard<std::mutex> 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;
+43 -26
ファイルの表示
@@ -2,8 +2,12 @@
#include <algorithm>
#include <atomic>
#include <condition_variable>
#include <map>
#include <mutex>
#include <chrono>
#include <string>
#include <thread>
#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<bool> g_scanning{false};
std::atomic<unsigned long long> g_last_scan{0};
std::atomic<bool> g_requested{false};
std::atomic<bool> 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<OBJECT_HANDLE> objects;
std::vector<std::wstring> 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<std::mutex> 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);
}
}
+2 -2
ファイルの表示
@@ -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);
}
}
+1 -1
ファイルの表示
@@ -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;