1 /*
2 * H.264/HEVC hardware encoding using nvidia nvenc
3 * Copyright (c) 2016 Timo Rothenpieler <timo@rothenpieler.org>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "config.h"
23 #include "config_components.h"
24
27
38
39 #define CHECK_CU(x) FF_CUDA_CHECK_DL(avctx, dl_fn->cuda_dl, x)
40
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)
45
62 #if CONFIG_D3D11VA
64 #endif
66 };
67
71 #if CONFIG_D3D11VA
74 #endif
76 };
77
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)
82
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)
87
88 #define IS_GBRP(pix_fmt) (pix_fmt == AV_PIX_FMT_GBRP || \
89 pix_fmt == AV_PIX_FMT_GBRP16)
90
91 static const struct {
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" },
122 };
123
125 {
132 }
133 }
135 *
desc =
"unknown error";
137 }
138
140 const char *error_string)
141 {
143 const char *details = "(no details)";
145
146 #ifdef NVENC_HAVE_GETLASTERRORSTRING
148 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
149
150 if (p_nvenc &&
ctx->nvencoder)
151 details = p_nvenc->nvEncGetLastErrorString(
ctx->nvencoder);
152 #endif
153
155
157 }
158
163
164 #define PRESET_ALIAS(alias, name, ...) \
165 [PRESET_ ## alias] = { NV_ENC_PRESET_ ## name ## _GUID, __VA_ARGS__ }
166
167 #define PRESET(name, ...) PRESET_ALIAS(name, name, __VA_ARGS__)
168
170 {
172 #ifdef NVENC_HAVE_NEW_PRESETS
183 // Compat aliases
193 #else
206 #endif
207 };
208
210
211 ctx->init_encode_params.presetGUID = t->
guid;
213
214 #ifdef NVENC_HAVE_NEW_PRESETS
215 if (
ctx->tuning_info == NV_ENC_TUNING_INFO_LOSSLESS)
217 #endif
218 }
219
220 #undef PRESET
221 #undef PRESET_ALIAS
222
224 {
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";
230 # else
231 const char *minver = "470.57.02";
232 # endif
233 #elif NVENCAPI_CHECK_VERSION(11, 0)
234 # if defined(_WIN32) || defined(__CYGWIN__)
235 const char *minver = "456.71";
236 # else
237 const char *minver = "455.28";
238 # endif
239 #elif NVENCAPI_CHECK_VERSION(10, 0)
240 # if defined(_WIN32) || defined(__CYGWIN__)
241 const char *minver = "450.51";
242 # else
243 const char *minver = "445.87";
244 # endif
245 #elif NVENCAPI_CHECK_VERSION(9, 1)
246 # if defined(_WIN32) || defined(__CYGWIN__)
247 const char *minver = "436.15";
248 # else
249 const char *minver = "435.21";
250 # endif
251 #elif NVENCAPI_CHECK_VERSION(9, 0)
252 # if defined(_WIN32) || defined(__CYGWIN__)
253 const char *minver = "418.81";
254 # else
255 const char *minver = "418.30";
256 # endif
257 #elif NVENCAPI_CHECK_VERSION(8, 2)
258 # if defined(_WIN32) || defined(__CYGWIN__)
259 const char *minver = "397.93";
260 # else
261 const char *minver = "396.24";
262 #endif
263 #elif NVENCAPI_CHECK_VERSION(8, 1)
264 # if defined(_WIN32) || defined(__CYGWIN__)
265 const char *minver = "390.77";
266 # else
267 const char *minver = "390.25";
268 # endif
269 #else
270 # if defined(_WIN32) || defined(__CYGWIN__)
271 const char *minver = "378.66";
272 # else
273 const char *minver = "378.13";
274 # endif
275 #endif
276 av_log(avctx,
level,
"The minimum required Nvidia driver for nvenc is %s or newer\n", minver);
277 }
278
280 {
283 NVENCSTATUS err;
284 uint32_t nvenc_max_ver;
286
287 ret = cuda_load_functions(&dl_fn->
cuda_dl, avctx);
290
291 ret = nvenc_load_functions(&dl_fn->
nvenc_dl, avctx);
295 }
296
297 err = dl_fn->
nvenc_dl->NvEncodeAPIGetMaxSupportedVersion(&nvenc_max_ver);
298 if (err != NV_ENC_SUCCESS)
300
302
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);
310 }
311
312 dl_fn->
nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
313
315 if (err != NV_ENC_SUCCESS)
317
319
320 return 0;
321 }
322
324 {
327
328 if (
ctx->d3d11_device)
329 return 0;
330
332 }
333
335 {
339
340 if (
ctx->d3d11_device)
341 return 0;
342
344 }
345
347 {
348 NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = { 0 };
350 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
352
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;
358 } else {
359 params.device =
ctx->cu_context;
360 params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
361 }
362
363 ret = p_nvenc->nvEncOpenEncodeSessionEx(¶ms, &
ctx->nvencoder);
364 if (
ret != NV_ENC_SUCCESS) {
367 }
368
369 return 0;
370 }
371
373 {
375 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
376 int i,
ret, count = 0;
378
379 ret = p_nvenc->nvEncGetEncodeGUIDCount(
ctx->nvencoder, &count);
380
381 if (
ret != NV_ENC_SUCCESS || !count)
383
385 if (!guids)
387
388 ret = p_nvenc->nvEncGetEncodeGUIDs(
ctx->nvencoder, guids, count, &count);
389 if (
ret != NV_ENC_SUCCESS) {
392 }
393
395 for (
i = 0;
i < count;
i++) {
396 if (!memcmp(&guids[
i], &
ctx->init_encode_params.encodeGUID,
sizeof(*guids))) {
398 break;
399 }
400 }
401
404
406 }
407
409 {
411 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
412 NV_ENC_CAPS_PARAM params = { 0 };
414
415 params.version = NV_ENC_CAPS_PARAM_VER;
416 params.capsToQuery = cap;
417
418 ret = p_nvenc->nvEncGetEncodeCaps(
ctx->nvencoder,
ctx->init_encode_params.encodeGUID, ¶ms, &
val);
419
420 if (
ret == NV_ENC_SUCCESS)
422 return 0;
423 }
424
426 {
429
434 }
435
440 }
441
446 }
447
449 if (ret < avctx->
width) {
453 }
454
456 if (ret < avctx->
height) {
460 }
461
463 if (ret < avctx->max_b_frames) {
466
468 }
469
473 "Interlaced encoding is not supported. Supported level: %d\n",
476 }
477
482 }
483
485 if (
ctx->rc_lookahead > 0 &&
ret <= 0) {
488 }
489
491 if (
ctx->temporal_aq > 0 &&
ret <= 0) {
494 }
495
497 if (
ctx->weighted_pred > 0 &&
ret <= 0) {
500 }
501
503 if (
ctx->coder == NV_ENC_H264_ENTROPY_CODING_MODE_CABAC &&
ret <= 0) {
506 }
507
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) {
516 }
517 #else
518 if (
ctx->b_ref_mode != 0) {
521 }
522 #endif
523
524 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
526 if(avctx->
refs != NV_ENC_NUM_REF_FRAMES_AUTOSELECT &&
ret <= 0) {
529 }
530 #else
531 if(avctx->
refs != 0) {
534 }
535 #endif
536
537 #ifdef NVENC_HAVE_SINGLE_SLICE_INTRA_REFRESH
539 if(
ctx->single_slice_intra_refresh &&
ret <= 0) {
542 }
543 #else
544 if(
ctx->single_slice_intra_refresh) {
547 }
548 #endif
549
551 if((
ctx->intra_refresh ||
ctx->single_slice_intra_refresh) &&
ret <= 0) {
554 }
555
556 #ifndef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
560 }
561 #endif
562
564 if(
ctx->constrained_encoding &&
ret <= 0) {
567 }
568
569 ctx->support_dyn_bitrate =
nvenc_check_cap(avctx, NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE);
570
571 return 0;
572 }
573
575 {
578 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
579 char name[128] = { 0};
581 CUdevice cu_device;
583
586
590
594
598
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");
603 }
604
606 return -1;
607
611
612 ctx->cu_context =
ctx->cu_context_internal;
614
616 goto fail2;
617
619 goto fail2;
620
622 goto fail3;
623
624 av_log(avctx, loglevel,
"supports NVENC\n");
625
627
629 return 0;
630
631 fail3:
634
635 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
637
640
641 fail2:
643 ctx->cu_context_internal =
NULL;
644
647 }
648
650 {
653
656 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_H264_GUID;
657 break;
659 ctx->init_encode_params.encodeGUID = NV_ENC_CODEC_HEVC_GUID;
660 break;
661 default:
663 }
664
666
668 av_log(avctx,
AV_LOG_WARNING,
"The selected preset is deprecated. Use p1 to p7 + -tune or fast/medium/slow.\n");
669
674 #if CONFIG_D3D11VA
676 #endif
678
683 #if CONFIG_D3D11VA
686 #endif
687 else
692 cuda_device_hwctx = hwdev_ctx->
hwctx;
693 #if CONFIG_D3D11VA
695 d3d11_device_hwctx = hwdev_ctx->
hwctx;
696 #endif
697 else
699 } else {
701 }
702
703 if (cuda_device_hwctx) {
705 ctx->cu_stream = cuda_device_hwctx->
stream;
706 }
707 #if CONFIG_D3D11VA
708 else if (d3d11_device_hwctx) {
709 ctx->d3d11_device = d3d11_device_hwctx->
device;
710 ID3D11Device_AddRef(
ctx->d3d11_device);
711 }
712 #endif
713
717
720 av_log(avctx,
AV_LOG_FATAL,
"Provided device doesn't support required NVENC features\n");
722 }
723 } else {
724 int i, nb_devices = 0;
725
728
731
732 if (!nb_devices) {
735 }
736
738
740 for (
i = 0;
i < nb_devices; ++
i) {
742 return 0;
743 }
744
747
751 }
752
753 av_log(avctx,
AV_LOG_FATAL,
"Requested GPU %d, but only %d GPUs are available!\n",
ctx->device, nb_devices);
755 }
756
757 return 0;
758 }
759
761 {
763 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
764
765 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
766
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(
777 } else {
778 rc->constQP.qpIntra = rc->constQP.qpInterP;
779 rc->constQP.qpInterB = rc->constQP.qpInterP;
780 }
781 }
else if (
ctx->cqp >= 0) {
782 rc->constQP.qpInterP = rc->constQP.qpInterB = rc->constQP.qpIntra =
ctx->cqp;
787 }
788
791 }
792
794 {
796 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
797 int qp_inter_p;
798
799 if (avctx->
qmin >= 0 && avctx->
qmax >= 0) {
800 rc->enableMinQP = 1;
801 rc->enableMaxQP = 1;
802
803 rc->minQP.qpInterB = avctx->
qmin;
804 rc->minQP.qpInterP = avctx->
qmin;
805 rc->minQP.qpIntra = avctx->
qmin;
806
807 rc->maxQP.qpInterB = avctx->
qmax;
808 rc->maxQP.qpInterP = avctx->
qmax;
809 rc->maxQP.qpIntra = avctx->
qmax;
810
811 qp_inter_p = (avctx->
qmax + 3 * avctx->
qmin) / 4;
// biased towards Qmin
812 }
else if (avctx->
qmin >= 0) {
813 rc->enableMinQP = 1;
814
815 rc->minQP.qpInterB = avctx->
qmin;
816 rc->minQP.qpInterP = avctx->
qmin;
817 rc->minQP.qpIntra = avctx->
qmin;
818
819 qp_inter_p = avctx->
qmin;
820 } else {
821 qp_inter_p = 26; // default to 26
822 }
823
824 rc->enableInitialRCQP = 1;
825
826 if (
ctx->init_qp_p < 0) {
827 rc->initialRCQP.qpInterP = qp_inter_p;
828 } else {
829 rc->initialRCQP.qpInterP =
ctx->init_qp_p;
830 }
831
832 if (
ctx->init_qp_i < 0) {
834 rc->initialRCQP.qpIntra =
av_clip(
836 } else {
837 rc->initialRCQP.qpIntra = rc->initialRCQP.qpInterP;
838 }
839 } else {
840 rc->initialRCQP.qpIntra =
ctx->init_qp_i;
841 }
842
843 if (
ctx->init_qp_b < 0) {
845 rc->initialRCQP.qpInterB =
av_clip(
847 } else {
848 rc->initialRCQP.qpInterB = rc->initialRCQP.qpInterP;
849 }
850 } else {
851 rc->initialRCQP.qpInterB =
ctx->init_qp_b;
852 }
853 }
854
856 {
858 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
859
860 rc->rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
861 rc->constQP.qpInterB = 0;
862 rc->constQP.qpInterP = 0;
863 rc->constQP.qpIntra = 0;
864
867 }
868
870 {
872 NV_ENC_RC_PARAMS *rc = &
ctx->encode_config.rcParams;
873
875 case NV_ENC_PARAMS_RC_CONSTQP:
877 return;
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");
884 return;
885 }
886 /* fall through */
887 case NV_ENC_PARAMS_RC_VBR_HQ:
888 case NV_ENC_PARAMS_RC_VBR:
890 break;
891 case NV_ENC_PARAMS_RC_CBR:
892 case NV_ENC_PARAMS_RC_CBR_HQ:
893 case NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ:
894 break;
895 }
896
897 rc->rateControlMode =
ctx->rc;
898 }
899
901 {
903 // default minimum of 4 surfaces
904 // multiply by 2 for number of NVENCs on gpu (hardcode to 2)
905 // another multiply by 2 to avoid blocking next PBB group
906 int nb_surfaces =
FFMAX(4,
ctx->encode_config.frameIntervalP * 2 * 2);
907
908 // lookahead enabled
909 if (
ctx->rc_lookahead > 0) {
910 // +1 is to account for lkd_bound calculation later
911 // +4 is to allow sufficient pipelining with lookahead
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)
914 {
916 "Defined rc_lookahead requires more surfaces, "
917 "increasing used surfaces %d -> %d\n",
ctx->nb_surfaces, nb_surfaces);
918 }
919 ctx->nb_surfaces =
FFMAX(nb_surfaces,
ctx->nb_surfaces);
920 } else {
921 if (
ctx->encode_config.frameIntervalP > 1 &&
ctx->nb_surfaces < nb_surfaces &&
ctx->nb_surfaces > 0)
922 {
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);
927 }
928 else if (
ctx->nb_surfaces <= 0)
929 ctx->nb_surfaces = nb_surfaces;
930 // otherwise use user specified value
931 }
932
935
936 return 0;
937 }
938
940 {
942
944 av_log(avctx,
AV_LOG_WARNING,
"Using global_quality with nvenc is deprecated. Use qp instead.\n");
945
948
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;
953 }
954
957
958 #ifdef NVENC_HAVE_MULTIPASS
959 ctx->encode_config.rcParams.multiPass =
ctx->multipass;
960
962 ctx->encode_config.rcParams.multiPass = NV_ENC_MULTI_PASS_DISABLED;
964 ctx->encode_config.rcParams.multiPass = NV_ENC_TWO_PASS_FULL_RESOLUTION;
965
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;
973 }
974 }
975 #else
981
982 if (
ctx->twopass < 0)
984
987 ctx->rc = NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ;
988 } else {
989 ctx->rc = NV_ENC_PARAMS_RC_CBR;
990 }
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;
997 }
998 }
999 #endif
1000
1004
1006 }
1007
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;
1011 #else
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");
1014 #endif
1015
1016 #ifdef NVENC_HAVE_LDKFS
1018 ctx->encode_config.rcParams.lowDelayKeyFrameScale =
ctx->ldkfs;
1019 #endif
1020
1023 }
else if (
ctx->rc >= 0) {
1025 } else {
1026 ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
1028 }
1029
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;
1034 }
1035
1037 ctx->encode_config.rcParams.enableAQ = 1;
1038 ctx->encode_config.rcParams.aqStrength =
ctx->aq_strength;
1040 }
1041
1042 if (
ctx->temporal_aq) {
1043 ctx->encode_config.rcParams.enableTemporalAQ = 1;
1045 }
1046
1047 if (
ctx->rc_lookahead > 0) {
1048 int lkd_bound =
FFMIN(
ctx->nb_surfaces,
ctx->async_depth) -
1049 ctx->encode_config.frameIntervalP - 4;
1050
1051 if (lkd_bound < 0) {
1053 "Lookahead not enabled. Increase buffer delay (-delay).\n");
1054 } else {
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");
1064 }
1065 }
1066
1067 if (
ctx->strict_gop) {
1068 ctx->encode_config.rcParams.strictGOPTarget = 1;
1070 }
1071
1073 ctx->encode_config.rcParams.enableNonRefP = 1;
1074
1075 if (
ctx->zerolatency)
1076 ctx->encode_config.rcParams.zeroReorderDelay = 1;
1077
1079 //convert from float to fixed point 8.8
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);
1083
1085
1086 //CQ mode shall discard avg bitrate & honor max bitrate;
1087 ctx->encode_config.rcParams.averageBitRate = avctx->
bit_rate = 0;
1089 }
1090 }
1091
1093 {
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;
1098
1101 vui->transferCharacteristics = avctx->
color_trc;
1104
1105 vui->colourDescriptionPresentFlag =
1106 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1107
1108 vui->videoSignalTypePresentFlag =
1109 (vui->colourDescriptionPresentFlag
1110 || vui->videoFormat != 5
1111 || vui->videoFullRangeFlag != 0);
1112
1113 h264->sliceMode = 3;
1114 h264->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1115
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;
1122 #endif
1123 }
1124
1125 if (
ctx->constrained_encoding)
1126 h264->enableConstrainedEncoding = 1;
1127
1130 h264->outputAUD =
ctx->aud;
1131
1132 if (
ctx->dpb_size >= 0) {
1133 /* 0 means "let the hardware decide" */
1134 h264->maxNumRefFrames =
ctx->dpb_size;
1135 }
1136
1137 if (
ctx->intra_refresh) {
1138 h264->idrPeriod = NVENC_INFINITE_GOPLENGTH;
1141 }
1142
1143 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1144 h264->outputBufferingPeriodSEI = 1;
1145 }
1146
1147 h264->outputPictureTimingSEI = 1;
1148
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;
1154 }
1155
1157 h264->qpPrimeYZeroTransformBypassFlag = 1;
1158 } else {
1159 switch(
ctx->profile) {
1161 cc->profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
1163 break;
1165 cc->profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
1167 break;
1169 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
1171 break;
1173 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1175 break;
1176 }
1177 }
1178
1179 // force setting profile as high444p if input is AV_PIX_FMT_YUV444P
1181 cc->profileGUID = NV_ENC_H264_PROFILE_HIGH_444_GUID;
1183 }
1184
1186
1187 h264->level =
ctx->level;
1188
1189 if (
ctx->coder >= 0)
1190 h264->entropyCodingMode =
ctx->coder;
1191
1192 #ifdef NVENC_HAVE_BFRAME_REF_MODE
1193 h264->useBFramesAsRef =
ctx->b_ref_mode;
1194 #endif
1195
1196 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1197 h264->numRefL0 = avctx->
refs;
1198 h264->numRefL1 = avctx->
refs;
1199 #endif
1200
1201 return 0;
1202 }
1203
1205 {
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;
1210
1213 vui->transferCharacteristics = avctx->
color_trc;
1216
1217 vui->colourDescriptionPresentFlag =
1218 (vui->colourMatrix != 2 || vui->colourPrimaries != 2 || vui->transferCharacteristics != 2);
1219
1220 vui->videoSignalTypePresentFlag =
1221 (vui->colourDescriptionPresentFlag
1222 || vui->videoFormat != 5
1223 || vui->videoFullRangeFlag != 0);
1224
1225 hevc->sliceMode = 3;
1226 hevc->sliceModeData = avctx->
slices > 0 ? avctx->
slices : 1;
1227
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;
1234 #endif
1235 }
1236
1237 #ifdef NVENC_HAVE_HEVC_CONSTRAINED_ENCODING
1238 if (
ctx->constrained_encoding)
1239 hevc->enableConstrainedEncoding = 1;
1240 #endif
1241
1244 hevc->outputAUD =
ctx->aud;
1245
1246 if (
ctx->dpb_size >= 0) {
1247 /* 0 means "let the hardware decide" */
1248 hevc->maxNumRefFramesInDPB =
ctx->dpb_size;
1249 }
1250
1251 if (
ctx->intra_refresh) {
1252 hevc->idrPeriod = NVENC_INFINITE_GOPLENGTH;
1255 }
1256
1257 if (
IS_CBR(cc->rcParams.rateControlMode)) {
1258 hevc->outputBufferingPeriodSEI = 1;
1259 }
1260
1261 hevc->outputPictureTimingSEI = 1;
1262
1263 switch (
ctx->profile) {
1265 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
1267 break;
1269 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1271 break;
1273 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1275 break;
1276 }
1277
1278 // force setting profile as main10 if input is 10 bit
1280 cc->profileGUID = NV_ENC_HEVC_PROFILE_MAIN10_GUID;
1282 }
1283
1284 // force setting profile as rext if input is yuv444
1286 cc->profileGUID = NV_ENC_HEVC_PROFILE_FREXT_GUID;
1288 }
1289
1290 hevc->chromaFormatIDC =
IS_YUV444(
ctx->data_pix_fmt) ? 3 : 1;
1291
1292 hevc->pixelBitDepthMinus8 =
IS_10BIT(
ctx->data_pix_fmt) ? 2 : 0;
1293
1294 hevc->level =
ctx->level;
1295
1296 hevc->tier =
ctx->tier;
1297
1298 #ifdef NVENC_HAVE_HEVC_BFRAME_REF_MODE
1299 hevc->useBFramesAsRef =
ctx->b_ref_mode;
1300 #endif
1301
1302 #ifdef NVENC_HAVE_MULTIPLE_REF_FRAMES
1303 hevc->numRefL0 = avctx->
refs;
1304 hevc->numRefL1 = avctx->
refs;
1305 #endif
1306
1307 return 0;
1308 }
1309
1311 {
1317 /* Earlier switch/case will return if unknown codec is passed. */
1318 }
1319
1320 return 0;
1321 }
1322
1324 int sw, sh;
1325
1328
1332 }
1333
1335 }
1336
1338 {
1341 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1342
1343 NV_ENC_PRESET_CONFIG preset_config = { 0 };
1344 NVENCSTATUS nv_status = NV_ENC_SUCCESS;
1346 int res = 0;
1347 int dw, dh;
1348
1349 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1350 ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
1351
1352 ctx->init_encode_params.encodeHeight = avctx->
height;
1353 ctx->init_encode_params.encodeWidth = avctx->
width;
1354
1355 ctx->init_encode_params.encodeConfig = &
ctx->encode_config;
1356
1357 preset_config.version = NV_ENC_PRESET_CONFIG_VER;
1358 preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
1359
1360 #ifdef NVENC_HAVE_NEW_PRESETS
1361 ctx->init_encode_params.tuningInfo =
ctx->tuning_info;
1362
1364 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOSSLESS;
1366 ctx->init_encode_params.tuningInfo = NV_ENC_TUNING_INFO_LOW_LATENCY;
1367
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,
1372 &preset_config);
1373 #else
1374 nv_status = p_nvenc->nvEncGetEncodePresetConfig(
ctx->nvencoder,
1375 ctx->init_encode_params.encodeGUID,
1376 ctx->init_encode_params.presetGUID,
1377 &preset_config);
1378 #endif
1379 if (nv_status != NV_ENC_SUCCESS)
1380 return nvenc_print_error(avctx, nv_status,
"Cannot get the preset configuration");
1381
1382 memcpy(&
ctx->encode_config, &preset_config.presetCfg,
sizeof(
ctx->encode_config));
1383
1384 ctx->encode_config.version = NV_ENC_CONFIG_VER;
1385
1387 ctx->init_encode_params.darHeight = dh;
1388 ctx->init_encode_params.darWidth = dw;
1389
1393 } else {
1396 }
1397
1398 ctx->init_encode_params.enableEncodeAsync = 0;
1399 ctx->init_encode_params.enablePTD = 1;
1400
1401 #ifdef NVENC_HAVE_NEW_PRESETS
1402 /* If lookahead isn't set from CLI, use value from preset.
1403 * P6 & P7 presets may enable lookahead for better quality.
1404 * */
1405 if (
ctx->rc_lookahead == 0 &&
ctx->encode_config.rcParams.enableLookahead)
1406 ctx->rc_lookahead =
ctx->encode_config.rcParams.lookaheadDepth;
1407 #endif
1408
1409 if (
ctx->weighted_pred == 1)
1410 ctx->init_encode_params.enableWeightedPrediction = 1;
1411
1412 if (
ctx->bluray_compat) {
1418 /* maximum level depends on used resolution */
1419 break;
1421 ctx->level = NV_ENC_LEVEL_HEVC_51;
1422 ctx->tier = NV_ENC_TIER_HEVC_HIGH;
1423 break;
1424 }
1425 }
1426
1429 /* 0 is intra-only, 1 is I/P only, 2 is one B-Frame, 3 two B-frames, and so on. */
1431 }
1432
1435 ctx->encode_config.frameIntervalP = 0;
1436 ctx->encode_config.gopLength = 1;
1437 }
1438
1439 /* force to enable intra refresh */
1440 if(
ctx->single_slice_intra_refresh)
1441 ctx->intra_refresh = 1;
1442
1443 if (
ctx->intra_refresh)
1444 ctx->encode_config.gopLength = NVENC_INFINITE_GOPLENGTH;
1445
1447
1449
1451 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
1452 } else {
1453 ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
1454 }
1455
1457 if (res)
1458 return res;
1459
1461 if (res < 0)
1462 return res;
1463
1464 nv_status = p_nvenc->nvEncInitializeEncoder(
ctx->nvencoder, &
ctx->init_encode_params);
1465 if (nv_status != NV_ENC_SUCCESS) {
1468 }
1469
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) {
1476 }
1477 }
1478 #endif
1479
1481 if (res < 0)
1482 return res;
1483
1484 if (
ctx->encode_config.frameIntervalP > 1)
1486
1487 if (
ctx->encode_config.rcParams.averageBitRate > 0)
1488 avctx->
bit_rate =
ctx->encode_config.rcParams.averageBitRate;
1489
1491 if (!cpb_props)
1495 cpb_props->
buffer_size =
ctx->encode_config.rcParams.vbvBufferSize;
1496
1497 return 0;
1498 }
1499
1501 {
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;
1526 default:
1527 return NV_ENC_BUFFER_FORMAT_UNDEFINED;
1528 }
1529 }
1530
1532 {
1535 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1537
1538 NVENCSTATUS nv_status;
1539 NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };
1540 allocOut.version = NV_ENC_CREATE_BITSTREAM_BUFFER_VER;
1541
1544 if (!
ctx->surfaces[idx].in_ref)
1546 } else {
1547 NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
1548
1550 if (
ctx->surfaces[idx].format == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
1554 }
1555
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;
1560
1561 nv_status = p_nvenc->nvEncCreateInputBuffer(
ctx->nvencoder, &allocSurf);
1562 if (nv_status != NV_ENC_SUCCESS) {
1564 }
1565
1566 ctx->surfaces[idx].input_surface = allocSurf.inputBuffer;
1567 ctx->surfaces[idx].width = allocSurf.width;
1568 ctx->surfaces[idx].height = allocSurf.height;
1569 }
1570
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);
1577 return err;
1578 }
1579
1580 ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer;
1581
1583
1584 return 0;
1585 }
1586
1588 {
1590 int i, res = 0, res2;
1591
1595
1597 if (!
ctx->timestamp_list)
1599
1601 if (!
ctx->unused_surface_queue)
1603
1605 if (!
ctx->output_surface_queue)
1608 if (!
ctx->output_surface_ready_queue)
1610
1612 if (res < 0)
1613 return res;
1614
1615 for (
i = 0;
i <
ctx->nb_surfaces;
i++) {
1618 }
1619
1622 if (res2 < 0)
1623 return res2;
1624
1625 return res;
1626 }
1627
1629 {
1632 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1633
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;
1639
1640 payload.spsppsBuffer = tmpHeader;
1641 payload.inBufferSize = sizeof(tmpHeader);
1642 payload.outSPSPPSPayloadSize = &outSize;
1643
1644 nv_status = p_nvenc->nvEncGetSequenceParams(
ctx->nvencoder, &payload);
1645 if (nv_status != NV_ENC_SUCCESS) {
1647 }
1648
1651
1654 }
1655
1656 memcpy(avctx->
extradata, tmpHeader, outSize);
1657
1658 return 0;
1659 }
1660
1662 {
1665 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1667
1668 /* the encoder has to be flushed before it can be closed */
1669 if (
ctx->nvencoder) {
1670 NV_ENC_PIC_PARAMS params = { .version = NV_ENC_PIC_PARAMS_VER,
1671 .encodePicFlags = NV_ENC_PIC_FLAG_EOS };
1672
1674 if (res < 0)
1675 return res;
1676
1677 p_nvenc->nvEncEncodePicture(
ctx->nvencoder, ¶ms);
1678 }
1679
1684
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);
1691 }
1692 ctx->nb_registered_frames = 0;
1693 }
1694
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);
1701 }
1702 }
1704 ctx->nb_surfaces = 0;
1705
1707
1709
1710 if (
ctx->nvencoder) {
1711 p_nvenc->nvEncDestroyEncoder(
ctx->nvencoder);
1712
1714 if (res < 0)
1715 return res;
1716 }
1718
1719 if (
ctx->cu_context_internal)
1721 ctx->cu_context =
ctx->cu_context_internal =
NULL;
1722
1723 #if CONFIG_D3D11VA
1724 if (
ctx->d3d11_device) {
1725 ID3D11Device_Release(
ctx->d3d11_device);
1727 }
1728 #endif
1729
1730 nvenc_free_functions(&dl_fn->
nvenc_dl);
1731 cuda_free_functions(&dl_fn->
cuda_dl);
1732
1734
1736
1737 return 0;
1738 }
1739
1741 {
1744
1749 "hw_frames_ctx must be set when using GPU frames as input\n");
1751 }
1755 "hw_frames_ctx must match the GPU frame type\n");
1757 }
1759 } else {
1761 }
1762
1766
1769
1772
1775
1778
1782 }
1783
1784 return 0;
1785 }
1786
1788 {
1790
1792 // queue empty
1794
1795 return tmp_surf;
1796 }
1797
1799 NV_ENC_LOCK_INPUT_BUFFER *lock_buffer_params,
const AVFrame *
frame)
1800 {
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
1806 };
1807 uint8_t *dst_data[4];
1809
1811 dst_linesize[1] = dst_linesize[2] >>= 1;
1812
1814 lock_buffer_params->bufferDataPtr, dst_linesize);
1817
1819 FFSWAP(uint8_t*, dst_data[1], dst_data[2]);
1820
1824
1825 return 0;
1826 }
1827
1829 {
1832 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1833 NVENCSTATUS nv_status;
1834
1836
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) {
1842 if (first_round)
1843 continue;
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;
1849 }
1851 }
1852 }
1853 }
1854 } else {
1855 return ctx->nb_registered_frames++;
1856 }
1857
1860 }
1861
1863 {
1866 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1867
1869 NV_ENC_REGISTER_RESOURCE reg = { 0 };
1871
1872 for (
i = 0;
i <
ctx->nb_registered_frames;
i++) {
1877 }
1878
1880 if (idx < 0)
1881 return idx;
1882
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];
1888
1890 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_CUDADEVICEPTR;
1891 }
1893 reg.resourceType = NV_ENC_INPUT_RESOURCE_TYPE_DIRECTX;
1894 reg.subResourceIndex = (intptr_t)
frame->data[1];
1895 }
1896
1898 if (reg.bufferFormat == NV_ENC_BUFFER_FORMAT_UNDEFINED) {
1902 }
1903
1904 ret = p_nvenc->nvEncRegisterResource(
ctx->nvencoder, ®);
1905 if (
ret != NV_ENC_SUCCESS) {
1908 }
1909
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;
1913 return idx;
1914 }
1915
1918 {
1921 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
1922
1923 int res;
1924 NVENCSTATUS nv_status;
1925
1928 if (reg_idx < 0) {
1930 return reg_idx;
1931 }
1932
1934 if (res < 0)
1935 return res;
1936
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) {
1944 }
1945 }
1946
1947 ctx->registered_frames[reg_idx].mapped += 1;
1948
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;
1953
1954 return 0;
1955 } else {
1956 NV_ENC_LOCK_INPUT_BUFFER lockBufferParams = { 0 };
1957
1958 lockBufferParams.version = NV_ENC_LOCK_INPUT_BUFFER_VER;
1960
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");
1964 }
1965
1966 nvenc_frame->
pitch = lockBufferParams.pitch;
1968
1969 nv_status = p_nvenc->nvEncUnlockInputBuffer(
ctx->nvencoder, nvenc_frame->
input_surface);
1970 if (nv_status != NV_ENC_SUCCESS) {
1972 }
1973
1974 return res;
1975 }
1976 }
1977
1979 NV_ENC_PIC_PARAMS *params,
1980 NV_ENC_SEI_PAYLOAD *sei_data,
1981 int sei_count)
1982 {
1984
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;
1994 }
1995
1996 break;
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;
2005 }
2006
2007 break;
2008 }
2009 }
2010
2012 {
2014 }
2015
2017 {
2019 // The following call might fail if the queue is empty.
2021
2022 return timestamp;
2023 }
2024
2026 NV_ENC_LOCK_BITSTREAM *params,
2028 {
2030
2031 pkt->
pts = params->outputTimeStamp;
2033
2035
2036 return 0;
2037 }
2038
2040 {
2043 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2044
2045 uint32_t slice_mode_data;
2046 uint32_t *slice_offsets =
NULL;
2047 NV_ENC_LOCK_BITSTREAM lock_params = { 0 };
2048 NVENCSTATUS nv_status;
2049 int res = 0;
2050
2052
2055 slice_mode_data =
ctx->encode_config.encodeCodecConfig.h264Config.sliceModeData;
2056 break;
2058 slice_mode_data =
ctx->encode_config.encodeCodecConfig.hevcConfig.sliceModeData;
2059 break;
2060 default:
2064 }
2065 slice_offsets =
av_mallocz(slice_mode_data *
sizeof(*slice_offsets));
2066
2067 if (!slice_offsets) {
2070 }
2071
2072 lock_params.version = NV_ENC_LOCK_BITSTREAM_VER;
2073
2074 lock_params.doNotWait = 0;
2076 lock_params.sliceOffsets = slice_offsets;
2077
2078 nv_status = p_nvenc->nvEncLockBitstream(
ctx->nvencoder, &lock_params);
2079 if (nv_status != NV_ENC_SUCCESS) {
2082 }
2083
2085
2086 if (res < 0) {
2089 }
2090
2091 memcpy(
pkt->
data, lock_params.bitstreamBufferPtr, lock_params.bitstreamSizeInBytes);
2092
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");
2097 }
2098
2099
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) {
2107 }
2108 }
else if (
ctx->registered_frames[tmpoutsurf->
reg_idx].mapped < 0) {
2111 }
2112
2114
2116 }
2117
2118 switch (lock_params.pictureType) {
2119 case NV_ENC_PIC_TYPE_IDR:
2121 case NV_ENC_PIC_TYPE_I:
2123 break;
2124 case NV_ENC_PIC_TYPE_P:
2126 break;
2127 case NV_ENC_PIC_TYPE_B:
2129 break;
2130 case NV_ENC_PIC_TYPE_BI:
2132 break;
2133 default:
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");
2138 }
2139
2142
2144 if (res < 0)
2145 goto error2;
2146
2148
2149 return 0;
2150
2153
2154 error2:
2156
2157 return res;
2158 }
2159
2161 {
2163 int nb_ready, nb_pending;
2164
2168 return nb_ready > 0;
2169 return (nb_ready > 0) && (nb_ready + nb_pending >=
ctx->async_depth);
2170 }
2171
2173 {
2175 int sei_count = 0;
2177
2179 void *a53_data =
NULL;
2180 size_t a53_size = 0;
2181
2184 }
2185
2186 if (a53_data) {
2188 &
ctx->sei_data_size,
2189 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2194 } else {
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;
2199 sei_count++;
2200 }
2201 }
2202 }
2203
2205 void *tc_data =
NULL;
2206 size_t tc_size = 0;
2207
2210 }
2211
2212 if (tc_data) {
2214 &
ctx->sei_data_size,
2215 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2220 } else {
2222 ctx->sei_data[sei_count].payloadSize = (uint32_t)tc_size;
2224 ctx->sei_data[sei_count].payload = (uint8_t*)tc_data;
2225 sei_count++;
2226 }
2227 }
2228 }
2229
2231 return sei_count;
2232
2233 for (
i = 0;
i <
frame->nb_side_data;
i++) {
2236
2238 continue;
2239
2241 &
ctx->sei_data_size,
2242 (sei_count + 1) *
sizeof(*
ctx->sei_data));
2246 } else {
2248 ctx->sei_data[sei_count].payloadSize = side_data->
size;
2251
2252 if (!
ctx->sei_data[sei_count].payload) {
2255 }
2256
2257 sei_count++;
2258 }
2259 }
2260
2261 return sei_count;
2262
2264 for (
i = 0;
i < sei_count;
i++)
2266
2267 return res;
2268 }
2269
2271 {
2273 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &
ctx->nvenc_dload_funcs.nvenc_funcs;
2275
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;
2280 int dw, dh;
2281
2282 params.version = NV_ENC_RECONFIGURE_PARAMS_VER;
2283 params.reInitEncodeParams =
ctx->init_encode_params;
2284
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);
2291
2292 params.reInitEncodeParams.darHeight = dh;
2293 params.reInitEncodeParams.darWidth = dw;
2294
2295 needs_reconfig = 1;
2296 reconfig_dar = 1;
2297 }
2298
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,
2305
2306 params.reInitEncodeParams.encodeConfig->rcParams.averageBitRate = avctx->
bit_rate;
2307 reconfig_bitrate = 1;
2308 }
2309
2312 "max bitrate change: %d -> %d\n",
2313 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate,
2315
2316 params.reInitEncodeParams.encodeConfig->rcParams.maxBitRate = avctx->
rc_max_rate;
2317 reconfig_bitrate = 1;
2318 }
2319
2322 "vbv buffer size change: %d -> %d\n",
2323 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize,
2325
2326 params.reInitEncodeParams.encodeConfig->rcParams.vbvBufferSize = avctx->
rc_buffer_size;
2327 reconfig_bitrate = 1;
2328 }
2329
2330 if (reconfig_bitrate) {
2331 params.resetEncoder = 1;
2332 params.forceIDR = 1;
2333
2334 needs_encode_config = 1;
2335 needs_reconfig = 1;
2336 }
2337 }
2338
2339 if (!needs_encode_config)
2340 params.reInitEncodeParams.encodeConfig =
NULL;
2341
2342 if (needs_reconfig) {
2343 ret = p_nvenc->nvEncReconfigureEncoder(
ctx->nvencoder, ¶ms);
2344 if (
ret != NV_ENC_SUCCESS) {
2346 } else {
2347 if (reconfig_dar) {
2348 ctx->init_encode_params.darHeight = dh;
2349 ctx->init_encode_params.darWidth = dw;
2350 }
2351
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;
2356 }
2357
2358 }
2359 }
2360 }
2361
2363 {
2364 NVENCSTATUS nv_status;
2366 int res, res2;
2367 int sei_count = 0;
2369
2372 NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->
nvenc_funcs;
2373
2374 NV_ENC_PIC_PARAMS pic_params = { 0 };
2375 pic_params.version = NV_ENC_PIC_PARAMS_VER;
2376
2377 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
2379
2382 if (!in_surf)
2384
2386 if (res < 0)
2387 return res;
2388
2390
2392
2394 if (res2 < 0)
2395 return res2;
2396
2397 if (res)
2398 return res;
2399
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;
2406
2408 if (
frame->top_field_first)
2409 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_TOP_BOTTOM;
2410 else
2411 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FIELD_BOTTOM_TOP;
2412 } else {
2413 pic_params.pictureStruct = NV_ENC_PIC_STRUCT_FRAME;
2414 }
2415
2417 pic_params.encodePicFlags =
2418 ctx->forced_idr ? NV_ENC_PIC_FLAG_FORCEIDR : NV_ENC_PIC_FLAG_FORCEINTRA;
2419 } else {
2420 pic_params.encodePicFlags = 0;
2421 }
2422
2423 pic_params.inputTimeStamp =
frame->pts;
2424
2425 if (
ctx->extra_sei) {
2427 if (res < 0)
2428 return res;
2429 sei_count = res;
2430 }
2431
2433 } else {
2434 pic_params.encodePicFlags = NV_ENC_PIC_FLAG_EOS;
2435 }
2436
2438 if (res < 0)
2439 return res;
2440
2441 nv_status = p_nvenc->nvEncEncodePicture(
ctx->nvencoder, &pic_params);
2442
2443 for (
i = 0;
i < sei_count;
i++)
2445
2447 if (res < 0)
2448 return res;
2449
2450 if (nv_status != NV_ENC_SUCCESS &&
2451 nv_status != NV_ENC_ERR_NEED_MORE_INPUT)
2453
2457 }
2458
2459 /* all the pending buffers are now ready for output */
2460 if (nv_status == NV_ENC_SUCCESS) {
2461 while (
av_fifo_read(
ctx->output_surface_queue, &tmp_out_surf, 1) >= 0)
2463 }
2464
2465 return 0;
2466 }
2467
2469 {
2471 int res, res2;
2472
2474
2476
2477 if ((!
ctx->cu_context && !
ctx->d3d11_device) || !
ctx->nvencoder)
2479
2480 if (!
frame->buf[0]) {
2483 return res;
2484 }
2485
2487 if (res < 0) {
2489 return res;
2490 } else
2492
2495
2497 if (res < 0)
2498 return res;
2499
2501
2503 if (res2 < 0)
2504 return res2;
2505
2506 if (res)
2507 return res;
2508
2512 } else {
2514 }
2515
2516 return 0;
2517 }
2518
2520 {
2522
2525 }