23 #include "config_components.h"
27 #if CONFIG_AV1_NVENC_ENCODER
44 #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, dl_fn->cuda_dl, x)
46 #define NVENC_CAP 0x30
47 #define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR || \
48 rc == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ || \
49 rc == NV_ENC_PARAMS_RC_CBR_HQ)
83 #define IS_10BIT(pix_fmt) (pix_fmt == AV_PIX_FMT_P010 || \
84 pix_fmt == AV_PIX_FMT_P016 || \
85 pix_fmt == AV_PIX_FMT_YUV444P16 || \
86 pix_fmt == AV_PIX_FMT_X2RGB10 || \
87 pix_fmt == AV_PIX_FMT_X2BGR10 || \
88 pix_fmt == AV_PIX_FMT_GBRP16)
90 #define IS_YUV444(pix_fmt) (pix_fmt == AV_PIX_FMT_YUV444P || \
91 pix_fmt == AV_PIX_FMT_YUV444P16 || \
92 pix_fmt == AV_PIX_FMT_GBRP || \
93 pix_fmt == AV_PIX_FMT_GBRP16)
95 #define IS_GBRP(pix_fmt) (pix_fmt == AV_PIX_FMT_GBRP || \
96 pix_fmt == AV_PIX_FMT_GBRP16)
103 { NV_ENC_SUCCESS, 0,
"success" },
104 { NV_ENC_ERR_NO_ENCODE_DEVICE,
AVERROR(ENOENT),
"no encode device" },
105 { NV_ENC_ERR_UNSUPPORTED_DEVICE,
AVERROR(ENOSYS),
"unsupported device" },
106 { NV_ENC_ERR_INVALID_ENCODERDEVICE,
AVERROR(EINVAL),
"invalid encoder device" },
107 { NV_ENC_ERR_INVALID_DEVICE,
AVERROR(EINVAL),
"invalid device" },
108 { NV_ENC_ERR_DEVICE_NOT_EXIST,
AVERROR(EIO),
"device does not exist" },
109 { NV_ENC_ERR_INVALID_PTR,
AVERROR(EFAULT),
"invalid ptr" },
110 { NV_ENC_ERR_INVALID_EVENT,
AVERROR(EINVAL),
"invalid event" },
111 { NV_ENC_ERR_INVALID_PARAM,
AVERROR(EINVAL),
"invalid param" },
112 { NV_ENC_ERR_INVALID_CALL,
AVERROR(EINVAL),
"invalid call" },
113 { NV_ENC_ERR_OUT_OF_MEMORY,
AVERROR(ENOMEM),
"out of memory" },
114 { NV_ENC_ERR_ENCODER_NOT_INITIALIZED,
AVERROR(EINVAL),
"encoder not initialized" },
115 { NV_ENC_ERR_UNSUPPORTED_PARAM,
AVERROR(ENOSYS),
"unsupported param" },
116 { NV_ENC_ERR_LOCK_BUSY,
AVERROR(EAGAIN),
"lock busy" },
118 { NV_ENC_ERR_INVALID_VERSION,
AVERROR(EINVAL),
"invalid version" },
119 { NV_ENC_ERR_MAP_FAILED,
AVERROR(EIO),
"map failed" },
120 { NV_ENC_ERR_NEED_MORE_INPUT,
AVERROR(EAGAIN),
"need more input" },
121 { NV_ENC_ERR_ENCODER_BUSY,
AVERROR(EAGAIN),
"encoder busy" },
122 { NV_ENC_ERR_EVENT_NOT_REGISTERD,
AVERROR(EBADF),
"event not registered" },
124 { NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY,
AVERROR(EINVAL),
"incompatible client key" },
125 { NV_ENC_ERR_UNIMPLEMENTED,
AVERROR(ENOSYS),
"unimplemented" },
126 { NV_ENC_ERR_RESOURCE_REGISTER_FAILED,
AVERROR(EIO),
"resource register failed" },
127 { NV_ENC_ERR_RESOURCE_NOT_REGISTERED,
AVERROR(EBADF),
"resource not registered" },
128 { NV_ENC_ERR_RESOURCE_NOT_MAPPED,
AVERROR(EBADF),
"resource not mapped" },
142 *
desc =
"unknown error";
147 const char *error_string)
150 const char *details =
"(no details)";
153 #ifdef NVENC_HAVE_GETLASTERRORSTRING
155 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
157 if (p_nvenc &&
ctx->nvencoder)
158 details = p_nvenc->nvEncGetLastErrorString(
ctx->nvencoder);
169 #if FF_API_REORDERED_OPAQUE
170 int64_t reordered_opaque;
192 #define PRESET_ALIAS(alias, name, ...) \
193 [PRESET_ ## alias] = { NV_ENC_PRESET_ ## name ## _GUID, __VA_ARGS__ }
195 #define PRESET(name, ...) PRESET_ALIAS(name, name, __VA_ARGS__)
200 #ifdef NVENC_HAVE_NEW_PRESETS
239 ctx->init_encode_params.presetGUID = t->
guid;
242 #ifdef NVENC_HAVE_NEW_PRESETS
243 if (
ctx->tuning_info == NV_ENC_TUNING_INFO_LOSSLESS)
253 #if NVENCAPI_CHECK_VERSION(12, 1)
254 const char *minver =
"(unknown)";
255 #elif NVENCAPI_CHECK_VERSION(12, 0)
256 # if defined(_WIN32) || defined(__CYGWIN__)
257 const char *minver =
"522.25";
259 const char *minver =
"520.56.06";
261 #elif NVENCAPI_CHECK_VERSION(11, 1)
262 # if defined(_WIN32) || defined(__CYGWIN__)
263 const char *minver =
"471.41";
265 const char *minver =
"470.57.02";
267 #elif NVENCAPI_CHECK_VERSION(11, 0)
268 # if defined(_WIN32) || defined(__CYGWIN__)
269 const char *minver =
"456.71";
271 const char *minver =
"455.28";
273 #elif NVENCAPI_CHECK_VERSION(10, 0)
274 # if defined(_WIN32) || defined(__CYGWIN__)
275 const char *minver =
"450.51";
277 const char *minver =
"445.87";
279 #elif NVENCAPI_CHECK_VERSION(9, 1)
280 # if defined(_WIN32) || defined(__CYGWIN__)
281 const char *minver =
"436.15";
283 const char *minver =
"435.21";
285 #elif NVENCAPI_CHECK_VERSION(9, 0)
286 # if defined(_WIN32) || defined(__CYGWIN__)
287 const char *minver =
"418.81";
289 const char *minver =
"418.30";
291 #elif NVENCAPI_CHECK_VERSION(8, 2)
292 # if defined(_WIN32) || defined(__CYGWIN__)
293 const char *minver =
"397.93";
295 const char *minver =
"396.24";
297 #elif NVENCAPI_CHECK_VERSION(8, 1)
298 # if defined(_WIN32) || defined(__CYGWIN__)
299 const char *minver =
"390.77";
301 const char *minver =
"390.25";
304 # if defined(_WIN32) || defined(__CYGWIN__)
305 const char *minver =
"378.66";
307 const char *minver =
"378.13";
310 av_log(avctx,
level,
"The minimum required Nvidia driver for nvenc is %s or newer\n", minver);
318 uint32_t nvenc_max_ver;
321 ret = cuda_load_functions(&dl_fn->
cuda_dl, avctx);
325 ret = nvenc_load_functions(&dl_fn->
nvenc_dl, avctx);
331 err = dl_fn->
nvenc_dl->NvEncodeAPIGetMaxSupportedVersion(&nvenc_max_ver);
332 if (err != NV_ENC_SUCCESS)
337 if ((NVENCAPI_MAJOR_VERSION << 4 | NVENCAPI_MINOR_VERSION) > nvenc_max_ver) {
338 av_log(avctx,
AV_LOG_ERROR,
"Driver does not support the required nvenc API version. "
339 "Required: %d.%d Found: %d.%d\n",
340 NVENCAPI_MAJOR_VERSION, NVENCAPI_MINOR_VERSION,
341 nvenc_max_ver >> 4, nvenc_max_ver & 0
xf);
346 dl_fn->
nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
349 if (err != NV_ENC_SUCCESS)
362 if (
ctx->d3d11_device)
374 if (
ctx->d3d11_device)
382 NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = { 0 };
384 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
387 params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
388 params.apiVersion = NVENCAPI_VERSION;
389 if (
ctx->d3d11_device) {
390 params.device =
ctx->d3d11_device;
391 params.deviceType = NV_ENC_DEVICE_TYPE_DIRECTX;
393 params.device =
ctx->cu_context;
394 params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
397 ret = p_nvenc->nvEncOpenEncodeSessionEx(¶ms, &
ctx->nvencoder);
398 if (
ret != NV_ENC_SUCCESS) {
409 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
410 int i,
ret, count = 0;
413 ret = p_nvenc->nvEncGetEncodeGUIDCount(
ctx->nvencoder, &count);
415 if (
ret != NV_ENC_SUCCESS || !count)
422 ret = p_nvenc->nvEncGetEncodeGUIDs(
ctx->nvencoder, guids, count, &count);
423 if (
ret != NV_ENC_SUCCESS) {
429 for (
i = 0;
i < count;
i++) {
430 if (!memcmp(&guids[
i], &
ctx->init_encode_params.encodeGUID,
sizeof(*guids))) {
445 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
446 NV_ENC_CAPS_PARAM params = { 0 };
449 params.version = NV_ENC_CAPS_PARAM_VER;
450 params.capsToQuery = cap;
452 ret = p_nvenc->nvEncGetEncodeCaps(
ctx->nvencoder,
ctx->init_encode_params.encodeGUID, ¶ms, &
val);
454 if (
ret == NV_ENC_SUCCESS)
483 if (ret < avctx->
width) {
490 if (ret < avctx->
height) {
497 if (ret < avctx->max_b_frames) {
507 "Interlaced encoding is not supported. Supported level: %d\n",
519 if (
ctx->rc_lookahead > 0 &&
ret <= 0) {
525 if (
ctx->temporal_aq > 0 &&
ret <= 0) {
531 if (
ctx->weighted_pred > 0 &&
ret <= 0) {
537 if (
ctx->coder == NV_ENC_H264_ENTROPY_CODING_MODE_CABAC &&
ret <= 0) {
542 #ifdef NVENC_HAVE_BFRAME_REF_MODE
543 tmp = (
ctx->b_ref_mode >= 0) ?
ctx->b_ref_mode : NV_ENC_BFRAME_REF_MODE_DISABLED;
545 if (
tmp == NV_ENC_BFRAME_REF_MODE_EACH &&
ret != 1 &&
ret != 3) {
548 }
else if (
tmp != NV_ENC_BFRAME_REF_MODE_DISABLED &&
ret == 0) {
553 tmp = (
ctx->b_ref_mode >= 0) ?
ctx->b_ref_mode : 0;
560 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
562 if(avctx->
refs != NV_ENC_NUM_REF_FRAMES_AUTOSELECT &&
ret <= 0) {
567 if(avctx->
refs != 0) {
573 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
575 if(
ctx->single_slice_intra_refresh &&
ret <= 0) {
580 if(
ctx->single_slice_intra_refresh) {
587 if((
ctx->intra_refresh ||
ctx->single_slice_intra_refresh) &&
ret <= 0) {
592 #ifndef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
600 if(
ctx->constrained_encoding &&
ret <= 0) {
605 ctx->support_dyn_bitrate =
nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE);
614 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
615 char name[128] = { 0};
635 av_log(avctx, loglevel,
"[ GPU #%d - < %s > has Compute SM %d.%d ]\n", idx,
name,
major,
minor);
637 av_log(avctx, loglevel,
"does not support NVENC\n");
648 ctx->cu_context =
ctx->cu_context_internal;
660 av_log(avctx, loglevel,
"supports NVENC\n");
671 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
679 ctx->cu_context_internal =
NULL;
692 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_H264_GUID;
695 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_HEVC_GUID;
697 #if CONFIG_AV1_NVENC_ENCODER
699 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_AV1_GUID;
709 av_log(avctx,
AV_LOG_WARNING,
"The selected preset is deprecated. Use p1 to p7 + -tune or fast/medium/slow.\n");
733 cuda_device_hwctx = hwdev_ctx->
hwctx;
736 d3d11_device_hwctx = hwdev_ctx->
hwctx;
744 if (cuda_device_hwctx) {
746 ctx->cu_stream = cuda_device_hwctx->
stream;
749 else if (d3d11_device_hwctx) {
750 ctx->d3d11_device = d3d11_device_hwctx->
device;
751 ID3D11Device_AddRef(
ctx->d3d11_device);
761 av_log(avctx,
AV_LOG_FATAL,
"Provided device doesn't support required NVENC features\n");
765 int i, nb_devices = 0;
781 for (
i = 0;
i < nb_devices; ++
i) {
794 av_log(avctx,
AV_LOG_FATAL,
"Requested GPU %d, but only %d GPUs are available!\n",
ctx->device, nb_devices);
804 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
805 #if CONFIG_AV1_NVENC_ENCODER
811 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
813 if (
ctx->init_qp_p >= 0) {
814 rc->constQP.qpInterP =
ctx->init_qp_p;
815 if (
ctx->init_qp_i >= 0 &&
ctx->init_qp_b >= 0) {
816 rc->constQP.qpIntra =
ctx->init_qp_i;
817 rc->constQP.qpInterB =
ctx->init_qp_b;
821 rc->constQP.qpInterB =
av_clip(
824 rc->constQP.qpIntra = rc->constQP.qpInterP;
825 rc->constQP.qpInterB = rc->constQP.qpInterP;
827 }
else if (
ctx->cqp >= 0) {
828 rc->constQP.qpInterP = rc->constQP.qpInterB = rc->constQP.qpIntra =
ctx->cqp;
842 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
844 #if CONFIG_AV1_NVENC_ENCODER
850 if (avctx->
qmin >= 0 && avctx->
qmax >= 0) {
854 rc->minQP.qpInterB = avctx->
qmin;
855 rc->minQP.qpInterP = avctx->
qmin;
856 rc->minQP.qpIntra = avctx->
qmin;
858 rc->maxQP.qpInterB = avctx->
qmax;
859 rc->maxQP.qpInterP = avctx->
qmax;
860 rc->maxQP.qpIntra = avctx->
qmax;
862 qp_inter_p = (avctx->
qmax + 3 * avctx->
qmin) / 4;
863 }
else if (avctx->
qmin >= 0) {
866 rc->minQP.qpInterB = avctx->
qmin;
867 rc->minQP.qpInterP = avctx->
qmin;
868 rc->minQP.qpIntra = avctx->
qmin;
870 qp_inter_p = avctx->
qmin;
875 rc->enableInitialRCQP = 1;
877 if (
ctx->init_qp_p < 0) {
878 rc->initialRCQP.qpInterP = qp_inter_p;
880 rc->initialRCQP.qpInterP =
ctx->init_qp_p;
883 if (
ctx->init_qp_i < 0) {
885 rc->initialRCQP.qpIntra =
av_clip(
888 rc->initialRCQP.qpIntra = rc->initialRCQP.qpInterP;
891 rc->initialRCQP.qpIntra =
ctx->init_qp_i;
894 if (
ctx->init_qp_b < 0) {
896 rc->initialRCQP.qpInterB =
av_clip(
899 rc->initialRCQP.qpInterB = rc->initialRCQP.qpInterP;
902 rc->initialRCQP.qpInterB =
ctx->init_qp_b;
909 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
911 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
912 rc->constQP.qpInterB = 0;
913 rc->constQP.qpInterP = 0;
914 rc->constQP.qpIntra = 0;
923 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
926 case NV_ENC_PARAMS_RC_CONSTQP:
929 case NV_ENC_PARAMS_RC_VBR_MINQP:
930 if (avctx->
qmin < 0) {
932 "The variable bitrate rate-control requires "
933 "the 'qmin' option set.\n");
938 case NV_ENC_PARAMS_RC_VBR_HQ:
939 case NV_ENC_PARAMS_RC_VBR:
942 case NV_ENC_PARAMS_RC_CBR:
943 case NV_ENC_PARAMS_RC_CBR_HQ:
944 case NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ:
948 rc->rateControlMode =
ctx->rc;
957 int nb_surfaces =
FFMAX(4,
ctx->encode_config.frameIntervalP * 2 * 2);
960 if (
ctx->rc_lookahead > 0) {
963 nb_surfaces =
FFMAX(1,
FFMAX(nb_surfaces,
ctx->rc_lookahead +
ctx->encode_config.frameIntervalP + 1 + 4));
964 if (nb_surfaces >
ctx->nb_surfaces &&
ctx->nb_surfaces > 0)
967 "Defined rc_lookahead requires more surfaces, "
968 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
970 ctx->nb_surfaces =
FFMAX(nb_surfaces,
ctx->nb_surfaces);
972 if (
ctx->encode_config.frameIntervalP > 1 &&
ctx->nb_surfaces < nb_surfaces &&
ctx->nb_surfaces > 0)
975 "Defined b-frame requires more surfaces, "
976 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
977 ctx->nb_surfaces =
FFMAX(
ctx->nb_surfaces, nb_surfaces);
979 else if (
ctx->nb_surfaces <= 0)
980 ctx->nb_surfaces = nb_surfaces;
995 av_log(avctx,
AV_LOG_WARNING,
"Using global_quality with nvenc is deprecated. Use qp instead.\n");
1001 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate;
1002 }
else if (
ctx->encode_config.rcParams.averageBitRate > 0) {
1003 ctx->encode_config.rcParams.maxBitRate =
ctx->encode_config.rcParams.averageBitRate;
1009 #ifdef NVENC_HAVE_MULTIPASS
1010 ctx->encode_config.rcParams.multiPass =
ctx->multipass;
1013 ctx->encode_config.rcParams.multiPass = NV_ENC_MULTI_PASS_DISABLED;
1015 ctx->encode_config.rcParams.multiPass = NV_ENC_TWO_PASS_FULL_RESOLUTION;
1019 ctx->rc = NV_ENC_PARAMS_RC_CBR;
1020 }
else if (
ctx->cqp >= 0) {
1021 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
1022 }
else if (
ctx->quality >= 0.0f) {
1023 ctx->rc = NV_ENC_PARAMS_RC_VBR;
1033 if (
ctx->twopass < 0)
1038 ctx->rc = NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ;
1040 ctx->rc = NV_ENC_PARAMS_RC_CBR;
1042 }
else if (
ctx->cqp >= 0) {
1043 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
1044 }
else if (
ctx->twopass) {
1045 ctx->rc = NV_ENC_PARAMS_RC_VBR_HQ;
1046 }
else if (avctx->
qmin >= 0 && avctx->
qmax >= 0) {
1047 ctx->rc = NV_ENC_PARAMS_RC_VBR_MINQP;
1059 #ifdef NVENC_HAVE_QP_CHROMA_OFFSETS
1060 ctx->encode_config.rcParams.cbQPIndexOffset =
ctx->qp_cb_offset;
1061 ctx->encode_config.rcParams.crQPIndexOffset =
ctx->qp_cr_offset;
1063 if (
ctx->qp_cb_offset ||
ctx->qp_cr_offset)
1064 av_log(avctx,
AV_LOG_WARNING,
"Failed setting QP CB/CR offsets, SDK 11.1 or greater required at compile time.\n");
1067 #ifdef NVENC_HAVE_LDKFS
1069 ctx->encode_config.rcParams.lowDelayKeyFrameScale =
ctx->ldkfs;
1074 }
else if (
ctx->rc >= 0) {
1077 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
1083 }
else if (
ctx->encode_config.rcParams.averageBitRate > 0) {
1084 avctx->
rc_buffer_size =
ctx->encode_config.rcParams.vbvBufferSize = 2 *
ctx->encode_config.rcParams.averageBitRate;
1088 ctx->encode_config.rcParams.enableAQ = 1;
1089 ctx->encode_config.rcParams.aqStrength =
ctx->aq_strength;
1093 if (
ctx->temporal_aq) {
1094 ctx->encode_config.rcParams.enableTemporalAQ = 1;
1098 if (
ctx->rc_lookahead > 0) {
1099 int lkd_bound =
FFMIN(
ctx->nb_surfaces,
ctx->async_depth) -
1100 ctx->encode_config.frameIntervalP - 4;
1102 if (lkd_bound < 0) {
1103 ctx->encode_config.rcParams.enableLookahead = 0;
1105 "Lookahead not enabled. Increase buffer delay (-delay).\n");
1107 ctx->encode_config.rcParams.enableLookahead = 1;
1108 ctx->encode_config.rcParams.lookaheadDepth =
av_clip(
ctx->rc_lookahead, 0, lkd_bound);
1109 ctx->encode_config.rcParams.disableIadapt =
ctx->no_scenecut;
1110 ctx->encode_config.rcParams.disableBadapt = !
ctx->b_adapt;
1112 "Lookahead enabled: depth %d, scenecut %s, B-adapt %s.\n",
1113 ctx->encode_config.rcParams.lookaheadDepth,
1114 ctx->encode_config.rcParams.disableIadapt ?
"disabled" :
"enabled",
1115 ctx->encode_config.rcParams.disableBadapt ?
"disabled" :
"enabled");
1116 if (
ctx->encode_config.rcParams.lookaheadDepth <
ctx->rc_lookahead)
1117 av_log(avctx,
AV_LOG_WARNING,
"Clipping lookahead depth to %d (from %d) due to lack of surfaces/delay",
1118 ctx->encode_config.rcParams.lookaheadDepth,
ctx->rc_lookahead);
1122 if (
ctx->strict_gop) {
1123 ctx->encode_config.rcParams.strictGOPTarget = 1;
1128 ctx->encode_config.rcParams.enableNonRefP = 1;
1130 if (
ctx->zerolatency)
1131 ctx->encode_config.rcParams.zeroReorderDelay = 1;
1135 int tmp_quality = (
int)(
ctx->quality * 256.0f);
1136 ctx->encode_config.rcParams.targetQuality = (uint8_t)(tmp_quality >> 8);
1137 ctx->encode_config.rcParams.targetQualityLSB = (uint8_t)(tmp_quality & 0xff);
1142 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate = 0;
1151 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1152 NV_ENC_CONFIG_H264 *h264 = &cc->encodeCodecConfig.h264Config;
1153 NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui = &h264->h264VUIParameters;
1160 vui->transferCharacteristics = avctx->
color_trc;
1161 vui->videoFullRangeFlag = 0;
1165 vui->transferCharacteristics = avctx->
color_trc;
1170 vui->colourDescriptionPresentFlag =
1171 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1173 vui->videoSignalTypePresentFlag =
1174 (vui->colourDescriptionPresentFlag
1175 || vui->videoFormat != 5
1176 || vui->videoFullRangeFlag != 0);
1178 h264->sliceMode = 3;
1179 h264->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1181 if (
ctx->intra_refresh) {
1182 h264->enableIntraRefresh = 1;
1183 h264->intraRefreshPeriod = avctx->
gop_size;
1184 h264->intraRefreshCnt = avctx->
gop_size - 1;
1185 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1186 h264->singleSliceIntraRefresh =
ctx->single_slice_intra_refresh;
1190 if (
ctx->constrained_encoding)
1191 h264->enableConstrainedEncoding = 1;
1195 h264->outputAUD =
ctx->aud;
1197 if (
ctx->dpb_size >= 0) {
1199 h264->maxNumRefFrames =
ctx->dpb_size;
1202 if (
ctx->intra_refresh) {
1203 h264->idrPeriod = NVENC_INFINITE_GOPLENGTH;
1208 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1209 h264->outputBufferingPeriodSEI = 1;
1212 h264->outputPictureTimingSEI = 1;
1214 if (cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ ||
1215 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_HQ ||
1216 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_VBR_HQ) {
1217 h264->adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
1218 h264->fmoMode = NV_ENC_H264_FMO_DISABLE;
1222 h264->qpPrimeYZeroTransformBypassFlag = 1;
1224 switch(
ctx->profile) {
1226 cc->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
1230 cc->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
1234 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
1238 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1246 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1252 h264->level =
ctx->level;
1254 if (
ctx->coder >= 0)
1255 h264->entropyCodingMode =
ctx->coder;
1257 #ifdef NVENC_HAVE_BFRAME_REF_MODE
1258 if (
ctx->b_ref_mode >= 0)
1259 h264->useBFramesAsRef =
ctx->b_ref_mode;
1262 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1263 h264->numRefL0 = avctx->
refs;
1264 h264->numRefL1 = avctx->
refs;
1273 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1274 NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig;
1275 NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui = &hevc->hevcVUIParameters;
1282 vui->transferCharacteristics = avctx->
color_trc;
1283 vui->videoFullRangeFlag = 0;
1287 vui->transferCharacteristics = avctx->
color_trc;
1292 vui->colourDescriptionPresentFlag =
1293 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1295 vui->videoSignalTypePresentFlag =
1296 (vui->colourDescriptionPresentFlag
1297 || vui->videoFormat != 5
1298 || vui->videoFullRangeFlag != 0);
1300 hevc->sliceMode = 3;
1301 hevc->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1303 if (
ctx->intra_refresh) {
1304 hevc->enableIntraRefresh = 1;
1305 hevc->intraRefreshPeriod = avctx->
gop_size;
1306 hevc->intraRefreshCnt = avctx->
gop_size - 1;
1307 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1308 hevc->singleSliceIntraRefresh =
ctx->single_slice_intra_refresh;
1312 #ifdef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
1313 if (
ctx->constrained_encoding)
1314 hevc->enableConstrainedEncoding = 1;
1319 hevc->outputAUD =
ctx->aud;
1321 if (
ctx->dpb_size >= 0) {
1323 hevc->maxNumRefFramesInDPB =
ctx->dpb_size;
1326 if (
ctx->intra_refresh) {
1327 hevc->idrPeriod = NVENC_INFINITE_GOPLENGTH;
1332 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1333 hevc->outputBufferingPeriodSEI = 1;
1336 hevc->outputPictureTimingSEI = 1;
1338 switch (
ctx->profile) {
1340 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
1344 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1348 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1355 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1361 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1365 hevc->chromaFormatIDC =
IS_YUV444(
ctx->data_pix_fmt) ? 3 : 1;
1367 hevc->pixelBitDepthMinus8 =
IS_10BIT(
ctx->data_pix_fmt) ? 2 : 0;
1369 hevc->level =
ctx->level;
1371 hevc->tier =
ctx->tier;
1373 #ifdef NVENC_HAVE_HEVC_BFRAME_REF_MODE
1374 if (
ctx->b_ref_mode >= 0)
1375 hevc->useBFramesAsRef =
ctx->b_ref_mode;
1378 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1379 hevc->numRefL0 = avctx->
refs;
1380 hevc->numRefL1 = avctx->
refs;
1386 #if CONFIG_AV1_NVENC_ENCODER
1390 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1391 NV_ENC_CONFIG_AV1 *av1 = &cc->encodeCodecConfig.av1Config;
1398 av1->transferCharacteristics = avctx->
color_trc;
1399 av1->colorRange = 0;
1403 av1->transferCharacteristics = avctx->
color_trc;
1409 av_log(avctx,
AV_LOG_ERROR,
"AV1 High Profile not supported, required for 4:4:4 encoding\n");
1412 cc->profileGUID = NV_ENC_AV1_PROFILE_MAIN_GUID;
1416 if (
ctx->dpb_size >= 0) {
1418 av1->maxNumRefFramesInDPB =
ctx->dpb_size;
1421 if (
ctx->intra_refresh) {
1422 av1->enableIntraRefresh = 1;
1423 av1->intraRefreshPeriod = avctx->
gop_size;
1424 av1->intraRefreshCnt = avctx->
gop_size - 1;
1426 av1->idrPeriod = NVENC_INFINITE_GOPLENGTH;
1431 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1432 av1->enableBitstreamPadding = 1;
1435 if (
ctx->tile_cols >= 0)
1436 av1->numTileColumns =
ctx->tile_cols;
1437 if (
ctx->tile_rows >= 0)
1438 av1->numTileRows =
ctx->tile_rows;
1440 av1->outputAnnexBFormat = 0;
1442 av1->level =
ctx->level;
1443 av1->tier =
ctx->tier;
1445 av1->enableTimingInfo =
ctx->timing_info;
1448 av1->disableSeqHdr = 0;
1449 av1->repeatSeqHdr = 1;
1451 av1->chromaFormatIDC =
IS_YUV444(
ctx->data_pix_fmt) ? 3 : 1;
1453 av1->inputPixelBitDepthMinus8 =
IS_10BIT(
ctx->data_pix_fmt) ? 2 : 0;
1454 av1->pixelBitDepthMinus8 = (
IS_10BIT(
ctx->data_pix_fmt) ||
ctx->highbitdepth) ? 2 : 0;
1456 if (
ctx->b_ref_mode >= 0)
1457 av1->useBFramesAsRef =
ctx->b_ref_mode;
1459 av1->numFwdRefs = avctx->
refs;
1460 av1->numBwdRefs = avctx->
refs;
1473 #if CONFIG_AV1_NVENC_ENCODER
1475 return nvenc_setup_av1_config(avctx);
1489 #if CONFIG_AV1_NVENC_ENCODER
1520 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1522 NV_ENC_PRESET_CONFIG preset_config = { 0 };
1523 NVENCSTATUS nv_status = NV_ENC_SUCCESS;
1528 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1529 ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
1531 ctx->init_encode_params.encodeHeight = avctx->
height;
1532 ctx->init_encode_params.encodeWidth = avctx->
width;
1534 ctx->init_encode_params.encodeConfig = &
ctx->encode_config;
1536 preset_config.version = NV_ENC_PRESET_CONFIG_VER;
1537 preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
1539 #ifdef NVENC_HAVE_NEW_PRESETS
1540 ctx->init_encode_params.tuningInfo =
ctx->tuning_info;
1543 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOSSLESS;
1545 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOW_LATENCY;
1547 nv_status = p_nvenc->nvEncGetEncodePresetConfigEx(
ctx->nvencoder,
1548 ctx->init_encode_params.encodeGUID,
1549 ctx->init_encode_params.presetGUID,
1550 ctx->init_encode_params.tuningInfo,
1553 nv_status = p_nvenc->nvEncGetEncodePresetConfig(
ctx->nvencoder,
1554 ctx->init_encode_params.encodeGUID,
1555 ctx->init_encode_params.presetGUID,
1558 if (nv_status != NV_ENC_SUCCESS)
1559 return nvenc_print_error(avctx, nv_status,
"Cannot get the preset configuration");
1561 memcpy(&
ctx->encode_config, &preset_config.presetCfg,
sizeof(
ctx->encode_config));
1563 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1566 ctx->init_encode_params.darHeight = dh;
1567 ctx->init_encode_params.darWidth = dw;
1577 ctx->init_encode_params.enableEncodeAsync = 0;
1578 ctx->init_encode_params.enablePTD = 1;
1580 #ifdef NVENC_HAVE_NEW_PRESETS
1584 if (
ctx->rc_lookahead == 0 &&
ctx->encode_config.rcParams.enableLookahead)
1585 ctx->rc_lookahead =
ctx->encode_config.rcParams.lookaheadDepth;
1588 if (
ctx->weighted_pred == 1)
1589 ctx->init_encode_params.enableWeightedPrediction = 1;
1591 if (
ctx->bluray_compat) {
1600 ctx->level = NV_ENC_LEVEL_HEVC_51;
1601 ctx->tier = NV_ENC_TIER_HEVC_HIGH;
1614 ctx->encode_config.frameIntervalP = 0;
1615 ctx->encode_config.gopLength = 1;
1619 if(
ctx->single_slice_intra_refresh)
1620 ctx->intra_refresh = 1;
1622 if (
ctx->intra_refresh)
1623 ctx->encode_config.gopLength = NVENC_INFINITE_GOPLENGTH;
1630 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
1632 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
1643 nv_status = p_nvenc->nvEncInitializeEncoder(
ctx->nvencoder, &
ctx->init_encode_params);
1644 if (nv_status != NV_ENC_SUCCESS) {
1649 #ifdef NVENC_HAVE_CUSTREAM_PTR
1650 if (
ctx->cu_context) {
1651 nv_status = p_nvenc->nvEncSetIOCudaStreams(
ctx->nvencoder, &
ctx->cu_stream, &
ctx->cu_stream);
1652 if (nv_status != NV_ENC_SUCCESS) {
1663 if (
ctx->encode_config.frameIntervalP > 1)
1666 if (
ctx->encode_config.rcParams.averageBitRate > 0)
1667 avctx->
bit_rate =
ctx->encode_config.rcParams.averageBitRate;
1674 cpb_props->
buffer_size =
ctx->encode_config.rcParams.vbvBufferSize;
1683 return NV_ENC_BUFFER_FORMAT_YV12_PL;
1685 return NV_ENC_BUFFER_FORMAT_NV12_PL;
1688 return NV_ENC_BUFFER_FORMAT_YUV420_10BIT;
1691 return NV_ENC_BUFFER_FORMAT_YUV444_PL;
1694 return NV_ENC_BUFFER_FORMAT_YUV444_10BIT;
1697 return NV_ENC_BUFFER_FORMAT_ARGB;
1700 return NV_ENC_BUFFER_FORMAT_ABGR;
1702 return NV_ENC_BUFFER_FORMAT_ARGB10;
1704 return NV_ENC_BUFFER_FORMAT_ABGR10;
1706 return NV_ENC_BUFFER_FORMAT_UNDEFINED;
1714 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1717 NVENCSTATUS nv_status;
1718 NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
1719 allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
1723 if (!
ctx->surfaces[idx].in_ref)
1726 NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
1729 if (
ctx->surfaces[idx].format == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
1735 allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
1736 allocSurf.width = avctx->
width;
1737 allocSurf.height = avctx->
height;
1738 allocSurf.bufferFmt =
ctx->surfaces[idx].format;
1740 nv_status = p_nvenc->nvEncCreateInputBuffer(
ctx->nvencoder, &allocSurf);
1741 if (nv_status != NV_ENC_SUCCESS) {
1745 ctx->surfaces[idx].input_surface = allocSurf.inputBuffer;
1746 ctx->surfaces[idx].width = allocSurf.width;
1747 ctx->surfaces[idx].height = allocSurf.height;
1750 nv_status = p_nvenc->nvEncCreateBitstreamBuffer(
ctx->nvencoder, &allocOut);
1751 if (nv_status != NV_ENC_SUCCESS) {
1754 p_nvenc->nvEncDestroyInputBuffer(
ctx->nvencoder,
ctx->surfaces[idx].input_surface);
1759 ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer;
1769 int i, res = 0, res2;
1776 if (!
ctx->reorder_queue)
1780 if (!
ctx->unused_surface_queue)
1784 if (!
ctx->output_surface_queue)
1787 if (!
ctx->output_surface_ready_queue)
1794 for (
i = 0;
i <
ctx->nb_surfaces;
i++) {
1811 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1813 NVENCSTATUS nv_status;
1814 uint32_t outSize = 0;
1815 char tmpHeader[NV_MAX_SEQ_HDR_LEN];
1817 NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
1818 payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
1820 payload.spsppsBuffer = tmpHeader;
1821 payload.inBufferSize =
sizeof(tmpHeader);
1822 payload.outSPSPPSPayloadSize = &outSize;
1824 nv_status = p_nvenc->nvEncGetSequenceParams(
ctx->nvencoder, &payload);
1825 if (nv_status != NV_ENC_SUCCESS) {
1836 memcpy(avctx->
extradata, tmpHeader, outSize);
1845 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1849 if (
ctx->nvencoder) {
1850 NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER,
1851 .encodePicFlags = NV_ENC_PIC_FLAG_EOS };
1857 p_nvenc->nvEncEncodePicture(
ctx->nvencoder, ¶ms);
1860 if (
ctx->reorder_queue) {
1870 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
1871 if (
ctx->registered_frames[
i].mapped)
1872 p_nvenc->nvEncUnmapInputResource(
ctx->nvencoder,
ctx->registered_frames[
i].in_map.mappedResource);
1873 if (
ctx->registered_frames[
i].regptr)
1874 p_nvenc->nvEncUnregisterResource(
ctx->nvencoder,
ctx->registered_frames[
i].regptr);
1876 ctx->nb_registered_frames = 0;
1879 if (
ctx->surfaces) {
1880 for (
i = 0;
i <
ctx->nb_surfaces; ++
i) {
1882 p_nvenc->nvEncDestroyInputBuffer(
ctx->nvencoder,
ctx->surfaces[
i].input_surface);
1884 p_nvenc->nvEncDestroyBitstreamBuffer(
ctx->nvencoder,
ctx->surfaces[
i].output_surface);
1888 ctx->nb_surfaces = 0;
1894 if (
ctx->nvencoder) {
1895 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
1903 if (
ctx->cu_context_internal)
1905 ctx->cu_context =
ctx->cu_context_internal =
NULL;
1908 if (
ctx->d3d11_device) {
1909 ID3D11Device_Release(
ctx->d3d11_device);
1914 nvenc_free_functions(&dl_fn->
nvenc_dl);
1915 cuda_free_functions(&dl_fn->
cuda_dl);
1933 "hw_frames_ctx must be set when using GPU frames as input\n");
1939 "hw_frames_ctx must match the GPU frame type\n");
1983 NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params,
const AVFrame *
frame)
1985 int dst_linesize[4] = {
1986 lock_buffer_params->pitch,
1987 lock_buffer_params->pitch,
1988 lock_buffer_params->pitch,
1989 lock_buffer_params->pitch
1991 uint8_t *dst_data[4];
1995 dst_linesize[1] = dst_linesize[2] >>= 1;
1998 lock_buffer_params->bufferDataPtr, dst_linesize);
2003 FFSWAP(uint8_t*, dst_data[1], dst_data[2]);
2016 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2017 NVENCSTATUS nv_status;
2022 for (first_round = 1; first_round >= 0; first_round--) {
2023 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
2024 if (!
ctx->registered_frames[
i].mapped) {
2025 if (
ctx->registered_frames[
i].regptr) {
2028 nv_status = p_nvenc->nvEncUnregisterResource(
ctx->nvencoder,
ctx->registered_frames[
i].regptr);
2029 if (nv_status != NV_ENC_SUCCESS)
2030 return nvenc_print_error(avctx, nv_status,
"Failed unregistering unused input resource");
2031 ctx->registered_frames[
i].ptr =
NULL;
2032 ctx->registered_frames[
i].regptr =
NULL;
2039 return ctx->nb_registered_frames++;
2050 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2053 NV_ENC_REGISTER_RESOURCE reg = { 0 };
2056 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
2067 reg.version = NV_ENC_REGISTER_RESOURCE_VER;
2068 reg.width = frames_ctx->width;
2069 reg.height = frames_ctx->height;
2070 reg.pitch =
frame->linesize[0];
2071 reg.resourceToRegister =
frame->data[0];
2074 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR;
2077 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX;
2078 reg.subResourceIndex = (intptr_t)
frame->data[1];
2082 if (reg.bufferFormat == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
2088 ret = p_nvenc->nvEncRegisterResource(
ctx->nvencoder, ®);
2089 if (
ret != NV_ENC_SUCCESS) {
2094 ctx->registered_frames[idx].ptr =
frame->data[0];
2095 ctx->registered_frames[idx].ptr_index = reg.subResourceIndex;
2096 ctx->registered_frames[idx].regptr = reg.registeredResource;
2105 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2108 NVENCSTATUS nv_status;
2121 if (!
ctx->registered_frames[reg_idx].mapped) {
2122 ctx->registered_frames[reg_idx].in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER;
2123 ctx->registered_frames[reg_idx].in_map.registeredResource =
ctx->registered_frames[reg_idx].regptr;
2124 nv_status = p_nvenc->nvEncMapInputResource(
ctx->nvencoder, &
ctx->registered_frames[reg_idx].in_map);
2125 if (nv_status != NV_ENC_SUCCESS) {
2131 ctx->registered_frames[reg_idx].mapped += 1;
2133 nvenc_frame->
reg_idx = reg_idx;
2134 nvenc_frame->
input_surface =
ctx->registered_frames[reg_idx].in_map.mappedResource;
2135 nvenc_frame->
format =
ctx->registered_frames[reg_idx].in_map.mappedBufferFmt;
2140 NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
2142 lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
2145 nv_status = p_nvenc->nvEncLockInputBuffer(
ctx->nvencoder, &lockBufferParams);
2146 if (nv_status != NV_ENC_SUCCESS) {
2147 return nvenc_print_error(avctx, nv_status,
"Failed locking nvenc input buffer");
2150 nvenc_frame->
pitch = lockBufferParams.pitch;
2153 nv_status = p_nvenc->nvEncUnlockInputBuffer(
ctx->nvencoder, nvenc_frame->
input_surface);
2154 if (nv_status != NV_ENC_SUCCESS) {
2163 NV_ENC_PIC_PARAMS *params,
2164 NV_ENC_SEI_PAYLOAD *sei_data,
2171 params->codecPicParams.h264PicParams.sliceMode =
2172 ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
2173 params->codecPicParams.h264PicParams.sliceModeData =
2174 ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
2175 if (sei_count > 0) {
2176 params->codecPicParams.h264PicParams.seiPayloadArray = sei_data;
2177 params->codecPicParams.h264PicParams.seiPayloadArrayCnt = sei_count;
2182 params->codecPicParams.hevcPicParams.sliceMode =
2183 ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
2184 params->codecPicParams.hevcPicParams.sliceModeData =
2185 ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
2186 if (sei_count > 0) {
2187 params->codecPicParams.hevcPicParams.seiPayloadArray = sei_data;
2188 params->codecPicParams.hevcPicParams.seiPayloadArrayCnt = sei_count;
2192 #if CONFIG_AV1_NVENC_ENCODER
2194 params->codecPicParams.av1PicParams.numTileColumns =
2195 ctx->encode_config.encodeCodecConfig.av1Config.numTileColumns;
2196 params->codecPicParams.av1PicParams.numTileRows =
2197 ctx->encode_config.encodeCodecConfig.av1Config.numTileRows;
2198 if (sei_count > 0) {
2199 params->codecPicParams.av1PicParams.obuPayloadArray = sei_data;
2200 params->codecPicParams.av1PicParams.obuPayloadArrayCnt = sei_count;
2215 #if FF_API_REORDERED_OPAQUE
2217 fd.reordered_opaque =
frame->reordered_opaque;
2238 #if FF_API_REORDERED_OPAQUE
2240 avctx->reordered_opaque = fd.reordered_opaque;
2258 NV_ENC_LOCK_BITSTREAM *params,
2264 pkt->
pts = params->outputTimeStamp;
2281 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2283 NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
2284 NVENCSTATUS nv_status;
2289 lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
2291 lock_params.doNotWait = 0;
2294 nv_status = p_nvenc->nvEncLockBitstream(
ctx->nvencoder, &lock_params);
2295 if (nv_status != NV_ENC_SUCCESS) {
2307 memcpy(
pkt->
data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
2309 nv_status = p_nvenc->nvEncUnlockBitstream(
ctx->nvencoder, tmpoutsurf->
output_surface);
2310 if (nv_status != NV_ENC_SUCCESS) {
2311 res =
nvenc_print_error(avctx, nv_status,
"Failed unlocking bitstream buffer, expect the gates of mordor to open");
2317 ctx->registered_frames[tmpoutsurf->
reg_idx].mapped -= 1;
2318 if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped == 0) {
2319 nv_status = p_nvenc->nvEncUnmapInputResource(
ctx->nvencoder,
ctx->registered_frames[tmpoutsurf->
reg_idx].in_map.mappedResource);
2320 if (nv_status != NV_ENC_SUCCESS) {
2324 }
else if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped < 0) {
2334 switch (lock_params.pictureType) {
2335 case NV_ENC_PIC_TYPE_IDR:
2337 case NV_ENC_PIC_TYPE_I:
2340 case NV_ENC_PIC_TYPE_P:
2343 case NV_ENC_PIC_TYPE_B:
2346 case NV_ENC_PIC_TYPE_BI:
2350 av_log(avctx,
AV_LOG_ERROR,
"Unknown picture type encountered, expect the output to be broken.\n");
2351 av_log(avctx,
AV_LOG_ERROR,
"Please report this error and include as much information on how to reproduce it as possible.\n");
2375 int nb_ready, nb_pending;
2380 return nb_ready > 0;
2381 return (nb_ready > 0) && (nb_ready + nb_pending >=
ctx->async_depth);
2391 void *a53_data =
NULL;
2392 size_t a53_size = 0;
2400 &
ctx->sei_data_size,
2401 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2408 ctx->sei_data[sei_count].payloadSize = (uint32_t)a53_size;
2409 ctx->sei_data[sei_count].payload = (uint8_t*)a53_data;
2411 #if CONFIG_AV1_NVENC_ENCODER
2424 void *tc_data =
NULL;
2433 &
ctx->sei_data_size,
2434 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2441 ctx->sei_data[sei_count].payloadSize = (uint32_t)tc_size;
2442 ctx->sei_data[sei_count].payload = (uint8_t*)tc_data;
2444 #if CONFIG_AV1_NVENC_ENCODER
2459 for (
i = 0;
i <
frame->nb_side_data;
i++) {
2467 &
ctx->sei_data_size,
2468 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2474 ctx->sei_data[sei_count].payloadSize = side_data->
size;
2478 if (!
ctx->sei_data[sei_count].payload) {
2490 for (
i = 0;
i < sei_count;
i++)
2499 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
2502 NV_ENC_RECONFIGURE_PARAMS params = { 0 };
2503 int needs_reconfig = 0;
2504 int needs_encode_config = 0;
2505 int reconfig_bitrate = 0, reconfig_dar = 0;
2508 params.version = NV_ENC_RECONFIGURE_PARAMS_VER;
2509 params.reInitEncodeParams =
ctx->init_encode_params;
2512 if (dw !=
ctx->init_encode_params.darWidth || dh !=
ctx->init_encode_params.darHeight) {
2514 "aspect ratio change (DAR): %d:%d -> %d:%d\n",
2515 ctx->init_encode_params.darWidth,
2516 ctx->init_encode_params.darHeight, dw, dh);
2518 params.reInitEncodeParams.darHeight = dh;
2519 params.reInitEncodeParams.darWidth = dw;
2525 if (
ctx->rc != NV_ENC_PARAMS_RC_CONSTQP &&
ctx->support_dyn_bitrate) {
2526 if (avctx->
bit_rate > 0 && params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate != avctx->
bit_rate) {
2528 "avg bitrate change: %d -> %d\n",
2529 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate,
2532 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate = avctx->
bit_rate;
2533 reconfig_bitrate = 1;
2538 "max bitrate change: %d -> %d\n",
2539 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate,
2542 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate = avctx->
rc_max_rate;
2543 reconfig_bitrate = 1;
2548 "vbv buffer size change: %d -> %d\n",
2549 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize,
2552 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize = avctx->
rc_buffer_size;
2553 reconfig_bitrate = 1;
2556 if (reconfig_bitrate) {
2557 params.resetEncoder = 1;
2558 params.forceIDR = 1;
2560 needs_encode_config = 1;
2565 if (!needs_encode_config)
2566 params.reInitEncodeParams.encodeConfig =
NULL;
2568 if (needs_reconfig) {
2569 ret = p_nvenc->nvEncReconfigureEncoder(
ctx->nvencoder, ¶ms);
2570 if (
ret != NV_ENC_SUCCESS) {
2574 ctx->init_encode_params.darHeight = dh;
2575 ctx->init_encode_params.darWidth = dw;
2578 if (reconfig_bitrate) {
2579 ctx->encode_config.rcParams.averageBitRate = params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate;
2580 ctx->encode_config.rcParams.maxBitRate = params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate;
2581 ctx->encode_config.rcParams.vbvBufferSize = params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize;
2590 NVENCSTATUS nv_status;
2600 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2602 NV_ENC_PIC_PARAMS pic_params = { 0 };
2603 pic_params.version = NV_ENC_PIC_PARAMS_VER;
2605 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
2629 pic_params.bufferFmt = in_surf->
format;
2630 pic_params.inputWidth = in_surf->
width;
2631 pic_params.inputHeight = in_surf->
height;
2632 pic_params.inputPitch = in_surf->
pitch;
2636 if (
frame->top_field_first)
2637 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
2639 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
2641 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
2645 pic_params.encodePicFlags =
2646 ctx->forced_idr ? NV_ENC_PIC_FLAG_FORCEIDR : NV_ENC_PIC_FLAG_FORCEINTRA;
2648 pic_params.encodePicFlags = 0;
2651 pic_params.inputTimeStamp =
frame->pts;
2653 if (
ctx->extra_sei) {
2662 pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
2675 goto opaque_ref_fail;
2677 nv_status = p_nvenc->nvEncEncodePicture(
ctx->nvencoder, &pic_params);
2679 for (
i = 0;
i < sei_count;
i++)
2684 goto opaque_ref_fail;
2686 if (nv_status != NV_ENC_SUCCESS &&
2687 nv_status != NV_ENC_ERR_NEED_MORE_INPUT) {
2689 goto opaque_ref_fail;
2698 if (nv_status == NV_ENC_SUCCESS) {
2699 while (
av_fifo_read(
ctx->output_surface_queue, &tmp_out_surf, 1) >= 0)
2719 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
2722 if (!
frame->buf[0]) {