2
0

h265nalunit.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * Copyright (c) 2023 Zita Liao (Dolby)
  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_H265_NAL_UNIT_H
  9. #define RTC_H265_NAL_UNIT_H
  10. #if RTC_ENABLE_MEDIA
  11. #include "common.hpp"
  12. #include "nalunit.hpp"
  13. #include <cassert>
  14. #include <vector>
  15. namespace rtc {
  16. #pragma pack(push, 1)
  17. #define H265_FU_HEADER_SIZE 1
  18. /// Nalu header
  19. struct RTC_CPP_EXPORT H265NalUnitHeader {
  20. /*
  21. * nal_unit_header( ) {
  22. * forbidden_zero_bit f(1)
  23. * nal_unit_type u(6)
  24. * nuh_layer_id u(6)
  25. * nuh_temporal_id_plus1 u(3)
  26. }
  27. */
  28. uint8_t _first = 0; // high byte of header
  29. uint8_t _second = 0; // low byte of header
  30. bool forbiddenBit() const { return _first >> 7; }
  31. uint8_t unitType() const { return (_first & 0b0111'1110) >> 1; }
  32. uint8_t nuhLayerId() const { return ((_first & 0x1) << 5) | ((_second & 0b1111'1000) >> 3); }
  33. uint8_t nuhTempIdPlus1() const { return _second & 0b111; }
  34. void setForbiddenBit(bool isSet) { _first = (_first & 0x7F) | (isSet << 7); }
  35. void setUnitType(uint8_t type) { _first = (_first & 0b1000'0001) | ((type & 0b11'1111) << 1); }
  36. void setNuhLayerId(uint8_t nuhLayerId) {
  37. _first = (_first & 0b1111'1110) | ((nuhLayerId & 0b10'0000) >> 5);
  38. _second = (_second & 0b0000'0111) | ((nuhLayerId & 0b01'1111) << 3);
  39. }
  40. void setNuhTempIdPlus1(uint8_t nuhTempIdPlus1) {
  41. _second = (_second & 0b1111'1000) | (nuhTempIdPlus1 & 0b111);
  42. }
  43. };
  44. /// Nalu fragment header
  45. struct RTC_CPP_EXPORT H265NalUnitFragmentHeader {
  46. /*
  47. * +---------------+
  48. * |0|1|2|3|4|5|6|7|
  49. * +-+-+-+-+-+-+-+-+
  50. * |S|E| FuType |
  51. * +---------------+
  52. */
  53. uint8_t _first = 0;
  54. bool isStart() const { return _first >> 7; }
  55. bool isEnd() const { return (_first >> 6) & 0x01; }
  56. uint8_t unitType() const { return _first & 0b11'1111; }
  57. void setStart(bool isSet) { _first = (_first & 0x7F) | (isSet << 7); }
  58. void setEnd(bool isSet) { _first = (_first & 0b1011'1111) | (isSet << 6); }
  59. void setUnitType(uint8_t type) { _first = (_first & 0b1100'0000) | (type & 0b11'1111); }
  60. };
  61. #pragma pack(pop)
  62. struct H265NalUnitFragment;
  63. /// NAL unit
  64. struct RTC_CPP_EXPORT H265NalUnit : NalUnit {
  65. static std::vector<binary> GenerateFragments(const std::vector<H265NalUnit> &nalus,
  66. size_t maxFragmentSize);
  67. H265NalUnit(const H265NalUnit &unit) = default;
  68. H265NalUnit(size_t size, bool includingHeader = true)
  69. : NalUnit(size, includingHeader, NalUnit::Type::H265) {}
  70. H265NalUnit(binary &&data) : NalUnit(std::move(data)) {}
  71. H265NalUnit() : NalUnit(NalUnit::Type::H265) {}
  72. template <typename Iterator>
  73. H265NalUnit(Iterator begin_, Iterator end_) : NalUnit(begin_, end_) {}
  74. bool forbiddenBit() const { return header()->forbiddenBit(); }
  75. uint8_t unitType() const { return header()->unitType(); }
  76. uint8_t nuhLayerId() const { return header()->nuhLayerId(); }
  77. uint8_t nuhTempIdPlus1() const { return header()->nuhTempIdPlus1(); }
  78. binary payload() const {
  79. assert(size() >= H265_NAL_HEADER_SIZE);
  80. return {begin() + H265_NAL_HEADER_SIZE, end()};
  81. }
  82. void setForbiddenBit(bool isSet) { header()->setForbiddenBit(isSet); }
  83. void setUnitType(uint8_t type) { header()->setUnitType(type); }
  84. void setNuhLayerId(uint8_t nuhLayerId) { header()->setNuhLayerId(nuhLayerId); }
  85. void setNuhTempIdPlus1(uint8_t nuhTempIdPlus1) { header()->setNuhTempIdPlus1(nuhTempIdPlus1); }
  86. void setPayload(binary payload) {
  87. assert(size() >= H265_NAL_HEADER_SIZE);
  88. erase(begin() + H265_NAL_HEADER_SIZE, end());
  89. insert(end(), payload.begin(), payload.end());
  90. }
  91. std::vector<H265NalUnitFragment> generateFragments(size_t maxFragmentSize) const;
  92. protected:
  93. const H265NalUnitHeader *header() const {
  94. assert(size() >= H265_NAL_HEADER_SIZE);
  95. return reinterpret_cast<const H265NalUnitHeader *>(data());
  96. }
  97. H265NalUnitHeader *header() {
  98. assert(size() >= H265_NAL_HEADER_SIZE);
  99. return reinterpret_cast<H265NalUnitHeader *>(data());
  100. }
  101. };
  102. /// NAL unit fragment
  103. struct RTC_CPP_EXPORT H265NalUnitFragment : H265NalUnit {
  104. [[deprecated]] static std::vector<shared_ptr<H265NalUnitFragment>> fragmentsFrom(shared_ptr<H265NalUnit> nalu,
  105. uint16_t maxFragmentSize);
  106. enum class FragmentType { Start, Middle, End };
  107. H265NalUnitFragment(FragmentType type, bool forbiddenBit, uint8_t nuhLayerId,
  108. uint8_t nuhTempIdPlus1, uint8_t unitType, binary data);
  109. uint8_t unitType() const { return fragmentHeader()->unitType(); }
  110. binary payload() const {
  111. assert(size() >= H265_NAL_HEADER_SIZE + H265_FU_HEADER_SIZE);
  112. return {begin() + H265_NAL_HEADER_SIZE + H265_FU_HEADER_SIZE, end()};
  113. }
  114. FragmentType type() const {
  115. if (fragmentHeader()->isStart()) {
  116. return FragmentType::Start;
  117. } else if (fragmentHeader()->isEnd()) {
  118. return FragmentType::End;
  119. } else {
  120. return FragmentType::Middle;
  121. }
  122. }
  123. void setUnitType(uint8_t type) { fragmentHeader()->setUnitType(type); }
  124. void setPayload(binary payload) {
  125. assert(size() >= H265_NAL_HEADER_SIZE + H265_FU_HEADER_SIZE);
  126. erase(begin() + H265_NAL_HEADER_SIZE + H265_FU_HEADER_SIZE, end());
  127. insert(end(), payload.begin(), payload.end());
  128. }
  129. void setFragmentType(FragmentType type);
  130. protected:
  131. const uint8_t nal_type_fu = 49;
  132. H265NalUnitHeader *fragmentIndicator() { return reinterpret_cast<H265NalUnitHeader *>(data()); }
  133. const H265NalUnitHeader *fragmentIndicator() const {
  134. return reinterpret_cast<const H265NalUnitHeader *>(data());
  135. }
  136. H265NalUnitFragmentHeader *fragmentHeader() {
  137. return reinterpret_cast<H265NalUnitFragmentHeader *>(data() + H265_NAL_HEADER_SIZE);
  138. }
  139. const H265NalUnitFragmentHeader *fragmentHeader() const {
  140. return reinterpret_cast<const H265NalUnitFragmentHeader *>(data() + H265_NAL_HEADER_SIZE);
  141. }
  142. };
  143. class [[deprecated]] RTC_CPP_EXPORT H265NalUnits : public std::vector<shared_ptr<H265NalUnit>> {
  144. public:
  145. static const uint16_t defaultMaximumFragmentSize =
  146. uint16_t(RTC_DEFAULT_MTU - 12 - 8 - 40); // SRTP/UDP/IPv6
  147. std::vector<shared_ptr<binary>> generateFragments(uint16_t maxFragmentSize);
  148. };
  149. } // namespace rtc
  150. #endif /* RTC_ENABLE_MEDIA */
  151. #endif /* RTC_NAL_UNIT_H */