forked from philipl/nv-video-info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnvencinfo.c
665 lines (558 loc) · 22 KB
/
nvencinfo.c
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
/*
* nvencinfo - enumerate nvenc capabilities of nvidia video devices
* Copyright (c) 2018 Philip Langdale <[email protected]>
*
* This library 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.
*
* This library 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 this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <string.h>
#include <sys/param.h>
#include <ffnvcodec/dynlink_loader.h>
static CudaFunctions *cu;
static NvencFunctions *nv;
static NV_ENCODE_API_FUNCTION_LIST nv_funcs;
static int check_cu(CUresult err, const char *func)
{
const char *err_name;
const char *err_string;
if (err == CUDA_SUCCESS) {
return 0;
}
cu->cuGetErrorName(err, &err_name);
cu->cuGetErrorString(err, &err_string);
fprintf(stderr, "%s failed", func);
if (err_name && err_string) {
fprintf(stderr, " -> %s: %s", err_name, err_string);
}
fprintf(stderr, "\n");
return -1;
}
#define CHECK_CU(x) { int ret = check_cu((x), #x); if (ret != 0) { return ret; } }
static const struct {
NVENCSTATUS nverr;
int averr;
const char *desc;
} nvenc_errors[] = {
{ NV_ENC_SUCCESS, 0, "success" },
{ NV_ENC_ERR_NO_ENCODE_DEVICE, -1, "no encode device" },
{ NV_ENC_ERR_UNSUPPORTED_DEVICE, -1, "unsupported device" },
{ NV_ENC_ERR_INVALID_ENCODERDEVICE, -1, "invalid encoder device" },
{ NV_ENC_ERR_INVALID_DEVICE, -1, "invalid device" },
{ NV_ENC_ERR_DEVICE_NOT_EXIST, -1, "device does not exist" },
{ NV_ENC_ERR_INVALID_PTR, -1, "invalid ptr" },
{ NV_ENC_ERR_INVALID_EVENT, -1, "invalid event" },
{ NV_ENC_ERR_INVALID_PARAM, -1, "invalid param" },
{ NV_ENC_ERR_INVALID_CALL, -1, "invalid call" },
{ NV_ENC_ERR_OUT_OF_MEMORY, -1, "out of memory" },
{ NV_ENC_ERR_ENCODER_NOT_INITIALIZED, -1, "encoder not initialized" },
{ NV_ENC_ERR_UNSUPPORTED_PARAM, -1, "unsupported param" },
{ NV_ENC_ERR_LOCK_BUSY, -1, "lock busy" },
{ NV_ENC_ERR_NOT_ENOUGH_BUFFER, -1, "not enough buffer" },
{ NV_ENC_ERR_INVALID_VERSION, -1, "invalid version" },
{ NV_ENC_ERR_MAP_FAILED, -1, "map failed" },
{ NV_ENC_ERR_NEED_MORE_INPUT, -1, "need more input" },
{ NV_ENC_ERR_ENCODER_BUSY, -1, "encoder busy" },
{ NV_ENC_ERR_EVENT_NOT_REGISTERD, -1, "event not registered" },
{ NV_ENC_ERR_GENERIC, -1, "generic error" },
{ NV_ENC_ERR_INCOMPATIBLE_CLIENT_KEY, -1, "incompatible client key" },
{ NV_ENC_ERR_UNIMPLEMENTED, -1, "unimplemented" },
{ NV_ENC_ERR_RESOURCE_REGISTER_FAILED, -1, "resource register failed" },
{ NV_ENC_ERR_RESOURCE_NOT_REGISTERED, -1, "resource not registered" },
{ NV_ENC_ERR_RESOURCE_NOT_MAPPED, -1, "resource not mapped" },
};
#define FF_ARRAY_ELEMS(a) (sizeof(a) / sizeof((a)[0]))
static int nvenc_map_error(NVENCSTATUS err, const char **desc)
{
int i;
for (i = 0; i < FF_ARRAY_ELEMS(nvenc_errors); i++) {
if (nvenc_errors[i].nverr == err) {
if (desc)
*desc = nvenc_errors[i].desc;
return nvenc_errors[i].averr;
}
}
if (desc)
*desc = "unknown error";
return -1;
}
static int check_nv(NVENCSTATUS err, const char *func)
{
const char *err_string;
if (err == NV_ENC_SUCCESS) {
return 0;
}
nvenc_map_error(err, &err_string);
fprintf(stderr, "%s failed", func);
if (err_string) {
fprintf(stderr, " -> %s", err_string);
}
fprintf(stderr, "\n");
return -1;
}
#define CHECK_NV(x) { int ret = check_nv((x), #x); if (ret != 0) { return ret; } }
typedef struct {
NV_ENC_CAPS cap;
const char *desc;
} cap_t;
static const cap_t nvenc_limits[] = {
{ NV_ENC_CAPS_WIDTH_MAX, "Maximum Width" },
{ NV_ENC_CAPS_HEIGHT_MAX, "Maximum Hight" },
{ NV_ENC_CAPS_MB_NUM_MAX, "Maximum Macroblocks/frame" },
{ NV_ENC_CAPS_MB_PER_SEC_MAX, "Maximum Macroblocks/second" },
{ NV_ENC_CAPS_LEVEL_MAX, "Max Encoding Level" },
{ NV_ENC_CAPS_LEVEL_MIN, "Min Encoding Level" },
{ NV_ENC_CAPS_NUM_MAX_BFRAMES, "Max No. of B-Frames" },
{ NV_ENC_CAPS_NUM_MAX_LTR_FRAMES, "Maxmimum LT Reference Frames" },
#if NVENCAPI_MAJOR_VERSION > 10
{ NV_ENC_CAPS_NUM_ENCODER_ENGINES, "Number of Encoder Engines" },
#endif
};
static const cap_t nvenc_caps[] = {
{ NV_ENC_CAPS_SUPPORTED_RATECONTROL_MODES, "Supported Rate-Control Modes" },
{ NV_ENC_CAPS_SUPPORT_FIELD_ENCODING, "Supports Field-Encoding" },
{ NV_ENC_CAPS_SUPPORT_MONOCHROME, "Supports Monochrome" },
{ NV_ENC_CAPS_SUPPORT_FMO, "Supports FMO" },
{ NV_ENC_CAPS_SUPPORT_QPELMV, "Supports QPEL Motion Estimation" },
{ NV_ENC_CAPS_SUPPORT_BDIRECT_MODE, "Supports BDirect Mode" },
{ NV_ENC_CAPS_SUPPORT_CABAC, "Supports CABAC" },
{ NV_ENC_CAPS_SUPPORT_ADAPTIVE_TRANSFORM, "Supports Adaptive Transform" },
{ NV_ENC_CAPS_NUM_MAX_TEMPORAL_LAYERS, "Supports Temporal Layers" },
{ NV_ENC_CAPS_SUPPORT_HIERARCHICAL_PFRAMES, "Supports Hierarchical P-Frames" },
{ NV_ENC_CAPS_SUPPORT_HIERARCHICAL_BFRAMES, "Supports Hierarchical B-Frames" },
{ NV_ENC_CAPS_SEPARATE_COLOUR_PLANE, "Supports Separate Colour Planes" },
{ NV_ENC_CAPS_SUPPORT_TEMPORAL_SVC, "Supports Temporal SVC" },
{ NV_ENC_CAPS_SUPPORT_DYN_RES_CHANGE, "Supports Dynamic Resolution Change" },
{ NV_ENC_CAPS_SUPPORT_DYN_BITRATE_CHANGE, "Supports Dynamic Bitrate Change" },
{ NV_ENC_CAPS_SUPPORT_DYN_FORCE_CONSTQP, "Supports Dynamic Force Const-QP" },
{ NV_ENC_CAPS_SUPPORT_DYN_RCMODE_CHANGE, "Supports Dynamic RC-Mode Change" },
{ NV_ENC_CAPS_SUPPORT_SUBFRAME_READBACK, "Supports Sub-Frame Read-back" },
{ NV_ENC_CAPS_SUPPORT_CONSTRAINED_ENCODING, "Supports Constrained Encoding" },
{ NV_ENC_CAPS_SUPPORT_INTRA_REFRESH, "Supports Intra Refresh" },
{ NV_ENC_CAPS_SUPPORT_CUSTOM_VBV_BUF_SIZE, "Supports Custom VBV Buffer Size" },
{ NV_ENC_CAPS_SUPPORT_DYNAMIC_SLICE_MODE, "Supports Dynamic Slice Mode" },
{ NV_ENC_CAPS_SUPPORT_REF_PIC_INVALIDATION, "Supports Ref Pic Invalidation" },
{ NV_ENC_CAPS_PREPROC_SUPPORT, "Supports PreProcessing" },
{ NV_ENC_CAPS_ASYNC_ENCODE_SUPPORT, "Supports Async Encoding" },
{ NV_ENC_CAPS_SUPPORT_YUV444_ENCODE, "Supports YUV444 Encoding" },
{ NV_ENC_CAPS_SUPPORT_LOSSLESS_ENCODE, "Supports Lossless Encoding" },
{ NV_ENC_CAPS_SUPPORT_SAO, "Supports SAO" },
{ NV_ENC_CAPS_SUPPORT_MEONLY_MODE, "Supports ME-Only Mode" },
{ NV_ENC_CAPS_SUPPORT_LOOKAHEAD, "Supports Lookahead Encoding" },
{ NV_ENC_CAPS_SUPPORT_TEMPORAL_AQ, "Supports Temporal AQ" },
{ NV_ENC_CAPS_SUPPORT_10BIT_ENCODE, "Supports 10-bit Encoding" },
{ NV_ENC_CAPS_SUPPORT_WEIGHTED_PREDICTION, "Supports Weighted Prediction" },
#if 0
/* This isn't really a capability. It's a runtime measurement. */
{ NV_ENC_CAPS_DYNAMIC_QUERY_ENCODER_CAPACITY, "Remaining Encoder Capacity" },
#endif
{ NV_ENC_CAPS_SUPPORT_BFRAME_REF_MODE, "Supports B-Frames as References" },
{ NV_ENC_CAPS_SUPPORT_EMPHASIS_LEVEL_MAP, "Supports Emphasis Level Map" },
{ NV_ENC_CAPS_SUPPORT_MULTIPLE_REF_FRAMES, "Supports Multiple Reference Frames" },
#if NVENCAPI_MAJOR_VERSION > 11 || (NVENCAPI_MAJOR_VERSION == 11 && NVENCAPI_MINOR_VERSION > 0)
{ NV_ENC_CAPS_SUPPORT_ALPHA_LAYER_ENCODING, "Supports Alpha Layer Encoding" },
{ NV_ENC_CAPS_SINGLE_SLICE_INTRA_REFRESH, "Supports Single Slice Intra Refresh" },
#endif
};
static const struct {
NV_ENC_BUFFER_FORMAT fmt;
const char *desc;
} nvenc_formats[] = {
{ NV_ENC_BUFFER_FORMAT_NV12, "NV12" },
{ NV_ENC_BUFFER_FORMAT_YV12, "YV12" },
{ NV_ENC_BUFFER_FORMAT_IYUV, "IYUV" },
{ NV_ENC_BUFFER_FORMAT_YUV444, "YUV444" },
{ NV_ENC_BUFFER_FORMAT_YUV420_10BIT, "P010" },
{ NV_ENC_BUFFER_FORMAT_YUV444_10BIT, "YUV444P10" },
{ NV_ENC_BUFFER_FORMAT_ARGB, "ARGB" },
{ NV_ENC_BUFFER_FORMAT_ARGB10, "ARGB10" },
{ NV_ENC_BUFFER_FORMAT_AYUV, "AYUV" },
{ NV_ENC_BUFFER_FORMAT_ABGR, "ABGR" },
{ NV_ENC_BUFFER_FORMAT_ABGR10, "ABGR10" },
};
static const struct {
const GUID *guid;
const char *desc;
} nvenc_profiles[] = {
{ &NV_ENC_CODEC_PROFILE_AUTOSELECT_GUID, "Auto" },
{ &NV_ENC_H264_PROFILE_BASELINE_GUID, "Baseline" },
{ &NV_ENC_H264_PROFILE_MAIN_GUID, "Main" },
{ &NV_ENC_H264_PROFILE_HIGH_GUID, "High" },
{ &NV_ENC_H264_PROFILE_HIGH_444_GUID, "High444" },
{ &NV_ENC_H264_PROFILE_STEREO_GUID, "MVC" },
{ &NV_ENC_H264_PROFILE_PROGRESSIVE_HIGH_GUID, "Progressive High" },
{ &NV_ENC_H264_PROFILE_CONSTRAINED_HIGH_GUID, "Constrained High" },
{ &NV_ENC_HEVC_PROFILE_MAIN_GUID, "Main" },
{ &NV_ENC_HEVC_PROFILE_MAIN10_GUID, "Main10" },
{ &NV_ENC_HEVC_PROFILE_FREXT_GUID, "Main444" },
#if NVENCAPI_MAJOR_VERSION > 11
{ &NV_ENC_AV1_PROFILE_MAIN_GUID, "Main" },
#endif
};
#if NVENCAPI_MAJOR_VERSION >= 12 && NVENCAPI_MINOR_VERSION >= 1
// =========================================================================================
// * Preset GUIDS supported by the NvEncodeAPI interface.
// =========================================================================================
// {B2DFB705-4EBD-4C49-9B5F-24A777D3E587}
static const GUID NV_ENC_PRESET_DEFAULT_GUID =
{ 0xb2dfb705, 0x4ebd, 0x4c49, { 0x9b, 0x5f, 0x24, 0xa7, 0x77, 0xd3, 0xe5, 0x87 } };
// {60E4C59F-E846-4484-A56D-CD45BE9FDDF6}
static const GUID NV_ENC_PRESET_HP_GUID =
{ 0x60e4c59f, 0xe846, 0x4484, { 0xa5, 0x6d, 0xcd, 0x45, 0xbe, 0x9f, 0xdd, 0xf6 } };
// {34DBA71D-A77B-4B8F-9C3E-B6D5DA24C012}
static const GUID NV_ENC_PRESET_HQ_GUID =
{ 0x34dba71d, 0xa77b, 0x4b8f, { 0x9c, 0x3e, 0xb6, 0xd5, 0xda, 0x24, 0xc0, 0x12 } };
// {82E3E450-BDBB-4e40-989C-82A90DF9EF32}
static const GUID NV_ENC_PRESET_BD_GUID =
{ 0x82e3e450, 0xbdbb, 0x4e40, { 0x98, 0x9c, 0x82, 0xa9, 0xd, 0xf9, 0xef, 0x32 } };
// {49DF21C5-6DFA-4feb-9787-6ACC9EFFB726}
static const GUID NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID =
{ 0x49df21c5, 0x6dfa, 0x4feb, { 0x97, 0x87, 0x6a, 0xcc, 0x9e, 0xff, 0xb7, 0x26 } };
// {C5F733B9-EA97-4cf9-BEC2-BF78A74FD105}
static const GUID NV_ENC_PRESET_LOW_LATENCY_HQ_GUID =
{ 0xc5f733b9, 0xea97, 0x4cf9, { 0xbe, 0xc2, 0xbf, 0x78, 0xa7, 0x4f, 0xd1, 0x5 } };
// {67082A44-4BAD-48FA-98EA-93056D150A58}
static const GUID NV_ENC_PRESET_LOW_LATENCY_HP_GUID =
{ 0x67082a44, 0x4bad, 0x48fa, { 0x98, 0xea, 0x93, 0x5, 0x6d, 0x15, 0xa, 0x58 } };
// {D5BFB716-C604-44e7-9BB8-DEA5510FC3AC}
static const GUID NV_ENC_PRESET_LOSSLESS_DEFAULT_GUID =
{ 0xd5bfb716, 0xc604, 0x44e7, { 0x9b, 0xb8, 0xde, 0xa5, 0x51, 0xf, 0xc3, 0xac } };
// {149998E7-2364-411d-82EF-179888093409}
static const GUID NV_ENC_PRESET_LOSSLESS_HP_GUID =
{ 0x149998e7, 0x2364, 0x411d, { 0x82, 0xef, 0x17, 0x98, 0x88, 0x9, 0x34, 0x9 } };
#endif
static const struct {
const GUID *guid;
const char *desc;
} nvenc_presets[] = {
{ &NV_ENC_PRESET_DEFAULT_GUID, "default" },
{ &NV_ENC_PRESET_HP_GUID, "hp"},
{ &NV_ENC_PRESET_HQ_GUID, "hq"},
{ &NV_ENC_PRESET_BD_GUID, "bluray"},
{ &NV_ENC_PRESET_LOW_LATENCY_DEFAULT_GUID, "ll" },
{ &NV_ENC_PRESET_LOW_LATENCY_HQ_GUID, "llhq" },
{ &NV_ENC_PRESET_LOW_LATENCY_HP_GUID, "llhp" },
{ &NV_ENC_PRESET_LOSSLESS_DEFAULT_GUID, "lossless" },
{ &NV_ENC_PRESET_LOSSLESS_HP_GUID, "losslesshp" },
#if NVENCAPI_MAJOR_VERSION > 10
{ &NV_ENC_PRESET_P1_GUID, "p1" },
{ &NV_ENC_PRESET_P2_GUID, "p2" },
{ &NV_ENC_PRESET_P3_GUID, "p3" },
{ &NV_ENC_PRESET_P4_GUID, "p4" },
{ &NV_ENC_PRESET_P5_GUID, "p5" },
{ &NV_ENC_PRESET_P6_GUID, "p6" },
{ &NV_ENC_PRESET_P7_GUID, "p7" },
#endif
};
#define NVENCAPI_CHECK_VERSION(major, minor) \
((major) < NVENCAPI_MAJOR_VERSION || ((major) == NVENCAPI_MAJOR_VERSION && (minor) <= NVENCAPI_MINOR_VERSION))
static void nvenc_print_driver_requirement()
{
#if NVENCAPI_CHECK_VERSION(9, 1)
# if defined(_WIN32) || defined(__CYGWIN__)
const char *minver = "436.15";
# else
const char *minver = "435.21";
# endif
#elif NVENCAPI_CHECK_VERSION(8, 1)
# if defined(_WIN32) || defined(__CYGWIN__)
const char *minver = "390.77";
# else
const char *minver = "390.25";
# endif
#else
# if defined(_WIN32) || defined(__CYGWIN__)
const char *minver = "378.66";
# else
const char *minver = "378.13";
# endif
#endif
printf("The minimum required Nvidia driver for nvenc is %s or newer\n", minver);
}
static int nvenc_load_libraries()
{
uint32_t nvenc_max_ver;
int ret;
ret = cuda_load_functions(&cu, NULL);
if (ret < 0)
return ret;
ret = nvenc_load_functions(&nv, NULL);
if (ret < 0) {
nvenc_print_driver_requirement();
return ret;
}
CHECK_NV(nv->NvEncodeAPIGetMaxSupportedVersion(&nvenc_max_ver));
printf("Loaded Nvenc version %d.%d\n", nvenc_max_ver >> 4, nvenc_max_ver & 0xf);
if ((NVENCAPI_MAJOR_VERSION << 4 | NVENCAPI_MINOR_VERSION) > nvenc_max_ver) {
printf("Driver does not support the required nvenc API version. "
"Required: %d.%d Found: %d.%d\n",
NVENCAPI_MAJOR_VERSION, NVENCAPI_MINOR_VERSION,
nvenc_max_ver >> 4, nvenc_max_ver & 0xf);
nvenc_print_driver_requirement();
return -1;
}
nv_funcs.version = NV_ENCODE_API_FUNCTION_LIST_VER;
CHECK_NV(nv->NvEncodeAPICreateInstance(&nv_funcs));
printf("Nvenc initialized successfully\n");
return 0;
}
static int print_divider(int count) {
printf("-------------------------------------");
for (int i = 0; i < count; i++) {
printf("------------");
}
printf("\n");
}
static int print_thick_divider(int count) {
printf("=====================================");
for (int i = 0; i < count; i++) {
printf("============");
}
printf("\n");
}
static int print_header(const char *text, int count) {
printf("%s", text);
for (int i = 0; i < count; i++) {
printf(" |");
}
printf("\n");
}
static int print_formats(void *encoder, GUID *guids, int guid_count)
{
NV_ENC_BUFFER_FORMAT *formats_for_guid = calloc(guid_count, sizeof(NV_ENC_BUFFER_FORMAT));
if (!formats_for_guid) {
return -1;
}
for (int i = 0; i < guid_count; i++) {
uint32_t count = 0;
CHECK_NV(nv_funcs.nvEncGetInputFormatCount(encoder, guids[i], &count));
NV_ENC_BUFFER_FORMAT *formats = malloc(count * sizeof(NV_ENC_BUFFER_FORMAT));
if (!formats) {
return -1;
}
CHECK_NV(nv_funcs.nvEncGetInputFormats(encoder, guids[i], formats, count, &count));
for (int j = 0; j < count; j++) {
formats_for_guid[i] |= formats[j];
}
free(formats);
}
print_header(" Input Buffer Formats |", guid_count);
print_divider(guid_count);
for (int i = 0; i < FF_ARRAY_ELEMS(nvenc_formats); i++) {
printf("%35s |", nvenc_formats[i].desc);
for (int j = 0; j < guid_count; j++) {
printf("%10s |", (formats_for_guid[j] & nvenc_formats[i].fmt) ? "x" : ".");
}
printf("\n");
}
print_divider(guid_count);
free(formats_for_guid);
return 0;
}
static int get_profiles(void *encoder, GUID encodeGUID, const char **profiles, uint32_t *count)
{
GUID guids[FF_ARRAY_ELEMS(nvenc_profiles)];
CHECK_NV(nv_funcs.nvEncGetEncodeProfileGUIDs(encoder, encodeGUID, guids,
FF_ARRAY_ELEMS(nvenc_profiles), count));
for (int i = 0; i < *count; i++) {
int matched = 0;
for (int j = 0; j < FF_ARRAY_ELEMS(nvenc_profiles); j++) {
if (memcmp(&guids[i], nvenc_profiles[j].guid, sizeof (GUID)) == 0) {
profiles[i] = nvenc_profiles[j].desc;
matched = 1;
}
}
if (!matched) {
profiles[i] = "Unknown";
}
}
return 0;
}
static int print_profiles(void *encoder, GUID *guids, int count)
{
print_divider(count);
print_header(" Profiles |", count);
print_divider(count);
const char *profileGuids[count][FF_ARRAY_ELEMS(nvenc_profiles)];
uint32_t profileCount[count];
uint32_t max = 0;
for (int i = 0; i < count; i++) {
get_profiles(encoder, guids[i], profileGuids[i], &profileCount[i]);
max = MAX(max, profileCount[i]);
}
for (int i = 0; i < max; i++) {
printf("%35s |", "");
for (int j = 0; j < count; j++) {
if (i < profileCount[j]) {
printf("%10s |", profileGuids[j][i]);
} else {
printf("%10s |", "");
}
}
printf("\n");
}
return 0;
}
static int get_presets(void *encoder, GUID encodeGUID, const char **presets, uint32_t *count)
{
GUID guids[FF_ARRAY_ELEMS(nvenc_presets)];
CHECK_NV(nv_funcs.nvEncGetEncodePresetGUIDs(encoder, encodeGUID, guids,
FF_ARRAY_ELEMS(nvenc_presets), count));
for (int i = 0; i < *count; i++) {
int matched = 0;
for (int j = 0; j < FF_ARRAY_ELEMS(nvenc_presets); j++) {
if (memcmp(&guids[i], nvenc_presets[j].guid, sizeof (GUID)) == 0) {
presets[i] = nvenc_presets[j].desc;
matched = 1;
}
}
if (!matched) {
presets[i] = "Unknown";
}
}
return 0;
}
static int print_presets(void *encoder, GUID *guids, int count)
{
print_divider(count);
print_header(" Presets |", count);
print_divider(count);
const char *presetGuids[count][FF_ARRAY_ELEMS(nvenc_presets)];
uint32_t presetCount[count];
uint32_t max = 0;
for (int i = 0; i < count; i++) {
get_presets(encoder, guids[i], presetGuids[i], &presetCount[i]);
max = MAX(max, presetCount[i]);
}
for (int i = 0; i < max; i++) {
printf("%35s |", "");
for (int j = 0; j < count; j++) {
if (i < presetCount[j]) {
printf("%10s |", presetGuids[j][i]);
} else {
printf("%10s |", "");
}
}
printf("\n");
}
return 0;
}
static int get_cap(void *encoder, GUID *guid, NV_ENC_CAPS cap)
{
NV_ENC_CAPS_PARAM params = { 0 };
int val = 0;
params.version = NV_ENC_CAPS_PARAM_VER;
params.capsToQuery = cap;
CHECK_NV(nv_funcs.nvEncGetEncodeCaps(encoder, *guid, ¶ms, &val));
return val;
}
static int print_caps(void *encoder, GUID *guids, int count)
{
print_header(" Limits |", count);
print_divider(count);
for (int i = 0; i < FF_ARRAY_ELEMS(nvenc_limits); i++) {
printf("%35s |", nvenc_limits[i].desc);
for (int j = 0; j < count; j++) {
int ret = get_cap(encoder, &guids[j], nvenc_limits[i].cap);
printf("%10d |", ret);
}
printf("\n");
}
print_divider(count);
print_header(" Capabilities |", count);
print_divider(count);
for (int i = 0; i < FF_ARRAY_ELEMS(nvenc_caps); i++) {
printf("%35s |", nvenc_caps[i].desc);
for (int j = 0; j < count; j++) {
int ret = get_cap(encoder, &guids[j], nvenc_caps[i].cap);
printf("%10d |", ret);
}
printf("\n");
}
return 0;
}
static int print_codecs(void *encoder)
{
uint32_t count = 0;
CHECK_NV(nv_funcs.nvEncGetEncodeGUIDCount(encoder, &count));
GUID *guids = malloc(count * sizeof(GUID));
if (!guids) {
return -1;
}
CHECK_NV(nv_funcs.nvEncGetEncodeGUIDs(encoder, guids, count, &count));
print_thick_divider(count);
printf(" Codec |");
for (int i = 0; i < count; i++) {
if (memcmp(&guids[i], &NV_ENC_CODEC_H264_GUID, 16) == 0) {
printf(" H264 |");
} else if (memcmp(&guids[i], &NV_ENC_CODEC_HEVC_GUID, 16) == 0) {
printf(" HEVC |");
#if NVENCAPI_MAJOR_VERSION > 11
} else if (memcmp(&guids[i], &NV_ENC_CODEC_AV1_GUID, 16) == 0) {
printf(" AV1 |");
#endif
} else {
printf(" Unknown |");
}
}
printf("\n");
print_thick_divider(count);
CHECK_NV(print_formats(encoder, guids, count));
CHECK_NV(print_caps(encoder, guids, count));
CHECK_NV(print_profiles(encoder, guids, count));
CHECK_NV(print_presets(encoder, guids, count));
print_thick_divider(count);
free(guids);
return 0;
}
static int print_nvenc_capabilities(CUcontext cuda_ctx)
{
NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = { 0 };
void *nvencoder;
params.version = NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER;
params.apiVersion = NVENCAPI_VERSION;
params.device = cuda_ctx;
params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
CHECK_NV(nv_funcs.nvEncOpenEncodeSessionEx(¶ms, &nvencoder));
CHECK_NV(print_codecs(nvencoder));
CHECK_NV(nv_funcs.nvEncDestroyEncoder(nvencoder));
return 0;
}
int main(int argc, char *argv[])
{
CUcontext cuda_ctx;
CUcontext dummy;
int ret;
ret = nvenc_load_libraries();
if (ret < 0) {
return ret;
}
CHECK_CU(cu->cuInit(0));
int count;
CHECK_CU(cu->cuDeviceGetCount(&count));
for (int i = 0; i < count; i++) {
CUdevice dev;
CHECK_CU(cu->cuDeviceGet(&dev, i));
char name[255];
CHECK_CU(cu->cuDeviceGetName(name, 255, dev));
printf("Device %d: %s\n", i, name);
CHECK_CU(cu->cuCtxCreate(&cuda_ctx, CU_CTX_SCHED_BLOCKING_SYNC, dev));
print_nvenc_capabilities(cuda_ctx);
printf("\n");
cu->cuCtxPopCurrent(&dummy);
}
nvenc_free_functions(&nv);
cuda_free_functions(&cu);
return 0;
}