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, 2)
267 const char *minver =
"(unknown)";
268 #elif NVENCAPI_CHECK_VERSION(13, 1)
269 const char *minver =
"610.00";
270 #elif NVENCAPI_CHECK_VERSION(13, 0)
271 const char *minver =
"570.0";
272 #elif NVENCAPI_CHECK_VERSION(12, 2)
273 # if defined(_WIN32) || defined(__CYGWIN__)
274 const char *minver =
"551.76";
276 const char *minver =
"550.54.14";
278 #elif NVENCAPI_CHECK_VERSION(12, 1)
279 # if defined(_WIN32) || defined(__CYGWIN__)
280 const char *minver =
"531.61";
282 const char *minver =
"530.41.03";
284 #elif NVENCAPI_CHECK_VERSION(12, 0)
285 # if defined(_WIN32) || defined(__CYGWIN__)
286 const char *minver =
"522.25";
288 const char *minver =
"520.56.06";
290 #elif NVENCAPI_CHECK_VERSION(11, 1)
291 # if defined(_WIN32) || defined(__CYGWIN__)
292 const char *minver =
"471.41";
294 const char *minver =
"470.57.02";
296 #elif NVENCAPI_CHECK_VERSION(11, 0)
297 # if defined(_WIN32) || defined(__CYGWIN__)
298 const char *minver =
"456.71";
300 const char *minver =
"455.28";
302 #elif NVENCAPI_CHECK_VERSION(10, 0)
303 # if defined(_WIN32) || defined(__CYGWIN__)
304 const char *minver =
"450.51";
306 const char *minver =
"445.87";
308 #elif NVENCAPI_CHECK_VERSION(9, 1)
309 # if defined(_WIN32) || defined(__CYGWIN__)
310 const char *minver =
"436.15";
312 const char *minver =
"435.21";
314 #elif NVENCAPI_CHECK_VERSION(9, 0)
315 # if defined(_WIN32) || defined(__CYGWIN__)
316 const char *minver =
"418.81";
318 const char *minver =
"418.30";
320 #elif NVENCAPI_CHECK_VERSION(8, 2)
321 # if defined(_WIN32) || defined(__CYGWIN__)
322 const char *minver =
"397.93";
324 const char *minver =
"396.24";
326 #elif NVENCAPI_CHECK_VERSION(8, 1)
327 # if defined(_WIN32) || defined(__CYGWIN__)
328 const char *minver =
"390.77";
330 const char *minver =
"390.25";
333 # if defined(_WIN32) || defined(__CYGWIN__)
334 const char *minver =
"378.66";
336 const char *minver =
"378.13";
339 av_log(avctx,
level,
"The minimum required Nvidia driver for nvenc is %s or newer\n", minver);
342 #if NVENCAPI_CHECK_VERSION(12, 0)
343 #define to_nv_color_matrix(n) (NV_ENC_VUI_MATRIX_COEFFS)(n)
344 #define to_nv_color_pri(n) (NV_ENC_VUI_COLOR_PRIMARIES)(n)
345 #define to_nv_color_trc(n) (NV_ENC_VUI_TRANSFER_CHARACTERISTIC)(n)
347 #define to_nv_color_matrix(n) (uint32_t)(n)
348 #define to_nv_color_pri(n) (uint32_t)(n)
349 #define to_nv_color_trc(n) (uint32_t)(n)
357 uint32_t nvenc_max_ver;
360 ret = cuda_load_functions(&dl_fn->
cuda_dl, avctx);
364 ret = nvenc_load_functions(&dl_fn->
nvenc_dl, avctx);
370 err = dl_fn->
nvenc_dl->NvEncodeAPIGetMaxSupportedVersion(&nvenc_max_ver);
371 if (err != NV_ENC_SUCCESS)
376 if ((NVENCAPI_MAJOR_VERSION << 4 | NVENCAPI_MINOR_VERSION) > nvenc_max_ver) {
377 av_log(avctx,
AV_LOG_ERROR,
"Driver does not support the required nvenc API version. "
378 "Required: %d.%d Found: %d.%d\n",
379 NVENCAPI_MAJOR_VERSION, NVENCAPI_MINOR_VERSION,
380 nvenc_max_ver >> 4, nvenc_max_ver & 0
xf);
385 dl_fn->
nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
388 if (err != NV_ENC_SUCCESS)
401 if (
ctx->d3d11_device)
413 if (
ctx->d3d11_device)
421 NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = { 0 };
423 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
426 params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
427 params.apiVersion = NVENCAPI_VERSION;
428 if (
ctx->d3d11_device) {
429 params.device =
ctx->d3d11_device;
430 params.deviceType = NV_ENC_DEVICE_TYPE_DIRECTX;
432 params.device =
ctx->cu_context;
433 params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
436 ret = p_nvenc->nvEncOpenEncodeSessionEx(¶ms, &
ctx->nvencoder);
437 if (
ret != NV_ENC_SUCCESS) {
448 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
449 int i,
ret, count = 0;
452 ret = p_nvenc->nvEncGetEncodeGUIDCount(
ctx->nvencoder, &count);
454 if (
ret != NV_ENC_SUCCESS || !count)
461 ret = p_nvenc->nvEncGetEncodeGUIDs(
ctx->nvencoder, guids, count, &count);
462 if (
ret != NV_ENC_SUCCESS) {
468 for (
i = 0;
i < count;
i++) {
469 if (!memcmp(&guids[
i], &
ctx->init_encode_params.encodeGUID,
sizeof(*guids))) {
484 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
485 NV_ENC_CAPS_PARAM params = { 0 };
488 params.version = NV_ENC_CAPS_PARAM_VER;
489 params.capsToQuery = cap;
491 ret = p_nvenc->nvEncGetEncodeCaps(
ctx->nvencoder,
ctx->init_encode_params.encodeGUID, ¶ms, &
val);
493 if (
ret == NV_ENC_SUCCESS)
515 #ifdef NVENC_HAVE_422_SUPPORT
532 if (ret < avctx->
width) {
539 if (ret < avctx->
height) {
546 if (ret < avctx->max_b_frames) {
556 "Interlaced encoding is not supported. Supported level: %d\n",
568 if (
ctx->rc_lookahead > 0 &&
ret <= 0) {
574 if (
ctx->temporal_aq > 0 &&
ret <= 0) {
580 if (
ctx->weighted_pred > 0 &&
ret <= 0) {
586 if (
ctx->coder == NV_ENC_H264_ENTROPY_CODING_MODE_CABAC &&
ret <= 0) {
591 #ifdef NVENC_HAVE_BFRAME_REF_MODE
592 tmp = (
ctx->b_ref_mode >= 0) ?
ctx->b_ref_mode : NV_ENC_BFRAME_REF_MODE_DISABLED;
595 case NV_ENC_BFRAME_REF_MODE_DISABLED:
597 case NV_ENC_BFRAME_REF_MODE_EACH:
603 case NV_ENC_BFRAME_REF_MODE_MIDDLE:
609 #ifdef NVENC_HAVE_AV1_HGOP_SUPPORT
610 case NV_ENC_BFRAME_REF_MODE_HIERARCHICAL:
622 tmp = (
ctx->b_ref_mode >= 0) ?
ctx->b_ref_mode : 0;
629 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
631 if(avctx->
refs != NV_ENC_NUM_REF_FRAMES_AUTOSELECT &&
ret <= 0) {
636 if(avctx->
refs != 0) {
642 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
644 if(
ctx->single_slice_intra_refresh &&
ret <= 0) {
649 if(
ctx->single_slice_intra_refresh) {
656 if((
ctx->intra_refresh ||
ctx->single_slice_intra_refresh) &&
ret <= 0) {
661 #ifndef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
669 if(
ctx->constrained_encoding &&
ret <= 0) {
674 #if defined(NVENC_HAVE_TEMPORAL_FILTER) || defined(NVENC_HAVE_H264_AND_AV1_TEMPORAL_FILTER)
676 if(
ctx->tf_level > 0 &&
ret <= 0) {
682 #ifdef NVENC_HAVE_LOOKAHEAD_LEVEL
684 if(
ctx->rc_lookahead > 0 &&
ctx->lookahead_level > 0 &&
685 ctx->lookahead_level != NV_ENC_LOOKAHEAD_LEVEL_AUTOSELECT &&
686 ctx->lookahead_level >
ret)
693 #ifdef NVENC_HAVE_UNIDIR_B
695 if(
ctx->unidir_b &&
ret <= 0) {
701 ctx->support_dyn_bitrate =
nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE);
703 #ifdef NVENC_HAVE_MVHEVC
704 ctx->multiview_supported =
nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_MVHEVC_ENCODE) > 0;
706 ctx->profile == NV_ENC_HEVC_PROFILE_MULTIVIEW_MAIN &&
707 !
ctx->multiview_supported) {
720 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
721 char name[128] = { 0};
722 int major, minor,
ret;
741 av_log(avctx, loglevel,
"[ GPU #%d - < %s > has Compute SM %d.%d ]\n", idx,
name, major, minor);
742 if (((major << 4) | minor) <
NVENC_CAP) {
743 av_log(avctx, loglevel,
"does not support NVENC\n");
754 ctx->cu_context =
ctx->cu_context_internal;
766 av_log(avctx, loglevel,
"supports NVENC\n");
777 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
785 ctx->cu_context_internal =
NULL;
798 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_H264_GUID;
801 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_HEVC_GUID;
803 #if CONFIG_AV1_NVENC_ENCODER
805 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_AV1_GUID;
815 av_log(avctx,
AV_LOG_WARNING,
"The selected preset is deprecated. Use p1 to p7 + -tune or fast/medium/slow.\n");
839 cuda_device_hwctx = hwdev_ctx->
hwctx;
842 d3d11_device_hwctx = hwdev_ctx->
hwctx;
850 if (cuda_device_hwctx) {
852 ctx->cu_stream = cuda_device_hwctx->
stream;
855 else if (d3d11_device_hwctx) {
856 ctx->d3d11_device = d3d11_device_hwctx->
device;
857 ID3D11Device_AddRef(
ctx->d3d11_device);
867 av_log(avctx,
AV_LOG_FATAL,
"Provided device doesn't support required NVENC features\n");
871 int i, nb_devices = 0;
887 for (
i = 0;
i < nb_devices; ++
i) {
900 av_log(avctx,
AV_LOG_FATAL,
"Requested GPU %d, but only %d GPUs are available!\n",
ctx->device, nb_devices);
910 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
911 #if CONFIG_AV1_NVENC_ENCODER
917 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
919 if (
ctx->init_qp_p >= 0) {
920 rc->constQP.qpInterP =
ctx->init_qp_p;
921 if (
ctx->init_qp_i >= 0 &&
ctx->init_qp_b >= 0) {
922 rc->constQP.qpIntra =
ctx->init_qp_i;
923 rc->constQP.qpInterB =
ctx->init_qp_b;
927 rc->constQP.qpInterB =
av_clip(
930 rc->constQP.qpIntra = rc->constQP.qpInterP;
931 rc->constQP.qpInterB = rc->constQP.qpInterP;
933 }
else if (
ctx->cqp >= 0) {
934 rc->constQP.qpInterP = rc->constQP.qpInterB = rc->constQP.qpIntra =
ctx->cqp;
948 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
950 #if CONFIG_AV1_NVENC_ENCODER
956 if (avctx->
qmin >= 0 || avctx->
qmax >= 0)
957 av_log(avctx,
AV_LOG_WARNING,
"Passing qmin/qmax via global AVCodecContext options. Use encoder options instead.\n");
959 if (avctx->
qmin >= 0 &&
ctx->qmin < 0)
961 if (avctx->
qmax >= 0 &&
ctx->qmax < 0)
966 if (
ctx->qmin >= 0 &&
ctx->qmax >= 0) {
970 rc->minQP.qpInterB =
ctx->qmin;
971 rc->minQP.qpInterP =
ctx->qmin;
972 rc->minQP.qpIntra =
ctx->qmin;
974 rc->maxQP.qpInterB =
ctx->qmax;
975 rc->maxQP.qpInterP =
ctx->qmax;
976 rc->maxQP.qpIntra =
ctx->qmax;
978 qp_inter_p = (
ctx->qmax + 3 *
ctx->qmin) / 4;
979 }
else if (
ctx->qmin >= 0) {
982 rc->minQP.qpInterB =
ctx->qmin;
983 rc->minQP.qpInterP =
ctx->qmin;
984 rc->minQP.qpIntra =
ctx->qmin;
986 qp_inter_p =
ctx->qmin;
991 rc->enableInitialRCQP = 1;
993 if (
ctx->init_qp_p < 0) {
994 rc->initialRCQP.qpInterP = qp_inter_p;
996 rc->initialRCQP.qpInterP =
ctx->init_qp_p;
999 if (
ctx->init_qp_i < 0) {
1001 rc->initialRCQP.qpIntra =
av_clip(
1004 rc->initialRCQP.qpIntra = rc->initialRCQP.qpInterP;
1007 rc->initialRCQP.qpIntra =
ctx->init_qp_i;
1010 if (
ctx->init_qp_b < 0) {
1012 rc->initialRCQP.qpInterB =
av_clip(
1015 rc->initialRCQP.qpInterB = rc->initialRCQP.qpInterP;
1018 rc->initialRCQP.qpInterB =
ctx->init_qp_b;
1025 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
1027 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
1028 rc->constQP.qpInterB = 0;
1029 rc->constQP.qpInterP = 0;
1030 rc->constQP.qpIntra = 0;
1039 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
1042 case NV_ENC_PARAMS_RC_CONSTQP:
1045 #ifndef NVENC_NO_DEPRECATED_RC
1046 case NV_ENC_PARAMS_RC_VBR_MINQP:
1047 if (avctx->
qmin < 0 &&
ctx->qmin < 0) {
1049 "The variable bitrate rate-control requires "
1050 "the 'qmin' option set.\n");
1055 case NV_ENC_PARAMS_RC_VBR_HQ:
1057 case NV_ENC_PARAMS_RC_VBR:
1060 case NV_ENC_PARAMS_RC_CBR:
1061 #ifndef NVENC_NO_DEPRECATED_RC
1062 case NV_ENC_PARAMS_RC_CBR_HQ:
1063 case NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ:
1068 rc->rateControlMode =
ctx->rc;
1077 int nb_surfaces =
FFMAX(4,
ctx->encode_config.frameIntervalP * 2 * 2);
1080 if (
ctx->rc_lookahead > 0) {
1083 nb_surfaces =
FFMAX(1,
FFMAX(nb_surfaces,
ctx->rc_lookahead +
ctx->encode_config.frameIntervalP + 1 + 4));
1084 if (nb_surfaces >
ctx->nb_surfaces &&
ctx->nb_surfaces > 0)
1087 "Defined rc_lookahead requires more surfaces, "
1088 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
1090 ctx->nb_surfaces =
FFMAX(nb_surfaces,
ctx->nb_surfaces);
1092 if (
ctx->encode_config.frameIntervalP > 1 &&
ctx->nb_surfaces < nb_surfaces &&
ctx->nb_surfaces > 0)
1095 "Defined b-frame requires more surfaces, "
1096 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
1097 ctx->nb_surfaces =
FFMAX(
ctx->nb_surfaces, nb_surfaces);
1099 else if (
ctx->nb_surfaces <= 0)
1100 ctx->nb_surfaces = nb_surfaces;
1105 ctx->async_depth =
FFMIN(
ctx->async_depth,
ctx->nb_surfaces - 1);
1109 ctx->frame_data_array_nb =
FFMAX(
ctx->nb_surfaces,
ctx->nb_surfaces +
ctx->encode_config.frameIntervalP - 1);
1119 av_log(avctx,
AV_LOG_WARNING,
"Using global_quality with nvenc is deprecated. Use qp instead.\n");
1125 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate;
1126 }
else if (
ctx->encode_config.rcParams.averageBitRate > 0) {
1127 ctx->encode_config.rcParams.maxBitRate =
ctx->encode_config.rcParams.averageBitRate;
1133 #ifdef NVENC_HAVE_MULTIPASS
1134 ctx->encode_config.rcParams.multiPass =
ctx->multipass;
1137 ctx->encode_config.rcParams.multiPass = NV_ENC_MULTI_PASS_DISABLED;
1139 ctx->encode_config.rcParams.multiPass = NV_ENC_TWO_PASS_FULL_RESOLUTION;
1143 ctx->rc = NV_ENC_PARAMS_RC_CBR;
1144 }
else if (
ctx->cqp >= 0) {
1145 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
1146 }
else if (
ctx->quality >= 0.0f) {
1147 ctx->rc = NV_ENC_PARAMS_RC_VBR;
1157 if (
ctx->twopass < 0)
1162 ctx->rc = NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ;
1164 ctx->rc = NV_ENC_PARAMS_RC_CBR;
1166 }
else if (
ctx->cqp >= 0) {
1167 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
1168 }
else if (
ctx->twopass) {
1169 ctx->rc = NV_ENC_PARAMS_RC_VBR_HQ;
1170 }
else if ((avctx->
qmin >= 0 && avctx->
qmax >= 0) ||
1171 (
ctx->qmin >= 0 &&
ctx->qmax >= 0)) {
1172 ctx->rc = NV_ENC_PARAMS_RC_VBR_MINQP;
1184 #ifdef NVENC_HAVE_QP_CHROMA_OFFSETS
1185 ctx->encode_config.rcParams.cbQPIndexOffset =
ctx->qp_cb_offset;
1186 ctx->encode_config.rcParams.crQPIndexOffset =
ctx->qp_cr_offset;
1189 ctx->qp_cr_offset !=
ctx->qp_cb_offset)
1191 "av1_nvenc: qp_cr_offset is currently ignored by the NVENC driver "
1192 "(deltaQ_v_ac is forced equal to deltaQ_u_ac); only qp_cb_offset "
1195 if (
ctx->qp_cb_offset ||
ctx->qp_cr_offset)
1196 av_log(avctx,
AV_LOG_WARNING,
"Failed setting QP CB/CR offsets, SDK 11.1 or greater required at compile time.\n");
1199 #ifdef NVENC_HAVE_LDKFS
1201 ctx->encode_config.rcParams.lowDelayKeyFrameScale =
ctx->ldkfs;
1206 }
else if (
ctx->rc >= 0) {
1209 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
1215 }
else if (
ctx->encode_config.rcParams.averageBitRate > 0) {
1216 avctx->
rc_buffer_size =
ctx->encode_config.rcParams.vbvBufferSize = 2 *
ctx->encode_config.rcParams.averageBitRate;
1220 ctx->encode_config.rcParams.enableAQ = 1;
1221 ctx->encode_config.rcParams.aqStrength =
ctx->aq_strength;
1225 if (
ctx->temporal_aq) {
1226 ctx->encode_config.rcParams.enableTemporalAQ = 1;
1230 if (
ctx->rc_lookahead > 0) {
1231 int lkd_bound =
FFMIN(
ctx->nb_surfaces,
ctx->async_depth) -
1232 ctx->encode_config.frameIntervalP - 4;
1234 if (lkd_bound < 0) {
1235 ctx->encode_config.rcParams.enableLookahead = 0;
1237 "Lookahead not enabled. Increase buffer delay (-delay).\n");
1239 ctx->encode_config.rcParams.enableLookahead = 1;
1240 ctx->encode_config.rcParams.lookaheadDepth =
av_clip(
ctx->rc_lookahead, 0, lkd_bound);
1241 ctx->encode_config.rcParams.disableIadapt =
ctx->no_scenecut;
1242 ctx->encode_config.rcParams.disableBadapt = !
ctx->b_adapt;
1244 "Lookahead enabled: depth %d, scenecut %s, B-adapt %s.\n",
1245 ctx->encode_config.rcParams.lookaheadDepth,
1246 ctx->encode_config.rcParams.disableIadapt ?
"disabled" :
"enabled",
1247 ctx->encode_config.rcParams.disableBadapt ?
"disabled" :
"enabled");
1248 if (
ctx->encode_config.rcParams.lookaheadDepth <
ctx->rc_lookahead)
1249 av_log(avctx,
AV_LOG_WARNING,
"Clipping lookahead depth to %d (from %d) due to lack of surfaces/delay",
1250 ctx->encode_config.rcParams.lookaheadDepth,
ctx->rc_lookahead);
1252 #ifdef NVENC_HAVE_LOOKAHEAD_LEVEL
1253 if (
ctx->lookahead_level >= 0) {
1254 switch (
ctx->lookahead_level) {
1255 case NV_ENC_LOOKAHEAD_LEVEL_0:
1256 case NV_ENC_LOOKAHEAD_LEVEL_1:
1257 case NV_ENC_LOOKAHEAD_LEVEL_2:
1258 case NV_ENC_LOOKAHEAD_LEVEL_3:
1259 case NV_ENC_LOOKAHEAD_LEVEL_AUTOSELECT:
1266 ctx->encode_config.rcParams.lookaheadLevel =
ctx->lookahead_level;
1272 if (
ctx->strict_gop) {
1273 ctx->encode_config.rcParams.strictGOPTarget = 1;
1278 ctx->encode_config.rcParams.enableNonRefP = 1;
1280 if (
ctx->zerolatency)
1281 ctx->encode_config.rcParams.zeroReorderDelay = 1;
1285 int tmp_quality = (int)(
ctx->quality * 256.0f);
1286 ctx->encode_config.rcParams.targetQuality = (uint8_t)(tmp_quality >> 8);
1287 ctx->encode_config.rcParams.targetQualityLSB = (uint8_t)(tmp_quality & 0xff);
1292 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate = 0;
1303 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1304 NV_ENC_CONFIG_H264 *h264 = &cc->encodeCodecConfig.h264Config;
1305 NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui = &h264->h264VUIParameters;
1313 vui->videoFullRangeFlag = 0;
1322 vui->colourDescriptionPresentFlag =
1323 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1325 vui->videoSignalTypePresentFlag =
1326 (vui->colourDescriptionPresentFlag
1327 || vui->videoFormat != 5
1328 || vui->videoFullRangeFlag != 0);
1330 if (
ctx->max_slice_size > 0) {
1331 h264->sliceMode = 1;
1332 h264->sliceModeData =
ctx->max_slice_size;
1334 h264->sliceMode = 3;
1335 h264->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1338 if (
ctx->intra_refresh) {
1339 h264->enableIntraRefresh = 1;
1340 h264->intraRefreshPeriod = cc->gopLength;
1341 h264->intraRefreshCnt = cc->gopLength - 1;
1342 cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1343 h264->outputRecoveryPointSEI = 1;
1344 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1345 h264->singleSliceIntraRefresh =
ctx->single_slice_intra_refresh;
1349 if (
ctx->constrained_encoding)
1350 h264->enableConstrainedEncoding = 1;
1354 h264->outputAUD =
ctx->aud;
1356 if (
ctx->dpb_size >= 0) {
1358 h264->maxNumRefFrames =
ctx->dpb_size;
1361 h264->idrPeriod = cc->gopLength;
1363 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1365 h264->outputBufferingPeriodSEI =
ctx->cbr_padding;
1367 #ifdef NVENC_HAVE_FILLER_DATA
1368 h264->enableFillerDataInsertion =
ctx->cbr_padding;
1372 h264->outputPictureTimingSEI = 1;
1374 #ifndef NVENC_NO_DEPRECATED_RC
1375 if (cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ ||
1376 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_HQ ||
1377 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_VBR_HQ) {
1378 h264->adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
1379 h264->fmoMode = NV_ENC_H264_FMO_DISABLE;
1384 h264->qpPrimeYZeroTransformBypassFlag = 1;
1386 switch(
ctx->profile) {
1388 cc->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
1390 if (cc->frameIntervalP > 1) {
1392 "B-frames are not supported by H.264 Baseline profile, disabling.\n");
1393 cc->frameIntervalP = 1;
1397 cc->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
1401 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
1404 #ifdef NVENC_HAVE_H264_10BIT_SUPPORT
1405 case NV_ENC_H264_PROFILE_HIGH_10:
1406 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_10_GUID;
1410 #ifdef NVENC_HAVE_422_SUPPORT
1411 case NV_ENC_H264_PROFILE_HIGH_422:
1412 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_422_GUID;
1417 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1423 #ifdef NVENC_HAVE_H264_10BIT_SUPPORT
1426 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_10_GUID;
1433 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1437 #ifdef NVENC_HAVE_422_SUPPORT
1440 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_422_GUID;
1449 h264->level =
ctx->level;
1451 #ifdef NVENC_HAVE_NEW_BIT_DEPTH_API
1452 h264->inputBitDepth =
IS_10BIT(
ctx->data_pix_fmt) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1453 h264->outputBitDepth = (
IS_10BIT(
ctx->data_pix_fmt) ||
ctx->highbitdepth) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1456 if (
ctx->coder >= 0)
1457 h264->entropyCodingMode =
ctx->coder;
1459 #ifdef NVENC_HAVE_BFRAME_REF_MODE
1460 if (
ctx->b_ref_mode >= 0)
1461 h264->useBFramesAsRef =
ctx->b_ref_mode;
1464 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1465 h264->numRefL0 = avctx->
refs;
1466 h264->numRefL1 = avctx->
refs;
1469 #ifdef NVENC_HAVE_H264_AND_AV1_TEMPORAL_FILTER
1470 if (
ctx->tf_level >= 0) {
1471 h264->tfLevel =
ctx->tf_level;
1473 switch (
ctx->tf_level)
1475 case NV_ENC_TEMPORAL_FILTER_LEVEL_0:
1476 case NV_ENC_TEMPORAL_FILTER_LEVEL_4:
1483 if (
ctx->encode_config.frameIntervalP < 5)
1488 #ifdef NVENC_HAVE_TIME_CODE
1490 h264->enableTimeCode = 1;
1499 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1500 NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig;
1501 NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui = &hevc->hevcVUIParameters;
1509 vui->videoFullRangeFlag = 0;
1518 vui->colourDescriptionPresentFlag =
1519 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1521 vui->videoSignalTypePresentFlag =
1522 (vui->colourDescriptionPresentFlag
1523 || vui->videoFormat != 5
1524 || vui->videoFullRangeFlag != 0);
1526 if (
ctx->max_slice_size > 0) {
1527 hevc->sliceMode = 1;
1528 hevc->sliceModeData =
ctx->max_slice_size;
1530 hevc->sliceMode = 3;
1531 hevc->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1534 if (
ctx->intra_refresh) {
1535 hevc->enableIntraRefresh = 1;
1536 hevc->intraRefreshPeriod = cc->gopLength;
1537 hevc->intraRefreshCnt = cc->gopLength - 1;
1538 cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1539 #ifdef NVENC_HAVE_HEVC_OUTPUT_RECOVERY_POINT_SEI
1540 hevc->outputRecoveryPointSEI = 1;
1542 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1543 hevc->singleSliceIntraRefresh =
ctx->single_slice_intra_refresh;
1547 #ifdef NVENC_HAVE_HEVC_AND_AV1_MASTERING_METADATA
1556 #ifdef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
1557 if (
ctx->constrained_encoding)
1558 hevc->enableConstrainedEncoding = 1;
1563 hevc->outputAUD =
ctx->aud;
1565 if (
ctx->dpb_size >= 0) {
1567 hevc->maxNumRefFramesInDPB =
ctx->dpb_size;
1570 hevc->idrPeriod = cc->gopLength;
1572 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1574 hevc->outputBufferingPeriodSEI =
ctx->cbr_padding;
1576 #ifdef NVENC_HAVE_FILLER_DATA
1577 hevc->enableFillerDataInsertion =
ctx->cbr_padding;
1581 hevc->outputPictureTimingSEI = 1;
1583 #ifdef NVENC_HAVE_MVHEVC
1590 ctx->profile = NV_ENC_HEVC_PROFILE_MULTIVIEW_MAIN;
1592 if (
ctx->profile == NV_ENC_HEVC_PROFILE_MULTIVIEW_MAIN && stereo3d &&
1603 switch (
ctx->profile) {
1605 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
1609 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1613 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1616 #ifdef NVENC_HAVE_MVHEVC
1617 case NV_ENC_HEVC_PROFILE_MULTIVIEW_MAIN:
1618 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
1622 hevc->enableMVHEVC = 1;
1623 hevc->outputHevc3DReferenceDisplayInfo = 1;
1632 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1638 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1642 #ifdef NVENC_HAVE_MVHEVC
1644 av_log(avctx,
AV_LOG_ERROR,
"Multiview encoding only works for Main profile content.\n");
1651 #ifdef NVENC_HAVE_NEW_BIT_DEPTH_API
1652 hevc->inputBitDepth =
IS_10BIT(
ctx->data_pix_fmt) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1653 hevc->outputBitDepth = (
IS_10BIT(
ctx->data_pix_fmt) ||
ctx->highbitdepth) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1655 hevc->pixelBitDepthMinus8 =
IS_10BIT(
ctx->data_pix_fmt) ? 2 : 0;
1658 hevc->level =
ctx->level;
1660 hevc->tier =
ctx->tier;
1662 #ifdef NVENC_HAVE_HEVC_BFRAME_REF_MODE
1663 if (
ctx->b_ref_mode >= 0)
1664 hevc->useBFramesAsRef =
ctx->b_ref_mode;
1667 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1668 hevc->numRefL0 = avctx->
refs;
1669 hevc->numRefL1 = avctx->
refs;
1672 #ifdef NVENC_HAVE_TEMPORAL_FILTER
1673 if (
ctx->tf_level >= 0) {
1674 hevc->tfLevel =
ctx->tf_level;
1676 switch (
ctx->tf_level)
1678 case NV_ENC_TEMPORAL_FILTER_LEVEL_0:
1679 case NV_ENC_TEMPORAL_FILTER_LEVEL_4:
1686 if (
ctx->encode_config.frameIntervalP < 5)
1694 #if CONFIG_AV1_NVENC_ENCODER
1698 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1699 NV_ENC_CONFIG_AV1 *av1 = &cc->encodeCodecConfig.av1Config;
1707 av1->colorRange = 0;
1717 av_log(avctx,
AV_LOG_ERROR,
"AV1 High Profile not supported, required for 4:4:4 encoding\n");
1720 cc->profileGUID = NV_ENC_AV1_PROFILE_MAIN_GUID;
1724 if (
ctx->dpb_size >= 0) {
1726 av1->maxNumRefFramesInDPB =
ctx->dpb_size;
1729 if (
ctx->intra_refresh) {
1730 av1->enableIntraRefresh = 1;
1731 av1->intraRefreshPeriod = cc->gopLength;
1732 av1->intraRefreshCnt = cc->gopLength - 1;
1733 cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1736 av1->idrPeriod = cc->gopLength;
1738 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1739 av1->enableBitstreamPadding =
ctx->cbr_padding;
1742 if (
ctx->tile_cols >= 0)
1743 av1->numTileColumns =
ctx->tile_cols;
1744 if (
ctx->tile_rows >= 0)
1745 av1->numTileRows =
ctx->tile_rows;
1747 av1->outputAnnexBFormat = 0;
1749 av1->level =
ctx->level;
1750 av1->tier =
ctx->tier;
1752 av1->enableTimingInfo =
ctx->timing_info;
1755 av1->disableSeqHdr = 0;
1756 av1->repeatSeqHdr = 1;
1758 av1->chromaFormatIDC =
IS_YUV444(
ctx->data_pix_fmt) ? 3 : 1;
1760 #ifdef NVENC_HAVE_NEW_BIT_DEPTH_API
1761 av1->inputBitDepth =
IS_10BIT(
ctx->data_pix_fmt) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1762 av1->outputBitDepth = (
IS_10BIT(
ctx->data_pix_fmt) ||
ctx->highbitdepth) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1764 av1->inputPixelBitDepthMinus8 =
IS_10BIT(
ctx->data_pix_fmt) ? 2 : 0;
1765 av1->pixelBitDepthMinus8 = (
IS_10BIT(
ctx->data_pix_fmt) ||
ctx->highbitdepth) ? 2 : 0;
1768 #ifdef NVENC_HAVE_HEVC_AND_AV1_MASTERING_METADATA
1777 if (
ctx->b_ref_mode >= 0)
1778 av1->useBFramesAsRef =
ctx->b_ref_mode;
1780 av1->numFwdRefs = avctx->
refs;
1781 av1->numBwdRefs = avctx->
refs;
1783 #ifdef NVENC_HAVE_H264_AND_AV1_TEMPORAL_FILTER
1784 if (
ctx->tf_level >= 0) {
1785 av1->tfLevel =
ctx->tf_level;
1787 switch (
ctx->tf_level)
1789 case NV_ENC_TEMPORAL_FILTER_LEVEL_0:
1790 case NV_ENC_TEMPORAL_FILTER_LEVEL_4:
1797 if (
ctx->encode_config.frameIntervalP < 5)
1813 #if CONFIG_AV1_NVENC_ENCODER
1815 return nvenc_setup_av1_config(avctx);
1829 #if CONFIG_AV1_NVENC_ENCODER
1860 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1862 NV_ENC_PRESET_CONFIG preset_config = { 0 };
1863 NVENCSTATUS nv_status = NV_ENC_SUCCESS;
1868 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1869 ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
1871 ctx->init_encode_params.encodeHeight = avctx->
height;
1872 ctx->init_encode_params.encodeWidth = avctx->
width;
1874 ctx->init_encode_params.encodeConfig = &
ctx->encode_config;
1876 preset_config.version = NV_ENC_PRESET_CONFIG_VER;
1877 preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
1879 #ifdef NVENC_HAVE_NEW_PRESETS
1880 ctx->init_encode_params.tuningInfo =
ctx->tuning_info;
1883 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOSSLESS;
1885 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOW_LATENCY;
1887 nv_status = p_nvenc->nvEncGetEncodePresetConfigEx(
ctx->nvencoder,
1888 ctx->init_encode_params.encodeGUID,
1889 ctx->init_encode_params.presetGUID,
1890 ctx->init_encode_params.tuningInfo,
1893 nv_status = p_nvenc->nvEncGetEncodePresetConfig(
ctx->nvencoder,
1894 ctx->init_encode_params.encodeGUID,
1895 ctx->init_encode_params.presetGUID,
1898 if (nv_status != NV_ENC_SUCCESS)
1899 return nvenc_print_error(avctx, nv_status,
"Cannot get the preset configuration");
1901 memcpy(&
ctx->encode_config, &preset_config.presetCfg,
sizeof(
ctx->encode_config));
1903 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1906 ctx->init_encode_params.darHeight = dh;
1907 ctx->init_encode_params.darWidth = dw;
1917 #ifdef NVENC_HAVE_UNIDIR_B
1918 ctx->init_encode_params.enableUniDirectionalB =
ctx->unidir_b;
1921 ctx->init_encode_params.enableEncodeAsync = 0;
1922 ctx->init_encode_params.enablePTD = 1;
1924 #ifdef NVENC_HAVE_NEW_PRESETS
1928 if (
ctx->rc_lookahead == 0 &&
ctx->encode_config.rcParams.enableLookahead)
1929 ctx->rc_lookahead =
ctx->encode_config.rcParams.lookaheadDepth;
1932 if (
ctx->weighted_pred == 1)
1933 ctx->init_encode_params.enableWeightedPrediction = 1;
1935 #ifdef NVENC_HAVE_SPLIT_FRAME_ENCODING
1936 ctx->init_encode_params.splitEncodeMode =
ctx->split_encode_mode;
1938 if (
ctx->split_encode_mode != NV_ENC_SPLIT_DISABLE_MODE) {
1940 av_log(avctx,
AV_LOG_WARNING,
"Split encoding not supported with weighted prediction enabled.\n");
1944 if (
ctx->bluray_compat) {
1953 ctx->level = NV_ENC_LEVEL_HEVC_51;
1954 ctx->tier = NV_ENC_TIER_HEVC_HIGH;
1963 ctx->encode_config.frameIntervalP = 0;
1964 ctx->encode_config.gopLength = 1;
1973 if(
ctx->single_slice_intra_refresh)
1974 ctx->intra_refresh = 1;
1983 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
1985 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
1996 nv_status = p_nvenc->nvEncInitializeEncoder(
ctx->nvencoder, &
ctx->init_encode_params);
1997 if (nv_status != NV_ENC_SUCCESS) {
2002 #ifdef NVENC_HAVE_CUSTREAM_PTR
2003 if (
ctx->cu_context) {
2004 nv_status = p_nvenc->nvEncSetIOCudaStreams(
ctx->nvencoder, &
ctx->cu_stream, &
ctx->cu_stream);
2005 if (nv_status != NV_ENC_SUCCESS) {
2016 if (
ctx->encode_config.frameIntervalP > 1)
2019 if (
ctx->encode_config.rcParams.averageBitRate > 0)
2020 avctx->
bit_rate =
ctx->encode_config.rcParams.averageBitRate;
2027 cpb_props->
buffer_size =
ctx->encode_config.rcParams.vbvBufferSize;
2036 return NV_ENC_BUFFER_FORMAT_YV12;
2038 return NV_ENC_BUFFER_FORMAT_NV12;
2041 return NV_ENC_BUFFER_FORMAT_YUV420_10BIT;
2044 return NV_ENC_BUFFER_FORMAT_YUV444;
2049 return NV_ENC_BUFFER_FORMAT_YUV444_10BIT;
2052 return NV_ENC_BUFFER_FORMAT_ARGB;
2055 return NV_ENC_BUFFER_FORMAT_ABGR;
2057 return NV_ENC_BUFFER_FORMAT_ARGB10;
2059 return NV_ENC_BUFFER_FORMAT_ABGR10;
2060 #ifdef NVENC_HAVE_422_SUPPORT
2062 return NV_ENC_BUFFER_FORMAT_NV16;
2065 return NV_ENC_BUFFER_FORMAT_P210;
2068 return NV_ENC_BUFFER_FORMAT_UNDEFINED;
2076 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2079 NVENCSTATUS nv_status;
2080 NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
2081 allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
2085 if (!
ctx->surfaces[idx].in_ref)
2088 NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
2091 if (
ctx->surfaces[idx].format == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
2097 allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
2098 allocSurf.width = avctx->
width;
2099 allocSurf.height = avctx->
height;
2100 allocSurf.bufferFmt =
ctx->surfaces[idx].format;
2102 nv_status = p_nvenc->nvEncCreateInputBuffer(
ctx->nvencoder, &allocSurf);
2103 if (nv_status != NV_ENC_SUCCESS) {
2107 ctx->surfaces[idx].input_surface = allocSurf.inputBuffer;
2108 ctx->surfaces[idx].width = allocSurf.width;
2109 ctx->surfaces[idx].height = allocSurf.height;
2112 nv_status = p_nvenc->nvEncCreateBitstreamBuffer(
ctx->nvencoder, &allocOut);
2113 if (nv_status != NV_ENC_SUCCESS) {
2116 p_nvenc->nvEncDestroyInputBuffer(
ctx->nvencoder,
ctx->surfaces[idx].input_surface);
2121 ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer;
2131 int i, res = 0, res2;
2137 ctx->frame_data_array =
av_calloc(
ctx->frame_data_array_nb,
sizeof(*
ctx->frame_data_array));
2138 if (!
ctx->frame_data_array)
2143 if (!
ctx->timestamp_list)
2147 if (!
ctx->unused_surface_queue)
2151 if (!
ctx->output_surface_queue)
2154 if (!
ctx->output_surface_ready_queue)
2161 for (
i = 0;
i <
ctx->nb_surfaces;
i++) {
2178 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2180 NVENCSTATUS nv_status;
2181 uint32_t outSize = 0;
2182 char tmpHeader[NV_MAX_SEQ_HDR_LEN];
2184 NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
2185 payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
2187 payload.spsppsBuffer = tmpHeader;
2188 payload.inBufferSize =
sizeof(tmpHeader);
2189 payload.outSPSPPSPayloadSize = &outSize;
2191 nv_status = p_nvenc->nvEncGetSequenceParams(
ctx->nvencoder, &payload);
2192 if (nv_status != NV_ENC_SUCCESS) {
2203 memcpy(avctx->
extradata, tmpHeader, outSize);
2212 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2216 if (
ctx->nvencoder) {
2217 NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER,
2218 .encodePicFlags = NV_ENC_PIC_FLAG_EOS };
2224 p_nvenc->nvEncEncodePicture(
ctx->nvencoder, ¶ms);
2232 if (
ctx->frame_data_array) {
2233 for (
i = 0;
i <
ctx->frame_data_array_nb;
i++)
2239 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
2240 if (
ctx->registered_frames[
i].mapped)
2241 p_nvenc->nvEncUnmapInputResource(
ctx->nvencoder,
ctx->registered_frames[
i].in_map.mappedResource);
2242 if (
ctx->registered_frames[
i].regptr)
2243 p_nvenc->nvEncUnregisterResource(
ctx->nvencoder,
ctx->registered_frames[
i].regptr);
2245 ctx->nb_registered_frames = 0;
2248 if (
ctx->surfaces) {
2249 for (
i = 0;
i <
ctx->nb_surfaces; ++
i) {
2251 p_nvenc->nvEncDestroyInputBuffer(
ctx->nvencoder,
ctx->surfaces[
i].input_surface);
2253 p_nvenc->nvEncDestroyBitstreamBuffer(
ctx->nvencoder,
ctx->surfaces[
i].output_surface);
2257 ctx->nb_surfaces = 0;
2263 if (
ctx->nvencoder) {
2264 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
2272 if (
ctx->cu_context_internal)
2274 ctx->cu_context =
ctx->cu_context_internal =
NULL;
2277 if (
ctx->d3d11_device) {
2278 ID3D11Device_Release(
ctx->d3d11_device);
2283 nvenc_free_functions(&dl_fn->
nvenc_dl);
2284 cuda_free_functions(&dl_fn->
cuda_dl);
2302 "hw_frames_ctx must be set when using GPU frames as input\n");
2308 "hw_frames_ctx must match the GPU frame type\n");
2357 NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params,
const AVFrame *
frame)
2359 int dst_linesize[4] = {
2360 lock_buffer_params->pitch,
2361 lock_buffer_params->pitch,
2362 lock_buffer_params->pitch,
2363 lock_buffer_params->pitch
2365 uint8_t *dst_data[4];
2369 dst_linesize[1] = dst_linesize[2] >>= 1;
2372 lock_buffer_params->bufferDataPtr, dst_linesize);
2377 FFSWAP(uint8_t*, dst_data[1], dst_data[2]);
2390 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2391 NVENCSTATUS nv_status;
2396 for (first_round = 1; first_round >= 0; first_round--) {
2397 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
2398 if (!
ctx->registered_frames[
i].mapped) {
2399 if (
ctx->registered_frames[
i].regptr) {
2402 nv_status = p_nvenc->nvEncUnregisterResource(
ctx->nvencoder,
ctx->registered_frames[
i].regptr);
2403 if (nv_status != NV_ENC_SUCCESS)
2404 return nvenc_print_error(avctx, nv_status,
"Failed unregistering unused input resource");
2405 ctx->registered_frames[
i].ptr =
NULL;
2406 ctx->registered_frames[
i].regptr =
NULL;
2413 return ctx->nb_registered_frames++;
2424 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2427 NV_ENC_REGISTER_RESOURCE reg = { 0 };
2430 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
2441 reg.version = NV_ENC_REGISTER_RESOURCE_VER;
2442 reg.width = frames_ctx->width;
2443 reg.height = frames_ctx->height;
2444 reg.pitch =
frame->linesize[0];
2445 reg.resourceToRegister =
frame->data[0];
2448 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR;
2451 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX;
2452 reg.subResourceIndex = (intptr_t)
frame->data[1];
2456 if (reg.bufferFormat == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
2462 ret = p_nvenc->nvEncRegisterResource(
ctx->nvencoder, ®);
2463 if (
ret != NV_ENC_SUCCESS) {
2468 ctx->registered_frames[idx].ptr =
frame->data[0];
2469 ctx->registered_frames[idx].ptr_index = reg.subResourceIndex;
2470 ctx->registered_frames[idx].regptr = reg.registeredResource;
2479 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2482 NVENCSTATUS nv_status;
2495 if (!
ctx->registered_frames[reg_idx].mapped) {
2496 ctx->registered_frames[reg_idx].in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER;
2497 ctx->registered_frames[reg_idx].in_map.registeredResource =
ctx->registered_frames[reg_idx].regptr;
2498 nv_status = p_nvenc->nvEncMapInputResource(
ctx->nvencoder, &
ctx->registered_frames[reg_idx].in_map);
2499 if (nv_status != NV_ENC_SUCCESS) {
2505 ctx->registered_frames[reg_idx].mapped += 1;
2507 nvenc_frame->
reg_idx = reg_idx;
2508 nvenc_frame->
input_surface =
ctx->registered_frames[reg_idx].in_map.mappedResource;
2509 nvenc_frame->
format =
ctx->registered_frames[reg_idx].in_map.mappedBufferFmt;
2514 NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
2516 lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
2519 nv_status = p_nvenc->nvEncLockInputBuffer(
ctx->nvencoder, &lockBufferParams);
2520 if (nv_status != NV_ENC_SUCCESS) {
2521 return nvenc_print_error(avctx, nv_status,
"Failed locking nvenc input buffer");
2524 nvenc_frame->
pitch = lockBufferParams.pitch;
2527 nv_status = p_nvenc->nvEncUnlockInputBuffer(
ctx->nvencoder, nvenc_frame->
input_surface);
2528 if (nv_status != NV_ENC_SUCCESS) {
2536 #ifdef NVENC_HAVE_TIME_CODE
2542 uint32_t *tc = (uint32_t*)sd->
data;
2547 time_code->displayPicStruct = NV_ENC_PIC_STRUCT_DISPLAY_FRAME;
2548 time_code->skipClockTimestampInsertion = 1;
2551 time_code->displayPicStruct = NV_ENC_PIC_STRUCT_DISPLAY_FRAME_DOUBLING;
2554 time_code->displayPicStruct = NV_ENC_PIC_STRUCT_DISPLAY_FRAME_TRIPLING;
2557 time_code->displayPicStruct = NV_ENC_PIC_STRUCT_DISPLAY_FRAME;
2561 for (
int i = 0;
i < cnt;
i++) {
2562 unsigned hh, mm,
ss, ff, drop;
2565 #ifdef NVENC_NEW_COUNTING_TYPE
2566 time_code->clockTimestamp[
i].countingTypeLSB = 0;
2567 time_code->clockTimestamp[
i].countingTypeMSB = 0;
2569 time_code->clockTimestamp[
i].countingType = 0;
2571 time_code->clockTimestamp[
i].discontinuityFlag = 0;
2572 time_code->clockTimestamp[
i].cntDroppedFrames = drop;
2573 time_code->clockTimestamp[
i].nFrames = ff;
2574 time_code->clockTimestamp[
i].secondsValue =
ss;
2575 time_code->clockTimestamp[
i].minutesValue = mm;
2576 time_code->clockTimestamp[
i].hoursValue = hh;
2577 time_code->clockTimestamp[
i].timeOffset = 0;
2580 time_code->displayPicStruct = NV_ENC_PIC_STRUCT_DISPLAY_FRAME;
2581 time_code->skipClockTimestampInsertion = 1;
2587 NV_ENC_PIC_PARAMS *params,
2588 NV_ENC_SEI_PAYLOAD *sei_data,
2595 params->codecPicParams.h264PicParams.sliceMode =
2596 ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
2597 params->codecPicParams.h264PicParams.sliceModeData =
2598 ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
2599 if (sei_count > 0) {
2600 params->codecPicParams.h264PicParams.seiPayloadArray = sei_data;
2601 params->codecPicParams.h264PicParams.seiPayloadArrayCnt = sei_count;
2604 #ifdef NVENC_HAVE_TIME_CODE
2606 nvenc_fill_time_code(avctx,
frame, ¶ms->codecPicParams.h264PicParams.timeCode);
2611 params->codecPicParams.hevcPicParams.sliceMode =
2612 ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
2613 params->codecPicParams.hevcPicParams.sliceModeData =
2614 ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
2615 if (sei_count > 0) {
2616 params->codecPicParams.hevcPicParams.seiPayloadArray = sei_data;
2617 params->codecPicParams.hevcPicParams.seiPayloadArrayCnt = sei_count;
2621 #if CONFIG_AV1_NVENC_ENCODER
2623 params->codecPicParams.av1PicParams.numTileColumns =
2624 ctx->encode_config.encodeCodecConfig.av1Config.numTileColumns;
2625 params->codecPicParams.av1PicParams.numTileRows =
2626 ctx->encode_config.encodeCodecConfig.av1Config.numTileRows;
2627 if (sei_count > 0) {
2628 params->codecPicParams.av1PicParams.obuPayloadArray = sei_data;
2629 params->codecPicParams.av1PicParams.obuPayloadArrayCnt = sei_count;
2660 NV_ENC_LOCK_BITSTREAM *params,
2667 pkt->
pts = params->outputTimeStamp;
2675 delay =
FFMAX(
ctx->encode_config.frameIntervalP - 1, 0);
2676 #ifdef NVENC_HAVE_MVHEVC
2677 delay *=
ctx->multiview ? 2 : 1;
2679 if (
ctx->output_frame_num >= delay) {
2681 ctx->output_frame_num++;
2685 delay_time =
ctx->initial_delay_time;
2693 delay_time = t1 - t2;
2698 delay_time = delay * (t3 - t2);
2702 ctx->initial_delay_time = delay_time;
2711 ctx->output_frame_num++;
2721 int idx =
ctx->frame_data_array_pos;
2736 ctx->frame_data_array_pos = (
ctx->frame_data_array_pos + 1) %
ctx->frame_data_array_nb;
2737 pic_params->inputDuration = idx;
2747 int idx = lock_params->outputDuration;
2767 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2769 NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
2770 NVENCSTATUS nv_status;
2775 lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
2777 lock_params.doNotWait = 0;
2780 nv_status = p_nvenc->nvEncLockBitstream(
ctx->nvencoder, &lock_params);
2781 if (nv_status != NV_ENC_SUCCESS) {
2793 memcpy(
pkt->
data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
2795 nv_status = p_nvenc->nvEncUnlockBitstream(
ctx->nvencoder, tmpoutsurf->
output_surface);
2796 if (nv_status != NV_ENC_SUCCESS) {
2797 res =
nvenc_print_error(avctx, nv_status,
"Failed unlocking bitstream buffer, expect the gates of mordor to open");
2803 ctx->registered_frames[tmpoutsurf->
reg_idx].mapped -= 1;
2804 if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped == 0) {
2805 nv_status = p_nvenc->nvEncUnmapInputResource(
ctx->nvencoder,
ctx->registered_frames[tmpoutsurf->
reg_idx].in_map.mappedResource);
2806 if (nv_status != NV_ENC_SUCCESS) {
2810 }
else if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped < 0) {
2820 switch (lock_params.pictureType) {
2821 case NV_ENC_PIC_TYPE_IDR:
2824 case NV_ENC_PIC_TYPE_I:
2827 case NV_ENC_PIC_TYPE_P:
2830 case NV_ENC_PIC_TYPE_B:
2833 case NV_ENC_PIC_TYPE_BI:
2837 av_log(avctx,
AV_LOG_ERROR,
"Unknown picture type encountered, expect the output to be broken.\n");
2838 av_log(avctx,
AV_LOG_ERROR,
"Please report this error and include as much information on how to reproduce it as possible.\n");
2866 int nb_ready, nb_pending;
2871 return nb_ready > 0;
2872 return (nb_ready > 0) && (nb_ready + nb_pending >=
ctx->async_depth);
2882 void *a53_data =
NULL;
2883 size_t a53_size = 0;
2891 &
ctx->sei_data_size,
2892 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2899 ctx->sei_data[sei_count].payloadSize = (uint32_t)a53_size;
2900 ctx->sei_data[sei_count].payload = (uint8_t*)a53_data;
2902 #if CONFIG_AV1_NVENC_ENCODER
2915 void *tc_data =
NULL;
2924 &
ctx->sei_data_size,
2925 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2932 ctx->sei_data[sei_count].payloadSize = (uint32_t)tc_size;
2933 ctx->sei_data[sei_count].payload = (uint8_t*)tc_data;
2935 #if CONFIG_AV1_NVENC_ENCODER
2950 for (
i = 0;
i <
frame->nb_side_data;
i++) {
2958 &
ctx->sei_data_size,
2959 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2965 ctx->sei_data[sei_count].payloadSize = side_data->
size;
2969 if (!
ctx->sei_data[sei_count].payload) {
2981 for (
i = 0;
i < sei_count;
i++)
2990 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
2993 NV_ENC_RECONFIGURE_PARAMS params = { 0 };
2994 int needs_reconfig = 0;
2995 int needs_encode_config = 0;
2996 int reconfig_bitrate = 0, reconfig_dar = 0;
2999 params.version = NV_ENC_RECONFIGURE_PARAMS_VER;
3000 params.reInitEncodeParams =
ctx->init_encode_params;
3003 if (dw !=
ctx->init_encode_params.darWidth || dh !=
ctx->init_encode_params.darHeight) {
3005 "aspect ratio change (DAR): %d:%d -> %d:%d\n",
3006 ctx->init_encode_params.darWidth,
3007 ctx->init_encode_params.darHeight, dw, dh);
3009 params.reInitEncodeParams.darHeight = dh;
3010 params.reInitEncodeParams.darWidth = dw;
3016 if (
ctx->rc != NV_ENC_PARAMS_RC_CONSTQP &&
ctx->support_dyn_bitrate) {
3017 if (avctx->
bit_rate > 0 && params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate != avctx->
bit_rate) {
3019 "avg bitrate change: %d -> %d\n",
3020 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate,
3023 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate = avctx->
bit_rate;
3024 reconfig_bitrate = 1;
3029 "max bitrate change: %d -> %d\n",
3030 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate,
3033 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate = avctx->
rc_max_rate;
3034 reconfig_bitrate = 1;
3039 "vbv buffer size change: %d -> %d\n",
3040 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize,
3043 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize = avctx->
rc_buffer_size;
3044 reconfig_bitrate = 1;
3047 if (reconfig_bitrate) {
3048 params.resetEncoder = 1;
3049 params.forceIDR = 1;
3051 needs_encode_config = 1;
3056 if (!needs_encode_config)
3057 params.reInitEncodeParams.encodeConfig =
NULL;
3059 if (needs_reconfig) {
3060 ret = p_nvenc->nvEncReconfigureEncoder(
ctx->nvencoder, ¶ms);
3061 if (
ret != NV_ENC_SUCCESS) {
3065 ctx->init_encode_params.darHeight = dh;
3066 ctx->init_encode_params.darWidth = dw;
3069 if (reconfig_bitrate) {
3070 ctx->encode_config.rcParams.averageBitRate = params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate;
3071 ctx->encode_config.rcParams.maxBitRate = params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate;
3072 ctx->encode_config.rcParams.vbvBufferSize = params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize;
3079 #ifdef NVENC_HAVE_HEVC_AND_AV1_MASTERING_METADATA
3081 MASTERING_DISPLAY_INFO *mastering_disp_info, CONTENT_LIGHT_LEVEL *content_light_level)
3085 if (
ctx->mdm ||
ctx->cll) {
3126 pic_params->codecPicParams.hevcPicParams.pMasteringDisplay = mastering_disp_info;
3128 pic_params->codecPicParams.av1PicParams.pMasteringDisplay = mastering_disp_info;
3135 content_light_level->maxContentLightLevel = cll->
MaxCLL;
3136 content_light_level->maxPicAverageLightLevel = cll->
MaxFALL;
3139 pic_params->codecPicParams.hevcPicParams.pMaxCll = content_light_level;
3141 pic_params->codecPicParams.av1PicParams.pMaxCll = content_light_level;
3153 NVENCSTATUS nv_status;
3158 #ifdef NVENC_HAVE_HEVC_AND_AV1_MASTERING_METADATA
3159 MASTERING_DISPLAY_INFO mastering_disp_info = { 0 };
3160 CONTENT_LIGHT_LEVEL content_light_level = { 0 };
3162 #ifdef NVENC_HAVE_MVHEVC
3163 HEVC_3D_REFERENCE_DISPLAY_INFO ref_disp_info = { 0 };
3168 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
3170 NV_ENC_PIC_PARAMS pic_params = { 0 };
3171 pic_params.version = NV_ENC_PIC_PARAMS_VER;
3173 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
3197 pic_params.bufferFmt = in_surf->
format;
3198 pic_params.inputWidth = in_surf->
width;
3199 pic_params.inputHeight = in_surf->
height;
3200 pic_params.inputPitch = in_surf->
pitch;
3205 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
3207 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
3209 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
3213 pic_params.encodePicFlags =
3214 ctx->forced_idr ? NV_ENC_PIC_FLAG_FORCEIDR : NV_ENC_PIC_FLAG_FORCEINTRA;
3216 pic_params.encodePicFlags = 0;
3219 pic_params.frameIdx =
ctx->frame_idx_counter++;
3220 pic_params.inputTimeStamp =
frame->pts;
3222 if (
ctx->extra_sei) {
3229 #ifdef NVENC_HAVE_HEVC_AND_AV1_MASTERING_METADATA
3230 res = nvenc_set_mastering_display_data(avctx,
frame, &pic_params, &mastering_disp_info, &content_light_level);
3235 #ifdef NVENC_HAVE_MVHEVC
3236 if (
ctx->multiview) {
3241 ctx->next_view_id = *(
int*)sd_view_id->
data;
3243 pic_params.codecPicParams.hevcPicParams.viewId =
ctx->next_view_id;
3267 pic_params.codecPicParams.hevcPicParams.p3DReferenceDisplayInfo = &ref_disp_info;
3268 ctx->display_sei_sent = 1;
3269 }
else if (!
ctx->display_sei_sent) {
3270 ref_disp_info.precRefDisplayWidth = 31;
3271 ref_disp_info.leftViewId[0] = 0;
3272 ref_disp_info.rightViewId[0] = 1;
3274 pic_params.codecPicParams.hevcPicParams.p3DReferenceDisplayInfo = &ref_disp_info;
3275 ctx->display_sei_sent = 1;
3278 ctx->next_view_id = !
ctx->next_view_id;
3288 pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
3295 nv_status = p_nvenc->nvEncEncodePicture(
ctx->nvencoder, &pic_params);
3297 for (
i = 0;
i < sei_count;
i++)
3304 if (nv_status != NV_ENC_SUCCESS &&
3305 nv_status != NV_ENC_ERR_NEED_MORE_INPUT)
3316 if (nv_status == NV_ENC_SUCCESS) {
3317 while (
av_fifo_read(
ctx->output_surface_queue, &tmp_out_surf, 1) >= 0)
3333 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
3336 if (!
frame->buf[0]) {
3381 ctx->output_frame_num = 0;
3382 ctx->initial_delay_time = 0;