23 #include "config_components.h"
27 #if CONFIG_AV1_NVENC_ENCODER
47 #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, dl_fn->cuda_dl, x)
49 #define NVENC_CAP 0x30
51 #ifndef NVENC_NO_DEPRECATED_RC
52 #define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR || \
53 rc == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ || \
54 rc == NV_ENC_PARAMS_RC_CBR_HQ)
56 #define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR)
65 #ifdef NVENC_HAVE_422_SUPPORT
98 #define IS_10BIT(pix_fmt) (pix_fmt == AV_PIX_FMT_P010 || \
99 pix_fmt == AV_PIX_FMT_P016 || \
100 pix_fmt == AV_PIX_FMT_P210 || \
101 pix_fmt == AV_PIX_FMT_P216 || \
102 pix_fmt == AV_PIX_FMT_YUV444P10MSB || \
103 pix_fmt == AV_PIX_FMT_YUV444P16 || \
104 pix_fmt == AV_PIX_FMT_X2RGB10 || \
105 pix_fmt == AV_PIX_FMT_X2BGR10 || \
106 pix_fmt == AV_PIX_FMT_GBRP10MSB || \
107 pix_fmt == AV_PIX_FMT_GBRP16)
109 #define IS_RGB(pix_fmt) (pix_fmt == AV_PIX_FMT_0RGB32 || \
110 pix_fmt == AV_PIX_FMT_RGB32 || \
111 pix_fmt == AV_PIX_FMT_0BGR32 || \
112 pix_fmt == AV_PIX_FMT_BGR32 || \
113 pix_fmt == AV_PIX_FMT_X2RGB10 || \
114 pix_fmt == AV_PIX_FMT_X2BGR10)
116 #define IS_YUV444(pix_fmt) (pix_fmt == AV_PIX_FMT_YUV444P || \
117 pix_fmt == AV_PIX_FMT_YUV444P10MSB || \
118 pix_fmt == AV_PIX_FMT_YUV444P16 || \
119 pix_fmt == AV_PIX_FMT_GBRP || \
120 pix_fmt == AV_PIX_FMT_GBRP10MSB || \
121 pix_fmt == AV_PIX_FMT_GBRP16 || \
122 (ctx->rgb_mode == NVENC_RGB_MODE_444 && IS_RGB(pix_fmt)))
124 #define IS_YUV422(pix_fmt) (pix_fmt == AV_PIX_FMT_NV16 || \
125 pix_fmt == AV_PIX_FMT_P210 || \
126 pix_fmt == AV_PIX_FMT_P216)
128 #define IS_GBRP(pix_fmt) (pix_fmt == AV_PIX_FMT_GBRP || \
129 pix_fmt == AV_PIX_FMT_GBRP10MSB || \
130 pix_fmt == AV_PIX_FMT_GBRP16)
132 static const struct {
137 { NV_ENC_SUCCESS, 0,
"success" },
138 { NV_ENC_ERR_NO_ENCODE_DEVICE,
AVERROR(ENOENT),
"no encode device" },
139 { NV_ENC_ERR_UNSUPPORTED_DEVICE,
AVERROR(ENOSYS),
"unsupported device" },
140 { NV_ENC_ERR_INVALID_ENCODERDEVICE,
AVERROR(EINVAL),
"invalid encoder device" },
141 { NV_ENC_ERR_INVALID_DEVICE,
AVERROR(EINVAL),
"invalid device" },
142 { NV_ENC_ERR_DEVICE_NOT_EXIST,
AVERROR(EIO),
"device does not exist" },
143 { NV_ENC_ERR_INVALID_PTR,
AVERROR(EFAULT),
"invalid ptr" },
144 { NV_ENC_ERR_INVALID_EVENT,
AVERROR(EINVAL),
"invalid event" },
145 { NV_ENC_ERR_INVALID_PARAM,
AVERROR(EINVAL),
"invalid param" },
146 { NV_ENC_ERR_INVALID_CALL,
AVERROR(EINVAL),
"invalid call" },
147 { NV_ENC_ERR_OUT_OF_MEMORY,
AVERROR(ENOMEM),
"out of memory" },
148 { NV_ENC_ERR_ENCODER_NOT_INITIALIZED,
AVERROR(EINVAL),
"encoder not initialized" },
149 { NV_ENC_ERR_UNSUPPORTED_PARAM,
AVERROR(ENOSYS),
"unsupported param" },
150 { NV_ENC_ERR_LOCK_BUSY,
AVERROR(EAGAIN),
"lock busy" },
152 { NV_ENC_ERR_INVALID_VERSION,
AVERROR(EINVAL),
"invalid version" },
153 { NV_ENC_ERR_MAP_FAILED,
AVERROR(EIO),
"map failed" },
154 { NV_ENC_ERR_NEED_MORE_INPUT,
AVERROR(EAGAIN),
"need more input" },
155 { NV_ENC_ERR_ENCODER_BUSY,
AVERROR(EAGAIN),
"encoder busy" },
156 { NV_ENC_ERR_EVENT_NOT_REGISTERD,
AVERROR(EBADF),
"event not registered" },
158 { NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY,
AVERROR(EINVAL),
"incompatible client key" },
159 { NV_ENC_ERR_UNIMPLEMENTED,
AVERROR(ENOSYS),
"unimplemented" },
160 { NV_ENC_ERR_RESOURCE_REGISTER_FAILED,
AVERROR(EIO),
"resource register failed" },
161 { NV_ENC_ERR_RESOURCE_NOT_REGISTERED,
AVERROR(EBADF),
"resource not registered" },
162 { NV_ENC_ERR_RESOURCE_NOT_MAPPED,
AVERROR(EBADF),
"resource not mapped" },
176 *
desc =
"unknown error";
181 const char *error_string)
184 const char *details =
"(no details)";
187 #ifdef NVENC_HAVE_GETLASTERRORSTRING
189 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
191 if (p_nvenc &&
ctx->nvencoder)
192 details = p_nvenc->nvEncGetLastErrorString(
ctx->nvencoder);
205 #define PRESET_ALIAS(alias, name, ...) \
206 [PRESET_ ## alias] = { NV_ENC_PRESET_ ## name ## _GUID, __VA_ARGS__ }
208 #define PRESET(name, ...) PRESET_ALIAS(name, name, __VA_ARGS__)
213 #ifdef NVENC_HAVE_NEW_PRESETS
252 ctx->init_encode_params.presetGUID = t->
guid;
255 #ifdef NVENC_HAVE_NEW_PRESETS
256 if (
ctx->tuning_info == NV_ENC_TUNING_INFO_LOSSLESS)
266 #if NVENCAPI_CHECK_VERSION(13, 1)
267 const char *minver =
"(unknown)";
268 #elif NVENCAPI_CHECK_VERSION(13, 0)
269 const char *minver =
"570.0";
270 #elif NVENCAPI_CHECK_VERSION(12, 2)
271 # if defined(_WIN32) || defined(__CYGWIN__)
272 const char *minver =
"551.76";
274 const char *minver =
"550.54.14";
276 #elif NVENCAPI_CHECK_VERSION(12, 1)
277 # if defined(_WIN32) || defined(__CYGWIN__)
278 const char *minver =
"531.61";
280 const char *minver =
"530.41.03";
282 #elif NVENCAPI_CHECK_VERSION(12, 0)
283 # if defined(_WIN32) || defined(__CYGWIN__)
284 const char *minver =
"522.25";
286 const char *minver =
"520.56.06";
288 #elif NVENCAPI_CHECK_VERSION(11, 1)
289 # if defined(_WIN32) || defined(__CYGWIN__)
290 const char *minver =
"471.41";
292 const char *minver =
"470.57.02";
294 #elif NVENCAPI_CHECK_VERSION(11, 0)
295 # if defined(_WIN32) || defined(__CYGWIN__)
296 const char *minver =
"456.71";
298 const char *minver =
"455.28";
300 #elif NVENCAPI_CHECK_VERSION(10, 0)
301 # if defined(_WIN32) || defined(__CYGWIN__)
302 const char *minver =
"450.51";
304 const char *minver =
"445.87";
306 #elif NVENCAPI_CHECK_VERSION(9, 1)
307 # if defined(_WIN32) || defined(__CYGWIN__)
308 const char *minver =
"436.15";
310 const char *minver =
"435.21";
312 #elif NVENCAPI_CHECK_VERSION(9, 0)
313 # if defined(_WIN32) || defined(__CYGWIN__)
314 const char *minver =
"418.81";
316 const char *minver =
"418.30";
318 #elif NVENCAPI_CHECK_VERSION(8, 2)
319 # if defined(_WIN32) || defined(__CYGWIN__)
320 const char *minver =
"397.93";
322 const char *minver =
"396.24";
324 #elif NVENCAPI_CHECK_VERSION(8, 1)
325 # if defined(_WIN32) || defined(__CYGWIN__)
326 const char *minver =
"390.77";
328 const char *minver =
"390.25";
331 # if defined(_WIN32) || defined(__CYGWIN__)
332 const char *minver =
"378.66";
334 const char *minver =
"378.13";
337 av_log(avctx,
level,
"The minimum required Nvidia driver for nvenc is %s or newer\n", minver);
340 #if NVENCAPI_CHECK_VERSION(12, 0)
341 #define to_nv_color_matrix(n) (NV_ENC_VUI_MATRIX_COEFFS)(n)
342 #define to_nv_color_pri(n) (NV_ENC_VUI_COLOR_PRIMARIES)(n)
343 #define to_nv_color_trc(n) (NV_ENC_VUI_TRANSFER_CHARACTERISTIC)(n)
345 #define to_nv_color_matrix(n) (uint32_t)(n)
346 #define to_nv_color_pri(n) (uint32_t)(n)
347 #define to_nv_color_trc(n) (uint32_t)(n)
355 uint32_t nvenc_max_ver;
358 ret = cuda_load_functions(&dl_fn->
cuda_dl, avctx);
362 ret = nvenc_load_functions(&dl_fn->
nvenc_dl, avctx);
368 err = dl_fn->
nvenc_dl->NvEncodeAPIGetMaxSupportedVersion(&nvenc_max_ver);
369 if (err != NV_ENC_SUCCESS)
374 if ((NVENCAPI_MAJOR_VERSION << 4 | NVENCAPI_MINOR_VERSION) > nvenc_max_ver) {
375 av_log(avctx,
AV_LOG_ERROR,
"Driver does not support the required nvenc API version. "
376 "Required: %d.%d Found: %d.%d\n",
377 NVENCAPI_MAJOR_VERSION, NVENCAPI_MINOR_VERSION,
378 nvenc_max_ver >> 4, nvenc_max_ver & 0
xf);
383 dl_fn->
nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
386 if (err != NV_ENC_SUCCESS)
399 if (
ctx->d3d11_device)
411 if (
ctx->d3d11_device)
419 NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = { 0 };
421 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
424 params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
425 params.apiVersion = NVENCAPI_VERSION;
426 if (
ctx->d3d11_device) {
427 params.device =
ctx->d3d11_device;
428 params.deviceType = NV_ENC_DEVICE_TYPE_DIRECTX;
430 params.device =
ctx->cu_context;
431 params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
434 ret = p_nvenc->nvEncOpenEncodeSessionEx(¶ms, &
ctx->nvencoder);
435 if (
ret != NV_ENC_SUCCESS) {
446 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
447 int i,
ret, count = 0;
450 ret = p_nvenc->nvEncGetEncodeGUIDCount(
ctx->nvencoder, &count);
452 if (
ret != NV_ENC_SUCCESS || !count)
459 ret = p_nvenc->nvEncGetEncodeGUIDs(
ctx->nvencoder, guids, count, &count);
460 if (
ret != NV_ENC_SUCCESS) {
466 for (
i = 0;
i < count;
i++) {
467 if (!memcmp(&guids[
i], &
ctx->init_encode_params.encodeGUID,
sizeof(*guids))) {
482 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
483 NV_ENC_CAPS_PARAM params = { 0 };
486 params.version = NV_ENC_CAPS_PARAM_VER;
487 params.capsToQuery = cap;
489 ret = p_nvenc->nvEncGetEncodeCaps(
ctx->nvencoder,
ctx->init_encode_params.encodeGUID, ¶ms, &
val);
491 if (
ret == NV_ENC_SUCCESS)
513 #ifdef NVENC_HAVE_422_SUPPORT
530 if (ret < avctx->
width) {
537 if (ret < avctx->
height) {
544 if (ret < avctx->max_b_frames) {
554 "Interlaced encoding is not supported. Supported level: %d\n",
566 if (
ctx->rc_lookahead > 0 &&
ret <= 0) {
572 if (
ctx->temporal_aq > 0 &&
ret <= 0) {
578 if (
ctx->weighted_pred > 0 &&
ret <= 0) {
584 if (
ctx->coder == NV_ENC_H264_ENTROPY_CODING_MODE_CABAC &&
ret <= 0) {
589 #ifdef NVENC_HAVE_BFRAME_REF_MODE
590 tmp = (
ctx->b_ref_mode >= 0) ?
ctx->b_ref_mode : NV_ENC_BFRAME_REF_MODE_DISABLED;
592 if (
tmp == NV_ENC_BFRAME_REF_MODE_EACH &&
ret != 1 &&
ret != 3) {
595 }
else if (
tmp != NV_ENC_BFRAME_REF_MODE_DISABLED &&
ret == 0) {
600 tmp = (
ctx->b_ref_mode >= 0) ?
ctx->b_ref_mode : 0;
607 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
609 if(avctx->
refs != NV_ENC_NUM_REF_FRAMES_AUTOSELECT &&
ret <= 0) {
614 if(avctx->
refs != 0) {
620 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
622 if(
ctx->single_slice_intra_refresh &&
ret <= 0) {
627 if(
ctx->single_slice_intra_refresh) {
634 if((
ctx->intra_refresh ||
ctx->single_slice_intra_refresh) &&
ret <= 0) {
639 #ifndef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
647 if(
ctx->constrained_encoding &&
ret <= 0) {
652 #if defined(NVENC_HAVE_TEMPORAL_FILTER) || defined(NVENC_HAVE_H264_AND_AV1_TEMPORAL_FILTER)
654 if(
ctx->tf_level > 0 &&
ret <= 0) {
660 #ifdef NVENC_HAVE_LOOKAHEAD_LEVEL
662 if(
ctx->rc_lookahead > 0 &&
ctx->lookahead_level > 0 &&
663 ctx->lookahead_level != NV_ENC_LOOKAHEAD_LEVEL_AUTOSELECT &&
664 ctx->lookahead_level >
ret)
671 #ifdef NVENC_HAVE_UNIDIR_B
673 if(
ctx->unidir_b &&
ret <= 0) {
679 ctx->support_dyn_bitrate =
nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE);
681 #ifdef NVENC_HAVE_MVHEVC
682 ctx->multiview_supported =
nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_MVHEVC_ENCODE) > 0;
683 if(
ctx->profile == NV_ENC_HEVC_PROFILE_MULTIVIEW_MAIN && !
ctx->multiview_supported) {
696 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
697 char name[128] = { 0};
698 int major, minor,
ret;
717 av_log(avctx, loglevel,
"[ GPU #%d - < %s > has Compute SM %d.%d ]\n", idx,
name, major, minor);
718 if (((major << 4) | minor) <
NVENC_CAP) {
719 av_log(avctx, loglevel,
"does not support NVENC\n");
730 ctx->cu_context =
ctx->cu_context_internal;
742 av_log(avctx, loglevel,
"supports NVENC\n");
753 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
761 ctx->cu_context_internal =
NULL;
774 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_H264_GUID;
777 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_HEVC_GUID;
779 #if CONFIG_AV1_NVENC_ENCODER
781 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_AV1_GUID;
791 av_log(avctx,
AV_LOG_WARNING,
"The selected preset is deprecated. Use p1 to p7 + -tune or fast/medium/slow.\n");
815 cuda_device_hwctx = hwdev_ctx->
hwctx;
818 d3d11_device_hwctx = hwdev_ctx->
hwctx;
826 if (cuda_device_hwctx) {
828 ctx->cu_stream = cuda_device_hwctx->
stream;
831 else if (d3d11_device_hwctx) {
832 ctx->d3d11_device = d3d11_device_hwctx->
device;
833 ID3D11Device_AddRef(
ctx->d3d11_device);
843 av_log(avctx,
AV_LOG_FATAL,
"Provided device doesn't support required NVENC features\n");
847 int i, nb_devices = 0;
863 for (
i = 0;
i < nb_devices; ++
i) {
876 av_log(avctx,
AV_LOG_FATAL,
"Requested GPU %d, but only %d GPUs are available!\n",
ctx->device, nb_devices);
886 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
887 #if CONFIG_AV1_NVENC_ENCODER
893 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
895 if (
ctx->init_qp_p >= 0) {
896 rc->constQP.qpInterP =
ctx->init_qp_p;
897 if (
ctx->init_qp_i >= 0 &&
ctx->init_qp_b >= 0) {
898 rc->constQP.qpIntra =
ctx->init_qp_i;
899 rc->constQP.qpInterB =
ctx->init_qp_b;
903 rc->constQP.qpInterB =
av_clip(
906 rc->constQP.qpIntra = rc->constQP.qpInterP;
907 rc->constQP.qpInterB = rc->constQP.qpInterP;
909 }
else if (
ctx->cqp >= 0) {
910 rc->constQP.qpInterP = rc->constQP.qpInterB = rc->constQP.qpIntra =
ctx->cqp;
924 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
926 #if CONFIG_AV1_NVENC_ENCODER
932 if (avctx->
qmin >= 0 || avctx->
qmax >= 0)
933 av_log(avctx,
AV_LOG_WARNING,
"Passing qmin/qmax via global AVCodecContext options. Use encoder options instead.\n");
935 if (avctx->
qmin >= 0 &&
ctx->qmin < 0)
937 if (avctx->
qmax >= 0 &&
ctx->qmax < 0)
942 if (
ctx->qmin >= 0 &&
ctx->qmax >= 0) {
946 rc->minQP.qpInterB =
ctx->qmin;
947 rc->minQP.qpInterP =
ctx->qmin;
948 rc->minQP.qpIntra =
ctx->qmin;
950 rc->maxQP.qpInterB =
ctx->qmax;
951 rc->maxQP.qpInterP =
ctx->qmax;
952 rc->maxQP.qpIntra =
ctx->qmax;
954 qp_inter_p = (
ctx->qmax + 3 *
ctx->qmin) / 4;
955 }
else if (
ctx->qmin >= 0) {
958 rc->minQP.qpInterB =
ctx->qmin;
959 rc->minQP.qpInterP =
ctx->qmin;
960 rc->minQP.qpIntra =
ctx->qmin;
962 qp_inter_p =
ctx->qmin;
967 rc->enableInitialRCQP = 1;
969 if (
ctx->init_qp_p < 0) {
970 rc->initialRCQP.qpInterP = qp_inter_p;
972 rc->initialRCQP.qpInterP =
ctx->init_qp_p;
975 if (
ctx->init_qp_i < 0) {
977 rc->initialRCQP.qpIntra =
av_clip(
980 rc->initialRCQP.qpIntra = rc->initialRCQP.qpInterP;
983 rc->initialRCQP.qpIntra =
ctx->init_qp_i;
986 if (
ctx->init_qp_b < 0) {
988 rc->initialRCQP.qpInterB =
av_clip(
991 rc->initialRCQP.qpInterB = rc->initialRCQP.qpInterP;
994 rc->initialRCQP.qpInterB =
ctx->init_qp_b;
1001 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
1003 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
1004 rc->constQP.qpInterB = 0;
1005 rc->constQP.qpInterP = 0;
1006 rc->constQP.qpIntra = 0;
1015 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
1018 case NV_ENC_PARAMS_RC_CONSTQP:
1021 #ifndef NVENC_NO_DEPRECATED_RC
1022 case NV_ENC_PARAMS_RC_VBR_MINQP:
1023 if (avctx->
qmin < 0 &&
ctx->qmin < 0) {
1025 "The variable bitrate rate-control requires "
1026 "the 'qmin' option set.\n");
1031 case NV_ENC_PARAMS_RC_VBR_HQ:
1033 case NV_ENC_PARAMS_RC_VBR:
1036 case NV_ENC_PARAMS_RC_CBR:
1037 #ifndef NVENC_NO_DEPRECATED_RC
1038 case NV_ENC_PARAMS_RC_CBR_HQ:
1039 case NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ:
1044 rc->rateControlMode =
ctx->rc;
1053 int nb_surfaces =
FFMAX(4,
ctx->encode_config.frameIntervalP * 2 * 2);
1056 if (
ctx->rc_lookahead > 0) {
1059 nb_surfaces =
FFMAX(1,
FFMAX(nb_surfaces,
ctx->rc_lookahead +
ctx->encode_config.frameIntervalP + 1 + 4));
1060 if (nb_surfaces >
ctx->nb_surfaces &&
ctx->nb_surfaces > 0)
1063 "Defined rc_lookahead requires more surfaces, "
1064 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
1066 ctx->nb_surfaces =
FFMAX(nb_surfaces,
ctx->nb_surfaces);
1068 if (
ctx->encode_config.frameIntervalP > 1 &&
ctx->nb_surfaces < nb_surfaces &&
ctx->nb_surfaces > 0)
1071 "Defined b-frame requires more surfaces, "
1072 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
1073 ctx->nb_surfaces =
FFMAX(
ctx->nb_surfaces, nb_surfaces);
1075 else if (
ctx->nb_surfaces <= 0)
1076 ctx->nb_surfaces = nb_surfaces;
1081 ctx->async_depth =
FFMIN(
ctx->async_depth,
ctx->nb_surfaces - 1);
1085 ctx->frame_data_array_nb =
FFMAX(
ctx->nb_surfaces,
ctx->nb_surfaces +
ctx->encode_config.frameIntervalP - 1);
1095 av_log(avctx,
AV_LOG_WARNING,
"Using global_quality with nvenc is deprecated. Use qp instead.\n");
1101 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate;
1102 }
else if (
ctx->encode_config.rcParams.averageBitRate > 0) {
1103 ctx->encode_config.rcParams.maxBitRate =
ctx->encode_config.rcParams.averageBitRate;
1109 #ifdef NVENC_HAVE_MULTIPASS
1110 ctx->encode_config.rcParams.multiPass =
ctx->multipass;
1113 ctx->encode_config.rcParams.multiPass = NV_ENC_MULTI_PASS_DISABLED;
1115 ctx->encode_config.rcParams.multiPass = NV_ENC_TWO_PASS_FULL_RESOLUTION;
1119 ctx->rc = NV_ENC_PARAMS_RC_CBR;
1120 }
else if (
ctx->cqp >= 0) {
1121 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
1122 }
else if (
ctx->quality >= 0.0f) {
1123 ctx->rc = NV_ENC_PARAMS_RC_VBR;
1133 if (
ctx->twopass < 0)
1138 ctx->rc = NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ;
1140 ctx->rc = NV_ENC_PARAMS_RC_CBR;
1142 }
else if (
ctx->cqp >= 0) {
1143 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
1144 }
else if (
ctx->twopass) {
1145 ctx->rc = NV_ENC_PARAMS_RC_VBR_HQ;
1146 }
else if ((avctx->
qmin >= 0 && avctx->
qmax >= 0) ||
1147 (
ctx->qmin >= 0 &&
ctx->qmax >= 0)) {
1148 ctx->rc = NV_ENC_PARAMS_RC_VBR_MINQP;
1160 #ifdef NVENC_HAVE_QP_CHROMA_OFFSETS
1161 ctx->encode_config.rcParams.cbQPIndexOffset =
ctx->qp_cb_offset;
1162 ctx->encode_config.rcParams.crQPIndexOffset =
ctx->qp_cr_offset;
1164 if (
ctx->qp_cb_offset ||
ctx->qp_cr_offset)
1165 av_log(avctx,
AV_LOG_WARNING,
"Failed setting QP CB/CR offsets, SDK 11.1 or greater required at compile time.\n");
1168 #ifdef NVENC_HAVE_LDKFS
1170 ctx->encode_config.rcParams.lowDelayKeyFrameScale =
ctx->ldkfs;
1175 }
else if (
ctx->rc >= 0) {
1178 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
1184 }
else if (
ctx->encode_config.rcParams.averageBitRate > 0) {
1185 avctx->
rc_buffer_size =
ctx->encode_config.rcParams.vbvBufferSize = 2 *
ctx->encode_config.rcParams.averageBitRate;
1189 ctx->encode_config.rcParams.enableAQ = 1;
1190 ctx->encode_config.rcParams.aqStrength =
ctx->aq_strength;
1194 if (
ctx->temporal_aq) {
1195 ctx->encode_config.rcParams.enableTemporalAQ = 1;
1199 if (
ctx->rc_lookahead > 0) {
1200 int lkd_bound =
FFMIN(
ctx->nb_surfaces,
ctx->async_depth) -
1201 ctx->encode_config.frameIntervalP - 4;
1203 if (lkd_bound < 0) {
1204 ctx->encode_config.rcParams.enableLookahead = 0;
1206 "Lookahead not enabled. Increase buffer delay (-delay).\n");
1208 ctx->encode_config.rcParams.enableLookahead = 1;
1209 ctx->encode_config.rcParams.lookaheadDepth =
av_clip(
ctx->rc_lookahead, 0, lkd_bound);
1210 ctx->encode_config.rcParams.disableIadapt =
ctx->no_scenecut;
1211 ctx->encode_config.rcParams.disableBadapt = !
ctx->b_adapt;
1213 "Lookahead enabled: depth %d, scenecut %s, B-adapt %s.\n",
1214 ctx->encode_config.rcParams.lookaheadDepth,
1215 ctx->encode_config.rcParams.disableIadapt ?
"disabled" :
"enabled",
1216 ctx->encode_config.rcParams.disableBadapt ?
"disabled" :
"enabled");
1217 if (
ctx->encode_config.rcParams.lookaheadDepth <
ctx->rc_lookahead)
1218 av_log(avctx,
AV_LOG_WARNING,
"Clipping lookahead depth to %d (from %d) due to lack of surfaces/delay",
1219 ctx->encode_config.rcParams.lookaheadDepth,
ctx->rc_lookahead);
1221 #ifdef NVENC_HAVE_LOOKAHEAD_LEVEL
1222 if (
ctx->lookahead_level >= 0) {
1223 switch (
ctx->lookahead_level) {
1224 case NV_ENC_LOOKAHEAD_LEVEL_0:
1225 case NV_ENC_LOOKAHEAD_LEVEL_1:
1226 case NV_ENC_LOOKAHEAD_LEVEL_2:
1227 case NV_ENC_LOOKAHEAD_LEVEL_3:
1228 case NV_ENC_LOOKAHEAD_LEVEL_AUTOSELECT:
1235 ctx->encode_config.rcParams.lookaheadLevel =
ctx->lookahead_level;
1241 if (
ctx->strict_gop) {
1242 ctx->encode_config.rcParams.strictGOPTarget = 1;
1247 ctx->encode_config.rcParams.enableNonRefP = 1;
1249 if (
ctx->zerolatency)
1250 ctx->encode_config.rcParams.zeroReorderDelay = 1;
1254 int tmp_quality = (int)(
ctx->quality * 256.0f);
1255 ctx->encode_config.rcParams.targetQuality = (uint8_t)(tmp_quality >> 8);
1256 ctx->encode_config.rcParams.targetQualityLSB = (uint8_t)(tmp_quality & 0xff);
1261 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate = 0;
1272 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1273 NV_ENC_CONFIG_H264 *h264 = &cc->encodeCodecConfig.h264Config;
1274 NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui = &h264->h264VUIParameters;
1282 vui->videoFullRangeFlag = 0;
1291 vui->colourDescriptionPresentFlag =
1292 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1294 vui->videoSignalTypePresentFlag =
1295 (vui->colourDescriptionPresentFlag
1296 || vui->videoFormat != 5
1297 || vui->videoFullRangeFlag != 0);
1299 if (
ctx->max_slice_size > 0) {
1300 h264->sliceMode = 1;
1301 h264->sliceModeData =
ctx->max_slice_size;
1303 h264->sliceMode = 3;
1304 h264->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1307 if (
ctx->intra_refresh) {
1308 h264->enableIntraRefresh = 1;
1309 h264->intraRefreshPeriod = cc->gopLength;
1310 h264->intraRefreshCnt = cc->gopLength - 1;
1311 cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1312 h264->outputRecoveryPointSEI = 1;
1313 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1314 h264->singleSliceIntraRefresh =
ctx->single_slice_intra_refresh;
1318 if (
ctx->constrained_encoding)
1319 h264->enableConstrainedEncoding = 1;
1323 h264->outputAUD =
ctx->aud;
1325 if (
ctx->dpb_size >= 0) {
1327 h264->maxNumRefFrames =
ctx->dpb_size;
1330 h264->idrPeriod = cc->gopLength;
1332 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1334 h264->outputBufferingPeriodSEI =
ctx->cbr_padding;
1336 #ifdef NVENC_HAVE_FILLER_DATA
1337 h264->enableFillerDataInsertion =
ctx->cbr_padding;
1341 h264->outputPictureTimingSEI = 1;
1343 #ifndef NVENC_NO_DEPRECATED_RC
1344 if (cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ ||
1345 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_HQ ||
1346 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_VBR_HQ) {
1347 h264->adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
1348 h264->fmoMode = NV_ENC_H264_FMO_DISABLE;
1353 h264->qpPrimeYZeroTransformBypassFlag = 1;
1355 switch(
ctx->profile) {
1357 cc->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
1361 cc->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
1365 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
1368 #ifdef NVENC_HAVE_H264_10BIT_SUPPORT
1369 case NV_ENC_H264_PROFILE_HIGH_10:
1370 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_10_GUID;
1374 #ifdef NVENC_HAVE_422_SUPPORT
1375 case NV_ENC_H264_PROFILE_HIGH_422:
1376 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_422_GUID;
1381 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1387 #ifdef NVENC_HAVE_H264_10BIT_SUPPORT
1390 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_10_GUID;
1397 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1401 #ifdef NVENC_HAVE_422_SUPPORT
1404 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_422_GUID;
1413 h264->level =
ctx->level;
1415 #ifdef NVENC_HAVE_NEW_BIT_DEPTH_API
1416 h264->inputBitDepth =
IS_10BIT(
ctx->data_pix_fmt) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1417 h264->outputBitDepth = (
IS_10BIT(
ctx->data_pix_fmt) ||
ctx->highbitdepth) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1420 if (
ctx->coder >= 0)
1421 h264->entropyCodingMode =
ctx->coder;
1423 #ifdef NVENC_HAVE_BFRAME_REF_MODE
1424 if (
ctx->b_ref_mode >= 0)
1425 h264->useBFramesAsRef =
ctx->b_ref_mode;
1428 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1429 h264->numRefL0 = avctx->
refs;
1430 h264->numRefL1 = avctx->
refs;
1433 #ifdef NVENC_HAVE_H264_AND_AV1_TEMPORAL_FILTER
1434 if (
ctx->tf_level >= 0) {
1435 h264->tfLevel =
ctx->tf_level;
1437 switch (
ctx->tf_level)
1439 case NV_ENC_TEMPORAL_FILTER_LEVEL_0:
1440 case NV_ENC_TEMPORAL_FILTER_LEVEL_4:
1447 if (
ctx->encode_config.frameIntervalP < 5)
1452 #ifdef NVENC_HAVE_TIME_CODE
1454 h264->enableTimeCode = 1;
1463 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1464 NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig;
1465 NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui = &hevc->hevcVUIParameters;
1473 vui->videoFullRangeFlag = 0;
1482 vui->colourDescriptionPresentFlag =
1483 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1485 vui->videoSignalTypePresentFlag =
1486 (vui->colourDescriptionPresentFlag
1487 || vui->videoFormat != 5
1488 || vui->videoFullRangeFlag != 0);
1490 if (
ctx->max_slice_size > 0) {
1491 hevc->sliceMode = 1;
1492 hevc->sliceModeData =
ctx->max_slice_size;
1494 hevc->sliceMode = 3;
1495 hevc->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1498 if (
ctx->intra_refresh) {
1499 hevc->enableIntraRefresh = 1;
1500 hevc->intraRefreshPeriod = cc->gopLength;
1501 hevc->intraRefreshCnt = cc->gopLength - 1;
1502 cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1503 #ifdef NVENC_HAVE_HEVC_OUTPUT_RECOVERY_POINT_SEI
1504 hevc->outputRecoveryPointSEI = 1;
1506 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1507 hevc->singleSliceIntraRefresh =
ctx->single_slice_intra_refresh;
1511 #ifdef NVENC_HAVE_HEVC_AND_AV1_MASTERING_METADATA
1520 #ifdef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
1521 if (
ctx->constrained_encoding)
1522 hevc->enableConstrainedEncoding = 1;
1527 hevc->outputAUD =
ctx->aud;
1529 if (
ctx->dpb_size >= 0) {
1531 hevc->maxNumRefFramesInDPB =
ctx->dpb_size;
1534 hevc->idrPeriod = cc->gopLength;
1536 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1538 hevc->outputBufferingPeriodSEI =
ctx->cbr_padding;
1540 #ifdef NVENC_HAVE_FILLER_DATA
1541 hevc->enableFillerDataInsertion =
ctx->cbr_padding;
1545 hevc->outputPictureTimingSEI = 1;
1547 #ifdef NVENC_HAVE_MVHEVC
1554 ctx->profile = NV_ENC_HEVC_PROFILE_MULTIVIEW_MAIN;
1556 if (
ctx->profile == NV_ENC_HEVC_PROFILE_MULTIVIEW_MAIN && stereo3d &&
1567 switch (
ctx->profile) {
1569 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
1573 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1577 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1580 #ifdef NVENC_HAVE_MVHEVC
1581 case NV_ENC_HEVC_PROFILE_MULTIVIEW_MAIN:
1582 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
1586 hevc->enableMVHEVC = 1;
1587 hevc->outputHevc3DReferenceDisplayInfo = 1;
1596 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1602 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1606 #ifdef NVENC_HAVE_MVHEVC
1608 av_log(avctx,
AV_LOG_ERROR,
"Multiview encoding only works for Main profile content.\n");
1615 #ifdef NVENC_HAVE_NEW_BIT_DEPTH_API
1616 hevc->inputBitDepth =
IS_10BIT(
ctx->data_pix_fmt) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1617 hevc->outputBitDepth = (
IS_10BIT(
ctx->data_pix_fmt) ||
ctx->highbitdepth) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1619 hevc->pixelBitDepthMinus8 =
IS_10BIT(
ctx->data_pix_fmt) ? 2 : 0;
1622 hevc->level =
ctx->level;
1624 hevc->tier =
ctx->tier;
1626 #ifdef NVENC_HAVE_HEVC_BFRAME_REF_MODE
1627 if (
ctx->b_ref_mode >= 0)
1628 hevc->useBFramesAsRef =
ctx->b_ref_mode;
1631 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1632 hevc->numRefL0 = avctx->
refs;
1633 hevc->numRefL1 = avctx->
refs;
1636 #ifdef NVENC_HAVE_TEMPORAL_FILTER
1637 if (
ctx->tf_level >= 0) {
1638 hevc->tfLevel =
ctx->tf_level;
1640 switch (
ctx->tf_level)
1642 case NV_ENC_TEMPORAL_FILTER_LEVEL_0:
1643 case NV_ENC_TEMPORAL_FILTER_LEVEL_4:
1650 if (
ctx->encode_config.frameIntervalP < 5)
1658 #if CONFIG_AV1_NVENC_ENCODER
1662 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1663 NV_ENC_CONFIG_AV1 *av1 = &cc->encodeCodecConfig.av1Config;
1671 av1->colorRange = 0;
1681 av_log(avctx,
AV_LOG_ERROR,
"AV1 High Profile not supported, required for 4:4:4 encoding\n");
1684 cc->profileGUID = NV_ENC_AV1_PROFILE_MAIN_GUID;
1688 if (
ctx->dpb_size >= 0) {
1690 av1->maxNumRefFramesInDPB =
ctx->dpb_size;
1693 if (
ctx->intra_refresh) {
1694 av1->enableIntraRefresh = 1;
1695 av1->intraRefreshPeriod = cc->gopLength;
1696 av1->intraRefreshCnt = cc->gopLength - 1;
1697 cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1700 av1->idrPeriod = cc->gopLength;
1702 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1703 av1->enableBitstreamPadding =
ctx->cbr_padding;
1706 if (
ctx->tile_cols >= 0)
1707 av1->numTileColumns =
ctx->tile_cols;
1708 if (
ctx->tile_rows >= 0)
1709 av1->numTileRows =
ctx->tile_rows;
1711 av1->outputAnnexBFormat = 0;
1713 av1->level =
ctx->level;
1714 av1->tier =
ctx->tier;
1716 av1->enableTimingInfo =
ctx->timing_info;
1719 av1->disableSeqHdr = 0;
1720 av1->repeatSeqHdr = 1;
1722 av1->chromaFormatIDC =
IS_YUV444(
ctx->data_pix_fmt) ? 3 : 1;
1724 #ifdef NVENC_HAVE_NEW_BIT_DEPTH_API
1725 av1->inputBitDepth =
IS_10BIT(
ctx->data_pix_fmt) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1726 av1->outputBitDepth = (
IS_10BIT(
ctx->data_pix_fmt) ||
ctx->highbitdepth) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1728 av1->inputPixelBitDepthMinus8 =
IS_10BIT(
ctx->data_pix_fmt) ? 2 : 0;
1729 av1->pixelBitDepthMinus8 = (
IS_10BIT(
ctx->data_pix_fmt) ||
ctx->highbitdepth) ? 2 : 0;
1732 #ifdef NVENC_HAVE_HEVC_AND_AV1_MASTERING_METADATA
1741 if (
ctx->b_ref_mode >= 0)
1742 av1->useBFramesAsRef =
ctx->b_ref_mode;
1744 av1->numFwdRefs = avctx->
refs;
1745 av1->numBwdRefs = avctx->
refs;
1747 #ifdef NVENC_HAVE_H264_AND_AV1_TEMPORAL_FILTER
1748 if (
ctx->tf_level >= 0) {
1749 av1->tfLevel =
ctx->tf_level;
1751 switch (
ctx->tf_level)
1753 case NV_ENC_TEMPORAL_FILTER_LEVEL_0:
1754 case NV_ENC_TEMPORAL_FILTER_LEVEL_4:
1761 if (
ctx->encode_config.frameIntervalP < 5)
1777 #if CONFIG_AV1_NVENC_ENCODER
1779 return nvenc_setup_av1_config(avctx);
1793 #if CONFIG_AV1_NVENC_ENCODER
1824 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1826 NV_ENC_PRESET_CONFIG preset_config = { 0 };
1827 NVENCSTATUS nv_status = NV_ENC_SUCCESS;
1832 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1833 ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
1835 ctx->init_encode_params.encodeHeight = avctx->
height;
1836 ctx->init_encode_params.encodeWidth = avctx->
width;
1838 ctx->init_encode_params.encodeConfig = &
ctx->encode_config;
1840 preset_config.version = NV_ENC_PRESET_CONFIG_VER;
1841 preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
1843 #ifdef NVENC_HAVE_NEW_PRESETS
1844 ctx->init_encode_params.tuningInfo =
ctx->tuning_info;
1847 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOSSLESS;
1849 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOW_LATENCY;
1851 nv_status = p_nvenc->nvEncGetEncodePresetConfigEx(
ctx->nvencoder,
1852 ctx->init_encode_params.encodeGUID,
1853 ctx->init_encode_params.presetGUID,
1854 ctx->init_encode_params.tuningInfo,
1857 nv_status = p_nvenc->nvEncGetEncodePresetConfig(
ctx->nvencoder,
1858 ctx->init_encode_params.encodeGUID,
1859 ctx->init_encode_params.presetGUID,
1862 if (nv_status != NV_ENC_SUCCESS)
1863 return nvenc_print_error(avctx, nv_status,
"Cannot get the preset configuration");
1865 memcpy(&
ctx->encode_config, &preset_config.presetCfg,
sizeof(
ctx->encode_config));
1867 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1870 ctx->init_encode_params.darHeight = dh;
1871 ctx->init_encode_params.darWidth = dw;
1881 #ifdef NVENC_HAVE_UNIDIR_B
1882 ctx->init_encode_params.enableUniDirectionalB =
ctx->unidir_b;
1885 ctx->init_encode_params.enableEncodeAsync = 0;
1886 ctx->init_encode_params.enablePTD = 1;
1888 #ifdef NVENC_HAVE_NEW_PRESETS
1892 if (
ctx->rc_lookahead == 0 &&
ctx->encode_config.rcParams.enableLookahead)
1893 ctx->rc_lookahead =
ctx->encode_config.rcParams.lookaheadDepth;
1896 if (
ctx->weighted_pred == 1)
1897 ctx->init_encode_params.enableWeightedPrediction = 1;
1899 #ifdef NVENC_HAVE_SPLIT_FRAME_ENCODING
1900 ctx->init_encode_params.splitEncodeMode =
ctx->split_encode_mode;
1902 if (
ctx->split_encode_mode != NV_ENC_SPLIT_DISABLE_MODE) {
1904 av_log(avctx,
AV_LOG_WARNING,
"Split encoding not supported with weighted prediction enabled.\n");
1908 if (
ctx->bluray_compat) {
1917 ctx->level = NV_ENC_LEVEL_HEVC_51;
1918 ctx->tier = NV_ENC_TIER_HEVC_HIGH;
1927 ctx->encode_config.frameIntervalP = 0;
1928 ctx->encode_config.gopLength = 1;
1937 if(
ctx->single_slice_intra_refresh)
1938 ctx->intra_refresh = 1;
1947 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
1949 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
1960 nv_status = p_nvenc->nvEncInitializeEncoder(
ctx->nvencoder, &
ctx->init_encode_params);
1961 if (nv_status != NV_ENC_SUCCESS) {
1966 #ifdef NVENC_HAVE_CUSTREAM_PTR
1967 if (
ctx->cu_context) {
1968 nv_status = p_nvenc->nvEncSetIOCudaStreams(
ctx->nvencoder, &
ctx->cu_stream, &
ctx->cu_stream);
1969 if (nv_status != NV_ENC_SUCCESS) {
1980 if (
ctx->encode_config.frameIntervalP > 1)
1983 if (
ctx->encode_config.rcParams.averageBitRate > 0)
1984 avctx->
bit_rate =
ctx->encode_config.rcParams.averageBitRate;
1991 cpb_props->
buffer_size =
ctx->encode_config.rcParams.vbvBufferSize;
2000 return NV_ENC_BUFFER_FORMAT_YV12;
2002 return NV_ENC_BUFFER_FORMAT_NV12;
2005 return NV_ENC_BUFFER_FORMAT_YUV420_10BIT;
2008 return NV_ENC_BUFFER_FORMAT_YUV444;
2013 return NV_ENC_BUFFER_FORMAT_YUV444_10BIT;
2016 return NV_ENC_BUFFER_FORMAT_ARGB;
2019 return NV_ENC_BUFFER_FORMAT_ABGR;
2021 return NV_ENC_BUFFER_FORMAT_ARGB10;
2023 return NV_ENC_BUFFER_FORMAT_ABGR10;
2024 #ifdef NVENC_HAVE_422_SUPPORT
2026 return NV_ENC_BUFFER_FORMAT_NV16;
2029 return NV_ENC_BUFFER_FORMAT_P210;
2032 return NV_ENC_BUFFER_FORMAT_UNDEFINED;
2040 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2043 NVENCSTATUS nv_status;
2044 NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
2045 allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
2049 if (!
ctx->surfaces[idx].in_ref)
2052 NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
2055 if (
ctx->surfaces[idx].format == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
2061 allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
2062 allocSurf.width = avctx->
width;
2063 allocSurf.height = avctx->
height;
2064 allocSurf.bufferFmt =
ctx->surfaces[idx].format;
2066 nv_status = p_nvenc->nvEncCreateInputBuffer(
ctx->nvencoder, &allocSurf);
2067 if (nv_status != NV_ENC_SUCCESS) {
2071 ctx->surfaces[idx].input_surface = allocSurf.inputBuffer;
2072 ctx->surfaces[idx].width = allocSurf.width;
2073 ctx->surfaces[idx].height = allocSurf.height;
2076 nv_status = p_nvenc->nvEncCreateBitstreamBuffer(
ctx->nvencoder, &allocOut);
2077 if (nv_status != NV_ENC_SUCCESS) {
2080 p_nvenc->nvEncDestroyInputBuffer(
ctx->nvencoder,
ctx->surfaces[idx].input_surface);
2085 ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer;
2095 int i, res = 0, res2;
2101 ctx->frame_data_array =
av_calloc(
ctx->frame_data_array_nb,
sizeof(*
ctx->frame_data_array));
2102 if (!
ctx->frame_data_array)
2107 if (!
ctx->timestamp_list)
2111 if (!
ctx->unused_surface_queue)
2115 if (!
ctx->output_surface_queue)
2118 if (!
ctx->output_surface_ready_queue)
2125 for (
i = 0;
i <
ctx->nb_surfaces;
i++) {
2142 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2144 NVENCSTATUS nv_status;
2145 uint32_t outSize = 0;
2146 char tmpHeader[NV_MAX_SEQ_HDR_LEN];
2148 NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
2149 payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
2151 payload.spsppsBuffer = tmpHeader;
2152 payload.inBufferSize =
sizeof(tmpHeader);
2153 payload.outSPSPPSPayloadSize = &outSize;
2155 nv_status = p_nvenc->nvEncGetSequenceParams(
ctx->nvencoder, &payload);
2156 if (nv_status != NV_ENC_SUCCESS) {
2167 memcpy(avctx->
extradata, tmpHeader, outSize);
2176 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2180 if (
ctx->nvencoder) {
2181 NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER,
2182 .encodePicFlags = NV_ENC_PIC_FLAG_EOS };
2188 p_nvenc->nvEncEncodePicture(
ctx->nvencoder, ¶ms);
2196 if (
ctx->frame_data_array) {
2197 for (
i = 0;
i <
ctx->frame_data_array_nb;
i++)
2203 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
2204 if (
ctx->registered_frames[
i].mapped)
2205 p_nvenc->nvEncUnmapInputResource(
ctx->nvencoder,
ctx->registered_frames[
i].in_map.mappedResource);
2206 if (
ctx->registered_frames[
i].regptr)
2207 p_nvenc->nvEncUnregisterResource(
ctx->nvencoder,
ctx->registered_frames[
i].regptr);
2209 ctx->nb_registered_frames = 0;
2212 if (
ctx->surfaces) {
2213 for (
i = 0;
i <
ctx->nb_surfaces; ++
i) {
2215 p_nvenc->nvEncDestroyInputBuffer(
ctx->nvencoder,
ctx->surfaces[
i].input_surface);
2217 p_nvenc->nvEncDestroyBitstreamBuffer(
ctx->nvencoder,
ctx->surfaces[
i].output_surface);
2221 ctx->nb_surfaces = 0;
2227 if (
ctx->nvencoder) {
2228 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
2236 if (
ctx->cu_context_internal)
2238 ctx->cu_context =
ctx->cu_context_internal =
NULL;
2241 if (
ctx->d3d11_device) {
2242 ID3D11Device_Release(
ctx->d3d11_device);
2247 nvenc_free_functions(&dl_fn->
nvenc_dl);
2248 cuda_free_functions(&dl_fn->
cuda_dl);
2266 "hw_frames_ctx must be set when using GPU frames as input\n");
2272 "hw_frames_ctx must match the GPU frame type\n");
2321 NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params,
const AVFrame *
frame)
2323 int dst_linesize[4] = {
2324 lock_buffer_params->pitch,
2325 lock_buffer_params->pitch,
2326 lock_buffer_params->pitch,
2327 lock_buffer_params->pitch
2329 uint8_t *dst_data[4];
2333 dst_linesize[1] = dst_linesize[2] >>= 1;
2336 lock_buffer_params->bufferDataPtr, dst_linesize);
2341 FFSWAP(uint8_t*, dst_data[1], dst_data[2]);
2354 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2355 NVENCSTATUS nv_status;
2360 for (first_round = 1; first_round >= 0; first_round--) {
2361 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
2362 if (!
ctx->registered_frames[
i].mapped) {
2363 if (
ctx->registered_frames[
i].regptr) {
2366 nv_status = p_nvenc->nvEncUnregisterResource(
ctx->nvencoder,
ctx->registered_frames[
i].regptr);
2367 if (nv_status != NV_ENC_SUCCESS)
2368 return nvenc_print_error(avctx, nv_status,
"Failed unregistering unused input resource");
2369 ctx->registered_frames[
i].ptr =
NULL;
2370 ctx->registered_frames[
i].regptr =
NULL;
2377 return ctx->nb_registered_frames++;
2388 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2391 NV_ENC_REGISTER_RESOURCE reg = { 0 };
2394 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
2405 reg.version = NV_ENC_REGISTER_RESOURCE_VER;
2406 reg.width = frames_ctx->width;
2407 reg.height = frames_ctx->height;
2408 reg.pitch =
frame->linesize[0];
2409 reg.resourceToRegister =
frame->data[0];
2412 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR;
2415 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX;
2416 reg.subResourceIndex = (intptr_t)
frame->data[1];
2420 if (reg.bufferFormat == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
2426 ret = p_nvenc->nvEncRegisterResource(
ctx->nvencoder, ®);
2427 if (
ret != NV_ENC_SUCCESS) {
2432 ctx->registered_frames[idx].ptr =
frame->data[0];
2433 ctx->registered_frames[idx].ptr_index = reg.subResourceIndex;
2434 ctx->registered_frames[idx].regptr = reg.registeredResource;
2443 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2446 NVENCSTATUS nv_status;
2459 if (!
ctx->registered_frames[reg_idx].mapped) {
2460 ctx->registered_frames[reg_idx].in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER;
2461 ctx->registered_frames[reg_idx].in_map.registeredResource =
ctx->registered_frames[reg_idx].regptr;
2462 nv_status = p_nvenc->nvEncMapInputResource(
ctx->nvencoder, &
ctx->registered_frames[reg_idx].in_map);
2463 if (nv_status != NV_ENC_SUCCESS) {
2469 ctx->registered_frames[reg_idx].mapped += 1;
2471 nvenc_frame->
reg_idx = reg_idx;
2472 nvenc_frame->
input_surface =
ctx->registered_frames[reg_idx].in_map.mappedResource;
2473 nvenc_frame->
format =
ctx->registered_frames[reg_idx].in_map.mappedBufferFmt;
2478 NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
2480 lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
2483 nv_status = p_nvenc->nvEncLockInputBuffer(
ctx->nvencoder, &lockBufferParams);
2484 if (nv_status != NV_ENC_SUCCESS) {
2485 return nvenc_print_error(avctx, nv_status,
"Failed locking nvenc input buffer");
2488 nvenc_frame->
pitch = lockBufferParams.pitch;
2491 nv_status = p_nvenc->nvEncUnlockInputBuffer(
ctx->nvencoder, nvenc_frame->
input_surface);
2492 if (nv_status != NV_ENC_SUCCESS) {
2500 #ifdef NVENC_HAVE_TIME_CODE
2506 uint32_t *tc = (uint32_t*)sd->
data;
2511 time_code->displayPicStruct = NV_ENC_PIC_STRUCT_DISPLAY_FRAME;
2512 time_code->skipClockTimestampInsertion = 1;
2515 time_code->displayPicStruct = NV_ENC_PIC_STRUCT_DISPLAY_FRAME_DOUBLING;
2518 time_code->displayPicStruct = NV_ENC_PIC_STRUCT_DISPLAY_FRAME_TRIPLING;
2521 time_code->displayPicStruct = NV_ENC_PIC_STRUCT_DISPLAY_FRAME;
2525 for (
int i = 0;
i < cnt;
i++) {
2526 unsigned hh, mm,
ss, ff, drop;
2529 time_code->clockTimestamp[
i].countingType = 0;
2530 time_code->clockTimestamp[
i].discontinuityFlag = 0;
2531 time_code->clockTimestamp[
i].cntDroppedFrames = drop;
2532 time_code->clockTimestamp[
i].nFrames = ff;
2533 time_code->clockTimestamp[
i].secondsValue =
ss;
2534 time_code->clockTimestamp[
i].minutesValue = mm;
2535 time_code->clockTimestamp[
i].hoursValue = hh;
2536 time_code->clockTimestamp[
i].timeOffset = 0;
2539 time_code->displayPicStruct = NV_ENC_PIC_STRUCT_DISPLAY_FRAME;
2540 time_code->skipClockTimestampInsertion = 1;
2546 NV_ENC_PIC_PARAMS *params,
2547 NV_ENC_SEI_PAYLOAD *sei_data,
2554 params->codecPicParams.h264PicParams.sliceMode =
2555 ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
2556 params->codecPicParams.h264PicParams.sliceModeData =
2557 ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
2558 if (sei_count > 0) {
2559 params->codecPicParams.h264PicParams.seiPayloadArray = sei_data;
2560 params->codecPicParams.h264PicParams.seiPayloadArrayCnt = sei_count;
2563 #ifdef NVENC_HAVE_TIME_CODE
2565 nvenc_fill_time_code(avctx,
frame, ¶ms->codecPicParams.h264PicParams.timeCode);
2570 params->codecPicParams.hevcPicParams.sliceMode =
2571 ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
2572 params->codecPicParams.hevcPicParams.sliceModeData =
2573 ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
2574 if (sei_count > 0) {
2575 params->codecPicParams.hevcPicParams.seiPayloadArray = sei_data;
2576 params->codecPicParams.hevcPicParams.seiPayloadArrayCnt = sei_count;
2580 #if CONFIG_AV1_NVENC_ENCODER
2582 params->codecPicParams.av1PicParams.numTileColumns =
2583 ctx->encode_config.encodeCodecConfig.av1Config.numTileColumns;
2584 params->codecPicParams.av1PicParams.numTileRows =
2585 ctx->encode_config.encodeCodecConfig.av1Config.numTileRows;
2586 if (sei_count > 0) {
2587 params->codecPicParams.av1PicParams.obuPayloadArray = sei_data;
2588 params->codecPicParams.av1PicParams.obuPayloadArrayCnt = sei_count;
2619 NV_ENC_LOCK_BITSTREAM *params,
2626 pkt->
pts = params->outputTimeStamp;
2634 delay =
FFMAX(
ctx->encode_config.frameIntervalP - 1, 0);
2635 #ifdef NVENC_HAVE_MVHEVC
2636 delay *=
ctx->multiview ? 2 : 1;
2638 if (
ctx->output_frame_num >= delay) {
2640 ctx->output_frame_num++;
2644 delay_time =
ctx->initial_delay_time;
2652 delay_time = t1 - t2;
2657 delay_time = delay * (t3 - t2);
2661 ctx->initial_delay_time = delay_time;
2670 ctx->output_frame_num++;
2680 int idx =
ctx->frame_data_array_pos;
2695 ctx->frame_data_array_pos = (
ctx->frame_data_array_pos + 1) %
ctx->frame_data_array_nb;
2696 pic_params->inputDuration = idx;
2706 int idx = lock_params->outputDuration;
2726 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2728 NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
2729 NVENCSTATUS nv_status;
2734 lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
2736 lock_params.doNotWait = 0;
2739 nv_status = p_nvenc->nvEncLockBitstream(
ctx->nvencoder, &lock_params);
2740 if (nv_status != NV_ENC_SUCCESS) {
2752 memcpy(
pkt->
data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
2754 nv_status = p_nvenc->nvEncUnlockBitstream(
ctx->nvencoder, tmpoutsurf->
output_surface);
2755 if (nv_status != NV_ENC_SUCCESS) {
2756 res =
nvenc_print_error(avctx, nv_status,
"Failed unlocking bitstream buffer, expect the gates of mordor to open");
2762 ctx->registered_frames[tmpoutsurf->
reg_idx].mapped -= 1;
2763 if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped == 0) {
2764 nv_status = p_nvenc->nvEncUnmapInputResource(
ctx->nvencoder,
ctx->registered_frames[tmpoutsurf->
reg_idx].in_map.mappedResource);
2765 if (nv_status != NV_ENC_SUCCESS) {
2769 }
else if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped < 0) {
2779 switch (lock_params.pictureType) {
2780 case NV_ENC_PIC_TYPE_IDR:
2782 case NV_ENC_PIC_TYPE_I:
2785 case NV_ENC_PIC_TYPE_P:
2788 case NV_ENC_PIC_TYPE_B:
2791 case NV_ENC_PIC_TYPE_BI:
2795 av_log(avctx,
AV_LOG_ERROR,
"Unknown picture type encountered, expect the output to be broken.\n");
2796 av_log(avctx,
AV_LOG_ERROR,
"Please report this error and include as much information on how to reproduce it as possible.\n");
2824 int nb_ready, nb_pending;
2829 return nb_ready > 0;
2830 return (nb_ready > 0) && (nb_ready + nb_pending >=
ctx->async_depth);
2840 void *a53_data =
NULL;
2841 size_t a53_size = 0;
2849 &
ctx->sei_data_size,
2850 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2857 ctx->sei_data[sei_count].payloadSize = (uint32_t)a53_size;
2858 ctx->sei_data[sei_count].payload = (uint8_t*)a53_data;
2860 #if CONFIG_AV1_NVENC_ENCODER
2873 void *tc_data =
NULL;
2882 &
ctx->sei_data_size,
2883 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2890 ctx->sei_data[sei_count].payloadSize = (uint32_t)tc_size;
2891 ctx->sei_data[sei_count].payload = (uint8_t*)tc_data;
2893 #if CONFIG_AV1_NVENC_ENCODER
2908 for (
i = 0;
i <
frame->nb_side_data;
i++) {
2916 &
ctx->sei_data_size,
2917 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2923 ctx->sei_data[sei_count].payloadSize = side_data->
size;
2927 if (!
ctx->sei_data[sei_count].payload) {
2939 for (
i = 0;
i < sei_count;
i++)
2948 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
2951 NV_ENC_RECONFIGURE_PARAMS params = { 0 };
2952 int needs_reconfig = 0;
2953 int needs_encode_config = 0;
2954 int reconfig_bitrate = 0, reconfig_dar = 0;
2957 params.version = NV_ENC_RECONFIGURE_PARAMS_VER;
2958 params.reInitEncodeParams =
ctx->init_encode_params;
2961 if (dw !=
ctx->init_encode_params.darWidth || dh !=
ctx->init_encode_params.darHeight) {
2963 "aspect ratio change (DAR): %d:%d -> %d:%d\n",
2964 ctx->init_encode_params.darWidth,
2965 ctx->init_encode_params.darHeight, dw, dh);
2967 params.reInitEncodeParams.darHeight = dh;
2968 params.reInitEncodeParams.darWidth = dw;
2974 if (
ctx->rc != NV_ENC_PARAMS_RC_CONSTQP &&
ctx->support_dyn_bitrate) {
2975 if (avctx->
bit_rate > 0 && params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate != avctx->
bit_rate) {
2977 "avg bitrate change: %d -> %d\n",
2978 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate,
2981 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate = avctx->
bit_rate;
2982 reconfig_bitrate = 1;
2987 "max bitrate change: %d -> %d\n",
2988 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate,
2991 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate = avctx->
rc_max_rate;
2992 reconfig_bitrate = 1;
2997 "vbv buffer size change: %d -> %d\n",
2998 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize,
3001 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize = avctx->
rc_buffer_size;
3002 reconfig_bitrate = 1;
3005 if (reconfig_bitrate) {
3006 params.resetEncoder = 1;
3007 params.forceIDR = 1;
3009 needs_encode_config = 1;
3014 if (!needs_encode_config)
3015 params.reInitEncodeParams.encodeConfig =
NULL;
3017 if (needs_reconfig) {
3018 ret = p_nvenc->nvEncReconfigureEncoder(
ctx->nvencoder, ¶ms);
3019 if (
ret != NV_ENC_SUCCESS) {
3023 ctx->init_encode_params.darHeight = dh;
3024 ctx->init_encode_params.darWidth = dw;
3027 if (reconfig_bitrate) {
3028 ctx->encode_config.rcParams.averageBitRate = params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate;
3029 ctx->encode_config.rcParams.maxBitRate = params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate;
3030 ctx->encode_config.rcParams.vbvBufferSize = params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize;
3037 #ifdef NVENC_HAVE_HEVC_AND_AV1_MASTERING_METADATA
3039 MASTERING_DISPLAY_INFO *mastering_disp_info, CONTENT_LIGHT_LEVEL *content_light_level)
3043 if (
ctx->mdm ||
ctx->cll) {
3084 pic_params->codecPicParams.hevcPicParams.pMasteringDisplay = mastering_disp_info;
3086 pic_params->codecPicParams.av1PicParams.pMasteringDisplay = mastering_disp_info;
3093 content_light_level->maxContentLightLevel = cll->
MaxCLL;
3094 content_light_level->maxPicAverageLightLevel = cll->
MaxFALL;
3097 pic_params->codecPicParams.hevcPicParams.pMaxCll = content_light_level;
3099 pic_params->codecPicParams.av1PicParams.pMaxCll = content_light_level;
3111 NVENCSTATUS nv_status;
3116 #ifdef NVENC_HAVE_HEVC_AND_AV1_MASTERING_METADATA
3117 MASTERING_DISPLAY_INFO mastering_disp_info = { 0 };
3118 CONTENT_LIGHT_LEVEL content_light_level = { 0 };
3120 #ifdef NVENC_HAVE_MVHEVC
3121 HEVC_3D_REFERENCE_DISPLAY_INFO ref_disp_info = { 0 };
3126 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
3128 NV_ENC_PIC_PARAMS pic_params = { 0 };
3129 pic_params.version = NV_ENC_PIC_PARAMS_VER;
3131 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
3155 pic_params.bufferFmt = in_surf->
format;
3156 pic_params.inputWidth = in_surf->
width;
3157 pic_params.inputHeight = in_surf->
height;
3158 pic_params.inputPitch = in_surf->
pitch;
3163 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
3165 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
3167 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
3171 pic_params.encodePicFlags =
3172 ctx->forced_idr ? NV_ENC_PIC_FLAG_FORCEIDR : NV_ENC_PIC_FLAG_FORCEINTRA;
3174 pic_params.encodePicFlags = 0;
3177 pic_params.frameIdx =
ctx->frame_idx_counter++;
3178 pic_params.inputTimeStamp =
frame->pts;
3180 if (
ctx->extra_sei) {
3187 #ifdef NVENC_HAVE_HEVC_AND_AV1_MASTERING_METADATA
3188 res = nvenc_set_mastering_display_data(avctx,
frame, &pic_params, &mastering_disp_info, &content_light_level);
3193 #ifdef NVENC_HAVE_MVHEVC
3194 if (
ctx->multiview) {
3199 ctx->next_view_id = *(
int*)sd_view_id->
data;
3201 pic_params.codecPicParams.hevcPicParams.viewId =
ctx->next_view_id;
3225 pic_params.codecPicParams.hevcPicParams.p3DReferenceDisplayInfo = &ref_disp_info;
3226 ctx->display_sei_sent = 1;
3227 }
else if (!
ctx->display_sei_sent) {
3228 ref_disp_info.precRefDisplayWidth = 31;
3229 ref_disp_info.leftViewId[0] = 0;
3230 ref_disp_info.rightViewId[0] = 1;
3232 pic_params.codecPicParams.hevcPicParams.p3DReferenceDisplayInfo = &ref_disp_info;
3233 ctx->display_sei_sent = 1;
3236 ctx->next_view_id = !
ctx->next_view_id;
3246 pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
3253 nv_status = p_nvenc->nvEncEncodePicture(
ctx->nvencoder, &pic_params);
3255 for (
i = 0;
i < sei_count;
i++)
3262 if (nv_status != NV_ENC_SUCCESS &&
3263 nv_status != NV_ENC_ERR_NEED_MORE_INPUT)
3274 if (nv_status == NV_ENC_SUCCESS) {
3275 while (
av_fifo_read(
ctx->output_surface_queue, &tmp_out_surf, 1) >= 0)
3291 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
3294 if (!
frame->buf[0]) {
3339 ctx->output_frame_num = 0;
3340 ctx->initial_delay_time = 0;