obs-hevc.c 5.2 KB

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