FFmpeg
vulkan_video.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 "libavutil/mem.h"
20 #include "vulkan_video.h"
21 
22 #define ASPECT_2PLANE (VK_IMAGE_ASPECT_PLANE_0_BIT | VK_IMAGE_ASPECT_PLANE_1_BIT)
23 #define ASPECT_3PLANE (VK_IMAGE_ASPECT_PLANE_0_BIT | VK_IMAGE_ASPECT_PLANE_1_BIT | VK_IMAGE_ASPECT_PLANE_2_BIT)
24 
25 static const struct FFVkFormatMapEntry {
28  VkImageAspectFlags aspect;
29 } vk_format_map[] = {
30  /* Gray formats */
31  { VK_FORMAT_R8_UNORM, AV_PIX_FMT_GRAY8, VK_IMAGE_ASPECT_COLOR_BIT },
32  { VK_FORMAT_R16_UNORM, AV_PIX_FMT_GRAY16, VK_IMAGE_ASPECT_COLOR_BIT },
33  { VK_FORMAT_R32_SFLOAT, AV_PIX_FMT_GRAYF32, VK_IMAGE_ASPECT_COLOR_BIT },
34 
35  /* RGB formats */
36  { VK_FORMAT_B8G8R8A8_UNORM, AV_PIX_FMT_BGRA, VK_IMAGE_ASPECT_COLOR_BIT },
37  { VK_FORMAT_R8G8B8A8_UNORM, AV_PIX_FMT_RGBA, VK_IMAGE_ASPECT_COLOR_BIT },
38  { VK_FORMAT_R8G8B8_UNORM, AV_PIX_FMT_RGB24, VK_IMAGE_ASPECT_COLOR_BIT },
39  { VK_FORMAT_B8G8R8_UNORM, AV_PIX_FMT_BGR24, VK_IMAGE_ASPECT_COLOR_BIT },
40  { VK_FORMAT_R16G16B16_UNORM, AV_PIX_FMT_RGB48, VK_IMAGE_ASPECT_COLOR_BIT },
41  { VK_FORMAT_R16G16B16A16_UNORM, AV_PIX_FMT_RGBA64, VK_IMAGE_ASPECT_COLOR_BIT },
42  { VK_FORMAT_R5G6B5_UNORM_PACK16, AV_PIX_FMT_RGB565, VK_IMAGE_ASPECT_COLOR_BIT },
43  { VK_FORMAT_B5G6R5_UNORM_PACK16, AV_PIX_FMT_BGR565, VK_IMAGE_ASPECT_COLOR_BIT },
44  { VK_FORMAT_B8G8R8A8_UNORM, AV_PIX_FMT_BGR0, VK_IMAGE_ASPECT_COLOR_BIT },
45  { VK_FORMAT_R8G8B8A8_UNORM, AV_PIX_FMT_RGB0, VK_IMAGE_ASPECT_COLOR_BIT },
46  { VK_FORMAT_A2R10G10B10_UNORM_PACK32, AV_PIX_FMT_X2RGB10, VK_IMAGE_ASPECT_COLOR_BIT },
47 
48  /* Planar RGB */
49  { VK_FORMAT_R8_UNORM, AV_PIX_FMT_GBRAP, VK_IMAGE_ASPECT_COLOR_BIT },
50  { VK_FORMAT_R16_UNORM, AV_PIX_FMT_GBRAP16, VK_IMAGE_ASPECT_COLOR_BIT },
51  { VK_FORMAT_R32_SFLOAT, AV_PIX_FMT_GBRPF32, VK_IMAGE_ASPECT_COLOR_BIT },
52  { VK_FORMAT_R32_SFLOAT, AV_PIX_FMT_GBRAPF32, VK_IMAGE_ASPECT_COLOR_BIT },
53 
54  /* Two-plane 420 YUV at 8, 10, 12 and 16 bits */
55  { VK_FORMAT_G8_B8R8_2PLANE_420_UNORM, AV_PIX_FMT_NV12, ASPECT_2PLANE },
56  { VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16, AV_PIX_FMT_P010, ASPECT_2PLANE },
57  { VK_FORMAT_G12X4_B12X4R12X4_2PLANE_420_UNORM_3PACK16, AV_PIX_FMT_P012, ASPECT_2PLANE },
58  { VK_FORMAT_G16_B16R16_2PLANE_420_UNORM, AV_PIX_FMT_P016, ASPECT_2PLANE },
59 
60  /* Two-plane 422 YUV at 8, 10 and 16 bits */
61  { VK_FORMAT_G8_B8R8_2PLANE_422_UNORM, AV_PIX_FMT_NV16, ASPECT_2PLANE },
62  { VK_FORMAT_G10X6_B10X6R10X6_2PLANE_422_UNORM_3PACK16, AV_PIX_FMT_P210, ASPECT_2PLANE },
63  { VK_FORMAT_G12X4_B12X4R12X4_2PLANE_422_UNORM_3PACK16, AV_PIX_FMT_P212, ASPECT_2PLANE },
64  { VK_FORMAT_G16_B16R16_2PLANE_422_UNORM, AV_PIX_FMT_P216, ASPECT_2PLANE },
65 
66  /* Two-plane 444 YUV at 8, 10 and 16 bits */
67  { VK_FORMAT_G8_B8R8_2PLANE_444_UNORM, AV_PIX_FMT_NV24, ASPECT_2PLANE },
68  { VK_FORMAT_G10X6_B10X6R10X6_2PLANE_444_UNORM_3PACK16, AV_PIX_FMT_P410, ASPECT_2PLANE },
69  { VK_FORMAT_G12X4_B12X4R12X4_2PLANE_444_UNORM_3PACK16, AV_PIX_FMT_P412, ASPECT_2PLANE },
70  { VK_FORMAT_G16_B16R16_2PLANE_444_UNORM, AV_PIX_FMT_P416, ASPECT_2PLANE },
71 
72  /* Three-plane 420, 422, 444 at 8, 10, 12 and 16 bits */
73  { VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM, AV_PIX_FMT_YUV420P, ASPECT_3PLANE },
74  { VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM, AV_PIX_FMT_YUV420P10, ASPECT_3PLANE },
75  { VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM, AV_PIX_FMT_YUV420P12, ASPECT_3PLANE },
76  { VK_FORMAT_G16_B16_R16_3PLANE_420_UNORM, AV_PIX_FMT_YUV420P16, ASPECT_3PLANE },
77  { VK_FORMAT_G8_B8_R8_3PLANE_422_UNORM, AV_PIX_FMT_YUV422P, ASPECT_3PLANE },
78  { VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM, AV_PIX_FMT_YUV422P10, ASPECT_3PLANE },
79  { VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM, AV_PIX_FMT_YUV422P12, ASPECT_3PLANE },
80  { VK_FORMAT_G16_B16_R16_3PLANE_422_UNORM, AV_PIX_FMT_YUV422P16, ASPECT_3PLANE },
81  { VK_FORMAT_G8_B8_R8_3PLANE_444_UNORM, AV_PIX_FMT_YUV444P, ASPECT_3PLANE },
82  { VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM, AV_PIX_FMT_YUV444P10, ASPECT_3PLANE },
83  { VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM, AV_PIX_FMT_YUV444P12, ASPECT_3PLANE },
84  { VK_FORMAT_G16_B16_R16_3PLANE_444_UNORM, AV_PIX_FMT_YUV444P16, ASPECT_3PLANE },
85 
86  /* Single plane 422 at 8, 10, 12 and 16 bits */
87  { VK_FORMAT_G8B8G8R8_422_UNORM, AV_PIX_FMT_YUYV422, VK_IMAGE_ASPECT_COLOR_BIT },
88  { VK_FORMAT_B8G8R8G8_422_UNORM, AV_PIX_FMT_UYVY422, VK_IMAGE_ASPECT_COLOR_BIT },
89  { VK_FORMAT_G10X6B10X6G10X6R10X6_422_UNORM_4PACK16, AV_PIX_FMT_Y210, VK_IMAGE_ASPECT_COLOR_BIT },
90  { VK_FORMAT_G12X4B12X4G12X4R12X4_422_UNORM_4PACK16, AV_PIX_FMT_Y212, VK_IMAGE_ASPECT_COLOR_BIT },
91  { VK_FORMAT_G16B16G16R16_422_UNORM, AV_PIX_FMT_Y216, VK_IMAGE_ASPECT_COLOR_BIT },
92 
93  /* Single plane 444 at 10 and 12 bits */
94  { VK_FORMAT_A2R10G10B10_UNORM_PACK32, AV_PIX_FMT_XV30, VK_IMAGE_ASPECT_COLOR_BIT },
95  { VK_FORMAT_R12X4G12X4B12X4A12X4_UNORM_4PACK16, AV_PIX_FMT_XV36, VK_IMAGE_ASPECT_COLOR_BIT },
96 };
98 
100 {
101  for (int i = 0; i < nb_vk_format_map; i++)
102  if (vk_format_map[i].vkf == vkf)
103  return vk_format_map[i].pixfmt;
104  return AV_PIX_FMT_NONE;
105 }
106 
107 VkImageAspectFlags ff_vk_aspect_bits_from_vkfmt(VkFormat vkf)
108 {
109  for (int i = 0; i < nb_vk_format_map; i++)
110  if (vk_format_map[i].vkf == vkf)
111  return vk_format_map[i].aspect;
112  return VK_IMAGE_ASPECT_NONE;
113 }
114 
115 VkVideoChromaSubsamplingFlagBitsKHR ff_vk_subsampling_from_av_desc(const AVPixFmtDescriptor *desc)
116 {
117  if (desc->nb_components == 1)
118  return VK_VIDEO_CHROMA_SUBSAMPLING_MONOCHROME_BIT_KHR;
119  else if (!desc->log2_chroma_w && !desc->log2_chroma_h)
120  return VK_VIDEO_CHROMA_SUBSAMPLING_444_BIT_KHR;
121  else if (!desc->log2_chroma_w && desc->log2_chroma_h == 1)
122  return VK_VIDEO_CHROMA_SUBSAMPLING_422_BIT_KHR;
123  else if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 1)
124  return VK_VIDEO_CHROMA_SUBSAMPLING_420_BIT_KHR;
125  return VK_VIDEO_CHROMA_SUBSAMPLING_INVALID_KHR;
126 }
127 
128 VkVideoComponentBitDepthFlagBitsKHR ff_vk_depth_from_av_depth(int depth)
129 {
130  switch (depth) {
131  case 8: return VK_VIDEO_COMPONENT_BIT_DEPTH_8_BIT_KHR;
132  case 10: return VK_VIDEO_COMPONENT_BIT_DEPTH_10_BIT_KHR;
133  case 12: return VK_VIDEO_COMPONENT_BIT_DEPTH_12_BIT_KHR;
134  default: break;
135  }
136  return VK_VIDEO_COMPONENT_BIT_DEPTH_INVALID_KHR;
137 }
138 
139 int ff_vk_h264_level_to_av(StdVideoH264LevelIdc level)
140 {
141  switch (level) {
142  case STD_VIDEO_H264_LEVEL_IDC_1_0: return 10;
143  case STD_VIDEO_H264_LEVEL_IDC_1_1: return 11;
144  case STD_VIDEO_H264_LEVEL_IDC_1_2: return 12;
145  case STD_VIDEO_H264_LEVEL_IDC_1_3: return 13;
146  case STD_VIDEO_H264_LEVEL_IDC_2_0: return 20;
147  case STD_VIDEO_H264_LEVEL_IDC_2_1: return 21;
148  case STD_VIDEO_H264_LEVEL_IDC_2_2: return 22;
149  case STD_VIDEO_H264_LEVEL_IDC_3_0: return 30;
150  case STD_VIDEO_H264_LEVEL_IDC_3_1: return 31;
151  case STD_VIDEO_H264_LEVEL_IDC_3_2: return 32;
152  case STD_VIDEO_H264_LEVEL_IDC_4_0: return 40;
153  case STD_VIDEO_H264_LEVEL_IDC_4_1: return 41;
154  case STD_VIDEO_H264_LEVEL_IDC_4_2: return 42;
155  case STD_VIDEO_H264_LEVEL_IDC_5_0: return 50;
156  case STD_VIDEO_H264_LEVEL_IDC_5_1: return 51;
157  case STD_VIDEO_H264_LEVEL_IDC_5_2: return 52;
158  case STD_VIDEO_H264_LEVEL_IDC_6_0: return 60;
159  case STD_VIDEO_H264_LEVEL_IDC_6_1: return 61;
160  default:
161  case STD_VIDEO_H264_LEVEL_IDC_6_2: return 62;
162  }
163 }
164 
165 StdVideoH264LevelIdc ff_vk_h264_level_to_vk(int level_idc)
166 {
167  switch (level_idc) {
168  case 10: return STD_VIDEO_H264_LEVEL_IDC_1_0;
169  case 11: return STD_VIDEO_H264_LEVEL_IDC_1_1;
170  case 12: return STD_VIDEO_H264_LEVEL_IDC_1_2;
171  case 13: return STD_VIDEO_H264_LEVEL_IDC_1_3;
172  case 20: return STD_VIDEO_H264_LEVEL_IDC_2_0;
173  case 21: return STD_VIDEO_H264_LEVEL_IDC_2_1;
174  case 22: return STD_VIDEO_H264_LEVEL_IDC_2_2;
175  case 30: return STD_VIDEO_H264_LEVEL_IDC_3_0;
176  case 31: return STD_VIDEO_H264_LEVEL_IDC_3_1;
177  case 32: return STD_VIDEO_H264_LEVEL_IDC_3_2;
178  case 40: return STD_VIDEO_H264_LEVEL_IDC_4_0;
179  case 41: return STD_VIDEO_H264_LEVEL_IDC_4_1;
180  case 42: return STD_VIDEO_H264_LEVEL_IDC_4_2;
181  case 50: return STD_VIDEO_H264_LEVEL_IDC_5_0;
182  case 51: return STD_VIDEO_H264_LEVEL_IDC_5_1;
183  case 52: return STD_VIDEO_H264_LEVEL_IDC_5_2;
184  case 60: return STD_VIDEO_H264_LEVEL_IDC_6_0;
185  case 61: return STD_VIDEO_H264_LEVEL_IDC_6_1;
186  default:
187  case 62: return STD_VIDEO_H264_LEVEL_IDC_6_2;
188  }
189 }
190 
191 int ff_vk_h265_level_to_av(StdVideoH265LevelIdc level)
192 {
193  switch (level) {
194  case STD_VIDEO_H265_LEVEL_IDC_1_0: return 10;
195  case STD_VIDEO_H265_LEVEL_IDC_2_0: return 20;
196  case STD_VIDEO_H265_LEVEL_IDC_2_1: return 21;
197  case STD_VIDEO_H265_LEVEL_IDC_3_0: return 30;
198  case STD_VIDEO_H265_LEVEL_IDC_3_1: return 31;
199  case STD_VIDEO_H265_LEVEL_IDC_4_0: return 40;
200  case STD_VIDEO_H265_LEVEL_IDC_4_1: return 41;
201  case STD_VIDEO_H265_LEVEL_IDC_5_0: return 50;
202  case STD_VIDEO_H265_LEVEL_IDC_5_1: return 51;
203  case STD_VIDEO_H265_LEVEL_IDC_6_0: return 60;
204  case STD_VIDEO_H265_LEVEL_IDC_6_1: return 61;
205  default:
206  case STD_VIDEO_H265_LEVEL_IDC_6_2: return 62;
207  }
208 }
209 
210 StdVideoH265LevelIdc ff_vk_h265_level_to_vk(int level_idc)
211 {
212  switch (level_idc) {
213  case 10: return STD_VIDEO_H265_LEVEL_IDC_1_0;
214  case 20: return STD_VIDEO_H265_LEVEL_IDC_2_0;
215  case 21: return STD_VIDEO_H265_LEVEL_IDC_2_1;
216  case 30: return STD_VIDEO_H265_LEVEL_IDC_3_0;
217  case 31: return STD_VIDEO_H265_LEVEL_IDC_3_1;
218  case 40: return STD_VIDEO_H265_LEVEL_IDC_4_0;
219  case 41: return STD_VIDEO_H265_LEVEL_IDC_4_1;
220  case 50: return STD_VIDEO_H265_LEVEL_IDC_5_0;
221  case 51: return STD_VIDEO_H265_LEVEL_IDC_5_1;
222  case 60: return STD_VIDEO_H265_LEVEL_IDC_6_0;
223  case 61: return STD_VIDEO_H265_LEVEL_IDC_6_1;
224  default:
225  case 62: return STD_VIDEO_H265_LEVEL_IDC_6_2;
226  }
227 }
228 
229 StdVideoH264ProfileIdc ff_vk_h264_profile_to_vk(int profile)
230 {
231  switch (profile) {
232  case AV_PROFILE_H264_CONSTRAINED_BASELINE: return STD_VIDEO_H264_PROFILE_IDC_BASELINE;
233  case AV_PROFILE_H264_MAIN: return STD_VIDEO_H264_PROFILE_IDC_MAIN;
234  case AV_PROFILE_H264_HIGH: return STD_VIDEO_H264_PROFILE_IDC_HIGH;
235  case AV_PROFILE_H264_HIGH_444_PREDICTIVE: return STD_VIDEO_H264_PROFILE_IDC_HIGH_444_PREDICTIVE;
236  default: return STD_VIDEO_H264_PROFILE_IDC_INVALID;
237  }
238 }
239 
240 StdVideoH265ProfileIdc ff_vk_h265_profile_to_vk(int profile)
241 {
242  switch (profile) {
243  case AV_PROFILE_HEVC_MAIN: return STD_VIDEO_H265_PROFILE_IDC_MAIN;
244  case AV_PROFILE_HEVC_MAIN_10: return STD_VIDEO_H265_PROFILE_IDC_MAIN_10;
245  case AV_PROFILE_HEVC_REXT: return STD_VIDEO_H265_PROFILE_IDC_FORMAT_RANGE_EXTENSIONS;
246  default: return STD_VIDEO_H265_PROFILE_IDC_INVALID;
247  }
248 }
249 
250 int ff_vk_h264_profile_to_av(StdVideoH264ProfileIdc profile)
251 {
252  switch (profile) {
253  case STD_VIDEO_H264_PROFILE_IDC_BASELINE: return AV_PROFILE_H264_CONSTRAINED_BASELINE;
254  case STD_VIDEO_H264_PROFILE_IDC_MAIN: return AV_PROFILE_H264_MAIN;
255  case STD_VIDEO_H264_PROFILE_IDC_HIGH: return AV_PROFILE_H264_HIGH;
256  case STD_VIDEO_H264_PROFILE_IDC_HIGH_444_PREDICTIVE: return AV_PROFILE_H264_HIGH_444_PREDICTIVE;
257  default: return AV_PROFILE_UNKNOWN;
258  }
259 }
260 
261 int ff_vk_h265_profile_to_av(StdVideoH264ProfileIdc profile)
262 {
263  switch (profile) {
264  case STD_VIDEO_H265_PROFILE_IDC_MAIN: return AV_PROFILE_HEVC_MAIN;
265  case STD_VIDEO_H265_PROFILE_IDC_MAIN_10: return AV_PROFILE_HEVC_MAIN_10;
266  case STD_VIDEO_H265_PROFILE_IDC_FORMAT_RANGE_EXTENSIONS: return AV_PROFILE_HEVC_REXT;
267  default: return AV_PROFILE_UNKNOWN;
268  }
269 }
270 
272  VkQueueFlagBits family, VkVideoCodecOperationFlagBitsKHR caps)
273 {
274  for (int i = 0; i < s->hwctx->nb_qf; i++) {
275  if ((s->hwctx->qf[i].flags & family) &&
276  (s->hwctx->qf[i].video_caps & caps)) {
277  qf->queue_family = s->hwctx->qf[i].idx;
278  qf->nb_queues = s->hwctx->qf[i].num;
279  return 0;
280  }
281  }
282  return AVERROR(ENOTSUP);
283 }
284 
285 int ff_vk_create_view(FFVulkanContext *s, FFVkVideoCommon *common,
286  VkImageView *view, VkImageAspectFlags *aspect,
287  AVVkFrame *src, VkFormat vkf, int is_dpb)
288 {
289  VkResult ret;
290  FFVulkanFunctions *vk = &s->vkfn;
291  VkImageAspectFlags aspect_mask = ff_vk_aspect_bits_from_vkfmt(vkf);
292 
293  VkSamplerYcbcrConversionInfo yuv_sampler_info = {
294  .sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO,
295  .conversion = common->yuv_sampler,
296  };
297  VkImageViewCreateInfo img_view_create_info = {
298  .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
299  .pNext = &yuv_sampler_info,
300  .viewType = common->layered_dpb && is_dpb ?
301  VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D,
302  .format = vkf,
303  .image = src->img[0],
304  .components = (VkComponentMapping) {
305  .r = VK_COMPONENT_SWIZZLE_IDENTITY,
306  .g = VK_COMPONENT_SWIZZLE_IDENTITY,
307  .b = VK_COMPONENT_SWIZZLE_IDENTITY,
308  .a = VK_COMPONENT_SWIZZLE_IDENTITY,
309  },
310  .subresourceRange = (VkImageSubresourceRange) {
311  .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
312  .baseArrayLayer = 0,
313  .layerCount = common->layered_dpb && is_dpb ?
314  VK_REMAINING_ARRAY_LAYERS : 1,
315  .levelCount = 1,
316  },
317  };
318 
319  ret = vk->CreateImageView(s->hwctx->act_dev, &img_view_create_info,
320  s->hwctx->alloc, view);
321  if (ret != VK_SUCCESS)
322  return AVERROR_EXTERNAL;
323 
324  *aspect = aspect_mask;
325 
326  return 0;
327 }
328 
330  FFVkVideoCommon *common)
331 {
332  FFVulkanFunctions *vk = &s->vkfn;
333 
334  if (common->session) {
335  vk->DestroyVideoSessionKHR(s->hwctx->act_dev, common->session,
336  s->hwctx->alloc);
337  common->session = VK_NULL_HANDLE;
338  }
339 
340  if (common->nb_mem && common->mem)
341  for (int i = 0; i < common->nb_mem; i++)
342  vk->FreeMemory(s->hwctx->act_dev, common->mem[i], s->hwctx->alloc);
343 
344  av_freep(&common->mem);
345 
346  if (common->layered_view)
347  vk->DestroyImageView(s->hwctx->act_dev, common->layered_view,
348  s->hwctx->alloc);
349 
350  av_frame_free(&common->layered_frame);
351 
352  av_buffer_unref(&common->dpb_hwfc_ref);
353 
354  if (common->yuv_sampler)
355  vk->DestroySamplerYcbcrConversion(s->hwctx->act_dev, common->yuv_sampler,
356  s->hwctx->alloc);
357 }
358 
360  FFVkVideoCommon *common,
361  VkVideoSessionCreateInfoKHR *session_create)
362 {
363  int err;
364  VkResult ret;
365  FFVulkanFunctions *vk = &s->vkfn;
366  VkVideoSessionMemoryRequirementsKHR *mem = NULL;
367  VkBindVideoSessionMemoryInfoKHR *bind_mem = NULL;
368 
369  int cxpos = 0, cypos = 0;
370  VkSamplerYcbcrConversionCreateInfo yuv_sampler_info = {
371  .sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_CREATE_INFO,
372  .components = ff_comp_identity_map,
373  .ycbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY,
374  .ycbcrRange = avctx->color_range == AVCOL_RANGE_MPEG, /* Ignored */
375  .format = session_create->pictureFormat,
376  };
377 
378  /* Create identity YUV sampler
379  * (VkImageViews of YUV image formats require it, even if it does nothing) */
381  yuv_sampler_info.xChromaOffset = cxpos >> 7;
382  yuv_sampler_info.yChromaOffset = cypos >> 7;
383  ret = vk->CreateSamplerYcbcrConversion(s->hwctx->act_dev, &yuv_sampler_info,
384  s->hwctx->alloc, &common->yuv_sampler);
385  if (ret != VK_SUCCESS)
386  return AVERROR_EXTERNAL;
387 
388  /* Create session */
389  ret = vk->CreateVideoSessionKHR(s->hwctx->act_dev, session_create,
390  s->hwctx->alloc, &common->session);
391  if (ret != VK_SUCCESS)
392  return AVERROR_EXTERNAL;
393 
394  /* Get memory requirements */
395  ret = vk->GetVideoSessionMemoryRequirementsKHR(s->hwctx->act_dev,
396  common->session,
397  &common->nb_mem,
398  NULL);
399  if (ret != VK_SUCCESS) {
400  err = AVERROR_EXTERNAL;
401  goto fail;
402  }
403 
404  /* Allocate all memory needed to actually allocate memory */
405  common->mem = av_mallocz(sizeof(*common->mem)*common->nb_mem);
406  if (!common->mem) {
407  err = AVERROR(ENOMEM);
408  goto fail;
409  }
410  mem = av_mallocz(sizeof(*mem)*common->nb_mem);
411  if (!mem) {
412  err = AVERROR(ENOMEM);
413  goto fail;
414  }
415  bind_mem = av_mallocz(sizeof(*bind_mem)*common->nb_mem);
416  if (!bind_mem) {
417  err = AVERROR(ENOMEM);
418  goto fail;
419  }
420 
421  /* Set the needed fields to get the memory requirements */
422  for (int i = 0; i < common->nb_mem; i++) {
423  mem[i] = (VkVideoSessionMemoryRequirementsKHR) {
424  .sType = VK_STRUCTURE_TYPE_VIDEO_SESSION_MEMORY_REQUIREMENTS_KHR,
425  };
426  }
427 
428  /* Finally get the memory requirements */
429  ret = vk->GetVideoSessionMemoryRequirementsKHR(s->hwctx->act_dev,
430  common->session, &common->nb_mem,
431  mem);
432  if (ret != VK_SUCCESS) {
433  err = AVERROR_EXTERNAL;
434  goto fail;
435  }
436 
437  /* Now allocate each requested memory.
438  * For ricing, could pool together memory that ends up in the same index. */
439  for (int i = 0; i < common->nb_mem; i++) {
440  err = ff_vk_alloc_mem(s, &mem[i].memoryRequirements,
441  UINT32_MAX, NULL, NULL, &common->mem[i]);
442  if (err < 0)
443  goto fail;
444 
445  bind_mem[i] = (VkBindVideoSessionMemoryInfoKHR) {
446  .sType = VK_STRUCTURE_TYPE_BIND_VIDEO_SESSION_MEMORY_INFO_KHR,
447  .memory = common->mem[i],
448  .memoryBindIndex = mem[i].memoryBindIndex,
449  .memoryOffset = 0,
450  .memorySize = mem[i].memoryRequirements.size,
451  };
452 
453  av_log(avctx, AV_LOG_VERBOSE, "Allocating %"PRIu64" bytes in bind index %i for video session\n",
454  bind_mem[i].memorySize, bind_mem[i].memoryBindIndex);
455  }
456 
457  /* Bind the allocated memory */
458  ret = vk->BindVideoSessionMemoryKHR(s->hwctx->act_dev, common->session,
459  common->nb_mem, bind_mem);
460  if (ret != VK_SUCCESS) {
461  err = AVERROR_EXTERNAL;
462  goto fail;
463  }
464 
465  av_freep(&mem);
466  av_freep(&bind_mem);
467 
468  return 0;
469 
470 fail:
471  av_freep(&mem);
472  av_freep(&bind_mem);
473 
474  ff_vk_video_common_uninit(s, common);
475  return err;
476 }
AV_PIX_FMT_GBRAP16
#define AV_PIX_FMT_GBRAP16
Definition: pixfmt.h:525
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
level
uint8_t level
Definition: svq3.c:205
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ff_comp_identity_map
const VkComponentMapping ff_comp_identity_map
Definition: vulkan.c:27
ff_vk_video_qf_init
int ff_vk_video_qf_init(FFVulkanContext *s, FFVkQueueFamilyCtx *qf, VkQueueFlagBits family, VkVideoCodecOperationFlagBitsKHR caps)
Chooses a QF and loads it into a context.
Definition: vulkan_video.c:271
AV_PROFILE_H264_MAIN
#define AV_PROFILE_H264_MAIN
Definition: defs.h:112
AV_PROFILE_HEVC_MAIN
#define AV_PROFILE_HEVC_MAIN
Definition: defs.h:159
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
ff_vk_depth_from_av_depth
VkVideoComponentBitDepthFlagBitsKHR ff_vk_depth_from_av_depth(int depth)
Get Vulkan's bit depth from an [8:12] integer.
Definition: vulkan_video.c:128
AV_PIX_FMT_Y216
#define AV_PIX_FMT_Y216
Definition: pixfmt.h:558
level_idc
int level_idc
Definition: h264_levels.c:29
ff_vk_create_view
int ff_vk_create_view(FFVulkanContext *s, FFVkVideoCommon *common, VkImageView *view, VkImageAspectFlags *aspect, AVVkFrame *src, VkFormat vkf, int is_dpb)
Creates image views for video frames.
Definition: vulkan_video.c:285
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:502
AV_PIX_FMT_XV30
#define AV_PIX_FMT_XV30
Definition: pixfmt.h:559
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:225
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:76
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:102
FFVkFormatMapEntry::vkf
VkFormat vkf
Definition: vulkan_video.c:26
AV_PIX_FMT_P212
#define AV_PIX_FMT_P212
Definition: pixfmt.h:568
ff_vk_h264_profile_to_vk
StdVideoH264ProfileIdc ff_vk_h264_profile_to_vk(int profile)
Convert profile from/to AV to Vulkan.
Definition: vulkan_video.c:229
ff_vk_subsampling_from_av_desc
VkVideoChromaSubsamplingFlagBitsKHR ff_vk_subsampling_from_av_desc(const AVPixFmtDescriptor *desc)
Get Vulkan's chroma subsampling from a pixfmt descriptor.
Definition: vulkan_video.c:115
ASPECT_3PLANE
#define ASPECT_3PLANE
Definition: vulkan_video.c:23
AV_PIX_FMT_GBRAP
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition: pixfmt.h:212
fail
#define fail()
Definition: checkasm.h:189
AV_PIX_FMT_GRAY16
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:486
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:505
AV_PIX_FMT_Y210
#define AV_PIX_FMT_Y210
Definition: pixfmt.h:556
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
AV_PIX_FMT_YUV422P16
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:514
AV_PROFILE_UNKNOWN
#define AV_PROFILE_UNKNOWN
Definition: defs.h:65
s
#define s(width, name)
Definition: cbs_vp9.c:198
ff_vk_video_common_init
av_cold int ff_vk_video_common_init(AVCodecContext *avctx, FFVulkanContext *s, FFVkVideoCommon *common, VkVideoSessionCreateInfoKHR *session_create)
Initialize video session, allocating and binding necessary memory.
Definition: vulkan_video.c:359
av_chroma_location_enum_to_pos
int av_chroma_location_enum_to_pos(int *xpos, int *ypos, enum AVChromaLocation pos)
Converts AVChromaLocation to swscale x/y chroma position.
Definition: pixdesc.c:3588
FFVkFormatMapEntry::pixfmt
enum AVPixelFormat pixfmt
Definition: vulkan_video.c:27
AV_PIX_FMT_YUV444P16
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:515
AV_PIX_FMT_YUV420P16
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:513
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
AV_PIX_FMT_GRAYF32
#define AV_PIX_FMT_GRAYF32
Definition: pixfmt.h:535
AV_PIX_FMT_RGBA
@ AV_PIX_FMT_RGBA
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:100
AV_PIX_FMT_RGBA64
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:492
ff_vk_alloc_mem
int ff_vk_alloc_mem(FFVulkanContext *s, VkMemoryRequirements *req, VkMemoryPropertyFlagBits req_flags, void *alloc_extension, VkMemoryPropertyFlagBits *mem_flags, VkDeviceMemory *mem)
Memory/buffer/image allocation helpers.
Definition: vulkan.c:905
NULL
#define NULL
Definition: coverity.c:32
ASPECT_2PLANE
#define ASPECT_2PLANE
Definition: vulkan_video.c:22
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:701
av_buffer_unref
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:139
ff_vk_video_common_uninit
av_cold void ff_vk_video_common_uninit(FFVulkanContext *s, FFVkVideoCommon *common)
Free video session and required resources.
Definition: vulkan_video.c:329
AV_PIX_FMT_YUYV422
@ AV_PIX_FMT_YUYV422
packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
Definition: pixfmt.h:74
ff_vk_h265_profile_to_av
int ff_vk_h265_profile_to_av(StdVideoH264ProfileIdc profile)
Definition: vulkan_video.c:261
AV_PIX_FMT_P410
#define AV_PIX_FMT_P410
Definition: pixfmt.h:567
AV_PIX_FMT_BGR0
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition: pixfmt.h:265
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:503
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
FFVulkanContext
Definition: vulkan.h:268
AV_PROFILE_HEVC_MAIN_10
#define AV_PROFILE_HEVC_MAIN_10
Definition: defs.h:160
AV_PROFILE_HEVC_REXT
#define AV_PROFILE_HEVC_REXT
Definition: defs.h:162
ff_vk_h264_profile_to_av
int ff_vk_h264_profile_to_av(StdVideoH264ProfileIdc profile)
Definition: vulkan_video.c:250
vk_format_map
static const struct FFVkFormatMapEntry vk_format_map[]
ff_vk_h265_level_to_av
int ff_vk_h265_level_to_av(StdVideoH265LevelIdc level)
Definition: vulkan_video.c:191
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:75
AV_PIX_FMT_P012
#define AV_PIX_FMT_P012
Definition: pixfmt.h:553
AVVkFrame
Definition: hwcontext_vulkan.h:302
AV_PIX_FMT_GBRPF32
#define AV_PIX_FMT_GBRPF32
Definition: pixfmt.h:532
AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:507
AV_PIX_FMT_RGB48
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:488
FFVkQueueFamilyCtx
Definition: vulkan.h:102
AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:509
AV_PIX_FMT_NV16
@ AV_PIX_FMT_NV16
interleaved chroma YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:198
ff_vk_h265_level_to_vk
StdVideoH265LevelIdc ff_vk_h265_level_to_vk(int level_idc)
Definition: vulkan_video.c:210
ff_vk_h264_level_to_vk
StdVideoH264LevelIdc ff_vk_h264_level_to_vk(int level_idc)
Definition: vulkan_video.c:165
AV_PIX_FMT_Y212
#define AV_PIX_FMT_Y212
Definition: pixfmt.h:557
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
AV_PIX_FMT_RGB0
@ AV_PIX_FMT_RGB0
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition: pixfmt.h:263
AV_PIX_FMT_P216
#define AV_PIX_FMT_P216
Definition: pixfmt.h:570
AV_PIX_FMT_P210
#define AV_PIX_FMT_P210
Definition: pixfmt.h:566
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
FFVkFormatMapEntry::aspect
VkImageAspectFlags aspect
Definition: vulkan_video.c:28
VkFormat
enum VkFormat VkFormat
Definition: hwcontext_stub.c:25
AV_PIX_FMT_NV24
@ 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:371
ff_vk_h264_level_to_av
int ff_vk_h264_level_to_av(StdVideoH264LevelIdc level)
Convert level from Vulkan to AV.
Definition: vulkan_video.c:139
AV_PIX_FMT_X2RGB10
#define AV_PIX_FMT_X2RGB10
Definition: pixfmt.h:563
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:708
AV_PIX_FMT_BGR565
#define AV_PIX_FMT_BGR565
Definition: pixfmt.h:494
profile
int profile
Definition: mxfenc.c:2233
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:700
AV_PIX_FMT_P016
#define AV_PIX_FMT_P016
Definition: pixfmt.h:554
AV_PIX_FMT_RGB565
#define AV_PIX_FMT_RGB565
Definition: pixfmt.h:489
FFVkFormatMapEntry
Definition: vulkan_video.c:25
AV_PROFILE_H264_HIGH_444_PREDICTIVE
#define AV_PROFILE_H264_HIGH_444_PREDICTIVE
Definition: defs.h:122
ret
ret
Definition: filter_design.txt:187
AV_PIX_FMT_NV12
@ 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:96
AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:506
AV_PIX_FMT_UYVY422
@ AV_PIX_FMT_UYVY422
packed YUV 4:2:2, 16bpp, Cb Y0 Cr Y1
Definition: pixfmt.h:88
FFVkQueueFamilyCtx::nb_queues
int nb_queues
Definition: vulkan.h:104
AVCodecContext
main external API structure.
Definition: avcodec.h:451
AV_PROFILE_H264_HIGH
#define AV_PROFILE_H264_HIGH
Definition: defs.h:114
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_PIX_FMT_GBRAPF32
#define AV_PIX_FMT_GBRAPF32
Definition: pixfmt.h:533
AV_PROFILE_H264_CONSTRAINED_BASELINE
#define AV_PROFILE_H264_CONSTRAINED_BASELINE
Definition: defs.h:111
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
nb_vk_format_map
static const int nb_vk_format_map
Definition: vulkan_video.c:97
AV_PIX_FMT_P010
#define AV_PIX_FMT_P010
Definition: pixfmt.h:552
vulkan_video.h
desc
const char * desc
Definition: libsvtav1.c:79
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:77
mem.h
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
AV_PIX_FMT_P416
#define AV_PIX_FMT_P416
Definition: pixfmt.h:571
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
AV_PIX_FMT_XV36
#define AV_PIX_FMT_XV36
Definition: pixfmt.h:560
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_PIX_FMT_P412
#define AV_PIX_FMT_P412
Definition: pixfmt.h:569
ff_vk_pix_fmt_from_vkfmt
enum AVPixelFormat ff_vk_pix_fmt_from_vkfmt(VkFormat vkf)
Get pixfmt from a Vulkan format.
Definition: vulkan_video.c:99
ff_vk_aspect_bits_from_vkfmt
VkImageAspectFlags ff_vk_aspect_bits_from_vkfmt(VkFormat vkf)
Get aspect bits which include all planes from a VkFormat.
Definition: vulkan_video.c:107
ff_vk_h265_profile_to_vk
StdVideoH265ProfileIdc ff_vk_h265_profile_to_vk(int profile)
Definition: vulkan_video.c:240
FFVulkanFunctions
Definition: vulkan_functions.h:263
src
#define src
Definition: vp8dsp.c:248
FFVkQueueFamilyCtx::queue_family
int queue_family
Definition: vulkan.h:103