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/avcodec.h"
22 #include "libavcodec/get_bits.h"
23 #include "libavcodec/golomb.h"
24 #include "libavcodec/hevc.h"
25 #include "libavutil/intreadwrite.h"
28 #include "avio_internal.h"
31 #define MAX_SPATIAL_SEGMENTATION 4096 // max. value of u(12) field
33 typedef struct HVCCNALUnitArray {
34 uint8_t array_completeness;
35 uint8_t NAL_unit_type;
37 uint16_t *nalUnitLength;
41 typedef struct HEVCDecoderConfigurationRecord {
42 uint8_t configurationVersion;
43 uint8_t general_profile_space;
44 uint8_t general_tier_flag;
45 uint8_t general_profile_idc;
46 uint32_t general_profile_compatibility_flags;
47 uint64_t general_constraint_indicator_flags;
48 uint8_t general_level_idc;
49 uint16_t min_spatial_segmentation_idc;
50 uint8_t parallelismType;
52 uint8_t bitDepthLumaMinus8;
53 uint8_t bitDepthChromaMinus8;
54 uint16_t avgFrameRate;
55 uint8_t constantFrameRate;
56 uint8_t numTemporalLayers;
57 uint8_t temporalIdNested;
58 uint8_t lengthSizeMinusOne;
60 HVCCNALUnitArray *array;
61 } HEVCDecoderConfigurationRecord;
63 typedef struct HVCCProfileTierLevel {
64 uint8_t profile_space;
67 uint32_t profile_compatibility_flags;
68 uint64_t constraint_indicator_flags;
70 } HVCCProfileTierLevel;
72 static void hvcc_update_ptl(HEVCDecoderConfigurationRecord *hvcc,
73 HVCCProfileTierLevel *ptl)
76 * The value of general_profile_space in all the parameter sets must be
79 hvcc->general_profile_space = ptl->profile_space;
82 * The level indication general_level_idc must indicate a level of
83 * capability equal to or greater than the highest level indicated for the
84 * highest tier in all the parameter sets.
86 if (hvcc->general_tier_flag < ptl->tier_flag)
87 hvcc->general_level_idc = ptl->level_idc;
89 hvcc->general_level_idc = FFMAX(hvcc->general_level_idc, ptl->level_idc);
92 * The tier indication general_tier_flag must indicate a tier equal to or
93 * greater than the highest tier indicated in all the parameter sets.
95 hvcc->general_tier_flag = FFMAX(hvcc->general_tier_flag, ptl->tier_flag);
98 * The profile indication general_profile_idc must indicate a profile to
99 * which the stream associated with this configuration record conforms.
101 * If the sequence parameter sets are marked with different profiles, then
102 * the stream may need examination to determine which profile, if any, the
103 * entire stream conforms to. If the entire stream is not examined, or the
104 * examination reveals that there is no profile to which the entire stream
105 * conforms, then the entire stream must be split into two or more
106 * sub-streams with separate configuration records in which these rules can
109 * Note: set the profile to the highest value for the sake of simplicity.
111 hvcc->general_profile_idc = FFMAX(hvcc->general_profile_idc, ptl->profile_idc);
114 * Each bit in general_profile_compatibility_flags may only be set if all
115 * the parameter sets set that bit.
117 hvcc->general_profile_compatibility_flags &= ptl->profile_compatibility_flags;
120 * Each bit in general_constraint_indicator_flags may only be set if all
121 * the parameter sets set that bit.
123 hvcc->general_constraint_indicator_flags &= ptl->constraint_indicator_flags;
126 static void hvcc_parse_ptl(GetBitContext *gb,
127 HEVCDecoderConfigurationRecord *hvcc,
128 unsigned int max_sub_layers_minus1)
131 HVCCProfileTierLevel general_ptl;
132 uint8_t sub_layer_profile_present_flag[HEVC_MAX_SUB_LAYERS];
133 uint8_t sub_layer_level_present_flag[HEVC_MAX_SUB_LAYERS];
135 general_ptl.profile_space = get_bits(gb, 2);
136 general_ptl.tier_flag = get_bits1(gb);
137 general_ptl.profile_idc = get_bits(gb, 5);
138 general_ptl.profile_compatibility_flags = get_bits_long(gb, 32);
139 general_ptl.constraint_indicator_flags = get_bits64(gb, 48);
140 general_ptl.level_idc = get_bits(gb, 8);
141 hvcc_update_ptl(hvcc, &general_ptl);
143 for (i = 0; i < max_sub_layers_minus1; i++) {
144 sub_layer_profile_present_flag[i] = get_bits1(gb);
145 sub_layer_level_present_flag[i] = get_bits1(gb);
148 if (max_sub_layers_minus1 > 0)
149 for (i = max_sub_layers_minus1; i < 8; i++)
150 skip_bits(gb, 2); // reserved_zero_2bits[i]
152 for (i = 0; i < max_sub_layers_minus1; i++) {
153 if (sub_layer_profile_present_flag[i]) {
155 * sub_layer_profile_space[i] u(2)
156 * sub_layer_tier_flag[i] u(1)
157 * sub_layer_profile_idc[i] u(5)
158 * sub_layer_profile_compatibility_flag[i][0..31] u(32)
159 * sub_layer_progressive_source_flag[i] u(1)
160 * sub_layer_interlaced_source_flag[i] u(1)
161 * sub_layer_non_packed_constraint_flag[i] u(1)
162 * sub_layer_frame_only_constraint_flag[i] u(1)
163 * sub_layer_reserved_zero_44bits[i] u(44)
165 skip_bits_long(gb, 32);
166 skip_bits_long(gb, 32);
170 if (sub_layer_level_present_flag[i])
175 static void skip_sub_layer_hrd_parameters(GetBitContext *gb,
176 unsigned int cpb_cnt_minus1,
177 uint8_t sub_pic_hrd_params_present_flag)
181 for (i = 0; i <= cpb_cnt_minus1; i++) {
182 get_ue_golomb_long(gb); // bit_rate_value_minus1
183 get_ue_golomb_long(gb); // cpb_size_value_minus1
185 if (sub_pic_hrd_params_present_flag) {
186 get_ue_golomb_long(gb); // cpb_size_du_value_minus1
187 get_ue_golomb_long(gb); // bit_rate_du_value_minus1
190 skip_bits1(gb); // cbr_flag
194 static int skip_hrd_parameters(GetBitContext *gb, uint8_t cprms_present_flag,
195 unsigned int max_sub_layers_minus1)
198 uint8_t sub_pic_hrd_params_present_flag = 0;
199 uint8_t nal_hrd_parameters_present_flag = 0;
200 uint8_t vcl_hrd_parameters_present_flag = 0;
202 if (cprms_present_flag) {
203 nal_hrd_parameters_present_flag = get_bits1(gb);
204 vcl_hrd_parameters_present_flag = get_bits1(gb);
206 if (nal_hrd_parameters_present_flag ||
207 vcl_hrd_parameters_present_flag) {
208 sub_pic_hrd_params_present_flag = get_bits1(gb);
210 if (sub_pic_hrd_params_present_flag)
212 * tick_divisor_minus2 u(8)
213 * du_cpb_removal_delay_increment_length_minus1 u(5)
214 * sub_pic_cpb_params_in_pic_timing_sei_flag u(1)
215 * dpb_output_delay_du_length_minus1 u(5)
220 * bit_rate_scale u(4)
221 * cpb_size_scale u(4)
225 if (sub_pic_hrd_params_present_flag)
226 skip_bits(gb, 4); // cpb_size_du_scale
229 * initial_cpb_removal_delay_length_minus1 u(5)
230 * au_cpb_removal_delay_length_minus1 u(5)
231 * dpb_output_delay_length_minus1 u(5)
237 for (i = 0; i <= max_sub_layers_minus1; i++) {
238 unsigned int cpb_cnt_minus1 = 0;
239 uint8_t low_delay_hrd_flag = 0;
240 uint8_t fixed_pic_rate_within_cvs_flag = 0;
241 uint8_t fixed_pic_rate_general_flag = get_bits1(gb);
243 if (!fixed_pic_rate_general_flag)
244 fixed_pic_rate_within_cvs_flag = get_bits1(gb);
246 if (fixed_pic_rate_within_cvs_flag)
247 get_ue_golomb_long(gb); // elemental_duration_in_tc_minus1
249 low_delay_hrd_flag = get_bits1(gb);
251 if (!low_delay_hrd_flag) {
252 cpb_cnt_minus1 = get_ue_golomb_long(gb);
253 if (cpb_cnt_minus1 > 31)
254 return AVERROR_INVALIDDATA;
257 if (nal_hrd_parameters_present_flag)
258 skip_sub_layer_hrd_parameters(gb, cpb_cnt_minus1,
259 sub_pic_hrd_params_present_flag);
261 if (vcl_hrd_parameters_present_flag)
262 skip_sub_layer_hrd_parameters(gb, cpb_cnt_minus1,
263 sub_pic_hrd_params_present_flag);
269 static void skip_timing_info(GetBitContext *gb)
271 skip_bits_long(gb, 32); // num_units_in_tick
272 skip_bits_long(gb, 32); // time_scale
274 if (get_bits1(gb)) // poc_proportional_to_timing_flag
275 get_ue_golomb_long(gb); // num_ticks_poc_diff_one_minus1
278 static void hvcc_parse_vui(GetBitContext *gb,
279 HEVCDecoderConfigurationRecord *hvcc,
280 unsigned int max_sub_layers_minus1)
282 unsigned int min_spatial_segmentation_idc;
284 if (get_bits1(gb)) // aspect_ratio_info_present_flag
285 if (get_bits(gb, 8) == 255) // aspect_ratio_idc
286 skip_bits_long(gb, 32); // sar_width u(16), sar_height u(16)
288 if (get_bits1(gb)) // overscan_info_present_flag
289 skip_bits1(gb); // overscan_appropriate_flag
291 if (get_bits1(gb)) { // video_signal_type_present_flag
292 skip_bits(gb, 4); // video_format u(3), video_full_range_flag u(1)
294 if (get_bits1(gb)) // colour_description_present_flag
296 * colour_primaries u(8)
297 * transfer_characteristics u(8)
303 if (get_bits1(gb)) { // chroma_loc_info_present_flag
304 get_ue_golomb_long(gb); // chroma_sample_loc_type_top_field
305 get_ue_golomb_long(gb); // chroma_sample_loc_type_bottom_field
309 * neutral_chroma_indication_flag u(1)
310 * field_seq_flag u(1)
311 * frame_field_info_present_flag u(1)
315 if (get_bits1(gb)) { // default_display_window_flag
316 get_ue_golomb_long(gb); // def_disp_win_left_offset
317 get_ue_golomb_long(gb); // def_disp_win_right_offset
318 get_ue_golomb_long(gb); // def_disp_win_top_offset
319 get_ue_golomb_long(gb); // def_disp_win_bottom_offset
322 if (get_bits1(gb)) { // vui_timing_info_present_flag
323 skip_timing_info(gb);
325 if (get_bits1(gb)) // vui_hrd_parameters_present_flag
326 skip_hrd_parameters(gb, 1, max_sub_layers_minus1);
329 if (get_bits1(gb)) { // bitstream_restriction_flag
331 * tiles_fixed_structure_flag u(1)
332 * motion_vectors_over_pic_boundaries_flag u(1)
333 * restricted_ref_pic_lists_flag u(1)
337 min_spatial_segmentation_idc = get_ue_golomb_long(gb);
340 * unsigned int(12) min_spatial_segmentation_idc;
342 * The min_spatial_segmentation_idc indication must indicate a level of
343 * spatial segmentation equal to or less than the lowest level of
344 * spatial segmentation indicated in all the parameter sets.
346 hvcc->min_spatial_segmentation_idc = FFMIN(hvcc->min_spatial_segmentation_idc,
347 min_spatial_segmentation_idc);
349 get_ue_golomb_long(gb); // max_bytes_per_pic_denom
350 get_ue_golomb_long(gb); // max_bits_per_min_cu_denom
351 get_ue_golomb_long(gb); // log2_max_mv_length_horizontal
352 get_ue_golomb_long(gb); // log2_max_mv_length_vertical
356 static void skip_sub_layer_ordering_info(GetBitContext *gb)
358 get_ue_golomb_long(gb); // max_dec_pic_buffering_minus1
359 get_ue_golomb_long(gb); // max_num_reorder_pics
360 get_ue_golomb_long(gb); // max_latency_increase_plus1
363 static int hvcc_parse_vps(GetBitContext *gb,
364 HEVCDecoderConfigurationRecord *hvcc)
366 unsigned int vps_max_sub_layers_minus1;
369 * vps_video_parameter_set_id u(4)
370 * vps_reserved_three_2bits u(2)
371 * vps_max_layers_minus1 u(6)
375 vps_max_sub_layers_minus1 = get_bits(gb, 3);
378 * numTemporalLayers greater than 1 indicates that the stream to which this
379 * configuration record applies is temporally scalable and the contained
380 * number of temporal layers (also referred to as temporal sub-layer or
381 * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
382 * indicates that the stream is not temporally scalable. Value 0 indicates
383 * that it is unknown whether the stream is temporally scalable.
385 hvcc->numTemporalLayers = FFMAX(hvcc->numTemporalLayers,
386 vps_max_sub_layers_minus1 + 1);
389 * vps_temporal_id_nesting_flag u(1)
390 * vps_reserved_0xffff_16bits u(16)
394 hvcc_parse_ptl(gb, hvcc, vps_max_sub_layers_minus1);
396 /* nothing useful for hvcC past this point */
400 static void skip_scaling_list_data(GetBitContext *gb)
402 int i, j, k, num_coeffs;
404 for (i = 0; i < 4; i++)
405 for (j = 0; j < (i == 3 ? 2 : 6); j++)
406 if (!get_bits1(gb)) // scaling_list_pred_mode_flag[i][j]
407 get_ue_golomb_long(gb); // scaling_list_pred_matrix_id_delta[i][j]
409 num_coeffs = FFMIN(64, 1 << (4 + (i << 1)));
412 get_se_golomb_long(gb); // scaling_list_dc_coef_minus8[i-2][j]
414 for (k = 0; k < num_coeffs; k++)
415 get_se_golomb_long(gb); // scaling_list_delta_coef
419 static int parse_rps(GetBitContext *gb, unsigned int rps_idx,
420 unsigned int num_rps,
421 unsigned int num_delta_pocs[HEVC_MAX_SHORT_TERM_REF_PIC_SETS])
425 if (rps_idx && get_bits1(gb)) { // inter_ref_pic_set_prediction_flag
426 /* this should only happen for slice headers, and this isn't one */
427 if (rps_idx >= num_rps)
428 return AVERROR_INVALIDDATA;
430 skip_bits1 (gb); // delta_rps_sign
431 get_ue_golomb_long(gb); // abs_delta_rps_minus1
433 num_delta_pocs[rps_idx] = 0;
436 * From libavcodec/hevc_ps.c:
438 * if (is_slice_header) {
441 * rps_ridx = &sps->st_rps[rps - sps->st_rps - 1];
444 * rps: &sps->st_rps[rps_idx]
445 * sps->st_rps: &sps->st_rps[0]
446 * is_slice_header: rps_idx == num_rps
449 * if (num_rps != rps_idx)
450 * rps_ridx = &sps->st_rps[rps_idx - 1];
452 * NumDeltaPocs[RefRpsIdx]: num_delta_pocs[rps_idx - 1]
454 for (i = 0; i <= num_delta_pocs[rps_idx - 1]; i++) {
455 uint8_t use_delta_flag = 0;
456 uint8_t used_by_curr_pic_flag = get_bits1(gb);
457 if (!used_by_curr_pic_flag)
458 use_delta_flag = get_bits1(gb);
460 if (used_by_curr_pic_flag || use_delta_flag)
461 num_delta_pocs[rps_idx]++;
464 unsigned int num_negative_pics = get_ue_golomb_long(gb);
465 unsigned int num_positive_pics = get_ue_golomb_long(gb);
467 if ((num_positive_pics + (uint64_t)num_negative_pics) * 2 > get_bits_left(gb))
468 return AVERROR_INVALIDDATA;
470 num_delta_pocs[rps_idx] = num_negative_pics + num_positive_pics;
472 for (i = 0; i < num_negative_pics; i++) {
473 get_ue_golomb_long(gb); // delta_poc_s0_minus1[rps_idx]
474 skip_bits1 (gb); // used_by_curr_pic_s0_flag[rps_idx]
477 for (i = 0; i < num_positive_pics; i++) {
478 get_ue_golomb_long(gb); // delta_poc_s1_minus1[rps_idx]
479 skip_bits1 (gb); // used_by_curr_pic_s1_flag[rps_idx]
486 static int hvcc_parse_sps(GetBitContext *gb,
487 HEVCDecoderConfigurationRecord *hvcc)
489 unsigned int i, sps_max_sub_layers_minus1, log2_max_pic_order_cnt_lsb_minus4;
490 unsigned int num_short_term_ref_pic_sets, num_delta_pocs[HEVC_MAX_SHORT_TERM_REF_PIC_SETS];
492 skip_bits(gb, 4); // sps_video_parameter_set_id
494 sps_max_sub_layers_minus1 = get_bits (gb, 3);
497 * numTemporalLayers greater than 1 indicates that the stream to which this
498 * configuration record applies is temporally scalable and the contained
499 * number of temporal layers (also referred to as temporal sub-layer or
500 * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
501 * indicates that the stream is not temporally scalable. Value 0 indicates
502 * that it is unknown whether the stream is temporally scalable.
504 hvcc->numTemporalLayers = FFMAX(hvcc->numTemporalLayers,
505 sps_max_sub_layers_minus1 + 1);
507 hvcc->temporalIdNested = get_bits1(gb);
509 hvcc_parse_ptl(gb, hvcc, sps_max_sub_layers_minus1);
511 get_ue_golomb_long(gb); // sps_seq_parameter_set_id
513 hvcc->chromaFormat = get_ue_golomb_long(gb);
515 if (hvcc->chromaFormat == 3)
516 skip_bits1(gb); // separate_colour_plane_flag
518 get_ue_golomb_long(gb); // pic_width_in_luma_samples
519 get_ue_golomb_long(gb); // pic_height_in_luma_samples
521 if (get_bits1(gb)) { // conformance_window_flag
522 get_ue_golomb_long(gb); // conf_win_left_offset
523 get_ue_golomb_long(gb); // conf_win_right_offset
524 get_ue_golomb_long(gb); // conf_win_top_offset
525 get_ue_golomb_long(gb); // conf_win_bottom_offset
528 hvcc->bitDepthLumaMinus8 = get_ue_golomb_long(gb);
529 hvcc->bitDepthChromaMinus8 = get_ue_golomb_long(gb);
530 log2_max_pic_order_cnt_lsb_minus4 = get_ue_golomb_long(gb);
532 /* sps_sub_layer_ordering_info_present_flag */
533 i = get_bits1(gb) ? 0 : sps_max_sub_layers_minus1;
534 for (; i <= sps_max_sub_layers_minus1; i++)
535 skip_sub_layer_ordering_info(gb);
537 get_ue_golomb_long(gb); // log2_min_luma_coding_block_size_minus3
538 get_ue_golomb_long(gb); // log2_diff_max_min_luma_coding_block_size
539 get_ue_golomb_long(gb); // log2_min_transform_block_size_minus2
540 get_ue_golomb_long(gb); // log2_diff_max_min_transform_block_size
541 get_ue_golomb_long(gb); // max_transform_hierarchy_depth_inter
542 get_ue_golomb_long(gb); // max_transform_hierarchy_depth_intra
544 if (get_bits1(gb) && // scaling_list_enabled_flag
545 get_bits1(gb)) // sps_scaling_list_data_present_flag
546 skip_scaling_list_data(gb);
548 skip_bits1(gb); // amp_enabled_flag
549 skip_bits1(gb); // sample_adaptive_offset_enabled_flag
551 if (get_bits1(gb)) { // pcm_enabled_flag
552 skip_bits (gb, 4); // pcm_sample_bit_depth_luma_minus1
553 skip_bits (gb, 4); // pcm_sample_bit_depth_chroma_minus1
554 get_ue_golomb_long(gb); // log2_min_pcm_luma_coding_block_size_minus3
555 get_ue_golomb_long(gb); // log2_diff_max_min_pcm_luma_coding_block_size
556 skip_bits1 (gb); // pcm_loop_filter_disabled_flag
559 num_short_term_ref_pic_sets = get_ue_golomb_long(gb);
560 if (num_short_term_ref_pic_sets > HEVC_MAX_SHORT_TERM_REF_PIC_SETS)
561 return AVERROR_INVALIDDATA;
563 for (i = 0; i < num_short_term_ref_pic_sets; i++) {
564 int ret = parse_rps(gb, i, num_short_term_ref_pic_sets, num_delta_pocs);
569 if (get_bits1(gb)) { // long_term_ref_pics_present_flag
570 unsigned num_long_term_ref_pics_sps = get_ue_golomb_long(gb);
571 if (num_long_term_ref_pics_sps > 31U)
572 return AVERROR_INVALIDDATA;
573 for (i = 0; i < num_long_term_ref_pics_sps; i++) { // num_long_term_ref_pics_sps
574 int len = FFMIN(log2_max_pic_order_cnt_lsb_minus4 + 4, 16);
575 skip_bits (gb, len); // lt_ref_pic_poc_lsb_sps[i]
576 skip_bits1(gb); // used_by_curr_pic_lt_sps_flag[i]
580 skip_bits1(gb); // sps_temporal_mvp_enabled_flag
581 skip_bits1(gb); // strong_intra_smoothing_enabled_flag
583 if (get_bits1(gb)) // vui_parameters_present_flag
584 hvcc_parse_vui(gb, hvcc, sps_max_sub_layers_minus1);
586 /* nothing useful for hvcC past this point */
590 static int hvcc_parse_pps(GetBitContext *gb,
591 HEVCDecoderConfigurationRecord *hvcc)
593 uint8_t tiles_enabled_flag, entropy_coding_sync_enabled_flag;
595 get_ue_golomb_long(gb); // pps_pic_parameter_set_id
596 get_ue_golomb_long(gb); // pps_seq_parameter_set_id
599 * dependent_slice_segments_enabled_flag u(1)
600 * output_flag_present_flag u(1)
601 * num_extra_slice_header_bits u(3)
602 * sign_data_hiding_enabled_flag u(1)
603 * cabac_init_present_flag u(1)
607 get_ue_golomb_long(gb); // num_ref_idx_l0_default_active_minus1
608 get_ue_golomb_long(gb); // num_ref_idx_l1_default_active_minus1
609 get_se_golomb_long(gb); // init_qp_minus26
612 * constrained_intra_pred_flag u(1)
613 * transform_skip_enabled_flag u(1)
617 if (get_bits1(gb)) // cu_qp_delta_enabled_flag
618 get_ue_golomb_long(gb); // diff_cu_qp_delta_depth
620 get_se_golomb_long(gb); // pps_cb_qp_offset
621 get_se_golomb_long(gb); // pps_cr_qp_offset
624 * pps_slice_chroma_qp_offsets_present_flag u(1)
625 * weighted_pred_flag u(1)
626 * weighted_bipred_flag u(1)
627 * transquant_bypass_enabled_flag u(1)
631 tiles_enabled_flag = get_bits1(gb);
632 entropy_coding_sync_enabled_flag = get_bits1(gb);
634 if (entropy_coding_sync_enabled_flag && tiles_enabled_flag)
635 hvcc->parallelismType = 0; // mixed-type parallel decoding
636 else if (entropy_coding_sync_enabled_flag)
637 hvcc->parallelismType = 3; // wavefront-based parallel decoding
638 else if (tiles_enabled_flag)
639 hvcc->parallelismType = 2; // tile-based parallel decoding
641 hvcc->parallelismType = 1; // slice-based parallel decoding
643 /* nothing useful for hvcC past this point */
647 static void nal_unit_parse_header(GetBitContext *gb, uint8_t *nal_type)
649 skip_bits1(gb); // forbidden_zero_bit
651 *nal_type = get_bits(gb, 6);
655 * nuh_temporal_id_plus1 u(3)
660 static int hvcc_array_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size,
661 uint8_t nal_type, int ps_array_completeness,
662 HEVCDecoderConfigurationRecord *hvcc)
667 HVCCNALUnitArray *array;
669 for (index = 0; index < hvcc->numOfArrays; index++)
670 if (hvcc->array[index].NAL_unit_type == nal_type)
673 if (index >= hvcc->numOfArrays) {
676 ret = av_reallocp_array(&hvcc->array, index + 1, sizeof(HVCCNALUnitArray));
680 for (i = hvcc->numOfArrays; i <= index; i++)
681 memset(&hvcc->array[i], 0, sizeof(HVCCNALUnitArray));
682 hvcc->numOfArrays = index + 1;
685 array = &hvcc->array[index];
686 numNalus = array->numNalus;
688 ret = av_reallocp_array(&array->nalUnit, numNalus + 1, sizeof(uint8_t*));
692 ret = av_reallocp_array(&array->nalUnitLength, numNalus + 1, sizeof(uint16_t));
696 array->nalUnit [numNalus] = nal_buf;
697 array->nalUnitLength[numNalus] = nal_size;
698 array->NAL_unit_type = nal_type;
702 * When the sample entry name is ‘hvc1’, the default and mandatory value of
703 * array_completeness is 1 for arrays of all types of parameter sets, and 0
704 * for all other arrays. When the sample entry name is ‘hev1’, the default
705 * value of array_completeness is 0 for all arrays.
707 if (nal_type == HEVC_NAL_VPS || nal_type == HEVC_NAL_SPS || nal_type == HEVC_NAL_PPS)
708 array->array_completeness = ps_array_completeness;
713 static int hvcc_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size,
714 int ps_array_completeness,
715 HEVCDecoderConfigurationRecord *hvcc)
723 rbsp_buf = ff_nal_unit_extract_rbsp(nal_buf, nal_size, &rbsp_size, 2);
725 ret = AVERROR(ENOMEM);
729 ret = init_get_bits8(&gbc, rbsp_buf, rbsp_size);
733 nal_unit_parse_header(&gbc, &nal_type);
736 * Note: only 'declarative' SEI messages are allowed in
737 * hvcC. Perhaps the SEI playload type should be checked
738 * and non-declarative SEI messages discarded?
744 case HEVC_NAL_SEI_PREFIX:
745 case HEVC_NAL_SEI_SUFFIX:
746 ret = hvcc_array_add_nal_unit(nal_buf, nal_size, nal_type,
747 ps_array_completeness, hvcc);
750 else if (nal_type == HEVC_NAL_VPS)
751 ret = hvcc_parse_vps(&gbc, hvcc);
752 else if (nal_type == HEVC_NAL_SPS)
753 ret = hvcc_parse_sps(&gbc, hvcc);
754 else if (nal_type == HEVC_NAL_PPS)
755 ret = hvcc_parse_pps(&gbc, hvcc);
760 ret = AVERROR_INVALIDDATA;
769 static void hvcc_init(HEVCDecoderConfigurationRecord *hvcc)
771 memset(hvcc, 0, sizeof(HEVCDecoderConfigurationRecord));
772 hvcc->configurationVersion = 1;
773 hvcc->lengthSizeMinusOne = 3; // 4 bytes
776 * The following fields have all their valid bits set by default,
777 * the ProfileTierLevel parsing code will unset them when needed.
779 hvcc->general_profile_compatibility_flags = 0xffffffff;
780 hvcc->general_constraint_indicator_flags = 0xffffffffffff;
783 * Initialize this field with an invalid value which can be used to detect
784 * whether we didn't see any VUI (in which case it should be reset to zero).
786 hvcc->min_spatial_segmentation_idc = MAX_SPATIAL_SEGMENTATION + 1;
789 static void hvcc_close(HEVCDecoderConfigurationRecord *hvcc)
793 for (i = 0; i < hvcc->numOfArrays; i++) {
794 hvcc->array[i].numNalus = 0;
795 av_freep(&hvcc->array[i].nalUnit);
796 av_freep(&hvcc->array[i].nalUnitLength);
799 hvcc->numOfArrays = 0;
800 av_freep(&hvcc->array);
803 static int hvcc_write(AVIOContext *pb, HEVCDecoderConfigurationRecord *hvcc)
806 uint16_t j, vps_count = 0, sps_count = 0, pps_count = 0;
809 * We only support writing HEVCDecoderConfigurationRecord version 1.
811 hvcc->configurationVersion = 1;
814 * If min_spatial_segmentation_idc is invalid, reset to 0 (unspecified).
816 if (hvcc->min_spatial_segmentation_idc > MAX_SPATIAL_SEGMENTATION)
817 hvcc->min_spatial_segmentation_idc = 0;
820 * parallelismType indicates the type of parallelism that is used to meet
821 * the restrictions imposed by min_spatial_segmentation_idc when the value
822 * of min_spatial_segmentation_idc is greater than 0.
824 if (!hvcc->min_spatial_segmentation_idc)
825 hvcc->parallelismType = 0;
828 * It's unclear how to properly compute these fields, so
829 * let's always set them to values meaning 'unspecified'.
831 hvcc->avgFrameRate = 0;
832 hvcc->constantFrameRate = 0;
834 av_log(NULL, AV_LOG_TRACE, "configurationVersion: %"PRIu8"\n",
835 hvcc->configurationVersion);
836 av_log(NULL, AV_LOG_TRACE, "general_profile_space: %"PRIu8"\n",
837 hvcc->general_profile_space);
838 av_log(NULL, AV_LOG_TRACE, "general_tier_flag: %"PRIu8"\n",
839 hvcc->general_tier_flag);
840 av_log(NULL, AV_LOG_TRACE, "general_profile_idc: %"PRIu8"\n",
841 hvcc->general_profile_idc);
842 av_log(NULL, AV_LOG_TRACE, "general_profile_compatibility_flags: 0x%08"PRIx32"\n",
843 hvcc->general_profile_compatibility_flags);
844 av_log(NULL, AV_LOG_TRACE, "general_constraint_indicator_flags: 0x%012"PRIx64"\n",
845 hvcc->general_constraint_indicator_flags);
846 av_log(NULL, AV_LOG_TRACE, "general_level_idc: %"PRIu8"\n",
847 hvcc->general_level_idc);
848 av_log(NULL, AV_LOG_TRACE, "min_spatial_segmentation_idc: %"PRIu16"\n",
849 hvcc->min_spatial_segmentation_idc);
850 av_log(NULL, AV_LOG_TRACE, "parallelismType: %"PRIu8"\n",
851 hvcc->parallelismType);
852 av_log(NULL, AV_LOG_TRACE, "chromaFormat: %"PRIu8"\n",
854 av_log(NULL, AV_LOG_TRACE, "bitDepthLumaMinus8: %"PRIu8"\n",
855 hvcc->bitDepthLumaMinus8);
856 av_log(NULL, AV_LOG_TRACE, "bitDepthChromaMinus8: %"PRIu8"\n",
857 hvcc->bitDepthChromaMinus8);
858 av_log(NULL, AV_LOG_TRACE, "avgFrameRate: %"PRIu16"\n",
860 av_log(NULL, AV_LOG_TRACE, "constantFrameRate: %"PRIu8"\n",
861 hvcc->constantFrameRate);
862 av_log(NULL, AV_LOG_TRACE, "numTemporalLayers: %"PRIu8"\n",
863 hvcc->numTemporalLayers);
864 av_log(NULL, AV_LOG_TRACE, "temporalIdNested: %"PRIu8"\n",
865 hvcc->temporalIdNested);
866 av_log(NULL, AV_LOG_TRACE, "lengthSizeMinusOne: %"PRIu8"\n",
867 hvcc->lengthSizeMinusOne);
868 av_log(NULL, AV_LOG_TRACE, "numOfArrays: %"PRIu8"\n",
870 for (i = 0; i < hvcc->numOfArrays; i++) {
871 av_log(NULL, AV_LOG_TRACE, "array_completeness[%"PRIu8"]: %"PRIu8"\n",
872 i, hvcc->array[i].array_completeness);
873 av_log(NULL, AV_LOG_TRACE, "NAL_unit_type[%"PRIu8"]: %"PRIu8"\n",
874 i, hvcc->array[i].NAL_unit_type);
875 av_log(NULL, AV_LOG_TRACE, "numNalus[%"PRIu8"]: %"PRIu16"\n",
876 i, hvcc->array[i].numNalus);
877 for (j = 0; j < hvcc->array[i].numNalus; j++)
878 av_log(NULL, AV_LOG_TRACE,
879 "nalUnitLength[%"PRIu8"][%"PRIu16"]: %"PRIu16"\n",
880 i, j, hvcc->array[i].nalUnitLength[j]);
884 * We need at least one of each: VPS, SPS and PPS.
886 for (i = 0; i < hvcc->numOfArrays; i++)
887 switch (hvcc->array[i].NAL_unit_type) {
889 vps_count += hvcc->array[i].numNalus;
892 sps_count += hvcc->array[i].numNalus;
895 pps_count += hvcc->array[i].numNalus;
900 if (!vps_count || vps_count > HEVC_MAX_VPS_COUNT ||
901 !sps_count || sps_count > HEVC_MAX_SPS_COUNT ||
902 !pps_count || pps_count > HEVC_MAX_PPS_COUNT)
903 return AVERROR_INVALIDDATA;
905 /* unsigned int(8) configurationVersion = 1; */
906 avio_w8(pb, hvcc->configurationVersion);
909 * unsigned int(2) general_profile_space;
910 * unsigned int(1) general_tier_flag;
911 * unsigned int(5) general_profile_idc;
913 avio_w8(pb, hvcc->general_profile_space << 6 |
914 hvcc->general_tier_flag << 5 |
915 hvcc->general_profile_idc);
917 /* unsigned int(32) general_profile_compatibility_flags; */
918 avio_wb32(pb, hvcc->general_profile_compatibility_flags);
920 /* unsigned int(48) general_constraint_indicator_flags; */
921 avio_wb32(pb, hvcc->general_constraint_indicator_flags >> 16);
922 avio_wb16(pb, hvcc->general_constraint_indicator_flags);
924 /* unsigned int(8) general_level_idc; */
925 avio_w8(pb, hvcc->general_level_idc);
928 * bit(4) reserved = ‘1111’b;
929 * unsigned int(12) min_spatial_segmentation_idc;
931 avio_wb16(pb, hvcc->min_spatial_segmentation_idc | 0xf000);
934 * bit(6) reserved = ‘111111’b;
935 * unsigned int(2) parallelismType;
937 avio_w8(pb, hvcc->parallelismType | 0xfc);
940 * bit(6) reserved = ‘111111’b;
941 * unsigned int(2) chromaFormat;
943 avio_w8(pb, hvcc->chromaFormat | 0xfc);
946 * bit(5) reserved = ‘11111’b;
947 * unsigned int(3) bitDepthLumaMinus8;
949 avio_w8(pb, hvcc->bitDepthLumaMinus8 | 0xf8);
952 * bit(5) reserved = ‘11111’b;
953 * unsigned int(3) bitDepthChromaMinus8;
955 avio_w8(pb, hvcc->bitDepthChromaMinus8 | 0xf8);
957 /* bit(16) avgFrameRate; */
958 avio_wb16(pb, hvcc->avgFrameRate);
961 * bit(2) constantFrameRate;
962 * bit(3) numTemporalLayers;
963 * bit(1) temporalIdNested;
964 * unsigned int(2) lengthSizeMinusOne;
966 avio_w8(pb, hvcc->constantFrameRate << 6 |
967 hvcc->numTemporalLayers << 3 |
968 hvcc->temporalIdNested << 2 |
969 hvcc->lengthSizeMinusOne);
971 /* unsigned int(8) numOfArrays; */
972 avio_w8(pb, hvcc->numOfArrays);
974 for (i = 0; i < hvcc->numOfArrays; i++) {
976 * bit(1) array_completeness;
977 * unsigned int(1) reserved = 0;
978 * unsigned int(6) NAL_unit_type;
980 avio_w8(pb, hvcc->array[i].array_completeness << 7 |
981 hvcc->array[i].NAL_unit_type & 0x3f);
983 /* unsigned int(16) numNalus; */
984 avio_wb16(pb, hvcc->array[i].numNalus);
986 for (j = 0; j < hvcc->array[i].numNalus; j++) {
987 /* unsigned int(16) nalUnitLength; */
988 avio_wb16(pb, hvcc->array[i].nalUnitLength[j]);
990 /* bit(8*nalUnitLength) nalUnit; */
991 avio_write(pb, hvcc->array[i].nalUnit[j],
992 hvcc->array[i].nalUnitLength[j]);
999 int ff_hevc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in,
1000 int size, int filter_ps, int *ps_count)
1002 int num_ps = 0, ret = 0;
1003 uint8_t *buf, *end, *start = NULL;
1006 ret = ff_avc_parse_nal_units(pb, buf_in, size);
1010 ret = ff_avc_parse_nal_units_buf(buf_in, &start, &size);
1018 while (end - buf > 4) {
1019 uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
1020 uint8_t type = (buf[4] >> 1) & 0x3f;
1033 avio_write(pb, buf, len);
1047 int ff_hevc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out,
1048 int *size, int filter_ps, int *ps_count)
1053 ret = avio_open_dyn_buf(&pb);
1057 ret = ff_hevc_annexb2mp4(pb, buf_in, *size, filter_ps, ps_count);
1059 ffio_free_dyn_buf(&pb);
1063 *size = avio_close_dyn_buf(pb, buf_out);
1068 int ff_isom_write_hvcc(AVIOContext *pb, const uint8_t *data,
1069 int size, int ps_array_completeness)
1071 HEVCDecoderConfigurationRecord hvcc;
1072 uint8_t *buf, *end, *start;
1076 /* We can't write a valid hvcC from the provided data */
1077 return AVERROR_INVALIDDATA;
1078 } else if (*data == 1) {
1079 /* Data is already hvcC-formatted */
1080 avio_write(pb, data, size);
1082 } else if (!(AV_RB24(data) == 1 || AV_RB32(data) == 1)) {
1083 /* Not a valid Annex B start code prefix */
1084 return AVERROR_INVALIDDATA;
1087 ret = ff_avc_parse_nal_units_buf(data, &start, &size);
1096 while (end - buf > 4) {
1097 uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
1098 uint8_t type = (buf[4] >> 1) & 0x3f;
1106 case HEVC_NAL_SEI_PREFIX:
1107 case HEVC_NAL_SEI_SUFFIX:
1108 ret = hvcc_add_nal_unit(buf, len, ps_array_completeness, &hvcc);
1119 ret = hvcc_write(pb, &hvcc);