2024-12-01 09:55:56 +00:00
|
|
|
#include "processor_factory.h"
|
|
|
|
|
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "filter_libplacebo.h"
|
2024-12-21 02:58:19 +00:00
|
|
|
#include "filter_realcugan.h"
|
2024-12-01 09:55:56 +00:00
|
|
|
#include "filter_realesrgan.h"
|
|
|
|
#include "interpolator_rife.h"
|
2024-12-20 04:46:10 +00:00
|
|
|
#include "logger_manager.h"
|
2024-12-01 09:55:56 +00:00
|
|
|
|
2024-12-17 16:24:51 +00:00
|
|
|
namespace video2x {
|
|
|
|
namespace processors {
|
|
|
|
|
2024-12-01 09:55:56 +00:00
|
|
|
// Access the singleton instance
|
2024-12-31 00:00:00 +00:00
|
|
|
ProcessorFactory& ProcessorFactory::instance() {
|
2024-12-01 09:55:56 +00:00
|
|
|
static ProcessorFactory factory;
|
|
|
|
|
|
|
|
// Ensure default processors are registered only once
|
|
|
|
static bool initialized = false;
|
|
|
|
if (!initialized) {
|
|
|
|
ProcessorFactory::init_default_processors(factory);
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return factory;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a processor type and its creator
|
|
|
|
void ProcessorFactory::register_processor(ProcessorType type, Creator creator) {
|
|
|
|
creators[type] = std::move(creator);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a processor instance
|
|
|
|
std::unique_ptr<Processor> ProcessorFactory::create_processor(
|
2024-12-31 00:00:00 +00:00
|
|
|
const ProcessorConfig& proc_cfg,
|
2024-12-01 09:55:56 +00:00
|
|
|
uint32_t vk_device_index
|
|
|
|
) const {
|
2024-12-02 07:24:30 +00:00
|
|
|
auto it = creators.find(proc_cfg.processor_type);
|
2024-12-01 09:55:56 +00:00
|
|
|
if (it == creators.end()) {
|
2024-12-20 04:46:10 +00:00
|
|
|
logger()->critical(
|
2024-12-02 07:24:30 +00:00
|
|
|
"Processor type not registered: {}", static_cast<int>(proc_cfg.processor_type)
|
2024-12-01 09:55:56 +00:00
|
|
|
);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the corresponding creator function
|
2024-12-02 07:24:30 +00:00
|
|
|
return it->second(proc_cfg, vk_device_index);
|
2024-12-01 09:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize default processors
|
2024-12-31 00:00:00 +00:00
|
|
|
void ProcessorFactory::init_default_processors(ProcessorFactory& factory) {
|
2024-12-01 09:55:56 +00:00
|
|
|
factory.register_processor(
|
2024-12-02 07:24:30 +00:00
|
|
|
ProcessorType::Libplacebo,
|
2024-12-31 00:00:00 +00:00
|
|
|
[](const ProcessorConfig& proc_cfg,
|
2024-12-02 07:24:30 +00:00
|
|
|
uint32_t vk_device_index) -> std::unique_ptr<Processor> {
|
2024-12-31 00:00:00 +00:00
|
|
|
const auto& config = std::get<LibplaceboConfig>(proc_cfg.config);
|
2024-12-02 07:24:30 +00:00
|
|
|
if (config.shader_path.empty()) {
|
2024-12-20 04:46:10 +00:00
|
|
|
logger()->critical("Shader path must be provided for the libplacebo filter");
|
2024-12-01 09:55:56 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2024-12-02 07:24:30 +00:00
|
|
|
if (proc_cfg.width <= 0 || proc_cfg.height <= 0) {
|
2024-12-20 04:46:10 +00:00
|
|
|
logger()->critical(
|
2024-12-01 09:55:56 +00:00
|
|
|
"Output width and height must be provided for the libplacebo filter"
|
|
|
|
);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return std::make_unique<FilterLibplacebo>(
|
|
|
|
vk_device_index,
|
2024-12-02 07:24:30 +00:00
|
|
|
std::filesystem::path(config.shader_path),
|
|
|
|
proc_cfg.width,
|
|
|
|
proc_cfg.height
|
2024-12-01 09:55:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
factory.register_processor(
|
2024-12-02 07:24:30 +00:00
|
|
|
ProcessorType::RealESRGAN,
|
2024-12-31 00:00:00 +00:00
|
|
|
[](const ProcessorConfig& proc_cfg,
|
2024-12-02 07:24:30 +00:00
|
|
|
uint32_t vk_device_index) -> std::unique_ptr<Processor> {
|
2024-12-31 00:00:00 +00:00
|
|
|
const auto& config = std::get<RealESRGANConfig>(proc_cfg.config);
|
2024-12-02 07:24:30 +00:00
|
|
|
if (proc_cfg.scaling_factor <= 0) {
|
2024-12-20 04:46:10 +00:00
|
|
|
logger()->critical("Scaling factor must be provided for the RealESRGAN filter");
|
2024-12-01 09:55:56 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2024-12-02 07:24:30 +00:00
|
|
|
if (config.model_name.empty()) {
|
2024-12-20 04:46:10 +00:00
|
|
|
logger()->critical("Model name must be provided for the RealESRGAN filter");
|
2024-12-01 09:55:56 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return std::make_unique<FilterRealesrgan>(
|
|
|
|
static_cast<int>(vk_device_index),
|
2024-12-02 07:24:30 +00:00
|
|
|
config.tta_mode,
|
|
|
|
proc_cfg.scaling_factor,
|
|
|
|
config.model_name
|
2024-12-01 09:55:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2024-12-21 02:58:19 +00:00
|
|
|
factory.register_processor(
|
|
|
|
ProcessorType::RealCUGAN,
|
2024-12-31 00:00:00 +00:00
|
|
|
[](const ProcessorConfig& proc_cfg,
|
2024-12-21 02:58:19 +00:00
|
|
|
uint32_t vk_device_index) -> std::unique_ptr<Processor> {
|
2024-12-31 00:00:00 +00:00
|
|
|
const auto& config = std::get<RealCUGANConfig>(proc_cfg.config);
|
2024-12-21 02:58:19 +00:00
|
|
|
if (proc_cfg.scaling_factor <= 0) {
|
|
|
|
logger()->critical("Scaling factor must be provided for the RealCUGAN filter");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (config.model_name.empty()) {
|
|
|
|
logger()->critical("Model name must be provided for the RealCUGAN filter");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return std::make_unique<FilterRealcugan>(
|
|
|
|
static_cast<int>(vk_device_index),
|
|
|
|
config.tta_mode,
|
|
|
|
proc_cfg.scaling_factor,
|
|
|
|
proc_cfg.noise_level,
|
|
|
|
config.num_threads,
|
|
|
|
config.syncgap,
|
|
|
|
config.model_name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2024-12-01 09:55:56 +00:00
|
|
|
factory.register_processor(
|
2024-12-02 07:24:30 +00:00
|
|
|
ProcessorType::RIFE,
|
2024-12-31 00:00:00 +00:00
|
|
|
[](const ProcessorConfig& proc_cfg,
|
2024-12-02 07:24:30 +00:00
|
|
|
uint32_t vk_device_index) -> std::unique_ptr<Processor> {
|
2024-12-31 00:00:00 +00:00
|
|
|
const auto& cfg = std::get<RIFEConfig>(proc_cfg.config);
|
2024-12-02 07:24:30 +00:00
|
|
|
if (cfg.model_name.empty()) {
|
2024-12-20 04:46:10 +00:00
|
|
|
logger()->critical("Model name must be provided for the RIFE filter");
|
2024-12-01 09:55:56 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return std::make_unique<InterpolatorRIFE>(
|
|
|
|
static_cast<int>(vk_device_index),
|
|
|
|
cfg.tta_mode,
|
|
|
|
cfg.tta_temporal_mode,
|
|
|
|
cfg.uhd_mode,
|
|
|
|
cfg.num_threads,
|
|
|
|
cfg.model_name
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2024-12-17 16:24:51 +00:00
|
|
|
|
|
|
|
} // namespace processors
|
|
|
|
} // namespace video2x
|