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 "h264dec.h"
28
29#define DECODER_IS_SDR(codec_id) \
30 (((codec_id) == AV_CODEC_ID_FFV1) || \
31 ((codec_id) == AV_CODEC_ID_DPX) || \
32 ((codec_id) == AV_CODEC_ID_APV) || \
33 ((codec_id) == AV_CODEC_ID_PRORES_RAW) || \
34 ((codec_id) == AV_CODEC_ID_PRORES))
35
36#if CONFIG_H264_VULKAN_HWACCEL
38#endif
39#if CONFIG_HEVC_VULKAN_HWACCEL
41#endif
42#if CONFIG_VP9_VULKAN_HWACCEL
44#endif
45#if CONFIG_AV1_VULKAN_HWACCEL
47#endif
48#if CONFIG_FFV1_VULKAN_HWACCEL
50#endif
51#if CONFIG_PRORES_RAW_VULKAN_HWACCEL
53#endif
54#if CONFIG_PRORES_VULKAN_HWACCEL
56#endif
57#if CONFIG_DPX_VULKAN_HWACCEL
59#endif
60#if CONFIG_APV_VULKAN_HWACCEL
62#endif
63
65#if CONFIG_H264_VULKAN_HWACCEL
67#endif
68#if CONFIG_HEVC_VULKAN_HWACCEL
70#endif
71#if CONFIG_VP9_VULKAN_HWACCEL
73#endif
74#if CONFIG_AV1_VULKAN_HWACCEL
76#endif
77#if CONFIG_FFV1_VULKAN_HWACCEL
79#endif
80#if CONFIG_PRORES_RAW_VULKAN_HWACCEL
82#endif
83#if CONFIG_PRORES_VULKAN_HWACCEL
85#endif
86#if CONFIG_DPX_VULKAN_HWACCEL
88#endif
89#if CONFIG_APV_VULKAN_HWACCEL
91#endif
92};
93
95 VkVideoDecodeH264ProfileInfoKHR h264_profile;
96 VkVideoDecodeH265ProfileInfoKHR h265_profile;
97#if CONFIG_VP9_VULKAN_HWACCEL
98 VkVideoDecodeVP9ProfileInfoKHR vp9_profile;
99#endif
100 VkVideoDecodeAV1ProfileInfoKHR av1_profile;
101
102 VkVideoDecodeUsageInfoKHR usage;
103 VkVideoProfileInfoKHR profile;
104 VkVideoProfileListInfoKHR profile_list;
106
108{
109 for (size_t i = 0; i < FF_ARRAY_ELEMS(dec_descs); i++)
110 if (dec_descs[i]->codec_id == codec_id)
111 return dec_descs[i];
112 av_assert1(!"no codec descriptor");
113 return NULL;
114}
115
116static const VkVideoProfileInfoKHR *get_video_profile(FFVulkanDecodeShared *ctx, enum AVCodecID codec_id)
117{
118 const VkVideoProfileListInfoKHR *profile_list;
119
120 VkStructureType profile_struct_type =
121 codec_id == AV_CODEC_ID_H264 ? VK_STRUCTURE_TYPE_VIDEO_DECODE_H264_PROFILE_INFO_KHR :
122 codec_id == AV_CODEC_ID_HEVC ? VK_STRUCTURE_TYPE_VIDEO_DECODE_H265_PROFILE_INFO_KHR :
123#if CONFIG_VP9_VULKAN_HWACCEL
124 codec_id == AV_CODEC_ID_VP9 ? VK_STRUCTURE_TYPE_VIDEO_DECODE_VP9_PROFILE_INFO_KHR :
125#endif
126 codec_id == AV_CODEC_ID_AV1 ? VK_STRUCTURE_TYPE_VIDEO_DECODE_AV1_PROFILE_INFO_KHR :
127 VK_STRUCTURE_TYPE_MAX_ENUM;
128 if (profile_struct_type == VK_STRUCTURE_TYPE_MAX_ENUM)
129 return NULL;
130
131 profile_list = ff_vk_find_struct(ctx->s.hwfc->create_pnext,
132 VK_STRUCTURE_TYPE_VIDEO_PROFILE_LIST_INFO_KHR);
133 if (!profile_list)
134 return NULL;
135
136 for (int i = 0; i < profile_list->profileCount; i++)
137 if (ff_vk_find_struct(profile_list->pProfiles[i].pNext, profile_struct_type))
138 return &profile_list->pProfiles[i];
139
140 return NULL;
141}
142
144{
145 FFVulkanDecodeContext *src_ctx = src->internal->hwaccel_priv_data;
146 FFVulkanDecodeContext *dst_ctx = dst->internal->hwaccel_priv_data;
147
148 av_refstruct_replace(&dst_ctx->shared_ctx, src_ctx->shared_ctx);
149
151
152 dst_ctx->dedicated_dpb = src_ctx->dedicated_dpb;
153 dst_ctx->external_fg = src_ctx->external_fg;
154
155 return 0;
156}
157
158int ff_vk_params_invalidate(AVCodecContext *avctx, int t, const uint8_t *b, uint32_t s)
159{
162 return 0;
163}
164
166{
168 FFVulkanFunctions *vk = &ctx->s.vkfn;
169
170 vkpic->dpb_frame = NULL;
171 vkpic->dpb_img = NULL;
172 vkpic->out_views = NULL;
173 vkpic->view.ref = VK_NULL_HANDLE;
174 vkpic->view.out = VK_NULL_HANDLE;
175
176 vkpic->invalidate_memory_ranges = vk->InvalidateMappedMemoryRanges;
177}
178
180 FFVulkanDecodePicture *vkpic, int is_current,
181 int alloc_dpb)
182{
183 int err;
185
186 vkpic->slices_size = 0;
187
188 /* If the decoder made a blank frame to make up for a missing ref, or the
189 * frame is the current frame so it's missing one, create a re-representation */
190 if (vkpic->view.ref)
191 return 0;
192
193 init_frame(dec, vkpic);
194
195 /* Refcounted, so executions keep it alive past the picture */
196 vkpic->out_views = ff_vk_imageviews_alloc(&ctx->s, 1);
197 if (!vkpic->out_views)
198 return AVERROR(ENOMEM);
199
200 if (ctx->common.layered_dpb && alloc_dpb) {
201 vkpic->view.ref = ctx->common.layered_view;
202 vkpic->view.aspect_ref = ctx->common.layered_aspect;
203 } else if (alloc_dpb) {
204 vkpic->dpb_img = av_refstruct_pool_get(ctx->common.dpb->img_pool);
205 if (!vkpic->dpb_img)
206 return AVERROR(ENOMEM);
207
208 /* The view is owned by the pool entry */
209 vkpic->view.ref = vkpic->dpb_img->view;
210 vkpic->view.aspect_ref = vkpic->dpb_img->aspect;
211 }
212
213 if (!alloc_dpb || is_current) {
215 AVVulkanFramesContext *hwfc = frames->hwctx;
216
217 err = ff_vk_create_view(&ctx->s,
218 &vkpic->out_views->views[0], &vkpic->view.aspect,
219 ((AVVkFrame *)pic->data[0])->img[0],
220 hwfc->format[0],
221 VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR |
222 (hwfc->usage & VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR),
223 0);
224 // the above fixes VUID-VkVideoBeginCodingInfoKHR-slotIndex-07245
225 if (err < 0)
226 return err;
227 vkpic->view.out = vkpic->out_views->views[0];
228
229 if (!alloc_dpb) {
230 vkpic->view.ref = vkpic->view.out;
231 vkpic->view.aspect_ref = vkpic->view.aspect;
232 }
233 }
234
235 return 0;
236}
237
239 const uint8_t *data, size_t size, int add_startcode,
240 uint32_t *nb_slices, const uint32_t **offsets)
241{
244
245 static const uint8_t startcode_prefix[3] = { 0x0, 0x0, 0x1 };
246 const size_t startcode_len = add_startcode ? sizeof(startcode_prefix) : 0;
247 const int nb = nb_slices ? *nb_slices : 0;
248 uint8_t *slices;
249 uint32_t *slice_off;
250 FFVkBuffer *vkbuf;
251
252 size_t new_size = vp->slices_size + startcode_len + size +
253 ctx->caps.minBitstreamBufferSizeAlignment;
254 new_size = FFALIGN(new_size, ctx->caps.minBitstreamBufferSizeAlignment);
255
256 if (offsets) {
257 slice_off = av_fast_realloc(dec->slice_off, &dec->slice_off_max,
258 (nb + 1)*sizeof(*slice_off));
259 if (!slice_off)
260 return AVERROR(ENOMEM);
261
262 *offsets = dec->slice_off = slice_off;
263
264 slice_off[nb] = vp->slices_size;
265 }
266
267 vkbuf = vp->slices_buf;
268 if (!vkbuf || vkbuf->size < new_size) {
269 int err;
270 FFVkBuffer *new_buf;
271
272 /* No point in requesting anything smaller. */
273 size_t buf_size = FFMAX(new_size, 1024*1024);
274
275 /* Align buffer to nearest power of two. Makes fragmentation management
276 * easier, and gives us ample headroom. */
277 buf_size = 2 << av_log2(buf_size);
278
279 /* When the frames context uses DRM modifier tiling,
280 * hwctx->create_pnext contains VkImageDrmFormatModifierListCreateInfoEXT,
281 * which per spec does not extend VkBufferCreateInfo, so we need to find
282 * the VkVideoProfileListInfoKHR structure within it. */
283 void *buf_pnext = ctx->s.hwfc->create_pnext;
284 if (ctx->s.hwfc->tiling == VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT)
285 buf_pnext = (void *)ff_vk_find_struct(ctx->s.hwfc->create_pnext,
286 VK_STRUCTURE_TYPE_VIDEO_PROFILE_LIST_INFO_KHR);
287
288 VkBufferUsageFlags buf_usage = DECODER_IS_SDR(avctx->codec_id) ?
289 (VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
290 VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) :
291 VK_BUFFER_USAGE_VIDEO_DECODE_SRC_BIT_KHR;
292
293 err = ff_vk_get_pooled_buffer(&ctx->s, &ctx->buf_pool, &new_buf,
294 buf_usage, buf_pnext, buf_size,
295 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
296 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
297 if (err < 0)
298 err = ff_vk_get_pooled_buffer(&ctx->s, &ctx->buf_pool, &new_buf,
299 buf_usage, buf_pnext, buf_size,
300 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
301 if (err < 0)
302 return err;
303
304 /* Copy data from the old buffer */
305 if (vkbuf) {
306 memcpy(new_buf->mapped_mem, vkbuf->mapped_mem, vp->slices_size);
308 }
309
310 vp->slices_buf = new_buf;
311 vkbuf = new_buf;
312 }
313 slices = vkbuf->mapped_mem;
314
315 /* Startcode */
316 memcpy(slices + vp->slices_size, startcode_prefix, startcode_len);
317
318 /* Slice data */
319 memcpy(slices + vp->slices_size + startcode_len, data, size);
320
321 if (nb_slices)
322 *nb_slices = nb + 1;
323
324 vp->slices_size += startcode_len + size;
325
326 return 0;
327}
328
331 VkVideoSessionParametersKHR *empty_session_params)
332{
333 if (avctx->codec_id == AV_CODEC_ID_VP9)
334 return 0;
335
336 VkVideoDecodeH264SessionParametersCreateInfoKHR h264_params = {
337 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H264_SESSION_PARAMETERS_CREATE_INFO_KHR,
338 };
339 VkVideoDecodeH265SessionParametersCreateInfoKHR h265_params = {
340 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H265_SESSION_PARAMETERS_CREATE_INFO_KHR,
341 };
342 StdVideoAV1SequenceHeader av1_empty_seq = { 0 };
343 VkVideoDecodeAV1SessionParametersCreateInfoKHR av1_params = {
344 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_AV1_SESSION_PARAMETERS_CREATE_INFO_KHR,
345 .pStdSequenceHeader = &av1_empty_seq,
346 };
347 VkVideoSessionParametersCreateInfoKHR session_params_create = {
348 .sType = VK_STRUCTURE_TYPE_VIDEO_SESSION_PARAMETERS_CREATE_INFO_KHR,
349 .pNext = avctx->codec_id == AV_CODEC_ID_H264 ? (void *)&h264_params :
350 avctx->codec_id == AV_CODEC_ID_HEVC ? (void *)&h265_params :
351 avctx->codec_id == AV_CODEC_ID_AV1 ? (void *)&av1_params :
352 NULL,
353 .videoSession = ctx->common.session,
354 };
355
356 VkResult ret;
357 FFVulkanContext *s = &ctx->s;
358 FFVulkanFunctions *vk = &s->vkfn;
359 ret = vk->CreateVideoSessionParametersKHR(s->hwctx->act_dev, &session_params_create,
360 s->hwctx->alloc, empty_session_params);
361 if (ret != VK_SUCCESS) {
362 av_log(avctx, AV_LOG_ERROR, "Unable to create empty Vulkan video session parameters: %s!\n",
363 ff_vk_ret2str(ret));
364 return AVERROR_EXTERNAL;
365 }
366
367 return 0;
368}
369
371{
372 int err;
373 FFVulkanContext *s = &ctx->s;
374 FFVulkanFunctions *vk = &s->vkfn;
375
376 VkVideoBeginCodingInfoKHR decode_start = {
377 .sType = VK_STRUCTURE_TYPE_VIDEO_BEGIN_CODING_INFO_KHR,
378 .videoSession = ctx->common.session,
379 };
380
381 if (!(ctx->s.extensions & FF_VK_EXT_VIDEO_MAINTENANCE_2)) {
383 &decode_start.videoSessionParameters);
384 if (err < 0)
385 return err;
386 }
387
388 VkVideoCodingControlInfoKHR decode_ctrl = {
389 .sType = VK_STRUCTURE_TYPE_VIDEO_CODING_CONTROL_INFO_KHR,
390 .flags = VK_VIDEO_CODING_CONTROL_RESET_BIT_KHR,
391 };
392 VkVideoEndCodingInfoKHR decode_end = {
393 .sType = VK_STRUCTURE_TYPE_VIDEO_END_CODING_INFO_KHR,
394 };
395
396 FFVkExecContext *exec = ff_vk_exec_get(&ctx->s, &ctx->exec_pool);
397 err = ff_vk_exec_start(&ctx->s, exec);
398 if (err < 0)
399 return err;
400
401 vk->CmdBeginVideoCodingKHR(exec->buf, &decode_start);
402 vk->CmdControlVideoCodingKHR(exec->buf, &decode_ctrl);
403 vk->CmdEndVideoCodingKHR(exec->buf, &decode_end);
404
405 err = ff_vk_exec_submit(&ctx->s, exec);
406
407 if (decode_start.videoSessionParameters) {
408 /* Wait to complete to delete the temporary session parameters */
409 if (err >= 0)
410 ff_vk_exec_wait(&ctx->s, exec);
411
412 vk->DestroyVideoSessionParametersKHR(s->hwctx->act_dev,
413 decode_start.videoSessionParameters,
414 s->hwctx->alloc);
415 }
416
417 if (err < 0)
418 av_log(avctx, AV_LOG_ERROR, "Unable to reset decoder: %s",
419 ff_vk_ret2str(err));
420
421 return err;
422}
423
426 AVFrame *rpic[], FFVulkanDecodePicture *rvkp[])
427{
428 int err;
429 VkResult ret;
430 VkCommandBuffer cmd_buf;
431 FFVkBuffer *sd_buf;
432
435 FFVulkanFunctions *vk = &ctx->s.vkfn;
436
437 /* Output */
438 AVVkFrame *vkf = (AVVkFrame *)pic->buf[0]->data;
439
440 /* Quirks */
441 const int layered_dpb = ctx->common.layered_dpb;
442
443 VkVideoBeginCodingInfoKHR decode_start = {
444 .sType = VK_STRUCTURE_TYPE_VIDEO_BEGIN_CODING_INFO_KHR,
445 .videoSession = ctx->common.session,
446 .videoSessionParameters = dec->session_params ?
447 *dec->session_params : VK_NULL_HANDLE,
448 .referenceSlotCount = vp->decode_info.referenceSlotCount,
449 .pReferenceSlots = vp->decode_info.pReferenceSlots,
450 };
451 VkVideoEndCodingInfoKHR decode_end = {
452 .sType = VK_STRUCTURE_TYPE_VIDEO_END_CODING_INFO_KHR,
453 };
454
455 VkImageMemoryBarrier2 img_bar[38];
456 int nb_img_bar = 0;
457
458 size_t data_size = FFALIGN(vp->slices_size,
459 ctx->caps.minBitstreamBufferSizeAlignment);
460
461 FFVkExecContext *exec = ff_vk_exec_get(&ctx->s, &ctx->exec_pool);
462
463 /* The current picture's resource has to be bound as an inactive
464 * reference, unless its slot is already bound as an active one, as when
465 * decoding the second field of a pair. */
466 int cur_bound = 0;
467 for (int i = 0; i < decode_start.referenceSlotCount; i++)
468 cur_bound |= decode_start.pReferenceSlots[i].slotIndex == vp->ref_slot.slotIndex;
469 if (!cur_bound) {
470 VkVideoReferenceSlotInfoKHR *cur_vk_ref;
471 cur_vk_ref = (void *)&decode_start.pReferenceSlots[decode_start.referenceSlotCount];
472 cur_vk_ref[0] = vp->ref_slot;
473 cur_vk_ref[0].slotIndex = -1;
474 decode_start.referenceSlotCount++;
475 }
476
477 sd_buf = vp->slices_buf;
478
479 /* Flush if needed */
480 if (!(sd_buf->flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) {
481 VkMappedMemoryRange flush_buf = {
482 .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
483 .memory = sd_buf->mem,
484 .offset = 0,
485 .size = FFALIGN(vp->slices_size,
486 ctx->s.props.properties.limits.nonCoherentAtomSize),
487 };
488
489 ret = vk->FlushMappedMemoryRanges(ctx->s.hwctx->act_dev, 1, &flush_buf);
490 if (ret != VK_SUCCESS) {
491 av_log(avctx, AV_LOG_ERROR, "Failed to flush memory: %s\n",
492 ff_vk_ret2str(ret));
493 return AVERROR_EXTERNAL;
494 }
495 }
496
497 vp->decode_info.srcBuffer = sd_buf->buf;
498 vp->decode_info.srcBufferOffset = 0;
499 vp->decode_info.srcBufferRange = data_size;
500
501 /* Start command buffer recording */
502 err = ff_vk_exec_start(&ctx->s, exec);
503 if (err < 0)
504 return err;
505 cmd_buf = exec->buf;
506
507 /* Slices; owned by the execution from now on */
509
510 /* Parameters; unset when inline session parameters are used */
511 if (dec->session_params)
513
514 err = ff_vk_exec_add_dep_frame(&ctx->s, exec, pic,
515 VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
516 VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR);
517 if (err < 0) {
518 ff_vk_exec_discard(&ctx->s, exec);
519 return err;
520 }
521
522 /* The output view is kept alive by the execution context; freeing the
523 * picture then needs no host wait. */
525
526 /* Output image - change layout, as it comes from a pool */
527 img_bar[nb_img_bar] = (VkImageMemoryBarrier2) {
528 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
529 .pNext = NULL,
530 .srcStageMask = VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
531 .dstStageMask = VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
532 .srcAccessMask = VK_ACCESS_2_NONE,
533 .dstAccessMask = VK_ACCESS_2_VIDEO_DECODE_WRITE_BIT_KHR,
534 .oldLayout = vkf->layout[0],
535 .newLayout = (layered_dpb || vp->dpb_img) ?
536 VK_IMAGE_LAYOUT_VIDEO_DECODE_DST_KHR :
537 VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR, /* Spec, 07252 utter madness */
538 .srcQueueFamilyIndex = vkf->queue_family[0],
539 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
540 .image = vkf->img[0],
541 .subresourceRange = (VkImageSubresourceRange) {
542 .aspectMask = vp->view.aspect,
543 .layerCount = 1,
544 .levelCount = 1,
545 },
546 };
547 ff_vk_exec_update_frame(&ctx->s, exec, pic,
548 &img_bar[nb_img_bar], &nb_img_bar);
549
550 /* Current picture's DISTINCT-mode DPB image: transition on first use */
551 if (vp->dpb_img) {
552 FFVkVideoDPBImage *di = vp->dpb_img;
553
554 if (di->layout != VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR) {
555 img_bar[nb_img_bar] = (VkImageMemoryBarrier2) {
556 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
557 .srcStageMask = VK_PIPELINE_STAGE_2_NONE,
558 .dstStageMask = VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
559 .srcAccessMask = VK_ACCESS_2_NONE,
560 .dstAccessMask = VK_ACCESS_2_VIDEO_DECODE_READ_BIT_KHR |
561 VK_ACCESS_2_VIDEO_DECODE_WRITE_BIT_KHR,
562 .oldLayout = di->layout,
563 .newLayout = VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR,
564 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
565 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
566 .image = di->img,
567 .subresourceRange = (VkImageSubresourceRange) {
568 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
569 .layerCount = 1,
570 .levelCount = 1,
571 },
572 };
573 nb_img_bar++;
574 di->layout = VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR;
575 }
576 }
577
578 /* COINCIDE-mode references only: DISTINCT-mode references are covered
579 * by the memory barrier, with pool-owned views */
580 if (!dec->dedicated_dpb) {
581 for (int i = 0; i < vp->decode_info.referenceSlotCount; i++) {
582 AVFrame *ref_frame = rpic[i];
583 FFVulkanDecodePicture *rvp = rvkp[i];
584
585 err = ff_vk_exec_add_dep_frame(&ctx->s, exec, ref_frame,
586 VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
587 VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR);
588 if (err < 0) {
589 ff_vk_exec_discard(&ctx->s, exec);
590 return err;
591 }
592
593 /* The reference's image views are kept alive by the execution,
594 * so freeing the picture needs no host wait. */
595 if (err == 0 && rvp->out_views)
597
598 {
599 AVVkFrame *rvkf = (AVVkFrame *)ref_frame->data[0];
600
601 img_bar[nb_img_bar] = (VkImageMemoryBarrier2) {
602 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
603 .pNext = NULL,
604 .srcStageMask = VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
605 .dstStageMask = VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
606 .srcAccessMask = VK_ACCESS_2_NONE,
607 .dstAccessMask = VK_ACCESS_2_VIDEO_DECODE_READ_BIT_KHR |
608 VK_ACCESS_2_VIDEO_DECODE_WRITE_BIT_KHR,
609 .oldLayout = rvkf->layout[0],
610 .newLayout = VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR,
611 .srcQueueFamilyIndex = rvkf->queue_family[0],
612 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
613 .image = rvkf->img[0],
614 .subresourceRange = (VkImageSubresourceRange) {
615 .aspectMask = rvp->view.aspect_ref,
616 .layerCount = 1,
617 .levelCount = 1,
618 },
619 };
621 &img_bar[nb_img_bar], &nb_img_bar);
622 }
623 }
624 }
625
626 /* Internal DPB images are queue-exclusive: one decode-stage memory
627 * barrier per submission orders all prior work, references included, so
628 * they need no semaphores or dependencies, and image barriers only for
629 * initial layout transitions. */
630 VkMemoryBarrier2 mem_bar = {
631 .sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2,
632 .srcStageMask = VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
633 .srcAccessMask = VK_ACCESS_2_VIDEO_DECODE_WRITE_BIT_KHR,
634 .dstStageMask = VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
635 .dstAccessMask = VK_ACCESS_2_VIDEO_DECODE_READ_BIT_KHR |
636 VK_ACCESS_2_VIDEO_DECODE_WRITE_BIT_KHR,
637 };
638
639 /* Change image layouts, and synchronize the internal DPB if in use */
640 vk->CmdPipelineBarrier2(cmd_buf, &(VkDependencyInfo) {
641 .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
642 .dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT,
643 .pMemoryBarriers = &mem_bar,
644 .memoryBarrierCount = ctx->common.dpb ? 1 : 0,
645 .pImageMemoryBarriers = img_bar,
646 .imageMemoryBarrierCount = nb_img_bar,
647 });
648
649 /* Start, use parameters, decode and end decoding */
650 vk->CmdBeginVideoCodingKHR(cmd_buf, &decode_start);
651 vk->CmdDecodeVideoKHR(cmd_buf, &vp->decode_info);
652 vk->CmdEndVideoCodingKHR(cmd_buf, &decode_end);
653
654 /* End recording and submit for execution */
655 return ff_vk_exec_submit(&ctx->s, exec);
656}
657
659{
660 /* Free slices data */
662
663 /* The views are kept alive by every execution referencing them, so no
664 * host wait is needed. */
666
668 /* No wait: pool reuse is ordered by the DPB barrier */
670}
671
672static void free_common(AVRefStructOpaque unused, void *obj)
673{
675 FFVulkanContext *s = &ctx->s;
676
677 /* Wait on and free execution pool */
678 ff_vk_exec_pool_free(&ctx->s, &ctx->exec_pool);
679
680 av_refstruct_pool_uninit(&ctx->buf_pool);
681
683
684 if (ctx->sd_ctx_free)
685 ctx->sd_ctx_free(ctx);
686
688}
689
691{
692 int err;
694 const FFVulkanDecodeDescriptor *vk_desc = get_codecdesc(avctx->codec_id);
696 AVHWDeviceContext *device = (AVHWDeviceContext *)frames->device_ref->data;
697 AVVulkanDeviceContext *hwctx = device->hwctx;
699
700 if (dec->shared_ctx)
701 return 0;
702
703 dec->shared_ctx = av_refstruct_alloc_ext(sizeof(*ctx), 0, NULL,
705 if (!dec->shared_ctx)
706 return AVERROR(ENOMEM);
707
708 ctx = dec->shared_ctx;
709
712
713 if (vk_desc->queue_flags & VK_QUEUE_VIDEO_DECODE_BIT_KHR) {
714 if (!(ctx->s.extensions & FF_VK_EXT_VIDEO_DECODE_QUEUE)) {
715 av_log(avctx, AV_LOG_ERROR, "Device does not support the %s extension!\n",
716 VK_KHR_VIDEO_DECODE_QUEUE_EXTENSION_NAME);
718 return AVERROR(ENOSYS);
719 }
720 }
721
722 err = ff_vk_load_functions(device, &ctx->s.vkfn, ctx->s.extensions, 1, 1);
723 if (err < 0) {
725 return err;
726 }
727
728 return 0;
729}
730
735 const FFVulkanDecodeDescriptor *vk_desc,
736 VkVideoDecodeH264CapabilitiesKHR *h264_caps,
737 VkVideoDecodeH265CapabilitiesKHR *h265_caps,
738#if CONFIG_VP9_VULKAN_HWACCEL
739 VkVideoDecodeVP9CapabilitiesKHR *vp9_caps,
740#endif
741 VkVideoDecodeAV1CapabilitiesKHR *av1_caps,
742 VkVideoCapabilitiesKHR *caps,
743 VkVideoDecodeCapabilitiesKHR *dec_caps,
744 int cur_profile)
745{
746 VkVideoDecodeUsageInfoKHR *usage = &prof->usage;
747 VkVideoProfileInfoKHR *profile = &prof->profile;
748 VkVideoProfileListInfoKHR *profile_list = &prof->profile_list;
749
750 VkVideoDecodeH264ProfileInfoKHR *h264_profile = &prof->h264_profile;
751 VkVideoDecodeH265ProfileInfoKHR *h265_profile = &prof->h265_profile;
752#if CONFIG_VP9_VULKAN_HWACCEL
753 VkVideoDecodeVP9ProfileInfoKHR *vp9_profile = &prof->vp9_profile;
754#endif
755 VkVideoDecodeAV1ProfileInfoKHR *av1_profile = &prof->av1_profile;
756
758 if (!desc)
759 return AVERROR(EINVAL);
760
761 if (avctx->codec_id == AV_CODEC_ID_H264) {
762 dec_caps->pNext = h264_caps;
763 usage->pNext = h264_profile;
764 h264_profile->sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H264_PROFILE_INFO_KHR;
765
766 /* Vulkan transmits all the constrant_set flags, rather than wanting them
767 * merged in the profile IDC */
768 h264_profile->stdProfileIdc = cur_profile & ~(AV_PROFILE_H264_CONSTRAINED |
770
771 h264_profile->pictureLayout =
772 VK_VIDEO_DECODE_H264_PICTURE_LAYOUT_PROGRESSIVE_KHR;
773 const H264Context *h = avctx->priv_data;
774 if (h && h->ps.sps && !h->ps.sps->frame_mbs_only_flag)
775 h264_profile->pictureLayout =
776 VK_VIDEO_DECODE_H264_PICTURE_LAYOUT_INTERLACED_INTERLEAVED_LINES_BIT_KHR;
777 } else if (avctx->codec_id == AV_CODEC_ID_H265) {
778 dec_caps->pNext = h265_caps;
779 usage->pNext = h265_profile;
780 h265_profile->sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H265_PROFILE_INFO_KHR;
781 h265_profile->stdProfileIdc = cur_profile;
782#if CONFIG_VP9_VULKAN_HWACCEL
783 } else if (avctx->codec_id == AV_CODEC_ID_VP9) {
784 dec_caps->pNext = vp9_caps;
785 usage->pNext = vp9_profile;
786 vp9_profile->sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_VP9_PROFILE_INFO_KHR;
787 vp9_profile->stdProfile = cur_profile;
788#endif
789 } else if (avctx->codec_id == AV_CODEC_ID_AV1) {
790 dec_caps->pNext = av1_caps;
791 usage->pNext = av1_profile;
792 av1_profile->sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_AV1_PROFILE_INFO_KHR;
793 av1_profile->stdProfile = cur_profile;
794 av1_profile->filmGrainSupport = !(avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN);
795 }
796
797 usage->sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_USAGE_INFO_KHR;
798 usage->videoUsageHints = VK_VIDEO_DECODE_USAGE_DEFAULT_KHR;
799
800 profile->sType = VK_STRUCTURE_TYPE_VIDEO_PROFILE_INFO_KHR;
801 profile->pNext = usage;
802 profile->videoCodecOperation = vk_desc->decode_op;
803 profile->chromaSubsampling = ff_vk_subsampling_from_av_desc(desc);
804 profile->lumaBitDepth = ff_vk_depth_from_av_depth(desc->comp[0].depth);
805 profile->chromaBitDepth = profile->lumaBitDepth;
806
807 /* A pixel format without a video profile representation is not a valid
808 * profile to query the capabilities for */
809 if (profile->chromaSubsampling == VK_VIDEO_CHROMA_SUBSAMPLING_INVALID_KHR ||
810 profile->lumaBitDepth == VK_VIDEO_COMPONENT_BIT_DEPTH_INVALID_KHR)
811 return VK_ERROR_VIDEO_PROFILE_FORMAT_NOT_SUPPORTED_KHR;
812
813 profile_list->sType = VK_STRUCTURE_TYPE_VIDEO_PROFILE_LIST_INFO_KHR;
814 profile_list->profileCount = 1;
815 profile_list->pProfiles = profile;
816
817 /* Get the capabilities of the decoder for the given profile */
818 caps->sType = VK_STRUCTURE_TYPE_VIDEO_CAPABILITIES_KHR;
819 caps->pNext = dec_caps;
820 dec_caps->sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_CAPABILITIES_KHR;
821 /* dec_caps->pNext already filled in */
822
823 return vk->GetPhysicalDeviceVideoCapabilitiesKHR(hwctx->phys_dev, profile,
824 caps);
825}
826
828 enum AVPixelFormat *pix_fmt, VkFormat *vk_fmt,
830 int *dpb_dedicate)
831{
832 VkResult ret;
833 int max_level, base_profile, cur_profile;
834 const FFVulkanDecodeDescriptor *vk_desc = get_codecdesc(avctx->codec_id);
836 AVHWDeviceContext *device = (AVHWDeviceContext *)frames->device_ref->data;
837 AVVulkanDeviceContext *hwctx = device->hwctx;
838 enum AVPixelFormat source_format;
839 enum AVPixelFormat best_format;
840 VkFormat best_vkfmt;
841
844 FFVulkanFunctions *vk = &ctx->s.vkfn;
845
846 VkVideoCapabilitiesKHR *caps = &ctx->caps;
847 VkVideoDecodeCapabilitiesKHR *dec_caps = &ctx->dec_caps;
848
849 VkVideoDecodeH264CapabilitiesKHR h264_caps = {
850 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H264_CAPABILITIES_KHR,
851 };
852 VkVideoDecodeH265CapabilitiesKHR h265_caps = {
853 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H265_CAPABILITIES_KHR,
854 };
855#if CONFIG_VP9_VULKAN_HWACCEL
856 VkVideoDecodeVP9CapabilitiesKHR vp9_caps = {
857 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_VP9_CAPABILITIES_KHR,
858 };
859#endif
860 VkVideoDecodeAV1CapabilitiesKHR av1_caps = {
861 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_AV1_CAPABILITIES_KHR,
862 };
863
864 VkPhysicalDeviceVideoFormatInfoKHR fmt_info = {
865 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VIDEO_FORMAT_INFO_KHR,
866 .pNext = &prof->profile_list,
867 };
868 VkVideoFormatPropertiesKHR *ret_info;
869 uint32_t nb_out_fmts = 0;
870
871 if (!(vk_desc->decode_extension & ctx->s.extensions)) {
872 av_log(avctx, AV_LOG_ERROR, "Device does not support decoding %s!\n",
873 avcodec_get_name(avctx->codec_id));
874 return AVERROR(ENOSYS);
875 }
876
877 cur_profile = avctx->profile;
880#if CONFIG_VP9_VULKAN_HWACCEL
881 avctx->codec_id == AV_CODEC_ID_VP9 ? STD_VIDEO_VP9_PROFILE_0 :
882#endif
883 avctx->codec_id == AV_CODEC_ID_AV1 ? STD_VIDEO_AV1_PROFILE_MAIN :
884 0;
885
886 ret = vulkan_setup_profile(avctx, prof, hwctx, vk, vk_desc,
887 &h264_caps,
888 &h265_caps,
889#if CONFIG_VP9_VULKAN_HWACCEL
890 &vp9_caps,
891#endif
892 &av1_caps,
893 caps,
894 dec_caps,
895 cur_profile);
896 if (ret == VK_ERROR_VIDEO_PROFILE_OPERATION_NOT_SUPPORTED_KHR &&
898 avctx->profile != base_profile) {
899 av_log(avctx, AV_LOG_VERBOSE, "%s profile %s not supported, attempting "
900 "again with profile %s\n",
902 avcodec_profile_name(avctx->codec_id, cur_profile),
903 avcodec_profile_name(avctx->codec_id, base_profile));
904 cur_profile = base_profile;
905 ret = vulkan_setup_profile(avctx, prof, hwctx, vk, vk_desc,
906 &h264_caps,
907 &h265_caps,
908#if CONFIG_VP9_VULKAN_HWACCEL
909 &vp9_caps,
910#endif
911 &av1_caps,
912 caps,
913 dec_caps,
914 cur_profile);
915 }
916
917 if (ret == VK_ERROR_VIDEO_PROFILE_OPERATION_NOT_SUPPORTED_KHR ||
918 ret == VK_ERROR_VIDEO_PROFILE_CODEC_NOT_SUPPORTED_KHR) {
919 av_log(avctx, AV_LOG_VERBOSE, "Unable to initialize video session: "
920 "%s profile \"%s\" not supported!\n",
922 avcodec_profile_name(avctx->codec_id, cur_profile));
923 return AVERROR(EINVAL);
924 } else if (ret == VK_ERROR_VIDEO_PICTURE_LAYOUT_NOT_SUPPORTED_KHR) {
925 if (avctx->codec_id == AV_CODEC_ID_H264)
926 av_log(avctx, AV_LOG_VERBOSE, "Unable to initialize video session: "
927 "pictureLayout %#x not supported!\n",
928 (unsigned)prof->h264_profile.pictureLayout);
929 else
930 av_log(avctx, AV_LOG_VERBOSE, "Unable to initialize video session: "
931 "pictureLayout not supported!\n");
932 return AVERROR(EINVAL);
933 } else if (ret == VK_ERROR_VIDEO_PROFILE_FORMAT_NOT_SUPPORTED_KHR) {
934 av_log(avctx, AV_LOG_VERBOSE, "Unable to initialize video session: "
935 "format (%s) not supported!\n",
937 return AVERROR(EINVAL);
938 } else if (ret == VK_ERROR_FEATURE_NOT_PRESENT ||
939 ret == VK_ERROR_FORMAT_NOT_SUPPORTED) {
940 return AVERROR(EINVAL);
941 } else if (ret != VK_SUCCESS) {
942 return AVERROR_EXTERNAL;
943 }
944
945 max_level = avctx->codec_id == AV_CODEC_ID_H264 ? ff_vk_h264_level_to_av(h264_caps.maxLevelIdc) :
946 avctx->codec_id == AV_CODEC_ID_H265 ? ff_vk_h265_level_to_av(h265_caps.maxLevelIdc) :
947#if CONFIG_VP9_VULKAN_HWACCEL
948 avctx->codec_id == AV_CODEC_ID_VP9 ? vp9_caps.maxLevel :
949#endif
950 avctx->codec_id == AV_CODEC_ID_AV1 ? av1_caps.maxLevel :
951 0;
952
953 av_log(avctx, AV_LOG_VERBOSE, "Decoder capabilities for %s profile \"%s\":\n",
955 avcodec_profile_name(avctx->codec_id, cur_profile));
956 av_log(avctx, AV_LOG_VERBOSE, " Maximum level: %i (stream %i)\n",
957 max_level, avctx->level);
958 av_log(avctx, AV_LOG_VERBOSE, " Width: from %i to %i\n",
959 caps->minCodedExtent.width, caps->maxCodedExtent.width);
960 av_log(avctx, AV_LOG_VERBOSE, " Height: from %i to %i\n",
961 caps->minCodedExtent.height, caps->maxCodedExtent.height);
962 av_log(avctx, AV_LOG_VERBOSE, " Width alignment: %i\n",
963 caps->pictureAccessGranularity.width);
964 av_log(avctx, AV_LOG_VERBOSE, " Height alignment: %i\n",
965 caps->pictureAccessGranularity.height);
966 av_log(avctx, AV_LOG_VERBOSE, " Bitstream offset alignment: %"PRIu64"\n",
967 caps->minBitstreamBufferOffsetAlignment);
968 av_log(avctx, AV_LOG_VERBOSE, " Bitstream size alignment: %"PRIu64"\n",
969 caps->minBitstreamBufferSizeAlignment);
970 av_log(avctx, AV_LOG_VERBOSE, " Maximum references: %u\n",
971 caps->maxDpbSlots);
972 av_log(avctx, AV_LOG_VERBOSE, " Maximum active references: %u\n",
973 caps->maxActiveReferencePictures);
974 av_log(avctx, AV_LOG_VERBOSE, " Codec header name: '%s' (driver), '%s' (compiled)\n",
975 caps->stdHeaderVersion.extensionName,
976 vk_desc->ext_props.extensionName);
977 av_log(avctx, AV_LOG_VERBOSE, " Codec header version: %i.%i.%i (driver), %i.%i.%i (compiled)\n",
978 CODEC_VER(caps->stdHeaderVersion.specVersion),
979 CODEC_VER(vk_desc->ext_props.specVersion));
980 av_log(avctx, AV_LOG_VERBOSE, " Decode modes:%s%s%s\n",
981 dec_caps->flags ? "" :
982 " invalid",
983 dec_caps->flags & VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_COINCIDE_BIT_KHR ?
984 " reuse_dst_dpb" : "",
985 dec_caps->flags & VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_DISTINCT_BIT_KHR ?
986 " dedicated_dpb" : "");
987 av_log(avctx, AV_LOG_VERBOSE, " Capability flags:%s%s%s\n",
988 caps->flags ? "" :
989 " none",
990 caps->flags & VK_VIDEO_CAPABILITY_PROTECTED_CONTENT_BIT_KHR ?
991 " protected" : "",
992 caps->flags & VK_VIDEO_CAPABILITY_SEPARATE_REFERENCE_IMAGES_BIT_KHR ?
993 " separate_references" : "");
994
995 /* Check if decoding is possible with the given parameters */
996 if (avctx->coded_width < caps->minCodedExtent.width ||
997 avctx->coded_height < caps->minCodedExtent.height ||
998 avctx->coded_width > caps->maxCodedExtent.width ||
999 avctx->coded_height > caps->maxCodedExtent.height)
1000 return AVERROR(EINVAL);
1001
1003 avctx->level > max_level)
1004 return AVERROR(EINVAL);
1005
1006 /* Some basic sanity checking */
1007 if (!(dec_caps->flags & (VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_COINCIDE_BIT_KHR |
1008 VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_DISTINCT_BIT_KHR))) {
1009 av_log(avctx, AV_LOG_ERROR, "Buggy driver signals invalid decoding mode: neither "
1010 "VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_COINCIDE_BIT_KHR nor "
1011 "VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_DISTINCT_BIT_KHR are set!\n");
1012 return AVERROR_EXTERNAL;
1013 } else if ((dec_caps->flags & (VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_COINCIDE_BIT_KHR |
1014 VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_DISTINCT_BIT_KHR)) ==
1015 VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_COINCIDE_BIT_KHR &&
1016 !(caps->flags & VK_VIDEO_CAPABILITY_SEPARATE_REFERENCE_IMAGES_BIT_KHR)) {
1017 av_log(avctx, AV_LOG_VERBOSE,
1018 "COINCIDE-only decode without SEPARATE_REFERENCE_IMAGES "
1019 "(layered DPB capability); continuing\n");
1020 }
1021
1022 dec->dedicated_dpb = !!(dec_caps->flags & VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_DISTINCT_BIT_KHR);
1023 ctx->common.layered_dpb = !dec->dedicated_dpb ? 0 :
1024 !(caps->flags & VK_VIDEO_CAPABILITY_SEPARATE_REFERENCE_IMAGES_BIT_KHR);
1025
1026 if (dec->dedicated_dpb) {
1027 fmt_info.imageUsage = VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR;
1028 } else {
1029 fmt_info.imageUsage = VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR |
1030 VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR |
1031 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
1032 VK_IMAGE_USAGE_SAMPLED_BIT;
1033
1034 if ((ctx->s.extensions & FF_VK_EXT_VIDEO_ENCODE_QUEUE) &&
1035 (ctx->s.extensions & FF_VK_EXT_VIDEO_MAINTENANCE_1))
1036 fmt_info.imageUsage |= VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR;
1037 }
1038
1039 /* Get the format of the images necessary */
1040 ret = vk->GetPhysicalDeviceVideoFormatPropertiesKHR(hwctx->phys_dev,
1041 &fmt_info,
1042 &nb_out_fmts, NULL);
1043 if (ret == VK_ERROR_FORMAT_NOT_SUPPORTED ||
1044 (!nb_out_fmts && ret == VK_SUCCESS)) {
1045 return AVERROR(EINVAL);
1046 } else if (ret != VK_SUCCESS) {
1047 av_log(avctx, AV_LOG_ERROR, "Unable to get Vulkan format properties: %s!\n",
1048 ff_vk_ret2str(ret));
1049 return AVERROR_EXTERNAL;
1050 }
1051
1052 ret_info = av_mallocz(sizeof(*ret_info)*nb_out_fmts);
1053 if (!ret_info)
1054 return AVERROR(ENOMEM);
1055
1056 for (int i = 0; i < nb_out_fmts; i++)
1057 ret_info[i].sType = VK_STRUCTURE_TYPE_VIDEO_FORMAT_PROPERTIES_KHR;
1058
1059 ret = vk->GetPhysicalDeviceVideoFormatPropertiesKHR(hwctx->phys_dev,
1060 &fmt_info,
1061 &nb_out_fmts, ret_info);
1062 if (ret == VK_ERROR_FORMAT_NOT_SUPPORTED ||
1063 (!nb_out_fmts && ret == VK_SUCCESS)) {
1064 av_free(ret_info);
1065 return AVERROR(EINVAL);
1066 } else if (ret != VK_SUCCESS) {
1067 av_log(avctx, AV_LOG_ERROR, "Unable to get Vulkan format properties: %s!\n",
1068 ff_vk_ret2str(ret));
1069 av_free(ret_info);
1070 return AVERROR_EXTERNAL;
1071 }
1072
1073 /* Find a format to use */
1074 *pix_fmt = best_format = AV_PIX_FMT_NONE;
1075 *vk_fmt = best_vkfmt = VK_FORMAT_UNDEFINED;
1076 source_format = avctx->sw_pix_fmt;
1077
1078 av_log(avctx, AV_LOG_DEBUG, "Choosing best pixel format for decoding from %i:\n", nb_out_fmts);
1079 for (int i = 0; i < nb_out_fmts; i++) {
1081 if (tmp == AV_PIX_FMT_NONE) {
1082 av_log(avctx, AV_LOG_WARNING, "Invalid/unknown Vulkan format %i!\n", ret_info[i].format);
1083 continue;
1084 }
1085
1086 best_format = av_find_best_pix_fmt_of_2(tmp, best_format, source_format, 0, NULL);
1087 if (tmp == best_format)
1088 best_vkfmt = ret_info[i].format;
1089
1090 av_log(avctx, AV_LOG_DEBUG, " %s%s (Vulkan ID: %i)\n",
1091 av_get_pix_fmt_name(tmp), tmp == best_format ? "*" : "",
1092 ret_info[i].format);
1093 }
1094
1095 av_free(ret_info);
1096
1097 if (best_format == AV_PIX_FMT_NONE) {
1098 av_log(avctx, AV_LOG_ERROR, "No valid/compatible pixel format found for decoding!\n");
1099 return AVERROR(EINVAL);
1100 } else {
1101 av_log(avctx, AV_LOG_VERBOSE, "Chosen frame pixfmt: %s (Vulkan ID: %i)\n",
1102 av_get_pix_fmt_name(best_format), best_vkfmt);
1103 }
1104
1105 *pix_fmt = best_format;
1106 *vk_fmt = best_vkfmt;
1107
1108 *dpb_dedicate = dec->dedicated_dpb;
1109
1110 return 0;
1111}
1112
1114{
1115 av_free(hwfc->user_opaque);
1116}
1117
1119{
1120 int err, dedicated_dpb;
1121 AVHWFramesContext *frames_ctx = (AVHWFramesContext*)hw_frames_ctx->data;
1122 AVVulkanFramesContext *hwfc = frames_ctx->hwctx;
1125
1126 err = vulkan_decode_bootstrap(avctx, hw_frames_ctx);
1127 if (err < 0)
1128 return err;
1129
1130 frames_ctx->format = AV_PIX_FMT_VULKAN;
1131 frames_ctx->sw_format = avctx->sw_pix_fmt;
1132 frames_ctx->width = avctx->coded_width;
1133 frames_ctx->height = avctx->coded_height;
1134
1135 if (!DECODER_IS_SDR(avctx->codec_id)) {
1136 prof = av_mallocz(sizeof(FFVulkanDecodeProfileData));
1137 if (!prof)
1138 return AVERROR(ENOMEM);
1139
1140 err = vulkan_decode_get_profile(avctx, hw_frames_ctx,
1141 &frames_ctx->sw_format,
1142 &hwfc->format[0],
1143 prof, &dedicated_dpb);
1144 if (err < 0) {
1145 av_free(prof);
1146 return err;
1147 }
1148
1149 const AVPixFmtDescriptor *pdesc = av_pix_fmt_desc_get(frames_ctx->sw_format);
1150 frames_ctx->width = FFALIGN(frames_ctx->width, 1 << pdesc->log2_chroma_w);
1151 frames_ctx->height = FFALIGN(frames_ctx->height, 1 << pdesc->log2_chroma_h);
1152 frames_ctx->user_opaque = prof;
1153 frames_ctx->free = free_profile_data;
1154
1155 hwfc->create_pnext = &prof->profile_list;
1156 } else {
1157 hwfc->format[0] = VK_FORMAT_UNDEFINED;
1158 switch (frames_ctx->sw_format) {
1159 case AV_PIX_FMT_GBRAP16:
1160 /* This should be more efficient for downloading and using */
1161 frames_ctx->sw_format = AV_PIX_FMT_RGBA64;
1162 break;
1163 case AV_PIX_FMT_GBRP10:
1164 /* This saves memory bandwidth when downloading */
1165 frames_ctx->sw_format = AV_PIX_FMT_X2BGR10;
1166 break;
1167 case AV_PIX_FMT_RGB24:
1168 case AV_PIX_FMT_BGR0:
1169 /* mpv has issues with bgr0 mapping, so just remap it */
1170 frames_ctx->sw_format = AV_PIX_FMT_RGB0;
1171 break;
1172 /* DPX endian mismatch remappings */
1173 case AV_PIX_FMT_RGB48LE:
1174 case AV_PIX_FMT_RGB48BE: frames_ctx->sw_format = AV_PIX_FMT_GBRP16; break;
1175 case AV_PIX_FMT_RGBA64BE: frames_ctx->sw_format = AV_PIX_FMT_RGBA64; break;
1176 case AV_PIX_FMT_GRAY16BE: frames_ctx->sw_format = AV_PIX_FMT_GRAY16; break;
1177 /* ProRes needs to clear the input image, which is not possible on YUV formats */
1182 hwfc->format[3] = VK_FORMAT_R16_UNORM;
1188 hwfc->format[0] = VK_FORMAT_R16_UNORM;
1189 hwfc->format[1] = VK_FORMAT_R16_UNORM;
1190 hwfc->format[2] = VK_FORMAT_R16_UNORM;
1191 break;
1192 default:
1193 break;
1194 }
1195 }
1196
1197 if (hwfc->tiling != VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT)
1198 hwfc->tiling = VK_IMAGE_TILING_OPTIMAL;
1199
1200 hwfc->usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
1201 VK_IMAGE_USAGE_STORAGE_BIT |
1202 VK_IMAGE_USAGE_SAMPLED_BIT;
1203
1204 if (prof) {
1206
1207 hwfc->usage |= VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR;
1208 if (!dec->dedicated_dpb)
1209 hwfc->usage |= VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR;
1210
1211 ctx = dec->shared_ctx;
1212 if ((ctx->s.extensions & FF_VK_EXT_VIDEO_ENCODE_QUEUE) &&
1213 (ctx->s.extensions & FF_VK_EXT_VIDEO_MAINTENANCE_1))
1214 hwfc->usage |= VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR;
1215 }
1216
1217 return err;
1218}
1219
1221{
1222 FFVulkanDecodeShared *ctx = opaque.nc;
1223 FFVulkanFunctions *vk = &ctx->s.vkfn;
1224 VkVideoSessionParametersKHR *par = obj;
1225 vk->DestroyVideoSessionParametersKHR(ctx->s.hwctx->act_dev, *par,
1226 ctx->s.hwctx->alloc);
1227}
1228
1229int ff_vk_decode_create_params(VkVideoSessionParametersKHR **par_ref, void *logctx, FFVulkanDecodeShared *ctx,
1230 const VkVideoSessionParametersCreateInfoKHR *session_params_create)
1231{
1232 VkVideoSessionParametersKHR *par;
1233 const FFVulkanFunctions *vk = &ctx->s.vkfn;
1234 VkResult ret;
1235
1236 par = av_refstruct_alloc_ext(sizeof(*par), 0, ctx, vk_decode_free_params);
1237 if (!par)
1238 return AVERROR(ENOMEM);
1239
1240 /* Create session parameters */
1241 ret = vk->CreateVideoSessionParametersKHR(ctx->s.hwctx->act_dev, session_params_create,
1242 ctx->s.hwctx->alloc, par);
1243 if (ret != VK_SUCCESS) {
1244 av_log(logctx, AV_LOG_ERROR, "Unable to create Vulkan video session parameters: %s!\n",
1245 ff_vk_ret2str(ret));
1246 *par = VK_NULL_HANDLE;
1247 av_refstruct_unref(&par);
1248 return AVERROR_EXTERNAL;
1249 }
1250 *par_ref = par;
1251
1252 return 0;
1253}
1254
1256{
1258
1259 av_freep(&dec->hevc_headers);
1262 av_freep(&dec->slice_off);
1263 return 0;
1264}
1265
1267{
1268 int err;
1272 int async_depth;
1273 const VkVideoProfileInfoKHR *profile;
1274 const FFVulkanDecodeDescriptor *vk_desc;
1275
1276 VkVideoSessionCreateInfoKHR session_create = {
1277 .sType = VK_STRUCTURE_TYPE_VIDEO_SESSION_CREATE_INFO_KHR,
1278 };
1279
1281 if (err < 0)
1282 return err;
1283
1284 /* Initialize contexts */
1285 ctx = dec->shared_ctx;
1286 s = &ctx->s;
1287
1288 err = ff_vk_init(s, avctx, NULL, avctx->hw_frames_ctx);
1289 if (err < 0)
1290 return err;
1291
1292 vk_desc = get_codecdesc(avctx->codec_id);
1293
1295 if ((vk_desc->queue_flags & VK_QUEUE_VIDEO_DECODE_BIT_KHR) && !profile) {
1296 av_log(avctx, AV_LOG_ERROR, "Video profile missing from frames context!");
1297 return AVERROR(EINVAL);
1298 }
1299
1300 /* Create queue context */
1301 vk_desc = get_codecdesc(avctx->codec_id);
1302 ctx->qf = ff_vk_qf_find(s, vk_desc->queue_flags, vk_desc->decode_op);
1303 if (!ctx->qf) {
1304 av_log(avctx, AV_LOG_ERROR, "Decoding of %s is not supported by this device\n",
1305 avcodec_get_name(avctx->codec_id));
1306 return err;
1307 }
1308
1309 session_create.queueFamilyIndex = ctx->qf->idx;
1310 session_create.maxCodedExtent = ctx->caps.maxCodedExtent;
1311 session_create.maxDpbSlots = ctx->caps.maxDpbSlots;
1312 session_create.maxActiveReferencePictures = ctx->caps.maxActiveReferencePictures;
1313 session_create.pictureFormat = s->hwfc->format[0];
1314 session_create.referencePictureFormat = session_create.pictureFormat;
1315 session_create.pStdHeaderVersion = &vk_desc->ext_props;
1316 session_create.pVideoProfile = profile;
1317#ifdef VK_KHR_video_maintenance2
1318 if (ctx->s.extensions & FF_VK_EXT_VIDEO_MAINTENANCE_2)
1319 session_create.flags = VK_VIDEO_SESSION_CREATE_INLINE_SESSION_PARAMETERS_BIT_KHR;
1320#endif
1321
1322 /* Create the decode exec context.
1323 * A small fixed depth is enough to keep the GPU fed, as submissions are
1324 * serialized for hardware decoding; thread-safe hwaccels submit from
1325 * every frame thread concurrently, and need a context per thread. */
1326 async_depth = FF_VK_DEFAULT_EXEC_CONTEXTS;
1328 async_depth = FFMAX(async_depth, avctx->thread_count);
1329
1330 err = ff_vk_exec_pool_init(s, ctx->qf, &ctx->exec_pool,
1331 async_depth, 0, 0, 0, profile);
1332 if (err < 0)
1333 goto fail;
1334
1335 if (!DECODER_IS_SDR(avctx->codec_id)) {
1336 err = ff_vk_video_common_init(avctx, s, &ctx->common, &session_create);
1337 if (err < 0)
1338 goto fail;
1339 }
1340
1341 /* Out-of-place decoding: create the internal, queue-exclusive DPB pool */
1342 if (dec->dedicated_dpb || avctx->codec_id == AV_CODEC_ID_AV1) {
1343 VkImageTiling tiling = VK_IMAGE_TILING_OPTIMAL;
1344 void *create_pnext = (void *)ff_vk_find_struct(ctx->s.hwfc->create_pnext,
1345 VK_STRUCTURE_TYPE_VIDEO_PROFILE_LIST_INFO_KHR);
1346 /* Reference (DPB) images use the same tiling and pNext chain as output.
1347 * If VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_DISTINCT_BIT_KHR is 0, the
1348 * driver does not support separate output and DPB with different layouts/tiling. */
1349 void *drm_create_pnext = (void *)ff_vk_find_struct(ctx->s.hwfc->create_pnext,
1350 VK_STRUCTURE_TYPE_IMAGE_DRM_FORMAT_MODIFIER_LIST_CREATE_INFO_EXT);
1351 if (drm_create_pnext) {
1352 create_pnext = drm_create_pnext;
1353 tiling = VK_IMAGE_TILING_DRM_FORMAT_MODIFIER_EXT;
1354 av_assert2(ff_vk_find_struct(drm_create_pnext, VK_STRUCTURE_TYPE_VIDEO_PROFILE_LIST_INFO_KHR));
1355 }
1356
1357 err = ff_vk_video_dpb_init(s, &ctx->common, s->hwfc->format[0],
1358 VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR,
1359 tiling, create_pnext,
1360 s->frames->width, s->frames->height,
1361 ctx->common.layered_dpb ?
1362 ctx->caps.maxDpbSlots : 1);
1363 if (err < 0)
1364 goto fail;
1365
1366 if (ctx->common.layered_dpb) {
1367 FFVulkanFunctions *vk = &ctx->s.vkfn;
1368 FFVkExecContext *exec;
1369 VkImageMemoryBarrier2 img_bar;
1370
1371 ctx->common.layered_img = av_refstruct_pool_get(ctx->common.dpb->img_pool);
1372 if (!ctx->common.layered_img) {
1373 err = AVERROR(ENOMEM);
1374 goto fail;
1375 }
1376
1377 ctx->common.layered_view = ctx->common.layered_img->view;
1378 ctx->common.layered_aspect = ctx->common.layered_img->aspect;
1379
1380 /* Created eagerly, so transitioned eagerly; per-picture images
1381 * transition on first use instead */
1382 exec = ff_vk_exec_get(&ctx->s, &ctx->exec_pool);
1383 err = ff_vk_exec_start(&ctx->s, exec);
1384 if (err < 0)
1385 goto fail;
1386
1387 img_bar = (VkImageMemoryBarrier2) {
1388 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
1389 .srcStageMask = VK_PIPELINE_STAGE_2_NONE,
1390 .dstStageMask = VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
1391 .srcAccessMask = VK_ACCESS_2_NONE,
1392 .dstAccessMask = VK_ACCESS_2_VIDEO_DECODE_READ_BIT_KHR |
1393 VK_ACCESS_2_VIDEO_DECODE_WRITE_BIT_KHR,
1394 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1395 .newLayout = VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR,
1396 .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
1397 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
1398 .image = ctx->common.layered_img->img,
1399 .subresourceRange = (VkImageSubresourceRange) {
1400 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1401 .layerCount = VK_REMAINING_ARRAY_LAYERS,
1402 .levelCount = 1,
1403 },
1404 };
1405 vk->CmdPipelineBarrier2(exec->buf, &(VkDependencyInfo) {
1406 .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
1407 .pImageMemoryBarriers = &img_bar,
1408 .imageMemoryBarrierCount = 1,
1409 });
1410
1411 err = ff_vk_exec_submit(&ctx->s, exec);
1412 if (err < 0)
1413 goto fail;
1414
1415 ctx->common.layered_img->layout = VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR;
1416 }
1417 }
1418
1419 if (!DECODER_IS_SDR(avctx->codec_id)) {
1420 err = decode_reset(avctx, ctx);
1421 if (err < 0)
1422 return err;
1423 } else {
1424 /* For SDR decoders, this alignment value will be 0. Since this will make
1425 * add_slice() malfunction, set it to a sane default value. */
1426 ctx->caps.minBitstreamBufferSizeAlignment = AV_INPUT_BUFFER_PADDING_SIZE;
1427 }
1428
1429 const VkPhysicalDeviceDriverProperties *driver_props;
1430 driver_props = &dec->shared_ctx->s.driver_props;
1431 if (driver_props->driverID == VK_DRIVER_ID_NVIDIA_PROPRIETARY &&
1432 driver_props->conformanceVersion.major == 1 &&
1433 driver_props->conformanceVersion.minor == 3 &&
1434 driver_props->conformanceVersion.subminor == 8 &&
1435 driver_props->conformanceVersion.patch < 3)
1436 dec->quirk_av1_offset = 1;
1437
1438 av_log(avctx, AV_LOG_VERBOSE, "Vulkan decoder initialization successful\n");
1439
1440 return 0;
1441
1442fail:
1443 ff_vk_decode_uninit(avctx);
1444
1445 return err;
1446}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
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:1073
#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
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:2013
#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:2033
#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:64
#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
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:980
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:649
void ff_vk_uninit(FFVulkanContext *s)
Frees main context.
Definition vulkan.c:2779
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:2795
int ff_vk_exec_start(FFVulkanContext *s, FFVkExecContext *e)
Start/submit/wait an execution.
Definition vulkan.c:664
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:1978
void ff_vk_exec_update_frame(FFVulkanContext *s, FFVkExecContext *e, AVFrame *f, VkImageMemoryBarrier2 *bar, uint32_t *nb_img_bar)
Definition vulkan.c:943
FFVkExecContext * ff_vk_exec_get(FFVulkanContext *s, FFVkExecPool *pool)
Retrieve an execution pool.
Definition vulkan.c:626
int ff_vk_exec_submit(FFVulkanContext *s, FFVkExecContext *e)
Definition vulkan.c:987
void ff_vk_exec_move_dep_refstruct(FFVulkanContext *s, FFVkExecContext *e, void *obj)
Definition vulkan.c:790
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:783
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:1444
void ff_vk_exec_discard(FFVulkanContext *s, FFVkExecContext *e)
Definition vulkan.c:767
int ff_vk_exec_add_dep_frame(FFVulkanContext *s, FFVkExecContext *e, AVFrame *f, VkPipelineStageFlagBits2 wait_stage, VkPipelineStageFlagBits2 signal_stage)
Definition vulkan.c:885
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:2299
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:3380
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:3739
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:552
#define AV_PIX_FMT_GBRAP16
Definition pixfmt.h:571
#define AV_PIX_FMT_YUVA444P10
Definition pixfmt.h:598
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:550
#define AV_PIX_FMT_GBRP10
Definition pixfmt.h:564
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
#define AV_PIX_FMT_RGBA64
Definition pixfmt.h:535
#define AV_PIX_FMT_YUVA422P10
Definition pixfmt.h:597
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:599
#define AV_PIX_FMT_X2BGR10
Definition pixfmt.h:620
#define AV_PIX_FMT_GRAY16
Definition pixfmt.h:528
#define AV_PIX_FMT_GBRP16
Definition pixfmt.h:567
#define AV_PIX_FMT_YUVA444P12
Definition pixfmt.h:600
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
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:1503
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:650
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
Definition avcodec.h:1472
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition avcodec.h:1424
int level
Encoding level descriptor.
Definition avcodec.h:1651
int profile
profile
Definition avcodec.h:1641
int export_side_data
Bit set of AV_CODEC_EXPORT_DATA_* flags, which affects the kind of metadata exported in frame,...
Definition avcodec.h:1784
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition avcodec.h:1584
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
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:619
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:776
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:563
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 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)
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 VkResult vulkan_setup_profile(AVCodecContext *avctx, FFVulkanDecodeProfileData *prof, AVVulkanDeviceContext *hwctx, FFVulkanFunctions *vk, const FFVulkanDecodeDescriptor *vk_desc, VkVideoDecodeH264CapabilitiesKHR *h264_caps, VkVideoDecodeH265CapabilitiesKHR *h265_caps, VkVideoDecodeAV1CapabilitiesKHR *av1_caps, VkVideoCapabilitiesKHR *caps, VkVideoDecodeCapabilitiesKHR *dec_caps, int cur_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