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