1 /* GStreamer NVENC plugin
2 * Copyright (C) 2015 Centricular Ltd
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "gstnvenc.h"
25 #include "gstnvh264enc.h"
26 #include "gstnvh265enc.h"
27 #include "gstcudabufferpool.h"
28
29 #include <gmodule.h>
30
31 #if HAVE_NVCODEC_GST_GL
32 #include <gst/gl/gl.h>
33 #endif
34
35 #ifdef _WIN32
36 #ifdef _WIN64
37 #define NVENC_LIBRARY_NAME "nvEncodeAPI64.dll"
38 #else
39 #define NVENC_LIBRARY_NAME "nvEncodeAPI.dll"
40 #endif
41 #else
42 #define NVENC_LIBRARY_NAME "libnvidia-encode.so.1"
43 #endif
44
45 /* For backward compatibility */
46 #define GST_NVENC_MIN_API_MAJOR_VERSION 8
47 #define GST_NVENC_MIN_API_MINOR_VERSION 1
48
49 #define GST_NVENCAPI_VERSION(major,minor) ((major) | ((minor) << 24))
50 #define GST_NVENCAPI_STRUCT_VERSION(ver,api_ver) ((uint32_t)(api_ver) | ((ver)<<16) | (0x7 << 28))
51
52 static guint32 gst_nvenc_api_version = NVENCAPI_VERSION;
53
54 typedef NVENCSTATUS NVENCAPI
55 tNvEncodeAPICreateInstance (NV_ENCODE_API_FUNCTION_LIST * functionList);
56 tNvEncodeAPICreateInstance *nvEncodeAPICreateInstance;
57
58 typedef NVENCSTATUS NVENCAPI
59 tNvEncodeAPIGetMaxSupportedVersion (uint32_t * version);
60 tNvEncodeAPIGetMaxSupportedVersion *nvEncodeAPIGetMaxSupportedVersion;
61
62 GST_DEBUG_CATEGORY_EXTERN (gst_nvenc_debug);
63 #define GST_CAT_DEFAULT gst_nvenc_debug
64
65 static NV_ENCODE_API_FUNCTION_LIST nvenc_api;
66
67 NVENCSTATUS NVENCAPI
NvEncOpenEncodeSessionEx(NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS * params,void ** encoder)68 NvEncOpenEncodeSessionEx (NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS * params,
69 void **encoder)
70 {
71 g_assert (nvenc_api.nvEncOpenEncodeSessionEx != NULL);
72 return nvenc_api.nvEncOpenEncodeSessionEx (params, encoder);
73 }
74
75 NVENCSTATUS NVENCAPI
NvEncDestroyEncoder(void * encoder)76 NvEncDestroyEncoder (void *encoder)
77 {
78 g_assert (nvenc_api.nvEncDestroyEncoder != NULL);
79 return nvenc_api.nvEncDestroyEncoder (encoder);
80 }
81
82 NVENCSTATUS NVENCAPI
NvEncGetEncodeGUIDs(void * encoder,GUID * array,uint32_t array_size,uint32_t * count)83 NvEncGetEncodeGUIDs (void *encoder, GUID * array, uint32_t array_size,
84 uint32_t * count)
85 {
86 g_assert (nvenc_api.nvEncGetEncodeGUIDs != NULL);
87 return nvenc_api.nvEncGetEncodeGUIDs (encoder, array, array_size, count);
88 }
89
90 NVENCSTATUS NVENCAPI
NvEncGetEncodeProfileGUIDCount(void * encoder,GUID encodeGUID,uint32_t * encodeProfileGUIDCount)91 NvEncGetEncodeProfileGUIDCount (void *encoder, GUID encodeGUID,
92 uint32_t * encodeProfileGUIDCount)
93 {
94 g_assert (nvenc_api.nvEncGetEncodeProfileGUIDCount != NULL);
95 return nvenc_api.nvEncGetEncodeProfileGUIDCount (encoder, encodeGUID,
96 encodeProfileGUIDCount);
97 }
98
99 NVENCSTATUS NVENCAPI
NvEncGetEncodeProfileGUIDs(void * encoder,GUID encodeGUID,GUID * profileGUIDs,uint32_t guidArraySize,uint32_t * GUIDCount)100 NvEncGetEncodeProfileGUIDs (void *encoder, GUID encodeGUID,
101 GUID * profileGUIDs, uint32_t guidArraySize, uint32_t * GUIDCount)
102 {
103 g_assert (nvenc_api.nvEncGetEncodeProfileGUIDs != NULL);
104 return nvenc_api.nvEncGetEncodeProfileGUIDs (encoder, encodeGUID,
105 profileGUIDs, guidArraySize, GUIDCount);
106 }
107
108 NVENCSTATUS NVENCAPI
NvEncGetInputFormats(void * encoder,GUID enc_guid,NV_ENC_BUFFER_FORMAT * array,uint32_t size,uint32_t * num)109 NvEncGetInputFormats (void *encoder, GUID enc_guid,
110 NV_ENC_BUFFER_FORMAT * array, uint32_t size, uint32_t * num)
111 {
112 g_assert (nvenc_api.nvEncGetInputFormats != NULL);
113 return nvenc_api.nvEncGetInputFormats (encoder, enc_guid, array, size, num);
114 }
115
116 NVENCSTATUS NVENCAPI
NvEncGetEncodePresetCount(void * encoder,GUID encodeGUID,uint32_t * encodePresetGUIDCount)117 NvEncGetEncodePresetCount (void *encoder, GUID encodeGUID,
118 uint32_t * encodePresetGUIDCount)
119 {
120 g_assert (nvenc_api.nvEncGetEncodeProfileGUIDCount != NULL);
121 return nvenc_api.nvEncGetEncodePresetCount (encoder, encodeGUID,
122 encodePresetGUIDCount);
123 }
124
125 NVENCSTATUS NVENCAPI
NvEncGetEncodePresetGUIDs(void * encoder,GUID encodeGUID,GUID * presetGUIDs,uint32_t guidArraySize,uint32_t * GUIDCount)126 NvEncGetEncodePresetGUIDs (void *encoder, GUID encodeGUID,
127 GUID * presetGUIDs, uint32_t guidArraySize, uint32_t * GUIDCount)
128 {
129 g_assert (nvenc_api.nvEncGetEncodeProfileGUIDs != NULL);
130 return nvenc_api.nvEncGetEncodePresetGUIDs (encoder, encodeGUID,
131 presetGUIDs, guidArraySize, GUIDCount);
132 }
133
134 NVENCSTATUS NVENCAPI
NvEncGetEncodePresetConfig(void * encoder,GUID encodeGUID,GUID presetGUID,NV_ENC_PRESET_CONFIG * presetConfig)135 NvEncGetEncodePresetConfig (void *encoder, GUID encodeGUID,
136 GUID presetGUID, NV_ENC_PRESET_CONFIG * presetConfig)
137 {
138 g_assert (nvenc_api.nvEncGetEncodePresetConfig != NULL);
139 return nvenc_api.nvEncGetEncodePresetConfig (encoder, encodeGUID, presetGUID,
140 presetConfig);
141 }
142
143 NVENCSTATUS NVENCAPI
NvEncGetEncodeCaps(void * encoder,GUID encodeGUID,NV_ENC_CAPS_PARAM * capsParam,int * capsVal)144 NvEncGetEncodeCaps (void *encoder, GUID encodeGUID,
145 NV_ENC_CAPS_PARAM * capsParam, int *capsVal)
146 {
147 g_assert (nvenc_api.nvEncGetEncodeCaps != NULL);
148 return nvenc_api.nvEncGetEncodeCaps (encoder, encodeGUID, capsParam, capsVal);
149 }
150
151 NVENCSTATUS NVENCAPI
NvEncGetSequenceParams(void * encoder,NV_ENC_SEQUENCE_PARAM_PAYLOAD * sequenceParamPayload)152 NvEncGetSequenceParams (void *encoder,
153 NV_ENC_SEQUENCE_PARAM_PAYLOAD * sequenceParamPayload)
154 {
155 g_assert (nvenc_api.nvEncGetSequenceParams != NULL);
156 return nvenc_api.nvEncGetSequenceParams (encoder, sequenceParamPayload);
157 }
158
159 NVENCSTATUS NVENCAPI
NvEncInitializeEncoder(void * encoder,NV_ENC_INITIALIZE_PARAMS * params)160 NvEncInitializeEncoder (void *encoder, NV_ENC_INITIALIZE_PARAMS * params)
161 {
162 g_assert (nvenc_api.nvEncInitializeEncoder != NULL);
163 return nvenc_api.nvEncInitializeEncoder (encoder, params);
164 }
165
166 NVENCSTATUS NVENCAPI
NvEncReconfigureEncoder(void * encoder,NV_ENC_RECONFIGURE_PARAMS * params)167 NvEncReconfigureEncoder (void *encoder, NV_ENC_RECONFIGURE_PARAMS * params)
168 {
169 g_assert (nvenc_api.nvEncReconfigureEncoder != NULL);
170 return nvenc_api.nvEncReconfigureEncoder (encoder, params);
171 }
172
173 NVENCSTATUS NVENCAPI
NvEncRegisterResource(void * encoder,NV_ENC_REGISTER_RESOURCE * params)174 NvEncRegisterResource (void *encoder, NV_ENC_REGISTER_RESOURCE * params)
175 {
176 g_assert (nvenc_api.nvEncRegisterResource != NULL);
177 return nvenc_api.nvEncRegisterResource (encoder, params);
178 }
179
180 NVENCSTATUS NVENCAPI
NvEncUnregisterResource(void * encoder,NV_ENC_REGISTERED_PTR resource)181 NvEncUnregisterResource (void *encoder, NV_ENC_REGISTERED_PTR resource)
182 {
183 g_assert (nvenc_api.nvEncUnregisterResource != NULL);
184 return nvenc_api.nvEncUnregisterResource (encoder, resource);
185 }
186
187 NVENCSTATUS NVENCAPI
NvEncMapInputResource(void * encoder,NV_ENC_MAP_INPUT_RESOURCE * params)188 NvEncMapInputResource (void *encoder, NV_ENC_MAP_INPUT_RESOURCE * params)
189 {
190 g_assert (nvenc_api.nvEncMapInputResource != NULL);
191 return nvenc_api.nvEncMapInputResource (encoder, params);
192 }
193
194 NVENCSTATUS NVENCAPI
NvEncUnmapInputResource(void * encoder,NV_ENC_INPUT_PTR input_buffer)195 NvEncUnmapInputResource (void *encoder, NV_ENC_INPUT_PTR input_buffer)
196 {
197 g_assert (nvenc_api.nvEncUnmapInputResource != NULL);
198 return nvenc_api.nvEncUnmapInputResource (encoder, input_buffer);
199 }
200
201 NVENCSTATUS NVENCAPI
NvEncCreateInputBuffer(void * encoder,NV_ENC_CREATE_INPUT_BUFFER * input_buf)202 NvEncCreateInputBuffer (void *encoder, NV_ENC_CREATE_INPUT_BUFFER * input_buf)
203 {
204 g_assert (nvenc_api.nvEncCreateInputBuffer != NULL);
205 return nvenc_api.nvEncCreateInputBuffer (encoder, input_buf);
206 }
207
208 NVENCSTATUS NVENCAPI
NvEncLockInputBuffer(void * encoder,NV_ENC_LOCK_INPUT_BUFFER * input_buf)209 NvEncLockInputBuffer (void *encoder, NV_ENC_LOCK_INPUT_BUFFER * input_buf)
210 {
211 g_assert (nvenc_api.nvEncLockInputBuffer != NULL);
212 return nvenc_api.nvEncLockInputBuffer (encoder, input_buf);
213 }
214
215 NVENCSTATUS NVENCAPI
NvEncUnlockInputBuffer(void * encoder,NV_ENC_INPUT_PTR input_buf)216 NvEncUnlockInputBuffer (void *encoder, NV_ENC_INPUT_PTR input_buf)
217 {
218 g_assert (nvenc_api.nvEncUnlockInputBuffer != NULL);
219 return nvenc_api.nvEncUnlockInputBuffer (encoder, input_buf);
220 }
221
222 NVENCSTATUS NVENCAPI
NvEncDestroyInputBuffer(void * encoder,NV_ENC_INPUT_PTR input_buf)223 NvEncDestroyInputBuffer (void *encoder, NV_ENC_INPUT_PTR input_buf)
224 {
225 g_assert (nvenc_api.nvEncDestroyInputBuffer != NULL);
226 return nvenc_api.nvEncDestroyInputBuffer (encoder, input_buf);
227 }
228
229 NVENCSTATUS NVENCAPI
NvEncCreateBitstreamBuffer(void * encoder,NV_ENC_CREATE_BITSTREAM_BUFFER * bb)230 NvEncCreateBitstreamBuffer (void *encoder, NV_ENC_CREATE_BITSTREAM_BUFFER * bb)
231 {
232 g_assert (nvenc_api.nvEncCreateBitstreamBuffer != NULL);
233 return nvenc_api.nvEncCreateBitstreamBuffer (encoder, bb);
234 }
235
236 NVENCSTATUS NVENCAPI
NvEncLockBitstream(void * encoder,NV_ENC_LOCK_BITSTREAM * lock_bs)237 NvEncLockBitstream (void *encoder, NV_ENC_LOCK_BITSTREAM * lock_bs)
238 {
239 g_assert (nvenc_api.nvEncLockBitstream != NULL);
240 return nvenc_api.nvEncLockBitstream (encoder, lock_bs);
241 }
242
243 NVENCSTATUS NVENCAPI
NvEncUnlockBitstream(void * encoder,NV_ENC_OUTPUT_PTR bb)244 NvEncUnlockBitstream (void *encoder, NV_ENC_OUTPUT_PTR bb)
245 {
246 g_assert (nvenc_api.nvEncUnlockBitstream != NULL);
247 return nvenc_api.nvEncUnlockBitstream (encoder, bb);
248 }
249
250 NVENCSTATUS NVENCAPI
NvEncDestroyBitstreamBuffer(void * encoder,NV_ENC_OUTPUT_PTR bit_buf)251 NvEncDestroyBitstreamBuffer (void *encoder, NV_ENC_OUTPUT_PTR bit_buf)
252 {
253 g_assert (nvenc_api.nvEncDestroyBitstreamBuffer != NULL);
254 return nvenc_api.nvEncDestroyBitstreamBuffer (encoder, bit_buf);
255 }
256
257 NVENCSTATUS NVENCAPI
NvEncEncodePicture(void * encoder,NV_ENC_PIC_PARAMS * pic_params)258 NvEncEncodePicture (void *encoder, NV_ENC_PIC_PARAMS * pic_params)
259 {
260 g_assert (nvenc_api.nvEncEncodePicture != NULL);
261 return nvenc_api.nvEncEncodePicture (encoder, pic_params);
262 }
263
264 gboolean
gst_nvenc_cmp_guid(GUID g1,GUID g2)265 gst_nvenc_cmp_guid (GUID g1, GUID g2)
266 {
267 return (g1.Data1 == g2.Data1 && g1.Data2 == g2.Data2 && g1.Data3 == g2.Data3
268 && g1.Data4[0] == g2.Data4[0] && g1.Data4[1] == g2.Data4[1]
269 && g1.Data4[2] == g2.Data4[2] && g1.Data4[3] == g2.Data4[3]
270 && g1.Data4[4] == g2.Data4[4] && g1.Data4[5] == g2.Data4[5]
271 && g1.Data4[6] == g2.Data4[6] && g1.Data4[7] == g2.Data4[7]);
272 }
273
274 NV_ENC_BUFFER_FORMAT
gst_nvenc_get_nv_buffer_format(GstVideoFormat fmt)275 gst_nvenc_get_nv_buffer_format (GstVideoFormat fmt)
276 {
277 switch (fmt) {
278 case GST_VIDEO_FORMAT_NV12:
279 return NV_ENC_BUFFER_FORMAT_NV12_PL;
280 case GST_VIDEO_FORMAT_YV12:
281 return NV_ENC_BUFFER_FORMAT_YV12_PL;
282 case GST_VIDEO_FORMAT_I420:
283 return NV_ENC_BUFFER_FORMAT_IYUV_PL;
284 case GST_VIDEO_FORMAT_Y444:
285 return NV_ENC_BUFFER_FORMAT_YUV444_PL;
286 case GST_VIDEO_FORMAT_P010_10LE:
287 case GST_VIDEO_FORMAT_P010_10BE:
288 return NV_ENC_BUFFER_FORMAT_YUV420_10BIT;
289 case GST_VIDEO_FORMAT_BGRA:
290 return NV_ENC_BUFFER_FORMAT_ARGB;
291 case GST_VIDEO_FORMAT_RGBA:
292 return NV_ENC_BUFFER_FORMAT_ABGR;
293 case GST_VIDEO_FORMAT_BGR10A2_LE:
294 return NV_ENC_BUFFER_FORMAT_ARGB10;
295 case GST_VIDEO_FORMAT_RGB10A2_LE:
296 return NV_ENC_BUFFER_FORMAT_ABGR10;
297 case GST_VIDEO_FORMAT_Y444_16LE:
298 case GST_VIDEO_FORMAT_Y444_16BE:
299 return NV_ENC_BUFFER_FORMAT_YUV444_10BIT;
300 case GST_VIDEO_FORMAT_VUYA:
301 return NV_ENC_BUFFER_FORMAT_AYUV;
302 default:
303 break;
304 }
305 return NV_ENC_BUFFER_FORMAT_UNDEFINED;
306 }
307
308 typedef struct
309 {
310 GstVideoFormat gst_format;
311 NV_ENC_BUFFER_FORMAT nv_format;
312 gboolean is_10bit;
313
314 gboolean supported;
315 } GstNvEncFormat;
316
317 gboolean
gst_nvenc_get_supported_input_formats(gpointer encoder,GUID codec_id,GValue ** formats)318 gst_nvenc_get_supported_input_formats (gpointer encoder, GUID codec_id,
319 GValue ** formats)
320 {
321 guint32 i, count = 0;
322 NV_ENC_BUFFER_FORMAT format_list[64];
323 GValue val = G_VALUE_INIT;
324 GValue *ret = NULL;
325 NV_ENC_CAPS_PARAM param = { 0, };
326 gint support_yuv444 = 0;
327 gint support_10bit = 0;
328 guint num_format = 0;
329 GstNvEncFormat format_map[] = {
330 {GST_VIDEO_FORMAT_NV12, NV_ENC_BUFFER_FORMAT_NV12, FALSE, FALSE},
331 {GST_VIDEO_FORMAT_YV12, NV_ENC_BUFFER_FORMAT_YV12, FALSE, FALSE},
332 {GST_VIDEO_FORMAT_I420, NV_ENC_BUFFER_FORMAT_IYUV, FALSE, FALSE},
333 {GST_VIDEO_FORMAT_BGRA, NV_ENC_BUFFER_FORMAT_ARGB, FALSE, FALSE},
334 {GST_VIDEO_FORMAT_RGBA, NV_ENC_BUFFER_FORMAT_ABGR, FALSE, FALSE},
335 {GST_VIDEO_FORMAT_Y444, NV_ENC_BUFFER_FORMAT_YUV444, FALSE, FALSE},
336 {GST_VIDEO_FORMAT_VUYA, NV_ENC_BUFFER_FORMAT_AYUV, FALSE, FALSE},
337 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
338 {GST_VIDEO_FORMAT_P010_10LE, NV_ENC_BUFFER_FORMAT_YUV420_10BIT, TRUE,
339 FALSE},
340 {GST_VIDEO_FORMAT_BGR10A2_LE, NV_ENC_BUFFER_FORMAT_ARGB10, TRUE,
341 FALSE},
342 {GST_VIDEO_FORMAT_RGB10A2_LE, NV_ENC_BUFFER_FORMAT_ABGR10, TRUE,
343 FALSE},
344 {GST_VIDEO_FORMAT_Y444_16LE, NV_ENC_BUFFER_FORMAT_YUV444_10BIT, TRUE,
345 FALSE},
346 #else
347 {GST_VIDEO_FORMAT_P010_10BE, NV_ENC_BUFFER_FORMAT_YUV420_10BIT, TRUE,
348 FALSE},
349 {GST_VIDEO_FORMAT_Y444_16BE, NV_ENC_BUFFER_FORMAT_YUV444_10BIT, TRUE,
350 FALSE},
351 /* FIXME: No 10bits big-endian ARGB10 format is defined */
352 #endif
353 };
354
355 param.version = gst_nvenc_get_caps_param_version ();
356 param.capsToQuery = NV_ENC_CAPS_SUPPORT_YUV444_ENCODE;
357 if (NvEncGetEncodeCaps (encoder,
358 codec_id, ¶m, &support_yuv444) != NV_ENC_SUCCESS) {
359 support_yuv444 = 0;
360 }
361
362 param.capsToQuery = NV_ENC_CAPS_SUPPORT_10BIT_ENCODE;
363 if (NvEncGetEncodeCaps (encoder,
364 codec_id, ¶m, &support_10bit) != NV_ENC_SUCCESS) {
365 support_10bit = 0;
366 }
367
368 if (NvEncGetInputFormats (encoder,
369 codec_id, format_list, G_N_ELEMENTS (format_list),
370 &count) != NV_ENC_SUCCESS || count == 0) {
371 return FALSE;
372 }
373
374 for (i = 0; i < count; i++) {
375 GST_INFO ("input format: 0x%08x", format_list[i]);
376 switch (format_list[i]) {
377 case NV_ENC_BUFFER_FORMAT_NV12:
378 case NV_ENC_BUFFER_FORMAT_YV12:
379 case NV_ENC_BUFFER_FORMAT_IYUV:
380 case NV_ENC_BUFFER_FORMAT_ARGB:
381 case NV_ENC_BUFFER_FORMAT_ABGR:
382 if (!format_map[i].supported) {
383 format_map[i].supported = TRUE;
384 num_format++;
385 }
386 break;
387 case NV_ENC_BUFFER_FORMAT_YUV444:
388 case NV_ENC_BUFFER_FORMAT_AYUV:
389 if (support_yuv444 && !format_map[i].supported) {
390 format_map[i].supported = TRUE;
391 num_format++;
392 }
393 break;
394 case NV_ENC_BUFFER_FORMAT_YUV420_10BIT:
395 if (support_10bit && !format_map[i].supported) {
396 format_map[i].supported = TRUE;
397 num_format++;
398 }
399 break;
400 case NV_ENC_BUFFER_FORMAT_YUV444_10BIT:
401 if (support_yuv444 && support_10bit && !format_map[i].supported) {
402 format_map[i].supported = TRUE;
403 num_format++;
404 }
405 break;
406 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
407 case NV_ENC_BUFFER_FORMAT_ARGB10:
408 case NV_ENC_BUFFER_FORMAT_ABGR10:
409 if (support_10bit && !format_map[i].supported) {
410 format_map[i].supported = TRUE;
411 num_format++;
412 }
413 break;
414 #endif
415 default:
416 GST_FIXME ("unmapped input format: 0x%08x", format_list[i]);
417 break;
418 }
419 }
420
421 if (num_format == 0)
422 return FALSE;
423
424 /* process a second time so we can add formats in the order we want */
425 g_value_init (&val, G_TYPE_STRING);
426 ret = g_new0 (GValue, 1);
427 g_value_init (ret, GST_TYPE_LIST);
428
429 for (i = 0; i < G_N_ELEMENTS (format_map); i++) {
430 if (!format_map[i].supported)
431 continue;
432
433 g_value_set_static_string (&val,
434 gst_video_format_to_string (format_map[i].gst_format));
435
436 gst_value_list_append_value (ret, &val);
437 }
438
439 g_value_unset (&val);
440
441 *formats = ret;
442
443 return TRUE;
444 }
445
446 GValue *
gst_nvenc_get_interlace_modes(gpointer enc,GUID codec_id)447 gst_nvenc_get_interlace_modes (gpointer enc, GUID codec_id)
448 {
449 NV_ENC_CAPS_PARAM caps_param = { 0, };
450 GValue *list;
451 GValue val = G_VALUE_INIT;
452 gint interlace_modes = 0;
453
454 caps_param.version = gst_nvenc_get_caps_param_version ();
455 caps_param.capsToQuery = NV_ENC_CAPS_SUPPORT_FIELD_ENCODING;
456
457 if (NvEncGetEncodeCaps (enc, codec_id, &caps_param,
458 &interlace_modes) != NV_ENC_SUCCESS)
459 interlace_modes = 0;
460
461 list = g_new0 (GValue, 1);
462
463 g_value_init (list, GST_TYPE_LIST);
464 g_value_init (&val, G_TYPE_STRING);
465
466 g_value_set_static_string (&val, "progressive");
467 gst_value_list_append_value (list, &val);
468
469 if (interlace_modes == 0)
470 return list;
471
472 if (interlace_modes >= 1) {
473 g_value_set_static_string (&val, "interleaved");
474 gst_value_list_append_value (list, &val);
475 g_value_set_static_string (&val, "mixed");
476 gst_value_list_append_value (list, &val);
477 g_value_unset (&val);
478 }
479 /* TODO: figure out what nvenc frame based interlacing means in gst terms */
480
481 return list;
482 }
483
484 typedef struct
485 {
486 const gchar *gst_profile;
487 const GUID nv_profile;
488 const GUID codec_id;
489 const gboolean need_yuv444;
490 const gboolean need_10bit;
491
492 gboolean supported;
493 } GstNvEncCodecProfile;
494
495 GValue *
gst_nvenc_get_supported_codec_profiles(gpointer enc,GUID codec_id)496 gst_nvenc_get_supported_codec_profiles (gpointer enc, GUID codec_id)
497 {
498 NVENCSTATUS nv_ret;
499 GUID profile_guids[64];
500 GValue *ret;
501 GValue val = G_VALUE_INIT;
502 guint i, j, n, n_profiles;
503 NV_ENC_CAPS_PARAM param = { 0, };
504 gint support_yuv444 = 0;
505 gint support_10bit = 0;
506 GstNvEncCodecProfile profiles[] = {
507 /* avc profiles */
508 {"main", NV_ENC_H264_PROFILE_MAIN_GUID, NV_ENC_CODEC_H264_GUID, FALSE,
509 FALSE, FALSE},
510 {"high", NV_ENC_H264_PROFILE_HIGH_GUID, NV_ENC_CODEC_H264_GUID, FALSE,
511 FALSE, FALSE},
512 {"high-4:4:4", NV_ENC_H264_PROFILE_HIGH_444_GUID, NV_ENC_CODEC_H264_GUID,
513 TRUE, FALSE, FALSE},
514 /* put baseline to last since it does not support bframe */
515 {"baseline", NV_ENC_H264_PROFILE_BASELINE_GUID, NV_ENC_CODEC_H264_GUID,
516 FALSE, FALSE, FALSE},
517 {"constrained-baseline", NV_ENC_H264_PROFILE_BASELINE_GUID,
518 NV_ENC_CODEC_H264_GUID,
519 FALSE, FALSE, FALSE},
520 /* hevc profiles */
521 {"main", NV_ENC_HEVC_PROFILE_MAIN_GUID, NV_ENC_CODEC_HEVC_GUID, FALSE,
522 FALSE, FALSE},
523 {"main-10", NV_ENC_HEVC_PROFILE_MAIN10_GUID, NV_ENC_CODEC_HEVC_GUID, FALSE,
524 TRUE, FALSE},
525 {"main-444", NV_ENC_HEVC_PROFILE_FREXT_GUID, NV_ENC_CODEC_HEVC_GUID, TRUE,
526 FALSE, FALSE},
527 #if 0
528 /* FIXME: seems to unsupported format */
529 {"main-444-10", NV_ENC_HEVC_PROFILE_FREXT_GUID, FALSE}
530 #endif
531 };
532
533 param.version = gst_nvenc_get_caps_param_version ();
534 param.capsToQuery = NV_ENC_CAPS_SUPPORT_YUV444_ENCODE;
535 if (NvEncGetEncodeCaps (enc,
536 codec_id, ¶m, &support_yuv444) != NV_ENC_SUCCESS) {
537 support_yuv444 = 0;
538 }
539
540 param.capsToQuery = NV_ENC_CAPS_SUPPORT_10BIT_ENCODE;
541 if (NvEncGetEncodeCaps (enc,
542 codec_id, ¶m, &support_10bit) != NV_ENC_SUCCESS) {
543 support_10bit = 0;
544 }
545
546 nv_ret = NvEncGetEncodeProfileGUIDCount (enc, codec_id, &n);
547
548 if (nv_ret != NV_ENC_SUCCESS)
549 return NULL;
550
551 nv_ret = NvEncGetEncodeProfileGUIDs (enc,
552 codec_id, profile_guids, G_N_ELEMENTS (profile_guids), &n);
553
554 if (nv_ret != NV_ENC_SUCCESS)
555 return NULL;
556
557 n_profiles = 0;
558
559 for (i = 0; i < n; i++) {
560 for (j = 0; j < G_N_ELEMENTS (profiles); j++) {
561 if (profiles[j].supported == FALSE &&
562 gst_nvenc_cmp_guid (profile_guids[i], profiles[j].nv_profile) &&
563 gst_nvenc_cmp_guid (codec_id, profiles[j].codec_id)) {
564 if (profiles[j].need_yuv444 && !support_yuv444)
565 continue;
566
567 if (profiles[j].need_10bit && !support_10bit)
568 continue;
569
570 profiles[j].supported = TRUE;
571 n_profiles++;
572 }
573 }
574 }
575
576 if (n_profiles == 0)
577 return NULL;
578
579 ret = g_new0 (GValue, 1);
580
581 g_value_init (ret, GST_TYPE_LIST);
582 g_value_init (&val, G_TYPE_STRING);
583
584 for (i = 0; i < G_N_ELEMENTS (profiles); i++) {
585 if (!profiles[i].supported)
586 continue;
587
588 g_value_set_static_string (&val, profiles[i].gst_profile);
589 gst_value_list_append_value (ret, &val);
590 }
591
592 g_value_unset (&val);
593
594 return ret;
595 }
596
597 #define DEBUG_DEVICE_CAPS(d,c,caps,s) \
598 GST_DEBUG ("[device-%d %s] %s: %s", \
599 d, c, caps, s ? "supported" : "not supported");
600
601 #define ERROR_DETAILS "codec %s, device %i, error code %i"
602
603 static void
gst_nv_enc_register(GstPlugin * plugin,GUID codec_id,const gchar * codec,guint rank,gint device_index,CUcontext cuda_ctx)604 gst_nv_enc_register (GstPlugin * plugin, GUID codec_id, const gchar * codec,
605 guint rank, gint device_index, CUcontext cuda_ctx)
606 {
607 {
608 GValue *formats = NULL;
609 GValue *profiles;
610 GValue *interlace_modes;
611 gpointer enc;
612 NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params = { 0, };
613 NV_ENC_CAPS_PARAM caps_param = { 0, };
614 GUID guids[16];
615 guint32 count;
616 gint max_width = 0;
617 gint max_height = 0;
618 gint min_width = 16;
619 gint min_height = 16;
620 GstCaps *sink_templ = NULL;
621 GstCaps *src_templ = NULL;
622 gchar *name;
623 gint j;
624 GstNvEncDeviceCaps device_caps = { 0, };
625 NVENCSTATUS status;
626 CUresult cu_res;
627
628 params.version = gst_nvenc_get_open_encode_session_ex_params_version ();
629 params.apiVersion = gst_nvenc_get_api_version ();
630 params.device = cuda_ctx;
631 params.deviceType = NV_ENC_DEVICE_TYPE_CUDA;
632
633 if ((cu_res = CuCtxPushCurrent (cuda_ctx)) != CUDA_SUCCESS) {
634 GST_ERROR ("CuCtxPushCurrent failed: " ERROR_DETAILS, codec,
635 device_index, cu_res);
636 goto done;
637 }
638
639 if ((status = NvEncOpenEncodeSessionEx (¶ms, &enc)) != NV_ENC_SUCCESS) {
640 CuCtxPopCurrent (NULL);
641 GST_ERROR ("NvEncOpenEncodeSessionEx failed: " ERROR_DETAILS, codec,
642 device_index, status);
643 goto done;
644 }
645
646 if ((status = NvEncGetEncodeGUIDs (enc, guids, G_N_ELEMENTS (guids),
647 &count)) != NV_ENC_SUCCESS) {
648 GST_ERROR ("NvEncGetEncodeGUIDs failed: " ERROR_DETAILS, codec,
649 device_index, status);
650 goto enc_free;
651 }
652
653 for (j = 0; j < count; j++) {
654 if (gst_nvenc_cmp_guid (guids[j], codec_id))
655 break;
656 }
657
658 if (j == count)
659 goto enc_free;
660
661 if (!gst_nvenc_get_supported_input_formats (enc, codec_id, &formats))
662 goto enc_free;
663
664 profiles = gst_nvenc_get_supported_codec_profiles (enc, codec_id);
665 if (!profiles)
666 goto free_format;
667
668 caps_param.version = gst_nvenc_get_caps_param_version ();
669 caps_param.capsToQuery = NV_ENC_CAPS_WIDTH_MAX;
670 if ((status = NvEncGetEncodeCaps (enc,
671 codec_id, &caps_param, &max_width)) != NV_ENC_SUCCESS) {
672 max_width = 4096;
673 GST_WARNING ("could not query max width, setting as %i: "
674 ERROR_DETAILS, max_width, codec, device_index, status);
675 } else if (max_width < 4096) {
676 GST_WARNING ("max width %d is less than expected value", max_width);
677 max_width = 4096;
678 }
679
680 caps_param.capsToQuery = NV_ENC_CAPS_HEIGHT_MAX;
681 if ((status = NvEncGetEncodeCaps (enc,
682 codec_id, &caps_param, &max_height)) != NV_ENC_SUCCESS) {
683 GST_WARNING ("could not query max height, setting as %i: "
684 ERROR_DETAILS, max_height, codec, device_index, status);
685 max_height = 4096;
686 } else if (max_height < 4096) {
687 GST_WARNING ("max height %d is less than expected value", max_height);
688 max_height = 4096;
689 }
690
691 caps_param.capsToQuery = NV_ENC_CAPS_WIDTH_MIN;
692 if ((status = NvEncGetEncodeCaps (enc,
693 codec_id, &caps_param, &min_width)) != NV_ENC_SUCCESS) {
694 GST_WARNING ("could not query min width, setting as %i: "
695 ERROR_DETAILS, min_width, codec, device_index, status);
696 min_width = 16;
697 }
698
699 caps_param.capsToQuery = NV_ENC_CAPS_HEIGHT_MIN;
700 if ((status = NvEncGetEncodeCaps (enc,
701 codec_id, &caps_param, &min_height)) != NV_ENC_SUCCESS) {
702 GST_WARNING ("could not query min height, setting as %i: "
703 ERROR_DETAILS, min_height, codec, device_index, status);
704 min_height = 16;
705 }
706
707 caps_param.capsToQuery = NV_ENC_CAPS_SUPPORTED_RATECONTROL_MODES;
708 if (NvEncGetEncodeCaps (enc, codec_id, &caps_param,
709 &device_caps.rc_modes) != NV_ENC_SUCCESS) {
710 device_caps.rc_modes = 0;
711 } else {
712 GST_DEBUG ("[device-%d %s] rate control modes: 0x%x",
713 device_index, codec, device_caps.rc_modes);
714 #define IS_SUPPORTED_RC(rc_modes,mode) \
715 ((((rc_modes) & (mode)) == mode) ? "supported" : "not supported")
716
717 GST_DEBUG ("\tconst-qp: %s",
718 IS_SUPPORTED_RC (device_caps.rc_modes, NV_ENC_PARAMS_RC_CONSTQP));
719 GST_DEBUG ("\tvbr: %s",
720 IS_SUPPORTED_RC (device_caps.rc_modes, NV_ENC_PARAMS_RC_VBR));
721 GST_DEBUG ("\tcbr: %s",
722 IS_SUPPORTED_RC (device_caps.rc_modes, NV_ENC_PARAMS_RC_CBR));
723 GST_DEBUG ("\tcbr-lowdelay-hq: %s",
724 IS_SUPPORTED_RC (device_caps.rc_modes,
725 NV_ENC_PARAMS_RC_CBR_LOWDELAY_HQ));
726 GST_DEBUG ("\tcbr-hq: %s",
727 IS_SUPPORTED_RC (device_caps.rc_modes, NV_ENC_PARAMS_RC_CBR_HQ));
728 GST_DEBUG ("\tvbr-hq: %s",
729 IS_SUPPORTED_RC (device_caps.rc_modes, NV_ENC_PARAMS_RC_VBR_HQ));
730 GST_DEBUG ("\tvbr-minqp: %s (deprecated)",
731 IS_SUPPORTED_RC (device_caps.rc_modes, NV_ENC_PARAMS_RC_VBR_MINQP));
732 #undef IS_SUPPORTED_RC
733 }
734
735 caps_param.capsToQuery = NV_ENC_CAPS_SUPPORT_WEIGHTED_PREDICTION;
736 if (NvEncGetEncodeCaps (enc, codec_id, &caps_param,
737 &device_caps.weighted_prediction) != NV_ENC_SUCCESS) {
738 device_caps.weighted_prediction = FALSE;
739 }
740
741 caps_param.capsToQuery = NV_ENC_CAPS_SUPPORT_CUSTOM_VBV_BUF_SIZE;
742 if (NvEncGetEncodeCaps (enc, codec_id, &caps_param,
743 &device_caps.custom_vbv_bufsize) != NV_ENC_SUCCESS) {
744 device_caps.custom_vbv_bufsize = FALSE;
745 }
746
747 caps_param.capsToQuery = NV_ENC_CAPS_SUPPORT_LOOKAHEAD;
748 if (NvEncGetEncodeCaps (enc,
749 codec_id, &caps_param, &device_caps.lookahead) != NV_ENC_SUCCESS) {
750 device_caps.lookahead = FALSE;
751 }
752
753 caps_param.capsToQuery = NV_ENC_CAPS_SUPPORT_TEMPORAL_AQ;
754 if (NvEncGetEncodeCaps (enc, codec_id, &caps_param,
755 &device_caps.temporal_aq) != NV_ENC_SUCCESS) {
756 device_caps.temporal_aq = FALSE;
757 }
758
759 caps_param.capsToQuery = NV_ENC_CAPS_NUM_MAX_BFRAMES;
760 if (NvEncGetEncodeCaps (enc, codec_id, &caps_param,
761 &device_caps.bframes) != NV_ENC_SUCCESS) {
762 device_caps.bframes = 0;
763 }
764
765 DEBUG_DEVICE_CAPS (device_index,
766 codec, "weighted prediction", device_caps.weighted_prediction);
767
768 DEBUG_DEVICE_CAPS (device_index, codec, "custom vbv-buffer-size",
769 device_caps.custom_vbv_bufsize);
770
771 DEBUG_DEVICE_CAPS (device_index, codec, "rc-loockahead",
772 device_caps.lookahead);
773
774 DEBUG_DEVICE_CAPS (device_index, codec, "temporal adaptive quantization",
775 device_caps.temporal_aq);
776
777 GST_DEBUG ("[device-%d %s] max bframes: %d", device_index, codec,
778 device_caps.bframes);
779
780 interlace_modes = gst_nvenc_get_interlace_modes (enc, codec_id);
781
782 sink_templ = gst_caps_new_empty_simple ("video/x-raw");
783 gst_caps_set_value (sink_templ, "format", formats);
784
785 gst_caps_set_simple (sink_templ,
786 "width", GST_TYPE_INT_RANGE, min_width, max_width,
787 "height", GST_TYPE_INT_RANGE, min_height, max_height,
788 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
789
790 if (interlace_modes) {
791 gst_caps_set_value (sink_templ, "interlace-mode", interlace_modes);
792 g_value_unset (interlace_modes);
793 g_free (interlace_modes);
794 }
795
796 {
797 GstCaps *cuda_caps = gst_caps_copy (sink_templ);
798 #if HAVE_NVCODEC_GST_GL
799 GstCaps *gl_caps = gst_caps_copy (sink_templ);
800 gst_caps_set_features_simple (gl_caps,
801 gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_GL_MEMORY));
802 gst_caps_append (sink_templ, gl_caps);
803 #endif
804
805 gst_caps_set_features_simple (cuda_caps,
806 gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_CUDA_MEMORY));
807 gst_caps_append (sink_templ, cuda_caps);
808 }
809
810 name = g_strdup_printf ("video/x-%s", codec);
811 src_templ = gst_caps_new_simple (name,
812 "width", GST_TYPE_INT_RANGE, min_width, max_width,
813 "height", GST_TYPE_INT_RANGE, min_height, max_height,
814 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1,
815 "stream-format", G_TYPE_STRING, "byte-stream",
816 "alignment", G_TYPE_STRING, "au", NULL);
817 gst_caps_set_value (src_templ, "profile", profiles);
818 g_free (name);
819
820 GST_DEBUG ("sink template caps %" GST_PTR_FORMAT, sink_templ);
821 GST_DEBUG ("src template caps %" GST_PTR_FORMAT, src_templ);
822
823 g_value_unset (profiles);
824 g_free (profiles);
825
826 free_format:
827 if (formats) {
828 g_value_unset (formats);
829 g_free (formats);
830 }
831 /* fall-through */
832
833 enc_free:
834 NvEncDestroyEncoder (enc);
835 CuCtxPopCurrent (NULL);
836 /* fall-through */
837
838 done:
839 if (sink_templ && src_templ) {
840 if (gst_nvenc_cmp_guid (codec_id, NV_ENC_CODEC_H264_GUID)) {
841 gst_nv_h264_enc_register (plugin, device_index, rank, sink_templ,
842 src_templ, &device_caps);
843 } else if (gst_nvenc_cmp_guid (codec_id, NV_ENC_CODEC_HEVC_GUID)) {
844 gst_nv_h265_enc_register (plugin, device_index, rank, sink_templ,
845 src_templ, &device_caps);
846 } else {
847 g_assert_not_reached ();
848 }
849 }
850
851 gst_clear_caps (&sink_templ);
852 gst_clear_caps (&src_templ);
853 }
854 }
855
856 typedef struct
857 {
858 gint major;
859 gint minor;
860 } GstNvEncVersion;
861
862 gboolean
gst_nvenc_load_library(guint * api_major_ver,guint * api_minor_ver)863 gst_nvenc_load_library (guint * api_major_ver, guint * api_minor_ver)
864 {
865 GModule *module;
866 NVENCSTATUS ret = NV_ENC_SUCCESS;
867 uint32_t max_supported_version;
868 gint major_ver, minor_ver;
869 gint i;
870 static const GstNvEncVersion version_list[] = {
871 {NVENCAPI_MAJOR_VERSION, NVENCAPI_MINOR_VERSION},
872 {9, 0},
873 {GST_NVENC_MIN_API_MAJOR_VERSION, GST_NVENC_MIN_API_MINOR_VERSION}
874 };
875
876 module = g_module_open (NVENC_LIBRARY_NAME, G_MODULE_BIND_LAZY);
877 if (module == NULL) {
878 GST_WARNING ("Could not open library %s, %s",
879 NVENC_LIBRARY_NAME, g_module_error ());
880 return FALSE;
881 }
882
883 if (!g_module_symbol (module, "NvEncodeAPICreateInstance",
884 (gpointer *) & nvEncodeAPICreateInstance)) {
885 GST_ERROR ("%s", g_module_error ());
886 return FALSE;
887 }
888
889 if (!g_module_symbol (module, "NvEncodeAPIGetMaxSupportedVersion",
890 (gpointer *) & nvEncodeAPIGetMaxSupportedVersion)) {
891 GST_ERROR ("NvEncodeAPIGetMaxSupportedVersion unavailable");
892 return FALSE;
893 }
894
895 /* WARNING: Any developers who want to bump SDK version must ensure that
896 * following macro values were not changed and also need to check ABI compatibility.
897 * Otherwise, gst_nvenc_get_ helpers also should be updated.
898 * Currently SDK 8.1 and 9.0 compatible
899 *
900 * NVENCAPI_VERSION (NVENCAPI_MAJOR_VERSION | (NVENCAPI_MINOR_VERSION << 24))
901 *
902 * NVENCAPI_STRUCT_VERSION(ver) ((uint32_t)NVENCAPI_VERSION | ((ver)<<16) | (0x7 << 28))
903 *
904 * NV_ENC_CAPS_PARAM_VER NVENCAPI_STRUCT_VERSION(1)
905 * NV_ENC_ENCODE_OUT_PARAMS_VER NVENCAPI_STRUCT_VERSION(1)
906 * NV_ENC_CREATE_INPUT_BUFFER_VER NVENCAPI_STRUCT_VERSION(1)
907 * NV_ENC_CREATE_BITSTREAM_BUFFER_VER NVENCAPI_STRUCT_VERSION(1)
908 * NV_ENC_CREATE_MV_BUFFER_VER NVENCAPI_STRUCT_VERSION(1)
909 * NV_ENC_RC_PARAMS_VER NVENCAPI_STRUCT_VERSION(1)
910 * NV_ENC_CONFIG_VER (NVENCAPI_STRUCT_VERSION(7) | ( 1<<31 ))
911 * NV_ENC_INITIALIZE_PARAMS_VER (NVENCAPI_STRUCT_VERSION(5) | ( 1<<31 ))
912 * NV_ENC_RECONFIGURE_PARAMS_VER (NVENCAPI_STRUCT_VERSION(1) | ( 1<<31 ))
913 * NV_ENC_PRESET_CONFIG_VER (NVENCAPI_STRUCT_VERSION(4) | ( 1<<31 ))
914 * NV_ENC_PIC_PARAMS_VER (NVENCAPI_STRUCT_VERSION(4) | ( 1<<31 ))
915 * NV_ENC_MEONLY_PARAMS_VER NVENCAPI_STRUCT_VERSION(3)
916 * NV_ENC_LOCK_BITSTREAM_VER NVENCAPI_STRUCT_VERSION(1)
917 * NV_ENC_LOCK_INPUT_BUFFER_VER NVENCAPI_STRUCT_VERSION(1)
918 * NV_ENC_MAP_INPUT_RESOURCE_VER NVENCAPI_STRUCT_VERSION(4)
919 * NV_ENC_REGISTER_RESOURCE_VER NVENCAPI_STRUCT_VERSION(3)
920 * NV_ENC_STAT_VER NVENCAPI_STRUCT_VERSION(1)
921 * NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER NVENCAPI_STRUCT_VERSION(1)
922 * NV_ENC_EVENT_PARAMS_VER NVENCAPI_STRUCT_VERSION(1)
923 * NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER NVENCAPI_STRUCT_VERSION(1)
924 * NV_ENCODE_API_FUNCTION_LIST_VER NVENCAPI_STRUCT_VERSION(2)
925 */
926
927 ret = nvEncodeAPIGetMaxSupportedVersion (&max_supported_version);
928
929 if (ret != NV_ENC_SUCCESS) {
930 GST_ERROR ("Could not query max supported api version, ret %d", ret);
931 return FALSE;
932 }
933
934 /* 4 LSB: minor version
935 * the rest: major version */
936 major_ver = max_supported_version >> 4;
937 minor_ver = max_supported_version & 0xf;
938
939 GST_INFO ("Maximum supported API version by driver: %d.%d",
940 major_ver, minor_ver);
941
942 ret = NV_ENC_ERR_INVALID_VERSION;
943 for (i = 0; i < G_N_ELEMENTS (version_list); i++) {
944 if (version_list[i].major > major_ver ||
945 (version_list[i].major == major_ver
946 && version_list[i].minor > minor_ver)) {
947 continue;
948 }
949
950 gst_nvenc_api_version =
951 GST_NVENCAPI_VERSION (version_list[i].major, version_list[i].minor);
952
953 nvenc_api.version = GST_NVENCAPI_STRUCT_VERSION (2, gst_nvenc_api_version);
954 ret = nvEncodeAPICreateInstance (&nvenc_api);
955
956 if (ret == NV_ENC_SUCCESS) {
957 GST_INFO ("API version %d.%d load done",
958 version_list[i].major, version_list[i].minor);
959
960 *api_major_ver = version_list[i].major;
961 *api_minor_ver = version_list[i].minor;
962 break;
963 }
964 }
965
966 return ret == NV_ENC_SUCCESS;
967 }
968
969 void
gst_nvenc_plugin_init(GstPlugin * plugin,guint device_index,CUcontext cuda_ctx)970 gst_nvenc_plugin_init (GstPlugin * plugin, guint device_index,
971 CUcontext cuda_ctx)
972 {
973 gst_nv_enc_register (plugin, NV_ENC_CODEC_H264_GUID,
974 "h264", GST_RANK_PRIMARY * 2, device_index, cuda_ctx);
975 gst_nv_enc_register (plugin, NV_ENC_CODEC_HEVC_GUID,
976 "h265", GST_RANK_PRIMARY * 2, device_index, cuda_ctx);
977 }
978
979 guint32
gst_nvenc_get_api_version(void)980 gst_nvenc_get_api_version (void)
981 {
982 /* NVENCAPI_VERSION == (NVENCAPI_MAJOR_VERSION | (NVENCAPI_MINOR_VERSION << 24)) */
983 return gst_nvenc_api_version;
984 }
985
986 guint32
gst_nvenc_get_caps_param_version(void)987 gst_nvenc_get_caps_param_version (void)
988 {
989 /* NV_ENC_CAPS_PARAM_VER == NVENCAPI_STRUCT_VERSION(1) */
990 return GST_NVENCAPI_STRUCT_VERSION (1, gst_nvenc_api_version);
991 }
992
993 guint32
gst_nvenc_get_encode_out_params_version(void)994 gst_nvenc_get_encode_out_params_version (void)
995 {
996 /* NV_ENC_ENCODE_OUT_PARAMS_VER == NVENCAPI_STRUCT_VERSION(1) */
997 return GST_NVENCAPI_STRUCT_VERSION (1, gst_nvenc_api_version);
998 }
999
1000 guint32
gst_nvenc_get_create_input_buffer_version(void)1001 gst_nvenc_get_create_input_buffer_version (void)
1002 {
1003 /* NV_ENC_CREATE_INPUT_BUFFER_VER == NVENCAPI_STRUCT_VERSION(1) */
1004 return GST_NVENCAPI_STRUCT_VERSION (1, gst_nvenc_api_version);
1005 }
1006
1007 guint32
gst_nvenc_get_create_bitstream_buffer_version(void)1008 gst_nvenc_get_create_bitstream_buffer_version (void)
1009 {
1010 /* NV_ENC_CREATE_BITSTREAM_BUFFER_VER == NVENCAPI_STRUCT_VERSION(1) */
1011 return GST_NVENCAPI_STRUCT_VERSION (1, gst_nvenc_api_version);
1012 }
1013
1014 guint32
gst_nvenc_get_create_mv_buffer_version(void)1015 gst_nvenc_get_create_mv_buffer_version (void)
1016 {
1017 /* NV_ENC_CREATE_MV_BUFFER_VER == NVENCAPI_STRUCT_VERSION(1) */
1018 return GST_NVENCAPI_STRUCT_VERSION (1, gst_nvenc_api_version);
1019 }
1020
1021 guint32
gst_nvenc_get_rc_params_version(void)1022 gst_nvenc_get_rc_params_version (void)
1023 {
1024 /* NV_ENC_RC_PARAMS_VER == NVENCAPI_STRUCT_VERSION(1) */
1025 return GST_NVENCAPI_STRUCT_VERSION (1, gst_nvenc_api_version);
1026 }
1027
1028 guint32
gst_nvenc_get_config_version(void)1029 gst_nvenc_get_config_version (void)
1030 {
1031 /* NV_ENC_CONFIG_VER ==
1032 * (NVENCAPI_STRUCT_VERSION(7) | ( 1<<31 )) */
1033 return GST_NVENCAPI_STRUCT_VERSION (7, gst_nvenc_api_version) | (1 << 31);
1034 }
1035
1036 guint32
gst_nvenc_get_initialize_params_version(void)1037 gst_nvenc_get_initialize_params_version (void)
1038 {
1039 /* NV_ENC_INITIALIZE_PARAMS_VER ==
1040 * (NVENCAPI_STRUCT_VERSION(5) | ( 1<<31 )) */
1041 return GST_NVENCAPI_STRUCT_VERSION (5, gst_nvenc_api_version) | (1 << 31);
1042 }
1043
1044 guint32
gst_nvenc_get_reconfigure_params_version(void)1045 gst_nvenc_get_reconfigure_params_version (void)
1046 {
1047 /* NV_ENC_RECONFIGURE_PARAMS_VER ==
1048 * (NVENCAPI_STRUCT_VERSION(1) | ( 1<<31 )) */
1049 return GST_NVENCAPI_STRUCT_VERSION (1, gst_nvenc_api_version) | (1 << 31);
1050 }
1051
1052 guint32
gst_nvenc_get_preset_config_version(void)1053 gst_nvenc_get_preset_config_version (void)
1054 {
1055 /* NV_ENC_PRESET_CONFIG_VER ==
1056 * (NVENCAPI_STRUCT_VERSION(4) | ( 1<<31 )) */
1057 return GST_NVENCAPI_STRUCT_VERSION (4, gst_nvenc_api_version) | (1 << 31);
1058 }
1059
1060 guint32
gst_nvenc_get_pic_params_version(void)1061 gst_nvenc_get_pic_params_version (void)
1062 {
1063 /* NV_ENC_PIC_PARAMS_VER ==
1064 * (NVENCAPI_STRUCT_VERSION(4) | ( 1<<31 )) */
1065 return GST_NVENCAPI_STRUCT_VERSION (4, gst_nvenc_api_version) | (1 << 31);
1066 }
1067
1068 guint32
gst_nvenc_get_meonly_params_version(void)1069 gst_nvenc_get_meonly_params_version (void)
1070 {
1071 /* NV_ENC_MEONLY_PARAMS_VER == NVENCAPI_STRUCT_VERSION(3) */
1072 return GST_NVENCAPI_STRUCT_VERSION (3, gst_nvenc_api_version);
1073 }
1074
1075 guint32
gst_nvenc_get_lock_bitstream_version(void)1076 gst_nvenc_get_lock_bitstream_version (void)
1077 {
1078 /* NV_ENC_LOCK_BITSTREAM_VER == NVENCAPI_STRUCT_VERSION(1) */
1079 return GST_NVENCAPI_STRUCT_VERSION (1, gst_nvenc_api_version);
1080 }
1081
1082 guint32
gst_nvenc_get_lock_input_buffer_version(void)1083 gst_nvenc_get_lock_input_buffer_version (void)
1084 {
1085 /* NV_ENC_LOCK_INPUT_BUFFER_VER == NVENCAPI_STRUCT_VERSION(1) */
1086 return GST_NVENCAPI_STRUCT_VERSION (1, gst_nvenc_api_version);
1087 }
1088
1089 guint32
gst_nvenc_get_map_input_resource_version(void)1090 gst_nvenc_get_map_input_resource_version (void)
1091 {
1092 /* NV_ENC_MAP_INPUT_RESOURCE_VER == NVENCAPI_STRUCT_VERSION(4) */
1093 return GST_NVENCAPI_STRUCT_VERSION (4, gst_nvenc_api_version);
1094 }
1095
1096 guint32
gst_nvenc_get_register_resource_version(void)1097 gst_nvenc_get_register_resource_version (void)
1098 {
1099 /* NV_ENC_REGISTER_RESOURCE_VER == NVENCAPI_STRUCT_VERSION(3) */
1100 return GST_NVENCAPI_STRUCT_VERSION (3, gst_nvenc_api_version);
1101 }
1102
1103 guint32
gst_nvenc_get_stat_version(void)1104 gst_nvenc_get_stat_version (void)
1105 {
1106 /* NV_ENC_STAT_VER == NVENCAPI_STRUCT_VERSION(1) */
1107 return GST_NVENCAPI_STRUCT_VERSION (1, gst_nvenc_api_version);
1108 }
1109
1110 guint32
gst_nvenc_get_sequence_param_payload_version(void)1111 gst_nvenc_get_sequence_param_payload_version (void)
1112 {
1113 /* NV_ENC_SEQUENCE_PARAM_PAYLOAD_VER == NVENCAPI_STRUCT_VERSION(1) */
1114 return GST_NVENCAPI_STRUCT_VERSION (1, gst_nvenc_api_version);
1115 }
1116
1117 guint32
gst_nvenc_get_event_params_version(void)1118 gst_nvenc_get_event_params_version (void)
1119 {
1120 /* NV_ENC_EVENT_PARAMS_VER == NVENCAPI_STRUCT_VERSION(1) */
1121 return GST_NVENCAPI_STRUCT_VERSION (1, gst_nvenc_api_version);
1122 }
1123
1124 guint32
gst_nvenc_get_open_encode_session_ex_params_version(void)1125 gst_nvenc_get_open_encode_session_ex_params_version (void)
1126 {
1127 /* NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS_VER == NVENCAPI_STRUCT_VERSION(1) */
1128 return GST_NVENCAPI_STRUCT_VERSION (1, gst_nvenc_api_version);
1129 }
1130