1 /*****************************************************************************
2 * Copyright © 2010-2014 VideoLAN
4 * Authors: Thomas Guillem <thomas.guillem@gmail.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
27 #include <vlc_common.h>
32 //#define HEVC_POC_DEBUG
34 typedef uint8_t nal_u1_t
;
35 typedef uint8_t nal_u2_t
;
36 typedef uint8_t nal_u3_t
;
37 typedef uint8_t nal_u4_t
;
38 typedef uint8_t nal_u5_t
;
39 typedef uint8_t nal_u6_t
;
40 typedef uint8_t nal_u7_t
;
41 typedef uint8_t nal_u8_t
;
42 typedef int32_t nal_se_t
;
43 typedef uint32_t nal_ue_t
;
47 nal_u2_t profile_space
;
50 uint32_t profile_compatibility_flag
; /* nal_u1_t * 32 */
51 nal_u1_t progressive_source_flag
;
52 nal_u1_t interlaced_source_flag
;
53 nal_u1_t non_packed_constraint_flag
;
54 nal_u1_t frame_only_constraint_flag
;
57 nal_u1_t max_12bit_constraint_flag
;
58 nal_u1_t max_10bit_constraint_flag
;
59 nal_u1_t max_8bit_constraint_flag
;
60 nal_u1_t max_422chroma_constraint_flag
;
61 nal_u1_t max_420chroma_constraint_flag
;
62 nal_u1_t max_monochrome_constraint_flag
;
63 nal_u1_t intra_constraint_flag
;
64 nal_u1_t one_picture_only_constraint_flag
;
65 nal_u1_t lower_bit_rate_constraint_flag
;
71 } hevc_inner_profile_tier_level_t
;
73 #define HEVC_MAX_SUBLAYERS 8
76 hevc_inner_profile_tier_level_t general
;
77 nal_u8_t general_level_idc
;
78 uint8_t sublayer_profile_present_flag
; /* nal_u1_t * 8 */
79 uint8_t sublayer_level_present_flag
; /* nal_u1_t * 8 */
80 hevc_inner_profile_tier_level_t sub_layer
[HEVC_MAX_SUBLAYERS
];
81 nal_u8_t sub_layer_level_idc
[HEVC_MAX_SUBLAYERS
];
82 } hevc_profile_tier_level_t
;
84 #define HEVC_MAX_SHORT_TERM_REF_PIC_SET 65
85 #define HEVC_MAX_LONG_TERM_REF_PIC_SET 33
89 unsigned num_delta_pocs
;
90 } hevc_short_term_ref_pic_set_t
;
94 nal_u1_t aspect_ratio_info_present_flag
;
97 nal_u8_t aspect_ratio_idc
;
101 nal_u1_t overscan_info_present_flag
;
102 nal_u1_t overscan_appropriate_flag
;
104 nal_u1_t video_signal_type_present_flag
;
107 nal_u3_t video_format
;
108 nal_u1_t video_full_range_flag
;
109 nal_u1_t colour_description_present_flag
;
112 nal_u8_t colour_primaries
;
113 nal_u8_t transfer_characteristics
;
114 nal_u8_t matrix_coeffs
;
118 nal_u1_t chroma_loc_info_present_flag
;
121 nal_ue_t sample_loc_type_top_field
;
122 nal_ue_t sample_loc_type_bottom_field
;
125 nal_u1_t neutral_chroma_indication_flag
;
126 nal_u1_t field_seq_flag
;
127 nal_u1_t frame_field_info_present_flag
;
129 nal_u1_t default_display_window_flag
;
132 nal_ue_t win_left_offset
;
133 nal_ue_t win_right_offset
;
134 nal_ue_t win_top_offset
;
135 nal_ue_t win_bottom_offset
;
138 nal_u1_t vui_timing_info_present_flag
;
141 uint32_t vui_num_units_in_tick
;
142 uint32_t vui_time_scale
;
147 } hevc_vui_parameters_t
;
149 struct hevc_video_parameter_set_t
151 nal_u4_t vps_video_parameter_set_id
;
152 nal_u1_t vps_base_layer_internal_flag
;
153 nal_u1_t vps_base_layer_available_flag
;
154 nal_u6_t vps_max_layers_minus1
;
155 nal_u3_t vps_max_sub_layers_minus1
;
156 nal_u1_t vps_temporal_id_nesting_flag
;
158 hevc_profile_tier_level_t profile_tier_level
;
160 nal_u1_t vps_sub_layer_ordering_info_present_flag
;
163 nal_ue_t dec_pic_buffering_minus1
;
164 nal_ue_t num_reorder_pics
;
165 nal_ue_t max_latency_increase_plus1
;
166 } vps_max
[1 + HEVC_MAX_SUBLAYERS
];
168 nal_u6_t vps_max_layer_id
;
169 nal_ue_t vps_num_layer_set_minus1
;
170 // layer_id_included_flag; read but discarded
172 nal_u1_t vps_timing_info_present_flag
;
173 uint32_t vps_num_units_in_tick
;
174 uint32_t vps_time_scale
;
179 struct hevc_sequence_parameter_set_t
181 nal_u4_t sps_video_parameter_set_id
;
182 nal_u3_t sps_max_sub_layers_minus1
;
183 nal_u1_t sps_temporal_id_nesting_flag
;
185 hevc_profile_tier_level_t profile_tier_level
;
187 nal_ue_t sps_seq_parameter_set_id
;
188 nal_ue_t chroma_format_idc
;
189 nal_u1_t separate_colour_plane_flag
;
191 nal_ue_t pic_width_in_luma_samples
;
192 nal_ue_t pic_height_in_luma_samples
;
194 nal_u1_t conformance_window_flag
;
197 nal_ue_t left_offset
;
198 nal_ue_t right_offset
;
200 nal_ue_t bottom_offset
;
203 nal_ue_t bit_depth_luma_minus8
;
204 nal_ue_t bit_depth_chroma_minus8
;
205 nal_ue_t log2_max_pic_order_cnt_lsb_minus4
;
207 nal_u1_t sps_sub_layer_ordering_info_present_flag
;
210 nal_ue_t dec_pic_buffering_minus1
;
211 nal_ue_t num_reorder_pics
;
212 nal_ue_t latency_increase_plus1
;
213 } sps_max
[1 + HEVC_MAX_SUBLAYERS
];
215 nal_ue_t log2_min_luma_coding_block_size_minus3
;
216 nal_ue_t log2_diff_max_min_luma_coding_block_size
;
217 nal_ue_t log2_min_luma_transform_block_size_minus2
;
218 nal_ue_t log2_diff_max_min_luma_transform_block_size
;
221 nal_ue_t max_transform_hierarchy_depth_inter
;
222 nal_ue_t max_transform_hierarchy_depth_intra
;
223 nal_u1_t scaling_list_enabled
;
224 nal_u1_t sps_scaling_list_data_present_flag
;
225 // scaling_list_data; read but discarded
227 nal_u1_t amp_enabled_flag
;
228 nal_u1_t sample_adaptive_offset_enabled_flag
;
230 nal_u1_t pcm_enabled_flag
;
231 nal_u4_t pcm_sample_bit_depth_luma_minus1
;
232 nal_u4_t pcm_sample_bit_depth_chroma_minus1
;
233 nal_ue_t log2_min_pcm_luma_coding_block_size_minus3
;
234 nal_ue_t log2_diff_max_min_pcm_luma_coding_block_size
;
235 nal_u1_t pcm_loop_filter_disabled_flag
;
237 nal_ue_t num_short_term_ref_pic_sets
;
240 nal_u1_t long_term_ref_pics_present_flag
;
241 nal_ue_t num_long_term_ref_pics_sps
;
244 nal_u1_t sps_temporal_mvp_enabled_flag
;
245 nal_u1_t strong_intra_smoothing_enabled_flag
;
247 nal_u1_t vui_parameters_present_flag
;
248 hevc_vui_parameters_t vui
;
252 struct hevc_picture_parameter_set_t
254 nal_ue_t pps_pic_parameter_set_id
;
255 nal_ue_t pps_seq_parameter_set_id
;
256 nal_u1_t dependent_slice_segments_enabled_flag
;
257 nal_u1_t output_flag_present_flag
;
258 nal_u3_t num_extra_slice_header_bits
;
259 nal_u1_t sign_data_hiding_enabled_flag
;
260 nal_u1_t cabac_init_present_flag
;
261 nal_ue_t num_ref_idx_l0_default_active_minus1
;
262 nal_ue_t num_ref_idx_l1_default_active_minus1
;
263 nal_se_t init_qp_minus26
;
264 nal_u1_t constrained_intra_pred_flag
;
265 nal_u1_t transform_skip_enabled_flag
;
267 nal_u1_t cu_qp_delta_enabled_flag
;
268 nal_ue_t diff_cu_qp_delta_depth
;
270 nal_se_t pps_cb_qp_offset
;
271 nal_se_t pps_cr_qp_offset
;
272 nal_u1_t pic_slice_level_chroma_qp_offsets_present_flag
;
273 nal_u1_t weighted_pred_flag
;
274 nal_u1_t weighted_bipred_flag
;
275 nal_u1_t transquant_bypass_enable_flag
;
277 nal_u1_t tiles_enabled_flag
;
278 nal_u1_t entropy_coding_sync_enabled_flag
;
279 nal_ue_t num_tile_columns_minus1
;
280 nal_ue_t num_tile_rows_minus1
;
281 nal_u1_t uniform_spacing_flag
;
282 // nal_ue_t *p_column_width_minus1; read but discarded
283 // nal_ue_t *p_row_height_minus1; read but discarded
284 nal_u1_t loop_filter_across_tiles_enabled_flag
;
286 nal_u1_t pps_loop_filter_across_slices_enabled_flag
;
288 nal_u1_t deblocking_filter_control_present_flag
;
289 nal_u1_t deblocking_filter_override_enabled_flag
;
290 nal_u1_t pps_deblocking_filter_disabled_flag
;
291 nal_se_t pps_beta_offset_div2
;
292 nal_se_t pps_tc_offset_div2
;
294 nal_u1_t scaling_list_data_present_flag
;
295 // scaling_list_data; read but discarded
297 nal_u1_t lists_modification_present_flag
;
298 nal_ue_t log2_parallel_merge_level_minus2
;
299 nal_u1_t slice_header_extension_present_flag
;
301 nal_u1_t pps_extension_present_flag
;
302 nal_u1_t pps_range_extension_flag
;
303 nal_u1_t pps_multilayer_extension_flag
;
304 nal_u1_t pps_3d_extension_flag
;
305 nal_u5_t pps_extension_5bits
;
310 struct hevc_slice_segment_header_t
313 nal_u6_t nuh_layer_id
;
314 nal_u3_t temporal_id_plus1
;
315 nal_u1_t first_slice_segment_in_pic_flag
;
316 nal_u1_t no_output_of_prior_pics_flag
;
317 nal_ue_t slice_pic_parameter_set_id
;
318 nal_u1_t dependent_slice_segment_flag
;
319 // slice_segment_address; read but discarded
321 nal_u1_t pic_output_flag
;
323 uint32_t pic_order_cnt_lsb
;
328 /* Computes size and does check the whole struct integrity */
329 static size_t get_hvcC_to_AnnexB_NAL_size( const uint8_t *p_buf
, size_t i_buf
)
333 if( i_buf
< HEVC_MIN_HVCC_SIZE
)
336 const uint8_t i_nal_length_size
= (p_buf
[21] & 0x03) + 1;
337 if(i_nal_length_size
== 3)
340 const uint8_t i_num_array
= p_buf
[22];
341 p_buf
+= 23; i_buf
-= 23;
343 for( uint8_t i
= 0; i
< i_num_array
; i
++ )
348 const uint16_t i_num_nalu
= p_buf
[1] << 8 | p_buf
[2];
349 p_buf
+= 3; i_buf
-= 3;
351 for( uint16_t j
= 0; j
< i_num_nalu
; j
++ )
356 const uint16_t i_nalu_length
= p_buf
[0] << 8 | p_buf
[1];
357 if(i_buf
< (size_t)i_nalu_length
+ 2)
360 i_total
+= i_nalu_length
+ i_nal_length_size
;
361 p_buf
+= i_nalu_length
+ 2;
362 i_buf
-= i_nalu_length
+ 2;
369 uint8_t * hevc_hvcC_to_AnnexB_NAL( const uint8_t *p_buf
, size_t i_buf
,
370 size_t *pi_result
, uint8_t *pi_nal_length_size
)
372 *pi_result
= get_hvcC_to_AnnexB_NAL_size( p_buf
, i_buf
); /* Does all checks */
373 if( *pi_result
== 0 )
376 if( pi_nal_length_size
)
377 *pi_nal_length_size
= hevc_getNALLengthSize( p_buf
);
380 uint8_t *p_out_buf
= p_ret
= malloc( *pi_result
);
387 const uint8_t i_num_array
= p_buf
[22];
390 for( uint8_t i
= 0; i
< i_num_array
; i
++ )
392 const uint16_t i_num_nalu
= p_buf
[1] << 8 | p_buf
[2];
395 for( uint16_t j
= 0; j
< i_num_nalu
; j
++ )
397 const uint16_t i_nalu_length
= p_buf
[0] << 8 | p_buf
[1];
399 memcpy( p_out_buf
, annexb_startcode4
, 4 );
400 memcpy( &p_out_buf
[4], &p_buf
[2], i_nalu_length
);
402 p_out_buf
+= 4 + i_nalu_length
;
403 p_buf
+= 2 + i_nalu_length
;
410 static bool hevc_parse_scaling_list_rbsp( bs_t
*p_bs
)
412 if( bs_remain( p_bs
) < 16 )
415 for( int i
=0; i
<4; i
++ )
417 for( int j
=0; j
<6; j
+= (i
== 3) ? 3 : 1 )
419 if( bs_read1( p_bs
) == 0 )
423 unsigned nextCoef
= 8;
424 unsigned coefNum
= __MIN( 64, (1 << (4 + (i
<< 1))));
427 nextCoef
= bs_read_se( p_bs
) + 8;
429 for( unsigned k
=0; k
<coefNum
; k
++ )
431 nextCoef
= ( nextCoef
+ bs_read_se( p_bs
) + 256 ) % 256;
440 static bool hevc_parse_vui_parameters_rbsp( bs_t
*p_bs
, hevc_vui_parameters_t
*p_vui
)
442 if( bs_remain( p_bs
) < 10 )
445 p_vui
->aspect_ratio_info_present_flag
= bs_read1( p_bs
);
446 if( p_vui
->aspect_ratio_info_present_flag
)
448 p_vui
->ar
.aspect_ratio_idc
= bs_read( p_bs
, 8 );
449 if( p_vui
->ar
.aspect_ratio_idc
== 0xFF ) //HEVC_SAR__IDC_EXTENDED_SAR )
451 p_vui
->ar
.sar_width
= bs_read( p_bs
, 16 );
452 p_vui
->ar
.sar_height
= bs_read( p_bs
, 16 );
456 p_vui
->overscan_info_present_flag
= bs_read1( p_bs
);
457 if( p_vui
->overscan_info_present_flag
)
458 p_vui
->overscan_appropriate_flag
= bs_read1( p_bs
);
460 p_vui
->video_signal_type_present_flag
= bs_read1( p_bs
);
461 if( p_vui
->video_signal_type_present_flag
)
463 p_vui
->vs
.video_format
= bs_read( p_bs
, 3 );
464 p_vui
->vs
.video_full_range_flag
= bs_read1( p_bs
);
465 p_vui
->vs
.colour_description_present_flag
= bs_read1( p_bs
);
466 if( p_vui
->vs
.colour_description_present_flag
)
468 p_vui
->vs
.colour
.colour_primaries
= bs_read( p_bs
, 8 );
469 p_vui
->vs
.colour
.transfer_characteristics
= bs_read( p_bs
, 8 );
470 p_vui
->vs
.colour
.matrix_coeffs
= bs_read( p_bs
, 8 );
474 p_vui
->vs
.colour
.colour_primaries
= HXXX_PRIMARIES_UNSPECIFIED
;
475 p_vui
->vs
.colour
.transfer_characteristics
= HXXX_TRANSFER_UNSPECIFIED
;
476 p_vui
->vs
.colour
.matrix_coeffs
= HXXX_MATRIX_UNSPECIFIED
;
480 p_vui
->chroma_loc_info_present_flag
= bs_read1( p_bs
);
481 if( p_vui
->chroma_loc_info_present_flag
)
483 p_vui
->chroma
.sample_loc_type_top_field
= bs_read_ue( p_bs
);
484 p_vui
->chroma
.sample_loc_type_bottom_field
= bs_read_ue( p_bs
);
487 p_vui
->neutral_chroma_indication_flag
= bs_read1( p_bs
);
488 p_vui
->field_seq_flag
= bs_read1( p_bs
);
489 p_vui
->frame_field_info_present_flag
= bs_read1( p_bs
);
491 p_vui
->default_display_window_flag
= bs_read1( p_bs
);
492 if( p_vui
->default_display_window_flag
)
494 p_vui
->def_disp
.win_left_offset
= bs_read_ue( p_bs
);
495 p_vui
->def_disp
.win_right_offset
= bs_read_ue( p_bs
);
496 p_vui
->def_disp
.win_top_offset
= bs_read_ue( p_bs
);
497 p_vui
->def_disp
.win_bottom_offset
= bs_read_ue( p_bs
);
500 p_vui
->vui_timing_info_present_flag
= bs_read1( p_bs
);
501 if( p_vui
->vui_timing_info_present_flag
)
503 p_vui
->timing
.vui_num_units_in_tick
= bs_read( p_bs
, 32 );
504 p_vui
->timing
.vui_time_scale
= bs_read( p_bs
, 32 );
506 if( bs_remain( p_bs
) < 3 )
511 if( bs_remain( p_bs
) < 1 ) /* late fail */
517 /* Shortcut for retrieving vps/sps/pps id */
518 bool hevc_get_xps_id(const uint8_t *p_buf
, size_t i_buf
, uint8_t *pi_id
)
522 /* No need to lookup convert from emulation for that data */
523 uint8_t i_nal_type
= hevc_getNALType(p_buf
);
525 bs_init(&bs
, &p_buf
[2], i_buf
- 2);
526 if(i_nal_type
== HEVC_NAL_PPS
)
528 *pi_id
= bs_read_ue( &bs
);
529 if(*pi_id
> HEVC_PPS_ID_MAX
)
534 *pi_id
= bs_read( &bs
, 4 );
535 if(i_nal_type
== HEVC_NAL_SPS
)
537 if(*pi_id
> HEVC_SPS_ID_MAX
)
540 else if(*pi_id
> HEVC_VPS_ID_MAX
)
546 static bool hevc_parse_inner_profile_tier_level_rbsp( bs_t
*p_bs
,
547 hevc_inner_profile_tier_level_t
*p_in
)
549 if( bs_remain( p_bs
) < 88 )
552 p_in
->profile_space
= bs_read( p_bs
, 2 );
553 p_in
->tier_flag
= bs_read1( p_bs
);
554 p_in
->profile_idc
= bs_read( p_bs
, 5 );
555 p_in
->profile_compatibility_flag
= bs_read( p_bs
, 32 );
556 p_in
->progressive_source_flag
= bs_read1( p_bs
);
557 p_in
->interlaced_source_flag
= bs_read1( p_bs
);
558 p_in
->non_packed_constraint_flag
= bs_read1( p_bs
);
559 p_in
->frame_only_constraint_flag
= bs_read1( p_bs
);
561 if( ( p_in
->profile_idc
>= 4 && p_in
->profile_idc
<= 7 ) ||
562 ( p_in
->profile_compatibility_flag
& 0x0F000000 ) )
564 p_in
->idc4to7
.max_12bit_constraint_flag
= bs_read1( p_bs
);
565 p_in
->idc4to7
.max_10bit_constraint_flag
= bs_read1( p_bs
);
566 p_in
->idc4to7
.max_8bit_constraint_flag
= bs_read1( p_bs
);
567 p_in
->idc4to7
.max_422chroma_constraint_flag
= bs_read1( p_bs
);
568 p_in
->idc4to7
.max_420chroma_constraint_flag
= bs_read1( p_bs
);
569 p_in
->idc4to7
.max_monochrome_constraint_flag
= bs_read1( p_bs
);
570 p_in
->idc4to7
.intra_constraint_flag
= bs_read1( p_bs
);
571 p_in
->idc4to7
.one_picture_only_constraint_flag
= bs_read1( p_bs
);
572 p_in
->idc4to7
.lower_bit_rate_constraint_flag
= bs_read1( p_bs
);
573 (void) bs_read( p_bs
, 2 );
577 (void) bs_read( p_bs
, 11 );
579 (void) bs_read( p_bs
, 32 );
581 if( ( p_in
->profile_idc
>= 1 && p_in
->profile_idc
<= 5 ) ||
582 ( p_in
->profile_compatibility_flag
& 0x7C000000 ) )
583 p_in
->idc1to5
.inbld_flag
= bs_read1( p_bs
);
585 (void) bs_read1( p_bs
);
590 static bool hevc_parse_profile_tier_level_rbsp( bs_t
*p_bs
, bool profile_present
,
591 uint8_t max_num_sub_layers_minus1
,
592 hevc_profile_tier_level_t
*p_ptl
)
594 if( profile_present
&& !hevc_parse_inner_profile_tier_level_rbsp( p_bs
, &p_ptl
->general
) )
597 if( bs_remain( p_bs
) < 8)
600 p_ptl
->general_level_idc
= bs_read( p_bs
, 8 );
602 if( max_num_sub_layers_minus1
> 0 )
604 if( bs_remain( p_bs
) < 16 )
607 for( uint8_t i
=0; i
< 8; i
++ )
609 if( i
< max_num_sub_layers_minus1
)
611 if( bs_read1( p_bs
) )
612 p_ptl
->sublayer_profile_present_flag
|= (0x80 >> i
);
613 if( bs_read1( p_bs
) )
614 p_ptl
->sublayer_level_present_flag
|= (0x80 >> i
);
620 for( uint8_t i
=0; i
< max_num_sub_layers_minus1
; i
++ )
622 if( ( p_ptl
->sublayer_profile_present_flag
& (0x80 >> i
) ) &&
623 ! hevc_parse_inner_profile_tier_level_rbsp( p_bs
, &p_ptl
->sub_layer
[i
] ) )
626 if( p_ptl
->sublayer_profile_present_flag
& (0x80 >> i
) )
628 if( bs_remain( p_bs
) < 8 )
630 p_ptl
->sub_layer_level_idc
[i
] = bs_read( p_bs
, 8 );
638 static bool hevc_parse_video_parameter_set_rbsp( bs_t
*p_bs
,
639 hevc_video_parameter_set_t
*p_vps
)
641 if( bs_remain( p_bs
) < 134 )
644 p_vps
->vps_video_parameter_set_id
= bs_read( p_bs
, 4 );
645 p_vps
->vps_base_layer_internal_flag
= bs_read1( p_bs
);
646 p_vps
->vps_base_layer_available_flag
= bs_read1( p_bs
);
647 p_vps
->vps_max_layers_minus1
= bs_read( p_bs
, 6 );
648 p_vps
->vps_max_sub_layers_minus1
= bs_read( p_bs
, 3 );
649 p_vps
->vps_temporal_id_nesting_flag
= bs_read1( p_bs
);
652 if( !hevc_parse_profile_tier_level_rbsp( p_bs
, true, p_vps
->vps_max_sub_layers_minus1
,
653 &p_vps
->profile_tier_level
) )
656 p_vps
->vps_sub_layer_ordering_info_present_flag
= bs_read1( p_bs
);
657 for( unsigned i
= (p_vps
->vps_sub_layer_ordering_info_present_flag
?
658 0 : p_vps
->vps_max_sub_layers_minus1
);
659 i
<= p_vps
->vps_max_sub_layers_minus1
; i
++ )
661 p_vps
->vps_max
[i
].dec_pic_buffering_minus1
= bs_read_ue( p_bs
);
662 p_vps
->vps_max
[i
].num_reorder_pics
= bs_read_ue( p_bs
);
663 p_vps
->vps_max
[i
].max_latency_increase_plus1
= bs_read_ue( p_bs
);
665 if( bs_remain( p_bs
) < 10 )
668 p_vps
->vps_max_layer_id
= bs_read( p_bs
, 6 );
669 p_vps
->vps_num_layer_set_minus1
= bs_read_ue( p_bs
);
670 // layer_id_included_flag; read but discarded
671 bs_skip( p_bs
, p_vps
->vps_num_layer_set_minus1
* (p_vps
->vps_max_layer_id
+ 1) );
672 if( bs_remain( p_bs
) < 2 )
675 p_vps
->vps_timing_info_present_flag
= bs_read1( p_bs
);
676 if( p_vps
->vps_timing_info_present_flag
)
678 p_vps
->vps_num_units_in_tick
= bs_read( p_bs
, 32 );
679 p_vps
->vps_time_scale
= bs_read( p_bs
, 32 );
681 /* parsing incomplete */
683 if( bs_remain( p_bs
) < 1 )
689 void hevc_rbsp_release_vps( hevc_video_parameter_set_t
*p_vps
)
694 #define IMPL_hevc_generic_decode( name, hevctype, decode, release ) \
695 hevctype * name( const uint8_t *p_buf, size_t i_buf, bool b_escaped ) \
697 hevctype *p_hevctype = calloc(1, sizeof(hevctype)); \
698 if(likely(p_hevctype)) \
701 bs_init( &bs, p_buf, i_buf ); \
702 unsigned i_bitflow = 0; \
705 bs.p_fwpriv = &i_bitflow; \
706 bs.pf_forward = hxxx_bsfw_ep3b_to_rbsp; /* Does the emulated 3bytes conversion to rbsp */ \
708 else (void) i_bitflow;\
709 bs_skip( &bs, 7 ); /* nal_unit_header */ \
710 uint8_t i_nuh_layer_id = bs_read( &bs, 6 ); \
711 bs_skip( &bs, 3 ); /* !nal_unit_header */ \
712 if( i_nuh_layer_id > 62 || !decode( &bs, p_hevctype ) ) \
714 release( p_hevctype ); \
721 IMPL_hevc_generic_decode( hevc_decode_vps
, hevc_video_parameter_set_t
,
722 hevc_parse_video_parameter_set_rbsp
, hevc_rbsp_release_vps
)
724 static bool hevc_parse_st_ref_pic_set( bs_t
*p_bs
, unsigned stRpsIdx
,
725 unsigned num_short_term_ref_pic_sets
,
726 hevc_short_term_ref_pic_set_t
*p_sets
)
728 if( stRpsIdx
&& bs_read1( p_bs
) ) /* Interref pic set prediction flag */
730 nal_ue_t delta_idx_minus_1
= 0;
731 if( stRpsIdx
== num_short_term_ref_pic_sets
)
733 delta_idx_minus_1
= bs_read_ue( p_bs
);
734 if( delta_idx_minus_1
>= stRpsIdx
)
737 if(delta_idx_minus_1
== stRpsIdx
)
740 nal_u1_t delta_rps_sign
= bs_read1( p_bs
);
741 nal_ue_t abs_delta_rps_minus1
= bs_read_ue( p_bs
);
742 unsigned RefRpsIdx
= stRpsIdx
- delta_idx_minus_1
- 1;
743 int deltaRps
= ( 1 - ( delta_rps_sign
<< 1 ) ) * ( abs_delta_rps_minus1
+ 1 );
744 VLC_UNUSED(deltaRps
);
746 unsigned numDeltaPocs
= p_sets
[RefRpsIdx
].num_delta_pocs
;
747 p_sets
[stRpsIdx
].num_delta_pocs
= 0;
748 for( unsigned j
=0; j
<= numDeltaPocs
; j
++ )
750 if( ! bs_read1( p_bs
) ) /* used_by_curr_pic_flag */
752 if( bs_read1( p_bs
) ) /* use_delta_flag */
753 p_sets
[stRpsIdx
].num_delta_pocs
++;
756 p_sets
[stRpsIdx
].num_delta_pocs
++;
761 nal_ue_t num_negative_pics
= bs_read_ue( p_bs
);
762 nal_ue_t num_positive_pics
= bs_read_ue( p_bs
);
763 if( bs_remain( p_bs
) < ((int64_t)num_negative_pics
+ num_positive_pics
) * 2 )
765 for(unsigned int i
=0; i
<num_negative_pics
; i
++)
767 (void) bs_read_ue( p_bs
); /* delta_poc_s0_minus1 */
768 (void) bs_read1( p_bs
); /* used_by_current_pic_s0_flag */
770 for(unsigned int i
=0; i
<num_positive_pics
; i
++)
772 (void) bs_read_ue( p_bs
); /* delta_poc_s1_minus1 */
773 (void) bs_read1( p_bs
); /* used_by_current_pic_s1_flag */
775 p_sets
[stRpsIdx
].num_delta_pocs
= num_positive_pics
+ num_negative_pics
;
781 static bool hevc_parse_sequence_parameter_set_rbsp( bs_t
*p_bs
,
782 hevc_sequence_parameter_set_t
*p_sps
)
784 p_sps
->sps_video_parameter_set_id
= bs_read( p_bs
, 4 );
785 p_sps
->sps_max_sub_layers_minus1
= bs_read( p_bs
, 3 );
786 p_sps
->sps_temporal_id_nesting_flag
= bs_read1( p_bs
);
787 if( !hevc_parse_profile_tier_level_rbsp( p_bs
, true, p_sps
->sps_max_sub_layers_minus1
,
788 &p_sps
->profile_tier_level
) )
791 if( bs_remain( p_bs
) < 1 )
794 p_sps
->sps_seq_parameter_set_id
= bs_read_ue( p_bs
);
795 if( p_sps
->sps_seq_parameter_set_id
> HEVC_SPS_ID_MAX
)
798 p_sps
->chroma_format_idc
= bs_read_ue( p_bs
);
799 if( p_sps
->chroma_format_idc
== 3 )
800 p_sps
->separate_colour_plane_flag
= bs_read1( p_bs
);
801 p_sps
->pic_width_in_luma_samples
= bs_read_ue( p_bs
);
802 p_sps
->pic_height_in_luma_samples
= bs_read_ue( p_bs
);
803 if( !p_sps
->pic_width_in_luma_samples
|| !p_sps
->pic_height_in_luma_samples
)
806 p_sps
->conformance_window_flag
= bs_read1( p_bs
);
807 if( p_sps
->conformance_window_flag
)
809 p_sps
->conf_win
.left_offset
= bs_read_ue( p_bs
);
810 p_sps
->conf_win
.right_offset
= bs_read_ue( p_bs
);
811 p_sps
->conf_win
.top_offset
= bs_read_ue( p_bs
);
812 p_sps
->conf_win
.bottom_offset
= bs_read_ue( p_bs
);
815 p_sps
->bit_depth_luma_minus8
= bs_read_ue( p_bs
);
816 p_sps
->bit_depth_chroma_minus8
= bs_read_ue( p_bs
);
817 p_sps
->log2_max_pic_order_cnt_lsb_minus4
= bs_read_ue( p_bs
);
819 p_sps
->sps_sub_layer_ordering_info_present_flag
= bs_read1( p_bs
);
820 for( uint8_t i
=(p_sps
->sps_sub_layer_ordering_info_present_flag
? 0 : p_sps
->sps_max_sub_layers_minus1
);
821 i
<= p_sps
->sps_max_sub_layers_minus1
; i
++ )
823 p_sps
->sps_max
[i
].dec_pic_buffering_minus1
= bs_read_ue( p_bs
);
824 p_sps
->sps_max
[i
].num_reorder_pics
= bs_read_ue( p_bs
);
825 p_sps
->sps_max
[i
].latency_increase_plus1
= bs_read_ue( p_bs
);
828 if( bs_remain( p_bs
) < 4 )
831 p_sps
->log2_min_luma_coding_block_size_minus3
= bs_read_ue( p_bs
);
832 p_sps
->log2_diff_max_min_luma_coding_block_size
= bs_read_ue( p_bs
);
833 p_sps
->log2_min_luma_transform_block_size_minus2
= bs_read_ue( p_bs
);
834 if( bs_remain( p_bs
) < 1 ) /* last late fail check */
836 p_sps
->log2_diff_max_min_luma_transform_block_size
= bs_read_ue( p_bs
);
838 /* parsing incomplete */
840 p_sps
->max_transform_hierarchy_depth_inter
= bs_read_ue( p_bs
);
841 p_sps
->max_transform_hierarchy_depth_intra
= bs_read_ue( p_bs
);
842 p_sps
->scaling_list_enabled
= bs_read1( p_bs
);
843 if( p_sps
->scaling_list_enabled
)
845 p_sps
->sps_scaling_list_data_present_flag
= bs_read1( p_bs
);
846 if( p_sps
->sps_scaling_list_data_present_flag
&&
847 ! hevc_parse_scaling_list_rbsp( p_bs
) )
853 p_sps
->amp_enabled_flag
= bs_read1( p_bs
);
854 p_sps
->sample_adaptive_offset_enabled_flag
= bs_read1( p_bs
);
856 p_sps
->pcm_enabled_flag
= bs_read1( p_bs
);
857 if( p_sps
->pcm_enabled_flag
)
859 p_sps
->pcm_sample_bit_depth_luma_minus1
= bs_read( p_bs
, 4 );
860 p_sps
->pcm_sample_bit_depth_chroma_minus1
= bs_read( p_bs
, 4 );
861 p_sps
->log2_min_pcm_luma_coding_block_size_minus3
= bs_read_ue( p_bs
);
862 p_sps
->log2_diff_max_min_pcm_luma_coding_block_size
= bs_read_ue( p_bs
);
863 p_sps
->pcm_loop_filter_disabled_flag
= bs_read1( p_bs
);
866 p_sps
->num_short_term_ref_pic_sets
= bs_read_ue( p_bs
);
867 if( p_sps
->num_short_term_ref_pic_sets
> HEVC_MAX_SHORT_TERM_REF_PIC_SET
)
870 hevc_short_term_ref_pic_set_t sets
[HEVC_MAX_SHORT_TERM_REF_PIC_SET
];
871 memset(&sets
, 0, sizeof(hevc_short_term_ref_pic_set_t
) * HEVC_MAX_SHORT_TERM_REF_PIC_SET
);
872 for( unsigned int i
=0; i
<p_sps
->num_short_term_ref_pic_sets
; i
++ )
874 if( !hevc_parse_st_ref_pic_set( p_bs
, i
, p_sps
->num_short_term_ref_pic_sets
, sets
) )
878 p_sps
->long_term_ref_pics_present_flag
= bs_read1( p_bs
);
879 if( p_sps
->long_term_ref_pics_present_flag
)
881 p_sps
->num_long_term_ref_pics_sps
= bs_read_ue( p_bs
);
882 if( p_sps
->num_long_term_ref_pics_sps
> HEVC_MAX_LONG_TERM_REF_PIC_SET
)
884 for( unsigned int i
=0; i
< p_sps
->num_long_term_ref_pics_sps
; i
++ )
886 /* lt_ref_pic_poc_lsb_sps */
887 bs_skip( p_bs
, p_sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4 );
888 /* used_by_curr_pic_lt_sps_flag */
893 p_sps
->sps_temporal_mvp_enabled_flag
= bs_read1( p_bs
);
894 p_sps
->strong_intra_smoothing_enabled_flag
= bs_read1( p_bs
);
896 if( bs_remain( p_bs
) < 1 ) /* late fail */
899 p_sps
->vui_parameters_present_flag
= bs_read1( p_bs
);
900 if( p_sps
->vui_parameters_present_flag
&&
901 !hevc_parse_vui_parameters_rbsp( p_bs
, &p_sps
->vui
) )
909 void hevc_rbsp_release_sps( hevc_sequence_parameter_set_t
*p_sps
)
914 IMPL_hevc_generic_decode( hevc_decode_sps
, hevc_sequence_parameter_set_t
,
915 hevc_parse_sequence_parameter_set_rbsp
, hevc_rbsp_release_sps
)
917 static bool hevc_parse_pic_parameter_set_rbsp( bs_t
*p_bs
,
918 hevc_picture_parameter_set_t
*p_pps
)
920 if( bs_remain( p_bs
) < 1 )
922 p_pps
->pps_pic_parameter_set_id
= bs_read_ue( p_bs
);
923 if( p_pps
->pps_pic_parameter_set_id
> HEVC_PPS_ID_MAX
|| bs_remain( p_bs
) < 1 )
925 p_pps
->pps_seq_parameter_set_id
= bs_read_ue( p_bs
);
926 if( p_pps
->pps_seq_parameter_set_id
> HEVC_SPS_ID_MAX
)
928 p_pps
->dependent_slice_segments_enabled_flag
= bs_read1( p_bs
);
929 p_pps
->output_flag_present_flag
= bs_read1( p_bs
);
930 p_pps
->num_extra_slice_header_bits
= bs_read( p_bs
, 3 );
931 p_pps
->sign_data_hiding_enabled_flag
= bs_read1( p_bs
);
932 p_pps
->cabac_init_present_flag
= bs_read1( p_bs
);
934 p_pps
->num_ref_idx_l0_default_active_minus1
= bs_read_ue( p_bs
);
935 p_pps
->num_ref_idx_l1_default_active_minus1
= bs_read_ue( p_bs
);
937 p_pps
->init_qp_minus26
= bs_read_se( p_bs
);
938 p_pps
->constrained_intra_pred_flag
= bs_read1( p_bs
);
939 p_pps
->transform_skip_enabled_flag
= bs_read1( p_bs
);
940 p_pps
->cu_qp_delta_enabled_flag
= bs_read1( p_bs
);
941 if( p_pps
->cu_qp_delta_enabled_flag
)
942 p_pps
->diff_cu_qp_delta_depth
= bs_read_ue( p_bs
);
944 if( bs_remain( p_bs
) < 1 )
947 p_pps
->pps_cb_qp_offset
= bs_read_se( p_bs
);
948 p_pps
->pps_cr_qp_offset
= bs_read_se( p_bs
);
949 p_pps
->pic_slice_level_chroma_qp_offsets_present_flag
= bs_read1( p_bs
);
950 p_pps
->weighted_pred_flag
= bs_read1( p_bs
);
951 p_pps
->weighted_bipred_flag
= bs_read1( p_bs
);
952 p_pps
->transquant_bypass_enable_flag
= bs_read1( p_bs
);
953 p_pps
->tiles_enabled_flag
= bs_read1( p_bs
);
954 p_pps
->entropy_coding_sync_enabled_flag
= bs_read1( p_bs
);
956 if( p_pps
->tiles_enabled_flag
)
958 p_pps
->num_tile_columns_minus1
= bs_read_ue( p_bs
); /* TODO: validate max col/row values */
959 p_pps
->num_tile_rows_minus1
= bs_read_ue( p_bs
); /* against sps PicWidthInCtbsY */
960 p_pps
->uniform_spacing_flag
= bs_read1( p_bs
);
961 if( !p_pps
->uniform_spacing_flag
)
963 if( bs_remain( p_bs
) < (int64_t) p_pps
->num_tile_columns_minus1
+
964 p_pps
->num_tile_rows_minus1
+ 1 )
966 for( unsigned i
=0; i
< p_pps
->num_tile_columns_minus1
; i
++ )
967 (void) bs_read_ue( p_bs
);
968 for( unsigned i
=0; i
< p_pps
->num_tile_rows_minus1
; i
++ )
969 (void) bs_read_ue( p_bs
);
971 p_pps
->loop_filter_across_tiles_enabled_flag
= bs_read1( p_bs
);
974 p_pps
->pps_loop_filter_across_slices_enabled_flag
= bs_read1( p_bs
);
975 p_pps
->deblocking_filter_control_present_flag
= bs_read1( p_bs
);
976 if( p_pps
->deblocking_filter_control_present_flag
)
978 p_pps
->deblocking_filter_override_enabled_flag
= bs_read1( p_bs
);
979 p_pps
->pps_deblocking_filter_disabled_flag
= bs_read1( p_bs
);
980 if( !p_pps
->pps_deblocking_filter_disabled_flag
)
982 p_pps
->pps_beta_offset_div2
= bs_read_se( p_bs
);
983 p_pps
->pps_tc_offset_div2
= bs_read_se( p_bs
);
987 p_pps
->scaling_list_data_present_flag
= bs_read1( p_bs
);
988 if( p_pps
->scaling_list_data_present_flag
&& !hevc_parse_scaling_list_rbsp( p_bs
) )
991 p_pps
->lists_modification_present_flag
= bs_read1( p_bs
);
992 p_pps
->log2_parallel_merge_level_minus2
= bs_read_ue( p_bs
);
993 p_pps
->slice_header_extension_present_flag
= bs_read1( p_bs
);
995 if( bs_remain( p_bs
) < 1 )
998 p_pps
->pps_extension_present_flag
= bs_read1( p_bs
);
999 if( p_pps
->pps_extension_present_flag
)
1001 p_pps
->pps_range_extension_flag
= bs_read1( p_bs
);
1002 p_pps
->pps_multilayer_extension_flag
= bs_read1( p_bs
);
1003 p_pps
->pps_3d_extension_flag
= bs_read1( p_bs
);
1004 if( bs_remain( p_bs
) < 5 )
1006 p_pps
->pps_extension_5bits
= bs_read( p_bs
, 5 );
1012 void hevc_rbsp_release_pps( hevc_picture_parameter_set_t
*p_pps
)
1017 IMPL_hevc_generic_decode( hevc_decode_pps
, hevc_picture_parameter_set_t
,
1018 hevc_parse_pic_parameter_set_rbsp
, hevc_rbsp_release_pps
)
1020 uint8_t hevc_get_sps_vps_id( const hevc_sequence_parameter_set_t
*p_sps
)
1022 return p_sps
->sps_video_parameter_set_id
;
1025 uint8_t hevc_get_pps_sps_id( const hevc_picture_parameter_set_t
*p_pps
)
1027 return p_pps
->pps_seq_parameter_set_id
;
1030 uint8_t hevc_get_slice_pps_id( const hevc_slice_segment_header_t
*p_slice
)
1032 return p_slice
->slice_pic_parameter_set_id
;
1035 bool hevc_get_sps_profile_tier_level( const hevc_sequence_parameter_set_t
*p_sps
,
1036 uint8_t *pi_profile
, uint8_t *pi_level
)
1038 if(p_sps
->profile_tier_level
.general
.profile_idc
)
1040 *pi_profile
= p_sps
->profile_tier_level
.general
.profile_idc
;
1041 *pi_level
= p_sps
->profile_tier_level
.general_level_idc
;
1047 bool hevc_get_picture_size( const hevc_sequence_parameter_set_t
*p_sps
,
1048 unsigned *p_w
, unsigned *p_h
, unsigned *p_vw
, unsigned *p_vh
)
1050 *p_w
= *p_vw
= p_sps
->pic_width_in_luma_samples
;
1051 *p_h
= *p_vh
= p_sps
->pic_height_in_luma_samples
;
1052 if( p_sps
->conformance_window_flag
)
1054 *p_vh
-= p_sps
->conf_win
.bottom_offset
+ p_sps
->conf_win
.top_offset
;
1055 *p_vh
-= p_sps
->conf_win
.left_offset
+ p_sps
->conf_win
.right_offset
;
1060 uint8_t hevc_get_max_num_reorder( const hevc_video_parameter_set_t
*p_vps
)
1062 return p_vps
->vps_max
[0/* base layer */].num_reorder_pics
;
1065 static inline uint8_t vlc_ceil_log2( uint32_t val
)
1067 uint8_t n
= 31 - clz(val
);
1068 if (((unsigned)1 << n
) != val
)
1073 static bool hevc_get_picture_CtbsYsize( const hevc_sequence_parameter_set_t
*p_sps
, unsigned *p_w
, unsigned *p_h
)
1075 const unsigned int MinCbLog2SizeY
= p_sps
->log2_min_luma_coding_block_size_minus3
+ 3;
1076 const unsigned int CtbLog2SizeY
= MinCbLog2SizeY
+ p_sps
->log2_diff_max_min_luma_coding_block_size
;
1077 if( CtbLog2SizeY
> 31 )
1079 const unsigned int CtbSizeY
= 1 << CtbLog2SizeY
;
1080 *p_w
= (p_sps
->pic_width_in_luma_samples
- 1) / CtbSizeY
+ 1;
1081 *p_h
= (p_sps
->pic_height_in_luma_samples
- 1) / CtbSizeY
+ 1;
1085 bool hevc_get_frame_rate( const hevc_sequence_parameter_set_t
*p_sps
,
1086 const hevc_video_parameter_set_t
*p_vps
,
1087 unsigned *pi_num
, unsigned *pi_den
)
1089 if( p_sps
->vui_parameters_present_flag
&& p_sps
->vui
.vui_timing_info_present_flag
)
1091 *pi_den
= p_sps
->vui
.timing
.vui_num_units_in_tick
;
1092 *pi_num
= p_sps
->vui
.timing
.vui_time_scale
;
1093 return (*pi_den
&& *pi_num
);
1095 else if( p_vps
&& p_vps
->vps_timing_info_present_flag
)
1097 *pi_den
= p_vps
->vps_num_units_in_tick
;
1098 *pi_num
= p_vps
->vps_time_scale
;
1099 return (*pi_den
&& *pi_num
);
1104 bool hevc_get_aspect_ratio( const hevc_sequence_parameter_set_t
*p_sps
,
1105 unsigned *num
, unsigned *den
)
1107 if( p_sps
->vui_parameters_present_flag
)
1109 if( p_sps
->vui
.ar
.aspect_ratio_idc
!= 255 )
1111 static const uint8_t ar_table
[16][2] =
1130 if( p_sps
->vui
.ar
.aspect_ratio_idc
> 0 &&
1131 p_sps
->vui
.ar
.aspect_ratio_idc
< 17 )
1133 *num
= ar_table
[p_sps
->vui
.ar
.aspect_ratio_idc
- 1][0];
1134 *den
= ar_table
[p_sps
->vui
.ar
.aspect_ratio_idc
- 1][1];
1140 *num
= p_sps
->vui
.ar
.sar_width
;
1141 *den
= p_sps
->vui
.ar
.sar_height
;
1148 bool hevc_get_colorimetry( const hevc_sequence_parameter_set_t
*p_sps
,
1149 video_color_primaries_t
*p_primaries
,
1150 video_transfer_func_t
*p_transfer
,
1151 video_color_space_t
*p_colorspace
,
1152 bool *p_full_range
)
1154 if( !p_sps
->vui_parameters_present_flag
)
1157 hxxx_colour_primaries_to_vlc( p_sps
->vui
.vs
.colour
.colour_primaries
);
1159 hxxx_transfer_characteristics_to_vlc( p_sps
->vui
.vs
.colour
.transfer_characteristics
);
1161 hxxx_matrix_coeffs_to_vlc( p_sps
->vui
.vs
.colour
.matrix_coeffs
);
1162 *p_full_range
= p_sps
->vui
.vs
.video_full_range_flag
;
1166 static bool hevc_parse_slice_segment_header_rbsp( bs_t
*p_bs
,
1167 pf_get_matchedxps get_matchedxps
,
1169 hevc_slice_segment_header_t
*p_sl
)
1171 hevc_sequence_parameter_set_t
*p_sps
;
1172 hevc_picture_parameter_set_t
*p_pps
;
1173 hevc_video_parameter_set_t
*p_vps
;
1175 if( bs_remain( p_bs
) < 3 )
1178 p_sl
->first_slice_segment_in_pic_flag
= bs_read1( p_bs
);
1179 if( p_sl
->nal_type
>= HEVC_NAL_BLA_W_LP
&& p_sl
->nal_type
<= HEVC_NAL_IRAP_VCL23
)
1180 p_sl
->no_output_of_prior_pics_flag
= bs_read1( p_bs
);
1181 p_sl
->slice_pic_parameter_set_id
= bs_read_ue( p_bs
);
1182 if( p_sl
->slice_pic_parameter_set_id
> HEVC_PPS_ID_MAX
|| bs_remain( p_bs
) < 1 )
1185 get_matchedxps( p_sl
->slice_pic_parameter_set_id
, priv
, &p_pps
, &p_sps
, &p_vps
);
1186 if(!p_sps
|| !p_pps
)
1189 if( !p_sl
->first_slice_segment_in_pic_flag
)
1191 if( p_pps
->dependent_slice_segments_enabled_flag
)
1192 p_sl
->dependent_slice_segment_flag
= bs_read1( p_bs
);
1195 if( !hevc_get_picture_CtbsYsize( p_sps
, &w
, &h
) )
1198 (void) bs_read( p_bs
, vlc_ceil_log2( w
* h
) ); /* slice_segment_address */
1201 if( !p_sl
->dependent_slice_segment_flag
)
1204 if( p_pps
->num_extra_slice_header_bits
> i
)
1207 bs_skip( p_bs
, 1 ); /* discardable_flag */
1210 if( p_pps
->num_extra_slice_header_bits
> i
)
1213 bs_skip( p_bs
, 1 ); /* cross_layer_bla_flag */
1216 if( i
< p_pps
->num_extra_slice_header_bits
)
1217 bs_skip( p_bs
, p_pps
->num_extra_slice_header_bits
- i
);
1219 p_sl
->slice_type
= bs_read_ue( p_bs
);
1220 if( p_sl
->slice_type
> HEVC_SLICE_TYPE_I
)
1223 if( p_pps
->output_flag_present_flag
)
1224 p_sl
->pic_output_flag
= bs_read1( p_bs
);
1227 if( p_sps
->separate_colour_plane_flag
)
1228 bs_skip( p_bs
, 2 ); /* colour_plane_id */
1230 if( p_sl
->nal_type
!= HEVC_NAL_IDR_W_RADL
&& p_sl
->nal_type
!= HEVC_NAL_IDR_N_LP
)
1231 p_sl
->pic_order_cnt_lsb
= bs_read( p_bs
, p_sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4 );
1233 p_sl
->pic_order_cnt_lsb
= 0;
1235 if( bs_remain( p_bs
) < 1 )
1241 void hevc_rbsp_release_slice_header( hevc_slice_segment_header_t
*p_sh
)
1246 hevc_slice_segment_header_t
* hevc_decode_slice_header( const uint8_t *p_buf
, size_t i_buf
, bool b_escaped
,
1247 pf_get_matchedxps get_matchedxps
, void *priv
)
1249 hevc_slice_segment_header_t
*p_sh
= calloc(1, sizeof(hevc_slice_segment_header_t
));
1253 bs_init( &bs
, p_buf
, i_buf
);
1254 unsigned i_bitflow
= 0;
1257 bs
.p_fwpriv
= &i_bitflow
;
1258 bs
.pf_forward
= hxxx_bsfw_ep3b_to_rbsp
; /* Does the emulated 3bytes conversion to rbsp */
1260 else (void) i_bitflow
;
1262 p_sh
->nal_type
= bs_read( &bs
, 6 );
1263 p_sh
->nuh_layer_id
= bs_read( &bs
, 6 );
1264 p_sh
->temporal_id_plus1
= bs_read( &bs
, 3 );
1265 if( p_sh
->nuh_layer_id
> 62 || p_sh
->temporal_id_plus1
== 0 ||
1266 !hevc_parse_slice_segment_header_rbsp( &bs
, get_matchedxps
, priv
, p_sh
) )
1268 hevc_rbsp_release_slice_header( p_sh
);
1275 bool hevc_get_slice_type( const hevc_slice_segment_header_t
*p_sli
, enum hevc_slice_type_e
*pi_type
)
1277 if( !p_sli
->dependent_slice_segment_flag
)
1279 *pi_type
= p_sli
->slice_type
;
1285 bool hevc_get_profile_level(const es_format_t
*p_fmt
, uint8_t *pi_profile
,
1286 uint8_t *pi_level
, uint8_t *pi_nal_length_size
)
1288 const uint8_t *p
= (const uint8_t*)p_fmt
->p_extra
;
1289 if(p_fmt
->i_extra
< 23 || p
[0] != 1)
1292 /* HEVCDecoderConfigurationRecord */
1294 *pi_profile
= p
[1] & 0x1F;
1299 if (pi_nal_length_size
)
1300 *pi_nal_length_size
= 1 + (p
[21]&0x03);
1306 * HEVCDecoderConfigurationRecord operations
1309 static void hevc_dcr_params_from_vps( const uint8_t * p_buffer
, size_t i_buffer
,
1310 struct hevc_dcr_values
*p_values
)
1316 bs_init( &bs
, p_buffer
, i_buffer
);
1317 unsigned i_bitflow
= 0;
1318 bs
.p_fwpriv
= &i_bitflow
;
1319 bs
.pf_forward
= hxxx_bsfw_ep3b_to_rbsp
; /* Does the emulated 3bytes conversion to rbsp */
1321 /* first two bytes are the NAL header, 3rd and 4th are:
1322 vps_video_parameter_set_id(4)
1323 vps_reserved_3_2bis(2)
1324 vps_max_layers_minus1(6)
1325 vps_max_sub_layers_minus1(3)
1326 vps_temporal_id_nesting_flags
1328 bs_skip( &bs
, 16 + 4 + 2 + 6 );
1329 p_values
->i_numTemporalLayer
= bs_read( &bs
, 3 ) + 1;
1330 p_values
->b_temporalIdNested
= bs_read1( &bs
);
1332 /* 5th & 6th are reserved 0xffff */
1334 /* copy the first 12 bytes of profile tier */
1335 for( unsigned i
=0; i
<12; i
++ )
1336 p_values
->general_configuration
[i
] = bs_read( &bs
, 8 );
1339 #define HEVC_DCR_ADD_NALS(type, count, buffers, sizes) \
1340 for (uint8_t i = 0; i < count; i++) \
1344 *p++ = (type | (b_completeness ? 0x80 : 0)); \
1345 SetWBE( p, count ); p += 2; \
1347 SetWBE( p, sizes[i]); p += 2; \
1348 memcpy( p, buffers[i], sizes[i] ); p += sizes[i];\
1351 #define HEVC_DCR_ADD_SIZES(count, sizes) \
1355 for(uint8_t i=0; i<count; i++)\
1356 i_total_size += 2 + sizes[i];\
1359 /* Generate HEVCDecoderConfiguration iso/iec 14496-15 3rd edition */
1360 uint8_t * hevc_create_dcr( const struct hevc_dcr_params
*p_params
,
1361 uint8_t i_nal_length_size
,
1362 bool b_completeness
, size_t *pi_size
)
1366 if( i_nal_length_size
!= 1 && i_nal_length_size
!= 2 && i_nal_length_size
!= 4 )
1369 struct hevc_dcr_values values
=
1371 .general_configuration
= {0},
1372 .i_numTemporalLayer
= 0,
1374 .i_bit_depth_luma_minus8
= 0,
1375 .i_bit_depth_chroma_minus8
= 0,
1376 .b_temporalIdNested
= false,
1379 if( p_params
->p_values
!= NULL
)
1381 values
= *p_params
->p_values
;
1385 if( p_params
->i_vps_count
== 0 || p_params
->i_sps_count
== 0 )
1386 return NULL
; /* required to extract info */
1388 hevc_dcr_params_from_vps( p_params
->p_vps
[0], p_params
->rgi_vps
[0], &values
);
1390 hevc_sequence_parameter_set_t
*p_sps
=
1391 hevc_decode_sps( p_params
->p_sps
[0], p_params
->rgi_sps
[0], true );
1394 values
.i_chroma_idc
= p_sps
->chroma_format_idc
;
1395 values
.i_bit_depth_chroma_minus8
= p_sps
->bit_depth_chroma_minus8
;
1396 values
.i_bit_depth_luma_minus8
= p_sps
->bit_depth_luma_minus8
;
1397 hevc_rbsp_release_sps( p_sps
);
1401 size_t i_total_size
= 1+12+2+4+2+2;
1402 HEVC_DCR_ADD_SIZES(p_params
->i_vps_count
, p_params
->rgi_vps
);
1403 HEVC_DCR_ADD_SIZES(p_params
->i_sps_count
, p_params
->rgi_sps
);
1404 HEVC_DCR_ADD_SIZES(p_params
->i_pps_count
, p_params
->rgi_pps
);
1405 HEVC_DCR_ADD_SIZES(p_params
->i_sei_count
, p_params
->rgi_sei
);
1407 uint8_t *p_data
= malloc( i_total_size
);
1408 if( p_data
== NULL
)
1411 *pi_size
= i_total_size
;
1412 uint8_t *p
= p_data
;
1416 memcpy( p
, values
.general_configuration
, 12 ); p
+= 12;
1417 /* Don't set min spatial segmentation */
1418 SetWBE( p
, 0xF000 ); p
+= 2;
1419 /* Don't set parallelism type since segmentation isn't set */
1421 *p
++ = (0xFC | (values
.i_chroma_idc
& 0x03));
1422 *p
++ = (0xF8 | (values
.i_bit_depth_luma_minus8
& 0x07));
1423 *p
++ = (0xF8 | (values
.i_bit_depth_chroma_minus8
& 0x07));
1425 /* Don't set framerate */
1426 SetWBE( p
, 0x0000); p
+= 2;
1427 /* Force NAL size of 4 bytes that replace the startcode */
1428 *p
++ = ( ((values
.i_numTemporalLayer
& 0x07) << 3) |
1429 (values
.b_temporalIdNested
<< 2) |
1430 (i_nal_length_size
- 1) );
1431 /* total number of arrays */
1432 *p
++ = !!p_params
->i_vps_count
+ !!p_params
->i_sps_count
+
1433 !!p_params
->i_pps_count
+ !!p_params
->i_sei_count
;
1435 /* Write NAL arrays */
1436 HEVC_DCR_ADD_NALS(HEVC_NAL_VPS
, p_params
->i_vps_count
,
1437 p_params
->p_vps
, p_params
->rgi_vps
);
1438 HEVC_DCR_ADD_NALS(HEVC_NAL_SPS
, p_params
->i_sps_count
,
1439 p_params
->p_sps
, p_params
->rgi_sps
);
1440 HEVC_DCR_ADD_NALS(HEVC_NAL_PPS
, p_params
->i_pps_count
,
1441 p_params
->p_pps
, p_params
->rgi_pps
);
1442 HEVC_DCR_ADD_NALS(HEVC_NAL_PREF_SEI
, p_params
->i_sei_count
,
1443 p_params
->p_sei
, p_params
->rgi_sei
);
1448 #undef HEVC_DCR_ADD_NALS
1449 #undef HEVC_DCR_ADD_SIZES
1453 * 8.3.1 Decoding process for POC
1455 int hevc_compute_picture_order_count( const hevc_sequence_parameter_set_t
*p_sps
,
1456 const hevc_slice_segment_header_t
*p_slice
,
1457 hevc_poc_ctx_t
*p_ctx
)
1465 bool NoRaslOutputFlag
;
1466 bool IsIRAP
= ( p_slice
->nal_type
>= HEVC_NAL_BLA_W_LP
&&
1467 p_slice
->nal_type
<= HEVC_NAL_IRAP_VCL23
);
1471 /* if( IRAP ) NoRaslOutputFlag = first || IDR || BLA || after(EOSNAL) */
1472 NoRaslOutputFlag
=(p_ctx
->first_picture
||
1473 p_slice
->nal_type
== HEVC_NAL_IDR_N_LP
||
1474 p_slice
->nal_type
== HEVC_NAL_IDR_W_RADL
||
1475 p_slice
->nal_type
== HEVC_NAL_BLA_W_LP
||
1476 p_slice
->nal_type
== HEVC_NAL_BLA_W_RADL
||
1477 p_slice
->nal_type
== HEVC_NAL_BLA_N_LP
||
1478 p_ctx
->HandleCraAsBlaFlag
);
1482 NoRaslOutputFlag
= false;
1485 #ifdef HEVC_POC_DEBUG
1486 printf("slice lsb=%"PRIu32
" irap=%d norasl=%d tid=%d msb=%d lsb=%d",
1487 p_slice
->pic_order_cnt_lsb
,
1488 IsIRAP
, NoRaslOutputFlag
, p_slice
->temporal_id_plus1
,
1489 p_ctx
->prevTid0PicOrderCnt
.msb
, p_ctx
->prevTid0PicOrderCnt
.lsb
);
1492 if( p_slice
->nal_type
== HEVC_NAL_IDR_N_LP
||
1493 p_slice
->nal_type
== HEVC_NAL_IDR_W_RADL
)
1495 prevPicOrderCnt
.msb
= 0;
1496 prevPicOrderCnt
.lsb
= 0;
1498 /* Not an IRAP with NoRaslOutputFlag == 1 */
1499 else if( !IsIRAP
|| !NoRaslOutputFlag
)
1501 prevPicOrderCnt
.msb
= p_ctx
->prevTid0PicOrderCnt
.msb
;
1502 prevPicOrderCnt
.lsb
= p_ctx
->prevTid0PicOrderCnt
.lsb
;
1505 if( IsIRAP
&& NoRaslOutputFlag
)
1511 const unsigned maxPocLSB
= 1U << (p_sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4);
1512 pocMSB
= prevPicOrderCnt
.msb
;
1513 int64_t orderDiff
= (int64_t)p_slice
->pic_order_cnt_lsb
- prevPicOrderCnt
.lsb
;
1514 if( orderDiff
< 0 && -orderDiff
>= maxPocLSB
/ 2 )
1515 pocMSB
+= maxPocLSB
;
1516 else if( orderDiff
> maxPocLSB
/ 2 )
1517 pocMSB
-= maxPocLSB
;
1520 /* Set prevTid0Pic for next pic */
1521 if( p_slice
->temporal_id_plus1
== 1 &&
1522 !( ( p_slice
->nal_type
<= HEVC_NAL_RSV_VCL_N14
&& p_slice
->nal_type
% 2 == 0 /* SLNR */ ) ||
1523 ( p_slice
->nal_type
>= HEVC_NAL_RADL_N
&& p_slice
->nal_type
<= HEVC_NAL_RASL_R
) /* RADL or RASL */ ) )
1525 p_ctx
->prevTid0PicOrderCnt
.msb
= pocMSB
;
1526 p_ctx
->prevTid0PicOrderCnt
.lsb
= p_slice
->pic_order_cnt_lsb
;
1529 p_ctx
->first_picture
= false;
1531 #ifdef HEVC_POC_DEBUG
1532 printf(" POC=%"PRIu32
"\n", pocMSB
+ p_slice
->pic_order_cnt_lsb
);
1535 return pocMSB
+ p_slice
->pic_order_cnt_lsb
;
1538 struct hevc_sei_pic_timing_t
1540 nal_u4_t pic_struct
;
1541 nal_u2_t source_scan_type
;
1544 void hevc_release_sei_pic_timing( hevc_sei_pic_timing_t
*p_timing
)
1549 hevc_sei_pic_timing_t
* hevc_decode_sei_pic_timing( bs_t
*p_bs
,
1550 const hevc_sequence_parameter_set_t
*p_sps
)
1552 hevc_sei_pic_timing_t
*p_timing
= malloc(sizeof(*p_timing
));
1555 if( p_sps
->vui
.frame_field_info_present_flag
)
1557 p_timing
->pic_struct
= bs_read( p_bs
, 4 );
1558 p_timing
->source_scan_type
= bs_read( p_bs
, 2 );
1562 p_timing
->pic_struct
= 0;
1563 p_timing
->source_scan_type
= 1;
1569 bool hevc_frame_is_progressive( const hevc_sequence_parameter_set_t
*p_sps
,
1570 const hevc_sei_pic_timing_t
*p_timing
)
1572 if( p_sps
->vui_parameters_present_flag
&&
1573 p_sps
->vui
.field_seq_flag
)
1576 if( p_sps
->profile_tier_level
.general
.interlaced_source_flag
&&
1577 !p_sps
->profile_tier_level
.general
.progressive_source_flag
)
1580 if( p_timing
&& p_sps
->vui
.frame_field_info_present_flag
)
1582 if( p_timing
->source_scan_type
< 2 )
1583 return p_timing
->source_scan_type
!= 0;
1589 uint8_t hevc_get_num_clock_ts( const hevc_sequence_parameter_set_t
*p_sps
,
1590 const hevc_sei_pic_timing_t
*p_timing
)
1592 if( p_sps
->vui
.frame_field_info_present_flag
&& p_timing
&& p_timing
->pic_struct
< 13 )
1594 /* !WARN modified with units_field_based_flag (D.3.25) for values 0, 7 and 8 */
1595 const uint8_t rgi_numclock
[13] = { 2, 1, 1, 2, 2, 3, 3, 4, 6, 1, 1, 1, 1 };
1596 return rgi_numclock
[p_timing
->pic_struct
];
1599 if( p_sps
->vui_parameters_present_flag
)
1601 if( p_sps
->vui
.field_seq_flag
)
1602 return 1; /* D.3.27 */
1604 else if( p_sps
->profile_tier_level
.general
.interlaced_source_flag
&&
1605 !p_sps
->profile_tier_level
.general
.progressive_source_flag
)