nalunit.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * Copyright (c) 2020 Filip Klembara (in2core)
  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_NAL_UNIT_H
  9. #define RTC_NAL_UNIT_H
  10. #if RTC_ENABLE_MEDIA
  11. #include "common.hpp"
  12. #include <cassert>
  13. namespace rtc {
  14. #pragma pack(push, 1)
  15. /// Nalu header
  16. struct RTC_CPP_EXPORT NalUnitHeader {
  17. uint8_t _first = 0;
  18. bool forbiddenBit() const { return _first >> 7; }
  19. uint8_t nri() const { return _first >> 5 & 0x03; }
  20. uint8_t unitType() const { return _first & 0x1F; }
  21. void setForbiddenBit(bool isSet) { _first = (_first & 0x7F) | (isSet << 7); }
  22. void setNRI(uint8_t nri) { _first = (_first & 0x9F) | ((nri & 0x03) << 5); }
  23. void setUnitType(uint8_t type) { _first = (_first & 0xE0) | (type & 0x1F); }
  24. };
  25. /// Nalu fragment header
  26. struct RTC_CPP_EXPORT NalUnitFragmentHeader {
  27. uint8_t _first = 0;
  28. bool isStart() const { return _first >> 7; }
  29. bool reservedBit6() const { return (_first >> 5) & 0x01; }
  30. bool isEnd() const { return (_first >> 6) & 0x01; }
  31. uint8_t unitType() const { return _first & 0x1F; }
  32. void setStart(bool isSet) { _first = (_first & 0x7F) | (isSet << 7); }
  33. void setEnd(bool isSet) { _first = (_first & 0xBF) | (isSet << 6); }
  34. void setReservedBit6(bool isSet) { _first = (_first & 0xDF) | (isSet << 5); }
  35. void setUnitType(uint8_t type) { _first = (_first & 0xE0) | (type & 0x1F); }
  36. };
  37. #pragma pack(pop)
  38. enum NalUnitStartSequenceMatch {
  39. NUSM_noMatch,
  40. NUSM_firstZero,
  41. NUSM_secondZero,
  42. NUSM_thirdZero,
  43. NUSM_shortMatch,
  44. NUSM_longMatch
  45. };
  46. static const size_t H264_NAL_HEADER_SIZE = 1;
  47. static const size_t H265_NAL_HEADER_SIZE = 2;
  48. /// Nal unit
  49. struct RTC_CPP_EXPORT NalUnit : binary {
  50. enum class Type { H264, H265 };
  51. NalUnit(const NalUnit &unit) = default;
  52. NalUnit(size_t size, bool includingHeader = true, Type type = Type::H264)
  53. : binary(size + (includingHeader
  54. ? 0
  55. : (type == Type::H264 ? H264_NAL_HEADER_SIZE : H265_NAL_HEADER_SIZE))) {}
  56. NalUnit(binary &&data) : binary(std::move(data)) {}
  57. NalUnit(Type type = Type::H264)
  58. : binary(type == Type::H264 ? H264_NAL_HEADER_SIZE : H265_NAL_HEADER_SIZE) {}
  59. template <typename Iterator> NalUnit(Iterator begin_, Iterator end_) : binary(begin_, end_) {}
  60. bool forbiddenBit() const { return header()->forbiddenBit(); }
  61. uint8_t nri() const { return header()->nri(); }
  62. uint8_t unitType() const { return header()->unitType(); }
  63. binary payload() const {
  64. assert(size() >= 1);
  65. return {begin() + 1, end()};
  66. }
  67. void setForbiddenBit(bool isSet) { header()->setForbiddenBit(isSet); }
  68. void setNRI(uint8_t nri) { header()->setNRI(nri); }
  69. void setUnitType(uint8_t type) { header()->setUnitType(type); }
  70. void setPayload(binary payload) {
  71. assert(size() >= 1);
  72. erase(begin() + 1, end());
  73. insert(end(), payload.begin(), payload.end());
  74. }
  75. /// NAL unit separator
  76. enum class Separator {
  77. Length = RTC_NAL_SEPARATOR_LENGTH, // first 4 bytes are NAL unit length
  78. LongStartSequence = RTC_NAL_SEPARATOR_LONG_START_SEQUENCE, // 0x00, 0x00, 0x00, 0x01
  79. ShortStartSequence = RTC_NAL_SEPARATOR_SHORT_START_SEQUENCE, // 0x00, 0x00, 0x01
  80. StartSequence = RTC_NAL_SEPARATOR_START_SEQUENCE, // LongStartSequence or ShortStartSequence
  81. };
  82. static NalUnitStartSequenceMatch StartSequenceMatchSucc(NalUnitStartSequenceMatch match,
  83. std::byte _byte, Separator separator) {
  84. assert(separator != Separator::Length);
  85. auto byte = (uint8_t)_byte;
  86. auto detectShort =
  87. separator == Separator::ShortStartSequence || separator == Separator::StartSequence;
  88. auto detectLong =
  89. separator == Separator::LongStartSequence || separator == Separator::StartSequence;
  90. switch (match) {
  91. case NUSM_noMatch:
  92. if (byte == 0x00) {
  93. return NUSM_firstZero;
  94. }
  95. break;
  96. case NUSM_firstZero:
  97. if (byte == 0x00) {
  98. return NUSM_secondZero;
  99. }
  100. break;
  101. case NUSM_secondZero:
  102. if (byte == 0x00 && detectLong) {
  103. return NUSM_thirdZero;
  104. } else if (byte == 0x00 && detectShort) {
  105. return NUSM_secondZero;
  106. } else if (byte == 0x01 && detectShort) {
  107. return NUSM_shortMatch;
  108. }
  109. break;
  110. case NUSM_thirdZero:
  111. if (byte == 0x00 && detectLong) {
  112. return NUSM_thirdZero;
  113. } else if (byte == 0x01 && detectLong) {
  114. return NUSM_longMatch;
  115. }
  116. break;
  117. case NUSM_shortMatch:
  118. return NUSM_shortMatch;
  119. case NUSM_longMatch:
  120. return NUSM_longMatch;
  121. }
  122. return NUSM_noMatch;
  123. }
  124. protected:
  125. const NalUnitHeader *header() const {
  126. assert(size() >= 1);
  127. return reinterpret_cast<const NalUnitHeader *>(data());
  128. }
  129. NalUnitHeader *header() {
  130. assert(size() >= 1);
  131. return reinterpret_cast<NalUnitHeader *>(data());
  132. }
  133. };
  134. /// Nal unit fragment A
  135. struct RTC_CPP_EXPORT NalUnitFragmentA : NalUnit {
  136. static std::vector<shared_ptr<NalUnitFragmentA>> fragmentsFrom(shared_ptr<NalUnit> nalu,
  137. uint16_t maxFragmentSize);
  138. enum class FragmentType { Start, Middle, End };
  139. NalUnitFragmentA(FragmentType type, bool forbiddenBit, uint8_t nri, uint8_t unitType,
  140. binary data);
  141. uint8_t unitType() const { return fragmentHeader()->unitType(); }
  142. binary payload() const {
  143. assert(size() >= 2);
  144. return {begin() + 2, end()};
  145. }
  146. FragmentType type() const {
  147. if (fragmentHeader()->isStart()) {
  148. return FragmentType::Start;
  149. } else if (fragmentHeader()->isEnd()) {
  150. return FragmentType::End;
  151. } else {
  152. return FragmentType::Middle;
  153. }
  154. }
  155. void setUnitType(uint8_t type) { fragmentHeader()->setUnitType(type); }
  156. void setPayload(binary payload) {
  157. assert(size() >= 2);
  158. erase(begin() + 2, end());
  159. insert(end(), payload.begin(), payload.end());
  160. }
  161. void setFragmentType(FragmentType type);
  162. protected:
  163. const uint8_t nal_type_fu_A = 28;
  164. NalUnitHeader *fragmentIndicator() { return reinterpret_cast<NalUnitHeader *>(data()); }
  165. const NalUnitHeader *fragmentIndicator() const {
  166. return reinterpret_cast<const NalUnitHeader *>(data());
  167. }
  168. NalUnitFragmentHeader *fragmentHeader() {
  169. return reinterpret_cast<NalUnitFragmentHeader *>(fragmentIndicator() + 1);
  170. }
  171. const NalUnitFragmentHeader *fragmentHeader() const {
  172. return reinterpret_cast<const NalUnitFragmentHeader *>(fragmentIndicator() + 1);
  173. }
  174. };
  175. class RTC_CPP_EXPORT NalUnits : public std::vector<shared_ptr<NalUnit>> {
  176. public:
  177. static const uint16_t defaultMaximumFragmentSize =
  178. uint16_t(RTC_DEFAULT_MTU - 12 - 8 - 40); // SRTP/UDP/IPv6
  179. std::vector<shared_ptr<binary>> generateFragments(uint16_t maxFragmentSize);
  180. };
  181. } // namespace rtc
  182. #endif /* RTC_ENABLE_MEDIA */
  183. #endif /* RTC_NAL_UNIT_H */