obs-avc.c 8.3 KB

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