forked from HandBrake/HandBrake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvenc_common.c
276 lines (235 loc) · 6.25 KB
/
nvenc_common.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
/* nvenc_common.c
*
* Copyright (c) 2003-2024 HandBrake Team
* This file is part of the HandBrake source code.
* Homepage: <http://handbrake.fr/>.
* It may be used under the terms of the GNU General Public License v2.
* For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
*/
#include "handbrake/hbffmpeg.h"
#include "handbrake/nvenc_common.h"
#include "handbrake/handbrake.h"
#if HB_PROJECT_FEATURE_NVENC
#include <ffnvcodec/nvEncodeAPI.h>
#include <ffnvcodec/dynlink_loader.h>
#endif
static int is_nvenc_available = -1;
static int is_nvenc_av1_available = -1;
static int is_nvenc_h264_available = -1;
static int is_nvenc_hevc_available = -1;
static double cuda_version = -1;
int hb_nvenc_get_cuda_version()
{
if (cuda_version != -1)
{
return cuda_version;
}
#if HB_PROJECT_FEATURE_NVENC
CUresult apiErr = 0;
CUcontext cuda_ctx;
CudaFunctions *cu = NULL;
CUdevice dev;
int major, minor, devices;
apiErr = cuda_load_functions(&cu, NULL);
if (apiErr == CUDA_SUCCESS)
{
apiErr = cu->cuInit(0);
if (apiErr == CUDA_SUCCESS)
{
cu->cuDeviceGetCount(&devices);
if (!devices)
{
cuda_version = 0;
free(cu);
return cuda_version;
}
// For now, lets just work off the primary device we find.
for (int i = 0; i < devices; ++i)
{
int result = cu->cuDeviceGet(&dev, i);
if (result == CUDA_SUCCESS)
{
cu->cuCtxCreate(&cuda_ctx, 0, dev);
break;
}
}
apiErr = cu->cuDeviceComputeCapability(&major, &minor, dev);
if (apiErr == CUDA_SUCCESS)
{
cuda_version = major * 1000 + minor;
hb_log("CUDA Version: %i.%i", major, minor);
free(cu);
return cuda_version;
}
}
}
#else
cuda_version = 0;
#endif
return cuda_version;
}
int hb_check_nvenc_available()
{
if (is_hardware_disabled())
{
return 0;
}
if (is_nvenc_available != -1)
{
return is_nvenc_available;
}
#if HB_PROJECT_FEATURE_NVENC
uint32_t nvenc_ver;
void *context = NULL;
NvencFunctions *nvenc_dl = NULL;
int loadErr = nvenc_load_functions(&nvenc_dl, context);
if (loadErr < 0)
{
is_nvenc_available = 0;
return 0;
}
NVENCSTATUS apiErr = nvenc_dl->NvEncodeAPIGetMaxSupportedVersion(&nvenc_ver);
if (apiErr != NV_ENC_SUCCESS)
{
is_nvenc_available = 0;
hb_log("nvenc: not available");
return 0;
}
else
{
hb_log("nvenc: version %d.%d is available", nvenc_ver >> 4, nvenc_ver & 0xf);
is_nvenc_available = 1;
if (hb_check_nvdec_available())
{
hb_log("nvdec: is available");
}
else
{
hb_log("nvdec: is not compiled into this build");
}
return 1;
}
return 1;
#else
is_nvenc_available = 0;
hb_log("nvenc: not available");
return 0;
#endif
}
int hb_nvenc_h264_available()
{
if (is_nvenc_h264_available != -1)
{
return is_nvenc_h264_available;
}
if (!hb_check_nvenc_available())
{
is_nvenc_h264_available = 0;
return is_nvenc_h264_available;
}
if (hb_nvenc_get_cuda_version() > 0)
{
is_nvenc_h264_available = 1;
}
else
{
is_nvenc_h264_available = 0;
}
return is_nvenc_h264_available;
}
int hb_nvenc_h265_available()
{
if (is_nvenc_hevc_available != -1)
{
return is_nvenc_hevc_available;
}
if (!hb_check_nvenc_available())
{
is_nvenc_hevc_available = 0;
return is_nvenc_hevc_available;
}
if (hb_nvenc_get_cuda_version() >= 5002)
{
is_nvenc_hevc_available = 1;
}
else
{
is_nvenc_hevc_available = 0;
}
return is_nvenc_hevc_available;
}
int hb_nvenc_av1_available()
{
if (is_nvenc_av1_available != -1)
{
return is_nvenc_av1_available;
}
if (!hb_check_nvenc_available()){
is_nvenc_av1_available = 0;
return is_nvenc_av1_available;
}
if (hb_nvenc_get_cuda_version() >= 8009)
{
is_nvenc_av1_available = 1;
}
else
{
is_nvenc_av1_available = 0;
}
return is_nvenc_av1_available;
}
int hb_check_nvdec_available()
{
#if HB_PROJECT_FEATURE_NVDEC
return 1;
#else
return 0;
#endif
}
const char * hb_map_nvenc_preset_name (const char * preset)
{
if (preset == NULL)
{
return "p4";
}
if (strcmp(preset, "fastest") == 0) {
return "p1";
} else if (strcmp(preset, "faster") == 0) {
return "p2";
} else if (strcmp(preset, "fast") == 0) {
return "p3";
} else if (strcmp(preset, "medium") == 0) {
return "p4";
} else if (strcmp(preset, "slow") == 0) {
return "p5";
} else if (strcmp(preset, "slower") == 0) {
return "p6";
} else if (strcmp(preset, "slowest") == 0) {
return "p7";
}
return "p4"; // Default to Medium
}
int hb_nvenc_are_filters_supported(hb_list_t *filters)
{
int ret = 1;
for (int i = 0; i < hb_list_count(filters); i++)
{
int supported = 1;
hb_filter_object_t *filter = hb_list_item(filters, i);
switch (filter->id)
{
case HB_FILTER_VFR:
// Mode 0 doesn't require access to the frame data
supported = hb_dict_get_int(filter->settings, "mode") == 0;
break;
default:
supported = 0;
}
if (supported == 0)
{
hb_deep_log(2, "hwaccel: %s isn't yet supported for hw video frames", filter->name);
ret = 0;
}
}
return ret;
}