feat(*): rewritten the project with C/C++ (#1172)

This commit is contained in:
K4YT3X
2024-10-07 19:29:00 -07:00
committed by GitHub
parent 721de8cbce
commit a7952fc493
80 changed files with 6664 additions and 5734 deletions

16
include/conversions.h Normal file
View File

@@ -0,0 +1,16 @@
#ifndef CONVERSIONS_H
#define CONVERSIONS_H
#include <libavutil/frame.h>
#include <mat.h>
// Convert AVFrame to another pixel format
AVFrame *convert_avframe_pix_fmt(AVFrame *src_frame, AVPixelFormat pix_fmt);
// Convert AVFrame to ncnn::Mat
ncnn::Mat avframe_to_ncnn_mat(AVFrame *frame);
// Convert ncnn::Mat to AVFrame
AVFrame *ncnn_mat_to_avframe(const ncnn::Mat &mat, AVPixelFormat pix_fmt);
#endif // CONVERSIONS_H

15
include/decoder.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef DECODER_H
#define DECODER_H
#include <libavcodec/avcodec.h>
#include <libavfilter/avfilter.h>
#include <libavformat/avformat.h>
int init_decoder(
const char *input_filename,
AVFormatContext **fmt_ctx,
AVCodecContext **dec_ctx,
int *video_stream_index
);
#endif // DECODER_H

21
include/encoder.h Normal file
View File

@@ -0,0 +1,21 @@
#ifndef ENCODER_H
#define ENCODER_H
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include "libvideo2x.h"
int init_encoder(
const char *output_filename,
AVFormatContext **ofmt_ctx,
AVCodecContext **enc_ctx,
AVCodecContext *dec_ctx,
EncoderConfig *encoder_config
);
int encode_and_write_frame(AVFrame *frame, AVCodecContext *enc_ctx, AVFormatContext *ofmt_ctx);
int flush_encoder(AVCodecContext *enc_ctx, AVFormatContext *ofmt_ctx);
#endif // ENCODER_H

20
include/filter.h Normal file
View File

@@ -0,0 +1,20 @@
#ifndef FILTER_H
#define FILTER_H
#include <vector>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavfilter/avfilter.h>
}
// Abstract base class for filters
class Filter {
public:
virtual ~Filter() {}
virtual int init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx) = 0;
virtual AVFrame *process_frame(AVFrame *input_frame) = 0;
virtual int flush(std::vector<AVFrame *> &processed_frames) = 0;
};
#endif // FILTER_H

12
include/fsutils.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef FSUTILS_H
#define FSUTILS_H
#include <filesystem>
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);
#endif // FSUTILS_H

32
include/getopt.h Normal file
View File

@@ -0,0 +1,32 @@
#ifndef __GETOPT_H__
#define __GETOPT_H__
#ifdef __cplusplus
extern "C" {
#endif
extern int opterr; /* if error message should be printed */
extern int optind; /* index into parent argv vector */
extern int optopt; /* character checked for validity */
extern int optreset; /* reset getopt */
extern char *optarg; /* argument associated with option */
struct option {
const char *name;
int has_arg;
int *flag;
int val;
};
#define no_argument 0
#define required_argument 1
#define optional_argument 2
int getopt(int, char **, const char *);
int getopt_long(int, char **, const char *, const struct option *, int *);
#ifdef __cplusplus
}
#endif
#endif /* __GETOPT_H__ */

21
include/libplacebo.h Normal file
View File

@@ -0,0 +1,21 @@
#ifndef PLACEBO_H
#define PLACEBO_H
#include <filesystem>
#include <libavcodec/avcodec.h>
#include <libavfilter/avfilter.h>
#include <libavutil/buffer.h>
int init_libplacebo(
AVFilterGraph **filter_graph,
AVFilterContext **buffersrc_ctx,
AVFilterContext **buffersink_ctx,
AVBufferRef **device_ctx,
AVCodecContext *dec_ctx,
int output_width,
int output_height,
const std::filesystem::path &shader_path
);
#endif // PLACEBO_H

View File

