FFmpeg
hwcontext_vulkan.h
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 #ifndef AVUTIL_HWCONTEXT_VULKAN_H
20 #define AVUTIL_HWCONTEXT_VULKAN_H
21 
22 #if defined(_WIN32) && !defined(VK_USE_PLATFORM_WIN32_KHR)
23 #define VK_USE_PLATFORM_WIN32_KHR
24 #endif
25 #include <vulkan/vulkan.h>
26 
27 #include "pixfmt.h"
28 #include "frame.h"
29 #include "hwcontext.h"
30 
31 typedef struct AVVkFrame AVVkFrame;
32 
33 typedef struct AVVulkanDeviceQueueFamily {
34  /* Queue family index */
35  int idx;
36  /* Number of queues in the queue family in use */
37  int num;
38  /* Queue family capabilities. Must be non-zero.
39  * Flags may be removed to indicate the queue family may not be used
40  * for a given purpose. */
41  VkQueueFlagBits flags;
42  /* Vulkan implementations are allowed to list multiple video queues
43  * which differ in what they can encode or decode. */
44  VkVideoCodecOperationFlagBitsKHR video_caps;
46 
47 /**
48  * @file
49  * API-specific header for AV_HWDEVICE_TYPE_VULKAN.
50  *
51  * For user-allocated pools, AVHWFramesContext.pool must return AVBufferRefs
52  * with the data pointer set to an AVVkFrame.
53  */
54 
55 /**
56  * Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
57  * All of these can be set before init to change what the context uses
58  */
59 typedef struct AVVulkanDeviceContext {
60  /**
61  * Custom memory allocator, else NULL
62  */
63  const VkAllocationCallbacks *alloc;
64 
65  /**
66  * Pointer to a vkGetInstanceProcAddr loading function.
67  * If unset, will dynamically load and use libvulkan.
68  */
69  PFN_vkGetInstanceProcAddr get_proc_addr;
70 
71  /**
72  * Vulkan instance. Must be at least version 1.3.
73  */
74  VkInstance inst;
75 
76  /**
77  * Physical device
78  */
79  VkPhysicalDevice phys_dev;
80 
81  /**
82  * Active device
83  */
84  VkDevice act_dev;
85 
86  /**
87  * This structure should be set to the set of features that present and enabled
88  * during device creation. When a device is created by FFmpeg, it will default to
89  * enabling all that are present of the shaderImageGatherExtended,
90  * fragmentStoresAndAtomics, shaderInt64 and vertexPipelineStoresAndAtomics features.
91  */
92  VkPhysicalDeviceFeatures2 device_features;
93 
94  /**
95  * Enabled instance extensions.
96  * If supplying your own device context, set this to an array of strings, with
97  * each entry containing the specified Vulkan extension string to enable.
98  * Duplicates are possible and accepted.
99  * If no extensions are enabled, set these fields to NULL, and 0 respectively.
100  * av_vk_get_optional_instance_extensions() can be used to enumerate extensions
101  * that FFmpeg may use if enabled.
102  */
103  const char * const *enabled_inst_extensions;
105 
106  /**
107  * Enabled device extensions. By default, VK_KHR_external_memory_fd,
108  * VK_EXT_external_memory_dma_buf, VK_EXT_image_drm_format_modifier,
109  * VK_KHR_external_semaphore_fd and VK_EXT_external_memory_host are enabled if found.
110  * If supplying your own device context, these fields takes the same format as
111  * the above fields, with the same conditions that duplicates are possible
112  * and accepted, and that NULL and 0 respectively means no extensions are enabled.
113  * av_vk_get_optional_device_extensions() can be used to enumerate extensions
114  * that FFmpeg may use if enabled.
115  */
116  const char * const *enabled_dev_extensions;
118 
119 #if FF_API_VULKAN_SYNC_QUEUES
120  /**
121  * Locks a queue, preventing other threads from submitting any command
122  * buffers to this queue.
123  * If set to NULL, will be set to lavu-internal functions that utilize a
124  * mutex.
125  *
126  * Deprecated: use VK_KHR_internally_synchronized_queues.
127  */
129  void (*lock_queue)(struct AVHWDeviceContext *ctx, uint32_t queue_family, uint32_t index);
130 
131  /**
132  * Similar to lock_queue(), unlocks a queue. Must only be called after locking.
133  *
134  * Deprecated: use VK_KHR_internally_synchronized_queues.
135  */
137  void (*unlock_queue)(struct AVHWDeviceContext *ctx, uint32_t queue_family, uint32_t index);
138 #endif
139 
140  /**
141  * Queue families used. Must be preferentially ordered. List may contain
142  * duplicates.
143  *
144  * For compatibility reasons, all the enabled queue families listed above
145  * (queue_family_(tx/comp/encode/decode)_index) must also be included in
146  * this list until they're removed after deprecation.
147  */
149  int nb_qf;
150 
151  /* Queue creation flags, for vkGetDeviceQueue2. */
152  VkDeviceQueueCreateFlags queue_flags;
154 
155 /**
156  * Defines the behaviour of frame allocation.
157  */
158 typedef enum AVVkFrameFlags {
159  /* Unless this flag is set, autodetected flags will be OR'd based on the
160  * device and tiling during av_hwframe_ctx_init(). */
161  AV_VK_FRAME_FLAG_NONE = (1ULL << 0),
162 
163  /* Disables multiplane images.
164  * This is required to export/import images from CUDA. */
167 
168 /**
169  * Allocated as AVHWFramesContext.hwctx, used to set pool-specific options
170  */
171 typedef struct AVVulkanFramesContext {
172  /**
173  * Controls the tiling of allocated frames.
174  * If left as VK_IMAGE_TILING_OPTIMAL (0), will use optimal tiling.
175  * Can be set to VK_IMAGE_TILING_LINEAR to force linear images,
176  * or VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT to force DMABUF-backed
177  * images.
178  * @note Imported frames from other APIs ignore this.
179  */
180  VkImageTiling tiling;
181 
182  /**
183  * Defines extra usage of output frames. If non-zero, all flags MUST be
184  * supported by the VkFormat. Regardless, frames will always have the
185  * following usage flags enabled, if supported by the format:
186  * - VK_IMAGE_USAGE_SAMPLED_BIT
187  * - VK_IMAGE_USAGE_STORAGE_BIT
188  * - VK_IMAGE_USAGE_TRANSFER_SRC_BIT
189  * - VK_IMAGE_USAGE_TRANSFER_DST_BIT
190  */
191  VkImageUsageFlagBits usage;
192 
193  /**
194  * Extension data for image creation.
195  * If DRM tiling is used, a VkImageDrmFormatModifierListCreateInfoEXT structure
196  * can be added to specify the exact modifier to use.
197  *
198  * Additional structures may be added at av_hwframe_ctx_init() time,
199  * which will be freed automatically on uninit(), so users must only free
200  * any structures they've allocated themselves.
201  */
203 
204  /**
205  * Extension data for memory allocation. Must have as many entries as
206  * the number of planes of the sw_format.
207  * This will be chained to VkExportMemoryAllocateInfo, which is used
208  * to make all pool images exportable to other APIs if the necessary
209  * extensions are present in enabled_dev_extensions.
210  */
212 
213  /**
214  * A combination of AVVkFrameFlags. Unless AV_VK_FRAME_FLAG_NONE is set,
215  * autodetected flags will be OR'd based on the device and tiling during
216  * av_hwframe_ctx_init().
217  */
219 
220  /**
221  * Flags to set during image creation. If unset, defaults to
222  * VK_IMAGE_CREATE_ALIAS_BIT.
223  */
224  VkImageCreateFlags img_flags;
225 
226  /**
227  * Vulkan format for each image. MUST be compatible with the pixel format.
228  * If unset, will be automatically set.
229  * There are at most two compatible formats for a frame - a multiplane
230  * format, and a single-plane multi-image format.
231  */
233 
234  /**
235  * Number of layers each image will have.
236  */
238 
239  /**
240  * Locks a frame, preventing other threads from changing frame properties.
241  * Users SHOULD only ever lock just before command submission in order
242  * to get accurate frame properties, and unlock immediately after command
243  * submission without waiting for it to finish.
244  *
245  * If unset, will be set to lavu-internal functions that utilize a mutex.
246  */
247  void (*lock_frame)(struct AVHWFramesContext *fc, AVVkFrame *vkf);
248 
249  /**
250  * Similar to lock_frame(), unlocks a frame. Must only be called after locking.
251  */
252  void (*unlock_frame)(struct AVHWFramesContext *fc, AVVkFrame *vkf);
254 
255 /*
256  * Frame structure.
257  *
258  * @note the size of this structure is not part of the ABI, to allocate
259  * you must use @av_vk_frame_alloc().
260  */
261 struct AVVkFrame {
262  /**
263  * Vulkan images to which the memory is bound to.
264  * May be one for multiplane formats, or multiple.
265  */
267 
268  /**
269  * Tiling for the frame.
270  */
271  VkImageTiling tiling;
272 
273  /**
274  * Memory backing the images. Either one, or as many as there are planes
275  * in the sw_format.
276  * In case of having multiple VkImages, but one memory, the offset field
277  * will indicate the bound offset for each image.
278  */
279  VkDeviceMemory mem[AV_NUM_DATA_POINTERS];
281 
282  /**
283  * OR'd flags for all memory allocated
284  */
285  VkMemoryPropertyFlagBits flags;
286 
287  /**
288  * Updated after every barrier. One per VkImage.
289  */
290  VkAccessFlagBits2 access[AV_NUM_DATA_POINTERS];
291  VkImageLayout layout[AV_NUM_DATA_POINTERS];
292 
293  /**
294  * Synchronization timeline semaphores, one for each VkImage.
295  * Must not be freed manually. Must be waited on at every submission using
296  * the value in sem_value, and must be signalled at every submission,
297  * using an incremented value.
298  */
299  VkSemaphore sem[AV_NUM_DATA_POINTERS];
300 
301  /**
302  * Up to date semaphore value at which each image becomes accessible.
303  * One per VkImage.
304  * Clients must wait on this value when submitting a command queue,
305  * and increment it when signalling.
306  */
308 
309  /**
310  * Describes the binding offset of each image to the VkDeviceMemory.
311  * One per VkImage.
312  */
314 
315  /**
316  * Queue family of the images. Must be VK_QUEUE_FAMILY_IGNORED if
317  * the image was allocated with the CONCURRENT concurrency option.
318  * One per VkImage.
319  */
321 
322  /**
323  * Internal data. Not to be accessed by users in any way.
324  */
325  struct AVVkFrameInternal *internal;
326 };
327 
328 /**
329  * Allocates a single AVVkFrame and initializes everything as 0.
330  * @note Must be freed via av_free()
331  */
333 
334 /**
335  * Returns the optimal per-plane Vulkan format for a given sw_format,
336  * one for each plane.
337  * Returns NULL on unsupported formats.
338  */
340 
341 /**
342  * Returns an array of optional Vulkan instance extensions that FFmpeg
343  * may use if enabled.
344  * @note Must be freed via av_free()
345  */
346 const char **av_vk_get_optional_instance_extensions(int *count);
347 
348 /**
349  * Returns an array of optional Vulkan device extensions that FFmpeg
350  * may use if enabled.
351  * @note Must be freed via av_free()
352  */
353 const char **av_vk_get_optional_device_extensions(int *count);
354 
355 #endif /* AVUTIL_HWCONTEXT_VULKAN_H */
AVVulkanDeviceContext::phys_dev
VkPhysicalDevice phys_dev
Physical device.
Definition: hwcontext_vulkan.h:79
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AV_VK_FRAME_FLAG_DISABLE_MULTIPLANE
@ AV_VK_FRAME_FLAG_DISABLE_MULTIPLANE
Definition: hwcontext_vulkan.h:165
AVVulkanDeviceContext::get_proc_addr
PFN_vkGetInstanceProcAddr get_proc_addr
Pointer to a vkGetInstanceProcAddr loading function.
Definition: hwcontext_vulkan.h:69
AVVulkanFramesContext::create_pnext
void * create_pnext
Extension data for image creation.
Definition: hwcontext_vulkan.h:202
AVVulkanDeviceContext::inst
VkInstance inst
Vulkan instance.
Definition: hwcontext_vulkan.h:74
AVVulkanFramesContext::lock_frame
void(* lock_frame)(struct AVHWFramesContext *fc, AVVkFrame *vkf)
Locks a frame, preventing other threads from changing frame properties.
Definition: hwcontext_vulkan.h:247
AVVulkanDeviceContext::unlock_queue
attribute_deprecated void(* unlock_queue)(struct AVHWDeviceContext *ctx, uint32_t queue_family, uint32_t index)
Similar to lock_queue(), unlocks a queue.
Definition: hwcontext_vulkan.h:137
AVVulkanDeviceContext::alloc
const VkAllocationCallbacks * alloc
Custom memory allocator, else NULL.
Definition: hwcontext_vulkan.h:63
AVVkFrame::img
VkImage img[AV_NUM_DATA_POINTERS]
Vulkan images to which the memory is bound to.
Definition: hwcontext_vulkan.h:266
AVVkFrame::offset
ptrdiff_t offset[AV_NUM_DATA_POINTERS]
Describes the binding offset of each image to the VkDeviceMemory.
Definition: hwcontext_vulkan.h:313
AVVulkanDeviceContext::lock_queue
attribute_deprecated void(* lock_queue)(struct AVHWDeviceContext *ctx, uint32_t queue_family, uint32_t index)
Locks a queue, preventing other threads from submitting any command buffers to this queue.
Definition: hwcontext_vulkan.h:129
AVVulkanFramesContext::flags
AVVkFrameFlags flags
A combination of AVVkFrameFlags.
Definition: hwcontext_vulkan.h:218
AVVkFrame::mem
VkDeviceMemory mem[AV_NUM_DATA_POINTERS]
Memory backing the images.
Definition: hwcontext_vulkan.h:279
AVVulkanFramesContext
Allocated as AVHWFramesContext.hwctx, used to set pool-specific options.
Definition: hwcontext_vulkan.h:171
AVHWDeviceContext
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:63
AVVulkanDeviceQueueFamily::num
int num
Definition: hwcontext_vulkan.h:37
AV_VK_FRAME_FLAG_NONE
@ AV_VK_FRAME_FLAG_NONE
Definition: hwcontext_vulkan.h:161
av_vk_get_optional_device_extensions
const char ** av_vk_get_optional_device_extensions(int *count)
Returns an array of optional Vulkan device extensions that FFmpeg may use if enabled.
Definition: hwcontext_vulkan.c:776
fc
#define fc(width, name, range_min, range_max)
Definition: cbs_av1.c:494
AVVulkanFramesContext::unlock_frame
void(* unlock_frame)(struct AVHWFramesContext *fc, AVVkFrame *vkf)
Similar to lock_frame(), unlocks a frame.
Definition: hwcontext_vulkan.h:252
AVVulkanFramesContext::img_flags
VkImageCreateFlags img_flags
Flags to set during image creation.
Definition: hwcontext_vulkan.h:224
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
AVVulkanDeviceContext
Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
Definition: hwcontext_vulkan.h:59
AVVulkanDeviceContext::nb_enabled_dev_extensions
int nb_enabled_dev_extensions
Definition: hwcontext_vulkan.h:117
AVVkFrameInternal
Definition: hwcontext_vulkan.c:202
AVVkFrame::flags
VkMemoryPropertyFlagBits flags
OR'd flags for all memory allocated.
Definition: hwcontext_vulkan.h:285
AVVulkanFramesContext::alloc_pnext
void * alloc_pnext[AV_NUM_DATA_POINTERS]
Extension data for memory allocation.
Definition: hwcontext_vulkan.h:211
AVVulkanDeviceContext::nb_qf
int nb_qf
Definition: hwcontext_vulkan.h:149
AVVulkanDeviceContext::enabled_inst_extensions
const char *const * enabled_inst_extensions
Enabled instance extensions.
Definition: hwcontext_vulkan.h:103
AVVulkanFramesContext::format
VkFormat format[AV_NUM_DATA_POINTERS]
Vulkan format for each image.
Definition: hwcontext_vulkan.h:232
AVVulkanFramesContext::usage
VkImageUsageFlagBits usage
Defines extra usage of output frames.
Definition: hwcontext_vulkan.h:191
AVVkFrame::access
VkAccessFlagBits2 access[AV_NUM_DATA_POINTERS]
Updated after every barrier.
Definition: hwcontext_vulkan.h:290
AVVulkanDeviceContext::qf
AVVulkanDeviceQueueFamily qf[64]
Queue families used.
Definition: hwcontext_vulkan.h:148
index
int index
Definition: gxfenc.c:90
AVVkFrame::size
size_t size[AV_NUM_DATA_POINTERS]
Definition: hwcontext_vulkan.h:280
av_vkfmt_from_pixfmt
const VkFormat * av_vkfmt_from_pixfmt(enum AVPixelFormat p)
Returns the optimal per-plane Vulkan format for a given sw_format, one for each plane.
Definition: hwcontext_stub.c:30
AVVkFrame
Definition: hwcontext_vulkan.h:261
AV_NUM_DATA_POINTERS
#define AV_NUM_DATA_POINTERS
Definition: frame.h:467
frame.h
av_vk_frame_alloc
AVVkFrame * av_vk_frame_alloc(void)
Allocates a single AVVkFrame and initializes everything as 0.
Definition: hwcontext_stub.c:35
AVVkFrame::queue_family
uint32_t queue_family[AV_NUM_DATA_POINTERS]
Queue family of the images.
Definition: hwcontext_vulkan.h:320
AVVulkanDeviceQueueFamily::idx
int idx
Definition: hwcontext_vulkan.h:35
VkFormat
enum VkFormat VkFormat
Definition: hwcontext_stub.c:25
AVVkFrameFlags
AVVkFrameFlags
Defines the behaviour of frame allocation.
Definition: hwcontext_vulkan.h:158
AVVkFrame::sem
VkSemaphore sem[AV_NUM_DATA_POINTERS]
Synchronization timeline semaphores, one for each VkImage.
Definition: hwcontext_vulkan.h:299
AVHWFramesContext
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:118
AVVkFrame::tiling
VkImageTiling tiling
Tiling for the frame.
Definition: hwcontext_vulkan.h:271
pixfmt.h
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53
AVVulkanFramesContext::tiling
VkImageTiling tiling
Controls the tiling of allocated frames.
Definition: hwcontext_vulkan.h:180
AVVkFrame::sem_value
uint64_t sem_value[AV_NUM_DATA_POINTERS]
Up to date semaphore value at which each image becomes accessible.
Definition: hwcontext_vulkan.h:307
AVVulkanDeviceContext::enabled_dev_extensions
const char *const * enabled_dev_extensions
Enabled device extensions.
Definition: hwcontext_vulkan.h:116
AVVulkanFramesContext::nb_layers
int nb_layers
Number of layers each image will have.
Definition: hwcontext_vulkan.h:237
AVVkFrame::layout
VkImageLayout layout[AV_NUM_DATA_POINTERS]
Definition: hwcontext_vulkan.h:291
AVVulkanDeviceContext::act_dev
VkDevice act_dev
Active device.
Definition: hwcontext_vulkan.h:84
AVVulkanDeviceContext::nb_enabled_inst_extensions
int nb_enabled_inst_extensions
Definition: hwcontext_vulkan.h:104
hwcontext.h
AVVulkanDeviceQueueFamily
Definition: hwcontext_vulkan.h:33
av_vk_get_optional_instance_extensions
const char ** av_vk_get_optional_instance_extensions(int *count)
Returns an array of optional Vulkan instance extensions that FFmpeg may use if enabled.
Definition: hwcontext_vulkan.c:762
AVVulkanDeviceContext::device_features
VkPhysicalDeviceFeatures2 device_features
This structure should be set to the set of features that present and enabled during device creation.
Definition: hwcontext_vulkan.h:92
attribute_deprecated
#define attribute_deprecated
Definition: attributes.h:135
AVVulkanDeviceQueueFamily::flags
VkQueueFlagBits flags
Definition: hwcontext_vulkan.h:41
AVVulkanDeviceContext::queue_flags
VkDeviceQueueCreateFlags queue_flags
Definition: hwcontext_vulkan.h:152
AVVulkanDeviceQueueFamily::video_caps
VkVideoCodecOperationFlagBitsKHR video_caps
Definition: hwcontext_vulkan.h:44