]> git.sesse.net Git - ffmpeg/blob - libavutil/hwcontext_vulkan.c
hwcontext_vulkan: support more than one plane per DMABUF layer
[ffmpeg] / libavutil / hwcontext_vulkan.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include "config.h"
20 #include "pixdesc.h"
21 #include "avstring.h"
22 #include "imgutils.h"
23 #include "hwcontext.h"
24 #include "hwcontext_internal.h"
25 #include "hwcontext_vulkan.h"
26
27 #if CONFIG_LIBDRM
28 #include <unistd.h>
29 #include <xf86drm.h>
30 #include <drm_fourcc.h>
31 #include "hwcontext_drm.h"
32 #if CONFIG_VAAPI
33 #include <va/va_drmcommon.h>
34 #include "hwcontext_vaapi.h"
35 #endif
36 #endif
37
38 #if CONFIG_CUDA
39 #include "hwcontext_cuda_internal.h"
40 #include "cuda_check.h"
41 #define CHECK_CU(x) FF_CUDA_CHECK_DL(cuda_cu, cu, x)
42 #endif
43
44 typedef struct VulkanExecCtx {
45     VkCommandPool pool;
46     VkCommandBuffer buf;
47     VkQueue queue;
48     VkFence fence;
49 } VulkanExecCtx;
50
51 typedef struct VulkanDevicePriv {
52     /* Properties */
53     VkPhysicalDeviceProperties props;
54     VkPhysicalDeviceMemoryProperties mprops;
55
56     /* Debug callback */
57     VkDebugUtilsMessengerEXT debug_ctx;
58
59     /* Image uploading */
60     VulkanExecCtx cmd;
61
62     /* Extensions */
63     uint64_t extensions;
64
65     /* Settings */
66     int use_linear_images;
67
68     /* Nvidia */
69     int dev_is_nvidia;
70 } VulkanDevicePriv;
71
72 typedef struct VulkanFramesPriv {
73     VulkanExecCtx cmd;
74 } VulkanFramesPriv;
75
76 typedef struct AVVkFrameInternal {
77 #if CONFIG_CUDA
78     /* Importing external memory into cuda is really expensive so we keep the
79      * memory imported all the time */
80     AVBufferRef *cuda_fc_ref; /* Need to keep it around for uninit */
81     CUexternalMemory ext_mem[AV_NUM_DATA_POINTERS];
82     CUmipmappedArray cu_mma[AV_NUM_DATA_POINTERS];
83     CUarray cu_array[AV_NUM_DATA_POINTERS];
84     CUexternalSemaphore cu_sem[AV_NUM_DATA_POINTERS];
85 #endif
86 } AVVkFrameInternal;
87
88 #define VK_LOAD_PFN(inst, name) PFN_##name pfn_##name = (PFN_##name)           \
89                                               vkGetInstanceProcAddr(inst, #name)
90
91 #define DEFAULT_USAGE_FLAGS (VK_IMAGE_USAGE_SAMPLED_BIT      |                 \
92                              VK_IMAGE_USAGE_STORAGE_BIT      |                 \
93                              VK_IMAGE_USAGE_TRANSFER_SRC_BIT |                 \
94                              VK_IMAGE_USAGE_TRANSFER_DST_BIT)
95
96 #define ADD_VAL_TO_LIST(list, count, val)                                      \
97     do {                                                                       \
98         list = av_realloc_array(list, sizeof(*list), ++count);                 \
99         if (!list) {                                                           \
100             err = AVERROR(ENOMEM);                                             \
101             goto end;                                                          \
102         }                                                                      \
103         list[count - 1] = val;                                                 \
104     } while(0)
105
106 static const struct {
107     enum AVPixelFormat pixfmt;
108     const VkFormat vkfmts[3];
109 } vk_pixfmt_map[] = {
110     { AV_PIX_FMT_GRAY8,   { VK_FORMAT_R8_UNORM } },
111     { AV_PIX_FMT_GRAY16,  { VK_FORMAT_R16_UNORM } },
112     { AV_PIX_FMT_GRAYF32, { VK_FORMAT_R32_SFLOAT } },
113
114     { AV_PIX_FMT_NV12, { VK_FORMAT_R8_UNORM, VK_FORMAT_R8G8_UNORM } },
115     { AV_PIX_FMT_P010, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16G16_UNORM } },
116     { AV_PIX_FMT_P016, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16G16_UNORM } },
117
118     { AV_PIX_FMT_YUV420P, { VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM } },
119     { AV_PIX_FMT_YUV422P, { VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM } },
120     { AV_PIX_FMT_YUV444P, { VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM } },
121
122     { AV_PIX_FMT_YUV420P16, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
123     { AV_PIX_FMT_YUV422P16, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
124     { AV_PIX_FMT_YUV444P16, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
125
126     { AV_PIX_FMT_ABGR,   { VK_FORMAT_A8B8G8R8_UNORM_PACK32 } },
127     { AV_PIX_FMT_BGRA,   { VK_FORMAT_B8G8R8A8_UNORM } },
128     { AV_PIX_FMT_RGBA,   { VK_FORMAT_R8G8B8A8_UNORM } },
129     { AV_PIX_FMT_RGB24,  { VK_FORMAT_R8G8B8_UNORM } },
130     { AV_PIX_FMT_BGR24,  { VK_FORMAT_B8G8R8_UNORM } },
131     { AV_PIX_FMT_RGB48,  { VK_FORMAT_R16G16B16_UNORM } },
132     { AV_PIX_FMT_RGBA64, { VK_FORMAT_R16G16B16A16_UNORM } },
133     { AV_PIX_FMT_RGB565, { VK_FORMAT_R5G6B5_UNORM_PACK16 } },
134     { AV_PIX_FMT_BGR565, { VK_FORMAT_B5G6R5_UNORM_PACK16 } },
135     { AV_PIX_FMT_BGR0,   { VK_FORMAT_B8G8R8A8_UNORM } },
136     { AV_PIX_FMT_0BGR,   { VK_FORMAT_A8B8G8R8_UNORM_PACK32 } },
137     { AV_PIX_FMT_RGB0,   { VK_FORMAT_R8G8B8A8_UNORM } },
138
139     { AV_PIX_FMT_GBRPF32, { VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT } },
140 };
141
142 const VkFormat *av_vkfmt_from_pixfmt(enum AVPixelFormat p)
143 {
144     for (enum AVPixelFormat i = 0; i < FF_ARRAY_ELEMS(vk_pixfmt_map); i++)
145         if (vk_pixfmt_map[i].pixfmt == p)
146             return vk_pixfmt_map[i].vkfmts;
147     return NULL;
148 }
149
150 static int pixfmt_is_supported(AVVulkanDeviceContext *hwctx, enum AVPixelFormat p,
151                                int linear)
152 {
153     const VkFormat *fmt = av_vkfmt_from_pixfmt(p);
154     int planes = av_pix_fmt_count_planes(p);
155
156     if (!fmt)
157         return 0;
158
159     for (int i = 0; i < planes; i++) {
160         VkFormatFeatureFlags flags;
161         VkFormatProperties2 prop = {
162             .sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2,
163         };
164         vkGetPhysicalDeviceFormatProperties2(hwctx->phys_dev, fmt[i], &prop);
165         flags = linear ? prop.formatProperties.linearTilingFeatures :
166                          prop.formatProperties.optimalTilingFeatures;
167         if (!(flags & DEFAULT_USAGE_FLAGS))
168             return 0;
169     }
170
171     return 1;
172 }
173
174 enum VulkanExtensions {
175     EXT_EXTERNAL_DMABUF_MEMORY = 1ULL <<  0, /* VK_EXT_external_memory_dma_buf */
176     EXT_DRM_MODIFIER_FLAGS     = 1ULL <<  1, /* VK_EXT_image_drm_format_modifier */
177     EXT_EXTERNAL_FD_MEMORY     = 1ULL <<  2, /* VK_KHR_external_memory_fd */
178     EXT_EXTERNAL_FD_SEM        = 1ULL <<  3, /* VK_KHR_external_semaphore_fd */
179
180     EXT_OPTIONAL               = 1ULL << 62,
181     EXT_REQUIRED               = 1ULL << 63,
182 };
183
184 typedef struct VulkanOptExtension {
185     const char *name;
186     uint64_t flag;
187 } VulkanOptExtension;
188
189 static const VulkanOptExtension optional_instance_exts[] = {
190     /* For future use */
191 };
192
193 static const VulkanOptExtension optional_device_exts[] = {
194     { VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,               EXT_EXTERNAL_FD_MEMORY,     },
195     { VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME,          EXT_EXTERNAL_DMABUF_MEMORY, },
196     { VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME,        EXT_DRM_MODIFIER_FLAGS,     },
197     { VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME,            EXT_EXTERNAL_FD_SEM,        },
198 };
199
200 /* Converts return values to strings */
201 static const char *vk_ret2str(VkResult res)
202 {
203 #define CASE(VAL) case VAL: return #VAL
204     switch (res) {
205     CASE(VK_SUCCESS);
206     CASE(VK_NOT_READY);
207     CASE(VK_TIMEOUT);
208     CASE(VK_EVENT_SET);
209     CASE(VK_EVENT_RESET);
210     CASE(VK_INCOMPLETE);
211     CASE(VK_ERROR_OUT_OF_HOST_MEMORY);
212     CASE(VK_ERROR_OUT_OF_DEVICE_MEMORY);
213     CASE(VK_ERROR_INITIALIZATION_FAILED);
214     CASE(VK_ERROR_DEVICE_LOST);
215     CASE(VK_ERROR_MEMORY_MAP_FAILED);
216     CASE(VK_ERROR_LAYER_NOT_PRESENT);
217     CASE(VK_ERROR_EXTENSION_NOT_PRESENT);
218     CASE(VK_ERROR_FEATURE_NOT_PRESENT);
219     CASE(VK_ERROR_INCOMPATIBLE_DRIVER);
220     CASE(VK_ERROR_TOO_MANY_OBJECTS);
221     CASE(VK_ERROR_FORMAT_NOT_SUPPORTED);
222     CASE(VK_ERROR_FRAGMENTED_POOL);
223     CASE(VK_ERROR_SURFACE_LOST_KHR);
224     CASE(VK_ERROR_NATIVE_WINDOW_IN_USE_KHR);
225     CASE(VK_SUBOPTIMAL_KHR);
226     CASE(VK_ERROR_OUT_OF_DATE_KHR);
227     CASE(VK_ERROR_INCOMPATIBLE_DISPLAY_KHR);
228     CASE(VK_ERROR_VALIDATION_FAILED_EXT);
229     CASE(VK_ERROR_INVALID_SHADER_NV);
230     CASE(VK_ERROR_OUT_OF_POOL_MEMORY);
231     CASE(VK_ERROR_INVALID_EXTERNAL_HANDLE);
232     CASE(VK_ERROR_NOT_PERMITTED_EXT);
233     CASE(VK_ERROR_INVALID_DRM_FORMAT_MODIFIER_PLANE_LAYOUT_EXT);
234     CASE(VK_ERROR_INVALID_DEVICE_ADDRESS_EXT);
235     CASE(VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT);
236     default: return "Unknown error";
237     }
238 #undef CASE
239 }
240
241 static VkBool32 vk_dbg_callback(VkDebugUtilsMessageSeverityFlagBitsEXT severity,
242                                 VkDebugUtilsMessageTypeFlagsEXT messageType,
243                                 const VkDebugUtilsMessengerCallbackDataEXT *data,
244                                 void *priv)
245 {
246     int l;
247     AVHWDeviceContext *ctx = priv;
248
249     switch (severity) {
250     case VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT: l = AV_LOG_VERBOSE; break;
251     case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT:    l = AV_LOG_INFO;    break;
252     case VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT: l = AV_LOG_WARNING; break;
253     case VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT:   l = AV_LOG_ERROR;   break;
254     default:                                              l = AV_LOG_DEBUG;   break;
255     }
256
257     av_log(ctx, l, "%s\n", data->pMessage);
258     for (int i = 0; i < data->cmdBufLabelCount; i++)
259         av_log(ctx, l, "\t%i: %s\n", i, data->pCmdBufLabels[i].pLabelName);
260
261     return 0;
262 }
263
264 static int check_extensions(AVHWDeviceContext *ctx, int dev,
265                             const char * const **dst, uint32_t *num, int debug)
266 {
267     const char *tstr;
268     const char **extension_names = NULL;
269     VulkanDevicePriv *p = ctx->internal->priv;
270     AVVulkanDeviceContext *hwctx = ctx->hwctx;
271     int err = 0, found, extensions_found = 0;
272
273     const char *mod;
274     int optional_exts_num;
275     uint32_t sup_ext_count;
276     VkExtensionProperties *sup_ext;
277     const VulkanOptExtension *optional_exts;
278
279     if (!dev) {
280         mod = "instance";
281         optional_exts = optional_instance_exts;
282         optional_exts_num = FF_ARRAY_ELEMS(optional_instance_exts);
283         vkEnumerateInstanceExtensionProperties(NULL, &sup_ext_count, NULL);
284         sup_ext = av_malloc_array(sup_ext_count, sizeof(VkExtensionProperties));
285         if (!sup_ext)
286             return AVERROR(ENOMEM);
287         vkEnumerateInstanceExtensionProperties(NULL, &sup_ext_count, sup_ext);
288     } else {
289         mod = "device";
290         optional_exts = optional_device_exts;
291         optional_exts_num = FF_ARRAY_ELEMS(optional_device_exts);
292         vkEnumerateDeviceExtensionProperties(hwctx->phys_dev, NULL,
293                                              &sup_ext_count, NULL);
294         sup_ext = av_malloc_array(sup_ext_count, sizeof(VkExtensionProperties));
295         if (!sup_ext)
296             return AVERROR(ENOMEM);
297         vkEnumerateDeviceExtensionProperties(hwctx->phys_dev, NULL,
298                                              &sup_ext_count, sup_ext);
299     }
300
301     for (int i = 0; i < optional_exts_num; i++) {
302         int req = optional_exts[i].flag & EXT_REQUIRED;
303         tstr = optional_exts[i].name;
304
305         found = 0;
306         for (int j = 0; j < sup_ext_count; j++) {
307             if (!strcmp(tstr, sup_ext[j].extensionName)) {
308                 found = 1;
309                 break;
310             }
311         }
312         if (!found) {
313             int lvl = req ? AV_LOG_ERROR : AV_LOG_VERBOSE;
314             av_log(ctx, lvl, "Extension \"%s\" not found!\n", tstr);
315             if (req) {
316                 err = AVERROR(EINVAL);
317                 goto end;
318             }
319             continue;
320         }
321         if (!req)
322             p->extensions |= optional_exts[i].flag;
323
324         av_log(ctx, AV_LOG_VERBOSE, "Using %s extension \"%s\"\n", mod, tstr);
325
326         ADD_VAL_TO_LIST(extension_names, extensions_found, tstr);
327     }
328
329     if (debug && !dev) {
330         tstr = VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
331         found = 0;
332         for (int j = 0; j < sup_ext_count; j++) {
333             if (!strcmp(tstr, sup_ext[j].extensionName)) {
334                 found = 1;
335                 break;
336             }
337         }
338         if (found) {
339             ADD_VAL_TO_LIST(extension_names, extensions_found, tstr);
340         } else {
341             av_log(ctx, AV_LOG_ERROR, "Debug extension \"%s\" not found!\n",
342                    tstr);
343             err = AVERROR(EINVAL);
344             goto end;
345         }
346     }
347
348     *dst = extension_names;
349     *num = extensions_found;
350
351 end:
352     av_free(sup_ext);
353     return err;
354 }
355
356 /* Creates a VkInstance */
357 static int create_instance(AVHWDeviceContext *ctx, AVDictionary *opts)
358 {
359     int err = 0;
360     VkResult ret;
361     VulkanDevicePriv *p = ctx->internal->priv;
362     AVVulkanDeviceContext *hwctx = ctx->hwctx;
363     AVDictionaryEntry *debug_opt = av_dict_get(opts, "debug", NULL, 0);
364     const int debug_mode = debug_opt && strtol(debug_opt->value, NULL, 10);
365     VkApplicationInfo application_info = {
366         .sType              = VK_STRUCTURE_TYPE_APPLICATION_INFO,
367         .pEngineName        = "libavutil",
368         .apiVersion         = VK_API_VERSION_1_1,
369         .engineVersion      = VK_MAKE_VERSION(LIBAVUTIL_VERSION_MAJOR,
370                                               LIBAVUTIL_VERSION_MINOR,
371                                               LIBAVUTIL_VERSION_MICRO),
372     };
373     VkInstanceCreateInfo inst_props = {
374         .sType            = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
375         .pApplicationInfo = &application_info,
376     };
377
378     /* Check for present/missing extensions */
379     err = check_extensions(ctx, 0, &inst_props.ppEnabledExtensionNames,
380                            &inst_props.enabledExtensionCount, debug_mode);
381     if (err < 0)
382         return err;
383
384     if (debug_mode) {
385         static const char *layers[] = { "VK_LAYER_LUNARG_standard_validation" };
386         inst_props.ppEnabledLayerNames = layers;
387         inst_props.enabledLayerCount = FF_ARRAY_ELEMS(layers);
388     }
389
390     /* Try to create the instance */
391     ret = vkCreateInstance(&inst_props, hwctx->alloc, &hwctx->inst);
392
393     /* Free used memory */
394     av_free((void *)inst_props.ppEnabledExtensionNames);
395
396     /* Check for errors */
397     if (ret != VK_SUCCESS) {
398         av_log(ctx, AV_LOG_ERROR, "Instance creation failure: %s\n",
399                vk_ret2str(ret));
400         return AVERROR_EXTERNAL;
401     }
402
403     if (debug_mode) {
404         VkDebugUtilsMessengerCreateInfoEXT dbg = {
405             .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
406             .messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
407                                VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT    |
408                                VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
409                                VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT,
410             .messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT    |
411                            VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
412                            VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT,
413             .pfnUserCallback = vk_dbg_callback,
414             .pUserData = ctx,
415         };
416         VK_LOAD_PFN(hwctx->inst, vkCreateDebugUtilsMessengerEXT);
417
418         pfn_vkCreateDebugUtilsMessengerEXT(hwctx->inst, &dbg,
419                                            hwctx->alloc, &p->debug_ctx);
420     }
421
422     return 0;
423 }
424
425 typedef struct VulkanDeviceSelection {
426     uint8_t uuid[VK_UUID_SIZE]; /* Will use this first unless !has_uuid */
427     int has_uuid;
428     const char *name; /* Will use this second unless NULL */
429     uint32_t pci_device; /* Will use this third unless 0x0 */
430     uint32_t vendor_id; /* Last resort to find something deterministic */
431     int index; /* Finally fall back to index */
432 } VulkanDeviceSelection;
433
434 static const char *vk_dev_type(enum VkPhysicalDeviceType type)
435 {
436     switch (type) {
437     case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU: return "integrated";
438     case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:   return "discrete";
439     case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:    return "virtual";
440     case VK_PHYSICAL_DEVICE_TYPE_CPU:            return "software";
441     default:                                     return "unknown";
442     }
443 }
444
445 /* Finds a device */
446 static int find_device(AVHWDeviceContext *ctx, VulkanDeviceSelection *select)
447 {
448     int err = 0, choice = -1;
449     uint32_t num;
450     VkResult ret;
451     VkPhysicalDevice *devices = NULL;
452     VkPhysicalDeviceIDProperties *idp = NULL;
453     VkPhysicalDeviceProperties2 *prop = NULL;
454     VulkanDevicePriv *p = ctx->internal->priv;
455     AVVulkanDeviceContext *hwctx = ctx->hwctx;
456
457     ret = vkEnumeratePhysicalDevices(hwctx->inst, &num, NULL);
458     if (ret != VK_SUCCESS || !num) {
459         av_log(ctx, AV_LOG_ERROR, "No devices found: %s!\n", vk_ret2str(ret));
460         return AVERROR(ENODEV);
461     }
462
463     devices = av_malloc_array(num, sizeof(VkPhysicalDevice));
464     if (!devices)
465         return AVERROR(ENOMEM);
466
467     ret = vkEnumeratePhysicalDevices(hwctx->inst, &num, devices);
468     if (ret != VK_SUCCESS) {
469         av_log(ctx, AV_LOG_ERROR, "Failed enumerating devices: %s\n",
470                vk_ret2str(ret));
471         err = AVERROR(ENODEV);
472         goto end;
473     }
474
475     prop = av_mallocz_array(num, sizeof(*prop));
476     if (!prop) {
477         err = AVERROR(ENOMEM);
478         goto end;
479     }
480
481     idp = av_mallocz_array(num, sizeof(*idp));
482     if (!idp) {
483         err = AVERROR(ENOMEM);
484         goto end;
485     }
486
487     av_log(ctx, AV_LOG_VERBOSE, "GPU listing:\n");
488     for (int i = 0; i < num; i++) {
489         idp[i].sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES;
490         prop[i].sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
491         prop[i].pNext = &idp[i];
492
493         vkGetPhysicalDeviceProperties2(devices[i], &prop[i]);
494         av_log(ctx, AV_LOG_VERBOSE, "    %d: %s (%s) (0x%x)\n", i,
495                prop[i].properties.deviceName,
496                vk_dev_type(prop[i].properties.deviceType),
497                prop[i].properties.deviceID);
498     }
499
500     if (select->has_uuid) {
501         for (int i = 0; i < num; i++) {
502             if (!strncmp(idp[i].deviceUUID, select->uuid, VK_UUID_SIZE)) {
503                 choice = i;
504                 goto end;
505              }
506         }
507         av_log(ctx, AV_LOG_ERROR, "Unable to find device by given UUID!\n");
508         err = AVERROR(ENODEV);
509         goto end;
510     } else if (select->name) {
511         av_log(ctx, AV_LOG_VERBOSE, "Requested device: %s\n", select->name);
512         for (int i = 0; i < num; i++) {
513             if (strstr(prop[i].properties.deviceName, select->name)) {
514                 choice = i;
515                 goto end;
516              }
517         }
518         av_log(ctx, AV_LOG_ERROR, "Unable to find device \"%s\"!\n",
519                select->name);
520         err = AVERROR(ENODEV);
521         goto end;
522     } else if (select->pci_device) {
523         av_log(ctx, AV_LOG_VERBOSE, "Requested device: 0x%x\n", select->pci_device);
524         for (int i = 0; i < num; i++) {
525             if (select->pci_device == prop[i].properties.deviceID) {
526                 choice = i;
527                 goto end;
528             }
529         }
530         av_log(ctx, AV_LOG_ERROR, "Unable to find device with PCI ID 0x%x!\n",
531                select->pci_device);
532         err = AVERROR(EINVAL);
533         goto end;
534     } else if (select->vendor_id) {
535         av_log(ctx, AV_LOG_VERBOSE, "Requested vendor: 0x%x\n", select->vendor_id);
536         for (int i = 0; i < num; i++) {
537             if (select->vendor_id == prop[i].properties.vendorID) {
538                 choice = i;
539                 goto end;
540             }
541         }
542         av_log(ctx, AV_LOG_ERROR, "Unable to find device with Vendor ID 0x%x!\n",
543                select->vendor_id);
544         err = AVERROR(ENODEV);
545         goto end;
546     } else {
547         if (select->index < num) {
548             choice = select->index;
549             goto end;
550         }
551         av_log(ctx, AV_LOG_ERROR, "Unable to find device with index %i!\n",
552                select->index);
553         err = AVERROR(ENODEV);
554         goto end;
555     }
556
557 end:
558     if (choice > -1) {
559         p->dev_is_nvidia = (prop[choice].properties.vendorID == 0x10de);
560         hwctx->phys_dev = devices[choice];
561     }
562     av_free(devices);
563     av_free(prop);
564     av_free(idp);
565
566     return err;
567 }
568
569 static int search_queue_families(AVHWDeviceContext *ctx, VkDeviceCreateInfo *cd)
570 {
571     uint32_t num;
572     VkQueueFamilyProperties *qs = NULL;
573     AVVulkanDeviceContext *hwctx = ctx->hwctx;
574     int graph_index = -1, comp_index = -1, tx_index = -1;
575     VkDeviceQueueCreateInfo *pc = (VkDeviceQueueCreateInfo *)cd->pQueueCreateInfos;
576
577     /* First get the number of queue families */
578     vkGetPhysicalDeviceQueueFamilyProperties(hwctx->phys_dev, &num, NULL);
579     if (!num) {
580         av_log(ctx, AV_LOG_ERROR, "Failed to get queues!\n");
581         return AVERROR_EXTERNAL;
582     }
583
584     /* Then allocate memory */
585     qs = av_malloc_array(num, sizeof(VkQueueFamilyProperties));
586     if (!qs)
587         return AVERROR(ENOMEM);
588
589     /* Finally retrieve the queue families */
590     vkGetPhysicalDeviceQueueFamilyProperties(hwctx->phys_dev, &num, qs);
591
592 #define SEARCH_FLAGS(expr, out)                                                \
593     for (int i = 0; i < num; i++) {                                            \
594         const VkQueueFlagBits flags = qs[i].queueFlags;                        \
595         if (expr) {                                                            \
596             out = i;                                                           \
597             break;                                                             \
598         }                                                                      \
599     }
600
601     SEARCH_FLAGS(flags & VK_QUEUE_GRAPHICS_BIT, graph_index)
602
603     SEARCH_FLAGS((flags &  VK_QUEUE_COMPUTE_BIT) && (i != graph_index),
604                  comp_index)
605
606     SEARCH_FLAGS((flags & VK_QUEUE_TRANSFER_BIT) && (i != graph_index) &&
607                  (i != comp_index), tx_index)
608
609 #undef SEARCH_FLAGS
610 #define QF_FLAGS(flags)                                                        \
611     ((flags) & VK_QUEUE_GRAPHICS_BIT      ) ? "(graphics) " : "",              \
612     ((flags) & VK_QUEUE_COMPUTE_BIT       ) ? "(compute) "  : "",              \
613     ((flags) & VK_QUEUE_TRANSFER_BIT      ) ? "(transfer) " : "",              \
614     ((flags) & VK_QUEUE_SPARSE_BINDING_BIT) ? "(sparse) "   : ""
615
616     av_log(ctx, AV_LOG_VERBOSE, "Using queue family %i for graphics, "
617            "flags: %s%s%s%s\n", graph_index, QF_FLAGS(qs[graph_index].queueFlags));
618
619     hwctx->queue_family_index      = graph_index;
620     hwctx->queue_family_tx_index   = graph_index;
621     hwctx->queue_family_comp_index = graph_index;
622
623     pc[cd->queueCreateInfoCount++].queueFamilyIndex = graph_index;
624
625     if (comp_index != -1) {
626         av_log(ctx, AV_LOG_VERBOSE, "Using queue family %i for compute, "
627                "flags: %s%s%s%s\n", comp_index, QF_FLAGS(qs[comp_index].queueFlags));
628         hwctx->queue_family_tx_index                    = comp_index;
629         hwctx->queue_family_comp_index                  = comp_index;
630         pc[cd->queueCreateInfoCount++].queueFamilyIndex = comp_index;
631     }
632
633     if (tx_index != -1) {
634         av_log(ctx, AV_LOG_VERBOSE, "Using queue family %i for transfers, "
635                "flags: %s%s%s%s\n", tx_index, QF_FLAGS(qs[tx_index].queueFlags));
636         hwctx->queue_family_tx_index                    = tx_index;
637         pc[cd->queueCreateInfoCount++].queueFamilyIndex = tx_index;
638     }
639
640 #undef QF_FLAGS
641
642     av_free(qs);
643
644     return 0;
645 }
646
647 static int create_exec_ctx(AVHWDeviceContext *ctx, VulkanExecCtx *cmd,
648                            int queue_family_index)
649 {
650     VkResult ret;
651     AVVulkanDeviceContext *hwctx = ctx->hwctx;
652
653     VkCommandPoolCreateInfo cqueue_create = {
654         .sType              = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
655         .flags              = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
656         .queueFamilyIndex   = queue_family_index,
657     };
658     VkCommandBufferAllocateInfo cbuf_create = {
659         .sType              = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
660         .level              = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
661         .commandBufferCount = 1,
662     };
663
664     VkFenceCreateInfo fence_spawn = {
665         .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
666     };
667
668     ret = vkCreateFence(hwctx->act_dev, &fence_spawn,
669                         hwctx->alloc, &cmd->fence);
670     if (ret != VK_SUCCESS) {
671         av_log(ctx, AV_LOG_ERROR, "Failed to create frame fence: %s\n",
672                vk_ret2str(ret));
673         return AVERROR_EXTERNAL;
674     }
675
676     ret = vkCreateCommandPool(hwctx->act_dev, &cqueue_create,
677                               hwctx->alloc, &cmd->pool);
678     if (ret != VK_SUCCESS) {
679         av_log(ctx, AV_LOG_ERROR, "Command pool creation failure: %s\n",
680                vk_ret2str(ret));
681         return AVERROR_EXTERNAL;
682     }
683
684     cbuf_create.commandPool = cmd->pool;
685
686     ret = vkAllocateCommandBuffers(hwctx->act_dev, &cbuf_create, &cmd->buf);
687     if (ret != VK_SUCCESS) {
688         av_log(ctx, AV_LOG_ERROR, "Command buffer alloc failure: %s\n",
689                vk_ret2str(ret));
690         return AVERROR_EXTERNAL;
691     }
692
693     vkGetDeviceQueue(hwctx->act_dev, cqueue_create.queueFamilyIndex, 0,
694                      &cmd->queue);
695
696     return 0;
697 }
698
699 static void free_exec_ctx(AVHWDeviceContext *ctx, VulkanExecCtx *cmd)
700 {
701     AVVulkanDeviceContext *hwctx = ctx->hwctx;
702
703     if (cmd->fence)
704         vkDestroyFence(hwctx->act_dev, cmd->fence, hwctx->alloc);
705     if (cmd->buf)
706         vkFreeCommandBuffers(hwctx->act_dev, cmd->pool, 1, &cmd->buf);
707     if (cmd->pool)
708         vkDestroyCommandPool(hwctx->act_dev, cmd->pool, hwctx->alloc);
709 }
710
711 static void vulkan_device_free(AVHWDeviceContext *ctx)
712 {
713     VulkanDevicePriv *p = ctx->internal->priv;
714     AVVulkanDeviceContext *hwctx = ctx->hwctx;
715
716     free_exec_ctx(ctx, &p->cmd);
717
718     vkDestroyDevice(hwctx->act_dev, hwctx->alloc);
719
720     if (p->debug_ctx) {
721         VK_LOAD_PFN(hwctx->inst, vkDestroyDebugUtilsMessengerEXT);
722         pfn_vkDestroyDebugUtilsMessengerEXT(hwctx->inst, p->debug_ctx,
723                                             hwctx->alloc);
724     }
725
726     vkDestroyInstance(hwctx->inst, hwctx->alloc);
727 }
728
729 static int vulkan_device_create_internal(AVHWDeviceContext *ctx,
730                                          VulkanDeviceSelection *dev_select,
731                                          AVDictionary *opts, int flags)
732 {
733     int err = 0;
734     VkResult ret;
735     AVDictionaryEntry *opt_d;
736     VulkanDevicePriv *p = ctx->internal->priv;
737     AVVulkanDeviceContext *hwctx = ctx->hwctx;
738     VkDeviceQueueCreateInfo queue_create_info[3] = {
739         {   .sType            = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
740             .pQueuePriorities = (float []){ 1.0f },
741             .queueCount       = 1, },
742         {   .sType            = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
743             .pQueuePriorities = (float []){ 1.0f },
744             .queueCount       = 1, },
745         {   .sType            = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
746             .pQueuePriorities = (float []){ 1.0f },
747             .queueCount       = 1, },
748     };
749
750     VkDeviceCreateInfo dev_info = {
751         .sType                = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
752         .pQueueCreateInfos    = queue_create_info,
753         .queueCreateInfoCount = 0,
754     };
755
756     ctx->free = vulkan_device_free;
757
758     /* Create an instance if not given one */
759     if ((err = create_instance(ctx, opts)))
760         goto end;
761
762     /* Find a device (if not given one) */
763     if ((err = find_device(ctx, dev_select)))
764         goto end;
765
766     vkGetPhysicalDeviceProperties(hwctx->phys_dev, &p->props);
767     av_log(ctx, AV_LOG_VERBOSE, "Using device: %s\n", p->props.deviceName);
768     av_log(ctx, AV_LOG_VERBOSE, "Alignments:\n");
769     av_log(ctx, AV_LOG_VERBOSE, "    optimalBufferCopyOffsetAlignment:   %li\n",
770            p->props.limits.optimalBufferCopyOffsetAlignment);
771     av_log(ctx, AV_LOG_VERBOSE, "    optimalBufferCopyRowPitchAlignment: %li\n",
772            p->props.limits.optimalBufferCopyRowPitchAlignment);
773     av_log(ctx, AV_LOG_VERBOSE, "    minMemoryMapAlignment:              %li\n",
774            p->props.limits.minMemoryMapAlignment);
775
776     /* Search queue family */
777     if ((err = search_queue_families(ctx, &dev_info)))
778         goto end;
779
780     if ((err = check_extensions(ctx, 1, &dev_info.ppEnabledExtensionNames,
781                                 &dev_info.enabledExtensionCount, 0)))
782         goto end;
783
784     ret = vkCreateDevice(hwctx->phys_dev, &dev_info, hwctx->alloc,
785                          &hwctx->act_dev);
786
787     av_free((void *)dev_info.ppEnabledExtensionNames);
788
789     if (ret != VK_SUCCESS) {
790         av_log(ctx, AV_LOG_ERROR, "Device creation failure: %s\n",
791                vk_ret2str(ret));
792         err = AVERROR_EXTERNAL;
793         goto end;
794     }
795
796     /* Tiled images setting, use them by default */
797     opt_d = av_dict_get(opts, "linear_images", NULL, 0);
798     if (opt_d)
799         p->use_linear_images = strtol(opt_d->value, NULL, 10);
800
801 end:
802     return err;
803 }
804
805 static int vulkan_device_init(AVHWDeviceContext *ctx)
806 {
807     int err;
808     uint32_t queue_num;
809     AVVulkanDeviceContext *hwctx = ctx->hwctx;
810     VulkanDevicePriv *p = ctx->internal->priv;
811
812     vkGetPhysicalDeviceQueueFamilyProperties(hwctx->phys_dev, &queue_num, NULL);
813     if (!queue_num) {
814         av_log(ctx, AV_LOG_ERROR, "Failed to get queues!\n");
815         return AVERROR_EXTERNAL;
816     }
817
818 #define CHECK_QUEUE(type, n)                                                         \
819 if (n >= queue_num) {                                                                \
820     av_log(ctx, AV_LOG_ERROR, "Invalid %s queue index %i (device has %i queues)!\n", \
821            type, n, queue_num);                                                      \
822     return AVERROR(EINVAL);                                                          \
823 }
824
825     CHECK_QUEUE("graphics", hwctx->queue_family_index)
826     CHECK_QUEUE("upload",   hwctx->queue_family_tx_index)
827     CHECK_QUEUE("compute",  hwctx->queue_family_comp_index)
828
829 #undef CHECK_QUEUE
830
831     /* Create exec context - if there's something invalid this will error out */
832     err = create_exec_ctx(ctx, &p->cmd, hwctx->queue_family_tx_index);
833     if (err)
834         return err;
835
836     /* Get device capabilities */
837     vkGetPhysicalDeviceMemoryProperties(hwctx->phys_dev, &p->mprops);
838
839     return 0;
840 }
841
842 static int vulkan_device_create(AVHWDeviceContext *ctx, const char *device,
843                                 AVDictionary *opts, int flags)
844 {
845     VulkanDeviceSelection dev_select = { 0 };
846     if (device && device[0]) {
847         char *end = NULL;
848         dev_select.index = strtol(device, &end, 10);
849         if (end == device) {
850             dev_select.index = 0;
851             dev_select.name  = device;
852         }
853     }
854
855     return vulkan_device_create_internal(ctx, &dev_select, opts, flags);
856 }
857
858 static int vulkan_device_derive(AVHWDeviceContext *ctx,
859                                 AVHWDeviceContext *src_ctx, int flags)
860 {
861     av_unused VulkanDeviceSelection dev_select = { 0 };
862
863     /* If there's only one device on the system, then even if its not covered
864      * by the following checks (e.g. non-PCIe ARM GPU), having an empty
865      * dev_select will mean it'll get picked. */
866     switch(src_ctx->type) {
867 #if CONFIG_LIBDRM
868 #if CONFIG_VAAPI
869     case AV_HWDEVICE_TYPE_VAAPI: {
870         AVVAAPIDeviceContext *src_hwctx = src_ctx->hwctx;
871
872         const char *vendor = vaQueryVendorString(src_hwctx->display);
873         if (!vendor) {
874             av_log(ctx, AV_LOG_ERROR, "Unable to get device info from VAAPI!\n");
875             return AVERROR_EXTERNAL;
876         }
877
878         if (strstr(vendor, "Intel"))
879             dev_select.vendor_id = 0x8086;
880         if (strstr(vendor, "AMD"))
881             dev_select.vendor_id = 0x1002;
882
883         return vulkan_device_create_internal(ctx, &dev_select, NULL, flags);
884     }
885 #endif
886     case AV_HWDEVICE_TYPE_DRM: {
887         AVDRMDeviceContext *src_hwctx = src_ctx->hwctx;
888
889         drmDevice *drm_dev_info;
890         int err = drmGetDevice(src_hwctx->fd, &drm_dev_info);
891         if (err) {
892             av_log(ctx, AV_LOG_ERROR, "Unable to get device info from DRM fd!\n");
893             return AVERROR_EXTERNAL;
894         }
895
896         if (drm_dev_info->bustype == DRM_BUS_PCI)
897             dev_select.pci_device = drm_dev_info->deviceinfo.pci->device_id;
898
899         drmFreeDevice(&drm_dev_info);
900
901         return vulkan_device_create_internal(ctx, &dev_select, NULL, flags);
902     }
903 #endif
904 #if CONFIG_CUDA
905     case AV_HWDEVICE_TYPE_CUDA: {
906         AVHWDeviceContext *cuda_cu = src_ctx;
907         AVCUDADeviceContext *src_hwctx = src_ctx->hwctx;
908         AVCUDADeviceContextInternal *cu_internal = src_hwctx->internal;
909         CudaFunctions *cu = cu_internal->cuda_dl;
910
911         int ret = CHECK_CU(cu->cuDeviceGetUuid((CUuuid *)&dev_select.uuid,
912                                                cu_internal->cuda_device));
913         if (ret < 0) {
914             av_log(ctx, AV_LOG_ERROR, "Unable to get UUID from CUDA!\n");
915             return AVERROR_EXTERNAL;
916         }
917
918         dev_select.has_uuid = 1;
919
920         return vulkan_device_create_internal(ctx, &dev_select, NULL, flags);
921     }
922 #endif
923     default:
924         return AVERROR(ENOSYS);
925     }
926 }
927
928 static int vulkan_frames_get_constraints(AVHWDeviceContext *ctx,
929                                          const void *hwconfig,
930                                          AVHWFramesConstraints *constraints)
931 {
932     int count = 0;
933     AVVulkanDeviceContext *hwctx = ctx->hwctx;
934     VulkanDevicePriv *p = ctx->internal->priv;
935
936     for (enum AVPixelFormat i = 0; i < AV_PIX_FMT_NB; i++)
937         count += pixfmt_is_supported(hwctx, i, p->use_linear_images);
938
939 #if CONFIG_CUDA
940     if (p->dev_is_nvidia)
941         count++;
942 #endif
943
944     constraints->valid_sw_formats = av_malloc_array(count + 1,
945                                                     sizeof(enum AVPixelFormat));
946     if (!constraints->valid_sw_formats)
947         return AVERROR(ENOMEM);
948
949     count = 0;
950     for (enum AVPixelFormat i = 0; i < AV_PIX_FMT_NB; i++)
951         if (pixfmt_is_supported(hwctx, i, p->use_linear_images))
952             constraints->valid_sw_formats[count++] = i;
953
954 #if CONFIG_CUDA
955     if (p->dev_is_nvidia)
956         constraints->valid_sw_formats[count++] = AV_PIX_FMT_CUDA;
957 #endif
958     constraints->valid_sw_formats[count++] = AV_PIX_FMT_NONE;
959
960     constraints->min_width  = 0;
961     constraints->min_height = 0;
962     constraints->max_width  = p->props.limits.maxImageDimension2D;
963     constraints->max_height = p->props.limits.maxImageDimension2D;
964
965     constraints->valid_hw_formats = av_malloc_array(2, sizeof(enum AVPixelFormat));
966     if (!constraints->valid_hw_formats)
967         return AVERROR(ENOMEM);
968
969     constraints->valid_hw_formats[0] = AV_PIX_FMT_VULKAN;
970     constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
971
972     return 0;
973 }
974
975 static int alloc_mem(AVHWDeviceContext *ctx, VkMemoryRequirements *req,
976                      VkMemoryPropertyFlagBits req_flags, void *alloc_extension,
977                      VkMemoryPropertyFlagBits *mem_flags, VkDeviceMemory *mem)
978 {
979     VkResult ret;
980     int index = -1;
981     VulkanDevicePriv *p = ctx->internal->priv;
982     AVVulkanDeviceContext *dev_hwctx = ctx->hwctx;
983     VkMemoryAllocateInfo alloc_info = {
984         .sType           = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
985         .pNext           = alloc_extension,
986     };
987
988     /* Align if we need to */
989     if (req_flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
990         req->size = FFALIGN(req->size, p->props.limits.minMemoryMapAlignment);
991
992     alloc_info.allocationSize = req->size;
993
994     /* The vulkan spec requires memory types to be sorted in the "optimal"
995      * order, so the first matching type we find will be the best/fastest one */
996     for (int i = 0; i < p->mprops.memoryTypeCount; i++) {
997         /* The memory type must be supported by the requirements (bitfield) */
998         if (!(req->memoryTypeBits & (1 << i)))
999             continue;
1000
1001         /* The memory type flags must include our properties */
1002         if ((p->mprops.memoryTypes[i].propertyFlags & req_flags) != req_flags)
1003             continue;
1004
1005         /* Found a suitable memory type */
1006         index = i;
1007         break;
1008     }
1009
1010     if (index < 0) {
1011         av_log(ctx, AV_LOG_ERROR, "No memory type found for flags 0x%x\n",
1012                req_flags);
1013         return AVERROR(EINVAL);
1014     }
1015
1016     alloc_info.memoryTypeIndex = index;
1017
1018     ret = vkAllocateMemory(dev_hwctx->act_dev, &alloc_info,
1019                            dev_hwctx->alloc, mem);
1020     if (ret != VK_SUCCESS) {
1021         av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory: %s\n",
1022                vk_ret2str(ret));
1023         return AVERROR(ENOMEM);
1024     }
1025
1026     *mem_flags |= p->mprops.memoryTypes[index].propertyFlags;
1027
1028     return 0;
1029 }
1030
1031 static void vulkan_free_internal(AVVkFrameInternal *internal)
1032 {
1033     if (!internal)
1034         return;
1035
1036 #if CONFIG_CUDA
1037     if (internal->cuda_fc_ref) {
1038         AVHWFramesContext *cuda_fc = (AVHWFramesContext *)internal->cuda_fc_ref->data;
1039         int planes = av_pix_fmt_count_planes(cuda_fc->sw_format);
1040         AVHWDeviceContext *cuda_cu = cuda_fc->device_ctx;
1041         AVCUDADeviceContext *cuda_dev = cuda_cu->hwctx;
1042         AVCUDADeviceContextInternal *cu_internal = cuda_dev->internal;
1043         CudaFunctions *cu = cu_internal->cuda_dl;
1044
1045         for (int i = 0; i < planes; i++) {
1046             if (internal->cu_sem[i])
1047                 CHECK_CU(cu->cuDestroyExternalSemaphore(internal->cu_sem[i]));
1048             if (internal->cu_mma[i])
1049                 CHECK_CU(cu->cuMipmappedArrayDestroy(internal->cu_mma[i]));
1050             if (internal->ext_mem[i])
1051                 CHECK_CU(cu->cuDestroyExternalMemory(internal->ext_mem[i]));
1052         }
1053
1054         av_buffer_unref(&internal->cuda_fc_ref);
1055     }
1056 #endif
1057
1058     av_free(internal);
1059 }
1060
1061 static void vulkan_frame_free(void *opaque, uint8_t *data)
1062 {
1063     AVVkFrame *f = (AVVkFrame *)data;
1064     AVHWFramesContext *hwfc = opaque;
1065     AVVulkanDeviceContext *hwctx = hwfc->device_ctx->hwctx;
1066     int planes = av_pix_fmt_count_planes(hwfc->sw_format);
1067
1068     vulkan_free_internal(f->internal);
1069
1070     for (int i = 0; i < planes; i++) {
1071         vkDestroyImage(hwctx->act_dev, f->img[i], hwctx->alloc);
1072         vkFreeMemory(hwctx->act_dev, f->mem[i], hwctx->alloc);
1073         vkDestroySemaphore(hwctx->act_dev, f->sem[i], hwctx->alloc);
1074     }
1075
1076     av_free(f);
1077 }
1078
1079 static int alloc_bind_mem(AVHWFramesContext *hwfc, AVVkFrame *f,
1080                           void *alloc_pnext, size_t alloc_pnext_stride)
1081 {
1082     int err;
1083     VkResult ret;
1084     AVHWDeviceContext *ctx = hwfc->device_ctx;
1085     const int planes = av_pix_fmt_count_planes(hwfc->sw_format);
1086     VkBindImageMemoryInfo bind_info[AV_NUM_DATA_POINTERS] = { { 0 } };
1087
1088     AVVulkanDeviceContext *hwctx = ctx->hwctx;
1089
1090     for (int i = 0; i < planes; i++) {
1091         int use_ded_mem;
1092         VkImageMemoryRequirementsInfo2 req_desc = {
1093             .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
1094             .image = f->img[i],
1095         };
1096         VkMemoryDedicatedAllocateInfo ded_alloc = {
1097             .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
1098             .pNext = (void *)(((uint8_t *)alloc_pnext) + i*alloc_pnext_stride),
1099         };
1100         VkMemoryDedicatedRequirements ded_req = {
1101             .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS,
1102         };
1103         VkMemoryRequirements2 req = {
1104             .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
1105             .pNext = &ded_req,
1106         };
1107
1108         vkGetImageMemoryRequirements2(hwctx->act_dev, &req_desc, &req);
1109
1110         /* In case the implementation prefers/requires dedicated allocation */
1111         use_ded_mem = ded_req.prefersDedicatedAllocation |
1112                       ded_req.requiresDedicatedAllocation;
1113         if (use_ded_mem)
1114             ded_alloc.image = f->img[i];
1115
1116         /* Allocate memory */
1117         if ((err = alloc_mem(ctx, &req.memoryRequirements,
1118                              f->tiling == VK_IMAGE_TILING_LINEAR ?
1119                              VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT :
1120                              VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
1121                              use_ded_mem ? &ded_alloc : (void *)ded_alloc.pNext,
1122                              &f->flags, &f->mem[i])))
1123             return err;
1124
1125         f->size[i] = req.memoryRequirements.size;
1126         bind_info[i].sType  = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO;
1127         bind_info[i].image  = f->img[i];
1128         bind_info[i].memory = f->mem[i];
1129     }
1130
1131     /* Bind the allocated memory to the images */
1132     ret = vkBindImageMemory2(hwctx->act_dev, planes, bind_info);
1133     if (ret != VK_SUCCESS) {
1134         av_log(ctx, AV_LOG_ERROR, "Failed to bind memory: %s\n",
1135                vk_ret2str(ret));
1136         return AVERROR_EXTERNAL;
1137     }
1138
1139     return 0;
1140 }
1141
1142 enum PrepMode {
1143     PREP_MODE_WRITE,
1144     PREP_MODE_RO_SHADER,
1145 };
1146
1147 static int prepare_frame(AVHWFramesContext *hwfc, VulkanExecCtx *ectx,
1148                          AVVkFrame *frame, enum PrepMode pmode)
1149 {
1150     VkResult ret;
1151     VkImageLayout new_layout;
1152     VkAccessFlags new_access;
1153     AVHWDeviceContext *ctx = hwfc->device_ctx;
1154     AVVulkanDeviceContext *hwctx = ctx->hwctx;
1155     const int planes = av_pix_fmt_count_planes(hwfc->sw_format);
1156
1157     VkImageMemoryBarrier img_bar[AV_NUM_DATA_POINTERS] = { 0 };
1158
1159     VkCommandBufferBeginInfo cmd_start = {
1160         .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
1161         .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
1162     };
1163
1164     VkSubmitInfo s_info = {
1165         .sType                = VK_STRUCTURE_TYPE_SUBMIT_INFO,
1166         .commandBufferCount   = 1,
1167         .pCommandBuffers      = &ectx->buf,
1168
1169         .pSignalSemaphores    = frame->sem,
1170         .signalSemaphoreCount = planes,
1171     };
1172
1173     switch (pmode) {
1174     case PREP_MODE_WRITE:
1175         new_layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1176         new_access = VK_ACCESS_TRANSFER_WRITE_BIT;
1177         break;
1178     case PREP_MODE_RO_SHADER:
1179         new_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
1180         new_access = VK_ACCESS_TRANSFER_READ_BIT;
1181         break;
1182     }
1183
1184     ret = vkBeginCommandBuffer(ectx->buf, &cmd_start);
1185     if (ret != VK_SUCCESS)
1186         return AVERROR_EXTERNAL;
1187
1188     /* Change the image layout to something more optimal for writes.
1189      * This also signals the newly created semaphore, making it usable
1190      * for synchronization */
1191     for (int i = 0; i < planes; i++) {
1192         img_bar[i].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1193         img_bar[i].srcAccessMask = 0x0;
1194         img_bar[i].dstAccessMask = new_access;
1195         img_bar[i].oldLayout = frame->layout[i];
1196         img_bar[i].newLayout = new_layout;
1197         img_bar[i].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1198         img_bar[i].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1199         img_bar[i].image = frame->img[i];
1200         img_bar[i].subresourceRange.levelCount = 1;
1201         img_bar[i].subresourceRange.layerCount = 1;
1202         img_bar[i].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1203
1204         frame->layout[i] = img_bar[i].newLayout;
1205         frame->access[i] = img_bar[i].dstAccessMask;
1206     }
1207
1208     vkCmdPipelineBarrier(ectx->buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1209                          VK_PIPELINE_STAGE_TRANSFER_BIT, 0,
1210                          0, NULL, 0, NULL, planes, img_bar);
1211
1212     ret = vkEndCommandBuffer(ectx->buf);
1213     if (ret != VK_SUCCESS)
1214         return AVERROR_EXTERNAL;
1215
1216     ret = vkQueueSubmit(ectx->queue, 1, &s_info, ectx->fence);
1217     if (ret != VK_SUCCESS) {
1218         return AVERROR_EXTERNAL;
1219     } else {
1220         vkWaitForFences(hwctx->act_dev, 1, &ectx->fence, VK_TRUE, UINT64_MAX);
1221         vkResetFences(hwctx->act_dev, 1, &ectx->fence);
1222     }
1223
1224     return 0;
1225 }
1226
1227 static int create_frame(AVHWFramesContext *hwfc, AVVkFrame **frame,
1228                         VkImageTiling tiling, VkImageUsageFlagBits usage,
1229                         void *create_pnext)
1230 {
1231     int err;
1232     VkResult ret;
1233     AVHWDeviceContext *ctx = hwfc->device_ctx;
1234     VulkanDevicePriv *p = ctx->internal->priv;
1235     AVVulkanDeviceContext *hwctx = ctx->hwctx;
1236     enum AVPixelFormat format = hwfc->sw_format;
1237     const VkFormat *img_fmts = av_vkfmt_from_pixfmt(format);
1238     const int planes = av_pix_fmt_count_planes(format);
1239
1240     VkExportSemaphoreCreateInfo ext_sem_info = {
1241         .sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO,
1242         .handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT,
1243     };
1244
1245     VkSemaphoreCreateInfo sem_spawn = {
1246         .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
1247         .pNext = p->extensions & EXT_EXTERNAL_FD_SEM ? &ext_sem_info : NULL,
1248     };
1249
1250     AVVkFrame *f = av_vk_frame_alloc();
1251     if (!f) {
1252         av_log(ctx, AV_LOG_ERROR, "Unable to allocate memory for AVVkFrame!\n");
1253         return AVERROR(ENOMEM);
1254     }
1255
1256     /* Create the images */
1257     for (int i = 0; i < planes; i++) {
1258         const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format);
1259         int w = hwfc->width;
1260         int h = hwfc->height;
1261         const int p_w = i > 0 ? AV_CEIL_RSHIFT(w, desc->log2_chroma_w) : w;
1262         const int p_h = i > 0 ? AV_CEIL_RSHIFT(h, desc->log2_chroma_h) : h;
1263
1264         VkImageCreateInfo image_create_info = {
1265             .sType         = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1266             .pNext         = create_pnext,
1267             .imageType     = VK_IMAGE_TYPE_2D,
1268             .format        = img_fmts[i],
1269             .extent.width  = p_w,
1270             .extent.height = p_h,
1271             .extent.depth  = 1,
1272             .mipLevels     = 1,
1273             .arrayLayers   = 1,
1274             .flags         = VK_IMAGE_CREATE_ALIAS_BIT,
1275             .tiling        = tiling,
1276             .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1277             .usage         = usage,
1278             .sharingMode   = VK_SHARING_MODE_EXCLUSIVE,
1279             .samples       = VK_SAMPLE_COUNT_1_BIT,
1280         };
1281
1282         ret = vkCreateImage(hwctx->act_dev, &image_create_info,
1283                             hwctx->alloc, &f->img[i]);
1284         if (ret != VK_SUCCESS) {
1285             av_log(ctx, AV_LOG_ERROR, "Image creation failure: %s\n",
1286                    vk_ret2str(ret));
1287             err = AVERROR(EINVAL);
1288             goto fail;
1289         }
1290
1291         /* Create semaphore */
1292         ret = vkCreateSemaphore(hwctx->act_dev, &sem_spawn,
1293                                 hwctx->alloc, &f->sem[i]);
1294         if (ret != VK_SUCCESS) {
1295             av_log(hwctx, AV_LOG_ERROR, "Failed to create semaphore: %s\n",
1296                    vk_ret2str(ret));
1297             return AVERROR_EXTERNAL;
1298         }
1299
1300         f->layout[i] = image_create_info.initialLayout;
1301         f->access[i] = 0x0;
1302     }
1303
1304     f->flags     = 0x0;
1305     f->tiling    = tiling;
1306
1307     *frame = f;
1308     return 0;
1309
1310 fail:
1311     vulkan_frame_free(hwfc, (uint8_t *)f);
1312     return err;
1313 }
1314
1315 /* Checks if an export flag is enabled, and if it is ORs it with *iexp */
1316 static void try_export_flags(AVHWFramesContext *hwfc,
1317                              VkExternalMemoryHandleTypeFlags *comp_handle_types,
1318                              VkExternalMemoryHandleTypeFlagBits *iexp,
1319                              VkExternalMemoryHandleTypeFlagBits exp)
1320 {
1321     VkResult ret;
1322     AVVulkanFramesContext *hwctx = hwfc->hwctx;
1323     AVVulkanDeviceContext *dev_hwctx = hwfc->device_ctx->hwctx;
1324     VkExternalImageFormatProperties eprops = {
1325         .sType = VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES_KHR,
1326     };
1327     VkImageFormatProperties2 props = {
1328         .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
1329         .pNext = &eprops,
1330     };
1331     VkPhysicalDeviceExternalImageFormatInfo enext = {
1332         .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO,
1333         .handleType = exp,
1334     };
1335     VkPhysicalDeviceImageFormatInfo2 pinfo = {
1336         .sType  = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
1337         .pNext  = !exp ? NULL : &enext,
1338         .format = av_vkfmt_from_pixfmt(hwfc->sw_format)[0],
1339         .type   = VK_IMAGE_TYPE_2D,
1340         .tiling = hwctx->tiling,
1341         .usage  = hwctx->usage,
1342         .flags  = VK_IMAGE_CREATE_ALIAS_BIT,
1343     };
1344
1345     ret = vkGetPhysicalDeviceImageFormatProperties2(dev_hwctx->phys_dev,
1346                                                     &pinfo, &props);
1347     if (ret == VK_SUCCESS) {
1348         *iexp |= exp;
1349         *comp_handle_types |= eprops.externalMemoryProperties.compatibleHandleTypes;
1350     }
1351 }
1352
1353 static AVBufferRef *vulkan_pool_alloc(void *opaque, int size)
1354 {
1355     int err;
1356     AVVkFrame *f;
1357     AVBufferRef *avbuf = NULL;
1358     AVHWFramesContext *hwfc = opaque;
1359     AVVulkanFramesContext *hwctx = hwfc->hwctx;
1360     VulkanDevicePriv *p = hwfc->device_ctx->internal->priv;
1361     VkExportMemoryAllocateInfo eminfo[AV_NUM_DATA_POINTERS];
1362     VkExternalMemoryHandleTypeFlags e = 0x0;
1363
1364     VkExternalMemoryImageCreateInfo eiinfo = {
1365         .sType       = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
1366         .pNext       = hwctx->create_pnext,
1367     };
1368
1369     if (p->extensions & EXT_EXTERNAL_FD_MEMORY)
1370         try_export_flags(hwfc, &eiinfo.handleTypes, &e,
1371                          VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT);
1372
1373     if (p->extensions & EXT_EXTERNAL_DMABUF_MEMORY)
1374         try_export_flags(hwfc, &eiinfo.handleTypes, &e,
1375                          VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
1376
1377     for (int i = 0; i < av_pix_fmt_count_planes(hwfc->sw_format); i++) {
1378         eminfo[i].sType       = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO;
1379         eminfo[i].pNext       = hwctx->alloc_pnext[i];
1380         eminfo[i].handleTypes = e;
1381     }
1382
1383     err = create_frame(hwfc, &f, hwctx->tiling, hwctx->usage,
1384                        eiinfo.handleTypes ? &eiinfo : NULL);
1385     if (err)
1386         return NULL;
1387
1388     err = alloc_bind_mem(hwfc, f, eminfo, sizeof(*eminfo));
1389     if (err)
1390         goto fail;
1391
1392     err = prepare_frame(hwfc, &p->cmd, f, PREP_MODE_WRITE);
1393     if (err)
1394         goto fail;
1395
1396     avbuf = av_buffer_create((uint8_t *)f, sizeof(AVVkFrame),
1397                              vulkan_frame_free, hwfc, 0);
1398     if (!avbuf)
1399         goto fail;
1400
1401     return avbuf;
1402
1403 fail:
1404     vulkan_frame_free(hwfc, (uint8_t *)f);
1405     return NULL;
1406 }
1407
1408 static void vulkan_frames_uninit(AVHWFramesContext *hwfc)
1409 {
1410     VulkanFramesPriv *fp = hwfc->internal->priv;
1411
1412     free_exec_ctx(hwfc->device_ctx, &fp->cmd);
1413 }
1414
1415 static int vulkan_frames_init(AVHWFramesContext *hwfc)
1416 {
1417     int err;
1418     AVVkFrame *f;
1419     AVVulkanFramesContext *hwctx = hwfc->hwctx;
1420     VulkanFramesPriv *fp = hwfc->internal->priv;
1421     AVVulkanDeviceContext *dev_hwctx = hwfc->device_ctx->hwctx;
1422     VulkanDevicePriv *p = hwfc->device_ctx->internal->priv;
1423
1424     if (hwfc->pool)
1425         return 0;
1426
1427     /* Default pool flags */
1428     hwctx->tiling = hwctx->tiling ? hwctx->tiling : p->use_linear_images ?
1429                     VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL;
1430
1431     hwctx->usage |= DEFAULT_USAGE_FLAGS;
1432
1433     err = create_exec_ctx(hwfc->device_ctx, &fp->cmd,
1434                           dev_hwctx->queue_family_tx_index);
1435     if (err)
1436         return err;
1437
1438     /* Test to see if allocation will fail */
1439     err = create_frame(hwfc, &f, hwctx->tiling, hwctx->usage,
1440                        hwctx->create_pnext);
1441     if (err) {
1442         free_exec_ctx(hwfc->device_ctx, &p->cmd);
1443         return err;
1444     }
1445
1446     vulkan_frame_free(hwfc, (uint8_t *)f);
1447
1448     hwfc->internal->pool_internal = av_buffer_pool_init2(sizeof(AVVkFrame),
1449                                                          hwfc, vulkan_pool_alloc,
1450                                                          NULL);
1451     if (!hwfc->internal->pool_internal) {
1452         free_exec_ctx(hwfc->device_ctx, &p->cmd);
1453         return AVERROR(ENOMEM);
1454     }
1455
1456     return 0;
1457 }
1458
1459 static int vulkan_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame)
1460 {
1461     frame->buf[0] = av_buffer_pool_get(hwfc->pool);
1462     if (!frame->buf[0])
1463         return AVERROR(ENOMEM);
1464
1465     frame->data[0] = frame->buf[0]->data;
1466     frame->format  = AV_PIX_FMT_VULKAN;
1467     frame->width   = hwfc->width;
1468     frame->height  = hwfc->height;
1469
1470     return 0;
1471 }
1472
1473 static int vulkan_transfer_get_formats(AVHWFramesContext *hwfc,
1474                                        enum AVHWFrameTransferDirection dir,
1475                                        enum AVPixelFormat **formats)
1476 {
1477     enum AVPixelFormat *fmts = av_malloc_array(2, sizeof(*fmts));
1478     if (!fmts)
1479         return AVERROR(ENOMEM);
1480
1481     fmts[0] = hwfc->sw_format;
1482     fmts[1] = AV_PIX_FMT_NONE;
1483
1484     *formats = fmts;
1485     return 0;
1486 }
1487
1488 typedef struct VulkanMapping {
1489     AVVkFrame *frame;
1490     int flags;
1491 } VulkanMapping;
1492
1493 static void vulkan_unmap_frame(AVHWFramesContext *hwfc, HWMapDescriptor *hwmap)
1494 {
1495     VulkanMapping *map = hwmap->priv;
1496     AVVulkanDeviceContext *hwctx = hwfc->device_ctx->hwctx;
1497     const int planes = av_pix_fmt_count_planes(hwfc->sw_format);
1498
1499     /* Check if buffer needs flushing */
1500     if ((map->flags & AV_HWFRAME_MAP_WRITE) &&
1501         !(map->frame->flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) {
1502         VkResult ret;
1503         VkMappedMemoryRange flush_ranges[AV_NUM_DATA_POINTERS] = { { 0 } };
1504
1505         for (int i = 0; i < planes; i++) {
1506             flush_ranges[i].sType  = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
1507             flush_ranges[i].memory = map->frame->mem[i];
1508             flush_ranges[i].size   = VK_WHOLE_SIZE;
1509         }
1510
1511         ret = vkFlushMappedMemoryRanges(hwctx->act_dev, planes,
1512                                         flush_ranges);
1513         if (ret != VK_SUCCESS) {
1514             av_log(hwfc, AV_LOG_ERROR, "Failed to flush memory: %s\n",
1515                    vk_ret2str(ret));
1516         }
1517     }
1518
1519     for (int i = 0; i < planes; i++)
1520         vkUnmapMemory(hwctx->act_dev, map->frame->mem[i]);
1521
1522     av_free(map);
1523 }
1524
1525 static int vulkan_map_frame_to_mem(AVHWFramesContext *hwfc, AVFrame *dst,
1526                                    const AVFrame *src, int flags)
1527 {
1528     VkResult ret;
1529     int err, mapped_mem_count = 0;
1530     AVVkFrame *f = (AVVkFrame *)src->data[0];
1531     AVVulkanDeviceContext *hwctx = hwfc->device_ctx->hwctx;
1532     const int planes = av_pix_fmt_count_planes(hwfc->sw_format);
1533
1534     VulkanMapping *map = av_mallocz(sizeof(VulkanMapping));
1535     if (!map)
1536         return AVERROR(EINVAL);
1537
1538     if (src->format != AV_PIX_FMT_VULKAN) {
1539         av_log(hwfc, AV_LOG_ERROR, "Cannot map from pixel format %s!\n",
1540                av_get_pix_fmt_name(src->format));
1541         err = AVERROR(EINVAL);
1542         goto fail;
1543     }
1544
1545     if (!(f->flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) ||
1546         !(f->tiling == VK_IMAGE_TILING_LINEAR)) {
1547         av_log(hwfc, AV_LOG_ERROR, "Unable to map frame, not host visible "
1548                "and linear!\n");
1549         err = AVERROR(EINVAL);
1550         goto fail;
1551     }
1552
1553     dst->width  = src->width;
1554     dst->height = src->height;
1555
1556     for (int i = 0; i < planes; i++) {
1557         ret = vkMapMemory(hwctx->act_dev, f->mem[i], 0,
1558                           VK_WHOLE_SIZE, 0, (void **)&dst->data[i]);
1559         if (ret != VK_SUCCESS) {
1560             av_log(hwfc, AV_LOG_ERROR, "Failed to map image memory: %s\n",
1561                 vk_ret2str(ret));
1562             err = AVERROR_EXTERNAL;
1563             goto fail;
1564         }
1565         mapped_mem_count++;
1566     }
1567
1568     /* Check if the memory contents matter */
1569     if (((flags & AV_HWFRAME_MAP_READ) || !(flags & AV_HWFRAME_MAP_OVERWRITE)) &&
1570         !(f->flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) {
1571         VkMappedMemoryRange map_mem_ranges[AV_NUM_DATA_POINTERS] = { { 0 } };
1572         for (int i = 0; i < planes; i++) {
1573             map_mem_ranges[i].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
1574             map_mem_ranges[i].size = VK_WHOLE_SIZE;
1575             map_mem_ranges[i].memory = f->mem[i];
1576         }
1577
1578         ret = vkInvalidateMappedMemoryRanges(hwctx->act_dev, planes,
1579                                              map_mem_ranges);
1580         if (ret != VK_SUCCESS) {
1581             av_log(hwfc, AV_LOG_ERROR, "Failed to invalidate memory: %s\n",
1582                    vk_ret2str(ret));
1583             err = AVERROR_EXTERNAL;
1584             goto fail;
1585         }
1586     }
1587
1588     for (int i = 0; i < planes; i++) {
1589         VkImageSubresource sub = {
1590             .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1591         };
1592         VkSubresourceLayout layout;
1593         vkGetImageSubresourceLayout(hwctx->act_dev, f->img[i], &sub, &layout);
1594         dst->linesize[i] = layout.rowPitch;
1595     }
1596
1597     map->frame = f;
1598     map->flags = flags;
1599
1600     err = ff_hwframe_map_create(src->hw_frames_ctx, dst, src,
1601                                 &vulkan_unmap_frame, map);
1602     if (err < 0)
1603         goto fail;
1604
1605     return 0;
1606
1607 fail:
1608     for (int i = 0; i < mapped_mem_count; i++)
1609         vkUnmapMemory(hwctx->act_dev, f->mem[i]);
1610
1611     av_free(map);
1612     return err;
1613 }
1614
1615 #if CONFIG_LIBDRM
1616 static void vulkan_unmap_from(AVHWFramesContext *hwfc, HWMapDescriptor *hwmap)
1617 {
1618     VulkanMapping *map = hwmap->priv;
1619     AVVulkanDeviceContext *hwctx = hwfc->device_ctx->hwctx;
1620     const int planes = av_pix_fmt_count_planes(hwfc->sw_format);
1621
1622     for (int i = 0; i < planes; i++) {
1623         vkDestroyImage(hwctx->act_dev, map->frame->img[i], hwctx->alloc);
1624         vkFreeMemory(hwctx->act_dev, map->frame->mem[i], hwctx->alloc);
1625         vkDestroySemaphore(hwctx->act_dev, map->frame->sem[i], hwctx->alloc);
1626     }
1627
1628     av_freep(&map->frame);
1629 }
1630
1631 static const struct {
1632     uint32_t drm_fourcc;
1633     VkFormat vk_format;
1634 } vulkan_drm_format_map[] = {
1635     { DRM_FORMAT_R8,       VK_FORMAT_R8_UNORM       },
1636     { DRM_FORMAT_R16,      VK_FORMAT_R16_UNORM      },
1637     { DRM_FORMAT_GR88,     VK_FORMAT_R8G8_UNORM     },
1638     { DRM_FORMAT_RG88,     VK_FORMAT_R8G8_UNORM     },
1639     { DRM_FORMAT_GR1616,   VK_FORMAT_R16G16_UNORM   },
1640     { DRM_FORMAT_RG1616,   VK_FORMAT_R16G16_UNORM   },
1641     { DRM_FORMAT_ARGB8888, VK_FORMAT_B8G8R8A8_UNORM },
1642     { DRM_FORMAT_XRGB8888, VK_FORMAT_B8G8R8A8_UNORM },
1643     { DRM_FORMAT_ABGR8888, VK_FORMAT_R8G8B8A8_UNORM },
1644     { DRM_FORMAT_XBGR8888, VK_FORMAT_R8G8B8A8_UNORM },
1645 };
1646
1647 static inline VkFormat drm_to_vulkan_fmt(uint32_t drm_fourcc)
1648 {
1649     for (int i = 0; i < FF_ARRAY_ELEMS(vulkan_drm_format_map); i++)
1650         if (vulkan_drm_format_map[i].drm_fourcc == drm_fourcc)
1651             return vulkan_drm_format_map[i].vk_format;
1652     return VK_FORMAT_UNDEFINED;
1653 }
1654
1655 static int vulkan_map_from_drm_frame_desc(AVHWFramesContext *hwfc, AVVkFrame **frame,
1656                                           AVDRMFrameDescriptor *desc)
1657 {
1658     int err = 0;
1659     VkResult ret;
1660     AVVkFrame *f;
1661     int bind_counts = 0;
1662     AVHWDeviceContext *ctx = hwfc->device_ctx;
1663     AVVulkanDeviceContext *hwctx = ctx->hwctx;
1664     VulkanDevicePriv *p = ctx->internal->priv;
1665     const AVPixFmtDescriptor *fmt_desc = av_pix_fmt_desc_get(hwfc->sw_format);
1666     const int has_modifiers = p->extensions & EXT_DRM_MODIFIER_FLAGS;
1667     VkSubresourceLayout plane_data[AV_NUM_DATA_POINTERS] = { 0 };
1668     VkBindImageMemoryInfo bind_info[AV_NUM_DATA_POINTERS] = { 0 };
1669     VkBindImagePlaneMemoryInfo plane_info[AV_NUM_DATA_POINTERS] = { 0 };
1670     VkExternalMemoryHandleTypeFlagBits htype = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
1671
1672     VK_LOAD_PFN(hwctx->inst, vkGetMemoryFdPropertiesKHR);
1673
1674     for (int i = 0; i < desc->nb_layers; i++) {
1675         if (drm_to_vulkan_fmt(desc->layers[i].format) == VK_FORMAT_UNDEFINED) {
1676             av_log(ctx, AV_LOG_ERROR, "Unsupported DMABUF layer format %#08x!\n",
1677                    desc->layers[i].format);
1678             return AVERROR(EINVAL);
1679         }
1680     }
1681
1682     if (!(f = av_vk_frame_alloc())) {
1683         av_log(ctx, AV_LOG_ERROR, "Unable to allocate memory for AVVkFrame!\n");
1684         err = AVERROR(ENOMEM);
1685         goto fail;
1686     }
1687
1688     for (int i = 0; i < desc->nb_objects; i++) {
1689         VkMemoryFdPropertiesKHR fdmp = {
1690             .sType = VK_STRUCTURE_TYPE_MEMORY_FD_PROPERTIES_KHR,
1691         };
1692         VkMemoryRequirements req = {
1693             .size = desc->objects[i].size,
1694         };
1695         VkImportMemoryFdInfoKHR idesc = {
1696             .sType      = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR,
1697             .handleType = htype,
1698             .fd         = dup(desc->objects[i].fd),
1699         };
1700
1701         ret = pfn_vkGetMemoryFdPropertiesKHR(hwctx->act_dev, htype,
1702                                              idesc.fd, &fdmp);
1703         if (ret != VK_SUCCESS) {
1704             av_log(hwfc, AV_LOG_ERROR, "Failed to get FD properties: %s\n",
1705                    vk_ret2str(ret));
1706             err = AVERROR_EXTERNAL;
1707             close(idesc.fd);
1708             goto fail;
1709         }
1710
1711         req.memoryTypeBits = fdmp.memoryTypeBits;
1712
1713         err = alloc_mem(ctx, &req, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
1714                         &idesc, &f->flags, &f->mem[i]);
1715         if (err) {
1716             close(idesc.fd);
1717             return err;
1718         }
1719
1720         f->size[i] = desc->objects[i].size;
1721     }
1722
1723     f->tiling = has_modifiers ? VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT :
1724                 desc->objects[0].format_modifier == DRM_FORMAT_MOD_LINEAR ?
1725                 VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL;
1726
1727     for (int i = 0; i < desc->nb_layers; i++) {
1728         const int planes = desc->layers[i].nb_planes;
1729         const int signal_p = has_modifiers && (planes > 1);
1730
1731         VkImageDrmFormatModifierExplicitCreateInfoEXT drm_info = {
1732             .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT,
1733             .drmFormatModifier = desc->objects[0].format_modifier,
1734             .drmFormatModifierPlaneCount = planes,
1735             .pPlaneLayouts = (const VkSubresourceLayout *)&plane_data,
1736         };
1737
1738         VkExternalMemoryImageCreateInfo einfo = {
1739             .sType       = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
1740             .pNext       = has_modifiers ? &drm_info : NULL,
1741             .handleTypes = htype,
1742         };
1743
1744         VkSemaphoreCreateInfo sem_spawn = {
1745             .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
1746         };
1747
1748         const int p_w = i > 0 ? AV_CEIL_RSHIFT(hwfc->width, fmt_desc->log2_chroma_w) : hwfc->width;
1749         const int p_h = i > 0 ? AV_CEIL_RSHIFT(hwfc->height, fmt_desc->log2_chroma_h) : hwfc->height;
1750
1751         VkImageCreateInfo image_create_info = {
1752             .sType         = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1753             .pNext         = &einfo,
1754             .imageType     = VK_IMAGE_TYPE_2D,
1755             .format        = drm_to_vulkan_fmt(desc->layers[i].format),
1756             .extent.width  = p_w,
1757             .extent.height = p_h,
1758             .extent.depth  = 1,
1759             .mipLevels     = 1,
1760             .arrayLayers   = 1,
1761             .flags         = VK_IMAGE_CREATE_ALIAS_BIT | signal_p ? VK_IMAGE_CREATE_DISJOINT_BIT : 0x0,
1762             .tiling        = f->tiling,
1763             .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, /* specs say so */
1764             .usage         = DEFAULT_USAGE_FLAGS,
1765             .sharingMode   = VK_SHARING_MODE_EXCLUSIVE,
1766             .samples       = VK_SAMPLE_COUNT_1_BIT,
1767         };
1768
1769         for (int j = 0; j < planes; j++) {
1770             plane_data[j].offset     = desc->layers[i].planes[j].offset;
1771             plane_data[j].rowPitch   = desc->layers[i].planes[j].pitch;
1772             plane_data[j].size       = 0; /* The specs say so for all 3 */
1773             plane_data[j].arrayPitch = 0;
1774             plane_data[j].depthPitch = 0;
1775         }
1776
1777         /* Create image */
1778         ret = vkCreateImage(hwctx->act_dev, &image_create_info,
1779                             hwctx->alloc, &f->img[i]);
1780         if (ret != VK_SUCCESS) {
1781             av_log(ctx, AV_LOG_ERROR, "Image creation failure: %s\n",
1782                    vk_ret2str(ret));
1783             err = AVERROR(EINVAL);
1784             goto fail;
1785         }
1786
1787         ret = vkCreateSemaphore(hwctx->act_dev, &sem_spawn,
1788                                 hwctx->alloc, &f->sem[i]);
1789         if (ret != VK_SUCCESS) {
1790             av_log(hwctx, AV_LOG_ERROR, "Failed to create semaphore: %s\n",
1791                    vk_ret2str(ret));
1792             return AVERROR_EXTERNAL;
1793         }
1794
1795         /* We'd import a semaphore onto the one we created using
1796          * vkImportSemaphoreFdKHR but unfortunately neither DRM nor VAAPI
1797          * offer us anything we could import and sync with, so instead
1798          * just signal the semaphore we created. */
1799
1800         f->layout[i] = image_create_info.initialLayout;
1801         f->access[i] = 0x0;
1802
1803         for (int j = 0; j < planes; j++) {
1804             VkImageAspectFlagBits aspect = j == 0 ? VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT :
1805                                            j == 1 ? VK_IMAGE_ASPECT_MEMORY_PLANE_1_BIT_EXT :
1806                                                     VK_IMAGE_ASPECT_MEMORY_PLANE_2_BIT_EXT;
1807
1808             plane_info[bind_counts].sType = VK_STRUCTURE_TYPE_BIND_IMAGE_PLANE_MEMORY_INFO;
1809             plane_info[bind_counts].planeAspect = aspect;
1810
1811             bind_info[bind_counts].sType  = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO;
1812             bind_info[bind_counts].pNext  = signal_p ? &plane_info[bind_counts] : NULL;
1813             bind_info[bind_counts].image  = f->img[i];
1814             bind_info[bind_counts].memory = f->mem[desc->layers[i].planes[j].object_index];
1815             bind_info[bind_counts].memoryOffset = desc->layers[i].planes[j].offset;
1816             bind_counts++;
1817         }
1818     }
1819
1820     /* Bind the allocated memory to the images */
1821     ret = vkBindImageMemory2(hwctx->act_dev, bind_counts, bind_info);
1822     if (ret != VK_SUCCESS) {
1823         av_log(ctx, AV_LOG_ERROR, "Failed to bind memory: %s\n",
1824                vk_ret2str(ret));
1825         return AVERROR_EXTERNAL;
1826     }
1827
1828     /* NOTE: This is completely uneccesary and unneeded once we can import
1829      * semaphores from DRM. Otherwise we have to activate the semaphores.
1830      * We're reusing the exec context that's also used for uploads/downloads. */
1831     err = prepare_frame(hwfc, &p->cmd, f, PREP_MODE_RO_SHADER);
1832     if (err)
1833         goto fail;
1834
1835     *frame = f;
1836
1837     return 0;
1838
1839 fail:
1840     for (int i = 0; i < desc->nb_layers; i++) {
1841         vkDestroyImage(hwctx->act_dev, f->img[i], hwctx->alloc);
1842         vkDestroySemaphore(hwctx->act_dev, f->sem[i], hwctx->alloc);
1843     }
1844     for (int i = 0; i < desc->nb_objects; i++)
1845         vkFreeMemory(hwctx->act_dev, f->mem[i], hwctx->alloc);
1846
1847     av_free(f);
1848
1849     return err;
1850 }
1851
1852 static int vulkan_map_from_drm(AVHWFramesContext *hwfc, AVFrame *dst,
1853                                const AVFrame *src, int flags)
1854 {
1855     int err = 0;
1856     AVVkFrame *f;
1857     VulkanMapping *map = NULL;
1858
1859     err = vulkan_map_from_drm_frame_desc(hwfc, &f,
1860                                          (AVDRMFrameDescriptor *)src->data[0]);
1861     if (err)
1862         return err;
1863
1864     /* The unmapping function will free this */
1865     dst->data[0] = (uint8_t *)f;
1866     dst->width   = src->width;
1867     dst->height  = src->height;
1868
1869     map = av_mallocz(sizeof(VulkanMapping));
1870     if (!map)
1871         goto fail;
1872
1873     map->frame = f;
1874     map->flags = flags;
1875
1876     err = ff_hwframe_map_create(dst->hw_frames_ctx, dst, src,
1877                                 &vulkan_unmap_from, map);
1878     if (err < 0)
1879         goto fail;
1880
1881     av_log(hwfc, AV_LOG_DEBUG, "Mapped DRM object to Vulkan!\n");
1882
1883     return 0;
1884
1885 fail:
1886     vulkan_frame_free(hwfc->device_ctx->hwctx, (uint8_t *)f);
1887     av_free(map);
1888     return err;
1889 }
1890
1891 #if CONFIG_VAAPI
1892 static int vulkan_map_from_vaapi(AVHWFramesContext *dst_fc,
1893                                  AVFrame *dst, const AVFrame *src,
1894                                  int flags)
1895 {
1896     int err;
1897     AVFrame *tmp = av_frame_alloc();
1898     AVHWFramesContext *vaapi_fc = (AVHWFramesContext*)src->hw_frames_ctx->data;
1899     AVVAAPIDeviceContext *vaapi_ctx = vaapi_fc->device_ctx->hwctx;
1900     VASurfaceID surface_id = (VASurfaceID)(uintptr_t)src->data[3];
1901
1902     if (!tmp)
1903         return AVERROR(ENOMEM);
1904
1905     /* We have to sync since like the previous comment said, no semaphores */
1906     vaSyncSurface(vaapi_ctx->display, surface_id);
1907
1908     tmp->format = AV_PIX_FMT_DRM_PRIME;
1909
1910     err = av_hwframe_map(tmp, src, flags);
1911     if (err < 0)
1912         goto fail;
1913
1914     err = vulkan_map_from_drm(dst_fc, dst, tmp, flags);
1915     if (err < 0)
1916         goto fail;
1917
1918     err = ff_hwframe_map_replace(dst, src);
1919
1920 fail:
1921     av_frame_free(&tmp);
1922     return err;
1923 }
1924 #endif
1925 #endif
1926
1927 #if CONFIG_CUDA
1928 static int vulkan_export_to_cuda(AVHWFramesContext *hwfc,
1929                                  AVBufferRef *cuda_hwfc,
1930                                  const AVFrame *frame)
1931 {
1932     int err;
1933     VkResult ret;
1934     AVVkFrame *dst_f;
1935     AVVkFrameInternal *dst_int;
1936     AVHWDeviceContext *ctx = hwfc->device_ctx;
1937     AVVulkanDeviceContext *hwctx = ctx->hwctx;
1938     const int planes = av_pix_fmt_count_planes(hwfc->sw_format);
1939     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(hwfc->sw_format);
1940     VK_LOAD_PFN(hwctx->inst, vkGetMemoryFdKHR);
1941     VK_LOAD_PFN(hwctx->inst, vkGetSemaphoreFdKHR);
1942
1943     AVHWFramesContext *cuda_fc = (AVHWFramesContext*)cuda_hwfc->data;
1944     AVHWDeviceContext *cuda_cu = cuda_fc->device_ctx;
1945     AVCUDADeviceContext *cuda_dev = cuda_cu->hwctx;
1946     AVCUDADeviceContextInternal *cu_internal = cuda_dev->internal;
1947     CudaFunctions *cu = cu_internal->cuda_dl;
1948     CUarray_format cufmt = desc->comp[0].depth > 8 ? CU_AD_FORMAT_UNSIGNED_INT16 :
1949                                                      CU_AD_FORMAT_UNSIGNED_INT8;
1950
1951     dst_f = (AVVkFrame *)frame->data[0];
1952
1953     dst_int = dst_f->internal;
1954     if (!dst_int || !dst_int->cuda_fc_ref) {
1955         if (!dst_f->internal)
1956             dst_f->internal = dst_int = av_mallocz(sizeof(*dst_f->internal));
1957
1958         if (!dst_int) {
1959             err = AVERROR(ENOMEM);
1960             goto fail;
1961         }
1962
1963         dst_int->cuda_fc_ref = av_buffer_ref(cuda_hwfc);
1964         if (!dst_int->cuda_fc_ref) {
1965             err = AVERROR(ENOMEM);
1966             goto fail;
1967         }
1968
1969         for (int i = 0; i < planes; i++) {
1970             CUDA_EXTERNAL_MEMORY_MIPMAPPED_ARRAY_DESC tex_desc = {
1971                 .offset = 0,
1972                 .arrayDesc = {
1973                     .Width  = i > 0 ? AV_CEIL_RSHIFT(hwfc->width, desc->log2_chroma_w)
1974                                     : hwfc->width,
1975                     .Height = i > 0 ? AV_CEIL_RSHIFT(hwfc->height, desc->log2_chroma_h)
1976                                     : hwfc->height,
1977                     .Depth = 0,
1978                     .Format = cufmt,
1979                     .NumChannels = 1 + ((planes == 2) && i),
1980                     .Flags = 0,
1981                 },
1982                 .numLevels = 1,
1983             };
1984             CUDA_EXTERNAL_MEMORY_HANDLE_DESC ext_desc = {
1985                 .type = CU_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD,
1986                 .size = dst_f->size[i],
1987             };
1988             VkMemoryGetFdInfoKHR export_info = {
1989                 .sType      = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
1990                 .memory     = dst_f->mem[i],
1991                 .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR,
1992             };
1993             VkSemaphoreGetFdInfoKHR sem_export = {
1994                 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR,
1995                 .semaphore = dst_f->sem[i],
1996                 .handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT,
1997             };
1998             CUDA_EXTERNAL_SEMAPHORE_HANDLE_DESC ext_sem_desc = {
1999                 .type = CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD,
2000             };
2001
2002             ret = pfn_vkGetMemoryFdKHR(hwctx->act_dev, &export_info,
2003                                        &ext_desc.handle.fd);
2004             if (ret != VK_SUCCESS) {
2005                 av_log(hwfc, AV_LOG_ERROR, "Unable to export the image as a FD!\n");
2006                 err = AVERROR_EXTERNAL;
2007                 goto fail;
2008             }
2009
2010             ret = CHECK_CU(cu->cuImportExternalMemory(&dst_int->ext_mem[i], &ext_desc));
2011             if (ret < 0) {
2012                 err = AVERROR_EXTERNAL;
2013                 goto fail;
2014             }
2015
2016             ret = CHECK_CU(cu->cuExternalMemoryGetMappedMipmappedArray(&dst_int->cu_mma[i],
2017                                                                        dst_int->ext_mem[i],
2018                                                                        &tex_desc));
2019             if (ret < 0) {
2020                 err = AVERROR_EXTERNAL;
2021                 goto fail;
2022             }
2023
2024             ret = CHECK_CU(cu->cuMipmappedArrayGetLevel(&dst_int->cu_array[i],
2025                                                         dst_int->cu_mma[i], 0));
2026             if (ret < 0) {
2027                 err = AVERROR_EXTERNAL;
2028                 goto fail;
2029             }
2030
2031             ret = pfn_vkGetSemaphoreFdKHR(hwctx->act_dev, &sem_export,
2032                                           &ext_sem_desc.handle.fd);
2033             if (ret != VK_SUCCESS) {
2034                 av_log(ctx, AV_LOG_ERROR, "Failed to export semaphore: %s\n",
2035                        vk_ret2str(ret));
2036                 err = AVERROR_EXTERNAL;
2037                 goto fail;
2038             }
2039
2040             ret = CHECK_CU(cu->cuImportExternalSemaphore(&dst_int->cu_sem[i],
2041                                                          &ext_sem_desc));
2042             if (ret < 0) {
2043                 err = AVERROR_EXTERNAL;
2044                 goto fail;
2045             }
2046         }
2047     }
2048
2049     return 0;
2050
2051 fail:
2052     return err;
2053 }
2054
2055 static int vulkan_transfer_data_from_cuda(AVHWFramesContext *hwfc,
2056                                           AVFrame *dst, const AVFrame *src)
2057 {
2058     int err;
2059     VkResult ret;
2060     CUcontext dummy;
2061     AVVkFrame *dst_f;
2062     AVVkFrameInternal *dst_int;
2063     const int planes = av_pix_fmt_count_planes(hwfc->sw_format);
2064     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(hwfc->sw_format);
2065
2066     AVHWFramesContext *cuda_fc = (AVHWFramesContext*)src->hw_frames_ctx->data;
2067     AVHWDeviceContext *cuda_cu = cuda_fc->device_ctx;
2068     AVCUDADeviceContext *cuda_dev = cuda_cu->hwctx;
2069     AVCUDADeviceContextInternal *cu_internal = cuda_dev->internal;
2070     CudaFunctions *cu = cu_internal->cuda_dl;
2071     CUDA_EXTERNAL_SEMAPHORE_WAIT_PARAMS s_w_par[AV_NUM_DATA_POINTERS] = { 0 };
2072     CUDA_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS s_s_par[AV_NUM_DATA_POINTERS] = { 0 };
2073
2074     ret = CHECK_CU(cu->cuCtxPushCurrent(cuda_dev->cuda_ctx));
2075     if (ret < 0) {
2076         err = AVERROR_EXTERNAL;
2077         goto fail;
2078     }
2079
2080     dst_f = (AVVkFrame *)dst->data[0];
2081
2082     ret = vulkan_export_to_cuda(hwfc, src->hw_frames_ctx, dst);
2083     if (ret < 0) {
2084         goto fail;
2085     }
2086     dst_int = dst_f->internal;
2087
2088     ret = CHECK_CU(cu->cuWaitExternalSemaphoresAsync(dst_int->cu_sem, s_w_par,
2089                                                      planes, cuda_dev->stream));
2090     if (ret < 0) {
2091         err = AVERROR_EXTERNAL;
2092         goto fail;
2093     }
2094
2095     for (int i = 0; i < planes; i++) {
2096         CUDA_MEMCPY2D cpy = {
2097             .srcMemoryType = CU_MEMORYTYPE_DEVICE,
2098             .srcDevice     = (CUdeviceptr)src->data[i],
2099             .srcPitch      = src->linesize[i],
2100             .srcY          = 0,
2101
2102             .dstMemoryType = CU_MEMORYTYPE_ARRAY,
2103             .dstArray      = dst_int->cu_array[i],
2104             .WidthInBytes  = (i > 0 ? AV_CEIL_RSHIFT(hwfc->width, desc->log2_chroma_w)
2105                                     : hwfc->width) * desc->comp[i].step,
2106             .Height        = i > 0 ? AV_CEIL_RSHIFT(hwfc->height, desc->log2_chroma_h)
2107                                    : hwfc->height,
2108         };
2109
2110         ret = CHECK_CU(cu->cuMemcpy2DAsync(&cpy, cuda_dev->stream));
2111         if (ret < 0) {
2112             err = AVERROR_EXTERNAL;
2113             goto fail;
2114         }
2115     }
2116
2117     ret = CHECK_CU(cu->cuSignalExternalSemaphoresAsync(dst_int->cu_sem, s_s_par,
2118                                                        planes, cuda_dev->stream));
2119     if (ret < 0) {
2120         err = AVERROR_EXTERNAL;
2121         goto fail;
2122     }
2123
2124     CHECK_CU(cu->cuCtxPopCurrent(&dummy));
2125
2126     av_log(hwfc, AV_LOG_VERBOSE, "Transfered CUDA image to Vulkan!\n");
2127
2128     return 0;
2129
2130 fail:
2131     CHECK_CU(cu->cuCtxPopCurrent(&dummy));
2132     vulkan_free_internal(dst_int);
2133     dst_f->internal = NULL;
2134     av_buffer_unref(&dst->buf[0]);
2135     return err;
2136 }
2137 #endif
2138
2139 static int vulkan_map_to(AVHWFramesContext *hwfc, AVFrame *dst,
2140                          const AVFrame *src, int flags)
2141 {
2142     av_unused VulkanDevicePriv *p = hwfc->device_ctx->internal->priv;
2143
2144     switch (src->format) {
2145 #if CONFIG_LIBDRM
2146 #if CONFIG_VAAPI
2147     case AV_PIX_FMT_VAAPI:
2148         if (p->extensions & EXT_EXTERNAL_DMABUF_MEMORY)
2149             return vulkan_map_from_vaapi(hwfc, dst, src, flags);
2150 #endif
2151     case AV_PIX_FMT_DRM_PRIME:
2152         if (p->extensions & EXT_EXTERNAL_DMABUF_MEMORY)
2153             return vulkan_map_from_drm(hwfc, dst, src, flags);
2154 #endif
2155     default:
2156         return AVERROR(ENOSYS);
2157     }
2158 }
2159
2160 #if CONFIG_LIBDRM
2161 typedef struct VulkanDRMMapping {
2162     AVDRMFrameDescriptor drm_desc;
2163     AVVkFrame *source;
2164 } VulkanDRMMapping;
2165
2166 static void vulkan_unmap_to_drm(AVHWFramesContext *hwfc, HWMapDescriptor *hwmap)
2167 {
2168     AVDRMFrameDescriptor *drm_desc = hwmap->priv;
2169
2170     for (int i = 0; i < drm_desc->nb_objects; i++)
2171         close(drm_desc->objects[i].fd);
2172
2173     av_free(drm_desc);
2174 }
2175
2176 static inline uint32_t vulkan_fmt_to_drm(VkFormat vkfmt)
2177 {
2178     for (int i = 0; i < FF_ARRAY_ELEMS(vulkan_drm_format_map); i++)
2179         if (vulkan_drm_format_map[i].vk_format == vkfmt)
2180             return vulkan_drm_format_map[i].drm_fourcc;
2181     return DRM_FORMAT_INVALID;
2182 }
2183
2184 static int vulkan_map_to_drm(AVHWFramesContext *hwfc, AVFrame *dst,
2185                              const AVFrame *src, int flags)
2186 {
2187     int err = 0;
2188     VkResult ret;
2189     AVVkFrame *f = (AVVkFrame *)src->data[0];
2190     VulkanDevicePriv *p = hwfc->device_ctx->internal->priv;
2191     AVVulkanDeviceContext *hwctx = hwfc->device_ctx->hwctx;
2192     const int planes = av_pix_fmt_count_planes(hwfc->sw_format);
2193     VK_LOAD_PFN(hwctx->inst, vkGetMemoryFdKHR);
2194     VkImageDrmFormatModifierPropertiesEXT drm_mod = {
2195         .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_PROPERTIES_EXT,
2196     };
2197
2198     AVDRMFrameDescriptor *drm_desc = av_mallocz(sizeof(*drm_desc));
2199     if (!drm_desc)
2200         return AVERROR(ENOMEM);
2201
2202     err = ff_hwframe_map_create(src->hw_frames_ctx, dst, src, &vulkan_unmap_to_drm, drm_desc);
2203     if (err < 0)
2204         goto end;
2205
2206     if (p->extensions & EXT_DRM_MODIFIER_FLAGS) {
2207         VK_LOAD_PFN(hwctx->inst, vkGetImageDrmFormatModifierPropertiesEXT);
2208         ret = pfn_vkGetImageDrmFormatModifierPropertiesEXT(hwctx->act_dev, f->img[0],
2209                                                            &drm_mod);
2210         if (ret != VK_SUCCESS) {
2211             av_log(hwfc, AV_LOG_ERROR, "Failed to retrieve DRM format modifier!\n");
2212             err = AVERROR_EXTERNAL;
2213             goto end;
2214         }
2215     }
2216
2217     for (int i = 0; (i < planes) && (f->mem[i]); i++) {
2218         VkMemoryGetFdInfoKHR export_info = {
2219             .sType      = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
2220             .memory     = f->mem[i],
2221             .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
2222         };
2223
2224         ret = pfn_vkGetMemoryFdKHR(hwctx->act_dev, &export_info,
2225                                    &drm_desc->objects[i].fd);
2226         if (ret != VK_SUCCESS) {
2227             av_log(hwfc, AV_LOG_ERROR, "Unable to export the image as a FD!\n");
2228             err = AVERROR_EXTERNAL;
2229             goto end;
2230         }
2231
2232         drm_desc->nb_objects++;
2233         drm_desc->objects[i].size = f->size[i];
2234         drm_desc->objects[i].format_modifier = drm_mod.drmFormatModifier;
2235     }
2236
2237     drm_desc->nb_layers = planes;
2238     for (int i = 0; i < drm_desc->nb_layers; i++) {
2239         VkSubresourceLayout layout;
2240         VkImageSubresource sub = {
2241             .aspectMask = p->extensions & EXT_DRM_MODIFIER_FLAGS ?
2242                           VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT :
2243                           VK_IMAGE_ASPECT_COLOR_BIT,
2244         };
2245         VkFormat plane_vkfmt = av_vkfmt_from_pixfmt(hwfc->sw_format)[i];
2246
2247         drm_desc->layers[i].format    = vulkan_fmt_to_drm(plane_vkfmt);
2248         drm_desc->layers[i].nb_planes = 1;
2249
2250         if (drm_desc->layers[i].format == DRM_FORMAT_INVALID) {
2251             av_log(hwfc, AV_LOG_ERROR, "Cannot map to DRM layer, unsupported!\n");
2252             err = AVERROR_PATCHWELCOME;
2253             goto end;
2254         }
2255
2256         drm_desc->layers[i].planes[0].object_index = FFMIN(i, drm_desc->nb_objects - 1);
2257
2258         if (f->tiling != VK_IMAGE_TILING_OPTIMAL)
2259             continue;
2260
2261         vkGetImageSubresourceLayout(hwctx->act_dev, f->img[i], &sub, &layout);
2262         drm_desc->layers[i].planes[0].offset       = layout.offset;
2263         drm_desc->layers[i].planes[0].pitch        = layout.rowPitch;
2264     }
2265
2266     dst->width   = src->width;
2267     dst->height  = src->height;
2268     dst->data[0] = (uint8_t *)drm_desc;
2269
2270     av_log(hwfc, AV_LOG_VERBOSE, "Mapped AVVkFrame to a DRM object!\n");
2271
2272     return 0;
2273
2274 end:
2275     av_free(drm_desc);
2276     return err;
2277 }
2278
2279 #if CONFIG_VAAPI
2280 static int vulkan_map_to_vaapi(AVHWFramesContext *hwfc, AVFrame *dst,
2281                                const AVFrame *src, int flags)
2282 {
2283     int err;
2284     AVFrame *tmp = av_frame_alloc();
2285     if (!tmp)
2286         return AVERROR(ENOMEM);
2287
2288     tmp->format = AV_PIX_FMT_DRM_PRIME;
2289
2290     err = vulkan_map_to_drm(hwfc, tmp, src, flags);
2291     if (err < 0)
2292         goto fail;
2293
2294     err = av_hwframe_map(dst, tmp, flags);
2295     if (err < 0)
2296         goto fail;
2297
2298     err = ff_hwframe_map_replace(dst, src);
2299
2300 fail:
2301     av_frame_free(&tmp);
2302     return err;
2303 }
2304 #endif
2305 #endif
2306
2307 static int vulkan_map_from(AVHWFramesContext *hwfc, AVFrame *dst,
2308                            const AVFrame *src, int flags)
2309 {
2310     av_unused VulkanDevicePriv *p = hwfc->device_ctx->internal->priv;
2311
2312     switch (dst->format) {
2313 #if CONFIG_LIBDRM
2314     case AV_PIX_FMT_DRM_PRIME:
2315         if (p->extensions & EXT_EXTERNAL_DMABUF_MEMORY)
2316             return vulkan_map_to_drm(hwfc, dst, src, flags);
2317 #if CONFIG_VAAPI
2318     case AV_PIX_FMT_VAAPI:
2319         if (p->extensions & EXT_EXTERNAL_DMABUF_MEMORY)
2320             return vulkan_map_to_vaapi(hwfc, dst, src, flags);
2321 #endif
2322 #endif
2323     default:
2324         return vulkan_map_frame_to_mem(hwfc, dst, src, flags);
2325     }
2326 }
2327
2328 typedef struct ImageBuffer {
2329     VkBuffer buf;
2330     VkDeviceMemory mem;
2331     VkMemoryPropertyFlagBits flags;
2332 } ImageBuffer;
2333
2334 static void free_buf(AVHWDeviceContext *ctx, ImageBuffer *buf)
2335 {
2336     AVVulkanDeviceContext *hwctx = ctx->hwctx;
2337     if (!buf)
2338         return;
2339
2340     vkDestroyBuffer(hwctx->act_dev, buf->buf, hwctx->alloc);
2341     vkFreeMemory(hwctx->act_dev, buf->mem, hwctx->alloc);
2342 }
2343
2344 static int create_buf(AVHWDeviceContext *ctx, ImageBuffer *buf, int height,
2345                       int *stride, VkBufferUsageFlags usage,
2346                       VkMemoryPropertyFlagBits flags, void *create_pnext,
2347                       void *alloc_pnext)
2348 {
2349     int err;
2350     VkResult ret;
2351     VkMemoryRequirements req;
2352     AVVulkanDeviceContext *hwctx = ctx->hwctx;
2353     VulkanDevicePriv *p = ctx->internal->priv;
2354
2355     VkBufferCreateInfo buf_spawn = {
2356         .sType       = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
2357         .pNext       = create_pnext,
2358         .usage       = usage,
2359         .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
2360     };
2361
2362     *stride = FFALIGN(*stride, p->props.limits.optimalBufferCopyRowPitchAlignment);
2363     buf_spawn.size = height*(*stride);
2364
2365     ret = vkCreateBuffer(hwctx->act_dev, &buf_spawn, NULL, &buf->buf);
2366     if (ret != VK_SUCCESS) {
2367         av_log(ctx, AV_LOG_ERROR, "Failed to create buffer: %s\n",
2368                vk_ret2str(ret));
2369         return AVERROR_EXTERNAL;
2370     }
2371
2372     vkGetBufferMemoryRequirements(hwctx->act_dev, buf->buf, &req);
2373
2374     err = alloc_mem(ctx, &req, flags, alloc_pnext, &buf->flags, &buf->mem);
2375     if (err)
2376         return err;
2377
2378     ret = vkBindBufferMemory(hwctx->act_dev, buf->buf, buf->mem, 0);
2379     if (ret != VK_SUCCESS) {
2380         av_log(ctx, AV_LOG_ERROR, "Failed to bind memory to buffer: %s\n",
2381                vk_ret2str(ret));
2382         free_buf(ctx, buf);
2383         return AVERROR_EXTERNAL;
2384     }
2385
2386     return 0;
2387 }
2388
2389 static int map_buffers(AVHWDeviceContext *ctx, ImageBuffer *buf, uint8_t *mem[],
2390                        int nb_buffers, int invalidate)
2391 {
2392     VkResult ret;
2393     AVVulkanDeviceContext *hwctx = ctx->hwctx;
2394     VkMappedMemoryRange invalidate_ctx[AV_NUM_DATA_POINTERS];
2395     int invalidate_count = 0;
2396
2397     for (int i = 0; i < nb_buffers; i++) {
2398         ret = vkMapMemory(hwctx->act_dev, buf[i].mem, 0,
2399                           VK_WHOLE_SIZE, 0, (void **)&mem[i]);
2400         if (ret != VK_SUCCESS) {
2401             av_log(ctx, AV_LOG_ERROR, "Failed to map buffer memory: %s\n",
2402                    vk_ret2str(ret));
2403             return AVERROR_EXTERNAL;
2404         }
2405     }
2406
2407     if (!invalidate)
2408         return 0;
2409
2410     for (int i = 0; i < nb_buffers; i++) {
2411         const VkMappedMemoryRange ival_buf = {
2412             .sType  = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
2413             .memory = buf[i].mem,
2414             .size   = VK_WHOLE_SIZE,
2415         };
2416         if (buf[i].flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
2417             continue;
2418         invalidate_ctx[invalidate_count++] = ival_buf;
2419     }
2420
2421     if (invalidate_count) {
2422         ret = vkInvalidateMappedMemoryRanges(hwctx->act_dev, invalidate_count,
2423                                              invalidate_ctx);
2424         if (ret != VK_SUCCESS)
2425             av_log(ctx, AV_LOG_WARNING, "Failed to invalidate memory: %s\n",
2426                    vk_ret2str(ret));
2427     }
2428
2429     return 0;
2430 }
2431
2432 static int unmap_buffers(AVHWDeviceContext *ctx, ImageBuffer *buf,
2433                          int nb_buffers, int flush)
2434 {
2435     int err = 0;
2436     VkResult ret;
2437     AVVulkanDeviceContext *hwctx = ctx->hwctx;
2438     VkMappedMemoryRange flush_ctx[AV_NUM_DATA_POINTERS];
2439     int flush_count = 0;
2440
2441     if (flush) {
2442         for (int i = 0; i < nb_buffers; i++) {
2443             const VkMappedMemoryRange flush_buf = {
2444                 .sType  = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
2445                 .memory = buf[i].mem,
2446                 .size   = VK_WHOLE_SIZE,
2447             };
2448             if (buf[i].flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
2449                 continue;
2450             flush_ctx[flush_count++] = flush_buf;
2451         }
2452     }
2453
2454     if (flush_count) {
2455         ret = vkFlushMappedMemoryRanges(hwctx->act_dev, flush_count, flush_ctx);
2456         if (ret != VK_SUCCESS) {
2457             av_log(ctx, AV_LOG_ERROR, "Failed to flush memory: %s\n",
2458                     vk_ret2str(ret));
2459             err = AVERROR_EXTERNAL; /* We still want to try to unmap them */
2460         }
2461     }
2462
2463     for (int i = 0; i < nb_buffers; i++)
2464         vkUnmapMemory(hwctx->act_dev, buf[i].mem);
2465
2466     return err;
2467 }
2468
2469 static int transfer_image_buf(AVHWDeviceContext *ctx, AVVkFrame *frame,
2470                               ImageBuffer *buffer, const int *buf_stride, int w,
2471                               int h, enum AVPixelFormat pix_fmt, int to_buf)
2472 {
2473     VkResult ret;
2474     AVVulkanDeviceContext *hwctx = ctx->hwctx;
2475     VulkanDevicePriv *s = ctx->internal->priv;
2476
2477     int bar_num = 0;
2478     VkPipelineStageFlagBits sem_wait_dst[AV_NUM_DATA_POINTERS];
2479
2480     const int planes = av_pix_fmt_count_planes(pix_fmt);
2481     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
2482
2483     VkCommandBufferBeginInfo cmd_start = {
2484         .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
2485         .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
2486     };
2487
2488     VkImageMemoryBarrier img_bar[AV_NUM_DATA_POINTERS] = { 0 };
2489
2490     VkSubmitInfo s_info = {
2491         .sType                = VK_STRUCTURE_TYPE_SUBMIT_INFO,
2492         .commandBufferCount   = 1,
2493         .pCommandBuffers      = &s->cmd.buf,
2494         .pSignalSemaphores    = frame->sem,
2495         .pWaitSemaphores      = frame->sem,
2496         .pWaitDstStageMask    = sem_wait_dst,
2497         .signalSemaphoreCount = planes,
2498         .waitSemaphoreCount   = planes,
2499     };
2500
2501     ret = vkBeginCommandBuffer(s->cmd.buf, &cmd_start);
2502     if (ret != VK_SUCCESS) {
2503         av_log(ctx, AV_LOG_ERROR, "Unable to init command buffer: %s\n",
2504                vk_ret2str(ret));
2505         return AVERROR_EXTERNAL;
2506     }
2507
2508     /* Change the image layout to something more optimal for transfers */
2509     for (int i = 0; i < planes; i++) {
2510         VkImageLayout new_layout = to_buf ? VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL :
2511                                             VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
2512         VkAccessFlags new_access = to_buf ? VK_ACCESS_TRANSFER_READ_BIT :
2513                                             VK_ACCESS_TRANSFER_WRITE_BIT;
2514
2515         sem_wait_dst[i] = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
2516
2517         /* If the layout matches and we have read access skip the barrier */
2518         if ((frame->layout[i] == new_layout) && (frame->access[i] & new_access))
2519             continue;
2520
2521         img_bar[bar_num].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
2522         img_bar[bar_num].srcAccessMask = 0x0;
2523         img_bar[bar_num].dstAccessMask = new_access;
2524         img_bar[bar_num].oldLayout = frame->layout[i];
2525         img_bar[bar_num].newLayout = new_layout;
2526         img_bar[bar_num].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
2527         img_bar[bar_num].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
2528         img_bar[bar_num].image = frame->img[i];
2529         img_bar[bar_num].subresourceRange.levelCount = 1;
2530         img_bar[bar_num].subresourceRange.layerCount = 1;
2531         img_bar[bar_num].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
2532
2533         frame->layout[i] = img_bar[bar_num].newLayout;
2534         frame->access[i] = img_bar[bar_num].dstAccessMask;
2535
2536         bar_num++;
2537     }
2538
2539     if (bar_num)
2540         vkCmdPipelineBarrier(s->cmd.buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
2541                              VK_PIPELINE_STAGE_TRANSFER_BIT, 0,
2542                              0, NULL, 0, NULL, bar_num, img_bar);
2543
2544     /* Schedule a copy for each plane */
2545     for (int i = 0; i < planes; i++) {
2546         const int p_w = i > 0 ? AV_CEIL_RSHIFT(w, desc->log2_chroma_w) : w;
2547         const int p_h = i > 0 ? AV_CEIL_RSHIFT(h, desc->log2_chroma_h) : h;
2548         VkBufferImageCopy buf_reg = {
2549             .bufferOffset = 0,
2550             /* Buffer stride isn't in bytes, it's in samples, the implementation
2551              * uses the image's VkFormat to know how many bytes per sample
2552              * the buffer has. So we have to convert by dividing. Stupid.
2553              * Won't work with YUVA or other planar formats with alpha. */
2554             .bufferRowLength = buf_stride[i] / desc->comp[i].step,
2555             .bufferImageHeight = p_h,
2556             .imageSubresource.layerCount = 1,
2557             .imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
2558             .imageOffset = { 0, 0, 0, },
2559             .imageExtent = { p_w, p_h, 1, },
2560         };
2561
2562         if (to_buf)
2563             vkCmdCopyImageToBuffer(s->cmd.buf, frame->img[i], frame->layout[i],
2564                                    buffer[i].buf, 1, &buf_reg);
2565         else
2566             vkCmdCopyBufferToImage(s->cmd.buf, buffer[i].buf, frame->img[i],
2567                                    frame->layout[i], 1, &buf_reg);
2568     }
2569
2570     ret = vkEndCommandBuffer(s->cmd.buf);
2571     if (ret != VK_SUCCESS) {
2572         av_log(ctx, AV_LOG_ERROR, "Unable to finish command buffer: %s\n",
2573                vk_ret2str(ret));
2574         return AVERROR_EXTERNAL;
2575     }
2576
2577     /* Wait for the download/upload to finish if uploading, otherwise the
2578      * semaphore will take care of synchronization when uploading */
2579     ret = vkQueueSubmit(s->cmd.queue, 1, &s_info, s->cmd.fence);
2580     if (ret != VK_SUCCESS) {
2581         av_log(ctx, AV_LOG_ERROR, "Unable to submit command buffer: %s\n",
2582                vk_ret2str(ret));
2583         return AVERROR_EXTERNAL;
2584     } else {
2585         vkWaitForFences(hwctx->act_dev, 1, &s->cmd.fence, VK_TRUE, UINT64_MAX);
2586         vkResetFences(hwctx->act_dev, 1, &s->cmd.fence);
2587     }
2588
2589     return 0;
2590 }
2591
2592 /* Technically we can use VK_EXT_external_memory_host to upload and download,
2593  * however the alignment requirements make this unfeasible as both the pointer
2594  * and the size of each plane need to be aligned to the minimum alignment
2595  * requirement, which on all current implementations (anv, radv) is 4096.
2596  * If the requirement gets relaxed (unlikely) this can easily be implemented. */
2597 static int vulkan_transfer_data_from_mem(AVHWFramesContext *hwfc, AVFrame *dst,
2598                                          const AVFrame *src)
2599 {
2600     int err = 0;
2601     AVFrame tmp;
2602     AVVkFrame *f = (AVVkFrame *)dst->data[0];
2603     AVHWDeviceContext *dev_ctx = hwfc->device_ctx;
2604     ImageBuffer buf[AV_NUM_DATA_POINTERS] = { { 0 } };
2605     const int planes = av_pix_fmt_count_planes(src->format);
2606     int log2_chroma = av_pix_fmt_desc_get(src->format)->log2_chroma_h;
2607
2608     if ((src->format != AV_PIX_FMT_NONE && !av_vkfmt_from_pixfmt(src->format))) {
2609         av_log(hwfc, AV_LOG_ERROR, "Unsupported source pixel format!\n");
2610         return AVERROR(EINVAL);
2611     }
2612
2613     if (src->width > hwfc->width || src->height > hwfc->height)
2614         return AVERROR(EINVAL);
2615
2616     /* For linear, host visiable images */
2617     if (f->tiling == VK_IMAGE_TILING_LINEAR &&
2618         f->flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
2619         AVFrame *map = av_frame_alloc();
2620         if (!map)
2621             return AVERROR(ENOMEM);
2622         map->format = src->format;
2623
2624         err = vulkan_map_frame_to_mem(hwfc, map, dst, AV_HWFRAME_MAP_WRITE);
2625         if (err)
2626             goto end;
2627
2628         err = av_frame_copy(map, src);
2629         av_frame_free(&map);
2630         goto end;
2631     }
2632
2633     /* Create buffers */
2634     for (int i = 0; i < planes; i++) {
2635         int h = src->height;
2636         int p_height = i > 0 ? AV_CEIL_RSHIFT(h, log2_chroma) : h;
2637
2638         tmp.linesize[i] = src->linesize[i];
2639         err = create_buf(dev_ctx, &buf[i], p_height,
2640                          &tmp.linesize[i], VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
2641                          VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, NULL, NULL);
2642         if (err)
2643             goto end;
2644     }
2645
2646     /* Map, copy image to buffer, unmap */
2647     if ((err = map_buffers(dev_ctx, buf, tmp.data, planes, 0)))
2648         goto end;
2649
2650     av_image_copy(tmp.data, tmp.linesize, (const uint8_t **)src->data,
2651                   src->linesize, src->format, src->width, src->height);
2652
2653     if ((err = unmap_buffers(dev_ctx, buf, planes, 1)))
2654         goto end;
2655
2656     /* Copy buffers to image */
2657     err = transfer_image_buf(dev_ctx, f, buf, tmp.linesize,
2658                              src->width, src->height, src->format, 0);
2659
2660 end:
2661     for (int i = 0; i < planes; i++)
2662         free_buf(dev_ctx, &buf[i]);
2663
2664     return err;
2665 }
2666
2667 static int vulkan_transfer_data_to(AVHWFramesContext *hwfc, AVFrame *dst,
2668                                         const AVFrame *src)
2669 {
2670     av_unused VulkanDevicePriv *p = hwfc->device_ctx->internal->priv;
2671
2672     switch (src->format) {
2673 #if CONFIG_CUDA
2674     case AV_PIX_FMT_CUDA:
2675         if ((p->extensions & EXT_EXTERNAL_FD_MEMORY) &&
2676             (p->extensions & EXT_EXTERNAL_FD_SEM))
2677             return vulkan_transfer_data_from_cuda(hwfc, dst, src);
2678 #endif
2679     default:
2680         if (src->hw_frames_ctx)
2681             return AVERROR(ENOSYS);
2682         else
2683             return vulkan_transfer_data_from_mem(hwfc, dst, src);
2684     }
2685 }
2686
2687 #if CONFIG_CUDA
2688 static int vulkan_transfer_data_to_cuda(AVHWFramesContext *hwfc, AVFrame *dst,
2689                                       const AVFrame *src)
2690 {
2691     int err;
2692     VkResult ret;
2693     CUcontext dummy;
2694     AVVkFrame *dst_f;
2695     AVVkFrameInternal *dst_int;
2696     const int planes = av_pix_fmt_count_planes(hwfc->sw_format);
2697     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(hwfc->sw_format);
2698
2699     AVHWFramesContext *cuda_fc = (AVHWFramesContext*)dst->hw_frames_ctx->data;
2700     AVHWDeviceContext *cuda_cu = cuda_fc->device_ctx;
2701     AVCUDADeviceContext *cuda_dev = cuda_cu->hwctx;
2702     AVCUDADeviceContextInternal *cu_internal = cuda_dev->internal;
2703     CudaFunctions *cu = cu_internal->cuda_dl;
2704
2705     ret = CHECK_CU(cu->cuCtxPushCurrent(cuda_dev->cuda_ctx));
2706     if (ret < 0) {
2707         err = AVERROR_EXTERNAL;
2708         goto fail;
2709     }
2710
2711     dst_f = (AVVkFrame *)src->data[0];
2712
2713     err = vulkan_export_to_cuda(hwfc, dst->hw_frames_ctx, src);
2714     if (err < 0) {
2715         goto fail;
2716     }
2717
2718     dst_int = dst_f->internal;
2719
2720     for (int i = 0; i < planes; i++) {
2721         CUDA_MEMCPY2D cpy = {
2722             .dstMemoryType = CU_MEMORYTYPE_DEVICE,
2723             .dstDevice     = (CUdeviceptr)dst->data[i],
2724             .dstPitch      = dst->linesize[i],
2725             .dstY          = 0,
2726
2727             .srcMemoryType = CU_MEMORYTYPE_ARRAY,
2728             .srcArray      = dst_int->cu_array[i],
2729             .WidthInBytes  = (i > 0 ? AV_CEIL_RSHIFT(hwfc->width, desc->log2_chroma_w)
2730                                     : hwfc->width) * desc->comp[i].step,
2731             .Height        = i > 0 ? AV_CEIL_RSHIFT(hwfc->height, desc->log2_chroma_h)
2732                                    : hwfc->height,
2733         };
2734
2735         ret = CHECK_CU(cu->cuMemcpy2DAsync(&cpy, cuda_dev->stream));
2736         if (ret < 0) {
2737             err = AVERROR_EXTERNAL;
2738             goto fail;
2739         }
2740     }
2741
2742     CHECK_CU(cu->cuCtxPopCurrent(&dummy));
2743
2744     av_log(hwfc, AV_LOG_VERBOSE, "Transfered Vulkan image to CUDA!\n");
2745
2746     return 0;
2747
2748 fail:
2749     CHECK_CU(cu->cuCtxPopCurrent(&dummy));
2750     vulkan_free_internal(dst_int);
2751     dst_f->internal = NULL;
2752     av_buffer_unref(&dst->buf[0]);
2753     return err;
2754 }
2755 #endif
2756
2757 static int vulkan_transfer_data_to_mem(AVHWFramesContext *hwfc, AVFrame *dst,
2758                                        const AVFrame *src)
2759 {
2760     int err = 0;
2761     AVFrame tmp;
2762     AVVkFrame *f = (AVVkFrame *)src->data[0];
2763     AVHWDeviceContext *dev_ctx = hwfc->device_ctx;
2764     ImageBuffer buf[AV_NUM_DATA_POINTERS] = { { 0 } };
2765     const int planes = av_pix_fmt_count_planes(dst->format);
2766     int log2_chroma = av_pix_fmt_desc_get(dst->format)->log2_chroma_h;
2767
2768     if (dst->width > hwfc->width || dst->height > hwfc->height)
2769         return AVERROR(EINVAL);
2770
2771     /* For linear, host visiable images */
2772     if (f->tiling == VK_IMAGE_TILING_LINEAR &&
2773         f->flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
2774         AVFrame *map = av_frame_alloc();
2775         if (!map)
2776             return AVERROR(ENOMEM);
2777         map->format = dst->format;
2778
2779         err = vulkan_map_frame_to_mem(hwfc, map, src, AV_HWFRAME_MAP_READ);
2780         if (err)
2781             return err;
2782
2783         err = av_frame_copy(dst, map);
2784         av_frame_free(&map);
2785         return err;
2786     }
2787
2788     /* Create buffers */
2789     for (int i = 0; i < planes; i++) {
2790         int h = dst->height;
2791         int p_height = i > 0 ? AV_CEIL_RSHIFT(h, log2_chroma) : h;
2792
2793         tmp.linesize[i] = dst->linesize[i];
2794         err = create_buf(dev_ctx, &buf[i], p_height,
2795                          &tmp.linesize[i], VK_BUFFER_USAGE_TRANSFER_DST_BIT,
2796                          VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, NULL, NULL);
2797     }
2798
2799     /* Copy image to buffer */
2800     if ((err = transfer_image_buf(dev_ctx, f, buf, tmp.linesize,
2801                                   dst->width, dst->height, dst->format, 1)))
2802         goto end;
2803
2804     /* Map, copy buffer to frame, unmap */
2805     if ((err = map_buffers(dev_ctx, buf, tmp.data, planes, 1)))
2806         goto end;
2807
2808     av_image_copy(dst->data, dst->linesize, (const uint8_t **)tmp.data,
2809                   tmp.linesize, dst->format, dst->width, dst->height);
2810
2811     err = unmap_buffers(dev_ctx, buf, planes, 0);
2812
2813 end:
2814     for (int i = 0; i < planes; i++)
2815         free_buf(dev_ctx, &buf[i]);
2816
2817     return err;
2818 }
2819
2820 static int vulkan_transfer_data_from(AVHWFramesContext *hwfc, AVFrame *dst,
2821                                      const AVFrame *src)
2822 {
2823     av_unused VulkanDevicePriv *p = hwfc->device_ctx->internal->priv;
2824
2825     switch (dst->format) {
2826 #if CONFIG_CUDA
2827     case AV_PIX_FMT_CUDA:
2828         if ((p->extensions & EXT_EXTERNAL_FD_MEMORY) &&
2829             (p->extensions & EXT_EXTERNAL_FD_SEM))
2830             return vulkan_transfer_data_to_cuda(hwfc, dst, src);
2831 #endif
2832     default:
2833         if (dst->hw_frames_ctx)
2834             return AVERROR(ENOSYS);
2835         else
2836             return vulkan_transfer_data_to_mem(hwfc, dst, src);
2837     }
2838 }
2839
2840 AVVkFrame *av_vk_frame_alloc(void)
2841 {
2842     return av_mallocz(sizeof(AVVkFrame));
2843 }
2844
2845 const HWContextType ff_hwcontext_type_vulkan = {
2846     .type                   = AV_HWDEVICE_TYPE_VULKAN,
2847     .name                   = "Vulkan",
2848
2849     .device_hwctx_size      = sizeof(AVVulkanDeviceContext),
2850     .device_priv_size       = sizeof(VulkanDevicePriv),
2851     .frames_hwctx_size      = sizeof(AVVulkanFramesContext),
2852     .frames_priv_size       = sizeof(VulkanFramesPriv),
2853
2854     .device_init            = &vulkan_device_init,
2855     .device_create          = &vulkan_device_create,
2856     .device_derive          = &vulkan_device_derive,
2857
2858     .frames_get_constraints = &vulkan_frames_get_constraints,
2859     .frames_init            = vulkan_frames_init,
2860     .frames_get_buffer      = vulkan_get_buffer,
2861     .frames_uninit          = vulkan_frames_uninit,
2862
2863     .transfer_get_formats   = vulkan_transfer_get_formats,
2864     .transfer_data_to       = vulkan_transfer_data_to,
2865     .transfer_data_from     = vulkan_transfer_data_from,
2866
2867     .map_to                 = vulkan_map_to,
2868     .map_from               = vulkan_map_from,
2869
2870     .pix_fmts = (const enum AVPixelFormat []) {
2871         AV_PIX_FMT_VULKAN,
2872         AV_PIX_FMT_NONE
2873     },
2874 };