obs-avc.c 6.7 KB

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