rtmp-hevc.c 25 KB

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