23 #include "config_components.h"
27 #if CONFIG_AV1_NVENC_ENCODER
45 #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, dl_fn->cuda_dl, x)
47 #define NVENC_CAP 0x30
49 #ifndef NVENC_NO_DEPRECATED_RC
50 #define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR || \
51 rc == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ || \
52 rc == NV_ENC_PARAMS_RC_CBR_HQ)
54 #define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR)
63 #ifdef NVENC_HAVE_422_SUPPORT
94 #define IS_10BIT(pix_fmt) (pix_fmt == AV_PIX_FMT_P010 || \
95 pix_fmt == AV_PIX_FMT_P016 || \
96 pix_fmt == AV_PIX_FMT_P210 || \
97 pix_fmt == AV_PIX_FMT_P216 || \
98 pix_fmt == AV_PIX_FMT_YUV444P16 || \
99 pix_fmt == AV_PIX_FMT_X2RGB10 || \
100 pix_fmt == AV_PIX_FMT_X2BGR10 || \
101 pix_fmt == AV_PIX_FMT_GBRP16)
103 #define IS_RGB(pix_fmt) (pix_fmt == AV_PIX_FMT_0RGB32 || \
104 pix_fmt == AV_PIX_FMT_RGB32 || \
105 pix_fmt == AV_PIX_FMT_0BGR32 || \
106 pix_fmt == AV_PIX_FMT_BGR32 || \
107 pix_fmt == AV_PIX_FMT_X2RGB10 || \
108 pix_fmt == AV_PIX_FMT_X2BGR10)
110 #define IS_YUV444(pix_fmt) (pix_fmt == AV_PIX_FMT_YUV444P || \
111 pix_fmt == AV_PIX_FMT_YUV444P16 || \
112 pix_fmt == AV_PIX_FMT_GBRP || \
113 pix_fmt == AV_PIX_FMT_GBRP16 || \
114 (ctx->rgb_mode == NVENC_RGB_MODE_444 && IS_RGB(pix_fmt)))
116 #define IS_YUV422(pix_fmt) (pix_fmt == AV_PIX_FMT_NV16 || \
117 pix_fmt == AV_PIX_FMT_P210 || \
118 pix_fmt == AV_PIX_FMT_P216)
120 #define IS_GBRP(pix_fmt) (pix_fmt == AV_PIX_FMT_GBRP || \
121 pix_fmt == AV_PIX_FMT_GBRP16)
123 static const struct {
128 { NV_ENC_SUCCESS, 0,
"success" },
129 { NV_ENC_ERR_NO_ENCODE_DEVICE,
AVERROR(ENOENT),
"no encode device" },
130 { NV_ENC_ERR_UNSUPPORTED_DEVICE,
AVERROR(ENOSYS),
"unsupported device" },
131 { NV_ENC_ERR_INVALID_ENCODERDEVICE,
AVERROR(EINVAL),
"invalid encoder device" },
132 { NV_ENC_ERR_INVALID_DEVICE,
AVERROR(EINVAL),
"invalid device" },
133 { NV_ENC_ERR_DEVICE_NOT_EXIST,
AVERROR(EIO),
"device does not exist" },
134 { NV_ENC_ERR_INVALID_PTR,
AVERROR(EFAULT),
"invalid ptr" },
135 { NV_ENC_ERR_INVALID_EVENT,
AVERROR(EINVAL),
"invalid event" },
136 { NV_ENC_ERR_INVALID_PARAM,
AVERROR(EINVAL),
"invalid param" },
137 { NV_ENC_ERR_INVALID_CALL,
AVERROR(EINVAL),
"invalid call" },
138 { NV_ENC_ERR_OUT_OF_MEMORY,
AVERROR(ENOMEM),
"out of memory" },
139 { NV_ENC_ERR_ENCODER_NOT_INITIALIZED,
AVERROR(EINVAL),
"encoder not initialized" },
140 { NV_ENC_ERR_UNSUPPORTED_PARAM,
AVERROR(ENOSYS),
"unsupported param" },
141 { NV_ENC_ERR_LOCK_BUSY,
AVERROR(EAGAIN),
"lock busy" },
143 { NV_ENC_ERR_INVALID_VERSION,
AVERROR(EINVAL),
"invalid version" },
144 { NV_ENC_ERR_MAP_FAILED,
AVERROR(EIO),
"map failed" },
145 { NV_ENC_ERR_NEED_MORE_INPUT,
AVERROR(EAGAIN),
"need more input" },
146 { NV_ENC_ERR_ENCODER_BUSY,
AVERROR(EAGAIN),
"encoder busy" },
147 { NV_ENC_ERR_EVENT_NOT_REGISTERD,
AVERROR(EBADF),
"event not registered" },
149 { NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY,
AVERROR(EINVAL),
"incompatible client key" },
150 { NV_ENC_ERR_UNIMPLEMENTED,
AVERROR(ENOSYS),
"unimplemented" },
151 { NV_ENC_ERR_RESOURCE_REGISTER_FAILED,
AVERROR(EIO),
"resource register failed" },
152 { NV_ENC_ERR_RESOURCE_NOT_REGISTERED,
AVERROR(EBADF),
"resource not registered" },
153 { NV_ENC_ERR_RESOURCE_NOT_MAPPED,
AVERROR(EBADF),
"resource not mapped" },
167 *
desc =
"unknown error";
172 const char *error_string)
175 const char *details =
"(no details)";
178 #ifdef NVENC_HAVE_GETLASTERRORSTRING
180 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
182 if (p_nvenc &&
ctx->nvencoder)
183 details = p_nvenc->nvEncGetLastErrorString(
ctx->nvencoder);
196 #define PRESET_ALIAS(alias, name, ...) \
197 [PRESET_ ## alias] = { NV_ENC_PRESET_ ## name ## _GUID, __VA_ARGS__ }
199 #define PRESET(name, ...) PRESET_ALIAS(name, name, __VA_ARGS__)
204 #ifdef NVENC_HAVE_NEW_PRESETS
243 ctx->init_encode_params.presetGUID = t->
guid;
246 #ifdef NVENC_HAVE_NEW_PRESETS
247 if (
ctx->tuning_info == NV_ENC_TUNING_INFO_LOSSLESS)
257 #if NVENCAPI_CHECK_VERSION(13, 1)
258 const char *minver =
"(unknown)";
259 #elif NVENCAPI_CHECK_VERSION(13, 0)
260 const char *minver =
"570.0";
261 #elif NVENCAPI_CHECK_VERSION(12, 2)
262 # if defined(_WIN32) || defined(__CYGWIN__)
263 const char *minver =
"551.76";
265 const char *minver =
"550.54.14";
267 #elif NVENCAPI_CHECK_VERSION(12, 1)
268 # if defined(_WIN32) || defined(__CYGWIN__)
269 const char *minver =
"531.61";
271 const char *minver =
"530.41.03";
273 #elif NVENCAPI_CHECK_VERSION(12, 0)
274 # if defined(_WIN32) || defined(__CYGWIN__)
275 const char *minver =
"522.25";
277 const char *minver =
"520.56.06";
279 #elif NVENCAPI_CHECK_VERSION(11, 1)
280 # if defined(_WIN32) || defined(__CYGWIN__)
281 const char *minver =
"471.41";
283 const char *minver =
"470.57.02";
285 #elif NVENCAPI_CHECK_VERSION(11, 0)
286 # if defined(_WIN32) || defined(__CYGWIN__)
287 const char *minver =
"456.71";
289 const char *minver =
"455.28";
291 #elif NVENCAPI_CHECK_VERSION(10, 0)
292 # if defined(_WIN32) || defined(__CYGWIN__)
293 const char *minver =
"450.51";
295 const char *minver =
"445.87";
297 #elif NVENCAPI_CHECK_VERSION(9, 1)
298 # if defined(_WIN32) || defined(__CYGWIN__)
299 const char *minver =
"436.15";
301 const char *minver =
"435.21";
303 #elif NVENCAPI_CHECK_VERSION(9, 0)
304 # if defined(_WIN32) || defined(__CYGWIN__)
305 const char *minver =
"418.81";
307 const char *minver =
"418.30";
309 #elif NVENCAPI_CHECK_VERSION(8, 2)
310 # if defined(_WIN32) || defined(__CYGWIN__)
311 const char *minver =
"397.93";
313 const char *minver =
"396.24";
315 #elif NVENCAPI_CHECK_VERSION(8, 1)
316 # if defined(_WIN32) || defined(__CYGWIN__)
317 const char *minver =
"390.77";
319 const char *minver =
"390.25";
322 # if defined(_WIN32) || defined(__CYGWIN__)
323 const char *minver =
"378.66";
325 const char *minver =
"378.13";
328 av_log(avctx,
level,
"The minimum required Nvidia driver for nvenc is %s or newer\n", minver);
336 uint32_t nvenc_max_ver;
339 ret = cuda_load_functions(&dl_fn->
cuda_dl, avctx);
343 ret = nvenc_load_functions(&dl_fn->
nvenc_dl, avctx);
349 err = dl_fn->
nvenc_dl->NvEncodeAPIGetMaxSupportedVersion(&nvenc_max_ver);
350 if (err != NV_ENC_SUCCESS)
355 if ((NVENCAPI_MAJOR_VERSION << 4 | NVENCAPI_MINOR_VERSION) > nvenc_max_ver) {
356 av_log(avctx,
AV_LOG_ERROR,
"Driver does not support the required nvenc API version. "
357 "Required: %d.%d Found: %d.%d\n",
358 NVENCAPI_MAJOR_VERSION, NVENCAPI_MINOR_VERSION,
359 nvenc_max_ver >> 4, nvenc_max_ver & 0
xf);
364 dl_fn->
nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
367 if (err != NV_ENC_SUCCESS)
380 if (
ctx->d3d11_device)
392 if (
ctx->d3d11_device)
400 NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = { 0 };
402 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
405 params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
406 params.apiVersion = NVENCAPI_VERSION;
407 if (
ctx->d3d11_device) {
408 params.device =
ctx->d3d11_device;
409 params.deviceType = NV_ENC_DEVICE_TYPE_DIRECTX;
411 params.device =
ctx->cu_context;
412 params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
415 ret = p_nvenc->nvEncOpenEncodeSessionEx(¶ms, &
ctx->nvencoder);
416 if (
ret != NV_ENC_SUCCESS) {
427 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
428 int i,
ret, count = 0;
431 ret = p_nvenc->nvEncGetEncodeGUIDCount(
ctx->nvencoder, &count);
433 if (
ret != NV_ENC_SUCCESS || !count)
440 ret = p_nvenc->nvEncGetEncodeGUIDs(
ctx->nvencoder, guids, count, &count);
441 if (
ret != NV_ENC_SUCCESS) {
447 for (
i = 0;
i < count;
i++) {
448 if (!memcmp(&guids[
i], &
ctx->init_encode_params.encodeGUID,
sizeof(*guids))) {
463 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
464 NV_ENC_CAPS_PARAM params = { 0 };
467 params.version = NV_ENC_CAPS_PARAM_VER;
468 params.capsToQuery = cap;
470 ret = p_nvenc->nvEncGetEncodeCaps(
ctx->nvencoder,
ctx->init_encode_params.encodeGUID, ¶ms, &
val);
472 if (
ret == NV_ENC_SUCCESS)
494 #ifdef NVENC_HAVE_422_SUPPORT
511 if (ret < avctx->
width) {
518 if (ret < avctx->
height) {
525 if (ret < avctx->max_b_frames) {
535 "Interlaced encoding is not supported. Supported level: %d\n",
547 if (
ctx->rc_lookahead > 0 &&
ret <= 0) {
553 if (
ctx->temporal_aq > 0 &&
ret <= 0) {
559 if (
ctx->weighted_pred > 0 &&
ret <= 0) {
565 if (
ctx->coder == NV_ENC_H264_ENTROPY_CODING_MODE_CABAC &&
ret <= 0) {
570 #ifdef NVENC_HAVE_BFRAME_REF_MODE
571 tmp = (
ctx->b_ref_mode >= 0) ?
ctx->b_ref_mode : NV_ENC_BFRAME_REF_MODE_DISABLED;
573 if (
tmp == NV_ENC_BFRAME_REF_MODE_EACH &&
ret != 1 &&
ret != 3) {
576 }
else if (
tmp != NV_ENC_BFRAME_REF_MODE_DISABLED &&
ret == 0) {
581 tmp = (
ctx->b_ref_mode >= 0) ?
ctx->b_ref_mode : 0;
588 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
590 if(avctx->
refs != NV_ENC_NUM_REF_FRAMES_AUTOSELECT &&
ret <= 0) {
595 if(avctx->
refs != 0) {
601 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
603 if(
ctx->single_slice_intra_refresh &&
ret <= 0) {
608 if(
ctx->single_slice_intra_refresh) {
615 if((
ctx->intra_refresh ||
ctx->single_slice_intra_refresh) &&
ret <= 0) {
620 #ifndef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
628 if(
ctx->constrained_encoding &&
ret <= 0) {
633 #if defined(NVENC_HAVE_TEMPORAL_FILTER) || defined(NVENC_HAVE_H264_AND_AV1_TEMPORAL_FILTER)
635 if(
ctx->tf_level > 0 &&
ret <= 0) {
641 #ifdef NVENC_HAVE_LOOKAHEAD_LEVEL
643 if(
ctx->rc_lookahead > 0 &&
ctx->lookahead_level > 0 &&
644 ctx->lookahead_level != NV_ENC_LOOKAHEAD_LEVEL_AUTOSELECT &&
645 ctx->lookahead_level >
ret)
652 #ifdef NVENC_HAVE_UNIDIR_B
654 if(
ctx->unidir_b &&
ret <= 0) {
660 ctx->support_dyn_bitrate =
nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE);
669 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
670 char name[128] = { 0};
671 int major, minor,
ret;
690 av_log(avctx, loglevel,
"[ GPU #%d - < %s > has Compute SM %d.%d ]\n", idx,
name, major, minor);
691 if (((major << 4) | minor) <
NVENC_CAP) {
692 av_log(avctx, loglevel,
"does not support NVENC\n");
703 ctx->cu_context =
ctx->cu_context_internal;
715 av_log(avctx, loglevel,
"supports NVENC\n");
726 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
734 ctx->cu_context_internal =
NULL;
747 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_H264_GUID;
750 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_HEVC_GUID;
752 #if CONFIG_AV1_NVENC_ENCODER
754 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_AV1_GUID;
764 av_log(avctx,
AV_LOG_WARNING,
"The selected preset is deprecated. Use p1 to p7 + -tune or fast/medium/slow.\n");
788 cuda_device_hwctx = hwdev_ctx->
hwctx;
791 d3d11_device_hwctx = hwdev_ctx->
hwctx;
799 if (cuda_device_hwctx) {
801 ctx->cu_stream = cuda_device_hwctx->
stream;
804 else if (d3d11_device_hwctx) {
805 ctx->d3d11_device = d3d11_device_hwctx->
device;
806 ID3D11Device_AddRef(
ctx->d3d11_device);
816 av_log(avctx,
AV_LOG_FATAL,
"Provided device doesn't support required NVENC features\n");
820 int i, nb_devices = 0;
836 for (
i = 0;
i < nb_devices; ++
i) {
849 av_log(avctx,
AV_LOG_FATAL,
"Requested GPU %d, but only %d GPUs are available!\n",
ctx->device, nb_devices);
859 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
860 #if CONFIG_AV1_NVENC_ENCODER
866 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
868 if (
ctx->init_qp_p >= 0) {
869 rc->constQP.qpInterP =
ctx->init_qp_p;
870 if (
ctx->init_qp_i >= 0 &&
ctx->init_qp_b >= 0) {
871 rc->constQP.qpIntra =
ctx->init_qp_i;
872 rc->constQP.qpInterB =
ctx->init_qp_b;
876 rc->constQP.qpInterB =
av_clip(
879 rc->constQP.qpIntra = rc->constQP.qpInterP;
880 rc->constQP.qpInterB = rc->constQP.qpInterP;
882 }
else if (
ctx->cqp >= 0) {
883 rc->constQP.qpInterP = rc->constQP.qpInterB = rc->constQP.qpIntra =
ctx->cqp;
897 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
899 #if CONFIG_AV1_NVENC_ENCODER
905 if (avctx->
qmin >= 0 || avctx->
qmax >= 0)
906 av_log(avctx,
AV_LOG_WARNING,
"Passing qmin/qmax via global AVCodecContext options. Use encoder options instead.\n");
908 if (avctx->
qmin >= 0 &&
ctx->qmin < 0)
910 if (avctx->
qmax >= 0 &&
ctx->qmax < 0)
915 if (
ctx->qmin >= 0 &&
ctx->qmax >= 0) {
919 rc->minQP.qpInterB =
ctx->qmin;
920 rc->minQP.qpInterP =
ctx->qmin;
921 rc->minQP.qpIntra =
ctx->qmin;
923 rc->maxQP.qpInterB =
ctx->qmax;
924 rc->maxQP.qpInterP =
ctx->qmax;
925 rc->maxQP.qpIntra =
ctx->qmax;
927 qp_inter_p = (
ctx->qmax + 3 *
ctx->qmin) / 4;
928 }
else if (
ctx->qmin >= 0) {
931 rc->minQP.qpInterB =
ctx->qmin;
932 rc->minQP.qpInterP =
ctx->qmin;
933 rc->minQP.qpIntra =
ctx->qmin;
935 qp_inter_p =
ctx->qmin;
940 rc->enableInitialRCQP = 1;
942 if (
ctx->init_qp_p < 0) {
943 rc->initialRCQP.qpInterP = qp_inter_p;
945 rc->initialRCQP.qpInterP =
ctx->init_qp_p;
948 if (
ctx->init_qp_i < 0) {
950 rc->initialRCQP.qpIntra =
av_clip(
953 rc->initialRCQP.qpIntra = rc->initialRCQP.qpInterP;
956 rc->initialRCQP.qpIntra =
ctx->init_qp_i;
959 if (
ctx->init_qp_b < 0) {
961 rc->initialRCQP.qpInterB =
av_clip(
964 rc->initialRCQP.qpInterB = rc->initialRCQP.qpInterP;
967 rc->initialRCQP.qpInterB =
ctx->init_qp_b;
974 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
976 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
977 rc->constQP.qpInterB = 0;
978 rc->constQP.qpInterP = 0;
979 rc->constQP.qpIntra = 0;
988 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
991 case NV_ENC_PARAMS_RC_CONSTQP:
994 #ifndef NVENC_NO_DEPRECATED_RC
995 case NV_ENC_PARAMS_RC_VBR_MINQP:
996 if (avctx->
qmin < 0 &&
ctx->qmin < 0) {
998 "The variable bitrate rate-control requires "
999 "the 'qmin' option set.\n");
1004 case NV_ENC_PARAMS_RC_VBR_HQ:
1006 case NV_ENC_PARAMS_RC_VBR:
1009 case NV_ENC_PARAMS_RC_CBR:
1010 #ifndef NVENC_NO_DEPRECATED_RC
1011 case NV_ENC_PARAMS_RC_CBR_HQ:
1012 case NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ:
1017 rc->rateControlMode =
ctx->rc;
1026 int nb_surfaces =
FFMAX(4,
ctx->encode_config.frameIntervalP * 2 * 2);
1029 if (
ctx->rc_lookahead > 0) {
1032 nb_surfaces =
FFMAX(1,
FFMAX(nb_surfaces,
ctx->rc_lookahead +
ctx->encode_config.frameIntervalP + 1 + 4));
1033 if (nb_surfaces >
ctx->nb_surfaces &&
ctx->nb_surfaces > 0)
1036 "Defined rc_lookahead requires more surfaces, "
1037 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
1039 ctx->nb_surfaces =
FFMAX(nb_surfaces,
ctx->nb_surfaces);
1041 if (
ctx->encode_config.frameIntervalP > 1 &&
ctx->nb_surfaces < nb_surfaces &&
ctx->nb_surfaces > 0)
1044 "Defined b-frame requires more surfaces, "
1045 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
1046 ctx->nb_surfaces =
FFMAX(
ctx->nb_surfaces, nb_surfaces);
1048 else if (
ctx->nb_surfaces <= 0)
1049 ctx->nb_surfaces = nb_surfaces;
1054 ctx->async_depth =
FFMIN(
ctx->async_depth,
ctx->nb_surfaces - 1);
1058 ctx->frame_data_array_nb =
FFMAX(
ctx->nb_surfaces,
ctx->nb_surfaces +
ctx->encode_config.frameIntervalP - 1);
1068 av_log(avctx,
AV_LOG_WARNING,
"Using global_quality with nvenc is deprecated. Use qp instead.\n");
1074 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate;
1075 }
else if (
ctx->encode_config.rcParams.averageBitRate > 0) {
1076 ctx->encode_config.rcParams.maxBitRate =
ctx->encode_config.rcParams.averageBitRate;
1082 #ifdef NVENC_HAVE_MULTIPASS
1083 ctx->encode_config.rcParams.multiPass =
ctx->multipass;
1086 ctx->encode_config.rcParams.multiPass = NV_ENC_MULTI_PASS_DISABLED;
1088 ctx->encode_config.rcParams.multiPass = NV_ENC_TWO_PASS_FULL_RESOLUTION;
1092 ctx->rc = NV_ENC_PARAMS_RC_CBR;
1093 }
else if (
ctx->cqp >= 0) {
1094 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
1095 }
else if (
ctx->quality >= 0.0f) {
1096 ctx->rc = NV_ENC_PARAMS_RC_VBR;
1106 if (
ctx->twopass < 0)
1111 ctx->rc = NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ;
1113 ctx->rc = NV_ENC_PARAMS_RC_CBR;
1115 }
else if (
ctx->cqp >= 0) {
1116 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
1117 }
else if (
ctx->twopass) {
1118 ctx->rc = NV_ENC_PARAMS_RC_VBR_HQ;
1119 }
else if ((avctx->
qmin >= 0 && avctx->
qmax >= 0) ||
1120 (
ctx->qmin >= 0 &&
ctx->qmax >= 0)) {
1121 ctx->rc = NV_ENC_PARAMS_RC_VBR_MINQP;
1133 #ifdef NVENC_HAVE_QP_CHROMA_OFFSETS
1134 ctx->encode_config.rcParams.cbQPIndexOffset =
ctx->qp_cb_offset;
1135 ctx->encode_config.rcParams.crQPIndexOffset =
ctx->qp_cr_offset;
1137 if (
ctx->qp_cb_offset ||
ctx->qp_cr_offset)
1138 av_log(avctx,
AV_LOG_WARNING,
"Failed setting QP CB/CR offsets, SDK 11.1 or greater required at compile time.\n");
1141 #ifdef NVENC_HAVE_LDKFS
1143 ctx->encode_config.rcParams.lowDelayKeyFrameScale =
ctx->ldkfs;
1148 }
else if (
ctx->rc >= 0) {
1151 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
1157 }
else if (
ctx->encode_config.rcParams.averageBitRate > 0) {
1158 avctx->
rc_buffer_size =
ctx->encode_config.rcParams.vbvBufferSize = 2 *
ctx->encode_config.rcParams.averageBitRate;
1162 ctx->encode_config.rcParams.enableAQ = 1;
1163 ctx->encode_config.rcParams.aqStrength =
ctx->aq_strength;
1167 if (
ctx->temporal_aq) {
1168 ctx->encode_config.rcParams.enableTemporalAQ = 1;
1172 if (
ctx->rc_lookahead > 0) {
1173 int lkd_bound =
FFMIN(
ctx->nb_surfaces,
ctx->async_depth) -
1174 ctx->encode_config.frameIntervalP - 4;
1176 if (lkd_bound < 0) {
1177 ctx->encode_config.rcParams.enableLookahead = 0;
1179 "Lookahead not enabled. Increase buffer delay (-delay).\n");
1181 ctx->encode_config.rcParams.enableLookahead = 1;
1182 ctx->encode_config.rcParams.lookaheadDepth =
av_clip(
ctx->rc_lookahead, 0, lkd_bound);
1183 ctx->encode_config.rcParams.disableIadapt =
ctx->no_scenecut;
1184 ctx->encode_config.rcParams.disableBadapt = !
ctx->b_adapt;
1186 "Lookahead enabled: depth %d, scenecut %s, B-adapt %s.\n",
1187 ctx->encode_config.rcParams.lookaheadDepth,
1188 ctx->encode_config.rcParams.disableIadapt ?
"disabled" :
"enabled",
1189 ctx->encode_config.rcParams.disableBadapt ?
"disabled" :
"enabled");
1190 if (
ctx->encode_config.rcParams.lookaheadDepth <
ctx->rc_lookahead)
1191 av_log(avctx,
AV_LOG_WARNING,
"Clipping lookahead depth to %d (from %d) due to lack of surfaces/delay",
1192 ctx->encode_config.rcParams.lookaheadDepth,
ctx->rc_lookahead);
1194 #ifdef NVENC_HAVE_LOOKAHEAD_LEVEL
1195 if (
ctx->lookahead_level >= 0) {
1196 switch (
ctx->lookahead_level) {
1197 case NV_ENC_LOOKAHEAD_LEVEL_0:
1198 case NV_ENC_LOOKAHEAD_LEVEL_1:
1199 case NV_ENC_LOOKAHEAD_LEVEL_2:
1200 case NV_ENC_LOOKAHEAD_LEVEL_3:
1201 case NV_ENC_LOOKAHEAD_LEVEL_AUTOSELECT:
1208 ctx->encode_config.rcParams.lookaheadLevel =
ctx->lookahead_level;
1214 if (
ctx->strict_gop) {
1215 ctx->encode_config.rcParams.strictGOPTarget = 1;
1220 ctx->encode_config.rcParams.enableNonRefP = 1;
1222 if (
ctx->zerolatency)
1223 ctx->encode_config.rcParams.zeroReorderDelay = 1;
1227 int tmp_quality = (int)(
ctx->quality * 256.0f);
1228 ctx->encode_config.rcParams.targetQuality = (uint8_t)(tmp_quality >> 8);
1229 ctx->encode_config.rcParams.targetQualityLSB = (uint8_t)(tmp_quality & 0xff);
1234 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate = 0;
1245 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1246 NV_ENC_CONFIG_H264 *h264 = &cc->encodeCodecConfig.h264Config;
1247 NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui = &h264->h264VUIParameters;
1254 vui->transferCharacteristics = avctx->
color_trc;
1255 vui->videoFullRangeFlag = 0;
1259 vui->transferCharacteristics = avctx->
color_trc;
1264 vui->colourDescriptionPresentFlag =
1265 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1267 vui->videoSignalTypePresentFlag =
1268 (vui->colourDescriptionPresentFlag
1269 || vui->videoFormat != 5
1270 || vui->videoFullRangeFlag != 0);
1272 if (
ctx->max_slice_size > 0) {
1273 h264->sliceMode = 1;
1274 h264->sliceModeData =
ctx->max_slice_size;
1276 h264->sliceMode = 3;
1277 h264->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1280 if (
ctx->intra_refresh) {
1281 h264->enableIntraRefresh = 1;
1282 h264->intraRefreshPeriod = cc->gopLength;
1283 h264->intraRefreshCnt = cc->gopLength - 1;
1284 cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1285 h264->outputRecoveryPointSEI = 1;
1286 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1287 h264->singleSliceIntraRefresh =
ctx->single_slice_intra_refresh;
1291 if (
ctx->constrained_encoding)
1292 h264->enableConstrainedEncoding = 1;
1296 h264->outputAUD =
ctx->aud;
1298 if (
ctx->dpb_size >= 0) {
1300 h264->maxNumRefFrames =
ctx->dpb_size;
1303 h264->idrPeriod = cc->gopLength;
1305 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1306 h264->outputBufferingPeriodSEI = 1;
1309 h264->outputPictureTimingSEI = 1;
1311 #ifndef NVENC_NO_DEPRECATED_RC
1312 if (cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ ||
1313 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_HQ ||
1314 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_VBR_HQ) {
1315 h264->adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
1316 h264->fmoMode = NV_ENC_H264_FMO_DISABLE;
1321 h264->qpPrimeYZeroTransformBypassFlag = 1;
1323 switch(
ctx->profile) {
1325 cc->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
1329 cc->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
1333 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
1336 #ifdef NVENC_HAVE_H264_10BIT_SUPPORT
1337 case NV_ENC_H264_PROFILE_HIGH_10:
1338 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_10_GUID;
1342 #ifdef NVENC_HAVE_422_SUPPORT
1343 case NV_ENC_H264_PROFILE_HIGH_422:
1344 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_422_GUID;
1349 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1355 #ifdef NVENC_HAVE_H264_10BIT_SUPPORT
1358 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_10_GUID;
1365 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1369 #ifdef NVENC_HAVE_422_SUPPORT
1372 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_422_GUID;
1381 h264->level =
ctx->level;
1383 #ifdef NVENC_HAVE_NEW_BIT_DEPTH_API
1384 h264->inputBitDepth =
IS_10BIT(
ctx->data_pix_fmt) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1385 h264->outputBitDepth = (
IS_10BIT(
ctx->data_pix_fmt) ||
ctx->highbitdepth) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1388 if (
ctx->coder >= 0)
1389 h264->entropyCodingMode =
ctx->coder;
1391 #ifdef NVENC_HAVE_BFRAME_REF_MODE
1392 if (
ctx->b_ref_mode >= 0)
1393 h264->useBFramesAsRef =
ctx->b_ref_mode;
1396 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1397 h264->numRefL0 = avctx->
refs;
1398 h264->numRefL1 = avctx->
refs;
1401 #ifdef NVENC_HAVE_H264_AND_AV1_TEMPORAL_FILTER
1402 if (
ctx->tf_level >= 0) {
1403 h264->tfLevel =
ctx->tf_level;
1405 switch (
ctx->tf_level)
1407 case NV_ENC_TEMPORAL_FILTER_LEVEL_0:
1408 case NV_ENC_TEMPORAL_FILTER_LEVEL_4:
1415 if (
ctx->encode_config.frameIntervalP < 5)
1426 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1427 NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig;
1428 NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui = &hevc->hevcVUIParameters;
1435 vui->transferCharacteristics = avctx->
color_trc;
1436 vui->videoFullRangeFlag = 0;
1440 vui->transferCharacteristics = avctx->
color_trc;
1445 vui->colourDescriptionPresentFlag =
1446 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1448 vui->videoSignalTypePresentFlag =
1449 (vui->colourDescriptionPresentFlag
1450 || vui->videoFormat != 5
1451 || vui->videoFullRangeFlag != 0);
1453 if (
ctx->max_slice_size > 0) {
1454 hevc->sliceMode = 1;
1455 hevc->sliceModeData =
ctx->max_slice_size;
1457 hevc->sliceMode = 3;
1458 hevc->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1461 if (
ctx->intra_refresh) {
1462 hevc->enableIntraRefresh = 1;
1463 hevc->intraRefreshPeriod = cc->gopLength;
1464 hevc->intraRefreshCnt = cc->gopLength - 1;
1465 cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1466 #ifdef NVENC_HAVE_HEVC_OUTPUT_RECOVERY_POINT_SEI
1467 hevc->outputRecoveryPointSEI = 1;
1469 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1470 hevc->singleSliceIntraRefresh =
ctx->single_slice_intra_refresh;
1474 #ifdef NVENC_HAVE_HEVC_AND_AV1_MASTERING_METADATA
1483 #ifdef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
1484 if (
ctx->constrained_encoding)
1485 hevc->enableConstrainedEncoding = 1;
1490 hevc->outputAUD =
ctx->aud;
1492 if (
ctx->dpb_size >= 0) {
1494 hevc->maxNumRefFramesInDPB =
ctx->dpb_size;
1497 hevc->idrPeriod = cc->gopLength;
1499 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1500 hevc->outputBufferingPeriodSEI = 1;
1503 hevc->outputPictureTimingSEI = 1;
1505 switch (
ctx->profile) {
1507 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
1511 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1515 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1522 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1528 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1534 #ifdef NVENC_HAVE_NEW_BIT_DEPTH_API
1535 hevc->inputBitDepth =
IS_10BIT(
ctx->data_pix_fmt) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1536 hevc->outputBitDepth = (
IS_10BIT(
ctx->data_pix_fmt) ||
ctx->highbitdepth) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1538 hevc->pixelBitDepthMinus8 =
IS_10BIT(
ctx->data_pix_fmt) ? 2 : 0;
1541 hevc->level =
ctx->level;
1543 hevc->tier =
ctx->tier;
1545 #ifdef NVENC_HAVE_HEVC_BFRAME_REF_MODE
1546 if (
ctx->b_ref_mode >= 0)
1547 hevc->useBFramesAsRef =
ctx->b_ref_mode;
1550 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1551 hevc->numRefL0 = avctx->
refs;
1552 hevc->numRefL1 = avctx->
refs;
1555 #ifdef NVENC_HAVE_TEMPORAL_FILTER
1556 if (
ctx->tf_level >= 0) {
1557 hevc->tfLevel =
ctx->tf_level;
1559 switch (
ctx->tf_level)
1561 case NV_ENC_TEMPORAL_FILTER_LEVEL_0:
1562 case NV_ENC_TEMPORAL_FILTER_LEVEL_4:
1569 if (
ctx->encode_config.frameIntervalP < 5)
1577 #if CONFIG_AV1_NVENC_ENCODER
1581 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1582 NV_ENC_CONFIG_AV1 *av1 = &cc->encodeCodecConfig.av1Config;
1589 av1->transferCharacteristics = avctx->
color_trc;
1590 av1->colorRange = 0;
1594 av1->transferCharacteristics = avctx->
color_trc;
1600 av_log(avctx,
AV_LOG_ERROR,
"AV1 High Profile not supported, required for 4:4:4 encoding\n");
1603 cc->profileGUID = NV_ENC_AV1_PROFILE_MAIN_GUID;
1607 if (
ctx->dpb_size >= 0) {
1609 av1->maxNumRefFramesInDPB =
ctx->dpb_size;
1612 if (
ctx->intra_refresh) {
1613 av1->enableIntraRefresh = 1;
1614 av1->intraRefreshPeriod = cc->gopLength;
1615 av1->intraRefreshCnt = cc->gopLength - 1;
1616 cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1619 av1->idrPeriod = cc->gopLength;
1621 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1622 av1->enableBitstreamPadding = 1;
1625 if (
ctx->tile_cols >= 0)
1626 av1->numTileColumns =
ctx->tile_cols;
1627 if (
ctx->tile_rows >= 0)
1628 av1->numTileRows =
ctx->tile_rows;
1630 av1->outputAnnexBFormat = 0;
1632 av1->level =
ctx->level;
1633 av1->tier =
ctx->tier;
1635 av1->enableTimingInfo =
ctx->timing_info;
1638 av1->disableSeqHdr = 0;
1639 av1->repeatSeqHdr = 1;
1641 av1->chromaFormatIDC =
IS_YUV444(
ctx->data_pix_fmt) ? 3 : 1;
1643 #ifdef NVENC_HAVE_NEW_BIT_DEPTH_API
1644 av1->inputBitDepth =
IS_10BIT(
ctx->data_pix_fmt) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1645 av1->outputBitDepth = (
IS_10BIT(
ctx->data_pix_fmt) ||
ctx->highbitdepth) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1647 av1->inputPixelBitDepthMinus8 =
IS_10BIT(
ctx->data_pix_fmt) ? 2 : 0;
1648 av1->pixelBitDepthMinus8 = (
IS_10BIT(
ctx->data_pix_fmt) ||
ctx->highbitdepth) ? 2 : 0;
1651 #ifdef NVENC_HAVE_HEVC_AND_AV1_MASTERING_METADATA
1660 if (
ctx->b_ref_mode >= 0)
1661 av1->useBFramesAsRef =
ctx->b_ref_mode;
1663 av1->numFwdRefs = avctx->
refs;
1664 av1->numBwdRefs = avctx->
refs;
1666 #ifdef NVENC_HAVE_H264_AND_AV1_TEMPORAL_FILTER
1667 if (
ctx->tf_level >= 0) {
1668 av1->tfLevel =
ctx->tf_level;
1670 switch (
ctx->tf_level)
1672 case NV_ENC_TEMPORAL_FILTER_LEVEL_0:
1673 case NV_ENC_TEMPORAL_FILTER_LEVEL_4:
1680 if (
ctx->encode_config.frameIntervalP < 5)
1696 #if CONFIG_AV1_NVENC_ENCODER
1698 return nvenc_setup_av1_config(avctx);
1712 #if CONFIG_AV1_NVENC_ENCODER
1743 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1745 NV_ENC_PRESET_CONFIG preset_config = { 0 };
1746 NVENCSTATUS nv_status = NV_ENC_SUCCESS;
1751 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1752 ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
1754 ctx->init_encode_params.encodeHeight = avctx->
height;
1755 ctx->init_encode_params.encodeWidth = avctx->
width;
1757 ctx->init_encode_params.encodeConfig = &
ctx->encode_config;
1759 preset_config.version = NV_ENC_PRESET_CONFIG_VER;
1760 preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
1762 #ifdef NVENC_HAVE_NEW_PRESETS
1763 ctx->init_encode_params.tuningInfo =
ctx->tuning_info;
1766 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOSSLESS;
1768 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOW_LATENCY;
1770 nv_status = p_nvenc->nvEncGetEncodePresetConfigEx(
ctx->nvencoder,
1771 ctx->init_encode_params.encodeGUID,
1772 ctx->init_encode_params.presetGUID,
1773 ctx->init_encode_params.tuningInfo,
1776 nv_status = p_nvenc->nvEncGetEncodePresetConfig(
ctx->nvencoder,
1777 ctx->init_encode_params.encodeGUID,
1778 ctx->init_encode_params.presetGUID,
1781 if (nv_status != NV_ENC_SUCCESS)
1782 return nvenc_print_error(avctx, nv_status,
"Cannot get the preset configuration");
1784 memcpy(&
ctx->encode_config, &preset_config.presetCfg,
sizeof(
ctx->encode_config));
1786 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1789 ctx->init_encode_params.darHeight = dh;
1790 ctx->init_encode_params.darWidth = dw;
1799 #if FF_API_TICKS_PER_FRAME
1806 #ifdef NVENC_HAVE_UNIDIR_B
1807 ctx->init_encode_params.enableUniDirectionalB =
ctx->unidir_b;
1810 ctx->init_encode_params.enableEncodeAsync = 0;
1811 ctx->init_encode_params.enablePTD = 1;
1813 #ifdef NVENC_HAVE_NEW_PRESETS
1817 if (
ctx->rc_lookahead == 0 &&
ctx->encode_config.rcParams.enableLookahead)
1818 ctx->rc_lookahead =
ctx->encode_config.rcParams.lookaheadDepth;
1821 if (
ctx->weighted_pred == 1)
1822 ctx->init_encode_params.enableWeightedPrediction = 1;
1824 #ifdef NVENC_HAVE_SPLIT_FRAME_ENCODING
1825 ctx->init_encode_params.splitEncodeMode =
ctx->split_encode_mode;
1827 if (
ctx->split_encode_mode != NV_ENC_SPLIT_DISABLE_MODE) {
1829 av_log(avctx,
AV_LOG_WARNING,
"Split encoding not supported with weighted prediction enabled.\n");
1833 if (
ctx->bluray_compat) {
1842 ctx->level = NV_ENC_LEVEL_HEVC_51;
1843 ctx->tier = NV_ENC_TIER_HEVC_HIGH;
1852 ctx->encode_config.frameIntervalP = 0;
1853 ctx->encode_config.gopLength = 1;
1862 if(
ctx->single_slice_intra_refresh)
1863 ctx->intra_refresh = 1;
1872 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
1874 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
1885 nv_status = p_nvenc->nvEncInitializeEncoder(
ctx->nvencoder, &
ctx->init_encode_params);
1886 if (nv_status != NV_ENC_SUCCESS) {
1891 #ifdef NVENC_HAVE_CUSTREAM_PTR
1892 if (
ctx->cu_context) {
1893 nv_status = p_nvenc->nvEncSetIOCudaStreams(
ctx->nvencoder, &
ctx->cu_stream, &
ctx->cu_stream);
1894 if (nv_status != NV_ENC_SUCCESS) {
1905 if (
ctx->encode_config.frameIntervalP > 1)
1908 if (
ctx->encode_config.rcParams.averageBitRate > 0)
1909 avctx->
bit_rate =
ctx->encode_config.rcParams.averageBitRate;
1916 cpb_props->
buffer_size =
ctx->encode_config.rcParams.vbvBufferSize;
1925 return NV_ENC_BUFFER_FORMAT_YV12;
1927 return NV_ENC_BUFFER_FORMAT_NV12;
1930 return NV_ENC_BUFFER_FORMAT_YUV420_10BIT;
1933 return NV_ENC_BUFFER_FORMAT_YUV444;
1936 return NV_ENC_BUFFER_FORMAT_YUV444_10BIT;
1939 return NV_ENC_BUFFER_FORMAT_ARGB;
1942 return NV_ENC_BUFFER_FORMAT_ABGR;
1944 return NV_ENC_BUFFER_FORMAT_ARGB10;
1946 return NV_ENC_BUFFER_FORMAT_ABGR10;
1947 #ifdef NVENC_HAVE_422_SUPPORT
1949 return NV_ENC_BUFFER_FORMAT_NV16;
1952 return NV_ENC_BUFFER_FORMAT_P210;
1955 return NV_ENC_BUFFER_FORMAT_UNDEFINED;
1963 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1966 NVENCSTATUS nv_status;
1967 NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
1968 allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
1972 if (!
ctx->surfaces[idx].in_ref)
1975 NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
1978 if (
ctx->surfaces[idx].format == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
1984 allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
1985 allocSurf.width = avctx->
width;
1986 allocSurf.height = avctx->
height;
1987 allocSurf.bufferFmt =
ctx->surfaces[idx].format;
1989 nv_status = p_nvenc->nvEncCreateInputBuffer(
ctx->nvencoder, &allocSurf);
1990 if (nv_status != NV_ENC_SUCCESS) {
1994 ctx->surfaces[idx].input_surface = allocSurf.inputBuffer;
1995 ctx->surfaces[idx].width = allocSurf.width;
1996 ctx->surfaces[idx].height = allocSurf.height;
1999 nv_status = p_nvenc->nvEncCreateBitstreamBuffer(
ctx->nvencoder, &allocOut);
2000 if (nv_status != NV_ENC_SUCCESS) {
2003 p_nvenc->nvEncDestroyInputBuffer(
ctx->nvencoder,
ctx->surfaces[idx].input_surface);
2008 ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer;
2018 int i, res = 0, res2;
2024 ctx->frame_data_array =
av_calloc(
ctx->frame_data_array_nb,
sizeof(*
ctx->frame_data_array));
2025 if (!
ctx->frame_data_array)
2030 if (!
ctx->timestamp_list)
2034 if (!
ctx->unused_surface_queue)
2038 if (!
ctx->output_surface_queue)
2041 if (!
ctx->output_surface_ready_queue)
2048 for (
i = 0;
i <
ctx->nb_surfaces;
i++) {
2065 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2067 NVENCSTATUS nv_status;
2068 uint32_t outSize = 0;
2069 char tmpHeader[NV_MAX_SEQ_HDR_LEN];
2071 NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
2072 payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
2074 payload.spsppsBuffer = tmpHeader;
2075 payload.inBufferSize =
sizeof(tmpHeader);
2076 payload.outSPSPPSPayloadSize = &outSize;
2078 nv_status = p_nvenc->nvEncGetSequenceParams(
ctx->nvencoder, &payload);
2079 if (nv_status != NV_ENC_SUCCESS) {
2090 memcpy(avctx->
extradata, tmpHeader, outSize);
2099 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2103 if (
ctx->nvencoder) {
2104 NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER,
2105 .encodePicFlags = NV_ENC_PIC_FLAG_EOS };
2111 p_nvenc->nvEncEncodePicture(
ctx->nvencoder, ¶ms);
2119 if (
ctx->frame_data_array) {
2120 for (
i = 0;
i <
ctx->frame_data_array_nb;
i++)
2126 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
2127 if (
ctx->registered_frames[
i].mapped)
2128 p_nvenc->nvEncUnmapInputResource(
ctx->nvencoder,
ctx->registered_frames[
i].in_map.mappedResource);
2129 if (
ctx->registered_frames[
i].regptr)
2130 p_nvenc->nvEncUnregisterResource(
ctx->nvencoder,
ctx->registered_frames[
i].regptr);
2132 ctx->nb_registered_frames = 0;
2135 if (
ctx->surfaces) {
2136 for (
i = 0;
i <
ctx->nb_surfaces; ++
i) {
2138 p_nvenc->nvEncDestroyInputBuffer(
ctx->nvencoder,
ctx->surfaces[
i].input_surface);
2140 p_nvenc->nvEncDestroyBitstreamBuffer(
ctx->nvencoder,
ctx->surfaces[
i].output_surface);
2144 ctx->nb_surfaces = 0;
2150 if (
ctx->nvencoder) {
2151 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
2159 if (
ctx->cu_context_internal)
2161 ctx->cu_context =
ctx->cu_context_internal =
NULL;
2164 if (
ctx->d3d11_device) {
2165 ID3D11Device_Release(
ctx->d3d11_device);
2170 nvenc_free_functions(&dl_fn->
nvenc_dl);
2171 cuda_free_functions(&dl_fn->
cuda_dl);
2189 "hw_frames_ctx must be set when using GPU frames as input\n");
2195 "hw_frames_ctx must match the GPU frame type\n");
2244 NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params,
const AVFrame *
frame)
2246 int dst_linesize[4] = {
2247 lock_buffer_params->pitch,
2248 lock_buffer_params->pitch,
2249 lock_buffer_params->pitch,
2250 lock_buffer_params->pitch
2252 uint8_t *dst_data[4];
2256 dst_linesize[1] = dst_linesize[2] >>= 1;
2259 lock_buffer_params->bufferDataPtr, dst_linesize);
2264 FFSWAP(uint8_t*, dst_data[1], dst_data[2]);
2277 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2278 NVENCSTATUS nv_status;
2283 for (first_round = 1; first_round >= 0; first_round--) {
2284 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
2285 if (!
ctx->registered_frames[
i].mapped) {
2286 if (
ctx->registered_frames[
i].regptr) {
2289 nv_status = p_nvenc->nvEncUnregisterResource(
ctx->nvencoder,
ctx->registered_frames[
i].regptr);
2290 if (nv_status != NV_ENC_SUCCESS)
2291 return nvenc_print_error(avctx, nv_status,
"Failed unregistering unused input resource");
2292 ctx->registered_frames[
i].ptr =
NULL;
2293 ctx->registered_frames[
i].regptr =
NULL;
2300 return ctx->nb_registered_frames++;
2311 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2314 NV_ENC_REGISTER_RESOURCE reg = { 0 };
2317 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
2328 reg.version = NV_ENC_REGISTER_RESOURCE_VER;
2329 reg.width = frames_ctx->width;
2330 reg.height = frames_ctx->height;
2331 reg.pitch =
frame->linesize[0];
2332 reg.resourceToRegister =
frame->data[0];
2335 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR;
2338 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX;
2339 reg.subResourceIndex = (intptr_t)
frame->data[1];
2343 if (reg.bufferFormat == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
2349 ret = p_nvenc->nvEncRegisterResource(
ctx->nvencoder, ®);
2350 if (
ret != NV_ENC_SUCCESS) {
2355 ctx->registered_frames[idx].ptr =
frame->data[0];
2356 ctx->registered_frames[idx].ptr_index = reg.subResourceIndex;
2357 ctx->registered_frames[idx].regptr = reg.registeredResource;
2366 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2369 NVENCSTATUS nv_status;
2382 if (!
ctx->registered_frames[reg_idx].mapped) {
2383 ctx->registered_frames[reg_idx].in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER;
2384 ctx->registered_frames[reg_idx].in_map.registeredResource =
ctx->registered_frames[reg_idx].regptr;
2385 nv_status = p_nvenc->nvEncMapInputResource(
ctx->nvencoder, &
ctx->registered_frames[reg_idx].in_map);
2386 if (nv_status != NV_ENC_SUCCESS) {
2392 ctx->registered_frames[reg_idx].mapped += 1;
2394 nvenc_frame->
reg_idx = reg_idx;
2395 nvenc_frame->
input_surface =
ctx->registered_frames[reg_idx].in_map.mappedResource;
2396 nvenc_frame->
format =
ctx->registered_frames[reg_idx].in_map.mappedBufferFmt;
2401 NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
2403 lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
2406 nv_status = p_nvenc->nvEncLockInputBuffer(
ctx->nvencoder, &lockBufferParams);
2407 if (nv_status != NV_ENC_SUCCESS) {
2408 return nvenc_print_error(avctx, nv_status,
"Failed locking nvenc input buffer");
2411 nvenc_frame->
pitch = lockBufferParams.pitch;
2414 nv_status = p_nvenc->nvEncUnlockInputBuffer(
ctx->nvencoder, nvenc_frame->
input_surface);
2415 if (nv_status != NV_ENC_SUCCESS) {
2424 NV_ENC_PIC_PARAMS *params,
2425 NV_ENC_SEI_PAYLOAD *sei_data,
2432 params->codecPicParams.h264PicParams.sliceMode =
2433 ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
2434 params->codecPicParams.h264PicParams.sliceModeData =
2435 ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
2436 if (sei_count > 0) {
2437 params->codecPicParams.h264PicParams.seiPayloadArray = sei_data;
2438 params->codecPicParams.h264PicParams.seiPayloadArrayCnt = sei_count;
2443 params->codecPicParams.hevcPicParams.sliceMode =
2444 ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
2445 params->codecPicParams.hevcPicParams.sliceModeData =
2446 ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
2447 if (sei_count > 0) {
2448 params->codecPicParams.hevcPicParams.seiPayloadArray = sei_data;
2449 params->codecPicParams.hevcPicParams.seiPayloadArrayCnt = sei_count;
2453 #if CONFIG_AV1_NVENC_ENCODER
2455 params->codecPicParams.av1PicParams.numTileColumns =
2456 ctx->encode_config.encodeCodecConfig.av1Config.numTileColumns;
2457 params->codecPicParams.av1PicParams.numTileRows =
2458 ctx->encode_config.encodeCodecConfig.av1Config.numTileRows;
2459 if (sei_count > 0) {
2460 params->codecPicParams.av1PicParams.obuPayloadArray = sei_data;
2461 params->codecPicParams.av1PicParams.obuPayloadArrayCnt = sei_count;
2492 NV_ENC_LOCK_BITSTREAM *params,
2499 pkt->
pts = params->outputTimeStamp;
2507 delay =
FFMAX(
ctx->encode_config.frameIntervalP - 1, 0);
2508 if (
ctx->output_frame_num >= delay) {
2510 ctx->output_frame_num++;
2514 delay_time =
ctx->initial_delay_time;
2522 delay_time = t1 - t2;
2527 delay_time = delay * (t3 - t2);
2531 ctx->initial_delay_time = delay_time;
2540 ctx->output_frame_num++;
2550 int idx =
ctx->frame_data_array_pos;
2565 ctx->frame_data_array_pos = (
ctx->frame_data_array_pos + 1) %
ctx->frame_data_array_nb;
2566 pic_params->inputDuration = idx;
2576 int idx = lock_params->outputDuration;
2596 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2598 NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
2599 NVENCSTATUS nv_status;
2604 lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
2606 lock_params.doNotWait = 0;
2609 nv_status = p_nvenc->nvEncLockBitstream(
ctx->nvencoder, &lock_params);
2610 if (nv_status != NV_ENC_SUCCESS) {
2622 memcpy(
pkt->
data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
2624 nv_status = p_nvenc->nvEncUnlockBitstream(
ctx->nvencoder, tmpoutsurf->
output_surface);
2625 if (nv_status != NV_ENC_SUCCESS) {
2626 res =
nvenc_print_error(avctx, nv_status,
"Failed unlocking bitstream buffer, expect the gates of mordor to open");
2632 ctx->registered_frames[tmpoutsurf->
reg_idx].mapped -= 1;
2633 if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped == 0) {
2634 nv_status = p_nvenc->nvEncUnmapInputResource(
ctx->nvencoder,
ctx->registered_frames[tmpoutsurf->
reg_idx].in_map.mappedResource);
2635 if (nv_status != NV_ENC_SUCCESS) {
2639 }
else if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped < 0) {
2649 switch (lock_params.pictureType) {
2650 case NV_ENC_PIC_TYPE_IDR:
2652 case NV_ENC_PIC_TYPE_I:
2655 case NV_ENC_PIC_TYPE_P:
2658 case NV_ENC_PIC_TYPE_B:
2661 case NV_ENC_PIC_TYPE_BI:
2665 av_log(avctx,
AV_LOG_ERROR,
"Unknown picture type encountered, expect the output to be broken.\n");
2666 av_log(avctx,
AV_LOG_ERROR,
"Please report this error and include as much information on how to reproduce it as possible.\n");
2694 int nb_ready, nb_pending;
2699 return nb_ready > 0;
2700 return (nb_ready > 0) && (nb_ready + nb_pending >=
ctx->async_depth);
2710 void *a53_data =
NULL;
2711 size_t a53_size = 0;
2719 &
ctx->sei_data_size,
2720 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2727 ctx->sei_data[sei_count].payloadSize = (uint32_t)a53_size;
2728 ctx->sei_data[sei_count].payload = (uint8_t*)a53_data;
2730 #if CONFIG_AV1_NVENC_ENCODER
2743 void *tc_data =
NULL;
2752 &
ctx->sei_data_size,
2753 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2760 ctx->sei_data[sei_count].payloadSize = (uint32_t)tc_size;
2761 ctx->sei_data[sei_count].payload = (uint8_t*)tc_data;
2763 #if CONFIG_AV1_NVENC_ENCODER
2778 for (
i = 0;
i <
frame->nb_side_data;
i++) {
2786 &
ctx->sei_data_size,
2787 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2793 ctx->sei_data[sei_count].payloadSize = side_data->
size;
2797 if (!
ctx->sei_data[sei_count].payload) {
2809 for (
i = 0;
i < sei_count;
i++)
2818 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
2821 NV_ENC_RECONFIGURE_PARAMS params = { 0 };
2822 int needs_reconfig = 0;
2823 int needs_encode_config = 0;
2824 int reconfig_bitrate = 0, reconfig_dar = 0;
2827 params.version = NV_ENC_RECONFIGURE_PARAMS_VER;
2828 params.reInitEncodeParams =
ctx->init_encode_params;
2831 if (dw !=
ctx->init_encode_params.darWidth || dh !=
ctx->init_encode_params.darHeight) {
2833 "aspect ratio change (DAR): %d:%d -> %d:%d\n",
2834 ctx->init_encode_params.darWidth,
2835 ctx->init_encode_params.darHeight, dw, dh);
2837 params.reInitEncodeParams.darHeight = dh;
2838 params.reInitEncodeParams.darWidth = dw;
2844 if (
ctx->rc != NV_ENC_PARAMS_RC_CONSTQP &&
ctx->support_dyn_bitrate) {
2845 if (avctx->
bit_rate > 0 && params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate != avctx->
bit_rate) {
2847 "avg bitrate change: %d -> %d\n",
2848 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate,
2851 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate = avctx->
bit_rate;
2852 reconfig_bitrate = 1;
2857 "max bitrate change: %d -> %d\n",
2858 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate,
2861 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate = avctx->
rc_max_rate;
2862 reconfig_bitrate = 1;
2867 "vbv buffer size change: %d -> %d\n",
2868 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize,
2871 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize = avctx->
rc_buffer_size;
2872 reconfig_bitrate = 1;
2875 if (reconfig_bitrate) {
2876 params.resetEncoder = 1;
2877 params.forceIDR = 1;
2879 needs_encode_config = 1;
2884 if (!needs_encode_config)
2885 params.reInitEncodeParams.encodeConfig =
NULL;
2887 if (needs_reconfig) {
2888 ret = p_nvenc->nvEncReconfigureEncoder(
ctx->nvencoder, ¶ms);
2889 if (
ret != NV_ENC_SUCCESS) {
2893 ctx->init_encode_params.darHeight = dh;
2894 ctx->init_encode_params.darWidth = dw;
2897 if (reconfig_bitrate) {
2898 ctx->encode_config.rcParams.averageBitRate = params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate;
2899 ctx->encode_config.rcParams.maxBitRate = params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate;
2900 ctx->encode_config.rcParams.vbvBufferSize = params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize;
2909 NVENCSTATUS nv_status;
2914 #ifdef NVENC_HAVE_HEVC_AND_AV1_MASTERING_METADATA
2915 MASTERING_DISPLAY_INFO mastering_disp_info = { 0 };
2916 CONTENT_LIGHT_LEVEL content_light_level = { 0 };
2921 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2923 NV_ENC_PIC_PARAMS pic_params = { 0 };
2924 pic_params.version = NV_ENC_PIC_PARAMS_VER;
2926 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
2950 pic_params.bufferFmt = in_surf->
format;
2951 pic_params.inputWidth = in_surf->
width;
2952 pic_params.inputHeight = in_surf->
height;
2953 pic_params.inputPitch = in_surf->
pitch;
2958 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
2960 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
2962 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
2966 pic_params.encodePicFlags =
2967 ctx->forced_idr ? NV_ENC_PIC_FLAG_FORCEIDR : NV_ENC_PIC_FLAG_FORCEINTRA;
2969 pic_params.encodePicFlags = 0;
2972 pic_params.frameIdx =
ctx->frame_idx_counter++;
2973 pic_params.inputTimeStamp =
frame->pts;
2975 if (
ctx->extra_sei) {
2982 #ifdef NVENC_HAVE_HEVC_AND_AV1_MASTERING_METADATA
2983 if (
ctx->mdm ||
ctx->cll) {
3024 pic_params.codecPicParams.hevcPicParams.pMasteringDisplay = &mastering_disp_info;
3026 pic_params.codecPicParams.av1PicParams.pMasteringDisplay = &mastering_disp_info;
3033 content_light_level.maxContentLightLevel = cll->
MaxCLL;
3034 content_light_level.maxPicAverageLightLevel = cll->
MaxFALL;
3037 pic_params.codecPicParams.hevcPicParams.pMaxCll = &content_light_level;
3039 pic_params.codecPicParams.av1PicParams.pMaxCll = &content_light_level;
3051 pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
3058 nv_status = p_nvenc->nvEncEncodePicture(
ctx->nvencoder, &pic_params);
3060 for (
i = 0;
i < sei_count;
i++)
3067 if (nv_status != NV_ENC_SUCCESS &&
3068 nv_status != NV_ENC_ERR_NEED_MORE_INPUT)
3079 if (nv_status == NV_ENC_SUCCESS) {
3080 while (
av_fifo_read(
ctx->output_surface_queue, &tmp_out_surf, 1) >= 0)
3096 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
3099 if (!
frame->buf[0]) {
3144 ctx->output_frame_num = 0;
3145 ctx->initial_delay_time = 0;