Skip to content

Commit

Permalink
style: added platform-dependent type aliases for char and string
Browse files Browse the repository at this point in the history
Signed-off-by: k4yt3x <[email protected]>
  • Loading branch information
k4yt3x committed Nov 4, 2024
1 parent 851f13b commit e60650f
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 244 deletions.
31 changes: 30 additions & 1 deletion include/libvideo2x/fsutils.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,41 @@
#ifndef FSUTILS_H
#define FSUTILS_H

#ifdef __cplusplus
#include <filesystem>
#endif

#ifdef _WIN32
#ifdef UNICODE
typedef wchar_t CharType;
#define STR(x) L##x
#else
typedef char CharType;
#define STR(x) x
#endif
#else
typedef char CharType;
#define STR(x) x
#endif

#ifdef __cplusplus
#include <string>
#ifdef _WIN32
#ifdef UNICODE
typedef std::wstring StringType;
#else
typedef std::string StringType;
#endif
#else
typedef std::string StringType;
#endif

bool filepath_is_readable(const std::filesystem::path &path);

std::filesystem::path find_resource_file(const std::filesystem::path &path);

std::string path_to_string(const std::filesystem::path& path);
std::string path_to_string(const std::filesystem::path &path);

#endif // __cplusplus

#endif // FSUTILS_H
51 changes: 28 additions & 23 deletions include/libvideo2x/libvideo2x.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
#include <stdint.h>
#include <time.h>

