-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplex_nvenc.patch
More file actions
1634 lines (1630 loc) · 56 KB
/
Copy pathplex_nvenc.patch
File metadata and controls
1634 lines (1630 loc) · 56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff -aur -N ffmpeg-snapshot/configure ffmpeg-snapshot/configure
--- ffmpeg-snapshot/configure 2015-04-10 05:18:28.000000000 +0200
+++ ffmpeg-snapshot/configure 2016-08-04 00:27:26.540842079 +0200
@@ -263,6 +263,7 @@
--enable-libzvbi enable teletext support via libzvbi [no]
--disable-lzma disable lzma [autodetect]
--enable-decklink enable Blackmagick DeckLink I/O support [no]
+ --enable-nvenc enable NVIDIA NVENC support [no]
--enable-openal enable OpenAL 1.1 capture support [no]
--enable-opencl enable OpenCL code
--enable-opengl enable OpenGL rendering [no]
@@ -1400,6 +1401,7 @@
libzmq
libzvbi
lzma
+ nvenc
openal
opencl
opengl
@@ -2398,6 +2400,7 @@
libutvideo_decoder_deps="libutvideo"
libutvideo_encoder_deps="libutvideo"
libzvbi_teletext_decoder_deps="libzvbi"
+nvenc_encoder_deps="nvenc"
# demuxers / muxers
ac3_demuxer_select="ac3_parser"
@@ -4354,6 +4357,7 @@
die_license_disabled nonfree libaacplus
die_license_disabled nonfree libfaac
+die_license_disabled nonfree nvenc
enabled gpl && die_license_disabled_gpl nonfree libfdk_aac
enabled gpl && die_license_disabled_gpl nonfree openssl
@@ -4663,6 +4667,7 @@
frei0r_filter_extralibs='$ldl'
frei0r_src_filter_extralibs='$ldl'
ladspa_filter_extralibs='$ldl'
+nvenc_encoder_extralibs='$ldl'
if ! disabled network; then
check_func getaddrinfo $network_extralibs
@@ -4926,6 +4931,8 @@
enabled libxvid && require libxvid xvid.h xvid_global -lxvidcore
enabled libzmq && require_pkg_config libzmq zmq.h zmq_ctx_new
enabled libzvbi && require libzvbi libzvbi.h vbi_decoder_new -lzvbi
+enabled nvenc && { check_header nvEncodeAPI.h || die "ERROR: nvEncodeAPI.h not found."; } &&
+ { [ $target_os != cygwin ] || die "ERROR: NVENC is not supported on Cygwin currently."; }
enabled openal && { { for al_libs in "${OPENAL_LIBS}" "-lopenal" "-lOpenAL32"; do
check_lib 'AL/al.h' alGetError "${al_libs}" && break; done } ||
die "ERROR: openal not found"; } &&
diff -aur -N ffmpeg-snapshot/libavcodec/Makefile ffmpeg-snapshot/libavcodec/Makefile
--- ffmpeg-snapshot/libavcodec/Makefile 2015-04-10 05:18:28.000000000 +0200
+++ ffmpeg-snapshot/libavcodec/Makefile 2016-08-04 00:27:26.544175394 +0200
@@ -347,6 +347,7 @@
OBJS-$(CONFIG_NELLYMOSER_DECODER) += nellymoserdec.o nellymoser.o
OBJS-$(CONFIG_NELLYMOSER_ENCODER) += nellymoserenc.o nellymoser.o
OBJS-$(CONFIG_NUV_DECODER) += nuv.o rtjpeg.o
+OBJS-$(CONFIG_NVENC_ENCODER) += nvenc.o
OBJS-$(CONFIG_ON2AVC_DECODER) += on2avc.o on2avcdata.o
OBJS-$(CONFIG_OPUS_DECODER) += opusdec.o opus.o opus_celt.o \
opus_imdct.o opus_silk.o \
diff -aur -N ffmpeg-snapshot/libavcodec/allcodecs.c ffmpeg-snapshot/libavcodec/allcodecs.c
--- ffmpeg-snapshot/libavcodec/allcodecs.c 2015-04-10 05:18:28.000000000 +0200
+++ ffmpeg-snapshot/libavcodec/allcodecs.c 2016-08-04 00:27:26.544175394 +0200
@@ -223,6 +223,7 @@
REGISTER_DECODER(MVC2, mvc2);
REGISTER_DECODER(MXPEG, mxpeg);
REGISTER_DECODER(NUV, nuv);
+ REGISTER_ENCODER(NVENC, nvenc);
REGISTER_DECODER(PAF_VIDEO, paf_video);
REGISTER_ENCDEC (PAM, pam);
REGISTER_ENCDEC (PBM, pbm);
diff -aur -N ffmpeg-snapshot/libavcodec/nvenc.c ffmpeg-snapshot/libavcodec/nvenc.c
--- ffmpeg-snapshot/libavcodec/nvenc.c 1970-01-01 01:00:00.000000000 +0100
+++ ffmpeg-snapshot/libavcodec/nvenc.c 2016-02-01 02:46:30.000000000 +0100
@@ -0,0 +1,1477 @@
+/*
+ * H.264 hardware encoding using nvidia nvenc
+ * Copyright (c) 2014 Timo Rothenpieler <timo@rothenpieler.org>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#if defined(_WIN32)
+#include <windows.h>
+#else
+#include <dlfcn.h>
+#endif
+
+#include <nvEncodeAPI.h>
+
+#include "libavutil/internal.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/avassert.h"
+#include "libavutil/opt.h"
+#include "libavutil/mem.h"
+#include "avcodec.h"
+#include "internal.h"
+#include "thread.h"
+
+#if defined(_WIN32)
+#define CUDAAPI __stdcall
+#else
+#define CUDAAPI
+#endif
+
+#if defined(_WIN32)
+#define LOAD_FUNC(l, s) GetProcAddress(l, s)
+#define DL_CLOSE_FUNC(l) FreeLibrary(l)
+#else
+#define LOAD_FUNC(l, s) dlsym(l, s)
+#define DL_CLOSE_FUNC(l) dlclose(l)
+#endif
+
+typedef enum cudaError_enum {
+ CUDA_SUCCESS = 0
+} CUresult;
+typedef int CUdevice;
+typedef void* CUcontext;
+
+typedef CUresult(CUDAAPI *PCUINIT)(unsigned int Flags);
+typedef CUresult(CUDAAPI *PCUDEVICEGETCOUNT)(int *count);
+typedef CUresult(CUDAAPI *PCUDEVICEGET)(CUdevice *device, int ordinal);
+typedef CUresult(CUDAAPI *PCUDEVICEGETNAME)(char *name, int len, CUdevice dev);
+typedef CUresult(CUDAAPI *PCUDEVICECOMPUTECAPABILITY)(int *major, int *minor, CUdevice dev);
+typedef CUresult(CUDAAPI *PCUCTXCREATE)(CUcontext *pctx, unsigned int flags, CUdevice dev);
+typedef CUresult(CUDAAPI *PCUCTXPOPCURRENT)(CUcontext *pctx);
+typedef CUresult(CUDAAPI *PCUCTXDESTROY)(CUcontext ctx);
+
+typedef NVENCSTATUS (NVENCAPI* PNVENCODEAPICREATEINSTANCE)(NV_ENCODE_API_FUNCTION_LIST *functionList);
+
+typedef struct NvencInputSurface
+{
+ NV_ENC_INPUT_PTR input_surface;
+ int width;
+ int height;
+
+ int lockCount;
+
+ NV_ENC_BUFFER_FORMAT format;
+} NvencInputSurface;
+
+typedef struct NvencOutputSurface
+{
+ NV_ENC_OUTPUT_PTR output_surface;
+ int size;
+
+ NvencInputSurface* input_surface;
+
+ int busy;
+} NvencOutputSurface;
+
+typedef struct NvencData
+{
+ union {
+ int64_t timestamp;
+ NvencOutputSurface *surface;
+ };
+} NvencData;
+
+typedef struct NvencDataList
+{
+ NvencData* data;
+
+ uint32_t pos;
+ uint32_t count;
+ uint32_t size;
+} NvencDataList;
+
+typedef struct NvencDynLoadFunctions
+{
+ PCUINIT cu_init;
+ PCUDEVICEGETCOUNT cu_device_get_count;
+ PCUDEVICEGET cu_device_get;
+ PCUDEVICEGETNAME cu_device_get_name;
+ PCUDEVICECOMPUTECAPABILITY cu_device_compute_capability;
+ PCUCTXCREATE cu_ctx_create;
+ PCUCTXPOPCURRENT cu_ctx_pop_current;
+ PCUCTXDESTROY cu_ctx_destroy;
+
+ NV_ENCODE_API_FUNCTION_LIST nvenc_funcs;
+ int nvenc_device_count;
+ CUdevice nvenc_devices[16];
+
+#if defined(_WIN32)
+ HMODULE cuda_lib;
+ HMODULE nvenc_lib;
+#else
+ void* cuda_lib;
+ void* nvenc_lib;
+#endif
+} NvencDynLoadFunctions;
+
+typedef struct NvencValuePair
+{
+ const char *str;
+ uint32_t num;
+} NvencValuePair;
+
+typedef struct NvencContext
+{
+ AVClass *avclass;
+
+ NvencDynLoadFunctions nvenc_dload_funcs;
+
+ NV_ENC_INITIALIZE_PARAMS init_encode_params;
+ NV_ENC_CONFIG encode_config;
+ CUcontext cu_context;
+
+ int max_surface_count;
+ NvencInputSurface *input_surfaces;
+ NvencOutputSurface *output_surfaces;
+
+ NvencDataList output_surface_queue;
+ NvencDataList output_surface_ready_queue;
+ NvencDataList timestamp_list;
+ int64_t last_dts;
+
+ void *nvencoder;
+
+ char *preset;
+ char *profile;
+ char *level;
+ char *tier;
+ int cbr;
+ int twopass;
+ int gpu;
+} NvencContext;
+
+static const NvencValuePair nvenc_h264_level_pairs[] = {
+ { "auto", NV_ENC_LEVEL_AUTOSELECT },
+ { "1" , NV_ENC_LEVEL_H264_1 },
+ { "1.0" , NV_ENC_LEVEL_H264_1 },
+ { "1b" , NV_ENC_LEVEL_H264_1b },
+ { "1.0b", NV_ENC_LEVEL_H264_1b },
+ { "1.1" , NV_ENC_LEVEL_H264_11 },
+ { "1.2" , NV_ENC_LEVEL_H264_12 },
+ { "1.3" , NV_ENC_LEVEL_H264_13 },
+ { "2" , NV_ENC_LEVEL_H264_2 },
+ { "2.0" , NV_ENC_LEVEL_H264_2 },
+ { "2.1" , NV_ENC_LEVEL_H264_21 },
+ { "2.2" , NV_ENC_LEVEL_H264_22 },
+ { "3" , NV_ENC_LEVEL_H264_3 },
+ { "3.0" , NV_ENC_LEVEL_H264_3 },
+ { "3.1" , NV_ENC_LEVEL_H264_31 },
+ { "3.2" , NV_ENC_LEVEL_H264_32 },
+ { "4" , NV_ENC_LEVEL_H264_4 },
+ { "4.0" , NV_ENC_LEVEL_H264_4 },
+ { "4.1" , NV_ENC_LEVEL_H264_41 },
+ { "4.2" , NV_ENC_LEVEL_H264_42 },
+ { "5" , NV_ENC_LEVEL_H264_5 },
+ { "5.0" , NV_ENC_LEVEL_H264_5 },
+ { "5.1" , NV_ENC_LEVEL_H264_51 },
+ { NULL }
+};
+
+static const NvencValuePair nvenc_hevc_level_pairs[] = {
+ { "auto", NV_ENC_LEVEL_AUTOSELECT },
+ { "1" , NV_ENC_LEVEL_HEVC_1 },
+ { "1.0" , NV_ENC_LEVEL_HEVC_1 },
+ { "2" , NV_ENC_LEVEL_HEVC_2 },
+ { "2.0" , NV_ENC_LEVEL_HEVC_2 },
+ { "2.1" , NV_ENC_LEVEL_HEVC_21 },
+ { "3" , NV_ENC_LEVEL_HEVC_3 },
+ { "3.0" , NV_ENC_LEVEL_HEVC_3 },
+ { "3.1" , NV_ENC_LEVEL_HEVC_31 },
+ { "4" , NV_ENC_LEVEL_HEVC_4 },
+ { "4.0" , NV_ENC_LEVEL_HEVC_4 },
+ { "4.1" , NV_ENC_LEVEL_HEVC_41 },
+ { "5" , NV_ENC_LEVEL_HEVC_5 },
+ { "5.0" , NV_ENC_LEVEL_HEVC_5 },
+ { "5.1" , NV_ENC_LEVEL_HEVC_51 },
+ { "5.2" , NV_ENC_LEVEL_HEVC_52 },
+ { "6" , NV_ENC_LEVEL_HEVC_6 },
+ { "6.0" , NV_ENC_LEVEL_HEVC_6 },
+ { "6.1" , NV_ENC_LEVEL_HEVC_61 },
+ { "6.2" , NV_ENC_LEVEL_HEVC_62 },
+ { NULL }
+};
+
+static int input_string_to_uint32(AVCodecContext *avctx, const NvencValuePair *pair, const char *input, uint32_t *output)
+{
+ for (; pair->str; ++pair) {
+ if (!strcmp(input, pair->str)) {
+ *output = pair->num;
+ return 0;
+ }
+ }
+
+ return AVERROR(EINVAL);
+}
+
+static NvencData* data_queue_dequeue(NvencDataList* queue)
+{
+ uint32_t mask;
+ uint32_t read_pos;
+
+ av_assert0(queue);
+ av_assert0(queue->size);
+ av_assert0(queue->data);
+
+ if (!queue->count)
+ return NULL;
+
+ /* Size always is a multiple of two */
+ mask = queue->size - 1;
+ read_pos = (queue->pos - queue->count) & mask;
+ queue->count--;
+
+ return &queue->data[read_pos];
+}
+
+static int data_queue_enqueue(NvencDataList* queue, NvencData *data)
+{
+ NvencDataList new_queue;
+ NvencData* tmp_data;
+ uint32_t mask;
+
+ if (!queue->size) {
+ /* size always has to be a multiple of two */
+ queue->size = 4;
+ queue->pos = 0;
+ queue->count = 0;
+
+ queue->data = av_malloc(queue->size * sizeof(*(queue->data)));
+
+ if (!queue->data) {
+ queue->size = 0;
+ return AVERROR(ENOMEM);
+ }
+ }
+
+ if (queue->count == queue->size) {
+ new_queue.size = queue->size << 1;
+ new_queue.pos = 0;
+ new_queue.count = 0;
+ new_queue.data = av_malloc(new_queue.size * sizeof(*(queue->data)));
+
+ if (!new_queue.data)
+ return AVERROR(ENOMEM);
+
+ while (tmp_data = data_queue_dequeue(queue))
+ data_queue_enqueue(&new_queue, tmp_data);
+
+ av_free(queue->data);
+ *queue = new_queue;
+ }
+
+ mask = queue->size - 1;
+
+ queue->data[queue->pos] = *data;
+ queue->pos = (queue->pos + 1) & mask;
+ queue->count++;
+
+ return 0;
+}
+
+static int out_surf_queue_enqueue(NvencDataList* queue, NvencOutputSurface* surface)
+{
+ NvencData data;
+ data.surface = surface;
+
+ return data_queue_enqueue(queue, &data);
+}
+
+static NvencOutputSurface* out_surf_queue_dequeue(NvencDataList* queue)
+{
+ NvencData* res = data_queue_dequeue(queue);
+
+ if (!res)
+ return NULL;
+
+ return res->surface;
+}
+
+static int timestamp_queue_enqueue(NvencDataList* queue, int64_t timestamp)
+{
+ NvencData data;
+ data.timestamp = timestamp;
+
+ return data_queue_enqueue(queue, &data);
+}
+
+static int64_t timestamp_queue_dequeue(NvencDataList* queue)
+{
+ NvencData* res = data_queue_dequeue(queue);
+
+ if (!res)
+ return AV_NOPTS_VALUE;
+
+ return res->timestamp;
+}
+
+#define CHECK_LOAD_FUNC(t, f, s) \
+do { \
+ (f) = (t)LOAD_FUNC(dl_fn->cuda_lib, s); \
+ if (!(f)) { \
+ av_log(avctx, AV_LOG_FATAL, "Failed loading %s from CUDA library\n", s); \
+ goto error; \
+ } \
+} while (0)
+
+static av_cold int nvenc_dyload_cuda(AVCodecContext *avctx)
+{
+ NvencContext *ctx = avctx->priv_data;
+ NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
+
+ if (dl_fn->cuda_lib)
+ return 1;
+
+#if defined(_WIN32)
+ dl_fn->cuda_lib = LoadLibrary(TEXT("nvcuda.dll"));
+#else
+ dl_fn->cuda_lib = dlopen("libcuda.so", RTLD_LAZY);
+#endif
+
+ if (!dl_fn->cuda_lib) {
+ av_log(avctx, AV_LOG_FATAL, "Failed loading CUDA library\n");
+ goto error;
+ }
+
+ CHECK_LOAD_FUNC(PCUINIT, dl_fn->cu_init, "cuInit");
+ CHECK_LOAD_FUNC(PCUDEVICEGETCOUNT, dl_fn->cu_device_get_count, "cuDeviceGetCount");
+ CHECK_LOAD_FUNC(PCUDEVICEGET, dl_fn->cu_device_get, "cuDeviceGet");
+ CHECK_LOAD_FUNC(PCUDEVICEGETNAME, dl_fn->cu_device_get_name, "cuDeviceGetName");
+ CHECK_LOAD_FUNC(PCUDEVICECOMPUTECAPABILITY, dl_fn->cu_device_compute_capability, "cuDeviceComputeCapability");
+ CHECK_LOAD_FUNC(PCUCTXCREATE, dl_fn->cu_ctx_create, "cuCtxCreate_v2");
+ CHECK_LOAD_FUNC(PCUCTXPOPCURRENT, dl_fn->cu_ctx_pop_current, "cuCtxPopCurrent_v2");
+ CHECK_LOAD_FUNC(PCUCTXDESTROY, dl_fn->cu_ctx_destroy, "cuCtxDestroy_v2");
+
+ return 1;
+
+error:
+
+ if (dl_fn->cuda_lib)
+ DL_CLOSE_FUNC(dl_fn->cuda_lib);
+
+ dl_fn->cuda_lib = NULL;
+
+ return 0;
+}
+
+static av_cold int check_cuda_errors(AVCodecContext *avctx, CUresult err, const char *func)
+{
+ if (err != CUDA_SUCCESS) {
+ av_log(avctx, AV_LOG_FATAL, ">> %s - failed with error code 0x%x\n", func, err);
+ return 0;
+ }
+ return 1;
+}
+#define check_cuda_errors(f) if (!check_cuda_errors(avctx, f, #f)) goto error
+
+static av_cold int nvenc_check_cuda(AVCodecContext *avctx)
+{
+ int device_count = 0;
+ CUdevice cu_device = 0;
+ char gpu_name[128];
+ int smminor = 0, smmajor = 0;
+ int i, smver, target_smver;
+
+ NvencContext *ctx = avctx->priv_data;
+ NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
+
+ switch (avctx->codec->id) {
+ case AV_CODEC_ID_H264:
+ target_smver = 0x30;
+ break;
+ case AV_CODEC_ID_H265:
+ target_smver = 0x52;
+ break;
+ default:
+ av_log(avctx, AV_LOG_FATAL, "nvenc: Unknown codec name\n");
+ goto error;
+ }
+
+ if (!nvenc_dyload_cuda(avctx))
+ return 0;
+
+ if (dl_fn->nvenc_device_count > 0)
+ return 1;
+
+ check_cuda_errors(dl_fn->cu_init(0));
+
+ check_cuda_errors(dl_fn->cu_device_get_count(&device_count));
+
+ if (!device_count) {
+ av_log(avctx, AV_LOG_FATAL, "No CUDA capable devices found\n");
+ goto error;
+ }
+
+ av_log(avctx, AV_LOG_VERBOSE, "%d CUDA capable devices found\n", device_count);
+
+ dl_fn->nvenc_device_count = 0;
+
+ for (i = 0; i < device_count; ++i) {
+ check_cuda_errors(dl_fn->cu_device_get(&cu_device, i));
+ check_cuda_errors(dl_fn->cu_device_get_name(gpu_name, sizeof(gpu_name), cu_device));
+ check_cuda_errors(dl_fn->cu_device_compute_capability(&smmajor, &smminor, cu_device));
+
+ smver = (smmajor << 4) | smminor;
+
+ av_log(avctx, AV_LOG_VERBOSE, "[ GPU #%d - < %s > has Compute SM %d.%d, NVENC %s ]\n", i, gpu_name, smmajor, smminor, (smver >= target_smver) ? "Available" : "Not Available");
+
+ if (smver >= target_smver)
+ dl_fn->nvenc_devices[dl_fn->nvenc_device_count++] = cu_device;
+ }
+
+ if (!dl_fn->nvenc_device_count) {
+ av_log(avctx, AV_LOG_FATAL, "No NVENC capable devices found\n");
+ goto error;
+ }
+
+ return 1;
+
+error:
+
+ dl_fn->nvenc_device_count = 0;
+
+ return 0;
+}
+
+static av_cold int nvenc_dyload_nvenc(AVCodecContext *avctx)
+{
+ PNVENCODEAPICREATEINSTANCE nvEncodeAPICreateInstance = 0;
+ NVENCSTATUS nvstatus;
+
+ NvencContext *ctx = avctx->priv_data;
+ NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
+
+ if (!nvenc_check_cuda(avctx))
+ return 0;
+
+ if (dl_fn->nvenc_lib)
+ return 1;
+
+#if defined(_WIN32)
+ if (sizeof(void*) == 8) {
+ dl_fn->nvenc_lib = LoadLibrary(TEXT("nvEncodeAPI64.dll"));
+ } else {
+ dl_fn->nvenc_lib = LoadLibrary(TEXT("nvEncodeAPI.dll"));
+ }
+#else
+ dl_fn->nvenc_lib = dlopen("libnvidia-encode.so.1", RTLD_LAZY);
+#endif
+
+ if (!dl_fn->nvenc_lib) {
+ av_log(avctx, AV_LOG_FATAL, "Failed loading the nvenc library\n");
+ goto error;
+ }
+
+ nvEncodeAPICreateInstance = (PNVENCODEAPICREATEINSTANCE)LOAD_FUNC(dl_fn->nvenc_lib, "NvEncodeAPICreateInstance");
+
+ if (!nvEncodeAPICreateInstance) {
+ av_log(avctx, AV_LOG_FATAL, "Failed to load nvenc entrypoint\n");
+ goto error;
+ }
+
+ dl_fn->nvenc_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
+
+ nvstatus = nvEncodeAPICreateInstance(&dl_fn->nvenc_funcs);
+
+ if (nvstatus != NV_ENC_SUCCESS) {
+ av_log(avctx, AV_LOG_FATAL, "Failed to create nvenc instance\n");
+ goto error;
+ }
+
+ av_log(avctx, AV_LOG_VERBOSE, "Nvenc initialized successfully\n");
+
+ return 1;
+
+error:
+ if (dl_fn->nvenc_lib)
+ DL_CLOSE_FUNC(dl_fn->nvenc_lib);
+
+ dl_fn->nvenc_lib = NULL;
+
+ return 0;
+}
+
+static av_cold void nvenc_unload_nvenc(AVCodecContext *avctx)
+{
+ NvencContext *ctx = avctx->priv_data;
+ NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
+
+ DL_CLOSE_FUNC(dl_fn->nvenc_lib);
+ dl_fn->nvenc_lib = NULL;
+
+ dl_fn->nvenc_device_count = 0;
+
+ DL_CLOSE_FUNC(dl_fn->cuda_lib);
+ dl_fn->cuda_lib = NULL;
+
+ dl_fn->cu_init = NULL;
+ dl_fn->cu_device_get_count = NULL;
+ dl_fn->cu_device_get = NULL;
+ dl_fn->cu_device_get_name = NULL;
+ dl_fn->cu_device_compute_capability = NULL;
+ dl_fn->cu_ctx_create = NULL;
+ dl_fn->cu_ctx_pop_current = NULL;
+ dl_fn->cu_ctx_destroy = NULL;
+
+ av_log(avctx, AV_LOG_VERBOSE, "Nvenc unloaded\n");
+}
+
+static av_cold int nvenc_encode_init(AVCodecContext *avctx)
+{
+ NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS encode_session_params = { 0 };
+ NV_ENC_PRESET_CONFIG preset_config = { 0 };
+ CUcontext cu_context_curr;
+ CUresult cu_res;
+ GUID encoder_preset = NV_ENC_PRESET_HQ_GUID;
+ GUID codec;
+ NVENCSTATUS nv_status = NV_ENC_SUCCESS;
+ int surfaceCount = 0;
+ int i, num_mbs;
+ int isLL = 0;
+ int res = 0;
+ int dw, dh;
+
+ NvencContext *ctx = avctx->priv_data;
+ NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
+ NV_ENCODE_API_FUNCTION_LIST *p_nvenc = &dl_fn->nvenc_funcs;
+
+ if (!nvenc_dyload_nvenc(avctx))
+ return AVERROR_EXTERNAL;
+
+ avctx->coded_frame = av_frame_alloc();
+ if (!avctx->coded_frame) {
+ res = AVERROR(ENOMEM);
+ goto error;
+ }
+
+ ctx->last_dts = AV_NOPTS_VALUE;
+
+ ctx->encode_config.version = NV_ENC_CONFIG_VER;
+ ctx->init_encode_params.version = NV_ENC_INITIALIZE_PARAMS_VER;
+ preset_config.version = NV_ENC_PRESET_CONFIG_VER;
+ preset_config.presetCfg.version = NV_ENC_CONFIG_VER;
+ encode_session_params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
+ encode_session_params.apiVersion = NVENCAPI_VERSION;
+
+ if (ctx->gpu >= dl_fn->nvenc_device_count) {
+ av_log(avctx, AV_LOG_FATAL, "Requested GPU %d, but only %d GPUs are available!\n", ctx->gpu, dl_fn->nvenc_device_count);
+ res = AVERROR(EINVAL);
+ goto error;
+ }
+
+ ctx->cu_context = NULL;
+ cu_res = dl_fn->cu_ctx_create(&ctx->cu_context, 0, dl_fn->nvenc_devices[ctx->gpu]);
+
+ if (cu_res != CUDA_SUCCESS) {
+ av_log(avctx, AV_LOG_FATAL, "Failed creating CUDA context for NVENC: 0x%x\n", (int)cu_res);
+ res = AVERROR_EXTERNAL;
+ goto error;
+ }
+
+ cu_res = dl_fn->cu_ctx_pop_current(&cu_context_curr);
+
+ if (cu_res != CUDA_SUCCESS) {
+ av_log(avctx, AV_LOG_FATAL, "Failed popping CUDA context: 0x%x\n", (int)cu_res);
+ res = AVERROR_EXTERNAL;
+ goto error;
+ }
+
+ encode_session_params.device = ctx->cu_context;
+ encode_session_params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
+
+ nv_status = p_nvenc->nvEncOpenEncodeSessionEx(&encode_session_params, &ctx->nvencoder);
+ if (nv_status != NV_ENC_SUCCESS) {
+ ctx->nvencoder = NULL;
+ av_log(avctx, AV_LOG_FATAL, "OpenEncodeSessionEx failed: 0x%x - invalid license key?\n", (int)nv_status);
+ res = AVERROR_EXTERNAL;
+ goto error;
+ }
+
+ if (ctx->preset) {
+ if (!strcmp(ctx->preset, "hp")) {
+ encoder_preset = NV_ENC_PRESET_HP_GUID;
+ } else if (!strcmp(ctx->preset, "hq")) {
+ encoder_preset = NV_ENC_PRESET_HQ_GUID;
+ } else if (!strcmp(ctx->preset, "bd")) {
+ encoder_preset = NV_ENC_PRESET_BD_GUID;
+ } else if (!strcmp(ctx->preset, "ll")) {
+ encoder_preset = NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID;
+ isLL = 1;
+ } else if (!strcmp(ctx->preset, "llhp")) {
+ encoder_preset = NV_ENC_PRESET_LOW_LATENCY_HP_GUID;
+ isLL = 1;
+ } else if (!strcmp(ctx->preset, "llhq")) {
+ encoder_preset = NV_ENC_PRESET_LOW_LATENCY_HQ_GUID;
+ isLL = 1;
+ } else if (!strcmp(ctx->preset, "default")) {
+ encoder_preset = NV_ENC_PRESET_DEFAULT_GUID;
+ } else {
+ av_log(avctx, AV_LOG_FATAL, "Preset \"%s\" is unknown! Supported presets: hp, hq, bd, ll, llhp, llhq, default\n", ctx->preset);
+ res = AVERROR(EINVAL);
+ goto error;
+ }
+ }
+
+ switch (avctx->codec->id) {
+ case AV_CODEC_ID_H264:
+ codec = NV_ENC_CODEC_H264_GUID;
+ break;
+ case AV_CODEC_ID_H265:
+ codec = NV_ENC_CODEC_HEVC_GUID;
+ break;
+ default:
+ av_log(avctx, AV_LOG_ERROR, "nvenc: Unknown codec name\n");
+ res = AVERROR(EINVAL);
+ goto error;
+ }
+
+ nv_status = p_nvenc->nvEncGetEncodePresetConfig(ctx->nvencoder, codec, encoder_preset, &preset_config);
+ if (nv_status != NV_ENC_SUCCESS) {
+ av_log(avctx, AV_LOG_FATAL, "GetEncodePresetConfig failed: 0x%x\n", (int)nv_status);
+ res = AVERROR_EXTERNAL;
+ goto error;
+ }
+
+ ctx->init_encode_params.encodeGUID = codec;
+ ctx->init_encode_params.encodeHeight = avctx->height;
+ ctx->init_encode_params.encodeWidth = avctx->width;
+
+ if (avctx->sample_aspect_ratio.num && avctx->sample_aspect_ratio.den &&
+ (avctx->sample_aspect_ratio.num != 1 || avctx->sample_aspect_ratio.num != 1)) {
+ av_reduce(&dw, &dh,
+ avctx->width * avctx->sample_aspect_ratio.num,
+ avctx->height * avctx->sample_aspect_ratio.den,
+ 1024 * 1024);
+ ctx->init_encode_params.darHeight = dh;
+ ctx->init_encode_params.darWidth = dw;
+ } else {
+ ctx->init_encode_params.darHeight = avctx->height;
+ ctx->init_encode_params.darWidth = avctx->width;
+ }
+
+ // De-compensate for hardware, dubiously, trying to compensate for
+ // playback at 704 pixel width.
+ if (avctx->width == 720 &&
+ (avctx->height == 480 || avctx->height == 576)) {
+ av_reduce(&dw, &dh,
+ ctx->init_encode_params.darWidth * 44,
+ ctx->init_encode_params.darHeight * 45,
+ 1024 * 1024);
+ ctx->init_encode_params.darHeight = dh;
+ ctx->init_encode_params.darWidth = dw;
+ }
+
+ ctx->init_encode_params.frameRateNum = avctx->time_base.den;
+ ctx->init_encode_params.frameRateDen = avctx->time_base.num * avctx->ticks_per_frame;
+
+ num_mbs = ((avctx->width + 15) >> 4) * ((avctx->height + 15) >> 4);
+ ctx->max_surface_count = (num_mbs >= 8160) ? 32 : 48;
+
+ ctx->init_encode_params.enableEncodeAsync = 0;
+ ctx->init_encode_params.enablePTD = 1;
+
+ ctx->init_encode_params.presetGUID = encoder_preset;
+
+ ctx->init_encode_params.encodeConfig = &ctx->encode_config;
+ memcpy(&ctx->encode_config, &preset_config.presetCfg, sizeof(ctx->encode_config));
+ ctx->encode_config.version = NV_ENC_CONFIG_VER;
+
+ if (avctx->refs >= 0) {
+ /* 0 means "let the hardware decide" */
+ switch (avctx->codec->id) {
+ case AV_CODEC_ID_H264:
+ ctx->encode_config.encodeCodecConfig.h264Config.maxNumRefFrames = avctx->refs;
+ break;
+ case AV_CODEC_ID_H265:
+ ctx->encode_config.encodeCodecConfig.hevcConfig.maxNumRefFramesInDPB = avctx->refs;
+ break;
+ /* Earlier switch/case will return if unknown codec is passed. */
+ }
+ }
+
+ if (avctx->gop_size > 0) {
+ if (avctx->max_b_frames >= 0) {
+ /* 0 is intra-only, 1 is I/P only, 2 is one B Frame, 3 two B frames, and so on. */
+ ctx->encode_config.frameIntervalP = avctx->max_b_frames + 1;
+ }
+
+ ctx->encode_config.gopLength = avctx->gop_size;
+ switch (avctx->codec->id) {
+ case AV_CODEC_ID_H264:
+ ctx->encode_config.encodeCodecConfig.h264Config.idrPeriod = avctx->gop_size;
+ break;
+ case AV_CODEC_ID_H265:
+ ctx->encode_config.encodeCodecConfig.hevcConfig.idrPeriod = avctx->gop_size;
+ break;
+ /* Earlier switch/case will return if unknown codec is passed. */
+ }
+ } else if (avctx->gop_size == 0) {
+ ctx->encode_config.frameIntervalP = 0;
+ ctx->encode_config.gopLength = 1;
+ switch (avctx->codec->id) {
+ case AV_CODEC_ID_H264:
+ ctx->encode_config.encodeCodecConfig.h264Config.idrPeriod = 1;
+ break;
+ case AV_CODEC_ID_H265:
+ ctx->encode_config.encodeCodecConfig.hevcConfig.idrPeriod = 1;
+ break;
+ /* Earlier switch/case will return if unknown codec is passed. */
+ }
+ }
+
+ /* when there're b frames, set dts offset */
+ if (ctx->encode_config.frameIntervalP >= 2)
+ ctx->last_dts = -2;
+
+ if (avctx->bit_rate > 0)
+ ctx->encode_config.rcParams.averageBitRate = avctx->bit_rate;
+
+ if (avctx->rc_max_rate > 0)
+ ctx->encode_config.rcParams.maxBitRate = avctx->rc_max_rate;
+
+ if (ctx->cbr) {
+ if (!ctx->twopass) {
+ ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CBR;
+ } else if (ctx->twopass == 1 || isLL) {
+ ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_2_PASS_QUALITY;
+
+ if (avctx->codec->id == AV_CODEC_ID_H264) {
+ ctx->encode_config.encodeCodecConfig.h264Config.adaptiveTransformMode = NV_ENC_H264_ADAPTIVE_TRANSFORM_ENABLE;
+ ctx->encode_config.encodeCodecConfig.h264Config.fmoMode = NV_ENC_H264_FMO_DISABLE;
+ }
+
+ if (!isLL)
+ av_log(avctx, AV_LOG_WARNING, "Twopass mode is only known to work with low latency (ll, llhq, llhp) presets.\n");
+ } else {
+ ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CBR;
+ }
+ } else if (avctx->global_quality > 0) {
+ ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_CONSTQP;
+ ctx->encode_config.rcParams.constQP.qpInterB = avctx->global_quality;
+ ctx->encode_config.rcParams.constQP.qpInterP = avctx->global_quality;
+ ctx->encode_config.rcParams.constQP.qpIntra = avctx->global_quality;
+
+ avctx->qmin = -1;
+ avctx->qmax = -1;
+ } else if (avctx->qmin >= 0 && avctx->qmax >= 0) {
+ ctx->encode_config.rcParams.rateControlMode = NV_ENC_PARAMS_RC_VBR;
+
+ ctx->encode_config.rcParams.enableMinQP = 1;
+ ctx->encode_config.rcParams.enableMaxQP = 1;
+
+ ctx->encode_config.rcParams.minQP.qpInterB = avctx->qmin;
+ ctx->encode_config.rcParams.minQP.qpInterP = avctx->qmin;
+ ctx->encode_config.rcParams.minQP.qpIntra = avctx->qmin;
+
+ ctx->encode_config.rcParams.maxQP.qpInterB = avctx->qmax;
+ ctx->encode_config.rcParams.maxQP.qpInterP = avctx->qmax;
+ ctx->encode_config.rcParams.maxQP.qpIntra = avctx->qmax;
+ }
+
+ if (avctx->rc_buffer_size > 0)
+ ctx->encode_config.rcParams.vbvBufferSize = avctx->rc_buffer_size;
+
+ if (avctx->flags & CODEC_FLAG_INTERLACED_DCT) {
+ ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FIELD;
+ } else {
+ ctx->encode_config.frameFieldMode = NV_ENC_PARAMS_FRAME_FIELD_MODE_FRAME;
+ }
+
+ switch (avctx->codec->id) {
+ case AV_CODEC_ID_H264:
+ ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourDescriptionPresentFlag = 1;
+ ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.videoSignalTypePresentFlag = 1;
+
+ ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourMatrix = avctx->colorspace;
+ ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.colourPrimaries = avctx->color_primaries;
+ ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.transferCharacteristics = avctx->color_trc;
+
+ ctx->encode_config.encodeCodecConfig.h264Config.h264VUIParameters.videoFullRangeFlag = avctx->color_range == AVCOL_RANGE_JPEG;
+
+ ctx->encode_config.encodeCodecConfig.h264Config.disableSPSPPS = (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
+ ctx->encode_config.encodeCodecConfig.h264Config.repeatSPSPPS = (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
+
+ if (!ctx->profile) {
+ switch (avctx->profile) {
+ case FF_PROFILE_H264_BASELINE:
+ ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
+ break;
+ case FF_PROFILE_H264_MAIN:
+ ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
+ break;
+ case FF_PROFILE_H264_HIGH:
+ case FF_PROFILE_UNKNOWN:
+ ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
+ break;
+ default:
+ av_log(avctx, AV_LOG_WARNING, "Unsupported profile requested, falling back to high\n");
+ ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
+ break;
+ }
+ } else {
+ if (!strcmp(ctx->profile, "high")) {
+ ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_HIGH_GUID;
+ avctx->profile = FF_PROFILE_H264_HIGH;
+ } else if (!strcmp(ctx->profile, "main")) {
+ ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_MAIN_GUID;
+ avctx->profile = FF_PROFILE_H264_MAIN;
+ } else if (!strcmp(ctx->profile, "baseline")) {
+ ctx->encode_config.profileGUID = NV_ENC_H264_PROFILE_BASELINE_GUID;
+ avctx->profile = FF_PROFILE_H264_BASELINE;
+ } else {
+ av_log(avctx, AV_LOG_FATAL, "Profile \"%s\" is unknown! Supported profiles: high, main, baseline\n", ctx->profile);
+ res = AVERROR(EINVAL);
+ goto error;
+ }
+ }
+
+ if (ctx->level) {
+ res = input_string_to_uint32(avctx, nvenc_h264_level_pairs, ctx->level, &ctx->encode_config.encodeCodecConfig.h264Config.level);
+
+ if (res) {
+ av_log(avctx, AV_LOG_FATAL, "Level \"%s\" is unknown! Supported levels: auto, 1, 1b, 1.1, 1.2, 1.3, 2, 2.1, 2.2, 3, 3.1, 3.2, 4, 4.1, 4.2, 5, 5.1\n", ctx->level);
+ goto error;
+ }
+ } else {
+ ctx->encode_config.encodeCodecConfig.h264Config.level = NV_ENC_LEVEL_AUTOSELECT;
+ }
+
+ break;
+ case AV_CODEC_ID_H265:
+ ctx->encode_config.encodeCodecConfig.hevcConfig.disableSPSPPS = (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) ? 1 : 0;
+ ctx->encode_config.encodeCodecConfig.hevcConfig.repeatSPSPPS = (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) ? 0 : 1;
+
+ /* No other profile is supported in the current SDK version 5 */
+ ctx->encode_config.profileGUID = NV_ENC_HEVC_PROFILE_MAIN_GUID;
+ avctx->profile = FF_PROFILE_HEVC_MAIN;
+
+ if (ctx->level) {
+ res = input_string_to_uint32(avctx, nvenc_hevc_level_pairs, ctx->level, &ctx->encode_config.encodeCodecConfig.hevcConfig.level);
+
+ if (res) {
+ av_log(avctx, AV_LOG_FATAL, "Level \"%s\" is unknown! Supported levels: auto, 1, 2, 2.1, 3, 3.1, 4, 4.1, 5, 5.1, 5.2, 6, 6.1, 6.2\n", ctx->level);
+ goto error;
+ }
+ } else {
+ ctx->encode_config.encodeCodecConfig.hevcConfig.level = NV_ENC_LEVEL_AUTOSELECT;
+ }
+
+ if (ctx->tier) {
+ if (!strcmp(ctx->tier, "main")) {
+ ctx->encode_config.encodeCodecConfig.hevcConfig.tier = NV_ENC_TIER_HEVC_MAIN;
+ } else if (!strcmp(ctx->tier, "high")) {
+ ctx->encode_config.encodeCodecConfig.hevcConfig.tier = NV_ENC_TIER_HEVC_HIGH;
+ } else {
+ av_log(avctx, AV_LOG_FATAL, "Tier \"%s\" is unknown! Supported tiers: main, high\n", ctx->tier);
+ res = AVERROR(EINVAL);
+ goto error;
+ }
+ }
+
+ break;
+ /* Earlier switch/case will return if unknown codec is passed. */
+ }
+
+ nv_status = p_nvenc->nvEncInitializeEncoder(ctx->nvencoder, &ctx->init_encode_params);
+ if (nv_status != NV_ENC_SUCCESS) {
+ av_log(avctx, AV_LOG_FATAL, "InitializeEncoder failed: 0x%x\n", (int)nv_status);
+ res = AVERROR_EXTERNAL;
+ goto error;
+ }
+
+ ctx->input_surfaces = av_malloc(ctx->max_surface_count * sizeof(*ctx->input_surfaces));
+
+ if (!ctx->input_surfaces) {
+ res = AVERROR(ENOMEM);
+ goto error;
+ }
+
+ ctx->output_surfaces = av_malloc(ctx->max_surface_count * sizeof(*ctx->output_surfaces));
+
+ if (!ctx->output_surfaces) {
+ res = AVERROR(ENOMEM);
+ goto error;
+ }
+
+ for (surfaceCount = 0; surfaceCount < ctx->max_surface_count; ++surfaceCount) {
+ NV_ENC_CREATE_INPUT_BUFFER allocSurf = { 0 };
+ NV_ENC_CREATE_BITSTREAM_BUFFER allocOut = { 0 };