From 4f709f0fa62068639102ab98775e06947d05459e Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 1 Oct 2024 18:30:11 +0200 Subject: Rename codec query cuda to codec query nvenc --- include/codec_query/cuda.h | 8 -- include/codec_query/nvenc.h | 8 ++ meson.build | 2 +- src/codec_query/cuda.c | 235 -------------------------------------------- src/codec_query/nvenc.c | 235 ++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 2 +- 6 files changed, 245 insertions(+), 245 deletions(-) delete mode 100644 include/codec_query/cuda.h create mode 100644 include/codec_query/nvenc.h delete mode 100644 src/codec_query/cuda.c create mode 100644 src/codec_query/nvenc.c diff --git a/include/codec_query/cuda.h b/include/codec_query/cuda.h deleted file mode 100644 index b57c4ee..0000000 --- a/include/codec_query/cuda.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef GSR_CODEC_QUERY_CUDA_H -#define GSR_CODEC_QUERY_CUDA_H - -#include "codec_query.h" - -bool gsr_get_supported_video_codecs_nvenc(gsr_supported_video_codecs *video_codecs, bool cleanup); - -#endif /* GSR_CODEC_QUERY_CUDA_H */ diff --git a/include/codec_query/nvenc.h b/include/codec_query/nvenc.h new file mode 100644 index 0000000..c01acf6 --- /dev/null +++ b/include/codec_query/nvenc.h @@ -0,0 +1,8 @@ +#ifndef GSR_CODEC_QUERY_NVENC_H +#define GSR_CODEC_QUERY_NVENC_H + +#include "codec_query.h" + +bool gsr_get_supported_video_codecs_nvenc(gsr_supported_video_codecs *video_codecs, bool cleanup); + +#endif /* GSR_CODEC_QUERY_NVENC_H */ diff --git a/meson.build b/meson.build index 33eaec8..74ac845 100644 --- a/meson.build +++ b/meson.build @@ -18,7 +18,7 @@ src = [ 'src/encoder/video/vaapi.c', 'src/encoder/video/vulkan.c', 'src/encoder/video/software.c', - 'src/codec_query/cuda.c', + 'src/codec_query/nvenc.c', 'src/codec_query/vaapi.c', 'src/codec_query/vulkan.c', 'src/egl.c', diff --git a/src/codec_query/cuda.c b/src/codec_query/cuda.c deleted file mode 100644 index 4692b1d..0000000 --- a/src/codec_query/cuda.c +++ /dev/null @@ -1,235 +0,0 @@ -#include "../../include/codec_query/cuda.h" -#include "../../include/cuda.h" -#include "../../external/nvEncodeAPI.h" - -#include -#include -#include - -static void* open_nvenc_library(void) { - dlerror(); /* clear */ - void *lib = dlopen("libnvidia-encode.so.1", RTLD_LAZY); - if(!lib) { - lib = dlopen("libnvidia-encode.so", RTLD_LAZY); - if(!lib) { - fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc failed: failed to load libnvidia-encode.so/libnvidia-encode.so.1, error: %s\n", dlerror()); - return NULL; - } - } - return lib; -} - -static bool profile_is_h264(const GUID *profile_guid) { - const GUID *h264_guids[] = { - &NV_ENC_H264_PROFILE_BASELINE_GUID, - &NV_ENC_H264_PROFILE_MAIN_GUID, - &NV_ENC_H264_PROFILE_HIGH_GUID, - &NV_ENC_H264_PROFILE_PROGRESSIVE_HIGH_GUID, - &NV_ENC_H264_PROFILE_CONSTRAINED_HIGH_GUID - }; - - for(int i = 0; i < 5; ++i) { - if(memcmp(profile_guid, h264_guids[i], sizeof(GUID)) == 0) - return true; - } - - return false; -} - -static bool profile_is_hevc(const GUID *profile_guid) { - const GUID *h264_guids[] = { - &NV_ENC_HEVC_PROFILE_MAIN_GUID, - }; - - for(int i = 0; i < 1; ++i) { - if(memcmp(profile_guid, h264_guids[i], sizeof(GUID)) == 0) - return true; - } - - return false; -} - -static bool profile_is_hevc_10bit(const GUID *profile_guid) { - const GUID *h264_guids[] = { - &NV_ENC_HEVC_PROFILE_MAIN10_GUID, - }; - - for(int i = 0; i < 1; ++i) { - if(memcmp(profile_guid, h264_guids[i], sizeof(GUID)) == 0) - return true; - } - - return false; -} - -static bool profile_is_av1(const GUID *profile_guid) { - const GUID *h264_guids[] = { - &NV_ENC_AV1_PROFILE_MAIN_GUID, - }; - - for(int i = 0; i < 1; ++i) { - if(memcmp(profile_guid, h264_guids[i], sizeof(GUID)) == 0) - return true; - } - - return false; -} - -static bool encoder_get_supported_profiles(const NV_ENCODE_API_FUNCTION_LIST *function_list, void *nvenc_encoder, const GUID *encoder_guid, gsr_supported_video_codecs *supported_video_codecs) { - bool success = false; - GUID *profile_guids = NULL; - - uint32_t profile_guid_count = 0; - if(function_list->nvEncGetEncodeProfileGUIDCount(nvenc_encoder, *encoder_guid, &profile_guid_count) != NV_ENC_SUCCESS) { - fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncGetEncodeProfileGUIDCount failed, error: %s\n", function_list->nvEncGetLastErrorString(nvenc_encoder)); - goto fail; - } - - if(profile_guid_count == 0) - goto fail; - - profile_guids = calloc(profile_guid_count, sizeof(GUID)); - if(!profile_guids) { - fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: failed to allocate %d guids\n", (int)profile_guid_count); - goto fail; - } - - if(function_list->nvEncGetEncodeProfileGUIDs(nvenc_encoder, *encoder_guid, profile_guids, profile_guid_count, &profile_guid_count) != NV_ENC_SUCCESS) { - fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncGetEncodeProfileGUIDs failed, error: %s\n", function_list->nvEncGetLastErrorString(nvenc_encoder)); - goto fail; - } - - for(uint32_t i = 0; i < profile_guid_count; ++i) { - if(profile_is_h264(&profile_guids[i])) { - supported_video_codecs->h264 = (gsr_supported_video_codec){ true, false }; - } else if(profile_is_hevc(&profile_guids[i])) { - supported_video_codecs->hevc = (gsr_supported_video_codec){ true, false }; - } else if(profile_is_hevc_10bit(&profile_guids[i])) { - supported_video_codecs->hevc_hdr = (gsr_supported_video_codec){ true, false }; - supported_video_codecs->hevc_10bit = (gsr_supported_video_codec){ true, false }; - } else if(profile_is_av1(&profile_guids[i])) { - supported_video_codecs->av1 = (gsr_supported_video_codec){ true, false }; - supported_video_codecs->av1_hdr = (gsr_supported_video_codec){ true, false }; - supported_video_codecs->av1_10bit = (gsr_supported_video_codec){ true, false }; - } - } - - success = true; - fail: - - if(profile_guids) - free(profile_guids); - - return success; -} - -static bool get_supported_video_codecs(const NV_ENCODE_API_FUNCTION_LIST *function_list, void *nvenc_encoder, gsr_supported_video_codecs *supported_video_codecs) { - bool success = false; - GUID *encoder_guids = NULL; - *supported_video_codecs = (gsr_supported_video_codecs){0}; - - uint32_t encode_guid_count = 0; - if(function_list->nvEncGetEncodeGUIDCount(nvenc_encoder, &encode_guid_count) != NV_ENC_SUCCESS) { - fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncGetEncodeGUIDCount failed, error: %s\n", function_list->nvEncGetLastErrorString(nvenc_encoder)); - goto fail; - } - - if(encode_guid_count == 0) - goto fail; - - encoder_guids = calloc(encode_guid_count, sizeof(GUID)); - if(!encoder_guids) { - fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: failed to allocate %d guids\n", (int)encode_guid_count); - goto fail; - } - - if(function_list->nvEncGetEncodeGUIDs(nvenc_encoder, encoder_guids, encode_guid_count, &encode_guid_count) != NV_ENC_SUCCESS) { - fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncGetEncodeGUIDs failed, error: %s\n", function_list->nvEncGetLastErrorString(nvenc_encoder)); - goto fail; - } - - for(uint32_t i = 0; i < encode_guid_count; ++i) { - encoder_get_supported_profiles(function_list, nvenc_encoder, &encoder_guids[i], supported_video_codecs); - } - - success = true; - fail: - - if(encoder_guids) - free(encoder_guids); - - return success; -} - -#define NVENCAPI_VERSION_470 (11 | (1 << 24)) -#define NVENCAPI_STRUCT_VERSION_470(ver) ((uint32_t)NVENCAPI_VERSION_470 | ((ver)<<16) | (0x7 << 28)) - -bool gsr_get_supported_video_codecs_nvenc(gsr_supported_video_codecs *video_codecs, bool cleanup) { - memset(video_codecs, 0, sizeof(*video_codecs)); - - bool success = false; - void *nvenc_lib = NULL; - void *nvenc_encoder = NULL; - gsr_cuda cuda; - memset(&cuda, 0, sizeof(cuda)); - - if(!gsr_cuda_load(&cuda, NULL, false)) { - fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: failed to load cuda\n"); - goto done; - } - - nvenc_lib = open_nvenc_library(); - if(!nvenc_lib) - goto done; - - typedef NVENCSTATUS NVENCAPI (*FUNC_NvEncodeAPICreateInstance)(NV_ENCODE_API_FUNCTION_LIST *functionList); - FUNC_NvEncodeAPICreateInstance nvEncodeAPICreateInstance = (FUNC_NvEncodeAPICreateInstance)dlsym(nvenc_lib, "NvEncodeAPICreateInstance"); - if(!nvEncodeAPICreateInstance) { - fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: failed to find NvEncodeAPICreateInstance in libnvidia-encode.so\n"); - goto done; - } - - NV_ENCODE_API_FUNCTION_LIST function_list; - memset(&function_list, 0, sizeof(function_list)); - function_list.version = NVENCAPI_STRUCT_VERSION(2); - if(nvEncodeAPICreateInstance(&function_list) != NV_ENC_SUCCESS) { - fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncodeAPICreateInstance failed\n"); - goto done; - } - - NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params; - memset(¶ms, 0, sizeof(params)); - params.version = NVENCAPI_STRUCT_VERSION(1); - params.deviceType = NV_ENC_DEVICE_TYPE_CUDA; - params.device = cuda.cu_ctx; - params.apiVersion = NVENCAPI_VERSION; - if(function_list.nvEncOpenEncodeSessionEx(¶ms, &nvenc_encoder) != NV_ENC_SUCCESS) { - // Old nvidia gpus dont support the new nvenc api (which is required for av1). - // In such cases fallback to old api version if possible and try again. - function_list.version = NVENCAPI_STRUCT_VERSION_470(2); - if(nvEncodeAPICreateInstance(&function_list) != NV_ENC_SUCCESS) { - fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncodeAPICreateInstance (retry) failed\n"); - goto done; - } - - params.version = NVENCAPI_STRUCT_VERSION_470(1); - params.apiVersion = NVENCAPI_VERSION_470; - if(function_list.nvEncOpenEncodeSessionEx(¶ms, &nvenc_encoder) != NV_ENC_SUCCESS) { - fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncOpenEncodeSessionEx (retry) failed\n"); - goto done; - } - } - - success = get_supported_video_codecs(&function_list, nvenc_encoder, video_codecs); - - done: - if(cleanup) { - if(nvenc_encoder) - function_list.nvEncDestroyEncoder(nvenc_encoder); - if(nvenc_lib) - dlclose(nvenc_lib); - gsr_cuda_unload(&cuda); - } - - return success; -} diff --git a/src/codec_query/nvenc.c b/src/codec_query/nvenc.c new file mode 100644 index 0000000..0501851 --- /dev/null +++ b/src/codec_query/nvenc.c @@ -0,0 +1,235 @@ +#include "../../include/codec_query/nvenc.h" +#include "../../include/cuda.h" +#include "../../external/nvEncodeAPI.h" + +#include +#include +#include + +static void* open_nvenc_library(void) { + dlerror(); /* clear */ + void *lib = dlopen("libnvidia-encode.so.1", RTLD_LAZY); + if(!lib) { + lib = dlopen("libnvidia-encode.so", RTLD_LAZY); + if(!lib) { + fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc failed: failed to load libnvidia-encode.so/libnvidia-encode.so.1, error: %s\n", dlerror()); + return NULL; + } + } + return lib; +} + +static bool profile_is_h264(const GUID *profile_guid) { + const GUID *h264_guids[] = { + &NV_ENC_H264_PROFILE_BASELINE_GUID, + &NV_ENC_H264_PROFILE_MAIN_GUID, + &NV_ENC_H264_PROFILE_HIGH_GUID, + &NV_ENC_H264_PROFILE_PROGRESSIVE_HIGH_GUID, + &NV_ENC_H264_PROFILE_CONSTRAINED_HIGH_GUID + }; + + for(int i = 0; i < 5; ++i) { + if(memcmp(profile_guid, h264_guids[i], sizeof(GUID)) == 0) + return true; + } + + return false; +} + +static bool profile_is_hevc(const GUID *profile_guid) { + const GUID *h264_guids[] = { + &NV_ENC_HEVC_PROFILE_MAIN_GUID, + }; + + for(int i = 0; i < 1; ++i) { + if(memcmp(profile_guid, h264_guids[i], sizeof(GUID)) == 0) + return true; + } + + return false; +} + +static bool profile_is_hevc_10bit(const GUID *profile_guid) { + const GUID *h264_guids[] = { + &NV_ENC_HEVC_PROFILE_MAIN10_GUID, + }; + + for(int i = 0; i < 1; ++i) { + if(memcmp(profile_guid, h264_guids[i], sizeof(GUID)) == 0) + return true; + } + + return false; +} + +static bool profile_is_av1(const GUID *profile_guid) { + const GUID *h264_guids[] = { + &NV_ENC_AV1_PROFILE_MAIN_GUID, + }; + + for(int i = 0; i < 1; ++i) { + if(memcmp(profile_guid, h264_guids[i], sizeof(GUID)) == 0) + return true; + } + + return false; +} + +static bool encoder_get_supported_profiles(const NV_ENCODE_API_FUNCTION_LIST *function_list, void *nvenc_encoder, const GUID *encoder_guid, gsr_supported_video_codecs *supported_video_codecs) { + bool success = false; + GUID *profile_guids = NULL; + + uint32_t profile_guid_count = 0; + if(function_list->nvEncGetEncodeProfileGUIDCount(nvenc_encoder, *encoder_guid, &profile_guid_count) != NV_ENC_SUCCESS) { + fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncGetEncodeProfileGUIDCount failed, error: %s\n", function_list->nvEncGetLastErrorString(nvenc_encoder)); + goto fail; + } + + if(profile_guid_count == 0) + goto fail; + + profile_guids = calloc(profile_guid_count, sizeof(GUID)); + if(!profile_guids) { + fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: failed to allocate %d guids\n", (int)profile_guid_count); + goto fail; + } + + if(function_list->nvEncGetEncodeProfileGUIDs(nvenc_encoder, *encoder_guid, profile_guids, profile_guid_count, &profile_guid_count) != NV_ENC_SUCCESS) { + fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncGetEncodeProfileGUIDs failed, error: %s\n", function_list->nvEncGetLastErrorString(nvenc_encoder)); + goto fail; + } + + for(uint32_t i = 0; i < profile_guid_count; ++i) { + if(profile_is_h264(&profile_guids[i])) { + supported_video_codecs->h264 = (gsr_supported_video_codec){ true, false }; + } else if(profile_is_hevc(&profile_guids[i])) { + supported_video_codecs->hevc = (gsr_supported_video_codec){ true, false }; + } else if(profile_is_hevc_10bit(&profile_guids[i])) { + supported_video_codecs->hevc_hdr = (gsr_supported_video_codec){ true, false }; + supported_video_codecs->hevc_10bit = (gsr_supported_video_codec){ true, false }; + } else if(profile_is_av1(&profile_guids[i])) { + supported_video_codecs->av1 = (gsr_supported_video_codec){ true, false }; + supported_video_codecs->av1_hdr = (gsr_supported_video_codec){ true, false }; + supported_video_codecs->av1_10bit = (gsr_supported_video_codec){ true, false }; + } + } + + success = true; + fail: + + if(profile_guids) + free(profile_guids); + + return success; +} + +static bool get_supported_video_codecs(const NV_ENCODE_API_FUNCTION_LIST *function_list, void *nvenc_encoder, gsr_supported_video_codecs *supported_video_codecs) { + bool success = false; + GUID *encoder_guids = NULL; + *supported_video_codecs = (gsr_supported_video_codecs){0}; + + uint32_t encode_guid_count = 0; + if(function_list->nvEncGetEncodeGUIDCount(nvenc_encoder, &encode_guid_count) != NV_ENC_SUCCESS) { + fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncGetEncodeGUIDCount failed, error: %s\n", function_list->nvEncGetLastErrorString(nvenc_encoder)); + goto fail; + } + + if(encode_guid_count == 0) + goto fail; + + encoder_guids = calloc(encode_guid_count, sizeof(GUID)); + if(!encoder_guids) { + fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: failed to allocate %d guids\n", (int)encode_guid_count); + goto fail; + } + + if(function_list->nvEncGetEncodeGUIDs(nvenc_encoder, encoder_guids, encode_guid_count, &encode_guid_count) != NV_ENC_SUCCESS) { + fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncGetEncodeGUIDs failed, error: %s\n", function_list->nvEncGetLastErrorString(nvenc_encoder)); + goto fail; + } + + for(uint32_t i = 0; i < encode_guid_count; ++i) { + encoder_get_supported_profiles(function_list, nvenc_encoder, &encoder_guids[i], supported_video_codecs); + } + + success = true; + fail: + + if(encoder_guids) + free(encoder_guids); + + return success; +} + +#define NVENCAPI_VERSION_470 (11 | (1 << 24)) +#define NVENCAPI_STRUCT_VERSION_470(ver) ((uint32_t)NVENCAPI_VERSION_470 | ((ver)<<16) | (0x7 << 28)) + +bool gsr_get_supported_video_codecs_nvenc(gsr_supported_video_codecs *video_codecs, bool cleanup) { + memset(video_codecs, 0, sizeof(*video_codecs)); + + bool success = false; + void *nvenc_lib = NULL; + void *nvenc_encoder = NULL; + gsr_cuda cuda; + memset(&cuda, 0, sizeof(cuda)); + + if(!gsr_cuda_load(&cuda, NULL, false)) { + fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: failed to load cuda\n"); + goto done; + } + + nvenc_lib = open_nvenc_library(); + if(!nvenc_lib) + goto done; + + typedef NVENCSTATUS NVENCAPI (*FUNC_NvEncodeAPICreateInstance)(NV_ENCODE_API_FUNCTION_LIST *functionList); + FUNC_NvEncodeAPICreateInstance nvEncodeAPICreateInstance = (FUNC_NvEncodeAPICreateInstance)dlsym(nvenc_lib, "NvEncodeAPICreateInstance"); + if(!nvEncodeAPICreateInstance) { + fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: failed to find NvEncodeAPICreateInstance in libnvidia-encode.so\n"); + goto done; + } + + NV_ENCODE_API_FUNCTION_LIST function_list; + memset(&function_list, 0, sizeof(function_list)); + function_list.version = NVENCAPI_STRUCT_VERSION(2); + if(nvEncodeAPICreateInstance(&function_list) != NV_ENC_SUCCESS) { + fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncodeAPICreateInstance failed\n"); + goto done; + } + + NV_ENC_OPEN_ENCODE_SESSION_EX_PARAMS params; + memset(¶ms, 0, sizeof(params)); + params.version = NVENCAPI_STRUCT_VERSION(1); + params.deviceType = NV_ENC_DEVICE_TYPE_CUDA; + params.device = cuda.cu_ctx; + params.apiVersion = NVENCAPI_VERSION; + if(function_list.nvEncOpenEncodeSessionEx(¶ms, &nvenc_encoder) != NV_ENC_SUCCESS) { + // Old nvidia gpus dont support the new nvenc api (which is required for av1). + // In such cases fallback to old api version if possible and try again. + function_list.version = NVENCAPI_STRUCT_VERSION_470(2); + if(nvEncodeAPICreateInstance(&function_list) != NV_ENC_SUCCESS) { + fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncodeAPICreateInstance (retry) failed\n"); + goto done; + } + + params.version = NVENCAPI_STRUCT_VERSION_470(1); + params.apiVersion = NVENCAPI_VERSION_470; + if(function_list.nvEncOpenEncodeSessionEx(¶ms, &nvenc_encoder) != NV_ENC_SUCCESS) { + fprintf(stderr, "gsr error: gsr_get_supported_video_codecs_nvenc: nvEncOpenEncodeSessionEx (retry) failed\n"); + goto done; + } + } + + success = get_supported_video_codecs(&function_list, nvenc_encoder, video_codecs); + + done: + if(cleanup) { + if(nvenc_encoder) + function_list.nvEncDestroyEncoder(nvenc_encoder); + if(nvenc_lib) + dlclose(nvenc_lib); + gsr_cuda_unload(&cuda); + } + + return success; +} diff --git a/src/main.cpp b/src/main.cpp index 3751fbb..ff44763 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,7 +10,7 @@ extern "C" { #include "../include/encoder/video/vaapi.h" #include "../include/encoder/video/vulkan.h" #include "../include/encoder/video/software.h" -#include "../include/codec_query/cuda.h" +#include "../include/codec_query/nvenc.h" #include "../include/codec_query/vaapi.h" #include "../include/codec_query/vulkan.h" #include "../include/egl.h" -- cgit v1.2.3