FFmpeg
Loading...
Searching...
No Matches
vulkan_decode.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
20#include "libavutil/refstruct.h"
21#include "vulkan_video.h"
22#include "vulkan_decode.h"
23#include "config_components.h"
24#include "libavutil/avassert.h"
25#include "libavutil/mem.h"
27#include "av1dec.h"
28#include "h264dec.h"
29
30#define DECODER_IS_SDR(codec_id) \
31 (((codec_id) == AV_CODEC_ID_FFV1) || \
32 ((codec_id) == AV_CODEC_ID_DPX) || \
33 ((codec_id) == AV_CODEC_ID_APV) || \
34 ((codec_id) == AV_CODEC_ID_PRORES_RAW) || \
35 ((codec_id) == AV_CODEC_ID_PRORES))
36
37#if CONFIG_H264_VULKAN_HWACCEL
39#endif
40#if CONFIG_HEVC_VULKAN_HWACCEL
42#endif
43#if CONFIG_VP9_VULKAN_HWACCEL
45#endif
46#if CONFIG_AV1_VULKAN_HWACCEL
48#endif
49#if CONFIG_FFV1_VULKAN_HWACCEL
51#endif
52#if CONFIG_PRORES_RAW_VULKAN_HWACCEL
54#endif
55#if CONFIG_PRORES_VULKAN_HWACCEL
57#endif
58#if CONFIG_DPX_VULKAN_HWACCEL
60#endif
61#if CONFIG_APV_VULKAN_HWACCEL
63#endif
64
66#if CONFIG_H264_VULKAN_HWACCEL
68#endif
69#if CONFIG_HEVC_VULKAN_HWACCEL
71#endif
72#if CONFIG_VP9_VULKAN_HWACCEL
74#endif
75#if CONFIG_AV1_VULKAN_HWACCEL
77#endif
78#if CONFIG_FFV1_VULKAN_HWACCEL
80#endif
81#if CONFIG_PRORES_RAW_VULKAN_HWACCEL
83#endif
84#if CONFIG_PRORES_VULKAN_HWACCEL
86#endif
87#if CONFIG_DPX_VULKAN_HWACCEL
89#endif
90#if CONFIG_APV_VULKAN_HWACCEL
92#endif
93};
94
96 VkVideoDecodeH264ProfileInfoKHR h264_profile;
97 VkVideoDecodeH265ProfileInfoKHR h265_profile;
98#if CONFIG_VP9_VULKAN_HWACCEL
99 VkVideoDecodeVP9ProfileInfoKHR vp9_profile;
100#endif
101 VkVideoDecodeAV1ProfileInfoKHR av1_profile;
102
103 VkVideoDecodeUsageInfoKHR usage;
104 VkVideoProfileInfoKHR profile;
105 VkVideoProfileListInfoKHR profile_list;
107
109{
110 for (size_t i = 0; i < FF_ARRAY_ELEMS(dec_descs); i++)
111 if (dec_descs[i]->codec_id == codec_id)
112 return dec_descs[i];
113 av_assert1(!"no codec descriptor");
114 return NULL;
115}
116
117static const VkVideoProfileInfoKHR *get_video_profile(FFVulkanDecodeShared *ctx, enum AVCodecID codec_id)
118{
119 const VkVideoProfileListInfoKHR *profile_list;
120
121 VkStructureType profile_struct_type =
122 codec_id == AV_CODEC_ID_H264 ? VK_STRUCTURE_TYPE_VIDEO_DECODE_H264_PROFILE_INFO_KHR :
123 codec_id == AV_CODEC_ID_HEVC ? VK_STRUCTURE_TYPE_VIDEO_DECODE_H265_PROFILE_INFO_KHR :
124#if CONFIG_VP9_VULKAN_HWACCEL
125 codec_id == AV_CODEC_ID_VP9 ? VK_STRUCTURE_TYPE_VIDEO_DECODE_VP9_PROFILE_INFO_KHR :
126#endif
127 codec_id == AV_CODEC_ID_AV1 ? VK_STRUCTURE_TYPE_VIDEO_DECODE_AV1_PROFILE_INFO_KHR :
128 VK_STRUCTURE_TYPE_MAX_ENUM;
129 if (profile_struct_type == VK_STRUCTURE_TYPE_MAX_ENUM)
130 return NULL;
131
132 profile_list = ff_vk_find_struct(ctx->s.hwfc->create_pnext,
133 VK_STRUCTURE_TYPE_VIDEO_PROFILE_LIST_INFO_KHR);
134 if (!profile_list)
135 return NULL;
136
137 for (int i = 0; i < profile_list->profileCount; i++)
138 if (ff_vk_find_struct(profile_list->pProfiles[i].pNext, profile_struct_type))
139 return &profile_list->pProfiles[i];
140
141 return NULL;
142}
143
145{
146 FFVulkanDecodeContext *src_ctx = src->internal->hwaccel_priv_data;
147 FFVulkanDecodeContext *dst_ctx = dst->internal->hwaccel_priv_data;
148
149 av_refstruct_replace(&dst_ctx->shared_ctx, src_ctx->shared_ctx);
150
152
153 dst_ctx->dedicated_dpb = src_ctx->dedicated_dpb;
154 dst_ctx->external_fg = src_ctx->external_fg;
155
156 return 0;
157}
158
159int ff_vk_params_invalidate(AVCodecContext *avctx, int t, const uint8_t *b, uint32_t s)
160{
163 return 0;
164}
165
167{
169 FFVulkanFunctions *vk = &ctx->s.vkfn;
170
171 vkpic->dpb_frame = NULL;
172 vkpic->dpb_img = NULL;
173 vkpic->out_views = NULL;
174 vkpic->view.ref = VK_NULL_HANDLE;
175 vkpic->view.out = VK_NULL_HANDLE;
176
177 vkpic->invalidate_memory_ranges = vk->InvalidateMappedMemoryRanges;
178}
179
181 FFVulkanDecodePicture *vkpic, int is_current,
182 int alloc_dpb)
183{
184 int err;
186
187 vkpic->slices_size = 0;
188
189 /* If the decoder made a blank frame to make up for a missing ref, or the
190 * frame is the current frame so it's missing one, create a re-representation */
191 if (vkpic->view.ref)
192 return 0;
193
194 init_frame(dec, vkpic);
195
196 /* Refcounted, so executions keep it alive past the picture */
197 vkpic->out_views = ff_vk_imageviews_alloc(&ctx->s, 1);
198 if (!vkpic->out_views)
199 return AVERROR(ENOMEM);
200
201 if (ctx->common.layered_dpb && alloc_dpb) {
202 vkpic->view.ref = ctx->common.layered_view;
203 vkpic->view.aspect_ref = ctx->common.layered_aspect;
204 } else if (alloc_dpb) {
205 vkpic->dpb_img = av_refstruct_pool_get(ctx->common.dpb->img_pool);
206 if (!vkpic->dpb_img)
207 return AVERROR(ENOMEM);
208
209 /* The view is owned by the pool entry */
210 vkpic->view.ref = vkpic->dpb_img->view;
211 vkpic->view.aspect_ref = vkpic->dpb_img->aspect;
212 }
213
214 if (!alloc_dpb || is_current) {
216 AVVulkanFramesContext *hwfc = frames->hwctx;
217
218 err = ff_vk_create_view(&ctx->s,
219 &vkpic->out_views->views[0], &vkpic->view.aspect,
220 ((AVVkFrame *)pic->data[0])->img[0],
221 hwfc->format[0],
222 VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR |
223 (hwfc->usage & VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR),
224 0);
225 // the above fixes VUID-VkVideoBeginCodingInfoKHR-slotIndex-07245
226 if (err < 0)
227 return err;
228 vkpic->view.out = vkpic->out_views->views[0];
229
230 if (!alloc_dpb) {
231 vkpic->view.ref = vkpic->view.out;
232 vkpic->view.aspect_ref = vkpic->view.aspect;
233 }
234 }
235
236 return 0;
237}
238
240 const uint8_t *data, size_t size, int add_startcode,
241 uint32_t *nb_slices, const uint32_t **offsets)
242{
245
246 static const uint8_t startcode_prefix[3] = { 0x0, 0x0, 0x1 };
247 const size_t startcode_len = add_startcode ? sizeof(startcode_prefix) : 0;
248 const int nb = nb_slices ? *nb_slices : 0;
249 uint8_t *slices;
250 uint32_t *slice_off;
251 FFVkBuffer *vkbuf;
252
253 uint64_t new_size = vp->slices_size + startcode_len + size +
254 ctx->caps.minBitstreamBufferSizeAlignment;
255 new_size = FFALIGN(new_size, ctx->caps.minBitstreamBufferSizeAlignment);
256 if (new_size > INT32_MAX)
257 return AVERROR(ENOSYS);
258
259 if (offsets) {
260 slice_off = av_fast_realloc(dec->slice_off, &dec->slice_off_max,
261 (nb + 1)*sizeof(*slice_off));
262 if (!slice_off)
263 return AVERROR(ENOMEM);
264
265 *offsets = dec->slice_off = slice_off;
266
267 slice_off[nb] = vp->slices_size;
268 }
269
270 vkbuf = vp->slices_buf;
271 if (!vkbuf || vkbuf->size < new_size) {
272 int err;
273 FFVkBuffer *new_buf;
274
275 /* No point in requesting anything smaller. */
276 size_t buf_size = FFMAX(new_size, 1024*1024);
277
278 /* Align buffer to nearest power of two. Makes fragmentation management
279 * easier, and gives us ample headroom. */
280 buf_size = 2ULL << av_log2(buf_size);
281
282 /* When the frames context uses DRM modifier tiling,
283 * hwctx->create_pnext contains VkImageDrmFormatModifierListCreateInfoEXT,
284 * which per spec does not extend VkBufferCreateInfo, so we need to find
285 * the VkVideoProfileListInfoKHR structure within it. */
286 void *buf_pnext = ctx->s.hwfc->create_pnext;
287 if (ctx->s.hwfc->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT)
288 buf_pnext = (void *)ff_vk_find_struct(ctx->s.hwfc->create_pnext,
289 VK_STRUCTURE_TYPE_VIDEO_PROFILE_LIST_INFO_KHR);
290
291 VkBufferUsageFlags buf_usage = DECODER_IS_SDR(avctx->codec_id) ?
292 (VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
293 VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) :
294 VK_BUFFER_USAGE_VIDEO_DECODE_SRC_BIT_KHR;
295
296 err = ff_vk_get_pooled_buffer(&ctx->s, &ctx->buf_pool, &new_buf,
297 buf_usage, buf_pnext, buf_size,
298 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
299 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
300 if (err < 0)
301 err = ff_vk_get_pooled_buffer(&ctx->s, &ctx->buf_pool, &new_buf,
302 buf_usage, buf_pnext, buf_size,
303 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
304 if (err < 0)
305 return err;
306
307 /* Copy data from the old buffer */
308 if (vkbuf) {
309 memcpy(new_buf->mapped_mem, vkbuf->mapped_mem, vp->slices_size);
311 }
312
313 vp->slices_buf = new_buf;
314 vkbuf = new_buf;
315 }
316 slices = vkbuf->mapped_mem;
317
318 /* Startcode */
319 memcpy(slices + vp->slices_size, startcode_prefix, startcode_len);
320
321 /* Slice data */
322 memcpy(slices + vp->slices_size + startcode_len, data, size);
323
324 if (nb_slices)
325 *nb_slices = nb + 1;
326
327 vp->slices_size += startcode_len + size;
328
329 return 0;
330}
331
334 VkVideoSessionParametersKHR *empty_session_params)
335{
336 if (avctx->codec_id == AV_CODEC_ID_VP9)
337 return 0;
338
339 VkVideoDecodeH264SessionParametersCreateInfoKHR h264_params = {
340 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H264_SESSION_PARAMETERS_CREATE_INFO_KHR,
341 };
342 VkVideoDecodeH265SessionParametersCreateInfoKHR h265_params = {
343 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H265_SESSION_PARAMETERS_CREATE_INFO_KHR,
344 };
345 StdVideoAV1SequenceHeader av1_empty_seq = { 0 };
346 VkVideoDecodeAV1SessionParametersCreateInfoKHR av1_params = {
347 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_AV1_SESSION_PARAMETERS_CREATE_INFO_KHR,
348 .pStdSequenceHeader = &av1_empty_seq,
349 };
350 VkVideoSessionParametersCreateInfoKHR session_params_create = {
351 .sType = VK_STRUCTURE_TYPE_VIDEO_SESSION_PARAMETERS_CREATE_INFO_KHR,
352 .pNext = avctx->codec_id == AV_CODEC_ID_H264 ? (void *)&h264_params :
353 avctx->codec_id == AV_CODEC_ID_HEVC ? (void *)&h265_params :
354 avctx->codec_id == AV_CODEC_ID_AV1 ? (void *)&av1_params :
355 NULL,
356 .videoSession = ctx->common.session,
357 };
358
359 VkResult ret;
360 FFVulkanContext *s = &ctx->s;
361 FFVulkanFunctions *vk = &s->vkfn;
362 ret = vk->CreateVideoSessionParametersKHR(s->hwctx->act_dev, &session_params_create,
363 s->hwctx->alloc, empty_session_params);
364 if (ret != VK_SUCCESS) {
365 av_log(avctx, AV_LOG_ERROR, "Unable to create empty Vulkan video session parameters: %s!\n",
367 return AVERROR_EXTERNAL;
368 }
369
370 return 0;
371}
372
374{
375 int err;
376 FFVulkanContext *s = &ctx->s;
377 FFVulkanFunctions *vk = &s->vkfn;
378
379 VkVideoBeginCodingInfoKHR decode_start = {
380 .sType = VK_STRUCTURE_TYPE_VIDEO_BEGIN_CODING_INFO_KHR,
381 .videoSession = ctx->common.session,
382 };
383
384 if (!(ctx->s.extensions & FF_VK_EXT_VIDEO_MAINTENANCE_2)) {
386 &decode_start.videoSessionParameters);
387 if (err < 0)
388 return err;
389 }
390
391 VkVideoCodingControlInfoKHR decode_ctrl = {
392 .sType = VK_STRUCTURE_TYPE_VIDEO_CODING_CONTROL_INFO_KHR,
393 .flags = VK_VIDEO_CODING_CONTROL_RESET_BIT_KHR,
394 };
395 VkVideoEndCodingInfoKHR decode_end = {
396 .sType = VK_STRUCTURE_TYPE_VIDEO_END_CODING_INFO_KHR,
397 };
398
399 FFVkExecContext *exec = ff_vk_exec_get(&ctx->s, &ctx->exec_pool);
400 err = ff_vk_exec_start(&ctx->s, exec);
401 if (err < 0)
402 return err;
403
404 vk->CmdBeginVideoCodingKHR(exec->buf, &decode_start);
405 vk->CmdControlVideoCodingKHR(exec->buf, &decode_ctrl);
406 vk->CmdEndVideoCodingKHR(exec->buf, &decode_end);
407
408 err = ff_vk_exec_submit(&ctx->s, exec);
409
410 if (decode_start.videoSessionParameters) {
411 /* Wait to complete to delete the temporary session parameters */
412 if (err >= 0)
413 ff_vk_exec_wait(&ctx->s, exec);
414
415 vk->DestroyVideoSessionParametersKHR(s->hwctx->act_dev,
416 decode_start.videoSessionParameters,
417 s->hwctx->alloc);
418 }
419
420 if (err < 0)
421 av_log(avctx, AV_LOG_ERROR, "Unable to reset decoder: %s",
422 ff_vk_ret2str(err));
423
424 return err;
425}
426
429 AVFrame *rpic[], FFVulkanDecodePicture *rvkp[])
430{
431 int err;
432 VkResult ret;
433 VkCommandBuffer cmd_buf;
434 FFVkBuffer *sd_buf;
435
438 FFVulkanFunctions *vk = &ctx->s.vkfn;
439
440 /* Output */
441 AVVkFrame *vkf = (AVVkFrame *)pic->buf[0]->data;
442
443 /* Quirks */
444 const int layered_dpb = ctx->common.layered_dpb;
445
446 VkVideoBeginCodingInfoKHR decode_start = {
447 .sType = VK_STRUCTURE_TYPE_VIDEO_BEGIN_CODING_INFO_KHR,
448 .videoSession = ctx->common.session,
449 .videoSessionParameters = dec->session_params ?
450 *dec->session_params : VK_NULL_HANDLE,
451 .referenceSlotCount = vp->decode_info.referenceSlotCount,
452 .pReferenceSlots = vp->decode_info.pReferenceSlots,
453 };
454 VkVideoEndCodingInfoKHR decode_end = {
455 .sType = VK_STRUCTURE_TYPE_VIDEO_END_CODING_INFO_KHR,
456 };
457
458 VkImageMemoryBarrier2 img_bar[38];
459 int nb_img_bar = 0;
460
461 size_t data_size = FFALIGN(vp->slices_size,
462 ctx->caps.minBitstreamBufferSizeAlignment);
463
464 FFVkExecContext *exec = ff_vk_exec_get(&ctx->s, &ctx->exec_pool);
465
466 /* The current picture's resource has to be bound as an inactive
467 * reference, unless its slot is already bound as an active one, as when
468 * decoding the second field of a pair. */
469 int cur_bound = 0;
470 for (int i = 0; i < decode_start.referenceSlotCount; i++)
471 cur_bound |= decode_start.pReferenceSlots[i].slotIndex == vp->ref_slot.slotIndex;
472 if (!cur_bound) {
473 VkVideoReferenceSlotInfoKHR *cur_vk_ref;
474 cur_vk_ref = (void *)&decode_start.pReferenceSlots[decode_start.referenceSlotCount];
475 cur_vk_ref[0] = vp->ref_slot;
476 cur_vk_ref[0].slotIndex = -1;
477 decode_start.referenceSlotCount++;
478 }
479
480 sd_buf = vp->slices_buf;
481
482 /* Flush if needed */
483 if (!(sd_buf->flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) {
484 VkMappedMemoryRange flush_buf = {
485 .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
486 .memory = sd_buf->mem,
487 .offset = 0,
488 .size = FFALIGN(vp->slices_size,
489 ctx->s.props.properties.limits.nonCoherentAtomSize),
490 };
491
492 ret = vk->FlushMappedMemoryRanges(ctx->s.hwctx->act_dev, 1, &flush_buf);
493 if (ret != VK_SUCCESS) {
494 av_log(avctx, AV_LOG_ERROR, "Failed to flush memory: %s\n",
496 return AVERROR_EXTERNAL;
497 }
498 }
499
500 vp->decode_info.srcBuffer = sd_buf->buf;
501 vp->decode_info.srcBufferOffset = 0;
502 vp->decode_info.srcBufferRange = data_size;
503
504 /* Start command buffer recording */
505 err = ff_vk_exec_start(&ctx->s, exec);
506 if (err < 0)
507 return err;
508 cmd_buf = exec->buf;
509
510 /* Slices; owned by the execution from now on */
512
513 /* Parameters; unset when inline session parameters are used */
514 if (dec->session_params)
516
517 err = ff_vk_exec_add_dep_frame(&ctx->s, exec, pic,
518 VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
519 VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR);
520 if (err < 0) {
521 ff_vk_exec_discard(&ctx->s, exec);
522 return err;
523 }
524
525 /* The output view is kept alive by the execution context; freeing the
526 * picture then needs no host wait. */
528
529 /* Output image - change layout, as it comes from a pool */
530 img_bar[nb_img_bar] = (VkImageMemoryBarrier2) {
531 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
532 .pNext = NULL,
533 .srcStageMask = VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
534 .dstStageMask = VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
535 .srcAccessMask = VK_ACCESS_2_NONE,
536 .dstAccessMask = VK_ACCESS_2_VIDEO_DECODE_WRITE_BIT_KHR,
537 .oldLayout = vkf->layout[0],
538 .newLayout = (layered_dpb || vp->dpb_img) ?
539 VK_IMAGE_LAYOUT_VIDEO_DECODE_DST_KHR :
540 VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR, /* Spec, 07252 utter madness */
541 .srcQueueFamilyIndex = vkf->queue_family[0],
542 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
543 .image = vkf->img[0],
544 .subresourceRange = (VkImageSubresourceRange) {
545 .aspectMask = vp->view.aspect,
546 .layerCount = 1,
547 .levelCount = 1,
548 },
549 };
550 ff_vk_exec_update_frame(&ctx->s, exec, pic,
551 &img_bar[nb_img_bar], &nb_img_bar);
552
553 /* Current picture's DISTINCT-mode DPB image: transition on first use */
554 if (vp->dpb_img) {
555 FFVkVideoDPBImage *di = vp->dpb_img;
556
557 if (di->layout != VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR) {
558 img_bar[nb_img_bar] = (VkImageMemoryBarrier2) {
559 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
560 .srcStageMask = VK_PIPELINE_STAGE_2_NONE,
561 .dstStageMask = VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
562 .srcAccessMask = VK_ACCESS_2_NONE,
563 .dstAccessMask = VK_ACCESS_2_VIDEO_DECODE_READ_BIT_KHR |
564 VK_ACCESS_2_VIDEO_DECODE_WRITE_BIT_KHR,
565 .oldLayout = di->layout,
566 .newLayout = VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR,
567 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
568 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
569 .image = di->img,
570 .subresourceRange = (VkImageSubresourceRange) {
571 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
572 .layerCount = 1,
573 .levelCount = 1,
574 },
575 };
576 nb_img_bar++;
577 di->layout = VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR;
578 }
579 }
580
581 /* COINCIDE-mode references only: DISTINCT-mode references are covered
582 * by the memory barrier, with pool-owned views */
583 if (!dec->dedicated_dpb) {
584 for (int i = 0; i < vp->decode_info.referenceSlotCount; i++) {
585 AVFrame *ref_frame = rpic[i];
586 FFVulkanDecodePicture *rvp = rvkp[i];
587
588 err = ff_vk_exec_add_dep_frame(&ctx->s, exec, ref_frame,
589 VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
590 VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR);
591 if (err < 0) {
592 ff_vk_exec_discard(&ctx->s, exec);
593 return err;
594 }
595
596 /* The reference's image views are kept alive by the execution,
597 * so freeing the picture needs no host wait. */
598 if (err == 0 && rvp->out_views)
600
601 {
602 AVVkFrame *rvkf = (AVVkFrame *)ref_frame->data[0];
603
604 img_bar[nb_img_bar] = (VkImageMemoryBarrier2) {
605 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
606 .pNext = NULL,
607 .srcStageMask = VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
608 .dstStageMask = VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
609 .srcAccessMask = VK_ACCESS_2_NONE,
610 .dstAccessMask = VK_ACCESS_2_VIDEO_DECODE_READ_BIT_KHR |
611 VK_ACCESS_2_VIDEO_DECODE_WRITE_BIT_KHR,
612 .oldLayout = rvkf->layout[0],
613 .newLayout = VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR,
614 .srcQueueFamilyIndex = rvkf->queue_family[0],
615 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
616 .image = rvkf->img[0],
617 .subresourceRange = (VkImageSubresourceRange) {
618 .aspectMask = rvp->view.aspect_ref,
619 .layerCount = 1,
620 .levelCount = 1,
621 },
622 };
624 &img_bar[nb_img_bar], &nb_img_bar);
625 }
626 }
627 }
628
629 /* Internal DPB images are queue-exclusive: one decode-stage memory
630 * barrier per submission orders all prior work, references included, so
631 * they need no semaphores or dependencies, and image barriers only for
632 * initial layout transitions. */
633 VkMemoryBarrier2 mem_bar = {
634 .sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2,
635 .srcStageMask = VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
636 .srcAccessMask = VK_ACCESS_2_VIDEO_DECODE_WRITE_BIT_KHR,
637 .dstStageMask = VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
638 .dstAccessMask = VK_ACCESS_2_VIDEO_DECODE_READ_BIT_KHR |
639 VK_ACCESS_2_VIDEO_DECODE_WRITE_BIT_KHR,
640 };
641
642 /* Change image layouts, and synchronize the internal DPB if in use */
643 vk->CmdPipelineBarrier2(cmd_buf, &(VkDependencyInfo) {
644 .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
645 .dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT,
646 .pMemoryBarriers = &mem_bar,
647 .memoryBarrierCount = ctx->common.dpb ? 1 : 0,
648 .pImageMemoryBarriers = img_bar,
649 .imageMemoryBarrierCount = nb_img_bar,
650 });
651
652 /* Start, use parameters, decode and end decoding */
653 vk->CmdBeginVideoCodingKHR(cmd_buf, &decode_start);
654 vk->CmdDecodeVideoKHR(cmd_buf, &vp->decode_info);
655 vk->CmdEndVideoCodingKHR(cmd_buf, &decode_end);
656
657 /* End recording and submit for execution */
658 return ff_vk_exec_submit(&ctx->s, exec);
659}
660
662{
663 /* Free slices data */
665
666 /* The views are kept alive by every execution referencing them, so no
667 * host wait is needed. */
669
671 /* No wait: pool reuse is ordered by the DPB barrier */
673}
674
675static void free_common(AVRefStructOpaque unused, void *obj)
676{
678 FFVulkanContext *s = &ctx->s;
679
680 /* Wait on and free execution pool */
681 ff_vk_exec_pool_free(&ctx->s, &ctx->exec_pool);
682
683 av_refstruct_pool_uninit(&ctx->buf_pool);
684
686
687 if (ctx->sd_ctx_free)
688 ctx->sd_ctx_free(ctx);
689
691}
692
694{
695 int err;
697 const FFVulkanDecodeDescriptor *vk_desc = get_codecdesc(avctx->codec_id);
699 AVHWDeviceContext *device = (AVHWDeviceContext *)frames->device_ref->data;
700 AVVulkanDeviceContext *hwctx = device->hwctx;
702
703 if (dec->shared_ctx)
704 return 0;
705
706 dec->shared_ctx = av_refstruct_alloc_ext(sizeof(*ctx), 0, NULL,
708 if (!dec->shared_ctx)
709 return AVERROR(ENOMEM);
710
711 ctx = dec->shared_ctx;
712
715
716 if (vk_desc->queue_flags & VK_QUEUE_VIDEO_DECODE_BIT_KHR) {
717 if (!(ctx->s.extensions & FF_VK_EXT_VIDEO_DECODE_QUEUE)) {
718 av_log(avctx, AV_LOG_ERROR, "Device does not support the %s extension!\n",
719 VK_KHR_VIDEO_DECODE_QUEUE_EXTENSION_NAME);
721 return AVERROR(ENOSYS);
722 }
723 }
724
725 err = ff_vk_load_functions(device, &ctx->s.vkfn, ctx->s.extensions, 1, 1);
726 if (err < 0) {
728 return err;
729 }
730
731 return 0;
732}
733
738 const FFVulkanDecodeDescriptor *vk_desc,
739 VkVideoCapabilitiesKHR *caps,
740 VkVideoDecodeCapabilitiesKHR *dec_caps,
741 int cur_profile)
742{
743 VkVideoDecodeUsageInfoKHR *usage = &prof->usage;
744 VkVideoProfileInfoKHR *profile = &prof->profile;
745 VkVideoProfileListInfoKHR *profile_list = &prof->profile_list;
746
747 VkVideoDecodeH264ProfileInfoKHR *h264_profile = &prof->h264_profile;
748 VkVideoDecodeH265ProfileInfoKHR *h265_profile = &prof->h265_profile;
749#if CONFIG_VP9_VULKAN_HWACCEL
750 VkVideoDecodeVP9ProfileInfoKHR *vp9_profile = &prof->vp9_profile;
751#endif
752 VkVideoDecodeAV1ProfileInfoKHR *av1_profile = &prof->av1_profile;
753
755 if (!desc)
756 return AVERROR(EINVAL);
757
758 if (avctx->codec_id == AV_CODEC_ID_H264) {
759 usage->pNext = h264_profile;
760 h264_profile->sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H264_PROFILE_INFO_KHR;
761
762 /* Vulkan transmits all the constrant_set flags, rather than wanting them
763 * merged in the profile IDC */
764 h264_profile->stdProfileIdc = cur_profile & ~(AV_PROFILE_H264_CONSTRAINED |
766
767 h264_profile->pictureLayout =
768 VK_VIDEO_DECODE_H264_PICTURE_LAYOUT_PROGRESSIVE_KHR;
769 const H264Context *h = avctx->priv_data;
770 if (h && h->ps.sps && !h->ps.sps->frame_mbs_only_flag)
771 h264_profile->pictureLayout =
772 VK_VIDEO_DECODE_H264_PICTURE_LAYOUT_INTERLACED_INTERLEAVED_LINES_BIT_KHR;
773 } else if (avctx->codec_id == AV_CODEC_ID_H265) {
774 usage->pNext = h265_profile;
775 h265_profile->sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H265_PROFILE_INFO_KHR;
776 h265_profile->stdProfileIdc = cur_profile;
777#if CONFIG_VP9_VULKAN_HWACCEL
778 } else if (avctx->codec_id == AV_CODEC_ID_VP9) {
779 usage->pNext = vp9_profile;
780 vp9_profile->sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_VP9_PROFILE_INFO_KHR;
781 vp9_profile->stdProfile = cur_profile;
782#endif
783 } else if (avctx->codec_id == AV_CODEC_ID_AV1) {
784 usage->pNext = av1_profile;
785 av1_profile->sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_AV1_PROFILE_INFO_KHR;
786 av1_profile->stdProfile = cur_profile;
787 }
788
789 usage->sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_USAGE_INFO_KHR;
790 usage->videoUsageHints = VK_VIDEO_DECODE_USAGE_DEFAULT_KHR;
791
792 profile->sType = VK_STRUCTURE_TYPE_VIDEO_PROFILE_INFO_KHR;
793 profile->pNext = usage;
794 profile->videoCodecOperation = vk_desc->decode_op;
795 profile->chromaSubsampling = ff_vk_subsampling_from_av_desc(desc);
796 profile->lumaBitDepth = ff_vk_depth_from_av_depth(desc->comp[0].depth);
797 profile->chromaBitDepth = profile->lumaBitDepth;
798
799 /* A pixel format without a video profile representation is not a valid
800 * profile to query the capabilities for */
801 if (profile->chromaSubsampling == VK_VIDEO_CHROMA_SUBSAMPLING_INVALID_KHR ||
802 profile->lumaBitDepth == VK_VIDEO_COMPONENT_BIT_DEPTH_INVALID_KHR)
803 return VK_ERROR_VIDEO_PROFILE_FORMAT_NOT_SUPPORTED_KHR;
804
805 profile_list->sType = VK_STRUCTURE_TYPE_VIDEO_PROFILE_LIST_INFO_KHR;
806 profile_list->profileCount = 1;
807 profile_list->pProfiles = profile;
808
809 /* Get the capabilities of the decoder for the given profile */
810 caps->sType = VK_STRUCTURE_TYPE_VIDEO_CAPABILITIES_KHR;
811 caps->pNext = dec_caps;
812 dec_caps->sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_CAPABILITIES_KHR;
813 /* dec_caps->pNext already filled in */
814
815 return vk->GetPhysicalDeviceVideoCapabilitiesKHR(hwctx->phys_dev, profile,
816 caps);
817}
818
819static int profile_unsupported(VkResult ret)
820{
821 return ret == VK_ERROR_VIDEO_PROFILE_OPERATION_NOT_SUPPORTED_KHR ||
822 ret == VK_ERROR_VIDEO_PROFILE_CODEC_NOT_SUPPORTED_KHR;
823}
824
825static const char *profile_name(enum AVCodecID codec_id, int profile)
826{
828}
829
831 enum AVPixelFormat *pix_fmt, VkFormat *vk_fmt,
833 int *dpb_dedicate)
834{
835 VkResult ret;
836 int max_level, base_profile, cur_profile;
837 const FFVulkanDecodeDescriptor *vk_desc = get_codecdesc(avctx->codec_id);
839 AVHWDeviceContext *device = (AVHWDeviceContext *)frames->device_ref->data;
840 AVVulkanDeviceContext *hwctx = device->hwctx;
841 enum AVPixelFormat source_format;
842 enum AVPixelFormat best_format;
843 VkFormat best_vkfmt;
844
847 FFVulkanFunctions *vk = &ctx->s.vkfn;
848
849 VkVideoCapabilitiesKHR *caps = &ctx->caps;
850 VkVideoDecodeCapabilitiesKHR *dec_caps = &ctx->dec_caps;
851
852 VkVideoDecodeH264CapabilitiesKHR h264_caps = {
853 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H264_CAPABILITIES_KHR,
854 };
855 VkVideoDecodeH265CapabilitiesKHR h265_caps = {
856 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H265_CAPABILITIES_KHR,
857 };
858#if CONFIG_VP9_VULKAN_HWACCEL
859 VkVideoDecodeVP9CapabilitiesKHR vp9_caps = {
860 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_VP9_CAPABILITIES_KHR,
861 };
862#endif
863 VkVideoDecodeAV1CapabilitiesKHR av1_caps = {
864 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_AV1_CAPABILITIES_KHR,
865 };
866
867 VkPhysicalDeviceVideoFormatInfoKHR fmt_info = {
868 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VIDEO_FORMAT_INFO_KHR,
869 .pNext = &prof->profile_list,
870 };
871 VkVideoFormatPropertiesKHR *ret_info;
872 uint32_t nb_out_fmts = 0;
873
874 if (!(vk_desc->decode_extension & ctx->s.extensions)) {
875 av_log(avctx, AV_LOG_ERROR, "Device does not support decoding %s!\n",
876 avcodec_get_name(avctx->codec_id));
877 return AVERROR(ENOSYS);
878 }
879
880 cur_profile = avctx->profile;
883#if CONFIG_VP9_VULKAN_HWACCEL
884 avctx->codec_id == AV_CODEC_ID_VP9 ? STD_VIDEO_VP9_PROFILE_0 :
885#endif
886 avctx->codec_id == AV_CODEC_ID_AV1 ? STD_VIDEO_AV1_PROFILE_MAIN :
887 0;
888
889 if (avctx->codec_id == AV_CODEC_ID_H264)
890 dec_caps->pNext = &h264_caps;
891 else if (avctx->codec_id == AV_CODEC_ID_H265)
892 dec_caps->pNext = &h265_caps;
893#if CONFIG_VP9_VULKAN_HWACCEL
894 else if (avctx->codec_id == AV_CODEC_ID_VP9)
895 dec_caps->pNext = &vp9_caps;
896#endif
897 else if (avctx->codec_id == AV_CODEC_ID_AV1)
898 dec_caps->pNext = &av1_caps;
899
900 if (avctx->codec_id == AV_CODEC_ID_AV1)
901 prof->av1_profile.filmGrainSupport =
903
904 ret = vulkan_setup_profile(avctx, prof, hwctx, vk, vk_desc, caps, dec_caps,
905 cur_profile);
906
907 /* ANV returns OPERATION_NOT_SUPPORTED when it lacks filmGrainSupport */
908 if (profile_unsupported(ret) && prof->av1_profile.filmGrainSupport) {
909 const AV1DecContext *s = avctx->priv_data;
910
911 av_log(avctx, AV_LOG_VERBOSE, "%s profile %s not supported, "
912 "attempting again without filmGrainSupport\n",
914 profile_name(avctx->codec_id, cur_profile));
915 prof->av1_profile.filmGrainSupport = 0;
916 if (vulkan_setup_profile(avctx, prof, hwctx, vk, vk_desc, caps,
917 dec_caps, cur_profile) != VK_SUCCESS) {
918 prof->av1_profile.filmGrainSupport = 1;
919 } else if (s->raw_seq->film_grain_params_present) {
920 av_log(avctx, AV_LOG_ERROR, "The device does not support film "
921 "grain; set export_side_data to film_grain to export it as "
922 "side data instead\n");
923 return AVERROR(ENOTSUP);
924 } else {
925 ret = VK_SUCCESS;
926 }
927 }
928
931 avctx->profile != base_profile) {
932 av_log(avctx, AV_LOG_VERBOSE, "%s profile %s not supported, attempting "
933 "again with profile %s\n",
935 profile_name(avctx->codec_id, cur_profile),
936 profile_name(avctx->codec_id, base_profile));
937 cur_profile = base_profile;
938 ret = vulkan_setup_profile(avctx, prof, hwctx, vk, vk_desc, caps,
939 dec_caps, cur_profile);
940 }
941
943 av_log(avctx, AV_LOG_VERBOSE, "Unable to initialize video session: "
944 "%s profile \"%s\" not supported!\n",
946 profile_name(avctx->codec_id, cur_profile));
947 return AVERROR(EINVAL);
948 } else if (ret == VK_ERROR_VIDEO_PICTURE_LAYOUT_NOT_SUPPORTED_KHR) {
949 if (avctx->codec_id == AV_CODEC_ID_H264)
950 av_log(avctx, AV_LOG_VERBOSE, "Unable to initialize video session: "
951 "pictureLayout %#x not supported!\n",
952 (unsigned)prof->h264_profile.pictureLayout);
953 else
954 av_log(avctx, AV_LOG_VERBOSE, "Unable to initialize video session: "
955 "pictureLayout not supported!\n");
956 return AVERROR(EINVAL);
957 } else if (ret == VK_ERROR_VIDEO_PROFILE_FORMAT_NOT_SUPPORTED_KHR) {
958 av_log(avctx, AV_LOG_VERBOSE, "Unable to initialize video session: "
959 "format (%s) not supported!\n",
961 return AVERROR(EINVAL);
962 } else if (ret == VK_ERROR_FEATURE_NOT_PRESENT ||
963 ret == VK_ERROR_FORMAT_NOT_SUPPORTED) {
964 return AVERROR(EINVAL);
965 } else if (ret != VK_SUCCESS) {
966 return AVERROR_EXTERNAL;
967 }
968
969 max_level = avctx->codec_id == AV_CODEC_ID_H264 ? ff_vk_h264_level_to_av(h264_caps.maxLevelIdc) :
970 avctx->codec_id == AV_CODEC_ID_H265 ? ff_vk_h265_level_to_av(h265_caps.maxLevelIdc) :
971#if CONFIG_VP9_VULKAN_HWACCEL
972 avctx->codec_id == AV_CODEC_ID_VP9 ? vp9_caps.maxLevel :
973#endif
974 avctx->codec_id == AV_CODEC_ID_AV1 ? av1_caps.maxLevel :
975 0;
976
977 av_log(avctx, AV_LOG_VERBOSE, "Decoder capabilities for %s profile \"%s\":\n",
979 profile_name(avctx->codec_id, cur_profile));
980 av_log(avctx, AV_LOG_VERBOSE, " Maximum level: %i (stream %i)\n",
981 max_level, avctx->level);
982 av_log(avctx, AV_LOG_VERBOSE, " Width: from %i to %i\n",
983 caps->minCodedExtent.width, caps->maxCodedExtent.width);
984 av_log(avctx, AV_LOG_VERBOSE, " Height: from %i to %i\n",
985 caps->minCodedExtent.height, caps->maxCodedExtent.height);
986 av_log(avctx, AV_LOG_VERBOSE, " Width alignment: %i\n",
987 caps->pictureAccessGranularity.width);
988 av_log(avctx, AV_LOG_VERBOSE, " Height alignment: %i\n",
989 caps->pictureAccessGranularity.height);
990 av_log(avctx, AV_LOG_VERBOSE, " Bitstream offset alignment: %"PRIu64"\n",
991 caps->minBitstreamBufferOffsetAlignment);
992 av_log(avctx, AV_LOG_VERBOSE, " Bitstream size alignment: %"PRIu64"\n",
993 caps->minBitstreamBufferSizeAlignment);
994 av_log(avctx, AV_LOG_VERBOSE, " Maximum references: %u\n",
995 caps->maxDpbSlots);
996 av_log(avctx, AV_LOG_VERBOSE, " Maximum active references: %u\n",
997 caps->maxActiveReferencePictures);
998 av_log(avctx, AV_LOG_VERBOSE, " Codec header name: '%s' (driver), '%s' (compiled)\n",
999 caps->stdHeaderVersion.extensionName,
1000 vk_desc->ext_props.extensionName);
1001 av_log(avctx, AV_LOG_VERBOSE, " Codec header version: %i.%i.%i (driver), %i.%i.%i (compiled)\n",
1002 CODEC_VER(caps->stdHeaderVersion.specVersion),
1003 CODEC_VER(vk_desc->ext_props.specVersion));
1004 av_log(avctx, AV_LOG_VERBOSE, " Decode modes:%s%s%s\n",
1005 dec_caps->flags ? "" :
1006 " invalid",
1007 dec_caps->flags & VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_COINCIDE_BIT_KHR ?
1008 " reuse_dst_dpb" : "",
1009 dec_caps->flags & VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_DISTINCT_BIT_KHR ?
1010 " dedicated_dpb" : "");
1011 av_log(avctx, AV_LOG_VERBOSE, " Capability flags:%s%s%s\n",
1012 caps->flags ? "" :
1013 " none",
1014 caps->flags & VK_VIDEO_CAPABILITY_PROTECTED_CONTENT_BIT_KHR ?
1015 " protected" : "",
1016 caps->flags & VK_VIDEO_CAPABILITY_SEPARATE_REFERENCE_IMAGES_BIT_KHR ?
1017 " separate_references" : "");
1018
1019 /* Check if decoding is possible with the given parameters */
1020 if (avctx->coded_width < caps->minCodedExtent.width ||
1021 avctx->coded_height < caps->minCodedExtent.height ||
1022 avctx->coded_width > caps->maxCodedExtent.width ||
1023 avctx->coded_height > caps->maxCodedExtent.height)
1024 return AVERROR(EINVAL);
1025
1027 avctx->level > max_level)
1028 return AVERROR(EINVAL);
1029
1030 /* Some basic sanity checking */
1031 if (!(dec_caps->flags & (VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_COINCIDE_BIT_KHR |
1032 VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_DISTINCT_BIT_KHR))) {
1033 av_log(avctx, AV_LOG_ERROR, "Buggy driver signals invalid decoding mode: neither "
1034 "VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_COINCIDE_BIT_KHR nor "
1035 "VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_DISTINCT_BIT_KHR are set!\n");
1036 return AVERROR_EXTERNAL;
1037 } else if ((dec_caps->flags & (VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_COINCIDE_BIT_KHR |
1038 VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_DISTINCT_BIT_KHR)) ==
1039 VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_COINCIDE_BIT_KHR &&
1040 !(caps->flags & VK_VIDEO_CAPABILITY_SEPARATE_REFERENCE_IMAGES_BIT_KHR)) {
1041 av_log(avctx, AV_LOG_VERBOSE,
1042 "COINCIDE-only decode without SEPARATE_REFERENCE_IMAGES "
1043 "(layered DPB capability); continuing\n");
1044 }
1045
1046 dec->dedicated_dpb = !!(dec_caps->flags & VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_DISTINCT_BIT_KHR);
1047 ctx->common.layered_dpb = !dec->dedicated_dpb ? 0 :
1048 !(caps->flags & VK_VIDEO_CAPABILITY_SEPARATE_REFERENCE_IMAGES_BIT_KHR);
1049
1050 if (dec->dedicated_dpb) {
1051 fmt_info.imageUsage = VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR;
1052 } else {
1053 fmt_info.imageUsage = VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR |
1054 VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR |
1055 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
1056 VK_IMAGE_USAGE_SAMPLED_BIT;
1057
1058 if ((ctx->s.extensions & FF_VK_EXT_VIDEO_ENCODE_QUEUE) &&
1059 (ctx->s.extensions & FF_VK_EXT_VIDEO_MAINTENANCE_1))
1060 fmt_info.imageUsage |= VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR;
1061 }
1062
1063 /* Get the format of the images necessary */
1064 ret = vk->GetPhysicalDeviceVideoFormatPropertiesKHR(hwctx->phys_dev,
1065 &fmt_info,
1066 &nb_out_fmts, NULL);
1067 if (ret == VK_ERROR_FORMAT_NOT_SUPPORTED ||
1068 (!nb_out_fmts && ret == VK_SUCCESS)) {
1069 return AVERROR(EINVAL);
1070 } else if (ret != VK_SUCCESS) {
1071 av_log(avctx, AV_LOG_ERROR, "Unable to get Vulkan format properties: %s!\n",
1073 return AVERROR_EXTERNAL;
1074 }
1075
1076 ret_info = av_mallocz(sizeof(*ret_info)*nb_out_fmts);
1077 if (!ret_info)
1078 return AVERROR(ENOMEM);
1079
1080 for (int i = 0; i < nb_out_fmts; i++)
1081 ret_info[i].sType = VK_STRUCTURE_TYPE_VIDEO_FORMAT_PROPERTIES_KHR;
1082
1083 ret = vk->GetPhysicalDeviceVideoFormatPropertiesKHR(hwctx->phys_dev,
1084 &fmt_info,
1085 &nb_out_fmts, ret_info);
1086 if (ret == VK_ERROR_FORMAT_NOT_SUPPORTED ||
1087 (!nb_out_fmts && ret == VK_SUCCESS)) {
1088 av_free(ret_info);
1089 return AVERROR(EINVAL);
1090 } else if (ret != VK_SUCCESS) {
1091 av_log(avctx, AV_LOG_ERROR, "Unable to get Vulkan format properties: %s!\n",
1093 av_free(ret_info);
1094 return AVERROR_EXTERNAL;
1095 }
1096
1097 /* Find a format to use */
1098 *pix_fmt = best_format = AV_PIX_FMT_NONE;
1099 *vk_fmt = best_vkfmt = VK_FORMAT_UNDEFINED;
1100 source_format = avctx->sw_pix_fmt;
1101
1102 av_log(avctx, AV_LOG_DEBUG, "Choosing best pixel format for decoding from %i:\n", nb_out_fmts);
1103 for (int i = 0; i < nb_out_fmts; i++) {
1105 if (tmp == AV_PIX_FMT_NONE) {
1106 av_log(avctx, AV_LOG_WARNING, "Invalid/unknown Vulkan format %i!\n", ret_info[i].format);
1107 continue;
1108 }
1109
1110 best_format = av_find_best_pix_fmt_of_2(tmp, best_format, source_format, 0, NULL);
1111 if (tmp == best_format)
1112 best_vkfmt = ret_info[i].format;
1113
1114 av_log(avctx, AV_LOG_DEBUG, " %s%s (Vulkan ID: %i)\n",
1115 av_get_pix_fmt_name(tmp), tmp == best_format ? "*" : "",
1116 ret_info[i].format);
1117 }
1118
1119 av_free(ret_info);
1120
1121 if (best_format == AV_PIX_FMT_NONE) {
1122 av_log(avctx, AV_LOG_ERROR, "No valid/compatible pixel format found for decoding!\n");
1123 return AVERROR(EINVAL);
1124 } else {
1125 av_log(avctx, AV_LOG_VERBOSE, "Chosen frame pixfmt: %s (Vulkan ID: %i)\n",
1126 av_get_pix_fmt_name(best_format), best_vkfmt);
1127 }
1128
1129 *pix_fmt = best_format;
1130 *vk_fmt = best_vkfmt;
1131
1132 *dpb_dedicate = dec->dedicated_dpb;
1133
1134 return 0;
1135}
1136
1138{
1139 av_free(hwfc->user_opaque);
1140}
1141
1143{
1144 int err, dedicated_dpb;
1145 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ctx->data;
1146 AVVulkanFramesContext *hwfc = frames_ctx->hwctx;
1149
1150 err = vulkan_decode_bootstrap(avctx, hw_frames_ctx);
1151 if (err < 0)
1152 return err;
1153
1154 frames_ctx->format = AV_PIX_FMT_VULKAN;
1155 frames_ctx->sw_format = avctx->sw_pix_fmt;
1156 frames_ctx->width = avctx->coded_width;
1157 frames_ctx->height = avctx->coded_height;
1158
1159 if (!DECODER_IS_SDR(avctx->codec_id)) {
1160 prof = av_mallocz(sizeof(FFVulkanDecodeProfileData));
1161 if (!prof)
1162 return AVERROR(ENOMEM);
1163
1164 err = vulkan_decode_get_profile(avctx, hw_frames_ctx,
1165 &frames_ctx->sw_format,
1166 &hwfc->format[0],
1167 prof, &dedicated_dpb);
1168 if (err < 0) {
1169 av_free(prof);
1170 return err;
1171 }
1172
1173 const AVPixFmtDescriptor *pdesc = av_pix_fmt_desc_get(frames_ctx->sw_format);
1174 frames_ctx->width = FFALIGN(frames_ctx->width, 1 << pdesc->log2_chroma_w);
1175 frames_ctx->height = FFALIGN(frames_ctx->height, 1 << pdesc->log2_chroma_h);
1176 frames_ctx->user_opaque = prof;
1177 frames_ctx->free = free_profile_data;
1178
1179 hwfc->create_pnext = &prof->profile_list;
1180 } else {
1181 hwfc->format[0] = VK_FORMAT_UNDEFINED;
1182 switch (frames_ctx->sw_format) {
1183 case AV_PIX_FMT_GBRAP16:
1184 /* This should be more efficient for downloading and using */
1185 frames_ctx->sw_format = AV_PIX_FMT_RGBA64;
1186 break;
1187 case AV_PIX_FMT_GBRP10:
1188 /* This saves memory bandwidth when downloading */
1189 frames_ctx->sw_format = AV_PIX_FMT_X2BGR10;
1190 break;
1191 case AV_PIX_FMT_RGB24:
1192 case AV_PIX_FMT_BGR0:
1193 /* mpv has issues with bgr0 mapping, so just remap it */
1194 frames_ctx->sw_format = AV_PIX_FMT_RGB0;
1195 break;
1196 /* DPX endian mismatch remappings */
1197 case AV_PIX_FMT_RGB48LE:
1198 case AV_PIX_FMT_RGB48BE: frames_ctx->sw_format = AV_PIX_FMT_GBRP16; break;
1199 case AV_PIX_FMT_RGBA64BE: frames_ctx->sw_format = AV_PIX_FMT_RGBA64; break;
1200 case AV_PIX_FMT_GRAY16BE: frames_ctx->sw_format = AV_PIX_FMT_GRAY16; break;
1201 /* ProRes needs to clear the input image, which is not possible on YUV formats */
1206 hwfc->format[3] = VK_FORMAT_R16_UNORM;
1212 hwfc->format[0] = VK_FORMAT_R16_UNORM;
1213 hwfc->format[1] = VK_FORMAT_R16_UNORM;
1214 hwfc->format[2] = VK_FORMAT_R16_UNORM;
1215 break;
1216 default:
1217 break;
1218 }
1219 }
1220
1221 if (hwfc->tiling != VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT)
1222 hwfc->tiling = VK_IMAGE_TILING_OPTIMAL;
1223
1224 hwfc->usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
1225 VK_IMAGE_USAGE_STORAGE_BIT |
1226 VK_IMAGE_USAGE_SAMPLED_BIT;
1227
1228 if (prof) {
1230
1231 hwfc->usage |= VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR;
1232 if (!dec->dedicated_dpb)
1233 hwfc->usage |= VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR;
1234
1235 ctx = dec->shared_ctx;
1236 if ((ctx->s.extensions & FF_VK_EXT_VIDEO_ENCODE_QUEUE) &&
1237 (ctx->s.extensions & FF_VK_EXT_VIDEO_MAINTENANCE_1))
1238 hwfc->usage |= VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR;
1239 }
1240
1241 return err;
1242}
1243
1245{
1246 FFVulkanDecodeShared *ctx = opaque.nc;
1247 FFVulkanFunctions *vk = &ctx->s.vkfn;
1248 VkVideoSessionParametersKHR *par = obj;
1249 vk->DestroyVideoSessionParametersKHR(ctx->s.hwctx->act_dev, *par,
1250 ctx->s.hwctx->alloc);
1251}
1252
1253int ff_vk_decode_create_params(VkVideoSessionParametersKHR **par_ref, void *logctx, FFVulkanDecodeShared *ctx,
1254 const VkVideoSessionParametersCreateInfoKHR *session_params_create)
1255{
1256 VkVideoSessionParametersKHR *par;
1257 const FFVulkanFunctions *vk = &ctx->s.vkfn;
1258 VkResult ret;
1259
1260 par = av_refstruct_alloc_ext(sizeof(*par), 0, ctx, vk_decode_free_params);
1261 if (!par)
1262 return AVERROR(ENOMEM);
1263
1264 /* Create session parameters */
1265 ret = vk->CreateVideoSessionParametersKHR(ctx->s.hwctx->act_dev, session_params_create,
1266 ctx->s.hwctx->alloc, par);
1267 if (ret != VK_SUCCESS) {
1268 av_log(logctx, AV_LOG_ERROR, "Unable to create Vulkan video session parameters: %s!\n",
1270 *par = VK_NULL_HANDLE;
1271 av_refstruct_unref(&par);
1272 return AVERROR_EXTERNAL;
1273 }
1274 *par_ref = par;
1275
1276 return 0;
1277}
1278
1280{
1282
1283 av_freep(&dec->hevc_headers);
1286 av_freep(&dec->slice_off);
1287 return 0;
1288}
1289
1291{
1292 int err;
1296 int async_depth;
1297 const VkVideoProfileInfoKHR *profile;
1298 const FFVulkanDecodeDescriptor *vk_desc;
1299
1300 VkVideoSessionCreateInfoKHR session_create = {
1301 .sType = VK_STRUCTURE_TYPE_VIDEO_SESSION_CREATE_INFO_KHR,
1302 };
1303
1305 if (err < 0)
1306 return err;
1307
1308 /* Initialize contexts */
1309 ctx = dec->shared_ctx;
1310 s = &ctx->s;
1311
1312 err = ff_vk_init(s, avctx, NULL, avctx->hw_frames_ctx);
1313 if (err < 0)
1314 return err;
1315
1316 vk_desc = get_codecdesc(avctx->codec_id);
1317
1319 if ((vk_desc->queue_flags & VK_QUEUE_VIDEO_DECODE_BIT_KHR) && !profile) {
1320 av_log(avctx, AV_LOG_ERROR, "Video profile missing from frames context!");
1321 return AVERROR(EINVAL);
1322 }
1323
1324 if (avctx->codec_id == AV_CODEC_ID_AV1) {
1325 const VkVideoDecodeAV1ProfileInfoKHR *av1_profile =
1327 VK_STRUCTURE_TYPE_VIDEO_DECODE_AV1_PROFILE_INFO_KHR);
1328 dec->external_fg = !av1_profile->filmGrainSupport;
1329 }
1330
1331 /* Create queue context */
1332 vk_desc = get_codecdesc(avctx->codec_id);
1333 ctx->qf = ff_vk_qf_find(s, vk_desc->queue_flags, vk_desc->decode_op);
1334 if (!ctx->qf) {
1335 av_log(avctx, AV_LOG_ERROR, "Decoding of %s is not supported by this device\n",
1336 avcodec_get_name(avctx->codec_id));
1337 return err;
1338 }
1339
1340 session_create.queueFamilyIndex = ctx->qf->idx;
1341 session_create.maxCodedExtent = ctx->caps.maxCodedExtent;
1342 session_create.maxDpbSlots = ctx->caps.maxDpbSlots;
1343 session_create.maxActiveReferencePictures = ctx->caps.maxActiveReferencePictures;
1344 session_create.pictureFormat = s->hwfc->format[0];
1345 session_create.referencePictureFormat = session_create.pictureFormat;
1346 session_create.pStdHeaderVersion = &vk_desc->ext_props;
1347 session_create.pVideoProfile = profile;
1348#ifdef VK_KHR_video_maintenance2
1349 if (ctx->s.extensions & FF_VK_EXT_VIDEO_MAINTENANCE_2)
1350 session_create.flags = VK_VIDEO_SESSION_CREATE_INLINE_SESSION_PARAMETERS_BIT_KHR;
1351#endif
1352
1353 /* Create the decode exec context.
1354 * A small fixed depth is enough to keep the GPU fed, as submissions are
1355 * serialized for hardware decoding; thread-safe hwaccels submit from
1356 * every frame thread concurrently, and need a context per thread. */
1357 async_depth = FF_VK_DEFAULT_EXEC_CONTEXTS;
1359 async_depth = FFMAX(async_depth, avctx->thread_count);
1360
1361 err = ff_vk_exec_pool_init(s, ctx->qf, &ctx->exec_pool,
1362 async_depth, 0, 0, 0, profile);
1363 if (err < 0)
1364 goto fail;
1365
1366 if (!DECODER_IS_SDR(avctx->codec_id)) {
1367 err = ff_vk_video_common_init(avctx, s, &ctx->common, &session_create);
1368 if (err < 0)
1369 goto fail;
1370 }
1371
1372 /* Out-of-place decoding: create the internal, queue-exclusive DPB pool */
1373 if (dec->dedicated_dpb || avctx->codec_id == AV_CODEC_ID_AV1) {
1374 VkImageTiling tiling = VK_IMAGE_TILING_OPTIMAL;
1375 void *create_pnext = (void *)ff_vk_find_struct(ctx->s.hwfc->create_pnext,
1376 VK_STRUCTURE_TYPE_VIDEO_PROFILE_LIST_INFO_KHR);
1377 /* With DPB_AND_OUTPUT_DISTINCT, DPB images never leave the decoder,
1378 * so they don't need the output's DRM modifiers. */
1379 void *drm_create_pnext = (void *)ff_vk_find_struct(ctx->s.hwfc->create_pnext,
1380 VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT);
1381 if (drm_create_pnext &&
1382 !(ctx->dec_caps.flags & VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_DISTINCT_BIT_KHR)) {
1383 create_pnext = drm_create_pnext;
1384 tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT;
1385 av_assert2(ff_vk_find_struct(drm_create_pnext, VK_STRUCTURE_TYPE_VIDEO_PROFILE_LIST_INFO_KHR));
1386 }
1387
1388 err = ff_vk_video_dpb_init(s, &ctx->common, s->hwfc->format[0],
1389 VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR,
1390 tiling, create_pnext,
1391 s->frames->width, s->frames->height,
1392 ctx->common.layered_dpb ?
1393 ctx->caps.maxDpbSlots : 1);
1394 if (err < 0)
1395 goto fail;
1396
1397 if (ctx->common.layered_dpb) {
1398 FFVulkanFunctions *vk = &ctx->s.vkfn;
1399 FFVkExecContext *exec;
1400 VkImageMemoryBarrier2 img_bar;
1401
1402 ctx->common.layered_img = av_refstruct_pool_get(ctx->common.dpb->img_pool);
1403 if (!ctx->common.layered_img) {
1404 err = AVERROR(ENOMEM);
1405 goto fail;
1406 }
1407
1408 ctx->common.layered_view = ctx->common.layered_img->view;
1409 ctx->common.layered_aspect = ctx->common.layered_img->aspect;
1410
1411 /* Created eagerly, so transitioned eagerly; per-picture images
1412 * transition on first use instead */
1413 exec = ff_vk_exec_get(&ctx->s, &ctx->exec_pool);
1414 err = ff_vk_exec_start(&ctx->s, exec);
1415 if (err < 0)
1416 goto fail;
1417
1418 img_bar = (VkImageMemoryBarrier2) {
1419 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
1420 .srcStageMask = VK_PIPELINE_STAGE_2_NONE,
1421 .dstStageMask = VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
1422 .srcAccessMask = VK_ACCESS_2_NONE,
1423 .dstAccessMask = VK_ACCESS_2_VIDEO_DECODE_READ_BIT_KHR |
1424 VK_ACCESS_2_VIDEO_DECODE_WRITE_BIT_KHR,
1425 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1426 .newLayout = VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR,
1427 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
1428 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
1429 .image = ctx->common.layered_img->img,
1430 .subresourceRange = (VkImageSubresourceRange) {
1431 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1432 .layerCount = VK_REMAINING_ARRAY_LAYERS,
1433 .levelCount = 1,
1434 },
1435 };
1436 vk->CmdPipelineBarrier2(exec->buf, &(VkDependencyInfo) {
1437 .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
1438 .pImageMemoryBarriers = &img_bar,
1439 .imageMemoryBarrierCount = 1,
1440 });
1441
1442 err = ff_vk_exec_submit(&ctx->s, exec);
1443 if (err < 0)
1444 goto fail;
1445
1446 ctx->common.layered_img->layout = VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR;
1447 }
1448 }
1449
1450 if (!DECODER_IS_SDR(avctx->codec_id)) {
1451 err = decode_reset(avctx, ctx);
1452 if (err < 0)
1453 return err;
1454 } else {
1455 /* For SDR decoders, this alignment value will be 0. Since this will make
1456 * add_slice() malfunction, set it to a sane default value. */
1457 ctx->caps.minBitstreamBufferSizeAlignment = AV_INPUT_BUFFER_PADDING_SIZE;
1458 }
1459
1460 const VkPhysicalDeviceDriverProperties *driver_props;
1461 driver_props = &dec->shared_ctx->s.driver_props;
1462 if (driver_props->driverID == VK_DRIVER_ID_NVIDIA_PROPRIETARY &&
1463 driver_props->conformanceVersion.major == 1 &&
1464 driver_props->conformanceVersion.minor == 3 &&
1465 driver_props->conformanceVersion.subminor == 8 &&
1466 driver_props->conformanceVersion.patch < 3)
1467 dec->quirk_av1_offset = 1;
1468
1469 av_log(avctx, AV_LOG_VERBOSE, "Vulkan decoder initialization successful\n");
1470
1471 return 0;
1472
1473fail:
1474 ff_vk_decode_uninit(avctx);
1475
1476 return err;
1477}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
static const char *const format[]
Definition af_aiir.c:445
static int frames
static AVFormatContext * ctx
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition avassert.h:68
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define AV_CODEC_ID_H265
Definition codec_id.h:224
#define NULL
Definition coverity.c:32
int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx, enum AVHWDeviceType dev_type)
Make sure avctx.hw_frames_ctx is set.
Definition decode.c:1088
#define AV_PROFILE_H264_CONSTRAINED
Definition defs.h:121
#define AV_PROFILE_HEVC_MAIN
Definition defs.h:173
#define AV_PROFILE_H264_INTRA
Definition defs.h:122
#define AV_PROFILE_H264_CONSTRAINED_BASELINE
Definition defs.h:125
static enum AVPixelFormat pix_fmt
int ret
Definition ffplay.c:391
const char * usage
#define fail
Definition test.h:479
const char * avcodec_profile_name(enum AVCodecID codec_id, int profile)
Return a name for the specified profile, if available.
Definition utils.c:446
#define AV_CODEC_EXPORT_DATA_FILM_GRAIN
Decoding only.
Definition avcodec.h:404
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition utils.c:421
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
@ AV_CODEC_ID_H264
Definition codec_id.h:77
@ AV_CODEC_ID_AV1
Definition codec_id.h:275
@ AV_CODEC_ID_HEVC
Definition codec_id.h:223
@ AV_CODEC_ID_VP9
Definition codec_id.h:217
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
#define AV_HWACCEL_FLAG_IGNORE_LEVEL
Hardware acceleration should be used for decoding even if the codec level used is unknown or higher t...
Definition avcodec.h:2018
#define AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH
Hardware acceleration should still be attempted for decoding when the codec profile does not match th...
Definition avcodec.h:2038
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
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:601
static void * av_x_if_null(const void *p, const void *x)
Return x default pointer in case p is NULL.
Definition avutil.h:311
H.264 / AVC / MPEG-4 part10 codec.
static const int offsets[]
Definition hevc_pel.c:34
static const FFHWAccel * ffhwaccel(const AVHWAccel *codec)
#define HWACCEL_CAP_THREAD_SAFE
@ AV_HWDEVICE_TYPE_VULKAN
Definition hwcontext.h:39
enum VkFormat VkFormat
#define b
Definition input.c:43
#define av_log2
Definition intmath.h:84
static av_cold int decode_end(AVCodecContext *avctx)
Definition 4xm.c:982
Macro definitions for various function/variable attributes.
#define av_fallthrough
Definition attributes.h:67
void ff_vk_exec_pool_free(FFVulkanContext *s, FFVkExecPool *pool)
Definition vulkan.c:335
int ff_vk_exec_pool_init(FFVulkanContext *s, AVVulkanDeviceQueueFamily *qf, FFVkExecPool *pool, int nb_contexts, int nb_queries, VkQueryType query_type, int query_64bit, const void *query_create_pnext)
Allocates/frees an execution pool.
Definition vulkan.c:399
void ff_vk_exec_wait(FFVulkanContext *s, FFVkExecContext *e)
Definition vulkan.c:653
void ff_vk_uninit(FFVulkanContext *s)
Frees main context.
Definition vulkan.c:2800
const char * ff_vk_ret2str(VkResult res)
Converts Vulkan return values to strings.
Definition vulkan.c:42
int ff_vk_init(FFVulkanContext *s, void *log_parent, AVBufferRef *device_ref, AVBufferRef *frames_ref)
Initializes the AVClass, in case this context is not used as the main user's context.
Definition vulkan.c:2816
int ff_vk_exec_start(FFVulkanContext *s, FFVkExecContext *e)
Start/submit/wait an execution.
Definition vulkan.c:668
FFVkImageViews * ff_vk_imageviews_alloc(FFVulkanContext *s, int nb_views)
Allocate a reference-counted set of image views, zero-initialized for the caller to create.
Definition vulkan.c:1984
void ff_vk_exec_update_frame(FFVulkanContext *s, FFVkExecContext *e, AVFrame *f, VkImageMemoryBarrier2 *bar, uint32_t *nb_img_bar)
Definition vulkan.c:947
FFVkExecContext * ff_vk_exec_get(FFVulkanContext *s, FFVkExecPool *pool)
Retrieve an execution pool.
Definition vulkan.c:630
int ff_vk_exec_submit(FFVulkanContext *s, FFVkExecContext *e)
Definition vulkan.c:991
void ff_vk_exec_move_dep_refstruct(FFVulkanContext *s, FFVkExecContext *e, void *obj)
Definition vulkan.c:794
AVVulkanDeviceQueueFamily * ff_vk_qf_find(FFVulkanContext *s, VkQueueFlagBits dev_family, VkVideoCodecOperationFlagBitsKHR vid_ops)
Chooses an appropriate QF.
Definition vulkan.c:320
void ff_vk_exec_add_dep_refstruct(FFVulkanContext *s, FFVkExecContext *e, void *obj)
Execution dependency management.
Definition vulkan.c:787
int ff_vk_get_pooled_buffer(FFVulkanContext *ctx, AVRefStructPool **buf_pool, FFVkBuffer **buf, VkBufferUsageFlags usage, void *create_pNext, size_t size, VkMemoryPropertyFlagBits mem_props)
Initialize a pool and create AVBufferRefs containing FFVkBuffer.
Definition vulkan.c:1448
void ff_vk_exec_discard(FFVulkanContext *s, FFVkExecContext *e)
Definition vulkan.c:771
int ff_vk_exec_add_dep_frame(FFVulkanContext *s, FFVkExecContext *e, AVFrame *f, VkPipelineStageFlagBits2 wait_stage, VkPipelineStageFlagBits2 signal_stage)
Definition vulkan.c:889
const char * desc
Definition libsvtav1.c:83
#define FFMAX(a, b)
Definition macros.h:47
#define FFALIGN(x, a)
Definition macros.h:78
Memory handling functions.
static const char * obj
Definition mscl.c:57
const char data[16]
Definition mxf.c:149
int profile
Definition mxfenc.c:2304
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:3404
enum AVPixelFormat av_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2, enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
Compute what kind of losses will occur when converting from one specific pixel format to another.
Definition pixdesc.c:3763
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:555
#define AV_PIX_FMT_GBRAP16
Definition pixfmt.h:574
#define AV_PIX_FMT_YUVA444P10
Definition pixfmt.h:601
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:553
#define AV_PIX_FMT_GBRP10
Definition pixfmt.h:567
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:549
#define AV_PIX_FMT_RGBA64
Definition pixfmt.h:538
#define AV_PIX_FMT_YUVA422P10
Definition pixfmt.h:600
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_GRAY16BE
Y , 16bpp, big-endian.
Definition pixfmt.h:104
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_VULKAN
Vulkan hardware images.
Definition pixfmt.h:379
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition pixfmt.h:75
@ AV_PIX_FMT_BGR0
packed BGR 8:8:8, 32bpp, BGRXBGRX... X=unused/undefined
Definition pixfmt.h:265
@ AV_PIX_FMT_RGB48BE
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as big...
Definition pixfmt.h:109
@ AV_PIX_FMT_RGBA64BE
packed RGBA 16:16:16:16, 64bpp, 16R, 16G, 16B, 16A, the 2-byte value for each R/G/B/A component is st...
Definition pixfmt.h:202
@ AV_PIX_FMT_RGB48LE
packed RGB 16:16:16, 48bpp, 16R, 16G, 16B, the 2-byte value for each R/G/B component is stored as lit...
Definition pixfmt.h:110
@ AV_PIX_FMT_RGB0
packed RGB 8:8:8, 32bpp, RGBXRGBX... X=unused/undefined
Definition pixfmt.h:263
#define AV_PIX_FMT_YUVA422P12
Definition pixfmt.h:602
#define AV_PIX_FMT_X2BGR10
Definition pixfmt.h:625
#define AV_PIX_FMT_GRAY16
Definition pixfmt.h:531
#define AV_PIX_FMT_GBRP16
Definition pixfmt.h:570
#define AV_PIX_FMT_YUVA444P12
Definition pixfmt.h:603
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:551
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition refstruct.c:121
void av_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
Definition refstruct.c:161
void * av_refstruct_pool_get(AVRefStructPool *pool)
Get an object from the pool, reusing an old one from the pool when available.
Definition refstruct.c:315
static void av_refstruct_pool_uninit(AVRefStructPool **poolp)
Mark the pool as being available for freeing.
Definition refstruct.h:292
static void * av_refstruct_alloc_ext(size_t size, unsigned flags, void *opaque, void(*free_cb)(AVRefStructOpaque opaque, void *obj))
A wrapper around av_refstruct_alloc_ext_c() for the common case of a non-const qualified opaque.
Definition refstruct.h:94
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
Definition buffer.h:82
uint8_t * data
The data buffer.
Definition buffer.h:90
main external API structure.
Definition avcodec.h:443
int hwaccel_flags
Bit set of AV_HWACCEL_FLAG_* flags, which affect hardware accelerated decoding (if active).
Definition avcodec.h:1508
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:651
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
Definition avcodec.h:1477
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition avcodec.h:1429
int level
Encoding level descriptor.
Definition avcodec.h:1656
int profile
profile
Definition avcodec.h:1646
int export_side_data
Bit set of AV_CODEC_EXPORT_DATA_* flags, which affects the kind of metadata exported in frame,...
Definition avcodec.h:1789
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition avcodec.h:1589
enum AVCodecID codec_id
Definition avcodec.h:453
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition avcodec.h:620
struct AVCodecInternal * internal
Private context used for internal data.
Definition avcodec.h:478
void * priv_data
Definition avcodec.h:470
void * hwaccel_priv_data
hwaccel-specific private data
Definition internal.h:130
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:500
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame.
Definition frame.h:779
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition frame.h:656
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition hwcontext.h:63
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition hwcontext.h:88
This struct describes a set or pool of "hardware" frames (i.e.
Definition hwcontext.h:118
enum AVPixelFormat format
The pixel format identifying the underlying HW surface type.
Definition hwcontext.h:200
void * user_opaque
Arbitrary user data, to be used e.g.
Definition hwcontext.h:166
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
Definition hwcontext.h:153
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition hwcontext.h:213
int width
The allocated dimensions of the frames in this pool.
Definition hwcontext.h:220
void(* free)(struct AVHWFramesContext *ctx)
This field may be set by the caller before calling av_hwframe_ctx_init().
Definition hwcontext.h:161
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition pixdesc.h:80
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition pixdesc.h:89
VkImageLayout layout[AV_NUM_DATA_POINTERS]
uint32_t queue_family[AV_NUM_DATA_POINTERS]
Queue family of the images.
VkImage img[AV_NUM_DATA_POINTERS]
Vulkan images to which the memory is bound to.
Main Vulkan context, allocated as AVHWDeviceContext.hwctx.
VkPhysicalDevice phys_dev
Physical device.
const char *const * enabled_dev_extensions
Enabled device extensions.
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.
VkImageUsageFlagBits usage
Defines extra usage of output frames.
VkFormat format[AV_NUM_DATA_POINTERS]
Vulkan format for each image.
int caps_internal
Internal hwaccel capabilities.
VkBuffer buf
Definition vulkan.h:95
size_t size
Definition vulkan.h:98
VkMemoryPropertyFlagBits flags
Definition vulkan.h:97
VkDeviceMemory mem
Definition vulkan.h:96
uint8_t * mapped_mem
Definition vulkan.h:103
VkCommandBuffer buf
Definition vulkan.h:142
VkImageView views[AV_NUM_DATA_POINTERS]
Definition vulkan.h:569
VkImageLayout layout
VkImageAspectFlags aspect
VkImageView view
VkPhysicalDeviceDriverProperties driver_props
Definition vulkan.h:300
VkVideoSessionParametersKHR * session_params
FFVulkanDecodeShared * shared_ctx
struct HEVCHeaderSet * hevc_headers
unsigned int slice_off_max
FFVulkanExtensions decode_extension
VkVideoCodecOperationFlagBitsKHR decode_op
VkQueueFlagBits queue_flags
VkExtensionProperties ext_props
struct FFVulkanDecodePicture::@321376256110110067000314232201030232256240036361 view
VkVideoReferenceSlotInfoKHR ref_slot
VkImageAspectFlags aspect_ref
VkVideoDecodeInfoKHR decode_info
FFVkVideoDPBImage * dpb_img
PFN_vkInvalidateMappedMemoryRanges invalidate_memory_ranges
VkImageAspectFlags aspect
FFVkImageViews * out_views
VkVideoDecodeAV1ProfileInfoKHR av1_profile
VkVideoProfileListInfoKHR profile_list
VkVideoDecodeUsageInfoKHR usage
VkVideoDecodeH265ProfileInfoKHR h265_profile
VkVideoDecodeH264ProfileInfoKHR h264_profile
VkVideoProfileInfoKHR profile
FFVulkanContext s
H264Context.
Definition h264dec.h:347
#define av_free(p)
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static uint8_t tmp[40]
Definition aes_ctr.c:52
#define src
Definition vp8dsp.c:248
int size
RefStruct is an API for creating reference-counted objects with minimal overhead.
Definition refstruct.h:58
enum AVCodecID codec_id
#define FF_VK_DEFAULT_EXEC_CONTEXTS
Definition vulkan.h:121
static const void * ff_vk_find_struct(const void *chain, VkStructureType stype)
Definition vulkan.h:366
const FFVulkanDecodeDescriptor ff_vk_dec_apv_desc
Definition vulkan_apv.c:33
const FFVulkanDecodeDescriptor ff_vk_dec_av1_desc
Definition vulkan_av1.c:26
int ff_vk_decode_frame(AVCodecContext *avctx, AVFrame *pic, FFVulkanDecodePicture *vp, AVFrame *rpic[], FFVulkanDecodePicture *rvkp[])
Decode a frame.
int ff_vk_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
Synchronize the contexts between 2 threads.
static const VkVideoProfileInfoKHR * get_video_profile(FFVulkanDecodeShared *ctx, enum AVCodecID codec_id)
int ff_vk_decode_init(AVCodecContext *avctx)
Initialize decoder.
int ff_vk_decode_create_params(VkVideoSessionParametersKHR **par_ref, void *logctx, FFVulkanDecodeShared *ctx, const VkVideoSessionParametersCreateInfoKHR *session_params_create)
Create VkVideoSessionParametersKHR wrapped in an AVBufferRef.
static void free_common(AVRefStructOpaque unused, void *obj)
int ff_vk_params_invalidate(AVCodecContext *avctx, int t, const uint8_t *b, uint32_t s)
Removes current session parameters to recreate them.
static int profile_unsupported(VkResult ret)
static const FFVulkanDecodeDescriptor * dec_descs[]
int ff_vk_frame_params(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx)
Initialize hw_frames_ctx with the parameters needed to decode the stream using the parameters from av...
int ff_vk_decode_add_slice(AVCodecContext *avctx, FFVulkanDecodePicture *vp, const uint8_t *data, size_t size, int add_startcode, uint32_t *nb_slices, const uint32_t **offsets)
Add slice data to frame.
#define DECODER_IS_SDR(codec_id)
static int create_empty_session_parameters(AVCodecContext *avctx, FFVulkanDecodeShared *ctx, VkVideoSessionParametersKHR *empty_session_params)
static int vulkan_decode_bootstrap(AVCodecContext *avctx, AVBufferRef *frames_ref)
static int decode_reset(AVCodecContext *avctx, FFVulkanDecodeShared *ctx)
static VkResult vulkan_setup_profile(AVCodecContext *avctx, FFVulkanDecodeProfileData *prof, AVVulkanDeviceContext *hwctx, FFVulkanFunctions *vk, const FFVulkanDecodeDescriptor *vk_desc, VkVideoCapabilitiesKHR *caps, VkVideoDecodeCapabilitiesKHR *dec_caps, int cur_profile)
void ff_vk_decode_free_frame(AVHWDeviceContext *dev_ctx, FFVulkanDecodePicture *vp)
Free a frame and its state.
static const FFVulkanDecodeDescriptor * get_codecdesc(enum AVCodecID codec_id)
static void init_frame(FFVulkanDecodeContext *dec, FFVulkanDecodePicture *vkpic)
static void free_profile_data(AVHWFramesContext *hwfc)
int ff_vk_decode_uninit(AVCodecContext *avctx)
Free decoder.
static int vulkan_decode_get_profile(AVCodecContext *avctx, AVBufferRef *frames_ref, enum AVPixelFormat *pix_fmt, VkFormat *vk_fmt, FFVulkanDecodeProfileData *prof, int *dpb_dedicate)
static void vk_decode_free_params(AVRefStructOpaque opaque, void *obj)
int ff_vk_decode_prepare_frame(FFVulkanDecodeContext *dec, AVFrame *pic, FFVulkanDecodePicture *vkpic, int is_current, int alloc_dpb)
Prepare a frame, creates the image view, and sets up the dpb fields.
static const char * profile_name(enum AVCodecID codec_id, int profile)
const FFVulkanDecodeDescriptor ff_vk_dec_dpx_desc
Definition vulkan_dpx.c:34
const FFVulkanDecodeDescriptor ff_vk_dec_ffv1_desc
Definition vulkan_ffv1.c:63
#define FF_VK_EXT_VIDEO_MAINTENANCE_1
#define FF_VK_EXT_VIDEO_ENCODE_QUEUE
#define FF_VK_EXT_VIDEO_DECODE_QUEUE
#define FF_VK_EXT_VIDEO_MAINTENANCE_2
const FFVulkanDecodeDescriptor ff_vk_dec_h264_desc
Definition vulkan_h264.c:26
const FFVulkanDecodeDescriptor ff_vk_dec_hevc_desc
Definition vulkan_hevc.c:26
static uint64_t ff_vk_extensions_to_mask(const char *const *extensions, int nb_extensions)
static int ff_vk_load_functions(AVHWDeviceContext *ctx, FFVulkanFunctions *vk, uint64_t extensions_mask, int has_inst, int has_dev)
Function loader.
const FFVulkanDecodeDescriptor ff_vk_dec_prores_desc
const FFVulkanDecodeDescriptor ff_vk_dec_prores_raw_desc
VkVideoComponentBitDepthFlagBitsKHR ff_vk_depth_from_av_depth(int depth)
Get Vulkan's bit depth from an [8:12] integer.
VkVideoChromaSubsamplingFlagBitsKHR ff_vk_subsampling_from_av_desc(const AVPixFmtDescriptor *desc)
Get Vulkan's chroma subsampling from a pixfmt descriptor.
int ff_vk_h265_level_to_av(StdVideoH265LevelIdc level)
int ff_vk_create_view(FFVulkanContext *s, VkImageView *view, VkImageAspectFlags *aspect, VkImage img, VkFormat vkf, VkImageUsageFlags usage, int layered)
Creates image views for video frames.
int ff_vk_h264_level_to_av(StdVideoH264LevelIdc level)
Convert level from Vulkan to AV.
av_cold void ff_vk_video_common_uninit(FFVulkanContext *s, FFVkVideoCommon *common)
Free video session and required resources.
av_cold int ff_vk_video_dpb_init(FFVulkanContext *s, FFVkVideoCommon *common, VkFormat format, VkImageUsageFlags usage, VkImageTiling tiling, void *create_pnext, int width, int height, int nb_layers)
Initialize the internal DPB image pool.
enum AVPixelFormat ff_vk_pix_fmt_from_vkfmt(VkFormat vkf)
Get pixfmt from a Vulkan format.
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.
#define CODEC_VER(ver)
const FFVulkanDecodeDescriptor ff_vk_dec_vp9_desc
Definition vulkan_vp9.c:23
static int ref_frame(VVCFrame *dst, const VVCFrame *src)
Definition dec.c:619