obs-hevc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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. bool obs_hevc_keyframe(const uint8_t *data, size_t size)
  19. {
  20. const uint8_t *nal_start, *nal_end;
  21. const uint8_t *end = data + size;
  22. nal_start = obs_nal_find_startcode(data, end);
  23. while (true) {
  24. while (nal_start < end && !*(nal_start++))
  25. ;
  26. if (nal_start == end)
  27. break;
  28. const uint8_t type = (nal_start[0] & 0x7F) >> 1;
  29. if (type <= OBS_HEVC_NAL_RSV_IRAP_VCL23)
  30. return type >= OBS_HEVC_NAL_BLA_W_LP;
  31. nal_end = obs_nal_find_startcode(nal_start, end);
  32. nal_start = nal_end;
  33. }
  34. return false;
  35. }
  36. static int compute_hevc_keyframe_priority(const uint8_t *nal_start,
  37. bool *is_keyframe, int priority)
  38. {
  39. // HEVC contains NAL unit specifier at [6..1] bits of
  40. // the byte next to the startcode 0x000001
  41. const int type = (nal_start[0] & 0x7F) >> 1;
  42. // Mark IDR slices as key-frames and set them to highest
  43. // priority if needed. Assume other slices are non-key
  44. // frames and set their priority as high
  45. if (type >= OBS_HEVC_NAL_BLA_W_LP &&
  46. type <= OBS_HEVC_NAL_RSV_IRAP_VCL23) {
  47. *is_keyframe = 1;
  48. priority = OBS_NAL_PRIORITY_HIGHEST;
  49. } else if (type >= OBS_HEVC_NAL_TRAIL_N &&
  50. type <= OBS_HEVC_NAL_RASL_R) {
  51. if (priority < OBS_NAL_PRIORITY_HIGH)
  52. priority = OBS_NAL_PRIORITY_HIGH;
  53. }
  54. return priority;
  55. }
  56. static void serialize_hevc_data(struct serializer *s, const uint8_t *data,
  57. size_t size, bool *is_keyframe, int *priority)
  58. {
  59. const uint8_t *const end = data + size;
  60. const uint8_t *nal_start = obs_nal_find_startcode(data, end);
  61. while (true) {
  62. while (nal_start < end && !*(nal_start++))
  63. ;
  64. if (nal_start == end)
  65. break;
  66. *priority = compute_hevc_keyframe_priority(
  67. nal_start, is_keyframe, *priority);
  68. const uint8_t *const nal_end =
  69. obs_nal_find_startcode(nal_start, end);
  70. const size_t nal_size = nal_end - nal_start;
  71. s_wb32(s, (uint32_t)nal_size);
  72. s_write(s, nal_start, nal_size);
  73. nal_start = nal_end;
  74. }
  75. }
  76. void obs_parse_hevc_packet(struct encoder_packet *hevc_packet,
  77. const struct encoder_packet *src)
  78. {
  79. struct array_output_data output;
  80. struct serializer s;
  81. long ref = 1;
  82. array_output_serializer_init(&s, &output);
  83. *hevc_packet = *src;
  84. serialize(&s, &ref, sizeof(ref));
  85. serialize_hevc_data(&s, src->data, src->size, &hevc_packet->keyframe,
  86. &hevc_packet->priority);
  87. hevc_packet->data = output.bytes.array + sizeof(ref);
  88. hevc_packet->size = output.bytes.num - sizeof(ref);
  89. hevc_packet->drop_priority = hevc_packet->priority;
  90. }
  91. int obs_parse_hevc_packet_priority(const struct encoder_packet *packet)
  92. {
  93. int priority = packet->priority;
  94. const uint8_t *const data = packet->data;
  95. const uint8_t *const end = data + packet->size;
  96. const uint8_t *nal_start = obs_nal_find_startcode(data, end);
  97. while (true) {
  98. while (nal_start < end && !*(nal_start++))
  99. ;
  100. if (nal_start == end)
  101. break;
  102. bool unused;
  103. priority = compute_hevc_keyframe_priority(nal_start, &unused,
  104. priority);
  105. nal_start = obs_nal_find_startcode(nal_start, end);
  106. }
  107. return priority;
  108. }
  109. void obs_extract_hevc_headers(const uint8_t *packet, size_t size,
  110. uint8_t **new_packet_data,
  111. size_t *new_packet_size, uint8_t **header_data,
  112. size_t *header_size, uint8_t **sei_data,
  113. size_t *sei_size)
  114. {
  115. DARRAY(uint8_t) new_packet;
  116. DARRAY(uint8_t) header;
  117. DARRAY(uint8_t) sei;
  118. const uint8_t *nal_start, *nal_end, *nal_codestart;
  119. const uint8_t *end = packet + size;
  120. da_init(new_packet);
  121. da_init(header);
  122. da_init(sei);
  123. nal_start = obs_nal_find_startcode(packet, end);
  124. nal_end = NULL;
  125. while (nal_end != end) {
  126. nal_codestart = nal_start;
  127. while (nal_start < end && !*(nal_start++))
  128. ;
  129. if (nal_start == end)
  130. break;
  131. const uint8_t type = (nal_start[0] & 0x7F) >> 1;
  132. nal_end = obs_nal_find_startcode(nal_start, end);
  133. if (!nal_end)
  134. nal_end = end;
  135. if (type == OBS_HEVC_NAL_VPS || type == OBS_HEVC_NAL_SPS ||
  136. type == OBS_HEVC_NAL_PPS) {
  137. da_push_back_array(header, nal_codestart,
  138. nal_end - nal_codestart);
  139. } else if (type == OBS_HEVC_NAL_SEI_PREFIX ||
  140. type == OBS_HEVC_NAL_SEI_SUFFIX) {
  141. da_push_back_array(sei, nal_codestart,
  142. nal_end - nal_codestart);
  143. } else {
  144. da_push_back_array(new_packet, nal_codestart,
  145. nal_end - nal_codestart);
  146. }
  147. nal_start = nal_end;
  148. }
  149. *new_packet_data = new_packet.array;
  150. *new_packet_size = new_packet.num;
  151. *header_data = header.array;
  152. *header_size = header.num;
  153. *sei_data = sei.array;
  154. *sei_size = sei.num;
  155. }