obs-hevc.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 int compute_hevc_keyframe_priority(const uint8_t *nal_start,
  80. bool *is_keyframe, int priority)
  81. {
  82. // HEVC contains NAL unit specifier at [6..1] bits of
  83. // the byte next to the startcode 0x000001
  84. const int type = (nal_start[0] & 0x7F) >> 1;
  85. // Mark IDR slices as key-frames and set them to highest
  86. // priority if needed. Assume other slices are non-key
  87. // frames and set their priority as high
  88. if (type >= OBS_HEVC_NAL_BLA_W_LP &&
  89. type <= OBS_HEVC_NAL_RSV_IRAP_VCL23) {
  90. *is_keyframe = 1;
  91. priority = OBS_NAL_PRIORITY_HIGHEST;
  92. } else if (type >= OBS_HEVC_NAL_TRAIL_N &&
  93. type <= OBS_HEVC_NAL_RASL_R) {
  94. if (priority < OBS_NAL_PRIORITY_HIGH)
  95. priority = OBS_NAL_PRIORITY_HIGH;
  96. }
  97. return priority;
  98. }
  99. static void serialize_hevc_data(struct serializer *s, const uint8_t *data,
  100. size_t size, bool *is_keyframe, int *priority)
  101. {
  102. const uint8_t *const end = data + size;
  103. const uint8_t *nal_start = obs_nal_find_startcode(data, end);
  104. while (true) {
  105. while (nal_start < end && !*(nal_start++))
  106. ;
  107. if (nal_start == end)
  108. break;
  109. *priority = compute_hevc_keyframe_priority(
  110. nal_start, is_keyframe, *priority);
  111. const uint8_t *const nal_end =
  112. obs_nal_find_startcode(nal_start, end);
  113. const size_t nal_size = nal_end - nal_start;
  114. s_wb32(s, (uint32_t)nal_size);
  115. s_write(s, nal_start, nal_size);
  116. nal_start = nal_end;
  117. }
  118. }
  119. void obs_parse_hevc_packet(struct encoder_packet *hevc_packet,
  120. const struct encoder_packet *src)
  121. {
  122. struct array_output_data output;
  123. struct serializer s;
  124. long ref = 1;
  125. array_output_serializer_init(&s, &output);
  126. *hevc_packet = *src;
  127. serialize(&s, &ref, sizeof(ref));
  128. serialize_hevc_data(&s, src->data, src->size, &hevc_packet->keyframe,
  129. &hevc_packet->priority);
  130. hevc_packet->data = output.bytes.array + sizeof(ref);
  131. hevc_packet->size = output.bytes.num - sizeof(ref);
  132. hevc_packet->drop_priority = hevc_packet->priority;
  133. }
  134. int obs_parse_hevc_packet_priority(const struct encoder_packet *packet)
  135. {
  136. int priority = packet->priority;
  137. const uint8_t *const data = packet->data;
  138. const uint8_t *const end = data + packet->size;
  139. const uint8_t *nal_start = obs_nal_find_startcode(data, end);
  140. while (true) {
  141. while (nal_start < end && !*(nal_start++))
  142. ;
  143. if (nal_start == end)
  144. break;
  145. bool unused;
  146. priority = compute_hevc_keyframe_priority(nal_start, &unused,
  147. priority);
  148. nal_start = obs_nal_find_startcode(nal_start, end);
  149. }
  150. return priority;
  151. }
  152. void obs_extract_hevc_headers(const uint8_t *packet, size_t size,
  153. uint8_t **new_packet_data,
  154. size_t *new_packet_size, uint8_t **header_data,
  155. size_t *header_size, uint8_t **sei_data,
  156. size_t *sei_size)
  157. {
  158. DARRAY(uint8_t) new_packet;
  159. DARRAY(uint8_t) header;
  160. DARRAY(uint8_t) sei;
  161. const uint8_t *nal_start, *nal_end, *nal_codestart;
  162. const uint8_t *end = packet + size;
  163. da_init(new_packet);
  164. da_init(header);
  165. da_init(sei);
  166. nal_start = obs_nal_find_startcode(packet, end);
  167. nal_end = NULL;
  168. while (nal_end != end) {
  169. nal_codestart = nal_start;
  170. while (nal_start < end && !*(nal_start++))
  171. ;
  172. if (nal_start == end)
  173. break;
  174. const uint8_t type = (nal_start[0] & 0x7F) >> 1;
  175. nal_end = obs_nal_find_startcode(nal_start, end);
  176. if (!nal_end)
  177. nal_end = end;
  178. if (type == OBS_HEVC_NAL_VPS || type == OBS_HEVC_NAL_SPS ||
  179. type == OBS_HEVC_NAL_PPS) {
  180. da_push_back_array(header, nal_codestart,
  181. nal_end - nal_codestart);
  182. } else if (type == OBS_HEVC_NAL_SEI_PREFIX ||
  183. type == OBS_HEVC_NAL_SEI_SUFFIX) {
  184. da_push_back_array(sei, nal_codestart,
  185. nal_end - nal_codestart);
  186. } else {
  187. da_push_back_array(new_packet, nal_codestart,
  188. nal_end - nal_codestart);
  189. }
  190. nal_start = nal_end;
  191. }
  192. *new_packet_data = new_packet.array;
  193. *new_packet_size = new_packet.num;
  194. *header_data = header.array;
  195. *header_size = header.num;
  196. *sei_data = sei.array;
  197. *sei_size = sei.num;
  198. }