h265nalunit.hpp 5.8 KB

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