23 #include "config_components.h"
39 #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, dl_fn->cuda_dl, x)
41 #define NVENC_CAP 0x30
42 #define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR || \
43 rc == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ || \
44 rc == NV_ENC_PARAMS_RC_CBR_HQ)
78 #define IS_10BIT(pix_fmt) (pix_fmt == AV_PIX_FMT_P010 || \
79 pix_fmt == AV_PIX_FMT_P016 || \
80 pix_fmt == AV_PIX_FMT_YUV444P16 || \
81 pix_fmt == AV_PIX_FMT_GBRP16)
83 #define IS_YUV444(pix_fmt) (pix_fmt == AV_PIX_FMT_YUV444P || \
84 pix_fmt == AV_PIX_FMT_YUV444P16 || \
85 pix_fmt == AV_PIX_FMT_GBRP || \
86 pix_fmt == AV_PIX_FMT_GBRP16)
88 #define IS_GBRP(pix_fmt) (pix_fmt == AV_PIX_FMT_GBRP || \
89 pix_fmt == AV_PIX_FMT_GBRP16)
96 { NV_ENC_SUCCESS, 0,
"success" },
97 { NV_ENC_ERR_NO_ENCODE_DEVICE,
AVERROR(ENOENT),
"no encode device" },
98 { NV_ENC_ERR_UNSUPPORTED_DEVICE,
AVERROR(ENOSYS),
"unsupported device" },
99 { NV_ENC_ERR_INVALID_ENCODERDEVICE,
AVERROR(EINVAL),
"invalid encoder device" },
100 { NV_ENC_ERR_INVALID_DEVICE,
AVERROR(EINVAL),
"invalid device" },
101 { NV_ENC_ERR_DEVICE_NOT_EXIST,
AVERROR(EIO),
"device does not exist" },
102 { NV_ENC_ERR_INVALID_PTR,
AVERROR(EFAULT),
"invalid ptr" },
103 { NV_ENC_ERR_INVALID_EVENT,
AVERROR(EINVAL),
"invalid event" },
104 { NV_ENC_ERR_INVALID_PARAM,
AVERROR(EINVAL),
"invalid param" },
105 { NV_ENC_ERR_INVALID_CALL,
AVERROR(EINVAL),
"invalid call" },
106 { NV_ENC_ERR_OUT_OF_MEMORY,
AVERROR(ENOMEM),
"out of memory" },
107 { NV_ENC_ERR_ENCODER_NOT_INITIALIZED,
AVERROR(EINVAL),
"encoder not initialized" },
108 { NV_ENC_ERR_UNSUPPORTED_PARAM,
AVERROR(ENOSYS),
"unsupported param" },
109 { NV_ENC_ERR_LOCK_BUSY,
AVERROR(EAGAIN),
"lock busy" },
111 { NV_ENC_ERR_INVALID_VERSION,
AVERROR(EINVAL),
"invalid version" },
112 { NV_ENC_ERR_MAP_FAILED,
AVERROR(EIO),
"map failed" },
113 { NV_ENC_ERR_NEED_MORE_INPUT,
AVERROR(EAGAIN),
"need more input" },
114 { NV_ENC_ERR_ENCODER_BUSY,
AVERROR(EAGAIN),
"encoder busy" },
115 { NV_ENC_ERR_EVENT_NOT_REGISTERD,
AVERROR(EBADF),
"event not registered" },
117 { NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY,
AVERROR(EINVAL),
"incompatible client key" },
118 { NV_ENC_ERR_UNIMPLEMENTED,
AVERROR(ENOSYS),
"unimplemented" },
119 { NV_ENC_ERR_RESOURCE_REGISTER_FAILED,
AVERROR(EIO),
"resource register failed" },
120 { NV_ENC_ERR_RESOURCE_NOT_REGISTERED,
AVERROR(EBADF),
"resource not registered" },
121 { NV_ENC_ERR_RESOURCE_NOT_MAPPED,
AVERROR(EBADF),
"resource not mapped" },
135 *
desc =
"unknown error";
140 const char *error_string)
143 const char *details =
"(no details)";
146 #ifdef NVENC_HAVE_GETLASTERRORSTRING
148 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
150 if (p_nvenc &&
ctx->nvencoder)
151 details = p_nvenc->nvEncGetLastErrorString(
ctx->nvencoder);
164 #define PRESET_ALIAS(alias, name, ...) \
165 [PRESET_ ## alias] = { NV_ENC_PRESET_ ## name ## _GUID, __VA_ARGS__ }
167 #define PRESET(name, ...) PRESET_ALIAS(name, name, __VA_ARGS__)
172 #ifdef NVENC_HAVE_NEW_PRESETS
211 ctx->init_encode_params.presetGUID = t->
guid;
214 #ifdef NVENC_HAVE_NEW_PRESETS
215 if (
ctx->tuning_info == NV_ENC_TUNING_INFO_LOSSLESS)
225 #if NVENCAPI_CHECK_VERSION(11, 2)
226 const char *minver =
"(unknown)";
227 #elif NVENCAPI_CHECK_VERSION(11, 1)
228 # if defined(_WIN32) || defined(__CYGWIN__)
229 const char *minver =
"471.41";
231 const char *minver =
"470.57.02";
233 #elif NVENCAPI_CHECK_VERSION(11, 0)
234 # if defined(_WIN32) || defined(__CYGWIN__)
235 const char *minver =
"456.71";
237 const char *minver =
"455.28";
239 #elif NVENCAPI_CHECK_VERSION(10, 0)
240 # if defined(_WIN32) || defined(__CYGWIN__)
241 const char *minver =
"450.51";
243 const char *minver =
"445.87";
245 #elif NVENCAPI_CHECK_VERSION(9, 1)
246 # if defined(_WIN32) || defined(__CYGWIN__)
247 const char *minver =
"436.15";
249 const char *minver =
"435.21";
251 #elif NVENCAPI_CHECK_VERSION(9, 0)
252 # if defined(_WIN32) || defined(__CYGWIN__)
253 const char *minver =
"418.81";
255 const char *minver =
"418.30";
257 #elif NVENCAPI_CHECK_VERSION(8, 2)
258 # if defined(_WIN32) || defined(__CYGWIN__)
259 const char *minver =
"397.93";
261 const char *minver =
"396.24";
263 #elif NVENCAPI_CHECK_VERSION(8, 1)
264 # if defined(_WIN32) || defined(__CYGWIN__)
265 const char *minver =
"390.77";
267 const char *minver =
"390.25";
270 # if defined(_WIN32) || defined(__CYGWIN__)
271 const char *minver =
"378.66";
273 const char *minver =
"378.13";
276 av_log(avctx,
level,
"The minimum required Nvidia driver for nvenc is %s or newer\n", minver);
284 uint32_t nvenc_max_ver;
287 ret = cuda_load_functions(&dl_fn->
cuda_dl, avctx);
291 ret = nvenc_load_functions(&dl_fn->
nvenc_dl, avctx);
297 err = dl_fn->
nvenc_dl->NvEncodeAPIGetMaxSupportedVersion(&nvenc_max_ver);
298 if (err != NV_ENC_SUCCESS)
303 if ((NVENCAPI_MAJOR_VERSION << 4 | NVENCAPI_MINOR_VERSION) > nvenc_max_ver) {
304 av_log(avctx,
AV_LOG_ERROR,
"Driver does not support the required nvenc API version. "
305 "Required: %d.%d Found: %d.%d\n",
306 NVENCAPI_MAJOR_VERSION, NVENCAPI_MINOR_VERSION,
307 nvenc_max_ver >> 4, nvenc_max_ver & 0
xf);
312 dl_fn->
nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
315 if (err != NV_ENC_SUCCESS)
328 if (
ctx->d3d11_device)
340 if (
ctx->d3d11_device)
348 NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = { 0 };
350 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
353 params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
354 params.apiVersion = NVENCAPI_VERSION;
355 if (
ctx->d3d11_device) {
356 params.device =
ctx->d3d11_device;
357 params.deviceType = NV_ENC_DEVICE_TYPE_DIRECTX;
359 params.device =
ctx->cu_context;
360 params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
363 ret = p_nvenc->nvEncOpenEncodeSessionEx(¶ms, &
ctx->nvencoder);
364 if (
ret != NV_ENC_SUCCESS) {
375 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
376 int i,
ret, count = 0;
379 ret = p_nvenc->nvEncGetEncodeGUIDCount(
ctx->nvencoder, &count);
381 if (
ret != NV_ENC_SUCCESS || !count)
388 ret = p_nvenc->nvEncGetEncodeGUIDs(
ctx->nvencoder, guids, count, &count);
389 if (
ret != NV_ENC_SUCCESS) {
395 for (
i = 0;
i < count;
i++) {
396 if (!memcmp(&guids[
i], &
ctx->init_encode_params.encodeGUID,
sizeof(*guids))) {
411 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
412 NV_ENC_CAPS_PARAM params = { 0 };
415 params.version = NV_ENC_CAPS_PARAM_VER;
416 params.capsToQuery = cap;
418 ret = p_nvenc->nvEncGetEncodeCaps(
ctx->nvencoder,
ctx->init_encode_params.encodeGUID, ¶ms, &
val);
420 if (
ret == NV_ENC_SUCCESS)
449 if (ret < avctx->
width) {
456 if (ret < avctx->
height) {
463 if (ret < avctx->max_b_frames) {
473 "Interlaced encoding is not supported. Supported level: %d\n",
485 if (
ctx->rc_lookahead > 0 &&
ret <= 0) {
491 if (
ctx->temporal_aq > 0 &&
ret <= 0) {
497 if (
ctx->weighted_pred > 0 &&
ret <= 0) {
503 if (
ctx->coder == NV_ENC_H264_ENTROPY_CODING_MODE_CABAC &&
ret <= 0) {
508 #ifdef NVENC_HAVE_BFRAME_REF_MODE
510 if (
ctx->b_ref_mode == NV_ENC_BFRAME_REF_MODE_EACH &&
ret != 1 &&
ret != 3) {
513 }
else if (
ctx->b_ref_mode != NV_ENC_BFRAME_REF_MODE_DISABLED &&
ret == 0) {
518 if (
ctx->b_ref_mode != 0) {
524 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
526 if(avctx->
refs != NV_ENC_NUM_REF_FRAMES_AUTOSELECT &&
ret <= 0) {
531 if(avctx->
refs != 0) {
537 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
539 if(
ctx->single_slice_intra_refresh &&
ret <= 0) {
544 if(
ctx->single_slice_intra_refresh) {
551 if((
ctx->intra_refresh ||
ctx->single_slice_intra_refresh) &&
ret <= 0) {
556 #ifndef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
564 if(
ctx->constrained_encoding &&
ret <= 0) {
569 ctx->support_dyn_bitrate =
nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE);
578 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
579 char name[128] = { 0};
599 av_log(avctx, loglevel,
"[ GPU #%d - < %s > has Compute SM %d.%d ]\n", idx,
name,
major,
minor);
601 av_log(avctx, loglevel,
"does not support NVENC\n");
612 ctx->cu_context =
ctx->cu_context_internal;
624 av_log(avctx, loglevel,
"supports NVENC\n");
635 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
643 ctx->cu_context_internal =
NULL;
656 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_H264_GUID;
659 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_HEVC_GUID;
668 av_log(avctx,
AV_LOG_WARNING,
"The selected preset is deprecated. Use p1 to p7 + -tune or fast/medium/slow.\n");
692 cuda_device_hwctx = hwdev_ctx->
hwctx;
695 d3d11_device_hwctx = hwdev_ctx->
hwctx;
703 if (cuda_device_hwctx) {
705 ctx->cu_stream = cuda_device_hwctx->
stream;
708 else if (d3d11_device_hwctx) {
709 ctx->d3d11_device = d3d11_device_hwctx->
device;
710 ID3D11Device_AddRef(
ctx->d3d11_device);
720 av_log(avctx,
AV_LOG_FATAL,
"Provided device doesn't support required NVENC features\n");
724 int i, nb_devices = 0;
740 for (
i = 0;
i < nb_devices; ++
i) {
753 av_log(avctx,
AV_LOG_FATAL,
"Requested GPU %d, but only %d GPUs are available!\n",
ctx->device, nb_devices);
763 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
765 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
767 if (
ctx->init_qp_p >= 0) {
768 rc->constQP.qpInterP =
ctx->init_qp_p;
769 if (
ctx->init_qp_i >= 0 &&
ctx->init_qp_b >= 0) {
770 rc->constQP.qpIntra =
ctx->init_qp_i;
771 rc->constQP.qpInterB =
ctx->init_qp_b;
775 rc->constQP.qpInterB =
av_clip(
778 rc->constQP.qpIntra = rc->constQP.qpInterP;
779 rc->constQP.qpInterB = rc->constQP.qpInterP;
781 }
else if (
ctx->cqp >= 0) {
782 rc->constQP.qpInterP = rc->constQP.qpInterB = rc->constQP.qpIntra =
ctx->cqp;
796 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
799 if (avctx->
qmin >= 0 && avctx->
qmax >= 0) {
803 rc->minQP.qpInterB = avctx->
qmin;
804 rc->minQP.qpInterP = avctx->
qmin;
805 rc->minQP.qpIntra = avctx->
qmin;
807 rc->maxQP.qpInterB = avctx->
qmax;
808 rc->maxQP.qpInterP = avctx->
qmax;
809 rc->maxQP.qpIntra = avctx->
qmax;
811 qp_inter_p = (avctx->
qmax + 3 * avctx->
qmin) / 4;
812 }
else if (avctx->
qmin >= 0) {
815 rc->minQP.qpInterB = avctx->
qmin;
816 rc->minQP.qpInterP = avctx->
qmin;
817 rc->minQP.qpIntra = avctx->
qmin;
819 qp_inter_p = avctx->
qmin;
824 rc->enableInitialRCQP = 1;
826 if (
ctx->init_qp_p < 0) {
827 rc->initialRCQP.qpInterP = qp_inter_p;
829 rc->initialRCQP.qpInterP =
ctx->init_qp_p;
832 if (
ctx->init_qp_i < 0) {
834 rc->initialRCQP.qpIntra =
av_clip(
837 rc->initialRCQP.qpIntra = rc->initialRCQP.qpInterP;
840 rc->initialRCQP.qpIntra =
ctx->init_qp_i;
843 if (
ctx->init_qp_b < 0) {
845 rc->initialRCQP.qpInterB =
av_clip(
848 rc->initialRCQP.qpInterB = rc->initialRCQP.qpInterP;
851 rc->initialRCQP.qpInterB =
ctx->init_qp_b;
858 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
860 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
861 rc->constQP.qpInterB = 0;
862 rc->constQP.qpInterP = 0;
863 rc->constQP.qpIntra = 0;
872 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
875 case NV_ENC_PARAMS_RC_CONSTQP:
878 case NV_ENC_PARAMS_RC_VBR_MINQP:
879 if (avctx->
qmin < 0) {
881 "The variable bitrate rate-control requires "
882 "the 'qmin' option set.\n");
887 case NV_ENC_PARAMS_RC_VBR_HQ:
888 case NV_ENC_PARAMS_RC_VBR:
891 case NV_ENC_PARAMS_RC_CBR:
892 case NV_ENC_PARAMS_RC_CBR_HQ:
893 case NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ:
897 rc->rateControlMode =
ctx->rc;
906 int nb_surfaces =
FFMAX(4,
ctx->encode_config.frameIntervalP * 2 * 2);
909 if (
ctx->rc_lookahead > 0) {
912 nb_surfaces =
FFMAX(1,
FFMAX(nb_surfaces,
ctx->rc_lookahead +
ctx->encode_config.frameIntervalP + 1 + 4));
913 if (nb_surfaces >
ctx->nb_surfaces &&
ctx->nb_surfaces > 0)
916 "Defined rc_lookahead requires more surfaces, "
917 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
919 ctx->nb_surfaces =
FFMAX(nb_surfaces,
ctx->nb_surfaces);
921 if (
ctx->encode_config.frameIntervalP > 1 &&
ctx->nb_surfaces < nb_surfaces &&
ctx->nb_surfaces > 0)
924 "Defined b-frame requires more surfaces, "
925 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
926 ctx->nb_surfaces =
FFMAX(
ctx->nb_surfaces, nb_surfaces);
928 else if (
ctx->nb_surfaces <= 0)
929 ctx->nb_surfaces = nb_surfaces;
944 av_log(avctx,
AV_LOG_WARNING,
"Using global_quality with nvenc is deprecated. Use qp instead.\n");
950 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate;
951 }
else if (
ctx->encode_config.rcParams.averageBitRate > 0) {
952 ctx->encode_config.rcParams.maxBitRate =
ctx->encode_config.rcParams.averageBitRate;
958 #ifdef NVENC_HAVE_MULTIPASS
959 ctx->encode_config.rcParams.multiPass =
ctx->multipass;
962 ctx->encode_config.rcParams.multiPass = NV_ENC_MULTI_PASS_DISABLED;
964 ctx->encode_config.rcParams.multiPass = NV_ENC_TWO_PASS_FULL_RESOLUTION;
968 ctx->rc = NV_ENC_PARAMS_RC_CBR;
969 }
else if (
ctx->cqp >= 0) {
970 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
971 }
else if (
ctx->quality >= 0.0f) {
972 ctx->rc = NV_ENC_PARAMS_RC_VBR;
982 if (
ctx->twopass < 0)
987 ctx->rc = NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ;
989 ctx->rc = NV_ENC_PARAMS_RC_CBR;
991 }
else if (
ctx->cqp >= 0) {
992 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
993 }
else if (
ctx->twopass) {
994 ctx->rc = NV_ENC_PARAMS_RC_VBR_HQ;
995 }
else if (avctx->
qmin >= 0 && avctx->
qmax >= 0) {
996 ctx->rc = NV_ENC_PARAMS_RC_VBR_MINQP;
1008 #ifdef NVENC_HAVE_QP_CHROMA_OFFSETS
1009 ctx->encode_config.rcParams.cbQPIndexOffset =
ctx->qp_cb_offset;
1010 ctx->encode_config.rcParams.crQPIndexOffset =
ctx->qp_cr_offset;
1012 if (
ctx->qp_cb_offset ||
ctx->qp_cr_offset)
1013 av_log(avctx,
AV_LOG_WARNING,
"Failed setting QP CB/CR offsets, SDK 11.1 or greater required at compile time.\n");
1016 #ifdef NVENC_HAVE_LDKFS
1018 ctx->encode_config.rcParams.lowDelayKeyFrameScale =
ctx->ldkfs;
1023 }
else if (
ctx->rc >= 0) {
1026 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
1032 }
else if (
ctx->encode_config.rcParams.averageBitRate > 0) {
1033 avctx->
rc_buffer_size =
ctx->encode_config.rcParams.vbvBufferSize = 2 *
ctx->encode_config.rcParams.averageBitRate;
1037 ctx->encode_config.rcParams.enableAQ = 1;
1038 ctx->encode_config.rcParams.aqStrength =
ctx->aq_strength;
1042 if (
ctx->temporal_aq) {
1043 ctx->encode_config.rcParams.enableTemporalAQ = 1;
1047 if (
ctx->rc_lookahead > 0) {
1048 int lkd_bound =
FFMIN(
ctx->nb_surfaces,
ctx->async_depth) -
1049 ctx->encode_config.frameIntervalP - 4;
1051 if (lkd_bound < 0) {
1053 "Lookahead not enabled. Increase buffer delay (-delay).\n");
1055 ctx->encode_config.rcParams.enableLookahead = 1;
1056 ctx->encode_config.rcParams.lookaheadDepth =
av_clip(
ctx->rc_lookahead, 0, lkd_bound);
1057 ctx->encode_config.rcParams.disableIadapt =
ctx->no_scenecut;
1058 ctx->encode_config.rcParams.disableBadapt = !
ctx->b_adapt;
1060 "Lookahead enabled: depth %d, scenecut %s, B-adapt %s.\n",
1061 ctx->encode_config.rcParams.lookaheadDepth,
1062 ctx->encode_config.rcParams.disableIadapt ?
"disabled" :
"enabled",
1063 ctx->encode_config.rcParams.disableBadapt ?
"disabled" :
"enabled");
1067 if (
ctx->strict_gop) {
1068 ctx->encode_config.rcParams.strictGOPTarget = 1;
1073 ctx->encode_config.rcParams.enableNonRefP = 1;
1075 if (
ctx->zerolatency)
1076 ctx->encode_config.rcParams.zeroReorderDelay = 1;
1080 int tmp_quality = (
int)(
ctx->quality * 256.0f);
1081 ctx->encode_config.rcParams.targetQuality = (uint8_t)(tmp_quality >> 8);
1082 ctx->encode_config.rcParams.targetQualityLSB = (uint8_t)(tmp_quality & 0xff);
1087 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate = 0;
1095 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1096 NV_ENC_CONFIG_H264 *h264 = &cc->encodeCodecConfig.h264Config;
1097 NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui = &h264->h264VUIParameters;
1101 vui->transferCharacteristics = avctx->
color_trc;
1105 vui->colourDescriptionPresentFlag =
1106 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1108 vui->videoSignalTypePresentFlag =
1109 (vui->colourDescriptionPresentFlag
1110 || vui->videoFormat != 5
1111 || vui->videoFullRangeFlag != 0);
1113 h264->sliceMode = 3;
1114 h264->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1116 if (
ctx->intra_refresh) {
1117 h264->enableIntraRefresh = 1;
1118 h264->intraRefreshPeriod = avctx->
gop_size;
1119 h264->intraRefreshCnt = avctx->
gop_size - 1;
1120 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1121 h264->singleSliceIntraRefresh =
ctx->single_slice_intra_refresh;
1125 if (
ctx->constrained_encoding)
1126 h264->enableConstrainedEncoding = 1;
1130 h264->outputAUD =
ctx->aud;
1132 if (
ctx->dpb_size >= 0) {
1134 h264->maxNumRefFrames =
ctx->dpb_size;
1137 if (
ctx->intra_refresh) {
1138 h264->idrPeriod = NVENC_INFINITE_GOPLENGTH;
1143 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1144 h264->outputBufferingPeriodSEI = 1;
1147 h264->outputPictureTimingSEI = 1;
1149 if (cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ ||
1150 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_HQ ||
1151 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_VBR_HQ) {
1152 h264->adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
1153 h264->fmoMode = NV_ENC_H264_FMO_DISABLE;
1157 h264->qpPrimeYZeroTransformBypassFlag = 1;
1159 switch(
ctx->profile) {
1161 cc->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
1165 cc->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
1169 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
1173 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1181 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1187 h264->level =
ctx->level;
1189 if (
ctx->coder >= 0)
1190 h264->entropyCodingMode =
ctx->coder;
1192 #ifdef NVENC_HAVE_BFRAME_REF_MODE
1193 h264->useBFramesAsRef =
ctx->b_ref_mode;
1196 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1197 h264->numRefL0 = avctx->
refs;
1198 h264->numRefL1 = avctx->
refs;
1207 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1208 NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig;
1209 NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui = &hevc->hevcVUIParameters;
1213 vui->transferCharacteristics = avctx->
color_trc;
1217 vui->colourDescriptionPresentFlag =
1218 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1220 vui->videoSignalTypePresentFlag =
1221 (vui->colourDescriptionPresentFlag
1222 || vui->videoFormat != 5
1223 || vui->videoFullRangeFlag != 0);
1225 hevc->sliceMode = 3;
1226 hevc->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1228 if (
ctx->intra_refresh) {
1229 hevc->enableIntraRefresh = 1;
1230 hevc->intraRefreshPeriod = avctx->
gop_size;
1231 hevc->intraRefreshCnt = avctx->
gop_size - 1;
1232 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1233 hevc->singleSliceIntraRefresh =
ctx->single_slice_intra_refresh;
1237 #ifdef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
1238 if (
ctx->constrained_encoding)
1239 hevc->enableConstrainedEncoding = 1;
1244 hevc->outputAUD =
ctx->aud;
1246 if (
ctx->dpb_size >= 0) {
1248 hevc->maxNumRefFramesInDPB =
ctx->dpb_size;
1251 if (
ctx->intra_refresh) {
1252 hevc->idrPeriod = NVENC_INFINITE_GOPLENGTH;
1257 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1258 hevc->outputBufferingPeriodSEI = 1;
1261 hevc->outputPictureTimingSEI = 1;
1263 switch (
ctx->profile) {
1265 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
1269 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1273 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1280 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1286 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1290 hevc->chromaFormatIDC =
IS_YUV444(
ctx->data_pix_fmt) ? 3 : 1;
1292 hevc->pixelBitDepthMinus8 =
IS_10BIT(
ctx->data_pix_fmt) ? 2 : 0;
1294 hevc->level =
ctx->level;
1296 hevc->tier =
ctx->tier;
1298 #ifdef NVENC_HAVE_HEVC_BFRAME_REF_MODE
1299 hevc->useBFramesAsRef =
ctx->b_ref_mode;
1302 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1303 hevc->numRefL0 = avctx->
refs;
1304 hevc->numRefL1 = avctx->
refs;
1341 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1343 NV_ENC_PRESET_CONFIG preset_config = { 0 };
1344 NVENCSTATUS nv_status = NV_ENC_SUCCESS;
1349 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1350 ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
1352 ctx->init_encode_params.encodeHeight = avctx->
height;
1353 ctx->init_encode_params.encodeWidth = avctx->
width;
1355 ctx->init_encode_params.encodeConfig = &
ctx->encode_config;
1357 preset_config.version = NV_ENC_PRESET_CONFIG_VER;
1358 preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
1360 #ifdef NVENC_HAVE_NEW_PRESETS
1361 ctx->init_encode_params.tuningInfo =
ctx->tuning_info;
1364 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOSSLESS;
1366 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOW_LATENCY;
1368 nv_status = p_nvenc->nvEncGetEncodePresetConfigEx(
ctx->nvencoder,
1369 ctx->init_encode_params.encodeGUID,
1370 ctx->init_encode_params.presetGUID,
1371 ctx->init_encode_params.tuningInfo,
1374 nv_status = p_nvenc->nvEncGetEncodePresetConfig(
ctx->nvencoder,
1375 ctx->init_encode_params.encodeGUID,
1376 ctx->init_encode_params.presetGUID,
1379 if (nv_status != NV_ENC_SUCCESS)
1380 return nvenc_print_error(avctx, nv_status,
"Cannot get the preset configuration");
1382 memcpy(&
ctx->encode_config, &preset_config.presetCfg,
sizeof(
ctx->encode_config));
1384 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1387 ctx->init_encode_params.darHeight = dh;
1388 ctx->init_encode_params.darWidth = dw;
1398 ctx->init_encode_params.enableEncodeAsync = 0;
1399 ctx->init_encode_params.enablePTD = 1;
1401 #ifdef NVENC_HAVE_NEW_PRESETS
1405 if (
ctx->rc_lookahead == 0 &&
ctx->encode_config.rcParams.enableLookahead)
1406 ctx->rc_lookahead =
ctx->encode_config.rcParams.lookaheadDepth;
1409 if (
ctx->weighted_pred == 1)
1410 ctx->init_encode_params.enableWeightedPrediction = 1;
1412 if (
ctx->bluray_compat) {
1421 ctx->level = NV_ENC_LEVEL_HEVC_51;
1422 ctx->tier = NV_ENC_TIER_HEVC_HIGH;
1435 ctx->encode_config.frameIntervalP = 0;
1436 ctx->encode_config.gopLength = 1;
1440 if(
ctx->single_slice_intra_refresh)
1441 ctx->intra_refresh = 1;
1443 if (
ctx->intra_refresh)
1444 ctx->encode_config.gopLength = NVENC_INFINITE_GOPLENGTH;
1451 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
1453 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
1464 nv_status = p_nvenc->nvEncInitializeEncoder(
ctx->nvencoder, &
ctx->init_encode_params);
1465 if (nv_status != NV_ENC_SUCCESS) {
1470 #ifdef NVENC_HAVE_CUSTREAM_PTR
1471 if (
ctx->cu_context) {
1472 nv_status = p_nvenc->nvEncSetIOCudaStreams(
ctx->nvencoder, &
ctx->cu_stream, &
ctx->cu_stream);
1473 if (nv_status != NV_ENC_SUCCESS) {
1484 if (
ctx->encode_config.frameIntervalP > 1)
1487 if (
ctx->encode_config.rcParams.averageBitRate > 0)
1488 avctx->
bit_rate =
ctx->encode_config.rcParams.averageBitRate;
1495 cpb_props->
buffer_size =
ctx->encode_config.rcParams.vbvBufferSize;
1504 return NV_ENC_BUFFER_FORMAT_YV12_PL;
1506 return NV_ENC_BUFFER_FORMAT_NV12_PL;
1509 return NV_ENC_BUFFER_FORMAT_YUV420_10BIT;
1512 return NV_ENC_BUFFER_FORMAT_YUV444_PL;
1515 return NV_ENC_BUFFER_FORMAT_YUV444_10BIT;
1518 return NV_ENC_BUFFER_FORMAT_ARGB;
1521 return NV_ENC_BUFFER_FORMAT_ABGR;
1523 return NV_ENC_BUFFER_FORMAT_ARGB10;
1525 return NV_ENC_BUFFER_FORMAT_ABGR10;
1527 return NV_ENC_BUFFER_FORMAT_UNDEFINED;
1535 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1538 NVENCSTATUS nv_status;
1539 NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
1540 allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
1544 if (!
ctx->surfaces[idx].in_ref)
1547 NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
1550 if (
ctx->surfaces[idx].format == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
1556 allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
1557 allocSurf.width = avctx->
width;
1558 allocSurf.height = avctx->
height;
1559 allocSurf.bufferFmt =
ctx->surfaces[idx].format;
1561 nv_status = p_nvenc->nvEncCreateInputBuffer(
ctx->nvencoder, &allocSurf);
1562 if (nv_status != NV_ENC_SUCCESS) {
1566 ctx->surfaces[idx].input_surface = allocSurf.inputBuffer;
1567 ctx->surfaces[idx].width = allocSurf.width;
1568 ctx->surfaces[idx].height = allocSurf.height;
1571 nv_status = p_nvenc->nvEncCreateBitstreamBuffer(
ctx->nvencoder, &allocOut);
1572 if (nv_status != NV_ENC_SUCCESS) {
1575 p_nvenc->nvEncDestroyInputBuffer(
ctx->nvencoder,
ctx->surfaces[idx].input_surface);
1580 ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer;
1590 int i, res = 0, res2;
1597 if (!
ctx->timestamp_list)
1601 if (!
ctx->unused_surface_queue)
1605 if (!
ctx->output_surface_queue)
1608 if (!
ctx->output_surface_ready_queue)
1615 for (
i = 0;
i <
ctx->nb_surfaces;
i++) {
1632 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1634 NVENCSTATUS nv_status;
1635 uint32_t outSize = 0;
1636 char tmpHeader[256];
1637 NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
1638 payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
1640 payload.spsppsBuffer = tmpHeader;
1641 payload.inBufferSize =
sizeof(tmpHeader);
1642 payload.outSPSPPSPayloadSize = &outSize;
1644 nv_status = p_nvenc->nvEncGetSequenceParams(
ctx->nvencoder, &payload);
1645 if (nv_status != NV_ENC_SUCCESS) {
1656 memcpy(avctx->
extradata, tmpHeader, outSize);
1665 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1669 if (
ctx->nvencoder) {
1670 NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER,
1671 .encodePicFlags = NV_ENC_PIC_FLAG_EOS };
1677 p_nvenc->nvEncEncodePicture(
ctx->nvencoder, ¶ms);
1686 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
1687 if (
ctx->registered_frames[
i].mapped)
1688 p_nvenc->nvEncUnmapInputResource(
ctx->nvencoder,
ctx->registered_frames[
i].in_map.mappedResource);
1689 if (
ctx->registered_frames[
i].regptr)
1690 p_nvenc->nvEncUnregisterResource(
ctx->nvencoder,
ctx->registered_frames[
i].regptr);
1692 ctx->nb_registered_frames = 0;
1695 if (
ctx->surfaces) {
1696 for (
i = 0;
i <
ctx->nb_surfaces; ++
i) {
1698 p_nvenc->nvEncDestroyInputBuffer(
ctx->nvencoder,
ctx->surfaces[
i].input_surface);
1700 p_nvenc->nvEncDestroyBitstreamBuffer(
ctx->nvencoder,
ctx->surfaces[
i].output_surface);
1704 ctx->nb_surfaces = 0;
1710 if (
ctx->nvencoder) {
1711 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
1719 if (
ctx->cu_context_internal)
1721 ctx->cu_context =
ctx->cu_context_internal =
NULL;
1724 if (
ctx->d3d11_device) {
1725 ID3D11Device_Release(
ctx->d3d11_device);
1730 nvenc_free_functions(&dl_fn->
nvenc_dl);
1731 cuda_free_functions(&dl_fn->
cuda_dl);
1749 "hw_frames_ctx must be set when using GPU frames as input\n");
1755 "hw_frames_ctx must match the GPU frame type\n");
1799 NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params,
const AVFrame *
frame)
1801 int dst_linesize[4] = {
1802 lock_buffer_params->pitch,
1803 lock_buffer_params->pitch,
1804 lock_buffer_params->pitch,
1805 lock_buffer_params->pitch
1807 uint8_t *dst_data[4];
1811 dst_linesize[1] = dst_linesize[2] >>= 1;
1814 lock_buffer_params->bufferDataPtr, dst_linesize);
1819 FFSWAP(uint8_t*, dst_data[1], dst_data[2]);
1832 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1833 NVENCSTATUS nv_status;
1838 for (first_round = 1; first_round >= 0; first_round--) {
1839 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
1840 if (!
ctx->registered_frames[
i].mapped) {
1841 if (
ctx->registered_frames[
i].regptr) {
1844 nv_status = p_nvenc->nvEncUnregisterResource(
ctx->nvencoder,
ctx->registered_frames[
i].regptr);
1845 if (nv_status != NV_ENC_SUCCESS)
1846 return nvenc_print_error(avctx, nv_status,
"Failed unregistering unused input resource");
1847 ctx->registered_frames[
i].ptr =
NULL;
1848 ctx->registered_frames[
i].regptr =
NULL;
1855 return ctx->nb_registered_frames++;
1866 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1869 NV_ENC_REGISTER_RESOURCE reg = { 0 };
1872 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
1883 reg.version = NV_ENC_REGISTER_RESOURCE_VER;
1884 reg.width = frames_ctx->width;
1885 reg.height = frames_ctx->height;
1886 reg.pitch =
frame->linesize[0];
1887 reg.resourceToRegister =
frame->data[0];
1890 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR;
1893 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX;
1894 reg.subResourceIndex = (intptr_t)
frame->data[1];
1898 if (reg.bufferFormat == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
1904 ret = p_nvenc->nvEncRegisterResource(
ctx->nvencoder, ®);
1905 if (
ret != NV_ENC_SUCCESS) {
1910 ctx->registered_frames[idx].ptr =
frame->data[0];
1911 ctx->registered_frames[idx].ptr_index = reg.subResourceIndex;
1912 ctx->registered_frames[idx].regptr = reg.registeredResource;
1921 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1924 NVENCSTATUS nv_status;
1937 if (!
ctx->registered_frames[reg_idx].mapped) {
1938 ctx->registered_frames[reg_idx].in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER;
1939 ctx->registered_frames[reg_idx].in_map.registeredResource =
ctx->registered_frames[reg_idx].regptr;
1940 nv_status = p_nvenc->nvEncMapInputResource(
ctx->nvencoder, &
ctx->registered_frames[reg_idx].in_map);
1941 if (nv_status != NV_ENC_SUCCESS) {
1947 ctx->registered_frames[reg_idx].mapped += 1;
1949 nvenc_frame->
reg_idx = reg_idx;
1950 nvenc_frame->
input_surface =
ctx->registered_frames[reg_idx].in_map.mappedResource;
1951 nvenc_frame->
format =
ctx->registered_frames[reg_idx].in_map.mappedBufferFmt;
1956 NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
1958 lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
1961 nv_status = p_nvenc->nvEncLockInputBuffer(
ctx->nvencoder, &lockBufferParams);
1962 if (nv_status != NV_ENC_SUCCESS) {
1963 return nvenc_print_error(avctx, nv_status,
"Failed locking nvenc input buffer");
1966 nvenc_frame->
pitch = lockBufferParams.pitch;
1969 nv_status = p_nvenc->nvEncUnlockInputBuffer(
ctx->nvencoder, nvenc_frame->
input_surface);
1970 if (nv_status != NV_ENC_SUCCESS) {
1979 NV_ENC_PIC_PARAMS *params,
1980 NV_ENC_SEI_PAYLOAD *sei_data,
1987 params->codecPicParams.h264PicParams.sliceMode =
1988 ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
1989 params->codecPicParams.h264PicParams.sliceModeData =
1990 ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
1991 if (sei_count > 0) {
1992 params->codecPicParams.h264PicParams.seiPayloadArray = sei_data;
1993 params->codecPicParams.h264PicParams.seiPayloadArrayCnt = sei_count;
1998 params->codecPicParams.hevcPicParams.sliceMode =
1999 ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
2000 params->codecPicParams.hevcPicParams.sliceModeData =
2001 ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
2002 if (sei_count > 0) {
2003 params->codecPicParams.hevcPicParams.seiPayloadArray = sei_data;
2004 params->codecPicParams.hevcPicParams.seiPayloadArrayCnt = sei_count;
2026 NV_ENC_LOCK_BITSTREAM *params,
2031 pkt->
pts = params->outputTimeStamp;
2043 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2045 uint32_t slice_mode_data;
2046 uint32_t *slice_offsets =
NULL;
2047 NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
2048 NVENCSTATUS nv_status;
2055 slice_mode_data =
ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
2058 slice_mode_data =
ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
2065 slice_offsets =
av_mallocz(slice_mode_data *
sizeof(*slice_offsets));
2067 if (!slice_offsets) {
2072 lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
2074 lock_params.doNotWait = 0;
2076 lock_params.sliceOffsets = slice_offsets;
2078 nv_status = p_nvenc->nvEncLockBitstream(
ctx->nvencoder, &lock_params);
2079 if (nv_status != NV_ENC_SUCCESS) {
2091 memcpy(
pkt->
data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
2093 nv_status = p_nvenc->nvEncUnlockBitstream(
ctx->nvencoder, tmpoutsurf->
output_surface);
2094 if (nv_status != NV_ENC_SUCCESS) {
2095 res =
nvenc_print_error(avctx, nv_status,
"Failed unlocking bitstream buffer, expect the gates of mordor to open");
2101 ctx->registered_frames[tmpoutsurf->
reg_idx].mapped -= 1;
2102 if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped == 0) {
2103 nv_status = p_nvenc->nvEncUnmapInputResource(
ctx->nvencoder,
ctx->registered_frames[tmpoutsurf->
reg_idx].in_map.mappedResource);
2104 if (nv_status != NV_ENC_SUCCESS) {
2108 }
else if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped < 0) {
2118 switch (lock_params.pictureType) {
2119 case NV_ENC_PIC_TYPE_IDR:
2121 case NV_ENC_PIC_TYPE_I:
2124 case NV_ENC_PIC_TYPE_P:
2127 case NV_ENC_PIC_TYPE_B:
2130 case NV_ENC_PIC_TYPE_BI:
2134 av_log(avctx,
AV_LOG_ERROR,
"Unknown picture type encountered, expect the output to be broken.\n");
2135 av_log(avctx,
AV_LOG_ERROR,
"Please report this error and include as much information on how to reproduce it as possible.\n");
2163 int nb_ready, nb_pending;
2168 return nb_ready > 0;
2169 return (nb_ready > 0) && (nb_ready + nb_pending >=
ctx->async_depth);
2179 void *a53_data =
NULL;
2180 size_t a53_size = 0;
2188 &
ctx->sei_data_size,
2189 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2196 ctx->sei_data[sei_count].payloadSize = (uint32_t)a53_size;
2197 ctx->sei_data[sei_count].payloadType = 4;
2198 ctx->sei_data[sei_count].payload = (uint8_t*)a53_data;
2205 void *tc_data =
NULL;
2214 &
ctx->sei_data_size,
2215 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2222 ctx->sei_data[sei_count].payloadSize = (uint32_t)tc_size;
2224 ctx->sei_data[sei_count].payload = (uint8_t*)tc_data;
2233 for (
i = 0;
i <
frame->nb_side_data;
i++) {
2241 &
ctx->sei_data_size,
2242 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2248 ctx->sei_data[sei_count].payloadSize = side_data->
size;
2252 if (!
ctx->sei_data[sei_count].payload) {
2264 for (
i = 0;
i < sei_count;
i++)
2273 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
2276 NV_ENC_RECONFIGURE_PARAMS params = { 0 };
2277 int needs_reconfig = 0;
2278 int needs_encode_config = 0;
2279 int reconfig_bitrate = 0, reconfig_dar = 0;
2282 params.version = NV_ENC_RECONFIGURE_PARAMS_VER;
2283 params.reInitEncodeParams =
ctx->init_encode_params;
2286 if (dw !=
ctx->init_encode_params.darWidth || dh !=
ctx->init_encode_params.darHeight) {
2288 "aspect ratio change (DAR): %d:%d -> %d:%d\n",
2289 ctx->init_encode_params.darWidth,
2290 ctx->init_encode_params.darHeight, dw, dh);
2292 params.reInitEncodeParams.darHeight = dh;
2293 params.reInitEncodeParams.darWidth = dw;
2299 if (
ctx->rc != NV_ENC_PARAMS_RC_CONSTQP &&
ctx->support_dyn_bitrate) {
2300 if (avctx->
bit_rate > 0 && params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate != avctx->
bit_rate) {
2302 "avg bitrate change: %d -> %d\n",
2303 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate,
2306 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate = avctx->
bit_rate;
2307 reconfig_bitrate = 1;
2312 "max bitrate change: %d -> %d\n",
2313 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate,
2316 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate = avctx->
rc_max_rate;
2317 reconfig_bitrate = 1;
2322 "vbv buffer size change: %d -> %d\n",
2323 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize,
2326 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize = avctx->
rc_buffer_size;
2327 reconfig_bitrate = 1;
2330 if (reconfig_bitrate) {
2331 params.resetEncoder = 1;
2332 params.forceIDR = 1;
2334 needs_encode_config = 1;
2339 if (!needs_encode_config)
2340 params.reInitEncodeParams.encodeConfig =
NULL;
2342 if (needs_reconfig) {
2343 ret = p_nvenc->nvEncReconfigureEncoder(
ctx->nvencoder, ¶ms);
2344 if (
ret != NV_ENC_SUCCESS) {
2348 ctx->init_encode_params.darHeight = dh;
2349 ctx->init_encode_params.darWidth = dw;
2352 if (reconfig_bitrate) {
2353 ctx->encode_config.rcParams.averageBitRate = params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate;
2354 ctx->encode_config.rcParams.maxBitRate = params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate;
2355 ctx->encode_config.rcParams.vbvBufferSize = params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize;
2364 NVENCSTATUS nv_status;
2372 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2374 NV_ENC_PIC_PARAMS pic_params = { 0 };
2375 pic_params.version = NV_ENC_PIC_PARAMS_VER;
2377 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
2401 pic_params.bufferFmt = in_surf->
format;
2402 pic_params.inputWidth = in_surf->
width;
2403 pic_params.inputHeight = in_surf->
height;
2404 pic_params.inputPitch = in_surf->
pitch;
2408 if (
frame->top_field_first)
2409 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
2411 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
2413 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
2417 pic_params.encodePicFlags =
2418 ctx->forced_idr ? NV_ENC_PIC_FLAG_FORCEIDR : NV_ENC_PIC_FLAG_FORCEINTRA;
2420 pic_params.encodePicFlags = 0;
2423 pic_params.inputTimeStamp =
frame->pts;
2425 if (
ctx->extra_sei) {
2434 pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
2441 nv_status = p_nvenc->nvEncEncodePicture(
ctx->nvencoder, &pic_params);
2443 for (
i = 0;
i < sei_count;
i++)
2450 if (nv_status != NV_ENC_SUCCESS &&
2451 nv_status != NV_ENC_ERR_NEED_MORE_INPUT)
2460 if (nv_status == NV_ENC_SUCCESS) {
2461 while (
av_fifo_read(
ctx->output_surface_queue, &tmp_out_surf, 1) >= 0)
2477 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
2480 if (!
frame->buf[0]) {