obs-avc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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.h"
  15. #include "obs-avc.h"
  16. #include "util/array-serializer.h"
  17. bool obs_avc_keyframe(const uint8_t *data, size_t size)
  18. {
  19. const uint8_t *nal_start, *nal_end;
  20. const uint8_t *end = data + size;
  21. int type;
  22. nal_start = obs_avc_find_startcode(data, end);
  23. while (true) {
  24. while (nal_start < end && !*(nal_start++))
  25. ;
  26. if (nal_start == end)
  27. break;
  28. 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_avc_find_startcode(nal_start, end);
  32. nal_start = nal_end;
  33. }
  34. return false;
  35. }
  36. /* NOTE: I noticed that FFmpeg does some unusual special handling of certain
  37. * scenarios that I was unaware of, so instead of just searching for {0, 0, 1}
  38. * we'll just use the code from FFmpeg - http://www.ffmpeg.org/ */
  39. static const uint8_t *ff_avc_find_startcode_internal(const uint8_t *p,
  40. const uint8_t *end)
  41. {
  42. const uint8_t *a = p + 4 - ((intptr_t)p & 3);
  43. for (end -= 3; p < a && p < end; p++) {
  44. if (p[0] == 0 && p[1] == 0 && p[2] == 1)
  45. return p;
  46. }
  47. for (end -= 3; p < end; p += 4) {
  48. uint32_t x = *(const uint32_t *)p;
  49. if ((x - 0x01010101) & (~x) & 0x80808080) {
  50. if (p[1] == 0) {
  51. if (p[0] == 0 && p[2] == 1)
  52. return p;
  53. if (p[2] == 0 && p[3] == 1)
  54. return p + 1;
  55. }
  56. if (p[3] == 0) {
  57. if (p[2] == 0 && p[4] == 1)
  58. return p + 2;
  59. if (p[4] == 0 && p[5] == 1)
  60. return p + 3;
  61. }
  62. }
  63. }
  64. for (end += 3; p < end; p++) {
  65. if (p[0] == 0 && p[1] == 0 && p[2] == 1)
  66. return p;
  67. }
  68. return end + 3;
  69. }
  70. const uint8_t *obs_avc_find_startcode(const uint8_t *p, const uint8_t *end)
  71. {
  72. const uint8_t *out = ff_avc_find_startcode_internal(p, end);
  73. if (p < out && out < end && !out[-1])
  74. out--;
  75. return out;
  76. }
  77. static inline int get_drop_priority(int priority)
  78. {
  79. return priority;
  80. }
  81. static void serialize_avc_data(struct serializer *s, const uint8_t *data,
  82. size_t size, bool *is_keyframe, int *priority)
  83. {
  84. const uint8_t *nal_start, *nal_end;
  85. const uint8_t *end = data + size;
  86. int type;
  87. nal_start = obs_avc_find_startcode(data, end);
  88. while (true) {
  89. while (nal_start < end && !*(nal_start++))
  90. ;
  91. if (nal_start == end)
  92. break;
  93. type = nal_start[0] & 0x1F;
  94. if (type == OBS_NAL_SLICE_IDR || type == OBS_NAL_SLICE) {
  95. if (is_keyframe)
  96. *is_keyframe = (type == OBS_NAL_SLICE_IDR);
  97. if (priority)
  98. *priority = nal_start[0] >> 5;
  99. }
  100. nal_end = obs_avc_find_startcode(nal_start, end);
  101. s_wb32(s, (uint32_t)(nal_end - nal_start));
  102. s_write(s, nal_start, nal_end - nal_start);
  103. nal_start = nal_end;
  104. }
  105. }
  106. void obs_parse_avc_packet(struct encoder_packet *avc_packet,
  107. const struct encoder_packet *src)
  108. {
  109. struct array_output_data output;
  110. struct serializer s;
  111. long ref = 1;
  112. array_output_serializer_init(&s, &output);
  113. *avc_packet = *src;
  114. serialize(&s, &ref, sizeof(ref));
  115. serialize_avc_data(&s, src->data, src->size, &avc_packet->keyframe,
  116. &avc_packet->priority);
  117. avc_packet->data = output.bytes.array + sizeof(ref);
  118. avc_packet->size = output.bytes.num - sizeof(ref);
  119. avc_packet->drop_priority = get_drop_priority(avc_packet->priority);
  120. }
  121. static inline bool has_start_code(const uint8_t *data)
  122. {
  123. if (data[0] != 0 || data[1] != 0)
  124. return false;
  125. return data[2] == 1 || (data[2] == 0 && data[3] == 1);
  126. }
  127. static void get_sps_pps(const uint8_t *data, size_t size, const uint8_t **sps,
  128. size_t *sps_size, const uint8_t **pps, size_t *pps_size)
  129. {
  130. const uint8_t *nal_start, *nal_end;
  131. const uint8_t *end = data + size;
  132. int type;
  133. nal_start = obs_avc_find_startcode(data, end);
  134. while (true) {
  135. while (nal_start < end && !*(nal_start++))
  136. ;
  137. if (nal_start == end)
  138. break;
  139. nal_end = obs_avc_find_startcode(nal_start, end);
  140. type = nal_start[0] & 0x1F;
  141. if (type == OBS_NAL_SPS) {
  142. *sps = nal_start;
  143. *sps_size = nal_end - nal_start;
  144. } else if (type == OBS_NAL_PPS) {
  145. *pps = nal_start;
  146. *pps_size = nal_end - nal_start;
  147. }
  148. nal_start = nal_end;
  149. }
  150. }
  151. size_t obs_parse_avc_header(uint8_t **header, const uint8_t *data, size_t size)
  152. {
  153. struct array_output_data output;
  154. struct serializer s;
  155. const uint8_t *sps = NULL, *pps = NULL;
  156. size_t sps_size = 0, pps_size = 0;
  157. array_output_serializer_init(&s, &output);
  158. if (size <= 6)
  159. return 0;
  160. if (!has_start_code(data)) {
  161. *header = bmemdup(data, size);
  162. return size;
  163. }
  164. get_sps_pps(data, size, &sps, &sps_size, &pps, &pps_size);
  165. if (!sps || !pps || sps_size < 4)
  166. return 0;
  167. s_w8(&s, 0x01);
  168. s_write(&s, sps + 1, 3);
  169. s_w8(&s, 0xff);
  170. s_w8(&s, 0xe1);
  171. s_wb16(&s, (uint16_t)sps_size);
  172. s_write(&s, sps, sps_size);
  173. s_w8(&s, 0x01);
  174. s_wb16(&s, (uint16_t)pps_size);
  175. s_write(&s, pps, pps_size);
  176. *header = output.bytes.array;
  177. return output.bytes.num;
  178. }
  179. void obs_extract_avc_headers(const uint8_t *packet, size_t size,
  180. uint8_t **new_packet_data, size_t *new_packet_size,
  181. uint8_t **header_data, size_t *header_size,
  182. uint8_t **sei_data, size_t *sei_size)
  183. {
  184. DARRAY(uint8_t) new_packet;
  185. DARRAY(uint8_t) header;
  186. DARRAY(uint8_t) sei;
  187. const uint8_t *nal_start, *nal_end, *nal_codestart;
  188. const uint8_t *end = packet + size;
  189. int type;
  190. da_init(new_packet);
  191. da_init(header);
  192. da_init(sei);
  193. nal_start = obs_avc_find_startcode(packet, end);
  194. nal_end = NULL;
  195. while (nal_end != end) {
  196. nal_codestart = nal_start;
  197. while (nal_start < end && !*(nal_start++))
  198. ;
  199. if (nal_start == end)
  200. break;
  201. type = nal_start[0] & 0x1F;
  202. nal_end = obs_avc_find_startcode(nal_start, end);
  203. if (!nal_end)
  204. nal_end = end;
  205. if (type == OBS_NAL_SPS || type == OBS_NAL_PPS) {
  206. da_push_back_array(header, nal_codestart,
  207. nal_end - nal_codestart);
  208. } else if (type == OBS_NAL_SEI) {
  209. da_push_back_array(sei, nal_codestart,
  210. nal_end - nal_codestart);
  211. } else {
  212. da_push_back_array(new_packet, nal_codestart,
  213. nal_end - nal_codestart);
  214. }
  215. nal_start = nal_end;
  216. }
  217. *new_packet_data = new_packet.array;
  218. *new_packet_size = new_packet.num;
  219. *header_data = header.array;
  220. *header_size = header.num;
  221. *sei_data = sei.array;
  222. *sei_size = sei.num;
  223. }