obs-hevc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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-nal.h"
  16. #include "util/darray.h"
  17. enum {
  18. OBS_HEVC_NAL_TRAIL_N = 0,
  19. OBS_HEVC_NAL_TRAIL_R = 1,
  20. OBS_HEVC_NAL_TSA_N = 2,
  21. OBS_HEVC_NAL_TSA_R = 3,
  22. OBS_HEVC_NAL_STSA_N = 4,
  23. OBS_HEVC_NAL_STSA_R = 5,
  24. OBS_HEVC_NAL_RADL_N = 6,
  25. OBS_HEVC_NAL_RADL_R = 7,
  26. OBS_HEVC_NAL_RASL_N = 8,
  27. OBS_HEVC_NAL_RASL_R = 9,
  28. OBS_HEVC_NAL_VCL_N10 = 10,
  29. OBS_HEVC_NAL_VCL_R11 = 11,
  30. OBS_HEVC_NAL_VCL_N12 = 12,
  31. OBS_HEVC_NAL_VCL_R13 = 13,
  32. OBS_HEVC_NAL_VCL_N14 = 14,
  33. OBS_HEVC_NAL_VCL_R15 = 15,
  34. OBS_HEVC_NAL_BLA_W_LP = 16,
  35. OBS_HEVC_NAL_BLA_W_RADL = 17,
  36. OBS_HEVC_NAL_BLA_N_LP = 18,
  37. OBS_HEVC_NAL_IDR_W_RADL = 19,
  38. OBS_HEVC_NAL_IDR_N_LP = 20,
  39. OBS_HEVC_NAL_CRA_NUT = 21,
  40. OBS_HEVC_NAL_RSV_IRAP_VCL22 = 22,
  41. OBS_HEVC_NAL_RSV_IRAP_VCL23 = 23,
  42. OBS_HEVC_NAL_RSV_VCL24 = 24,
  43. OBS_HEVC_NAL_RSV_VCL25 = 25,
  44. OBS_HEVC_NAL_RSV_VCL26 = 26,
  45. OBS_HEVC_NAL_RSV_VCL27 = 27,
  46. OBS_HEVC_NAL_RSV_VCL28 = 28,
  47. OBS_HEVC_NAL_RSV_VCL29 = 29,
  48. OBS_HEVC_NAL_RSV_VCL30 = 30,
  49. OBS_HEVC_NAL_RSV_VCL31 = 31,
  50. OBS_HEVC_NAL_VPS = 32,
  51. OBS_HEVC_NAL_SPS = 33,
  52. OBS_HEVC_NAL_PPS = 34,
  53. OBS_HEVC_NAL_AUD = 35,
  54. OBS_HEVC_NAL_EOS_NUT = 36,
  55. OBS_HEVC_NAL_EOB_NUT = 37,
  56. OBS_HEVC_NAL_FD_NUT = 38,
  57. OBS_HEVC_NAL_SEI_PREFIX = 39,
  58. OBS_HEVC_NAL_SEI_SUFFIX = 40,
  59. };
  60. bool obs_hevc_keyframe(const uint8_t *data, size_t size)
  61. {
  62. const uint8_t *nal_start, *nal_end;
  63. const uint8_t *end = data + size;
  64. nal_start = obs_nal_find_startcode(data, end);
  65. while (true) {
  66. while (nal_start < end && !*(nal_start++))
  67. ;
  68. if (nal_start == end)
  69. break;
  70. const uint8_t type = (nal_start[0] & 0x7F) >> 1;
  71. if (type <= OBS_HEVC_NAL_RSV_IRAP_VCL23)
  72. return type >= OBS_HEVC_NAL_BLA_W_LP;
  73. nal_end = obs_nal_find_startcode(nal_start, end);
  74. nal_start = nal_end;
  75. }
  76. return false;
  77. }
  78. void obs_extract_hevc_headers(const uint8_t *packet, size_t size,
  79. uint8_t **new_packet_data,
  80. size_t *new_packet_size, uint8_t **header_data,
  81. size_t *header_size, uint8_t **sei_data,
  82. size_t *sei_size)
  83. {
  84. DARRAY(uint8_t) new_packet;
  85. DARRAY(uint8_t) header;
  86. DARRAY(uint8_t) sei;
  87. const uint8_t *nal_start, *nal_end, *nal_codestart;
  88. const uint8_t *end = packet + size;
  89. da_init(new_packet);
  90. da_init(header);
  91. da_init(sei);
  92. nal_start = obs_nal_find_startcode(packet, end);
  93. nal_end = NULL;
  94. while (nal_end != end) {
  95. nal_codestart = nal_start;
  96. while (nal_start < end && !*(nal_start++))
  97. ;
  98. if (nal_start == end)
  99. break;
  100. const uint8_t type = (nal_start[0] & 0x7F) >> 1;
  101. nal_end = obs_nal_find_startcode(nal_start, end);
  102. if (!nal_end)
  103. nal_end = end;
  104. if (type == OBS_HEVC_NAL_VPS || type == OBS_HEVC_NAL_SPS ||
  105. type == OBS_HEVC_NAL_PPS) {
  106. da_push_back_array(header, nal_codestart,
  107. nal_end - nal_codestart);
  108. } else if (type == OBS_HEVC_NAL_SEI_PREFIX ||
  109. type == OBS_HEVC_NAL_SEI_SUFFIX) {
  110. da_push_back_array(sei, nal_codestart,
  111. nal_end - nal_codestart);
  112. } else {
  113. da_push_back_array(new_packet, nal_codestart,
  114. nal_end - nal_codestart);
  115. }
  116. nal_start = nal_end;
  117. }
  118. *new_packet_data = new_packet.array;
  119. *new_packet_size = new_packet.num;
  120. *header_data = header.array;
  121. *header_size = header.num;
  122. *sei_data = sei.array;
  123. *sei_size = sei.num;
  124. }