obs-avc.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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. #include "util/bitstream.h"
  19. bool obs_avc_keyframe(const uint8_t *data, size_t size)
  20. {
  21. const uint8_t *nal_start, *nal_end;
  22. const uint8_t *end = data + size;
  23. nal_start = obs_nal_find_startcode(data, end);
  24. while (true) {
  25. while (nal_start < end && !*(nal_start++))
  26. ;
  27. if (nal_start == end)
  28. break;
  29. const uint8_t type = nal_start[0] & 0x1F;
  30. if (type == OBS_NAL_SLICE_IDR || type == OBS_NAL_SLICE)
  31. return type == OBS_NAL_SLICE_IDR;
  32. nal_end = obs_nal_find_startcode(nal_start, end);
  33. nal_start = nal_end;
  34. }
  35. return false;
  36. }
  37. const uint8_t *obs_avc_find_startcode(const uint8_t *p, const uint8_t *end)
  38. {
  39. return obs_nal_find_startcode(p, end);
  40. }
  41. static int compute_avc_keyframe_priority(const uint8_t *nal_start,
  42. bool *is_keyframe, int priority)
  43. {
  44. const int type = nal_start[0] & 0x1F;
  45. if (type == OBS_NAL_SLICE_IDR)
  46. *is_keyframe = true;
  47. const int new_priority = nal_start[0] >> 5;
  48. if (priority < new_priority)
  49. priority = new_priority;
  50. return priority;
  51. }
  52. static void serialize_avc_data(struct serializer *s, const uint8_t *data,
  53. size_t size, bool *is_keyframe, int *priority)
  54. {
  55. const uint8_t *const end = data + size;
  56. const uint8_t *nal_start = obs_nal_find_startcode(data, end);
  57. while (true) {
  58. while (nal_start < end && !*(nal_start++))
  59. ;
  60. if (nal_start == end)
  61. break;
  62. *priority = compute_avc_keyframe_priority(
  63. nal_start, is_keyframe, *priority);
  64. const uint8_t *const nal_end =
  65. obs_nal_find_startcode(nal_start, end);
  66. const size_t nal_size = nal_end - nal_start;
  67. s_wb32(s, (uint32_t)nal_size);
  68. s_write(s, nal_start, nal_size);
  69. nal_start = nal_end;
  70. }
  71. }
  72. void obs_parse_avc_packet(struct encoder_packet *avc_packet,
  73. const struct encoder_packet *src)
  74. {
  75. struct array_output_data output;
  76. struct serializer s;
  77. long ref = 1;
  78. array_output_serializer_init(&s, &output);
  79. *avc_packet = *src;
  80. serialize(&s, &ref, sizeof(ref));
  81. serialize_avc_data(&s, src->data, src->size, &avc_packet->keyframe,
  82. &avc_packet->priority);
  83. avc_packet->data = output.bytes.array + sizeof(ref);
  84. avc_packet->size = output.bytes.num - sizeof(ref);
  85. avc_packet->drop_priority = avc_packet->priority;
  86. }
  87. int obs_parse_avc_packet_priority(const struct encoder_packet *packet)
  88. {
  89. int priority = packet->priority;
  90. const uint8_t *const data = packet->data;
  91. const uint8_t *const end = data + packet->size;
  92. const uint8_t *nal_start = obs_nal_find_startcode(data, end);
  93. while (true) {
  94. while (nal_start < end && !*(nal_start++))
  95. ;
  96. if (nal_start == end)
  97. break;
  98. bool unused;
  99. priority = compute_avc_keyframe_priority(nal_start, &unused,
  100. priority);
  101. nal_start = obs_nal_find_startcode(nal_start, end);
  102. }
  103. return priority;
  104. }
  105. static inline bool has_start_code(const uint8_t *data)
  106. {
  107. if (data[0] != 0 || data[1] != 0)
  108. return false;
  109. return data[2] == 1 || (data[2] == 0 && data[3] == 1);
  110. }
  111. static void get_sps_pps(const uint8_t *data, size_t size, const uint8_t **sps,
  112. size_t *sps_size, const uint8_t **pps, size_t *pps_size)
  113. {
  114. const uint8_t *nal_start, *nal_end;
  115. const uint8_t *end = data + size;
  116. int type;
  117. nal_start = obs_nal_find_startcode(data, end);
  118. while (true) {
  119. while (nal_start < end && !*(nal_start++))
  120. ;
  121. if (nal_start == end)
  122. break;
  123. nal_end = obs_nal_find_startcode(nal_start, end);
  124. type = nal_start[0] & 0x1F;
  125. if (type == OBS_NAL_SPS) {
  126. *sps = nal_start;
  127. *sps_size = nal_end - nal_start;
  128. } else if (type == OBS_NAL_PPS) {
  129. *pps = nal_start;
  130. *pps_size = nal_end - nal_start;
  131. }
  132. nal_start = nal_end;
  133. }
  134. }
  135. static inline uint8_t get_ue_golomb(struct bitstream_reader *gb)
  136. {
  137. int i = 0;
  138. while (i < 32 && !bitstream_reader_read_bits(gb, 1))
  139. i++;
  140. return bitstream_reader_read_bits(gb, i) + (1 << i) - 1;
  141. }
  142. static void get_sps_high_params(const uint8_t *sps, size_t size,
  143. uint8_t *chroma_format_idc,
  144. uint8_t *bit_depth_luma,
  145. uint8_t *bit_depth_chroma)
  146. {
  147. struct bitstream_reader gb;
  148. /* Extract RBSP */
  149. uint8_t *rbsp = bzalloc(size);
  150. size_t i = 0;
  151. size_t rbsp_size = 0;
  152. while (i + 2 < size) {
  153. if (sps[i] == 0 && sps[i + 1] == 0 && sps[i + 2] == 3) {
  154. rbsp[rbsp_size++] = sps[i++];
  155. rbsp[rbsp_size++] = sps[i++];
  156. // skip emulation_prevention_three_byte
  157. i++;
  158. } else {
  159. rbsp[rbsp_size++] = sps[i++];
  160. }
  161. }
  162. while (i < size)
  163. rbsp[rbsp_size++] = sps[i++];
  164. /* Read relevant information from SPS */
  165. bitstream_reader_init(&gb, rbsp, rbsp_size);
  166. // skip a whole bunch of stuff we don't care about
  167. bitstream_reader_read_bits(&gb, 24); // profile, constraint flags, level
  168. get_ue_golomb(&gb); // id
  169. *chroma_format_idc = get_ue_golomb(&gb);
  170. // skip separate_colour_plane_flag
  171. if (*chroma_format_idc == 3)
  172. bitstream_reader_read_bits(&gb, 1);
  173. *bit_depth_luma = get_ue_golomb(&gb);
  174. *bit_depth_chroma = get_ue_golomb(&gb);
  175. bfree(rbsp);
  176. }
  177. size_t obs_parse_avc_header(uint8_t **header, const uint8_t *data, size_t size)
  178. {
  179. struct array_output_data output;
  180. struct serializer s;
  181. const uint8_t *sps = NULL, *pps = NULL;
  182. size_t sps_size = 0, pps_size = 0;
  183. array_output_serializer_init(&s, &output);
  184. if (size <= 6)
  185. return 0;
  186. if (!has_start_code(data)) {
  187. *header = bmemdup(data, size);
  188. return size;
  189. }
  190. get_sps_pps(data, size, &sps, &sps_size, &pps, &pps_size);
  191. if (!sps || !pps || sps_size < 4)
  192. return 0;
  193. s_w8(&s, 0x01);
  194. s_write(&s, sps + 1, 3);
  195. s_w8(&s, 0xff);
  196. s_w8(&s, 0xe1);
  197. s_wb16(&s, (uint16_t)sps_size);
  198. s_write(&s, sps, sps_size);
  199. s_w8(&s, 0x01);
  200. s_wb16(&s, (uint16_t)pps_size);
  201. s_write(&s, pps, pps_size);
  202. uint8_t profile_idc = sps[1];
  203. /* Additional data required for high, high10, high422, high444 profiles.
  204. * See ISO/IEC 14496-15 Section 5.3.3.1.2. */
  205. if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 ||
  206. profile_idc == 244) {
  207. uint8_t chroma_format_idc, bit_depth_luma, bit_depth_chroma;
  208. get_sps_high_params(sps + 1, sps_size - 1, &chroma_format_idc,
  209. &bit_depth_luma, &bit_depth_chroma);
  210. // reserved + chroma_format
  211. s_w8(&s, 0xfc | chroma_format_idc);
  212. // reserved + bit_depth_luma_minus8
  213. s_w8(&s, 0xf8 | bit_depth_luma);
  214. // reserved + bit_depth_chroma_minus8
  215. s_w8(&s, 0xf8 | bit_depth_chroma);
  216. // numOfSequenceParameterSetExt
  217. s_w8(&s, 0);
  218. }
  219. *header = output.bytes.array;
  220. return output.bytes.num;
  221. }
  222. void obs_extract_avc_headers(const uint8_t *packet, size_t size,
  223. uint8_t **new_packet_data, size_t *new_packet_size,
  224. uint8_t **header_data, size_t *header_size,
  225. uint8_t **sei_data, size_t *sei_size)
  226. {
  227. DARRAY(uint8_t) new_packet;
  228. DARRAY(uint8_t) header;
  229. DARRAY(uint8_t) sei;
  230. const uint8_t *nal_start, *nal_end, *nal_codestart;
  231. const uint8_t *end = packet + size;
  232. da_init(new_packet);
  233. da_init(header);
  234. da_init(sei);
  235. nal_start = obs_nal_find_startcode(packet, end);
  236. nal_end = NULL;
  237. while (nal_end != end) {
  238. nal_codestart = nal_start;
  239. while (nal_start < end && !*(nal_start++))
  240. ;
  241. if (nal_start == end)
  242. break;
  243. const uint8_t type = nal_start[0] & 0x1F;
  244. nal_end = obs_nal_find_startcode(nal_start, end);
  245. if (!nal_end)
  246. nal_end = end;
  247. if (type == OBS_NAL_SPS || type == OBS_NAL_PPS) {
  248. da_push_back_array(header, nal_codestart,
  249. nal_end - nal_codestart);
  250. } else if (type == OBS_NAL_SEI) {
  251. da_push_back_array(sei, nal_codestart,
  252. nal_end - nal_codestart);
  253. } else {
  254. da_push_back_array(new_packet, nal_codestart,
  255. nal_end - nal_codestart);
  256. }
  257. nal_start = nal_end;
  258. }
  259. *new_packet_data = new_packet.array;
  260. *new_packet_size = new_packet.num;
  261. *header_data = header.array;
  262. *header_size = header.num;
  263. *sei_data = sei.array;
  264. *sei_size = sei.num;
  265. }