1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
|
/* Copyright (c) 2022-2024 The Khronos Group Inc.
* Copyright (c) 2022-2024 RasterGrid Kft.
* Modifications Copyright (C) 2024 Advanced Micro Devices, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "state_tracker/state_object.h"
#include "utils/hash_util.h"
#include <vulkan/utility/vk_safe_struct.hpp>
#include <memory>
#include <mutex>
#include <vector>
#include <functional>
namespace vvl {
class DeviceState;
class DeviceProxy;
class Image;
class ImageView;
class VideoSession;
struct QuantizationMapTexelSize {
struct compare {
bool operator()(VkExtent2D const &lhs, VkExtent2D const &rhs) const {
return lhs.width == rhs.width && lhs.height == rhs.height;
}
};
struct hash {
std::size_t operator()(VkExtent2D const &texel_size) const {
hash_util::HashCombiner hc;
hc << texel_size.width << texel_size.height;
return hc.Value();
}
};
};
using SupportedQuantizationMapTexelSizes =
unordered_set<VkExtent2D, QuantizationMapTexelSize::hash, QuantizationMapTexelSize::compare>;
using SupportedVideoProfiles = unordered_set<std::shared_ptr<const class VideoProfileDesc>>;
// The VideoProfileDesc contains the entire video profile description, which includes all
// parameters specified in VkVideoProfileInfoKHR and its pNext chain. This includes any
// parameters specific to the coding operation type (e.g. decode/encode usage hints/modes)
// and the codec. Accordingly, hashing and comparison takes into consideration all the
// relevant parameters.
class VideoProfileDesc : public std::enable_shared_from_this<VideoProfileDesc> {
public:
struct Profile {
bool valid;
bool is_decode;
bool is_encode;
VkVideoProfileInfoKHR base;
union {
VkVideoDecodeUsageInfoKHR decode_usage;
VkVideoEncodeUsageInfoKHR encode_usage;
};
union {
VkVideoDecodeH264ProfileInfoKHR decode_h264;
VkVideoDecodeH265ProfileInfoKHR decode_h265;
VkVideoDecodeAV1ProfileInfoKHR decode_av1;
VkVideoDecodeVP9ProfileInfoKHR decode_vp9;
VkVideoEncodeH264ProfileInfoKHR encode_h264;
VkVideoEncodeH265ProfileInfoKHR encode_h265;
VkVideoEncodeAV1ProfileInfoKHR encode_av1;
};
};
struct Capabilities {
bool supported;
VkVideoCapabilitiesKHR base;
union {
VkVideoDecodeCapabilitiesKHR decode;
VkVideoEncodeCapabilitiesKHR encode;
};
union {
VkVideoDecodeH264CapabilitiesKHR decode_h264;
VkVideoDecodeH265CapabilitiesKHR decode_h265;
VkVideoDecodeAV1CapabilitiesKHR decode_av1;
VkVideoDecodeVP9CapabilitiesKHR decode_vp9;
VkVideoEncodeH264CapabilitiesKHR encode_h264;
VkVideoEncodeH265CapabilitiesKHR encode_h265;
VkVideoEncodeAV1CapabilitiesKHR encode_av1;
};
union {
struct {
} decode_ext;
struct {
VkVideoEncodeQuantizationMapCapabilitiesKHR quantization_map;
VkVideoEncodeIntraRefreshCapabilitiesKHR intra_refresh;
} encode_ext;
};
};
VideoProfileDesc(VkPhysicalDevice physical_device, VkVideoProfileInfoKHR const *profile);
~VideoProfileDesc();
const Profile &GetProfile() const { return profile_; }
const Capabilities &GetCapabilities() const { return capabilities_; }
const SupportedQuantizationMapTexelSizes &GetSupportedQuantDeltaMapTexelSizes() const { return quant_delta_map_texel_sizes_; }
const SupportedQuantizationMapTexelSizes &GetSupportedEmphasisMapTexelSizes() const { return emphasis_map_texel_sizes_; }
bool IsDecode() const { return profile_.is_decode; }
bool IsEncode() const { return profile_.is_encode; }
VkVideoCodecOperationFlagBitsKHR GetCodecOp() const { return profile_.base.videoCodecOperation; }
VkVideoDecodeH264PictureLayoutFlagBitsKHR GetH264PictureLayout() const { return profile_.decode_h264.pictureLayout; }
bool HasAV1FilmGrainSupport() const { return profile_.decode_av1.filmGrainSupport; }
VkExtent2D GetMaxCodingBlockSize() const {
switch (profile_.base.videoCodecOperation) {
case VK_VIDEO_CODEC_OPERATION_ENCODE_H264_BIT_KHR: {
const uint32_t mb_size = 16;
return {mb_size, mb_size};
}
case VK_VIDEO_CODEC_OPERATION_ENCODE_H265_BIT_KHR: {
uint32_t max_ctb_size = 16 * static_cast<uint32_t>(log2(capabilities_.encode_h265.ctbSizes));
return {max_ctb_size, max_ctb_size};
}
case VK_VIDEO_CODEC_OPERATION_ENCODE_AV1_BIT_KHR: {
uint32_t max_sb_size = 128 * static_cast<uint32_t>(log2(capabilities_.encode_av1.superblockSizes));
return {max_sb_size, max_sb_size};
}
default:
assert(false);
return {0, 0};
}
}
struct compare {
public:
bool operator()(VideoProfileDesc const *lhs, VideoProfileDesc const *rhs) const {
bool match = lhs->profile_.base.videoCodecOperation == rhs->profile_.base.videoCodecOperation &&
lhs->profile_.base.chromaSubsampling == rhs->profile_.base.chromaSubsampling &&
lhs->profile_.base.lumaBitDepth == rhs->profile_.base.lumaBitDepth &&
lhs->profile_.base.chromaBitDepth == rhs->profile_.base.chromaBitDepth;
if (match && lhs->profile_.is_decode) {
match = match && lhs->profile_.decode_usage.videoUsageHints == rhs->profile_.decode_usage.videoUsageHints;
}
if (match && lhs->profile_.is_encode) {
match = match && lhs->profile_.encode_usage.videoUsageHints == rhs->profile_.encode_usage.videoUsageHints &&
lhs->profile_.encode_usage.videoContentHints == rhs->profile_.encode_usage.videoContentHints &&
lhs->profile_.encode_usage.tuningMode == rhs->profile_.encode_usage.tuningMode;
}
if (match) {
switch (lhs->profile_.base.videoCodecOperation) {
case VK_VIDEO_CODEC_OPERATION_DECODE_H264_BIT_KHR:
match = match && lhs->profile_.decode_h264.stdProfileIdc == rhs->profile_.decode_h264.stdProfileIdc &&
lhs->profile_.decode_h264.pictureLayout == rhs->profile_.decode_h264.pictureLayout;
break;
case VK_VIDEO_CODEC_OPERATION_DECODE_H265_BIT_KHR:
match = match && lhs->profile_.decode_h265.stdProfileIdc == rhs->profile_.decode_h265.stdProfileIdc;
break;
case VK_VIDEO_CODEC_OPERATION_DECODE_AV1_BIT_KHR:
match = match && lhs->profile_.decode_av1.stdProfile == rhs->profile_.decode_av1.stdProfile &&
lhs->profile_.decode_av1.filmGrainSupport == rhs->profile_.decode_av1.filmGrainSupport;
break;
case VK_VIDEO_CODEC_OPERATION_DECODE_VP9_BIT_KHR:
match = match && lhs->profile_.decode_vp9.stdProfile == rhs->profile_.decode_vp9.stdProfile;
break;
case VK_VIDEO_CODEC_OPERATION_ENCODE_H264_BIT_KHR:
match = match && lhs->profile_.encode_h264.stdProfileIdc == rhs->profile_.encode_h264.stdProfileIdc;
break;
case VK_VIDEO_CODEC_OPERATION_ENCODE_H265_BIT_KHR:
match = match && lhs->profile_.encode_h265.stdProfileIdc == rhs->profile_.encode_h265.stdProfileIdc;
break;
case VK_VIDEO_CODEC_OPERATION_ENCODE_AV1_BIT_KHR:
match = match && lhs->profile_.encode_av1.stdProfile == rhs->profile_.encode_av1.stdProfile;
break;
default:
break;
}
}
return match;
}
};
struct hash {
public:
std::size_t operator()(VideoProfileDesc const *desc) const {
hash_util::HashCombiner hc;
hc << desc->profile_.base.videoCodecOperation << desc->profile_.base.chromaSubsampling
<< desc->profile_.base.lumaBitDepth << desc->profile_.base.chromaBitDepth;
if (desc->profile_.is_decode) {
hc << desc->profile_.decode_usage.videoUsageHints;
}
if (desc->profile_.is_encode) {
hc << desc->profile_.encode_usage.videoUsageHints << desc->profile_.encode_usage.videoContentHints
<< desc->profile_.encode_usage.tuningMode;
}
switch (desc->profile_.base.videoCodecOperation) {
case VK_VIDEO_CODEC_OPERATION_DECODE_H264_BIT_KHR: {
hc << desc->profile_.decode_h264.stdProfileIdc << desc->profile_.decode_h264.pictureLayout;
break;
}
case VK_VIDEO_CODEC_OPERATION_DECODE_H265_BIT_KHR: {
hc << desc->profile_.decode_h265.stdProfileIdc;
break;
}
case VK_VIDEO_CODEC_OPERATION_DECODE_AV1_BIT_KHR: {
hc << desc->profile_.decode_av1.stdProfile << desc->profile_.decode_av1.filmGrainSupport;
break;
}
case VK_VIDEO_CODEC_OPERATION_DECODE_VP9_BIT_KHR: {
hc << desc->profile_.decode_vp9.stdProfile;
break;
}
case VK_VIDEO_CODEC_OPERATION_ENCODE_H264_BIT_KHR: {
hc << desc->profile_.encode_h264.stdProfileIdc;
break;
}
case VK_VIDEO_CODEC_OPERATION_ENCODE_H265_BIT_KHR: {
hc << desc->profile_.encode_h265.stdProfileIdc;
break;
}
case VK_VIDEO_CODEC_OPERATION_ENCODE_AV1_BIT_KHR: {
hc << desc->profile_.encode_av1.stdProfile;
break;
}
default:
break;
}
return hc.Value();
}
};
// The cache maintains non-owning references to all VideoProfileDesc objects that are referred to by shared
// pointers of VideoProfileDesc owned by any object.
// This ensured that every VideoProfileDesc object has only a unique instance acquired from the cache using the
// Get() method and thus enable us to compare video profiles through a single pointer comparison instead of a
// deep compare of the structure chains.
// Once all shared pointers to a VideoProfileDesc go away the destructor will call Release() to remove the
// non-owning reference to the to-be-deleted object.
class Cache {
public:
std::shared_ptr<const VideoProfileDesc> Get(VkPhysicalDevice physical_device, VkVideoProfileInfoKHR const *profile);
SupportedVideoProfiles Get(VkPhysicalDevice physical_device, VkVideoProfileListInfoKHR const *profile_list);
void Release(VideoProfileDesc const *desc);
private:
using PerDeviceVideoProfileDescSet =
unordered_set<VideoProfileDesc const *, VideoProfileDesc::hash, VideoProfileDesc::compare>;
std::mutex mutex_;
unordered_map<VkPhysicalDevice, PerDeviceVideoProfileDescSet> entries_;
std::shared_ptr<const VideoProfileDesc> GetOrCreate(VkPhysicalDevice physical_device, VkVideoProfileInfoKHR const *profile);
};
private:
VkPhysicalDevice physical_device_;
Profile profile_;
Capabilities capabilities_;
SupportedQuantizationMapTexelSizes quant_delta_map_texel_sizes_;
SupportedQuantizationMapTexelSizes emphasis_map_texel_sizes_;
Cache *cache_;
bool InitProfile(VkVideoProfileInfoKHR const *profile);
void InitCapabilities(VkPhysicalDevice physical_device);
void InitQuantizationMapFormats(VkPhysicalDevice physical_device);
};
class VideoPictureResource {
public:
std::shared_ptr<const ImageView> image_view_state;
std::shared_ptr<const Image> image_state;
uint32_t base_array_layer;
VkImageSubresourceRange range;
VkOffset2D coded_offset;
VkExtent2D coded_extent;
VideoPictureResource();
VideoPictureResource(const DeviceState &dev_data, VkVideoPictureResourceInfoKHR const &res);
operator bool() const { return image_view_state != nullptr; }
bool operator==(VideoPictureResource const &rhs) const {
return image_state == rhs.image_state && range.baseMipLevel == rhs.range.baseMipLevel &&
range.baseArrayLayer == rhs.range.baseArrayLayer && coded_offset.x == rhs.coded_offset.x &&
coded_offset.y == rhs.coded_offset.y && coded_extent.width == rhs.coded_extent.width &&
coded_extent.height == rhs.coded_extent.height;
}
bool operator!=(VideoPictureResource const &rhs) const {
return image_state != rhs.image_state || range.baseMipLevel != rhs.range.baseMipLevel ||
range.baseArrayLayer != rhs.range.baseArrayLayer || coded_offset.x != rhs.coded_offset.x ||
coded_offset.y != rhs.coded_offset.y || coded_extent.width != rhs.coded_extent.width ||
coded_extent.height != rhs.coded_extent.height;
}
struct hash {
public:
std::size_t operator()(VideoPictureResource const &res) const {
hash_util::HashCombiner hc;
hc << res.image_state.get() << res.range.baseMipLevel << res.range.baseArrayLayer << res.coded_offset.x
<< res.coded_offset.y << res.coded_extent.width << res.coded_extent.height;
return hc.Value();
}
};
VkOffset3D GetEffectiveImageOffset(const vvl::VideoSession &vs_state) const;
VkExtent3D GetEffectiveImageExtent(const vvl::VideoSession &vs_state) const;
private:
VkImageSubresourceRange GetImageSubresourceRange(ImageView const *image_view_state, uint32_t layer);
};
using VideoPictureResources = unordered_set<VideoPictureResource, VideoPictureResource::hash>;
using BoundVideoPictureResources = unordered_map<VideoPictureResource, int32_t, VideoPictureResource::hash>;
struct VideoPictureID {
// Used by H.264 to indicate it's a top field picture
bool top_field = false;
// Used by H.264 to indicate it's a bottom field picture
bool bottom_field = false;
VideoPictureID() {}
VideoPictureID(VideoProfileDesc const &profile, VkVideoReferenceSlotInfoKHR const &slot);
static VideoPictureID Frame() {
VideoPictureID id{};
return id;
}
static VideoPictureID TopField() {
VideoPictureID id{};
id.top_field = true;
return id;
}
static VideoPictureID BottomField() {
VideoPictureID id{};
id.bottom_field = true;
return id;
}
bool IsFrame() const { return !top_field && !bottom_field; }
bool IsTopField() const { return top_field && !bottom_field; }
bool IsBottomField() const { return !top_field && bottom_field; }
bool IsBothFields() const { return top_field && bottom_field; }
bool ContainsTopField() const { return top_field; }
bool ContainsBottomField() const { return bottom_field; }
bool operator==(VideoPictureID const &rhs) const { return top_field == rhs.top_field && bottom_field == rhs.bottom_field; }
struct hash {
public:
std::size_t operator()(VideoPictureID const &id) const {
hash_util::HashCombiner hc;
hc << id.top_field << id.bottom_field;
return hc.Value();
}
};
};
struct VideoReferenceSlot {
int32_t index;
VideoPictureID picture_id;
VideoPictureResource resource;
VideoReferenceSlot() : index(-1), picture_id(), resource() {}
VideoReferenceSlot(const DeviceState &dev_data, VideoProfileDesc const &profile, VkVideoReferenceSlotInfoKHR const &slot,
bool has_picture_id = true)
: index(slot.slotIndex),
picture_id(has_picture_id ? VideoPictureID(profile, slot) : VideoPictureID()),
resource(slot.pPictureResource ? VideoPictureResource(dev_data, *slot.pPictureResource) : VideoPictureResource()) {}
// The reference is only valid if it refers to a valid DPB index and resource
operator bool() const { return index >= 0 && resource; }
};
struct VideoEncodeRateControlLayerState {
VkVideoEncodeRateControlLayerInfoKHR base;
union {
VkVideoEncodeH264RateControlLayerInfoKHR h264;
VkVideoEncodeH265RateControlLayerInfoKHR h265;
VkVideoEncodeAV1RateControlLayerInfoKHR av1;
};
VideoEncodeRateControlLayerState(VkVideoCodecOperationFlagBitsKHR op = VK_VIDEO_CODEC_OPERATION_NONE_KHR,
const VkVideoEncodeRateControlLayerInfoKHR *info = nullptr) {
base = info ? *info : vku::InitStructHelper();
switch (op) {
case VK_VIDEO_CODEC_OPERATION_ENCODE_H264_BIT_KHR: {
auto h264_info = vku::FindStructInPNextChain<VkVideoEncodeH264RateControlLayerInfoKHR>(info);
h264 = h264_info ? *h264_info : vku::InitStructHelper();
break;
}
case VK_VIDEO_CODEC_OPERATION_ENCODE_H265_BIT_KHR: {
auto h265_info = vku::FindStructInPNextChain<VkVideoEncodeH265RateControlLayerInfoKHR>(info);
h265 = h265_info ? *h265_info : vku::InitStructHelper();
break;
}
case VK_VIDEO_CODEC_OPERATION_ENCODE_AV1_BIT_KHR: {
auto av1_info = vku::FindStructInPNextChain<VkVideoEncodeAV1RateControlLayerInfoKHR>(info);
av1 = av1_info ? *av1_info : vku::InitStructHelper();
break;
}
default:
assert(false);
break;
}
}
};
struct VideoEncodeRateControlState {
bool default_state{true};
VkVideoEncodeRateControlInfoKHR base;
union {
VkVideoEncodeH264RateControlInfoKHR h264;
VkVideoEncodeH265RateControlInfoKHR h265;
VkVideoEncodeAV1RateControlInfoKHR av1;
};
std::vector<VideoEncodeRateControlLayerState> layers;
VideoEncodeRateControlState(VkVideoCodecOperationFlagBitsKHR op = VK_VIDEO_CODEC_OPERATION_NONE_KHR,
const void *info = nullptr) {
auto base_info = vku::FindStructInPNextChain<VkVideoEncodeRateControlInfoKHR>(info);
if (base_info != nullptr) {
default_state = false;
base = *base_info;
} else {
default_state = true;
base = vku::InitStructHelper();
}
switch (op) {
case VK_VIDEO_CODEC_OPERATION_ENCODE_H264_BIT_KHR: {
auto h264_info = vku::FindStructInPNextChain<VkVideoEncodeH264RateControlInfoKHR>(info);
h264 = h264_info ? *h264_info : vku::InitStructHelper();
break;
}
case VK_VIDEO_CODEC_OPERATION_ENCODE_H265_BIT_KHR: {
auto h265_info = vku::FindStructInPNextChain<VkVideoEncodeH265RateControlInfoKHR>(info);
h265 = h265_info ? *h265_info : vku::InitStructHelper();
break;
}
case VK_VIDEO_CODEC_OPERATION_ENCODE_AV1_BIT_KHR: {
auto av1_info = vku::FindStructInPNextChain<VkVideoEncodeAV1RateControlInfoKHR>(info);
av1 = av1_info ? *av1_info : vku::InitStructHelper();
break;
}
default:
break;
}
layers.reserve(base.layerCount);
for (uint32_t i = 0; i < base.layerCount; ++i) {
layers.emplace_back(op, &base.pLayers[i]);
}
}
operator bool() const { return !default_state; }
};
class VideoSessionDeviceState {
public:
VideoSessionDeviceState(uint32_t reference_slot_count = 0)
: initialized_(false),
is_active_(reference_slot_count, false),
all_pictures_(reference_slot_count),
pictures_per_id_(reference_slot_count),
encode_() {}
bool IsInitialized() const { return initialized_; }
bool IsSlotActive(int32_t slot_index) const {
if (slot_index < 0 || static_cast<uint32_t>(slot_index) >= is_active_.size()) {
// Out-of-bounds slot index
return false;
}
return is_active_[slot_index];
}
bool IsSlotPicture(int32_t slot_index, const VideoPictureResource &res) const {
if (slot_index < 0 || static_cast<uint32_t>(slot_index) >= all_pictures_.size()) {
// Out-of-bounds slot index
return false;
}
return all_pictures_[slot_index].find(res) != all_pictures_[slot_index].end();
}
bool IsSlotPicture(int32_t slot_index, const VideoPictureID &picture_id, const VideoPictureResource &res) const {
if (slot_index < 0 || static_cast<uint32_t>(slot_index) >= pictures_per_id_.size()) {
// Out-of-bounds slot index
return false;
}
auto it = pictures_per_id_[slot_index].find(picture_id);
return it != pictures_per_id_[slot_index].end() && it->second == res;
}
uint32_t GetEncodeQualityLevel() const { return encode_.quality_level; }
const VideoEncodeRateControlState &GetRateControlState() const { return encode_.rate_control_state; }
void Reset();
void Activate(int32_t slot_index, const VideoPictureID &picture_id, const VideoPictureResource &res);
void Invalidate(int32_t slot_index, const VideoPictureID &picture_id);
void Deactivate(int32_t slot_index);
void SetEncodeQualityLevel(uint32_t quality_level) { encode_.quality_level = quality_level; }
void SetRateControlState(const VideoEncodeRateControlState &rate_control_state) {
encode_.rate_control_state = rate_control_state;
}
bool ValidateRateControlState(const Logger &log, const VideoSession *vs_state,
const vku::safe_VkVideoBeginCodingInfoKHR &begin_info, const Location &loc) const;
private:
bool initialized_;
std::vector<bool> is_active_;
std::vector<VideoPictureResources> all_pictures_;
std::vector<unordered_map<VideoPictureID, VideoPictureResource, VideoPictureID::hash>> pictures_per_id_;
struct {
uint32_t quality_level{0};
VideoEncodeRateControlState rate_control_state{};
} encode_;
};
class VideoSession : public StateObject {
public:
struct MemoryBindingInfo {
VkMemoryRequirements requirements;
bool bound;
};
using MemoryBindingMap = unordered_map<uint32_t, MemoryBindingInfo>;
const vku::safe_VkVideoSessionCreateInfoKHR safe_create_info;
const VkVideoSessionCreateInfoKHR &create_info;
std::shared_ptr<const VideoProfileDesc> profile;
bool memory_binding_count_queried;
uint32_t memory_bindings_queried;
class DeviceStateWriter {
public:
DeviceStateWriter(VideoSession *state) : lock_(state->device_state_mutex_), state_(state->device_state_) {}
VideoSessionDeviceState &operator*() { return state_; }
private:
std::unique_lock<std::mutex> lock_;
VideoSessionDeviceState &state_;
};
VideoSession(const DeviceState &dev_data, VkVideoSessionKHR handle, VkVideoSessionCreateInfoKHR const *pCreateInfo,
std::shared_ptr<const VideoProfileDesc> &&profile_desc);
VkVideoSessionKHR VkHandle() const { return handle_.Cast<VkVideoSessionKHR>(); }
bool IsDecode() const { return profile->IsDecode(); }
bool IsEncode() const { return profile->IsEncode(); }
VkVideoCodecOperationFlagBitsKHR GetCodecOp() const { return profile->GetCodecOp(); }
VkVideoDecodeH264PictureLayoutFlagBitsKHR GetH264PictureLayout() const { return profile->GetH264PictureLayout(); }
VideoSessionDeviceState DeviceStateCopy() const {
std::unique_lock<std::mutex> lock(device_state_mutex_);
return device_state_;
}
DeviceStateWriter DeviceStateWrite() { return DeviceStateWriter(this); }
const MemoryBindingInfo *GetMemoryBindingInfo(uint32_t index) const {
auto it = memory_bindings_.find(index);
if (it != memory_bindings_.end()) {
return &it->second;
} else {
return nullptr;
}
}
uint32_t GetMemoryBindingCount() const { return (uint32_t)memory_bindings_.size(); }
uint32_t GetUnboundMemoryBindingCount() const { return unbound_memory_binding_count_; }
void BindMemoryBindingIndex(uint32_t index) {
auto it = memory_bindings_.find(index);
if (it != memory_bindings_.end() && !it->second.bound) {
it->second.bound = true;
--unbound_memory_binding_count_;
}
}
uint32_t GetVideoDecodeOperationCount(VkVideoDecodeInfoKHR const *) const { return 1; }
uint32_t GetVideoEncodeOperationCount(VkVideoEncodeInfoKHR const *) const { return 1; }
bool ReferenceSetupRequested(VkVideoDecodeInfoKHR const &decode_info) const;
bool ReferenceSetupRequested(VkVideoEncodeInfoKHR const &encode_info) const;
VkVideoEncodeIntraRefreshModeFlagBitsKHR GetIntraRefreshMode() const { return intra_refresh_mode_; }
private:
MemoryBindingMap GetMemoryBindings(const DeviceState &dev_data, VkVideoSessionKHR vs);
MemoryBindingMap memory_bindings_;
uint32_t unbound_memory_binding_count_;
const VkVideoEncodeIntraRefreshModeFlagBitsKHR intra_refresh_mode_;
mutable std::mutex device_state_mutex_;
VideoSessionDeviceState device_state_;
};
class VideoSessionParameters : public StateObject {
public:
using H264SPSKey = uint8_t;
using H264PPSKey = uint16_t;
using H265VPSKey = uint8_t;
using H265SPSKey = uint16_t;
using H265PPSKey = uint32_t;
using ParameterKey = uint32_t;
struct H264Parameters {
unordered_map<H264SPSKey, StdVideoH264SequenceParameterSet> sps;
unordered_map<H264PPSKey, StdVideoH264PictureParameterSet> pps;
uint32_t sps_capacity;
uint32_t pps_capacity;
};
struct H265Parameters {
unordered_map<H265VPSKey, StdVideoH265VideoParameterSet> vps;
unordered_map<H265SPSKey, StdVideoH265SequenceParameterSet> sps;
unordered_map<H265PPSKey, StdVideoH265PictureParameterSet> pps;
uint32_t vps_capacity;
uint32_t sps_capacity;
uint32_t pps_capacity;
};
struct AV1Parameters {
std::unique_ptr<StdVideoAV1SequenceHeader> seq_header;
};
struct Data {
uint32_t update_sequence_counter;
H264Parameters h264;
H265Parameters h265;
AV1Parameters av1;
};
struct EncodeConfig {
uint32_t quality_level{0};
VkExtent2D quantization_map_texel_size{0, 0};
};
struct Config {
EncodeConfig encode;
};
static H264SPSKey GetH264SPSKey(const StdVideoH264SequenceParameterSet &sps) { return sps.seq_parameter_set_id; }
static H264PPSKey GetH264PPSKey(const StdVideoH264PictureParameterSet &pps) {
return GetKeyFor2xID8(pps.seq_parameter_set_id, pps.pic_parameter_set_id);
}
static H265VPSKey GetH265VPSKey(const StdVideoH265VideoParameterSet &vps) { return vps.vps_video_parameter_set_id; }
static H265SPSKey GetH265SPSKey(const StdVideoH265SequenceParameterSet &sps) {
return GetKeyFor2xID8(sps.sps_video_parameter_set_id, sps.sps_seq_parameter_set_id);
}
static H265PPSKey GetH265PPSKey(const StdVideoH265PictureParameterSet &pps) {
return GetKeyFor3xID8(pps.sps_video_parameter_set_id, pps.pps_seq_parameter_set_id, pps.pps_pic_parameter_set_id);
}
class ReadOnlyAccessor {
public:
ReadOnlyAccessor() : lock_(), data_(nullptr) {}
ReadOnlyAccessor(const VideoSessionParameters *state) : lock_(state->mutex_), data_(&state->data_) {}
operator bool() const { return data_ != nullptr; }
const Data *operator->() const { return data_; }
const StdVideoH264SequenceParameterSet *GetH264SPS(uint32_t sps_id) const {
if (sps_id > 0xFF) return nullptr;
auto it = data_->h264.sps.find(sps_id & 0xFF);
if (it != data_->h264.sps.end()) {
return &it->second;
} else {
return nullptr;
}
}
const StdVideoH264PictureParameterSet *GetH264PPS(uint32_t sps_id, uint32_t pps_id) const {
if (sps_id > 0xFF) return nullptr;
if (pps_id > 0xFF) return nullptr;
auto it = data_->h264.pps.find(GetKeyFor2xID8(sps_id & 0xFF, pps_id & 0xFF));
if (it != data_->h264.pps.end()) {
return &it->second;
} else {
return nullptr;
}
}
const StdVideoH265VideoParameterSet *GetH265VPS(uint32_t vps_id) const {
if (vps_id > 0xFF) return nullptr;
auto it = data_->h265.vps.find(vps_id & 0xFF);
if (it != data_->h265.vps.end()) {
return &it->second;
} else {
return nullptr;
}
}
const StdVideoH265SequenceParameterSet *GetH265SPS(uint32_t vps_id, uint32_t sps_id) const {
if (vps_id > 0xFF) return nullptr;
if (sps_id > 0xFF) return nullptr;
auto it = data_->h265.sps.find(GetKeyFor2xID8(vps_id & 0xFF, sps_id & 0xFF));
if (it != data_->h265.sps.end()) {
return &it->second;
} else {
return nullptr;
}
}
const StdVideoH265PictureParameterSet *GetH265PPS(uint32_t vps_id, uint32_t sps_id, uint32_t pps_id) const {
if (vps_id > 0xFF) return nullptr;
if (sps_id > 0xFF) return nullptr;
if (pps_id > 0xFF) return nullptr;
auto it = data_->h265.pps.find(GetKeyFor3xID8(vps_id & 0xFF, sps_id & 0xFF, pps_id & 0xFF));
if (it != data_->h265.pps.end()) {
return &it->second;
} else {
return nullptr;
}
}
const StdVideoAV1SequenceHeader *GetAV1SequenceHeader() const { return data_->av1.seq_header.get(); }
private:
std::unique_lock<std::mutex> lock_;
const Data *data_;
};
const vku::safe_VkVideoSessionParametersCreateInfoKHR safe_create_info;
const VkVideoSessionParametersCreateInfoKHR &create_info;
std::shared_ptr<const VideoSession> vs_state;
VideoSessionParameters(VkVideoSessionParametersKHR handle, VkVideoSessionParametersCreateInfoKHR const *pCreateInfo,
std::shared_ptr<VideoSession> &&vsstate, std::shared_ptr<VideoSessionParameters> &&vsp_template);
VkVideoSessionParametersKHR VkHandle() const { return handle_.Cast<VkVideoSessionParametersKHR>(); }
bool IsDecode() const { return vs_state->IsDecode(); }
bool IsEncode() const { return vs_state->IsEncode(); }
VkVideoCodecOperationFlagBitsKHR GetCodecOp() const { return vs_state->GetCodecOp(); }
uint32_t GetEncodeQualityLevel() const { return config_.encode.quality_level; }
VkExtent2D GetEncodeQuantizationMapTexelSize() const { return config_.encode.quantization_map_texel_size; }
void Update(VkVideoSessionParametersUpdateInfoKHR const *info);
ReadOnlyAccessor Lock() const { return ReadOnlyAccessor(this); }
private:
mutable std::mutex mutex_;
Data data_;
const Config config_;
Config InitConfig(VkVideoSessionParametersCreateInfoKHR const *pCreateInfo);
static uint16_t GetKeyFor2xID8(uint8_t id1, uint8_t id2) { return (id1 << 8) | id2; }
static uint32_t GetKeyFor3xID8(uint8_t id1, uint8_t id2, uint8_t id3) { return (id1 << 16) | (id2 << 8) | id3; }
void AddDecodeH264(VkVideoDecodeH264SessionParametersAddInfoKHR const *info);
void AddDecodeH265(VkVideoDecodeH265SessionParametersAddInfoKHR const *info);
void AddEncodeH264(VkVideoEncodeH264SessionParametersAddInfoKHR const *info);
void AddEncodeH265(VkVideoEncodeH265SessionParametersAddInfoKHR const *info);
};
using VideoSessionUpdateList =
std::vector<std::function<bool(const VideoSession *vs_state, VideoSessionDeviceState &dev_state, bool do_validate)>>;
using VideoSessionUpdateMap = unordered_map<VkVideoSessionKHR, VideoSessionUpdateList>;
std::string string_VideoProfileDesc(const vvl::VideoProfileDesc &profile);
std::string string_SupportedVideoProfiles(const SupportedVideoProfiles &profiles);
} // namespace vvl
|