avcodec/nvenc: add support for lookahead_level

This commit is contained in:
Timo Rothenpieler 2024-03-31 20:52:27 +02:00
parent 64e3fc9069
commit 77d23bcb1b
5 changed files with 64 additions and 2 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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 }
};

View File

@ -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 }
};

View File

@ -201,6 +201,15 @@ static const AVOption options[] = {
OFFSET(tf_level), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, .unit = "tf_level" },
{ "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 }
};