38 #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, dl_fn->cuda_dl, x)
40 #define NVENC_CAP 0x30
41 #define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR || \
42 rc == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ || \
43 rc == NV_ENC_PARAMS_RC_CBR_HQ)
73 #define IS_10BIT(pix_fmt) (pix_fmt == AV_PIX_FMT_P010 || \
74 pix_fmt == AV_PIX_FMT_P016 || \
75 pix_fmt == AV_PIX_FMT_YUV444P16 || \
76 pix_fmt == AV_PIX_FMT_GBRP16)
78 #define IS_YUV444(pix_fmt) (pix_fmt == AV_PIX_FMT_YUV444P || \
79 pix_fmt == AV_PIX_FMT_YUV444P16 || \
80 pix_fmt == AV_PIX_FMT_GBRP || \
81 pix_fmt == AV_PIX_FMT_GBRP16)
83 #define IS_GBRP(pix_fmt) (pix_fmt == AV_PIX_FMT_GBRP || \
84 pix_fmt == AV_PIX_FMT_GBRP16)
91 { NV_ENC_SUCCESS, 0,
"success" },
92 { NV_ENC_ERR_NO_ENCODE_DEVICE,
AVERROR(ENOENT),
"no encode device" },
93 { NV_ENC_ERR_UNSUPPORTED_DEVICE,
AVERROR(ENOSYS),
"unsupported device" },
94 { NV_ENC_ERR_INVALID_ENCODERDEVICE,
AVERROR(EINVAL),
"invalid encoder device" },
95 { NV_ENC_ERR_INVALID_DEVICE,
AVERROR(EINVAL),
"invalid device" },
96 { NV_ENC_ERR_DEVICE_NOT_EXIST,
AVERROR(EIO),
"device does not exist" },
97 { NV_ENC_ERR_INVALID_PTR,
AVERROR(EFAULT),
"invalid ptr" },
98 { NV_ENC_ERR_INVALID_EVENT,
AVERROR(EINVAL),
"invalid event" },
99 { NV_ENC_ERR_INVALID_PARAM,
AVERROR(EINVAL),
"invalid param" },
100 { NV_ENC_ERR_INVALID_CALL,
AVERROR(EINVAL),
"invalid call" },
101 { NV_ENC_ERR_OUT_OF_MEMORY,
AVERROR(ENOMEM),
"out of memory" },
102 { NV_ENC_ERR_ENCODER_NOT_INITIALIZED,
AVERROR(EINVAL),
"encoder not initialized" },
103 { NV_ENC_ERR_UNSUPPORTED_PARAM,
AVERROR(ENOSYS),
"unsupported param" },
104 { NV_ENC_ERR_LOCK_BUSY,
AVERROR(EAGAIN),
"lock busy" },
106 { NV_ENC_ERR_INVALID_VERSION,
AVERROR(EINVAL),
"invalid version" },
107 { NV_ENC_ERR_MAP_FAILED,
AVERROR(EIO),
"map failed" },
108 { NV_ENC_ERR_NEED_MORE_INPUT,
AVERROR(EAGAIN),
"need more input" },
109 { NV_ENC_ERR_ENCODER_BUSY,
AVERROR(EAGAIN),
"encoder busy" },
110 { NV_ENC_ERR_EVENT_NOT_REGISTERD,
AVERROR(EBADF),
"event not registered" },
112 { NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY,
AVERROR(EINVAL),
"incompatible client key" },
113 { NV_ENC_ERR_UNIMPLEMENTED,
AVERROR(ENOSYS),
"unimplemented" },
114 { NV_ENC_ERR_RESOURCE_REGISTER_FAILED,
AVERROR(EIO),
"resource register failed" },
115 { NV_ENC_ERR_RESOURCE_NOT_REGISTERED,
AVERROR(EBADF),
"resource not registered" },
116 { NV_ENC_ERR_RESOURCE_NOT_MAPPED,
AVERROR(EBADF),
"resource not mapped" },
130 *
desc =
"unknown error";
135 const char *error_string)
138 const char *details =
"(no details)";
141 #ifdef NVENC_HAVE_GETLASTERRORSTRING
143 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
145 if (p_nvenc &&
ctx->nvencoder)
146 details = p_nvenc->nvEncGetLastErrorString(
ctx->nvencoder);
159 #define PRESET_ALIAS(alias, name, ...) \
160 [PRESET_ ## alias] = { NV_ENC_PRESET_ ## name ## _GUID, __VA_ARGS__ }
162 #define PRESET(name, ...) PRESET_ALIAS(name, name, __VA_ARGS__)
167 #ifdef NVENC_HAVE_NEW_PRESETS
206 ctx->init_encode_params.presetGUID = t->
guid;
209 #ifdef NVENC_HAVE_NEW_PRESETS
210 if (
ctx->tuning_info == NV_ENC_TUNING_INFO_LOSSLESS)
220 #if NVENCAPI_CHECK_VERSION(11, 2)
221 const char *minver =
"(unknown)";
222 #elif NVENCAPI_CHECK_VERSION(11, 1)
223 # if defined(_WIN32) || defined(__CYGWIN__)
224 const char *minver =
"471.41";
226 const char *minver =
"470.57.02";
228 #elif NVENCAPI_CHECK_VERSION(11, 0)
229 # if defined(_WIN32) || defined(__CYGWIN__)
230 const char *minver =
"456.71";
232 const char *minver =
"455.28";
234 #elif NVENCAPI_CHECK_VERSION(10, 0)
235 # if defined(_WIN32) || defined(__CYGWIN__)
236 const char *minver =
"450.51";
238 const char *minver =
"445.87";
240 #elif NVENCAPI_CHECK_VERSION(9, 1)
241 # if defined(_WIN32) || defined(__CYGWIN__)
242 const char *minver =
"436.15";
244 const char *minver =
"435.21";
246 #elif NVENCAPI_CHECK_VERSION(9, 0)
247 # if defined(_WIN32) || defined(__CYGWIN__)
248 const char *minver =
"418.81";
250 const char *minver =
"418.30";
252 #elif NVENCAPI_CHECK_VERSION(8, 2)
253 # if defined(_WIN32) || defined(__CYGWIN__)
254 const char *minver =
"397.93";
256 const char *minver =
"396.24";
258 #elif NVENCAPI_CHECK_VERSION(8, 1)
259 # if defined(_WIN32) || defined(__CYGWIN__)
260 const char *minver =
"390.77";
262 const char *minver =
"390.25";
265 # if defined(_WIN32) || defined(__CYGWIN__)
266 const char *minver =
"378.66";
268 const char *minver =
"378.13";
271 av_log(avctx,
level,
"The minimum required Nvidia driver for nvenc is %s or newer\n", minver);
279 uint32_t nvenc_max_ver;
282 ret = cuda_load_functions(&dl_fn->
cuda_dl, avctx);
286 ret = nvenc_load_functions(&dl_fn->
nvenc_dl, avctx);
292 err = dl_fn->
nvenc_dl->NvEncodeAPIGetMaxSupportedVersion(&nvenc_max_ver);
293 if (err != NV_ENC_SUCCESS)
298 if ((NVENCAPI_MAJOR_VERSION << 4 | NVENCAPI_MINOR_VERSION) > nvenc_max_ver) {
299 av_log(avctx,
AV_LOG_ERROR,
"Driver does not support the required nvenc API version. "
300 "Required: %d.%d Found: %d.%d\n",
301 NVENCAPI_MAJOR_VERSION, NVENCAPI_MINOR_VERSION,
302 nvenc_max_ver >> 4, nvenc_max_ver & 0
xf);
307 dl_fn->
nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
310 if (err != NV_ENC_SUCCESS)
323 if (
ctx->d3d11_device)
335 if (
ctx->d3d11_device)
343 NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = { 0 };
345 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
348 params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
349 params.apiVersion = NVENCAPI_VERSION;
350 if (
ctx->d3d11_device) {
351 params.device =
ctx->d3d11_device;
352 params.deviceType = NV_ENC_DEVICE_TYPE_DIRECTX;
354 params.device =
ctx->cu_context;
355 params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
358 ret = p_nvenc->nvEncOpenEncodeSessionEx(¶ms, &
ctx->nvencoder);
359 if (
ret != NV_ENC_SUCCESS) {
370 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
371 int i,
ret, count = 0;
374 ret = p_nvenc->nvEncGetEncodeGUIDCount(
ctx->nvencoder, &count);
376 if (
ret != NV_ENC_SUCCESS || !count)
383 ret = p_nvenc->nvEncGetEncodeGUIDs(
ctx->nvencoder, guids, count, &count);
384 if (
ret != NV_ENC_SUCCESS) {
390 for (
i = 0;
i < count;
i++) {
391 if (!memcmp(&guids[
i], &
ctx->init_encode_params.encodeGUID,
sizeof(*guids))) {
406 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
407 NV_ENC_CAPS_PARAM params = { 0 };
410 params.version = NV_ENC_CAPS_PARAM_VER;
411 params.capsToQuery = cap;
413 ret = p_nvenc->nvEncGetEncodeCaps(
ctx->nvencoder,
ctx->init_encode_params.encodeGUID, ¶ms, &
val);
415 if (
ret == NV_ENC_SUCCESS)
444 if (ret < avctx->
width) {
451 if (ret < avctx->
height) {
458 if (ret < avctx->max_b_frames) {
468 "Interlaced encoding is not supported. Supported level: %d\n",
480 if (
ctx->rc_lookahead > 0 &&
ret <= 0) {
486 if (
ctx->temporal_aq > 0 &&
ret <= 0) {
492 if (
ctx->weighted_pred > 0 &&
ret <= 0) {
498 if (
ctx->coder == NV_ENC_H264_ENTROPY_CODING_MODE_CABAC &&
ret <= 0) {
503 #ifdef NVENC_HAVE_BFRAME_REF_MODE
505 if (
ctx->b_ref_mode == NV_ENC_BFRAME_REF_MODE_EACH &&
ret != 1 &&
ret != 3) {
508 }
else if (
ctx->b_ref_mode != NV_ENC_BFRAME_REF_MODE_DISABLED &&
ret == 0) {
513 if (
ctx->b_ref_mode != 0) {
519 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
521 if(avctx->
refs != NV_ENC_NUM_REF_FRAMES_AUTOSELECT &&
ret <= 0) {
526 if(avctx->
refs != 0) {
532 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
534 if(
ctx->single_slice_intra_refresh &&
ret <= 0) {
539 if(
ctx->single_slice_intra_refresh) {
546 if((
ctx->intra_refresh ||
ctx->single_slice_intra_refresh) &&
ret <= 0) {
551 #ifndef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
559 if(
ctx->constrained_encoding &&
ret <= 0) {
564 ctx->support_dyn_bitrate =
nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE);
573 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
574 char name[128] = { 0};
594 av_log(avctx, loglevel,
"[ GPU #%d - < %s > has Compute SM %d.%d ]\n", idx,
name,
major,
minor);
596 av_log(avctx, loglevel,
"does not support NVENC\n");
607 ctx->cu_context =
ctx->cu_context_internal;
619 av_log(avctx, loglevel,
"supports NVENC\n");
630 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
638 ctx->cu_context_internal =
NULL;
651 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_H264_GUID;
654 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_HEVC_GUID;
663 av_log(avctx,
AV_LOG_WARNING,
"The selected preset is deprecated. Use p1 to p7 + -tune or fast/medium/slow.\n");
687 cuda_device_hwctx = hwdev_ctx->
hwctx;
690 d3d11_device_hwctx = hwdev_ctx->
hwctx;
698 if (cuda_device_hwctx) {
700 ctx->cu_stream = cuda_device_hwctx->
stream;
703 else if (d3d11_device_hwctx) {
704 ctx->d3d11_device = d3d11_device_hwctx->
device;
705 ID3D11Device_AddRef(
ctx->d3d11_device);
715 av_log(avctx,
AV_LOG_FATAL,
"Provided device doesn't support required NVENC features\n");
719 int i, nb_devices = 0;
735 for (
i = 0;
i < nb_devices; ++
i) {
748 av_log(avctx,
AV_LOG_FATAL,
"Requested GPU %d, but only %d GPUs are available!\n",
ctx->device, nb_devices);
758 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
760 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
762 if (
ctx->init_qp_p >= 0) {
763 rc->constQP.qpInterP =
ctx->init_qp_p;
764 if (
ctx->init_qp_i >= 0 &&
ctx->init_qp_b >= 0) {
765 rc->constQP.qpIntra =
ctx->init_qp_i;
766 rc->constQP.qpInterB =
ctx->init_qp_b;
770 rc->constQP.qpInterB =
av_clip(
773 rc->constQP.qpIntra = rc->constQP.qpInterP;
774 rc->constQP.qpInterB = rc->constQP.qpInterP;
776 }
else if (
ctx->cqp >= 0) {
777 rc->constQP.qpInterP = rc->constQP.qpInterB = rc->constQP.qpIntra =
ctx->cqp;
791 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
794 if (avctx->
qmin >= 0 && avctx->
qmax >= 0) {
798 rc->minQP.qpInterB = avctx->
qmin;
799 rc->minQP.qpInterP = avctx->
qmin;
800 rc->minQP.qpIntra = avctx->
qmin;
802 rc->maxQP.qpInterB = avctx->
qmax;
803 rc->maxQP.qpInterP = avctx->
qmax;
804 rc->maxQP.qpIntra = avctx->
qmax;
806 qp_inter_p = (avctx->
qmax + 3 * avctx->
qmin) / 4;
807 }
else if (avctx->
qmin >= 0) {
810 rc->minQP.qpInterB = avctx->
qmin;
811 rc->minQP.qpInterP = avctx->
qmin;
812 rc->minQP.qpIntra = avctx->
qmin;
814 qp_inter_p = avctx->
qmin;
819 rc->enableInitialRCQP = 1;
821 if (
ctx->init_qp_p < 0) {
822 rc->initialRCQP.qpInterP = qp_inter_p;
824 rc->initialRCQP.qpInterP =
ctx->init_qp_p;
827 if (
ctx->init_qp_i < 0) {
829 rc->initialRCQP.qpIntra =
av_clip(
832 rc->initialRCQP.qpIntra = rc->initialRCQP.qpInterP;
835 rc->initialRCQP.qpIntra =
ctx->init_qp_i;
838 if (
ctx->init_qp_b < 0) {
840 rc->initialRCQP.qpInterB =
av_clip(
843 rc->initialRCQP.qpInterB = rc->initialRCQP.qpInterP;
846 rc->initialRCQP.qpInterB =
ctx->init_qp_b;
853 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
855 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
856 rc->constQP.qpInterB = 0;
857 rc->constQP.qpInterP = 0;
858 rc->constQP.qpIntra = 0;
867 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
870 case NV_ENC_PARAMS_RC_CONSTQP:
873 case NV_ENC_PARAMS_RC_VBR_MINQP:
874 if (avctx->
qmin < 0) {
876 "The variable bitrate rate-control requires "
877 "the 'qmin' option set.\n");
882 case NV_ENC_PARAMS_RC_VBR_HQ:
883 case NV_ENC_PARAMS_RC_VBR:
886 case NV_ENC_PARAMS_RC_CBR:
887 case NV_ENC_PARAMS_RC_CBR_HQ:
888 case NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ:
892 rc->rateControlMode =
ctx->rc;
901 int nb_surfaces =
FFMAX(4,
ctx->encode_config.frameIntervalP * 2 * 2);
904 if (
ctx->rc_lookahead > 0) {
907 nb_surfaces =
FFMAX(1,
FFMAX(nb_surfaces,
ctx->rc_lookahead +
ctx->encode_config.frameIntervalP + 1 + 4));
908 if (nb_surfaces >
ctx->nb_surfaces &&
ctx->nb_surfaces > 0)
911 "Defined rc_lookahead requires more surfaces, "
912 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
914 ctx->nb_surfaces =
FFMAX(nb_surfaces,
ctx->nb_surfaces);
916 if (
ctx->encode_config.frameIntervalP > 1 &&
ctx->nb_surfaces < nb_surfaces &&
ctx->nb_surfaces > 0)
919 "Defined b-frame requires more surfaces, "
920 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
921 ctx->nb_surfaces =
FFMAX(
ctx->nb_surfaces, nb_surfaces);
923 else if (
ctx->nb_surfaces <= 0)
924 ctx->nb_surfaces = nb_surfaces;
939 av_log(avctx,
AV_LOG_WARNING,
"Using global_quality with nvenc is deprecated. Use qp instead.\n");
945 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate;
946 }
else if (
ctx->encode_config.rcParams.averageBitRate > 0) {
947 ctx->encode_config.rcParams.maxBitRate =
ctx->encode_config.rcParams.averageBitRate;
953 #ifdef NVENC_HAVE_MULTIPASS
954 ctx->encode_config.rcParams.multiPass =
ctx->multipass;
957 ctx->encode_config.rcParams.multiPass = NV_ENC_MULTI_PASS_DISABLED;
959 ctx->encode_config.rcParams.multiPass = NV_ENC_TWO_PASS_FULL_RESOLUTION;
963 ctx->rc = NV_ENC_PARAMS_RC_CBR;
964 }
else if (
ctx->cqp >= 0) {
965 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
966 }
else if (
ctx->quality >= 0.0f) {
967 ctx->rc = NV_ENC_PARAMS_RC_VBR;
977 if (
ctx->twopass < 0)
982 ctx->rc = NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ;
984 ctx->rc = NV_ENC_PARAMS_RC_CBR;
986 }
else if (
ctx->cqp >= 0) {
987 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
988 }
else if (
ctx->twopass) {
989 ctx->rc = NV_ENC_PARAMS_RC_VBR_HQ;
990 }
else if (avctx->
qmin >= 0 && avctx->
qmax >= 0) {
991 ctx->rc = NV_ENC_PARAMS_RC_VBR_MINQP;
1003 #ifdef NVENC_HAVE_QP_CHROMA_OFFSETS
1004 ctx->encode_config.rcParams.cbQPIndexOffset =
ctx->qp_cb_offset;
1005 ctx->encode_config.rcParams.crQPIndexOffset =
ctx->qp_cr_offset;
1007 if (
ctx->qp_cb_offset ||
ctx->qp_cr_offset)
1008 av_log(avctx,
AV_LOG_WARNING,
"Failed setting QP CB/CR offsets, SDK 11.1 or greater required at compile time.\n");
1011 #ifdef NVENC_HAVE_LDKFS
1013 ctx->encode_config.rcParams.lowDelayKeyFrameScale =
ctx->ldkfs;
1018 }
else if (
ctx->rc >= 0) {
1021 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
1027 }
else if (
ctx->encode_config.rcParams.averageBitRate > 0) {
1028 avctx->
rc_buffer_size =
ctx->encode_config.rcParams.vbvBufferSize = 2 *
ctx->encode_config.rcParams.averageBitRate;
1032 ctx->encode_config.rcParams.enableAQ = 1;
1033 ctx->encode_config.rcParams.aqStrength =
ctx->aq_strength;
1037 if (
ctx->temporal_aq) {
1038 ctx->encode_config.rcParams.enableTemporalAQ = 1;
1042 if (
ctx->rc_lookahead > 0) {
1043 int lkd_bound =
FFMIN(
ctx->nb_surfaces,
ctx->async_depth) -
1044 ctx->encode_config.frameIntervalP - 4;
1046 if (lkd_bound < 0) {
1048 "Lookahead not enabled. Increase buffer delay (-delay).\n");
1050 ctx->encode_config.rcParams.enableLookahead = 1;
1051 ctx->encode_config.rcParams.lookaheadDepth =
av_clip(
ctx->rc_lookahead, 0, lkd_bound);
1052 ctx->encode_config.rcParams.disableIadapt =
ctx->no_scenecut;
1053 ctx->encode_config.rcParams.disableBadapt = !
ctx->b_adapt;
1055 "Lookahead enabled: depth %d, scenecut %s, B-adapt %s.\n",
1056 ctx->encode_config.rcParams.lookaheadDepth,
1057 ctx->encode_config.rcParams.disableIadapt ?
"disabled" :
"enabled",
1058 ctx->encode_config.rcParams.disableBadapt ?
"disabled" :
"enabled");
1062 if (
ctx->strict_gop) {
1063 ctx->encode_config.rcParams.strictGOPTarget = 1;
1068 ctx->encode_config.rcParams.enableNonRefP = 1;
1070 if (
ctx->zerolatency)
1071 ctx->encode_config.rcParams.zeroReorderDelay = 1;
1075 int tmp_quality = (
int)(
ctx->quality * 256.0f);
1076 ctx->encode_config.rcParams.targetQuality = (uint8_t)(tmp_quality >> 8);
1077 ctx->encode_config.rcParams.targetQualityLSB = (uint8_t)(tmp_quality & 0xff);
1082 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate = 0;
1090 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1091 NV_ENC_CONFIG_H264 *h264 = &cc->encodeCodecConfig.h264Config;
1092 NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui = &h264->h264VUIParameters;
1096 vui->transferCharacteristics = avctx->
color_trc;
1100 vui->colourDescriptionPresentFlag =
1101 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1103 vui->videoSignalTypePresentFlag =
1104 (vui->colourDescriptionPresentFlag
1105 || vui->videoFormat != 5
1106 || vui->videoFullRangeFlag != 0);
1108 h264->sliceMode = 3;
1109 h264->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1111 if (
ctx->intra_refresh) {
1112 h264->enableIntraRefresh = 1;
1113 h264->intraRefreshPeriod = avctx->
gop_size;
1114 h264->intraRefreshCnt = avctx->
gop_size - 1;
1115 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1116 h264->singleSliceIntraRefresh =
ctx->single_slice_intra_refresh;
1120 if (
ctx->constrained_encoding)
1121 h264->enableConstrainedEncoding = 1;
1125 h264->outputAUD =
ctx->aud;
1127 if (
ctx->dpb_size >= 0) {
1129 h264->maxNumRefFrames =
ctx->dpb_size;
1132 if (
ctx->intra_refresh) {
1133 h264->idrPeriod = NVENC_INFINITE_GOPLENGTH;
1138 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1139 h264->outputBufferingPeriodSEI = 1;
1142 h264->outputPictureTimingSEI = 1;
1144 if (cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ ||
1145 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_HQ ||
1146 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_VBR_HQ) {
1147 h264->adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
1148 h264->fmoMode = NV_ENC_H264_FMO_DISABLE;
1152 h264->qpPrimeYZeroTransformBypassFlag = 1;
1154 switch(
ctx->profile) {
1156 cc->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
1160 cc->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
1164 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
1168 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1176 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1182 h264->level =
ctx->level;
1184 if (
ctx->coder >= 0)
1185 h264->entropyCodingMode =
ctx->coder;
1187 #ifdef NVENC_HAVE_BFRAME_REF_MODE
1188 h264->useBFramesAsRef =
ctx->b_ref_mode;
1191 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1192 h264->numRefL0 = avctx->
refs;
1193 h264->numRefL1 = avctx->
refs;
1202 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1203 NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig;
1204 NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui = &hevc->hevcVUIParameters;
1208 vui->transferCharacteristics = avctx->
color_trc;
1212 vui->colourDescriptionPresentFlag =
1213 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1215 vui->videoSignalTypePresentFlag =
1216 (vui->colourDescriptionPresentFlag
1217 || vui->videoFormat != 5
1218 || vui->videoFullRangeFlag != 0);
1220 hevc->sliceMode = 3;
1221 hevc->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1223 if (
ctx->intra_refresh) {
1224 hevc->enableIntraRefresh = 1;
1225 hevc->intraRefreshPeriod = avctx->
gop_size;
1226 hevc->intraRefreshCnt = avctx->
gop_size - 1;
1227 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1228 hevc->singleSliceIntraRefresh =
ctx->single_slice_intra_refresh;
1232 #ifdef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
1233 if (
ctx->constrained_encoding)
1234 hevc->enableConstrainedEncoding = 1;
1239 hevc->outputAUD =
ctx->aud;
1241 if (
ctx->dpb_size >= 0) {
1243 hevc->maxNumRefFramesInDPB =
ctx->dpb_size;
1246 if (
ctx->intra_refresh) {
1247 hevc->idrPeriod = NVENC_INFINITE_GOPLENGTH;
1252 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1253 hevc->outputBufferingPeriodSEI = 1;
1256 hevc->outputPictureTimingSEI = 1;
1258 switch (
ctx->profile) {
1260 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
1264 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1268 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1275 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1281 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1285 hevc->chromaFormatIDC =
IS_YUV444(
ctx->data_pix_fmt) ? 3 : 1;
1287 hevc->pixelBitDepthMinus8 =
IS_10BIT(
ctx->data_pix_fmt) ? 2 : 0;
1289 hevc->level =
ctx->level;
1291 hevc->tier =
ctx->tier;
1293 #ifdef NVENC_HAVE_HEVC_BFRAME_REF_MODE
1294 hevc->useBFramesAsRef =
ctx->b_ref_mode;
1297 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1298 hevc->numRefL0 = avctx->
refs;
1299 hevc->numRefL1 = avctx->
refs;
1336 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1338 NV_ENC_PRESET_CONFIG preset_config = { 0 };
1339 NVENCSTATUS nv_status = NV_ENC_SUCCESS;
1344 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1345 ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
1347 ctx->init_encode_params.encodeHeight = avctx->
height;
1348 ctx->init_encode_params.encodeWidth = avctx->
width;
1350 ctx->init_encode_params.encodeConfig = &
ctx->encode_config;
1352 preset_config.version = NV_ENC_PRESET_CONFIG_VER;
1353 preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
1355 #ifdef NVENC_HAVE_NEW_PRESETS
1356 ctx->init_encode_params.tuningInfo =
ctx->tuning_info;
1359 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOSSLESS;
1361 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOW_LATENCY;
1363 nv_status = p_nvenc->nvEncGetEncodePresetConfigEx(
ctx->nvencoder,
1364 ctx->init_encode_params.encodeGUID,
1365 ctx->init_encode_params.presetGUID,
1366 ctx->init_encode_params.tuningInfo,
1369 nv_status = p_nvenc->nvEncGetEncodePresetConfig(
ctx->nvencoder,
1370 ctx->init_encode_params.encodeGUID,
1371 ctx->init_encode_params.presetGUID,
1374 if (nv_status != NV_ENC_SUCCESS)
1375 return nvenc_print_error(avctx, nv_status,
"Cannot get the preset configuration");
1377 memcpy(&
ctx->encode_config, &preset_config.presetCfg,
sizeof(
ctx->encode_config));
1379 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1382 ctx->init_encode_params.darHeight = dh;
1383 ctx->init_encode_params.darWidth = dw;
1393 ctx->init_encode_params.enableEncodeAsync = 0;
1394 ctx->init_encode_params.enablePTD = 1;
1396 #ifdef NVENC_HAVE_NEW_PRESETS
1400 if (
ctx->rc_lookahead == 0 &&
ctx->encode_config.rcParams.enableLookahead)
1401 ctx->rc_lookahead =
ctx->encode_config.rcParams.lookaheadDepth;
1404 if (
ctx->weighted_pred == 1)
1405 ctx->init_encode_params.enableWeightedPrediction = 1;
1407 if (
ctx->bluray_compat) {
1416 ctx->level = NV_ENC_LEVEL_HEVC_51;
1417 ctx->tier = NV_ENC_TIER_HEVC_HIGH;
1430 ctx->encode_config.frameIntervalP = 0;
1431 ctx->encode_config.gopLength = 1;
1435 if(
ctx->single_slice_intra_refresh)
1436 ctx->intra_refresh = 1;
1438 if (
ctx->intra_refresh)
1439 ctx->encode_config.gopLength = NVENC_INFINITE_GOPLENGTH;
1446 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
1448 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
1459 nv_status = p_nvenc->nvEncInitializeEncoder(
ctx->nvencoder, &
ctx->init_encode_params);
1460 if (nv_status != NV_ENC_SUCCESS) {
1465 #ifdef NVENC_HAVE_CUSTREAM_PTR
1466 if (
ctx->cu_context) {
1467 nv_status = p_nvenc->nvEncSetIOCudaStreams(
ctx->nvencoder, &
ctx->cu_stream, &
ctx->cu_stream);
1468 if (nv_status != NV_ENC_SUCCESS) {
1479 if (
ctx->encode_config.frameIntervalP > 1)
1482 if (
ctx->encode_config.rcParams.averageBitRate > 0)
1483 avctx->
bit_rate =
ctx->encode_config.rcParams.averageBitRate;
1490 cpb_props->
buffer_size =
ctx->encode_config.rcParams.vbvBufferSize;
1499 return NV_ENC_BUFFER_FORMAT_YV12_PL;
1501 return NV_ENC_BUFFER_FORMAT_NV12_PL;
1504 return NV_ENC_BUFFER_FORMAT_YUV420_10BIT;
1507 return NV_ENC_BUFFER_FORMAT_YUV444_PL;
1510 return NV_ENC_BUFFER_FORMAT_YUV444_10BIT;
1512 return NV_ENC_BUFFER_FORMAT_ARGB;
1514 return NV_ENC_BUFFER_FORMAT_ABGR;
1516 return NV_ENC_BUFFER_FORMAT_UNDEFINED;
1524 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1527 NVENCSTATUS nv_status;
1528 NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
1529 allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
1533 if (!
ctx->surfaces[idx].in_ref)
1536 NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
1539 if (
ctx->surfaces[idx].format == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
1545 allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
1546 allocSurf.width = avctx->
width;
1547 allocSurf.height = avctx->
height;
1548 allocSurf.bufferFmt =
ctx->surfaces[idx].format;
1550 nv_status = p_nvenc->nvEncCreateInputBuffer(
ctx->nvencoder, &allocSurf);
1551 if (nv_status != NV_ENC_SUCCESS) {
1555 ctx->surfaces[idx].input_surface = allocSurf.inputBuffer;
1556 ctx->surfaces[idx].width = allocSurf.width;
1557 ctx->surfaces[idx].height = allocSurf.height;
1560 nv_status = p_nvenc->nvEncCreateBitstreamBuffer(
ctx->nvencoder, &allocOut);
1561 if (nv_status != NV_ENC_SUCCESS) {
1564 p_nvenc->nvEncDestroyInputBuffer(
ctx->nvencoder,
ctx->surfaces[idx].input_surface);
1569 ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer;
1579 int i, res = 0, res2;
1586 if (!
ctx->timestamp_list)
1590 if (!
ctx->unused_surface_queue)
1594 if (!
ctx->output_surface_queue)
1597 if (!
ctx->output_surface_ready_queue)
1604 for (
i = 0;
i <
ctx->nb_surfaces;
i++) {
1621 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1623 NVENCSTATUS nv_status;
1624 uint32_t outSize = 0;
1625 char tmpHeader[256];
1626 NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
1627 payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
1629 payload.spsppsBuffer = tmpHeader;
1630 payload.inBufferSize =
sizeof(tmpHeader);
1631 payload.outSPSPPSPayloadSize = &outSize;
1633 nv_status = p_nvenc->nvEncGetSequenceParams(
ctx->nvencoder, &payload);
1634 if (nv_status != NV_ENC_SUCCESS) {
1645 memcpy(avctx->
extradata, tmpHeader, outSize);
1654 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1658 if (
ctx->nvencoder) {
1659 NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER,
1660 .encodePicFlags = NV_ENC_PIC_FLAG_EOS };
1666 p_nvenc->nvEncEncodePicture(
ctx->nvencoder, ¶ms);
1675 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
1676 if (
ctx->registered_frames[
i].mapped)
1677 p_nvenc->nvEncUnmapInputResource(
ctx->nvencoder,
ctx->registered_frames[
i].in_map.mappedResource);
1678 if (
ctx->registered_frames[
i].regptr)
1679 p_nvenc->nvEncUnregisterResource(
ctx->nvencoder,
ctx->registered_frames[
i].regptr);
1681 ctx->nb_registered_frames = 0;
1684 if (
ctx->surfaces) {
1685 for (
i = 0;
i <
ctx->nb_surfaces; ++
i) {
1687 p_nvenc->nvEncDestroyInputBuffer(
ctx->nvencoder,
ctx->surfaces[
i].input_surface);
1689 p_nvenc->nvEncDestroyBitstreamBuffer(
ctx->nvencoder,
ctx->surfaces[
i].output_surface);
1693 ctx->nb_surfaces = 0;
1699 if (
ctx->nvencoder) {
1700 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
1708 if (
ctx->cu_context_internal)
1710 ctx->cu_context =
ctx->cu_context_internal =
NULL;
1713 if (
ctx->d3d11_device) {
1714 ID3D11Device_Release(
ctx->d3d11_device);
1719 nvenc_free_functions(&dl_fn->
nvenc_dl);
1720 cuda_free_functions(&dl_fn->
cuda_dl);
1738 "hw_frames_ctx must be set when using GPU frames as input\n");
1744 "hw_frames_ctx must match the GPU frame type\n");
1789 NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params,
const AVFrame *
frame)
1791 int dst_linesize[4] = {
1792 lock_buffer_params->pitch,
1793 lock_buffer_params->pitch,
1794 lock_buffer_params->pitch,
1795 lock_buffer_params->pitch
1797 uint8_t *dst_data[4];
1801 dst_linesize[1] = dst_linesize[2] >>= 1;
1804 lock_buffer_params->bufferDataPtr, dst_linesize);
1809 FFSWAP(uint8_t*, dst_data[1], dst_data[2]);
1822 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1823 NVENCSTATUS nv_status;
1828 for (first_round = 1; first_round >= 0; first_round--) {
1829 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
1830 if (!
ctx->registered_frames[
i].mapped) {
1831 if (
ctx->registered_frames[
i].regptr) {
1834 nv_status = p_nvenc->nvEncUnregisterResource(
ctx->nvencoder,
ctx->registered_frames[
i].regptr);
1835 if (nv_status != NV_ENC_SUCCESS)
1836 return nvenc_print_error(avctx, nv_status,
"Failed unregistering unused input resource");
1837 ctx->registered_frames[
i].ptr =
NULL;
1838 ctx->registered_frames[
i].regptr =
NULL;
1845 return ctx->nb_registered_frames++;
1856 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1859 NV_ENC_REGISTER_RESOURCE reg = { 0 };
1862 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
1873 reg.version = NV_ENC_REGISTER_RESOURCE_VER;
1874 reg.width = frames_ctx->width;
1875 reg.height = frames_ctx->height;
1876 reg.pitch =
frame->linesize[0];
1877 reg.resourceToRegister =
frame->data[0];
1880 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR;
1883 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX;
1884 reg.subResourceIndex = (intptr_t)
frame->data[1];
1888 if (reg.bufferFormat == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
1894 ret = p_nvenc->nvEncRegisterResource(
ctx->nvencoder, ®);
1895 if (
ret != NV_ENC_SUCCESS) {
1900 ctx->registered_frames[idx].ptr =
frame->data[0];
1901 ctx->registered_frames[idx].ptr_index = reg.subResourceIndex;
1902 ctx->registered_frames[idx].regptr = reg.registeredResource;
1911 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1914 NVENCSTATUS nv_status;
1927 if (!
ctx->registered_frames[reg_idx].mapped) {
1928 ctx->registered_frames[reg_idx].in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER;
1929 ctx->registered_frames[reg_idx].in_map.registeredResource =
ctx->registered_frames[reg_idx].regptr;
1930 nv_status = p_nvenc->nvEncMapInputResource(
ctx->nvencoder, &
ctx->registered_frames[reg_idx].in_map);
1931 if (nv_status != NV_ENC_SUCCESS) {
1937 ctx->registered_frames[reg_idx].mapped += 1;
1939 nvenc_frame->
reg_idx = reg_idx;
1940 nvenc_frame->
input_surface =
ctx->registered_frames[reg_idx].in_map.mappedResource;
1941 nvenc_frame->
format =
ctx->registered_frames[reg_idx].in_map.mappedBufferFmt;
1946 NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
1948 lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
1951 nv_status = p_nvenc->nvEncLockInputBuffer(
ctx->nvencoder, &lockBufferParams);
1952 if (nv_status != NV_ENC_SUCCESS) {
1953 return nvenc_print_error(avctx, nv_status,
"Failed locking nvenc input buffer");
1956 nvenc_frame->
pitch = lockBufferParams.pitch;
1959 nv_status = p_nvenc->nvEncUnlockInputBuffer(
ctx->nvencoder, nvenc_frame->
input_surface);
1960 if (nv_status != NV_ENC_SUCCESS) {
1969 NV_ENC_PIC_PARAMS *params,
1970 NV_ENC_SEI_PAYLOAD *sei_data,
1977 params->codecPicParams.h264PicParams.sliceMode =
1978 ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
1979 params->codecPicParams.h264PicParams.sliceModeData =
1980 ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
1981 if (sei_count > 0) {
1982 params->codecPicParams.h264PicParams.seiPayloadArray = sei_data;
1983 params->codecPicParams.h264PicParams.seiPayloadArrayCnt = sei_count;
1988 params->codecPicParams.hevcPicParams.sliceMode =
1989 ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
1990 params->codecPicParams.hevcPicParams.sliceModeData =
1991 ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
1992 if (sei_count > 0) {
1993 params->codecPicParams.hevcPicParams.seiPayloadArray = sei_data;
1994 params->codecPicParams.hevcPicParams.seiPayloadArrayCnt = sei_count;
2016 NV_ENC_LOCK_BITSTREAM *params,
2021 pkt->
pts = params->outputTimeStamp;
2033 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2035 uint32_t slice_mode_data;
2036 uint32_t *slice_offsets =
NULL;
2037 NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
2038 NVENCSTATUS nv_status;
2045 slice_mode_data =
ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
2048 slice_mode_data =
ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
2055 slice_offsets =
av_mallocz(slice_mode_data *
sizeof(*slice_offsets));
2057 if (!slice_offsets) {
2062 lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
2064 lock_params.doNotWait = 0;
2066 lock_params.sliceOffsets = slice_offsets;
2068 nv_status = p_nvenc->nvEncLockBitstream(
ctx->nvencoder, &lock_params);
2069 if (nv_status != NV_ENC_SUCCESS) {
2081 memcpy(
pkt->
data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
2083 nv_status = p_nvenc->nvEncUnlockBitstream(
ctx->nvencoder, tmpoutsurf->
output_surface);
2084 if (nv_status != NV_ENC_SUCCESS) {
2085 res =
nvenc_print_error(avctx, nv_status,
"Failed unlocking bitstream buffer, expect the gates of mordor to open");
2091 ctx->registered_frames[tmpoutsurf->
reg_idx].mapped -= 1;
2092 if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped == 0) {
2093 nv_status = p_nvenc->nvEncUnmapInputResource(
ctx->nvencoder,
ctx->registered_frames[tmpoutsurf->
reg_idx].in_map.mappedResource);
2094 if (nv_status != NV_ENC_SUCCESS) {
2098 }
else if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped < 0) {
2108 switch (lock_params.pictureType) {
2109 case NV_ENC_PIC_TYPE_IDR:
2111 case NV_ENC_PIC_TYPE_I:
2114 case NV_ENC_PIC_TYPE_P:
2117 case NV_ENC_PIC_TYPE_B:
2120 case NV_ENC_PIC_TYPE_BI:
2124 av_log(avctx,
AV_LOG_ERROR,
"Unknown picture type encountered, expect the output to be broken.\n");
2125 av_log(avctx,
AV_LOG_ERROR,
"Please report this error and include as much information on how to reproduce it as possible.\n");
2153 int nb_ready, nb_pending;
2158 return nb_ready > 0;
2159 return (nb_ready > 0) && (nb_ready + nb_pending >=
ctx->async_depth);
2169 void *a53_data =
NULL;
2170 size_t a53_size = 0;
2178 &
ctx->sei_data_size,
2179 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2186 ctx->sei_data[sei_count].payloadSize = (uint32_t)a53_size;
2187 ctx->sei_data[sei_count].payloadType = 4;
2188 ctx->sei_data[sei_count].payload = (uint8_t*)a53_data;
2195 void *tc_data =
NULL;
2204 &
ctx->sei_data_size,
2205 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2212 ctx->sei_data[sei_count].payloadSize = (uint32_t)tc_size;
2214 ctx->sei_data[sei_count].payload = (uint8_t*)tc_data;
2223 for (
i = 0;
i <
frame->nb_side_data;
i++) {
2231 &
ctx->sei_data_size,
2232 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2238 ctx->sei_data[sei_count].payloadSize = side_data->
size;
2242 if (!
ctx->sei_data[sei_count].payload) {
2254 for (
i = 0;
i < sei_count;
i++)
2263 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
2266 NV_ENC_RECONFIGURE_PARAMS params = { 0 };
2267 int needs_reconfig = 0;
2268 int needs_encode_config = 0;
2269 int reconfig_bitrate = 0, reconfig_dar = 0;
2272 params.version = NV_ENC_RECONFIGURE_PARAMS_VER;
2273 params.reInitEncodeParams =
ctx->init_encode_params;
2276 if (dw !=
ctx->init_encode_params.darWidth || dh !=
ctx->init_encode_params.darHeight) {
2278 "aspect ratio change (DAR): %d:%d -> %d:%d\n",
2279 ctx->init_encode_params.darWidth,
2280 ctx->init_encode_params.darHeight, dw, dh);
2282 params.reInitEncodeParams.darHeight = dh;
2283 params.reInitEncodeParams.darWidth = dw;
2289 if (
ctx->rc != NV_ENC_PARAMS_RC_CONSTQP &&
ctx->support_dyn_bitrate) {
2290 if (avctx->
bit_rate > 0 && params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate != avctx->
bit_rate) {
2292 "avg bitrate change: %d -> %d\n",
2293 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate,
2296 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate = avctx->
bit_rate;
2297 reconfig_bitrate = 1;
2302 "max bitrate change: %d -> %d\n",
2303 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate,
2306 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate = avctx->
rc_max_rate;
2307 reconfig_bitrate = 1;
2312 "vbv buffer size change: %d -> %d\n",
2313 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize,
2316 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize = avctx->
rc_buffer_size;
2317 reconfig_bitrate = 1;
2320 if (reconfig_bitrate) {
2321 params.resetEncoder = 1;
2322 params.forceIDR = 1;
2324 needs_encode_config = 1;
2329 if (!needs_encode_config)
2330 params.reInitEncodeParams.encodeConfig =
NULL;
2332 if (needs_reconfig) {
2333 ret = p_nvenc->nvEncReconfigureEncoder(
ctx->nvencoder, ¶ms);
2334 if (
ret != NV_ENC_SUCCESS) {
2338 ctx->init_encode_params.darHeight = dh;
2339 ctx->init_encode_params.darWidth = dw;
2342 if (reconfig_bitrate) {
2343 ctx->encode_config.rcParams.averageBitRate = params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate;
2344 ctx->encode_config.rcParams.maxBitRate = params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate;
2345 ctx->encode_config.rcParams.vbvBufferSize = params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize;
2354 NVENCSTATUS nv_status;
2362 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2364 NV_ENC_PIC_PARAMS pic_params = { 0 };
2365 pic_params.version = NV_ENC_PIC_PARAMS_VER;
2367 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
2391 pic_params.bufferFmt = in_surf->
format;
2392 pic_params.inputWidth = in_surf->
width;
2393 pic_params.inputHeight = in_surf->
height;
2394 pic_params.inputPitch = in_surf->
pitch;
2398 if (
frame->top_field_first)
2399 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
2401 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
2403 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
2407 pic_params.encodePicFlags =
2408 ctx->forced_idr ? NV_ENC_PIC_FLAG_FORCEIDR : NV_ENC_PIC_FLAG_FORCEINTRA;
2410 pic_params.encodePicFlags = 0;
2413 pic_params.inputTimeStamp =
frame->pts;
2415 if (
ctx->extra_sei) {
2424 pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
2431 nv_status = p_nvenc->nvEncEncodePicture(
ctx->nvencoder, &pic_params);
2433 for (
i = 0;
i < sei_count;
i++)
2440 if (nv_status != NV_ENC_SUCCESS &&
2441 nv_status != NV_ENC_ERR_NEED_MORE_INPUT)
2450 if (nv_status == NV_ENC_SUCCESS) {
2469 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
2472 if (!
frame->buf[0]) {