diff --git a/CHANGELOG.md b/CHANGELOG.md index f4a2092..56372f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Improve the CMake optimization flags and option names. +### Fixed + +- A bug that causes the wrong encoder to be selected. + ## [6.3.1] - 2024-12-21 ### Fixed diff --git a/include/libvideo2x/encoder.h b/include/libvideo2x/encoder.h index fba93a5..c485c1d 100644 --- a/include/libvideo2x/encoder.h +++ b/include/libvideo2x/encoder.h @@ -10,15 +10,13 @@ extern "C" { #include } -#include "fsutils.h" - namespace video2x { namespace encoder { // Encoder configurations struct EncoderConfig { // Non-AVCodecContext options - AVCodecID codec = AV_CODEC_ID_NONE; + std::string codec = "libx264"; bool copy_streams = true; // Basic video options @@ -45,7 +43,7 @@ struct EncoderConfig { int delay = -1; // Extra AVOptions - std::vector> extra_opts; + std::vector> extra_opts; }; class Encoder { diff --git a/src/encoder.cpp b/src/encoder.cpp index 7954e9c..48a90b3 100644 --- a/src/encoder.cpp +++ b/src/encoder.cpp @@ -53,11 +53,9 @@ int Encoder::init( } // Find the encoder - const AVCodec* encoder = avcodec_find_encoder(enc_cfg.codec); + const AVCodec* encoder = avcodec_find_encoder_by_name(enc_cfg.codec.c_str()); if (!encoder) { - logger()->error( - "Required video encoder not found for codec {}", avcodec_get_name(enc_cfg.codec) - ); + logger()->error("Could not find encoder '{}'", enc_cfg.codec); return AVERROR_ENCODER_NOT_FOUND; } @@ -151,8 +149,8 @@ int Encoder::init( // Set extra AVOptions for (const auto& [opt_name, opt_value] : enc_cfg.extra_opts) { - std::string opt_name_str = fsutils::wstring_to_u8string(opt_name); - std::string opt_value_str = fsutils::wstring_to_u8string(opt_value); + std::string opt_name_str = opt_name; + std::string opt_value_str = opt_value; logger()->debug("Setting encoder option '{}' to '{}'", opt_name_str, opt_value_str); ret = av_opt_set(enc_ctx_->priv_data, opt_name_str.c_str(), opt_value_str.c_str(), 0); diff --git a/tools/video2x/src/argparse.cpp b/tools/video2x/src/argparse.cpp index 54218c1..2dad38a 100644 --- a/tools/video2x/src/argparse.cpp +++ b/tools/video2x/src/argparse.cpp @@ -307,18 +307,15 @@ int parse_args( } // Parse codec to AVCodec - enc_cfg.codec = AV_CODEC_ID_H264; + enc_cfg.codec = "libx264"; if (vm.count("codec")) { - video2x::fsutils::StringType codec_str = vm["codec"].as(); - const AVCodec* codec = - avcodec_find_encoder_by_name(wstring_to_u8string(codec_str).c_str()); - if (codec == nullptr) { - video2x::logger()->critical( - "Codec '{}' not found.", wstring_to_u8string(codec_str) - ); + std::string codec_str = + wstring_to_u8string(vm["codec"].as()); + if (avcodec_find_encoder_by_name(codec_str.c_str()) == nullptr) { + video2x::logger()->critical("Invalid encoder '{}'.", codec_str); return -1; } - enc_cfg.codec = codec->id; + enc_cfg.codec = codec_str; } // Parse copy streams flag @@ -348,7 +345,9 @@ int parse_args( if (eq_pos != video2x::fsutils::StringType::npos) { video2x::fsutils::StringType key = opt.substr(0, eq_pos); video2x::fsutils::StringType value = opt.substr(eq_pos + 1); - enc_cfg.extra_opts.push_back(std::make_pair(key, value)); + enc_cfg.extra_opts.push_back( + std::make_pair(wstring_to_u8string(key), wstring_to_u8string(value)) + ); } else { video2x::logger()->critical( "Invalid extra AVOption format: {}", wstring_to_u8string(opt)