@@ -0,0 +1,39 @@
#ifndef LIBPLACEBO_FILTER_H
#define LIBPLACEBO_FILTER_H
#include <filesystem>
#include <libavutil/buffer.h>
#include "filter.h"
// LibplaceboFilter class definition
class LibplaceboFilter : public Filter {
private:
AVFilterGraph *filter_graph;
AVFilterContext *buffersrc_ctx;
AVFilterContext *buffersink_ctx;
AVBufferRef *device_ctx;
int output_width;
int output_height;
const std::filesystem::path shader_path;
AVRational output_time_base;
public:
// Constructor
LibplaceboFilter(int width, int height, const std::filesystem::path &shader_path);
// Destructor
virtual ~LibplaceboFilter();
// Initializes the filter with decoder and encoder contexts
int init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx) override;
// Processes an input frame and returns the processed frame
AVFrame *process_frame(AVFrame *input_frame) override;
// Flushes any remaining frames
int flush(std::vector<AVFrame *> &processed_frames) override;
};
#endif // LIBPLACEBO_FILTER_H

86
include/libvideo2x.h Normal file
View File

@@ -0,0 +1,86 @@
#ifndef LIBVIDEO2X_H
#define LIBVIDEO2X_H
#include <libavutil/pixfmt.h>
#include <stdint.h>
#include <time.h>
#include <libavcodec/avcodec.h>
#include <libavcodec/codec_id.h>
#ifdef _WIN32
#ifdef LIBVIDEO2X_EXPORTS
#define LIBVIDEO2X_API __declspec(dllexport)
#else
#define LIBVIDEO2X_API __declspec(dllimport)
#endif
#else
#define LIBVIDEO2X_API
#endif
#ifdef __cplusplus
extern "C" {
#endif
// Enum to specify filter type
enum FilterType {
FILTER_LIBPLACEBO,
FILTER_REALESRGAN
};
// Configuration for Libplacebo filter
struct LibplaceboConfig {
int output_width;
int output_height;
const char *shader_path;
};
// Configuration for RealESRGAN filter
struct RealESRGANConfig {
int gpuid;
int tta_mode;
int scaling_factor;
const char *model;
};
// Unified filter configuration
struct FilterConfig {
enum FilterType filter_type;
union {
struct LibplaceboConfig libplacebo;
struct RealESRGANConfig realesrgan;
} config;
};
// Encoder configuration
struct EncoderConfig {
int output_width;
int output_height;
enum AVCodecID codec;
enum AVPixelFormat pix_fmt;
const char *preset;
int64_t bit_rate;
float crf;
};
// Processing status
struct ProcessingStatus {
int64_t processed_frames;
int64_t total_frames;
time_t start_time;
};
// C-compatible process_video function
LIBVIDEO2X_API int process_video(
const char *input_filename,
const char *output_filename,
const struct FilterConfig *filter_config,
struct EncoderConfig *encoder_config,
struct ProcessingStatus *status
);
#ifdef __cplusplus
}
#endif
#endif // LIBVIDEO2X_H

View File

@@ -0,0 +1,47 @@
#ifndef REALSRGAN_FILTER_H
#define REALSRGAN_FILTER_H
#include <filesystem>
#include "filter.h"
#include "realesrgan.h"
// RealesrganFilter class definition
class RealesrganFilter : public Filter {
private:
RealESRGAN *realesrgan;
int gpuid;
bool tta_mode;
int scaling_factor;
const char *model;
const std::filesystem::path custom_model_param_path;
const std::filesystem::path custom_model_bin_path;
AVRational input_time_base;
AVRational output_time_base;
AVPixelFormat output_pix_fmt;
public:
// Constructor
RealesrganFilter(
int gpuid = 0,
bool tta_mode = false,
int scaling_factor = 4,
const char *model = "realesr-animevideov3",
const std::filesystem::path custom_model_bin_pathmodel_param_path = std::filesystem::path(),
const std::filesystem::path custom_model_bin_pathmodel_bin_path = std::filesystem::path()
);
// Destructor
virtual ~RealesrganFilter();
// Initializes the filter with decoder and encoder contexts
int init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx) override;
// Processes an input frame and returns the processed frame
AVFrame *process_frame(AVFrame *input_frame) override;
// Flushes any remaining frames (if necessary)
int flush(std::vector<AVFrame *> &processed_frames) override;
};
#endif