obs-avc.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 int compute_avc_keyframe_priority(const uint8_t *nal_start,
  41. bool *is_keyframe, int priority)
  42. {
  43. const int type = nal_start[0] & 0x1F;
  44. if (type == OBS_NAL_SLICE_IDR)
  45. *is_keyframe = true;
  46. const int new_priority = nal_start[0] >> 5;
  47. if (priority < new_priority)
  48. priority = new_priority;
  49. return priority;
  50. }
  51. static void serialize_avc_data(struct serializer *s, const uint8_t *data,
  52. size_t size, bool *is_keyframe, int *priority)
  53. {
  54. const uint8_t *const end = data + size;
  55. const uint8_t *nal_start = obs_nal_find_startcode(data, end);
  56. while (true) {
  57. while (nal_start < end && !*(nal_start++))
  58. ;
  59. if (nal_start == end)
  60. break;
  61. *priority = compute_avc_keyframe_priority(
  62. nal_start, is_keyframe, *priority);
  63. const uint8_t *const nal_end =
  64. 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_avc_packet(struct encoder_packet *avc_packet,
  72. const struct encoder_packet *src)
  73. {
  74. struct array_output_data output;
  75. struct serializer s;
  76. long ref = 1;
  77. array_output_serializer_init(&s, &output);
  78. *avc_packet = *src;
  79. serialize(&s, &ref, sizeof(ref));
  80. serialize_avc_data(&s, src->data, src->size, &avc_packet->keyframe,
  81. &avc_packet->priority);
  82. avc_packet->data = output.bytes.array + sizeof(ref);
  83. avc_packet->size = output.bytes.num - sizeof(ref);
  84. avc_packet->drop_priority = avc_packet->priority;
  85. }
  86. int obs_parse_avc_packet_priority(const struct encoder_packet *packet)
  87. {
  88. int priority = packet->priority;
  89. const uint8_t *const data = packet->data;
  90. const uint8_t *const end = data + packet->size;
  91. const uint8_t *nal_start = obs_nal_find_startcode(data, end);
  92. while (true) {
  93. while (nal_start < end && !*(nal_start++))
  94. ;
  95. if (nal_start == end)
  96. break;
  97. bool unused;
  98. priority = compute_avc_keyframe_priority(nal_start, &unused,
  99. priority);
  100. nal_start = obs_nal_find_startcode(nal_start, end);
  101. }
  102. return priority;
  103. }
  104. static inline bool has_start_code(const uint8_t *data)
  105. {
  106. if (data[0] != 0 || data[1] != 0)
  107. return false;
  108. return data[2] == 1 || (data[2] == 0 && data[3] == 1);
  109. }
  110. static void get_sps_pps(const uint8_t *data, size_t size, const uint8_t **sps,
  111. size_t *sps_size, const uint8_t **pps, size_t *pps_size)
  112. {
  113. const uint8_t *nal_start, *nal_end;
  114. const uint8_t *end = data + size;
  115. int type;
  116. nal_start = obs_nal_find_startcode(data, end);
  117. while (true) {
  118. while (nal_start < end && !*(nal_start++))
  119. ;
  120. if (nal_start == end)
  121. break;
  122. nal_end = obs_nal_find_startcode(nal_start, end);
  123. type = nal_start[0] & 0x1F;
  124. if (type == OBS_NAL_SPS) {
  125. *sps = nal_start;
  126. *sps_size = nal_end - nal_start;
  127. } else if (type == OBS_NAL_PPS) {
  128. *pps = nal_start;
  129. *pps_size = nal_end - nal_start;
  130. }
  131. nal_start = nal_end;
  132. }
  133. }
  134. size_t obs_parse_avc_header(uint8_t **header, const uint8_t *data, size_t size)
  135. {
  136. struct array_output_data output;
  137. struct serializer s;
  138. const uint8_t *sps = NULL, *pps = NULL;
  139. size_t sps_size = 0, pps_size = 0;
  140. array_output_serializer_init(&s, &output);
  141. if (size <= 6)
  142. return 0;
  143. if (!has_start_code(data)) {
  144. *header = bmemdup(data, size);
  145. return size;
  146. }
  147. get_sps_pps(data, size, &sps, &sps_size, &pps, &pps_size);
  148. if (!sps || !pps || sps_size < 4)
  149. return 0;
  150. s_w8(&s, 0x01);
  151. s_write(&s, sps + 1, 3);
  152. s_w8(&s, 0xff);
  153. s_w8(&s, 0xe1);
  154. s_wb16(&s, (uint16_t)sps_size);
  155. s_write(&s, sps, sps_size);
  156. s_w8(&s, 0x01);
  157. s_wb16(&s, (uint16_t)pps_size);
  158. s_write(&s, pps, pps_size);
  159. *header = output.bytes.array;
  160. return output.bytes.num;
  161. }
  162. void obs_extract_avc_headers(const uint8_t *packet, size_t size,
  163. uint8_t **new_packet_data, size_t *new_packet_size,
  164. uint8_t **header_data, size_t *header_size,
  165. uint8_t **sei_data, size_t *sei_size)
  166. {
  167. DARRAY(uint8_t) new_packet;
  168. DARRAY(uint8_t) header;
  169. DARRAY(uint8_t) sei;
  170. const uint8_t *nal_start, *nal_end, *nal_codestart;
  171. const uint8_t *end = packet + size;
  172. da_init(new_packet);
  173. da_init(header);
  174. da_init(sei);
  175. nal_start = obs_nal_find_startcode(packet, end);
  176. nal_end = NULL;
  177. while (nal_end != end) {
  178. nal_codestart = nal_start;
  179. while (nal_start < end && !*(nal_start++))
  180. ;
  181. if (nal_start == end)
  182. break;
  183. const uint8_t type = nal_start[0] & 0x1F;
  184. nal_end = obs_nal_find_startcode(nal_start, end);
  185. if (!nal_end)
  186. nal_end = end;
  187. if (type == OBS_NAL_SPS || type == OBS_NAL_PPS) {
  188. da_push_back_array(header, nal_codestart,
  189. nal_end - nal_codestart);
  190. } else if (type == OBS_NAL_SEI) {
  191. da_push_back_array(sei, nal_codestart,
  192. nal_end - nal_codestart);
  193. } else {
  194. da_push_back_array(new_packet, nal_codestart,
  195. nal_end - nal_codestart);
  196. }
  197. nal_start = nal_end;
  198. }
  199. *new_packet_data = new_packet.array;
  200. *new_packet_size = new_packet.num;
  201. *header_data = header.array;
  202. *header_size = header.num;
  203. *sei_data = sei.array;
  204. *sei_size = sei.num;
  205. }