#ifdef __cplusplus
extern "C" {
#endif

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>

#ifdef _WIN32
#ifdef LIBVIDEO2X_EXPORTS
#define LIBVIDEO2X_API __declspec(dllexport)
Expand All @@ -15,13 +22,12 @@
#define LIBVIDEO2X_API
#endif

#ifdef __cplusplus
extern "C" {
#ifdef _WIN32
typedef wchar_t tchar;
#else
typedef char tchar;
#endif

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>

// Enum to specify filter type
enum FilterType {
FILTER_LIBPLACEBO,
Expand All @@ -43,23 +49,15 @@ enum Libvideo2xLogLevel {
struct LibplaceboConfig {
int out_width;
int out_height;
#ifdef _WIN32
const wchar_t *shader_path;
#else
const char *shader_path;
#endif
const tchar *shader_path;
};

// Configuration for RealESRGAN filter
struct RealESRGANConfig {
int gpuid;
bool tta_mode;
int scaling_factor;
#ifdef _WIN32
const wchar_t *model_name;
#else
const char *model_name;
#endif
const tchar *model_name;
};

// Unified filter configuration
Expand Down Expand Up @@ -93,15 +91,22 @@ struct VideoProcessingContext {
bool completed;
};

// C-compatible process_video function
/**
* @brief Process a video file using the selected filter and encoder settings.
*
* @param[in] in_fname Path to the input video file
* @param[in] out_fname Path to the output video file
* @param[in] log_level Log level
* @param[in] benchmark Flag to enable benchmarking mode
* @param[in] hw_type Hardware device type
* @param[in] filter_config Filter configurations
* @param[in] encoder_config Encoder configurations
* @param[in,out] proc_ctx Video processing context
* @return int 0 on success, non-zero value on error
*/
LIBVIDEO2X_API int process_video(
#ifdef _WIN32
const wchar_t *in_fname,
const wchar_t *out_fname,
#else
const char *in_fname,
const char *out_fname,
#endif
const tchar *in_fname,
const tchar *out_fname,
enum Libvideo2xLogLevel log_level,
bool benchmark,
enum AVHWDeviceType hw_device_type,
Expand Down
13 changes: 3 additions & 10 deletions include/libvideo2x/realesrgan_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern "C" {
}

#include "filter.h"
#include "fsutils.h"
#include "realesrgan.h"

// RealesrganFilter class definition
Expand All @@ -15,11 +16,7 @@ class RealesrganFilter : public Filter {
int gpuid;
bool tta_mode;
int scaling_factor;
#ifdef _WIN32
const std::wstring model_name;
#else
const std::string model_name;
#endif
const StringType model_name;
AVRational in_time_base;
AVRational out_time_base;
AVPixelFormat out_pix_fmt;
Expand All @@ -30,11 +27,7 @@ class RealesrganFilter : public Filter {
int gpuid = 0,
bool tta_mode = false,
int scaling_factor = 4,
#ifdef _WIN32
const std::wstring model_name = L"realesr-animevideov3"
#else
const std::string model_name = "realesr-animevideov3"
#endif
const StringType model_name = STR("realesr-animevideov3")
);

// Destructor
Expand Down
7 changes: 2 additions & 5 deletions src/libplacebo_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ int LibplaceboFilter::init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, AVB
} else {
// Construct the fallback path using std::filesystem
shader_full_path = find_resource_file(
#ifdef _WIN32
std::filesystem::path("models") / L"libplacebo" / (shader_path.wstring() + L".glsl")
#else
std::filesystem::path("models") / "libplacebo" / (shader_path.string() + ".glsl")
#endif
std::filesystem::path(STR("models")) / STR("libplacebo") /
(path_to_string(shader_path) + STR(".glsl"))
);
}

Expand Down
41 changes: 5 additions & 36 deletions src/libvideo2x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,11 @@ extern "C" {
#include "decoder.h"
#include "encoder.h"
#include "filter.h"
#include "fsutils.h"
#include "libplacebo_filter.h"
#include "realesrgan_filter.h"

/**
* @brief Process frames using the selected filter.
*
* @param[in] encoder_config Encoder configurations
* @param[in,out] proc_ctx Struct containing the processing context
* @param[in] ifmt_ctx Input format context
* @param[in] ofmt_ctx Output format context
* @param[in] dec_ctx Decoder context
* @param[in] enc_ctx Encoder context
* @param[in] filter Filter instance
* @param[in] vstream_idx Index of the video stream in the input format context
* @param[in] stream_map Array mapping input stream indexes to output stream indexes
* @param[in] benchmark Flag to enable benchmarking mode
* @return int 0 on success, negative value on error
*/
// Process frames using the selected filter.
static int process_frames(
EncoderConfig *encoder_config,
VideoProcessingContext *proc_ctx,
Expand Down Expand Up @@ -252,27 +239,9 @@ static int process_frames(
return ret;
}

/**
* @brief Process a video file using the selected filter and encoder settings.
*
* @param[in] in_fname Path to the input video file
* @param[in] out_fname Path to the output video file
* @param[in] log_level Log level
* @param[in] benchmark Flag to enable benchmarking mode
* @param[in] hw_type Hardware device type
* @param[in] filter_config Filter configurations
* @param[in] encoder_config Encoder configurations
* @param[in,out] proc_ctx Video processing context
* @return int 0 on success, non-zero value on error
*/
extern "C" int process_video(
#ifdef _WIN32
const wchar_t *in_fname,
const wchar_t *out_fname,
#else
const char *in_fname,
const char *out_fname,
#endif
const CharType *in_fname,
const CharType *out_fname,
Libvideo2xLogLevel log_level,
bool benchmark,
AVHWDeviceType hw_type,
Expand Down Expand Up @@ -454,7 +423,7 @@ extern "C" int process_video(
return -1;
}
filter = new RealesrganFilter{
config.gpuid, config.tta_mode, config.scaling_factor, config.model_name
config.gpuid, config.tta_mode, config.scaling_factor, StringType(config.model_name)
};
} else {
spdlog::error("Unknown filter type");
Expand Down
25 changes: 8 additions & 17 deletions src/realesrgan_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,18 @@
#include <spdlog/spdlog.h>

#include "conversions.h"
#include "fsutils.h"

RealesrganFilter::RealesrganFilter(
int gpuid,
bool tta_mode,
int scaling_factor,
#ifdef _WIN32
const std::wstring model_name
#else
const std::string model_name
#endif
const StringType model_name
)
: realesrgan(nullptr),
gpuid(gpuid),
tta_mode(tta_mode),
scaling_factor(scaling_factor),
model_name(std::move(model_name)) {
}
model_name(std::move(model_name)) {}

RealesrganFilter::~RealesrganFilter() {
if (realesrgan) {
Expand All @@ -39,17 +33,14 @@ int RealesrganFilter::init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, AVB
std::filesystem::path model_param_path;
std::filesystem::path model_bin_path;

#ifdef _WIN32
std::wstring param_file_name = model_name + L"-x" + std::to_wstring(scaling_factor) + L".param";
std::wstring bin_file_name = model_name + L"-x" + std::to_wstring(scaling_factor) + L".bin";
#else
std::string param_file_name = model_name + "-x" + std::to_string(scaling_factor) + ".param";
std::string bin_file_name = model_name + "-x" + std::to_string(scaling_factor) + ".bin";
#endif
std::string param_file_name =
model_name + STR("-x") + std::to_string(scaling_factor) + STR(".param");
std::string bin_file_name =
model_name + STR("-x") + std::to_string(scaling_factor) + STR(".bin");

// Find the model paths by model name if provided
model_param_path = std::filesystem::path("models") / "realesrgan" / param_file_name;
model_bin_path = std::filesystem::path("models") / "realesrgan" / bin_file_name;
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;

// 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);
Expand Down
Loading

0 comments on commit e60650f

Please sign in to comment.