2 * Copyright (c) 2014 Tim Walker <tdskywalker@gmail.com>
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "libavcodec/get_bits.h"
22 #include "libavcodec/golomb.h"
23 #include "libavcodec/hevc.h"
24 #include "libavutil/intreadwrite.h"
29 #define MAX_SPATIAL_SEGMENTATION 4096 // max. value of u(12) field
31 typedef struct HVCCNALUnitArray {
32 uint8_t array_completeness;
33 uint8_t NAL_unit_type;
35 uint16_t *nalUnitLength;
39 typedef struct HEVCDecoderConfigurationRecord {
40 uint8_t configurationVersion;
41 uint8_t general_profile_space;
42 uint8_t general_tier_flag;
43 uint8_t general_profile_idc;
44 uint32_t general_profile_compatibility_flags;
45 uint64_t general_constraint_indicator_flags;
46 uint8_t general_level_idc;
47 uint16_t min_spatial_segmentation_idc;
48 uint8_t parallelismType;
50 uint8_t bitDepthLumaMinus8;
51 uint8_t bitDepthChromaMinus8;
52 uint16_t avgFrameRate;
53 uint8_t constantFrameRate;
54 uint8_t numTemporalLayers;
55 uint8_t temporalIdNested;
56 uint8_t lengthSizeMinusOne;
58 HVCCNALUnitArray *array;
59 } HEVCDecoderConfigurationRecord;
61 typedef struct HVCCProfileTierLevel {
62 uint8_t profile_space;
65 uint32_t profile_compatibility_flags;
66 uint64_t constraint_indicator_flags;
68 } HVCCProfileTierLevel;
70 static void hvcc_update_ptl(HEVCDecoderConfigurationRecord *hvcc,
71 HVCCProfileTierLevel *ptl)
74 * The value of general_profile_space in all the parameter sets must be
77 hvcc->general_profile_space = ptl->profile_space;
80 * The level indication general_level_idc must indicate a level of
81 * capability equal to or greater than the highest level indicated for the
82 * highest tier in all the parameter sets.
84 if (hvcc->general_tier_flag < ptl->tier_flag)
85 hvcc->general_level_idc = ptl->level_idc;
87 hvcc->general_level_idc = FFMAX(hvcc->general_level_idc, ptl->level_idc);
90 * The tier indication general_tier_flag must indicate a tier equal to or
91 * greater than the highest tier indicated in all the parameter sets.
93 hvcc->general_tier_flag = FFMAX(hvcc->general_tier_flag, ptl->tier_flag);
96 * The profile indication general_profile_idc must indicate a profile to
97 * which the stream associated with this configuration record conforms.
99 * If the sequence parameter sets are marked with different profiles, then
100 * the stream may need examination to determine which profile, if any, the
101 * entire stream conforms to. If the entire stream is not examined, or the
102 * examination reveals that there is no profile to which the entire stream
103 * conforms, then the entire stream must be split into two or more
104 * sub-streams with separate configuration records in which these rules can
107 * Note: set the profile to the highest value for the sake of simplicity.
109 hvcc->general_profile_idc = FFMAX(hvcc->general_profile_idc, ptl->profile_idc);
112 * Each bit in general_profile_compatibility_flags may only be set if all
113 * the parameter sets set that bit.
115 hvcc->general_profile_compatibility_flags &= ptl->profile_compatibility_flags;
118 * Each bit in general_constraint_indicator_flags may only be set if all
119 * the parameter sets set that bit.
121 hvcc->general_constraint_indicator_flags &= ptl->constraint_indicator_flags;
124 static void hvcc_parse_ptl(GetBitContext *gb,
125 HEVCDecoderConfigurationRecord *hvcc,
126 unsigned int max_sub_layers_minus1)
129 HVCCProfileTierLevel general_ptl;
130 uint8_t sub_layer_profile_present_flag[MAX_SUB_LAYERS];
131 uint8_t sub_layer_level_present_flag[MAX_SUB_LAYERS];
133 general_ptl.profile_space = get_bits(gb, 2);
134 general_ptl.tier_flag = get_bits1(gb);
135 general_ptl.profile_idc = get_bits(gb, 5);
136 general_ptl.profile_compatibility_flags = get_bits_long(gb, 32);
137 general_ptl.constraint_indicator_flags = get_bits64(gb, 48);
138 general_ptl.level_idc = get_bits(gb, 8);
139 hvcc_update_ptl(hvcc, &general_ptl);
141 for (i = 0; i < max_sub_layers_minus1; i++) {
142 sub_layer_profile_present_flag[i] = get_bits1(gb);
143 sub_layer_level_present_flag[i] = get_bits1(gb);
146 if (max_sub_layers_minus1 > 0)
147 for (i = max_sub_layers_minus1; i < 8; i++)
148 skip_bits(gb, 2); // reserved_zero_2bits[i]
150 for (i = 0; i < max_sub_layers_minus1; i++) {
151 if (sub_layer_profile_present_flag[i]) {
153 * sub_layer_profile_space[i] u(2)
154 * sub_layer_tier_flag[i] u(1)
155 * sub_layer_profile_idc[i] u(5)
156 * sub_layer_profile_compatibility_flag[i][0..31] u(32)
157 * sub_layer_progressive_source_flag[i] u(1)
158 * sub_layer_interlaced_source_flag[i] u(1)
159 * sub_layer_non_packed_constraint_flag[i] u(1)
160 * sub_layer_frame_only_constraint_flag[i] u(1)
161 * sub_layer_reserved_zero_44bits[i] u(44)
163 skip_bits_long(gb, 32);
164 skip_bits_long(gb, 32);
168 if (sub_layer_level_present_flag[i])
173 static void skip_sub_layer_hrd_parameters(GetBitContext *gb,
174 unsigned int cpb_cnt_minus1,
175 uint8_t sub_pic_hrd_params_present_flag)
179 for (i = 0; i <= cpb_cnt_minus1; i++) {
180 get_ue_golomb_long(gb); // bit_rate_value_minus1
181 get_ue_golomb_long(gb); // cpb_size_value_minus1
183 if (sub_pic_hrd_params_present_flag) {
184 get_ue_golomb_long(gb); // cpb_size_du_value_minus1
185 get_ue_golomb_long(gb); // bit_rate_du_value_minus1
188 skip_bits1(gb); // cbr_flag
192 static void skip_hrd_parameters(GetBitContext *gb, uint8_t cprms_present_flag,
193 unsigned int max_sub_layers_minus1)
196 uint8_t sub_pic_hrd_params_present_flag = 0;
197 uint8_t nal_hrd_parameters_present_flag = 0;
198 uint8_t vcl_hrd_parameters_present_flag = 0;
200 if (cprms_present_flag) {
201 nal_hrd_parameters_present_flag = get_bits1(gb);
202 vcl_hrd_parameters_present_flag = get_bits1(gb);
204 if (nal_hrd_parameters_present_flag ||
205 vcl_hrd_parameters_present_flag) {
206 sub_pic_hrd_params_present_flag = get_bits1(gb);
208 if (sub_pic_hrd_params_present_flag)
210 * tick_divisor_minus2 u(8)
211 * du_cpb_removal_delay_increment_length_minus1 u(5)
212 * sub_pic_cpb_params_in_pic_timing_sei_flag u(1)
213 * dpb_output_delay_du_length_minus1 u(5)
218 * bit_rate_scale u(4)
219 * cpb_size_scale u(4)
223 if (sub_pic_hrd_params_present_flag)
224 skip_bits(gb, 4); // cpb_size_du_scale
227 * initial_cpb_removal_delay_length_minus1 u(5)
228 * au_cpb_removal_delay_length_minus1 u(5)
229 * dpb_output_delay_length_minus1 u(5)
235 for (i = 0; i <= max_sub_layers_minus1; i++) {
236 unsigned int cpb_cnt_minus1 = 0;
237 uint8_t low_delay_hrd_flag = 0;
238 uint8_t fixed_pic_rate_within_cvs_flag = 0;
239 uint8_t fixed_pic_rate_general_flag = get_bits1(gb);
241 if (!fixed_pic_rate_general_flag)
242 fixed_pic_rate_within_cvs_flag = get_bits1(gb);
244 if (fixed_pic_rate_within_cvs_flag)
245 get_ue_golomb_long(gb); // elemental_duration_in_tc_minus1
247 low_delay_hrd_flag = get_bits1(gb);
249 if (!low_delay_hrd_flag)
250 cpb_cnt_minus1 = get_ue_golomb_long(gb);
252 if (nal_hrd_parameters_present_flag)
253 skip_sub_layer_hrd_parameters(gb, cpb_cnt_minus1,
254 sub_pic_hrd_params_present_flag);
256 if (vcl_hrd_parameters_present_flag)
257 skip_sub_layer_hrd_parameters(gb, cpb_cnt_minus1,
258 sub_pic_hrd_params_present_flag);
262 static void skip_timing_info(GetBitContext *gb)
264 skip_bits_long(gb, 32); // num_units_in_tick
265 skip_bits_long(gb, 32); // time_scale
267 if (get_bits1(gb)) // poc_proportional_to_timing_flag
268 get_ue_golomb_long(gb); // num_ticks_poc_diff_one_minus1
271 static void hvcc_parse_vui(GetBitContext *gb,
272 HEVCDecoderConfigurationRecord *hvcc,
273 unsigned int max_sub_layers_minus1)
275 unsigned int min_spatial_segmentation_idc;
277 if (get_bits1(gb)) // aspect_ratio_info_present_flag
278 if (get_bits(gb, 8) == 255) // aspect_ratio_idc
279 skip_bits_long(gb, 32); // sar_width u(16), sar_height u(16)
281 if (get_bits1(gb)) // overscan_info_present_flag
282 skip_bits1(gb); // overscan_appropriate_flag
284 if (get_bits1(gb)) { // video_signal_type_present_flag
285 skip_bits(gb, 4); // video_format u(3), video_full_range_flag u(1)
287 if (get_bits1(gb)) // colour_description_present_flag
289 * colour_primaries u(8)
290 * transfer_characteristics u(8)
296 if (get_bits1(gb)) { // chroma_loc_info_present_flag
297 get_ue_golomb_long(gb); // chroma_sample_loc_type_top_field
298 get_ue_golomb_long(gb); // chroma_sample_loc_type_bottom_field
302 * neutral_chroma_indication_flag u(1)
303 * field_seq_flag u(1)
304 * frame_field_info_present_flag u(1)
308 if (get_bits1(gb)) { // default_display_window_flag
309 get_ue_golomb_long(gb); // def_disp_win_left_offset
310 get_ue_golomb_long(gb); // def_disp_win_right_offset
311 get_ue_golomb_long(gb); // def_disp_win_top_offset
312 get_ue_golomb_long(gb); // def_disp_win_bottom_offset
315 if (get_bits1(gb)) { // vui_timing_info_present_flag
316 skip_timing_info(gb);
318 if (get_bits1(gb)) // vui_hrd_parameters_present_flag
319 skip_hrd_parameters(gb, 1, max_sub_layers_minus1);
322 if (get_bits1(gb)) { // bitstream_restriction_flag
324 * tiles_fixed_structure_flag u(1)
325 * motion_vectors_over_pic_boundaries_flag u(1)
326 * restricted_ref_pic_lists_flag u(1)
330 min_spatial_segmentation_idc = get_ue_golomb_long(gb);
333 * unsigned int(12) min_spatial_segmentation_idc;
335 * The min_spatial_segmentation_idc indication must indicate a level of
336 * spatial segmentation equal to or less than the lowest level of
337 * spatial segmentation indicated in all the parameter sets.
339 hvcc->min_spatial_segmentation_idc = FFMIN(hvcc->min_spatial_segmentation_idc,
340 min_spatial_segmentation_idc);
342 get_ue_golomb_long(gb); // max_bytes_per_pic_denom
343 get_ue_golomb_long(gb); // max_bits_per_min_cu_denom
344 get_ue_golomb_long(gb); // log2_max_mv_length_horizontal
345 get_ue_golomb_long(gb); // log2_max_mv_length_vertical
349 static void skip_sub_layer_ordering_info(GetBitContext *gb)
351 get_ue_golomb_long(gb); // max_dec_pic_buffering_minus1
352 get_ue_golomb_long(gb); // max_num_reorder_pics
353 get_ue_golomb_long(gb); // max_latency_increase_plus1
356 static int hvcc_parse_vps(GetBitContext *gb,
357 HEVCDecoderConfigurationRecord *hvcc)
359 unsigned int vps_max_sub_layers_minus1;
362 * vps_video_parameter_set_id u(4)
363 * vps_reserved_three_2bits u(2)
364 * vps_max_layers_minus1 u(6)
368 vps_max_sub_layers_minus1 = get_bits(gb, 3);
371 * numTemporalLayers greater than 1 indicates that the stream to which this
372 * configuration record applies is temporally scalable and the contained
373 * number of temporal layers (also referred to as temporal sub-layer or
374 * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
375 * indicates that the stream is not temporally scalable. Value 0 indicates
376 * that it is unknown whether the stream is temporally scalable.
378 hvcc->numTemporalLayers = FFMAX(hvcc->numTemporalLayers,
379 vps_max_sub_layers_minus1 + 1);
382 * vps_temporal_id_nesting_flag u(1)
383 * vps_reserved_0xffff_16bits u(16)
387 hvcc_parse_ptl(gb, hvcc, vps_max_sub_layers_minus1);
389 /* nothing useful for hvcC past this point */
393 static void skip_scaling_list_data(GetBitContext *gb)
395 int i, j, k, num_coeffs;
397 for (i = 0; i < 4; i++)
398 for (j = 0; j < (i == 3 ? 2 : 6); j++)
399 if (!get_bits1(gb)) // scaling_list_pred_mode_flag[i][j]
400 get_ue_golomb_long(gb); // scaling_list_pred_matrix_id_delta[i][j]
402 num_coeffs = FFMIN(64, 1 << (4 + (i << 1)));
405 get_se_golomb_long(gb); // scaling_list_dc_coef_minus8[i-2][j]
407 for (k = 0; k < num_coeffs; k++)
408 get_se_golomb_long(gb); // scaling_list_delta_coef
412 static int parse_rps(GetBitContext *gb, unsigned int rps_idx,
413 unsigned int num_rps,
414 unsigned int num_delta_pocs[MAX_SHORT_TERM_RPS_COUNT])
418 if (rps_idx && get_bits1(gb)) { // inter_ref_pic_set_prediction_flag
419 /* this should only happen for slice headers, and this isn't one */
420 if (rps_idx >= num_rps)
421 return AVERROR_INVALIDDATA;
423 skip_bits1 (gb); // delta_rps_sign
424 get_ue_golomb_long(gb); // abs_delta_rps_minus1
426 num_delta_pocs[rps_idx] = 0;
429 * From libavcodec/hevc_ps.c:
431 * if (is_slice_header) {
434 * rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
437 * rps: &sps->st_rps[rps_idx]
438 * sps->st_rps: &sps->st_rps[0]
439 * is_slice_header: rps_idx == num_rps
442 * if (num_rps != rps_idx)
443 * rps_ridx = &sps->st_rps[rps_idx - 1];
445 * NumDeltaPocs[RefRpsIdx]: num_delta_pocs[rps_idx - 1]
447 for (i = 0; i < num_delta_pocs[rps_idx - 1]; i++) {
448 uint8_t use_delta_flag = 0;
449 uint8_t used_by_curr_pic_flag = get_bits1(gb);
450 if (!used_by_curr_pic_flag)
451 use_delta_flag = get_bits1(gb);
453 if (used_by_curr_pic_flag || use_delta_flag)
454 num_delta_pocs[rps_idx]++;
457 unsigned int num_negative_pics = get_ue_golomb_long(gb);
458 unsigned int num_positive_pics = get_ue_golomb_long(gb);
460 num_delta_pocs[rps_idx] = num_negative_pics + num_positive_pics;
462 for (i = 0; i < num_negative_pics; i++) {
463 get_ue_golomb_long(gb); // delta_poc_s0_minus1[rps_idx]
464 skip_bits1 (gb); // used_by_curr_pic_s0_flag[rps_idx]
467 for (i = 0; i < num_positive_pics; i++) {
468 get_ue_golomb_long(gb); // delta_poc_s1_minus1[rps_idx]
469 skip_bits1 (gb); // used_by_curr_pic_s1_flag[rps_idx]
476 static int hvcc_parse_sps(GetBitContext *gb,
477 HEVCDecoderConfigurationRecord *hvcc)
479 unsigned int i, sps_max_sub_layers_minus1, log2_max_pic_order_cnt_lsb_minus4;
480 unsigned int num_short_term_ref_pic_sets, num_delta_pocs[MAX_SHORT_TERM_RPS_COUNT];
482 skip_bits(gb, 4); // sps_video_parameter_set_id
484 sps_max_sub_layers_minus1 = get_bits (gb, 3);
487 * numTemporalLayers greater than 1 indicates that the stream to which this
488 * configuration record applies is temporally scalable and the contained
489 * number of temporal layers (also referred to as temporal sub-layer or
490 * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
491 * indicates that the stream is not temporally scalable. Value 0 indicates
492 * that it is unknown whether the stream is temporally scalable.
494 hvcc->numTemporalLayers = FFMAX(hvcc->numTemporalLayers,
495 sps_max_sub_layers_minus1 + 1);
497 hvcc->temporalIdNested = get_bits1(gb);
499 hvcc_parse_ptl(gb, hvcc, sps_max_sub_layers_minus1);
501 get_ue_golomb_long(gb); // sps_seq_parameter_set_id
503 hvcc->chromaFormat = get_ue_golomb_long(gb);
505 if (hvcc->chromaFormat == 3)
506 skip_bits1(gb); // separate_colour_plane_flag
508 get_ue_golomb_long(gb); // pic_width_in_luma_samples
509 get_ue_golomb_long(gb); // pic_height_in_luma_samples
511 if (get_bits1(gb)) { // conformance_window_flag
512 get_ue_golomb_long(gb); // conf_win_left_offset
513 get_ue_golomb_long(gb); // conf_win_right_offset
514 get_ue_golomb_long(gb); // conf_win_top_offset
515 get_ue_golomb_long(gb); // conf_win_bottom_offset
518 hvcc->bitDepthLumaMinus8 = get_ue_golomb_long(gb);
519 hvcc->bitDepthChromaMinus8 = get_ue_golomb_long(gb);
520 log2_max_pic_order_cnt_lsb_minus4 = get_ue_golomb_long(gb);
522 /* sps_sub_layer_ordering_info_present_flag */
523 i = get_bits1(gb) ? 0 : sps_max_sub_layers_minus1;
524 for (; i <= sps_max_sub_layers_minus1; i++)
525 skip_sub_layer_ordering_info(gb);
527 get_ue_golomb_long(gb); // log2_min_luma_coding_block_size_minus3
528 get_ue_golomb_long(gb); // log2_diff_max_min_luma_coding_block_size
529 get_ue_golomb_long(gb); // log2_min_transform_block_size_minus2
530 get_ue_golomb_long(gb); // log2_diff_max_min_transform_block_size
531 get_ue_golomb_long(gb); // max_transform_hierarchy_depth_inter
532 get_ue_golomb_long(gb); // max_transform_hierarchy_depth_intra
534 if (get_bits1(gb) && // scaling_list_enabled_flag
535 get_bits1(gb)) // sps_scaling_list_data_present_flag
536 skip_scaling_list_data(gb);
538 skip_bits1(gb); // amp_enabled_flag
539 skip_bits1(gb); // sample_adaptive_offset_enabled_flag
541 if (get_bits1(gb)) { // pcm_enabled_flag
542 skip_bits (gb, 4); // pcm_sample_bit_depth_luma_minus1
543 skip_bits (gb, 4); // pcm_sample_bit_depth_chroma_minus1
544 get_ue_golomb_long(gb); // log2_min_pcm_luma_coding_block_size_minus3
545 get_ue_golomb_long(gb); // log2_diff_max_min_pcm_luma_coding_block_size
546 skip_bits1 (gb); // pcm_loop_filter_disabled_flag
549 num_short_term_ref_pic_sets = get_ue_golomb_long(gb);
550 if (num_short_term_ref_pic_sets > MAX_SHORT_TERM_RPS_COUNT)
551 return AVERROR_INVALIDDATA;
553 for (i = 0; i < num_short_term_ref_pic_sets; i++) {
554 int ret = parse_rps(gb, i, num_short_term_ref_pic_sets, num_delta_pocs);
559 if (get_bits1(gb)) { // long_term_ref_pics_present_flag
560 for (i = 0; i < get_ue_golomb_long(gb); i++) { // num_long_term_ref_pics_sps
561 int len = FFMIN(log2_max_pic_order_cnt_lsb_minus4 + 4, 16);
562 skip_bits (gb, len); // lt_ref_pic_poc_lsb_sps[i]
563 skip_bits1(gb); // used_by_curr_pic_lt_sps_flag[i]
567 skip_bits1(gb); // sps_temporal_mvp_enabled_flag
568 skip_bits1(gb); // strong_intra_smoothing_enabled_flag
570 if (get_bits1(gb)) // vui_parameters_present_flag
571 hvcc_parse_vui(gb, hvcc, sps_max_sub_layers_minus1);
573 /* nothing useful for hvcC past this point */
577 static int hvcc_parse_pps(GetBitContext *gb,
578 HEVCDecoderConfigurationRecord *hvcc)
580 uint8_t tiles_enabled_flag, entropy_coding_sync_enabled_flag;
582 get_ue_golomb_long(gb); // pps_pic_parameter_set_id
583 get_ue_golomb_long(gb); // pps_seq_parameter_set_id
586 * dependent_slice_segments_enabled_flag u(1)
587 * output_flag_present_flag u(1)
588 * num_extra_slice_header_bits u(3)
589 * sign_data_hiding_enabled_flag u(1)
590 * cabac_init_present_flag u(1)
594 get_ue_golomb_long(gb); // num_ref_idx_l0_default_active_minus1
595 get_ue_golomb_long(gb); // num_ref_idx_l1_default_active_minus1
596 get_se_golomb_long(gb); // init_qp_minus26
599 * constrained_intra_pred_flag u(1)
600 * transform_skip_enabled_flag u(1)
604 if (get_bits1(gb)) // cu_qp_delta_enabled_flag
605 get_ue_golomb_long(gb); // diff_cu_qp_delta_depth
607 get_se_golomb_long(gb); // pps_cb_qp_offset
608 get_se_golomb_long(gb); // pps_cr_qp_offset
611 * weighted_pred_flag u(1)
612 * weighted_bipred_flag u(1)
613 * transquant_bypass_enabled_flag u(1)
617 tiles_enabled_flag = get_bits1(gb);
618 entropy_coding_sync_enabled_flag = get_bits1(gb);
620 if (entropy_coding_sync_enabled_flag && tiles_enabled_flag)
621 hvcc->parallelismType = 0; // mixed-type parallel decoding
622 else if (entropy_coding_sync_enabled_flag)
623 hvcc->parallelismType = 3; // wavefront-based parallel decoding
624 else if (tiles_enabled_flag)
625 hvcc->parallelismType = 2; // tile-based parallel decoding
627 hvcc->parallelismType = 1; // slice-based parallel decoding
629 /* nothing useful for hvcC past this point */
633 static uint8_t *nal_unit_extract_rbsp(const uint8_t *src, uint32_t src_len,
639 dst = av_malloc(src_len);
643 /* NAL unit header (2 bytes) */
645 while (i < 2 && i < src_len)
646 dst[len++] = src[i++];
648 while (i + 2 < src_len)
649 if (!src[i] && !src[i + 1] && src[i + 2] == 3) {
650 dst[len++] = src[i++];
651 dst[len++] = src[i++];
652 i++; // remove emulation_prevention_three_byte
654 dst[len++] = src[i++];
657 dst[len++] = src[i++];
665 static void nal_unit_parse_header(GetBitContext *gb, uint8_t *nal_type)
667 skip_bits1(gb); // forbidden_zero_bit
669 *nal_type = get_bits(gb, 6);
673 * nuh_temporal_id_plus1 u(3)
678 static int hvcc_array_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size,
679 uint8_t nal_type, int ps_array_completeness,
680 HEVCDecoderConfigurationRecord *hvcc)
685 HVCCNALUnitArray *array;
687 for (index = 0; index < hvcc->numOfArrays; index++)
688 if (hvcc->array[index].NAL_unit_type == nal_type)
691 if (index >= hvcc->numOfArrays) {
694 ret = av_reallocp_array(&hvcc->array, index + 1, sizeof(HVCCNALUnitArray));
698 for (i = hvcc->numOfArrays; i <= index; i++)
699 memset(&hvcc->array[i], 0, sizeof(HVCCNALUnitArray));
700 hvcc->numOfArrays = index + 1;
703 array = &hvcc->array[index];
704 numNalus = array->numNalus;
706 ret = av_reallocp_array(&array->nalUnit, numNalus + 1, sizeof(uint8_t*));
710 ret = av_reallocp_array(&array->nalUnitLength, numNalus + 1, sizeof(uint16_t));
714 array->nalUnit [numNalus] = nal_buf;
715 array->nalUnitLength[numNalus] = nal_size;
716 array->NAL_unit_type = nal_type;
720 * When the sample entry name is ‘hvc1’, the default and mandatory value of
721 * array_completeness is 1 for arrays of all types of parameter sets, and 0
722 * for all other arrays. When the sample entry name is ‘hev1’, the default
723 * value of array_completeness is 0 for all arrays.
725 if (nal_type == NAL_VPS || nal_type == NAL_SPS || nal_type == NAL_PPS)
726 array->array_completeness = ps_array_completeness;
731 static int hvcc_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size,
732 int ps_array_completeness,
733 HEVCDecoderConfigurationRecord *hvcc)
741 rbsp_buf = nal_unit_extract_rbsp(nal_buf, nal_size, &rbsp_size);
743 ret = AVERROR(ENOMEM);
747 ret = init_get_bits8(&gbc, rbsp_buf, rbsp_size);
751 nal_unit_parse_header(&gbc, &nal_type);
754 * Note: only 'declarative' SEI messages are allowed in
755 * hvcC. Perhaps the SEI playload type should be checked
756 * and non-declarative SEI messages discarded?
764 ret = hvcc_array_add_nal_unit(nal_buf, nal_size, nal_type,
765 ps_array_completeness, hvcc);
768 else if (nal_type == NAL_VPS)
769 ret = hvcc_parse_vps(&gbc, hvcc);
770 else if (nal_type == NAL_SPS)
771 ret = hvcc_parse_sps(&gbc, hvcc);
772 else if (nal_type == NAL_PPS)
773 ret = hvcc_parse_pps(&gbc, hvcc);
778 ret = AVERROR_INVALIDDATA;
787 static void hvcc_init(HEVCDecoderConfigurationRecord *hvcc)
789 memset(hvcc, 0, sizeof(HEVCDecoderConfigurationRecord));
790 hvcc->configurationVersion = 1;
791 hvcc->lengthSizeMinusOne = 3; // 4 bytes
794 * The following fields have all their valid bits set by default,
795 * the ProfileTierLevel parsing code will unset them when needed.
797 hvcc->general_profile_compatibility_flags = 0xffffffff;
798 hvcc->general_constraint_indicator_flags = 0xffffffffffff;
801 * Initialize this field with an invalid value which can be used to detect
802 * whether we didn't see any VUI (in which case it should be reset to zero).
804 hvcc->min_spatial_segmentation_idc = MAX_SPATIAL_SEGMENTATION + 1;
807 static void hvcc_close(HEVCDecoderConfigurationRecord *hvcc)
811 for (i = 0; i < hvcc->numOfArrays; i++) {
812 hvcc->array[i].numNalus = 0;
813 av_freep(&hvcc->array[i].nalUnit);
814 av_freep(&hvcc->array[i].nalUnitLength);
817 hvcc->numOfArrays = 0;
818 av_freep(&hvcc->array);
821 static int hvcc_write(AVIOContext *pb, HEVCDecoderConfigurationRecord *hvcc)
824 uint16_t j, vps_count = 0, sps_count = 0, pps_count = 0;
827 * We only support writing HEVCDecoderConfigurationRecord version 1.
829 hvcc->configurationVersion = 1;
832 * If min_spatial_segmentation_idc is invalid, reset to 0 (unspecified).
834 if (hvcc->min_spatial_segmentation_idc > MAX_SPATIAL_SEGMENTATION)
835 hvcc->min_spatial_segmentation_idc = 0;
838 * parallelismType indicates the type of parallelism that is used to meet
839 * the restrictions imposed by min_spatial_segmentation_idc when the value
840 * of min_spatial_segmentation_idc is greater than 0.
842 if (!hvcc->min_spatial_segmentation_idc)
843 hvcc->parallelismType = 0;
846 * It's unclear how to properly compute these fields, so
847 * let's always set them to values meaning 'unspecified'.
849 hvcc->avgFrameRate = 0;
850 hvcc->constantFrameRate = 0;
852 av_dlog(NULL, "configurationVersion: %"PRIu8"\n",
853 hvcc->configurationVersion);
854 av_dlog(NULL, "general_profile_space: %"PRIu8"\n",
855 hvcc->general_profile_space);
856 av_dlog(NULL, "general_tier_flag: %"PRIu8"\n",
857 hvcc->general_tier_flag);
858 av_dlog(NULL, "general_profile_idc: %"PRIu8"\n",
859 hvcc->general_profile_idc);
860 av_dlog(NULL, "general_profile_compatibility_flags: 0x%08"PRIx32"\n",
861 hvcc->general_profile_compatibility_flags);
862 av_dlog(NULL, "general_constraint_indicator_flags: 0x%012"PRIx64"\n",
863 hvcc->general_constraint_indicator_flags);
864 av_dlog(NULL, "general_level_idc: %"PRIu8"\n",
865 hvcc->general_level_idc);
866 av_dlog(NULL, "min_spatial_segmentation_idc: %"PRIu16"\n",
867 hvcc->min_spatial_segmentation_idc);
868 av_dlog(NULL, "parallelismType: %"PRIu8"\n",
869 hvcc->parallelismType);
870 av_dlog(NULL, "chromaFormat: %"PRIu8"\n",
872 av_dlog(NULL, "bitDepthLumaMinus8: %"PRIu8"\n",
873 hvcc->bitDepthLumaMinus8);
874 av_dlog(NULL, "bitDepthChromaMinus8: %"PRIu8"\n",
875 hvcc->bitDepthChromaMinus8);
876 av_dlog(NULL, "avgFrameRate: %"PRIu16"\n",
878 av_dlog(NULL, "constantFrameRate: %"PRIu8"\n",
879 hvcc->constantFrameRate);
880 av_dlog(NULL, "numTemporalLayers: %"PRIu8"\n",
881 hvcc->numTemporalLayers);
882 av_dlog(NULL, "temporalIdNested: %"PRIu8"\n",
883 hvcc->temporalIdNested);
884 av_dlog(NULL, "lengthSizeMinusOne: %"PRIu8"\n",
885 hvcc->lengthSizeMinusOne);
886 av_dlog(NULL, "numOfArrays: %"PRIu8"\n",
888 for (i = 0; i < hvcc->numOfArrays; i++) {
889 av_dlog(NULL, "array_completeness[%"PRIu8"]: %"PRIu8"\n",
890 i, hvcc->array[i].array_completeness);
891 av_dlog(NULL, "NAL_unit_type[%"PRIu8"]: %"PRIu8"\n",
892 i, hvcc->array[i].NAL_unit_type);
893 av_dlog(NULL, "numNalus[%"PRIu8"]: %"PRIu16"\n",
894 i, hvcc->array[i].numNalus);
895 for (j = 0; j < hvcc->array[i].numNalus; j++)
897 "nalUnitLength[%"PRIu8"][%"PRIu16"]: %"PRIu16"\n",
898 i, j, hvcc->array[i].nalUnitLength[j]);
902 * We need at least one of each: VPS, SPS and PPS.
904 for (i = 0; i < hvcc->numOfArrays; i++)
905 switch (hvcc->array[i].NAL_unit_type) {
907 vps_count += hvcc->array[i].numNalus;
910 sps_count += hvcc->array[i].numNalus;
913 pps_count += hvcc->array[i].numNalus;
918 if (!vps_count || vps_count > MAX_VPS_COUNT ||
919 !sps_count || sps_count > MAX_SPS_COUNT ||
920 !pps_count || pps_count > MAX_PPS_COUNT)
921 return AVERROR_INVALIDDATA;
923 /* unsigned int(8) configurationVersion = 1; */
924 avio_w8(pb, hvcc->configurationVersion);
927 * unsigned int(2) general_profile_space;
928 * unsigned int(1) general_tier_flag;
929 * unsigned int(5) general_profile_idc;
931 avio_w8(pb, hvcc->general_profile_space << 6 |
932 hvcc->general_tier_flag << 5 |
933 hvcc->general_profile_idc);
935 /* unsigned int(32) general_profile_compatibility_flags; */
936 avio_wb32(pb, hvcc->general_profile_compatibility_flags);
938 /* unsigned int(48) general_constraint_indicator_flags; */
939 avio_wb32(pb, hvcc->general_constraint_indicator_flags >> 16);
940 avio_wb16(pb, hvcc->general_constraint_indicator_flags);
942 /* unsigned int(8) general_level_idc; */
943 avio_w8(pb, hvcc->general_level_idc);
946 * bit(4) reserved = ‘1111’b;
947 * unsigned int(12) min_spatial_segmentation_idc;
949 avio_wb16(pb, hvcc->min_spatial_segmentation_idc | 0xf000);
952 * bit(6) reserved = ‘111111’b;
953 * unsigned int(2) parallelismType;
955 avio_w8(pb, hvcc->parallelismType | 0xfc);
958 * bit(6) reserved = ‘111111’b;
959 * unsigned int(2) chromaFormat;
961 avio_w8(pb, hvcc->chromaFormat | 0xfc);
964 * bit(5) reserved = ‘11111’b;
965 * unsigned int(3) bitDepthLumaMinus8;
967 avio_w8(pb, hvcc->bitDepthLumaMinus8 | 0xf8);
970 * bit(5) reserved = ‘11111’b;
971 * unsigned int(3) bitDepthChromaMinus8;
973 avio_w8(pb, hvcc->bitDepthChromaMinus8 | 0xf8);
975 /* bit(16) avgFrameRate; */
976 avio_wb16(pb, hvcc->avgFrameRate);
979 * bit(2) constantFrameRate;
980 * bit(3) numTemporalLayers;
981 * bit(1) temporalIdNested;
982 * unsigned int(2) lengthSizeMinusOne;
984 avio_w8(pb, hvcc->constantFrameRate << 6 |
985 hvcc->numTemporalLayers << 3 |
986 hvcc->temporalIdNested << 2 |
987 hvcc->lengthSizeMinusOne);
989 /* unsigned int(8) numOfArrays; */
990 avio_w8(pb, hvcc->numOfArrays);
992 for (i = 0; i < hvcc->numOfArrays; i++) {
994 * bit(1) array_completeness;
995 * unsigned int(1) reserved = 0;
996 * unsigned int(6) NAL_unit_type;
998 avio_w8(pb, hvcc->array[i].array_completeness << 7 |
999 hvcc->array[i].NAL_unit_type & 0x3f);
1001 /* unsigned int(16) numNalus; */
1002 avio_wb16(pb, hvcc->array[i].numNalus);
1004 for (j = 0; j < hvcc->array[i].numNalus; j++) {
1005 /* unsigned int(16) nalUnitLength; */
1006 avio_wb16(pb, hvcc->array[i].nalUnitLength[j]);
1008 /* bit(8*nalUnitLength) nalUnit; */
1009 avio_write(pb, hvcc->array[i].nalUnit[j],
1010 hvcc->array[i].nalUnitLength[j]);
1017 int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in,
1018 int size, int filter_ps, int *ps_count)
1020 int num_ps = 0, ret = 0;
1021 uint8_t *buf, *end, *start = NULL;
1024 ret = ff_avc_parse_nal_units(pb, buf_in, size);
1028 ret = ff_avc_parse_nal_units_buf(buf_in, &start, &size);
1036 while (end - buf > 4) {
1037 uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
1038 uint8_t type = (buf[4] >> 1) & 0x3f;
1051 avio_write(pb, buf, len);
1065 int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out,
1066 int *size, int filter_ps, int *ps_count)
1071 ret = avio_open_dyn_buf(&pb);
1075 ret = ff_hevc_annexb2mp4(pb, buf_in, *size, filter_ps, ps_count);
1076 *size = avio_close_dyn_buf(pb, buf_out);
1081 int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data,
1082 int size, int ps_array_completeness)
1085 uint8_t *buf, *end, *start = NULL;
1086 HEVCDecoderConfigurationRecord hvcc;
1091 /* We can't write a valid hvcC from the provided data */
1092 ret = AVERROR_INVALIDDATA;
1094 } else if (*data == 1) {
1095 /* Data is already hvcC-formatted */
1096 avio_write(pb, data, size);
1098 } else if (!(AV_RB24(data) == 1 || AV_RB32(data) == 1)) {
1099 /* Not a valid Annex B start code prefix */
1100 ret = AVERROR_INVALIDDATA;
1104 ret = ff_avc_parse_nal_units_buf(data, &start, &size);
1111 while (end - buf > 4) {
1112 uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
1113 uint8_t type = (buf[4] >> 1) & 0x3f;
1121 case NAL_SEI_PREFIX:
1122 case NAL_SEI_SUFFIX:
1123 ret = hvcc_add_nal_unit(buf, len, ps_array_completeness, &hvcc);
1134 ret = hvcc_write(pb, &hvcc);