23 #include "config_components.h"
27 #if CONFIG_AV1_NVENC_ENCODER
43 #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, dl_fn->cuda_dl, x)
45 #define NVENC_CAP 0x30
47 #ifndef NVENC_NO_DEPRECATED_RC
48 #define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR || \
49 rc == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ || \
50 rc == NV_ENC_PARAMS_RC_CBR_HQ)
52 #define IS_CBR(rc) (rc == NV_ENC_PARAMS_RC_CBR)
87 #define IS_10BIT(pix_fmt) (pix_fmt == AV_PIX_FMT_P010 || \
88 pix_fmt == AV_PIX_FMT_P016 || \
89 pix_fmt == AV_PIX_FMT_YUV444P16 || \
90 pix_fmt == AV_PIX_FMT_X2RGB10 || \
91 pix_fmt == AV_PIX_FMT_X2BGR10 || \
92 pix_fmt == AV_PIX_FMT_GBRP16)
94 #define IS_YUV444(pix_fmt) (pix_fmt == AV_PIX_FMT_YUV444P || \
95 pix_fmt == AV_PIX_FMT_YUV444P16 || \
96 pix_fmt == AV_PIX_FMT_GBRP || \
97 pix_fmt == AV_PIX_FMT_GBRP16)
99 #define IS_GBRP(pix_fmt) (pix_fmt == AV_PIX_FMT_GBRP || \
100 pix_fmt == AV_PIX_FMT_GBRP16)
102 static const struct {
107 { NV_ENC_SUCCESS, 0,
"success" },
108 { NV_ENC_ERR_NO_ENCODE_DEVICE,
AVERROR(ENOENT),
"no encode device" },
109 { NV_ENC_ERR_UNSUPPORTED_DEVICE,
AVERROR(ENOSYS),
"unsupported device" },
110 { NV_ENC_ERR_INVALID_ENCODERDEVICE,
AVERROR(EINVAL),
"invalid encoder device" },
111 { NV_ENC_ERR_INVALID_DEVICE,
AVERROR(EINVAL),
"invalid device" },
112 { NV_ENC_ERR_DEVICE_NOT_EXIST,
AVERROR(EIO),
"device does not exist" },
113 { NV_ENC_ERR_INVALID_PTR,
AVERROR(EFAULT),
"invalid ptr" },
114 { NV_ENC_ERR_INVALID_EVENT,
AVERROR(EINVAL),
"invalid event" },
115 { NV_ENC_ERR_INVALID_PARAM,
AVERROR(EINVAL),
"invalid param" },
116 { NV_ENC_ERR_INVALID_CALL,
AVERROR(EINVAL),
"invalid call" },
117 { NV_ENC_ERR_OUT_OF_MEMORY,
AVERROR(ENOMEM),
"out of memory" },
118 { NV_ENC_ERR_ENCODER_NOT_INITIALIZED,
AVERROR(EINVAL),
"encoder not initialized" },
119 { NV_ENC_ERR_UNSUPPORTED_PARAM,
AVERROR(ENOSYS),
"unsupported param" },
120 { NV_ENC_ERR_LOCK_BUSY,
AVERROR(EAGAIN),
"lock busy" },
122 { NV_ENC_ERR_INVALID_VERSION,
AVERROR(EINVAL),
"invalid version" },
123 { NV_ENC_ERR_MAP_FAILED,
AVERROR(EIO),
"map failed" },
124 { NV_ENC_ERR_NEED_MORE_INPUT,
AVERROR(EAGAIN),
"need more input" },
125 { NV_ENC_ERR_ENCODER_BUSY,
AVERROR(EAGAIN),
"encoder busy" },
126 { NV_ENC_ERR_EVENT_NOT_REGISTERD,
AVERROR(EBADF),
"event not registered" },
128 { NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY,
AVERROR(EINVAL),
"incompatible client key" },
129 { NV_ENC_ERR_UNIMPLEMENTED,
AVERROR(ENOSYS),
"unimplemented" },
130 { NV_ENC_ERR_RESOURCE_REGISTER_FAILED,
AVERROR(EIO),
"resource register failed" },
131 { NV_ENC_ERR_RESOURCE_NOT_REGISTERED,
AVERROR(EBADF),
"resource not registered" },
132 { NV_ENC_ERR_RESOURCE_NOT_MAPPED,
AVERROR(EBADF),
"resource not mapped" },
146 *
desc =
"unknown error";
151 const char *error_string)
154 const char *details =
"(no details)";
157 #ifdef NVENC_HAVE_GETLASTERRORSTRING
159 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
161 if (p_nvenc &&
ctx->nvencoder)
162 details = p_nvenc->nvEncGetLastErrorString(
ctx->nvencoder);
175 #define PRESET_ALIAS(alias, name, ...) \
176 [PRESET_ ## alias] = { NV_ENC_PRESET_ ## name ## _GUID, __VA_ARGS__ }
178 #define PRESET(name, ...) PRESET_ALIAS(name, name, __VA_ARGS__)
183 #ifdef NVENC_HAVE_NEW_PRESETS
222 ctx->init_encode_params.presetGUID = t->
guid;
225 #ifdef NVENC_HAVE_NEW_PRESETS
226 if (
ctx->tuning_info == NV_ENC_TUNING_INFO_LOSSLESS)
236 #if NVENCAPI_CHECK_VERSION(12, 1)
237 const char *minver =
"(unknown)";
238 #elif NVENCAPI_CHECK_VERSION(12, 0)
239 # if defined(_WIN32) || defined(__CYGWIN__)
240 const char *minver =
"522.25";
242 const char *minver =
"520.56.06";
244 #elif NVENCAPI_CHECK_VERSION(11, 1)
245 # if defined(_WIN32) || defined(__CYGWIN__)
246 const char *minver =
"471.41";
248 const char *minver =
"470.57.02";
250 #elif NVENCAPI_CHECK_VERSION(11, 0)
251 # if defined(_WIN32) || defined(__CYGWIN__)
252 const char *minver =
"456.71";
254 const char *minver =
"455.28";
256 #elif NVENCAPI_CHECK_VERSION(10, 0)
257 # if defined(_WIN32) || defined(__CYGWIN__)
258 const char *minver =
"450.51";
260 const char *minver =
"445.87";
262 #elif NVENCAPI_CHECK_VERSION(9, 1)
263 # if defined(_WIN32) || defined(__CYGWIN__)
264 const char *minver =
"436.15";
266 const char *minver =
"435.21";
268 #elif NVENCAPI_CHECK_VERSION(9, 0)
269 # if defined(_WIN32) || defined(__CYGWIN__)
270 const char *minver =
"418.81";
272 const char *minver =
"418.30";
274 #elif NVENCAPI_CHECK_VERSION(8, 2)
275 # if defined(_WIN32) || defined(__CYGWIN__)
276 const char *minver =
"397.93";
278 const char *minver =
"396.24";
280 #elif NVENCAPI_CHECK_VERSION(8, 1)
281 # if defined(_WIN32) || defined(__CYGWIN__)
282 const char *minver =
"390.77";
284 const char *minver =
"390.25";
287 # if defined(_WIN32) || defined(__CYGWIN__)
288 const char *minver =
"378.66";
290 const char *minver =
"378.13";
293 av_log(avctx,
level,
"The minimum required Nvidia driver for nvenc is %s or newer\n", minver);
301 uint32_t nvenc_max_ver;
304 ret = cuda_load_functions(&dl_fn->
cuda_dl, avctx);
308 ret = nvenc_load_functions(&dl_fn->
nvenc_dl, avctx);
314 err = dl_fn->
nvenc_dl->NvEncodeAPIGetMaxSupportedVersion(&nvenc_max_ver);
315 if (err != NV_ENC_SUCCESS)
320 if ((NVENCAPI_MAJOR_VERSION << 4 | NVENCAPI_MINOR_VERSION) > nvenc_max_ver) {
321 av_log(avctx,
AV_LOG_ERROR,
"Driver does not support the required nvenc API version. "
322 "Required: %d.%d Found: %d.%d\n",
323 NVENCAPI_MAJOR_VERSION, NVENCAPI_MINOR_VERSION,
324 nvenc_max_ver >> 4, nvenc_max_ver & 0
xf);
329 dl_fn->
nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
332 if (err != NV_ENC_SUCCESS)
345 if (
ctx->d3d11_device)
357 if (
ctx->d3d11_device)
365 NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = { 0 };
367 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
370 params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
371 params.apiVersion = NVENCAPI_VERSION;
372 if (
ctx->d3d11_device) {
373 params.device =
ctx->d3d11_device;
374 params.deviceType = NV_ENC_DEVICE_TYPE_DIRECTX;
376 params.device =
ctx->cu_context;
377 params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
380 ret = p_nvenc->nvEncOpenEncodeSessionEx(¶ms, &
ctx->nvencoder);
381 if (
ret != NV_ENC_SUCCESS) {
392 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
393 int i,
ret, count = 0;
396 ret = p_nvenc->nvEncGetEncodeGUIDCount(
ctx->nvencoder, &count);
398 if (
ret != NV_ENC_SUCCESS || !count)
405 ret = p_nvenc->nvEncGetEncodeGUIDs(
ctx->nvencoder, guids, count, &count);
406 if (
ret != NV_ENC_SUCCESS) {
412 for (
i = 0;
i < count;
i++) {
413 if (!memcmp(&guids[
i], &
ctx->init_encode_params.encodeGUID,
sizeof(*guids))) {
428 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
429 NV_ENC_CAPS_PARAM params = { 0 };
432 params.version = NV_ENC_CAPS_PARAM_VER;
433 params.capsToQuery = cap;
435 ret = p_nvenc->nvEncGetEncodeCaps(
ctx->nvencoder,
ctx->init_encode_params.encodeGUID, ¶ms, &
val);
437 if (
ret == NV_ENC_SUCCESS)
466 if (ret < avctx->
width) {
473 if (ret < avctx->
height) {
480 if (ret < avctx->max_b_frames) {
490 "Interlaced encoding is not supported. Supported level: %d\n",
502 if (
ctx->rc_lookahead > 0 &&
ret <= 0) {
508 if (
ctx->temporal_aq > 0 &&
ret <= 0) {
514 if (
ctx->weighted_pred > 0 &&
ret <= 0) {
520 if (
ctx->coder == NV_ENC_H264_ENTROPY_CODING_MODE_CABAC &&
ret <= 0) {
525 #ifdef NVENC_HAVE_BFRAME_REF_MODE
526 tmp = (
ctx->b_ref_mode >= 0) ?
ctx->b_ref_mode : NV_ENC_BFRAME_REF_MODE_DISABLED;
528 if (
tmp == NV_ENC_BFRAME_REF_MODE_EACH &&
ret != 1 &&
ret != 3) {
531 }
else if (
tmp != NV_ENC_BFRAME_REF_MODE_DISABLED &&
ret == 0) {
536 tmp = (
ctx->b_ref_mode >= 0) ?
ctx->b_ref_mode : 0;
543 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
545 if(avctx->
refs != NV_ENC_NUM_REF_FRAMES_AUTOSELECT &&
ret <= 0) {
550 if(avctx->
refs != 0) {
556 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
558 if(
ctx->single_slice_intra_refresh &&
ret <= 0) {
563 if(
ctx->single_slice_intra_refresh) {
570 if((
ctx->intra_refresh ||
ctx->single_slice_intra_refresh) &&
ret <= 0) {
575 #ifndef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
583 if(
ctx->constrained_encoding &&
ret <= 0) {
588 ctx->support_dyn_bitrate =
nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE);
597 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
598 char name[128] = { 0};
599 int major, minor,
ret;
618 av_log(avctx, loglevel,
"[ GPU #%d - < %s > has Compute SM %d.%d ]\n", idx,
name, major, minor);
619 if (((major << 4) | minor) <
NVENC_CAP) {
620 av_log(avctx, loglevel,
"does not support NVENC\n");
631 ctx->cu_context =
ctx->cu_context_internal;
643 av_log(avctx, loglevel,
"supports NVENC\n");
654 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
662 ctx->cu_context_internal =
NULL;
675 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_H264_GUID;
678 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_HEVC_GUID;
680 #if CONFIG_AV1_NVENC_ENCODER
682 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_AV1_GUID;
692 av_log(avctx,
AV_LOG_WARNING,
"The selected preset is deprecated. Use p1 to p7 + -tune or fast/medium/slow.\n");
716 cuda_device_hwctx = hwdev_ctx->
hwctx;
719 d3d11_device_hwctx = hwdev_ctx->
hwctx;
727 if (cuda_device_hwctx) {
729 ctx->cu_stream = cuda_device_hwctx->
stream;
732 else if (d3d11_device_hwctx) {
733 ctx->d3d11_device = d3d11_device_hwctx->
device;
734 ID3D11Device_AddRef(
ctx->d3d11_device);
744 av_log(avctx,
AV_LOG_FATAL,
"Provided device doesn't support required NVENC features\n");
748 int i, nb_devices = 0;
764 for (
i = 0;
i < nb_devices; ++
i) {
777 av_log(avctx,
AV_LOG_FATAL,
"Requested GPU %d, but only %d GPUs are available!\n",
ctx->device, nb_devices);
787 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
788 #if CONFIG_AV1_NVENC_ENCODER
794 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
796 if (
ctx->init_qp_p >= 0) {
797 rc->constQP.qpInterP =
ctx->init_qp_p;
798 if (
ctx->init_qp_i >= 0 &&
ctx->init_qp_b >= 0) {
799 rc->constQP.qpIntra =
ctx->init_qp_i;
800 rc->constQP.qpInterB =
ctx->init_qp_b;
804 rc->constQP.qpInterB =
av_clip(
807 rc->constQP.qpIntra = rc->constQP.qpInterP;
808 rc->constQP.qpInterB = rc->constQP.qpInterP;
810 }
else if (
ctx->cqp >= 0) {
811 rc->constQP.qpInterP = rc->constQP.qpInterB = rc->constQP.qpIntra =
ctx->cqp;
825 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
827 #if CONFIG_AV1_NVENC_ENCODER
833 if (avctx->
qmin >= 0 && avctx->
qmax >= 0) {
837 rc->minQP.qpInterB = avctx->
qmin;
838 rc->minQP.qpInterP = avctx->
qmin;
839 rc->minQP.qpIntra = avctx->
qmin;
841 rc->maxQP.qpInterB = avctx->
qmax;
842 rc->maxQP.qpInterP = avctx->
qmax;
843 rc->maxQP.qpIntra = avctx->
qmax;
845 qp_inter_p = (avctx->
qmax + 3 * avctx->
qmin) / 4;
846 }
else if (avctx->
qmin >= 0) {
849 rc->minQP.qpInterB = avctx->
qmin;
850 rc->minQP.qpInterP = avctx->
qmin;
851 rc->minQP.qpIntra = avctx->
qmin;
853 qp_inter_p = avctx->
qmin;
858 rc->enableInitialRCQP = 1;
860 if (
ctx->init_qp_p < 0) {
861 rc->initialRCQP.qpInterP = qp_inter_p;
863 rc->initialRCQP.qpInterP =
ctx->init_qp_p;
866 if (
ctx->init_qp_i < 0) {
868 rc->initialRCQP.qpIntra =
av_clip(
871 rc->initialRCQP.qpIntra = rc->initialRCQP.qpInterP;
874 rc->initialRCQP.qpIntra =
ctx->init_qp_i;
877 if (
ctx->init_qp_b < 0) {
879 rc->initialRCQP.qpInterB =
av_clip(
882 rc->initialRCQP.qpInterB = rc->initialRCQP.qpInterP;
885 rc->initialRCQP.qpInterB =
ctx->init_qp_b;
892 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
894 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
895 rc->constQP.qpInterB = 0;
896 rc->constQP.qpInterP = 0;
897 rc->constQP.qpIntra = 0;
906 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
909 case NV_ENC_PARAMS_RC_CONSTQP:
912 #ifndef NVENC_NO_DEPRECATED_RC
913 case NV_ENC_PARAMS_RC_VBR_MINQP:
914 if (avctx->
qmin < 0) {
916 "The variable bitrate rate-control requires "
917 "the 'qmin' option set.\n");
922 case NV_ENC_PARAMS_RC_VBR_HQ:
924 case NV_ENC_PARAMS_RC_VBR:
927 case NV_ENC_PARAMS_RC_CBR:
928 #ifndef NVENC_NO_DEPRECATED_RC
929 case NV_ENC_PARAMS_RC_CBR_HQ:
930 case NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ:
935 rc->rateControlMode =
ctx->rc;
944 int nb_surfaces =
FFMAX(4,
ctx->encode_config.frameIntervalP * 2 * 2);
947 if (
ctx->rc_lookahead > 0) {
950 nb_surfaces =
FFMAX(1,
FFMAX(nb_surfaces,
ctx->rc_lookahead +
ctx->encode_config.frameIntervalP + 1 + 4));
951 if (nb_surfaces >
ctx->nb_surfaces &&
ctx->nb_surfaces > 0)
954 "Defined rc_lookahead requires more surfaces, "
955 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
957 ctx->nb_surfaces =
FFMAX(nb_surfaces,
ctx->nb_surfaces);
959 if (
ctx->encode_config.frameIntervalP > 1 &&
ctx->nb_surfaces < nb_surfaces &&
ctx->nb_surfaces > 0)
962 "Defined b-frame requires more surfaces, "
963 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
964 ctx->nb_surfaces =
FFMAX(
ctx->nb_surfaces, nb_surfaces);
966 else if (
ctx->nb_surfaces <= 0)
967 ctx->nb_surfaces = nb_surfaces;
976 ctx->frame_data_array_nb =
ctx->nb_surfaces +
ctx->encode_config.frameIntervalP - 1;
986 av_log(avctx,
AV_LOG_WARNING,
"Using global_quality with nvenc is deprecated. Use qp instead.\n");
992 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate;
993 }
else if (
ctx->encode_config.rcParams.averageBitRate > 0) {
994 ctx->encode_config.rcParams.maxBitRate =
ctx->encode_config.rcParams.averageBitRate;
1000 #ifdef NVENC_HAVE_MULTIPASS
1001 ctx->encode_config.rcParams.multiPass =
ctx->multipass;
1004 ctx->encode_config.rcParams.multiPass = NV_ENC_MULTI_PASS_DISABLED;
1006 ctx->encode_config.rcParams.multiPass = NV_ENC_TWO_PASS_FULL_RESOLUTION;
1010 ctx->rc = NV_ENC_PARAMS_RC_CBR;
1011 }
else if (
ctx->cqp >= 0) {
1012 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
1013 }
else if (
ctx->quality >= 0.0f) {
1014 ctx->rc = NV_ENC_PARAMS_RC_VBR;
1024 if (
ctx->twopass < 0)
1029 ctx->rc = NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ;
1031 ctx->rc = NV_ENC_PARAMS_RC_CBR;
1033 }
else if (
ctx->cqp >= 0) {
1034 ctx->rc = NV_ENC_PARAMS_RC_CONSTQP;
1035 }
else if (
ctx->twopass) {
1036 ctx->rc = NV_ENC_PARAMS_RC_VBR_HQ;
1037 }
else if (avctx->
qmin >= 0 && avctx->
qmax >= 0) {
1038 ctx->rc = NV_ENC_PARAMS_RC_VBR_MINQP;
1050 #ifdef NVENC_HAVE_QP_CHROMA_OFFSETS
1051 ctx->encode_config.rcParams.cbQPIndexOffset =
ctx->qp_cb_offset;
1052 ctx->encode_config.rcParams.crQPIndexOffset =
ctx->qp_cr_offset;
1054 if (
ctx->qp_cb_offset ||
ctx->qp_cr_offset)
1055 av_log(avctx,
AV_LOG_WARNING,
"Failed setting QP CB/CR offsets, SDK 11.1 or greater required at compile time.\n");
1058 #ifdef NVENC_HAVE_LDKFS
1060 ctx->encode_config.rcParams.lowDelayKeyFrameScale =
ctx->ldkfs;
1065 }
else if (
ctx->rc >= 0) {
1068 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
1074 }
else if (
ctx->encode_config.rcParams.averageBitRate > 0) {
1075 avctx->
rc_buffer_size =
ctx->encode_config.rcParams.vbvBufferSize = 2 *
ctx->encode_config.rcParams.averageBitRate;
1079 ctx->encode_config.rcParams.enableAQ = 1;
1080 ctx->encode_config.rcParams.aqStrength =
ctx->aq_strength;
1084 if (
ctx->temporal_aq) {
1085 ctx->encode_config.rcParams.enableTemporalAQ = 1;
1089 if (
ctx->rc_lookahead > 0) {
1090 int lkd_bound =
FFMIN(
ctx->nb_surfaces,
ctx->async_depth) -
1091 ctx->encode_config.frameIntervalP - 4;
1093 if (lkd_bound < 0) {
1094 ctx->encode_config.rcParams.enableLookahead = 0;
1096 "Lookahead not enabled. Increase buffer delay (-delay).\n");
1098 ctx->encode_config.rcParams.enableLookahead = 1;
1099 ctx->encode_config.rcParams.lookaheadDepth =
av_clip(
ctx->rc_lookahead, 0, lkd_bound);
1100 ctx->encode_config.rcParams.disableIadapt =
ctx->no_scenecut;
1101 ctx->encode_config.rcParams.disableBadapt = !
ctx->b_adapt;
1103 "Lookahead enabled: depth %d, scenecut %s, B-adapt %s.\n",
1104 ctx->encode_config.rcParams.lookaheadDepth,
1105 ctx->encode_config.rcParams.disableIadapt ?
"disabled" :
"enabled",
1106 ctx->encode_config.rcParams.disableBadapt ?
"disabled" :
"enabled");
1107 if (
ctx->encode_config.rcParams.lookaheadDepth <
ctx->rc_lookahead)
1108 av_log(avctx,
AV_LOG_WARNING,
"Clipping lookahead depth to %d (from %d) due to lack of surfaces/delay",
1109 ctx->encode_config.rcParams.lookaheadDepth,
ctx->rc_lookahead);
1113 if (
ctx->strict_gop) {
1114 ctx->encode_config.rcParams.strictGOPTarget = 1;
1119 ctx->encode_config.rcParams.enableNonRefP = 1;
1121 if (
ctx->zerolatency)
1122 ctx->encode_config.rcParams.zeroReorderDelay = 1;
1126 int tmp_quality = (
int)(
ctx->quality * 256.0f);
1127 ctx->encode_config.rcParams.targetQuality = (uint8_t)(tmp_quality >> 8);
1128 ctx->encode_config.rcParams.targetQualityLSB = (uint8_t)(tmp_quality & 0xff);
1133 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate = 0;
1142 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1143 NV_ENC_CONFIG_H264 *h264 = &cc->encodeCodecConfig.h264Config;
1144 NV_ENC_CONFIG_H264_VUI_PARAMETERS *vui = &h264->h264VUIParameters;
1151 vui->transferCharacteristics = avctx->
color_trc;
1152 vui->videoFullRangeFlag = 0;
1156 vui->transferCharacteristics = avctx->
color_trc;
1161 vui->colourDescriptionPresentFlag =
1162 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1164 vui->videoSignalTypePresentFlag =
1165 (vui->colourDescriptionPresentFlag
1166 || vui->videoFormat != 5
1167 || vui->videoFullRangeFlag != 0);
1169 if (
ctx->max_slice_size > 0) {
1170 h264->sliceMode = 1;
1171 h264->sliceModeData =
ctx->max_slice_size;
1173 h264->sliceMode = 3;
1174 h264->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1177 if (
ctx->intra_refresh) {
1178 h264->enableIntraRefresh = 1;
1179 h264->intraRefreshPeriod = cc->gopLength;
1180 h264->intraRefreshCnt = cc->gopLength - 1;
1181 cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1182 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1183 h264->singleSliceIntraRefresh =
ctx->single_slice_intra_refresh;
1187 if (
ctx->constrained_encoding)
1188 h264->enableConstrainedEncoding = 1;
1192 h264->outputAUD =
ctx->aud;
1194 if (
ctx->dpb_size >= 0) {
1196 h264->maxNumRefFrames =
ctx->dpb_size;
1199 h264->idrPeriod = cc->gopLength;
1201 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1202 h264->outputBufferingPeriodSEI = 1;
1205 h264->outputPictureTimingSEI = 1;
1207 #ifndef NVENC_NO_DEPRECATED_RC
1208 if (cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ ||
1209 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_CBR_HQ ||
1210 cc->rcParams.rateControlMode == NV_ENC_PARAMS_RC_VBR_HQ) {
1211 h264->adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
1212 h264->fmoMode = NV_ENC_H264_FMO_DISABLE;
1217 h264->qpPrimeYZeroTransformBypassFlag = 1;
1219 switch(
ctx->profile) {
1221 cc->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
1225 cc->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
1229 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
1233 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1241 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1247 h264->level =
ctx->level;
1249 if (
ctx->coder >= 0)
1250 h264->entropyCodingMode =
ctx->coder;
1252 #ifdef NVENC_HAVE_BFRAME_REF_MODE
1253 if (
ctx->b_ref_mode >= 0)
1254 h264->useBFramesAsRef =
ctx->b_ref_mode;
1257 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1258 h264->numRefL0 = avctx->
refs;
1259 h264->numRefL1 = avctx->
refs;
1268 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1269 NV_ENC_CONFIG_HEVC *hevc = &cc->encodeCodecConfig.hevcConfig;
1270 NV_ENC_CONFIG_HEVC_VUI_PARAMETERS *vui = &hevc->hevcVUIParameters;
1277 vui->transferCharacteristics = avctx->
color_trc;
1278 vui->videoFullRangeFlag = 0;
1282 vui->transferCharacteristics = avctx->
color_trc;
1287 vui->colourDescriptionPresentFlag =
1288 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1290 vui->videoSignalTypePresentFlag =
1291 (vui->colourDescriptionPresentFlag
1292 || vui->videoFormat != 5
1293 || vui->videoFullRangeFlag != 0);
1295 if (
ctx->max_slice_size > 0) {
1296 hevc->sliceMode = 1;
1297 hevc->sliceModeData =
ctx->max_slice_size;
1299 hevc->sliceMode = 3;
1300 hevc->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1303 if (
ctx->intra_refresh) {
1304 hevc->enableIntraRefresh = 1;
1305 hevc->intraRefreshPeriod = cc->gopLength;
1306 hevc->intraRefreshCnt = cc->gopLength - 1;
1307 cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1308 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
1309 hevc->singleSliceIntraRefresh =
ctx->single_slice_intra_refresh;
1313 #ifdef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
1314 if (
ctx->constrained_encoding)
1315 hevc->enableConstrainedEncoding = 1;
1320 hevc->outputAUD =
ctx->aud;
1322 if (
ctx->dpb_size >= 0) {
1324 hevc->maxNumRefFramesInDPB =
ctx->dpb_size;
1327 hevc->idrPeriod = cc->gopLength;
1329 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1330 hevc->outputBufferingPeriodSEI = 1;
1333 hevc->outputPictureTimingSEI = 1;
1335 switch (
ctx->profile) {
1337 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
1341 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1345 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1352 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1358 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1362 hevc->chromaFormatIDC =
IS_YUV444(
ctx->data_pix_fmt) ? 3 : 1;
1364 hevc->pixelBitDepthMinus8 =
IS_10BIT(
ctx->data_pix_fmt) ? 2 : 0;
1366 hevc->level =
ctx->level;
1368 hevc->tier =
ctx->tier;
1370 #ifdef NVENC_HAVE_HEVC_BFRAME_REF_MODE
1371 if (
ctx->b_ref_mode >= 0)
1372 hevc->useBFramesAsRef =
ctx->b_ref_mode;
1375 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1376 hevc->numRefL0 = avctx->
refs;
1377 hevc->numRefL1 = avctx->
refs;
1383 #if CONFIG_AV1_NVENC_ENCODER
1387 NV_ENC_CONFIG *cc = &
ctx->encode_config;
1388 NV_ENC_CONFIG_AV1 *av1 = &cc->encodeCodecConfig.av1Config;
1395 av1->transferCharacteristics = avctx->
color_trc;
1396 av1->colorRange = 0;
1400 av1->transferCharacteristics = avctx->
color_trc;
1406 av_log(avctx,
AV_LOG_ERROR,
"AV1 High Profile not supported, required for 4:4:4 encoding\n");
1409 cc->profileGUID = NV_ENC_AV1_PROFILE_MAIN_GUID;
1413 if (
ctx->dpb_size >= 0) {
1415 av1->maxNumRefFramesInDPB =
ctx->dpb_size;
1418 if (
ctx->intra_refresh) {
1419 av1->enableIntraRefresh = 1;
1420 av1->intraRefreshPeriod = cc->gopLength;
1421 av1->intraRefreshCnt = cc->gopLength - 1;
1422 cc->gopLength = NVENC_INFINITE_GOPLENGTH;
1425 av1->idrPeriod = cc->gopLength;
1427 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1428 av1->enableBitstreamPadding = 1;
1431 if (
ctx->tile_cols >= 0)
1432 av1->numTileColumns =
ctx->tile_cols;
1433 if (
ctx->tile_rows >= 0)
1434 av1->numTileRows =
ctx->tile_rows;
1436 av1->outputAnnexBFormat = 0;
1438 av1->level =
ctx->level;
1439 av1->tier =
ctx->tier;
1441 av1->enableTimingInfo =
ctx->timing_info;
1444 av1->disableSeqHdr = 0;
1445 av1->repeatSeqHdr = 1;
1447 av1->chromaFormatIDC =
IS_YUV444(
ctx->data_pix_fmt) ? 3 : 1;
1449 av1->inputPixelBitDepthMinus8 =
IS_10BIT(
ctx->data_pix_fmt) ? 2 : 0;
1450 av1->pixelBitDepthMinus8 = (
IS_10BIT(
ctx->data_pix_fmt) ||
ctx->highbitdepth) ? 2 : 0;
1452 if (
ctx->b_ref_mode >= 0)
1453 av1->useBFramesAsRef =
ctx->b_ref_mode;
1455 av1->numFwdRefs = avctx->
refs;
1456 av1->numBwdRefs = avctx->
refs;
1469 #if CONFIG_AV1_NVENC_ENCODER
1471 return nvenc_setup_av1_config(avctx);
1485 #if CONFIG_AV1_NVENC_ENCODER
1516 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1518 NV_ENC_PRESET_CONFIG preset_config = { 0 };
1519 NVENCSTATUS nv_status = NV_ENC_SUCCESS;
1524 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1525 ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
1527 ctx->init_encode_params.encodeHeight = avctx->
height;
1528 ctx->init_encode_params.encodeWidth = avctx->
width;
1530 ctx->init_encode_params.encodeConfig = &
ctx->encode_config;
1532 preset_config.version = NV_ENC_PRESET_CONFIG_VER;
1533 preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
1535 #ifdef NVENC_HAVE_NEW_PRESETS
1536 ctx->init_encode_params.tuningInfo =
ctx->tuning_info;
1539 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOSSLESS;
1541 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOW_LATENCY;
1543 nv_status = p_nvenc->nvEncGetEncodePresetConfigEx(
ctx->nvencoder,
1544 ctx->init_encode_params.encodeGUID,
1545 ctx->init_encode_params.presetGUID,
1546 ctx->init_encode_params.tuningInfo,
1549 nv_status = p_nvenc->nvEncGetEncodePresetConfig(
ctx->nvencoder,
1550 ctx->init_encode_params.encodeGUID,
1551 ctx->init_encode_params.presetGUID,
1554 if (nv_status != NV_ENC_SUCCESS)
1555 return nvenc_print_error(avctx, nv_status,
"Cannot get the preset configuration");
1557 memcpy(&
ctx->encode_config, &preset_config.presetCfg,
sizeof(
ctx->encode_config));
1559 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1562 ctx->init_encode_params.darHeight = dh;
1563 ctx->init_encode_params.darWidth = dw;
1572 #if FF_API_TICKS_PER_FRAME
1579 ctx->init_encode_params.enableEncodeAsync = 0;
1580 ctx->init_encode_params.enablePTD = 1;
1582 #ifdef NVENC_HAVE_NEW_PRESETS
1586 if (
ctx->rc_lookahead == 0 &&
ctx->encode_config.rcParams.enableLookahead)
1587 ctx->rc_lookahead =
ctx->encode_config.rcParams.lookaheadDepth;
1590 if (
ctx->weighted_pred == 1)
1591 ctx->init_encode_params.enableWeightedPrediction = 1;
1593 if (
ctx->bluray_compat) {
1602 ctx->level = NV_ENC_LEVEL_HEVC_51;
1603 ctx->tier = NV_ENC_TIER_HEVC_HIGH;
1612 ctx->encode_config.frameIntervalP = 0;
1613 ctx->encode_config.gopLength = 1;
1622 if(
ctx->single_slice_intra_refresh)
1623 ctx->intra_refresh = 1;
1630 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
1632 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
1643 nv_status = p_nvenc->nvEncInitializeEncoder(
ctx->nvencoder, &
ctx->init_encode_params);
1644 if (nv_status != NV_ENC_SUCCESS) {
1649 #ifdef NVENC_HAVE_CUSTREAM_PTR
1650 if (
ctx->cu_context) {
1651 nv_status = p_nvenc->nvEncSetIOCudaStreams(
ctx->nvencoder, &
ctx->cu_stream, &
ctx->cu_stream);
1652 if (nv_status != NV_ENC_SUCCESS) {
1663 if (
ctx->encode_config.frameIntervalP > 1)
1666 if (
ctx->encode_config.rcParams.averageBitRate > 0)
1667 avctx->
bit_rate =
ctx->encode_config.rcParams.averageBitRate;
1674 cpb_props->
buffer_size =
ctx->encode_config.rcParams.vbvBufferSize;
1683 return NV_ENC_BUFFER_FORMAT_YV12_PL;
1685 return NV_ENC_BUFFER_FORMAT_NV12_PL;
1688 return NV_ENC_BUFFER_FORMAT_YUV420_10BIT;
1691 return NV_ENC_BUFFER_FORMAT_YUV444_PL;
1694 return NV_ENC_BUFFER_FORMAT_YUV444_10BIT;
1697 return NV_ENC_BUFFER_FORMAT_ARGB;
1700 return NV_ENC_BUFFER_FORMAT_ABGR;
1702 return NV_ENC_BUFFER_FORMAT_ARGB10;
1704 return NV_ENC_BUFFER_FORMAT_ABGR10;
1706 return NV_ENC_BUFFER_FORMAT_UNDEFINED;
1714 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1717 NVENCSTATUS nv_status;
1718 NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
1719 allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
1723 if (!
ctx->surfaces[idx].in_ref)
1726 NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
1729 if (
ctx->surfaces[idx].format == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
1735 allocSurf.version = NV_ENC_CREATE_INPUT_BUFFER_VER;
1736 allocSurf.width = avctx->
width;
1737 allocSurf.height = avctx->
height;
1738 allocSurf.bufferFmt =
ctx->surfaces[idx].format;
1740 nv_status = p_nvenc->nvEncCreateInputBuffer(
ctx->nvencoder, &allocSurf);
1741 if (nv_status != NV_ENC_SUCCESS) {
1745 ctx->surfaces[idx].input_surface = allocSurf.inputBuffer;
1746 ctx->surfaces[idx].width = allocSurf.width;
1747 ctx->surfaces[idx].height = allocSurf.height;
1750 nv_status = p_nvenc->nvEncCreateBitstreamBuffer(
ctx->nvencoder, &allocOut);
1751 if (nv_status != NV_ENC_SUCCESS) {
1754 p_nvenc->nvEncDestroyInputBuffer(
ctx->nvencoder,
ctx->surfaces[idx].input_surface);
1759 ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer;
1769 int i, res = 0, res2;
1775 ctx->frame_data_array =
av_calloc(
ctx->frame_data_array_nb,
sizeof(*
ctx->frame_data_array));
1776 if (!
ctx->frame_data_array)
1780 if (!
ctx->timestamp_list)
1784 if (!
ctx->unused_surface_queue)
1788 if (!
ctx->output_surface_queue)
1791 if (!
ctx->output_surface_ready_queue)
1798 for (
i = 0;
i <
ctx->nb_surfaces;
i++) {
1815 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1817 NVENCSTATUS nv_status;
1818 uint32_t outSize = 0;
1819 char tmpHeader[NV_MAX_SEQ_HDR_LEN];
1821 NV_ENC_SEQUENCE_PARAM_PAYLOAD payload = { 0 };
1822 payload.version = NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER;
1824 payload.spsppsBuffer = tmpHeader;
1825 payload.inBufferSize =
sizeof(tmpHeader);
1826 payload.outSPSPPSPayloadSize = &outSize;
1828 nv_status = p_nvenc->nvEncGetSequenceParams(
ctx->nvencoder, &payload);
1829 if (nv_status != NV_ENC_SUCCESS) {
1840 memcpy(avctx->
extradata, tmpHeader, outSize);
1849 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1853 if (
ctx->nvencoder) {
1854 NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER,
1855 .encodePicFlags = NV_ENC_PIC_FLAG_EOS };
1861 p_nvenc->nvEncEncodePicture(
ctx->nvencoder, ¶ms);
1869 if (
ctx->frame_data_array) {
1870 for (
i = 0;
i <
ctx->nb_surfaces;
i++)
1876 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
1877 if (
ctx->registered_frames[
i].mapped)
1878 p_nvenc->nvEncUnmapInputResource(
ctx->nvencoder,
ctx->registered_frames[
i].in_map.mappedResource);
1879 if (
ctx->registered_frames[
i].regptr)
1880 p_nvenc->nvEncUnregisterResource(
ctx->nvencoder,
ctx->registered_frames[
i].regptr);
1882 ctx->nb_registered_frames = 0;
1885 if (
ctx->surfaces) {
1886 for (
i = 0;
i <
ctx->nb_surfaces; ++
i) {
1888 p_nvenc->nvEncDestroyInputBuffer(
ctx->nvencoder,
ctx->surfaces[
i].input_surface);
1890 p_nvenc->nvEncDestroyBitstreamBuffer(
ctx->nvencoder,
ctx->surfaces[
i].output_surface);
1894 ctx->nb_surfaces = 0;
1900 if (
ctx->nvencoder) {
1901 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
1909 if (
ctx->cu_context_internal)
1911 ctx->cu_context =
ctx->cu_context_internal =
NULL;
1914 if (
ctx->d3d11_device) {
1915 ID3D11Device_Release(
ctx->d3d11_device);
1920 nvenc_free_functions(&dl_fn->
nvenc_dl);
1921 cuda_free_functions(&dl_fn->
cuda_dl);
1939 "hw_frames_ctx must be set when using GPU frames as input\n");
1945 "hw_frames_ctx must match the GPU frame type\n");
1989 NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params,
const AVFrame *
frame)
1991 int dst_linesize[4] = {
1992 lock_buffer_params->pitch,
1993 lock_buffer_params->pitch,
1994 lock_buffer_params->pitch,
1995 lock_buffer_params->pitch
1997 uint8_t *dst_data[4];
2001 dst_linesize[1] = dst_linesize[2] >>= 1;
2004 lock_buffer_params->bufferDataPtr, dst_linesize);
2009 FFSWAP(uint8_t*, dst_data[1], dst_data[2]);
2022 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2023 NVENCSTATUS nv_status;
2028 for (first_round = 1; first_round >= 0; first_round--) {
2029 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
2030 if (!
ctx->registered_frames[
i].mapped) {
2031 if (
ctx->registered_frames[
i].regptr) {
2034 nv_status = p_nvenc->nvEncUnregisterResource(
ctx->nvencoder,
ctx->registered_frames[
i].regptr);
2035 if (nv_status != NV_ENC_SUCCESS)
2036 return nvenc_print_error(avctx, nv_status,
"Failed unregistering unused input resource");
2037 ctx->registered_frames[
i].ptr =
NULL;
2038 ctx->registered_frames[
i].regptr =
NULL;
2045 return ctx->nb_registered_frames++;
2056 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2059 NV_ENC_REGISTER_RESOURCE reg = { 0 };
2062 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
2073 reg.version = NV_ENC_REGISTER_RESOURCE_VER;
2074 reg.width = frames_ctx->width;
2075 reg.height = frames_ctx->height;
2076 reg.pitch =
frame->linesize[0];
2077 reg.resourceToRegister =
frame->data[0];
2080 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR;
2083 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX;
2084 reg.subResourceIndex = (intptr_t)
frame->data[1];
2088 if (reg.bufferFormat == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
2094 ret = p_nvenc->nvEncRegisterResource(
ctx->nvencoder, ®);
2095 if (
ret != NV_ENC_SUCCESS) {
2100 ctx->registered_frames[idx].ptr =
frame->data[0];
2101 ctx->registered_frames[idx].ptr_index = reg.subResourceIndex;
2102 ctx->registered_frames[idx].regptr = reg.registeredResource;
2111 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2114 NVENCSTATUS nv_status;
2127 if (!
ctx->registered_frames[reg_idx].mapped) {
2128 ctx->registered_frames[reg_idx].in_map.version = NV_ENC_MAP_INPUT_RESOURCE_VER;
2129 ctx->registered_frames[reg_idx].in_map.registeredResource =
ctx->registered_frames[reg_idx].regptr;
2130 nv_status = p_nvenc->nvEncMapInputResource(
ctx->nvencoder, &
ctx->registered_frames[reg_idx].in_map);
2131 if (nv_status != NV_ENC_SUCCESS) {
2137 ctx->registered_frames[reg_idx].mapped += 1;
2139 nvenc_frame->
reg_idx = reg_idx;
2140 nvenc_frame->
input_surface =
ctx->registered_frames[reg_idx].in_map.mappedResource;
2141 nvenc_frame->
format =
ctx->registered_frames[reg_idx].in_map.mappedBufferFmt;
2146 NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
2148 lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
2151 nv_status = p_nvenc->nvEncLockInputBuffer(
ctx->nvencoder, &lockBufferParams);
2152 if (nv_status != NV_ENC_SUCCESS) {
2153 return nvenc_print_error(avctx, nv_status,
"Failed locking nvenc input buffer");
2156 nvenc_frame->
pitch = lockBufferParams.pitch;
2159 nv_status = p_nvenc->nvEncUnlockInputBuffer(
ctx->nvencoder, nvenc_frame->
input_surface);
2160 if (nv_status != NV_ENC_SUCCESS) {
2169 NV_ENC_PIC_PARAMS *params,
2170 NV_ENC_SEI_PAYLOAD *sei_data,
2177 params->codecPicParams.h264PicParams.sliceMode =
2178 ctx->encode_config.encodeCodecConfig.h264Config.sliceMode;
2179 params->codecPicParams.h264PicParams.sliceModeData =
2180 ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
2181 if (sei_count > 0) {
2182 params->codecPicParams.h264PicParams.seiPayloadArray = sei_data;
2183 params->codecPicParams.h264PicParams.seiPayloadArrayCnt = sei_count;
2188 params->codecPicParams.hevcPicParams.sliceMode =
2189 ctx->encode_config.encodeCodecConfig.hevcConfig.sliceMode;
2190 params->codecPicParams.hevcPicParams.sliceModeData =
2191 ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
2192 if (sei_count > 0) {
2193 params->codecPicParams.hevcPicParams.seiPayloadArray = sei_data;
2194 params->codecPicParams.hevcPicParams.seiPayloadArrayCnt = sei_count;
2198 #if CONFIG_AV1_NVENC_ENCODER
2200 params->codecPicParams.av1PicParams.numTileColumns =
2201 ctx->encode_config.encodeCodecConfig.av1Config.numTileColumns;
2202 params->codecPicParams.av1PicParams.numTileRows =
2203 ctx->encode_config.encodeCodecConfig.av1Config.numTileRows;
2204 if (sei_count > 0) {
2205 params->codecPicParams.av1PicParams.obuPayloadArray = sei_data;
2206 params->codecPicParams.av1PicParams.obuPayloadArrayCnt = sei_count;
2229 NV_ENC_LOCK_BITSTREAM *params,
2234 pkt->
pts = params->outputTimeStamp;
2239 #if FF_API_TICKS_PER_FRAME
2242 FFMAX(
ctx->encode_config.frameIntervalP - 1, 0);
2256 int idx =
ctx->frame_data_array_pos;
2271 #if FF_API_REORDERED_OPAQUE
2277 ctx->frame_data_array_pos = (
ctx->frame_data_array_pos + 1) %
ctx->frame_data_array_nb;
2278 pic_params->inputDuration = idx;
2288 int idx = lock_params->outputDuration;
2293 #if FF_API_REORDERED_OPAQUE
2295 avctx->reordered_opaque =
frame_data->reordered_opaque;
2314 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2316 NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
2317 NVENCSTATUS nv_status;
2322 lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
2324 lock_params.doNotWait = 0;
2327 nv_status = p_nvenc->nvEncLockBitstream(
ctx->nvencoder, &lock_params);
2328 if (nv_status != NV_ENC_SUCCESS) {
2340 memcpy(
pkt->
data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
2342 nv_status = p_nvenc->nvEncUnlockBitstream(
ctx->nvencoder, tmpoutsurf->
output_surface);
2343 if (nv_status != NV_ENC_SUCCESS) {
2344 res =
nvenc_print_error(avctx, nv_status,
"Failed unlocking bitstream buffer, expect the gates of mordor to open");
2350 ctx->registered_frames[tmpoutsurf->
reg_idx].mapped -= 1;
2351 if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped == 0) {
2352 nv_status = p_nvenc->nvEncUnmapInputResource(
ctx->nvencoder,
ctx->registered_frames[tmpoutsurf->
reg_idx].in_map.mappedResource);
2353 if (nv_status != NV_ENC_SUCCESS) {
2357 }
else if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped < 0) {
2367 switch (lock_params.pictureType) {
2368 case NV_ENC_PIC_TYPE_IDR:
2370 case NV_ENC_PIC_TYPE_I:
2373 case NV_ENC_PIC_TYPE_P:
2376 case NV_ENC_PIC_TYPE_B:
2379 case NV_ENC_PIC_TYPE_BI:
2383 av_log(avctx,
AV_LOG_ERROR,
"Unknown picture type encountered, expect the output to be broken.\n");
2384 av_log(avctx,
AV_LOG_ERROR,
"Please report this error and include as much information on how to reproduce it as possible.\n");
2412 int nb_ready, nb_pending;
2417 return nb_ready > 0;
2418 return (nb_ready > 0) && (nb_ready + nb_pending >=
ctx->async_depth);
2428 void *a53_data =
NULL;
2429 size_t a53_size = 0;
2437 &
ctx->sei_data_size,
2438 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2445 ctx->sei_data[sei_count].payloadSize = (uint32_t)a53_size;
2446 ctx->sei_data[sei_count].payload = (uint8_t*)a53_data;
2448 #if CONFIG_AV1_NVENC_ENCODER
2461 void *tc_data =
NULL;
2470 &
ctx->sei_data_size,
2471 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2478 ctx->sei_data[sei_count].payloadSize = (uint32_t)tc_size;
2479 ctx->sei_data[sei_count].payload = (uint8_t*)tc_data;
2481 #if CONFIG_AV1_NVENC_ENCODER
2496 for (
i = 0;
i <
frame->nb_side_data;
i++) {
2504 &
ctx->sei_data_size,
2505 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2511 ctx->sei_data[sei_count].payloadSize = side_data->
size;
2515 if (!
ctx->sei_data[sei_count].payload) {
2527 for (
i = 0;
i < sei_count;
i++)
2536 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
2539 NV_ENC_RECONFIGURE_PARAMS params = { 0 };
2540 int needs_reconfig = 0;
2541 int needs_encode_config = 0;
2542 int reconfig_bitrate = 0, reconfig_dar = 0;
2545 params.version = NV_ENC_RECONFIGURE_PARAMS_VER;
2546 params.reInitEncodeParams =
ctx->init_encode_params;
2549 if (dw !=
ctx->init_encode_params.darWidth || dh !=
ctx->init_encode_params.darHeight) {
2551 "aspect ratio change (DAR): %d:%d -> %d:%d\n",
2552 ctx->init_encode_params.darWidth,
2553 ctx->init_encode_params.darHeight, dw, dh);
2555 params.reInitEncodeParams.darHeight = dh;
2556 params.reInitEncodeParams.darWidth = dw;
2562 if (
ctx->rc != NV_ENC_PARAMS_RC_CONSTQP &&
ctx->support_dyn_bitrate) {
2563 if (avctx->
bit_rate > 0 && params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate != avctx->
bit_rate) {
2565 "avg bitrate change: %d -> %d\n",
2566 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate,
2569 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate = avctx->
bit_rate;
2570 reconfig_bitrate = 1;
2575 "max bitrate change: %d -> %d\n",
2576 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate,
2579 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate = avctx->
rc_max_rate;
2580 reconfig_bitrate = 1;
2585 "vbv buffer size change: %d -> %d\n",
2586 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize,
2589 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize = avctx->
rc_buffer_size;
2590 reconfig_bitrate = 1;
2593 if (reconfig_bitrate) {
2594 params.resetEncoder = 1;
2595 params.forceIDR = 1;
2597 needs_encode_config = 1;
2602 if (!needs_encode_config)
2603 params.reInitEncodeParams.encodeConfig =
NULL;
2605 if (needs_reconfig) {
2606 ret = p_nvenc->nvEncReconfigureEncoder(
ctx->nvencoder, ¶ms);
2607 if (
ret != NV_ENC_SUCCESS) {
2611 ctx->init_encode_params.darHeight = dh;
2612 ctx->init_encode_params.darWidth = dw;
2615 if (reconfig_bitrate) {
2616 ctx->encode_config.rcParams.averageBitRate = params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate;
2617 ctx->encode_config.rcParams.maxBitRate = params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate;
2618 ctx->encode_config.rcParams.vbvBufferSize = params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize;
2627 NVENCSTATUS nv_status;
2635 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2637 NV_ENC_PIC_PARAMS pic_params = { 0 };
2638 pic_params.version = NV_ENC_PIC_PARAMS_VER;
2640 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
2664 pic_params.bufferFmt = in_surf->
format;
2665 pic_params.inputWidth = in_surf->
width;
2666 pic_params.inputHeight = in_surf->
height;
2667 pic_params.inputPitch = in_surf->
pitch;
2672 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
2674 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
2676 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
2680 pic_params.encodePicFlags =
2681 ctx->forced_idr ? NV_ENC_PIC_FLAG_FORCEIDR : NV_ENC_PIC_FLAG_FORCEINTRA;
2683 pic_params.encodePicFlags = 0;
2686 pic_params.inputTimeStamp =
frame->pts;
2688 if (
ctx->extra_sei) {
2701 pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
2708 nv_status = p_nvenc->nvEncEncodePicture(
ctx->nvencoder, &pic_params);
2710 for (
i = 0;
i < sei_count;
i++)
2717 if (nv_status != NV_ENC_SUCCESS &&
2718 nv_status != NV_ENC_ERR_NEED_MORE_INPUT)
2729 if (nv_status == NV_ENC_SUCCESS) {
2730 while (
av_fifo_read(
ctx->output_surface_queue, &tmp_out_surf, 1) >= 0)
2746 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
2749 if (!
frame->buf[0]) {