dependencydescriptor.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 <stdint.h>
  14. #include <vector>
  15. namespace rtc {
  16. struct BitWriter {
  17. static BitWriter fromSizeBits(std::byte *buf, size_t offsetBits, size_t sizeBits);
  18. static BitWriter fromNull();
  19. size_t getWrittenBits() const;
  20. bool write(uint64_t v, size_t bits);
  21. // Write non-symmetric unsigned encoded integer
  22. // ref: https://aomediacodec.github.io/av1-rtp-spec/#a82-syntax
  23. bool writeNonSymmetric(uint64_t v, uint64_t n);
  24. private:
  25. size_t writePartialByte(uint8_t *p, size_t offset, uint64_t v, size_t bits);
  26. private:
  27. std::byte *mBuf = nullptr;
  28. size_t mInitialOffset = 0;
  29. size_t mOffset = 0;
  30. size_t mSize = 0;
  31. };
  32. enum class DecodeTargetIndication {
  33. NotPresent = 0,
  34. Discardable = 1,
  35. Switch = 2,
  36. Required = 3,
  37. };
  38. struct RenderResolution {
  39. int width = 0;
  40. int height = 0;
  41. };
  42. struct FrameDependencyTemplate {
  43. int spatialId = 0;
  44. int temporalId = 0;
  45. std::vector<DecodeTargetIndication> decodeTargetIndications;
  46. std::vector<int> frameDiffs;
  47. std::vector<int> chainDiffs;
  48. };
  49. struct FrameDependencyStructure {
  50. int templateIdOffset = 0;
  51. int decodeTargetCount = 0;
  52. int chainCount = 0;
  53. std::vector<int> decodeTargetProtectedBy;
  54. std::vector<RenderResolution> resolutions;
  55. std::vector<FrameDependencyTemplate> templates;
  56. };
  57. struct DependencyDescriptor {
  58. bool startOfFrame = true;
  59. bool endOfFrame = true;
  60. int frameNumber = 0;
  61. FrameDependencyTemplate dependencyTemplate;
  62. std::optional<RenderResolution> resolution;
  63. std::optional<uint32_t> activeDecodeTargetsBitmask;
  64. bool structureAttached;
  65. };
  66. struct DependencyDescriptorContext {
  67. DependencyDescriptor descriptor;
  68. std::bitset<32> activeChains;
  69. FrameDependencyStructure structure;
  70. };
  71. // Write dependency descriptor to RTP Header Extension
  72. // Dependency descriptor specification is here:
  73. // https://aomediacodec.github.io/av1-rtp-spec/#dependency-descriptor-rtp-header-extension
  74. class DependencyDescriptorWriter {
  75. public:
  76. explicit DependencyDescriptorWriter(const DependencyDescriptorContext& context);
  77. size_t getSizeBits() const;
  78. size_t getSize() const;
  79. void writeTo(std::byte *buf, size_t sizeBytes) const;
  80. private:
  81. void doWriteTo(BitWriter &writer) const;
  82. void writeBits(BitWriter &writer, uint64_t v, size_t bits) const;
  83. void writeNonSymmetric(BitWriter &writer, uint64_t v, uint64_t n) const;
  84. private:
  85. const FrameDependencyStructure &mStructure;
  86. std::bitset<32> mActiveChains;
  87. const DependencyDescriptor &mDescriptor;
  88. };
  89. } // namespace rtc
  90. #endif