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
544 if (
ctx->b_ref_mode == NV_ENC_BFRAME_REF_MODE_EACH &&
ret != 1 &&
ret != 3) {
547 }
else if (
ctx->b_ref_mode != NV_ENC_BFRAME_REF_MODE_DISABLED &&
ret == 0) {
552 if (
ctx->b_ref_mode != 0) {
558 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
560 if(avctx->
refs != NV_ENC_NUM_REF_FRAMES_AUTOSELECT &&
ret <= 0) {
565 if(avctx->
refs != 0) {
571 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
573 if(
ctx->single_slice_intra_refresh &&
ret <= 0) {
578 if(
ctx->single_slice_intra_refresh) {
585 if((
ctx->intra_refresh ||
ctx->single_slice_intra_refresh) &&
ret <= 0) {
590 #ifndef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
598 if(
ctx->constrained_encoding &&
ret <= 0) {
603 ctx->support_dyn_bitrate =
nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE);
612 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
613 char name[128] = { 0};
633 av_log(avctx, loglevel,
"[ GPU #%d - < %s > has Compute SM %d.%d ]\n", idx,
name,
major,
minor);
635 av_log(avctx, loglevel,
"does not support NVENC\n");
646 ctx->cu_context =
ctx->cu_context_internal;
658 av_log(avctx, loglevel,
"supports NVENC\n");
669 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
677 ctx->cu_context_internal =
NULL;
690 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_H264_GUID;
693 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_HEVC_GUID;
695 #if CONFIG_AV1_NVENC_ENCODER
697 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_AV1_GUID;
707 av_log(avctx,
AV_LOG_WARNING,
"The selected preset is deprecated. Use p1 to p7 + -tune or fast/medium/slow.\n");
731 cuda_device_hwctx = hwdev_ctx->
hwctx;
734 d3d11_device_hwctx = hwdev_ctx->
hwctx;
742 if (cuda_device_hwctx) {
744 ctx->cu_stream = cuda_device_hwctx->
stream;
747 else if (d3d11_device_hwctx) {
748 ctx->d3d11_device = d3d11_device_hwctx->
device;
749 ID3D11Device_AddRef(
ctx->d3d11_device);
759 av_log(avctx,
AV_LOG_FATAL,
"Provided device doesn't support required NVENC features\n");
763 int i, nb_devices = 0;
779 for (
i = 0;
i < nb_devices; ++
i) {
792 av_log(avctx,
AV_LOG_FATAL,
"Requested GPU %d, but only %d GPUs are available!\n",
ctx->device, nb_devices);
802 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
803 #if CONFIG_AV1_NVENC_ENCODER
809 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
811 if (
ctx->init_qp_p >= 0) {
812 rc->constQP.qpInterP =
ctx->init_qp_p;
813 if (
ctx->init_qp_i >= 0 &&
ctx->init_qp_b >= 0) {
814 rc->constQP.qpIntra =
ctx->init_qp_i;
815 rc->constQP.qpInterB =
ctx->init_qp_b;
819 rc->constQP.qpInterB =
av_clip(
822 rc->constQP.qpIntra = rc->constQP.qpInterP;
823 rc->constQP.qpInterB = rc->constQP.qpInterP;
825 }
else if (
ctx->cqp >= 0) {
826 rc->constQP.qpInterP = rc->constQP.qpInterB = rc->constQP.qpIntra =
ctx->cqp;
840 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
842 #if CONFIG_AV1_NVENC_ENCODER
848 if (avctx->
qmin >= 0 && avctx->
qmax >= 0) {
852 rc->minQP.qpInterB = avctx->
qmin;
853 rc->minQP.qpInterP = avctx->
qmin;
854 rc->minQP.qpIntra = avctx->
qmin;
856 rc->maxQP.qpInterB = avctx->
qmax;
857 rc->maxQP.qpInterP = avctx->
qmax;
858 rc->maxQP.qpIntra = avctx->
qmax;
860 qp_inter_p = (avctx->
qmax + 3 * avctx->
qmin) / 4;
861 }
else if (avctx->
qmin >= 0) {
864 rc->minQP.qpInterB = avctx->
qmin;
865 rc->minQP.qpInterP = avctx->
qmin;
866 rc->minQP.qpIntra = avctx->
qmin;
868 qp_inter_p = avctx->
qmin;
873 rc->enableInitialRCQP = 1;
875 if (
ctx->init_qp_p < 0) {
876 rc->initialRCQP.qpInterP = qp_inter_p;
878 rc->initialRCQP.qpInterP =
ctx->init_qp_p;
881 if (
ctx->init_qp_i < 0) {
883 rc->initialRCQP.qpIntra =
av_clip(
886 rc->initialRCQP.qpIntra = rc->initialRCQP.qpInterP;
889 rc->initialRCQP.qpIntra =
ctx->init_qp_i;
892 if (
ctx->init_qp_b < 0) {
894 rc->initialRCQP.qpInterB =
av_clip(
897 rc->initialRCQP.qpInterB = rc->initialRCQP.qpInterP;
900 rc->initialRCQP.qpInterB =
ctx->init_qp_b;
907 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
909 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
910 rc->constQP.qpInterB = 0;
911 rc->constQP.qpInterP = 0;
912 rc->constQP.qpIntra = 0;
921 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
924 case NV_ENC_PARAMS_RC_CONSTQP:
927 case NV_ENC_PARAMS_RC_VBR_MINQP:
928 if (avctx->
qmin < 0) {
930 "The variable bitrate rate-control requires "
931 "the 'qmin' option set.\n");
936 case NV_ENC_PARAMS_RC_VBR_HQ:
937 case NV_ENC_PARAMS_RC_VBR:
940 case NV_ENC_PARAMS_RC_CBR:
941 case NV_ENC_PARAMS_RC_CBR_HQ:
942 case NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ:
946 rc->rateControlMode =
ctx->rc;
955 int nb_surfaces =
FFMAX(4,
ctx->encode_config.frameIntervalP * 2 * 2);
958 if (
ctx->rc_lookahead > 0) {
961 nb_surfaces =
FFMAX(1,
FFMAX(nb_surfaces,
ctx->rc_lookahead +
ctx->encode_config.frameIntervalP + 1 + 4));
962 if (nb_surfaces >
ctx->nb_surfaces &&
ctx->nb_surfaces > 0)
965 "Defined rc_lookahead requires more surfaces, "
966 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
968 ctx->nb_surfaces =
FFMAX(nb_surfaces,
ctx->nb_surfaces);
970 if (
ctx->encode_config.frameIntervalP > 1 &&
ctx->nb_surfaces < nb_surfaces &&
ctx->nb_surfaces > 0)
973 "Defined b-frame requires more surfaces, "
974 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
975 ctx->nb_surfaces =
FFMAX(
ctx->nb_surfaces, nb_surfaces);
977 else if (
ctx->nb_surfaces <= 0)
978 ctx->nb_surfaces = nb_surfaces;
993 av_log(avctx,
AV_LOG_WARNING,
"Using global_quality with nvenc is deprecated. Use qp instead.\n");
999 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate;
1000 }
else if (
ctx->encode_config.rcParams.averageBitRate > 0) {
1001 ctx->encode_config.rcParams.maxBitRate =
ctx->encode_config.rcParams.averageBitRate;
1007 #ifdef NVENC_HAVE_MULTIPASS
1008 ctx->encode_config.rcParams.multiPass =
ctx->multipass;
1011 ctx->encode_config.rcParams.multiPass = NV_ENC_MULTI_PASS_DISABLED;
1013 ctx->encode_config.rcParams.multiPass = NV_ENC_TWO_PASS_FULL_RESOLUTION;
1017 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->quality >= 0.0f) {
1021 ctx->rc = NV_ENC_PARAMS_RC_VBR;
1031 if (
ctx->twopass < 0)
1036 ctx->rc = NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ;
1038 ctx->rc = NV_ENC_PARAMS_RC_CBR;
1040 }
else if (
ctx->cqp >= 0) {
1041 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
1042 }
else if (
ctx->twopass) {
1043 ctx->rc = NV_ENC_PARAMS_RC_VBR_HQ;
1044 }
else if (avctx->
qmin >= 0 && avctx->
qmax >= 0) {
1045 ctx->rc = NV_ENC_PARAMS_RC_VBR_MINQP;
1057 #ifdef NVENC_HAVE_QP_CHROMA_OFFSETS
1058 ctx->encode_config.rcParams.cbQPIndexOffset =
ctx->qp_cb_offset;
1059 ctx->encode_config.rcParams.crQPIndexOffset =
ctx->qp_cr_offset;
1061 if (
ctx->qp_cb_offset ||
ctx->qp_cr_offset)
1062 av_log(avctx,
AV_LOG_WARNING,
"Failed setting QP CB/CR offsets, SDK 11.1 or greater required at compile time.\n");
1065 #ifdef NVENC_HAVE_LDKFS
1067 ctx->encode_config.rcParams.lowDelayKeyFrameScale =
ctx->ldkfs;
1072 }
else if (
ctx->rc >= 0) {
1075 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
1081 }
else if (
ctx->encode_config.rcParams.averageBitRate > 0) {
1082 avctx->
rc_buffer_size =
ctx->encode_config.rcParams.vbvBufferSize = 2 *
ctx->encode_config.rcParams.averageBitRate;
1086 ctx->encode_config.rcParams.enableAQ = 1;
1087 ctx->encode_config.rcParams.aqStrength =
ctx->aq_strength;
1091 if (
ctx->temporal_aq) {
1092 ctx->encode_config.rcParams.enableTemporalAQ = 1;
1096 if (
ctx->rc_lookahead > 0) {
1097 int lkd_bound =
FFMIN(
ctx->nb_surfaces,
ctx->async_depth) -
1098 ctx->encode_config.frameIntervalP - 4;
1100 if (lkd_bound < 0) {
1101 ctx->encode_config.rcParams.enableLookahead = 0;
1103 "Lookahead not enabled. Increase buffer delay (-delay).\n");
1105 ctx->encode_config.rcParams.enableLookahead = 1;
1106 ctx->encode_config.rcParams.lookaheadDepth =
av_clip(
ctx->rc_lookahead, 0, lkd_bound);
1107 ctx->encode_config.rcParams.disableIadapt =
ctx->no_scenecut;
1108 ctx->encode_config.rcParams.disableBadapt = !
ctx->b_adapt;
1110 "Lookahead enabled: depth %d, scenecut %s, B-adapt %s.\n",
1111 ctx->encode_config.rcParams.lookaheadDepth,
1112 ctx->encode_config.rcParams.disableIadapt ?
"disabled" :
"enabled",
1113 ctx->encode_config.rcParams.disableBadapt ?
"disabled" :
"enabled");
1114 if (
ctx->encode_config.rcParams.lookaheadDepth <
ctx->rc_lookahead)
1115 av_log(avctx,
AV_LOG_WARNING,
"Clipping lookahead depth to %d (from %d) due to lack of surfaces/delay",
1116 ctx->encode_config.rcParams.lookaheadDepth,
ctx->rc_lookahead);
1120 if (
ctx->strict_gop) {
1121 ctx->encode_config.rcParams.strictGOPTarget = 1;
1126 ctx->encode_config.rcParams.enableNonRefP = 1;
1128 if (
ctx->zerolatency)
1129 ctx->encode_config.rcParams.zeroReorderDelay = 1;
1133 int tmp_quality = (
int)(
ctx->quality * 256.0f);
1134 ctx->encode_config.rcParams.targetQuality = (uint8_t)(tmp_quality >> 8);
1135 ctx->encode_config.rcParams.targetQualityLSB = (uint8_t)(tmp_quality & 0xff);
1140 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate = 0;
1149 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1150 NV_ENC_CONFIG_H264 *h264 = &cc->encodeCodecConfig.h264Config;
1151 NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui = &h264->h264VUIParameters;
1158 vui->transferCharacteristics = avctx->
color_trc;
1159 vui->videoFullRangeFlag = 0;
1163 vui->transferCharacteristics = avctx->
color_trc;
1168 vui->colourDescriptionPresentFlag =
1169 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1171 vui->videoSignalTypePresentFlag =
1172 (vui->colourDescriptionPresentFlag
1173 || vui->videoFormat != 5
1174 || vui->videoFullRangeFlag != 0);
1176 h264->sliceMode = 3;
1177 h264->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1179 if (
ctx->intra_refresh) {
1180 h264->enableIntraRefresh = 1;
1181 h264->intraRefreshPeriod = avctx->
gop_size;
1182 h264->intraRefreshCnt = avctx->
gop_size - 1;
1183 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1184 h264->singleSliceIntraRefresh =
ctx->single_slice_intra_refresh;
1188 if (
ctx->constrained_encoding)
1189 h264->enableConstrainedEncoding = 1;
1193 h264->outputAUD =
ctx->aud;
1195 if (
ctx->dpb_size >= 0) {
1197 h264->maxNumRefFrames =
ctx->dpb_size;
1200 if (
ctx->intra_refresh) {
1201 h264->idrPeriod = NVENC_INFINITE_GOPLENGTH;
1206 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1207 h264->outputBufferingPeriodSEI = 1;
1210 h264->outputPictureTimingSEI = 1;
1212 if (cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ ||
1213 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_HQ ||
1214 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_VBR_HQ) {
1215 h264->adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
1216 h264->fmoMode = NV_ENC_H264_FMO_DISABLE;
1220 h264->qpPrimeYZeroTransformBypassFlag = 1;
1222 switch(
ctx->profile) {
1224 cc->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
1228 cc->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
1232 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
1236 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1244 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1250 h264->level =
ctx->level;
1252 if (
ctx->coder >= 0)
1253 h264->entropyCodingMode =
ctx->coder;
1255 #ifdef NVENC_HAVE_BFRAME_REF_MODE
1256 if (
ctx->b_ref_mode >= 0)
1257 h264->useBFramesAsRef =
ctx->b_ref_mode;
1260 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1261 h264->numRefL0 = avctx->
refs;
1262 h264->numRefL1 = avctx->
refs;
1271 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1272 NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig;
1273 NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui = &hevc->hevcVUIParameters;
1280 vui->transferCharacteristics = avctx->
color_trc;
1281 vui->videoFullRangeFlag = 0;
1285 vui->transferCharacteristics = avctx->
color_trc;
1290 vui->colourDescriptionPresentFlag =
1291 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1293 vui->videoSignalTypePresentFlag =
1294 (vui->colourDescriptionPresentFlag
1295 || vui->videoFormat != 5
1296 || vui->videoFullRangeFlag != 0);
1298 hevc->sliceMode = 3;
1299 hevc->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1301 if (
ctx->intra_refresh) {
1302 hevc->enableIntraRefresh = 1;
1303 hevc->intraRefreshPeriod = avctx->
gop_size;
1304 hevc->intraRefreshCnt = avctx->
gop_size - 1;
1305 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1306 hevc->singleSliceIntraRefresh =
ctx->single_slice_intra_refresh;
1310 #ifdef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
1311 if (
ctx->constrained_encoding)
1312 hevc->enableConstrainedEncoding = 1;
1317 hevc->outputAUD =
ctx->aud;
1319 if (
ctx->dpb_size >= 0) {
1321 hevc->maxNumRefFramesInDPB =
ctx->dpb_size;
1324 if (
ctx->intra_refresh) {
1325 hevc->idrPeriod = NVENC_INFINITE_GOPLENGTH;
1330 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1331 hevc->outputBufferingPeriodSEI = 1;
1334 hevc->outputPictureTimingSEI = 1;
1336 switch (
ctx->profile) {
1338 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
1342 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1346 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1353 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1359 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1363 hevc->chromaFormatIDC =
IS_YUV444(
ctx->data_pix_fmt) ? 3 : 1;
1365 hevc->pixelBitDepthMinus8 =
IS_10BIT(
ctx->data_pix_fmt) ? 2 : 0;
1367 hevc->level =
ctx->level;
1369 hevc->tier =
ctx->tier;
1371 #ifdef NVENC_HAVE_HEVC_BFRAME_REF_MODE
1372 if (
ctx->b_ref_mode >= 0)
1373 hevc->useBFramesAsRef =
ctx->b_ref_mode;
1376 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1377 hevc->numRefL0 = avctx->
refs;
1378 hevc->numRefL1 = avctx->
refs;
1384 #if CONFIG_AV1_NVENC_ENCODER
1388 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1389 NV_ENC_CONFIG_AV1 *av1 = &cc->encodeCodecConfig.av1Config;
1396 av1->transferCharacteristics = avctx->
color_trc;
1397 av1->colorRange = 0;
1401 av1->transferCharacteristics = avctx->
color_trc;
1407 av_log(avctx,
AV_LOG_ERROR,
"AV1 High Profile not supported, required for 4:4:4 encoding\n");
1410 cc->profileGUID = NV_ENC_AV1_PROFILE_MAIN_GUID;
1414 if (
ctx->dpb_size >= 0) {
1416 av1->maxNumRefFramesInDPB =
ctx->dpb_size;
1419 if (
ctx->intra_refresh) {
1420 av1->enableIntraRefresh = 1;
1421 av1->intraRefreshPeriod = avctx->
gop_size;
1422 av1->intraRefreshCnt = avctx->
gop_size - 1;
1424 av1->idrPeriod = NVENC_INFINITE_GOPLENGTH;
1429 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1430 av1->enableBitstreamPadding = 1;
1433 if (
ctx->tile_cols >= 0)
1434 av1->numTileColumns =
ctx->tile_cols;
1435 if (
ctx->tile_rows >= 0)
1436 av1->numTileRows =
ctx->tile_rows;
1438 av1->outputAnnexBFormat = 0;
1440 av1->level =
ctx->level;
1441 av1->tier =
ctx->tier;
1443 av1->enableTimingInfo =
ctx->timing_info;
1446 av1->disableSeqHdr = 0;
1447 av1->repeatSeqHdr = 1;
1449 av1->chromaFormatIDC =
IS_YUV444(
ctx->data_pix_fmt) ? 3 : 1;
1451 av1->inputPixelBitDepthMinus8 =
IS_10BIT(
ctx->data_pix_fmt) ? 2 : 0;
1452 av1->pixelBitDepthMinus8 = (
IS_10BIT(
ctx->data_pix_fmt) ||
ctx->highbitdepth) ? 2 : 0;
1454 if (
ctx->b_ref_mode >= 0)
1455 av1->useBFramesAsRef =
ctx->b_ref_mode;
1457 av1->numFwdRefs = avctx->
refs;
1458 av1->numBwdRefs = avctx->
refs;
1471 #if CONFIG_AV1_NVENC_ENCODER
1473 return nvenc_setup_av1_config(avctx);
1487 #if CONFIG_AV1_NVENC_ENCODER
1518 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1520 NV_ENC_PRESET_CONFIG preset_config = { 0 };
1521 NVENCSTATUS nv_status = NV_ENC_SUCCESS;
1526 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1527 ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
1529 ctx->init_encode_params.encodeHeight = avctx->
height;
1530 ctx->init_encode_params.encodeWidth = avctx->
width;
1532 ctx->init_encode_params.encodeConfig = &
ctx->encode_config;
1534 preset_config.version = NV_ENC_PRESET_CONFIG_VER;
1535 preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
1537 #ifdef NVENC_HAVE_NEW_PRESETS
1538 ctx->init_encode_params.tuningInfo =
ctx->tuning_info;
1541 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOSSLESS;
1543 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOW_LATENCY;
1545 nv_status = p_nvenc->nvEncGetEncodePresetConfigEx(
ctx->nvencoder,
1546 ctx->init_encode_params.encodeGUID,
1547 ctx->init_encode_params.presetGUID,
1548 ctx->init_encode_params.tuningInfo,
1551 nv_status = p_nvenc->nvEncGetEncodePresetConfig(
ctx->nvencoder,
1552 ctx->init_encode_params.encodeGUID,
1553 ctx->init_encode_params.presetGUID,
1556 if (nv_status != NV_ENC_SUCCESS)
1557 return nvenc_print_error(avctx, nv_status,
"Cannot get the preset configuration");
1559 memcpy(&
ctx->encode_config, &preset_config.presetCfg,
sizeof(
ctx->encode_config));
1561 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1564 ctx->init_encode_params.darHeight = dh;
1565 ctx->init_encode_params.darWidth = dw;
1575 ctx->init_encode_params.enableEncodeAsync = 0;
1576 ctx->init_encode_params.enablePTD = 1;
1578 #ifdef NVENC_HAVE_NEW_PRESETS
1582 if (
ctx->rc_lookahead == 0 &&
ctx->encode_config.rcParams.enableLookahead)
1583 ctx->rc_lookahead =
ctx->encode_config.rcParams.lookaheadDepth;
1586 if (
ctx->weighted_pred == 1)
1587 ctx->init_encode_params.enableWeightedPrediction = 1;
1589 if (
ctx->bluray_compat) {
1598 ctx->level = NV_ENC_LEVEL_HEVC_51;
1599 ctx->tier = NV_ENC_TIER_HEVC_HIGH;
1612 ctx->encode_config.frameIntervalP = 0;
1613 ctx->encode_config.gopLength = 1;
1617 if(
ctx->single_slice_intra_refresh)
1618 ctx->intra_refresh = 1;
1620 if (
ctx->intra_refresh)
1621 ctx->encode_config.gopLength = NVENC_INFINITE_GOPLENGTH;
1628 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
1630 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
1641 nv_status = p_nvenc->nvEncInitializeEncoder(
ctx->nvencoder, &
ctx->init_encode_params);
1642 if (nv_status != NV_ENC_SUCCESS) {
1647 #ifdef NVENC_HAVE_CUSTREAM_PTR
1648 if (
ctx->cu_context) {
1649 nv_status = p_nvenc->nvEncSetIOCudaStreams(
ctx->nvencoder, &
ctx->cu_stream, &
ctx->cu_stream);
1650 if (nv_status != NV_ENC_SUCCESS) {
1661 if (
ctx->encode_config.frameIntervalP > 1)
1664 if (
ctx->encode_config.rcParams.averageBitRate > 0)
1665 avctx->
bit_rate =
ctx->encode_config.rcParams.averageBitRate;
1672 cpb_props->
buffer_size =
ctx->encode_config.rcParams.vbvBufferSize;
1681 return NV_ENC_BUFFER_FORMAT_YV12_PL;
1683 return NV_ENC_BUFFER_FORMAT_NV12_PL;
1686 return NV_ENC_BUFFER_FORMAT_YUV420_10BIT;
1689 return NV_ENC_BUFFER_FORMAT_YUV444_PL;
1692 return NV_ENC_BUFFER_FORMAT_YUV444_10BIT;
1695 return NV_ENC_BUFFER_FORMAT_ARGB;
1698 return NV_ENC_BUFFER_FORMAT_ABGR;
1700 return NV_ENC_BUFFER_FORMAT_ARGB10;
1702 return NV_ENC_BUFFER_FORMAT_ABGR10;
1704 return NV_ENC_BUFFER_FORMAT_UNDEFINED;
1712 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1715 NVENCSTATUS nv_status;
1716 NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
1717 allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
1721 if (!
ctx->surfaces[idx].in_ref)
1724 NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
1727 if (
ctx->surfaces[idx].format == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
1733 allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
1734 allocSurf.width = avctx->
width;
1735 allocSurf.height = avctx->
height;
1736 allocSurf.bufferFmt =
ctx->surfaces[idx].format;
1738 nv_status = p_nvenc->nvEncCreateInputBuffer(
ctx->nvencoder, &allocSurf);
1739 if (nv_status != NV_ENC_SUCCESS) {
1743 ctx->surfaces[idx].input_surface = allocSurf.inputBuffer;
1744 ctx->surfaces[idx].width = allocSurf.width;
1745 ctx->surfaces[idx].height = allocSurf.height;
1748 nv_status = p_nvenc->nvEncCreateBitstreamBuffer(
ctx->nvencoder, &allocOut);
1749 if (nv_status != NV_ENC_SUCCESS) {
1752 p_nvenc->nvEncDestroyInputBuffer(
ctx->nvencoder,
ctx->surfaces[idx].input_surface);
1757 ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer;
1767 int i, res = 0, res2;
1774 if (!
ctx->reorder_queue)
1778 if (!
ctx->unused_surface_queue)
1782 if (!
ctx->output_surface_queue)
1785 if (!
ctx->output_surface_ready_queue)
1792 for (
i = 0;
i <
ctx->nb_surfaces;
i++) {
1809 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1811 NVENCSTATUS nv_status;
1812 uint32_t outSize = 0;
1813 char tmpHeader[NV_MAX_SEQ_HDR_LEN];
1815 NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
1816 payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
1818 payload.spsppsBuffer = tmpHeader;
1819 payload.inBufferSize =
sizeof(tmpHeader);
1820 payload.outSPSPPSPayloadSize = &outSize;
1822 nv_status = p_nvenc->nvEncGetSequenceParams(
ctx->nvencoder, &payload);
1823 if (nv_status != NV_ENC_SUCCESS) {
1834 memcpy(avctx->
extradata, tmpHeader, outSize);
1843 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1847 if (
ctx->nvencoder) {
1848 NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER,
1849 .encodePicFlags = NV_ENC_PIC_FLAG_EOS };
1855 p_nvenc->nvEncEncodePicture(
ctx->nvencoder, ¶ms);
1858 if (
ctx->reorder_queue) {
1868 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
1869 if (
ctx->registered_frames[
i].mapped)
1870 p_nvenc->nvEncUnmapInputResource(
ctx->nvencoder,
ctx->registered_frames[
i].in_map.mappedResource);
1871 if (
ctx->registered_frames[
i].regptr)
1872 p_nvenc->nvEncUnregisterResource(
ctx->nvencoder,
ctx->registered_frames[
i].regptr);
1874 ctx->nb_registered_frames = 0;
1877 if (
ctx->surfaces) {
1878 for (
i = 0;
i <
ctx->nb_surfaces; ++
i) {
1880 p_nvenc->nvEncDestroyInputBuffer(
ctx->nvencoder,
ctx->surfaces[
i].input_surface);
1882 p_nvenc->nvEncDestroyBitstreamBuffer(
ctx->nvencoder,
ctx->surfaces[
i].output_surface);
1886 ctx->nb_surfaces = 0;
1892 if (
ctx->nvencoder) {
1893 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
1901 if (
ctx->cu_context_internal)
1903 ctx->cu_context =
ctx->cu_context_internal =
NULL;
1906 if (
ctx->d3d11_device) {
1907 ID3D11Device_Release(
ctx->d3d11_device);
1912 nvenc_free_functions(&dl_fn->
nvenc_dl);
1913 cuda_free_functions(&dl_fn->
cuda_dl);
1931 "hw_frames_ctx must be set when using GPU frames as input\n");
1937 "hw_frames_ctx must match the GPU frame type\n");
1981 NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params,
const AVFrame *
frame)
1983 int dst_linesize[4] = {
1984 lock_buffer_params->pitch,
1985 lock_buffer_params->pitch,
1986 lock_buffer_params->pitch,
1987 lock_buffer_params->pitch
1989 uint8_t *dst_data[4];
1993 dst_linesize[1] = dst_linesize[2] >>= 1;
1996 lock_buffer_params->bufferDataPtr, dst_linesize);
2001 FFSWAP(uint8_t*, dst_data[1], dst_data[2]);
2014 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2015 NVENCSTATUS nv_status;
2020 for (first_round = 1; first_round >= 0; first_round--) {
2021 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
2022 if (!
ctx->registered_frames[
i].mapped) {
2023 if (
ctx->registered_frames[
i].regptr) {
2026 nv_status = p_nvenc->nvEncUnregisterResource(
ctx->nvencoder,
ctx->registered_frames[
i].regptr);
2027 if (nv_status != NV_ENC_SUCCESS)
2028 return nvenc_print_error(avctx, nv_status,
"Failed unregistering unused input resource");
2029 ctx->registered_frames[
i].ptr =
NULL;
2030 ctx->registered_frames[
i].regptr =
NULL;
2037 return ctx->nb_registered_frames++;
2048 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2051 NV_ENC_REGISTER_RESOURCE reg = { 0 };
2054 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
2065 reg.version = NV_ENC_REGISTER_RESOURCE_VER;
2066 reg.width = frames_ctx->width;
2067 reg.height = frames_ctx->height;
2068 reg.pitch =
frame->linesize[0];
2069 reg.resourceToRegister =
frame->data[0];
2072 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR;
2075 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX;
2076 reg.subResourceIndex = (intptr_t)
frame->data[1];
2080 if (reg.bufferFormat == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
2086 ret = p_nvenc->nvEncRegisterResource(
ctx->nvencoder, ®);
2087 if (
ret != NV_ENC_SUCCESS) {
2092 ctx->registered_frames[idx].ptr =
frame->data[0];
2093 ctx->registered_frames[idx].ptr_index = reg.subResourceIndex;
2094 ctx->registered_frames[idx].regptr = reg.registeredResource;
2103 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2106 NVENCSTATUS nv_status;
2119 if (!
ctx->registered_frames[reg_idx].mapped) {
2120 ctx->registered_frames[reg_idx].in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER;
2121 ctx->registered_frames[reg_idx].in_map.registeredResource =
ctx->registered_frames[reg_idx].regptr;
2122 nv_status = p_nvenc->nvEncMapInputResource(
ctx->nvencoder, &
ctx->registered_frames[reg_idx].in_map);
2123 if (nv_status != NV_ENC_SUCCESS) {
2129 ctx->registered_frames[reg_idx].mapped += 1;
2131 nvenc_frame->
reg_idx = reg_idx;
2132 nvenc_frame->
input_surface =
ctx->registered_frames[reg_idx].in_map.mappedResource;
2133 nvenc_frame->
format =
ctx->registered_frames[reg_idx].in_map.mappedBufferFmt;
2138 NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
2140 lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
2143 nv_status = p_nvenc->nvEncLockInputBuffer(
ctx->nvencoder, &lockBufferParams);
2144 if (nv_status != NV_ENC_SUCCESS) {
2145 return nvenc_print_error(avctx, nv_status,
"Failed locking nvenc input buffer");
2148 nvenc_frame->
pitch = lockBufferParams.pitch;
2151 nv_status = p_nvenc->nvEncUnlockInputBuffer(
ctx->nvencoder, nvenc_frame->
input_surface);
2152 if (nv_status != NV_ENC_SUCCESS) {
2161 NV_ENC_PIC_PARAMS *params,
2162 NV_ENC_SEI_PAYLOAD *sei_data,
2169 params->codecPicParams.h264PicParams.sliceMode =
2170 ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
2171 params->codecPicParams.h264PicParams.sliceModeData =
2172 ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
2173 if (sei_count > 0) {
2174 params->codecPicParams.h264PicParams.seiPayloadArray = sei_data;
2175 params->codecPicParams.h264PicParams.seiPayloadArrayCnt = sei_count;
2180 params->codecPicParams.hevcPicParams.sliceMode =
2181 ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
2182 params->codecPicParams.hevcPicParams.sliceModeData =
2183 ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
2184 if (sei_count > 0) {
2185 params->codecPicParams.hevcPicParams.seiPayloadArray = sei_data;
2186 params->codecPicParams.hevcPicParams.seiPayloadArrayCnt = sei_count;
2190 #if CONFIG_AV1_NVENC_ENCODER
2192 params->codecPicParams.av1PicParams.numTileColumns =
2193 ctx->encode_config.encodeCodecConfig.av1Config.numTileColumns;
2194 params->codecPicParams.av1PicParams.numTileRows =
2195 ctx->encode_config.encodeCodecConfig.av1Config.numTileRows;
2196 if (sei_count > 0) {
2197 params->codecPicParams.av1PicParams.obuPayloadArray = sei_data;
2198 params->codecPicParams.av1PicParams.obuPayloadArrayCnt = sei_count;
2213 #if FF_API_REORDERED_OPAQUE
2215 fd.reordered_opaque =
frame->reordered_opaque;
2236 #if FF_API_REORDERED_OPAQUE
2238 avctx->reordered_opaque = fd.reordered_opaque;
2256 NV_ENC_LOCK_BITSTREAM *params,
2262 pkt->
pts = params->outputTimeStamp;
2279 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2281 NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
2282 NVENCSTATUS nv_status;
2287 lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
2289 lock_params.doNotWait = 0;
2292 nv_status = p_nvenc->nvEncLockBitstream(
ctx->nvencoder, &lock_params);
2293 if (nv_status != NV_ENC_SUCCESS) {
2305 memcpy(
pkt->
data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
2307 nv_status = p_nvenc->nvEncUnlockBitstream(
ctx->nvencoder, tmpoutsurf->
output_surface);
2308 if (nv_status != NV_ENC_SUCCESS) {
2309 res =
nvenc_print_error(avctx, nv_status,
"Failed unlocking bitstream buffer, expect the gates of mordor to open");
2315 ctx->registered_frames[tmpoutsurf->
reg_idx].mapped -= 1;
2316 if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped == 0) {
2317 nv_status = p_nvenc->nvEncUnmapInputResource(
ctx->nvencoder,
ctx->registered_frames[tmpoutsurf->
reg_idx].in_map.mappedResource);
2318 if (nv_status != NV_ENC_SUCCESS) {
2322 }
else if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped < 0) {
2332 switch (lock_params.pictureType) {
2333 case NV_ENC_PIC_TYPE_IDR:
2335 case NV_ENC_PIC_TYPE_I:
2338 case NV_ENC_PIC_TYPE_P:
2341 case NV_ENC_PIC_TYPE_B:
2344 case NV_ENC_PIC_TYPE_BI:
2348 av_log(avctx,
AV_LOG_ERROR,
"Unknown picture type encountered, expect the output to be broken.\n");
2349 av_log(avctx,
AV_LOG_ERROR,
"Please report this error and include as much information on how to reproduce it as possible.\n");
2373 int nb_ready, nb_pending;
2378 return nb_ready > 0;
2379 return (nb_ready > 0) && (nb_ready + nb_pending >=
ctx->async_depth);
2389 void *a53_data =
NULL;
2390 size_t a53_size = 0;
2398 &
ctx->sei_data_size,
2399 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2406 ctx->sei_data[sei_count].payloadSize = (uint32_t)a53_size;
2407 ctx->sei_data[sei_count].payload = (uint8_t*)a53_data;
2409 #if CONFIG_AV1_NVENC_ENCODER
2422 void *tc_data =
NULL;
2431 &
ctx->sei_data_size,
2432 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2439 ctx->sei_data[sei_count].payloadSize = (uint32_t)tc_size;
2440 ctx->sei_data[sei_count].payload = (uint8_t*)tc_data;
2442 #if CONFIG_AV1_NVENC_ENCODER
2457 for (
i = 0;
i <
frame->nb_side_data;
i++) {
2465 &
ctx->sei_data_size,
2466 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2472 ctx->sei_data[sei_count].payloadSize = side_data->
size;
2476 if (!
ctx->sei_data[sei_count].payload) {
2488 for (
i = 0;
i < sei_count;
i++)
2497 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
2500 NV_ENC_RECONFIGURE_PARAMS params = { 0 };
2501 int needs_reconfig = 0;
2502 int needs_encode_config = 0;
2503 int reconfig_bitrate = 0, reconfig_dar = 0;
2506 params.version = NV_ENC_RECONFIGURE_PARAMS_VER;
2507 params.reInitEncodeParams =
ctx->init_encode_params;
2510 if (dw !=
ctx->init_encode_params.darWidth || dh !=
ctx->init_encode_params.darHeight) {
2512 "aspect ratio change (DAR): %d:%d -> %d:%d\n",
2513 ctx->init_encode_params.darWidth,
2514 ctx->init_encode_params.darHeight, dw, dh);
2516 params.reInitEncodeParams.darHeight = dh;
2517 params.reInitEncodeParams.darWidth = dw;
2523 if (
ctx->rc != NV_ENC_PARAMS_RC_CONSTQP &&
ctx->support_dyn_bitrate) {
2524 if (avctx->
bit_rate > 0 && params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate != avctx->
bit_rate) {
2526 "avg bitrate change: %d -> %d\n",
2527 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate,
2530 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate = avctx->
bit_rate;
2531 reconfig_bitrate = 1;
2536 "max bitrate change: %d -> %d\n",
2537 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate,
2540 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate = avctx->
rc_max_rate;
2541 reconfig_bitrate = 1;
2546 "vbv buffer size change: %d -> %d\n",
2547 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize,
2550 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize = avctx->
rc_buffer_size;
2551 reconfig_bitrate = 1;
2554 if (reconfig_bitrate) {
2555 params.resetEncoder = 1;
2556 params.forceIDR = 1;
2558 needs_encode_config = 1;
2563 if (!needs_encode_config)
2564 params.reInitEncodeParams.encodeConfig =
NULL;
2566 if (needs_reconfig) {
2567 ret = p_nvenc->nvEncReconfigureEncoder(
ctx->nvencoder, ¶ms);
2568 if (
ret != NV_ENC_SUCCESS) {
2572 ctx->init_encode_params.darHeight = dh;
2573 ctx->init_encode_params.darWidth = dw;
2576 if (reconfig_bitrate) {
2577 ctx->encode_config.rcParams.averageBitRate = params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate;
2578 ctx->encode_config.rcParams.maxBitRate = params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate;
2579 ctx->encode_config.rcParams.vbvBufferSize = params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize;
2588 NVENCSTATUS nv_status;
2598 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2600 NV_ENC_PIC_PARAMS pic_params = { 0 };
2601 pic_params.version = NV_ENC_PIC_PARAMS_VER;
2603 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
2627 pic_params.bufferFmt = in_surf->
format;
2628 pic_params.inputWidth = in_surf->
width;
2629 pic_params.inputHeight = in_surf->
height;
2630 pic_params.inputPitch = in_surf->
pitch;
2634 if (
frame->top_field_first)
2635 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
2637 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
2639 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
2643 pic_params.encodePicFlags =
2644 ctx->forced_idr ? NV_ENC_PIC_FLAG_FORCEIDR : NV_ENC_PIC_FLAG_FORCEINTRA;
2646 pic_params.encodePicFlags = 0;
2649 pic_params.inputTimeStamp =
frame->pts;
2651 if (
ctx->extra_sei) {
2660 pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
2673 goto opaque_ref_fail;
2675 nv_status = p_nvenc->nvEncEncodePicture(
ctx->nvencoder, &pic_params);
2677 for (
i = 0;
i < sei_count;
i++)
2682 goto opaque_ref_fail;
2684 if (nv_status != NV_ENC_SUCCESS &&
2685 nv_status != NV_ENC_ERR_NEED_MORE_INPUT) {
2687 goto opaque_ref_fail;
2696 if (nv_status == NV_ENC_SUCCESS) {
2697 while (
av_fifo_read(
ctx->output_surface_queue, &tmp_out_surf, 1) >= 0)
2717 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
2720 if (!
frame->buf[0]) {