h265.go 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. package codec
  2. import (
  3. "bytes"
  4. )
  5. // nal_unit_header() {
  6. // forbidden_zero_bit f(1)
  7. // nal_unit_type u(6)
  8. // nuh_layer_id u(6)
  9. // nuh_temporal_id_plus1 u(3)
  10. // }
  11. type H265NaluHdr struct {
  12. Forbidden_zero_bit uint8
  13. Nal_unit_type uint8
  14. Nuh_layer_id uint8
  15. Nuh_temporal_id_plus1 uint8
  16. }
  17. func (hdr *H265NaluHdr) Decode(bs *BitStream) {
  18. hdr.Forbidden_zero_bit = bs.GetBit()
  19. hdr.Nal_unit_type = bs.Uint8(6)
  20. hdr.Nuh_layer_id = bs.Uint8(6)
  21. hdr.Nuh_temporal_id_plus1 = bs.Uint8(3)
  22. }
  23. type VPS struct {
  24. Vps_video_parameter_set_id uint8
  25. Vps_base_layer_internal_flag uint8
  26. Vps_base_layer_available_flag uint8
  27. Vps_max_layers_minus1 uint8
  28. Vps_max_sub_layers_minus1 uint8
  29. Vps_temporal_id_nesting_flag uint8
  30. Vps_reserved_0xffff_16bits uint16
  31. Ptl ProfileTierLevel
  32. Vps_sub_layer_ordering_info_present_flag uint8
  33. Vps_max_dec_pic_buffering_minus1 [8]uint64
  34. Vps_max_num_reorder_pics [8]uint64
  35. Vps_max_latency_increase_plus1 [8]uint64
  36. Vps_max_layer_id uint8
  37. Vps_num_layer_sets_minus1 uint64
  38. Layer_id_included_flag [][]uint8
  39. Vps_timing_info_present_flag uint8
  40. TimeInfo VPSTimeInfo
  41. // Vps_extension_flag uint8
  42. }
  43. type VPSTimeInfo struct {
  44. Vps_num_units_in_tick uint32
  45. Vps_time_scale uint32
  46. Vps_poc_proportional_to_timing_flag uint8
  47. Vps_num_ticks_poc_diff_one_minus1 uint64
  48. Vps_num_hrd_parameters uint64
  49. Hrd_layer_set_idx []uint64
  50. Cprms_present_flag []uint8
  51. }
  52. type ProfileTierLevel struct {
  53. General_profile_space uint8
  54. General_tier_flag uint8
  55. General_profile_idc uint8
  56. General_profile_compatibility_flag uint32
  57. General_constraint_indicator_flag uint64
  58. General_level_idc uint8
  59. Sub_layer_profile_present_flag [8]uint8
  60. Sub_layer_level_present_flag [8]uint8
  61. }
  62. //nalu without startcode
  63. func (vps *VPS) Decode(nalu []byte) {
  64. sodb := CovertRbspToSodb(nalu)
  65. bs := NewBitStream(sodb)
  66. hdr := H265NaluHdr{}
  67. hdr.Decode(bs)
  68. vps.Vps_video_parameter_set_id = bs.Uint8(4)
  69. vps.Vps_base_layer_internal_flag = bs.Uint8(1)
  70. vps.Vps_base_layer_available_flag = bs.Uint8(1)
  71. vps.Vps_max_layers_minus1 = bs.Uint8(6)
  72. vps.Vps_max_sub_layers_minus1 = bs.Uint8(3)
  73. vps.Vps_temporal_id_nesting_flag = bs.Uint8(1)
  74. vps.Vps_reserved_0xffff_16bits = bs.Uint16(16)
  75. vps.Ptl = Profile_tier_level(1, vps.Vps_max_sub_layers_minus1, bs)
  76. vps.Vps_sub_layer_ordering_info_present_flag = bs.Uint8(1)
  77. var i int
  78. if vps.Vps_sub_layer_ordering_info_present_flag > 0 {
  79. i = 0
  80. } else {
  81. i = int(vps.Vps_max_sub_layers_minus1)
  82. }
  83. for ; i <= int(vps.Vps_max_sub_layers_minus1); i++ {
  84. vps.Vps_max_dec_pic_buffering_minus1[i] = bs.ReadUE()
  85. vps.Vps_max_num_reorder_pics[i] = bs.ReadUE()
  86. vps.Vps_max_latency_increase_plus1[i] = bs.ReadUE()
  87. }
  88. vps.Vps_max_layer_id = bs.Uint8(6)
  89. vps.Vps_num_layer_sets_minus1 = bs.ReadUE()
  90. vps.Layer_id_included_flag = make([][]uint8, vps.Vps_num_layer_sets_minus1)
  91. for i := 1; i <= int(vps.Vps_num_layer_sets_minus1); i++ {
  92. vps.Layer_id_included_flag[i] = make([]uint8, vps.Vps_max_layer_id)
  93. for j := 0; j <= int(vps.Vps_max_layer_id); j++ {
  94. vps.Layer_id_included_flag[i][j] = bs.Uint8(1)
  95. }
  96. }
  97. vps.Vps_timing_info_present_flag = bs.Uint8(1)
  98. if vps.Vps_timing_info_present_flag == 1 {
  99. vps.TimeInfo = ParserVPSTimeinfo(bs)
  100. }
  101. }
  102. //ffmpeg hevc.c
  103. //static void hvcc_parse_ptl(GetBitContext *gb,HEVCDecoderConfigurationRecord *hvcc,unsigned int max_sub_layers_minus1)
  104. func Profile_tier_level(profilePresentFlag uint8, maxNumSubLayersMinus1 uint8, bs *BitStream) ProfileTierLevel {
  105. var ptl ProfileTierLevel
  106. ptl.General_profile_space = bs.Uint8(2)
  107. ptl.General_tier_flag = bs.Uint8(1)
  108. ptl.General_profile_idc = bs.Uint8(5)
  109. ptl.General_profile_compatibility_flag = bs.Uint32(32)
  110. ptl.General_constraint_indicator_flag = bs.GetBits(48)
  111. ptl.General_level_idc = bs.Uint8(8)
  112. for i := 0; i < int(maxNumSubLayersMinus1); i++ {
  113. ptl.Sub_layer_profile_present_flag[i] = bs.GetBit()
  114. ptl.Sub_layer_level_present_flag[i] = bs.GetBit()
  115. }
  116. if maxNumSubLayersMinus1 > 0 {
  117. for i := maxNumSubLayersMinus1; i < 8; i++ {
  118. bs.SkipBits(2)
  119. }
  120. }
  121. for i := 0; i < int(maxNumSubLayersMinus1); i++ {
  122. if ptl.Sub_layer_profile_present_flag[i] == 1 {
  123. /*
  124. * sub_layer_profile_space[i] u(2)
  125. * sub_layer_tier_flag[i] u(1)
  126. * sub_layer_profile_idc[i] u(5)
  127. * sub_layer_profile_compatibility_flag[i][0..31] u(32)
  128. * sub_layer_progressive_source_flag[i] u(1)
  129. * sub_layer_interlaced_source_flag[i] u(1)
  130. * sub_layer_non_packed_constraint_flag[i] u(1)
  131. * sub_layer_frame_only_constraint_flag[i] u(1)
  132. * sub_layer_reserved_zero_44bits[i] u(44)
  133. */
  134. bs.SkipBits(88)
  135. }
  136. if ptl.Sub_layer_level_present_flag[i] == 1 {
  137. bs.SkipBits(8)
  138. }
  139. }
  140. return ptl
  141. }
  142. func ParserVPSTimeinfo(bs *BitStream) VPSTimeInfo {
  143. var ti VPSTimeInfo
  144. ti.Vps_num_units_in_tick = bs.Uint32(32)
  145. ti.Vps_time_scale = bs.Uint32(32)
  146. ti.Vps_poc_proportional_to_timing_flag = bs.Uint8(1)
  147. if ti.Vps_poc_proportional_to_timing_flag == 1 {
  148. ti.Vps_num_ticks_poc_diff_one_minus1 = bs.ReadUE()
  149. }
  150. ti.Vps_num_hrd_parameters = bs.ReadUE()
  151. // for i := 0; i < int(ti.Vps_num_hrd_parameters); i++ {
  152. // ti.Hrd_layer_set_idx[i] = bs.ReadUE()
  153. // if i > 0 {
  154. // ti.Cprms_present_flag[i] = bs.Uint8(1)
  155. // }
  156. // //Hrd_parameters(ti.Cprms_present_flag[i])
  157. // }
  158. return ti
  159. }
  160. type H265RawSPS struct {
  161. Sps_video_parameter_set_id uint8
  162. Sps_max_sub_layers_minus1 uint8
  163. Sps_temporal_id_nesting_flag uint8
  164. Ptl ProfileTierLevel
  165. Sps_seq_parameter_set_id uint64
  166. Chroma_format_idc uint64
  167. Pic_width_in_luma_samples uint64
  168. Pic_height_in_luma_samples uint64
  169. Conformance_window_flag uint8
  170. Conf_win_left_offset uint64
  171. Conf_win_right_offset uint64
  172. Conf_win_top_offset uint64
  173. Conf_win_bottom_offset uint64
  174. Bit_depth_luma_minus8 uint64
  175. Bit_depth_chroma_minus8 uint64
  176. Log2_max_pic_order_cnt_lsb_minus4 uint64
  177. Sps_sub_layer_ordering_info_present_flag uint8
  178. Vui_parameters_present_flag uint8
  179. Vui VUI_Parameters
  180. }
  181. //nalu without startcode
  182. func (sps *H265RawSPS) Decode(nalu []byte) {
  183. sodb := CovertRbspToSodb(nalu)
  184. bs := NewBitStream(sodb)
  185. hdr := H265NaluHdr{}
  186. hdr.Decode(bs)
  187. sps.Sps_video_parameter_set_id = bs.Uint8(4)
  188. sps.Sps_max_sub_layers_minus1 = bs.Uint8(3)
  189. sps.Sps_temporal_id_nesting_flag = bs.Uint8(1)
  190. sps.Ptl = Profile_tier_level(1, sps.Sps_max_sub_layers_minus1, bs)
  191. sps.Sps_seq_parameter_set_id = bs.ReadUE()
  192. sps.Chroma_format_idc = bs.ReadUE()
  193. if sps.Chroma_format_idc == 3 {
  194. bs.SkipBits(1)
  195. }
  196. sps.Pic_width_in_luma_samples = bs.ReadUE()
  197. sps.Pic_height_in_luma_samples = bs.ReadUE()
  198. sps.Conformance_window_flag = bs.Uint8(1)
  199. if sps.Conformance_window_flag == 1 {
  200. sps.Conf_win_left_offset = bs.ReadUE()
  201. sps.Conf_win_right_offset = bs.ReadUE()
  202. sps.Conf_win_top_offset = bs.ReadUE()
  203. sps.Conf_win_bottom_offset = bs.ReadUE()
  204. }
  205. sps.Bit_depth_luma_minus8 = bs.ReadUE()
  206. sps.Bit_depth_chroma_minus8 = bs.ReadUE()
  207. sps.Log2_max_pic_order_cnt_lsb_minus4 = bs.ReadUE()
  208. sps.Sps_sub_layer_ordering_info_present_flag = bs.Uint8(1)
  209. i := 0
  210. if sps.Sps_sub_layer_ordering_info_present_flag == 0 {
  211. i = int(sps.Sps_max_sub_layers_minus1)
  212. }
  213. for ; i <= int(sps.Sps_max_sub_layers_minus1); i++ {
  214. bs.ReadUE()
  215. bs.ReadUE()
  216. bs.ReadUE()
  217. }
  218. bs.ReadUE() // log2_min_luma_coding_block_size_minus3
  219. bs.ReadUE() // log2_diff_max_min_luma_coding_block_size
  220. bs.ReadUE() // log2_min_transform_block_size_minus2
  221. bs.ReadUE() // log2_diff_max_min_transform_block_size
  222. bs.ReadUE() // max_transform_hierarchy_depth_inter
  223. bs.ReadUE() // max_transform_hierarchy_depth_intra
  224. scaling_list_enabled_flag := bs.GetBit()
  225. if scaling_list_enabled_flag > 0 {
  226. sps_scaling_list_data_present_flag := bs.GetBit()
  227. if sps_scaling_list_data_present_flag > 0 {
  228. scaling_list_data(bs)
  229. }
  230. }
  231. bs.SkipBits(1)
  232. bs.SkipBits(1)
  233. if bs.GetBit() == 1 {
  234. bs.GetBits(4)
  235. bs.GetBits(4)
  236. bs.ReadUE()
  237. bs.ReadUE()
  238. bs.GetBit()
  239. }
  240. num_short_term_ref_pic_sets := bs.ReadUE()
  241. if num_short_term_ref_pic_sets > 64 {
  242. panic("beyond HEVC_MAX_SHORT_TERM_REF_PIC_SETS")
  243. }
  244. var num_delta_pocs [64]uint32
  245. for i := 0; i < int(num_short_term_ref_pic_sets); i++ {
  246. parse_rps(i, num_short_term_ref_pic_sets, num_delta_pocs, bs)
  247. }
  248. if bs.GetBit() == 1 {
  249. num_long_term_ref_pics_sps := bs.ReadUE()
  250. for i := 0; i < int(num_long_term_ref_pics_sps); i++ {
  251. length := Min(int(sps.Log2_max_pic_order_cnt_lsb_minus4+4), 16)
  252. bs.SkipBits(length)
  253. bs.SkipBits(1)
  254. }
  255. }
  256. bs.SkipBits(1)
  257. bs.SkipBits(1)
  258. sps.Vui_parameters_present_flag = bs.GetBit()
  259. if sps.Vui_parameters_present_flag == 1 {
  260. sps.Vui.Decode(bs, sps.Sps_max_sub_layers_minus1)
  261. }
  262. }
  263. type VUI_Parameters struct {
  264. Aspect_ratio_info_present_flag uint8
  265. Overscan_info_present_flag uint8
  266. Chroma_loc_info_present_flag uint8
  267. Neutral_chroma_indication_flag uint8
  268. Field_seq_flag uint8
  269. Frame_field_info_present_flag uint8
  270. Default_display_window_flag uint8
  271. Vui_timing_info_present_flag uint8
  272. Vui_num_units_in_tick uint32
  273. Vui_time_scale uint32
  274. Vui_poc_proportional_to_timing_flag uint8
  275. Vui_hrd_parameters_present_flag uint8
  276. Bitstream_restriction_flag uint8
  277. Tiles_fixed_structure_flag uint8
  278. Motion_vectors_over_pic_boundaries_flag uint8
  279. Restricted_ref_pic_lists_flag uint8
  280. Min_spatial_segmentation_idc uint64
  281. Max_bytes_per_pic_denom uint64
  282. Max_bits_per_min_cu_denom uint64
  283. Log2_max_mv_length_horizontal uint64
  284. Log2_max_mv_length_vertical uint64
  285. }
  286. func (vui *VUI_Parameters) Decode(bs *BitStream, max_sub_layers_minus1 uint8) {
  287. vui.Aspect_ratio_info_present_flag = bs.Uint8(1)
  288. if vui.Aspect_ratio_info_present_flag == 1 {
  289. if bs.Uint8(8) == 255 {
  290. bs.SkipBits(32)
  291. }
  292. }
  293. vui.Overscan_info_present_flag = bs.Uint8(1)
  294. if vui.Overscan_info_present_flag == 1 {
  295. bs.SkipBits(1)
  296. }
  297. if bs.GetBit() == 1 {
  298. bs.SkipBits(4)
  299. if bs.GetBit() == 1 {
  300. bs.SkipBits(24)
  301. }
  302. }
  303. vui.Chroma_loc_info_present_flag = bs.GetBit()
  304. if vui.Chroma_loc_info_present_flag == 1 {
  305. bs.ReadUE()
  306. bs.ReadUE()
  307. }
  308. vui.Neutral_chroma_indication_flag = bs.GetBit()
  309. vui.Field_seq_flag = bs.GetBit()
  310. vui.Frame_field_info_present_flag = bs.GetBit()
  311. vui.Default_display_window_flag = bs.GetBit()
  312. if vui.Default_display_window_flag == 1 {
  313. bs.ReadUE()
  314. bs.ReadUE()
  315. bs.ReadUE()
  316. bs.ReadUE()
  317. }
  318. vui.Vui_timing_info_present_flag = bs.GetBit()
  319. if vui.Vui_timing_info_present_flag == 1 {
  320. vui.Vui_num_units_in_tick = bs.Uint32(32)
  321. vui.Vui_time_scale = bs.Uint32(32)
  322. vui.Vui_poc_proportional_to_timing_flag = bs.GetBit()
  323. if vui.Vui_poc_proportional_to_timing_flag == 1 {
  324. bs.ReadUE()
  325. }
  326. vui.Vui_hrd_parameters_present_flag = bs.GetBit()
  327. if vui.Vui_hrd_parameters_present_flag == 1 {
  328. skip_hrd_parameters(1, uint32(max_sub_layers_minus1), bs)
  329. }
  330. }
  331. vui.Bitstream_restriction_flag = bs.GetBit()
  332. if vui.Bitstream_restriction_flag == 1 {
  333. vui.Tiles_fixed_structure_flag = bs.GetBit()
  334. vui.Motion_vectors_over_pic_boundaries_flag = bs.GetBit()
  335. vui.Restricted_ref_pic_lists_flag = bs.GetBit()
  336. vui.Min_spatial_segmentation_idc = bs.ReadUE()
  337. vui.Max_bytes_per_pic_denom = bs.ReadUE()
  338. vui.Max_bits_per_min_cu_denom = bs.ReadUE()
  339. vui.Log2_max_mv_length_horizontal = bs.ReadUE()
  340. vui.Log2_max_mv_length_vertical = bs.ReadUE()
  341. }
  342. }
  343. func skip_hrd_parameters(cprms_present_flag uint8, max_sub_layers_minus1 uint32, bs *BitStream) {
  344. nal_hrd_parameters_present_flag := uint8(0)
  345. vcl_hrd_parameters_present_flag := uint8(0)
  346. sub_pic_hrd_params_present_flag := uint8(0)
  347. if cprms_present_flag == 1 {
  348. nal_hrd_parameters_present_flag = bs.GetBit()
  349. vcl_hrd_parameters_present_flag = bs.GetBit()
  350. if nal_hrd_parameters_present_flag == 1 || vcl_hrd_parameters_present_flag == 1 {
  351. sub_pic_hrd_params_present_flag = bs.GetBit()
  352. if sub_pic_hrd_params_present_flag == 1 {
  353. /*
  354. * tick_divisor_minus2 u(8)
  355. * du_cpb_removal_delay_increment_length_minus1 u(5)
  356. * sub_pic_cpb_params_in_pic_timing_sei_flag u(1)
  357. * dpb_output_delay_du_length_minus1 u(5)
  358. */
  359. bs.SkipBits(19)
  360. }
  361. bs.SkipBits(8)
  362. if sub_pic_hrd_params_present_flag == 1 {
  363. // cpb_size_du_scale
  364. bs.SkipBits(4)
  365. }
  366. /*
  367. * initial_cpb_removal_delay_length_minus1 u(5)
  368. * au_cpb_removal_delay_length_minus1 u(5)
  369. * dpb_output_delay_length_minus1 u(5)
  370. */
  371. bs.SkipBits(15)
  372. }
  373. }
  374. for i := 0; i <= int(max_sub_layers_minus1); i++ {
  375. fixed_pic_rate_general_flag := bs.GetBit()
  376. fixed_pic_rate_within_cvs_flag := uint8(0)
  377. low_delay_hrd_flag := uint8(0)
  378. cpb_cnt_minus1 := uint32(0)
  379. if fixed_pic_rate_general_flag == 0 {
  380. fixed_pic_rate_within_cvs_flag = bs.GetBit()
  381. }
  382. if fixed_pic_rate_within_cvs_flag == 1 {
  383. bs.ReadUE()
  384. } else {
  385. low_delay_hrd_flag = bs.GetBit()
  386. }
  387. if low_delay_hrd_flag == 0 {
  388. cpb_cnt_minus1 = uint32(bs.ReadUE())
  389. if cpb_cnt_minus1 > 31 {
  390. panic("cpb_cnt_minus1 > 31")
  391. }
  392. }
  393. skip_sub_layer_hrd_parameters := func() {
  394. for i := 0; i < int(cpb_cnt_minus1); i++ {
  395. bs.ReadUE()
  396. bs.ReadUE()
  397. if sub_pic_hrd_params_present_flag == 1 {
  398. bs.ReadUE()
  399. bs.ReadUE()
  400. }
  401. bs.SkipBits(1)
  402. }
  403. }
  404. if nal_hrd_parameters_present_flag == 1 {
  405. skip_sub_layer_hrd_parameters()
  406. }
  407. if vcl_hrd_parameters_present_flag == 1 {
  408. skip_sub_layer_hrd_parameters()
  409. }
  410. }
  411. }
  412. func scaling_list_data(bs *BitStream) {
  413. for i := 0; i < 4; i++ {
  414. maxj := 6
  415. if i == 3 {
  416. maxj = 2
  417. }
  418. for j := 0; j < maxj; j++ {
  419. if bs.GetBit() == 0 {
  420. bs.ReadUE()
  421. } else {
  422. num_coeffs := Min(64, 1<<(4+(i<<1)))
  423. if i > 1 {
  424. bs.ReadSE()
  425. }
  426. for k := 0; k < num_coeffs; k++ {
  427. bs.ReadSE()
  428. }
  429. }
  430. }
  431. }
  432. }
  433. func parse_rps(rps_idx int, nums_rps uint64, num_delta_pocs [64]uint32, bs *BitStream) {
  434. if rps_idx > 0 && bs.GetBit() > 0 {
  435. if rps_idx > int(nums_rps) {
  436. panic("rps_idx > int(nums_rps)")
  437. }
  438. bs.SkipBits(1)
  439. bs.ReadUE()
  440. num_delta_pocs[rps_idx] = 0
  441. for i := uint32(0); i <= num_delta_pocs[rps_idx-1]; i++ {
  442. var use_delta_flag uint8
  443. var used_by_curr_pic_flag uint8 = bs.GetBit()
  444. if used_by_curr_pic_flag == 0 {
  445. use_delta_flag = bs.GetBit()
  446. }
  447. if use_delta_flag > 0 || used_by_curr_pic_flag > 0 {
  448. num_delta_pocs[rps_idx]++
  449. }
  450. }
  451. } else {
  452. num_negative_pics := bs.ReadUE()
  453. num_positive_pics := bs.ReadUE()
  454. if (num_negative_pics+num_positive_pics)*2 > uint64(bs.RemainBits()) {
  455. panic("(num_negative_pics + num_positive_pics) * 2> uint64(bs.RemainBits())")
  456. }
  457. for i := 0; i < int(num_negative_pics); i++ {
  458. bs.ReadUE()
  459. bs.SkipBits(1)
  460. }
  461. for i := 0; i < int(num_positive_pics); i++ {
  462. bs.ReadUE()
  463. bs.SkipBits(1)
  464. }
  465. }
  466. }
  467. type H265RawPPS struct {
  468. Pps_pic_parameter_set_id uint64
  469. Pps_seq_parameter_set_id uint64
  470. Dependent_slice_segments_enabled_flag uint8
  471. Output_flag_present_flag uint8
  472. Num_extra_slice_header_bits uint8
  473. Sign_data_hiding_enabled_flag uint8
  474. Cabac_init_present_flag uint8
  475. Num_ref_idx_l0_default_active_minus1 uint64
  476. Num_ref_idx_l1_default_active_minus1 uint64
  477. Init_qp_minus26 int64
  478. Constrained_intra_pred_flag uint8
  479. Transform_skip_enabled_flag uint8
  480. Cu_qp_delta_enabled_flag uint8
  481. Diff_cu_qp_delta_depth uint64
  482. Pps_cb_qp_offset int64
  483. Pps_cr_qp_offset int64
  484. Pps_slice_chroma_qp_offsets_present_flag uint8
  485. Weighted_pred_flag uint8
  486. Weighted_bipred_flag uint8
  487. Transquant_bypass_enabled_flag uint8
  488. Tiles_enabled_flag uint8
  489. Entropy_coding_sync_enabled_flag uint8
  490. }
  491. //nalu without startcode
  492. func (pps *H265RawPPS) Decode(nalu []byte) {
  493. sodb := CovertRbspToSodb(nalu)
  494. bs := NewBitStream(sodb)
  495. hdr := H265NaluHdr{}
  496. hdr.Decode(bs)
  497. pps.Pps_pic_parameter_set_id = bs.ReadUE()
  498. pps.Pps_seq_parameter_set_id = bs.ReadUE()
  499. pps.Dependent_slice_segments_enabled_flag = bs.GetBit()
  500. pps.Output_flag_present_flag = bs.GetBit()
  501. pps.Num_extra_slice_header_bits = bs.Uint8(3)
  502. pps.Sign_data_hiding_enabled_flag = bs.GetBit()
  503. pps.Cabac_init_present_flag = bs.GetBit()
  504. pps.Num_ref_idx_l0_default_active_minus1 = bs.ReadUE()
  505. pps.Num_ref_idx_l1_default_active_minus1 = bs.ReadUE()
  506. pps.Init_qp_minus26 = bs.ReadSE()
  507. pps.Constrained_intra_pred_flag = bs.GetBit()
  508. pps.Transform_skip_enabled_flag = bs.GetBit()
  509. pps.Cu_qp_delta_enabled_flag = bs.GetBit()
  510. if pps.Cu_qp_delta_enabled_flag == 1 {
  511. pps.Diff_cu_qp_delta_depth = bs.ReadUE()
  512. }
  513. pps.Pps_cb_qp_offset = bs.ReadSE()
  514. pps.Pps_cr_qp_offset = bs.ReadSE()
  515. pps.Pps_slice_chroma_qp_offsets_present_flag = bs.GetBit()
  516. pps.Weighted_pred_flag = bs.GetBit()
  517. pps.Weighted_bipred_flag = bs.GetBit()
  518. pps.Transquant_bypass_enabled_flag = bs.GetBit()
  519. pps.Tiles_enabled_flag = bs.GetBit()
  520. pps.Entropy_coding_sync_enabled_flag = bs.GetBit()
  521. }
  522. func GetH265Resolution(sps []byte) (width uint32, height uint32) {
  523. start, sc := FindStartCode(sps, 0)
  524. h265sps := H265RawSPS{}
  525. h265sps.Decode(sps[start+int(sc):])
  526. width = uint32(h265sps.Pic_width_in_luma_samples)
  527. height = uint32(h265sps.Pic_height_in_luma_samples)
  528. return
  529. }
  530. func GetVPSIdWithStartCode(vps []byte) uint8 {
  531. start, sc := FindStartCode(vps, 0)
  532. return GetVPSId(vps[start+int(sc):])
  533. }
  534. func GetVPSId(vps []byte) uint8 {
  535. var rawvps VPS
  536. rawvps.Decode(vps)
  537. return rawvps.Vps_video_parameter_set_id
  538. }
  539. func GetH265SPSIdWithStartCode(sps []byte) uint64 {
  540. start, sc := FindStartCode(sps, 0)
  541. return GetH265SPSId(sps[start+int(sc):])
  542. }
  543. func GetH265SPSId(sps []byte) uint64 {
  544. var rawsps H265RawSPS
  545. rawsps.Decode(sps)
  546. return rawsps.Sps_seq_parameter_set_id
  547. }
  548. func GetH65PPSIdWithStartCode(pps []byte) uint64 {
  549. start, sc := FindStartCode(pps, 0)
  550. return GetH265SPSId(pps[start+int(sc):])
  551. }
  552. func GetH265PPSId(pps []byte) uint64 {
  553. var rawpps H265RawPPS
  554. rawpps.Decode(pps)
  555. return rawpps.Pps_pic_parameter_set_id
  556. }
  557. /*
  558. ISO/IEC 14496-15:2017(E) 8.3.3.1.2 Syntax (p71)
  559. aligned(8) class HEVCDecoderConfigurationRecord {
  560. unsigned int(8) configurationVersion = 1;
  561. unsigned int(2) general_profile_space;
  562. unsigned int(1) general_tier_flag;
  563. unsigned int(5) general_profile_idc;
  564. unsigned int(32) general_profile_compatibility_flags;
  565. unsigned int(48) general_constraint_indicator_flags;
  566. unsigned int(8) general_level_idc;
  567. bit(4) reserved = '1111'b;
  568. unsigned int(12) min_spatial_segmentation_idc;
  569. bit(6) reserved = '111111'b;
  570. unsigned int(2) parallelismType;
  571. bit(6) reserved = '111111'b;
  572. unsigned int(2) chromaFormat;
  573. bit(5) reserved = '11111'b;
  574. unsigned int(3) bitDepthLumaMinus8;
  575. bit(5) reserved = '11111'b;
  576. unsigned int(3) bitDepthChromaMinus8;
  577. bit(16) avgFrameRate;
  578. bit(2) constantFrameRate;
  579. bit(3) numTemporalLayers;
  580. bit(1) temporalIdNested;
  581. unsigned int(2) lengthSizeMinusOne;
  582. unsigned int(8) numOfArrays;
  583. for (j=0; j < numOfArrays; j++) {
  584. bit(1) array_completeness;
  585. unsigned int(1) reserved = 0;
  586. unsigned int(6) NAL_unit_type;
  587. unsigned int(16) numNalus;
  588. for (i=0; i< numNalus; i++) {
  589. unsigned int(16) nalUnitLength;
  590. bit(8*nalUnitLength) nalUnit;
  591. }
  592. }
  593. }
  594. */
  595. type NalUnit struct {
  596. NalUnitLength uint16
  597. Nalu []byte
  598. }
  599. type HVCCNALUnitArray struct {
  600. Array_completeness uint8
  601. NAL_unit_type uint8
  602. NumNalus uint16
  603. NalUnits []*NalUnit
  604. }
  605. type HEVCRecordConfiguration struct {
  606. ConfigurationVersion uint8
  607. General_profile_space uint8
  608. General_tier_flag uint8
  609. General_profile_idc uint8
  610. General_profile_compatibility_flags uint32
  611. General_constraint_indicator_flags uint64
  612. General_level_idc uint8
  613. Min_spatial_segmentation_idc uint16
  614. ParallelismType uint8
  615. ChromaFormat uint8
  616. BitDepthLumaMinus8 uint8
  617. BitDepthChromaMinus8 uint8
  618. AvgFrameRate uint16
  619. ConstantFrameRate uint8
  620. NumTemporalLayers uint8
  621. TemporalIdNested uint8
  622. LengthSizeMinusOne uint8
  623. NumOfArrays uint8
  624. Arrays []*HVCCNALUnitArray
  625. }
  626. func NewHEVCRecordConfiguration() *HEVCRecordConfiguration {
  627. return &HEVCRecordConfiguration{
  628. ConfigurationVersion: 1,
  629. General_profile_compatibility_flags: 0xffffffff,
  630. General_constraint_indicator_flags: 0xffffffffffffffff,
  631. Min_spatial_segmentation_idc: 4097,
  632. LengthSizeMinusOne: 3,
  633. }
  634. }
  635. func (hvcc *HEVCRecordConfiguration) Encode() []byte {
  636. bsw := NewBitStreamWriter(512)
  637. bsw.PutByte(hvcc.ConfigurationVersion)
  638. bsw.PutUint8(hvcc.General_profile_space, 2)
  639. bsw.PutUint8(hvcc.General_tier_flag, 1)
  640. bsw.PutUint8(hvcc.General_profile_idc, 5)
  641. bsw.PutUint32(hvcc.General_profile_compatibility_flags, 32)
  642. bsw.PutUint64(hvcc.General_constraint_indicator_flags, 48)
  643. bsw.PutByte(hvcc.General_level_idc)
  644. bsw.PutUint8(0x0F, 4)
  645. bsw.PutUint16(hvcc.Min_spatial_segmentation_idc, 12)
  646. bsw.PutUint8(0x3F, 6)
  647. //ffmpeg hvcc_write(AVIOContext *pb, HEVCDecoderConfigurationRecord *hvcc)
  648. /*
  649. * parallelismType indicates the type of parallelism that is used to meet
  650. * the restrictions imposed by min_spatial_segmentation_idc when the value
  651. * of min_spatial_segmentation_idc is greater than 0.
  652. */
  653. if hvcc.Min_spatial_segmentation_idc == 0 {
  654. hvcc.ParallelismType = 0
  655. }
  656. bsw.PutUint8(hvcc.ParallelismType, 2)
  657. bsw.PutUint8(0x3F, 6)
  658. bsw.PutUint8(hvcc.ChromaFormat, 2)
  659. bsw.PutUint8(0x1F, 5)
  660. bsw.PutUint8(hvcc.BitDepthLumaMinus8, 3)
  661. bsw.PutUint8(0x1F, 5)
  662. bsw.PutUint8(hvcc.BitDepthChromaMinus8, 3)
  663. bsw.PutUint16(hvcc.AvgFrameRate, 16)
  664. bsw.PutUint8(hvcc.ConstantFrameRate, 2)
  665. bsw.PutUint8(hvcc.NumTemporalLayers, 3)
  666. bsw.PutUint8(hvcc.TemporalIdNested, 1)
  667. bsw.PutUint8(hvcc.LengthSizeMinusOne, 2)
  668. bsw.PutByte(uint8(len(hvcc.Arrays)))
  669. for _, arrays := range hvcc.Arrays {
  670. bsw.PutUint8(arrays.Array_completeness, 1)
  671. bsw.PutUint8(0, 1)
  672. bsw.PutUint8(arrays.NAL_unit_type, 6)
  673. bsw.PutUint16(arrays.NumNalus, 16)
  674. for _, nalu := range arrays.NalUnits {
  675. bsw.PutUint16(nalu.NalUnitLength, 16)
  676. bsw.PutBytes(nalu.Nalu)
  677. }
  678. }
  679. return bsw.Bits()
  680. }
  681. func (hvcc *HEVCRecordConfiguration) Decode(hevc []byte) {
  682. bs := NewBitStream(hevc)
  683. hvcc.ConfigurationVersion = bs.Uint8(8)
  684. hvcc.General_profile_space = bs.Uint8(2)
  685. hvcc.General_tier_flag = bs.Uint8(1)
  686. hvcc.General_profile_idc = bs.Uint8(5)
  687. hvcc.General_profile_compatibility_flags = bs.Uint32(32)
  688. hvcc.General_constraint_indicator_flags = bs.GetBits(48)
  689. hvcc.General_level_idc = bs.Uint8(8)
  690. bs.SkipBits(4)
  691. hvcc.Min_spatial_segmentation_idc = bs.Uint16(12)
  692. bs.SkipBits(6)
  693. hvcc.ParallelismType = bs.Uint8(2)
  694. bs.SkipBits(6)
  695. hvcc.ChromaFormat = bs.Uint8(2)
  696. bs.SkipBits(5)
  697. hvcc.BitDepthLumaMinus8 = bs.Uint8(3)
  698. bs.SkipBits(5)
  699. hvcc.BitDepthChromaMinus8 = bs.Uint8(3)
  700. hvcc.AvgFrameRate = bs.Uint16(16)
  701. hvcc.ConstantFrameRate = bs.Uint8(2)
  702. hvcc.NumTemporalLayers = bs.Uint8(3)
  703. hvcc.TemporalIdNested = bs.Uint8(1)
  704. hvcc.LengthSizeMinusOne = bs.Uint8(2)
  705. hvcc.NumOfArrays = bs.Uint8(8)
  706. hvcc.Arrays = make([]*HVCCNALUnitArray, hvcc.NumOfArrays)
  707. for i := 0; i < int(hvcc.NumOfArrays); i++ {
  708. hvcc.Arrays[i] = new(HVCCNALUnitArray)
  709. hvcc.Arrays[i].Array_completeness = bs.GetBit()
  710. bs.SkipBits(1)
  711. hvcc.Arrays[i].NAL_unit_type = bs.Uint8(6)
  712. hvcc.Arrays[i].NumNalus = bs.Uint16(16)
  713. hvcc.Arrays[i].NalUnits = make([]*NalUnit, hvcc.Arrays[i].NumNalus)
  714. for j := 0; j < int(hvcc.Arrays[i].NumNalus); j++ {
  715. hvcc.Arrays[i].NalUnits[j] = new(NalUnit)
  716. hvcc.Arrays[i].NalUnits[j].NalUnitLength = bs.Uint16(16)
  717. hvcc.Arrays[i].NalUnits[j].Nalu = bs.GetBytes(int(hvcc.Arrays[i].NalUnits[j].NalUnitLength))
  718. }
  719. }
  720. }
  721. func (hvcc *HEVCRecordConfiguration) UpdateSPS(sps []byte) {
  722. start, sc := FindStartCode(sps, 0)
  723. sps = sps[start+int(sc):]
  724. var rawsps H265RawSPS
  725. rawsps.Decode(sps)
  726. spsid := rawsps.Sps_seq_parameter_set_id
  727. var needUpdate bool = false
  728. i := 0
  729. for ; i < len(hvcc.Arrays); i++ {
  730. arrays := hvcc.Arrays[i]
  731. found := false
  732. if arrays.NAL_unit_type == uint8(H265_NAL_SPS) {
  733. j := 0
  734. for ; j < len(arrays.NalUnits); j++ {
  735. if spsid != GetH265SPSId(arrays.NalUnits[j].Nalu) {
  736. found = true
  737. continue
  738. }
  739. //find the same sps nalu
  740. if arrays.NalUnits[j].NalUnitLength == uint16(len(sps)) && bytes.Equal(arrays.NalUnits[j].Nalu, sps) {
  741. return
  742. }
  743. tmpsps := make([]byte, len(sps))
  744. copy(tmpsps, sps)
  745. arrays.NalUnits[j].Nalu = tmpsps
  746. arrays.NalUnits[j].NalUnitLength = uint16(len(tmpsps))
  747. needUpdate = true
  748. break
  749. }
  750. if j == len(arrays.NalUnits) {
  751. nalu := &NalUnit{
  752. Nalu: make([]byte, len(sps)),
  753. NalUnitLength: uint16(len(sps)),
  754. }
  755. copy(nalu.Nalu, sps)
  756. arrays.NalUnits = append(arrays.NalUnits, nalu)
  757. needUpdate = true
  758. }
  759. }
  760. if found {
  761. break
  762. }
  763. }
  764. if i == len(hvcc.Arrays) {
  765. nua := &HVCCNALUnitArray{
  766. Array_completeness: 1,
  767. NAL_unit_type: 33,
  768. NumNalus: 1,
  769. NalUnits: make([]*NalUnit, 1),
  770. }
  771. nu := &NalUnit{
  772. NalUnitLength: uint16(len(sps)),
  773. Nalu: make([]byte, len(sps)),
  774. }
  775. copy(nu.Nalu, sps)
  776. nua.NalUnits[0] = nu
  777. hvcc.Arrays = append(hvcc.Arrays, nua)
  778. needUpdate = true
  779. }
  780. if needUpdate {
  781. hvcc.NumTemporalLayers = uint8(Max(int(hvcc.NumTemporalLayers), int(rawsps.Sps_max_sub_layers_minus1+1)))
  782. hvcc.TemporalIdNested = rawsps.Sps_temporal_id_nesting_flag
  783. hvcc.ChromaFormat = uint8(rawsps.Chroma_format_idc)
  784. hvcc.BitDepthChromaMinus8 = uint8(rawsps.Bit_depth_chroma_minus8)
  785. hvcc.BitDepthLumaMinus8 = uint8(rawsps.Bit_depth_luma_minus8)
  786. hvcc.updatePtl(rawsps.Ptl)
  787. hvcc.updateVui(rawsps.Vui)
  788. }
  789. }
  790. func (hvcc *HEVCRecordConfiguration) UpdatePPS(pps []byte) {
  791. start, sc := FindStartCode(pps, 0)
  792. pps = pps[start+int(sc):]
  793. var rawpps H265RawPPS
  794. rawpps.Decode(pps)
  795. ppsid := rawpps.Pps_pic_parameter_set_id
  796. var needUpdate bool = false
  797. i := 0
  798. for ; i < len(hvcc.Arrays); i++ {
  799. arrays := hvcc.Arrays[i]
  800. found := false
  801. if arrays.NAL_unit_type == uint8(H265_NAL_PPS) {
  802. j := 0
  803. for ; j < len(arrays.NalUnits); j++ {
  804. if ppsid != GetH265PPSId(arrays.NalUnits[j].Nalu) {
  805. found = true
  806. continue
  807. }
  808. //find the same sps nalu
  809. if arrays.NalUnits[j].NalUnitLength == uint16(len(pps)) && bytes.Equal(arrays.NalUnits[j].Nalu, pps) {
  810. return
  811. }
  812. tmppps := make([]byte, len(pps))
  813. copy(tmppps, pps)
  814. arrays.NalUnits[j].Nalu = tmppps
  815. arrays.NalUnits[j].NalUnitLength = uint16(len(tmppps))
  816. needUpdate = true
  817. break
  818. }
  819. if j == len(arrays.NalUnits) {
  820. nalu := &NalUnit{
  821. Nalu: make([]byte, len(pps)),
  822. NalUnitLength: uint16(len(pps)),
  823. }
  824. copy(nalu.Nalu, pps)
  825. arrays.NalUnits = append(arrays.NalUnits, nalu)
  826. needUpdate = true
  827. }
  828. }
  829. if found {
  830. break
  831. }
  832. }
  833. if i == len(hvcc.Arrays) {
  834. nua := &HVCCNALUnitArray{
  835. Array_completeness: 1,
  836. NAL_unit_type: 34,
  837. NumNalus: 1,
  838. NalUnits: make([]*NalUnit, 1),
  839. }
  840. nu := &NalUnit{
  841. NalUnitLength: uint16(len(pps)),
  842. Nalu: make([]byte, len(pps)),
  843. }
  844. copy(nu.Nalu, pps)
  845. nua.NalUnits[0] = nu
  846. hvcc.Arrays = append(hvcc.Arrays, nua)
  847. needUpdate = true
  848. }
  849. if needUpdate {
  850. if rawpps.Entropy_coding_sync_enabled_flag == 1 && rawpps.Tiles_enabled_flag == 1 {
  851. hvcc.ParallelismType = 0
  852. } else if rawpps.Entropy_coding_sync_enabled_flag == 1 {
  853. hvcc.ParallelismType = 3
  854. } else if rawpps.Tiles_enabled_flag == 1 {
  855. hvcc.ParallelismType = 2
  856. } else {
  857. hvcc.ParallelismType = 1
  858. }
  859. }
  860. }
  861. func (hvcc *HEVCRecordConfiguration) UpdateVPS(vps []byte) {
  862. start, sc := FindStartCode(vps, 0)
  863. vps = vps[start+int(sc):]
  864. var rawvps VPS
  865. rawvps.Decode(vps)
  866. vpsid := rawvps.Vps_video_parameter_set_id
  867. var needUpdate bool = false
  868. i := 0
  869. for ; i < len(hvcc.Arrays); i++ {
  870. arrays := hvcc.Arrays[i]
  871. found := false
  872. if arrays.NAL_unit_type == uint8(H265_NAL_VPS) {
  873. found = true
  874. j := 0
  875. for ; j < len(arrays.NalUnits); j++ {
  876. if vpsid != GetVPSId(arrays.NalUnits[j].Nalu) {
  877. found = true
  878. continue
  879. }
  880. //find the same sps nalu
  881. if arrays.NalUnits[j].NalUnitLength == uint16(len(vps)) && bytes.Equal(arrays.NalUnits[j].Nalu, vps) {
  882. return
  883. }
  884. tmpvps := make([]byte, len(vps))
  885. copy(tmpvps, vps)
  886. arrays.NalUnits[j].Nalu = tmpvps
  887. arrays.NalUnits[j].NalUnitLength = uint16(len(tmpvps))
  888. needUpdate = true
  889. break
  890. }
  891. if j == len(arrays.NalUnits) {
  892. nalu := &NalUnit{
  893. Nalu: make([]byte, len(vps)),
  894. NalUnitLength: uint16(len(vps)),
  895. }
  896. copy(nalu.Nalu, vps)
  897. arrays.NalUnits = append(arrays.NalUnits, nalu)
  898. needUpdate = true
  899. }
  900. }
  901. if found {
  902. break
  903. }
  904. }
  905. if i == len(hvcc.Arrays) {
  906. nua := &HVCCNALUnitArray{
  907. Array_completeness: 1,
  908. NAL_unit_type: 32,
  909. NumNalus: 1,
  910. NalUnits: make([]*NalUnit, 1),
  911. }
  912. nu := &NalUnit{
  913. NalUnitLength: uint16(len(vps)),
  914. Nalu: make([]byte, len(vps)),
  915. }
  916. copy(nu.Nalu, vps)
  917. nua.NalUnits[0] = nu
  918. hvcc.Arrays = append(hvcc.Arrays, nua)
  919. needUpdate = true
  920. }
  921. if needUpdate {
  922. hvcc.NumTemporalLayers = uint8(Max(int(hvcc.NumTemporalLayers), int(rawvps.Vps_max_layers_minus1+1)))
  923. hvcc.updatePtl(rawvps.Ptl)
  924. }
  925. }
  926. func (hvcc *HEVCRecordConfiguration) ToNalus() (nalus []byte) {
  927. startcode := []byte{0x00, 0x00, 0x00, 0x01}
  928. for _, arrays := range hvcc.Arrays {
  929. for _, unit := range arrays.NalUnits {
  930. nalus = append(nalus, startcode...)
  931. nalus = append(nalus, unit.Nalu[:unit.NalUnitLength]...)
  932. }
  933. }
  934. return
  935. }
  936. func (hvcc *HEVCRecordConfiguration) updatePtl(ptl ProfileTierLevel) {
  937. hvcc.General_profile_space = ptl.General_profile_space
  938. if hvcc.General_tier_flag < ptl.General_tier_flag {
  939. hvcc.General_level_idc = ptl.General_level_idc
  940. } else {
  941. hvcc.General_level_idc = uint8(Max(int(hvcc.General_level_idc), int(ptl.General_level_idc)))
  942. }
  943. hvcc.General_tier_flag = uint8(Max(int(hvcc.General_tier_flag), int(ptl.General_tier_flag)))
  944. hvcc.General_profile_idc = uint8(Max(int(hvcc.General_profile_idc), int(ptl.General_profile_idc)))
  945. hvcc.General_profile_compatibility_flags &= ptl.General_profile_compatibility_flag
  946. hvcc.General_constraint_indicator_flags &= ptl.General_constraint_indicator_flag
  947. }
  948. func (hvcc *HEVCRecordConfiguration) updateVui(vui VUI_Parameters) {
  949. hvcc.Min_spatial_segmentation_idc = uint16(Min(int(hvcc.Min_spatial_segmentation_idc), int(vui.Min_spatial_segmentation_idc)))
  950. }