nalunit.hpp 6.7 KB

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