rtmp-hevc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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 "rtmp-hevc.h"
  15. #include "utils.h"
  16. #include <obs.h>
  17. #include <obs-nal.h>
  18. #include <obs-hevc.h>
  19. #include <util/array-serializer.h>
  20. /* Adapted from FFmpeg's libavformat/hevc.c for our FLV muxer. */
  21. enum { OBS_VPS_INDEX, OBS_SPS_INDEX, OBS_PPS_INDEX, OBS_SEI_PREFIX_INDEX, OBS_SEI_SUFFIX_INDEX, OBS_NB_ARRAYS };
  22. enum {
  23. OBS_HEVC_MAX_VPS_COUNT = 16,
  24. OBS_HEVC_MAX_SPS_COUNT = 16,
  25. OBS_HEVC_MAX_PPS_COUNT = 64,
  26. };
  27. typedef struct HVCCNALUnitArray {
  28. uint8_t array_completeness;
  29. uint8_t NAL_unit_type;
  30. uint16_t numNalus;
  31. struct array_output_data nalUnitData;
  32. struct serializer nalUnit;
  33. } HVCCNALUnitArray;
  34. typedef struct HEVCDecoderConfigurationRecord {
  35. uint8_t general_profile_space;
  36. uint8_t general_tier_flag;
  37. uint8_t general_profile_idc;
  38. uint32_t general_profile_compatibility_flags;
  39. uint64_t general_constraint_indicator_flags;
  40. uint8_t general_level_idc;
  41. uint16_t min_spatial_segmentation_idc;
  42. uint8_t parallelismType;
  43. uint8_t chromaFormat;
  44. uint8_t bitDepthLumaMinus8;
  45. uint8_t bitDepthChromaMinus8;
  46. uint16_t avgFrameRate;
  47. uint8_t constantFrameRate;
  48. uint8_t numTemporalLayers;
  49. uint8_t temporalIdNested;
  50. uint8_t lengthSizeMinusOne;
  51. uint8_t numOfArrays;
  52. HVCCNALUnitArray arrays[OBS_NB_ARRAYS];
  53. } HEVCDecoderConfigurationRecord;
  54. typedef struct HVCCProfileTierLevel {
  55. uint8_t profile_space;
  56. uint8_t tier_flag;
  57. uint8_t profile_idc;
  58. uint32_t profile_compatibility_flags;
  59. uint64_t constraint_indicator_flags;
  60. uint8_t level_idc;
  61. } HVCCProfileTierLevel;
  62. typedef struct HevcGetBitContext {
  63. const uint8_t *buffer, *buffer_end;
  64. uint64_t cache;
  65. unsigned bits_left;
  66. int index;
  67. int size_in_bits;
  68. int size_in_bits_plus8;
  69. } HevcGetBitContext;
  70. static inline uint32_t rb32(const uint8_t *ptr)
  71. {
  72. return (ptr[0] << 24) + (ptr[1] << 16) + (ptr[2] << 8) + ptr[3];
  73. }
  74. static inline void refill_32(HevcGetBitContext *s)
  75. {
  76. s->cache = s->cache | (uint64_t)rb32(s->buffer + (s->index >> 3)) << (32 - s->bits_left);
  77. s->index += 32;
  78. s->bits_left += 32;
  79. }
  80. static inline uint64_t get_val(HevcGetBitContext *s, unsigned n)
  81. {
  82. uint64_t ret;
  83. ret = s->cache >> (64 - n);
  84. s->cache <<= n;
  85. s->bits_left -= n;
  86. return ret;
  87. }
  88. static inline int init_get_bits_xe(HevcGetBitContext *s, const uint8_t *buffer, int bit_size)
  89. {
  90. int buffer_size;
  91. int ret = 0;
  92. if (bit_size >= INT_MAX - 64 * 8 || bit_size < 0 || !buffer) {
  93. bit_size = 0;
  94. buffer = NULL;
  95. ret = -1;
  96. }
  97. buffer_size = (bit_size + 7) >> 3;
  98. s->buffer = buffer;
  99. s->size_in_bits = bit_size;
  100. s->size_in_bits_plus8 = bit_size + 8;
  101. s->buffer_end = buffer + buffer_size;
  102. s->index = 0;
  103. s->cache = 0;
  104. s->bits_left = 0;
  105. refill_32(s);
  106. return ret;
  107. }
  108. static inline int init_get_bits(HevcGetBitContext *s, const uint8_t *buffer, int bit_size)
  109. {
  110. return init_get_bits_xe(s, buffer, bit_size);
  111. }
  112. static inline int init_get_bits8(HevcGetBitContext *s, const uint8_t *buffer, int byte_size)
  113. {
  114. if (byte_size > INT_MAX / 8 || byte_size < 0)
  115. byte_size = -1;
  116. return init_get_bits(s, buffer, byte_size * 8);
  117. }
  118. static inline unsigned int get_bits(HevcGetBitContext *s, unsigned int n)
  119. {
  120. register unsigned int tmp;
  121. if (n > s->bits_left) {
  122. refill_32(s);
  123. }
  124. tmp = (unsigned int)get_val(s, n);
  125. return tmp;
  126. }
  127. #define skip_bits get_bits
  128. static inline int get_bits_count(HevcGetBitContext *s)
  129. {
  130. return s->index - s->bits_left;
  131. }
  132. static inline int get_bits_left(HevcGetBitContext *gb)
  133. {
  134. return gb->size_in_bits - get_bits_count(gb);
  135. }
  136. static inline unsigned int get_bits_long(HevcGetBitContext *s, int n)
  137. {
  138. if (!n)
  139. return 0;
  140. return get_bits(s, n);
  141. }
  142. #define skip_bits_long get_bits_long
  143. static inline uint64_t get_bits64(HevcGetBitContext *s, int n)
  144. {
  145. if (n <= 32) {
  146. return get_bits_long(s, n);
  147. } else {
  148. uint64_t ret = (uint64_t)get_bits_long(s, n - 32) << 32;
  149. return ret | get_bits_long(s, 32);
  150. }
  151. }
  152. static inline int ilog2(unsigned x)
  153. {
  154. return (31 - clz32(x | 1));
  155. }
  156. static inline unsigned show_val(const HevcGetBitContext *s, unsigned n)
  157. {
  158. return s->cache & ((UINT64_C(1) << n) - 1);
  159. }
  160. static inline unsigned int show_bits(HevcGetBitContext *s, unsigned int n)
  161. {
  162. register unsigned int tmp;
  163. if (n > s->bits_left)
  164. refill_32(s);
  165. tmp = show_val(s, n);
  166. return tmp;
  167. }
  168. static inline unsigned int show_bits_long(HevcGetBitContext *s, int n)
  169. {
  170. if (n <= 25) {
  171. return show_bits(s, n);
  172. } else {
  173. HevcGetBitContext gb = *s;
  174. return get_bits_long(&gb, n);
  175. }
  176. }
  177. static inline unsigned get_ue_golomb_long(HevcGetBitContext *gb)
  178. {
  179. unsigned buf, log;
  180. buf = show_bits_long(gb, 32);
  181. log = 31 - ilog2(buf);
  182. skip_bits_long(gb, log);
  183. return get_bits_long(gb, log + 1) - 1;
  184. }
  185. static inline int get_se_golomb_long(HevcGetBitContext *gb)
  186. {
  187. unsigned int buf = get_ue_golomb_long(gb);
  188. int sign = (buf & 1) - 1;
  189. return ((buf >> 1) ^ sign) + 1;
  190. }
  191. static inline bool has_start_code(const uint8_t *data, size_t size)
  192. {
  193. if (size > 3 && data[0] == 0 && data[1] == 0 && data[2] == 1)
  194. return true;
  195. if (size > 4 && data[0] == 0 && data[1] == 0 && data[2] == 0 && data[3] == 1)
  196. return true;
  197. return false;
  198. }
  199. uint8_t *ff_nal_unit_extract_rbsp(uint8_t *dst, const uint8_t *src, int src_len, uint32_t *dst_len, int header_len)
  200. {
  201. int i, len;
  202. /* NAL unit header */
  203. i = len = 0;
  204. while (i < header_len && i < src_len)
  205. dst[len++] = src[i++];
  206. while (i + 2 < src_len)
  207. if (!src[i] && !src[i + 1] && src[i + 2] == 3) {
  208. dst[len++] = src[i++];
  209. dst[len++] = src[i++];
  210. i++; // remove emulation_prevention_three_byte
  211. } else
  212. dst[len++] = src[i++];
  213. while (i < src_len)
  214. dst[len++] = src[i++];
  215. memset(dst + len, 0, 64);
  216. *dst_len = (uint32_t)len;
  217. return dst;
  218. }
  219. static int hvcc_array_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size, uint8_t nal_type, int ps_array_completeness,
  220. HVCCNALUnitArray *array)
  221. {
  222. s_wb16(&array->nalUnit, nal_size);
  223. s_write(&array->nalUnit, nal_buf, nal_size);
  224. array->NAL_unit_type = nal_type;
  225. array->numNalus++;
  226. if (nal_type == OBS_HEVC_NAL_VPS || nal_type == OBS_HEVC_NAL_SPS || nal_type == OBS_HEVC_NAL_PPS)
  227. array->array_completeness = ps_array_completeness;
  228. return 0;
  229. }
  230. static void nal_unit_parse_header(HevcGetBitContext *gb, uint8_t *nal_type)
  231. {
  232. skip_bits(gb, 1); // forbidden_zero_bit
  233. *nal_type = get_bits(gb, 6);
  234. get_bits(gb, 9);
  235. }
  236. static void hvcc_update_ptl(HEVCDecoderConfigurationRecord *hvcc, HVCCProfileTierLevel *ptl)
  237. {
  238. hvcc->general_profile_space = ptl->profile_space;
  239. if (hvcc->general_tier_flag < ptl->tier_flag)
  240. hvcc->general_level_idc = ptl->level_idc;
  241. else
  242. hvcc->general_level_idc = max_u8(hvcc->general_level_idc, ptl->level_idc);
  243. hvcc->general_tier_flag = max_u8(hvcc->general_tier_flag, ptl->tier_flag);
  244. hvcc->general_profile_idc = max_u8(hvcc->general_profile_idc, ptl->profile_idc);
  245. hvcc->general_profile_compatibility_flags &= ptl->profile_compatibility_flags;
  246. hvcc->general_constraint_indicator_flags &= ptl->constraint_indicator_flags;
  247. }
  248. static void hvcc_parse_ptl(HevcGetBitContext *gb, HEVCDecoderConfigurationRecord *hvcc,
  249. unsigned int max_sub_layers_minus1)
  250. {
  251. unsigned int i;
  252. HVCCProfileTierLevel general_ptl;
  253. uint8_t sub_layer_profile_present_flag[7]; // max sublayers
  254. uint8_t sub_layer_level_present_flag[7]; // max sublayers
  255. general_ptl.profile_space = get_bits(gb, 2);
  256. general_ptl.tier_flag = get_bits(gb, 1);
  257. general_ptl.profile_idc = get_bits(gb, 5);
  258. general_ptl.profile_compatibility_flags = get_bits_long(gb, 32);
  259. general_ptl.constraint_indicator_flags = get_bits64(gb, 48);
  260. general_ptl.level_idc = get_bits(gb, 8);
  261. hvcc_update_ptl(hvcc, &general_ptl);
  262. for (i = 0; i < max_sub_layers_minus1; i++) {
  263. sub_layer_profile_present_flag[i] = get_bits(gb, 1);
  264. sub_layer_level_present_flag[i] = get_bits(gb, 1);
  265. }
  266. // skip the rest
  267. if (max_sub_layers_minus1 > 0)
  268. for (i = max_sub_layers_minus1; i < 8; i++)
  269. skip_bits(gb, 2);
  270. for (i = 0; i < max_sub_layers_minus1; i++) {
  271. if (sub_layer_profile_present_flag[i]) {
  272. skip_bits_long(gb, 32);
  273. skip_bits_long(gb, 32);
  274. skip_bits(gb, 24);
  275. }
  276. if (sub_layer_level_present_flag[i])
  277. skip_bits(gb, 8);
  278. }
  279. }
  280. static int hvcc_parse_vps(HevcGetBitContext *gb, HEVCDecoderConfigurationRecord *hvcc)
  281. {
  282. unsigned int vps_max_sub_layers_minus1;
  283. skip_bits(gb, 12);
  284. vps_max_sub_layers_minus1 = get_bits(gb, 3);
  285. hvcc->numTemporalLayers = max_u8(hvcc->numTemporalLayers, vps_max_sub_layers_minus1 + 1);
  286. skip_bits(gb, 17);
  287. hvcc_parse_ptl(gb, hvcc, vps_max_sub_layers_minus1);
  288. return 0;
  289. }
  290. #define HEVC_MAX_SHORT_TERM_REF_PIC_SETS 64
  291. static void skip_scaling_list_data(HevcGetBitContext *gb)
  292. {
  293. int i, j, k, num_coeffs;
  294. for (i = 0; i < 4; i++) {
  295. for (j = 0; j < (i == 3 ? 2 : 6); j++) {
  296. if (!get_bits(gb, 1))
  297. get_ue_golomb_long(gb);
  298. else {
  299. num_coeffs = min_i32(64, 1 << (4 + (i << 1)));
  300. if (i > 1)
  301. get_se_golomb_long(gb);
  302. for (k = 0; k < num_coeffs; k++)
  303. get_se_golomb_long(gb);
  304. }
  305. }
  306. }
  307. }
  308. static int parse_rps(HevcGetBitContext *gb, unsigned int rps_idx, unsigned int num_rps,
  309. unsigned int num_delta_pocs[HEVC_MAX_SHORT_TERM_REF_PIC_SETS])
  310. {
  311. unsigned int i;
  312. if (rps_idx && get_bits(gb, 1)) { // inter_ref_pic_set_prediction_flag
  313. /* this should only happen for slice headers, and this isn't one */
  314. if (rps_idx >= num_rps)
  315. return -1;
  316. get_bits(gb, 1); // delta_rps_sign
  317. get_ue_golomb_long(gb); // abs_delta_rps_minus1
  318. num_delta_pocs[rps_idx] = 0;
  319. for (i = 0; i <= num_delta_pocs[rps_idx - 1]; i++) {
  320. uint8_t use_delta_flag = 0;
  321. uint8_t used_by_curr_pic_flag = get_bits(gb, 1);
  322. if (!used_by_curr_pic_flag)
  323. use_delta_flag = get_bits(gb, 1);
  324. if (used_by_curr_pic_flag || use_delta_flag)
  325. num_delta_pocs[rps_idx]++;
  326. }
  327. } else {
  328. unsigned int num_negative_pics = get_ue_golomb_long(gb);
  329. unsigned int num_positive_pics = get_ue_golomb_long(gb);
  330. if ((num_positive_pics + (uint64_t)num_negative_pics) * 2 > (uint64_t)get_bits_left(gb))
  331. return -1;
  332. num_delta_pocs[rps_idx] = num_negative_pics + num_positive_pics;
  333. for (i = 0; i < num_negative_pics; i++) {
  334. get_ue_golomb_long(gb); // delta_poc_s0_minus1[rps_idx]
  335. get_bits(gb, 1); // used_by_curr_pic_s0_flag[rps_idx]
  336. }
  337. for (i = 0; i < num_positive_pics; i++) {
  338. get_ue_golomb_long(gb); // delta_poc_s1_minus1[rps_idx]
  339. get_bits(gb, 1); // used_by_curr_pic_s1_flag[rps_idx]
  340. }
  341. }
  342. return 0;
  343. }
  344. static void skip_sub_layer_hrd_parameters(HevcGetBitContext *gb, unsigned int cpb_cnt_minus1,
  345. uint8_t sub_pic_hrd_params_present_flag)
  346. {
  347. unsigned int i;
  348. for (i = 0; i <= cpb_cnt_minus1; i++) {
  349. get_ue_golomb_long(gb); // bit_rate_value_minus1
  350. get_ue_golomb_long(gb); // cpb_size_value_minus1
  351. if (sub_pic_hrd_params_present_flag) {
  352. get_ue_golomb_long(gb); // cpb_size_du_value_minus1
  353. get_ue_golomb_long(gb); // bit_rate_du_value_minus1
  354. }
  355. get_bits(gb, 1); // cbr_flag
  356. }
  357. }
  358. static int skip_hrd_parameters(HevcGetBitContext *gb, uint8_t cprms_present_flag, unsigned int max_sub_layers_minus1)
  359. {
  360. unsigned int i;
  361. uint8_t sub_pic_hrd_params_present_flag = 0;
  362. uint8_t nal_hrd_parameters_present_flag = 0;
  363. uint8_t vcl_hrd_parameters_present_flag = 0;
  364. if (cprms_present_flag) {
  365. nal_hrd_parameters_present_flag = get_bits(gb, 1);
  366. vcl_hrd_parameters_present_flag = get_bits(gb, 1);
  367. if (nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag) {
  368. sub_pic_hrd_params_present_flag = get_bits(gb, 1);
  369. if (sub_pic_hrd_params_present_flag)
  370. get_bits(gb, 19);
  371. get_bits(gb, 8);
  372. if (sub_pic_hrd_params_present_flag)
  373. get_bits(gb, 4);
  374. get_bits(gb, 15);
  375. }
  376. }
  377. for (i = 0; i <= max_sub_layers_minus1; i++) {
  378. unsigned int cpb_cnt_minus1 = 0;
  379. uint8_t low_delay_hrd_flag = 0;
  380. uint8_t fixed_pic_rate_within_cvs_flag = 0;
  381. uint8_t fixed_pic_rate_general_flag = get_bits(gb, 1);
  382. if (!fixed_pic_rate_general_flag)
  383. fixed_pic_rate_within_cvs_flag = get_bits(gb, 1);
  384. if (fixed_pic_rate_within_cvs_flag)
  385. get_ue_golomb_long(gb);
  386. else
  387. low_delay_hrd_flag = get_bits(gb, 1);
  388. if (!low_delay_hrd_flag) {
  389. cpb_cnt_minus1 = get_ue_golomb_long(gb);
  390. if (cpb_cnt_minus1 > 31)
  391. return -1;
  392. }
  393. if (nal_hrd_parameters_present_flag)
  394. skip_sub_layer_hrd_parameters(gb, cpb_cnt_minus1, sub_pic_hrd_params_present_flag);
  395. if (vcl_hrd_parameters_present_flag)
  396. skip_sub_layer_hrd_parameters(gb, cpb_cnt_minus1, sub_pic_hrd_params_present_flag);
  397. }
  398. return 0;
  399. }
  400. static void hvcc_parse_vui(HevcGetBitContext *gb, HEVCDecoderConfigurationRecord *hvcc,
  401. unsigned int max_sub_layers_minus1)
  402. {
  403. unsigned int min_spatial_segmentation_idc;
  404. if (get_bits(gb, 1)) // aspect_ratio_info_present_flag
  405. if (get_bits(gb, 8) == 255) // aspect_ratio_idc
  406. get_bits_long(gb,
  407. 32); // sar_width u(16), sar_height u(16)
  408. if (get_bits(gb, 1)) // overscan_info_present_flag
  409. get_bits(gb, 1); // overscan_appropriate_flag
  410. if (get_bits(gb, 1)) { // video_signal_type_present_flag
  411. get_bits(gb,
  412. 4); // video_format u(3), video_full_range_flag u(1)
  413. if (get_bits(gb, 1)) // colour_description_present_flag
  414. get_bits(gb, 24);
  415. }
  416. if (get_bits(gb, 1)) {
  417. get_ue_golomb_long(gb);
  418. get_ue_golomb_long(gb);
  419. }
  420. get_bits(gb, 3);
  421. if (get_bits(gb, 1)) { // default_display_window_flag
  422. get_ue_golomb_long(gb); // def_disp_win_left_offset
  423. get_ue_golomb_long(gb); // def_disp_win_right_offset
  424. get_ue_golomb_long(gb); // def_disp_win_top_offset
  425. get_ue_golomb_long(gb); // def_disp_win_bottom_offset
  426. }
  427. if (get_bits(gb, 1)) { // vui_timing_info_present_flag
  428. // skip timing info
  429. get_bits_long(gb, 32); // num_units_in_tick
  430. get_bits_long(gb, 32); // time_scale
  431. if (get_bits(gb, 1)) // poc_proportional_to_timing_flag
  432. get_ue_golomb_long(gb); // num_ticks_poc_diff_one_minus1
  433. if (get_bits(gb, 1)) // vui_hrd_parameters_present_flag
  434. skip_hrd_parameters(gb, 1, max_sub_layers_minus1);
  435. }
  436. if (get_bits(gb, 1)) { // bitstream_restriction_flag
  437. get_bits(gb, 3);
  438. min_spatial_segmentation_idc = get_ue_golomb_long(gb);
  439. hvcc->min_spatial_segmentation_idc =
  440. min_u16(hvcc->min_spatial_segmentation_idc, min_spatial_segmentation_idc);
  441. get_ue_golomb_long(gb); // max_bytes_per_pic_denom
  442. get_ue_golomb_long(gb); // max_bits_per_min_cu_denom
  443. get_ue_golomb_long(gb); // log2_max_mv_length_horizontal
  444. get_ue_golomb_long(gb); // log2_max_mv_length_vertical
  445. }
  446. }
  447. static int hvcc_parse_sps(HevcGetBitContext *gb, HEVCDecoderConfigurationRecord *hvcc)
  448. {
  449. unsigned int i, sps_max_sub_layers_minus1, log2_max_pic_order_cnt_lsb_minus4;
  450. unsigned int num_short_term_ref_pic_sets, num_delta_pocs[HEVC_MAX_SHORT_TERM_REF_PIC_SETS];
  451. get_bits(gb, 4); // sps_video_parameter_set_id
  452. sps_max_sub_layers_minus1 = get_bits(gb, 3);
  453. hvcc->numTemporalLayers = max_u8(hvcc->numTemporalLayers, sps_max_sub_layers_minus1 + 1);
  454. hvcc->temporalIdNested = get_bits(gb, 1);
  455. hvcc_parse_ptl(gb, hvcc, sps_max_sub_layers_minus1);
  456. get_ue_golomb_long(gb); // sps_seq_parameter_set_id
  457. hvcc->chromaFormat = get_ue_golomb_long(gb);
  458. if (hvcc->chromaFormat == 3)
  459. get_bits(gb, 1); // separate_colour_plane_flag
  460. get_ue_golomb_long(gb); // pic_width_in_luma_samples
  461. get_ue_golomb_long(gb); // pic_height_in_luma_samples
  462. if (get_bits(gb, 1)) { // conformance_window_flag
  463. get_ue_golomb_long(gb); // conf_win_left_offset
  464. get_ue_golomb_long(gb); // conf_win_right_offset
  465. get_ue_golomb_long(gb); // conf_win_top_offset
  466. get_ue_golomb_long(gb); // conf_win_bottom_offset
  467. }
  468. hvcc->bitDepthLumaMinus8 = get_ue_golomb_long(gb);
  469. hvcc->bitDepthChromaMinus8 = get_ue_golomb_long(gb);
  470. log2_max_pic_order_cnt_lsb_minus4 = get_ue_golomb_long(gb);
  471. /* sps_sub_layer_ordering_info_present_flag */
  472. i = get_bits(gb, 1) ? 0 : sps_max_sub_layers_minus1;
  473. for (; i <= sps_max_sub_layers_minus1; i++) {
  474. get_ue_golomb_long(gb); // max_dec_pic_buffering_minus1
  475. get_ue_golomb_long(gb); // max_num_reorder_pics
  476. get_ue_golomb_long(gb); // max_latency_increase_plus1
  477. }
  478. get_ue_golomb_long(gb); // log2_min_luma_coding_block_size_minus3
  479. get_ue_golomb_long(gb); // log2_diff_max_min_luma_coding_block_size
  480. get_ue_golomb_long(gb); // log2_min_transform_block_size_minus2
  481. get_ue_golomb_long(gb); // log2_diff_max_min_transform_block_size
  482. get_ue_golomb_long(gb); // max_transform_hierarchy_depth_inter
  483. get_ue_golomb_long(gb); // max_transform_hierarchy_depth_intra
  484. if (get_bits(gb, 1) && // scaling_list_enabled_flag
  485. get_bits(gb, 1)) // sps_scaling_list_data_present_flag
  486. skip_scaling_list_data(gb);
  487. get_bits(gb, 1); // amp_enabled_flag
  488. get_bits(gb, 1); // sample_adaptive_offset_enabled_flag
  489. if (get_bits(gb, 1)) { // pcm_enabled_flag
  490. get_bits(gb, 4); // pcm_sample_bit_depth_luma_minus1
  491. get_bits(gb, 4); // pcm_sample_bit_depth_chroma_minus1
  492. get_ue_golomb_long(gb); // log2_min_pcm_luma_coding_block_size_minus3
  493. get_ue_golomb_long(gb); // log2_diff_max_min_pcm_luma_coding_block_size
  494. get_bits(gb, 1); // pcm_loop_filter_disabled_flag
  495. }
  496. num_short_term_ref_pic_sets = get_ue_golomb_long(gb);
  497. if (num_short_term_ref_pic_sets > HEVC_MAX_SHORT_TERM_REF_PIC_SETS)
  498. return -1;
  499. for (i = 0; i < num_short_term_ref_pic_sets; i++) {
  500. int ret = parse_rps(gb, i, num_short_term_ref_pic_sets, num_delta_pocs);
  501. if (ret < 0)
  502. return ret;
  503. }
  504. if (get_bits(gb, 1)) { // long_term_ref_pics_present_flag
  505. unsigned num_long_term_ref_pics_sps = get_ue_golomb_long(gb);
  506. if (num_long_term_ref_pics_sps > 31U)
  507. return -1;
  508. for (i = 0; i < num_long_term_ref_pics_sps; i++) { // num_long_term_ref_pics_sps
  509. int len = min_i32(log2_max_pic_order_cnt_lsb_minus4 + 4, 16);
  510. get_bits(gb, len); // lt_ref_pic_poc_lsb_sps[i]
  511. get_bits(gb, 1); // used_by_curr_pic_lt_sps_flag[i]
  512. }
  513. }
  514. get_bits(gb, 1); // sps_temporal_mvp_enabled_flag
  515. get_bits(gb, 1); // strong_intra_smoothing_enabled_flag
  516. if (get_bits(gb, 1)) // vui_parameters_present_flag
  517. hvcc_parse_vui(gb, hvcc, sps_max_sub_layers_minus1);
  518. /* nothing useful for hvcC past this point */
  519. return 0;
  520. }
  521. static int hvcc_parse_pps(HevcGetBitContext *gb, HEVCDecoderConfigurationRecord *hvcc)
  522. {
  523. uint8_t tiles_enabled_flag, entropy_coding_sync_enabled_flag;
  524. get_ue_golomb_long(gb); // pps_pic_parameter_set_id
  525. get_ue_golomb_long(gb); // pps_seq_parameter_set_id
  526. get_bits(gb, 7);
  527. get_ue_golomb_long(gb); // num_ref_idx_l0_default_active_minus1
  528. get_ue_golomb_long(gb); // num_ref_idx_l1_default_active_minus1
  529. get_se_golomb_long(gb); // init_qp_minus26
  530. get_bits(gb, 2);
  531. if (get_bits(gb, 1)) // cu_qp_delta_enabled_flag
  532. get_ue_golomb_long(gb); // diff_cu_qp_delta_depth
  533. get_se_golomb_long(gb); // pps_cb_qp_offset
  534. get_se_golomb_long(gb); // pps_cr_qp_offset
  535. get_bits(gb, 4);
  536. tiles_enabled_flag = get_bits(gb, 1);
  537. entropy_coding_sync_enabled_flag = get_bits(gb, 1);
  538. if (entropy_coding_sync_enabled_flag && tiles_enabled_flag)
  539. hvcc->parallelismType = 0; // mixed-type parallel decoding
  540. else if (entropy_coding_sync_enabled_flag)
  541. hvcc->parallelismType = 3; // wavefront-based parallel decoding
  542. else if (tiles_enabled_flag)
  543. hvcc->parallelismType = 2; // tile-based parallel decoding
  544. else
  545. hvcc->parallelismType = 1; // slice-based parallel decoding
  546. /* nothing useful for hvcC past this point */
  547. return 0;
  548. }
  549. static int hvcc_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size, int ps_array_completeness,
  550. HEVCDecoderConfigurationRecord *hvcc, unsigned array_idx)
  551. {
  552. int ret = 0;
  553. HevcGetBitContext gbc;
  554. uint8_t nal_type;
  555. uint8_t *rbsp_buf;
  556. uint32_t rbsp_size;
  557. uint8_t *dst;
  558. dst = bmalloc(nal_size + 64);
  559. rbsp_buf = ff_nal_unit_extract_rbsp(dst, nal_buf, nal_size, &rbsp_size, 2);
  560. if (!rbsp_buf) {
  561. ret = -1;
  562. goto end;
  563. }
  564. ret = init_get_bits8(&gbc, rbsp_buf, rbsp_size);
  565. if (ret < 0)
  566. goto end;
  567. nal_unit_parse_header(&gbc, &nal_type);
  568. ret = hvcc_array_add_nal_unit(nal_buf, nal_size, nal_type, ps_array_completeness, &hvcc->arrays[array_idx]);
  569. if (ret < 0)
  570. goto end;
  571. if (hvcc->arrays[array_idx].numNalus == 1)
  572. hvcc->numOfArrays++;
  573. if (nal_type == OBS_HEVC_NAL_VPS)
  574. ret = hvcc_parse_vps(&gbc, hvcc);
  575. else if (nal_type == OBS_HEVC_NAL_SPS)
  576. ret = hvcc_parse_sps(&gbc, hvcc);
  577. else if (nal_type == OBS_HEVC_NAL_PPS)
  578. ret = hvcc_parse_pps(&gbc, hvcc);
  579. if (ret < 0)
  580. goto end;
  581. end:
  582. bfree(dst);
  583. return ret;
  584. }
  585. size_t obs_parse_hevc_header(uint8_t **header, const uint8_t *data, size_t size)
  586. {
  587. const uint8_t *start;
  588. const uint8_t *end;
  589. if (!has_start_code(data, size)) {
  590. *header = bmemdup(data, size);
  591. return size;
  592. }
  593. if (size < 6)
  594. return 0; // invalid
  595. if (*data == 1) { // already hvcC-formatted
  596. *header = bmemdup(data, size);
  597. return size;
  598. }
  599. struct array_output_data nals;
  600. struct serializer sn;
  601. array_output_serializer_init(&sn, &nals);
  602. const uint8_t *nal_start, *nal_end;
  603. start = data;
  604. end = data + size;
  605. size = 0; // reset size
  606. nal_start = obs_nal_find_startcode(start, end);
  607. for (;;) {
  608. while (nal_start < end && !*(nal_start++))
  609. ;
  610. if (nal_start == end)
  611. break;
  612. nal_end = obs_nal_find_startcode(nal_start, end);
  613. assert(nal_end - nal_start <= INT_MAX);
  614. s_wb32(&sn, (uint32_t)(nal_end - nal_start));
  615. s_write(&sn, nal_start, nal_end - nal_start);
  616. size += 4 + nal_end - nal_start;
  617. nal_start = nal_end;
  618. }
  619. if (size == 0)
  620. goto done;
  621. start = nals.bytes.array;
  622. end = nals.bytes.array + nals.bytes.num;
  623. // HVCC init
  624. HEVCDecoderConfigurationRecord hvcc;
  625. memset(&hvcc, 0, sizeof(HEVCDecoderConfigurationRecord));
  626. hvcc.lengthSizeMinusOne = 3; // 4 bytes
  627. hvcc.general_profile_compatibility_flags = 0xffffffff; // all bits set
  628. hvcc.general_constraint_indicator_flags = 0xffffffffffff; // all bits set
  629. hvcc.min_spatial_segmentation_idc = 4096 + 1; // assume invalid value
  630. for (unsigned i = 0; i < OBS_NB_ARRAYS; i++) {
  631. HVCCNALUnitArray *const array = &hvcc.arrays[i];
  632. array_output_serializer_init(&array->nalUnit, &array->nalUnitData);
  633. }
  634. uint8_t *buf = (uint8_t *)start;
  635. while (end - buf > 4) {
  636. uint32_t len = rb32(buf);
  637. assert((end - buf - 4) <= INT_MAX);
  638. len = min_u32(len, (uint32_t)(end - buf - 4));
  639. uint8_t type = (buf[4] >> 1) & 0x3f;
  640. buf += 4;
  641. for (unsigned i = 0; i < OBS_NB_ARRAYS; i++) {
  642. static const uint8_t array_idx_to_type[] = {OBS_HEVC_NAL_VPS, OBS_HEVC_NAL_SPS,
  643. OBS_HEVC_NAL_PPS, OBS_HEVC_NAL_SEI_PREFIX,
  644. OBS_HEVC_NAL_SEI_SUFFIX};
  645. if (type == array_idx_to_type[i]) {
  646. int ps_array_completeness = 1;
  647. int ret = hvcc_add_nal_unit(buf, len, ps_array_completeness, &hvcc, i);
  648. if (ret < 0)
  649. goto free;
  650. break;
  651. }
  652. }
  653. buf += len;
  654. }
  655. // write hvcc data
  656. uint16_t vps_count, sps_count, pps_count;
  657. if (hvcc.min_spatial_segmentation_idc > 4096) // invalid?
  658. hvcc.min_spatial_segmentation_idc = 0;
  659. if (!hvcc.min_spatial_segmentation_idc)
  660. hvcc.parallelismType = 0;
  661. hvcc.avgFrameRate = 0;
  662. hvcc.constantFrameRate = 0;
  663. vps_count = hvcc.arrays[OBS_VPS_INDEX].numNalus;
  664. sps_count = hvcc.arrays[OBS_SPS_INDEX].numNalus;
  665. pps_count = hvcc.arrays[OBS_PPS_INDEX].numNalus;
  666. if (!vps_count || vps_count > OBS_HEVC_MAX_VPS_COUNT || !sps_count || sps_count > OBS_HEVC_MAX_SPS_COUNT ||
  667. !pps_count || pps_count > OBS_HEVC_MAX_PPS_COUNT)
  668. goto free;
  669. struct array_output_data output;
  670. struct serializer s;
  671. array_output_serializer_init(&s, &output);
  672. s_w8(&s, 1); // configurationVersion, always 1
  673. s_w8(&s, hvcc.general_profile_space << 6 | hvcc.general_tier_flag << 5 | hvcc.general_profile_idc);
  674. s_wb32(&s, hvcc.general_profile_compatibility_flags);
  675. s_wb32(&s, (uint32_t)(hvcc.general_constraint_indicator_flags >> 16));
  676. s_wb16(&s, (uint16_t)(hvcc.general_constraint_indicator_flags));
  677. s_w8(&s, hvcc.general_level_idc);
  678. s_wb16(&s, hvcc.min_spatial_segmentation_idc | 0xf000);
  679. s_w8(&s, hvcc.parallelismType | 0xfc);
  680. s_w8(&s, hvcc.chromaFormat | 0xfc);
  681. s_w8(&s, hvcc.bitDepthLumaMinus8 | 0xf8);
  682. s_w8(&s, hvcc.bitDepthChromaMinus8 | 0xf8);
  683. s_wb16(&s, hvcc.avgFrameRate);
  684. s_w8(&s, hvcc.constantFrameRate << 6 | hvcc.numTemporalLayers << 3 | hvcc.temporalIdNested << 2 |
  685. hvcc.lengthSizeMinusOne);
  686. s_w8(&s, hvcc.numOfArrays);
  687. for (unsigned i = 0; i < OBS_NB_ARRAYS; i++) {
  688. const HVCCNALUnitArray *const array = &hvcc.arrays[i];
  689. if (!array->numNalus)
  690. continue;
  691. s_w8(&s, (array->array_completeness << 7) | (array->NAL_unit_type & 0x3f));
  692. s_wb16(&s, array->numNalus);
  693. s_write(&s, array->nalUnitData.bytes.array, array->nalUnitData.bytes.num);
  694. }
  695. *header = output.bytes.array;
  696. size = output.bytes.num;
  697. free:
  698. for (unsigned i = 0; i < OBS_NB_ARRAYS; i++) {
  699. HVCCNALUnitArray *const array = &hvcc.arrays[i];
  700. array->numNalus = 0;
  701. array_output_serializer_free(&array->nalUnitData);
  702. }
  703. done:
  704. array_output_serializer_free(&nals);
  705. return size;
  706. }