Skip to content

Commit 9477882

Browse files
committed
fix(encoder): fix a bug that causes the wrong encoder to be selected
Signed-off-by: k4yt3x <[email protected]>
1 parent eae89ce commit 9477882

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717

1818
- Improve the CMake optimization flags and option names.
1919

20+
### Fixed
21+
22+
- A bug that causes the wrong encoder to be selected.
23+
2024
## [6.3.1] - 2024-12-21
2125

2226
### Fixed

include/libvideo2x/encoder.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ extern "C" {
1010
#include <libavutil/pixdesc.h>
1111
}
1212

13-
#include "fsutils.h"
14-
1513
namespace video2x {
1614
namespace encoder {
1715

1816
// Encoder configurations
1917
struct EncoderConfig {
2018
// Non-AVCodecContext options
21-
AVCodecID codec = AV_CODEC_ID_NONE;
19+
std::string codec = "libx264";
2220
bool copy_streams = true;
2321

2422
// Basic video options
@@ -45,7 +43,7 @@ struct EncoderConfig {
4543
int delay = -1;
4644

4745
// Extra AVOptions
48-
std::vector<std::pair<fsutils::StringType, fsutils::StringType>> extra_opts;
46+
std::vector<std::pair<std::string, std::string>> extra_opts;
4947
};
5048

5149
class Encoder {

src/encoder.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,9 @@ int Encoder::init(
5353
}
5454

5555
// Find the encoder
56-
const AVCodec* encoder = avcodec_find_encoder(enc_cfg.codec);
56+
const AVCodec* encoder = avcodec_find_encoder_by_name(enc_cfg.codec.c_str());
5757
if (!encoder) {
58-
logger()->error(
59-
"Required video encoder not found for codec {}", avcodec_get_name(enc_cfg.codec)
60-
);
58+
logger()->error("Could not find encoder '{}'", enc_cfg.codec);
6159
return AVERROR_ENCODER_NOT_FOUND;
6260
}
6361

@@ -151,8 +149,8 @@ int Encoder::init(
151149

152150
// Set extra AVOptions
153151
for (const auto& [opt_name, opt_value] : enc_cfg.extra_opts) {
154-
std::string opt_name_str = fsutils::wstring_to_u8string(opt_name);
155-
std::string opt_value_str = fsutils::wstring_to_u8string(opt_value);
152+
std::string opt_name_str = opt_name;
153+
std::string opt_value_str = opt_value;
156154
logger()->debug("Setting encoder option '{}' to '{}'", opt_name_str, opt_value_str);
157155

158156
ret = av_opt_set(enc_ctx_->priv_data, opt_name_str.c_str(), opt_value_str.c_str(), 0);

tools/video2x/src/argparse.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -307,18 +307,15 @@ int parse_args(
307307
}
308308

309309
// Parse codec to AVCodec
310-
enc_cfg.codec = AV_CODEC_ID_H264;
310+
enc_cfg.codec = "libx264";
311311
if (vm.count("codec")) {
312-
video2x::fsutils::StringType codec_str = vm["codec"].as<video2x::fsutils::StringType>();
313-
const AVCodec* codec =
314-
avcodec_find_encoder_by_name(wstring_to_u8string(codec_str).c_str());
315-
if (codec == nullptr) {
316-
video2x::logger()->critical(
317-
"Codec '{}' not found.", wstring_to_u8string(codec_str)
318-
);
312+
std::string codec_str =
313+
wstring_to_u8string(vm["codec"].as<video2x::fsutils::StringType>());
314+
if (avcodec_find_encoder_by_name(codec_str.c_str()) == nullptr) {
315+
video2x::logger()->critical("Invalid encoder '{}'.", codec_str);
319316
return -1;
320317
}
321-
enc_cfg.codec = codec->id;
318+
enc_cfg.codec = codec_str;
322319
}
323320

324321
// Parse copy streams flag
@@ -348,7 +345,9 @@ int parse_args(
348345
if (eq_pos != video2x::fsutils::StringType::npos) {
349346
video2x::fsutils::StringType key = opt.substr(0, eq_pos);
350347
video2x::fsutils::StringType value = opt.substr(eq_pos + 1);
351-
enc_cfg.extra_opts.push_back(std::make_pair(key, value));
348+
enc_cfg.extra_opts.push_back(
349+
std::make_pair(wstring_to_u8string(key), wstring_to_u8string(value))
350+
);
352351
} else {
353352
video2x::logger()->critical(
354353
"Invalid extra AVOption format: {}", wstring_to_u8string(opt)

0 commit comments

Comments
 (0)