From b1f1de084416768e8b4ba76b338029b974e518a2 Mon Sep 17 00:00:00 2001 From: Timo Rothenpieler Date: Mon, 22 Nov 2021 22:42:09 +0100 Subject: [PATCH] avutil/hwcontext_cuda: add option to use primary device context --- doc/ffmpeg.texi | 15 +++++++++++++++ libavutil/hwcontext_cuda.c | 24 ++++++++++++++++++++++++ libavutil/version.h | 2 +- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi index 8418573618..082eba5f14 100644 --- a/doc/ffmpeg.texi +++ b/doc/ffmpeg.texi @@ -1096,6 +1096,21 @@ device type: @item cuda @var{device} is the number of the CUDA device. +The following options are recognized: +@table @option +@item primary_ctx +If set to 1, uses the primary device context instead of creating a new one. +@end table + +Examples: +@table @emph +@item -init_hw_device cuda:1 +Choose the second device on the system. + +@item -init_hw_device cuda:0,primary_ctx=1 +Choose the first device and use the primary device context. +@end table + @item dxva2 @var{device} is the number of the Direct3D 9 display adapter. diff --git a/libavutil/hwcontext_cuda.c b/libavutil/hwcontext_cuda.c index 48f7ecbdd3..ed7eeecb8b 100644 --- a/libavutil/hwcontext_cuda.c +++ b/libavutil/hwcontext_cuda.c @@ -376,6 +376,22 @@ static int cuda_context_init(AVHWDeviceContext *device_ctx, int flags) { return 0; } +static int cuda_flags_from_opts(AVHWDeviceContext *device_ctx, + AVDictionary *opts, int *flags) +{ + AVDictionaryEntry *primary_ctx_opt = av_dict_get(opts, "primary_ctx", NULL, 0); + + if (primary_ctx_opt && strtol(primary_ctx_opt->value, NULL, 10)) { + av_log(device_ctx, AV_LOG_VERBOSE, "Using CUDA primary device context\n"); + *flags |= AV_CUDA_USE_PRIMARY_CONTEXT; + } else if (primary_ctx_opt) { + av_log(device_ctx, AV_LOG_VERBOSE, "Disabling use of CUDA primary device context\n"); + *flags &= ~AV_CUDA_USE_PRIMARY_CONTEXT; + } + + return 0; +} + static int cuda_device_create(AVHWDeviceContext *device_ctx, const char *device, AVDictionary *opts, int flags) @@ -384,6 +400,10 @@ static int cuda_device_create(AVHWDeviceContext *device_ctx, CudaFunctions *cu; int ret, device_idx = 0; + ret = cuda_flags_from_opts(device_ctx, opts, &flags); + if (ret < 0) + return ret; + if (device) device_idx = strtol(device, NULL, 0); @@ -419,6 +439,10 @@ static int cuda_device_derive(AVHWDeviceContext *device_ctx, const char *src_uuid = NULL; int ret, i, device_count; + ret = cuda_flags_from_opts(device_ctx, opts, &flags); + if (ret < 0) + return ret; + #if CONFIG_VULKAN VkPhysicalDeviceIDProperties vk_idp = { .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES, diff --git a/libavutil/version.h b/libavutil/version.h index d94fb691d7..a2615cd299 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -80,7 +80,7 @@ #define LIBAVUTIL_VERSION_MAJOR 57 #define LIBAVUTIL_VERSION_MINOR 9 -#define LIBAVUTIL_VERSION_MICRO 100 +#define LIBAVUTIL_VERSION_MICRO 101 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ LIBAVUTIL_VERSION_MINOR, \