dependencydescriptor.hpp 2.7 KB

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