22 #include "config_components.h"
27 #if CONFIG_H264_VULKAN_HWACCEL
30 #if CONFIG_HEVC_VULKAN_HWACCEL
33 #if CONFIG_AV1_VULKAN_HWACCEL
38 #if CONFIG_H264_VULKAN_HWACCEL
41 #if CONFIG_HEVC_VULKAN_HWACCEL
44 #if CONFIG_AV1_VULKAN_HWACCEL
60 const VkVideoProfileListInfoKHR *profile_list;
62 VkStructureType profile_struct_type =
69 VK_STRUCTURE_TYPE_VIDEO_PROFILE_LIST_INFO_KHR);
73 for (
int i = 0;
i < profile_list->profileCount;
i++)
75 return &profile_list->pProfiles[
i];
145 if (
ctx->common.layered_dpb && alloc_dpb) {
148 }
else if (alloc_dpb) {
159 dpb_hwfc->format[0], !is_current);
166 if (!alloc_dpb || is_current) {
173 hwfc->format[0], !is_current);
187 const uint8_t *
data,
size_t size,
int add_startcode,
188 uint32_t *nb_slices,
const uint32_t **
offsets)
193 static const uint8_t startcode_prefix[3] = { 0x0, 0x0, 0x1 };
194 const size_t startcode_len = add_startcode ?
sizeof(startcode_prefix) : 0;
195 const int nb = *nb_slices;
201 ctx->caps.minBitstreamBufferSizeAlignment;
202 new_size =
FFALIGN(new_size,
ctx->caps.minBitstreamBufferSizeAlignment);
205 (nb + 1)*
sizeof(slice_off));
213 if (!vkbuf || vkbuf->
size < new_size) {
219 size_t buf_size =
FFMAX(new_size, 1024*1024);
223 buf_size = 2 <<
av_log2(buf_size);
226 VK_BUFFER_USAGE_VIDEO_DECODE_SRC_BIT_KHR,
227 ctx->s.hwfc->create_pnext, buf_size,
228 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
246 memcpy(slices + vp->
slices_size, startcode_prefix, startcode_len);
263 VkVideoBeginCodingInfoKHR decode_start = {
264 .sType = VK_STRUCTURE_TYPE_VIDEO_BEGIN_CODING_INFO_KHR,
265 .videoSession =
ctx->common.session,
266 .videoSessionParameters =
ctx->empty_session_params,
268 VkVideoCodingControlInfoKHR decode_ctrl = {
269 .sType = VK_STRUCTURE_TYPE_VIDEO_CODING_CONTROL_INFO_KHR,
270 .
flags = VK_VIDEO_CODING_CONTROL_RESET_BIT_KHR,
273 .sType = VK_STRUCTURE_TYPE_VIDEO_END_CODING_INFO_KHR,
276 VkCommandBuffer cmd_buf;
283 vk->CmdBeginVideoCodingKHR(cmd_buf, &decode_start);
284 vk->CmdControlVideoCodingKHR(cmd_buf, &decode_ctrl);
285 vk->CmdEndVideoCodingKHR(cmd_buf, &
decode_end);
295 VkCommandBuffer cmd_buf;
306 const int layered_dpb =
ctx->common.layered_dpb;
308 VkVideoSessionParametersKHR *par = (VkVideoSessionParametersKHR *)dec->
session_params->
data;
309 VkVideoBeginCodingInfoKHR decode_start = {
310 .sType = VK_STRUCTURE_TYPE_VIDEO_BEGIN_CODING_INFO_KHR,
311 .videoSession =
ctx->common.session,
312 .videoSessionParameters = *par,
313 .referenceSlotCount = vp->
decode_info.referenceSlotCount,
314 .pReferenceSlots = vp->
decode_info.pReferenceSlots,
317 .sType = VK_STRUCTURE_TYPE_VIDEO_END_CODING_INFO_KHR,
320 VkImageMemoryBarrier2 img_bar[37];
323 ctx->caps.minBitstreamBufferSizeAlignment);
328 VkVideoReferenceSlotInfoKHR *cur_vk_ref;
329 cur_vk_ref = (
void *)&decode_start.pReferenceSlots[decode_start.referenceSlotCount];
331 cur_vk_ref[0].slotIndex = -1;
332 decode_start.referenceSlotCount++;
337 if (!(sd_buf->
flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) {
338 VkMappedMemoryRange flush_buf = {
339 .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
340 .memory = sd_buf->
mem,
343 ctx->s.props.properties.limits.nonCoherentAtomSize),
346 ret = vk->FlushMappedMemoryRanges(
ctx->s.hwctx->act_dev, 1, &flush_buf);
347 if (
ret != VK_SUCCESS) {
376 VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
377 VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR);
387 img_bar[nb_img_bar] = (VkImageMemoryBarrier2) {
388 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
390 .srcStageMask = VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
391 .dstStageMask = VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
392 .srcAccessMask = VK_ACCESS_2_NONE,
393 .dstAccessMask = VK_ACCESS_2_VIDEO_DECODE_WRITE_BIT_KHR,
394 .oldLayout = vkf->
layout[0],
395 .newLayout = (layered_dpb || vp->
dpb_frame) ?
396 VK_IMAGE_LAYOUT_VIDEO_DECODE_DST_KHR :
397 VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR,
399 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
400 .image = vkf->
img[0],
401 .subresourceRange = (VkImageSubresourceRange) {
408 &img_bar[nb_img_bar], &nb_img_bar);
413 VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
414 VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR);
428 VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
429 VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR);
444 img_bar[nb_img_bar] = (VkImageMemoryBarrier2) {
445 .sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2,
447 .srcStageMask = VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
448 .dstStageMask = VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
449 .srcAccessMask = VK_ACCESS_2_NONE,
450 .dstAccessMask = VK_ACCESS_2_VIDEO_DECODE_READ_BIT_KHR |
451 VK_ACCESS_2_VIDEO_DECODE_WRITE_BIT_KHR,
452 .oldLayout = rvkf->
layout[0],
453 .newLayout = VK_IMAGE_LAYOUT_VIDEO_DECODE_DPB_KHR,
455 .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
456 .image = rvkf->
img[0],
457 .subresourceRange = (VkImageSubresourceRange) {
464 &img_bar[nb_img_bar], &nb_img_bar);
471 VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR,
472 VK_PIPELINE_STAGE_2_VIDEO_DECODE_BIT_KHR);
478 vk->CmdPipelineBarrier2(cmd_buf, &(VkDependencyInfo) {
479 .sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO,
480 .dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT,
481 .pImageMemoryBarriers = img_bar,
482 .imageMemoryBarrierCount = nb_img_bar,
486 vk->CmdBeginVideoCodingKHR(cmd_buf, &decode_start);
488 vk->CmdEndVideoCodingKHR(cmd_buf, &
decode_end);
498 VkSemaphoreWaitInfo
sem_wait = (VkSemaphoreWaitInfo) {
499 .sType = VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO,
500 .pSemaphores = &vp->
sem,
538 if (
ctx->empty_session_params)
539 vk->DestroyVideoSessionParametersKHR(
s->hwctx->act_dev,
540 ctx->empty_session_params,
574 VK_KHR_VIDEO_DECODE_QUEUE_EXTENSION_NAME);
593 VkVideoDecodeH264CapabilitiesKHR *h264_caps,
594 VkVideoDecodeH265CapabilitiesKHR *h265_caps,
595 VkVideoDecodeAV1CapabilitiesKHR *av1_caps,
596 VkVideoCapabilitiesKHR *caps,
597 VkVideoDecodeCapabilitiesKHR *dec_caps,
600 VkVideoDecodeUsageInfoKHR *
usage = &prof->
usage;
602 VkVideoProfileListInfoKHR *profile_list = &prof->
profile_list;
604 VkVideoDecodeH264ProfileInfoKHR *h264_profile = &prof->
h264_profile;
605 VkVideoDecodeH265ProfileInfoKHR *h265_profile = &prof->
h265_profile;
606 VkVideoDecodeAV1ProfileInfoKHR *av1_profile = &prof->
av1_profile;
613 dec_caps->pNext = h264_caps;
614 usage->pNext = h264_profile;
615 h264_profile->sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H264_PROFILE_INFO_KHR;
624 VK_VIDEO_DECODE_H264_PICTURE_LAYOUT_PROGRESSIVE_KHR :
625 VK_VIDEO_DECODE_H264_PICTURE_LAYOUT_INTERLACED_INTERLEAVED_LINES_BIT_KHR;
627 dec_caps->pNext = h265_caps;
628 usage->pNext = h265_profile;
629 h265_profile->sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H265_PROFILE_INFO_KHR;
630 h265_profile->stdProfileIdc = cur_profile;
632 dec_caps->pNext = av1_caps;
633 usage->pNext = av1_profile;
634 av1_profile->sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_AV1_PROFILE_INFO_KHR;
635 av1_profile->stdProfile = cur_profile;
639 usage->sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_USAGE_INFO_KHR;
640 usage->videoUsageHints = VK_VIDEO_DECODE_USAGE_DEFAULT_KHR;
642 profile->sType = VK_STRUCTURE_TYPE_VIDEO_PROFILE_INFO_KHR;
649 profile_list->sType = VK_STRUCTURE_TYPE_VIDEO_PROFILE_LIST_INFO_KHR;
650 profile_list->profileCount = 1;
651 profile_list->pProfiles =
profile;
654 caps->sType = VK_STRUCTURE_TYPE_VIDEO_CAPABILITIES_KHR;
655 caps->pNext = dec_caps;
656 dec_caps->sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_CAPABILITIES_KHR;
659 return vk->GetPhysicalDeviceVideoCapabilitiesKHR(hwctx->
phys_dev,
profile,
669 int max_level, base_profile, cur_profile;
682 VkVideoCapabilitiesKHR *caps = &
ctx->caps;
683 VkVideoDecodeCapabilitiesKHR *dec_caps = &
ctx->dec_caps;
685 VkVideoDecodeH264CapabilitiesKHR h264_caps = {
686 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H264_CAPABILITIES_KHR,
688 VkVideoDecodeH265CapabilitiesKHR h265_caps = {
689 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H265_CAPABILITIES_KHR,
691 VkVideoDecodeAV1CapabilitiesKHR av1_caps = {
692 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_AV1_CAPABILITIES_KHR,
695 VkPhysicalDeviceVideoFormatInfoKHR fmt_info = {
696 .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VIDEO_FORMAT_INFO_KHR,
699 VkVideoFormatPropertiesKHR *ret_info;
700 uint32_t nb_out_fmts = 0;
721 if (
ret == VK_ERROR_VIDEO_PROFILE_OPERATION_NOT_SUPPORTED_KHR &&
723 avctx->
profile != base_profile) {
725 "again with profile %s\n",
729 cur_profile = base_profile;
739 if (
ret == VK_ERROR_VIDEO_PROFILE_OPERATION_NOT_SUPPORTED_KHR) {
741 "%s profile \"%s\" not supported!\n",
745 }
else if (
ret == VK_ERROR_VIDEO_PROFILE_FORMAT_NOT_SUPPORTED_KHR) {
747 "format (%s) not supported!\n",
750 }
else if (
ret == VK_ERROR_FEATURE_NOT_PRESENT ||
751 ret == VK_ERROR_FORMAT_NOT_SUPPORTED) {
753 }
else if (
ret != VK_SUCCESS) {
766 max_level, avctx->
level);
768 caps->minCodedExtent.width, caps->maxCodedExtent.width);
770 caps->minCodedExtent.height, caps->maxCodedExtent.height);
772 caps->pictureAccessGranularity.width);
774 caps->pictureAccessGranularity.height);
776 caps->minBitstreamBufferOffsetAlignment);
778 caps->minBitstreamBufferSizeAlignment);
782 caps->maxActiveReferencePictures);
784 caps->stdHeaderVersion.extensionName,
786 av_log(avctx,
AV_LOG_VERBOSE,
" Codec header version: %i.%i.%i (driver), %i.%i.%i (compiled)\n",
787 CODEC_VER(caps->stdHeaderVersion.specVersion),
790 dec_caps->flags ?
"" :
792 dec_caps->flags & VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_COINCIDE_BIT_KHR ?
793 " reuse_dst_dpb" :
"",
794 dec_caps->flags & VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_DISTINCT_BIT_KHR ?
795 " dedicated_dpb" :
"");
799 caps->flags & VK_VIDEO_CAPABILITY_PROTECTED_CONTENT_BIT_KHR ?
801 caps->flags & VK_VIDEO_CAPABILITY_SEPARATE_REFERENCE_IMAGES_BIT_KHR ?
802 " separate_references" :
"");
805 if (avctx->
coded_width < caps->minCodedExtent.width ||
812 avctx->
level > max_level)
816 if (!(dec_caps->flags & (VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_COINCIDE_BIT_KHR |
817 VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_DISTINCT_BIT_KHR))) {
819 "VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_COINCIDE_BIT_KHR nor "
820 "VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_DISTINCT_BIT_KHR are set!\n");
822 }
else if ((dec_caps->flags & (VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_COINCIDE_BIT_KHR |
823 VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_DISTINCT_BIT_KHR) ==
824 VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_COINCIDE_BIT_KHR) &&
825 !(caps->flags & VK_VIDEO_CAPABILITY_SEPARATE_REFERENCE_IMAGES_BIT_KHR)) {
826 av_log(avctx,
AV_LOG_ERROR,
"Cannot initialize Vulkan decoding session, buggy driver: "
827 "VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_COINCIDE_BIT_KHR set "
828 "but VK_VIDEO_CAPABILITY_SEPARATE_REFERENCE_IMAGES_BIT_KHR is unset!\n");
832 dec->
dedicated_dpb = !(dec_caps->flags & VK_VIDEO_DECODE_CAPABILITY_DPB_AND_OUTPUT_COINCIDE_BIT_KHR);
834 !(caps->flags & VK_VIDEO_CAPABILITY_SEPARATE_REFERENCE_IMAGES_BIT_KHR);
837 fmt_info.imageUsage = VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR;
839 fmt_info.imageUsage = VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR |
840 VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR |
841 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
842 VK_IMAGE_USAGE_SAMPLED_BIT;
846 fmt_info.imageUsage |= VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR;
850 ret = vk->GetPhysicalDeviceVideoFormatPropertiesKHR(hwctx->
phys_dev,
853 if (
ret == VK_ERROR_FORMAT_NOT_SUPPORTED ||
854 (!nb_out_fmts &&
ret == VK_SUCCESS)) {
856 }
else if (
ret != VK_SUCCESS) {
862 ret_info =
av_mallocz(
sizeof(*ret_info)*nb_out_fmts);
866 for (
int i = 0;
i < nb_out_fmts;
i++)
867 ret_info[
i].sType = VK_STRUCTURE_TYPE_VIDEO_FORMAT_PROPERTIES_KHR;
869 ret = vk->GetPhysicalDeviceVideoFormatPropertiesKHR(hwctx->
phys_dev,
871 &nb_out_fmts, ret_info);
872 if (
ret == VK_ERROR_FORMAT_NOT_SUPPORTED ||
873 (!nb_out_fmts &&
ret == VK_SUCCESS)) {
876 }
else if (
ret != VK_SUCCESS) {
885 *vk_fmt = best_vkfmt = VK_FORMAT_UNDEFINED;
888 av_log(avctx,
AV_LOG_DEBUG,
"Choosing best pixel format for decoding from %i:\n", nb_out_fmts);
889 for (
int i = 0;
i < nb_out_fmts;
i++) {
897 if (
tmp == best_format)
898 best_vkfmt = ret_info[
i].format;
908 av_log(avctx,
AV_LOG_ERROR,
"No valid/compatible pixel format found for decoding!\n");
916 *vk_fmt = best_vkfmt;
931 int err, dedicated_dpb;
950 prof, &dedicated_dpb);
963 hwfc->format[0] = vkfmt;
964 hwfc->create_pnext = &prof->profile_list;
965 hwfc->tiling = VK_IMAGE_TILING_OPTIMAL;
966 hwfc->usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
967 VK_IMAGE_USAGE_SAMPLED_BIT |
968 VK_IMAGE_USAGE_VIDEO_DECODE_DST_BIT_KHR;
970 if (!dec->dedicated_dpb)
971 hwfc->usage |= VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR;
973 ctx = dec->shared_ctx;
976 hwfc->usage |= VK_IMAGE_USAGE_VIDEO_ENCODE_SRC_BIT_KHR;
985 VkVideoSessionParametersKHR *par = (VkVideoSessionParametersKHR *)
data;
986 vk->DestroyVideoSessionParametersKHR(
ctx->s.hwctx->act_dev, *par,
987 ctx->s.hwctx->alloc);
992 const VkVideoSessionParametersCreateInfoKHR *session_params_create)
994 VkVideoSessionParametersKHR *par =
av_malloc(
sizeof(*par));
1002 ret = vk->CreateVideoSessionParametersKHR(
ctx->s.hwctx->act_dev, session_params_create,
1003 ctx->s.hwctx->alloc, par);
1004 if (
ret != VK_SUCCESS) {
1005 av_log(logctx,
AV_LOG_ERROR,
"Unable to create Vulkan video session parameters: %s!\n",
1039 const VkVideoProfileInfoKHR *
profile;
1041 const VkPhysicalDeviceDriverProperties *driver_props;
1043 VkVideoDecodeH264SessionParametersCreateInfoKHR h264_params = {
1044 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H264_SESSION_PARAMETERS_CREATE_INFO_KHR,
1046 VkVideoDecodeH265SessionParametersCreateInfoKHR h265_params = {
1047 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_H265_SESSION_PARAMETERS_CREATE_INFO_KHR,
1049 StdVideoAV1SequenceHeader av1_empty_seq = { 0 };
1050 VkVideoDecodeAV1SessionParametersCreateInfoKHR av1_params = {
1051 .sType = VK_STRUCTURE_TYPE_VIDEO_DECODE_AV1_SESSION_PARAMETERS_CREATE_INFO_KHR,
1052 .pStdSequenceHeader = &av1_empty_seq,
1054 VkVideoSessionParametersCreateInfoKHR session_params_create = {
1055 .sType = VK_STRUCTURE_TYPE_VIDEO_SESSION_PARAMETERS_CREATE_INFO_KHR,
1061 VkVideoSessionCreateInfoKHR session_create = {
1062 .sType = VK_STRUCTURE_TYPE_VIDEO_SESSION_CREATE_INFO_KHR,
1093 session_create.flags = 0x0;
1094 session_create.queueFamilyIndex =
ctx->qf->idx;
1095 session_create.maxCodedExtent =
ctx->caps.maxCodedExtent;
1096 session_create.maxDpbSlots =
ctx->caps.maxDpbSlots;
1097 session_create.maxActiveReferencePictures =
ctx->caps.maxActiveReferencePictures;
1098 session_create.pictureFormat =
s->hwfc->format[0];
1099 session_create.referencePictureFormat = session_create.pictureFormat;
1100 session_create.pStdHeaderVersion = &vk_desc->
ext_props;
1101 session_create.pVideoProfile =
profile;
1122 if (!
ctx->common.dpb_hwfc_ref) {
1135 VK_STRUCTURE_TYPE_VIDEO_PROFILE_LIST_INFO_KHR);
1136 dpb_hwfc->
format[0] =
s->hwfc->format[0];
1137 dpb_hwfc->
tiling = VK_IMAGE_TILING_OPTIMAL;
1138 dpb_hwfc->
usage = VK_IMAGE_USAGE_VIDEO_DECODE_DPB_BIT_KHR |
1139 VK_IMAGE_USAGE_SAMPLED_BIT;
1141 if (
ctx->common.layered_dpb)
1148 if (
ctx->common.layered_dpb) {
1150 if (!
ctx->common.layered_frame) {
1156 &
ctx->common.layered_view,
1157 &
ctx->common.layered_aspect,
1159 s->hwfc->format[0], 1);
1165 session_params_create.videoSession =
ctx->common.session;
1166 ret = vk->CreateVideoSessionParametersKHR(
s->hwctx->act_dev, &session_params_create,
1167 s->hwctx->alloc, &
ctx->empty_session_params);
1168 if (
ret != VK_SUCCESS) {
1169 av_log(avctx,
AV_LOG_ERROR,
"Unable to create empty Vulkan video session parameters: %s!\n",
1175 if (driver_props->driverID == VK_DRIVER_ID_NVIDIA_PROPRIETARY &&
1176 driver_props->conformanceVersion.major == 1 &&
1177 driver_props->conformanceVersion.minor == 3 &&
1178 driver_props->conformanceVersion.subminor == 8 &&
1179 driver_props->conformanceVersion.patch < 3)