feat(*): added support for copying audio/subtitle streams and pause/abort (#1179)

* feat: added Makefile target for debian
* fix: fixed Dockerfile installing the wrong package
* feat: added hwaccel for encoder and decoder
* feat: added benchmark mode
* feat: removed hard-coded keyframe info
* chore: cleaned up headers and organized code
* style: cleaned up headers and includes
* feat: added a progress bar for CLI
* feat: removed atomicity requirements on processed frames
* feat: added pause and abort for CLI
* chore: updated default preset and crf settings
* feat: added support for copying audio and subtitle streams
* fix: fixed syntax issues for MSVC
* fix: fixed audio/subtitle timestamp rescaling

Signed-off-by: k4yt3x <i@k4yt3x.com>
This commit is contained in:
K4YT3X
2024-10-10 00:23:13 -07:00
committed by GitHub
parent c7fa9c10e6
commit 37c2c4c647
21 changed files with 731 additions and 322 deletions

View File

@@ -1,7 +1,10 @@
#ifndef CONVERSIONS_H
#define CONVERSIONS_H
#include <libavutil/frame.h>
extern "C" {
#include <libswscale/swscale.h>
}
#include <mat.h>
// Convert AVFrame to another pixel format

View File

@@ -1,11 +1,14 @@
#ifndef DECODER_H
#define DECODER_H
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavfilter/avfilter.h>
#include <libavformat/avformat.h>
}
int init_decoder(
AVHWDeviceType hw_type,
AVBufferRef *hw_ctx,
const char *input_filename,
AVFormatContext **fmt_ctx,
AVCodecContext **dec_ctx,

View File

@@ -1,20 +1,32 @@
#ifndef ENCODER_H
#define ENCODER_H
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavutil/opt.h>
}
#include "libvideo2x.h"
int init_encoder(
AVBufferRef *hw_ctx,
const char *output_filename,
AVFormatContext *ifmt_ctx,
AVFormatContext **ofmt_ctx,
AVCodecContext **enc_ctx,
AVCodecContext *dec_ctx,
EncoderConfig *encoder_config
EncoderConfig *encoder_config,
int video_stream_index,
int **stream_mapping
);
int encode_and_write_frame(AVFrame *frame, AVCodecContext *enc_ctx, AVFormatContext *ofmt_ctx);
int encode_and_write_frame(
AVFrame *frame,
AVCodecContext *enc_ctx,
AVFormatContext *ofmt_ctx,
int video_stream_index
);
int flush_encoder(AVCodecContext *enc_ctx, AVFormatContext *ofmt_ctx);

View File

@@ -6,14 +6,15 @@
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavfilter/avfilter.h>
#include <libavutil/buffer.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 init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, AVBufferRef *hw_ctx) = 0;
virtual int process_frame(AVFrame *input_frame, AVFrame **output_frame) = 0;
virtual int flush(std::vector<AVFrame *> &processed_frames) = 0;
};

View File

@@ -22,7 +22,7 @@ struct option {
#define required_argument 1
#define optional_argument 2
int getopt(int, char **, const char *);
// int getopt(int, char **, const char *);
int getopt_long(int, char **, const char *, const struct option *, int *);
#ifdef __cplusplus

View File

@@ -3,15 +3,16 @@
#include <filesystem>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavfilter/avfilter.h>
#include <libavutil/buffer.h>
}
int init_libplacebo(
AVBufferRef *hw_ctx,
AVFilterGraph **filter_graph,
AVFilterContext **buffersrc_ctx,
AVFilterContext **buffersink_ctx,
AVBufferRef **device_ctx,
AVCodecContext *dec_ctx,
int output_width,
int output_height,

View File

@@ -3,7 +3,11 @@
#include <filesystem>
#include <libavutil/buffer.h>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h>
}
#include "filter.h"
@@ -13,7 +17,6 @@ class LibplaceboFilter : public Filter {
AVFilterGraph *filter_graph;
AVFilterContext *buffersrc_ctx;
AVFilterContext *buffersink_ctx;
AVBufferRef *device_ctx;
int output_width;
int output_height;
const std::filesystem::path shader_path;
@@ -27,10 +30,10 @@ class LibplaceboFilter : public Filter {
virtual ~LibplaceboFilter();
// Initializes the filter with decoder and encoder contexts
int init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx) override;
int init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, AVBufferRef *hw_ctx) override;
// Processes an input frame and returns the processed frame
AVFrame *process_frame(AVFrame *input_frame) override;
int process_frame(AVFrame *input_frame, AVFrame **output_frame) override;
// Flushes any remaining frames
int flush(std::vector<AVFrame *> &processed_frames) override;

View File

@@ -1,13 +1,10 @@
#ifndef LIBVIDEO2X_H
#define LIBVIDEO2X_H
#include <libavutil/pixfmt.h>
#include <stdbool.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)
@@ -22,6 +19,9 @@
extern "C" {
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
// Enum to specify filter type
enum FilterType {
FILTER_LIBPLACEBO,
@@ -38,7 +38,7 @@ struct LibplaceboConfig {
// Configuration for RealESRGAN filter
struct RealESRGANConfig {
int gpuid;
int tta_mode;
bool tta_mode;
int scaling_factor;
const char *model;
};
@@ -56,6 +56,7 @@ struct FilterConfig {
struct EncoderConfig {
int output_width;
int output_height;
bool copy_streams;
enum AVCodecID codec;
enum AVPixelFormat pix_fmt;
const char *preset;
@@ -63,20 +64,25 @@ struct EncoderConfig {
float crf;
};
// Processing status
struct ProcessingStatus {
// Video processing context
struct VideoProcessingContext {
int64_t processed_frames;
int64_t total_frames;
time_t start_time;
bool pause;
bool abort;
bool completed;
};
// C-compatible process_video function
LIBVIDEO2X_API int process_video(
const char *input_filename,
const char *output_filename,
bool benchmark,
enum AVHWDeviceType hw_device_type,
const struct FilterConfig *filter_config,
struct EncoderConfig *encoder_config,
struct ProcessingStatus *status
struct VideoProcessingContext *proc_ctx
);
#ifdef __cplusplus

View File

@@ -3,6 +3,10 @@
#include <filesystem>
extern "C" {
#include <libavcodec/avcodec.h>
}
#include "filter.h"
#include "realesrgan.h"
@@ -35,10 +39,10 @@ class RealesrganFilter : public Filter {
virtual ~RealesrganFilter();
// Initializes the filter with decoder and encoder contexts
int init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx) override;
int init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, AVBufferRef *hw_ctx) override;
// Processes an input frame and returns the processed frame
AVFrame *process_frame(AVFrame *input_frame) override;
int process_frame(AVFrame *input_frame, AVFrame **output_frame) override;
// Flushes any remaining frames (if necessary)
int flush(std::vector<AVFrame *> &processed_frames) override;