diff options
author | Timo Rothenpieler <timo@rothenpieler.org> | 2024-03-31 20:52:27 +0200 |
---|---|---|
committer | Timo Rothenpieler <timo@rothenpieler.org> | 2024-03-31 20:58:27 +0200 |
commit | 77d23bcb1b91b7b38808c3532945def5aaed5cd9 (patch) | |
tree | b098af07f9dce6a73d4566fc947a8271b5a75b08 | |
parent | 64e3fc906971e18ec9e2810826ac9bb285ea08bb (diff) | |
download | ffmpeg-77d23bcb1b91b7b38808c3532945def5aaed5cd9.tar.gz |
avcodec/nvenc: add support for lookahead_level
-rw-r--r-- | libavcodec/nvenc.c | 37 | ||||
-rw-r--r-- | libavcodec/nvenc.h | 2 | ||||
-rw-r--r-- | libavcodec/nvenc_av1.c | 9 | ||||
-rw-r--r-- | libavcodec/nvenc_h264.c | 9 | ||||
-rw-r--r-- | libavcodec/nvenc_hevc.c | 9 |
5 files changed, 64 insertions, 2 deletions
diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c index 5afd7bf218..7f938a519f 100644 --- a/libavcodec/nvenc.c +++ b/libavcodec/nvenc.c @@ -602,6 +602,17 @@ static int nvenc_check_capabilities(AVCodecContext *avctx) } #endif +#ifdef NVENC_HAVE_LOOKAHEAD_LEVEL + ret = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_LOOKAHEAD_LEVEL); + if(ctx->rc_lookahead > 0 && ctx->lookahead_level > 0 && + ctx->lookahead_level != NV_ENC_LOOKAHEAD_LEVEL_AUTOSELECT && + ctx->lookahead_level > ret) + { + av_log(avctx, AV_LOG_WARNING, "Lookahead level not supported. Maximum level: %d\n", ret); + return AVERROR(ENOSYS); + } +#endif + ctx->support_dyn_bitrate = nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE); return 0; @@ -995,7 +1006,7 @@ static av_cold int nvenc_recalc_surfaces(AVCodecContext *avctx) return 0; } -static av_cold void nvenc_setup_rate_control(AVCodecContext *avctx) +static av_cold int nvenc_setup_rate_control(AVCodecContext *avctx) { NvencContext *ctx = avctx->priv_data; @@ -1124,6 +1135,24 @@ static av_cold void nvenc_setup_rate_control(AVCodecContext *avctx) if (ctx->encode_config.rcParams.lookaheadDepth < ctx->rc_lookahead) av_log(avctx, AV_LOG_WARNING, "Clipping lookahead depth to %d (from %d) due to lack of surfaces/delay", ctx->encode_config.rcParams.lookaheadDepth, ctx->rc_lookahead); + +#ifdef NVENC_HAVE_LOOKAHEAD_LEVEL + if (ctx->lookahead_level >= 0) { + switch (ctx->lookahead_level) { + case NV_ENC_LOOKAHEAD_LEVEL_0: + case NV_ENC_LOOKAHEAD_LEVEL_1: + case NV_ENC_LOOKAHEAD_LEVEL_2: + case NV_ENC_LOOKAHEAD_LEVEL_3: + case NV_ENC_LOOKAHEAD_LEVEL_AUTOSELECT: + break; + default: + av_log(avctx, AV_LOG_ERROR, "Invalid lookahead level.\n"); + return AVERROR(EINVAL); + } + + ctx->encode_config.rcParams.lookaheadLevel = ctx->lookahead_level; + } +#endif } } @@ -1151,6 +1180,8 @@ static av_cold void nvenc_setup_rate_control(AVCodecContext *avctx) ctx->encode_config.rcParams.vbvBufferSize = avctx->rc_buffer_size = 0; ctx->encode_config.rcParams.maxBitRate = avctx->rc_max_rate; } + + return 0; } static av_cold int nvenc_setup_h264_config(AVCodecContext *avctx) @@ -1675,7 +1706,9 @@ FF_ENABLE_DEPRECATION_WARNINGS nvenc_recalc_surfaces(avctx); - nvenc_setup_rate_control(avctx); + res = nvenc_setup_rate_control(avctx); + if (res < 0) + return res; if (avctx->flags & AV_CODEC_FLAG_INTERLACED_DCT) { ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD; diff --git a/libavcodec/nvenc.h b/libavcodec/nvenc.h index c320c2514f..30f9800d21 100644 --- a/libavcodec/nvenc.h +++ b/libavcodec/nvenc.h @@ -87,6 +87,7 @@ typedef void ID3D11Device; #if NVENCAPI_CHECK_VERSION(12, 2) #define NVENC_HAVE_NEW_BIT_DEPTH_API #define NVENC_HAVE_TEMPORAL_FILTER +#define NVENC_HAVE_LOOKAHEAD_LEVEL #endif typedef struct NvencSurface @@ -273,6 +274,7 @@ typedef struct NvencContext int max_slice_size; int rgb_mode; int tf_level; + int lookahead_level; } NvencContext; int ff_nvenc_encode_init(AVCodecContext *avctx); diff --git a/libavcodec/nvenc_av1.c b/libavcodec/nvenc_av1.c index c46cee9fac..d37ee07bff 100644 --- a/libavcodec/nvenc_av1.c +++ b/libavcodec/nvenc_av1.c @@ -149,6 +149,15 @@ static const AVOption options[] = { OFFSET(extra_sei), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, VE }, { "a53cc", "Use A53 Closed Captions (if available)", OFFSET(a53_cc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, VE }, { "s12m_tc", "Use timecode (if available)", OFFSET(s12m_tc), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, VE }, +#ifdef NVENC_HAVE_LOOKAHEAD_LEVEL + { "lookahead_level", "Specifies the lookahead level. Higher level may improve quality at the expense of performance.", + OFFSET(lookahead_level), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, NV_ENC_LOOKAHEAD_LEVEL_AUTOSELECT, VE, .unit = "lookahead_level" }, + { "auto", "", 0, AV_OPT_TYPE_CONST, { .i64 = NV_ENC_LOOKAHEAD_LEVEL_AUTOSELECT }, 0, 0, VE, .unit = "lookahead_level" }, + { "0", "", 0, AV_OPT_TYPE_CONST, { .i64 = NV_ENC_LOOKAHEAD_LEVEL_0 }, 0, 0, VE, .unit = "lookahead_level" }, + { "1", "", 0, AV_OPT_TYPE_CONST, { .i64 = NV_ENC_LOOKAHEAD_LEVEL_1 }, 0, 0, VE, .unit = "lookahead_level" }, + { "2", "", 0, AV_OPT_TYPE_CONST, { .i64 = NV_ENC_LOOKAHEAD_LEVEL_2 }, 0, 0, VE, .unit = "lookahead_level" }, + { "3", "", 0, AV_OPT_TYPE_CONST, { .i64 = NV_ENC_LOOKAHEAD_LEVEL_3 }, 0, 0, VE, .unit = "lookahead_level" }, +#endif { NULL } }; diff --git a/libavcodec/nvenc_h264.c b/libavcodec/nvenc_h264.c index a555252a53..4d6f968e0b 100644 --- a/libavcodec/nvenc_h264.c +++ b/libavcodec/nvenc_h264.c @@ -215,6 +215,15 @@ static const AVOption options[] = { OFFSET(max_slice_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE }, { "constrained-encoding", "Enable constrainedFrame encoding where each slice in the constrained picture is independent of other slices", OFFSET(constrained_encoding), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE }, +#ifdef NVENC_HAVE_LOOKAHEAD_LEVEL + { "lookahead_level", "Specifies the lookahead level. Higher level may improve quality at the expense of performance.", + OFFSET(lookahead_level), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, NV_ENC_LOOKAHEAD_LEVEL_AUTOSELECT, VE, .unit = "lookahead_level" }, + { "auto", "", 0, AV_OPT_TYPE_CONST, { .i64 = NV_ENC_LOOKAHEAD_LEVEL_AUTOSELECT }, 0, 0, VE, .unit = "lookahead_level" }, + { "0", "", 0, AV_OPT_TYPE_CONST, { .i64 = NV_ENC_LOOKAHEAD_LEVEL_0 }, 0, 0, VE, .unit = "lookahead_level" }, + { "1", "", 0, AV_OPT_TYPE_CONST, { .i64 = NV_ENC_LOOKAHEAD_LEVEL_1 }, 0, 0, VE, .unit = "lookahead_level" }, + { "2", "", 0, AV_OPT_TYPE_CONST, { .i64 = NV_ENC_LOOKAHEAD_LEVEL_2 }, 0, 0, VE, .unit = "lookahead_level" }, + { "3", "", 0, AV_OPT_TYPE_CONST, { .i64 = NV_ENC_LOOKAHEAD_LEVEL_3 }, 0, 0, VE, .unit = "lookahead_level" }, +#endif { NULL } }; diff --git a/libavcodec/nvenc_hevc.c b/libavcodec/nvenc_hevc.c index 8559aa6cfb..7509a93516 100644 --- a/libavcodec/nvenc_hevc.c +++ b/libavcodec/nvenc_hevc.c @@ -202,6 +202,15 @@ static const AVOption options[] = { { "0", "", 0, AV_OPT_TYPE_CONST, { .i64 = NV_ENC_TEMPORAL_FILTER_LEVEL_0 }, 0, 0, VE, .unit = "tf_level" }, { "4", "", 0, AV_OPT_TYPE_CONST, { .i64 = NV_ENC_TEMPORAL_FILTER_LEVEL_4 }, 0, 0, VE, .unit = "tf_level" }, #endif +#ifdef NVENC_HAVE_LOOKAHEAD_LEVEL + { "lookahead_level", "Specifies the lookahead level. Higher level may improve quality at the expense of performance.", + OFFSET(lookahead_level), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, NV_ENC_LOOKAHEAD_LEVEL_AUTOSELECT, VE, .unit = "lookahead_level" }, + { "auto", "", 0, AV_OPT_TYPE_CONST, { .i64 = NV_ENC_LOOKAHEAD_LEVEL_AUTOSELECT }, 0, 0, VE, .unit = "lookahead_level" }, + { "0", "", 0, AV_OPT_TYPE_CONST, { .i64 = NV_ENC_LOOKAHEAD_LEVEL_0 }, 0, 0, VE, .unit = "lookahead_level" }, + { "1", "", 0, AV_OPT_TYPE_CONST, { .i64 = NV_ENC_LOOKAHEAD_LEVEL_1 }, 0, 0, VE, .unit = "lookahead_level" }, + { "2", "", 0, AV_OPT_TYPE_CONST, { .i64 = NV_ENC_LOOKAHEAD_LEVEL_2 }, 0, 0, VE, .unit = "lookahead_level" }, + { "3", "", 0, AV_OPT_TYPE_CONST, { .i64 = NV_ENC_LOOKAHEAD_LEVEL_3 }, 0, 0, VE, .unit = "lookahead_level" }, +#endif { NULL } }; |