From ca5044f09cc031e9f44d50683d44d3f29bec6d73 Mon Sep 17 00:00:00 2001 From: k4yt3x Date: Thu, 12 Dec 2024 00:00:00 +0000 Subject: [PATCH] feat(libvideo2x): add optimization for scene detection Signed-off-by: k4yt3x --- include/libvideo2x/encoder.h | 2 +- src/encoder.cpp | 5 +++-- src/libvideo2x.cpp | 6 ++---- tools/video2x/src/argparse.cpp | 4 ++-- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/include/libvideo2x/encoder.h b/include/libvideo2x/encoder.h index d85a4a0..7a83f10 100644 --- a/include/libvideo2x/encoder.h +++ b/include/libvideo2x/encoder.h @@ -19,7 +19,6 @@ struct EncoderConfig { bool copy_streams = true; // Basic video options - int frm_rate_mul = 0; AVPixelFormat pix_fmt = AV_PIX_FMT_NONE; // Rate control and compression @@ -59,6 +58,7 @@ class Encoder { EncoderConfig &enc_cfg, int width, int height, + int frm_rate_mul, int in_vstream_idx ); diff --git a/src/encoder.cpp b/src/encoder.cpp index 74b4520..a83a055 100644 --- a/src/encoder.cpp +++ b/src/encoder.cpp @@ -35,6 +35,7 @@ int Encoder::init( EncoderConfig &enc_cfg, int width, int height, + int frm_rate_mul, int in_vstream_idx ) { int ret; @@ -123,9 +124,9 @@ int Encoder::init( spdlog::debug("Auto-selected pixel format: {}", av_get_pix_fmt_name(enc_ctx_->pix_fmt)); } - if (enc_cfg.frm_rate_mul > 0) { + if (frm_rate_mul > 0) { AVRational in_frame_rate = get_video_frame_rate(ifmt_ctx, in_vstream_idx); - enc_ctx_->framerate = {in_frame_rate.num * enc_cfg.frm_rate_mul, in_frame_rate.den}; + enc_ctx_->framerate = {in_frame_rate.num * frm_rate_mul, in_frame_rate.den}; enc_ctx_->time_base = av_inv_q(enc_ctx_->framerate); } else { // Set the output video's time base diff --git a/src/libvideo2x.cpp b/src/libvideo2x.cpp index 67dbec0..07f1acf 100644 --- a/src/libvideo2x.cpp +++ b/src/libvideo2x.cpp @@ -93,9 +93,6 @@ int VideoProcessor::process( return handle_error(-1, "Failed to determine the output dimensions"); } - // Update encoder frame rate multiplier - enc_cfg_.frm_rate_mul = proc_cfg_.frm_rate_mul; - // Initialize the encoder Encoder encoder; ret = encoder.init( @@ -106,6 +103,7 @@ int VideoProcessor::process( enc_cfg_, output_width, output_height, + proc_cfg_.frm_rate_mul, in_vstream_idx ); if (ret < 0) { @@ -389,7 +387,7 @@ int VideoProcessor::process_interpolation( // Check if a scene change is detected bool skip_frame = false; - if (prev_frame.get() != nullptr) { + if (proc_cfg_.scn_det_thresh < 100.0 && prev_frame.get() != nullptr) { float frame_diff = get_frame_diff(prev_frame.get(), frame); if (frame_diff > proc_cfg_.scn_det_thresh) { spdlog::debug( diff --git a/tools/video2x/src/argparse.cpp b/tools/video2x/src/argparse.cpp index 1426a30..ff9b532 100644 --- a/tools/video2x/src/argparse.cpp +++ b/tools/video2x/src/argparse.cpp @@ -137,9 +137,9 @@ int parse_args( ("frame-rate-mul,m", po::value(&proc_cfg.frm_rate_mul) ->notifier([](int v) { validate_min(v, "frame-rate-mul", 2); }), "Frame rate multiplier") - ("scene-thresh,t", po::value(&proc_cfg.scn_det_thresh)->default_value(20.0f) + ("scene-thresh,t", po::value(&proc_cfg.scn_det_thresh)->default_value(100.0f) ->notifier([](float v) { validate_range(v, "scene-thresh", 0.0, 100.0); }), - "Scene detection threshold") + "Scene detection threshold (20 means 20% diff between frames is a scene change)") ; po::options_description libplacebo_opts("libplacebo options");