From 67c8ef25bbde08857e0e239aefa7a8cc71e34316 Mon Sep 17 00:00:00 2001
From: Roman Arzumanyan <r.arzumanyan@visionlabs.ai>
Date: Tue, 12 Sep 2023 17:31:30 +0300
Subject: [PATCH] Option added to use current CUDA ctx

---
 libavutil/hwcontext_cuda.c | 26 +++++++++++++++++++++++++-
 libavutil/hwcontext_cuda.h |  5 +++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/libavutil/hwcontext_cuda.c b/libavutil/hwcontext_cuda.c
index 4b298fa93e..2597a776c2 100644
--- a/libavutil/hwcontext_cuda.c
+++ b/libavutil/hwcontext_cuda.c
@@ -361,6 +361,11 @@ static int cuda_context_init(AVHWDeviceContext *device_ctx, int flags) {
                                                     hwctx->internal->cuda_device));
         if (ret < 0)
             return ret;
+    } else if (flags & AV_CUDA_USE_CURRENT_CONTEXT) {
+        ret = CHECK_CU(cu->cuCtxGetCurrent(&hwctx->cuda_ctx));
+        if (ret < 0)
+            return ret;
+        av_log(device_ctx, AV_LOG_INFO, "Using current CUDA context current .\n");
     } else {
         ret = CHECK_CU(cu->cuCtxCreate(&hwctx->cuda_ctx, desired_flags,
                                        hwctx->internal->cuda_device));
@@ -382,13 +387,32 @@ 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);
+    AVDictionaryEntry *current_ctx_opt = av_dict_get(opts, "current_ctx", NULL, 0);
+
+    int use_primary_ctx = 0, use_current_ctx = 0;
+    if (primary_ctx_opt)
+        use_primary_ctx = strtol(primary_ctx_opt->value, NULL, 10);
+    
+    if (current_ctx_opt)
+        use_current_ctx = strtol(current_ctx_opt->value, NULL, 10);
+
+    if (use_current_ctx != 0 && use_current_ctx != 0)
+        av_log(device_ctx, AV_LOG_ERROR, "Can't use primary and current CUDA ctx simultaneously\n");
 
-    if (primary_ctx_opt && strtol(primary_ctx_opt->value, NULL, 10)) {
+    if (primary_ctx_opt && use_primary_ctx != 0) {
         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;
+    } 
+    
+    if (current_ctx_opt && use_current_ctx != 0) {
+        av_log(device_ctx, AV_LOG_VERBOSE, "Using CUDA current device context\n");
+        *flags |= AV_CUDA_USE_CURRENT_CONTEXT;
+    } else if (current_ctx_opt) {
+        av_log(device_ctx, AV_LOG_VERBOSE, "Disabling use of CUDA current device context\n");
+        *flags &= ~AV_CUDA_USE_CURRENT_CONTEXT;
     }
 
     return 0;
diff --git a/libavutil/hwcontext_cuda.h b/libavutil/hwcontext_cuda.h
index cefbe0ceab..cbad434fea 100644
--- a/libavutil/hwcontext_cuda.h
+++ b/libavutil/hwcontext_cuda.h
@@ -62,6 +62,11 @@ typedef struct AVCUDADeviceContext {
  */
 #define AV_CUDA_USE_PRIMARY_CONTEXT (1 << 0)
 
+/**
+ * Use current device context instead of creating a new one.
+ */
+#define AV_CUDA_USE_CURRENT_CONTEXT (1 << 1)
+
 /**
  * @}
  */
-- 
2.39.2 (Apple Git-143)

