23 #include "config_components.h"
27 #if CONFIG_AV1_NVENC_ENCODER
43 #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, dl_fn->cuda_dl, x)
45 #define NVENC_CAP 0x30
46 #define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR || \
47 rc == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ || \
48 rc == NV_ENC_PARAMS_RC_CBR_HQ)
82 #define IS_10BIT(pix_fmt) (pix_fmt == AV_PIX_FMT_P010 || \
83 pix_fmt == AV_PIX_FMT_P016 || \
84 pix_fmt == AV_PIX_FMT_YUV444P16 || \
85 pix_fmt == AV_PIX_FMT_X2RGB10 || \
86 pix_fmt == AV_PIX_FMT_X2BGR10 || \
87 pix_fmt == AV_PIX_FMT_GBRP16)
89 #define IS_YUV444(pix_fmt) (pix_fmt == AV_PIX_FMT_YUV444P || \
90 pix_fmt == AV_PIX_FMT_YUV444P16 || \
91 pix_fmt == AV_PIX_FMT_GBRP || \
92 pix_fmt == AV_PIX_FMT_GBRP16)
94 #define IS_GBRP(pix_fmt) (pix_fmt == AV_PIX_FMT_GBRP || \
95 pix_fmt == AV_PIX_FMT_GBRP16)
102 { NV_ENC_SUCCESS, 0,
"success" },
103 { NV_ENC_ERR_NO_ENCODE_DEVICE,
AVERROR(ENOENT),
"no encode device" },
104 { NV_ENC_ERR_UNSUPPORTED_DEVICE,
AVERROR(ENOSYS),
"unsupported device" },
105 { NV_ENC_ERR_INVALID_ENCODERDEVICE,
AVERROR(EINVAL),
"invalid encoder device" },
106 { NV_ENC_ERR_INVALID_DEVICE,
AVERROR(EINVAL),
"invalid device" },
107 { NV_ENC_ERR_DEVICE_NOT_EXIST,
AVERROR(EIO),
"device does not exist" },
108 { NV_ENC_ERR_INVALID_PTR,
AVERROR(EFAULT),
"invalid ptr" },
109 { NV_ENC_ERR_INVALID_EVENT,
AVERROR(EINVAL),
"invalid event" },
110 { NV_ENC_ERR_INVALID_PARAM,
AVERROR(EINVAL),
"invalid param" },
111 { NV_ENC_ERR_INVALID_CALL,
AVERROR(EINVAL),
"invalid call" },
112 { NV_ENC_ERR_OUT_OF_MEMORY,
AVERROR(ENOMEM),
"out of memory" },
113 { NV_ENC_ERR_ENCODER_NOT_INITIALIZED,
AVERROR(EINVAL),
"encoder not initialized" },
114 { NV_ENC_ERR_UNSUPPORTED_PARAM,
AVERROR(ENOSYS),
"unsupported param" },
115 { NV_ENC_ERR_LOCK_BUSY,
AVERROR(EAGAIN),
"lock busy" },
117 { NV_ENC_ERR_INVALID_VERSION,
AVERROR(EINVAL),
"invalid version" },
118 { NV_ENC_ERR_MAP_FAILED,
AVERROR(EIO),
"map failed" },
119 { NV_ENC_ERR_NEED_MORE_INPUT,
AVERROR(EAGAIN),
"need more input" },
120 { NV_ENC_ERR_ENCODER_BUSY,
AVERROR(EAGAIN),
"encoder busy" },
121 { NV_ENC_ERR_EVENT_NOT_REGISTERD,
AVERROR(EBADF),
"event not registered" },
123 { NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY,
AVERROR(EINVAL),
"incompatible client key" },
124 { NV_ENC_ERR_UNIMPLEMENTED,
AVERROR(ENOSYS),
"unimplemented" },
125 { NV_ENC_ERR_RESOURCE_REGISTER_FAILED,
AVERROR(EIO),
"resource register failed" },
126 { NV_ENC_ERR_RESOURCE_NOT_REGISTERED,
AVERROR(EBADF),
"resource not registered" },
127 { NV_ENC_ERR_RESOURCE_NOT_MAPPED,
AVERROR(EBADF),
"resource not mapped" },
141 *
desc =
"unknown error";
146 const char *error_string)
149 const char *details =
"(no details)";
152 #ifdef NVENC_HAVE_GETLASTERRORSTRING
154 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
156 if (p_nvenc &&
ctx->nvencoder)
157 details = p_nvenc->nvEncGetLastErrorString(
ctx->nvencoder);
170 #define PRESET_ALIAS(alias, name, ...) \
171 [PRESET_ ## alias] = { NV_ENC_PRESET_ ## name ## _GUID, __VA_ARGS__ }
173 #define PRESET(name, ...) PRESET_ALIAS(name, name, __VA_ARGS__)
178 #ifdef NVENC_HAVE_NEW_PRESETS
217 ctx->init_encode_params.presetGUID = t->
guid;
220 #ifdef NVENC_HAVE_NEW_PRESETS
221 if (
ctx->tuning_info == NV_ENC_TUNING_INFO_LOSSLESS)
231 #if NVENCAPI_CHECK_VERSION(12, 1)
232 const char *minver =
"(unknown)";
233 #elif NVENCAPI_CHECK_VERSION(12, 0)
234 # if defined(_WIN32) || defined(__CYGWIN__)
235 const char *minver =
"522.25";
237 const char *minver =
"520.56.06";
239 #elif NVENCAPI_CHECK_VERSION(11, 1)
240 # if defined(_WIN32) || defined(__CYGWIN__)
241 const char *minver =
"471.41";
243 const char *minver =
"470.57.02";
245 #elif NVENCAPI_CHECK_VERSION(11, 0)
246 # if defined(_WIN32) || defined(__CYGWIN__)
247 const char *minver =
"456.71";
249 const char *minver =
"455.28";
251 #elif NVENCAPI_CHECK_VERSION(10, 0)
252 # if defined(_WIN32) || defined(__CYGWIN__)
253 const char *minver =
"450.51";
255 const char *minver =
"445.87";
257 #elif NVENCAPI_CHECK_VERSION(9, 1)
258 # if defined(_WIN32) || defined(__CYGWIN__)
259 const char *minver =
"436.15";
261 const char *minver =
"435.21";
263 #elif NVENCAPI_CHECK_VERSION(9, 0)
264 # if defined(_WIN32) || defined(__CYGWIN__)
265 const char *minver =
"418.81";
267 const char *minver =
"418.30";
269 #elif NVENCAPI_CHECK_VERSION(8, 2)
270 # if defined(_WIN32) || defined(__CYGWIN__)
271 const char *minver =
"397.93";
273 const char *minver =
"396.24";
275 #elif NVENCAPI_CHECK_VERSION(8, 1)
276 # if defined(_WIN32) || defined(__CYGWIN__)
277 const char *minver =
"390.77";
279 const char *minver =
"390.25";
282 # if defined(_WIN32) || defined(__CYGWIN__)
283 const char *minver =
"378.66";
285 const char *minver =
"378.13";
288 av_log(avctx,
level,
"The minimum required Nvidia driver for nvenc is %s or newer\n", minver);
296 uint32_t nvenc_max_ver;
299 ret = cuda_load_functions(&dl_fn->
cuda_dl, avctx);
303 ret = nvenc_load_functions(&dl_fn->
nvenc_dl, avctx);
309 err = dl_fn->
nvenc_dl->NvEncodeAPIGetMaxSupportedVersion(&nvenc_max_ver);
310 if (err != NV_ENC_SUCCESS)
315 if ((NVENCAPI_MAJOR_VERSION << 4 | NVENCAPI_MINOR_VERSION) > nvenc_max_ver) {
316 av_log(avctx,
AV_LOG_ERROR,
"Driver does not support the required nvenc API version. "
317 "Required: %d.%d Found: %d.%d\n",
318 NVENCAPI_MAJOR_VERSION, NVENCAPI_MINOR_VERSION,
319 nvenc_max_ver >> 4, nvenc_max_ver & 0
xf);
324 dl_fn->
nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
327 if (err != NV_ENC_SUCCESS)
340 if (
ctx->d3d11_device)
352 if (
ctx->d3d11_device)
360 NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = { 0 };
362 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
365 params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
366 params.apiVersion = NVENCAPI_VERSION;
367 if (
ctx->d3d11_device) {
368 params.device =
ctx->d3d11_device;
369 params.deviceType = NV_ENC_DEVICE_TYPE_DIRECTX;
371 params.device =
ctx->cu_context;
372 params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
375 ret = p_nvenc->nvEncOpenEncodeSessionEx(¶ms, &
ctx->nvencoder);
376 if (
ret != NV_ENC_SUCCESS) {
387 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
388 int i,
ret, count = 0;
391 ret = p_nvenc->nvEncGetEncodeGUIDCount(
ctx->nvencoder, &count);
393 if (
ret != NV_ENC_SUCCESS || !count)
400 ret = p_nvenc->nvEncGetEncodeGUIDs(
ctx->nvencoder, guids, count, &count);
401 if (
ret != NV_ENC_SUCCESS) {
407 for (
i = 0;
i < count;
i++) {
408 if (!memcmp(&guids[
i], &
ctx->init_encode_params.encodeGUID,
sizeof(*guids))) {
423 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
424 NV_ENC_CAPS_PARAM params = { 0 };
427 params.version = NV_ENC_CAPS_PARAM_VER;
428 params.capsToQuery = cap;
430 ret = p_nvenc->nvEncGetEncodeCaps(
ctx->nvencoder,
ctx->init_encode_params.encodeGUID, ¶ms, &
val);
432 if (
ret == NV_ENC_SUCCESS)
461 if (ret < avctx->
width) {
468 if (ret < avctx->
height) {
475 if (ret < avctx->max_b_frames) {
485 "Interlaced encoding is not supported. Supported level: %d\n",
497 if (
ctx->rc_lookahead > 0 &&
ret <= 0) {
503 if (
ctx->temporal_aq > 0 &&
ret <= 0) {
509 if (
ctx->weighted_pred > 0 &&
ret <= 0) {
515 if (
ctx->coder == NV_ENC_H264_ENTROPY_CODING_MODE_CABAC &&
ret <= 0) {
520 #ifdef NVENC_HAVE_BFRAME_REF_MODE
522 if (
ctx->b_ref_mode == NV_ENC_BFRAME_REF_MODE_EACH &&
ret != 1 &&
ret != 3) {
525 }
else if (
ctx->b_ref_mode != NV_ENC_BFRAME_REF_MODE_DISABLED &&
ret == 0) {
530 if (
ctx->b_ref_mode != 0) {
536 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
538 if(avctx->
refs != NV_ENC_NUM_REF_FRAMES_AUTOSELECT &&
ret <= 0) {
543 if(avctx->
refs != 0) {
549 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
551 if(
ctx->single_slice_intra_refresh &&
ret <= 0) {
556 if(
ctx->single_slice_intra_refresh) {
563 if((
ctx->intra_refresh ||
ctx->single_slice_intra_refresh) &&
ret <= 0) {
568 #ifndef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
576 if(
ctx->constrained_encoding &&
ret <= 0) {
581 ctx->support_dyn_bitrate =
nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE);
590 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
591 char name[128] = { 0};
611 av_log(avctx, loglevel,
"[ GPU #%d - < %s > has Compute SM %d.%d ]\n", idx,
name,
major,
minor);
613 av_log(avctx, loglevel,
"does not support NVENC\n");
624 ctx->cu_context =
ctx->cu_context_internal;
636 av_log(avctx, loglevel,
"supports NVENC\n");
647 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
655 ctx->cu_context_internal =
NULL;
668 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_H264_GUID;
671 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_HEVC_GUID;
673 #if CONFIG_AV1_NVENC_ENCODER
675 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_AV1_GUID;
685 av_log(avctx,
AV_LOG_WARNING,
"The selected preset is deprecated. Use p1 to p7 + -tune or fast/medium/slow.\n");
709 cuda_device_hwctx = hwdev_ctx->
hwctx;
712 d3d11_device_hwctx = hwdev_ctx->
hwctx;
720 if (cuda_device_hwctx) {
722 ctx->cu_stream = cuda_device_hwctx->
stream;
725 else if (d3d11_device_hwctx) {
726 ctx->d3d11_device = d3d11_device_hwctx->
device;
727 ID3D11Device_AddRef(
ctx->d3d11_device);
737 av_log(avctx,
AV_LOG_FATAL,
"Provided device doesn't support required NVENC features\n");
741 int i, nb_devices = 0;
757 for (
i = 0;
i < nb_devices; ++
i) {
770 av_log(avctx,
AV_LOG_FATAL,
"Requested GPU %d, but only %d GPUs are available!\n",
ctx->device, nb_devices);
780 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
781 #if CONFIG_AV1_NVENC_ENCODER
787 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
789 if (
ctx->init_qp_p >= 0) {
790 rc->constQP.qpInterP =
ctx->init_qp_p;
791 if (
ctx->init_qp_i >= 0 &&
ctx->init_qp_b >= 0) {
792 rc->constQP.qpIntra =
ctx->init_qp_i;
793 rc->constQP.qpInterB =
ctx->init_qp_b;
797 rc->constQP.qpInterB =
av_clip(
800 rc->constQP.qpIntra = rc->constQP.qpInterP;
801 rc->constQP.qpInterB = rc->constQP.qpInterP;
803 }
else if (
ctx->cqp >= 0) {
804 rc->constQP.qpInterP = rc->constQP.qpInterB = rc->constQP.qpIntra =
ctx->cqp;
818 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
820 #if CONFIG_AV1_NVENC_ENCODER
826 if (avctx->
qmin >= 0 && avctx->
qmax >= 0) {
830 rc->minQP.qpInterB = avctx->
qmin;
831 rc->minQP.qpInterP = avctx->
qmin;
832 rc->minQP.qpIntra = avctx->
qmin;
834 rc->maxQP.qpInterB = avctx->
qmax;
835 rc->maxQP.qpInterP = avctx->
qmax;
836 rc->maxQP.qpIntra = avctx->
qmax;
838 qp_inter_p = (avctx->
qmax + 3 * avctx->
qmin) / 4;
839 }
else if (avctx->
qmin >= 0) {
842 rc->minQP.qpInterB = avctx->
qmin;
843 rc->minQP.qpInterP = avctx->
qmin;
844 rc->minQP.qpIntra = avctx->
qmin;
846 qp_inter_p = avctx->
qmin;
851 rc->enableInitialRCQP = 1;
853 if (
ctx->init_qp_p < 0) {
854 rc->initialRCQP.qpInterP = qp_inter_p;
856 rc->initialRCQP.qpInterP =
ctx->init_qp_p;
859 if (
ctx->init_qp_i < 0) {
861 rc->initialRCQP.qpIntra =
av_clip(
864 rc->initialRCQP.qpIntra = rc->initialRCQP.qpInterP;
867 rc->initialRCQP.qpIntra =
ctx->init_qp_i;
870 if (
ctx->init_qp_b < 0) {
872 rc->initialRCQP.qpInterB =
av_clip(
875 rc->initialRCQP.qpInterB = rc->initialRCQP.qpInterP;
878 rc->initialRCQP.qpInterB =
ctx->init_qp_b;
885 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
887 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
888 rc->constQP.qpInterB = 0;
889 rc->constQP.qpInterP = 0;
890 rc->constQP.qpIntra = 0;
899 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
902 case NV_ENC_PARAMS_RC_CONSTQP:
905 case NV_ENC_PARAMS_RC_VBR_MINQP:
906 if (avctx->
qmin < 0) {
908 "The variable bitrate rate-control requires "
909 "the 'qmin' option set.\n");
914 case NV_ENC_PARAMS_RC_VBR_HQ:
915 case NV_ENC_PARAMS_RC_VBR:
918 case NV_ENC_PARAMS_RC_CBR:
919 case NV_ENC_PARAMS_RC_CBR_HQ:
920 case NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ:
924 rc->rateControlMode =
ctx->rc;
933 int nb_surfaces =
FFMAX(4,
ctx->encode_config.frameIntervalP * 2 * 2);
936 if (
ctx->rc_lookahead > 0) {
939 nb_surfaces =
FFMAX(1,
FFMAX(nb_surfaces,
ctx->rc_lookahead +
ctx->encode_config.frameIntervalP + 1 + 4));
940 if (nb_surfaces >
ctx->nb_surfaces &&
ctx->nb_surfaces > 0)
943 "Defined rc_lookahead requires more surfaces, "
944 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
946 ctx->nb_surfaces =
FFMAX(nb_surfaces,
ctx->nb_surfaces);
948 if (
ctx->encode_config.frameIntervalP > 1 &&
ctx->nb_surfaces < nb_surfaces &&
ctx->nb_surfaces > 0)
951 "Defined b-frame requires more surfaces, "
952 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
953 ctx->nb_surfaces =
FFMAX(
ctx->nb_surfaces, nb_surfaces);
955 else if (
ctx->nb_surfaces <= 0)
956 ctx->nb_surfaces = nb_surfaces;
971 av_log(avctx,
AV_LOG_WARNING,
"Using global_quality with nvenc is deprecated. Use qp instead.\n");
977 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate;
978 }
else if (
ctx->encode_config.rcParams.averageBitRate > 0) {
979 ctx->encode_config.rcParams.maxBitRate =
ctx->encode_config.rcParams.averageBitRate;
985 #ifdef NVENC_HAVE_MULTIPASS
986 ctx->encode_config.rcParams.multiPass =
ctx->multipass;
989 ctx->encode_config.rcParams.multiPass = NV_ENC_MULTI_PASS_DISABLED;
991 ctx->encode_config.rcParams.multiPass = NV_ENC_TWO_PASS_FULL_RESOLUTION;
995 ctx->rc = NV_ENC_PARAMS_RC_CBR;
996 }
else if (
ctx->cqp >= 0) {
997 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
998 }
else if (
ctx->quality >= 0.0f) {
999 ctx->rc = NV_ENC_PARAMS_RC_VBR;
1009 if (
ctx->twopass < 0)
1014 ctx->rc = NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ;
1016 ctx->rc = NV_ENC_PARAMS_RC_CBR;
1018 }
else if (
ctx->cqp >= 0) {
1019 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
1020 }
else if (
ctx->twopass) {
1021 ctx->rc = NV_ENC_PARAMS_RC_VBR_HQ;
1022 }
else if (avctx->
qmin >= 0 && avctx->
qmax >= 0) {
1023 ctx->rc = NV_ENC_PARAMS_RC_VBR_MINQP;
1035 #ifdef NVENC_HAVE_QP_CHROMA_OFFSETS
1036 ctx->encode_config.rcParams.cbQPIndexOffset =
ctx->qp_cb_offset;
1037 ctx->encode_config.rcParams.crQPIndexOffset =
ctx->qp_cr_offset;
1039 if (
ctx->qp_cb_offset ||
ctx->qp_cr_offset)
1040 av_log(avctx,
AV_LOG_WARNING,
"Failed setting QP CB/CR offsets, SDK 11.1 or greater required at compile time.\n");
1043 #ifdef NVENC_HAVE_LDKFS
1045 ctx->encode_config.rcParams.lowDelayKeyFrameScale =
ctx->ldkfs;
1050 }
else if (
ctx->rc >= 0) {
1053 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
1059 }
else if (
ctx->encode_config.rcParams.averageBitRate > 0) {
1060 avctx->
rc_buffer_size =
ctx->encode_config.rcParams.vbvBufferSize = 2 *
ctx->encode_config.rcParams.averageBitRate;
1064 ctx->encode_config.rcParams.enableAQ = 1;
1065 ctx->encode_config.rcParams.aqStrength =
ctx->aq_strength;
1069 if (
ctx->temporal_aq) {
1070 ctx->encode_config.rcParams.enableTemporalAQ = 1;
1074 if (
ctx->rc_lookahead > 0) {
1075 int lkd_bound =
FFMIN(
ctx->nb_surfaces,
ctx->async_depth) -
1076 ctx->encode_config.frameIntervalP - 4;
1078 if (lkd_bound < 0) {
1079 ctx->encode_config.rcParams.enableLookahead = 0;
1081 "Lookahead not enabled. Increase buffer delay (-delay).\n");
1083 ctx->encode_config.rcParams.enableLookahead = 1;
1084 ctx->encode_config.rcParams.lookaheadDepth =
av_clip(
ctx->rc_lookahead, 0, lkd_bound);
1085 ctx->encode_config.rcParams.disableIadapt =
ctx->no_scenecut;
1086 ctx->encode_config.rcParams.disableBadapt = !
ctx->b_adapt;
1088 "Lookahead enabled: depth %d, scenecut %s, B-adapt %s.\n",
1089 ctx->encode_config.rcParams.lookaheadDepth,
1090 ctx->encode_config.rcParams.disableIadapt ?
"disabled" :
"enabled",
1091 ctx->encode_config.rcParams.disableBadapt ?
"disabled" :
"enabled");
1092 if (
ctx->encode_config.rcParams.lookaheadDepth <
ctx->rc_lookahead)
1093 av_log(avctx,
AV_LOG_WARNING,
"Clipping lookahead depth to %d (from %d) due to lack of surfaces/delay",
1094 ctx->encode_config.rcParams.lookaheadDepth,
ctx->rc_lookahead);
1098 if (
ctx->strict_gop) {
1099 ctx->encode_config.rcParams.strictGOPTarget = 1;
1104 ctx->encode_config.rcParams.enableNonRefP = 1;
1106 if (
ctx->zerolatency)
1107 ctx->encode_config.rcParams.zeroReorderDelay = 1;
1111 int tmp_quality = (
int)(
ctx->quality * 256.0f);
1112 ctx->encode_config.rcParams.targetQuality = (uint8_t)(tmp_quality >> 8);
1113 ctx->encode_config.rcParams.targetQualityLSB = (uint8_t)(tmp_quality & 0xff);
1118 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate = 0;
1127 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1128 NV_ENC_CONFIG_H264 *h264 = &cc->encodeCodecConfig.h264Config;
1129 NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui = &h264->h264VUIParameters;
1136 vui->transferCharacteristics = avctx->
color_trc;
1137 vui->videoFullRangeFlag = 0;
1141 vui->transferCharacteristics = avctx->
color_trc;
1146 vui->colourDescriptionPresentFlag =
1147 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1149 vui->videoSignalTypePresentFlag =
1150 (vui->colourDescriptionPresentFlag
1151 || vui->videoFormat != 5
1152 || vui->videoFullRangeFlag != 0);
1154 h264->sliceMode = 3;
1155 h264->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1157 if (
ctx->intra_refresh) {
1158 h264->enableIntraRefresh = 1;
1159 h264->intraRefreshPeriod = avctx->
gop_size;
1160 h264->intraRefreshCnt = avctx->
gop_size - 1;
1161 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1162 h264->singleSliceIntraRefresh =
ctx->single_slice_intra_refresh;
1166 if (
ctx->constrained_encoding)
1167 h264->enableConstrainedEncoding = 1;
1171 h264->outputAUD =
ctx->aud;
1173 if (
ctx->dpb_size >= 0) {
1175 h264->maxNumRefFrames =
ctx->dpb_size;
1178 if (
ctx->intra_refresh) {
1179 h264->idrPeriod = NVENC_INFINITE_GOPLENGTH;
1184 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1185 h264->outputBufferingPeriodSEI = 1;
1188 h264->outputPictureTimingSEI = 1;
1190 if (cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ ||
1191 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_HQ ||
1192 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_VBR_HQ) {
1193 h264->adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
1194 h264->fmoMode = NV_ENC_H264_FMO_DISABLE;
1198 h264->qpPrimeYZeroTransformBypassFlag = 1;
1200 switch(
ctx->profile) {
1202 cc->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
1206 cc->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
1210 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
1214 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1222 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1228 h264->level =
ctx->level;
1230 if (
ctx->coder >= 0)
1231 h264->entropyCodingMode =
ctx->coder;
1233 #ifdef NVENC_HAVE_BFRAME_REF_MODE
1234 if (
ctx->b_ref_mode >= 0)
1235 h264->useBFramesAsRef =
ctx->b_ref_mode;
1238 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1239 h264->numRefL0 = avctx->
refs;
1240 h264->numRefL1 = avctx->
refs;
1249 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1250 NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig;
1251 NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui = &hevc->hevcVUIParameters;
1258 vui->transferCharacteristics = avctx->
color_trc;
1259 vui->videoFullRangeFlag = 0;
1263 vui->transferCharacteristics = avctx->
color_trc;
1268 vui->colourDescriptionPresentFlag =
1269 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1271 vui->videoSignalTypePresentFlag =
1272 (vui->colourDescriptionPresentFlag
1273 || vui->videoFormat != 5
1274 || vui->videoFullRangeFlag != 0);
1276 hevc->sliceMode = 3;
1277 hevc->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1279 if (
ctx->intra_refresh) {
1280 hevc->enableIntraRefresh = 1;
1281 hevc->intraRefreshPeriod = avctx->
gop_size;
1282 hevc->intraRefreshCnt = avctx->
gop_size - 1;
1283 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1284 hevc->singleSliceIntraRefresh =
ctx->single_slice_intra_refresh;
1288 #ifdef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
1289 if (
ctx->constrained_encoding)
1290 hevc->enableConstrainedEncoding = 1;
1295 hevc->outputAUD =
ctx->aud;
1297 if (
ctx->dpb_size >= 0) {
1299 hevc->maxNumRefFramesInDPB =
ctx->dpb_size;
1302 if (
ctx->intra_refresh) {
1303 hevc->idrPeriod = NVENC_INFINITE_GOPLENGTH;
1308 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1309 hevc->outputBufferingPeriodSEI = 1;
1312 hevc->outputPictureTimingSEI = 1;
1314 switch (
ctx->profile) {
1316 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
1320 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1324 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1331 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1337 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1341 hevc->chromaFormatIDC =
IS_YUV444(
ctx->data_pix_fmt) ? 3 : 1;
1343 hevc->pixelBitDepthMinus8 =
IS_10BIT(
ctx->data_pix_fmt) ? 2 : 0;
1345 hevc->level =
ctx->level;
1347 hevc->tier =
ctx->tier;
1349 #ifdef NVENC_HAVE_HEVC_BFRAME_REF_MODE
1350 if (
ctx->b_ref_mode >= 0)
1351 hevc->useBFramesAsRef =
ctx->b_ref_mode;
1354 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1355 hevc->numRefL0 = avctx->
refs;
1356 hevc->numRefL1 = avctx->
refs;
1362 #if CONFIG_AV1_NVENC_ENCODER
1366 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1367 NV_ENC_CONFIG_AV1 *av1 = &cc->encodeCodecConfig.av1Config;
1374 av1->transferCharacteristics = avctx->
color_trc;
1375 av1->colorRange = 0;
1379 av1->transferCharacteristics = avctx->
color_trc;
1385 av_log(avctx,
AV_LOG_ERROR,
"AV1 High Profile not supported, required for 4:4:4 encoding\n");
1388 cc->profileGUID = NV_ENC_AV1_PROFILE_MAIN_GUID;
1392 if (
ctx->dpb_size >= 0) {
1394 av1->maxNumRefFramesInDPB =
ctx->dpb_size;
1397 if (
ctx->intra_refresh) {
1398 av1->enableIntraRefresh = 1;
1399 av1->intraRefreshPeriod = avctx->
gop_size;
1400 av1->intraRefreshCnt = avctx->
gop_size - 1;
1402 av1->idrPeriod = NVENC_INFINITE_GOPLENGTH;
1407 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1408 av1->enableBitstreamPadding = 1;
1411 if (
ctx->tile_cols >= 0)
1412 av1->numTileColumns =
ctx->tile_cols;
1413 if (
ctx->tile_rows >= 0)
1414 av1->numTileRows =
ctx->tile_rows;
1416 av1->outputAnnexBFormat = 0;
1418 av1->level =
ctx->level;
1419 av1->tier =
ctx->tier;
1421 av1->enableTimingInfo =
ctx->timing_info;
1424 av1->disableSeqHdr = 0;
1425 av1->repeatSeqHdr = 1;
1427 av1->chromaFormatIDC =
IS_YUV444(
ctx->data_pix_fmt) ? 3 : 1;
1429 av1->inputPixelBitDepthMinus8 =
IS_10BIT(
ctx->data_pix_fmt) ? 2 : 0;
1430 av1->pixelBitDepthMinus8 = (
IS_10BIT(
ctx->data_pix_fmt) ||
ctx->highbitdepth) ? 2 : 0;
1432 if (
ctx->b_ref_mode >= 0)
1433 av1->useBFramesAsRef =
ctx->b_ref_mode;
1435 av1->numFwdRefs = avctx->
refs;
1436 av1->numBwdRefs = avctx->
refs;
1449 #if CONFIG_AV1_NVENC_ENCODER
1451 return nvenc_setup_av1_config(avctx);
1465 #if CONFIG_AV1_NVENC_ENCODER
1496 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1498 NV_ENC_PRESET_CONFIG preset_config = { 0 };
1499 NVENCSTATUS nv_status = NV_ENC_SUCCESS;
1504 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1505 ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
1507 ctx->init_encode_params.encodeHeight = avctx->
height;
1508 ctx->init_encode_params.encodeWidth = avctx->
width;
1510 ctx->init_encode_params.encodeConfig = &
ctx->encode_config;
1512 preset_config.version = NV_ENC_PRESET_CONFIG_VER;
1513 preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
1515 #ifdef NVENC_HAVE_NEW_PRESETS
1516 ctx->init_encode_params.tuningInfo =
ctx->tuning_info;
1519 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOSSLESS;
1521 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOW_LATENCY;
1523 nv_status = p_nvenc->nvEncGetEncodePresetConfigEx(
ctx->nvencoder,
1524 ctx->init_encode_params.encodeGUID,
1525 ctx->init_encode_params.presetGUID,
1526 ctx->init_encode_params.tuningInfo,
1529 nv_status = p_nvenc->nvEncGetEncodePresetConfig(
ctx->nvencoder,
1530 ctx->init_encode_params.encodeGUID,
1531 ctx->init_encode_params.presetGUID,
1534 if (nv_status != NV_ENC_SUCCESS)
1535 return nvenc_print_error(avctx, nv_status,
"Cannot get the preset configuration");
1537 memcpy(&
ctx->encode_config, &preset_config.presetCfg,
sizeof(
ctx->encode_config));
1539 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1542 ctx->init_encode_params.darHeight = dh;
1543 ctx->init_encode_params.darWidth = dw;
1553 ctx->init_encode_params.enableEncodeAsync = 0;
1554 ctx->init_encode_params.enablePTD = 1;
1556 #ifdef NVENC_HAVE_NEW_PRESETS
1560 if (
ctx->rc_lookahead == 0 &&
ctx->encode_config.rcParams.enableLookahead)
1561 ctx->rc_lookahead =
ctx->encode_config.rcParams.lookaheadDepth;
1564 if (
ctx->weighted_pred == 1)
1565 ctx->init_encode_params.enableWeightedPrediction = 1;
1567 if (
ctx->bluray_compat) {
1576 ctx->level = NV_ENC_LEVEL_HEVC_51;
1577 ctx->tier = NV_ENC_TIER_HEVC_HIGH;
1590 ctx->encode_config.frameIntervalP = 0;
1591 ctx->encode_config.gopLength = 1;
1595 if(
ctx->single_slice_intra_refresh)
1596 ctx->intra_refresh = 1;
1598 if (
ctx->intra_refresh)
1599 ctx->encode_config.gopLength = NVENC_INFINITE_GOPLENGTH;
1606 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
1608 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
1619 nv_status = p_nvenc->nvEncInitializeEncoder(
ctx->nvencoder, &
ctx->init_encode_params);
1620 if (nv_status != NV_ENC_SUCCESS) {
1625 #ifdef NVENC_HAVE_CUSTREAM_PTR
1626 if (
ctx->cu_context) {
1627 nv_status = p_nvenc->nvEncSetIOCudaStreams(
ctx->nvencoder, &
ctx->cu_stream, &
ctx->cu_stream);
1628 if (nv_status != NV_ENC_SUCCESS) {
1639 if (
ctx->encode_config.frameIntervalP > 1)
1642 if (
ctx->encode_config.rcParams.averageBitRate > 0)
1643 avctx->
bit_rate =
ctx->encode_config.rcParams.averageBitRate;
1650 cpb_props->
buffer_size =
ctx->encode_config.rcParams.vbvBufferSize;
1659 return NV_ENC_BUFFER_FORMAT_YV12_PL;
1661 return NV_ENC_BUFFER_FORMAT_NV12_PL;
1664 return NV_ENC_BUFFER_FORMAT_YUV420_10BIT;
1667 return NV_ENC_BUFFER_FORMAT_YUV444_PL;
1670 return NV_ENC_BUFFER_FORMAT_YUV444_10BIT;
1673 return NV_ENC_BUFFER_FORMAT_ARGB;
1676 return NV_ENC_BUFFER_FORMAT_ABGR;
1678 return NV_ENC_BUFFER_FORMAT_ARGB10;
1680 return NV_ENC_BUFFER_FORMAT_ABGR10;
1682 return NV_ENC_BUFFER_FORMAT_UNDEFINED;
1690 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1693 NVENCSTATUS nv_status;
1694 NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
1695 allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
1699 if (!
ctx->surfaces[idx].in_ref)
1702 NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
1705 if (
ctx->surfaces[idx].format == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
1711 allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
1712 allocSurf.width = avctx->
width;
1713 allocSurf.height = avctx->
height;
1714 allocSurf.bufferFmt =
ctx->surfaces[idx].format;
1716 nv_status = p_nvenc->nvEncCreateInputBuffer(
ctx->nvencoder, &allocSurf);
1717 if (nv_status != NV_ENC_SUCCESS) {
1721 ctx->surfaces[idx].input_surface = allocSurf.inputBuffer;
1722 ctx->surfaces[idx].width = allocSurf.width;
1723 ctx->surfaces[idx].height = allocSurf.height;
1726 nv_status = p_nvenc->nvEncCreateBitstreamBuffer(
ctx->nvencoder, &allocOut);
1727 if (nv_status != NV_ENC_SUCCESS) {
1730 p_nvenc->nvEncDestroyInputBuffer(
ctx->nvencoder,
ctx->surfaces[idx].input_surface);
1735 ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer;
1745 int i, res = 0, res2;
1752 if (!
ctx->timestamp_list)
1756 if (!
ctx->unused_surface_queue)
1760 if (!
ctx->output_surface_queue)
1763 if (!
ctx->output_surface_ready_queue)
1770 for (
i = 0;
i <
ctx->nb_surfaces;
i++) {
1787 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1789 NVENCSTATUS nv_status;
1790 uint32_t outSize = 0;
1791 char tmpHeader[NV_MAX_SEQ_HDR_LEN];
1793 NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
1794 payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
1796 payload.spsppsBuffer = tmpHeader;
1797 payload.inBufferSize =
sizeof(tmpHeader);
1798 payload.outSPSPPSPayloadSize = &outSize;
1800 nv_status = p_nvenc->nvEncGetSequenceParams(
ctx->nvencoder, &payload);
1801 if (nv_status != NV_ENC_SUCCESS) {
1812 memcpy(avctx->
extradata, tmpHeader, outSize);
1821 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1825 if (
ctx->nvencoder) {
1826 NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER,
1827 .encodePicFlags = NV_ENC_PIC_FLAG_EOS };
1833 p_nvenc->nvEncEncodePicture(
ctx->nvencoder, ¶ms);
1842 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
1843 if (
ctx->registered_frames[
i].mapped)
1844 p_nvenc->nvEncUnmapInputResource(
ctx->nvencoder,
ctx->registered_frames[
i].in_map.mappedResource);
1845 if (
ctx->registered_frames[
i].regptr)
1846 p_nvenc->nvEncUnregisterResource(
ctx->nvencoder,
ctx->registered_frames[
i].regptr);
1848 ctx->nb_registered_frames = 0;
1851 if (
ctx->surfaces) {
1852 for (
i = 0;
i <
ctx->nb_surfaces; ++
i) {
1854 p_nvenc->nvEncDestroyInputBuffer(
ctx->nvencoder,
ctx->surfaces[
i].input_surface);
1856 p_nvenc->nvEncDestroyBitstreamBuffer(
ctx->nvencoder,
ctx->surfaces[
i].output_surface);
1860 ctx->nb_surfaces = 0;
1866 if (
ctx->nvencoder) {
1867 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
1875 if (
ctx->cu_context_internal)
1877 ctx->cu_context =
ctx->cu_context_internal =
NULL;
1880 if (
ctx->d3d11_device) {
1881 ID3D11Device_Release(
ctx->d3d11_device);
1886 nvenc_free_functions(&dl_fn->
nvenc_dl);
1887 cuda_free_functions(&dl_fn->
cuda_dl);
1905 "hw_frames_ctx must be set when using GPU frames as input\n");
1911 "hw_frames_ctx must match the GPU frame type\n");
1955 NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params,
const AVFrame *
frame)
1957 int dst_linesize[4] = {
1958 lock_buffer_params->pitch,
1959 lock_buffer_params->pitch,
1960 lock_buffer_params->pitch,
1961 lock_buffer_params->pitch
1963 uint8_t *dst_data[4];
1967 dst_linesize[1] = dst_linesize[2] >>= 1;
1970 lock_buffer_params->bufferDataPtr, dst_linesize);
1975 FFSWAP(uint8_t*, dst_data[1], dst_data[2]);
1988 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1989 NVENCSTATUS nv_status;
1994 for (first_round = 1; first_round >= 0; first_round--) {
1995 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
1996 if (!
ctx->registered_frames[
i].mapped) {
1997 if (
ctx->registered_frames[
i].regptr) {
2000 nv_status = p_nvenc->nvEncUnregisterResource(
ctx->nvencoder,
ctx->registered_frames[
i].regptr);
2001 if (nv_status != NV_ENC_SUCCESS)
2002 return nvenc_print_error(avctx, nv_status,
"Failed unregistering unused input resource");
2003 ctx->registered_frames[
i].ptr =
NULL;
2004 ctx->registered_frames[
i].regptr =
NULL;
2011 return ctx->nb_registered_frames++;
2022 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2025 NV_ENC_REGISTER_RESOURCE reg = { 0 };
2028 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
2039 reg.version = NV_ENC_REGISTER_RESOURCE_VER;
2040 reg.width = frames_ctx->width;
2041 reg.height = frames_ctx->height;
2042 reg.pitch =
frame->linesize[0];
2043 reg.resourceToRegister =
frame->data[0];
2046 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR;
2049 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX;
2050 reg.subResourceIndex = (intptr_t)
frame->data[1];
2054 if (reg.bufferFormat == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
2060 ret = p_nvenc->nvEncRegisterResource(
ctx->nvencoder, ®);
2061 if (
ret != NV_ENC_SUCCESS) {
2066 ctx->registered_frames[idx].ptr =
frame->data[0];
2067 ctx->registered_frames[idx].ptr_index = reg.subResourceIndex;
2068 ctx->registered_frames[idx].regptr = reg.registeredResource;
2077 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2080 NVENCSTATUS nv_status;
2093 if (!
ctx->registered_frames[reg_idx].mapped) {
2094 ctx->registered_frames[reg_idx].in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER;
2095 ctx->registered_frames[reg_idx].in_map.registeredResource =
ctx->registered_frames[reg_idx].regptr;
2096 nv_status = p_nvenc->nvEncMapInputResource(
ctx->nvencoder, &
ctx->registered_frames[reg_idx].in_map);
2097 if (nv_status != NV_ENC_SUCCESS) {
2103 ctx->registered_frames[reg_idx].mapped += 1;
2105 nvenc_frame->
reg_idx = reg_idx;
2106 nvenc_frame->
input_surface =
ctx->registered_frames[reg_idx].in_map.mappedResource;
2107 nvenc_frame->
format =
ctx->registered_frames[reg_idx].in_map.mappedBufferFmt;
2112 NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
2114 lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
2117 nv_status = p_nvenc->nvEncLockInputBuffer(
ctx->nvencoder, &lockBufferParams);
2118 if (nv_status != NV_ENC_SUCCESS) {
2119 return nvenc_print_error(avctx, nv_status,
"Failed locking nvenc input buffer");
2122 nvenc_frame->
pitch = lockBufferParams.pitch;
2125 nv_status = p_nvenc->nvEncUnlockInputBuffer(
ctx->nvencoder, nvenc_frame->
input_surface);
2126 if (nv_status != NV_ENC_SUCCESS) {
2135 NV_ENC_PIC_PARAMS *params,
2136 NV_ENC_SEI_PAYLOAD *sei_data,
2143 params->codecPicParams.h264PicParams.sliceMode =
2144 ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
2145 params->codecPicParams.h264PicParams.sliceModeData =
2146 ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
2147 if (sei_count > 0) {
2148 params->codecPicParams.h264PicParams.seiPayloadArray = sei_data;
2149 params->codecPicParams.h264PicParams.seiPayloadArrayCnt = sei_count;
2154 params->codecPicParams.hevcPicParams.sliceMode =
2155 ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
2156 params->codecPicParams.hevcPicParams.sliceModeData =
2157 ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
2158 if (sei_count > 0) {
2159 params->codecPicParams.hevcPicParams.seiPayloadArray = sei_data;
2160 params->codecPicParams.hevcPicParams.seiPayloadArrayCnt = sei_count;
2164 #if CONFIG_AV1_NVENC_ENCODER
2166 params->codecPicParams.av1PicParams.numTileColumns =
2167 ctx->encode_config.encodeCodecConfig.av1Config.numTileColumns;
2168 params->codecPicParams.av1PicParams.numTileRows =
2169 ctx->encode_config.encodeCodecConfig.av1Config.numTileRows;
2170 if (sei_count > 0) {
2171 params->codecPicParams.av1PicParams.obuPayloadArray = sei_data;
2172 params->codecPicParams.av1PicParams.obuPayloadArrayCnt = sei_count;
2195 NV_ENC_LOCK_BITSTREAM *params,
2200 pkt->
pts = params->outputTimeStamp;
2216 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2218 NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
2219 NVENCSTATUS nv_status;
2224 lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
2226 lock_params.doNotWait = 0;
2229 nv_status = p_nvenc->nvEncLockBitstream(
ctx->nvencoder, &lock_params);
2230 if (nv_status != NV_ENC_SUCCESS) {
2242 memcpy(
pkt->
data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
2244 nv_status = p_nvenc->nvEncUnlockBitstream(
ctx->nvencoder, tmpoutsurf->
output_surface);
2245 if (nv_status != NV_ENC_SUCCESS) {
2246 res =
nvenc_print_error(avctx, nv_status,
"Failed unlocking bitstream buffer, expect the gates of mordor to open");
2252 ctx->registered_frames[tmpoutsurf->
reg_idx].mapped -= 1;
2253 if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped == 0) {
2254 nv_status = p_nvenc->nvEncUnmapInputResource(
ctx->nvencoder,
ctx->registered_frames[tmpoutsurf->
reg_idx].in_map.mappedResource);
2255 if (nv_status != NV_ENC_SUCCESS) {
2259 }
else if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped < 0) {
2269 switch (lock_params.pictureType) {
2270 case NV_ENC_PIC_TYPE_IDR:
2272 case NV_ENC_PIC_TYPE_I:
2275 case NV_ENC_PIC_TYPE_P:
2278 case NV_ENC_PIC_TYPE_B:
2281 case NV_ENC_PIC_TYPE_BI:
2285 av_log(avctx,
AV_LOG_ERROR,
"Unknown picture type encountered, expect the output to be broken.\n");
2286 av_log(avctx,
AV_LOG_ERROR,
"Please report this error and include as much information on how to reproduce it as possible.\n");
2310 int nb_ready, nb_pending;
2315 return nb_ready > 0;
2316 return (nb_ready > 0) && (nb_ready + nb_pending >=
ctx->async_depth);
2326 void *a53_data =
NULL;
2327 size_t a53_size = 0;
2335 &
ctx->sei_data_size,
2336 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2343 ctx->sei_data[sei_count].payloadSize = (uint32_t)a53_size;
2344 ctx->sei_data[sei_count].payload = (uint8_t*)a53_data;
2346 #if CONFIG_AV1_NVENC_ENCODER
2359 void *tc_data =
NULL;
2368 &
ctx->sei_data_size,
2369 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2376 ctx->sei_data[sei_count].payloadSize = (uint32_t)tc_size;
2377 ctx->sei_data[sei_count].payload = (uint8_t*)tc_data;
2379 #if CONFIG_AV1_NVENC_ENCODER
2394 for (
i = 0;
i <
frame->nb_side_data;
i++) {
2402 &
ctx->sei_data_size,
2403 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2409 ctx->sei_data[sei_count].payloadSize = side_data->
size;
2413 if (!
ctx->sei_data[sei_count].payload) {
2425 for (
i = 0;
i < sei_count;
i++)
2434 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
2437 NV_ENC_RECONFIGURE_PARAMS params = { 0 };
2438 int needs_reconfig = 0;
2439 int needs_encode_config = 0;
2440 int reconfig_bitrate = 0, reconfig_dar = 0;
2443 params.version = NV_ENC_RECONFIGURE_PARAMS_VER;
2444 params.reInitEncodeParams =
ctx->init_encode_params;
2447 if (dw !=
ctx->init_encode_params.darWidth || dh !=
ctx->init_encode_params.darHeight) {
2449 "aspect ratio change (DAR): %d:%d -> %d:%d\n",
2450 ctx->init_encode_params.darWidth,
2451 ctx->init_encode_params.darHeight, dw, dh);
2453 params.reInitEncodeParams.darHeight = dh;
2454 params.reInitEncodeParams.darWidth = dw;
2460 if (
ctx->rc != NV_ENC_PARAMS_RC_CONSTQP &&
ctx->support_dyn_bitrate) {
2461 if (avctx->
bit_rate > 0 && params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate != avctx->
bit_rate) {
2463 "avg bitrate change: %d -> %d\n",
2464 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate,
2467 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate = avctx->
bit_rate;
2468 reconfig_bitrate = 1;
2473 "max bitrate change: %d -> %d\n",
2474 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate,
2477 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate = avctx->
rc_max_rate;
2478 reconfig_bitrate = 1;
2483 "vbv buffer size change: %d -> %d\n",
2484 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize,
2487 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize = avctx->
rc_buffer_size;
2488 reconfig_bitrate = 1;
2491 if (reconfig_bitrate) {
2492 params.resetEncoder = 1;
2493 params.forceIDR = 1;
2495 needs_encode_config = 1;
2500 if (!needs_encode_config)
2501 params.reInitEncodeParams.encodeConfig =
NULL;
2503 if (needs_reconfig) {
2504 ret = p_nvenc->nvEncReconfigureEncoder(
ctx->nvencoder, ¶ms);
2505 if (
ret != NV_ENC_SUCCESS) {
2509 ctx->init_encode_params.darHeight = dh;
2510 ctx->init_encode_params.darWidth = dw;
2513 if (reconfig_bitrate) {
2514 ctx->encode_config.rcParams.averageBitRate = params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate;
2515 ctx->encode_config.rcParams.maxBitRate = params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate;
2516 ctx->encode_config.rcParams.vbvBufferSize = params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize;
2525 NVENCSTATUS nv_status;
2533 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2535 NV_ENC_PIC_PARAMS pic_params = { 0 };
2536 pic_params.version = NV_ENC_PIC_PARAMS_VER;
2538 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
2562 pic_params.bufferFmt = in_surf->
format;
2563 pic_params.inputWidth = in_surf->
width;
2564 pic_params.inputHeight = in_surf->
height;
2565 pic_params.inputPitch = in_surf->
pitch;
2569 if (
frame->top_field_first)
2570 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
2572 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
2574 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
2578 pic_params.encodePicFlags =
2579 ctx->forced_idr ? NV_ENC_PIC_FLAG_FORCEIDR : NV_ENC_PIC_FLAG_FORCEINTRA;
2581 pic_params.encodePicFlags = 0;
2584 pic_params.inputTimeStamp =
frame->pts;
2586 if (
ctx->extra_sei) {
2595 pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
2602 nv_status = p_nvenc->nvEncEncodePicture(
ctx->nvencoder, &pic_params);
2604 for (
i = 0;
i < sei_count;
i++)
2611 if (nv_status != NV_ENC_SUCCESS &&
2612 nv_status != NV_ENC_ERR_NEED_MORE_INPUT)
2623 if (nv_status == NV_ENC_SUCCESS) {
2624 while (
av_fifo_read(
ctx->output_surface_queue, &tmp_out_surf, 1) >= 0)
2640 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
2643 if (!
frame->buf[0]) {