dependencydescriptor.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Copyright (c) 2024 Shigemasa Watanabe (Wandbox)
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #ifndef RTC_DEPENDENCY_DESCRIPTOR_H
  9. #define RTC_DEPENDENCY_DESCRIPTOR_H
  10. #include <bitset>
  11. #include <cassert>
  12. #include <optional>
  13. #include <vector>
  14. namespace rtc {
  15. struct BitWriter {
  16. static BitWriter fromSizeBits(std::byte *buf, size_t offsetBits, size_t sizeBits);
  17. static BitWriter fromNull();
  18. size_t getWrittenBits() const;
  19. bool write(uint64_t v, size_t bits);
  20. // Write non-symmetric unsigned encoded integer
  21. // ref: https://aomediacodec.github.io/av1-rtp-spec/#a82-syntax
  22. bool writeNonSymmetric(uint64_t v, uint64_t n);
  23. private:
  24. size_t writePartialByte(uint8_t *p, size_t offset, uint64_t v, size_t bits);
  25. private:
  26. std::byte *mBuf = nullptr;
  27. size_t mInitialOffset = 0;
  28. size_t mOffset = 0;
  29. size_t mSize = 0;
  30. };
  31. enum class DecodeTargetIndication {
  32. NotPresent = 0,
  33. Discardable = 1,
  34. Switch = 2,
  35. Required = 3,
  36. };
  37. struct RenderResolution {
  38. int width = 0;
  39. int height = 0;
  40. };
  41. struct FrameDependencyTemplate {
  42. int spatialId = 0;
  43. int temporalId = 0;
  44. std::vector<DecodeTargetIndication> decodeTargetIndications;
  45. std::vector<int> frameDiffs;
  46. std::vector<int> chainDiffs;
  47. };
  48. struct FrameDependencyStructure {
  49. int templateIdOffset = 0;
  50. int decodeTargetCount = 0;
  51. int chainCount = 0;
  52. std::vector<int> decodeTargetProtectedBy;
  53. std::vector<RenderResolution> resolutions;
  54. std::vector<FrameDependencyTemplate> templates;
  55. };
  56. struct DependencyDescriptor {
  57. bool startOfFrame = true;
  58. bool endOfFrame = true;
  59. int frameNumber = 0;
  60. FrameDependencyTemplate dependencyTemplate;
  61. std::optional<RenderResolution> resolution;
  62. std::optional<uint32_t> activeDecodeTargetsBitmask;
  63. bool structureAttached;
  64. };
  65. // Write dependency descriptor to RTP Header Extension
  66. // Dependency descriptor specification is here:
  67. // https://aomediacodec.github.io/av1-rtp-spec/#dependency-descriptor-rtp-header-extension
  68. class DependencyDescriptorWriter {
  69. public:
  70. DependencyDescriptorWriter(const FrameDependencyStructure &structure,
  71. std::bitset<32> activeChains,
  72. const DependencyDescriptor &descriptor);
  73. size_t getSizeBits() const;
  74. void writeTo(std::byte *buf, size_t sizeBits) const;
  75. private:
  76. void doWriteTo(BitWriter &writer) const;
  77. void writeBits(BitWriter &writer, uint64_t v, size_t bits) const;
  78. void writeNonSymmetric(BitWriter &writer, uint64_t v, uint64_t n) const;
  79. private:
  80. const FrameDependencyStructure &mStructure;
  81. std::bitset<32> mActiveChains;
  82. const DependencyDescriptor &mDescriptor;
  83. };
  84. } // namespace rtc
  85. #endif