FFmpeg  4.4.9
hwcontext_vulkan.c
Go to the documentation of this file.
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
40 #include "cuda_check.h"
41 #define CHECK_CU(x) FF_CUDA_CHECK_DL(cuda_cu, cu, x)
42 #endif
43 
44 typedef struct VulkanQueueCtx {
45  VkFence fence;
46  VkQueue queue;
48 
49  /* Buffer dependencies */
54 
55 typedef struct VulkanExecCtx {
56  VkCommandPool pool;
57  VkCommandBuffer *bufs;
59  int nb_queues;
62 
63 typedef struct VulkanDevicePriv {
64  /* Properties */
65  VkPhysicalDeviceProperties2 props;
66  VkPhysicalDeviceMemoryProperties mprops;
67  VkPhysicalDeviceExternalMemoryHostPropertiesEXT hprops;
68 
69  /* Queues */
70  uint32_t qfs[3];
71  int num_qfs;
72 
73  /* Debug callback */
74  VkDebugUtilsMessengerEXT debug_ctx;
75 
76  /* Extensions */
77  uint64_t extensions;
78 
79  /* Settings */
81 
82  /* Nvidia */
85 
86 typedef struct VulkanFramesPriv {
87  /* Image conversions */
89 
90  /* Image transfers */
94 
95 typedef struct AVVkFrameInternal {
96 #if CONFIG_CUDA
97  /* Importing external memory into cuda is really expensive so we keep the
98  * memory imported all the time */
99  AVBufferRef *cuda_fc_ref; /* Need to keep it around for uninit */
100  CUexternalMemory ext_mem[AV_NUM_DATA_POINTERS];
101  CUmipmappedArray cu_mma[AV_NUM_DATA_POINTERS];
102  CUarray cu_array[AV_NUM_DATA_POINTERS];
103  CUexternalSemaphore cu_sem[AV_NUM_DATA_POINTERS];
104 #endif
106 
107 #define GET_QUEUE_COUNT(hwctx, graph, comp, tx) ( \
108  graph ? hwctx->nb_graphics_queues : \
109  comp ? (hwctx->nb_comp_queues ? \
110  hwctx->nb_comp_queues : hwctx->nb_graphics_queues) : \
111  tx ? (hwctx->nb_tx_queues ? hwctx->nb_tx_queues : \
112  (hwctx->nb_comp_queues ? \
113  hwctx->nb_comp_queues : hwctx->nb_graphics_queues)) : \
114  0 \
115 )
116 
117 #define VK_LOAD_PFN(inst, name) PFN_##name pfn_##name = (PFN_##name) \
118  vkGetInstanceProcAddr(inst, #name)
119 
120 #define DEFAULT_USAGE_FLAGS (VK_IMAGE_USAGE_SAMPLED_BIT | \
121  VK_IMAGE_USAGE_STORAGE_BIT | \
122  VK_IMAGE_USAGE_TRANSFER_SRC_BIT | \
123  VK_IMAGE_USAGE_TRANSFER_DST_BIT)
124 
125 #define ADD_VAL_TO_LIST(list, count, val) \
126  do { \
127  list = av_realloc_array(list, sizeof(*list), ++count); \
128  if (!list) { \
129  err = AVERROR(ENOMEM); \
130  goto fail; \
131  } \
132  list[count - 1] = av_strdup(val); \
133  if (!list[count - 1]) { \
134  err = AVERROR(ENOMEM); \
135  goto fail; \
136  } \
137  } while(0)
138 
139 static const struct {
140  enum AVPixelFormat pixfmt;
141  const VkFormat vkfmts[4];
142 } vk_pixfmt_map[] = {
143  { AV_PIX_FMT_GRAY8, { VK_FORMAT_R8_UNORM } },
144  { AV_PIX_FMT_GRAY16, { VK_FORMAT_R16_UNORM } },
145  { AV_PIX_FMT_GRAYF32, { VK_FORMAT_R32_SFLOAT } },
146 
147  { AV_PIX_FMT_NV12, { VK_FORMAT_R8_UNORM, VK_FORMAT_R8G8_UNORM } },
148  { AV_PIX_FMT_NV21, { VK_FORMAT_R8_UNORM, VK_FORMAT_R8G8_UNORM } },
149  { AV_PIX_FMT_P010, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16G16_UNORM } },
150  { AV_PIX_FMT_P016, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16G16_UNORM } },
151 
152  { AV_PIX_FMT_NV16, { VK_FORMAT_R8_UNORM, VK_FORMAT_R8G8_UNORM } },
153 
154  { AV_PIX_FMT_NV24, { VK_FORMAT_R8_UNORM, VK_FORMAT_R8G8_UNORM } },
155  { AV_PIX_FMT_NV42, { VK_FORMAT_R8_UNORM, VK_FORMAT_R8G8_UNORM } },
156 
157  { AV_PIX_FMT_YUV420P, { VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM } },
158  { AV_PIX_FMT_YUV420P10, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
159  { AV_PIX_FMT_YUV420P12, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
160  { AV_PIX_FMT_YUV420P16, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
161 
162  { AV_PIX_FMT_YUV422P, { VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM } },
163  { AV_PIX_FMT_YUV422P10, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
164  { AV_PIX_FMT_YUV422P12, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
165  { AV_PIX_FMT_YUV422P16, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
166 
167  { AV_PIX_FMT_YUV444P, { VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM } },
168  { AV_PIX_FMT_YUV444P10, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
169  { AV_PIX_FMT_YUV444P12, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
170  { AV_PIX_FMT_YUV444P16, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
171 
172  { AV_PIX_FMT_YUVA420P, { VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM } },
173  { AV_PIX_FMT_YUVA420P10, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
174  /* There is no AV_PIX_FMT_YUVA420P12 */
175  { AV_PIX_FMT_YUVA420P16, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
176 
177  { AV_PIX_FMT_YUVA422P, { VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM } },
178  { AV_PIX_FMT_YUVA422P10, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
179  { AV_PIX_FMT_YUVA422P12, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
180  { AV_PIX_FMT_YUVA422P16, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
181 
182  { AV_PIX_FMT_YUVA444P, { VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM } },
183  { AV_PIX_FMT_YUVA444P10, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
184  { AV_PIX_FMT_YUVA444P12, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
185  { AV_PIX_FMT_YUVA444P16, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
186 
187  { AV_PIX_FMT_BGRA, { VK_FORMAT_B8G8R8A8_UNORM } },
188  { AV_PIX_FMT_RGBA, { VK_FORMAT_R8G8B8A8_UNORM } },
189  { AV_PIX_FMT_RGB24, { VK_FORMAT_R8G8B8_UNORM } },
190  { AV_PIX_FMT_BGR24, { VK_FORMAT_B8G8R8_UNORM } },
191  { AV_PIX_FMT_RGB48, { VK_FORMAT_R16G16B16_UNORM } },
192  { AV_PIX_FMT_RGBA64, { VK_FORMAT_R16G16B16A16_UNORM } },
193  { AV_PIX_FMT_RGBA64, { VK_FORMAT_R16G16B16A16_UNORM } },
194  { AV_PIX_FMT_RGB565, { VK_FORMAT_R5G6B5_UNORM_PACK16 } },
195  { AV_PIX_FMT_BGR565, { VK_FORMAT_B5G6R5_UNORM_PACK16 } },
196  { AV_PIX_FMT_BGR0, { VK_FORMAT_B8G8R8A8_UNORM } },
197  { AV_PIX_FMT_RGB0, { VK_FORMAT_R8G8B8A8_UNORM } },
198 
199  /* Lower priority as there's an endianess-dependent overlap between these
200  * and rgba/bgr0, and PACK32 formats are more limited */
201  { AV_PIX_FMT_BGR32, { VK_FORMAT_A8B8G8R8_UNORM_PACK32 } },
202  { AV_PIX_FMT_0BGR32, { VK_FORMAT_A8B8G8R8_UNORM_PACK32 } },
203 
204  { AV_PIX_FMT_X2RGB10, { VK_FORMAT_A2R10G10B10_UNORM_PACK32 } },
205 
206  { AV_PIX_FMT_GBRAP, { VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM } },
207  { AV_PIX_FMT_GBRAP16, { VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM } },
208  { AV_PIX_FMT_GBRPF32, { VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT } },
209  { AV_PIX_FMT_GBRAPF32, { VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT } },
210 };
211 
212 const VkFormat *av_vkfmt_from_pixfmt(enum AVPixelFormat p)
213 {
214  for (enum AVPixelFormat i = 0; i < FF_ARRAY_ELEMS(vk_pixfmt_map); i++)
215  if (vk_pixfmt_map[i].pixfmt == p)
216  return vk_pixfmt_map[i].vkfmts;
217  return NULL;
218 }
219 
221  int linear)
222 {
223  const VkFormat *fmt = av_vkfmt_from_pixfmt(p);
225 
226  if (!fmt)
227  return 0;
228 
229  for (int i = 0; i < planes; i++) {
230  VkFormatFeatureFlags flags;
231  VkFormatProperties2 prop = {
232  .sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2,
233  };
234  vkGetPhysicalDeviceFormatProperties2(hwctx->phys_dev, fmt[i], &prop);
235  flags = linear ? prop.formatProperties.linearTilingFeatures :
236  prop.formatProperties.optimalTilingFeatures;
237  if (!(flags & DEFAULT_USAGE_FLAGS))
238  return 0;
239  }
240 
241  return 1;
242 }
243 
245  EXT_EXTERNAL_DMABUF_MEMORY = 1ULL << 0, /* VK_EXT_external_memory_dma_buf */
246  EXT_DRM_MODIFIER_FLAGS = 1ULL << 1, /* VK_EXT_image_drm_format_modifier */
247  EXT_EXTERNAL_FD_MEMORY = 1ULL << 2, /* VK_KHR_external_memory_fd */
248  EXT_EXTERNAL_FD_SEM = 1ULL << 3, /* VK_KHR_external_semaphore_fd */
249  EXT_EXTERNAL_HOST_MEMORY = 1ULL << 4, /* VK_EXT_external_memory_host */
250  EXT_PUSH_DESCRIPTORS = 1ULL << 5, /* VK_KHR_push_descriptor */
251  EXT_HOST_QUERY_RESET = 1ULL << 6, /* VK_EXT_host_query_reset */
252 
253  EXT_NO_FLAG = 1ULL << 63,
254 };
255 
256 typedef struct VulkanOptExtension {
257  const char *name;
258  uint64_t flag;
260 
262  /* For future use */
263 };
264 
266  { VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME, EXT_EXTERNAL_FD_MEMORY, },
267  { VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME, EXT_EXTERNAL_DMABUF_MEMORY, },
268  { VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME, EXT_DRM_MODIFIER_FLAGS, },
269  { VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME, EXT_EXTERNAL_FD_SEM, },
270  { VK_EXT_EXTERNAL_MEMORY_HOST_EXTENSION_NAME, EXT_EXTERNAL_HOST_MEMORY, },
271  { VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME, EXT_PUSH_DESCRIPTORS, },
272  { VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME, EXT_HOST_QUERY_RESET, },
273 };
274 
275 /* Converts return values to strings */
276 static const char *vk_ret2str(VkResult res)
277 {
278 #define CASE(VAL) case VAL: return #VAL
279  switch (res) {
280  CASE(VK_SUCCESS);
281  CASE(VK_NOT_READY);
282  CASE(VK_TIMEOUT);
283  CASE(VK_EVENT_SET);
284  CASE(VK_EVENT_RESET);
285  CASE(VK_INCOMPLETE);
286  CASE(VK_ERROR_OUT_OF_HOST_MEMORY);
287  CASE(VK_ERROR_OUT_OF_DEVICE_MEMORY);
288  CASE(VK_ERROR_INITIALIZATION_FAILED);
289  CASE(VK_ERROR_DEVICE_LOST);
290  CASE(VK_ERROR_MEMORY_MAP_FAILED);
291  CASE(VK_ERROR_LAYER_NOT_PRESENT);
292  CASE(VK_ERROR_EXTENSION_NOT_PRESENT);
293  CASE(VK_ERROR_FEATURE_NOT_PRESENT);
294  CASE(VK_ERROR_INCOMPATIBLE_DRIVER);
295  CASE(VK_ERROR_TOO_MANY_OBJECTS);
296  CASE(VK_ERROR_FORMAT_NOT_SUPPORTED);
297  CASE(VK_ERROR_FRAGMENTED_POOL);
298  CASE(VK_ERROR_SURFACE_LOST_KHR);
299  CASE(VK_ERROR_NATIVE_WINDOW_IN_USE_KHR);
300  CASE(VK_SUBOPTIMAL_KHR);
301  CASE(VK_ERROR_OUT_OF_DATE_KHR);
302  CASE(VK_ERROR_INCOMPATIBLE_DISPLAY_KHR);
303  CASE(VK_ERROR_VALIDATION_FAILED_EXT);
304  CASE(VK_ERROR_INVALID_SHADER_NV);
305  CASE(VK_ERROR_OUT_OF_POOL_MEMORY);
306  CASE(VK_ERROR_INVALID_EXTERNAL_HANDLE);
307  CASE(VK_ERROR_NOT_PERMITTED_EXT);
308  CASE(VK_ERROR_INVALID_DRM_FORMAT_MODIFIER_PLANE_LAYOUT_EXT);
309  CASE(VK_ERROR_INVALID_DEVICE_ADDRESS_EXT);
310  CASE(VK_ERROR_FULL_SCREEN_EXCLUSIVE_MODE_LOST_EXT);
311  default: return "Unknown error";
312  }
313 #undef CASE
314 }
315 
316 static VkBool32 VKAPI_CALL vk_dbg_callback(VkDebugUtilsMessageSeverityFlagBitsEXT severity,
317  VkDebugUtilsMessageTypeFlagsEXT messageType,
318  const VkDebugUtilsMessengerCallbackDataEXT *data,
319  void *priv)
320 {
321  int l;
322  AVHWDeviceContext *ctx = priv;
323 
324  switch (severity) {
325  case VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT: l = AV_LOG_VERBOSE; break;
326  case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT: l = AV_LOG_INFO; break;
327  case VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT: l = AV_LOG_WARNING; break;
328  case VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT: l = AV_LOG_ERROR; break;
329  default: l = AV_LOG_DEBUG; break;
330  }
331 
332  av_log(ctx, l, "%s\n", data->pMessage);
333  for (int i = 0; i < data->cmdBufLabelCount; i++)
334  av_log(ctx, l, "\t%i: %s\n", i, data->pCmdBufLabels[i].pLabelName);
335 
336  return 0;
337 }
338 
340  const char * const **dst, uint32_t *num, int debug)
341 {
342  const char *tstr;
343  const char **extension_names = NULL;
344  VulkanDevicePriv *p = ctx->internal->priv;
345  AVVulkanDeviceContext *hwctx = ctx->hwctx;
346  int err = 0, found, extensions_found = 0;
347 
348  const char *mod;
349  int optional_exts_num;
350  uint32_t sup_ext_count;
351  char *user_exts_str = NULL;
352  AVDictionaryEntry *user_exts;
353  VkExtensionProperties *sup_ext;
354  const VulkanOptExtension *optional_exts;
355 
356  if (!dev) {
357  mod = "instance";
358  optional_exts = optional_instance_exts;
359  optional_exts_num = FF_ARRAY_ELEMS(optional_instance_exts);
360  user_exts = av_dict_get(opts, "instance_extensions", NULL, 0);
361  if (user_exts) {
362  user_exts_str = av_strdup(user_exts->value);
363  if (!user_exts_str) {
364  err = AVERROR(ENOMEM);
365  goto fail;
366  }
367  }
368  vkEnumerateInstanceExtensionProperties(NULL, &sup_ext_count, NULL);
369  sup_ext = av_malloc_array(sup_ext_count, sizeof(VkExtensionProperties));
370  if (!sup_ext)
371  return AVERROR(ENOMEM);
372  vkEnumerateInstanceExtensionProperties(NULL, &sup_ext_count, sup_ext);
373  } else {
374  mod = "device";
375  optional_exts = optional_device_exts;
376  optional_exts_num = FF_ARRAY_ELEMS(optional_device_exts);
377  user_exts = av_dict_get(opts, "device_extensions", NULL, 0);
378  if (user_exts) {
379  user_exts_str = av_strdup(user_exts->value);
380  if (!user_exts_str) {
381  err = AVERROR(ENOMEM);
382  goto fail;
383  }
384  }
385  vkEnumerateDeviceExtensionProperties(hwctx->phys_dev, NULL,
386  &sup_ext_count, NULL);
387  sup_ext = av_malloc_array(sup_ext_count, sizeof(VkExtensionProperties));
388  if (!sup_ext)
389  return AVERROR(ENOMEM);
390  vkEnumerateDeviceExtensionProperties(hwctx->phys_dev, NULL,
391  &sup_ext_count, sup_ext);
392  }
393 
394  for (int i = 0; i < optional_exts_num; i++) {
395  tstr = optional_exts[i].name;
396  found = 0;
397  for (int j = 0; j < sup_ext_count; j++) {
398  if (!strcmp(tstr, sup_ext[j].extensionName)) {
399  found = 1;
400  break;
401  }
402  }
403  if (!found)
404  continue;
405 
406  av_log(ctx, AV_LOG_VERBOSE, "Using %s extension \"%s\"\n", mod, tstr);
407  p->extensions |= optional_exts[i].flag;
408  ADD_VAL_TO_LIST(extension_names, extensions_found, tstr);
409  }
410 
411  if (debug && !dev) {
412  tstr = VK_EXT_DEBUG_UTILS_EXTENSION_NAME;
413  found = 0;
414  for (int j = 0; j < sup_ext_count; j++) {
415  if (!strcmp(tstr, sup_ext[j].extensionName)) {
416  found = 1;
417  break;
418  }
419  }
420  if (found) {
421  av_log(ctx, AV_LOG_VERBOSE, "Using %s extension \"%s\"\n", mod, tstr);
422  ADD_VAL_TO_LIST(extension_names, extensions_found, tstr);
423  } else {
424  av_log(ctx, AV_LOG_ERROR, "Debug extension \"%s\" not found!\n",
425  tstr);
426  err = AVERROR(EINVAL);
427  goto fail;
428  }
429  }
430 
431  if (user_exts_str) {
432  char *save, *token = av_strtok(user_exts_str, "+", &save);
433  while (token) {
434  found = 0;
435  for (int j = 0; j < sup_ext_count; j++) {
436  if (!strcmp(token, sup_ext[j].extensionName)) {
437  found = 1;
438  break;
439  }
440  }
441  if (found) {
442  av_log(ctx, AV_LOG_VERBOSE, "Using %s extension \"%s\"\n", mod, token);
443  ADD_VAL_TO_LIST(extension_names, extensions_found, token);
444  } else {
445  av_log(ctx, AV_LOG_WARNING, "%s extension \"%s\" not found, excluding.\n",
446  mod, token);
447  }
448  token = av_strtok(NULL, "+", &save);
449  }
450  }
451 
452  *dst = extension_names;
453  *num = extensions_found;
454 
455  av_free(user_exts_str);
456  av_free(sup_ext);
457  return 0;
458 
459 fail:
460  if (extension_names)
461  for (int i = 0; i < extensions_found; i++)
462  av_free((void *)extension_names[i]);
463  av_free(extension_names);
464  av_free(user_exts_str);
465  av_free(sup_ext);
466  return err;
467 }
468 
469 /* Creates a VkInstance */
471 {
472  int err = 0;
473  VkResult ret;
474  VulkanDevicePriv *p = ctx->internal->priv;
475  AVVulkanDeviceContext *hwctx = ctx->hwctx;
476  AVDictionaryEntry *debug_opt = av_dict_get(opts, "debug", NULL, 0);
477  const int debug_mode = debug_opt && strtol(debug_opt->value, NULL, 10);
478  VkApplicationInfo application_info = {
479  .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
480  .pEngineName = "libavutil",
481  .apiVersion = VK_API_VERSION_1_1,
482  .engineVersion = VK_MAKE_VERSION(LIBAVUTIL_VERSION_MAJOR,
485  };
486  VkInstanceCreateInfo inst_props = {
487  .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
488  .pApplicationInfo = &application_info,
489  };
490 
491  /* Check for present/missing extensions */
492  err = check_extensions(ctx, 0, opts, &inst_props.ppEnabledExtensionNames,
493  &inst_props.enabledExtensionCount, debug_mode);
494  if (err < 0)
495  return err;
496 
497  if (debug_mode) {
498  static const char *layers[] = { "VK_LAYER_KHRONOS_validation" };
499  inst_props.ppEnabledLayerNames = layers;
500  inst_props.enabledLayerCount = FF_ARRAY_ELEMS(layers);
501  }
502 
503  /* Try to create the instance */
504  ret = vkCreateInstance(&inst_props, hwctx->alloc, &hwctx->inst);
505 
506  /* Check for errors */
507  if (ret != VK_SUCCESS) {
508  av_log(ctx, AV_LOG_ERROR, "Instance creation failure: %s\n",
509  vk_ret2str(ret));
510  for (int i = 0; i < inst_props.enabledExtensionCount; i++)
511  av_free((void *)inst_props.ppEnabledExtensionNames[i]);
512  av_free((void *)inst_props.ppEnabledExtensionNames);
513  return AVERROR_EXTERNAL;
514  }
515 
516  if (debug_mode) {
517  VkDebugUtilsMessengerCreateInfoEXT dbg = {
518  .sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT,
519  .messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
520  VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT |
521  VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
522  VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT,
523  .messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
524  VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
525  VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT,
526  .pfnUserCallback = vk_dbg_callback,
527  .pUserData = ctx,
528  };
529  VK_LOAD_PFN(hwctx->inst, vkCreateDebugUtilsMessengerEXT);
530 
531  pfn_vkCreateDebugUtilsMessengerEXT(hwctx->inst, &dbg,
532  hwctx->alloc, &p->debug_ctx);
533  }
534 
535  hwctx->enabled_inst_extensions = inst_props.ppEnabledExtensionNames;
536  hwctx->nb_enabled_inst_extensions = inst_props.enabledExtensionCount;
537 
538  return 0;
539 }
540 
541 typedef struct VulkanDeviceSelection {
542  uint8_t uuid[VK_UUID_SIZE]; /* Will use this first unless !has_uuid */
543  int has_uuid;
544  const char *name; /* Will use this second unless NULL */
545  uint32_t pci_device; /* Will use this third unless 0x0 */
546  uint32_t vendor_id; /* Last resort to find something deterministic */
547  int index; /* Finally fall back to index */
549 
550 static const char *vk_dev_type(enum VkPhysicalDeviceType type)
551 {
552  switch (type) {
553  case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU: return "integrated";
554  case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU: return "discrete";
555  case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU: return "virtual";
556  case VK_PHYSICAL_DEVICE_TYPE_CPU: return "software";
557  default: return "unknown";
558  }
559 }
560 
561 /* Finds a device */
563 {
564  int err = 0, choice = -1;
565  uint32_t num;
566  VkResult ret;
567  VkPhysicalDevice *devices = NULL;
568  VkPhysicalDeviceIDProperties *idp = NULL;
569  VkPhysicalDeviceProperties2 *prop = NULL;
570  AVVulkanDeviceContext *hwctx = ctx->hwctx;
571 
572  ret = vkEnumeratePhysicalDevices(hwctx->inst, &num, NULL);
573  if (ret != VK_SUCCESS || !num) {
574  av_log(ctx, AV_LOG_ERROR, "No devices found: %s!\n", vk_ret2str(ret));
575  return AVERROR(ENODEV);
576  }
577 
578  devices = av_malloc_array(num, sizeof(VkPhysicalDevice));
579  if (!devices)
580  return AVERROR(ENOMEM);
581 
582  ret = vkEnumeratePhysicalDevices(hwctx->inst, &num, devices);
583  if (ret != VK_SUCCESS) {
584  av_log(ctx, AV_LOG_ERROR, "Failed enumerating devices: %s\n",
585  vk_ret2str(ret));
586  err = AVERROR(ENODEV);
587  goto end;
588  }
589 
590  prop = av_mallocz_array(num, sizeof(*prop));
591  if (!prop) {
592  err = AVERROR(ENOMEM);
593  goto end;
594  }
595 
596  idp = av_mallocz_array(num, sizeof(*idp));
597  if (!idp) {
598  err = AVERROR(ENOMEM);
599  goto end;
600  }
601 
602  av_log(ctx, AV_LOG_VERBOSE, "GPU listing:\n");
603  for (int i = 0; i < num; i++) {
604  idp[i].sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES;
605  prop[i].sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
606  prop[i].pNext = &idp[i];
607 
608  vkGetPhysicalDeviceProperties2(devices[i], &prop[i]);
609  av_log(ctx, AV_LOG_VERBOSE, " %d: %s (%s) (0x%x)\n", i,
610  prop[i].properties.deviceName,
611  vk_dev_type(prop[i].properties.deviceType),
612  prop[i].properties.deviceID);
613  }
614 
615  if (select->has_uuid) {
616  for (int i = 0; i < num; i++) {
617  if (!strncmp(idp[i].deviceUUID, select->uuid, VK_UUID_SIZE)) {
618  choice = i;
619  goto end;
620  }
621  }
622  av_log(ctx, AV_LOG_ERROR, "Unable to find device by given UUID!\n");
623  err = AVERROR(ENODEV);
624  goto end;
625  } else if (select->name) {
626  av_log(ctx, AV_LOG_VERBOSE, "Requested device: %s\n", select->name);
627  for (int i = 0; i < num; i++) {
628  if (strstr(prop[i].properties.deviceName, select->name)) {
629  choice = i;
630  goto end;
631  }
632  }
633  av_log(ctx, AV_LOG_ERROR, "Unable to find device \"%s\"!\n",
634  select->name);
635  err = AVERROR(ENODEV);
636  goto end;
637  } else if (select->pci_device) {
638  av_log(ctx, AV_LOG_VERBOSE, "Requested device: 0x%x\n", select->pci_device);
639  for (int i = 0; i < num; i++) {
640  if (select->pci_device == prop[i].properties.deviceID) {
641  choice = i;
642  goto end;
643  }
644  }
645  av_log(ctx, AV_LOG_ERROR, "Unable to find device with PCI ID 0x%x!\n",
646  select->pci_device);
647  err = AVERROR(EINVAL);
648  goto end;
649  } else if (select->vendor_id) {
650  av_log(ctx, AV_LOG_VERBOSE, "Requested vendor: 0x%x\n", select->vendor_id);
651  for (int i = 0; i < num; i++) {
652  if (select->vendor_id == prop[i].properties.vendorID) {
653  choice = i;
654  goto end;
655  }
656  }
657  av_log(ctx, AV_LOG_ERROR, "Unable to find device with Vendor ID 0x%x!\n",
658  select->vendor_id);
659  err = AVERROR(ENODEV);
660  goto end;
661  } else {
662  if (select->index < num) {
663  choice = select->index;
664  goto end;
665  }
666  av_log(ctx, AV_LOG_ERROR, "Unable to find device with index %i!\n",
667  select->index);
668  err = AVERROR(ENODEV);
669  goto end;
670  }
671 
672 end:
673  if (choice > -1)
674  hwctx->phys_dev = devices[choice];
675 
676  av_free(devices);
677  av_free(prop);
678  av_free(idp);
679 
680  return err;
681 }
682 
683 static int search_queue_families(AVHWDeviceContext *ctx, VkDeviceCreateInfo *cd)
684 {
685  uint32_t num;
686  float *weights;
687  VkQueueFamilyProperties *qs = NULL;
688  AVVulkanDeviceContext *hwctx = ctx->hwctx;
689  int graph_index = -1, comp_index = -1, tx_index = -1;
690  VkDeviceQueueCreateInfo *pc = (VkDeviceQueueCreateInfo *)cd->pQueueCreateInfos;
691 
692  /* First get the number of queue families */
693  vkGetPhysicalDeviceQueueFamilyProperties(hwctx->phys_dev, &num, NULL);
694  if (!num) {
695  av_log(ctx, AV_LOG_ERROR, "Failed to get queues!\n");
696  return AVERROR_EXTERNAL;
697  }
698 
699  /* Then allocate memory */
700  qs = av_malloc_array(num, sizeof(VkQueueFamilyProperties));
701  if (!qs)
702  return AVERROR(ENOMEM);
703 
704  /* Finally retrieve the queue families */
705  vkGetPhysicalDeviceQueueFamilyProperties(hwctx->phys_dev, &num, qs);
706 
707 #define SEARCH_FLAGS(expr, out) \
708  for (int i = 0; i < num; i++) { \
709  const VkQueueFlagBits flags = qs[i].queueFlags; \
710  if (expr) { \
711  out = i; \
712  break; \
713  } \
714  }
715 
716  SEARCH_FLAGS(flags & VK_QUEUE_GRAPHICS_BIT, graph_index)
717 
718  SEARCH_FLAGS((flags & VK_QUEUE_COMPUTE_BIT) && (i != graph_index),
719  comp_index)
720 
721  SEARCH_FLAGS((flags & VK_QUEUE_TRANSFER_BIT) && (i != graph_index) &&
722  (i != comp_index), tx_index)
723 
724 #undef SEARCH_FLAGS
725 #define ADD_QUEUE(fidx, graph, comp, tx) \
726  av_log(ctx, AV_LOG_VERBOSE, "Using queue family %i (total queues: %i) for %s%s%s\n", \
727  fidx, qs[fidx].queueCount, graph ? "graphics " : "", \
728  comp ? "compute " : "", tx ? "transfers " : ""); \
729  av_log(ctx, AV_LOG_VERBOSE, " QF %i flags: %s%s%s%s\n", fidx, \
730  ((qs[fidx].queueFlags) & VK_QUEUE_GRAPHICS_BIT) ? "(graphics) " : "", \
731  ((qs[fidx].queueFlags) & VK_QUEUE_COMPUTE_BIT) ? "(compute) " : "", \
732  ((qs[fidx].queueFlags) & VK_QUEUE_TRANSFER_BIT) ? "(transfers) " : "", \
733  ((qs[fidx].queueFlags) & VK_QUEUE_SPARSE_BINDING_BIT) ? "(sparse) " : ""); \
734  pc[cd->queueCreateInfoCount].queueFamilyIndex = fidx; \
735  pc[cd->queueCreateInfoCount].queueCount = qs[fidx].queueCount; \
736  weights = av_malloc(qs[fidx].queueCount * sizeof(float)); \
737  pc[cd->queueCreateInfoCount].pQueuePriorities = weights; \
738  if (!weights) \
739  goto fail; \
740  for (int i = 0; i < qs[fidx].queueCount; i++) \
741  weights[i] = 1.0f; \
742  cd->queueCreateInfoCount++;
743 
744  ADD_QUEUE(graph_index, 1, comp_index < 0, tx_index < 0 && comp_index < 0)
745  hwctx->queue_family_index = graph_index;
746  hwctx->queue_family_comp_index = graph_index;
747  hwctx->queue_family_tx_index = graph_index;
748  hwctx->nb_graphics_queues = qs[graph_index].queueCount;
749 
750  if (comp_index != -1) {
751  ADD_QUEUE(comp_index, 0, 1, tx_index < 0)
752  hwctx->queue_family_tx_index = comp_index;
753  hwctx->queue_family_comp_index = comp_index;
754  hwctx->nb_comp_queues = qs[comp_index].queueCount;
755  }
756 
757  if (tx_index != -1) {
758  ADD_QUEUE(tx_index, 0, 0, 1)
759  hwctx->queue_family_tx_index = tx_index;
760  hwctx->nb_tx_queues = qs[tx_index].queueCount;
761  }
762 
763 #undef ADD_QUEUE
764  av_free(qs);
765 
766  return 0;
767 
768 fail:
769  av_freep(&pc[0].pQueuePriorities);
770  av_freep(&pc[1].pQueuePriorities);
771  av_freep(&pc[2].pQueuePriorities);
772  av_free(qs);
773 
774  return AVERROR(ENOMEM);
775 }
776 
778  int queue_family_index, int num_queues)
779 {
780  VkResult ret;
781  AVVulkanDeviceContext *hwctx = hwfc->device_ctx->hwctx;
782 
783  VkCommandPoolCreateInfo cqueue_create = {
784  .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
785  .flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
786  .queueFamilyIndex = queue_family_index,
787  };
788  VkCommandBufferAllocateInfo cbuf_create = {
789  .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
790  .level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
791  .commandBufferCount = num_queues,
792  };
793 
794  cmd->nb_queues = num_queues;
795 
796  /* Create command pool */
797  ret = vkCreateCommandPool(hwctx->act_dev, &cqueue_create,
798  hwctx->alloc, &cmd->pool);
799  if (ret != VK_SUCCESS) {
800  av_log(hwfc, AV_LOG_ERROR, "Command pool creation failure: %s\n",
801  vk_ret2str(ret));
802  return AVERROR_EXTERNAL;
803  }
804 
805  cmd->bufs = av_mallocz(num_queues * sizeof(*cmd->bufs));
806  if (!cmd->bufs)
807  return AVERROR(ENOMEM);
808 
809  cbuf_create.commandPool = cmd->pool;
810 
811  /* Allocate command buffer */
812  ret = vkAllocateCommandBuffers(hwctx->act_dev, &cbuf_create, cmd->bufs);
813  if (ret != VK_SUCCESS) {
814  av_log(hwfc, AV_LOG_ERROR, "Command buffer alloc failure: %s\n",
815  vk_ret2str(ret));
816  av_freep(&cmd->bufs);
817  return AVERROR_EXTERNAL;
818  }
819 
820  cmd->queues = av_mallocz(num_queues * sizeof(*cmd->queues));
821  if (!cmd->queues)
822  return AVERROR(ENOMEM);
823 
824  for (int i = 0; i < num_queues; i++) {
825  VulkanQueueCtx *q = &cmd->queues[i];
826  vkGetDeviceQueue(hwctx->act_dev, queue_family_index, i, &q->queue);
827  q->was_synchronous = 1;
828  }
829 
830  return 0;
831 }
832 
834 {
835  AVVulkanDeviceContext *hwctx = hwfc->device_ctx->hwctx;
836 
837  if (cmd->queues) {
838  for (int i = 0; i < cmd->nb_queues; i++) {
839  VulkanQueueCtx *q = &cmd->queues[i];
840 
841  /* Make sure all queues have finished executing */
842  if (q->fence && !q->was_synchronous) {
843  vkWaitForFences(hwctx->act_dev, 1, &q->fence, VK_TRUE, UINT64_MAX);
844  vkResetFences(hwctx->act_dev, 1, &q->fence);
845  }
846 
847  /* Free the fence */
848  if (q->fence)
849  vkDestroyFence(hwctx->act_dev, q->fence, hwctx->alloc);
850 
851  /* Free buffer dependencies */
852  for (int j = 0; j < q->nb_buf_deps; j++)
853  av_buffer_unref(&q->buf_deps[j]);
854  av_free(q->buf_deps);
855  }
856  }
857 
858  if (cmd->bufs)
859  vkFreeCommandBuffers(hwctx->act_dev, cmd->pool, cmd->nb_queues, cmd->bufs);
860  if (cmd->pool)
861  vkDestroyCommandPool(hwctx->act_dev, cmd->pool, hwctx->alloc);
862 
863  av_freep(&cmd->queues);
864  av_freep(&cmd->bufs);
865  cmd->pool = NULL;
866 }
867 
868 static VkCommandBuffer get_buf_exec_ctx(AVHWFramesContext *hwfc, VulkanExecCtx *cmd)
869 {
870  return cmd->bufs[cmd->cur_queue_idx];
871 }
872 
874 {
875  VulkanQueueCtx *q = &cmd->queues[cmd->cur_queue_idx];
876 
877  for (int j = 0; j < q->nb_buf_deps; j++)
878  av_buffer_unref(&q->buf_deps[j]);
879  q->nb_buf_deps = 0;
880 }
881 
883 {
884  VkResult ret;
885  AVVulkanDeviceContext *hwctx = hwfc->device_ctx->hwctx;
886  VulkanQueueCtx *q = &cmd->queues[cmd->cur_queue_idx];
887 
888  VkCommandBufferBeginInfo cmd_start = {
889  .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
890  .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
891  };
892 
893  /* Create the fence and don't wait for it initially */
894  if (!q->fence) {
895  VkFenceCreateInfo fence_spawn = {
896  .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
897  };
898  ret = vkCreateFence(hwctx->act_dev, &fence_spawn, hwctx->alloc,
899  &q->fence);
900  if (ret != VK_SUCCESS) {
901  av_log(hwfc, AV_LOG_ERROR, "Failed to queue frame fence: %s\n",
902  vk_ret2str(ret));
903  return AVERROR_EXTERNAL;
904  }
905  } else if (!q->was_synchronous) {
906  vkWaitForFences(hwctx->act_dev, 1, &q->fence, VK_TRUE, UINT64_MAX);
907  vkResetFences(hwctx->act_dev, 1, &q->fence);
908  }
909 
910  /* Discard queue dependencies */
911  unref_exec_ctx_deps(hwfc, cmd);
912 
913  ret = vkBeginCommandBuffer(cmd->bufs[cmd->cur_queue_idx], &cmd_start);
914  if (ret != VK_SUCCESS) {
915  av_log(hwfc, AV_LOG_ERROR, "Unable to init command buffer: %s\n",
916  vk_ret2str(ret));
917  return AVERROR_EXTERNAL;
918  }
919 
920  return 0;
921 }
922 
924  AVBufferRef * const *deps, int nb_deps)
925 {
926  AVBufferRef **dst;
927  VulkanQueueCtx *q = &cmd->queues[cmd->cur_queue_idx];
928 
929  if (!deps || !nb_deps)
930  return 0;
931 
933  (q->nb_buf_deps + nb_deps) * sizeof(*dst));
934  if (!dst)
935  goto err;
936 
937  q->buf_deps = dst;
938 
939  for (int i = 0; i < nb_deps; i++) {
940  q->buf_deps[q->nb_buf_deps] = av_buffer_ref(deps[i]);
941  if (!q->buf_deps[q->nb_buf_deps])
942  goto err;
943  q->nb_buf_deps++;
944  }
945 
946  return 0;
947 
948 err:
949  unref_exec_ctx_deps(hwfc, cmd);
950  return AVERROR(ENOMEM);
951 }
952 
954  VkSubmitInfo *s_info, int synchronous)
955 {
956  VkResult ret;
957  VulkanQueueCtx *q = &cmd->queues[cmd->cur_queue_idx];
958 
959  ret = vkEndCommandBuffer(cmd->bufs[cmd->cur_queue_idx]);
960  if (ret != VK_SUCCESS) {
961  av_log(hwfc, AV_LOG_ERROR, "Unable to finish command buffer: %s\n",
962  vk_ret2str(ret));
963  unref_exec_ctx_deps(hwfc, cmd);
964  return AVERROR_EXTERNAL;
965  }
966 
967  s_info->pCommandBuffers = &cmd->bufs[cmd->cur_queue_idx];
968  s_info->commandBufferCount = 1;
969 
970  ret = vkQueueSubmit(q->queue, 1, s_info, q->fence);
971  if (ret != VK_SUCCESS) {
972  unref_exec_ctx_deps(hwfc, cmd);
973  return AVERROR_EXTERNAL;
974  }
975 
976  q->was_synchronous = synchronous;
977 
978  if (synchronous) {
979  AVVulkanDeviceContext *hwctx = hwfc->device_ctx->hwctx;
980  vkWaitForFences(hwctx->act_dev, 1, &q->fence, VK_TRUE, UINT64_MAX);
981  vkResetFences(hwctx->act_dev, 1, &q->fence);
982  unref_exec_ctx_deps(hwfc, cmd);
983  } else { /* Rotate queues */
984  cmd->cur_queue_idx = (cmd->cur_queue_idx + 1) % cmd->nb_queues;
985  }
986 
987  return 0;
988 }
989 
991 {
992  VulkanDevicePriv *p = ctx->internal->priv;
993  AVVulkanDeviceContext *hwctx = ctx->hwctx;
994 
995  vkDestroyDevice(hwctx->act_dev, hwctx->alloc);
996 
997  if (p->debug_ctx) {
998  VK_LOAD_PFN(hwctx->inst, vkDestroyDebugUtilsMessengerEXT);
999  pfn_vkDestroyDebugUtilsMessengerEXT(hwctx->inst, p->debug_ctx,
1000  hwctx->alloc);
1001  }
1002 
1003  vkDestroyInstance(hwctx->inst, hwctx->alloc);
1004 
1005  for (int i = 0; i < hwctx->nb_enabled_inst_extensions; i++)
1006  av_free((void *)hwctx->enabled_inst_extensions[i]);
1007  av_free((void *)hwctx->enabled_inst_extensions);
1008 
1009  for (int i = 0; i < hwctx->nb_enabled_dev_extensions; i++)
1010  av_free((void *)hwctx->enabled_dev_extensions[i]);
1011  av_free((void *)hwctx->enabled_dev_extensions);
1012 }
1013 
1015  VulkanDeviceSelection *dev_select,
1016  AVDictionary *opts, int flags)
1017 {
1018  int err = 0;
1019  VkResult ret;
1020  AVDictionaryEntry *opt_d;
1021  VulkanDevicePriv *p = ctx->internal->priv;
1022  AVVulkanDeviceContext *hwctx = ctx->hwctx;
1023  VkPhysicalDeviceFeatures dev_features = { 0 };
1024  VkDeviceQueueCreateInfo queue_create_info[3] = {
1025  { .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, },
1026  { .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, },
1027  { .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, },
1028  };
1029 
1030  VkDeviceCreateInfo dev_info = {
1031  .sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
1032  .pNext = &hwctx->device_features,
1033  .pQueueCreateInfos = queue_create_info,
1034  .queueCreateInfoCount = 0,
1035  };
1036 
1037  hwctx->device_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
1038  ctx->free = vulkan_device_free;
1039 
1040  /* Create an instance if not given one */
1041  if ((err = create_instance(ctx, opts)))
1042  goto end;
1043 
1044  /* Find a device (if not given one) */
1045  if ((err = find_device(ctx, dev_select)))
1046  goto end;
1047 
1048  vkGetPhysicalDeviceFeatures(hwctx->phys_dev, &dev_features);
1049 #define COPY_FEATURE(DST, NAME) (DST).features.NAME = dev_features.NAME;
1050  COPY_FEATURE(hwctx->device_features, shaderImageGatherExtended)
1051  COPY_FEATURE(hwctx->device_features, shaderStorageImageReadWithoutFormat)
1052  COPY_FEATURE(hwctx->device_features, shaderStorageImageWriteWithoutFormat)
1053  COPY_FEATURE(hwctx->device_features, fragmentStoresAndAtomics)
1054  COPY_FEATURE(hwctx->device_features, vertexPipelineStoresAndAtomics)
1055  COPY_FEATURE(hwctx->device_features, shaderInt64)
1056 #undef COPY_FEATURE
1057 
1058  /* Search queue family */
1059  if ((err = search_queue_families(ctx, &dev_info)))
1060  goto end;
1061 
1062  if ((err = check_extensions(ctx, 1, opts, &dev_info.ppEnabledExtensionNames,
1063  &dev_info.enabledExtensionCount, 0))) {
1064  av_free((void *)queue_create_info[0].pQueuePriorities);
1065  av_free((void *)queue_create_info[1].pQueuePriorities);
1066  av_free((void *)queue_create_info[2].pQueuePriorities);
1067  goto end;
1068  }
1069 
1070  ret = vkCreateDevice(hwctx->phys_dev, &dev_info, hwctx->alloc,
1071  &hwctx->act_dev);
1072 
1073  av_free((void *)queue_create_info[0].pQueuePriorities);
1074  av_free((void *)queue_create_info[1].pQueuePriorities);
1075  av_free((void *)queue_create_info[2].pQueuePriorities);
1076 
1077  if (ret != VK_SUCCESS) {
1078  av_log(ctx, AV_LOG_ERROR, "Device creation failure: %s\n",
1079  vk_ret2str(ret));
1080  for (int i = 0; i < dev_info.enabledExtensionCount; i++)
1081  av_free((void *)dev_info.ppEnabledExtensionNames[i]);
1082  av_free((void *)dev_info.ppEnabledExtensionNames);
1083  err = AVERROR_EXTERNAL;
1084  goto end;
1085  }
1086 
1087  /* Tiled images setting, use them by default */
1088  opt_d = av_dict_get(opts, "linear_images", NULL, 0);
1089  if (opt_d)
1090  p->use_linear_images = strtol(opt_d->value, NULL, 10);
1091 
1092  hwctx->enabled_dev_extensions = dev_info.ppEnabledExtensionNames;
1093  hwctx->nb_enabled_dev_extensions = dev_info.enabledExtensionCount;
1094 
1095 end:
1096  return err;
1097 }
1098 
1100 {
1101  uint32_t queue_num;
1102  AVVulkanDeviceContext *hwctx = ctx->hwctx;
1103  VulkanDevicePriv *p = ctx->internal->priv;
1104 
1105  /* Set device extension flags */
1106  for (int i = 0; i < hwctx->nb_enabled_dev_extensions; i++) {
1107  for (int j = 0; j < FF_ARRAY_ELEMS(optional_device_exts); j++) {
1108  if (!strcmp(hwctx->enabled_dev_extensions[i],
1109  optional_device_exts[j].name)) {
1110  av_log(ctx, AV_LOG_VERBOSE, "Using device extension %s\n",
1111  hwctx->enabled_dev_extensions[i]);
1113  break;
1114  }
1115  }
1116  }
1117 
1118  p->props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
1119  p->props.pNext = &p->hprops;
1120  p->hprops.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_MEMORY_HOST_PROPERTIES_EXT;
1121 
1122  vkGetPhysicalDeviceProperties2(hwctx->phys_dev, &p->props);
1123  av_log(ctx, AV_LOG_VERBOSE, "Using device: %s\n",
1124  p->props.properties.deviceName);
1125  av_log(ctx, AV_LOG_VERBOSE, "Alignments:\n");
1126  av_log(ctx, AV_LOG_VERBOSE, " optimalBufferCopyRowPitchAlignment: %"PRIu64"\n",
1127  p->props.properties.limits.optimalBufferCopyRowPitchAlignment);
1128  av_log(ctx, AV_LOG_VERBOSE, " minMemoryMapAlignment: %"SIZE_SPECIFIER"\n",
1129  p->props.properties.limits.minMemoryMapAlignment);
1131  av_log(ctx, AV_LOG_VERBOSE, " minImportedHostPointerAlignment: %"PRIu64"\n",
1132  p->hprops.minImportedHostPointerAlignment);
1133 
1134  p->dev_is_nvidia = (p->props.properties.vendorID == 0x10de);
1135 
1136  vkGetPhysicalDeviceQueueFamilyProperties(hwctx->phys_dev, &queue_num, NULL);
1137  if (!queue_num) {
1138  av_log(ctx, AV_LOG_ERROR, "Failed to get queues!\n");
1139  return AVERROR_EXTERNAL;
1140  }
1141 
1142 #define CHECK_QUEUE(type, n) \
1143 if (n >= queue_num) { \
1144  av_log(ctx, AV_LOG_ERROR, "Invalid %s queue index %i (device has %i queues)!\n", \
1145  type, n, queue_num); \
1146  return AVERROR(EINVAL); \
1147 }
1148 
1149  CHECK_QUEUE("graphics", hwctx->queue_family_index)
1150  CHECK_QUEUE("upload", hwctx->queue_family_tx_index)
1151  CHECK_QUEUE("compute", hwctx->queue_family_comp_index)
1152 
1153 #undef CHECK_QUEUE
1154 
1155  p->qfs[p->num_qfs++] = hwctx->queue_family_index;
1156  if ((hwctx->queue_family_tx_index != hwctx->queue_family_index) &&
1158  p->qfs[p->num_qfs++] = hwctx->queue_family_tx_index;
1159  if ((hwctx->queue_family_comp_index != hwctx->queue_family_index) &&
1161  p->qfs[p->num_qfs++] = hwctx->queue_family_comp_index;
1162 
1163  /* Get device capabilities */
1164  vkGetPhysicalDeviceMemoryProperties(hwctx->phys_dev, &p->mprops);
1165 
1166  return 0;
1167 }
1168 
1169 static int vulkan_device_create(AVHWDeviceContext *ctx, const char *device,
1170  AVDictionary *opts, int flags)
1171 {
1172  VulkanDeviceSelection dev_select = { 0 };
1173  if (device && device[0]) {
1174  char *end = NULL;
1175  dev_select.index = strtol(device, &end, 10);
1176  if (end == device) {
1177  dev_select.index = 0;
1178  dev_select.name = device;
1179  }
1180  }
1181 
1182  return vulkan_device_create_internal(ctx, &dev_select, opts, flags);
1183 }
1184 
1186  AVHWDeviceContext *src_ctx,
1187  AVDictionary *opts, int flags)
1188 {
1189  av_unused VulkanDeviceSelection dev_select = { 0 };
1190 
1191  /* If there's only one device on the system, then even if its not covered
1192  * by the following checks (e.g. non-PCIe ARM GPU), having an empty
1193  * dev_select will mean it'll get picked. */
1194  switch(src_ctx->type) {
1195 #if CONFIG_LIBDRM
1196 #if CONFIG_VAAPI
1197  case AV_HWDEVICE_TYPE_VAAPI: {
1198  AVVAAPIDeviceContext *src_hwctx = src_ctx->hwctx;
1199 
1200  const char *vendor = vaQueryVendorString(src_hwctx->display);
1201  if (!vendor) {
1202  av_log(ctx, AV_LOG_ERROR, "Unable to get device info from VAAPI!\n");
1203  return AVERROR_EXTERNAL;
1204  }
1205 
1206  if (strstr(vendor, "Intel"))
1207  dev_select.vendor_id = 0x8086;
1208  if (strstr(vendor, "AMD"))
1209  dev_select.vendor_id = 0x1002;
1210 
1211  return vulkan_device_create_internal(ctx, &dev_select, opts, flags);
1212  }
1213 #endif
1214  case AV_HWDEVICE_TYPE_DRM: {
1215  AVDRMDeviceContext *src_hwctx = src_ctx->hwctx;
1216 
1217  drmDevice *drm_dev_info;
1218  int err = drmGetDevice(src_hwctx->fd, &drm_dev_info);
1219  if (err) {
1220  av_log(ctx, AV_LOG_ERROR, "Unable to get device info from DRM fd!\n");
1221  return AVERROR_EXTERNAL;
1222  }
1223 
1224  if (drm_dev_info->bustype == DRM_BUS_PCI)
1225  dev_select.pci_device = drm_dev_info->deviceinfo.pci->device_id;
1226 
1227  drmFreeDevice(&drm_dev_info);
1228 
1229  return vulkan_device_create_internal(ctx, &dev_select, opts, flags);
1230  }
1231 #endif
1232 #if CONFIG_CUDA
1233  case AV_HWDEVICE_TYPE_CUDA: {
1234  AVHWDeviceContext *cuda_cu = src_ctx;
1235  AVCUDADeviceContext *src_hwctx = src_ctx->hwctx;
1236  AVCUDADeviceContextInternal *cu_internal = src_hwctx->internal;
1237  CudaFunctions *cu = cu_internal->cuda_dl;
1238 
1239  int ret = CHECK_CU(cu->cuDeviceGetUuid((CUuuid *)&dev_select.uuid,
1240  cu_internal->cuda_device));
1241  if (ret < 0) {
1242  av_log(ctx, AV_LOG_ERROR, "Unable to get UUID from CUDA!\n");
1243  return AVERROR_EXTERNAL;
1244  }
1245 
1246  dev_select.has_uuid = 1;
1247 
1248  return vulkan_device_create_internal(ctx, &dev_select, opts, flags);
1249  }
1250 #endif
1251  default:
1252  return AVERROR(ENOSYS);
1253  }
1254 }
1255 
1257  const void *hwconfig,
1258  AVHWFramesConstraints *constraints)
1259 {
1260  int count = 0;
1261  AVVulkanDeviceContext *hwctx = ctx->hwctx;
1262  VulkanDevicePriv *p = ctx->internal->priv;
1263 
1264  for (enum AVPixelFormat i = 0; i < AV_PIX_FMT_NB; i++)
1265  count += pixfmt_is_supported(hwctx, i, p->use_linear_images);
1266 
1267 #if CONFIG_CUDA
1268  if (p->dev_is_nvidia)
1269  count++;
1270 #endif
1271 
1272  constraints->valid_sw_formats = av_malloc_array(count + 1,
1273  sizeof(enum AVPixelFormat));
1274  if (!constraints->valid_sw_formats)
1275  return AVERROR(ENOMEM);
1276 
1277  count = 0;
1278  for (enum AVPixelFormat i = 0; i < AV_PIX_FMT_NB; i++)
1279  if (pixfmt_is_supported(hwctx, i, p->use_linear_images))
1280  constraints->valid_sw_formats[count++] = i;
1281 
1282 #if CONFIG_CUDA
1283  if (p->dev_is_nvidia)
1284  constraints->valid_sw_formats[count++] = AV_PIX_FMT_CUDA;
1285 #endif
1286  constraints->valid_sw_formats[count++] = AV_PIX_FMT_NONE;
1287 
1288  constraints->min_width = 0;
1289  constraints->min_height = 0;
1290  constraints->max_width = p->props.properties.limits.maxImageDimension2D;
1291  constraints->max_height = p->props.properties.limits.maxImageDimension2D;
1292 
1293  constraints->valid_hw_formats = av_malloc_array(2, sizeof(enum AVPixelFormat));
1294  if (!constraints->valid_hw_formats)
1295  return AVERROR(ENOMEM);
1296 
1297  constraints->valid_hw_formats[0] = AV_PIX_FMT_VULKAN;
1298  constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
1299 
1300  return 0;
1301 }
1302 
1303 static int alloc_mem(AVHWDeviceContext *ctx, VkMemoryRequirements *req,
1304  VkMemoryPropertyFlagBits req_flags, const void *alloc_extension,
1305  VkMemoryPropertyFlagBits *mem_flags, VkDeviceMemory *mem)
1306 {
1307  VkResult ret;
1308  int index = -1;
1309  VulkanDevicePriv *p = ctx->internal->priv;
1310  AVVulkanDeviceContext *dev_hwctx = ctx->hwctx;
1311  VkMemoryAllocateInfo alloc_info = {
1312  .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
1313  .pNext = alloc_extension,
1314  .allocationSize = req->size,
1315  };
1316 
1317  /* The vulkan spec requires memory types to be sorted in the "optimal"
1318  * order, so the first matching type we find will be the best/fastest one */
1319  for (int i = 0; i < p->mprops.memoryTypeCount; i++) {
1320  const VkMemoryType *type = &p->mprops.memoryTypes[i];
1321 
1322  /* The memory type must be supported by the requirements (bitfield) */
1323  if (!(req->memoryTypeBits & (1 << i)))
1324  continue;
1325 
1326  /* The memory type flags must include our properties */
1327  if ((type->propertyFlags & req_flags) != req_flags)
1328  continue;
1329 
1330  /* The memory type must be large enough */
1331  if (req->size > p->mprops.memoryHeaps[type->heapIndex].size)
1332  continue;
1333 
1334  /* Found a suitable memory type */
1335  index = i;
1336  break;
1337  }
1338 
1339  if (index < 0) {
1340  av_log(ctx, AV_LOG_ERROR, "No memory type found for flags 0x%x\n",
1341  req_flags);
1342  return AVERROR(EINVAL);
1343  }
1344 
1345  alloc_info.memoryTypeIndex = index;
1346 
1347  ret = vkAllocateMemory(dev_hwctx->act_dev, &alloc_info,
1348  dev_hwctx->alloc, mem);
1349  if (ret != VK_SUCCESS) {
1350  av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory: %s\n",
1351  vk_ret2str(ret));
1352  return AVERROR(ENOMEM);
1353  }
1354 
1355  *mem_flags |= p->mprops.memoryTypes[index].propertyFlags;
1356 
1357  return 0;
1358 }
1359 
1361 {
1362  if (!internal)
1363  return;
1364 
1365 #if CONFIG_CUDA
1366  if (internal->cuda_fc_ref) {
1367  AVHWFramesContext *cuda_fc = (AVHWFramesContext *)internal->cuda_fc_ref->data;
1368  int planes = av_pix_fmt_count_planes(cuda_fc->sw_format);
1369  AVHWDeviceContext *cuda_cu = cuda_fc->device_ctx;
1370  AVCUDADeviceContext *cuda_dev = cuda_cu->hwctx;
1371  AVCUDADeviceContextInternal *cu_internal = cuda_dev->internal;
1372  CudaFunctions *cu = cu_internal->cuda_dl;
1373 
1374  for (int i = 0; i < planes; i++) {
1375  if (internal->cu_sem[i])
1376  CHECK_CU(cu->cuDestroyExternalSemaphore(internal->cu_sem[i]));
1377  if (internal->cu_mma[i])
1378  CHECK_CU(cu->cuMipmappedArrayDestroy(internal->cu_mma[i]));
1379  if (internal->ext_mem[i])
1380  CHECK_CU(cu->cuDestroyExternalMemory(internal->ext_mem[i]));
1381  }
1382 
1383  av_buffer_unref(&internal->cuda_fc_ref);
1384  }
1385 #endif
1386 
1387  av_free(internal);
1388 }
1389 
1390 static void vulkan_frame_free(void *opaque, uint8_t *data)
1391 {
1392  AVVkFrame *f = (AVVkFrame *)data;
1393  AVHWFramesContext *hwfc = opaque;
1394  AVVulkanDeviceContext *hwctx = hwfc->device_ctx->hwctx;
1396 
1397  vulkan_free_internal(f->internal);
1398 
1399  for (int i = 0; i < planes; i++) {
1400  vkDestroyImage(hwctx->act_dev, f->img[i], hwctx->alloc);
1401  vkFreeMemory(hwctx->act_dev, f->mem[i], hwctx->alloc);
1402  vkDestroySemaphore(hwctx->act_dev, f->sem[i], hwctx->alloc);
1403  }
1404 
1405  av_free(f);
1406 }
1407 
1409  void *alloc_pnext, size_t alloc_pnext_stride)
1410 {
1411  int err;
1412  VkResult ret;
1413  AVHWDeviceContext *ctx = hwfc->device_ctx;
1414  VulkanDevicePriv *p = ctx->internal->priv;
1415  const int planes = av_pix_fmt_count_planes(hwfc->sw_format);
1416  VkBindImageMemoryInfo bind_info[AV_NUM_DATA_POINTERS] = { { 0 } };
1417 
1418  AVVulkanDeviceContext *hwctx = ctx->hwctx;
1419 
1420  for (int i = 0; i < planes; i++) {
1421  int use_ded_mem;
1422  VkImageMemoryRequirementsInfo2 req_desc = {
1423  .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
1424  .image = f->img[i],
1425  };
1426  VkMemoryDedicatedAllocateInfo ded_alloc = {
1427  .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
1428  .pNext = (void *)(((uint8_t *)alloc_pnext) + i*alloc_pnext_stride),
1429  };
1430  VkMemoryDedicatedRequirements ded_req = {
1431  .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS,
1432  };
1433  VkMemoryRequirements2 req = {
1434  .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
1435  .pNext = &ded_req,
1436  };
1437 
1438  vkGetImageMemoryRequirements2(hwctx->act_dev, &req_desc, &req);
1439 
1440  if (f->tiling == VK_IMAGE_TILING_LINEAR)
1441  req.memoryRequirements.size = FFALIGN(req.memoryRequirements.size,
1442  p->props.properties.limits.minMemoryMapAlignment);
1443 
1444  /* In case the implementation prefers/requires dedicated allocation */
1445  use_ded_mem = ded_req.prefersDedicatedAllocation |
1446  ded_req.requiresDedicatedAllocation;
1447  if (use_ded_mem)
1448  ded_alloc.image = f->img[i];
1449 
1450  /* Allocate memory */
1451  if ((err = alloc_mem(ctx, &req.memoryRequirements,
1452  f->tiling == VK_IMAGE_TILING_LINEAR ?
1453  VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT :
1454  VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
1455  use_ded_mem ? &ded_alloc : (void *)ded_alloc.pNext,
1456  &f->flags, &f->mem[i])))
1457  return err;
1458 
1459  f->size[i] = req.memoryRequirements.size;
1460  bind_info[i].sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO;
1461  bind_info[i].image = f->img[i];
1462  bind_info[i].memory = f->mem[i];
1463  }
1464 
1465  /* Bind the allocated memory to the images */
1466  ret = vkBindImageMemory2(hwctx->act_dev, planes, bind_info);
1467  if (ret != VK_SUCCESS) {
1468  av_log(ctx, AV_LOG_ERROR, "Failed to bind memory: %s\n",
1469  vk_ret2str(ret));
1470  return AVERROR_EXTERNAL;
1471  }
1472 
1473  return 0;
1474 }
1475 
1476 enum PrepMode {
1480 };
1481 
1483  AVVkFrame *frame, enum PrepMode pmode)
1484 {
1485  int err;
1486  uint32_t dst_qf;
1487  VkImageLayout new_layout;
1488  VkAccessFlags new_access;
1489  const int planes = av_pix_fmt_count_planes(hwfc->sw_format);
1490 
1491  VkImageMemoryBarrier img_bar[AV_NUM_DATA_POINTERS] = { 0 };
1492 
1493  VkSubmitInfo s_info = {
1494  .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
1495  .pSignalSemaphores = frame->sem,
1496  .signalSemaphoreCount = planes,
1497  };
1498 
1499  VkPipelineStageFlagBits wait_st[AV_NUM_DATA_POINTERS];
1500  for (int i = 0; i < planes; i++)
1501  wait_st[i] = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
1502 
1503  switch (pmode) {
1504  case PREP_MODE_WRITE:
1505  new_layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
1506  new_access = VK_ACCESS_TRANSFER_WRITE_BIT;
1507  dst_qf = VK_QUEUE_FAMILY_IGNORED;
1508  break;
1509  case PREP_MODE_RO_SHADER:
1510  new_layout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
1511  new_access = VK_ACCESS_TRANSFER_READ_BIT;
1512  dst_qf = VK_QUEUE_FAMILY_IGNORED;
1513  break;
1515  new_layout = VK_IMAGE_LAYOUT_GENERAL;
1516  new_access = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT;
1517  dst_qf = VK_QUEUE_FAMILY_EXTERNAL_KHR;
1518  s_info.pWaitSemaphores = frame->sem;
1519  s_info.pWaitDstStageMask = wait_st;
1520  s_info.waitSemaphoreCount = planes;
1521  break;
1522  }
1523 
1524  if ((err = wait_start_exec_ctx(hwfc, ectx)))
1525  return err;
1526 
1527  /* Change the image layout to something more optimal for writes.
1528  * This also signals the newly created semaphore, making it usable
1529  * for synchronization */
1530  for (int i = 0; i < planes; i++) {
1531  img_bar[i].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
1532  img_bar[i].srcAccessMask = 0x0;
1533  img_bar[i].dstAccessMask = new_access;
1534  img_bar[i].oldLayout = frame->layout[i];
1535  img_bar[i].newLayout = new_layout;
1536  img_bar[i].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
1537  img_bar[i].dstQueueFamilyIndex = dst_qf;
1538  img_bar[i].image = frame->img[i];
1539  img_bar[i].subresourceRange.levelCount = 1;
1540  img_bar[i].subresourceRange.layerCount = 1;
1541  img_bar[i].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
1542 
1543  frame->layout[i] = img_bar[i].newLayout;
1544  frame->access[i] = img_bar[i].dstAccessMask;
1545  }
1546 
1547  vkCmdPipelineBarrier(get_buf_exec_ctx(hwfc, ectx),
1548  VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
1549  VK_PIPELINE_STAGE_TRANSFER_BIT,
1550  0, 0, NULL, 0, NULL, planes, img_bar);
1551 
1552  return submit_exec_ctx(hwfc, ectx, &s_info, 0);
1553 }
1554 
1555 static inline void get_plane_wh(int *w, int *h, enum AVPixelFormat format,
1556  int frame_w, int frame_h, int plane)
1557 {
1559 
1560  /* Currently always true unless gray + alpha support is added */
1561  if (!plane || (plane == 3) || desc->flags & AV_PIX_FMT_FLAG_RGB ||
1562  !(desc->flags & AV_PIX_FMT_FLAG_PLANAR)) {
1563  *w = frame_w;
1564  *h = frame_h;
1565  return;
1566  }
1567 
1568  *w = AV_CEIL_RSHIFT(frame_w, desc->log2_chroma_w);
1569  *h = AV_CEIL_RSHIFT(frame_h, desc->log2_chroma_h);
1570 }
1571 
1573  VkImageTiling tiling, VkImageUsageFlagBits usage,
1574  void *create_pnext)
1575 {
1576  int err;
1577  VkResult ret;
1578  AVHWDeviceContext *ctx = hwfc->device_ctx;
1579  VulkanDevicePriv *p = ctx->internal->priv;
1580  AVVulkanDeviceContext *hwctx = ctx->hwctx;
1581  enum AVPixelFormat format = hwfc->sw_format;
1582  const VkFormat *img_fmts = av_vkfmt_from_pixfmt(format);
1583  const int planes = av_pix_fmt_count_planes(format);
1584 
1585  VkExportSemaphoreCreateInfo ext_sem_info = {
1586  .sType = VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO,
1587  .handleTypes = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT,
1588  };
1589 
1590  VkSemaphoreCreateInfo sem_spawn = {
1591  .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
1592  .pNext = p->extensions & EXT_EXTERNAL_FD_SEM ? &ext_sem_info : NULL,
1593  };
1594 
1596  if (!f) {
1597  av_log(ctx, AV_LOG_ERROR, "Unable to allocate memory for AVVkFrame!\n");
1598  return AVERROR(ENOMEM);
1599  }
1600 
1601  /* Create the images */
1602  for (int i = 0; i < planes; i++) {
1603  VkImageCreateInfo create_info = {
1604  .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1605  .pNext = create_pnext,
1606  .imageType = VK_IMAGE_TYPE_2D,
1607  .format = img_fmts[i],
1608  .extent.depth = 1,
1609  .mipLevels = 1,
1610  .arrayLayers = 1,
1611  .flags = VK_IMAGE_CREATE_ALIAS_BIT,
1612  .tiling = tiling,
1613  .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1614  .usage = usage,
1615  .samples = VK_SAMPLE_COUNT_1_BIT,
1616  .pQueueFamilyIndices = p->qfs,
1617  .queueFamilyIndexCount = p->num_qfs,
1618  .sharingMode = p->num_qfs > 1 ? VK_SHARING_MODE_CONCURRENT :
1619  VK_SHARING_MODE_EXCLUSIVE,
1620  };
1621 
1622  get_plane_wh(&create_info.extent.width, &create_info.extent.height,
1623  format, hwfc->width, hwfc->height, i);
1624 
1625  ret = vkCreateImage(hwctx->act_dev, &create_info,
1626  hwctx->alloc, &f->img[i]);
1627  if (ret != VK_SUCCESS) {
1628  av_log(ctx, AV_LOG_ERROR, "Image creation failure: %s\n",
1629  vk_ret2str(ret));
1630  err = AVERROR(EINVAL);
1631  goto fail;
1632  }
1633 
1634  /* Create semaphore */
1635  ret = vkCreateSemaphore(hwctx->act_dev, &sem_spawn,
1636  hwctx->alloc, &f->sem[i]);
1637  if (ret != VK_SUCCESS) {
1638  av_log(hwctx, AV_LOG_ERROR, "Failed to create semaphore: %s\n",
1639  vk_ret2str(ret));
1640  return AVERROR_EXTERNAL;
1641  }
1642 
1643  f->layout[i] = create_info.initialLayout;
1644  f->access[i] = 0x0;
1645  }
1646 
1647  f->flags = 0x0;
1648  f->tiling = tiling;
1649 
1650  *frame = f;
1651  return 0;
1652 
1653 fail:
1654  vulkan_frame_free(hwfc, (uint8_t *)f);
1655  return err;
1656 }
1657 
1658 /* Checks if an export flag is enabled, and if it is ORs it with *iexp */
1660  VkExternalMemoryHandleTypeFlags *comp_handle_types,
1661  VkExternalMemoryHandleTypeFlagBits *iexp,
1662  VkExternalMemoryHandleTypeFlagBits exp)
1663 {
1664  VkResult ret;
1665  AVVulkanFramesContext *hwctx = hwfc->hwctx;
1666  AVVulkanDeviceContext *dev_hwctx = hwfc->device_ctx->hwctx;
1667  VkExternalImageFormatProperties eprops = {
1668  .sType = VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES_KHR,
1669  };
1670  VkImageFormatProperties2 props = {
1671  .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
1672  .pNext = &eprops,
1673  };
1674  VkPhysicalDeviceExternalImageFormatInfo enext = {
1675  .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO,
1676  .handleType = exp,
1677  };
1678  VkPhysicalDeviceImageFormatInfo2 pinfo = {
1679  .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
1680  .pNext = !exp ? NULL : &enext,
1681  .format = av_vkfmt_from_pixfmt(hwfc->sw_format)[0],
1682  .type = VK_IMAGE_TYPE_2D,
1683  .tiling = hwctx->tiling,
1684  .usage = hwctx->usage,
1685  .flags = VK_IMAGE_CREATE_ALIAS_BIT,
1686  };
1687 
1688  ret = vkGetPhysicalDeviceImageFormatProperties2(dev_hwctx->phys_dev,
1689  &pinfo, &props);
1690  if (ret == VK_SUCCESS) {
1691  *iexp |= exp;
1692  *comp_handle_types |= eprops.externalMemoryProperties.compatibleHandleTypes;
1693  }
1694 }
1695 
1697 {
1698  int err;
1699  AVVkFrame *f;
1700  AVBufferRef *avbuf = NULL;
1701  AVHWFramesContext *hwfc = opaque;
1702  AVVulkanFramesContext *hwctx = hwfc->hwctx;
1704  VulkanFramesPriv *fp = hwfc->internal->priv;
1705  VkExportMemoryAllocateInfo eminfo[AV_NUM_DATA_POINTERS];
1706  VkExternalMemoryHandleTypeFlags e = 0x0;
1707 
1708  VkExternalMemoryImageCreateInfo eiinfo = {
1709  .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
1710  .pNext = hwctx->create_pnext,
1711  };
1712 
1714  try_export_flags(hwfc, &eiinfo.handleTypes, &e,
1715  VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT);
1716 
1718  try_export_flags(hwfc, &eiinfo.handleTypes, &e,
1719  VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
1720 
1721  for (int i = 0; i < av_pix_fmt_count_planes(hwfc->sw_format); i++) {
1722  eminfo[i].sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO;
1723  eminfo[i].pNext = hwctx->alloc_pnext[i];
1724  eminfo[i].handleTypes = e;
1725  }
1726 
1727  err = create_frame(hwfc, &f, hwctx->tiling, hwctx->usage,
1728  eiinfo.handleTypes ? &eiinfo : NULL);
1729  if (err)
1730  return NULL;
1731 
1732  err = alloc_bind_mem(hwfc, f, eminfo, sizeof(*eminfo));
1733  if (err)
1734  goto fail;
1735 
1736  err = prepare_frame(hwfc, &fp->conv_ctx, f, PREP_MODE_WRITE);
1737  if (err)
1738  goto fail;
1739 
1740  avbuf = av_buffer_create((uint8_t *)f, sizeof(AVVkFrame),
1741  vulkan_frame_free, hwfc, 0);
1742  if (!avbuf)
1743  goto fail;
1744 
1745  return avbuf;
1746 
1747 fail:
1748  vulkan_frame_free(hwfc, (uint8_t *)f);
1749  return NULL;
1750 }
1751 
1753 {
1754  VulkanFramesPriv *fp = hwfc->internal->priv;
1755 
1756  free_exec_ctx(hwfc, &fp->conv_ctx);
1757  free_exec_ctx(hwfc, &fp->upload_ctx);
1758  free_exec_ctx(hwfc, &fp->download_ctx);
1759 }
1760 
1762 {
1763  int err;
1764  AVVkFrame *f;
1765  AVVulkanFramesContext *hwctx = hwfc->hwctx;
1766  VulkanFramesPriv *fp = hwfc->internal->priv;
1767  AVVulkanDeviceContext *dev_hwctx = hwfc->device_ctx->hwctx;
1769 
1770  /* Default pool flags */
1771  hwctx->tiling = hwctx->tiling ? hwctx->tiling : p->use_linear_images ?
1772  VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL;
1773 
1774  if (!hwctx->usage)
1775  hwctx->usage = DEFAULT_USAGE_FLAGS;
1776 
1777  err = create_exec_ctx(hwfc, &fp->conv_ctx,
1778  dev_hwctx->queue_family_comp_index,
1779  GET_QUEUE_COUNT(dev_hwctx, 0, 1, 0));
1780  if (err)
1781  return err;
1782 
1783  err = create_exec_ctx(hwfc, &fp->upload_ctx,
1784  dev_hwctx->queue_family_tx_index,
1785  GET_QUEUE_COUNT(dev_hwctx, 0, 0, 1));
1786  if (err)
1787  return err;
1788 
1789  err = create_exec_ctx(hwfc, &fp->download_ctx,
1790  dev_hwctx->queue_family_tx_index, 1);
1791  if (err)
1792  return err;
1793 
1794  /* Test to see if allocation will fail */
1795  err = create_frame(hwfc, &f, hwctx->tiling, hwctx->usage,
1796  hwctx->create_pnext);
1797  if (err)
1798  return err;
1799 
1800  vulkan_frame_free(hwfc, (uint8_t *)f);
1801 
1802  /* If user did not specify a pool, hwfc->pool will be set to the internal one
1803  * in hwcontext.c just after this gets called */
1804  if (!hwfc->pool) {
1806  hwfc, vulkan_pool_alloc,
1807  NULL);
1808  if (!hwfc->internal->pool_internal)
1809  return AVERROR(ENOMEM);
1810  }
1811 
1812  return 0;
1813 }
1814 
1816 {
1817  frame->buf[0] = av_buffer_pool_get(hwfc->pool);
1818  if (!frame->buf[0])
1819  return AVERROR(ENOMEM);
1820 
1821  frame->data[0] = frame->buf[0]->data;
1823  frame->width = hwfc->width;
1824  frame->height = hwfc->height;
1825 
1826  return 0;
1827 }
1828 
1830  enum AVHWFrameTransferDirection dir,
1831  enum AVPixelFormat **formats)
1832 {
1833  enum AVPixelFormat *fmts = av_malloc_array(2, sizeof(*fmts));
1834  if (!fmts)
1835  return AVERROR(ENOMEM);
1836 
1837  fmts[0] = hwfc->sw_format;
1838  fmts[1] = AV_PIX_FMT_NONE;
1839 
1840  *formats = fmts;
1841  return 0;
1842 }
1843 
1844 typedef struct VulkanMapping {
1846  int flags;
1847 } VulkanMapping;
1848 
1850 {
1851  VulkanMapping *map = hwmap->priv;
1852  AVVulkanDeviceContext *hwctx = hwfc->device_ctx->hwctx;
1853  const int planes = av_pix_fmt_count_planes(hwfc->sw_format);
1854 
1855  /* Check if buffer needs flushing */
1856  if ((map->flags & AV_HWFRAME_MAP_WRITE) &&
1857  !(map->frame->flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) {
1858  VkResult ret;
1859  VkMappedMemoryRange flush_ranges[AV_NUM_DATA_POINTERS] = { { 0 } };
1860 
1861  for (int i = 0; i < planes; i++) {
1862  flush_ranges[i].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
1863  flush_ranges[i].memory = map->frame->mem[i];
1864  flush_ranges[i].size = VK_WHOLE_SIZE;
1865  }
1866 
1867  ret = vkFlushMappedMemoryRanges(hwctx->act_dev, planes,
1868  flush_ranges);
1869  if (ret != VK_SUCCESS) {
1870  av_log(hwfc, AV_LOG_ERROR, "Failed to flush memory: %s\n",
1871  vk_ret2str(ret));
1872  }
1873  }
1874 
1875  for (int i = 0; i < planes; i++)
1876  vkUnmapMemory(hwctx->act_dev, map->frame->mem[i]);
1877 
1878  av_free(map);
1879 }
1880 
1882  const AVFrame *src, int flags)
1883 {
1884  VkResult ret;
1885  int err, mapped_mem_count = 0;
1886  AVVkFrame *f = (AVVkFrame *)src->data[0];
1887  AVVulkanDeviceContext *hwctx = hwfc->device_ctx->hwctx;
1888  const int planes = av_pix_fmt_count_planes(hwfc->sw_format);
1889 
1891  if (!map)
1892  return AVERROR(EINVAL);
1893 
1894  if (src->format != AV_PIX_FMT_VULKAN) {
1895  av_log(hwfc, AV_LOG_ERROR, "Cannot map from pixel format %s!\n",
1896  av_get_pix_fmt_name(src->format));
1897  err = AVERROR(EINVAL);
1898  goto fail;
1899  }
1900 
1901  if (!(f->flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) ||
1902  !(f->tiling == VK_IMAGE_TILING_LINEAR)) {
1903  av_log(hwfc, AV_LOG_ERROR, "Unable to map frame, not host visible "
1904  "and linear!\n");
1905  err = AVERROR(EINVAL);
1906  goto fail;
1907  }
1908 
1909  dst->width = src->width;
1910  dst->height = src->height;
1911 
1912  for (int i = 0; i < planes; i++) {
1913  ret = vkMapMemory(hwctx->act_dev, f->mem[i], 0,
1914  VK_WHOLE_SIZE, 0, (void **)&dst->data[i]);
1915  if (ret != VK_SUCCESS) {
1916  av_log(hwfc, AV_LOG_ERROR, "Failed to map image memory: %s\n",
1917  vk_ret2str(ret));
1918  err = AVERROR_EXTERNAL;
1919  goto fail;
1920  }
1921  mapped_mem_count++;
1922  }
1923 
1924  /* Check if the memory contents matter */
1926  !(f->flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) {
1927  VkMappedMemoryRange map_mem_ranges[AV_NUM_DATA_POINTERS] = { { 0 } };
1928  for (int i = 0; i < planes; i++) {
1929  map_mem_ranges[i].sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
1930  map_mem_ranges[i].size = VK_WHOLE_SIZE;
1931  map_mem_ranges[i].memory = f->mem[i];
1932  }
1933 
1934  ret = vkInvalidateMappedMemoryRanges(hwctx->act_dev, planes,
1935  map_mem_ranges);
1936  if (ret != VK_SUCCESS) {
1937  av_log(hwfc, AV_LOG_ERROR, "Failed to invalidate memory: %s\n",
1938  vk_ret2str(ret));
1939  err = AVERROR_EXTERNAL;
1940  goto fail;
1941  }
1942  }
1943 
1944  for (int i = 0; i < planes; i++) {
1945  VkImageSubresource sub = {
1946  .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1947  };
1948  VkSubresourceLayout layout;
1949  vkGetImageSubresourceLayout(hwctx->act_dev, f->img[i], &sub, &layout);
1950  dst->linesize[i] = layout.rowPitch;
1951  }
1952 
1953  map->frame = f;
1954  map->flags = flags;
1955 
1956  err = ff_hwframe_map_create(src->hw_frames_ctx, dst, src,
1958  if (err < 0)
1959  goto fail;
1960 
1961  return 0;
1962 
1963 fail:
1964  for (int i = 0; i < mapped_mem_count; i++)
1965  vkUnmapMemory(hwctx->act_dev, f->mem[i]);
1966 
1967  av_free(map);
1968  return err;
1969 }
1970 
1971 #if CONFIG_LIBDRM
1972 static void vulkan_unmap_from(AVHWFramesContext *hwfc, HWMapDescriptor *hwmap)
1973 {
1974  VulkanMapping *map = hwmap->priv;
1975  AVVulkanDeviceContext *hwctx = hwfc->device_ctx->hwctx;
1976  const int planes = av_pix_fmt_count_planes(hwfc->sw_format);
1977 
1978  for (int i = 0; i < planes; i++) {
1979  vkDestroyImage(hwctx->act_dev, map->frame->img[i], hwctx->alloc);
1980  vkFreeMemory(hwctx->act_dev, map->frame->mem[i], hwctx->alloc);
1981  vkDestroySemaphore(hwctx->act_dev, map->frame->sem[i], hwctx->alloc);
1982  }
1983 
1984  av_freep(&map->frame);
1985  av_free(map);
1986 }
1987 
1988 static const struct {
1989  uint32_t drm_fourcc;
1990  VkFormat vk_format;
1991 } vulkan_drm_format_map[] = {
1992  { DRM_FORMAT_R8, VK_FORMAT_R8_UNORM },
1993  { DRM_FORMAT_R16, VK_FORMAT_R16_UNORM },
1994  { DRM_FORMAT_GR88, VK_FORMAT_R8G8_UNORM },
1995  { DRM_FORMAT_RG88, VK_FORMAT_R8G8_UNORM },
1996  { DRM_FORMAT_GR1616, VK_FORMAT_R16G16_UNORM },
1997  { DRM_FORMAT_RG1616, VK_FORMAT_R16G16_UNORM },
1998  { DRM_FORMAT_ARGB8888, VK_FORMAT_B8G8R8A8_UNORM },
1999  { DRM_FORMAT_XRGB8888, VK_FORMAT_B8G8R8A8_UNORM },
2000  { DRM_FORMAT_ABGR8888, VK_FORMAT_R8G8B8A8_UNORM },
2001  { DRM_FORMAT_XBGR8888, VK_FORMAT_R8G8B8A8_UNORM },
2002 };
2003 
2004 static inline VkFormat drm_to_vulkan_fmt(uint32_t drm_fourcc)
2005 {
2006  for (int i = 0; i < FF_ARRAY_ELEMS(vulkan_drm_format_map); i++)
2007  if (vulkan_drm_format_map[i].drm_fourcc == drm_fourcc)
2008  return vulkan_drm_format_map[i].vk_format;
2009  return VK_FORMAT_UNDEFINED;
2010 }
2011 
2012 static int vulkan_map_from_drm_frame_desc(AVHWFramesContext *hwfc, AVVkFrame **frame,
2013  const AVFrame *src)
2014 {
2015  int err = 0;
2016  VkResult ret;
2017  AVVkFrame *f;
2018  int bind_counts = 0;
2019  AVHWDeviceContext *ctx = hwfc->device_ctx;
2020  AVVulkanDeviceContext *hwctx = ctx->hwctx;
2021  VulkanDevicePriv *p = ctx->internal->priv;
2022  VulkanFramesPriv *fp = hwfc->internal->priv;
2023  AVVulkanFramesContext *frames_hwctx = hwfc->hwctx;
2024  const AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor *)src->data[0];
2025  const int has_modifiers = !!(p->extensions & EXT_DRM_MODIFIER_FLAGS);
2026  VkSubresourceLayout plane_data[AV_NUM_DATA_POINTERS] = { 0 };
2027  VkBindImageMemoryInfo bind_info[AV_NUM_DATA_POINTERS] = { 0 };
2028  VkBindImagePlaneMemoryInfo plane_info[AV_NUM_DATA_POINTERS] = { 0 };
2029  VkExternalMemoryHandleTypeFlagBits htype = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
2030 
2031  VK_LOAD_PFN(hwctx->inst, vkGetMemoryFdPropertiesKHR);
2032 
2033  for (int i = 0; i < desc->nb_layers; i++) {
2034  if (drm_to_vulkan_fmt(desc->layers[i].format) == VK_FORMAT_UNDEFINED) {
2035  av_log(ctx, AV_LOG_ERROR, "Unsupported DMABUF layer format %#08x!\n",
2036  desc->layers[i].format);
2037  return AVERROR(EINVAL);
2038  }
2039  }
2040 
2041  if (!(f = av_vk_frame_alloc())) {
2042  av_log(ctx, AV_LOG_ERROR, "Unable to allocate memory for AVVkFrame!\n");
2043  err = AVERROR(ENOMEM);
2044  goto fail;
2045  }
2046 
2047  f->tiling = has_modifiers ? VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT :
2048  desc->objects[0].format_modifier == DRM_FORMAT_MOD_LINEAR ?
2049  VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL;
2050 
2051  for (int i = 0; i < desc->nb_layers; i++) {
2052  const int planes = desc->layers[i].nb_planes;
2053  VkImageDrmFormatModifierExplicitCreateInfoEXT drm_info = {
2054  .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_EXPLICIT_CREATE_INFO_EXT,
2055  .drmFormatModifier = desc->objects[0].format_modifier,
2056  .drmFormatModifierPlaneCount = planes,
2057  .pPlaneLayouts = (const VkSubresourceLayout *)&plane_data,
2058  };
2059 
2060  VkExternalMemoryImageCreateInfo einfo = {
2061  .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
2062  .pNext = has_modifiers ? &drm_info : NULL,
2063  .handleTypes = htype,
2064  };
2065 
2066  VkSemaphoreCreateInfo sem_spawn = {
2067  .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
2068  };
2069 
2070  VkImageCreateInfo create_info = {
2071  .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
2072  .pNext = &einfo,
2073  .imageType = VK_IMAGE_TYPE_2D,
2074  .format = drm_to_vulkan_fmt(desc->layers[i].format),
2075  .extent.depth = 1,
2076  .mipLevels = 1,
2077  .arrayLayers = 1,
2078  .flags = VK_IMAGE_CREATE_ALIAS_BIT,
2079  .tiling = f->tiling,
2080  .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, /* specs say so */
2081  .usage = frames_hwctx->usage,
2082  .samples = VK_SAMPLE_COUNT_1_BIT,
2083  .pQueueFamilyIndices = p->qfs,
2084  .queueFamilyIndexCount = p->num_qfs,
2085  .sharingMode = p->num_qfs > 1 ? VK_SHARING_MODE_CONCURRENT :
2086  VK_SHARING_MODE_EXCLUSIVE,
2087  };
2088 
2089  get_plane_wh(&create_info.extent.width, &create_info.extent.height,
2090  hwfc->sw_format, src->width, src->height, i);
2091 
2092  for (int j = 0; j < planes; j++) {
2093  plane_data[j].offset = desc->layers[i].planes[j].offset;
2094  plane_data[j].rowPitch = desc->layers[i].planes[j].pitch;
2095  plane_data[j].size = 0; /* The specs say so for all 3 */
2096  plane_data[j].arrayPitch = 0;
2097  plane_data[j].depthPitch = 0;
2098  }
2099 
2100  /* Create image */
2101  ret = vkCreateImage(hwctx->act_dev, &create_info,
2102  hwctx->alloc, &f->img[i]);
2103  if (ret != VK_SUCCESS) {
2104  av_log(ctx, AV_LOG_ERROR, "Image creation failure: %s\n",
2105  vk_ret2str(ret));
2106  err = AVERROR(EINVAL);
2107  goto fail;
2108  }
2109 
2110  ret = vkCreateSemaphore(hwctx->act_dev, &sem_spawn,
2111  hwctx->alloc, &f->sem[i]);
2112  if (ret != VK_SUCCESS) {
2113  av_log(hwctx, AV_LOG_ERROR, "Failed to create semaphore: %s\n",
2114  vk_ret2str(ret));
2115  return AVERROR_EXTERNAL;
2116  }
2117 
2118  /* We'd import a semaphore onto the one we created using
2119  * vkImportSemaphoreFdKHR but unfortunately neither DRM nor VAAPI
2120  * offer us anything we could import and sync with, so instead
2121  * just signal the semaphore we created. */
2122 
2123  f->layout[i] = create_info.initialLayout;
2124  f->access[i] = 0x0;
2125  }
2126 
2127  for (int i = 0; i < desc->nb_objects; i++) {
2128  int use_ded_mem = 0;
2129  VkMemoryFdPropertiesKHR fdmp = {
2130  .sType = VK_STRUCTURE_TYPE_MEMORY_FD_PROPERTIES_KHR,
2131  };
2132  VkMemoryRequirements req = {
2133  .size = desc->objects[i].size,
2134  };
2135  VkImportMemoryFdInfoKHR idesc = {
2136  .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR,
2137  .handleType = htype,
2138  .fd = dup(desc->objects[i].fd),
2139  };
2140  VkMemoryDedicatedAllocateInfo ded_alloc = {
2141  .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
2142  .pNext = &idesc,
2143  };
2144 
2145  ret = pfn_vkGetMemoryFdPropertiesKHR(hwctx->act_dev, htype,
2146  idesc.fd, &fdmp);
2147  if (ret != VK_SUCCESS) {
2148  av_log(hwfc, AV_LOG_ERROR, "Failed to get FD properties: %s\n",
2149  vk_ret2str(ret));
2150  err = AVERROR_EXTERNAL;
2151  close(idesc.fd);
2152  goto fail;
2153  }
2154 
2155  req.memoryTypeBits = fdmp.memoryTypeBits;
2156 
2157  /* Dedicated allocation only makes sense if there's a one to one mapping
2158  * between images and the memory backing them, so only check in this
2159  * case. */
2160  if (desc->nb_layers == desc->nb_objects) {
2161  VkImageMemoryRequirementsInfo2 req_desc = {
2162  .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_REQUIREMENTS_INFO_2,
2163  .image = f->img[i],
2164  };
2165  VkMemoryDedicatedRequirements ded_req = {
2166  .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS,
2167  };
2168  VkMemoryRequirements2 req2 = {
2169  .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
2170  .pNext = &ded_req,
2171  };
2172 
2173  vkGetImageMemoryRequirements2(hwctx->act_dev, &req_desc, &req2);
2174 
2175  use_ded_mem = ded_req.prefersDedicatedAllocation |
2176  ded_req.requiresDedicatedAllocation;
2177  if (use_ded_mem)
2178  ded_alloc.image = f->img[i];
2179  }
2180 
2181  err = alloc_mem(ctx, &req, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
2182  use_ded_mem ? &ded_alloc : ded_alloc.pNext,
2183  &f->flags, &f->mem[i]);
2184  if (err) {
2185  close(idesc.fd);
2186  goto fail;
2187  }
2188 
2189  f->size[i] = desc->objects[i].size;
2190  }
2191 
2192  for (int i = 0; i < desc->nb_layers; i++) {
2193  const int planes = desc->layers[i].nb_planes;
2194  const int signal_p = has_modifiers && (planes > 1);
2195  for (int j = 0; j < planes; j++) {
2196  VkImageAspectFlagBits aspect = j == 0 ? VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT :
2197  j == 1 ? VK_IMAGE_ASPECT_MEMORY_PLANE_1_BIT_EXT :
2198  VK_IMAGE_ASPECT_MEMORY_PLANE_2_BIT_EXT;
2199 
2200  plane_info[bind_counts].sType = VK_STRUCTURE_TYPE_BIND_IMAGE_PLANE_MEMORY_INFO;
2201  plane_info[bind_counts].planeAspect = aspect;
2202 
2203  bind_info[bind_counts].sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO;
2204  bind_info[bind_counts].pNext = signal_p ? &plane_info[bind_counts] : NULL;
2205  bind_info[bind_counts].image = f->img[i];
2206  bind_info[bind_counts].memory = f->mem[desc->layers[i].planes[j].object_index];
2207  bind_info[bind_counts].memoryOffset = desc->layers[i].planes[j].offset;
2208  bind_counts++;
2209  }
2210  }
2211 
2212  /* Bind the allocated memory to the images */
2213  ret = vkBindImageMemory2(hwctx->act_dev, bind_counts, bind_info);
2214  if (ret != VK_SUCCESS) {
2215  av_log(ctx, AV_LOG_ERROR, "Failed to bind memory: %s\n",
2216  vk_ret2str(ret));
2217  return AVERROR_EXTERNAL;
2218  }
2219 
2220  /* NOTE: This is completely uneccesary and unneeded once we can import
2221  * semaphores from DRM. Otherwise we have to activate the semaphores.
2222  * We're reusing the exec context that's also used for uploads/downloads. */
2223  err = prepare_frame(hwfc, &fp->conv_ctx, f, PREP_MODE_RO_SHADER);
2224  if (err)
2225  goto fail;
2226 
2227  *frame = f;
2228 
2229  return 0;
2230 
2231 fail:
2232  for (int i = 0; i < desc->nb_layers; i++) {
2233  vkDestroyImage(hwctx->act_dev, f->img[i], hwctx->alloc);
2234  vkDestroySemaphore(hwctx->act_dev, f->sem[i], hwctx->alloc);
2235  }
2236  for (int i = 0; i < desc->nb_objects; i++)
2237  vkFreeMemory(hwctx->act_dev, f->mem[i], hwctx->alloc);
2238 
2239  av_free(f);
2240 
2241  return err;
2242 }
2243 
2244 static int vulkan_map_from_drm(AVHWFramesContext *hwfc, AVFrame *dst,
2245  const AVFrame *src, int flags)
2246 {
2247  int err = 0;
2248  AVVkFrame *f;
2249  VulkanMapping *map = NULL;
2250 
2251  if ((err = vulkan_map_from_drm_frame_desc(hwfc, &f, src)))
2252  return err;
2253 
2254  /* The unmapping function will free this */
2255  dst->data[0] = (uint8_t *)f;
2256  dst->width = src->width;
2257  dst->height = src->height;
2258 
2259  map = av_mallocz(sizeof(VulkanMapping));
2260  if (!map)
2261  goto fail;
2262 
2263  map->frame = f;
2264  map->flags = flags;
2265 
2266  err = ff_hwframe_map_create(dst->hw_frames_ctx, dst, src,
2267  &vulkan_unmap_from, map);
2268  if (err < 0)
2269  goto fail;
2270 
2271  av_log(hwfc, AV_LOG_DEBUG, "Mapped DRM object to Vulkan!\n");
2272 
2273  return 0;
2274 
2275 fail:
2277  av_free(map);
2278  return err;
2279 }
2280 
2281 #if CONFIG_VAAPI
2282 static int vulkan_map_from_vaapi(AVHWFramesContext *dst_fc,
2283  AVFrame *dst, const AVFrame *src,
2284  int flags)
2285 {
2286  int err;
2287  AVFrame *tmp = av_frame_alloc();
2288  AVHWFramesContext *vaapi_fc = (AVHWFramesContext*)src->hw_frames_ctx->data;
2289  AVVAAPIDeviceContext *vaapi_ctx = vaapi_fc->device_ctx->hwctx;
2290  VASurfaceID surface_id = (VASurfaceID)(uintptr_t)src->data[3];
2291 
2292  if (!tmp)
2293  return AVERROR(ENOMEM);
2294 
2295  /* We have to sync since like the previous comment said, no semaphores */
2296  vaSyncSurface(vaapi_ctx->display, surface_id);
2297 
2298  tmp->format = AV_PIX_FMT_DRM_PRIME;
2299 
2300  err = av_hwframe_map(tmp, src, flags);
2301  if (err < 0)
2302  goto fail;
2303 
2304  err = vulkan_map_from_drm(dst_fc, dst, tmp, flags);
2305  if (err < 0)
2306  goto fail;
2307 
2308  err = ff_hwframe_map_replace(dst, src);
2309 
2310 fail:
2311  av_frame_free(&tmp);
2312  return err;
2313 }
2314 #endif
2315 #endif
2316 
2317 #if CONFIG_CUDA
2318 static int vulkan_export_to_cuda(AVHWFramesContext *hwfc,
2319  AVBufferRef *cuda_hwfc,
2320  const AVFrame *frame)
2321 {
2322  int err;
2323  VkResult ret;
2324  AVVkFrame *dst_f;
2325  AVVkFrameInternal *dst_int;
2326  AVHWDeviceContext *ctx = hwfc->device_ctx;
2327  AVVulkanDeviceContext *hwctx = ctx->hwctx;
2328  const int planes = av_pix_fmt_count_planes(hwfc->sw_format);
2330  VK_LOAD_PFN(hwctx->inst, vkGetMemoryFdKHR);
2331  VK_LOAD_PFN(hwctx->inst, vkGetSemaphoreFdKHR);
2332 
2333  AVHWFramesContext *cuda_fc = (AVHWFramesContext*)cuda_hwfc->data;
2334  AVHWDeviceContext *cuda_cu = cuda_fc->device_ctx;
2335  AVCUDADeviceContext *cuda_dev = cuda_cu->hwctx;
2336  AVCUDADeviceContextInternal *cu_internal = cuda_dev->internal;
2337  CudaFunctions *cu = cu_internal->cuda_dl;
2338  CUarray_format cufmt = desc->comp[0].depth > 8 ? CU_AD_FORMAT_UNSIGNED_INT16 :
2339  CU_AD_FORMAT_UNSIGNED_INT8;
2340  const int elem_size = 1 + (desc->comp[0].depth > 8);
2341 
2342  dst_f = (AVVkFrame *)frame->data[0];
2343 
2344  dst_int = dst_f->internal;
2345  if (!dst_int || !dst_int->cuda_fc_ref) {
2346  if (!dst_f->internal)
2347  dst_f->internal = dst_int = av_mallocz(sizeof(*dst_f->internal));
2348 
2349  if (!dst_int) {
2350  err = AVERROR(ENOMEM);
2351  goto fail;
2352  }
2353 
2354  dst_int->cuda_fc_ref = av_buffer_ref(cuda_hwfc);
2355  if (!dst_int->cuda_fc_ref) {
2356  err = AVERROR(ENOMEM);
2357  goto fail;
2358  }
2359 
2360  for (int i = 0; i < planes; i++) {
2361  CUDA_EXTERNAL_MEMORY_MIPMAPPED_ARRAY_DESC tex_desc = {
2362  .offset = 0,
2363  .arrayDesc = {
2364  .Depth = 0,
2365  .Format = cufmt,
2366  .NumChannels = desc->comp[i].step / elem_size,
2367  .Flags = 0,
2368  },
2369  .numLevels = 1,
2370  };
2371  CUDA_EXTERNAL_MEMORY_HANDLE_DESC ext_desc = {
2372  .type = CU_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD,
2373  .size = dst_f->size[i],
2374  };
2375  VkMemoryGetFdInfoKHR export_info = {
2376  .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
2377  .memory = dst_f->mem[i],
2378  .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT_KHR,
2379  };
2380  VkSemaphoreGetFdInfoKHR sem_export = {
2381  .sType = VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR,
2382  .semaphore = dst_f->sem[i],
2383  .handleType = VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT,
2384  };
2385  CUDA_EXTERNAL_SEMAPHORE_HANDLE_DESC ext_sem_desc = {
2386  .type = CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD,
2387  };
2388 
2389  int p_w, p_h;
2390  get_plane_wh(&p_w, &p_h, hwfc->sw_format, hwfc->width, hwfc->height, i);
2391 
2392  tex_desc.arrayDesc.Width = p_w;
2393  tex_desc.arrayDesc.Height = p_h;
2394 
2395  ret = pfn_vkGetMemoryFdKHR(hwctx->act_dev, &export_info,
2396  &ext_desc.handle.fd);
2397  if (ret != VK_SUCCESS) {
2398  av_log(hwfc, AV_LOG_ERROR, "Unable to export the image as a FD!\n");
2399  err = AVERROR_EXTERNAL;
2400  goto fail;
2401  }
2402 
2403  ret = CHECK_CU(cu->cuImportExternalMemory(&dst_int->ext_mem[i], &ext_desc));
2404  if (ret < 0) {
2405  err = AVERROR_EXTERNAL;
2406  goto fail;
2407  }
2408 
2409  ret = CHECK_CU(cu->cuExternalMemoryGetMappedMipmappedArray(&dst_int->cu_mma[i],
2410  dst_int->ext_mem[i],
2411  &tex_desc));
2412  if (ret < 0) {
2413  err = AVERROR_EXTERNAL;
2414  goto fail;
2415  }
2416 
2417  ret = CHECK_CU(cu->cuMipmappedArrayGetLevel(&dst_int->cu_array[i],
2418  dst_int->cu_mma[i], 0));
2419  if (ret < 0) {
2420  err = AVERROR_EXTERNAL;
2421  goto fail;
2422  }
2423 
2424  ret = pfn_vkGetSemaphoreFdKHR(hwctx->act_dev, &sem_export,
2425  &ext_sem_desc.handle.fd);
2426  if (ret != VK_SUCCESS) {
2427  av_log(ctx, AV_LOG_ERROR, "Failed to export semaphore: %s\n",
2428  vk_ret2str(ret));
2429  err = AVERROR_EXTERNAL;
2430  goto fail;
2431  }
2432 
2433  ret = CHECK_CU(cu->cuImportExternalSemaphore(&dst_int->cu_sem[i],
2434  &ext_sem_desc));
2435  if (ret < 0) {
2436  err = AVERROR_EXTERNAL;
2437  goto fail;
2438  }
2439  }
2440  }
2441 
2442  return 0;
2443 
2444 fail:
2445  return err;
2446 }
2447 
2448 static int vulkan_transfer_data_from_cuda(AVHWFramesContext *hwfc,
2449  AVFrame *dst, const AVFrame *src)
2450 {
2451  int err;
2452  VkResult ret;
2453  CUcontext dummy;
2454  AVVkFrame *dst_f;
2455  AVVkFrameInternal *dst_int;
2456  const int planes = av_pix_fmt_count_planes(hwfc->sw_format);
2458 
2459  AVHWFramesContext *cuda_fc = (AVHWFramesContext*)src->hw_frames_ctx->data;
2460  AVHWDeviceContext *cuda_cu = cuda_fc->device_ctx;
2461  AVCUDADeviceContext *cuda_dev = cuda_cu->hwctx;
2462  AVCUDADeviceContextInternal *cu_internal = cuda_dev->internal;
2463  CudaFunctions *cu = cu_internal->cuda_dl;
2464  CUDA_EXTERNAL_SEMAPHORE_WAIT_PARAMS s_w_par[AV_NUM_DATA_POINTERS] = { 0 };
2465  CUDA_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS s_s_par[AV_NUM_DATA_POINTERS] = { 0 };
2466 
2467  ret = CHECK_CU(cu->cuCtxPushCurrent(cuda_dev->cuda_ctx));
2468  if (ret < 0)
2469  return AVERROR_EXTERNAL;
2470 
2471  dst_f = (AVVkFrame *)dst->data[0];
2472 
2473  ret = vulkan_export_to_cuda(hwfc, src->hw_frames_ctx, dst);
2474  if (ret < 0) {
2475  CHECK_CU(cu->cuCtxPopCurrent(&dummy));
2476  return ret;
2477  }
2478 
2479  dst_int = dst_f->internal;
2480 
2481  ret = CHECK_CU(cu->cuWaitExternalSemaphoresAsync(dst_int->cu_sem, s_w_par,
2482  planes, cuda_dev->stream));
2483  if (ret < 0) {
2484  err = AVERROR_EXTERNAL;
2485  goto fail;
2486  }
2487 
2488  for (int i = 0; i < planes; i++) {
2489  CUDA_MEMCPY2D cpy = {
2490  .srcMemoryType = CU_MEMORYTYPE_DEVICE,
2491  .srcDevice = (CUdeviceptr)src->data[i],
2492  .srcPitch = src->linesize[i],
2493  .srcY = 0,
2494 
2495  .dstMemoryType = CU_MEMORYTYPE_ARRAY,
2496  .dstArray = dst_int->cu_array[i],
2497  };
2498 
2499  int p_w, p_h;
2500  get_plane_wh(&p_w, &p_h, hwfc->sw_format, hwfc->width, hwfc->height, i);
2501 
2502  cpy.WidthInBytes = p_w * desc->comp[i].step;
2503  cpy.Height = p_h;
2504 
2505  ret = CHECK_CU(cu->cuMemcpy2DAsync(&cpy, cuda_dev->stream));
2506  if (ret < 0) {
2507  err = AVERROR_EXTERNAL;
2508  goto fail;
2509  }
2510  }
2511 
2512  ret = CHECK_CU(cu->cuSignalExternalSemaphoresAsync(dst_int->cu_sem, s_s_par,
2513  planes, cuda_dev->stream));
2514  if (ret < 0) {
2515  err = AVERROR_EXTERNAL;
2516  goto fail;
2517  }
2518 
2519  CHECK_CU(cu->cuCtxPopCurrent(&dummy));
2520 
2521  av_log(hwfc, AV_LOG_VERBOSE, "Transfered CUDA image to Vulkan!\n");
2522 
2523  return 0;
2524 
2525 fail:
2526  CHECK_CU(cu->cuCtxPopCurrent(&dummy));
2527  vulkan_free_internal(dst_int);
2528  dst_f->internal = NULL;
2529  av_buffer_unref(&dst->buf[0]);
2530  return err;
2531 }
2532 #endif
2533 
2534 static int vulkan_map_to(AVHWFramesContext *hwfc, AVFrame *dst,
2535  const AVFrame *src, int flags)
2536 {
2538 
2539  switch (src->format) {
2540 #if CONFIG_LIBDRM
2541 #if CONFIG_VAAPI
2542  case AV_PIX_FMT_VAAPI:
2543  if (p->extensions & EXT_EXTERNAL_DMABUF_MEMORY)
2544  return vulkan_map_from_vaapi(hwfc, dst, src, flags);
2545 #endif
2546  case AV_PIX_FMT_DRM_PRIME:
2547  if (p->extensions & EXT_EXTERNAL_DMABUF_MEMORY)
2548  return vulkan_map_from_drm(hwfc, dst, src, flags);
2549 #endif
2550  default:
2551  return AVERROR(ENOSYS);
2552  }
2553 }
2554 
2555 #if CONFIG_LIBDRM
2556 typedef struct VulkanDRMMapping {
2557  AVDRMFrameDescriptor drm_desc;
2558  AVVkFrame *source;
2559 } VulkanDRMMapping;
2560 
2561 static void vulkan_unmap_to_drm(AVHWFramesContext *hwfc, HWMapDescriptor *hwmap)
2562 {
2563  AVDRMFrameDescriptor *drm_desc = hwmap->priv;
2564 
2565  for (int i = 0; i < drm_desc->nb_objects; i++)
2566  close(drm_desc->objects[i].fd);
2567 
2568  av_free(drm_desc);
2569 }
2570 
2571 static inline uint32_t vulkan_fmt_to_drm(VkFormat vkfmt)
2572 {
2573  for (int i = 0; i < FF_ARRAY_ELEMS(vulkan_drm_format_map); i++)
2574  if (vulkan_drm_format_map[i].vk_format == vkfmt)
2575  return vulkan_drm_format_map[i].drm_fourcc;
2576  return DRM_FORMAT_INVALID;
2577 }
2578 
2579 static int vulkan_map_to_drm(AVHWFramesContext *hwfc, AVFrame *dst,
2580  const AVFrame *src, int flags)
2581 {
2582  int err = 0;
2583  VkResult ret;
2584  AVVkFrame *f = (AVVkFrame *)src->data[0];
2586  VulkanFramesPriv *fp = hwfc->internal->priv;
2587  AVVulkanDeviceContext *hwctx = hwfc->device_ctx->hwctx;
2588  const int planes = av_pix_fmt_count_planes(hwfc->sw_format);
2589  VK_LOAD_PFN(hwctx->inst, vkGetMemoryFdKHR);
2590  VkImageDrmFormatModifierPropertiesEXT drm_mod = {
2591  .sType = VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_PROPERTIES_EXT,
2592  };
2593  int free_drm_desc_on_err = 1;
2594 
2595  AVDRMFrameDescriptor *drm_desc = av_mallocz(sizeof(*drm_desc));
2596  if (!drm_desc)
2597  return AVERROR(ENOMEM);
2598 
2599  err = prepare_frame(hwfc, &fp->conv_ctx, f, PREP_MODE_EXTERNAL_EXPORT);
2600  if (err < 0)
2601  goto end;
2602 
2603  err = ff_hwframe_map_create(src->hw_frames_ctx, dst, src, &vulkan_unmap_to_drm, drm_desc);
2604  if (err < 0)
2605  goto end;
2606 
2607  /* It will be freed in ff_hwframe_map_create callback */
2608  free_drm_desc_on_err = 0;
2609 
2611  VK_LOAD_PFN(hwctx->inst, vkGetImageDrmFormatModifierPropertiesEXT);
2612  ret = pfn_vkGetImageDrmFormatModifierPropertiesEXT(hwctx->act_dev, f->img[0],
2613  &drm_mod);
2614  if (ret != VK_SUCCESS) {
2615  av_log(hwfc, AV_LOG_ERROR, "Failed to retrieve DRM format modifier!\n");
2616  err = AVERROR_EXTERNAL;
2617  goto end;
2618  }
2619  }
2620 
2621  for (int i = 0; (i < planes) && (f->mem[i]); i++) {
2622  VkMemoryGetFdInfoKHR export_info = {
2623  .sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
2624  .memory = f->mem[i],
2625  .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
2626  };
2627 
2628  ret = pfn_vkGetMemoryFdKHR(hwctx->act_dev, &export_info,
2629  &drm_desc->objects[i].fd);
2630  if (ret != VK_SUCCESS) {
2631  av_log(hwfc, AV_LOG_ERROR, "Unable to export the image as a FD!\n");
2632  err = AVERROR_EXTERNAL;
2633  goto end;
2634  }
2635 
2636  drm_desc->nb_objects++;
2637  drm_desc->objects[i].size = f->size[i];
2638  drm_desc->objects[i].format_modifier = drm_mod.drmFormatModifier;
2639  }
2640 
2641  drm_desc->nb_layers = planes;
2642  for (int i = 0; i < drm_desc->nb_layers; i++) {
2643  VkSubresourceLayout layout;
2644  VkImageSubresource sub = {
2645  .aspectMask = p->extensions & EXT_DRM_MODIFIER_FLAGS ?
2646  VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT :
2647  VK_IMAGE_ASPECT_COLOR_BIT,
2648  };
2649  VkFormat plane_vkfmt = av_vkfmt_from_pixfmt(hwfc->sw_format)[i];
2650 
2651  drm_desc->layers[i].format = vulkan_fmt_to_drm(plane_vkfmt);
2652  drm_desc->layers[i].nb_planes = 1;
2653 
2654  if (drm_desc->layers[i].format == DRM_FORMAT_INVALID) {
2655  av_log(hwfc, AV_LOG_ERROR, "Cannot map to DRM layer, unsupported!\n");
2656  err = AVERROR_PATCHWELCOME;
2657  goto end;
2658  }
2659 
2660  drm_desc->layers[i].planes[0].object_index = FFMIN(i, drm_desc->nb_objects - 1);
2661 
2662  if (f->tiling == VK_IMAGE_TILING_OPTIMAL)
2663  continue;
2664 
2665  vkGetImageSubresourceLayout(hwctx->act_dev, f->img[i], &sub, &layout);
2666  drm_desc->layers[i].planes[0].offset = layout.offset;
2667  drm_desc->layers[i].planes[0].pitch = layout.rowPitch;
2668  }
2669 
2670  dst->width = src->width;
2671  dst->height = src->height;
2672  dst->data[0] = (uint8_t *)drm_desc;
2673 
2674  av_log(hwfc, AV_LOG_VERBOSE, "Mapped AVVkFrame to a DRM object!\n");
2675 
2676  return 0;
2677 
2678 end:
2679  if (free_drm_desc_on_err)
2680  av_free(drm_desc);
2681  return err;
2682 }
2683 
2684 #if CONFIG_VAAPI
2685 static int vulkan_map_to_vaapi(AVHWFramesContext *hwfc, AVFrame *dst,
2686  const AVFrame *src, int flags)
2687 {
2688  int err;
2689  AVFrame *tmp = av_frame_alloc();
2690  if (!tmp)
2691  return AVERROR(ENOMEM);
2692 
2693  tmp->format = AV_PIX_FMT_DRM_PRIME;
2694 
2695  err = vulkan_map_to_drm(hwfc, tmp, src, flags);
2696  if (err < 0)
2697  goto fail;
2698 
2699  err = av_hwframe_map(dst, tmp, flags);
2700  if (err < 0)
2701  goto fail;
2702 
2703  err = ff_hwframe_map_replace(dst, src);
2704 
2705 fail:
2706  av_frame_free(&tmp);
2707  return err;
2708 }
2709 #endif
2710 #endif
2711 
2713  const AVFrame *src, int flags)
2714 {
2716 
2717  switch (dst->format) {
2718 #if CONFIG_LIBDRM
2719  case AV_PIX_FMT_DRM_PRIME:
2720  if (p->extensions & EXT_EXTERNAL_DMABUF_MEMORY)
2721  return vulkan_map_to_drm(hwfc, dst, src, flags);
2722 #if CONFIG_VAAPI
2723  case AV_PIX_FMT_VAAPI:
2724  if (p->extensions & EXT_EXTERNAL_DMABUF_MEMORY)
2725  return vulkan_map_to_vaapi(hwfc, dst, src, flags);
2726 #endif
2727 #endif
2728  default:
2729  return vulkan_map_frame_to_mem(hwfc, dst, src, flags);
2730  }
2731 }
2732 
2733 typedef struct ImageBuffer {
2734  VkBuffer buf;
2735  VkDeviceMemory mem;
2736  VkMemoryPropertyFlagBits flags;
2738 } ImageBuffer;
2739 
2740 static void free_buf(void *opaque, uint8_t *data)
2741 {
2742  AVHWDeviceContext *ctx = opaque;
2743  AVVulkanDeviceContext *hwctx = ctx->hwctx;
2744  ImageBuffer *vkbuf = (ImageBuffer *)data;
2745 
2746  if (vkbuf->buf)
2747  vkDestroyBuffer(hwctx->act_dev, vkbuf->buf, hwctx->alloc);
2748  if (vkbuf->mem)
2749  vkFreeMemory(hwctx->act_dev, vkbuf->mem, hwctx->alloc);
2750 
2751  av_free(data);
2752 }
2753 
2755 {
2756  size_t size;
2757  *stride = FFALIGN(*stride, p->props.properties.limits.optimalBufferCopyRowPitchAlignment);
2758  size = height*(*stride);
2759  size = FFALIGN(size, p->props.properties.limits.minMemoryMapAlignment);
2760  return size;
2761 }
2762 
2764  VkBufferUsageFlags usage, VkMemoryPropertyFlagBits flags,
2765  size_t size, uint32_t req_memory_bits, int host_mapped,
2766  void *create_pnext, void *alloc_pnext)
2767 {
2768  int err;
2769  VkResult ret;
2770  int use_ded_mem;
2771  AVVulkanDeviceContext *hwctx = ctx->hwctx;
2772 
2773  VkBufferCreateInfo buf_spawn = {
2774  .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
2775  .pNext = create_pnext,
2776  .usage = usage,
2777  .size = size,
2778  .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
2779  };
2780 
2781  VkBufferMemoryRequirementsInfo2 req_desc = {
2782  .sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_REQUIREMENTS_INFO_2,
2783  };
2784  VkMemoryDedicatedAllocateInfo ded_alloc = {
2785  .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
2786  .pNext = alloc_pnext,
2787  };
2788  VkMemoryDedicatedRequirements ded_req = {
2789  .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS,
2790  };
2791  VkMemoryRequirements2 req = {
2792  .sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2,
2793  .pNext = &ded_req,
2794  };
2795 
2796  ImageBuffer *vkbuf = av_mallocz(sizeof(*vkbuf));
2797  if (!vkbuf)
2798  return AVERROR(ENOMEM);
2799 
2800  vkbuf->mapped_mem = host_mapped;
2801 
2802  ret = vkCreateBuffer(hwctx->act_dev, &buf_spawn, NULL, &vkbuf->buf);
2803  if (ret != VK_SUCCESS) {
2804  av_log(ctx, AV_LOG_ERROR, "Failed to create buffer: %s\n",
2805  vk_ret2str(ret));
2806  err = AVERROR_EXTERNAL;
2807  goto fail;
2808  }
2809 
2810  req_desc.buffer = vkbuf->buf;
2811 
2812  vkGetBufferMemoryRequirements2(hwctx->act_dev, &req_desc, &req);
2813 
2814  /* In case the implementation prefers/requires dedicated allocation */
2815  use_ded_mem = ded_req.prefersDedicatedAllocation |
2816  ded_req.requiresDedicatedAllocation;
2817  if (use_ded_mem)
2818  ded_alloc.buffer = vkbuf->buf;
2819 
2820  /* Additional requirements imposed on us */
2821  if (req_memory_bits)
2822  req.memoryRequirements.memoryTypeBits &= req_memory_bits;
2823 
2824  err = alloc_mem(ctx, &req.memoryRequirements, flags,
2825  use_ded_mem ? &ded_alloc : (void *)ded_alloc.pNext,
2826  &vkbuf->flags, &vkbuf->mem);
2827  if (err)
2828  goto fail;
2829 
2830  ret = vkBindBufferMemory(hwctx->act_dev, vkbuf->buf, vkbuf->mem, 0);
2831  if (ret != VK_SUCCESS) {
2832  av_log(ctx, AV_LOG_ERROR, "Failed to bind memory to buffer: %s\n",
2833  vk_ret2str(ret));
2834  err = AVERROR_EXTERNAL;
2835  goto fail;
2836  }
2837 
2838  *buf = av_buffer_create((uint8_t *)vkbuf, sizeof(*vkbuf), free_buf, ctx, 0);
2839  if (!(*buf)) {
2840  err = AVERROR(ENOMEM);
2841  goto fail;
2842  }
2843 
2844  return 0;
2845 
2846 fail:
2847  free_buf(ctx, (uint8_t *)vkbuf);
2848  return err;
2849 }
2850 
2851 /* Skips mapping of host mapped buffers but still invalidates them */
2853  int nb_buffers, int invalidate)
2854 {
2855  VkResult ret;
2856  AVVulkanDeviceContext *hwctx = ctx->hwctx;
2857  VkMappedMemoryRange invalidate_ctx[AV_NUM_DATA_POINTERS];
2858  int invalidate_count = 0;
2859 
2860  for (int i = 0; i < nb_buffers; i++) {
2861  ImageBuffer *vkbuf = (ImageBuffer *)bufs[i]->data;
2862  if (vkbuf->mapped_mem)
2863  continue;
2864 
2865  ret = vkMapMemory(hwctx->act_dev, vkbuf->mem, 0,
2866  VK_WHOLE_SIZE, 0, (void **)&mem[i]);
2867  if (ret != VK_SUCCESS) {
2868  av_log(ctx, AV_LOG_ERROR, "Failed to map buffer memory: %s\n",
2869  vk_ret2str(ret));
2870  return AVERROR_EXTERNAL;
2871  }
2872  }
2873 
2874  if (!invalidate)
2875  return 0;
2876 
2877  for (int i = 0; i < nb_buffers; i++) {
2878  ImageBuffer *vkbuf = (ImageBuffer *)bufs[i]->data;
2879  const VkMappedMemoryRange ival_buf = {
2880  .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
2881  .memory = vkbuf->mem,
2882  .size = VK_WHOLE_SIZE,
2883  };
2884 
2885  /* For host imported memory Vulkan says to use platform-defined
2886  * sync methods, but doesn't really say not to call flush or invalidate
2887  * on original host pointers. It does explicitly allow to do that on
2888  * host-mapped pointers which are then mapped again using vkMapMemory,
2889  * but known implementations return the original pointers when mapped
2890  * again. */
2891  if (vkbuf->flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
2892  continue;
2893 
2894  invalidate_ctx[invalidate_count++] = ival_buf;
2895  }
2896 
2897  if (invalidate_count) {
2898  ret = vkInvalidateMappedMemoryRanges(hwctx->act_dev, invalidate_count,
2899  invalidate_ctx);
2900  if (ret != VK_SUCCESS)
2901  av_log(ctx, AV_LOG_WARNING, "Failed to invalidate memory: %s\n",
2902  vk_ret2str(ret));
2903  }
2904 
2905  return 0;
2906 }
2907 
2909  int nb_buffers, int flush)
2910 {
2911  int err = 0;
2912  VkResult ret;
2913  AVVulkanDeviceContext *hwctx = ctx->hwctx;
2914  VkMappedMemoryRange flush_ctx[AV_NUM_DATA_POINTERS];
2915  int flush_count = 0;
2916 
2917  if (flush) {
2918  for (int i = 0; i < nb_buffers; i++) {
2919  ImageBuffer *vkbuf = (ImageBuffer *)bufs[i]->data;
2920  const VkMappedMemoryRange flush_buf = {
2921  .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
2922  .memory = vkbuf->mem,
2923  .size = VK_WHOLE_SIZE,
2924  };
2925 
2926  if (vkbuf->flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
2927  continue;
2928 
2929  flush_ctx[flush_count++] = flush_buf;
2930  }
2931  }
2932 
2933  if (flush_count) {
2934  ret = vkFlushMappedMemoryRanges(hwctx->act_dev, flush_count, flush_ctx);
2935  if (ret != VK_SUCCESS) {
2936  av_log(ctx, AV_LOG_ERROR, "Failed to flush memory: %s\n",
2937  vk_ret2str(ret));
2938  err = AVERROR_EXTERNAL; /* We still want to try to unmap them */
2939  }
2940  }
2941 
2942  for (int i = 0; i < nb_buffers; i++) {
2943  ImageBuffer *vkbuf = (ImageBuffer *)bufs[i]->data;
2944  if (vkbuf->mapped_mem)
2945  continue;
2946 
2947  vkUnmapMemory(hwctx->act_dev, vkbuf->mem);
2948  }
2949 
2950  return err;
2951 }
2952 
2954  AVBufferRef **bufs, size_t *buf_offsets,
2955  const int *buf_stride, int w,
2956  int h, enum AVPixelFormat pix_fmt, int to_buf)
2957 {
2958  int err;
2959  AVVkFrame *frame = (AVVkFrame *)f->data[0];
2960  VulkanFramesPriv *fp = hwfc->internal->priv;
2961 
2962  int bar_num = 0;
2963  VkPipelineStageFlagBits sem_wait_dst[AV_NUM_DATA_POINTERS];
2964 
2965  const int planes = av_pix_fmt_count_planes(pix_fmt);
2967 
2968  VkImageMemoryBarrier img_bar[AV_NUM_DATA_POINTERS] = { 0 };
2969  VulkanExecCtx *ectx = to_buf ? &fp->download_ctx : &fp->upload_ctx;
2970  VkCommandBuffer cmd_buf = get_buf_exec_ctx(hwfc, ectx);
2971 
2972  VkSubmitInfo s_info = {
2973  .sType = VK_STRUCTURE_TYPE_SUBMIT_INFO,
2974  .pSignalSemaphores = frame->sem,
2975  .pWaitSemaphores = frame->sem,
2976  .pWaitDstStageMask = sem_wait_dst,
2977  .signalSemaphoreCount = planes,
2978  .waitSemaphoreCount = planes,
2979  };
2980 
2981  if ((err = wait_start_exec_ctx(hwfc, ectx)))
2982  return err;
2983 
2984  /* Change the image layout to something more optimal for transfers */
2985  for (int i = 0; i < planes; i++) {
2986  VkImageLayout new_layout = to_buf ? VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL :
2987  VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
2988  VkAccessFlags new_access = to_buf ? VK_ACCESS_TRANSFER_READ_BIT :
2989  VK_ACCESS_TRANSFER_WRITE_BIT;
2990 
2991  sem_wait_dst[i] = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
2992 
2993  /* If the layout matches and we have read access skip the barrier */
2994  if ((frame->layout[i] == new_layout) && (frame->access[i] & new_access))
2995  continue;
2996 
2997  img_bar[bar_num].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
2998  img_bar[bar_num].srcAccessMask = 0x0;
2999  img_bar[bar_num].dstAccessMask = new_access;
3000  img_bar[bar_num].oldLayout = frame->layout[i];
3001  img_bar[bar_num].newLayout = new_layout;
3002  img_bar[bar_num].srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
3003  img_bar[bar_num].dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
3004  img_bar[bar_num].image = frame->img[i];
3005  img_bar[bar_num].subresourceRange.levelCount = 1;
3006  img_bar[bar_num].subresourceRange.layerCount = 1;
3007  img_bar[bar_num].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
3008 
3009  frame->layout[i] = img_bar[bar_num].newLayout;
3010  frame->access[i] = img_bar[bar_num].dstAccessMask;
3011 
3012  bar_num++;
3013  }
3014 
3015  if (bar_num)
3016  vkCmdPipelineBarrier(cmd_buf, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
3017  VK_PIPELINE_STAGE_TRANSFER_BIT, 0,
3018  0, NULL, 0, NULL, bar_num, img_bar);
3019 
3020  /* Schedule a copy for each plane */
3021  for (int i = 0; i < planes; i++) {
3022  ImageBuffer *vkbuf = (ImageBuffer *)bufs[i]->data;
3023  VkBufferImageCopy buf_reg = {
3024  .bufferOffset = buf_offsets[i],
3025  .bufferRowLength = buf_stride[i] / desc->comp[i].step,
3026  .imageSubresource.layerCount = 1,
3027  .imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
3028  .imageOffset = { 0, 0, 0, },
3029  };
3030 
3031  int p_w, p_h;
3032  get_plane_wh(&p_w, &p_h, pix_fmt, w, h, i);
3033 
3034  buf_reg.bufferImageHeight = p_h;
3035  buf_reg.imageExtent = (VkExtent3D){ p_w, p_h, 1, };
3036 
3037  if (to_buf)
3038  vkCmdCopyImageToBuffer(cmd_buf, frame->img[i], frame->layout[i],
3039  vkbuf->buf, 1, &buf_reg);
3040  else
3041  vkCmdCopyBufferToImage(cmd_buf, vkbuf->buf, frame->img[i],
3042  frame->layout[i], 1, &buf_reg);
3043  }
3044 
3045  /* When uploading, do this asynchronously if the source is refcounted by
3046  * keeping the buffers as a submission dependency.
3047  * The hwcontext is guaranteed to not be freed until all frames are freed
3048  * in the frames_unint function.
3049  * When downloading to buffer, do this synchronously and wait for the
3050  * queue submission to finish executing */
3051  if (!to_buf) {
3052  int ref;
3053  for (ref = 0; ref < AV_NUM_DATA_POINTERS; ref++) {
3054  if (!f->buf[ref])
3055  break;
3056  if ((err = add_buf_dep_exec_ctx(hwfc, ectx, &f->buf[ref], 1)))
3057  return err;
3058  }
3059  if (ref && (err = add_buf_dep_exec_ctx(hwfc, ectx, bufs, planes)))
3060  return err;
3061  return submit_exec_ctx(hwfc, ectx, &s_info, !ref);
3062  } else {
3063  return submit_exec_ctx(hwfc, ectx, &s_info, 1);
3064  }
3065 }
3066 
3067 static int vulkan_transfer_data(AVHWFramesContext *hwfc, const AVFrame *vkf,
3068  const AVFrame *swf, int from)
3069 {
3070  int err = 0;
3071  VkResult ret;
3072  AVVkFrame *f = (AVVkFrame *)vkf->data[0];
3073  AVHWDeviceContext *dev_ctx = hwfc->device_ctx;
3074  AVVulkanDeviceContext *hwctx = dev_ctx->hwctx;
3076 
3077  AVFrame tmp;
3078  AVBufferRef *bufs[AV_NUM_DATA_POINTERS] = { 0 };
3079  size_t buf_offsets[AV_NUM_DATA_POINTERS] = { 0 };
3080 
3081  int p_w, p_h;
3082  const int planes = av_pix_fmt_count_planes(swf->format);
3083 
3084  int host_mapped[AV_NUM_DATA_POINTERS] = { 0 };
3085  const int map_host = !!(p->extensions & EXT_EXTERNAL_HOST_MEMORY);
3086  int bytewidth[AV_NUM_DATA_POINTERS];
3087 
3088  VK_LOAD_PFN(hwctx->inst, vkGetMemoryHostPointerPropertiesEXT);
3089 
3090  if ((swf->format != AV_PIX_FMT_NONE && !av_vkfmt_from_pixfmt(swf->format))) {
3091  av_log(hwfc, AV_LOG_ERROR, "Unsupported software frame pixel format!\n");
3092  return AVERROR(EINVAL);
3093  }
3094 
3095  if (swf->width > hwfc->width || swf->height > hwfc->height)
3096  return AVERROR(EINVAL);
3097 
3098  /* Copy only the visible bytes of each row, the last row of a plane
3099  * does not have to extend to the full linesize. */
3100  for (int i = 0; i < planes; i++) {
3101  bytewidth[i] = av_image_get_linesize(swf->format, swf->width, i);
3102  if (bytewidth[i] < 0)
3103  return bytewidth[i];
3104  }
3105 
3106  /* For linear, host visiable images */
3107  if (f->tiling == VK_IMAGE_TILING_LINEAR &&
3108  f->flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) {
3109  AVFrame *map = av_frame_alloc();
3110  if (!map)
3111  return AVERROR(ENOMEM);
3112  map->format = swf->format;
3113 
3115  if (err)
3116  return err;
3117 
3118  err = av_frame_copy((AVFrame *)(from ? swf : map), from ? map : swf);
3119  av_frame_free(&map);
3120  return err;
3121  }
3122 
3123  /* Create buffers */
3124  for (int i = 0; i < planes; i++) {
3125  size_t req_size;
3126 
3127  VkExternalMemoryBufferCreateInfo create_desc = {
3128  .sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO,
3129  .handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT,
3130  };
3131 
3132  VkImportMemoryHostPointerInfoEXT import_desc = {
3133  .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_HOST_POINTER_INFO_EXT,
3134  .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_HOST_ALLOCATION_BIT_EXT,
3135  };
3136 
3137  VkMemoryHostPointerPropertiesEXT p_props = {
3138  .sType = VK_STRUCTURE_TYPE_MEMORY_HOST_POINTER_PROPERTIES_EXT,
3139  };
3140 
3141  get_plane_wh(&p_w, &p_h, swf->format, swf->width, swf->height, i);
3142 
3143  tmp.linesize[i] = FFABS(swf->linesize[i]);
3144 
3145  /* Do not map images with a negative stride */
3146  if (map_host && swf->linesize[i] > 0) {
3147  size_t offs;
3148  offs = (uintptr_t)swf->data[i] % p->hprops.minImportedHostPointerAlignment;
3149  import_desc.pHostPointer = swf->data[i] - offs;
3150 
3151  /* We have to compensate for the few extra bytes of padding we
3152  * completely ignore at the start */
3153  req_size = FFALIGN(offs + tmp.linesize[i] * p_h,
3154  p->hprops.minImportedHostPointerAlignment);
3155 
3156  ret = pfn_vkGetMemoryHostPointerPropertiesEXT(hwctx->act_dev,
3157  import_desc.handleType,
3158  import_desc.pHostPointer,
3159  &p_props);
3160 
3161  if (ret == VK_SUCCESS) {
3162  host_mapped[i] = 1;
3163  buf_offsets[i] = offs;
3164  }
3165  }
3166 
3167  if (!host_mapped[i])
3168  req_size = get_req_buffer_size(p, &tmp.linesize[i], p_h);
3169 
3170  err = create_buf(dev_ctx, &bufs[i],
3171  from ? VK_BUFFER_USAGE_TRANSFER_DST_BIT :
3172  VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
3173  VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
3174  req_size, p_props.memoryTypeBits, host_mapped[i],
3175  host_mapped[i] ? &create_desc : NULL,
3176  host_mapped[i] ? &import_desc : NULL);
3177  if (err)
3178  goto end;
3179  }
3180 
3181  if (!from) {
3182  /* Map, copy image to buffer, unmap */
3183  if ((err = map_buffers(dev_ctx, bufs, tmp.data, planes, 0)))
3184  goto end;
3185 
3186  for (int i = 0; i < planes; i++) {
3187  if (host_mapped[i])
3188  continue;
3189 
3190  get_plane_wh(&p_w, &p_h, swf->format, swf->width, swf->height, i);
3191 
3192  av_image_copy_plane(tmp.data[i], tmp.linesize[i],
3193  (const uint8_t *)swf->data[i], swf->linesize[i],
3194  bytewidth[i],
3195  p_h);
3196  }
3197 
3198  if ((err = unmap_buffers(dev_ctx, bufs, planes, 1)))
3199  goto end;
3200  }
3201 
3202  /* Copy buffers into/from image */
3203  err = transfer_image_buf(hwfc, vkf, bufs, buf_offsets, tmp.linesize,
3204  swf->width, swf->height, swf->format, from);
3205 
3206  if (from) {
3207  /* Map, copy image to buffer, unmap */
3208  if ((err = map_buffers(dev_ctx, bufs, tmp.data, planes, 0)))
3209  goto end;
3210 
3211  for (int i = 0; i < planes; i++) {
3212  if (host_mapped[i])
3213  continue;
3214 
3215  get_plane_wh(&p_w, &p_h, swf->format, swf->width, swf->height, i);
3216 
3217  av_image_copy_plane(swf->data[i], swf->linesize[i],
3218  (const uint8_t *)tmp.data[i], tmp.linesize[i],
3219  bytewidth[i],
3220  p_h);
3221  }
3222 
3223  if ((err = unmap_buffers(dev_ctx, bufs, planes, 1)))
3224  goto end;
3225  }
3226 
3227 end:
3228  for (int i = 0; i < planes; i++)
3229  av_buffer_unref(&bufs[i]);
3230 
3231  return err;
3232 }
3233 
3235  const AVFrame *src)
3236 {
3238 
3239  switch (src->format) {
3240 #if CONFIG_CUDA
3241  case AV_PIX_FMT_CUDA:
3242  if ((p->extensions & EXT_EXTERNAL_FD_MEMORY) &&
3243  (p->extensions & EXT_EXTERNAL_FD_SEM))
3244  return vulkan_transfer_data_from_cuda(hwfc, dst, src);
3245 #endif
3246  default:
3247  if (src->hw_frames_ctx)
3248  return AVERROR(ENOSYS);
3249  else
3250  return vulkan_transfer_data(hwfc, dst, src, 0);
3251  }
3252 }
3253 
3254 #if CONFIG_CUDA
3255 static int vulkan_transfer_data_to_cuda(AVHWFramesContext *hwfc, AVFrame *dst,
3256  const AVFrame *src)
3257 {
3258  int err;
3259  VkResult ret;
3260  CUcontext dummy;
3261  AVVkFrame *dst_f;
3262  AVVkFrameInternal *dst_int;
3263  const int planes = av_pix_fmt_count_planes(hwfc->sw_format);
3265 
3267  AVHWDeviceContext *cuda_cu = cuda_fc->device_ctx;
3268  AVCUDADeviceContext *cuda_dev = cuda_cu->hwctx;
3269  AVCUDADeviceContextInternal *cu_internal = cuda_dev->internal;
3270  CudaFunctions *cu = cu_internal->cuda_dl;
3271  CUDA_EXTERNAL_SEMAPHORE_WAIT_PARAMS s_w_par[AV_NUM_DATA_POINTERS] = { 0 };
3272  CUDA_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS s_s_par[AV_NUM_DATA_POINTERS] = { 0 };
3273 
3274  ret = CHECK_CU(cu->cuCtxPushCurrent(cuda_dev->cuda_ctx));
3275  if (ret < 0)
3276  return AVERROR_EXTERNAL;
3277 
3278  dst_f = (AVVkFrame *)src->data[0];
3279 
3280  err = vulkan_export_to_cuda(hwfc, dst->hw_frames_ctx, src);
3281  if (err < 0) {
3282  CHECK_CU(cu->cuCtxPopCurrent(&dummy));
3283  return err;
3284  }
3285 
3286  dst_int = dst_f->internal;
3287 
3288  ret = CHECK_CU(cu->cuWaitExternalSemaphoresAsync(dst_int->cu_sem, s_w_par,
3289  planes, cuda_dev->stream));
3290  if (ret < 0) {
3291  err = AVERROR_EXTERNAL;
3292  goto fail;
3293  }
3294 
3295  for (int i = 0; i < planes; i++) {
3296  CUDA_MEMCPY2D cpy = {
3297  .dstMemoryType = CU_MEMORYTYPE_DEVICE,
3298  .dstDevice = (CUdeviceptr)dst->data[i],
3299  .dstPitch = dst->linesize[i],
3300  .dstY = 0,
3301 
3302  .srcMemoryType = CU_MEMORYTYPE_ARRAY,
3303  .srcArray = dst_int->cu_array[i],
3304  };
3305 
3306  int w, h;
3307  get_plane_wh(&w, &h, hwfc->sw_format, hwfc->width, hwfc->height, i);
3308 
3309  cpy.WidthInBytes = w * desc->comp[i].step;
3310  cpy.Height = h;
3311 
3312  ret = CHECK_CU(cu->cuMemcpy2DAsync(&cpy, cuda_dev->stream));
3313  if (ret < 0) {
3314  err = AVERROR_EXTERNAL;
3315  goto fail;
3316  }
3317  }
3318 
3319  ret = CHECK_CU(cu->cuSignalExternalSemaphoresAsync(dst_int->cu_sem, s_s_par,
3320  planes, cuda_dev->stream));
3321  if (ret < 0) {
3322  err = AVERROR_EXTERNAL;
3323  goto fail;
3324  }
3325 
3326  CHECK_CU(cu->cuCtxPopCurrent(&dummy));
3327 
3328  av_log(hwfc, AV_LOG_VERBOSE, "Transfered Vulkan image to CUDA!\n");
3329 
3330  return 0;
3331 
3332 fail:
3333  CHECK_CU(cu->cuCtxPopCurrent(&dummy));
3334  vulkan_free_internal(dst_int);
3335  dst_f->internal = NULL;
3336  av_buffer_unref(&dst->buf[0]);
3337  return err;
3338 }
3339 #endif
3340 
3342  const AVFrame *src)
3343 {
3345 
3346  switch (dst->format) {
3347 #if CONFIG_CUDA
3348  case AV_PIX_FMT_CUDA:
3349  if ((p->extensions & EXT_EXTERNAL_FD_MEMORY) &&
3350  (p->extensions & EXT_EXTERNAL_FD_SEM))
3351  return vulkan_transfer_data_to_cuda(hwfc, dst, src);
3352 #endif
3353  default:
3354  if (dst->hw_frames_ctx)
3355  return AVERROR(ENOSYS);
3356  else
3357  return vulkan_transfer_data(hwfc, src, dst, 1);
3358  }
3359 }
3360 
3362  AVHWFramesContext *src_fc, int flags)
3363 {
3364  return vulkan_frames_init(dst_fc);
3365 }
3366 
3368 {
3369  return av_mallocz(sizeof(AVVkFrame));
3370 }
3371 
3374  .name = "Vulkan",
3375 
3376  .device_hwctx_size = sizeof(AVVulkanDeviceContext),
3377  .device_priv_size = sizeof(VulkanDevicePriv),
3378  .frames_hwctx_size = sizeof(AVVulkanFramesContext),
3379  .frames_priv_size = sizeof(VulkanFramesPriv),
3380 
3381  .device_init = &vulkan_device_init,
3382  .device_create = &vulkan_device_create,
3383  .device_derive = &vulkan_device_derive,
3384 
3385  .frames_get_constraints = &vulkan_frames_get_constraints,
3386  .frames_init = vulkan_frames_init,
3387  .frames_get_buffer = vulkan_get_buffer,
3388  .frames_uninit = vulkan_frames_uninit,
3389 
3390  .transfer_get_formats = vulkan_transfer_get_formats,
3391  .transfer_data_to = vulkan_transfer_data_to,
3392  .transfer_data_from = vulkan_transfer_data_from,
3393 
3394  .map_to = vulkan_map_to,
3395  .map_from = vulkan_map_from,
3396  .frames_derive_to = &vulkan_frames_derive_to,
3397 
3398  .pix_fmts = (const enum AVPixelFormat []) {
3401  },
3402 };
static void flush(AVCodecContext *avctx)
static const char *const format[]
Definition: af_aiir.c:457
static cqueue * cqueue_create(int size, int max_size)
#define av_unused
Definition: attributes.h:131
uint8_t
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define f(width, name)
Definition: cbs_vp9.c:255
uint64_t layout
#define fail()
Definition: checkasm.h:133
#define FFMIN(a, b)
Definition: common.h:105
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define NULL
Definition: coverity.c:32
#define CHECK_CU(x)
Definition: cuviddec.c:108
static enum AVPixelFormat pix_fmt
static AVFrame * frame
static float sub(float src0, float src1)
int8_t exp
Definition: eval.c:74
const char * usage
Definition: floatimg_cmp.c:60
#define AV_NUM_DATA_POINTERS
Definition: frame.h:319
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:125
AVBufferRef * av_buffer_create(uint8_t *data, buffer_size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:29
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
Definition: buffer.c:379
AVBufferPool * av_buffer_pool_init2(buffer_size_t size, void *opaque, AVBufferRef *(*alloc)(void *opaque, buffer_size_t size), void(*pool_free)(void *opaque))
Allocate and initialize a buffer pool with a more complex allocator.
Definition: buffer.c:245
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition: frame.c:799
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:253
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:478
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:190
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:376
int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
Compute the size of an image line with format pix_fmt and width width for the plane plane.
Definition: imgutils.c:76
char * av_strtok(char *s, const char *delim, char **saveptr)
Split the string into several tokens which can be accessed by successive calls to av_strtok().
Definition: avstring.c:186
#define LIBAVUTIL_VERSION_MINOR
Definition: version.h:82
#define LIBAVUTIL_VERSION_MAJOR
Definition: version.h:81
#define LIBAVUTIL_VERSION_MICRO
Definition: version.h:83
int index
Definition: gxfenc.c:89
static const int weights[]
Definition: hevc_pel.c:32
int ff_hwframe_map_create(AVBufferRef *hwframe_ref, AVFrame *dst, const AVFrame *src, void(*unmap)(AVHWFramesContext *ctx, HWMapDescriptor *hwmap), void *priv)
Definition: hwcontext.c:741
int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
Map a hardware frame.
Definition: hwcontext.c:793
int ff_hwframe_map_replace(AVFrame *dst, const AVFrame *src)
Replace the current hwmap of dst with the one from src, used for indirect mappings like VAAPI->(DRM)-...
Definition: hwcontext.c:927
@ AV_HWFRAME_MAP_READ
The mapping must be readable.
Definition: hwcontext.h:524
@ AV_HWFRAME_MAP_WRITE
The mapping must be writeable.
Definition: hwcontext.h:528
@ AV_HWFRAME_MAP_OVERWRITE
The mapped frame will be overwritten completely in subsequent operations, so the current frame data n...
Definition: hwcontext.h:534
AVHWFrameTransferDirection
Definition: hwcontext.h:415
@ AV_HWDEVICE_TYPE_VULKAN
Definition: hwcontext.h:39
@ AV_HWDEVICE_TYPE_CUDA
Definition: hwcontext.h:30
@ AV_HWDEVICE_TYPE_DRM
Definition: hwcontext.h:36
@ AV_HWDEVICE_TYPE_VAAPI
Definition: hwcontext.h:31
FFmpeg internal API for CUDA.
API-specific header for AV_HWDEVICE_TYPE_DRM.
cl_device_type type
API-specific header for AV_HWDEVICE_TYPE_VAAPI.
const VDPAUPixFmtMap * map
static int submit_exec_ctx(AVHWFramesContext *hwfc, VulkanExecCtx *cmd, VkSubmitInfo *s_info, int synchronous)
static int wait_start_exec_ctx(AVHWFramesContext *hwfc, VulkanExecCtx *cmd)
#define CASE(VAL)
static int vulkan_frames_init(AVHWFramesContext *hwfc)
#define SEARCH_FLAGS(expr, out)
static VkBool32 VKAPI_CALL vk_dbg_callback(VkDebugUtilsMessageSeverityFlagBitsEXT severity, VkDebugUtilsMessageTypeFlagsEXT messageType, const VkDebugUtilsMessengerCallbackDataEXT *data, void *priv)
static int alloc_mem(AVHWDeviceContext *ctx, VkMemoryRequirements *req, VkMemoryPropertyFlagBits req_flags, const void *alloc_extension, VkMemoryPropertyFlagBits *mem_flags, VkDeviceMemory *mem)
static int vulkan_transfer_data(AVHWFramesContext *hwfc, const AVFrame *vkf, const AVFrame *swf, int from)
static int vulkan_transfer_data_to(AVHWFramesContext *hwfc, AVFrame *dst, const AVFrame *src)
static void free_buf(void *opaque, uint8_t *data)
static int vulkan_map_frame_to_mem(AVHWFramesContext *hwfc, AVFrame *dst, const AVFrame *src, int flags)
#define CHECK_QUEUE(type, n)
static int alloc_bind_mem(AVHWFramesContext *hwfc, AVVkFrame *f, void *alloc_pnext, size_t alloc_pnext_stride)
static int create_instance(AVHWDeviceContext *ctx, AVDictionary *opts)
static void try_export_flags(AVHWFramesContext *hwfc, VkExternalMemoryHandleTypeFlags *comp_handle_types, VkExternalMemoryHandleTypeFlagBits *iexp, VkExternalMemoryHandleTypeFlagBits exp)
static int unmap_buffers(AVHWDeviceContext *ctx, AVBufferRef **bufs, int nb_buffers, int flush)
static VkCommandBuffer get_buf_exec_ctx(AVHWFramesContext *hwfc, VulkanExecCtx *cmd)
static int transfer_image_buf(AVHWFramesContext *hwfc, const AVFrame *f, AVBufferRef **bufs, size_t *buf_offsets, const int *buf_stride, int w, int h, enum AVPixelFormat pix_fmt, int to_buf)
static void unref_exec_ctx_deps(AVHWFramesContext *hwfc, VulkanExecCtx *cmd)
static int create_buf(AVHWDeviceContext *ctx, AVBufferRef **buf, VkBufferUsageFlags usage, VkMemoryPropertyFlagBits flags, size_t size, uint32_t req_memory_bits, int host_mapped, void *create_pnext, void *alloc_pnext)
static int find_device(AVHWDeviceContext *ctx, VulkanDeviceSelection *select)
#define ADD_VAL_TO_LIST(list, count, val)
static void vulkan_frame_free(void *opaque, uint8_t *data)
static int vulkan_device_create_internal(AVHWDeviceContext *ctx, VulkanDeviceSelection *dev_select, AVDictionary *opts, int flags)
static int check_extensions(AVHWDeviceContext *ctx, int dev, AVDictionary *opts, const char *const **dst, uint32_t *num, int debug)
static size_t get_req_buffer_size(VulkanDevicePriv *p, int *stride, int height)
static int search_queue_families(AVHWDeviceContext *ctx, VkDeviceCreateInfo *cd)
static int vulkan_frames_get_constraints(AVHWDeviceContext *ctx, const void *hwconfig, AVHWFramesConstraints *constraints)
static int vulkan_transfer_get_formats(AVHWFramesContext *hwfc, enum AVHWFrameTransferDirection dir, enum AVPixelFormat **formats)
static int vulkan_device_derive(AVHWDeviceContext *ctx, AVHWDeviceContext *src_ctx, AVDictionary *opts, int flags)
static int vulkan_device_init(AVHWDeviceContext *ctx)
static int vulkan_frames_derive_to(AVHWFramesContext *dst_fc, AVHWFramesContext *src_fc, int flags)
static int vulkan_device_create(AVHWDeviceContext *ctx, const char *device, AVDictionary *opts, int flags)
static int vulkan_transfer_data_from(AVHWFramesContext *hwfc, AVFrame *dst, const AVFrame *src)
static int vulkan_map_from(AVHWFramesContext *hwfc, AVFrame *dst, const AVFrame *src, int flags)
static int create_exec_ctx(AVHWFramesContext *hwfc, VulkanExecCtx *cmd, int queue_family_index, int num_queues)
AVVkFrame * av_vk_frame_alloc(void)
Allocates a single AVVkFrame and initializes everything as 0.
static int map_buffers(AVHWDeviceContext *ctx, AVBufferRef **bufs, uint8_t *mem[], int nb_buffers, int invalidate)
const VkFormat vkfmts[4]
static void get_plane_wh(int *w, int *h, enum AVPixelFormat format, int frame_w, int frame_h, int plane)
static int prepare_frame(AVHWFramesContext *hwfc, VulkanExecCtx *ectx, AVVkFrame *frame, enum PrepMode pmode)
static AVBufferRef * vulkan_pool_alloc(void *opaque, buffer_size_t size)
static const char * vk_ret2str(VkResult res)
enum AVPixelFormat pixfmt
static const char * vk_dev_type(enum VkPhysicalDeviceType type)
static void vulkan_device_free(AVHWDeviceContext *ctx)
#define GET_QUEUE_COUNT(hwctx, graph, comp, tx)
const VkFormat * av_vkfmt_from_pixfmt(enum AVPixelFormat p)
Returns the format of each image up to the number of planes for a given sw_format.
#define ADD_QUEUE(fidx, graph, comp, tx)
#define VK_LOAD_PFN(inst, name)
#define COPY_FEATURE(DST, NAME)
static void vulkan_frames_uninit(AVHWFramesContext *hwfc)
static int pixfmt_is_supported(AVVulkanDeviceContext *hwctx, enum AVPixelFormat p, int linear)
const HWContextType ff_hwcontext_type_vulkan
static int add_buf_dep_exec_ctx(AVHWFramesContext *hwfc, VulkanExecCtx *cmd, AVBufferRef *const *deps, int nb_deps)
static const VulkanOptExtension optional_instance_exts[]
static void vulkan_free_internal(AVVkFrameInternal *internal)
VulkanExtensions
@ EXT_HOST_QUERY_RESET
@ EXT_PUSH_DESCRIPTORS
@ EXT_EXTERNAL_HOST_MEMORY
@ EXT_EXTERNAL_FD_MEMORY
@ EXT_DRM_MODIFIER_FLAGS
@ EXT_EXTERNAL_DMABUF_MEMORY
@ EXT_NO_FLAG
@ EXT_EXTERNAL_FD_SEM
static void vulkan_unmap_frame(AVHWFramesContext *hwfc, HWMapDescriptor *hwmap)
static const struct @311 vk_pixfmt_map[]
static int vulkan_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame)
static const VulkanOptExtension optional_device_exts[]
static int create_frame(AVHWFramesContext *hwfc, AVVkFrame **frame, VkImageTiling tiling, VkImageUsageFlagBits usage, void *create_pnext)
@ PREP_MODE_WRITE
@ PREP_MODE_RO_SHADER
@ PREP_MODE_EXTERNAL_EXPORT
static int vulkan_map_to(AVHWFramesContext *hwfc, AVFrame *dst, const AVFrame *src, int flags)
#define DEFAULT_USAGE_FLAGS
static void free_exec_ctx(AVHWFramesContext *hwfc, VulkanExecCtx *cmd)
API-specific header for AV_HWDEVICE_TYPE_VULKAN.
misc image utilities
int i
Definition: input.c:407
static int linear(InterplayACMContext *s, unsigned ind, unsigned col)
Definition: interplayacm.c:129
const char * from
Definition: jacosubdec.c:66
#define SIZE_SPECIFIER
Definition: internal.h:193
int buffer_size_t
Definition: internal.h:306
const char * desc
Definition: libsvtav1.c:83
uint8_t w
Definition: llviddspenc.c:39
static const struct @323 planes[]
int stride
Definition: mace.c:144
#define FFALIGN(x, a)
Definition: macros.h:48
#define htype
int dummy
Definition: motion.c:64
const char data[16]
Definition: mxf.c:142
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2613
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2489
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition: pixdesc.h:148
#define AV_PIX_FMT_FLAG_PLANAR
At least one pixel component is not in the first data plane.
Definition: pixdesc.h:144
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:410
#define AV_PIX_FMT_GBRPF32
Definition: pixfmt.h:428
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:406
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:399
#define AV_PIX_FMT_GBRAP16
Definition: pixfmt.h:421
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:438
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:441
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:403
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:436
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:404
#define AV_PIX_FMT_P010
Definition: pixfmt.h:448
#define AV_PIX_FMT_P016
Definition: pixfmt.h:449
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:400
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:389
#define AV_PIX_FMT_BGR32
Definition: pixfmt.h:374
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:385
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:437
#define AV_PIX_FMT_X2RGB10
Definition: pixfmt.h:452
#define AV_PIX_FMT_GRAYF32
Definition: pixfmt.h:431
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:89
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_VULKAN
Vulkan hardware images.
Definition: pixfmt.h:356
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
@ AV_PIX_FMT_NV42
as above, but U and V bytes are swapped
Definition: pixfmt.h:349
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
@ AV_PIX_FMT_NV21
as above, but U and V bytes are swapped
Definition: pixfmt.h:90
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:240
@ AV_PIX_FMT_NB
number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of...
Definition: pixfmt.h:363
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:101
@ AV_PIX_FMT_CUDA
HW acceleration through CUDA.
Definition: pixfmt.h:235
@ AV_PIX_FMT_NV24
planar YUV 4:4:4, 24bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:348
@ AV_PIX_FMT_DRM_PRIME
DRM-managed buffers exposed through PRIME buffer sharing.
Definition: pixfmt.h:328
@ AV_PIX_FMT_NV16
interleaved chroma YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:201
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:177
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:215
@ AV_PIX_FMT_RGB0
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:238
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:176
@ AV_PIX_FMT_VAAPI
Definition: pixfmt.h:122
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
#define AV_PIX_FMT_YUVA422P12
Definition: pixfmt.h:439
#define AV_PIX_FMT_BGR565
Definition: pixfmt.h:391
#define AV_PIX_FMT_RGB565
Definition: pixfmt.h:386
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:411
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:383
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:443
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:442
#define AV_PIX_FMT_YUVA444P12
Definition: pixfmt.h:440
#define AV_PIX_FMT_GBRAPF32
Definition: pixfmt.h:429
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:412
#define AV_PIX_FMT_0BGR32
Definition: pixfmt.h:377
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:402
#define fp
Definition: regdef.h:44
formats
Definition: signature.h:48
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
Definition: buffer.h:84
uint8_t * data
The data buffer.
Definition: buffer.h:92
This struct is allocated as AVHWDeviceContext.hwctx.
AVCUDADeviceContextInternal * internal
int fd
File descriptor of DRM device.
DRM frame descriptor.
AVDRMObjectDescriptor objects[AV_DRM_MAX_PLANES]
Array of objects making up the frame.
int nb_layers
Number of layers in the frame.
int nb_objects
Number of DRM objects making up this frame.
AVDRMLayerDescriptor layers[AV_DRM_MAX_PLANES]
Array of layers in the frame.
AVDRMPlaneDescriptor planes[AV_DRM_MAX_PLANES]
Array of planes in this layer.
int nb_planes
Number of planes in the layer.
uint32_t format
Format of the layer (DRM_FORMAT_*).
int fd
DRM PRIME fd for the object.
Definition: hwcontext_drm.h:52
size_t size
Total size of the object.
Definition: hwcontext_drm.h:58
uint64_t format_modifier
Format modifier applied to the object (DRM_FORMAT_MOD_*).
Definition: hwcontext_drm.h:65
ptrdiff_t pitch
Pitch (linesize) of this plane.
Definition: hwcontext_drm.h:87
int object_index
Index of the object containing this plane in the objects array of the enclosing frame descriptor.
Definition: hwcontext_drm.h:79
ptrdiff_t offset
Offset within that object of this plane.
Definition: hwcontext_drm.h:83
char * value
Definition: dict.h:83
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1699
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int width
Definition: frame.h:376
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame.
Definition: frame.h:657
int height
Definition: frame.h:376
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:509
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:391
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:61
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:92
AVHWDeviceInternal * internal
Private data used internally by libavutil.
Definition: hwcontext.h:71
enum AVHWDeviceType type
This field identifies the underlying API used for hardware access.
Definition: hwcontext.h:79
This struct describes the constraints on hardware frames attached to a given device with a hardware-s...
Definition: hwcontext.h:453
int max_width
The maximum size of frames in this hw_frames_ctx.
Definition: hwcontext.h:478
enum AVPixelFormat * valid_hw_formats
A list of possible values for format in the hw_frames_ctx, terminated by AV_PIX_FMT_NONE.
Definition: hwcontext.h:458
enum AVPixelFormat * valid_sw_formats
A list of possible values for sw_format in the hw_frames_ctx, terminated by AV_PIX_FMT_NONE.
Definition: hwcontext.h:465
int min_width
The minimum size of frames in this hw_frames_ctx.
Definition: hwcontext.h:471
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:124
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
Definition: hwcontext.h:162
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:222
AVHWFramesInternal * internal
Private data used internally by libavutil.
Definition: hwcontext.h:134
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:229
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Definition: hwcontext.h:149
AVBufferPool * pool
A pool from which the frames are allocated by av_hwframe_get_buffer().
Definition: hwcontext.h:190
AVBufferPool * pool_internal
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
VAAPI connection details.
VADisplay display
The VADisplay handle, to be filled by the user.
size_t size[AV_NUM_DATA_POINTERS]
VkSemaphore sem[AV_NUM_DATA_POINTERS]
Synchronization semaphores.
struct AVVkFrameInternal * internal
Internal data.
VkDeviceMemory mem[AV_NUM_DATA_POINTERS]
Memory backing the images.
Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
VkPhysicalDevice phys_dev
Physical device.
int queue_family_tx_index
Queue family index to use for transfer operations, and the amount of queues enabled.
int queue_family_index
Queue family index for graphics.
const char *const * enabled_dev_extensions
Enabled device extensions.
const VkAllocationCallbacks * alloc
Custom memory allocator, else NULL.
VkDevice act_dev
Active device.
int queue_family_comp_index
Queue family index for compute ops, and the amount of queues enabled.
const char *const * enabled_inst_extensions
Enabled instance extensions.
VkInstance inst
Vulkan instance.
VkPhysicalDeviceFeatures2 device_features
This structure should be set to the set of features that present and enabled during device creation.
Allocated as AVHWFramesContext.hwctx, used to set pool-specific options.
void * create_pnext
Extension data for image creation.
VkImageTiling tiling
Controls the tiling of allocated frames.
void * alloc_pnext[AV_NUM_DATA_POINTERS]
Extension data for memory allocation.
VkImageUsageFlagBits usage
Defines extra usage of output frames.
enum AVHWDeviceType type
void * priv
Hardware-specific private data associated with the mapping.
VkMemoryPropertyFlagBits flags
VkDeviceMemory mem
VkPhysicalDeviceMemoryProperties mprops
VkPhysicalDeviceExternalMemoryHostPropertiesEXT hprops
VkPhysicalDeviceProperties2 props
VkDebugUtilsMessengerEXT debug_ctx
uint8_t uuid[VK_UUID_SIZE]
VkCommandPool pool
VkCommandBuffer * bufs
VulkanQueueCtx * queues
VulkanExecCtx download_ctx
VulkanExecCtx conv_ctx
VulkanExecCtx upload_ctx
AVVkFrame * frame
AVBufferRef ** buf_deps
#define av_free(p)
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[11]
Definition: aes_ctr.c:27
#define src
Definition: vp8dsp.c:255
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
AVFormatContext * ctx
Definition: movenc.c:48
AVDictionary * opts
Definition: movenc.c:50
#define height
int size
if(ret< 0)
Definition: vf_mcdeint.c:282
static int mod(int a, int b)
Modulo operation with only positive remainders.
Definition: vf_v360.c:749