You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1128 lines
38KB

  1. /*
  2. * Copyright (c) 2014 Tim Walker <tdskywalker@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavcodec/avcodec.h"
  21. #include "libavcodec/get_bits.h"
  22. #include "libavcodec/golomb.h"
  23. #include "libavcodec/hevc.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "avc.h"
  26. #include "avio.h"
  27. #include "avio_internal.h"
  28. #include "hevc.h"
  29. #define MAX_SPATIAL_SEGMENTATION 4096 // max. value of u(12) field
  30. typedef struct HVCCNALUnitArray {
  31. uint8_t array_completeness;
  32. uint8_t NAL_unit_type;
  33. uint16_t numNalus;
  34. uint16_t *nalUnitLength;
  35. uint8_t **nalUnit;
  36. } HVCCNALUnitArray;
  37. typedef struct HEVCDecoderConfigurationRecord {
  38. uint8_t configurationVersion;
  39. uint8_t general_profile_space;
  40. uint8_t general_tier_flag;
  41. uint8_t general_profile_idc;
  42. uint32_t general_profile_compatibility_flags;
  43. uint64_t general_constraint_indicator_flags;
  44. uint8_t general_level_idc;
  45. uint16_t min_spatial_segmentation_idc;
  46. uint8_t parallelismType;
  47. uint8_t chromaFormat;
  48. uint8_t bitDepthLumaMinus8;
  49. uint8_t bitDepthChromaMinus8;
  50. uint16_t avgFrameRate;
  51. uint8_t constantFrameRate;
  52. uint8_t numTemporalLayers;
  53. uint8_t temporalIdNested;
  54. uint8_t lengthSizeMinusOne;
  55. uint8_t numOfArrays;
  56. HVCCNALUnitArray *array;
  57. } HEVCDecoderConfigurationRecord;
  58. typedef struct HVCCProfileTierLevel {
  59. uint8_t profile_space;
  60. uint8_t tier_flag;
  61. uint8_t profile_idc;
  62. uint32_t profile_compatibility_flags;
  63. uint64_t constraint_indicator_flags;
  64. uint8_t level_idc;
  65. } HVCCProfileTierLevel;
  66. static void hvcc_update_ptl(HEVCDecoderConfigurationRecord *hvcc,
  67. HVCCProfileTierLevel *ptl)
  68. {
  69. /*
  70. * The value of general_profile_space in all the parameter sets must be
  71. * identical.
  72. */
  73. hvcc->general_profile_space = ptl->profile_space;
  74. /*
  75. * The level indication general_level_idc must indicate a level of
  76. * capability equal to or greater than the highest level indicated for the
  77. * highest tier in all the parameter sets.
  78. */
  79. if (hvcc->general_tier_flag < ptl->tier_flag)
  80. hvcc->general_level_idc = ptl->level_idc;
  81. else
  82. hvcc->general_level_idc = FFMAX(hvcc->general_level_idc, ptl->level_idc);
  83. /*
  84. * The tier indication general_tier_flag must indicate a tier equal to or
  85. * greater than the highest tier indicated in all the parameter sets.
  86. */
  87. hvcc->general_tier_flag = FFMAX(hvcc->general_tier_flag, ptl->tier_flag);
  88. /*
  89. * The profile indication general_profile_idc must indicate a profile to
  90. * which the stream associated with this configuration record conforms.
  91. *
  92. * If the sequence parameter sets are marked with different profiles, then
  93. * the stream may need examination to determine which profile, if any, the
  94. * entire stream conforms to. If the entire stream is not examined, or the
  95. * examination reveals that there is no profile to which the entire stream
  96. * conforms, then the entire stream must be split into two or more
  97. * sub-streams with separate configuration records in which these rules can
  98. * be met.
  99. *
  100. * Note: set the profile to the highest value for the sake of simplicity.
  101. */
  102. hvcc->general_profile_idc = FFMAX(hvcc->general_profile_idc, ptl->profile_idc);
  103. /*
  104. * Each bit in general_profile_compatibility_flags may only be set if all
  105. * the parameter sets set that bit.
  106. */
  107. hvcc->general_profile_compatibility_flags &= ptl->profile_compatibility_flags;
  108. /*
  109. * Each bit in general_constraint_indicator_flags may only be set if all
  110. * the parameter sets set that bit.
  111. */
  112. hvcc->general_constraint_indicator_flags &= ptl->constraint_indicator_flags;
  113. }
  114. static void hvcc_parse_ptl(GetBitContext *gb,
  115. HEVCDecoderConfigurationRecord *hvcc,
  116. unsigned int max_sub_layers_minus1)
  117. {
  118. unsigned int i;
  119. HVCCProfileTierLevel general_ptl;
  120. uint8_t sub_layer_profile_present_flag[HEVC_MAX_SUB_LAYERS];
  121. uint8_t sub_layer_level_present_flag[HEVC_MAX_SUB_LAYERS];
  122. general_ptl.profile_space = get_bits(gb, 2);
  123. general_ptl.tier_flag = get_bits1(gb);
  124. general_ptl.profile_idc = get_bits(gb, 5);
  125. general_ptl.profile_compatibility_flags = get_bits_long(gb, 32);
  126. general_ptl.constraint_indicator_flags = get_bits64(gb, 48);
  127. general_ptl.level_idc = get_bits(gb, 8);
  128. hvcc_update_ptl(hvcc, &general_ptl);
  129. for (i = 0; i < max_sub_layers_minus1; i++) {
  130. sub_layer_profile_present_flag[i] = get_bits1(gb);
  131. sub_layer_level_present_flag[i] = get_bits1(gb);
  132. }
  133. if (max_sub_layers_minus1 > 0)
  134. for (i = max_sub_layers_minus1; i < 8; i++)
  135. skip_bits(gb, 2); // reserved_zero_2bits[i]
  136. for (i = 0; i < max_sub_layers_minus1; i++) {
  137. if (sub_layer_profile_present_flag[i]) {
  138. /*
  139. * sub_layer_profile_space[i] u(2)
  140. * sub_layer_tier_flag[i] u(1)
  141. * sub_layer_profile_idc[i] u(5)
  142. * sub_layer_profile_compatibility_flag[i][0..31] u(32)
  143. * sub_layer_progressive_source_flag[i] u(1)
  144. * sub_layer_interlaced_source_flag[i] u(1)
  145. * sub_layer_non_packed_constraint_flag[i] u(1)
  146. * sub_layer_frame_only_constraint_flag[i] u(1)
  147. * sub_layer_reserved_zero_44bits[i] u(44)
  148. */
  149. skip_bits_long(gb, 32);
  150. skip_bits_long(gb, 32);
  151. skip_bits (gb, 24);
  152. }
  153. if (sub_layer_level_present_flag[i])
  154. skip_bits(gb, 8);
  155. }
  156. }
  157. static void skip_sub_layer_hrd_parameters(GetBitContext *gb,
  158. unsigned int cpb_cnt_minus1,
  159. uint8_t sub_pic_hrd_params_present_flag)
  160. {
  161. unsigned int i;
  162. for (i = 0; i <= cpb_cnt_minus1; i++) {
  163. get_ue_golomb_long(gb); // bit_rate_value_minus1
  164. get_ue_golomb_long(gb); // cpb_size_value_minus1
  165. if (sub_pic_hrd_params_present_flag) {
  166. get_ue_golomb_long(gb); // cpb_size_du_value_minus1
  167. get_ue_golomb_long(gb); // bit_rate_du_value_minus1
  168. }
  169. skip_bits1(gb); // cbr_flag
  170. }
  171. }
  172. static int skip_hrd_parameters(GetBitContext *gb, uint8_t cprms_present_flag,
  173. unsigned int max_sub_layers_minus1)
  174. {
  175. unsigned int i;
  176. uint8_t sub_pic_hrd_params_present_flag = 0;
  177. uint8_t nal_hrd_parameters_present_flag = 0;
  178. uint8_t vcl_hrd_parameters_present_flag = 0;
  179. if (cprms_present_flag) {
  180. nal_hrd_parameters_present_flag = get_bits1(gb);
  181. vcl_hrd_parameters_present_flag = get_bits1(gb);
  182. if (nal_hrd_parameters_present_flag ||
  183. vcl_hrd_parameters_present_flag) {
  184. sub_pic_hrd_params_present_flag = get_bits1(gb);
  185. if (sub_pic_hrd_params_present_flag)
  186. /*
  187. * tick_divisor_minus2 u(8)
  188. * du_cpb_removal_delay_increment_length_minus1 u(5)
  189. * sub_pic_cpb_params_in_pic_timing_sei_flag u(1)
  190. * dpb_output_delay_du_length_minus1 u(5)
  191. */
  192. skip_bits(gb, 19);
  193. /*
  194. * bit_rate_scale u(4)
  195. * cpb_size_scale u(4)
  196. */
  197. skip_bits(gb, 8);
  198. if (sub_pic_hrd_params_present_flag)
  199. skip_bits(gb, 4); // cpb_size_du_scale
  200. /*
  201. * initial_cpb_removal_delay_length_minus1 u(5)
  202. * au_cpb_removal_delay_length_minus1 u(5)
  203. * dpb_output_delay_length_minus1 u(5)
  204. */
  205. skip_bits(gb, 15);
  206. }
  207. }
  208. for (i = 0; i <= max_sub_layers_minus1; i++) {
  209. unsigned int cpb_cnt_minus1 = 0;
  210. uint8_t low_delay_hrd_flag = 0;
  211. uint8_t fixed_pic_rate_within_cvs_flag = 0;
  212. uint8_t fixed_pic_rate_general_flag = get_bits1(gb);
  213. if (!fixed_pic_rate_general_flag)
  214. fixed_pic_rate_within_cvs_flag = get_bits1(gb);
  215. if (fixed_pic_rate_within_cvs_flag)
  216. get_ue_golomb_long(gb); // elemental_duration_in_tc_minus1
  217. else
  218. low_delay_hrd_flag = get_bits1(gb);
  219. if (!low_delay_hrd_flag) {
  220. cpb_cnt_minus1 = get_ue_golomb_long(gb);
  221. if (cpb_cnt_minus1 > 31)
  222. return AVERROR_INVALIDDATA;
  223. }
  224. if (nal_hrd_parameters_present_flag)
  225. skip_sub_layer_hrd_parameters(gb, cpb_cnt_minus1,
  226. sub_pic_hrd_params_present_flag);
  227. if (vcl_hrd_parameters_present_flag)
  228. skip_sub_layer_hrd_parameters(gb, cpb_cnt_minus1,
  229. sub_pic_hrd_params_present_flag);
  230. }
  231. return 0;
  232. }
  233. static void skip_timing_info(GetBitContext *gb)
  234. {
  235. skip_bits_long(gb, 32); // num_units_in_tick
  236. skip_bits_long(gb, 32); // time_scale
  237. if (get_bits1(gb)) // poc_proportional_to_timing_flag
  238. get_ue_golomb_long(gb); // num_ticks_poc_diff_one_minus1
  239. }
  240. static void hvcc_parse_vui(GetBitContext *gb,
  241. HEVCDecoderConfigurationRecord *hvcc,
  242. unsigned int max_sub_layers_minus1)
  243. {
  244. unsigned int min_spatial_segmentation_idc;
  245. if (get_bits1(gb)) // aspect_ratio_info_present_flag
  246. if (get_bits(gb, 8) == 255) // aspect_ratio_idc
  247. skip_bits_long(gb, 32); // sar_width u(16), sar_height u(16)
  248. if (get_bits1(gb)) // overscan_info_present_flag
  249. skip_bits1(gb); // overscan_appropriate_flag
  250. if (get_bits1(gb)) { // video_signal_type_present_flag
  251. skip_bits(gb, 4); // video_format u(3), video_full_range_flag u(1)
  252. if (get_bits1(gb)) // colour_description_present_flag
  253. /*
  254. * colour_primaries u(8)
  255. * transfer_characteristics u(8)
  256. * matrix_coeffs u(8)
  257. */
  258. skip_bits(gb, 24);
  259. }
  260. if (get_bits1(gb)) { // chroma_loc_info_present_flag
  261. get_ue_golomb_long(gb); // chroma_sample_loc_type_top_field
  262. get_ue_golomb_long(gb); // chroma_sample_loc_type_bottom_field
  263. }
  264. /*
  265. * neutral_chroma_indication_flag u(1)
  266. * field_seq_flag u(1)
  267. * frame_field_info_present_flag u(1)
  268. */
  269. skip_bits(gb, 3);
  270. if (get_bits1(gb)) { // default_display_window_flag
  271. get_ue_golomb_long(gb); // def_disp_win_left_offset
  272. get_ue_golomb_long(gb); // def_disp_win_right_offset
  273. get_ue_golomb_long(gb); // def_disp_win_top_offset
  274. get_ue_golomb_long(gb); // def_disp_win_bottom_offset
  275. }
  276. if (get_bits1(gb)) { // vui_timing_info_present_flag
  277. skip_timing_info(gb);
  278. if (get_bits1(gb)) // vui_hrd_parameters_present_flag
  279. skip_hrd_parameters(gb, 1, max_sub_layers_minus1);
  280. }
  281. if (get_bits1(gb)) { // bitstream_restriction_flag
  282. /*
  283. * tiles_fixed_structure_flag u(1)
  284. * motion_vectors_over_pic_boundaries_flag u(1)
  285. * restricted_ref_pic_lists_flag u(1)
  286. */
  287. skip_bits(gb, 3);
  288. min_spatial_segmentation_idc = get_ue_golomb_long(gb);
  289. /*
  290. * unsigned int(12) min_spatial_segmentation_idc;
  291. *
  292. * The min_spatial_segmentation_idc indication must indicate a level of
  293. * spatial segmentation equal to or less than the lowest level of
  294. * spatial segmentation indicated in all the parameter sets.
  295. */
  296. hvcc->min_spatial_segmentation_idc = FFMIN(hvcc->min_spatial_segmentation_idc,
  297. min_spatial_segmentation_idc);
  298. get_ue_golomb_long(gb); // max_bytes_per_pic_denom
  299. get_ue_golomb_long(gb); // max_bits_per_min_cu_denom
  300. get_ue_golomb_long(gb); // log2_max_mv_length_horizontal
  301. get_ue_golomb_long(gb); // log2_max_mv_length_vertical
  302. }
  303. }
  304. static void skip_sub_layer_ordering_info(GetBitContext *gb)
  305. {
  306. get_ue_golomb_long(gb); // max_dec_pic_buffering_minus1
  307. get_ue_golomb_long(gb); // max_num_reorder_pics
  308. get_ue_golomb_long(gb); // max_latency_increase_plus1
  309. }
  310. static int hvcc_parse_vps(GetBitContext *gb,
  311. HEVCDecoderConfigurationRecord *hvcc)
  312. {
  313. unsigned int vps_max_sub_layers_minus1;
  314. /*
  315. * vps_video_parameter_set_id u(4)
  316. * vps_reserved_three_2bits u(2)
  317. * vps_max_layers_minus1 u(6)
  318. */
  319. skip_bits(gb, 12);
  320. vps_max_sub_layers_minus1 = get_bits(gb, 3);
  321. /*
  322. * numTemporalLayers greater than 1 indicates that the stream to which this
  323. * configuration record applies is temporally scalable and the contained
  324. * number of temporal layers (also referred to as temporal sub-layer or
  325. * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
  326. * indicates that the stream is not temporally scalable. Value 0 indicates
  327. * that it is unknown whether the stream is temporally scalable.
  328. */
  329. hvcc->numTemporalLayers = FFMAX(hvcc->numTemporalLayers,
  330. vps_max_sub_layers_minus1 + 1);
  331. /*
  332. * vps_temporal_id_nesting_flag u(1)
  333. * vps_reserved_0xffff_16bits u(16)
  334. */
  335. skip_bits(gb, 17);
  336. hvcc_parse_ptl(gb, hvcc, vps_max_sub_layers_minus1);
  337. /* nothing useful for hvcC past this point */
  338. return 0;
  339. }
  340. static void skip_scaling_list_data(GetBitContext *gb)
  341. {
  342. int i, j, k, num_coeffs;
  343. for (i = 0; i < 4; i++)
  344. for (j = 0; j < (i == 3 ? 2 : 6); j++)
  345. if (!get_bits1(gb)) // scaling_list_pred_mode_flag[i][j]
  346. get_ue_golomb_long(gb); // scaling_list_pred_matrix_id_delta[i][j]
  347. else {
  348. num_coeffs = FFMIN(64, 1 << (4 + (i << 1)));
  349. if (i > 1)
  350. get_se_golomb_long(gb); // scaling_list_dc_coef_minus8[i-2][j]
  351. for (k = 0; k < num_coeffs; k++)
  352. get_se_golomb_long(gb); // scaling_list_delta_coef
  353. }
  354. }
  355. static int parse_rps(GetBitContext *gb, unsigned int rps_idx,
  356. unsigned int num_rps,
  357. unsigned int num_delta_pocs[HEVC_MAX_SHORT_TERM_REF_PIC_SETS])
  358. {
  359. unsigned int i;
  360. if (rps_idx && get_bits1(gb)) { // inter_ref_pic_set_prediction_flag
  361. /* this should only happen for slice headers, and this isn't one */
  362. if (rps_idx >= num_rps)
  363. return AVERROR_INVALIDDATA;
  364. skip_bits1 (gb); // delta_rps_sign
  365. get_ue_golomb_long(gb); // abs_delta_rps_minus1
  366. num_delta_pocs[rps_idx] = 0;
  367. /*
  368. * From libavcodec/hevc_ps.c:
  369. *
  370. * if (is_slice_header) {
  371. * //foo
  372. * } else
  373. * rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
  374. *
  375. * where:
  376. * rps: &sps->st_rps[rps_idx]
  377. * sps->st_rps: &sps->st_rps[0]
  378. * is_slice_header: rps_idx == num_rps
  379. *
  380. * thus:
  381. * if (num_rps != rps_idx)
  382. * rps_ridx = &sps->st_rps[rps_idx - 1];
  383. *
  384. * NumDeltaPocs[RefRpsIdx]: num_delta_pocs[rps_idx - 1]
  385. */
  386. for (i = 0; i <= num_delta_pocs[rps_idx - 1]; i++) {
  387. uint8_t use_delta_flag = 0;
  388. uint8_t used_by_curr_pic_flag = get_bits1(gb);
  389. if (!used_by_curr_pic_flag)
  390. use_delta_flag = get_bits1(gb);
  391. if (used_by_curr_pic_flag || use_delta_flag)
  392. num_delta_pocs[rps_idx]++;
  393. }
  394. } else {
  395. unsigned int num_negative_pics = get_ue_golomb_long(gb);
  396. unsigned int num_positive_pics = get_ue_golomb_long(gb);
  397. if ((num_positive_pics + (uint64_t)num_negative_pics) * 2 > get_bits_left(gb))
  398. return AVERROR_INVALIDDATA;
  399. num_delta_pocs[rps_idx] = num_negative_pics + num_positive_pics;
  400. for (i = 0; i < num_negative_pics; i++) {
  401. get_ue_golomb_long(gb); // delta_poc_s0_minus1[rps_idx]
  402. skip_bits1 (gb); // used_by_curr_pic_s0_flag[rps_idx]
  403. }
  404. for (i = 0; i < num_positive_pics; i++) {
  405. get_ue_golomb_long(gb); // delta_poc_s1_minus1[rps_idx]
  406. skip_bits1 (gb); // used_by_curr_pic_s1_flag[rps_idx]
  407. }
  408. }
  409. return 0;
  410. }
  411. static int hvcc_parse_sps(GetBitContext *gb,
  412. HEVCDecoderConfigurationRecord *hvcc)
  413. {
  414. unsigned int i, sps_max_sub_layers_minus1, log2_max_pic_order_cnt_lsb_minus4;
  415. unsigned int num_short_term_ref_pic_sets, num_delta_pocs[HEVC_MAX_SHORT_TERM_REF_PIC_SETS];
  416. skip_bits(gb, 4); // sps_video_parameter_set_id
  417. sps_max_sub_layers_minus1 = get_bits (gb, 3);
  418. /*
  419. * numTemporalLayers greater than 1 indicates that the stream to which this
  420. * configuration record applies is temporally scalable and the contained
  421. * number of temporal layers (also referred to as temporal sub-layer or
  422. * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
  423. * indicates that the stream is not temporally scalable. Value 0 indicates
  424. * that it is unknown whether the stream is temporally scalable.
  425. */
  426. hvcc->numTemporalLayers = FFMAX(hvcc->numTemporalLayers,
  427. sps_max_sub_layers_minus1 + 1);
  428. hvcc->temporalIdNested = get_bits1(gb);
  429. hvcc_parse_ptl(gb, hvcc, sps_max_sub_layers_minus1);
  430. get_ue_golomb_long(gb); // sps_seq_parameter_set_id
  431. hvcc->chromaFormat = get_ue_golomb_long(gb);
  432. if (hvcc->chromaFormat == 3)
  433. skip_bits1(gb); // separate_colour_plane_flag
  434. get_ue_golomb_long(gb); // pic_width_in_luma_samples
  435. get_ue_golomb_long(gb); // pic_height_in_luma_samples
  436. if (get_bits1(gb)) { // conformance_window_flag
  437. get_ue_golomb_long(gb); // conf_win_left_offset
  438. get_ue_golomb_long(gb); // conf_win_right_offset
  439. get_ue_golomb_long(gb); // conf_win_top_offset
  440. get_ue_golomb_long(gb); // conf_win_bottom_offset
  441. }
  442. hvcc->bitDepthLumaMinus8 = get_ue_golomb_long(gb);
  443. hvcc->bitDepthChromaMinus8 = get_ue_golomb_long(gb);
  444. log2_max_pic_order_cnt_lsb_minus4 = get_ue_golomb_long(gb);
  445. /* sps_sub_layer_ordering_info_present_flag */
  446. i = get_bits1(gb) ? 0 : sps_max_sub_layers_minus1;
  447. for (; i <= sps_max_sub_layers_minus1; i++)
  448. skip_sub_layer_ordering_info(gb);
  449. get_ue_golomb_long(gb); // log2_min_luma_coding_block_size_minus3
  450. get_ue_golomb_long(gb); // log2_diff_max_min_luma_coding_block_size
  451. get_ue_golomb_long(gb); // log2_min_transform_block_size_minus2
  452. get_ue_golomb_long(gb); // log2_diff_max_min_transform_block_size
  453. get_ue_golomb_long(gb); // max_transform_hierarchy_depth_inter
  454. get_ue_golomb_long(gb); // max_transform_hierarchy_depth_intra
  455. if (get_bits1(gb) && // scaling_list_enabled_flag
  456. get_bits1(gb)) // sps_scaling_list_data_present_flag
  457. skip_scaling_list_data(gb);
  458. skip_bits1(gb); // amp_enabled_flag
  459. skip_bits1(gb); // sample_adaptive_offset_enabled_flag
  460. if (get_bits1(gb)) { // pcm_enabled_flag
  461. skip_bits (gb, 4); // pcm_sample_bit_depth_luma_minus1
  462. skip_bits (gb, 4); // pcm_sample_bit_depth_chroma_minus1
  463. get_ue_golomb_long(gb); // log2_min_pcm_luma_coding_block_size_minus3
  464. get_ue_golomb_long(gb); // log2_diff_max_min_pcm_luma_coding_block_size
  465. skip_bits1 (gb); // pcm_loop_filter_disabled_flag
  466. }
  467. num_short_term_ref_pic_sets = get_ue_golomb_long(gb);
  468. if (num_short_term_ref_pic_sets > HEVC_MAX_SHORT_TERM_REF_PIC_SETS)
  469. return AVERROR_INVALIDDATA;
  470. for (i = 0; i < num_short_term_ref_pic_sets; i++) {
  471. int ret = parse_rps(gb, i, num_short_term_ref_pic_sets, num_delta_pocs);
  472. if (ret < 0)
  473. return ret;
  474. }
  475. if (get_bits1(gb)) { // long_term_ref_pics_present_flag
  476. unsigned num_long_term_ref_pics_sps = get_ue_golomb_long(gb);
  477. if (num_long_term_ref_pics_sps > 31U)
  478. return AVERROR_INVALIDDATA;
  479. for (i = 0; i < num_long_term_ref_pics_sps; i++) { // num_long_term_ref_pics_sps
  480. int len = FFMIN(log2_max_pic_order_cnt_lsb_minus4 + 4, 16);
  481. skip_bits (gb, len); // lt_ref_pic_poc_lsb_sps[i]
  482. skip_bits1(gb); // used_by_curr_pic_lt_sps_flag[i]
  483. }
  484. }
  485. skip_bits1(gb); // sps_temporal_mvp_enabled_flag
  486. skip_bits1(gb); // strong_intra_smoothing_enabled_flag
  487. if (get_bits1(gb)) // vui_parameters_present_flag
  488. hvcc_parse_vui(gb, hvcc, sps_max_sub_layers_minus1);
  489. /* nothing useful for hvcC past this point */
  490. return 0;
  491. }
  492. static int hvcc_parse_pps(GetBitContext *gb,
  493. HEVCDecoderConfigurationRecord *hvcc)
  494. {
  495. uint8_t tiles_enabled_flag, entropy_coding_sync_enabled_flag;
  496. get_ue_golomb_long(gb); // pps_pic_parameter_set_id
  497. get_ue_golomb_long(gb); // pps_seq_parameter_set_id
  498. /*
  499. * dependent_slice_segments_enabled_flag u(1)
  500. * output_flag_present_flag u(1)
  501. * num_extra_slice_header_bits u(3)
  502. * sign_data_hiding_enabled_flag u(1)
  503. * cabac_init_present_flag u(1)
  504. */
  505. skip_bits(gb, 7);
  506. get_ue_golomb_long(gb); // num_ref_idx_l0_default_active_minus1
  507. get_ue_golomb_long(gb); // num_ref_idx_l1_default_active_minus1
  508. get_se_golomb_long(gb); // init_qp_minus26
  509. /*
  510. * constrained_intra_pred_flag u(1)
  511. * transform_skip_enabled_flag u(1)
  512. */
  513. skip_bits(gb, 2);
  514. if (get_bits1(gb)) // cu_qp_delta_enabled_flag
  515. get_ue_golomb_long(gb); // diff_cu_qp_delta_depth
  516. get_se_golomb_long(gb); // pps_cb_qp_offset
  517. get_se_golomb_long(gb); // pps_cr_qp_offset
  518. /*
  519. * pps_slice_chroma_qp_offsets_present_flag u(1)
  520. * weighted_pred_flag u(1)
  521. * weighted_bipred_flag u(1)
  522. * transquant_bypass_enabled_flag u(1)
  523. */
  524. skip_bits(gb, 4);
  525. tiles_enabled_flag = get_bits1(gb);
  526. entropy_coding_sync_enabled_flag = get_bits1(gb);
  527. if (entropy_coding_sync_enabled_flag && tiles_enabled_flag)
  528. hvcc->parallelismType = 0; // mixed-type parallel decoding
  529. else if (entropy_coding_sync_enabled_flag)
  530. hvcc->parallelismType = 3; // wavefront-based parallel decoding
  531. else if (tiles_enabled_flag)
  532. hvcc->parallelismType = 2; // tile-based parallel decoding
  533. else
  534. hvcc->parallelismType = 1; // slice-based parallel decoding
  535. /* nothing useful for hvcC past this point */
  536. return 0;
  537. }
  538. static void nal_unit_parse_header(GetBitContext *gb, uint8_t *nal_type)
  539. {
  540. skip_bits1(gb); // forbidden_zero_bit
  541. *nal_type = get_bits(gb, 6);
  542. /*
  543. * nuh_layer_id u(6)
  544. * nuh_temporal_id_plus1 u(3)
  545. */
  546. skip_bits(gb, 9);
  547. }
  548. static int hvcc_array_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size,
  549. uint8_t nal_type, int ps_array_completeness,
  550. HEVCDecoderConfigurationRecord *hvcc)
  551. {
  552. int ret;
  553. uint8_t index;
  554. uint16_t numNalus;
  555. HVCCNALUnitArray *array;
  556. for (index = 0; index < hvcc->numOfArrays; index++)
  557. if (hvcc->array[index].NAL_unit_type == nal_type)
  558. break;
  559. if (index >= hvcc->numOfArrays) {
  560. uint8_t i;
  561. ret = av_reallocp_array(&hvcc->array, index + 1, sizeof(HVCCNALUnitArray));
  562. if (ret < 0)
  563. return ret;
  564. for (i = hvcc->numOfArrays; i <= index; i++)
  565. memset(&hvcc->array[i], 0, sizeof(HVCCNALUnitArray));
  566. hvcc->numOfArrays = index + 1;
  567. }
  568. array = &hvcc->array[index];
  569. numNalus = array->numNalus;
  570. ret = av_reallocp_array(&array->nalUnit, numNalus + 1, sizeof(uint8_t*));
  571. if (ret < 0)
  572. return ret;
  573. ret = av_reallocp_array(&array->nalUnitLength, numNalus + 1, sizeof(uint16_t));
  574. if (ret < 0)
  575. return ret;
  576. array->nalUnit [numNalus] = nal_buf;
  577. array->nalUnitLength[numNalus] = nal_size;
  578. array->NAL_unit_type = nal_type;
  579. array->numNalus++;
  580. /*
  581. * When the sample entry name is ‘hvc1’, the default and mandatory value of
  582. * array_completeness is 1 for arrays of all types of parameter sets, and 0
  583. * for all other arrays. When the sample entry name is ‘hev1’, the default
  584. * value of array_completeness is 0 for all arrays.
  585. */
  586. if (nal_type == HEVC_NAL_VPS || nal_type == HEVC_NAL_SPS || nal_type == HEVC_NAL_PPS)
  587. array->array_completeness = ps_array_completeness;
  588. return 0;
  589. }
  590. static int hvcc_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size,
  591. int ps_array_completeness,
  592. HEVCDecoderConfigurationRecord *hvcc)
  593. {
  594. int ret = 0;
  595. GetBitContext gbc;
  596. uint8_t nal_type;
  597. uint8_t *rbsp_buf;
  598. uint32_t rbsp_size;
  599. rbsp_buf = ff_nal_unit_extract_rbsp(nal_buf, nal_size, &rbsp_size, 2);
  600. if (!rbsp_buf) {
  601. ret = AVERROR(ENOMEM);
  602. goto end;
  603. }
  604. ret = init_get_bits8(&gbc, rbsp_buf, rbsp_size);
  605. if (ret < 0)
  606. goto end;
  607. nal_unit_parse_header(&gbc, &nal_type);
  608. /*
  609. * Note: only 'declarative' SEI messages are allowed in
  610. * hvcC. Perhaps the SEI playload type should be checked
  611. * and non-declarative SEI messages discarded?
  612. */
  613. switch (nal_type) {
  614. case HEVC_NAL_VPS:
  615. case HEVC_NAL_SPS:
  616. case HEVC_NAL_PPS:
  617. case HEVC_NAL_SEI_PREFIX:
  618. case HEVC_NAL_SEI_SUFFIX:
  619. ret = hvcc_array_add_nal_unit(nal_buf, nal_size, nal_type,
  620. ps_array_completeness, hvcc);
  621. if (ret < 0)
  622. goto end;
  623. else if (nal_type == HEVC_NAL_VPS)
  624. ret = hvcc_parse_vps(&gbc, hvcc);
  625. else if (nal_type == HEVC_NAL_SPS)
  626. ret = hvcc_parse_sps(&gbc, hvcc);
  627. else if (nal_type == HEVC_NAL_PPS)
  628. ret = hvcc_parse_pps(&gbc, hvcc);
  629. if (ret < 0)
  630. goto end;
  631. break;
  632. default:
  633. ret = AVERROR_INVALIDDATA;
  634. goto end;
  635. }
  636. end:
  637. av_free(rbsp_buf);
  638. return ret;
  639. }
  640. static void hvcc_init(HEVCDecoderConfigurationRecord *hvcc)
  641. {
  642. memset(hvcc, 0, sizeof(HEVCDecoderConfigurationRecord));
  643. hvcc->configurationVersion = 1;
  644. hvcc->lengthSizeMinusOne = 3; // 4 bytes
  645. /*
  646. * The following fields have all their valid bits set by default,
  647. * the ProfileTierLevel parsing code will unset them when needed.
  648. */
  649. hvcc->general_profile_compatibility_flags = 0xffffffff;
  650. hvcc->general_constraint_indicator_flags = 0xffffffffffff;
  651. /*
  652. * Initialize this field with an invalid value which can be used to detect
  653. * whether we didn't see any VUI (in which case it should be reset to zero).
  654. */
  655. hvcc->min_spatial_segmentation_idc = MAX_SPATIAL_SEGMENTATION + 1;
  656. }
  657. static void hvcc_close(HEVCDecoderConfigurationRecord *hvcc)
  658. {
  659. uint8_t i;
  660. for (i = 0; i < hvcc->numOfArrays; i++) {
  661. hvcc->array[i].numNalus = 0;
  662. av_freep(&hvcc->array[i].nalUnit);
  663. av_freep(&hvcc->array[i].nalUnitLength);
  664. }
  665. hvcc->numOfArrays = 0;
  666. av_freep(&hvcc->array);
  667. }
  668. static int hvcc_write(AVIOContext *pb, HEVCDecoderConfigurationRecord *hvcc)
  669. {
  670. uint8_t i;
  671. uint16_t j, vps_count = 0, sps_count = 0, pps_count = 0;
  672. /*
  673. * We only support writing HEVCDecoderConfigurationRecord version 1.
  674. */
  675. hvcc->configurationVersion = 1;
  676. /*
  677. * If min_spatial_segmentation_idc is invalid, reset to 0 (unspecified).
  678. */
  679. if (hvcc->min_spatial_segmentation_idc > MAX_SPATIAL_SEGMENTATION)
  680. hvcc->min_spatial_segmentation_idc = 0;
  681. /*
  682. * parallelismType indicates the type of parallelism that is used to meet
  683. * the restrictions imposed by min_spatial_segmentation_idc when the value
  684. * of min_spatial_segmentation_idc is greater than 0.
  685. */
  686. if (!hvcc->min_spatial_segmentation_idc)
  687. hvcc->parallelismType = 0;
  688. /*
  689. * It's unclear how to properly compute these fields, so
  690. * let's always set them to values meaning 'unspecified'.
  691. */
  692. hvcc->avgFrameRate = 0;
  693. hvcc->constantFrameRate = 0;
  694. av_log(NULL, AV_LOG_TRACE, "configurationVersion: %"PRIu8"\n",
  695. hvcc->configurationVersion);
  696. av_log(NULL, AV_LOG_TRACE, "general_profile_space: %"PRIu8"\n",
  697. hvcc->general_profile_space);
  698. av_log(NULL, AV_LOG_TRACE, "general_tier_flag: %"PRIu8"\n",
  699. hvcc->general_tier_flag);
  700. av_log(NULL, AV_LOG_TRACE, "general_profile_idc: %"PRIu8"\n",
  701. hvcc->general_profile_idc);
  702. av_log(NULL, AV_LOG_TRACE, "general_profile_compatibility_flags: 0x%08"PRIx32"\n",
  703. hvcc->general_profile_compatibility_flags);
  704. av_log(NULL, AV_LOG_TRACE, "general_constraint_indicator_flags: 0x%012"PRIx64"\n",
  705. hvcc->general_constraint_indicator_flags);
  706. av_log(NULL, AV_LOG_TRACE, "general_level_idc: %"PRIu8"\n",
  707. hvcc->general_level_idc);
  708. av_log(NULL, AV_LOG_TRACE, "min_spatial_segmentation_idc: %"PRIu16"\n",
  709. hvcc->min_spatial_segmentation_idc);
  710. av_log(NULL, AV_LOG_TRACE, "parallelismType: %"PRIu8"\n",
  711. hvcc->parallelismType);
  712. av_log(NULL, AV_LOG_TRACE, "chromaFormat: %"PRIu8"\n",
  713. hvcc->chromaFormat);
  714. av_log(NULL, AV_LOG_TRACE, "bitDepthLumaMinus8: %"PRIu8"\n",
  715. hvcc->bitDepthLumaMinus8);
  716. av_log(NULL, AV_LOG_TRACE, "bitDepthChromaMinus8: %"PRIu8"\n",
  717. hvcc->bitDepthChromaMinus8);
  718. av_log(NULL, AV_LOG_TRACE, "avgFrameRate: %"PRIu16"\n",
  719. hvcc->avgFrameRate);
  720. av_log(NULL, AV_LOG_TRACE, "constantFrameRate: %"PRIu8"\n",
  721. hvcc->constantFrameRate);
  722. av_log(NULL, AV_LOG_TRACE, "numTemporalLayers: %"PRIu8"\n",
  723. hvcc->numTemporalLayers);
  724. av_log(NULL, AV_LOG_TRACE, "temporalIdNested: %"PRIu8"\n",
  725. hvcc->temporalIdNested);
  726. av_log(NULL, AV_LOG_TRACE, "lengthSizeMinusOne: %"PRIu8"\n",
  727. hvcc->lengthSizeMinusOne);
  728. av_log(NULL, AV_LOG_TRACE, "numOfArrays: %"PRIu8"\n",
  729. hvcc->numOfArrays);
  730. for (i = 0; i < hvcc->numOfArrays; i++) {
  731. av_log(NULL, AV_LOG_TRACE, "array_completeness[%"PRIu8"]: %"PRIu8"\n",
  732. i, hvcc->array[i].array_completeness);
  733. av_log(NULL, AV_LOG_TRACE, "NAL_unit_type[%"PRIu8"]: %"PRIu8"\n",
  734. i, hvcc->array[i].NAL_unit_type);
  735. av_log(NULL, AV_LOG_TRACE, "numNalus[%"PRIu8"]: %"PRIu16"\n",
  736. i, hvcc->array[i].numNalus);
  737. for (j = 0; j < hvcc->array[i].numNalus; j++)
  738. av_log(NULL, AV_LOG_TRACE,
  739. "nalUnitLength[%"PRIu8"][%"PRIu16"]: %"PRIu16"\n",
  740. i, j, hvcc->array[i].nalUnitLength[j]);
  741. }
  742. /*
  743. * We need at least one of each: VPS, SPS and PPS.
  744. */
  745. for (i = 0; i < hvcc->numOfArrays; i++)
  746. switch (hvcc->array[i].NAL_unit_type) {
  747. case HEVC_NAL_VPS:
  748. vps_count += hvcc->array[i].numNalus;
  749. break;
  750. case HEVC_NAL_SPS:
  751. sps_count += hvcc->array[i].numNalus;
  752. break;
  753. case HEVC_NAL_PPS:
  754. pps_count += hvcc->array[i].numNalus;
  755. break;
  756. default:
  757. break;
  758. }
  759. if (!vps_count || vps_count > HEVC_MAX_VPS_COUNT ||
  760. !sps_count || sps_count > HEVC_MAX_SPS_COUNT ||
  761. !pps_count || pps_count > HEVC_MAX_PPS_COUNT)
  762. return AVERROR_INVALIDDATA;
  763. /* unsigned int(8) configurationVersion = 1; */
  764. avio_w8(pb, hvcc->configurationVersion);
  765. /*
  766. * unsigned int(2) general_profile_space;
  767. * unsigned int(1) general_tier_flag;
  768. * unsigned int(5) general_profile_idc;
  769. */
  770. avio_w8(pb, hvcc->general_profile_space << 6 |
  771. hvcc->general_tier_flag << 5 |
  772. hvcc->general_profile_idc);
  773. /* unsigned int(32) general_profile_compatibility_flags; */
  774. avio_wb32(pb, hvcc->general_profile_compatibility_flags);
  775. /* unsigned int(48) general_constraint_indicator_flags; */
  776. avio_wb32(pb, hvcc->general_constraint_indicator_flags >> 16);
  777. avio_wb16(pb, hvcc->general_constraint_indicator_flags);
  778. /* unsigned int(8) general_level_idc; */
  779. avio_w8(pb, hvcc->general_level_idc);
  780. /*
  781. * bit(4) reserved = ‘1111’b;
  782. * unsigned int(12) min_spatial_segmentation_idc;
  783. */
  784. avio_wb16(pb, hvcc->min_spatial_segmentation_idc | 0xf000);
  785. /*
  786. * bit(6) reserved = ‘111111’b;
  787. * unsigned int(2) parallelismType;
  788. */
  789. avio_w8(pb, hvcc->parallelismType | 0xfc);
  790. /*
  791. * bit(6) reserved = ‘111111’b;
  792. * unsigned int(2) chromaFormat;
  793. */
  794. avio_w8(pb, hvcc->chromaFormat | 0xfc);
  795. /*
  796. * bit(5) reserved = ‘11111’b;
  797. * unsigned int(3) bitDepthLumaMinus8;
  798. */
  799. avio_w8(pb, hvcc->bitDepthLumaMinus8 | 0xf8);
  800. /*
  801. * bit(5) reserved = ‘11111’b;
  802. * unsigned int(3) bitDepthChromaMinus8;
  803. */
  804. avio_w8(pb, hvcc->bitDepthChromaMinus8 | 0xf8);
  805. /* bit(16) avgFrameRate; */
  806. avio_wb16(pb, hvcc->avgFrameRate);
  807. /*
  808. * bit(2) constantFrameRate;
  809. * bit(3) numTemporalLayers;
  810. * bit(1) temporalIdNested;
  811. * unsigned int(2) lengthSizeMinusOne;
  812. */
  813. avio_w8(pb, hvcc->constantFrameRate << 6 |
  814. hvcc->numTemporalLayers << 3 |
  815. hvcc->temporalIdNested << 2 |
  816. hvcc->lengthSizeMinusOne);
  817. /* unsigned int(8) numOfArrays; */
  818. avio_w8(pb, hvcc->numOfArrays);
  819. for (i = 0; i < hvcc->numOfArrays; i++) {
  820. /*
  821. * bit(1) array_completeness;
  822. * unsigned int(1) reserved = 0;
  823. * unsigned int(6) NAL_unit_type;
  824. */
  825. avio_w8(pb, hvcc->array[i].array_completeness << 7 |
  826. hvcc->array[i].NAL_unit_type & 0x3f);
  827. /* unsigned int(16) numNalus; */
  828. avio_wb16(pb, hvcc->array[i].numNalus);
  829. for (j = 0; j < hvcc->array[i].numNalus; j++) {
  830. /* unsigned int(16) nalUnitLength; */
  831. avio_wb16(pb, hvcc->array[i].nalUnitLength[j]);
  832. /* bit(8*nalUnitLength) nalUnit; */
  833. avio_write(pb, hvcc->array[i].nalUnit[j],
  834. hvcc->array[i].nalUnitLength[j]);
  835. }
  836. }
  837. return 0;
  838. }
  839. int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in,
  840. int size, int filter_ps, int *ps_count)
  841. {
  842. int num_ps = 0, ret = 0;
  843. uint8_t *buf, *end, *start = NULL;
  844. if (!filter_ps) {
  845. ret = ff_avc_parse_nal_units(pb, buf_in, size);
  846. goto end;
  847. }
  848. ret = ff_avc_parse_nal_units_buf(buf_in, &start, &size);
  849. if (ret < 0)
  850. goto end;
  851. ret = 0;
  852. buf = start;
  853. end = start + size;
  854. while (end - buf > 4) {
  855. uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
  856. uint8_t type = (buf[4] >> 1) & 0x3f;
  857. buf += 4;
  858. switch (type) {
  859. case HEVC_NAL_VPS:
  860. case HEVC_NAL_SPS:
  861. case HEVC_NAL_PPS:
  862. num_ps++;
  863. break;
  864. default:
  865. ret += 4 + len;
  866. avio_wb32(pb, len);
  867. avio_write(pb, buf, len);
  868. break;
  869. }
  870. buf += len;
  871. }
  872. end:
  873. av_free(start);
  874. if (ps_count)
  875. *ps_count = num_ps;
  876. return ret;
  877. }
  878. int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out,
  879. int *size, int filter_ps, int *ps_count)
  880. {
  881. AVIOContext *pb;
  882. int ret;
  883. ret = avio_open_dyn_buf(&pb);
  884. if (ret < 0)
  885. return ret;
  886. ret = ff_hevc_annexb2mp4(pb, buf_in, *size, filter_ps, ps_count);
  887. if (ret < 0) {
  888. ffio_free_dyn_buf(&pb);
  889. return ret;
  890. }
  891. *size = avio_close_dyn_buf(pb, buf_out);
  892. return 0;
  893. }
  894. int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data,
  895. int size, int ps_array_completeness)
  896. {
  897. int ret = 0;
  898. uint8_t *buf, *end, *start = NULL;
  899. HEVCDecoderConfigurationRecord hvcc;
  900. hvcc_init(&hvcc);
  901. if (size < 6) {
  902. /* We can't write a valid hvcC from the provided data */
  903. ret = AVERROR_INVALIDDATA;
  904. goto end;
  905. } else if (*data == 1) {
  906. /* Data is already hvcC-formatted */
  907. avio_write(pb, data, size);
  908. goto end;
  909. } else if (!(AV_RB24(data) == 1 || AV_RB32(data) == 1)) {
  910. /* Not a valid Annex B start code prefix */
  911. ret = AVERROR_INVALIDDATA;
  912. goto end;
  913. }
  914. ret = ff_avc_parse_nal_units_buf(data, &start, &size);
  915. if (ret < 0)
  916. goto end;
  917. buf = start;
  918. end = start + size;
  919. while (end - buf > 4) {
  920. uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
  921. uint8_t type = (buf[4] >> 1) & 0x3f;
  922. buf += 4;
  923. switch (type) {
  924. case HEVC_NAL_VPS:
  925. case HEVC_NAL_SPS:
  926. case HEVC_NAL_PPS:
  927. case HEVC_NAL_SEI_PREFIX:
  928. case HEVC_NAL_SEI_SUFFIX:
  929. ret = hvcc_add_nal_unit(buf, len, ps_array_completeness, &hvcc);
  930. if (ret < 0)
  931. goto end;
  932. break;
  933. default:
  934. break;
  935. }
  936. buf += len;
  937. }
  938. ret = hvcc_write(pb, &hvcc);
  939. end:
  940. hvcc_close(&hvcc);
  941. av_free(start);
  942. return ret;
  943. }