obs-avc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /******************************************************************************
  2. Copyright (C) 2014 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-avc.h"
  15. #include "obs.h"
  16. #include "obs-nal.h"
  17. #include "util/array-serializer.h"
  18. bool obs_avc_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] & 0x1F;
  29. if (type == OBS_NAL_SLICE_IDR || type == OBS_NAL_SLICE)
  30. return type == OBS_NAL_SLICE_IDR;
  31. nal_end = obs_nal_find_startcode(nal_start, end);
  32. nal_start = nal_end;
  33. }
  34. return false;
  35. }
  36. const uint8_t *obs_avc_find_startcode(const uint8_t *p, const uint8_t *end)
  37. {
  38. return obs_nal_find_startcode(p, end);
  39. }
  40. static inline int get_drop_priority(int priority)
  41. {
  42. return priority;
  43. }
  44. static void serialize_avc_data(struct serializer *s, const uint8_t *data,
  45. size_t size, bool *is_keyframe, int *priority)
  46. {
  47. const uint8_t *nal_start, *nal_end;
  48. const uint8_t *end = data + size;
  49. int type;
  50. nal_start = obs_avc_find_startcode(data, end);
  51. while (true) {
  52. while (nal_start < end && !*(nal_start++))
  53. ;
  54. if (nal_start == end)
  55. break;
  56. type = nal_start[0] & 0x1F;
  57. if (type == OBS_NAL_SLICE_IDR || type == OBS_NAL_SLICE) {
  58. if (is_keyframe)
  59. *is_keyframe = (type == OBS_NAL_SLICE_IDR);
  60. if (priority)
  61. *priority = nal_start[0] >> 5;
  62. }
  63. nal_end = obs_avc_find_startcode(nal_start, end);
  64. s_wb32(s, (uint32_t)(nal_end - nal_start));
  65. s_write(s, nal_start, nal_end - nal_start);
  66. nal_start = nal_end;
  67. }
  68. }
  69. void obs_parse_avc_packet(struct encoder_packet *avc_packet,
  70. const struct encoder_packet *src)
  71. {
  72. struct array_output_data output;
  73. struct serializer s;
  74. long ref = 1;
  75. array_output_serializer_init(&s, &output);
  76. *avc_packet = *src;
  77. serialize(&s, &ref, sizeof(ref));
  78. serialize_avc_data(&s, src->data, src->size, &avc_packet->keyframe,
  79. &avc_packet->priority);
  80. avc_packet->data = output.bytes.array + sizeof(ref);
  81. avc_packet->size = output.bytes.num - sizeof(ref);
  82. avc_packet->drop_priority = get_drop_priority(avc_packet->priority);
  83. }
  84. static inline bool has_start_code(const uint8_t *data)
  85. {
  86. if (data[0] != 0 || data[1] != 0)
  87. return false;
  88. return data[2] == 1 || (data[2] == 0 && data[3] == 1);
  89. }
  90. static void get_sps_pps(const uint8_t *data, size_t size, const uint8_t **sps,
  91. size_t *sps_size, const uint8_t **pps, size_t *pps_size)
  92. {
  93. const uint8_t *nal_start, *nal_end;
  94. const uint8_t *end = data + size;
  95. int type;
  96. nal_start = obs_avc_find_startcode(data, end);
  97. while (true) {
  98. while (nal_start < end && !*(nal_start++))
  99. ;
  100. if (nal_start == end)
  101. break;
  102. nal_end = obs_avc_find_startcode(nal_start, end);
  103. type = nal_start[0] & 0x1F;
  104. if (type == OBS_NAL_SPS) {
  105. *sps = nal_start;
  106. *sps_size = nal_end - nal_start;
  107. } else if (type == OBS_NAL_PPS) {
  108. *pps = nal_start;
  109. *pps_size = nal_end - nal_start;
  110. }
  111. nal_start = nal_end;
  112. }
  113. }
  114. size_t obs_parse_avc_header(uint8_t **header, const uint8_t *data, size_t size)
  115. {
  116. struct array_output_data output;
  117. struct serializer s;
  118. const uint8_t *sps = NULL, *pps = NULL;
  119. size_t sps_size = 0, pps_size = 0;
  120. array_output_serializer_init(&s, &output);
  121. if (size <= 6)
  122. return 0;
  123. if (!has_start_code(data)) {
  124. *header = bmemdup(data, size);
  125. return size;
  126. }
  127. get_sps_pps(data, size, &sps, &sps_size, &pps, &pps_size);
  128. if (!sps || !pps || sps_size < 4)
  129. return 0;
  130. s_w8(&s, 0x01);
  131. s_write(&s, sps + 1, 3);
  132. s_w8(&s, 0xff);
  133. s_w8(&s, 0xe1);
  134. s_wb16(&s, (uint16_t)sps_size);
  135. s_write(&s, sps, sps_size);
  136. s_w8(&s, 0x01);
  137. s_wb16(&s, (uint16_t)pps_size);
  138. s_write(&s, pps, pps_size);
  139. *header = output.bytes.array;
  140. return output.bytes.num;
  141. }
  142. void obs_extract_avc_headers(const uint8_t *packet, size_t size,
  143. uint8_t **new_packet_data, size_t *new_packet_size,
  144. uint8_t **header_data, size_t *header_size,
  145. uint8_t **sei_data, size_t *sei_size)
  146. {
  147. DARRAY(uint8_t) new_packet;
  148. DARRAY(uint8_t) header;
  149. DARRAY(uint8_t) sei;
  150. const uint8_t *nal_start, *nal_end, *nal_codestart;
  151. const uint8_t *end = packet + size;
  152. da_init(new_packet);
  153. da_init(header);
  154. da_init(sei);
  155. nal_start = obs_avc_find_startcode(packet, end);
  156. nal_end = NULL;
  157. while (nal_end != end) {
  158. nal_codestart = nal_start;
  159. while (nal_start < end && !*(nal_start++))
  160. ;
  161. if (nal_start == end)
  162. break;
  163. const uint8_t type = nal_start[0] & 0x1F;
  164. nal_end = obs_avc_find_startcode(nal_start, end);
  165. if (!nal_end)
  166. nal_end = end;
  167. if (type == OBS_NAL_SPS || type == OBS_NAL_PPS) {
  168. da_push_back_array(header, nal_codestart,
  169. nal_end - nal_codestart);
  170. } else if (type == OBS_NAL_SEI) {
  171. da_push_back_array(sei, nal_codestart,
  172. nal_end - nal_codestart);
  173. } else {
  174. da_push_back_array(new_packet, nal_codestart,
  175. nal_end - nal_codestart);
  176. }
  177. nal_start = nal_end;
  178. }
  179. *new_packet_data = new_packet.array;
  180. *new_packet_size = new_packet.num;
  181. *header_data = header.array;
  182. *header_size = header.num;
  183. *sei_data = sei.array;
  184. *sei_size = sei.num;
  185. }