2024-10-10 07:23:13 +00:00
|
|
|
#include "realesrgan_filter.h"
|
|
|
|
|
2024-10-08 02:29:00 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdio>
|
2024-11-02 00:00:00 +00:00
|
|
|
#include <filesystem>
|
2024-10-08 02:29:00 +00:00
|
|
|
|
2024-10-14 02:46:59 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
|
2024-10-08 02:29:00 +00:00
|
|
|
#include "conversions.h"
|
|
|
|
#include "fsutils.h"
|
|
|
|
|
|
|
|
RealesrganFilter::RealesrganFilter(
|
|
|
|
int gpuid,
|
|
|
|
bool tta_mode,
|
|
|
|
int scaling_factor,
|
2024-11-04 00:00:00 +00:00
|
|
|
const StringType model_name
|
2024-10-08 02:29:00 +00:00
|
|
|
)
|
|
|
|
: realesrgan(nullptr),
|
|
|
|
gpuid(gpuid),
|
|
|
|
tta_mode(tta_mode),
|
|
|
|
scaling_factor(scaling_factor),
|
2024-11-04 00:00:00 +00:00
|
|
|
model_name(std::move(model_name)) {}
|
2024-10-08 02:29:00 +00:00
|
|
|
|
|
|
|
RealesrganFilter::~RealesrganFilter() {
|
|
|
|
if (realesrgan) {
|
|
|
|
delete realesrgan;
|
|
|
|
realesrgan = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-25 00:00:00 +00:00
|
|
|
int RealesrganFilter::init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, AVBufferRef *_) {
|
2024-10-08 02:29:00 +00:00
|
|
|
// Construct the model paths using std::filesystem
|
|
|
|
std::filesystem::path model_param_path;
|
|
|
|
std::filesystem::path model_bin_path;
|
|
|
|
|
2024-11-04 00:00:00 +00:00
|
|
|
StringType param_file_name =
|
|
|
|
model_name + STR("-x") + to_string_type(scaling_factor) + STR(".param");
|
|
|
|
StringType bin_file_name =
|
|
|
|
model_name + STR("-x") + to_string_type(scaling_factor) + STR(".bin");
|
2024-11-02 00:00:00 +00:00
|
|
|
|
|
|
|
// Find the model paths by model name if provided
|
2024-11-04 00:00:00 +00:00
|
|
|
model_param_path = std::filesystem::path(STR("models")) / STR("realesrgan") / param_file_name;
|
|
|
|
model_bin_path = std::filesystem::path(STR("models")) / STR("realesrgan") / bin_file_name;
|
2024-10-08 02:29:00 +00:00
|
|
|
|
|
|
|
// Get the full paths using a function that possibly modifies or validates the path
|
|
|
|
std::filesystem::path model_param_full_path = find_resource_file(model_param_path);
|
|
|
|
std::filesystem::path model_bin_full_path = find_resource_file(model_bin_path);
|
|
|
|
|
2024-10-10 07:23:13 +00:00
|
|
|
// Check if the model files exist
|
|
|
|
if (!std::filesystem::exists(model_param_full_path)) {
|
2024-11-04 00:00:00 +00:00
|
|
|
spdlog::error("RealESRGAN model param file not found: {}", model_param_path.u8string());
|
2024-10-10 07:23:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!std::filesystem::exists(model_bin_full_path)) {
|
2024-11-04 00:00:00 +00:00
|
|
|
spdlog::error("RealESRGAN model bin file not found: {}", model_bin_path.u8string());
|
2024-10-10 07:23:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-10-08 02:29:00 +00:00
|
|
|
// Create a new RealESRGAN instance
|
|
|
|
realesrgan = new RealESRGAN(gpuid, tta_mode);
|
|
|
|
|
|
|
|
// Store the time bases
|
2024-10-21 23:54:22 +00:00
|
|
|
in_time_base = dec_ctx->time_base;
|
|
|
|
out_time_base = enc_ctx->time_base;
|
|
|
|
out_pix_fmt = enc_ctx->pix_fmt;
|
2024-10-08 02:29:00 +00:00
|
|
|
|
|
|
|
// Load the model
|
|
|
|
if (realesrgan->load(model_param_full_path, model_bin_full_path) != 0) {
|
2024-10-14 02:46:59 +00:00
|
|
|
spdlog::error("Failed to load RealESRGAN model");
|
2024-10-08 02:29:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set RealESRGAN parameters
|
|
|
|
realesrgan->scale = scaling_factor;
|
|
|
|
realesrgan->prepadding = 10;
|
|
|
|
|
|
|
|
// Calculate tilesize based on GPU heap budget
|
|
|
|
uint32_t heap_budget = ncnn::get_gpu_device(gpuid)->get_heap_budget();
|
|
|
|
if (heap_budget > 1900) {
|
|
|
|
realesrgan->tilesize = 200;
|
|
|
|
} else if (heap_budget > 550) {
|
|
|
|
realesrgan->tilesize = 100;
|
|
|
|
} else if (heap_budget > 190) {
|
|
|
|
realesrgan->tilesize = 64;
|
|
|
|
} else {
|
|
|
|
realesrgan->tilesize = 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-10-21 23:54:22 +00:00
|
|
|
int RealesrganFilter::process_frame(AVFrame *in_frame, AVFrame **out_frame) {
|
2024-10-10 07:23:13 +00:00
|
|
|
int ret;
|
|
|
|
|
2024-10-08 02:29:00 +00:00
|
|
|
// Convert the input frame to RGB24
|
2024-10-21 23:54:22 +00:00
|
|
|
ncnn::Mat in_mat = avframe_to_ncnn_mat(in_frame);
|
|
|
|
if (in_mat.empty()) {
|
2024-10-14 02:46:59 +00:00
|
|
|
spdlog::error("Failed to convert AVFrame to ncnn::Mat");
|
2024-10-10 07:23:13 +00:00
|
|
|
return -1;
|
2024-10-08 02:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate space for ouptut ncnn::Mat
|
2024-10-21 23:54:22 +00:00
|
|
|
int output_width = in_mat.w * realesrgan->scale;
|
|
|
|
int output_height = in_mat.h * realesrgan->scale;
|
2024-10-26 00:00:00 +00:00
|
|
|
ncnn::Mat out_mat = ncnn::Mat(output_width, output_height, static_cast<size_t>(3), 3);
|
2024-10-08 02:29:00 +00:00
|
|
|
|
2024-10-21 23:54:22 +00:00
|
|
|
ret = realesrgan->process(in_mat, out_mat);
|
2024-10-10 07:23:13 +00:00
|
|
|
if (ret != 0) {
|
2024-10-14 02:46:59 +00:00
|
|
|
spdlog::error("RealESRGAN processing failed");
|
2024-10-10 07:23:13 +00:00
|
|
|
return ret;
|
2024-10-08 02:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert ncnn::Mat to AVFrame
|
2024-10-21 23:54:22 +00:00
|
|
|
*out_frame = ncnn_mat_to_avframe(out_mat, out_pix_fmt);
|
2024-10-08 02:29:00 +00:00
|
|
|
|
|
|
|
// Rescale PTS to encoder's time base
|
2024-10-21 23:54:22 +00:00
|
|
|
(*out_frame)->pts = av_rescale_q(in_frame->pts, in_time_base, out_time_base);
|
2024-10-08 02:29:00 +00:00
|
|
|
|
|
|
|
// Return the processed frame to the caller
|
2024-10-10 07:23:13 +00:00
|
|
|
return ret;
|
2024-10-08 02:29:00 +00:00
|
|
|
}
|