From b755336216e46f87b83730444c99eea5e20fbdae Mon Sep 17 00:00:00 2001
From: Ganapathy Kasi <gkasi@nvidia.com>
Date: Tue, 17 Jan 2017 16:50:40 -0800
Subject: [PATCH] libavcodec/nvenc.c Reduce initialization time for gpu id > 0

---
 libavcodec/nvenc.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c
index 4ee99a9..5fca35e 100644
--- a/libavcodec/nvenc.c
+++ b/libavcodec/nvenc.c
@@ -301,6 +301,36 @@ static int nvenc_check_capabilities(AVCodecContext *avctx)
     return 0;
 }
 
+static av_cold int check_nvenc_supported(AVCodecContext *avctx, int gpuidx)
+{
+    NvencContext *ctx = avctx->priv_data;
+    NvencDynLoadFunctions *dl_fn = &ctx->nvenc_dload_funcs;
+    CUdevice cu_device;
+    CUresult cu_res;
+    int major, minor;
+
+    cu_res = dl_fn->cuda_dl->cuDeviceGet(&cu_device, gpuidx);
+    if (cu_res != CUDA_SUCCESS) {
+        av_log(avctx, AV_LOG_ERROR,
+               "Cannot access the CUDA device %d\n",
+               gpuidx);
+        return -1;
+    }
+
+    cu_res = dl_fn->cuda_dl->cuDeviceComputeCapability(&major, &minor, cu_device);
+    if (cu_res != CUDA_SUCCESS)
+        return -1;
+
+    if (((major << 4) | minor) < NVENC_CAP) {
+        av_log(avctx, AV_LOG_VERBOSE, "does not support NVENC\n");
+        return -1;
+    }
+
+    return 0;
+
+}
+
+
 static av_cold int nvenc_check_device(AVCodecContext *avctx, int idx)
 {
     NvencContext *ctx = avctx->priv_data;
@@ -438,7 +468,17 @@ static av_cold int nvenc_setup_device(AVCodecContext *avctx)
         av_log(avctx, AV_LOG_VERBOSE, "%d CUDA capable devices found\n", nb_devices);
 
         dl_fn->nvenc_device_count = 0;
+
         for (i = 0; i < nb_devices; ++i) {
+            if (ctx->device >= 0 && ctx->device != i)
+            {
+                if (check_nvenc_supported(avctx, i) >= 0)
+                {
+                   dl_fn->nvenc_device_count++;
+                }
+                continue;
+            }
+
             if ((nvenc_check_device(avctx, i)) >= 0 && ctx->device != LIST_DEVICES)
                 return 0;
         }
-- 
1.8.1.msysgit.1

