Skip to content

Commit fd15d5f

Browse files
committed
lavc opt setting simplified
use predefined error-checking function
1 parent f2884ae commit fd15d5f

File tree

1 file changed

+12
-37
lines changed

1 file changed

+12
-37
lines changed

src/video_compress/libavcodec.cpp

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ static enum AVPixelFormat get_first_matching_pix_fmt(list<enum AVPixelFormat>
718718
}
719719

720720
template<typename T>
721-
static inline void check_av_opt_set(void *priv_data, const char *key, T val, const char *desc = nullptr) {
721+
static inline bool check_av_opt_set(void *priv_data, const char *key, T val, const char *desc = nullptr) {
722722
int ret = 0;
723723
string val_str;
724724
if constexpr (std::is_same_v<T, int>) {
@@ -732,6 +732,7 @@ static inline void check_av_opt_set(void *priv_data, const char *key, T val, con
732732
string err = string(MOD_NAME) + "Unable to set " + (desc ? desc : key) + " to " + val_str;
733733
print_libav_error(LOG_LEVEL_WARNING, err.c_str(), ret);
734734
}
735+
return ret == 0;
735736
}
736737

737738
bool set_codec_ctx_params(struct state_video_compress_libav *s, AVPixelFormat pix_fmt, struct video_desc desc, codec_t ug_codec)
@@ -1826,19 +1827,13 @@ static void configure_nvenc(AVCodecContext *codec_ctx, struct setparam_param *pa
18261827

18271828
// important: if "tune" is not supported, then FALLBACK_NVENC_PRESET must be used (it is correlated). If unsupported preset
18281829
// were given, setting would succeed but would cause runtime errors.
1829-
if (int rc = av_opt_set(codec_ctx->priv_data, "tune", DEFAULT_NVENC_TUNE, 0)) {
1830-
array<char, LIBAV_ERRBUF_LEN> errbuf{};
1831-
av_strerror(rc, errbuf.data(), errbuf.size());
1832-
LOG(LOG_LEVEL_WARNING) << "[lavc] Cannot set NVENC tune to \"" << DEFAULT_NVENC_TUNE << "\" (" << errbuf.data() << "). Possibly old libavcodec or compiled with old NVIDIA NVENC headers.\n";
1830+
if (!check_av_opt_set<const char *>(codec_ctx->priv_data, "tune", DEFAULT_NVENC_TUNE, "NVENC tune")) {
1831+
LOG(LOG_LEVEL_WARNING) << MOD_NAME "Possibly old libavcodec or compiled with old NVIDIA NVENC headers.\n";
18331832
preset = FALLBACK_NVENC_PRESET;
18341833
}
18351834
if (!param->have_preset) {
1836-
if (int rc = av_opt_set(codec_ctx->priv_data, "preset", preset, 0)) {
1837-
array<char, LIBAV_ERRBUF_LEN> errbuf{};
1838-
av_strerror(rc, errbuf.data(), errbuf.size());
1839-
LOG(LOG_LEVEL_WARNING) << "[lavc] Cannot set NVENC preset to: " << preset << " (" << errbuf.data() << ").\n";
1840-
} else {
1841-
LOG(LOG_LEVEL_INFO) << "[lavc] Setting NVENC preset to " << preset << ".\n";
1835+
if (check_av_opt_set<const char *>(codec_ctx->priv_data, "preset", preset, "NVENC preset")) {
1836+
LOG(LOG_LEVEL_INFO) << MOD_NAME "Setting NVENC preset to " << preset << ".\n";
18421837
}
18431838
}
18441839

@@ -1848,34 +1843,14 @@ static void configure_nvenc(AVCodecContext *codec_ctx, struct setparam_param *pa
18481843
#else
18491844
if (param->periodic_intra == 1) {
18501845
#endif
1851-
if (int ret = av_opt_set(codec_ctx->priv_data, "intra-refresh", "1", 0) != 0) {
1852-
print_libav_error(LOG_LEVEL_WARNING, "[lavc] Unable to set Intra Refresh", ret);
1853-
}
1854-
}
1855-
1856-
int ret = av_opt_set(codec_ctx->priv_data, "rc", DEFAULT_NVENC_RC, 0);
1857-
if (ret != 0) { // older FFMPEG had only cbr
1858-
LOG(LOG_LEVEL_WARNING) << "[lavc] Cannot set RC " << DEFAULT_NVENC_RC << ".\n";
1846+
check_av_opt_set<int>(codec_ctx->priv_data, "intra-refresh", 1);
18591847
}
18601848

1861-
ret = av_opt_set(codec_ctx->priv_data, "spatial_aq", "0", 0);
1862-
if (ret != 0) {
1863-
log_msg(LOG_LEVEL_WARNING, "[lavc] Unable to unset spatial AQ.\n");
1864-
}
1865-
char gpu[3] = "";
1866-
snprintf(gpu, 2, "%d", cuda_devices[0]);
1867-
ret = av_opt_set(codec_ctx->priv_data, "gpu", gpu, 0);
1868-
if (ret != 0) {
1869-
log_msg(LOG_LEVEL_WARNING, "[lavc] Unable to set GPU.\n");
1870-
}
1871-
ret = av_opt_set(codec_ctx->priv_data, "delay", "2", 0);
1872-
if (ret != 0) {
1873-
log_msg(LOG_LEVEL_WARNING, "[lavc] Unable to set delay.\n");
1874-
}
1875-
ret = av_opt_set(codec_ctx->priv_data, "zerolatency", "1", 0);
1876-
if (ret != 0) {
1877-
log_msg(LOG_LEVEL_WARNING, "[lavc] Unable to set zero latency operation (no reordering delay).\n");
1878-
}
1849+
check_av_opt_set<const char *>(codec_ctx->priv_data, "rc", DEFAULT_NVENC_RC);
1850+
check_av_opt_set<int>(codec_ctx->priv_data, "spatial_aq", 0);
1851+
check_av_opt_set<int>(codec_ctx->priv_data, "gpu", cuda_devices[0]);
1852+
check_av_opt_set<int>(codec_ctx->priv_data, "delay", 2); // increases throughput 2x at expense of higher latency
1853+
check_av_opt_set<int>(codec_ctx->priv_data, "zerolatency", 1, "zero latency operation (no reordering delay)");
18791854
check_av_opt_set<const char *>(codec_ctx->priv_data, "b_ref_mode", "disabled", 0);
18801855
codec_ctx->rc_max_rate = codec_ctx->bit_rate;
18811856
codec_ctx->rc_buffer_size = codec_ctx->rc_max_rate / param->desc.fps;

0 commit comments

Comments
 (0)