obs-hevc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /******************************************************************************
  2. Copyright (C) 2022 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "obs-hevc.h"
  15. #include "obs.h"
  16. #include "obs-nal.h"
  17. #include "util/array-serializer.h"
  18. enum {
  19. OBS_HEVC_NAL_TRAIL_N = 0,
  20. OBS_HEVC_NAL_TRAIL_R = 1,
  21. OBS_HEVC_NAL_TSA_N = 2,
  22. OBS_HEVC_NAL_TSA_R = 3,
  23. OBS_HEVC_NAL_STSA_N = 4,
  24. OBS_HEVC_NAL_STSA_R = 5,
  25. OBS_HEVC_NAL_RADL_N = 6,
  26. OBS_HEVC_NAL_RADL_R = 7,
  27. OBS_HEVC_NAL_RASL_N = 8,
  28. OBS_HEVC_NAL_RASL_R = 9,
  29. OBS_HEVC_NAL_VCL_N10 = 10,
  30. OBS_HEVC_NAL_VCL_R11 = 11,
  31. OBS_HEVC_NAL_VCL_N12 = 12,
  32. OBS_HEVC_NAL_VCL_R13 = 13,
  33. OBS_HEVC_NAL_VCL_N14 = 14,
  34. OBS_HEVC_NAL_VCL_R15 = 15,
  35. OBS_HEVC_NAL_BLA_W_LP = 16,
  36. OBS_HEVC_NAL_BLA_W_RADL = 17,
  37. OBS_HEVC_NAL_BLA_N_LP = 18,
  38. OBS_HEVC_NAL_IDR_W_RADL = 19,
  39. OBS_HEVC_NAL_IDR_N_LP = 20,
  40. OBS_HEVC_NAL_CRA_NUT = 21,
  41. OBS_HEVC_NAL_RSV_IRAP_VCL22 = 22,
  42. OBS_HEVC_NAL_RSV_IRAP_VCL23 = 23,
  43. OBS_HEVC_NAL_RSV_VCL24 = 24,
  44. OBS_HEVC_NAL_RSV_VCL25 = 25,
  45. OBS_HEVC_NAL_RSV_VCL26 = 26,
  46. OBS_HEVC_NAL_RSV_VCL27 = 27,
  47. OBS_HEVC_NAL_RSV_VCL28 = 28,
  48. OBS_HEVC_NAL_RSV_VCL29 = 29,
  49. OBS_HEVC_NAL_RSV_VCL30 = 30,
  50. OBS_HEVC_NAL_RSV_VCL31 = 31,
  51. OBS_HEVC_NAL_VPS = 32,
  52. OBS_HEVC_NAL_SPS = 33,
  53. OBS_HEVC_NAL_PPS = 34,
  54. OBS_HEVC_NAL_AUD = 35,
  55. OBS_HEVC_NAL_EOS_NUT = 36,
  56. OBS_HEVC_NAL_EOB_NUT = 37,
  57. OBS_HEVC_NAL_FD_NUT = 38,
  58. OBS_HEVC_NAL_SEI_PREFIX = 39,
  59. OBS_HEVC_NAL_SEI_SUFFIX = 40,
  60. };
  61. bool obs_hevc_keyframe(const uint8_t *data, size_t size)
  62. {
  63. const uint8_t *nal_start, *nal_end;
  64. const uint8_t *end = data + size;
  65. nal_start = obs_nal_find_startcode(data, end);
  66. while (true) {
  67. while (nal_start < end && !*(nal_start++))
  68. ;
  69. if (nal_start == end)
  70. break;
  71. const uint8_t type = (nal_start[0] & 0x7F) >> 1;
  72. if (type <= OBS_HEVC_NAL_RSV_IRAP_VCL23)
  73. return type >= OBS_HEVC_NAL_BLA_W_LP;
  74. nal_end = obs_nal_find_startcode(nal_start, end);
  75. nal_start = nal_end;
  76. }
  77. return false;
  78. }
  79. static void serialize_hevc_data(struct serializer *s, const uint8_t *data,
  80. size_t size, bool *is_keyframe, int *priority)
  81. {
  82. const uint8_t *const end = data + size;
  83. const uint8_t *nal_start = obs_nal_find_startcode(data, end);
  84. while (true) {
  85. while (nal_start < end && !*(nal_start++))
  86. ;
  87. if (nal_start == end)
  88. break;
  89. // HEVC contains NAL unit specifier at [6..1] bits of
  90. // the byte next to the startcode 0x000001
  91. const int type = (nal_start[0] & 0x7F) >> 1;
  92. // Mark IDR slices as key-frames and set them to highest
  93. // priority if needed. Assume other slices are non-key
  94. // frames and set their priority as high
  95. if (type >= OBS_HEVC_NAL_BLA_W_LP &&
  96. type <= OBS_HEVC_NAL_RSV_IRAP_VCL23) {
  97. *is_keyframe = 1;
  98. *priority = OBS_NAL_PRIORITY_HIGHEST;
  99. } else if (type >= OBS_HEVC_NAL_TRAIL_N &&
  100. type <= OBS_HEVC_NAL_RASL_R) {
  101. if (*priority < OBS_NAL_PRIORITY_HIGH)
  102. *priority = OBS_NAL_PRIORITY_HIGH;
  103. }
  104. const uint8_t *const nal_end =
  105. obs_nal_find_startcode(nal_start, end);
  106. const size_t size = nal_end - nal_start;
  107. s_wb32(s, (uint32_t)size);
  108. s_write(s, nal_start, size);
  109. nal_start = nal_end;
  110. }
  111. }
  112. void obs_parse_hevc_packet(struct encoder_packet *hevc_packet,
  113. const struct encoder_packet *src)
  114. {
  115. struct array_output_data output;
  116. struct serializer s;
  117. long ref = 1;
  118. array_output_serializer_init(&s, &output);
  119. *hevc_packet = *src;
  120. serialize(&s, &ref, sizeof(ref));
  121. serialize_hevc_data(&s, src->data, src->size, &hevc_packet->keyframe,
  122. &hevc_packet->priority);
  123. hevc_packet->data = output.bytes.array + sizeof(ref);
  124. hevc_packet->size = output.bytes.num - sizeof(ref);
  125. hevc_packet->drop_priority = hevc_packet->priority;
  126. }
  127. void obs_extract_hevc_headers(const uint8_t *packet, size_t size,
  128. uint8_t **new_packet_data,
  129. size_t *new_packet_size, uint8_t **header_data,
  130. size_t *header_size, uint8_t **sei_data,
  131. size_t *sei_size)
  132. {
  133. DARRAY(uint8_t) new_packet;
  134. DARRAY(uint8_t) header;
  135. DARRAY(uint8_t) sei;
  136. const uint8_t *nal_start, *nal_end, *nal_codestart;
  137. const uint8_t *end = packet + size;
  138. da_init(new_packet);
  139. da_init(header);
  140. da_init(sei);
  141. nal_start = obs_nal_find_startcode(packet, end);
  142. nal_end = NULL;
  143. while (nal_end != end) {
  144. nal_codestart = nal_start;
  145. while (nal_start < end && !*(nal_start++))
  146. ;
  147. if (nal_start == end)
  148. break;
  149. const uint8_t type = (nal_start[0] & 0x7F) >> 1;
  150. nal_end = obs_nal_find_startcode(nal_start, end);
  151. if (!nal_end)
  152. nal_end = end;
  153. if (type == OBS_HEVC_NAL_VPS || type == OBS_HEVC_NAL_SPS ||
  154. type == OBS_HEVC_NAL_PPS) {
  155. da_push_back_array(header, nal_codestart,
  156. nal_end - nal_codestart);
  157. } else if (type == OBS_HEVC_NAL_SEI_PREFIX ||
  158. type == OBS_HEVC_NAL_SEI_SUFFIX) {
  159. da_push_back_array(sei, nal_codestart,
  160. nal_end - nal_codestart);
  161. } else {
  162. da_push_back_array(new_packet, nal_codestart,
  163. nal_end - nal_codestart);
  164. }
  165. nal_start = nal_end;
  166. }
  167. *new_packet_data = new_packet.array;
  168. *new_packet_size = new_packet.num;
  169. *header_data = header.array;
  170. *header_size = header.num;
  171. *sei_data = sei.array;
  172. *sei_size = sei.num;
  173. }