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
48 #ifndef NVENC_NO_DEPRECATED_RC
49 #define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR || \
50 rc == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ || \
51 rc == NV_ENC_PARAMS_RC_CBR_HQ)
53 #define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR)
88 #define IS_10BIT(pix_fmt) (pix_fmt == AV_PIX_FMT_P010 || \
89 pix_fmt == AV_PIX_FMT_P016 || \
90 pix_fmt == AV_PIX_FMT_YUV444P16 || \
91 pix_fmt == AV_PIX_FMT_X2RGB10 || \
92 pix_fmt == AV_PIX_FMT_X2BGR10 || \
93 pix_fmt == AV_PIX_FMT_GBRP16)
95 #define IS_RGB(pix_fmt) (pix_fmt == AV_PIX_FMT_0RGB32 || \
96 pix_fmt == AV_PIX_FMT_RGB32 || \
97 pix_fmt == AV_PIX_FMT_0BGR32 || \
98 pix_fmt == AV_PIX_FMT_BGR32 || \
99 pix_fmt == AV_PIX_FMT_X2RGB10 || \
100 pix_fmt == AV_PIX_FMT_X2BGR10)
102 #define IS_YUV444(pix_fmt) (pix_fmt == AV_PIX_FMT_YUV444P || \
103 pix_fmt == AV_PIX_FMT_YUV444P16 || \
104 pix_fmt == AV_PIX_FMT_GBRP || \
105 pix_fmt == AV_PIX_FMT_GBRP16 || \
106 (ctx->rgb_mode == NVENC_RGB_MODE_444 && IS_RGB(pix_fmt)))
108 #define IS_GBRP(pix_fmt) (pix_fmt == AV_PIX_FMT_GBRP || \
109 pix_fmt == AV_PIX_FMT_GBRP16)
111 static const struct {
116 { NV_ENC_SUCCESS, 0,
"success" },
117 { NV_ENC_ERR_NO_ENCODE_DEVICE,
AVERROR(ENOENT),
"no encode device" },
118 { NV_ENC_ERR_UNSUPPORTED_DEVICE,
AVERROR(ENOSYS),
"unsupported device" },
119 { NV_ENC_ERR_INVALID_ENCODERDEVICE,
AVERROR(EINVAL),
"invalid encoder device" },
120 { NV_ENC_ERR_INVALID_DEVICE,
AVERROR(EINVAL),
"invalid device" },
121 { NV_ENC_ERR_DEVICE_NOT_EXIST,
AVERROR(EIO),
"device does not exist" },
122 { NV_ENC_ERR_INVALID_PTR,
AVERROR(EFAULT),
"invalid ptr" },
123 { NV_ENC_ERR_INVALID_EVENT,
AVERROR(EINVAL),
"invalid event" },
124 { NV_ENC_ERR_INVALID_PARAM,
AVERROR(EINVAL),
"invalid param" },
125 { NV_ENC_ERR_INVALID_CALL,
AVERROR(EINVAL),
"invalid call" },
126 { NV_ENC_ERR_OUT_OF_MEMORY,
AVERROR(ENOMEM),
"out of memory" },
127 { NV_ENC_ERR_ENCODER_NOT_INITIALIZED,
AVERROR(EINVAL),
"encoder not initialized" },
128 { NV_ENC_ERR_UNSUPPORTED_PARAM,
AVERROR(ENOSYS),
"unsupported param" },
129 { NV_ENC_ERR_LOCK_BUSY,
AVERROR(EAGAIN),
"lock busy" },
131 { NV_ENC_ERR_INVALID_VERSION,
AVERROR(EINVAL),
"invalid version" },
132 { NV_ENC_ERR_MAP_FAILED,
AVERROR(EIO),
"map failed" },
133 { NV_ENC_ERR_NEED_MORE_INPUT,
AVERROR(EAGAIN),
"need more input" },
134 { NV_ENC_ERR_ENCODER_BUSY,
AVERROR(EAGAIN),
"encoder busy" },
135 { NV_ENC_ERR_EVENT_NOT_REGISTERD,
AVERROR(EBADF),
"event not registered" },
137 { NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY,
AVERROR(EINVAL),
"incompatible client key" },
138 { NV_ENC_ERR_UNIMPLEMENTED,
AVERROR(ENOSYS),
"unimplemented" },
139 { NV_ENC_ERR_RESOURCE_REGISTER_FAILED,
AVERROR(EIO),
"resource register failed" },
140 { NV_ENC_ERR_RESOURCE_NOT_REGISTERED,
AVERROR(EBADF),
"resource not registered" },
141 { NV_ENC_ERR_RESOURCE_NOT_MAPPED,
AVERROR(EBADF),
"resource not mapped" },
155 *
desc =
"unknown error";
160 const char *error_string)
163 const char *details =
"(no details)";
166 #ifdef NVENC_HAVE_GETLASTERRORSTRING
168 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
170 if (p_nvenc &&
ctx->nvencoder)
171 details = p_nvenc->nvEncGetLastErrorString(
ctx->nvencoder);
184 #define PRESET_ALIAS(alias, name, ...) \
185 [PRESET_ ## alias] = { NV_ENC_PRESET_ ## name ## _GUID, __VA_ARGS__ }
187 #define PRESET(name, ...) PRESET_ALIAS(name, name, __VA_ARGS__)
192 #ifdef NVENC_HAVE_NEW_PRESETS
231 ctx->init_encode_params.presetGUID = t->
guid;
234 #ifdef NVENC_HAVE_NEW_PRESETS
235 if (
ctx->tuning_info == NV_ENC_TUNING_INFO_LOSSLESS)
245 #if NVENCAPI_CHECK_VERSION(12, 1)
246 const char *minver =
"(unknown)";
247 #elif NVENCAPI_CHECK_VERSION(12, 0)
248 # if defined(_WIN32) || defined(__CYGWIN__)
249 const char *minver =
"522.25";
251 const char *minver =
"520.56.06";
253 #elif NVENCAPI_CHECK_VERSION(11, 1)
254 # if defined(_WIN32) || defined(__CYGWIN__)
255 const char *minver =
"471.41";
257 const char *minver =
"470.57.02";
259 #elif NVENCAPI_CHECK_VERSION(11, 0)
260 # if defined(_WIN32) || defined(__CYGWIN__)
261 const char *minver =
"456.71";
263 const char *minver =
"455.28";
265 #elif NVENCAPI_CHECK_VERSION(10, 0)
266 # if defined(_WIN32) || defined(__CYGWIN__)
267 const char *minver =
"450.51";
269 const char *minver =
"445.87";
271 #elif NVENCAPI_CHECK_VERSION(9, 1)
272 # if defined(_WIN32) || defined(__CYGWIN__)
273 const char *minver =
"436.15";
275 const char *minver =
"435.21";
277 #elif NVENCAPI_CHECK_VERSION(9, 0)
278 # if defined(_WIN32) || defined(__CYGWIN__)
279 const char *minver =
"418.81";
281 const char *minver =
"418.30";
283 #elif NVENCAPI_CHECK_VERSION(8, 2)
284 # if defined(_WIN32) || defined(__CYGWIN__)
285 const char *minver =
"397.93";
287 const char *minver =
"396.24";
289 #elif NVENCAPI_CHECK_VERSION(8, 1)
290 # if defined(_WIN32) || defined(__CYGWIN__)
291 const char *minver =
"390.77";
293 const char *minver =
"390.25";
296 # if defined(_WIN32) || defined(__CYGWIN__)
297 const char *minver =
"378.66";
299 const char *minver =
"378.13";
302 av_log(avctx,
level,
"The minimum required Nvidia driver for nvenc is %s or newer\n", minver);
310 uint32_t nvenc_max_ver;
313 ret = cuda_load_functions(&dl_fn->
cuda_dl, avctx);
317 ret = nvenc_load_functions(&dl_fn->
nvenc_dl, avctx);
323 err = dl_fn->
nvenc_dl->NvEncodeAPIGetMaxSupportedVersion(&nvenc_max_ver);
324 if (err != NV_ENC_SUCCESS)
329 if ((NVENCAPI_MAJOR_VERSION << 4 | NVENCAPI_MINOR_VERSION) > nvenc_max_ver) {
330 av_log(avctx,
AV_LOG_ERROR,
"Driver does not support the required nvenc API version. "
331 "Required: %d.%d Found: %d.%d\n",
332 NVENCAPI_MAJOR_VERSION, NVENCAPI_MINOR_VERSION,
333 nvenc_max_ver >> 4, nvenc_max_ver & 0
xf);
338 dl_fn->
nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
341 if (err != NV_ENC_SUCCESS)
354 if (
ctx->d3d11_device)
366 if (
ctx->d3d11_device)
374 NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = { 0 };
376 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
379 params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
380 params.apiVersion = NVENCAPI_VERSION;
381 if (
ctx->d3d11_device) {
382 params.device =
ctx->d3d11_device;
383 params.deviceType = NV_ENC_DEVICE_TYPE_DIRECTX;
385 params.device =
ctx->cu_context;
386 params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
389 ret = p_nvenc->nvEncOpenEncodeSessionEx(¶ms, &
ctx->nvencoder);
390 if (
ret != NV_ENC_SUCCESS) {
401 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
402 int i,
ret, count = 0;
405 ret = p_nvenc->nvEncGetEncodeGUIDCount(
ctx->nvencoder, &count);
407 if (
ret != NV_ENC_SUCCESS || !count)
414 ret = p_nvenc->nvEncGetEncodeGUIDs(
ctx->nvencoder, guids, count, &count);
415 if (
ret != NV_ENC_SUCCESS) {
421 for (
i = 0;
i < count;
i++) {
422 if (!memcmp(&guids[
i], &
ctx->init_encode_params.encodeGUID,
sizeof(*guids))) {
437 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
438 NV_ENC_CAPS_PARAM params = { 0 };
441 params.version = NV_ENC_CAPS_PARAM_VER;
442 params.capsToQuery = cap;
444 ret = p_nvenc->nvEncGetEncodeCaps(
ctx->nvencoder,
ctx->init_encode_params.encodeGUID, ¶ms, &
val);
446 if (
ret == NV_ENC_SUCCESS)
475 if (ret < avctx->
width) {
482 if (ret < avctx->
height) {
489 if (ret < avctx->max_b_frames) {
499 "Interlaced encoding is not supported. Supported level: %d\n",
511 if (
ctx->rc_lookahead > 0 &&
ret <= 0) {
517 if (
ctx->temporal_aq > 0 &&
ret <= 0) {
523 if (
ctx->weighted_pred > 0 &&
ret <= 0) {
529 if (
ctx->coder == NV_ENC_H264_ENTROPY_CODING_MODE_CABAC &&
ret <= 0) {
534 #ifdef NVENC_HAVE_BFRAME_REF_MODE
535 tmp = (
ctx->b_ref_mode >= 0) ?
ctx->b_ref_mode : NV_ENC_BFRAME_REF_MODE_DISABLED;
537 if (
tmp == NV_ENC_BFRAME_REF_MODE_EACH &&
ret != 1 &&
ret != 3) {
540 }
else if (
tmp != NV_ENC_BFRAME_REF_MODE_DISABLED &&
ret == 0) {
545 tmp = (
ctx->b_ref_mode >= 0) ?
ctx->b_ref_mode : 0;
552 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
554 if(avctx->
refs != NV_ENC_NUM_REF_FRAMES_AUTOSELECT &&
ret <= 0) {
559 if(avctx->
refs != 0) {
565 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
567 if(
ctx->single_slice_intra_refresh &&
ret <= 0) {
572 if(
ctx->single_slice_intra_refresh) {
579 if((
ctx->intra_refresh ||
ctx->single_slice_intra_refresh) &&
ret <= 0) {
584 #ifndef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
592 if(
ctx->constrained_encoding &&
ret <= 0) {
597 ctx->support_dyn_bitrate =
nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE);
606 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
607 char name[128] = { 0};
608 int major, minor,
ret;
627 av_log(avctx, loglevel,
"[ GPU #%d - < %s > has Compute SM %d.%d ]\n", idx,
name, major, minor);
628 if (((major << 4) | minor) <
NVENC_CAP) {
629 av_log(avctx, loglevel,
"does not support NVENC\n");
640 ctx->cu_context =
ctx->cu_context_internal;
652 av_log(avctx, loglevel,
"supports NVENC\n");
663 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
671 ctx->cu_context_internal =
NULL;
684 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_H264_GUID;
687 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_HEVC_GUID;
689 #if CONFIG_AV1_NVENC_ENCODER
691 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_AV1_GUID;
701 av_log(avctx,
AV_LOG_WARNING,
"The selected preset is deprecated. Use p1 to p7 + -tune or fast/medium/slow.\n");
725 cuda_device_hwctx = hwdev_ctx->
hwctx;
728 d3d11_device_hwctx = hwdev_ctx->
hwctx;
736 if (cuda_device_hwctx) {
738 ctx->cu_stream = cuda_device_hwctx->
stream;
741 else if (d3d11_device_hwctx) {
742 ctx->d3d11_device = d3d11_device_hwctx->
device;
743 ID3D11Device_AddRef(
ctx->d3d11_device);
753 av_log(avctx,
AV_LOG_FATAL,
"Provided device doesn't support required NVENC features\n");
757 int i, nb_devices = 0;
773 for (
i = 0;
i < nb_devices; ++
i) {
786 av_log(avctx,
AV_LOG_FATAL,
"Requested GPU %d, but only %d GPUs are available!\n",
ctx->device, nb_devices);
796 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
797 #if CONFIG_AV1_NVENC_ENCODER
803 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
805 if (
ctx->init_qp_p >= 0) {
806 rc->constQP.qpInterP =
ctx->init_qp_p;
807 if (
ctx->init_qp_i >= 0 &&
ctx->init_qp_b >= 0) {
808 rc->constQP.qpIntra =
ctx->init_qp_i;
809 rc->constQP.qpInterB =
ctx->init_qp_b;
813 rc->constQP.qpInterB =
av_clip(
816 rc->constQP.qpIntra = rc->constQP.qpInterP;
817 rc->constQP.qpInterB = rc->constQP.qpInterP;
819 }
else if (
ctx->cqp >= 0) {
820 rc->constQP.qpInterP = rc->constQP.qpInterB = rc->constQP.qpIntra =
ctx->cqp;
834 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
836 #if CONFIG_AV1_NVENC_ENCODER
842 if (avctx->
qmin >= 0 && avctx->
qmax >= 0) {
846 rc->minQP.qpInterB = avctx->
qmin;
847 rc->minQP.qpInterP = avctx->
qmin;
848 rc->minQP.qpIntra = avctx->
qmin;
850 rc->maxQP.qpInterB = avctx->
qmax;
851 rc->maxQP.qpInterP = avctx->
qmax;
852 rc->maxQP.qpIntra = avctx->
qmax;
854 qp_inter_p = (avctx->
qmax + 3 * avctx->
qmin) / 4;
855 }
else if (avctx->
qmin >= 0) {
858 rc->minQP.qpInterB = avctx->
qmin;
859 rc->minQP.qpInterP = avctx->
qmin;
860 rc->minQP.qpIntra = avctx->
qmin;
862 qp_inter_p = avctx->
qmin;
867 rc->enableInitialRCQP = 1;
869 if (
ctx->init_qp_p < 0) {
870 rc->initialRCQP.qpInterP = qp_inter_p;
872 rc->initialRCQP.qpInterP =
ctx->init_qp_p;
875 if (
ctx->init_qp_i < 0) {
877 rc->initialRCQP.qpIntra =
av_clip(
880 rc->initialRCQP.qpIntra = rc->initialRCQP.qpInterP;
883 rc->initialRCQP.qpIntra =
ctx->init_qp_i;
886 if (
ctx->init_qp_b < 0) {
888 rc->initialRCQP.qpInterB =
av_clip(
891 rc->initialRCQP.qpInterB = rc->initialRCQP.qpInterP;
894 rc->initialRCQP.qpInterB =
ctx->init_qp_b;
901 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
903 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
904 rc->constQP.qpInterB = 0;
905 rc->constQP.qpInterP = 0;
906 rc->constQP.qpIntra = 0;
915 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
918 case NV_ENC_PARAMS_RC_CONSTQP:
921 #ifndef NVENC_NO_DEPRECATED_RC
922 case NV_ENC_PARAMS_RC_VBR_MINQP:
923 if (avctx->
qmin < 0) {
925 "The variable bitrate rate-control requires "
926 "the 'qmin' option set.\n");
931 case NV_ENC_PARAMS_RC_VBR_HQ:
933 case NV_ENC_PARAMS_RC_VBR:
936 case NV_ENC_PARAMS_RC_CBR:
937 #ifndef NVENC_NO_DEPRECATED_RC
938 case NV_ENC_PARAMS_RC_CBR_HQ:
939 case NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ:
944 rc->rateControlMode =
ctx->rc;
953 int nb_surfaces =
FFMAX(4,
ctx->encode_config.frameIntervalP * 2 * 2);
956 if (
ctx->rc_lookahead > 0) {
959 nb_surfaces =
FFMAX(1,
FFMAX(nb_surfaces,
ctx->rc_lookahead +
ctx->encode_config.frameIntervalP + 1 + 4));
960 if (nb_surfaces >
ctx->nb_surfaces &&
ctx->nb_surfaces > 0)
963 "Defined rc_lookahead requires more surfaces, "
964 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
966 ctx->nb_surfaces =
FFMAX(nb_surfaces,
ctx->nb_surfaces);
968 if (
ctx->encode_config.frameIntervalP > 1 &&
ctx->nb_surfaces < nb_surfaces &&
ctx->nb_surfaces > 0)
971 "Defined b-frame requires more surfaces, "
972 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
973 ctx->nb_surfaces =
FFMAX(
ctx->nb_surfaces, nb_surfaces);
975 else if (
ctx->nb_surfaces <= 0)
976 ctx->nb_surfaces = nb_surfaces;
985 ctx->frame_data_array_nb =
FFMAX(
ctx->nb_surfaces,
ctx->nb_surfaces +
ctx->encode_config.frameIntervalP - 1);
995 av_log(avctx,
AV_LOG_WARNING,
"Using global_quality with nvenc is deprecated. Use qp instead.\n");
1001 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate;
1002 }
else if (
ctx->encode_config.rcParams.averageBitRate > 0) {
1003 ctx->encode_config.rcParams.maxBitRate =
ctx->encode_config.rcParams.averageBitRate;
1009 #ifdef NVENC_HAVE_MULTIPASS
1010 ctx->encode_config.rcParams.multiPass =
ctx->multipass;
1013 ctx->encode_config.rcParams.multiPass = NV_ENC_MULTI_PASS_DISABLED;
1015 ctx->encode_config.rcParams.multiPass = NV_ENC_TWO_PASS_FULL_RESOLUTION;
1019 ctx->rc = NV_ENC_PARAMS_RC_CBR;
1020 }
else if (
ctx->cqp >= 0) {
1021 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
1022 }
else if (
ctx->quality >= 0.0f) {
1023 ctx->rc = NV_ENC_PARAMS_RC_VBR;
1033 if (
ctx->twopass < 0)
1038 ctx->rc = NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ;
1040 ctx->rc = NV_ENC_PARAMS_RC_CBR;
1042 }
else if (
ctx->cqp >= 0) {
1043 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
1044 }
else if (
ctx->twopass) {
1045 ctx->rc = NV_ENC_PARAMS_RC_VBR_HQ;
1046 }
else if (avctx->
qmin >= 0 && avctx->
qmax >= 0) {
1047 ctx->rc = NV_ENC_PARAMS_RC_VBR_MINQP;
1059 #ifdef NVENC_HAVE_QP_CHROMA_OFFSETS
1060 ctx->encode_config.rcParams.cbQPIndexOffset =
ctx->qp_cb_offset;
1061 ctx->encode_config.rcParams.crQPIndexOffset =
ctx->qp_cr_offset;
1063 if (
ctx->qp_cb_offset ||
ctx->qp_cr_offset)
1064 av_log(avctx,
AV_LOG_WARNING,
"Failed setting QP CB/CR offsets, SDK 11.1 or greater required at compile time.\n");
1067 #ifdef NVENC_HAVE_LDKFS
1069 ctx->encode_config.rcParams.lowDelayKeyFrameScale =
ctx->ldkfs;
1074 }
else if (
ctx->rc >= 0) {
1077 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
1083 }
else if (
ctx->encode_config.rcParams.averageBitRate > 0) {
1084 avctx->
rc_buffer_size =
ctx->encode_config.rcParams.vbvBufferSize = 2 *
ctx->encode_config.rcParams.averageBitRate;
1088 ctx->encode_config.rcParams.enableAQ = 1;
1089 ctx->encode_config.rcParams.aqStrength =
ctx->aq_strength;
1093 if (
ctx->temporal_aq) {
1094 ctx->encode_config.rcParams.enableTemporalAQ = 1;
1098 if (
ctx->rc_lookahead > 0) {
1099 int lkd_bound =
FFMIN(
ctx->nb_surfaces,
ctx->async_depth) -
1100 ctx->encode_config.frameIntervalP - 4;
1102 if (lkd_bound < 0) {
1103 ctx->encode_config.rcParams.enableLookahead = 0;
1105 "Lookahead not enabled. Increase buffer delay (-delay).\n");
1107 ctx->encode_config.rcParams.enableLookahead = 1;
1108 ctx->encode_config.rcParams.lookaheadDepth =
av_clip(
ctx->rc_lookahead, 0, lkd_bound);
1109 ctx->encode_config.rcParams.disableIadapt =
ctx->no_scenecut;
1110 ctx->encode_config.rcParams.disableBadapt = !
ctx->b_adapt;
1112 "Lookahead enabled: depth %d, scenecut %s, B-adapt %s.\n",
1113 ctx->encode_config.rcParams.lookaheadDepth,
1114 ctx->encode_config.rcParams.disableIadapt ?
"disabled" :
"enabled",
1115 ctx->encode_config.rcParams.disableBadapt ?
"disabled" :
"enabled");
1116 if (
ctx->encode_config.rcParams.lookaheadDepth <
ctx->rc_lookahead)
1117 av_log(avctx,
AV_LOG_WARNING,
"Clipping lookahead depth to %d (from %d) due to lack of surfaces/delay",
1118 ctx->encode_config.rcParams.lookaheadDepth,
ctx->rc_lookahead);
1122 if (
ctx->strict_gop) {
1123 ctx->encode_config.rcParams.strictGOPTarget = 1;
1128 ctx->encode_config.rcParams.enableNonRefP = 1;
1130 if (
ctx->zerolatency)
1131 ctx->encode_config.rcParams.zeroReorderDelay = 1;
1135 int tmp_quality = (
int)(
ctx->quality * 256.0f);
1136 ctx->encode_config.rcParams.targetQuality = (uint8_t)(tmp_quality >> 8);
1137 ctx->encode_config.rcParams.targetQualityLSB = (uint8_t)(tmp_quality & 0xff);
1142 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate = 0;
1151 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1152 NV_ENC_CONFIG_H264 *h264 = &cc->encodeCodecConfig.h264Config;
1153 NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui = &h264->h264VUIParameters;
1160 vui->transferCharacteristics = avctx->
color_trc;
1161 vui->videoFullRangeFlag = 0;
1165 vui->transferCharacteristics = avctx->
color_trc;
1170 vui->colourDescriptionPresentFlag =
1171 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1173 vui->videoSignalTypePresentFlag =
1174 (vui->colourDescriptionPresentFlag
1175 || vui->videoFormat != 5
1176 || vui->videoFullRangeFlag != 0);
1178 if (
ctx->max_slice_size > 0) {
1179 h264->sliceMode = 1;
1180 h264->sliceModeData =
ctx->max_slice_size;
1182 h264->sliceMode = 3;
1183 h264->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1186 if (
ctx->intra_refresh) {
1187 h264->enableIntraRefresh = 1;
1188 h264->intraRefreshPeriod = cc->gopLength;
1189 h264->intraRefreshCnt = cc->gopLength - 1;
1190 cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1191 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1192 h264->singleSliceIntraRefresh =
ctx->single_slice_intra_refresh;
1196 if (
ctx->constrained_encoding)
1197 h264->enableConstrainedEncoding = 1;
1201 h264->outputAUD =
ctx->aud;
1203 if (
ctx->dpb_size >= 0) {
1205 h264->maxNumRefFrames =
ctx->dpb_size;
1208 h264->idrPeriod = cc->gopLength;
1210 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1211 h264->outputBufferingPeriodSEI = 1;
1214 h264->outputPictureTimingSEI = 1;
1216 #ifndef NVENC_NO_DEPRECATED_RC
1217 if (cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ ||
1218 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_HQ ||
1219 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_VBR_HQ) {
1220 h264->adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
1221 h264->fmoMode = NV_ENC_H264_FMO_DISABLE;
1226 h264->qpPrimeYZeroTransformBypassFlag = 1;
1228 switch(
ctx->profile) {
1230 cc->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
1234 cc->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
1238 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
1242 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1250 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1256 h264->level =
ctx->level;
1258 #ifdef NVENC_HAVE_NEW_BIT_DEPTH_API
1259 h264->inputBitDepth = h264->outputBitDepth =
1260 IS_10BIT(
ctx->data_pix_fmt) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1263 if (
ctx->coder >= 0)
1264 h264->entropyCodingMode =
ctx->coder;
1266 #ifdef NVENC_HAVE_BFRAME_REF_MODE
1267 if (
ctx->b_ref_mode >= 0)
1268 h264->useBFramesAsRef =
ctx->b_ref_mode;
1271 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1272 h264->numRefL0 = avctx->
refs;
1273 h264->numRefL1 = avctx->
refs;
1282 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1283 NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig;
1284 NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui = &hevc->hevcVUIParameters;
1291 vui->transferCharacteristics = avctx->
color_trc;
1292 vui->videoFullRangeFlag = 0;
1296 vui->transferCharacteristics = avctx->
color_trc;
1301 vui->colourDescriptionPresentFlag =
1302 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1304 vui->videoSignalTypePresentFlag =
1305 (vui->colourDescriptionPresentFlag
1306 || vui->videoFormat != 5
1307 || vui->videoFullRangeFlag != 0);
1309 if (
ctx->max_slice_size > 0) {
1310 hevc->sliceMode = 1;
1311 hevc->sliceModeData =
ctx->max_slice_size;
1313 hevc->sliceMode = 3;
1314 hevc->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1317 if (
ctx->intra_refresh) {
1318 hevc->enableIntraRefresh = 1;
1319 hevc->intraRefreshPeriod = cc->gopLength;
1320 hevc->intraRefreshCnt = cc->gopLength - 1;
1321 cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1322 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1323 hevc->singleSliceIntraRefresh =
ctx->single_slice_intra_refresh;
1327 #ifdef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
1328 if (
ctx->constrained_encoding)
1329 hevc->enableConstrainedEncoding = 1;
1334 hevc->outputAUD =
ctx->aud;
1336 if (
ctx->dpb_size >= 0) {
1338 hevc->maxNumRefFramesInDPB =
ctx->dpb_size;
1341 hevc->idrPeriod = cc->gopLength;
1343 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1344 hevc->outputBufferingPeriodSEI = 1;
1347 hevc->outputPictureTimingSEI = 1;
1349 switch (
ctx->profile) {
1351 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
1355 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1359 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1366 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1372 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1376 hevc->chromaFormatIDC =
IS_YUV444(
ctx->data_pix_fmt) ? 3 : 1;
1378 #ifdef NVENC_HAVE_NEW_BIT_DEPTH_API
1379 hevc->inputBitDepth = hevc->outputBitDepth =
1380 IS_10BIT(
ctx->data_pix_fmt) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1382 hevc->pixelBitDepthMinus8 =
IS_10BIT(
ctx->data_pix_fmt) ? 2 : 0;
1385 hevc->level =
ctx->level;
1387 hevc->tier =
ctx->tier;
1389 #ifdef NVENC_HAVE_HEVC_BFRAME_REF_MODE
1390 if (
ctx->b_ref_mode >= 0)
1391 hevc->useBFramesAsRef =
ctx->b_ref_mode;
1394 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1395 hevc->numRefL0 = avctx->
refs;
1396 hevc->numRefL1 = avctx->
refs;
1402 #if CONFIG_AV1_NVENC_ENCODER
1406 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1407 NV_ENC_CONFIG_AV1 *av1 = &cc->encodeCodecConfig.av1Config;
1414 av1->transferCharacteristics = avctx->
color_trc;
1415 av1->colorRange = 0;
1419 av1->transferCharacteristics = avctx->
color_trc;
1425 av_log(avctx,
AV_LOG_ERROR,
"AV1 High Profile not supported, required for 4:4:4 encoding\n");
1428 cc->profileGUID = NV_ENC_AV1_PROFILE_MAIN_GUID;
1432 if (
ctx->dpb_size >= 0) {
1434 av1->maxNumRefFramesInDPB =
ctx->dpb_size;
1437 if (
ctx->intra_refresh) {
1438 av1->enableIntraRefresh = 1;
1439 av1->intraRefreshPeriod = cc->gopLength;
1440 av1->intraRefreshCnt = cc->gopLength - 1;
1441 cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1444 av1->idrPeriod = cc->gopLength;
1446 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1447 av1->enableBitstreamPadding = 1;
1450 if (
ctx->tile_cols >= 0)
1451 av1->numTileColumns =
ctx->tile_cols;
1452 if (
ctx->tile_rows >= 0)
1453 av1->numTileRows =
ctx->tile_rows;
1455 av1->outputAnnexBFormat = 0;
1457 av1->level =
ctx->level;
1458 av1->tier =
ctx->tier;
1460 av1->enableTimingInfo =
ctx->timing_info;
1463 av1->disableSeqHdr = 0;
1464 av1->repeatSeqHdr = 1;
1466 av1->chromaFormatIDC =
IS_YUV444(
ctx->data_pix_fmt) ? 3 : 1;
1468 #ifdef NVENC_HAVE_NEW_BIT_DEPTH_API
1469 av1->inputBitDepth =
IS_10BIT(
ctx->data_pix_fmt) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1470 av1->outputBitDepth = (
IS_10BIT(
ctx->data_pix_fmt) ||
ctx->highbitdepth) ? NV_ENC_BIT_DEPTH_10 : NV_ENC_BIT_DEPTH_8;
1472 av1->inputPixelBitDepthMinus8 =
IS_10BIT(
ctx->data_pix_fmt) ? 2 : 0;
1473 av1->pixelBitDepthMinus8 = (
IS_10BIT(
ctx->data_pix_fmt) ||
ctx->highbitdepth) ? 2 : 0;
1476 if (
ctx->b_ref_mode >= 0)
1477 av1->useBFramesAsRef =
ctx->b_ref_mode;
1479 av1->numFwdRefs = avctx->
refs;
1480 av1->numBwdRefs = avctx->
refs;
1493 #if CONFIG_AV1_NVENC_ENCODER
1495 return nvenc_setup_av1_config(avctx);
1509 #if CONFIG_AV1_NVENC_ENCODER
1540 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1542 NV_ENC_PRESET_CONFIG preset_config = { 0 };
1543 NVENCSTATUS nv_status = NV_ENC_SUCCESS;
1548 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1549 ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
1551 ctx->init_encode_params.encodeHeight = avctx->
height;
1552 ctx->init_encode_params.encodeWidth = avctx->
width;
1554 ctx->init_encode_params.encodeConfig = &
ctx->encode_config;
1556 preset_config.version = NV_ENC_PRESET_CONFIG_VER;
1557 preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
1559 #ifdef NVENC_HAVE_NEW_PRESETS
1560 ctx->init_encode_params.tuningInfo =
ctx->tuning_info;
1563 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOSSLESS;
1565 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOW_LATENCY;
1567 nv_status = p_nvenc->nvEncGetEncodePresetConfigEx(
ctx->nvencoder,
1568 ctx->init_encode_params.encodeGUID,
1569 ctx->init_encode_params.presetGUID,
1570 ctx->init_encode_params.tuningInfo,
1573 nv_status = p_nvenc->nvEncGetEncodePresetConfig(
ctx->nvencoder,
1574 ctx->init_encode_params.encodeGUID,
1575 ctx->init_encode_params.presetGUID,
1578 if (nv_status != NV_ENC_SUCCESS)
1579 return nvenc_print_error(avctx, nv_status,
"Cannot get the preset configuration");
1581 memcpy(&
ctx->encode_config, &preset_config.presetCfg,
sizeof(
ctx->encode_config));
1583 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1586 ctx->init_encode_params.darHeight = dh;
1587 ctx->init_encode_params.darWidth = dw;
1596 #if FF_API_TICKS_PER_FRAME
1603 ctx->init_encode_params.enableEncodeAsync = 0;
1604 ctx->init_encode_params.enablePTD = 1;
1606 #ifdef NVENC_HAVE_NEW_PRESETS
1610 if (
ctx->rc_lookahead == 0 &&
ctx->encode_config.rcParams.enableLookahead)
1611 ctx->rc_lookahead =
ctx->encode_config.rcParams.lookaheadDepth;
1614 if (
ctx->weighted_pred == 1)
1615 ctx->init_encode_params.enableWeightedPrediction = 1;
1617 if (
ctx->bluray_compat) {
1626 ctx->level = NV_ENC_LEVEL_HEVC_51;
1627 ctx->tier = NV_ENC_TIER_HEVC_HIGH;
1636 ctx->encode_config.frameIntervalP = 0;
1637 ctx->encode_config.gopLength = 1;
1646 if(
ctx->single_slice_intra_refresh)
1647 ctx->intra_refresh = 1;
1654 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
1656 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
1667 nv_status = p_nvenc->nvEncInitializeEncoder(
ctx->nvencoder, &
ctx->init_encode_params);
1668 if (nv_status != NV_ENC_SUCCESS) {
1673 #ifdef NVENC_HAVE_CUSTREAM_PTR
1674 if (
ctx->cu_context) {
1675 nv_status = p_nvenc->nvEncSetIOCudaStreams(
ctx->nvencoder, &
ctx->cu_stream, &
ctx->cu_stream);
1676 if (nv_status != NV_ENC_SUCCESS) {
1687 if (
ctx->encode_config.frameIntervalP > 1)
1690 if (
ctx->encode_config.rcParams.averageBitRate > 0)
1691 avctx->
bit_rate =
ctx->encode_config.rcParams.averageBitRate;
1698 cpb_props->
buffer_size =
ctx->encode_config.rcParams.vbvBufferSize;
1707 return NV_ENC_BUFFER_FORMAT_YV12;
1709 return NV_ENC_BUFFER_FORMAT_NV12;
1712 return NV_ENC_BUFFER_FORMAT_YUV420_10BIT;
1715 return NV_ENC_BUFFER_FORMAT_YUV444;
1718 return NV_ENC_BUFFER_FORMAT_YUV444_10BIT;
1721 return NV_ENC_BUFFER_FORMAT_ARGB;
1724 return NV_ENC_BUFFER_FORMAT_ABGR;
1726 return NV_ENC_BUFFER_FORMAT_ARGB10;
1728 return NV_ENC_BUFFER_FORMAT_ABGR10;
1730 return NV_ENC_BUFFER_FORMAT_UNDEFINED;
1738 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1741 NVENCSTATUS nv_status;
1742 NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
1743 allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
1747 if (!
ctx->surfaces[idx].in_ref)
1750 NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
1753 if (
ctx->surfaces[idx].format == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
1759 allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
1760 allocSurf.width = avctx->
width;
1761 allocSurf.height = avctx->
height;
1762 allocSurf.bufferFmt =
ctx->surfaces[idx].format;
1764 nv_status = p_nvenc->nvEncCreateInputBuffer(
ctx->nvencoder, &allocSurf);
1765 if (nv_status != NV_ENC_SUCCESS) {
1769 ctx->surfaces[idx].input_surface = allocSurf.inputBuffer;
1770 ctx->surfaces[idx].width = allocSurf.width;
1771 ctx->surfaces[idx].height = allocSurf.height;
1774 nv_status = p_nvenc->nvEncCreateBitstreamBuffer(
ctx->nvencoder, &allocOut);
1775 if (nv_status != NV_ENC_SUCCESS) {
1778 p_nvenc->nvEncDestroyInputBuffer(
ctx->nvencoder,
ctx->surfaces[idx].input_surface);
1783 ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer;
1793 int i, res = 0, res2;
1799 ctx->frame_data_array =
av_calloc(
ctx->frame_data_array_nb,
sizeof(*
ctx->frame_data_array));
1800 if (!
ctx->frame_data_array)
1804 if (!
ctx->timestamp_list)
1808 if (!
ctx->unused_surface_queue)
1812 if (!
ctx->output_surface_queue)
1815 if (!
ctx->output_surface_ready_queue)
1822 for (
i = 0;
i <
ctx->nb_surfaces;
i++) {
1839 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1841 NVENCSTATUS nv_status;
1842 uint32_t outSize = 0;
1843 char tmpHeader[NV_MAX_SEQ_HDR_LEN];
1845 NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
1846 payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
1848 payload.spsppsBuffer = tmpHeader;
1849 payload.inBufferSize =
sizeof(tmpHeader);
1850 payload.outSPSPPSPayloadSize = &outSize;
1852 nv_status = p_nvenc->nvEncGetSequenceParams(
ctx->nvencoder, &payload);
1853 if (nv_status != NV_ENC_SUCCESS) {
1864 memcpy(avctx->
extradata, tmpHeader, outSize);
1873 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1877 if (
ctx->nvencoder) {
1878 NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER,
1879 .encodePicFlags = NV_ENC_PIC_FLAG_EOS };
1885 p_nvenc->nvEncEncodePicture(
ctx->nvencoder, ¶ms);
1893 if (
ctx->frame_data_array) {
1894 for (
i = 0;
i <
ctx->frame_data_array_nb;
i++)
1900 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
1901 if (
ctx->registered_frames[
i].mapped)
1902 p_nvenc->nvEncUnmapInputResource(
ctx->nvencoder,
ctx->registered_frames[
i].in_map.mappedResource);
1903 if (
ctx->registered_frames[
i].regptr)
1904 p_nvenc->nvEncUnregisterResource(
ctx->nvencoder,
ctx->registered_frames[
i].regptr);
1906 ctx->nb_registered_frames = 0;
1909 if (
ctx->surfaces) {
1910 for (
i = 0;
i <
ctx->nb_surfaces; ++
i) {
1912 p_nvenc->nvEncDestroyInputBuffer(
ctx->nvencoder,
ctx->surfaces[
i].input_surface);
1914 p_nvenc->nvEncDestroyBitstreamBuffer(
ctx->nvencoder,
ctx->surfaces[
i].output_surface);
1918 ctx->nb_surfaces = 0;
1924 if (
ctx->nvencoder) {
1925 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
1933 if (
ctx->cu_context_internal)
1935 ctx->cu_context =
ctx->cu_context_internal =
NULL;
1938 if (
ctx->d3d11_device) {
1939 ID3D11Device_Release(
ctx->d3d11_device);
1944 nvenc_free_functions(&dl_fn->
nvenc_dl);
1945 cuda_free_functions(&dl_fn->
cuda_dl);
1963 "hw_frames_ctx must be set when using GPU frames as input\n");
1969 "hw_frames_ctx must match the GPU frame type\n");
2018 NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params,
const AVFrame *
frame)
2020 int dst_linesize[4] = {
2021 lock_buffer_params->pitch,
2022 lock_buffer_params->pitch,
2023 lock_buffer_params->pitch,
2024 lock_buffer_params->pitch
2026 uint8_t *dst_data[4];
2030 dst_linesize[1] = dst_linesize[2] >>= 1;
2033 lock_buffer_params->bufferDataPtr, dst_linesize);
2038 FFSWAP(uint8_t*, dst_data[1], dst_data[2]);
2051 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2052 NVENCSTATUS nv_status;
2057 for (first_round = 1; first_round >= 0; first_round--) {
2058 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
2059 if (!
ctx->registered_frames[
i].mapped) {
2060 if (
ctx->registered_frames[
i].regptr) {
2063 nv_status = p_nvenc->nvEncUnregisterResource(
ctx->nvencoder,
ctx->registered_frames[
i].regptr);
2064 if (nv_status != NV_ENC_SUCCESS)
2065 return nvenc_print_error(avctx, nv_status,
"Failed unregistering unused input resource");
2066 ctx->registered_frames[
i].ptr =
NULL;
2067 ctx->registered_frames[
i].regptr =
NULL;
2074 return ctx->nb_registered_frames++;
2085 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2088 NV_ENC_REGISTER_RESOURCE reg = { 0 };
2091 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
2102 reg.version = NV_ENC_REGISTER_RESOURCE_VER;
2103 reg.width = frames_ctx->width;
2104 reg.height = frames_ctx->height;
2106 reg.resourceToRegister =
frame->
data[0];
2109 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR;
2112 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX;
2113 reg.subResourceIndex = (intptr_t)
frame->
data[1];
2117 if (reg.bufferFormat == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
2123 ret = p_nvenc->nvEncRegisterResource(
ctx->nvencoder, ®);
2124 if (
ret != NV_ENC_SUCCESS) {
2130 ctx->registered_frames[idx].ptr_index = reg.subResourceIndex;
2131 ctx->registered_frames[idx].regptr = reg.registeredResource;
2140 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2143 NVENCSTATUS nv_status;
2156 if (!
ctx->registered_frames[reg_idx].mapped) {
2157 ctx->registered_frames[reg_idx].in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER;
2158 ctx->registered_frames[reg_idx].in_map.registeredResource =
ctx->registered_frames[reg_idx].regptr;
2159 nv_status = p_nvenc->nvEncMapInputResource(
ctx->nvencoder, &
ctx->registered_frames[reg_idx].in_map);
2160 if (nv_status != NV_ENC_SUCCESS) {
2166 ctx->registered_frames[reg_idx].mapped += 1;
2168 nvenc_frame->
reg_idx = reg_idx;
2169 nvenc_frame->
input_surface =
ctx->registered_frames[reg_idx].in_map.mappedResource;
2170 nvenc_frame->
format =
ctx->registered_frames[reg_idx].in_map.mappedBufferFmt;
2175 NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
2177 lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
2180 nv_status = p_nvenc->nvEncLockInputBuffer(
ctx->nvencoder, &lockBufferParams);
2181 if (nv_status != NV_ENC_SUCCESS) {
2182 return nvenc_print_error(avctx, nv_status,
"Failed locking nvenc input buffer");
2185 nvenc_frame->
pitch = lockBufferParams.pitch;
2188 nv_status = p_nvenc->nvEncUnlockInputBuffer(
ctx->nvencoder, nvenc_frame->
input_surface);
2189 if (nv_status != NV_ENC_SUCCESS) {
2198 NV_ENC_PIC_PARAMS *params,
2199 NV_ENC_SEI_PAYLOAD *sei_data,
2206 params->codecPicParams.h264PicParams.sliceMode =
2207 ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
2208 params->codecPicParams.h264PicParams.sliceModeData =
2209 ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
2210 if (sei_count > 0) {
2211 params->codecPicParams.h264PicParams.seiPayloadArray = sei_data;
2212 params->codecPicParams.h264PicParams.seiPayloadArrayCnt = sei_count;
2217 params->codecPicParams.hevcPicParams.sliceMode =
2218 ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
2219 params->codecPicParams.hevcPicParams.sliceModeData =
2220 ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
2221 if (sei_count > 0) {
2222 params->codecPicParams.hevcPicParams.seiPayloadArray = sei_data;
2223 params->codecPicParams.hevcPicParams.seiPayloadArrayCnt = sei_count;
2227 #if CONFIG_AV1_NVENC_ENCODER
2229 params->codecPicParams.av1PicParams.numTileColumns =
2230 ctx->encode_config.encodeCodecConfig.av1Config.numTileColumns;
2231 params->codecPicParams.av1PicParams.numTileRows =
2232 ctx->encode_config.encodeCodecConfig.av1Config.numTileRows;
2233 if (sei_count > 0) {
2234 params->codecPicParams.av1PicParams.obuPayloadArray = sei_data;
2235 params->codecPicParams.av1PicParams.obuPayloadArrayCnt = sei_count;
2258 NV_ENC_LOCK_BITSTREAM *params,
2263 pkt->
pts = params->outputTimeStamp;
2268 #if FF_API_TICKS_PER_FRAME
2271 FFMAX(
ctx->encode_config.frameIntervalP - 1, 0);
2285 int idx =
ctx->frame_data_array_pos;
2300 ctx->frame_data_array_pos = (
ctx->frame_data_array_pos + 1) %
ctx->frame_data_array_nb;
2301 pic_params->inputDuration = idx;
2311 int idx = lock_params->outputDuration;
2331 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2333 NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
2334 NVENCSTATUS nv_status;
2339 lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
2341 lock_params.doNotWait = 0;
2344 nv_status = p_nvenc->nvEncLockBitstream(
ctx->nvencoder, &lock_params);
2345 if (nv_status != NV_ENC_SUCCESS) {
2357 memcpy(
pkt->
data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
2359 nv_status = p_nvenc->nvEncUnlockBitstream(
ctx->nvencoder, tmpoutsurf->
output_surface);
2360 if (nv_status != NV_ENC_SUCCESS) {
2361 res =
nvenc_print_error(avctx, nv_status,
"Failed unlocking bitstream buffer, expect the gates of mordor to open");
2367 ctx->registered_frames[tmpoutsurf->
reg_idx].mapped -= 1;
2368 if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped == 0) {
2369 nv_status = p_nvenc->nvEncUnmapInputResource(
ctx->nvencoder,
ctx->registered_frames[tmpoutsurf->
reg_idx].in_map.mappedResource);
2370 if (nv_status != NV_ENC_SUCCESS) {
2374 }
else if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped < 0) {
2384 switch (lock_params.pictureType) {
2385 case NV_ENC_PIC_TYPE_IDR:
2387 case NV_ENC_PIC_TYPE_I:
2390 case NV_ENC_PIC_TYPE_P:
2393 case NV_ENC_PIC_TYPE_B:
2396 case NV_ENC_PIC_TYPE_BI:
2400 av_log(avctx,
AV_LOG_ERROR,
"Unknown picture type encountered, expect the output to be broken.\n");
2401 av_log(avctx,
AV_LOG_ERROR,
"Please report this error and include as much information on how to reproduce it as possible.\n");
2429 int nb_ready, nb_pending;
2434 return nb_ready > 0;
2435 return (nb_ready > 0) && (nb_ready + nb_pending >=
ctx->async_depth);
2445 void *a53_data =
NULL;
2446 size_t a53_size = 0;
2454 &
ctx->sei_data_size,
2455 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2462 ctx->sei_data[sei_count].payloadSize = (uint32_t)a53_size;
2463 ctx->sei_data[sei_count].payload = (uint8_t*)a53_data;
2465 #if CONFIG_AV1_NVENC_ENCODER
2478 void *tc_data =
NULL;
2487 &
ctx->sei_data_size,
2488 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2495 ctx->sei_data[sei_count].payloadSize = (uint32_t)tc_size;
2496 ctx->sei_data[sei_count].payload = (uint8_t*)tc_data;
2498 #if CONFIG_AV1_NVENC_ENCODER
2521 &
ctx->sei_data_size,
2522 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2528 ctx->sei_data[sei_count].payloadSize = side_data->
size;
2532 if (!
ctx->sei_data[sei_count].payload) {
2544 for (
i = 0;
i < sei_count;
i++)
2553 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
2556 NV_ENC_RECONFIGURE_PARAMS params = { 0 };
2557 int needs_reconfig = 0;
2558 int needs_encode_config = 0;
2559 int reconfig_bitrate = 0, reconfig_dar = 0;
2562 params.version = NV_ENC_RECONFIGURE_PARAMS_VER;
2563 params.reInitEncodeParams =
ctx->init_encode_params;
2566 if (dw !=
ctx->init_encode_params.darWidth || dh !=
ctx->init_encode_params.darHeight) {
2568 "aspect ratio change (DAR): %d:%d -> %d:%d\n",
2569 ctx->init_encode_params.darWidth,
2570 ctx->init_encode_params.darHeight, dw, dh);
2572 params.reInitEncodeParams.darHeight = dh;
2573 params.reInitEncodeParams.darWidth = dw;
2579 if (
ctx->rc != NV_ENC_PARAMS_RC_CONSTQP &&
ctx->support_dyn_bitrate) {
2580 if (avctx->
bit_rate > 0 && params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate != avctx->
bit_rate) {
2582 "avg bitrate change: %d -> %d\n",
2583 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate,
2586 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate = avctx->
bit_rate;
2587 reconfig_bitrate = 1;
2592 "max bitrate change: %d -> %d\n",
2593 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate,
2596 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate = avctx->
rc_max_rate;
2597 reconfig_bitrate = 1;
2602 "vbv buffer size change: %d -> %d\n",
2603 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize,
2606 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize = avctx->
rc_buffer_size;
2607 reconfig_bitrate = 1;
2610 if (reconfig_bitrate) {
2611 params.resetEncoder = 1;
2612 params.forceIDR = 1;
2614 needs_encode_config = 1;
2619 if (!needs_encode_config)
2620 params.reInitEncodeParams.encodeConfig =
NULL;
2622 if (needs_reconfig) {
2623 ret = p_nvenc->nvEncReconfigureEncoder(
ctx->nvencoder, ¶ms);
2624 if (
ret != NV_ENC_SUCCESS) {
2628 ctx->init_encode_params.darHeight = dh;
2629 ctx->init_encode_params.darWidth = dw;
2632 if (reconfig_bitrate) {
2633 ctx->encode_config.rcParams.averageBitRate = params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate;
2634 ctx->encode_config.rcParams.maxBitRate = params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate;
2635 ctx->encode_config.rcParams.vbvBufferSize = params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize;
2644 NVENCSTATUS nv_status;
2652 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2654 NV_ENC_PIC_PARAMS pic_params = { 0 };
2655 pic_params.version = NV_ENC_PIC_PARAMS_VER;
2657 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
2681 pic_params.bufferFmt = in_surf->
format;
2682 pic_params.inputWidth = in_surf->
width;
2683 pic_params.inputHeight = in_surf->
height;
2684 pic_params.inputPitch = in_surf->
pitch;
2689 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
2691 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
2693 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
2697 pic_params.encodePicFlags =
2698 ctx->forced_idr ? NV_ENC_PIC_FLAG_FORCEIDR : NV_ENC_PIC_FLAG_FORCEINTRA;
2700 pic_params.encodePicFlags = 0;
2703 pic_params.inputTimeStamp =
frame->
pts;
2705 if (
ctx->extra_sei) {
2718 pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
2725 nv_status = p_nvenc->nvEncEncodePicture(
ctx->nvencoder, &pic_params);
2727 for (
i = 0;
i < sei_count;
i++)
2734 if (nv_status != NV_ENC_SUCCESS &&
2735 nv_status != NV_ENC_ERR_NEED_MORE_INPUT)
2746 if (nv_status == NV_ENC_SUCCESS) {
2747 while (
av_fifo_read(
ctx->output_surface_queue, &tmp_out_surf, 1) >= 0)
2763 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)