chore(libvideo2x)!: replace the C API with C++ API (#1245)

* chore(libvideo2x)!: replace the C API with C++ API
* fix: convert wide string to u8 for av_opt_set
* style: removed unnecessary enum and struct specifiers

Signed-off-by: k4yt3x <i@k4yt3x.com>
This commit is contained in:
K4YT3X
2024-12-02 07:24:30 +00:00
committed by GitHub
parent 24d43a8478
commit f8dcad3aef
27 changed files with 420 additions and 457 deletions

View File

@@ -11,7 +11,7 @@ AVRational get_video_frame_rate(AVFormatContext *ifmt_ctx, int in_vstream_idx);
int64_t get_video_frame_count(AVFormatContext *ifmt_ctx, int in_vstream_idx);
enum AVPixelFormat
AVPixelFormat
get_encoder_default_pix_fmt(const AVCodec *encoder, AVPixelFormat target_pix_fmt);
float get_frame_diff(AVFrame *frame1, AVFrame *frame2);

View File

@@ -1,22 +0,0 @@
#ifndef CHAR_DEFS_H
#define CHAR_DEFS_H
#ifdef _WIN32
typedef wchar_t CharType;
#define STR(x) L##x
#else
typedef char CharType;
#define STR(x) x
#endif
#ifdef __cplusplus
#include <string>
#ifdef _WIN32
typedef std::wstring StringType;
#else
typedef std::string StringType;
#endif
#endif // __cplusplus
#endif // CHAR_DEFS_H

View File

@@ -20,9 +20,8 @@ class Decoder {
int get_video_stream_index() const;
private:
static enum AVPixelFormat hw_pix_fmt_;
static enum AVPixelFormat
get_hw_format(AVCodecContext *ctx, const enum AVPixelFormat *pix_fmts);
static AVPixelFormat hw_pix_fmt_;
static AVPixelFormat get_hw_format(AVCodecContext *ctx, const AVPixelFormat *pix_fmts);
AVFormatContext *fmt_ctx_;
AVCodecContext *dec_ctx_;

View File

@@ -21,8 +21,8 @@ class Encoder {
const std::filesystem::path &out_fpath,
AVFormatContext *ifmt_ctx,
AVCodecContext *dec_ctx,
EncoderConfig *encoder_config,
const ProcessorConfig *processor_config,
EncoderConfig &enc_cfg,
const ProcessorConfig &proc_cfg,
int in_vstream_idx
);

View File

@@ -46,11 +46,11 @@ class FilterLibplacebo : public Filter {
int flush(std::vector<AVFrame *> &flushed_frames) override;
// Returns the filter's type
ProcessorType get_processor_type() const override { return PROCESSOR_LIBPLACEBO; }
ProcessorType get_processor_type() const override { return ProcessorType::Libplacebo; }
// Returns the filter's output dimensions
void get_output_dimensions(
const ProcessorConfig *processor_config,
const ProcessorConfig &proc_cfg,
int in_width,
int in_height,
int &out_width,

View File

@@ -5,7 +5,6 @@ extern "C" {
#include <libavcodec/avcodec.h>
}
#include "char_defs.h"
#include "processor.h"
#include "realesrgan.h"
@@ -40,11 +39,11 @@ class FilterRealesrgan : public Filter {
int filter(AVFrame *in_frame, AVFrame **out_frame) override;
// Returns the filter's type
ProcessorType get_processor_type() const override { return PROCESSOR_REALESRGAN; }
ProcessorType get_processor_type() const override { return ProcessorType::RealESRGAN; }
// Returns the filter's output dimensions
void get_output_dimensions(
const ProcessorConfig *processor_config,
const ProcessorConfig &proc_cfg,
int in_width,
int in_height,
int &out_width,

View File

@@ -7,8 +7,8 @@
#include "processor.h"
int process_frames(
const EncoderConfig *encoder_config,
const ProcessorConfig *processor_config,
const EncoderConfig &enc_cfg,
const ProcessorConfig &proc_cfg,
VideoProcessingContext *proc_ctx,
Decoder &decoder,
Encoder &encoder,

View File

@@ -4,7 +4,19 @@
#include <filesystem>
#include <string>
#include "char_defs.h"
#ifdef _WIN32
typedef wchar_t CharType;
#define STR(x) L##x
#else
typedef char CharType;
#define STR(x) x
#endif
#ifdef _WIN32
typedef std::wstring StringType;
#else
typedef std::string StringType;
#endif
bool filepath_is_readable(const std::filesystem::path &path);
@@ -12,6 +24,8 @@ std::filesystem::path find_resource_file(const std::filesystem::path &path);
std::string path_to_u8string(const std::filesystem::path &path);
std::string wstring_to_u8string(const StringType &wstr);
StringType path_to_string_type(const std::filesystem::path &path);
StringType to_string_type(int value);

View File

@@ -5,7 +5,6 @@ extern "C" {
#include <libavcodec/avcodec.h>
}
#include "char_defs.h"
#include "processor.h"
#include "rife.h"
@@ -45,11 +44,11 @@ class InterpolatorRIFE : public Interpolator {
override;
// Returns the interpolator's type
ProcessorType get_processor_type() const override { return PROCESSOR_RIFE; }
ProcessorType get_processor_type() const override { return ProcessorType::RIFE; }
// Returns the interpolator's output dimensions
void get_output_dimensions(
const ProcessorConfig *processor_config,
const ProcessorConfig &proc_cfg,
int in_width,
int in_height,
int &out_width,

View File

@@ -1,20 +1,17 @@
#ifndef LIBVIDEO2X_H
#define LIBVIDEO2X_H
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#include <filesystem>
#include <variant>
#include <vector>
#ifdef __cplusplus
extern "C" {
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#ifdef __cplusplus
}
#endif
#include "char_defs.h"
#include "fsutils.h"
#include "logging.h"
#ifdef _WIN32
#ifdef LIBVIDEO2X_EXPORTS
@@ -26,38 +23,24 @@ extern "C" {
#define LIBVIDEO2X_API
#endif
#ifdef __cplusplus
extern "C" {
#endif
enum ProcessingMode {
PROCESSING_MODE_FILTER,
PROCESSING_MODE_INTERPOLATE,
enum class ProcessingMode {
Filter,
Interpolate,
};
enum ProcessorType {
PROCESSOR_LIBPLACEBO,
PROCESSOR_REALESRGAN,
PROCESSOR_RIFE,
};
enum Libvideo2xLogLevel {
LIBVIDEO2X_LOG_LEVEL_TRACE,
LIBVIDEO2X_LOG_LEVEL_DEBUG,
LIBVIDEO2X_LOG_LEVEL_INFO,
LIBVIDEO2X_LOG_LEVEL_WARNING,
LIBVIDEO2X_LOG_LEVEL_ERROR,
LIBVIDEO2X_LOG_LEVEL_CRITICAL,
LIBVIDEO2X_LOG_LEVEL_OFF
enum class ProcessorType {
Libplacebo,
RealESRGAN,
RIFE,
};
struct LibplaceboConfig {
const CharType *shader_path;
StringType shader_path;
};
struct RealESRGANConfig {
bool tta_mode;
const CharType *model_name;
StringType model_name;
};
struct RIFEConfig {
@@ -65,34 +48,30 @@ struct RIFEConfig {
bool tta_temporal_mode;
bool uhd_mode;
int num_threads;
const CharType *model_name;
StringType model_name;
};
// Unified filter configuration
struct ProcessorConfig {
enum ProcessorType processor_type;
ProcessorType processor_type;
int width;
int height;
int scaling_factor;
int frm_rate_mul;
float scn_det_thresh;
union {
struct LibplaceboConfig libplacebo;
struct RealESRGANConfig realesrgan;
struct RIFEConfig rife;
} config;
std::variant<LibplaceboConfig, RealESRGANConfig, RIFEConfig> config;
};
// Encoder configurations
struct EncoderConfig {
// Non-AVCodecContext options
enum AVCodecID codec;
AVCodecID codec;
bool copy_streams;
// Basic video options
int width;
int height;
enum AVPixelFormat pix_fmt;
AVPixelFormat pix_fmt;
// Rate control and compression
int64_t bit_rate;
@@ -115,51 +94,34 @@ struct EncoderConfig {
int delay;
// Extra AVOptions
struct {
const char *key;
const char *value;
} *extra_options;
size_t nb_extra_options;
std::vector<std::pair<StringType, StringType>> extra_opts;
};
struct HardwareConfig {
uint32_t vk_device_index;
AVHWDeviceType hw_device_type;
};
// Video processing context
struct VideoProcessingContext {
int64_t processed_frames;
int64_t total_frames;
time_t start_time;
std::time_t start_time;
bool pause;
bool abort;
bool completed;
};
/**
* @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] vk_device_index Vulkan device index
* @param[in] hw_device_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(
const CharType *in_fname,
const CharType *out_fname,
enum Libvideo2xLogLevel log_level,
bool benchmark,
uint32_t vk_device_index,
enum AVHWDeviceType hw_device_type,
const struct ProcessorConfig *filter_config,
struct EncoderConfig *encoder_config,
struct VideoProcessingContext *proc_ctx
// Process a video file using the specified configurations
[[nodiscard]] LIBVIDEO2X_API int process_video(
const std::filesystem::path in_fname,
const std::filesystem::path out_fname,
const HardwareConfig hw_cfg,
const ProcessorConfig proc_cfg,
EncoderConfig enc_cfg,
VideoProcessingContext *proc_ctx,
Libvideo2xLogLevel log_level,
bool benchmark
);
#ifdef __cplusplus
}
#endif
#endif // LIBVIDEO2X_H

View File

@@ -0,0 +1,23 @@
#ifndef LOGGING_H
#define LOGGING_H
#include <optional>
#include "fsutils.h"
enum class Libvideo2xLogLevel {
Unknown,
Trace,
Debug,
Info,
Warning,
Error,
Critical,
Off
};
void set_log_level(Libvideo2xLogLevel log_level);
std::optional<Libvideo2xLogLevel> find_log_level_by_name(const StringType &log_level_name);
#endif // LOGGING_H

View File

@@ -19,7 +19,7 @@ class Processor {
virtual ProcessingMode get_processing_mode() const = 0;
virtual ProcessorType get_processor_type() const = 0;
virtual void get_output_dimensions(
const ProcessorConfig *processor_config,
const ProcessorConfig &proc_cfg,
int in_width,
int in_height,
int &width,
@@ -30,14 +30,14 @@ class Processor {
// Abstract base class for filters
class Filter : public Processor {
public:
ProcessingMode get_processing_mode() const override { return PROCESSING_MODE_FILTER; }
ProcessingMode get_processing_mode() const override { return ProcessingMode::Filter; }
virtual int filter(AVFrame *in_frame, AVFrame **out_frame) = 0;
};
// Abstract base class for interpolators
class Interpolator : public Processor {
public:
ProcessingMode get_processing_mode() const override { return PROCESSING_MODE_INTERPOLATE; }
ProcessingMode get_processing_mode() const override { return ProcessingMode::Interpolate; }
virtual int
interpolate(AVFrame *prev_frame, AVFrame *in_frame, AVFrame **out_frame, float time_step) = 0;
};

View File

@@ -10,7 +10,7 @@
// Processor Factory Class
class ProcessorFactory {
public:
using Creator = std::function<std::unique_ptr<Processor>(const ProcessorConfig *, uint32_t)>;
using Creator = std::function<std::unique_ptr<Processor>(const ProcessorConfig &, uint32_t)>;
// Singleton instance accessor
static ProcessorFactory &instance();
@@ -20,7 +20,7 @@ class ProcessorFactory {
// Create a processor instance based on configuration
std::unique_ptr<Processor>
create_processor(const ProcessorConfig *processor_config, uint32_t vk_device_index) const;
create_processor(const ProcessorConfig &proc_cfg, uint32_t vk_device_index) const;
private:
// Private constructor for Singleton