obs-hevc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. int new_priority;
  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. switch (type) {
  43. case OBS_HEVC_NAL_BLA_W_LP:
  44. case OBS_HEVC_NAL_BLA_W_RADL:
  45. case OBS_HEVC_NAL_BLA_N_LP:
  46. case OBS_HEVC_NAL_IDR_W_RADL:
  47. case OBS_HEVC_NAL_IDR_N_LP:
  48. case OBS_HEVC_NAL_CRA_NUT:
  49. case OBS_HEVC_NAL_RSV_IRAP_VCL22:
  50. case OBS_HEVC_NAL_RSV_IRAP_VCL23:
  51. /* intra random access point (IRAP) picture, keyframe and highest priority */
  52. *is_keyframe = true;
  53. new_priority = OBS_NAL_PRIORITY_HIGHEST;
  54. break;
  55. case OBS_HEVC_NAL_TRAIL_R:
  56. case OBS_HEVC_NAL_TSA_R:
  57. case OBS_HEVC_NAL_STSA_R:
  58. case OBS_HEVC_NAL_RADL_R:
  59. case OBS_HEVC_NAL_RASL_R:
  60. /* sub-layer reference picture (mainly P-frames), high priority */
  61. new_priority = OBS_NAL_PRIORITY_HIGH;
  62. break;
  63. case OBS_HEVC_NAL_TRAIL_N:
  64. case OBS_HEVC_NAL_TSA_N:
  65. case OBS_HEVC_NAL_STSA_N:
  66. case OBS_HEVC_NAL_RADL_N:
  67. case OBS_HEVC_NAL_RASL_N:
  68. /* sub-layer non-reference (SLNR) picture (mainly B-frames), disposable */
  69. new_priority = OBS_NAL_PRIORITY_DISPOSABLE;
  70. break;
  71. default:
  72. new_priority = OBS_NAL_PRIORITY_DISPOSABLE;
  73. }
  74. return priority > new_priority ? priority : new_priority;
  75. }
  76. static void serialize_hevc_data(struct serializer *s, const uint8_t *data, size_t size, bool *is_keyframe,
  77. int *priority)
  78. {
  79. const uint8_t *const end = data + size;
  80. const uint8_t *nal_start = obs_nal_find_startcode(data, end);
  81. while (true) {
  82. while (nal_start < end && !*(nal_start++))
  83. ;
  84. if (nal_start == end)
  85. break;
  86. *priority = compute_hevc_keyframe_priority(nal_start, is_keyframe, *priority);
  87. const uint8_t *const nal_end = obs_nal_find_startcode(nal_start, end);
  88. const size_t nal_size = nal_end - nal_start;
  89. s_wb32(s, (uint32_t)nal_size);
  90. s_write(s, nal_start, nal_size);
  91. nal_start = nal_end;
  92. }
  93. }
  94. void obs_parse_hevc_packet(struct encoder_packet *hevc_packet, const struct encoder_packet *src)
  95. {
  96. struct array_output_data output;
  97. struct serializer s;
  98. long ref = 1;
  99. array_output_serializer_init(&s, &output);
  100. *hevc_packet = *src;
  101. serialize(&s, &ref, sizeof(ref));
  102. serialize_hevc_data(&s, src->data, src->size, &hevc_packet->keyframe, &hevc_packet->priority);
  103. hevc_packet->data = output.bytes.array + sizeof(ref);
  104. hevc_packet->size = output.bytes.num - sizeof(ref);
  105. hevc_packet->drop_priority = hevc_packet->priority;
  106. }
  107. int obs_parse_hevc_packet_priority(const struct encoder_packet *packet)
  108. {
  109. int priority = packet->priority;
  110. const uint8_t *const data = packet->data;
  111. const uint8_t *const end = data + packet->size;
  112. const uint8_t *nal_start = obs_nal_find_startcode(data, end);
  113. while (true) {
  114. while (nal_start < end && !*(nal_start++))
  115. ;
  116. if (nal_start == end)
  117. break;
  118. bool unused;
  119. priority = compute_hevc_keyframe_priority(nal_start, &unused, priority);
  120. nal_start = obs_nal_find_startcode(nal_start, end);
  121. }
  122. return priority;
  123. }
  124. void obs_extract_hevc_headers(const uint8_t *packet, size_t size, uint8_t **new_packet_data, size_t *new_packet_size,
  125. uint8_t **header_data, size_t *header_size, uint8_t **sei_data, size_t *sei_size)
  126. {
  127. DARRAY(uint8_t) new_packet;
  128. DARRAY(uint8_t) header;
  129. DARRAY(uint8_t) sei;
  130. const uint8_t *nal_start, *nal_end, *nal_codestart;
  131. const uint8_t *end = packet + size;
  132. da_init(new_packet);
  133. da_init(header);
  134. da_init(sei);
  135. nal_start = obs_nal_find_startcode(packet, end);
  136. nal_end = NULL;
  137. while (nal_end != end) {
  138. nal_codestart = nal_start;
  139. while (nal_start < end && !*(nal_start++))
  140. ;
  141. if (nal_start == end)
  142. break;
  143. const uint8_t type = (nal_start[0] & 0x7F) >> 1;
  144. nal_end = obs_nal_find_startcode(nal_start, end);
  145. if (!nal_end)
  146. nal_end = end;
  147. if (type == OBS_HEVC_NAL_VPS || type == OBS_HEVC_NAL_SPS || type == OBS_HEVC_NAL_PPS) {
  148. da_push_back_array(header, nal_codestart, nal_end - nal_codestart);
  149. } else if (type == OBS_HEVC_NAL_SEI_PREFIX || type == OBS_HEVC_NAL_SEI_SUFFIX) {
  150. da_push_back_array(sei, nal_codestart, nal_end - nal_codestart);
  151. } else {
  152. da_push_back_array(new_packet, nal_codestart, nal_end - nal_codestart);
  153. }
  154. nal_start = nal_end;
  155. }
  156. *new_packet_data = new_packet.array;
  157. *new_packet_size = new_packet.num;
  158. *header_data = header.array;
  159. *header_size = header.num;
  160. *sei_data = sei.array;
  161. *sei_size = sei.num;
  162. }