+static int cuda_device_derive(AVHWDeviceContext *device_ctx,
+ AVHWDeviceContext *src_ctx, AVDictionary *opts,
+ int flags) {
+ AVCUDADeviceContext *hwctx = device_ctx->hwctx;
+ CudaFunctions *cu;
+ const char *src_uuid = NULL;
+ int ret, i, device_count;
+
+#if CONFIG_VULKAN
+ VkPhysicalDeviceIDProperties vk_idp = {
+ .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES,
+ };
+#endif
+
+ switch (src_ctx->type) {
+#if CONFIG_VULKAN
+ case AV_HWDEVICE_TYPE_VULKAN: {
+ AVVulkanDeviceContext *vkctx = src_ctx->hwctx;
+ VkPhysicalDeviceProperties2 vk_dev_props = {
+ .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
+ .pNext = &vk_idp,
+ };
+ vkGetPhysicalDeviceProperties2(vkctx->phys_dev, &vk_dev_props);
+ src_uuid = vk_idp.deviceUUID;
+ break;
+ }
+#endif
+ default:
+ return AVERROR(ENOSYS);
+ }
+
+ if (!src_uuid) {
+ av_log(device_ctx, AV_LOG_ERROR,
+ "Failed to get UUID of source device.\n");
+ goto error;
+ }
+
+ if (cuda_device_init(device_ctx) < 0)
+ goto error;
+
+ cu = hwctx->internal->cuda_dl;
+
+ ret = CHECK_CU(cu->cuInit(0));
+ if (ret < 0)
+ goto error;
+
+ ret = CHECK_CU(cu->cuDeviceGetCount(&device_count));
+ if (ret < 0)
+ goto error;
+
+ hwctx->internal->cuda_device = -1;
+ for (i = 0; i < device_count; i++) {
+ CUdevice dev;
+ CUuuid uuid;
+
+ ret = CHECK_CU(cu->cuDeviceGet(&dev, i));
+ if (ret < 0)
+ goto error;
+
+ ret = CHECK_CU(cu->cuDeviceGetUuid(&uuid, dev));
+ if (ret < 0)
+ goto error;
+
+ if (memcmp(src_uuid, uuid.bytes, sizeof (uuid.bytes)) == 0) {
+ hwctx->internal->cuda_device = dev;
+ break;
+ }
+ }
+
+ if (hwctx->internal->cuda_device == -1) {
+ av_log(device_ctx, AV_LOG_ERROR, "Could not derive CUDA device.\n");
+ goto error;
+ }
+
+ ret = cuda_context_init(device_ctx, flags);
+ if (ret < 0)
+ goto error;