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 *****************************************************************************/
26 #include "hxxx_ep3b.h"
27 #include "iso_color_tables.h"
29 #include <vlc_common.h>
34 //#define HEVC_POC_DEBUG
36 typedef uint8_t nal_u1_t
;
37 typedef uint8_t nal_u2_t
;
38 typedef uint8_t nal_u3_t
;
39 typedef uint8_t nal_u4_t
;
40 typedef uint8_t nal_u5_t
;
41 typedef uint8_t nal_u6_t
;
42 typedef uint8_t nal_u7_t
;
43 typedef uint8_t nal_u8_t
;
44 typedef int32_t nal_se_t
;
45 typedef uint32_t nal_ue_t
;
49 nal_u2_t profile_space
;
52 uint32_t profile_compatibility_flag
; /* nal_u1_t * 32 */
53 nal_u1_t progressive_source_flag
;
54 nal_u1_t interlaced_source_flag
;
55 nal_u1_t non_packed_constraint_flag
;
56 nal_u1_t frame_only_constraint_flag
;
59 nal_u1_t max_12bit_constraint_flag
;
60 nal_u1_t max_10bit_constraint_flag
;
61 nal_u1_t max_8bit_constraint_flag
;
62 nal_u1_t max_422chroma_constraint_flag
;
63 nal_u1_t max_420chroma_constraint_flag
;
64 nal_u1_t max_monochrome_constraint_flag
;
65 nal_u1_t intra_constraint_flag
;
66 nal_u1_t one_picture_only_constraint_flag
;
67 nal_u1_t lower_bit_rate_constraint_flag
;
73 } hevc_inner_profile_tier_level_t
;
75 #define HEVC_MAX_SUBLAYERS 8
78 hevc_inner_profile_tier_level_t general
;
79 nal_u8_t general_level_idc
;
80 uint8_t sublayer_profile_present_flag
; /* nal_u1_t * 8 */
81 uint8_t sublayer_level_present_flag
; /* nal_u1_t * 8 */
82 hevc_inner_profile_tier_level_t sub_layer
[HEVC_MAX_SUBLAYERS
];
83 nal_u8_t sub_layer_level_idc
[HEVC_MAX_SUBLAYERS
];
84 } hevc_profile_tier_level_t
;
86 #define HEVC_MAX_SHORT_TERM_REF_PIC_SET 65
87 #define HEVC_MAX_LONG_TERM_REF_PIC_SET 33
91 unsigned num_delta_pocs
;
92 } hevc_short_term_ref_pic_set_t
;
96 nal_u1_t aspect_ratio_info_present_flag
;
99 nal_u8_t aspect_ratio_idc
;
103 nal_u1_t overscan_info_present_flag
;
104 nal_u1_t overscan_appropriate_flag
;
106 nal_u1_t video_signal_type_present_flag
;
109 nal_u3_t video_format
;
110 nal_u1_t video_full_range_flag
;
111 nal_u1_t colour_description_present_flag
;
114 nal_u8_t colour_primaries
;
115 nal_u8_t transfer_characteristics
;
116 nal_u8_t matrix_coeffs
;
120 nal_u1_t chroma_loc_info_present_flag
;
123 nal_ue_t sample_loc_type_top_field
;
124 nal_ue_t sample_loc_type_bottom_field
;
127 nal_u1_t neutral_chroma_indication_flag
;
128 nal_u1_t field_seq_flag
;
129 nal_u1_t frame_field_info_present_flag
;
131 nal_u1_t default_display_window_flag
;
134 nal_ue_t win_left_offset
;
135 nal_ue_t win_right_offset
;
136 nal_ue_t win_top_offset
;
137 nal_ue_t win_bottom_offset
;
140 nal_u1_t vui_timing_info_present_flag
;
143 uint32_t vui_num_units_in_tick
;
144 uint32_t vui_time_scale
;
149 } hevc_vui_parameters_t
;
151 struct hevc_video_parameter_set_t
153 nal_u4_t vps_video_parameter_set_id
;
154 nal_u1_t vps_base_layer_internal_flag
;
155 nal_u1_t vps_base_layer_available_flag
;
156 nal_u6_t vps_max_layers_minus1
;
157 nal_u3_t vps_max_sub_layers_minus1
;
158 nal_u1_t vps_temporal_id_nesting_flag
;
160 hevc_profile_tier_level_t profile_tier_level
;
162 nal_u1_t vps_sub_layer_ordering_info_present_flag
;
165 nal_ue_t dec_pic_buffering_minus1
;
166 nal_ue_t num_reorder_pics
;
167 nal_ue_t max_latency_increase_plus1
;
168 } vps_max
[1 + HEVC_MAX_SUBLAYERS
];
170 nal_u6_t vps_max_layer_id
;
171 nal_ue_t vps_num_layer_set_minus1
;
172 // layer_id_included_flag; read but discarded
174 nal_u1_t vps_timing_info_present_flag
;
175 uint32_t vps_num_units_in_tick
;
176 uint32_t vps_time_scale
;
181 struct hevc_sequence_parameter_set_t
183 nal_u4_t sps_video_parameter_set_id
;
184 nal_u3_t sps_max_sub_layers_minus1
;
185 nal_u1_t sps_temporal_id_nesting_flag
;
187 hevc_profile_tier_level_t profile_tier_level
;
189 nal_ue_t sps_seq_parameter_set_id
;
190 nal_ue_t chroma_format_idc
;
191 nal_u1_t separate_colour_plane_flag
;
193 nal_ue_t pic_width_in_luma_samples
;
194 nal_ue_t pic_height_in_luma_samples
;
196 nal_u1_t conformance_window_flag
;
199 nal_ue_t left_offset
;
200 nal_ue_t right_offset
;
202 nal_ue_t bottom_offset
;
205 nal_ue_t bit_depth_luma_minus8
;
206 nal_ue_t bit_depth_chroma_minus8
;
207 nal_ue_t log2_max_pic_order_cnt_lsb_minus4
;
209 nal_u1_t sps_sub_layer_ordering_info_present_flag
;
212 nal_ue_t dec_pic_buffering_minus1
;
213 nal_ue_t num_reorder_pics
;
214 nal_ue_t latency_increase_plus1
;
215 } sps_max
[1 + HEVC_MAX_SUBLAYERS
];
217 nal_ue_t log2_min_luma_coding_block_size_minus3
;
218 nal_ue_t log2_diff_max_min_luma_coding_block_size
;
219 nal_ue_t log2_min_luma_transform_block_size_minus2
;
220 nal_ue_t log2_diff_max_min_luma_transform_block_size
;
223 nal_ue_t max_transform_hierarchy_depth_inter
;
224 nal_ue_t max_transform_hierarchy_depth_intra
;
225 nal_u1_t scaling_list_enabled
;
226 nal_u1_t sps_scaling_list_data_present_flag
;
227 // scaling_list_data; read but discarded
229 nal_u1_t amp_enabled_flag
;
230 nal_u1_t sample_adaptive_offset_enabled_flag
;
232 nal_u1_t pcm_enabled_flag
;
233 nal_u4_t pcm_sample_bit_depth_luma_minus1
;
234 nal_u4_t pcm_sample_bit_depth_chroma_minus1
;
235 nal_ue_t log2_min_pcm_luma_coding_block_size_minus3
;
236 nal_ue_t log2_diff_max_min_pcm_luma_coding_block_size
;
237 nal_u1_t pcm_loop_filter_disabled_flag
;
239 nal_ue_t num_short_term_ref_pic_sets
;
242 nal_u1_t long_term_ref_pics_present_flag
;
243 nal_ue_t num_long_term_ref_pics_sps
;
246 nal_u1_t sps_temporal_mvp_enabled_flag
;
247 nal_u1_t strong_intra_smoothing_enabled_flag
;
249 nal_u1_t vui_parameters_present_flag
;
250 hevc_vui_parameters_t vui
;
254 struct hevc_picture_parameter_set_t
256 nal_ue_t pps_pic_parameter_set_id
;
257 nal_ue_t pps_seq_parameter_set_id
;
258 nal_u1_t dependent_slice_segments_enabled_flag
;
259 nal_u1_t output_flag_present_flag
;
260 nal_u3_t num_extra_slice_header_bits
;
261 nal_u1_t sign_data_hiding_enabled_flag
;
262 nal_u1_t cabac_init_present_flag
;
263 nal_ue_t num_ref_idx_l0_default_active_minus1
;
264 nal_ue_t num_ref_idx_l1_default_active_minus1
;
265 nal_se_t init_qp_minus26
;
266 nal_u1_t constrained_intra_pred_flag
;
267 nal_u1_t transform_skip_enabled_flag
;
269 nal_u1_t cu_qp_delta_enabled_flag
;
270 nal_ue_t diff_cu_qp_delta_depth
;
272 nal_se_t pps_cb_qp_offset
;
273 nal_se_t pps_cr_qp_offset
;
274 nal_u1_t pic_slice_level_chroma_qp_offsets_present_flag
;
275 nal_u1_t weighted_pred_flag
;
276 nal_u1_t weighted_bipred_flag
;
277 nal_u1_t transquant_bypass_enable_flag
;
279 nal_u1_t tiles_enabled_flag
;
280 nal_u1_t entropy_coding_sync_enabled_flag
;
281 nal_ue_t num_tile_columns_minus1
;
282 nal_ue_t num_tile_rows_minus1
;
283 nal_u1_t uniform_spacing_flag
;
284 // nal_ue_t *p_column_width_minus1; read but discarded
285 // nal_ue_t *p_row_height_minus1; read but discarded
286 nal_u1_t loop_filter_across_tiles_enabled_flag
;
288 nal_u1_t pps_loop_filter_across_slices_enabled_flag
;
290 nal_u1_t deblocking_filter_control_present_flag
;
291 nal_u1_t deblocking_filter_override_enabled_flag
;
292 nal_u1_t pps_deblocking_filter_disabled_flag
;
293 nal_se_t pps_beta_offset_div2
;
294 nal_se_t pps_tc_offset_div2
;
296 nal_u1_t scaling_list_data_present_flag
;
297 // scaling_list_data; read but discarded
299 nal_u1_t lists_modification_present_flag
;
300 nal_ue_t log2_parallel_merge_level_minus2
;
301 nal_u1_t slice_header_extension_present_flag
;
303 nal_u1_t pps_extension_present_flag
;
304 nal_u1_t pps_range_extension_flag
;
305 nal_u1_t pps_multilayer_extension_flag
;
306 nal_u1_t pps_3d_extension_flag
;
307 nal_u5_t pps_extension_5bits
;
312 struct hevc_slice_segment_header_t
315 nal_u6_t nuh_layer_id
;
316 nal_u3_t temporal_id_plus1
;
317 nal_u1_t first_slice_segment_in_pic_flag
;
318 nal_u1_t no_output_of_prior_pics_flag
;
319 nal_ue_t slice_pic_parameter_set_id
;
320 nal_u1_t dependent_slice_segment_flag
;
321 // slice_segment_address; read but discarded
323 nal_u1_t pic_output_flag
;
325 uint32_t pic_order_cnt_lsb
;
330 /* Computes size and does check the whole struct integrity */
331 static size_t get_hvcC_to_AnnexB_NAL_size( const uint8_t *p_buf
, size_t i_buf
)
335 if( i_buf
< HEVC_MIN_HVCC_SIZE
)
338 const uint8_t i_nal_length_size
= (p_buf
[21] & 0x03) + 1;
339 if(i_nal_length_size
== 3)
342 const uint8_t i_num_array
= p_buf
[22];
343 p_buf
+= 23; i_buf
-= 23;
345 for( uint8_t i
= 0; i
< i_num_array
; i
++ )
350 const uint16_t i_num_nalu
= p_buf
[1] << 8 | p_buf
[2];
351 p_buf
+= 3; i_buf
-= 3;
353 for( uint16_t j
= 0; j
< i_num_nalu
; j
++ )
358 const uint16_t i_nalu_length
= p_buf
[0] << 8 | p_buf
[1];
359 if(i_buf
< (size_t)i_nalu_length
+ 2)
362 i_total
+= i_nalu_length
+ i_nal_length_size
;
363 p_buf
+= i_nalu_length
+ 2;
364 i_buf
-= i_nalu_length
+ 2;
371 uint8_t * hevc_hvcC_to_AnnexB_NAL( const uint8_t *p_buf
, size_t i_buf
,
372 size_t *pi_result
, uint8_t *pi_nal_length_size
)
374 *pi_result
= get_hvcC_to_AnnexB_NAL_size( p_buf
, i_buf
); /* Does all checks */
375 if( *pi_result
== 0 )
378 if( pi_nal_length_size
)
379 *pi_nal_length_size
= hevc_getNALLengthSize( p_buf
);
382 uint8_t *p_out_buf
= p_ret
= malloc( *pi_result
);
389 const uint8_t i_num_array
= p_buf
[22];
392 for( uint8_t i
= 0; i
< i_num_array
; i
++ )
394 const uint16_t i_num_nalu
= p_buf
[1] << 8 | p_buf
[2];
397 for( uint16_t j
= 0; j
< i_num_nalu
; j
++ )
399 const uint16_t i_nalu_length
= p_buf
[0] << 8 | p_buf
[1];
401 memcpy( p_out_buf
, annexb_startcode4
, 4 );
402 memcpy( &p_out_buf
[4], &p_buf
[2], i_nalu_length
);
404 p_out_buf
+= 4 + i_nalu_length
;
405 p_buf
+= 2 + i_nalu_length
;
412 static bool hevc_parse_scaling_list_rbsp( bs_t
*p_bs
)
414 if( bs_remain( p_bs
) < 16 )
417 for( int i
=0; i
<4; i
++ )
419 for( int j
=0; j
<6; j
+= (i
== 3) ? 3 : 1 )
421 if( bs_read1( p_bs
) == 0 )
425 unsigned nextCoef
= 8;
426 unsigned coefNum
= __MIN( 64, (1 << (4 + (i
<< 1))));
429 nextCoef
= bs_read_se( p_bs
) + 8;
431 for( unsigned k
=0; k
<coefNum
; k
++ )
433 nextCoef
= ( nextCoef
+ bs_read_se( p_bs
) + 256 ) % 256;
442 static bool hevc_parse_vui_parameters_rbsp( bs_t
*p_bs
, hevc_vui_parameters_t
*p_vui
,
445 if( bs_remain( p_bs
) < 10 )
448 p_vui
->aspect_ratio_info_present_flag
= bs_read1( p_bs
);
449 if( p_vui
->aspect_ratio_info_present_flag
)
451 p_vui
->ar
.aspect_ratio_idc
= bs_read( p_bs
, 8 );
452 if( p_vui
->ar
.aspect_ratio_idc
== 0xFF ) //HEVC_SAR__IDC_EXTENDED_SAR )
454 p_vui
->ar
.sar_width
= bs_read( p_bs
, 16 );
455 p_vui
->ar
.sar_height
= bs_read( p_bs
, 16 );
459 p_vui
->overscan_info_present_flag
= bs_read1( p_bs
);
460 if( p_vui
->overscan_info_present_flag
)
461 p_vui
->overscan_appropriate_flag
= bs_read1( p_bs
);
463 p_vui
->video_signal_type_present_flag
= bs_read1( p_bs
);
464 if( p_vui
->video_signal_type_present_flag
)
466 p_vui
->vs
.video_format
= bs_read( p_bs
, 3 );
467 p_vui
->vs
.video_full_range_flag
= bs_read1( p_bs
);
468 p_vui
->vs
.colour_description_present_flag
= bs_read1( p_bs
);
469 if( p_vui
->vs
.colour_description_present_flag
)
471 p_vui
->vs
.colour
.colour_primaries
= bs_read( p_bs
, 8 );
472 p_vui
->vs
.colour
.transfer_characteristics
= bs_read( p_bs
, 8 );
473 p_vui
->vs
.colour
.matrix_coeffs
= bs_read( p_bs
, 8 );
477 p_vui
->vs
.colour
.colour_primaries
= ISO_23001_8_CP_UNSPECIFIED
;
478 p_vui
->vs
.colour
.transfer_characteristics
= ISO_23001_8_TC_UNSPECIFIED
;
479 p_vui
->vs
.colour
.matrix_coeffs
= ISO_23001_8_MC_UNSPECIFIED
;
483 p_vui
->chroma_loc_info_present_flag
= bs_read1( p_bs
);
484 if( p_vui
->chroma_loc_info_present_flag
)
486 p_vui
->chroma
.sample_loc_type_top_field
= bs_read_ue( p_bs
);
487 p_vui
->chroma
.sample_loc_type_bottom_field
= bs_read_ue( p_bs
);
490 p_vui
->neutral_chroma_indication_flag
= bs_read1( p_bs
);
491 p_vui
->field_seq_flag
= bs_read1( p_bs
);
492 p_vui
->frame_field_info_present_flag
= bs_read1( p_bs
);
494 p_vui
->default_display_window_flag
= !b_broken
&& bs_read1( p_bs
);
495 if( p_vui
->default_display_window_flag
)
497 p_vui
->def_disp
.win_left_offset
= bs_read_ue( p_bs
);
498 p_vui
->def_disp
.win_right_offset
= bs_read_ue( p_bs
);
499 p_vui
->def_disp
.win_top_offset
= bs_read_ue( p_bs
);
500 p_vui
->def_disp
.win_bottom_offset
= bs_read_ue( p_bs
);
503 p_vui
->vui_timing_info_present_flag
= bs_read1( p_bs
);
504 if( p_vui
->vui_timing_info_present_flag
)
506 p_vui
->timing
.vui_num_units_in_tick
= bs_read( p_bs
, 32 );
507 p_vui
->timing
.vui_time_scale
= bs_read( p_bs
, 32 );
509 if( bs_remain( p_bs
) < 3 )
514 if( bs_remain( p_bs
) < 1 ) /* late fail */
520 /* Shortcut for retrieving vps/sps/pps id */
521 bool hevc_get_xps_id(const uint8_t *p_buf
, size_t i_buf
, uint8_t *pi_id
)
525 /* No need to lookup convert from emulation for that data */
526 uint8_t i_nal_type
= hevc_getNALType(p_buf
);
528 bs_init(&bs
, &p_buf
[2], i_buf
- 2);
529 if(i_nal_type
== HEVC_NAL_PPS
)
531 *pi_id
= bs_read_ue( &bs
);
532 if(*pi_id
> HEVC_PPS_ID_MAX
)
537 *pi_id
= bs_read( &bs
, 4 );
538 if(i_nal_type
== HEVC_NAL_SPS
)
540 if(*pi_id
> HEVC_SPS_ID_MAX
)
543 else if(*pi_id
> HEVC_VPS_ID_MAX
)
549 static bool hevc_parse_inner_profile_tier_level_rbsp( bs_t
*p_bs
,
550 hevc_inner_profile_tier_level_t
*p_in
)
552 if( bs_remain( p_bs
) < 88 )
555 p_in
->profile_space
= bs_read( p_bs
, 2 );
556 p_in
->tier_flag
= bs_read1( p_bs
);
557 p_in
->profile_idc
= bs_read( p_bs
, 5 );
558 p_in
->profile_compatibility_flag
= bs_read( p_bs
, 32 );
559 p_in
->progressive_source_flag
= bs_read1( p_bs
);
560 p_in
->interlaced_source_flag
= bs_read1( p_bs
);
561 p_in
->non_packed_constraint_flag
= bs_read1( p_bs
);
562 p_in
->frame_only_constraint_flag
= bs_read1( p_bs
);
564 if( ( p_in
->profile_idc
>= 4 && p_in
->profile_idc
<= 7 ) ||
565 ( p_in
->profile_compatibility_flag
& 0x0F000000 ) )
567 p_in
->idc4to7
.max_12bit_constraint_flag
= bs_read1( p_bs
);
568 p_in
->idc4to7
.max_10bit_constraint_flag
= bs_read1( p_bs
);
569 p_in
->idc4to7
.max_8bit_constraint_flag
= bs_read1( p_bs
);
570 p_in
->idc4to7
.max_422chroma_constraint_flag
= bs_read1( p_bs
);
571 p_in
->idc4to7
.max_420chroma_constraint_flag
= bs_read1( p_bs
);
572 p_in
->idc4to7
.max_monochrome_constraint_flag
= bs_read1( p_bs
);
573 p_in
->idc4to7
.intra_constraint_flag
= bs_read1( p_bs
);
574 p_in
->idc4to7
.one_picture_only_constraint_flag
= bs_read1( p_bs
);
575 p_in
->idc4to7
.lower_bit_rate_constraint_flag
= bs_read1( p_bs
);
576 (void) bs_read( p_bs
, 2 );
580 (void) bs_read( p_bs
, 11 );
582 (void) bs_read( p_bs
, 32 );
584 if( ( p_in
->profile_idc
>= 1 && p_in
->profile_idc
<= 5 ) ||
585 ( p_in
->profile_compatibility_flag
& 0x7C000000 ) )
586 p_in
->idc1to5
.inbld_flag
= bs_read1( p_bs
);
588 (void) bs_read1( p_bs
);
593 static bool hevc_parse_profile_tier_level_rbsp( bs_t
*p_bs
, bool profile_present
,
594 uint8_t max_num_sub_layers_minus1
,
595 hevc_profile_tier_level_t
*p_ptl
)
597 if( profile_present
&& !hevc_parse_inner_profile_tier_level_rbsp( p_bs
, &p_ptl
->general
) )
600 if( bs_remain( p_bs
) < 8)
603 p_ptl
->general_level_idc
= bs_read( p_bs
, 8 );
605 if( max_num_sub_layers_minus1
> 0 )
607 if( bs_remain( p_bs
) < 16 )
610 for( uint8_t i
=0; i
< 8; i
++ )
612 if( i
< max_num_sub_layers_minus1
)
614 if( bs_read1( p_bs
) )
615 p_ptl
->sublayer_profile_present_flag
|= (0x80 >> i
);
616 if( bs_read1( p_bs
) )
617 p_ptl
->sublayer_level_present_flag
|= (0x80 >> i
);
623 for( uint8_t i
=0; i
< max_num_sub_layers_minus1
; i
++ )
625 if( ( p_ptl
->sublayer_profile_present_flag
& (0x80 >> i
) ) &&
626 ! hevc_parse_inner_profile_tier_level_rbsp( p_bs
, &p_ptl
->sub_layer
[i
] ) )
629 if( p_ptl
->sublayer_profile_present_flag
& (0x80 >> i
) )
631 if( bs_remain( p_bs
) < 8 )
633 p_ptl
->sub_layer_level_idc
[i
] = bs_read( p_bs
, 8 );
641 static bool hevc_parse_video_parameter_set_rbsp( bs_t
*p_bs
,
642 hevc_video_parameter_set_t
*p_vps
)
644 if( bs_remain( p_bs
) < 134 )
647 p_vps
->vps_video_parameter_set_id
= bs_read( p_bs
, 4 );
648 p_vps
->vps_base_layer_internal_flag
= bs_read1( p_bs
);
649 p_vps
->vps_base_layer_available_flag
= bs_read1( p_bs
);
650 p_vps
->vps_max_layers_minus1
= bs_read( p_bs
, 6 );
651 p_vps
->vps_max_sub_layers_minus1
= bs_read( p_bs
, 3 );
652 p_vps
->vps_temporal_id_nesting_flag
= bs_read1( p_bs
);
655 if( !hevc_parse_profile_tier_level_rbsp( p_bs
, true, p_vps
->vps_max_sub_layers_minus1
,
656 &p_vps
->profile_tier_level
) )
659 p_vps
->vps_sub_layer_ordering_info_present_flag
= bs_read1( p_bs
);
660 for( unsigned i
= (p_vps
->vps_sub_layer_ordering_info_present_flag
?
661 0 : p_vps
->vps_max_sub_layers_minus1
);
662 i
<= p_vps
->vps_max_sub_layers_minus1
; i
++ )
664 p_vps
->vps_max
[i
].dec_pic_buffering_minus1
= bs_read_ue( p_bs
);
665 p_vps
->vps_max
[i
].num_reorder_pics
= bs_read_ue( p_bs
);
666 p_vps
->vps_max
[i
].max_latency_increase_plus1
= bs_read_ue( p_bs
);
668 if( bs_remain( p_bs
) < 10 )
671 p_vps
->vps_max_layer_id
= bs_read( p_bs
, 6 );
672 p_vps
->vps_num_layer_set_minus1
= bs_read_ue( p_bs
);
673 // layer_id_included_flag; read but discarded
674 bs_skip( p_bs
, p_vps
->vps_num_layer_set_minus1
* (p_vps
->vps_max_layer_id
+ 1) );
675 if( bs_remain( p_bs
) < 2 )
678 p_vps
->vps_timing_info_present_flag
= bs_read1( p_bs
);
679 if( p_vps
->vps_timing_info_present_flag
)
681 p_vps
->vps_num_units_in_tick
= bs_read( p_bs
, 32 );
682 p_vps
->vps_time_scale
= bs_read( p_bs
, 32 );
684 /* parsing incomplete */
686 if( bs_remain( p_bs
) < 1 )
692 void hevc_rbsp_release_vps( hevc_video_parameter_set_t
*p_vps
)
697 #define IMPL_hevc_generic_decode( name, hevctype, decode, release ) \
698 hevctype * name( const uint8_t *p_buf, size_t i_buf, bool b_escaped ) \
700 hevctype *p_hevctype = calloc(1, sizeof(hevctype)); \
701 if(likely(p_hevctype)) \
704 struct hxxx_bsfw_ep3b_ctx_s bsctx; \
707 hxxx_bsfw_ep3b_ctx_init( &bsctx ); \
708 bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );\
710 else bs_init( &bs, p_buf, i_buf ); \
711 bs_skip( &bs, 7 ); /* nal_unit_header */ \
712 uint8_t i_nuh_layer_id = bs_read( &bs, 6 ); \
713 bs_skip( &bs, 3 ); /* !nal_unit_header */ \
714 if( i_nuh_layer_id > 62 || !decode( &bs, p_hevctype ) ) \
716 release( p_hevctype ); \
723 IMPL_hevc_generic_decode( hevc_decode_vps
, hevc_video_parameter_set_t
,
724 hevc_parse_video_parameter_set_rbsp
, hevc_rbsp_release_vps
)
726 static bool hevc_parse_st_ref_pic_set( bs_t
*p_bs
, unsigned stRpsIdx
,
727 unsigned num_short_term_ref_pic_sets
,
728 hevc_short_term_ref_pic_set_t
*p_sets
)
730 if( stRpsIdx
&& bs_read1( p_bs
) ) /* Interref pic set prediction flag */
732 nal_ue_t delta_idx_minus_1
= 0;
733 if( stRpsIdx
== num_short_term_ref_pic_sets
)
735 delta_idx_minus_1
= bs_read_ue( p_bs
);
736 if( delta_idx_minus_1
>= stRpsIdx
)
739 if(delta_idx_minus_1
== stRpsIdx
)
742 nal_u1_t delta_rps_sign
= bs_read1( p_bs
);
743 nal_ue_t abs_delta_rps_minus1
= bs_read_ue( p_bs
);
744 unsigned RefRpsIdx
= stRpsIdx
- delta_idx_minus_1
- 1;
745 int deltaRps
= ( 1 - ( delta_rps_sign
<< 1 ) ) * ( abs_delta_rps_minus1
+ 1 );
746 VLC_UNUSED(deltaRps
);
748 unsigned numDeltaPocs
= p_sets
[RefRpsIdx
].num_delta_pocs
;
749 p_sets
[stRpsIdx
].num_delta_pocs
= 0;
750 for( unsigned j
=0; j
<= numDeltaPocs
; j
++ )
752 if( ! bs_read1( p_bs
) ) /* used_by_curr_pic_flag */
754 if( bs_read1( p_bs
) ) /* use_delta_flag */
755 p_sets
[stRpsIdx
].num_delta_pocs
++;
758 p_sets
[stRpsIdx
].num_delta_pocs
++;
763 nal_ue_t num_negative_pics
= bs_read_ue( p_bs
);
764 nal_ue_t num_positive_pics
= bs_read_ue( p_bs
);
765 if( bs_remain( p_bs
) < (num_negative_pics
+ num_positive_pics
) * 2 )
767 for(unsigned int i
=0; i
<num_negative_pics
; i
++)
769 (void) bs_read_ue( p_bs
); /* delta_poc_s0_minus1 */
770 (void) bs_read1( p_bs
); /* used_by_current_pic_s0_flag */
772 for(unsigned int i
=0; i
<num_positive_pics
; i
++)
774 (void) bs_read_ue( p_bs
); /* delta_poc_s1_minus1 */
775 (void) bs_read1( p_bs
); /* used_by_current_pic_s1_flag */
777 p_sets
[stRpsIdx
].num_delta_pocs
= num_positive_pics
+ num_negative_pics
;
783 static bool hevc_parse_sequence_parameter_set_rbsp( bs_t
*p_bs
,
784 hevc_sequence_parameter_set_t
*p_sps
)
786 p_sps
->sps_video_parameter_set_id
= bs_read( p_bs
, 4 );
787 p_sps
->sps_max_sub_layers_minus1
= bs_read( p_bs
, 3 );
788 p_sps
->sps_temporal_id_nesting_flag
= bs_read1( p_bs
);
789 if( !hevc_parse_profile_tier_level_rbsp( p_bs
, true, p_sps
->sps_max_sub_layers_minus1
,
790 &p_sps
->profile_tier_level
) )
793 if( bs_remain( p_bs
) < 1 )
796 p_sps
->sps_seq_parameter_set_id
= bs_read_ue( p_bs
);
797 if( p_sps
->sps_seq_parameter_set_id
> HEVC_SPS_ID_MAX
)
800 p_sps
->chroma_format_idc
= bs_read_ue( p_bs
);
801 if( p_sps
->chroma_format_idc
== 3 )
802 p_sps
->separate_colour_plane_flag
= bs_read1( p_bs
);
803 p_sps
->pic_width_in_luma_samples
= bs_read_ue( p_bs
);
804 p_sps
->pic_height_in_luma_samples
= bs_read_ue( p_bs
);
805 if( !p_sps
->pic_width_in_luma_samples
|| !p_sps
->pic_height_in_luma_samples
)
808 p_sps
->conformance_window_flag
= bs_read1( p_bs
);
809 if( p_sps
->conformance_window_flag
)
811 p_sps
->conf_win
.left_offset
= bs_read_ue( p_bs
);
812 p_sps
->conf_win
.right_offset
= bs_read_ue( p_bs
);
813 p_sps
->conf_win
.top_offset
= bs_read_ue( p_bs
);
814 p_sps
->conf_win
.bottom_offset
= bs_read_ue( p_bs
);
817 p_sps
->bit_depth_luma_minus8
= bs_read_ue( p_bs
);
818 p_sps
->bit_depth_chroma_minus8
= bs_read_ue( p_bs
);
819 p_sps
->log2_max_pic_order_cnt_lsb_minus4
= bs_read_ue( p_bs
);
821 p_sps
->sps_sub_layer_ordering_info_present_flag
= bs_read1( p_bs
);
822 for( uint8_t i
=(p_sps
->sps_sub_layer_ordering_info_present_flag
? 0 : p_sps
->sps_max_sub_layers_minus1
);
823 i
<= p_sps
->sps_max_sub_layers_minus1
; i
++ )
825 p_sps
->sps_max
[i
].dec_pic_buffering_minus1
= bs_read_ue( p_bs
);
826 p_sps
->sps_max
[i
].num_reorder_pics
= bs_read_ue( p_bs
);
827 p_sps
->sps_max
[i
].latency_increase_plus1
= bs_read_ue( p_bs
);
830 if( bs_remain( p_bs
) < 4 )
833 p_sps
->log2_min_luma_coding_block_size_minus3
= bs_read_ue( p_bs
);
834 p_sps
->log2_diff_max_min_luma_coding_block_size
= bs_read_ue( p_bs
);
835 p_sps
->log2_min_luma_transform_block_size_minus2
= bs_read_ue( p_bs
);
836 if( bs_remain( p_bs
) < 1 ) /* last late fail check */
838 p_sps
->log2_diff_max_min_luma_transform_block_size
= bs_read_ue( p_bs
);
840 /* parsing incomplete */
842 p_sps
->max_transform_hierarchy_depth_inter
= bs_read_ue( p_bs
);
843 p_sps
->max_transform_hierarchy_depth_intra
= bs_read_ue( p_bs
);
844 p_sps
->scaling_list_enabled
= bs_read1( p_bs
);
845 if( p_sps
->scaling_list_enabled
)
847 p_sps
->sps_scaling_list_data_present_flag
= bs_read1( p_bs
);
848 if( p_sps
->sps_scaling_list_data_present_flag
&&
849 ! hevc_parse_scaling_list_rbsp( p_bs
) )
855 p_sps
->amp_enabled_flag
= bs_read1( p_bs
);
856 p_sps
->sample_adaptive_offset_enabled_flag
= bs_read1( p_bs
);
858 p_sps
->pcm_enabled_flag
= bs_read1( p_bs
);
859 if( p_sps
->pcm_enabled_flag
)
861 p_sps
->pcm_sample_bit_depth_luma_minus1
= bs_read( p_bs
, 4 );
862 p_sps
->pcm_sample_bit_depth_chroma_minus1
= bs_read( p_bs
, 4 );
863 p_sps
->log2_min_pcm_luma_coding_block_size_minus3
= bs_read_ue( p_bs
);
864 p_sps
->log2_diff_max_min_pcm_luma_coding_block_size
= bs_read_ue( p_bs
);
865 p_sps
->pcm_loop_filter_disabled_flag
= bs_read1( p_bs
);
868 p_sps
->num_short_term_ref_pic_sets
= bs_read_ue( p_bs
);
869 if( p_sps
->num_short_term_ref_pic_sets
> HEVC_MAX_SHORT_TERM_REF_PIC_SET
)
872 hevc_short_term_ref_pic_set_t sets
[HEVC_MAX_SHORT_TERM_REF_PIC_SET
];
873 memset(&sets
, 0, sizeof(hevc_short_term_ref_pic_set_t
) * HEVC_MAX_SHORT_TERM_REF_PIC_SET
);
874 for( unsigned int i
=0; i
<p_sps
->num_short_term_ref_pic_sets
; i
++ )
876 if( !hevc_parse_st_ref_pic_set( p_bs
, i
, p_sps
->num_short_term_ref_pic_sets
, sets
) )
880 p_sps
->long_term_ref_pics_present_flag
= bs_read1( p_bs
);
881 if( p_sps
->long_term_ref_pics_present_flag
)
883 p_sps
->num_long_term_ref_pics_sps
= bs_read_ue( p_bs
);
884 if( p_sps
->num_long_term_ref_pics_sps
> HEVC_MAX_LONG_TERM_REF_PIC_SET
)
886 for( unsigned int i
=0; i
< p_sps
->num_long_term_ref_pics_sps
; i
++ )
888 /* lt_ref_pic_poc_lsb_sps */
889 bs_skip( p_bs
, p_sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4 );
890 /* used_by_curr_pic_lt_sps_flag */
895 p_sps
->sps_temporal_mvp_enabled_flag
= bs_read1( p_bs
);
896 p_sps
->strong_intra_smoothing_enabled_flag
= bs_read1( p_bs
);
898 if( bs_remain( p_bs
) < 1 ) /* late fail */
901 p_sps
->vui_parameters_present_flag
= bs_read1( p_bs
);
902 if( p_sps
->vui_parameters_present_flag
)
904 bs_t rollbackpoint
= *p_bs
;
905 if( !hevc_parse_vui_parameters_rbsp( p_bs
, &p_sps
->vui
, false ) &&
906 p_sps
->vui
.default_display_window_flag
&&
907 bs_remain( p_bs
) < 66 )
909 /* Broken MKV SPS vui bitstreams with missing display_window bits.
910 * Forced to accept it since some decided to accept it...
911 * see https://trac.ffmpeg.org/ticket/6644
912 * Might break decoders since cropping & clock rate have totally
913 * funky values when decoded properly */
914 *p_bs
= rollbackpoint
;
915 memset( &p_sps
->vui
, 0, sizeof(p_sps
->vui
) );
916 if( !hevc_parse_vui_parameters_rbsp( p_bs
, &p_sps
->vui
, true ) )
926 void hevc_rbsp_release_sps( hevc_sequence_parameter_set_t
*p_sps
)
931 IMPL_hevc_generic_decode( hevc_decode_sps
, hevc_sequence_parameter_set_t
,
932 hevc_parse_sequence_parameter_set_rbsp
, hevc_rbsp_release_sps
)
934 static bool hevc_parse_pic_parameter_set_rbsp( bs_t
*p_bs
,
935 hevc_picture_parameter_set_t
*p_pps
)
937 if( bs_remain( p_bs
) < 1 )
939 p_pps
->pps_pic_parameter_set_id
= bs_read_ue( p_bs
);
940 if( p_pps
->pps_pic_parameter_set_id
> HEVC_PPS_ID_MAX
|| bs_remain( p_bs
) < 1 )
942 p_pps
->pps_seq_parameter_set_id
= bs_read_ue( p_bs
);
943 if( p_pps
->pps_seq_parameter_set_id
> HEVC_SPS_ID_MAX
)
945 p_pps
->dependent_slice_segments_enabled_flag
= bs_read1( p_bs
);
946 p_pps
->output_flag_present_flag
= bs_read1( p_bs
);
947 p_pps
->num_extra_slice_header_bits
= bs_read( p_bs
, 3 );
948 p_pps
->sign_data_hiding_enabled_flag
= bs_read1( p_bs
);
949 p_pps
->cabac_init_present_flag
= bs_read1( p_bs
);
951 p_pps
->num_ref_idx_l0_default_active_minus1
= bs_read_ue( p_bs
);
952 p_pps
->num_ref_idx_l1_default_active_minus1
= bs_read_ue( p_bs
);
954 p_pps
->init_qp_minus26
= bs_read_se( p_bs
);
955 p_pps
->constrained_intra_pred_flag
= bs_read1( p_bs
);
956 p_pps
->transform_skip_enabled_flag
= bs_read1( p_bs
);
957 p_pps
->cu_qp_delta_enabled_flag
= bs_read1( p_bs
);
958 if( p_pps
->cu_qp_delta_enabled_flag
)
959 p_pps
->diff_cu_qp_delta_depth
= bs_read_ue( p_bs
);
961 if( bs_remain( p_bs
) < 1 )
964 p_pps
->pps_cb_qp_offset
= bs_read_se( p_bs
);
965 p_pps
->pps_cr_qp_offset
= bs_read_se( p_bs
);
966 p_pps
->pic_slice_level_chroma_qp_offsets_present_flag
= bs_read1( p_bs
);
967 p_pps
->weighted_pred_flag
= bs_read1( p_bs
);
968 p_pps
->weighted_bipred_flag
= bs_read1( p_bs
);
969 p_pps
->transquant_bypass_enable_flag
= bs_read1( p_bs
);
970 p_pps
->tiles_enabled_flag
= bs_read1( p_bs
);
971 p_pps
->entropy_coding_sync_enabled_flag
= bs_read1( p_bs
);
973 if( p_pps
->tiles_enabled_flag
)
975 p_pps
->num_tile_columns_minus1
= bs_read_ue( p_bs
); /* TODO: validate max col/row values */
976 p_pps
->num_tile_rows_minus1
= bs_read_ue( p_bs
); /* against sps PicWidthInCtbsY */
977 p_pps
->uniform_spacing_flag
= bs_read1( p_bs
);
978 if( !p_pps
->uniform_spacing_flag
)
980 if( bs_remain( p_bs
) < p_pps
->num_tile_columns_minus1
+
981 p_pps
->num_tile_rows_minus1
+ 1 )
983 for( unsigned i
=0; i
< p_pps
->num_tile_columns_minus1
; i
++ )
984 (void) bs_read_ue( p_bs
);
985 for( unsigned i
=0; i
< p_pps
->num_tile_rows_minus1
; i
++ )
986 (void) bs_read_ue( p_bs
);
988 p_pps
->loop_filter_across_tiles_enabled_flag
= bs_read1( p_bs
);
991 p_pps
->pps_loop_filter_across_slices_enabled_flag
= bs_read1( p_bs
);
992 p_pps
->deblocking_filter_control_present_flag
= bs_read1( p_bs
);
993 if( p_pps
->deblocking_filter_control_present_flag
)
995 p_pps
->deblocking_filter_override_enabled_flag
= bs_read1( p_bs
);
996 p_pps
->pps_deblocking_filter_disabled_flag
= bs_read1( p_bs
);
997 if( !p_pps
->pps_deblocking_filter_disabled_flag
)
999 p_pps
->pps_beta_offset_div2
= bs_read_se( p_bs
);
1000 p_pps
->pps_tc_offset_div2
= bs_read_se( p_bs
);
1004 p_pps
->scaling_list_data_present_flag
= bs_read1( p_bs
);
1005 if( p_pps
->scaling_list_data_present_flag
&& !hevc_parse_scaling_list_rbsp( p_bs
) )
1008 p_pps
->lists_modification_present_flag
= bs_read1( p_bs
);
1009 p_pps
->log2_parallel_merge_level_minus2
= bs_read_ue( p_bs
);
1010 p_pps
->slice_header_extension_present_flag
= bs_read1( p_bs
);
1012 if( bs_remain( p_bs
) < 1 )
1015 p_pps
->pps_extension_present_flag
= bs_read1( p_bs
);
1016 if( p_pps
->pps_extension_present_flag
)
1018 p_pps
->pps_range_extension_flag
= bs_read1( p_bs
);
1019 p_pps
->pps_multilayer_extension_flag
= bs_read1( p_bs
);
1020 p_pps
->pps_3d_extension_flag
= bs_read1( p_bs
);
1021 if( bs_remain( p_bs
) < 5 )
1023 p_pps
->pps_extension_5bits
= bs_read( p_bs
, 5 );
1029 void hevc_rbsp_release_pps( hevc_picture_parameter_set_t
*p_pps
)
1034 IMPL_hevc_generic_decode( hevc_decode_pps
, hevc_picture_parameter_set_t
,
1035 hevc_parse_pic_parameter_set_rbsp
, hevc_rbsp_release_pps
)
1037 uint8_t hevc_get_sps_vps_id( const hevc_sequence_parameter_set_t
*p_sps
)
1039 return p_sps
->sps_video_parameter_set_id
;
1042 uint8_t hevc_get_pps_sps_id( const hevc_picture_parameter_set_t
*p_pps
)
1044 return p_pps
->pps_seq_parameter_set_id
;
1047 uint8_t hevc_get_slice_pps_id( const hevc_slice_segment_header_t
*p_slice
)
1049 return p_slice
->slice_pic_parameter_set_id
;
1052 bool hevc_get_sps_profile_tier_level( const hevc_sequence_parameter_set_t
*p_sps
,
1053 uint8_t *pi_profile
, uint8_t *pi_level
)
1055 if(p_sps
->profile_tier_level
.general
.profile_idc
)
1057 *pi_profile
= p_sps
->profile_tier_level
.general
.profile_idc
;
1058 *pi_level
= p_sps
->profile_tier_level
.general_level_idc
;
1064 bool hevc_get_picture_size( const hevc_sequence_parameter_set_t
*p_sps
,
1065 unsigned *p_w
, unsigned *p_h
, unsigned *p_vw
, unsigned *p_vh
)
1067 *p_w
= *p_vw
= p_sps
->pic_width_in_luma_samples
;
1068 *p_h
= *p_vh
= p_sps
->pic_height_in_luma_samples
;
1069 if( p_sps
->conformance_window_flag
)
1071 unsigned sub_width_c
, sub_height_c
;
1073 if( p_sps
->chroma_format_idc
== 1 )
1078 else if( p_sps
->chroma_format_idc
== 2 )
1089 *p_vh
-= (p_sps
->conf_win
.bottom_offset
+ p_sps
->conf_win
.top_offset
) * sub_height_c
;
1090 *p_vw
-= (p_sps
->conf_win
.left_offset
+ p_sps
->conf_win
.right_offset
) * sub_width_c
;
1095 uint8_t hevc_get_max_num_reorder( const hevc_video_parameter_set_t
*p_vps
)
1097 return p_vps
->vps_max
[p_vps
->vps_max_sub_layers_minus1
/* HighestTid */].num_reorder_pics
;
1100 static inline uint8_t vlc_ceil_log2( uint32_t val
)
1102 uint8_t n
= 31 - clz(val
);
1103 if (((unsigned)1 << n
) != val
)
1108 static bool hevc_get_picture_CtbsYsize( const hevc_sequence_parameter_set_t
*p_sps
, unsigned *p_w
, unsigned *p_h
)
1110 const unsigned int MinCbLog2SizeY
= p_sps
->log2_min_luma_coding_block_size_minus3
+ 3;
1111 const unsigned int CtbLog2SizeY
= MinCbLog2SizeY
+ p_sps
->log2_diff_max_min_luma_coding_block_size
;
1112 if( CtbLog2SizeY
> 31 )
1114 const unsigned int CtbSizeY
= 1 << CtbLog2SizeY
;
1115 *p_w
= (p_sps
->pic_width_in_luma_samples
- 1) / CtbSizeY
+ 1;
1116 *p_h
= (p_sps
->pic_height_in_luma_samples
- 1) / CtbSizeY
+ 1;
1120 bool hevc_get_frame_rate( const hevc_sequence_parameter_set_t
*p_sps
,
1121 const hevc_video_parameter_set_t
*p_vps
,
1122 unsigned *pi_num
, unsigned *pi_den
)
1124 if( p_sps
->vui_parameters_present_flag
&& p_sps
->vui
.vui_timing_info_present_flag
)
1126 *pi_den
= p_sps
->vui
.timing
.vui_num_units_in_tick
;
1127 *pi_num
= p_sps
->vui
.timing
.vui_time_scale
;
1128 return (*pi_den
&& *pi_num
);
1130 else if( p_vps
&& p_vps
->vps_timing_info_present_flag
)
1132 *pi_den
= p_vps
->vps_num_units_in_tick
;
1133 *pi_num
= p_vps
->vps_time_scale
;
1134 return (*pi_den
&& *pi_num
);
1139 bool hevc_get_aspect_ratio( const hevc_sequence_parameter_set_t
*p_sps
,
1140 unsigned *num
, unsigned *den
)
1142 if( p_sps
->vui_parameters_present_flag
)
1144 if( p_sps
->vui
.ar
.aspect_ratio_idc
!= 255 )
1146 static const uint8_t ar_table
[16][2] =
1165 if( p_sps
->vui
.ar
.aspect_ratio_idc
> 0 &&
1166 p_sps
->vui
.ar
.aspect_ratio_idc
< 17 )
1168 *num
= ar_table
[p_sps
->vui
.ar
.aspect_ratio_idc
- 1][0];
1169 *den
= ar_table
[p_sps
->vui
.ar
.aspect_ratio_idc
- 1][1];
1175 *num
= p_sps
->vui
.ar
.sar_width
;
1176 *den
= p_sps
->vui
.ar
.sar_height
;
1183 bool hevc_get_chroma_luma( const hevc_sequence_parameter_set_t
*p_sps
, uint8_t *pi_chroma_format
,
1184 uint8_t *pi_depth_luma
, uint8_t *pi_depth_chroma
)
1186 *pi_chroma_format
= p_sps
->chroma_format_idc
;
1187 *pi_depth_luma
= p_sps
->bit_depth_chroma_minus8
+ 8;
1188 *pi_depth_chroma
= p_sps
->bit_depth_chroma_minus8
+ 8;
1192 bool hevc_get_colorimetry( const hevc_sequence_parameter_set_t
*p_sps
,
1193 video_color_primaries_t
*p_primaries
,
1194 video_transfer_func_t
*p_transfer
,
1195 video_color_space_t
*p_colorspace
,
1196 video_color_range_t
*p_full_range
)
1198 if( !p_sps
->vui_parameters_present_flag
)
1201 iso_23001_8_cp_to_vlc_primaries( p_sps
->vui
.vs
.colour
.colour_primaries
);
1203 iso_23001_8_tc_to_vlc_xfer( p_sps
->vui
.vs
.colour
.transfer_characteristics
);
1205 iso_23001_8_mc_to_vlc_coeffs( p_sps
->vui
.vs
.colour
.matrix_coeffs
);
1206 *p_full_range
= p_sps
->vui
.vs
.video_full_range_flag
? COLOR_RANGE_FULL
: COLOR_RANGE_LIMITED
;
1210 static bool hevc_parse_slice_segment_header_rbsp( bs_t
*p_bs
,
1211 pf_get_matchedxps get_matchedxps
,
1213 hevc_slice_segment_header_t
*p_sl
)
1215 hevc_sequence_parameter_set_t
*p_sps
;
1216 hevc_picture_parameter_set_t
*p_pps
;
1217 hevc_video_parameter_set_t
*p_vps
;
1219 if( bs_remain( p_bs
) < 3 )
1222 p_sl
->first_slice_segment_in_pic_flag
= bs_read1( p_bs
);
1223 if( p_sl
->nal_type
>= HEVC_NAL_BLA_W_LP
&& p_sl
->nal_type
<= HEVC_NAL_IRAP_VCL23
)
1224 p_sl
->no_output_of_prior_pics_flag
= bs_read1( p_bs
);
1225 p_sl
->slice_pic_parameter_set_id
= bs_read_ue( p_bs
);
1226 if( p_sl
->slice_pic_parameter_set_id
> HEVC_PPS_ID_MAX
|| bs_remain( p_bs
) < 1 )
1229 get_matchedxps( p_sl
->slice_pic_parameter_set_id
, priv
, &p_pps
, &p_sps
, &p_vps
);
1230 if(!p_sps
|| !p_pps
)
1233 if( !p_sl
->first_slice_segment_in_pic_flag
)
1235 if( p_pps
->dependent_slice_segments_enabled_flag
)
1236 p_sl
->dependent_slice_segment_flag
= bs_read1( p_bs
);
1239 if( !hevc_get_picture_CtbsYsize( p_sps
, &w
, &h
) )
1242 (void) bs_read( p_bs
, vlc_ceil_log2( w
* h
) ); /* slice_segment_address */
1245 if( !p_sl
->dependent_slice_segment_flag
)
1248 if( p_pps
->num_extra_slice_header_bits
> i
)
1251 bs_skip( p_bs
, 1 ); /* discardable_flag */
1254 if( p_pps
->num_extra_slice_header_bits
> i
)
1257 bs_skip( p_bs
, 1 ); /* cross_layer_bla_flag */
1260 if( i
< p_pps
->num_extra_slice_header_bits
)
1261 bs_skip( p_bs
, p_pps
->num_extra_slice_header_bits
- i
);
1263 p_sl
->slice_type
= bs_read_ue( p_bs
);
1264 if( p_sl
->slice_type
> HEVC_SLICE_TYPE_I
)
1267 if( p_pps
->output_flag_present_flag
)
1268 p_sl
->pic_output_flag
= bs_read1( p_bs
);
1270 p_sl
->pic_output_flag
= 1;
1273 if( p_sps
->separate_colour_plane_flag
)
1274 bs_skip( p_bs
, 2 ); /* colour_plane_id */
1276 if( p_sl
->nal_type
!= HEVC_NAL_IDR_W_RADL
&& p_sl
->nal_type
!= HEVC_NAL_IDR_N_LP
)
1277 p_sl
->pic_order_cnt_lsb
= bs_read( p_bs
, p_sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4 );
1279 p_sl
->pic_order_cnt_lsb
= 0;
1281 if( bs_remain( p_bs
) < 1 )
1287 void hevc_rbsp_release_slice_header( hevc_slice_segment_header_t
*p_sh
)
1292 hevc_slice_segment_header_t
* hevc_decode_slice_header( const uint8_t *p_buf
, size_t i_buf
, bool b_escaped
,
1293 pf_get_matchedxps get_matchedxps
, void *priv
)
1295 hevc_slice_segment_header_t
*p_sh
= calloc(1, sizeof(hevc_slice_segment_header_t
));
1299 struct hxxx_bsfw_ep3b_ctx_s bsctx
;
1302 hxxx_bsfw_ep3b_ctx_init( &bsctx
);
1303 bs_init_custom( &bs
, p_buf
, i_buf
, &hxxx_bsfw_ep3b_callbacks
, &bsctx
);
1305 else bs_init( &bs
, p_buf
, i_buf
);
1307 p_sh
->nal_type
= bs_read( &bs
, 6 );
1308 p_sh
->nuh_layer_id
= bs_read( &bs
, 6 );
1309 p_sh
->temporal_id_plus1
= bs_read( &bs
, 3 );
1310 if( p_sh
->nuh_layer_id
> 62 || p_sh
->temporal_id_plus1
== 0 ||
1311 !hevc_parse_slice_segment_header_rbsp( &bs
, get_matchedxps
, priv
, p_sh
) )
1313 hevc_rbsp_release_slice_header( p_sh
);
1320 bool hevc_get_slice_type( const hevc_slice_segment_header_t
*p_sli
, enum hevc_slice_type_e
*pi_type
)
1322 if( !p_sli
->dependent_slice_segment_flag
)
1324 *pi_type
= p_sli
->slice_type
;
1330 bool hevc_get_profile_level(const es_format_t
*p_fmt
, uint8_t *pi_profile
,
1331 uint8_t *pi_level
, uint8_t *pi_nal_length_size
)
1333 const uint8_t *p
= (const uint8_t*)p_fmt
->p_extra
;
1334 if(p_fmt
->i_extra
< 23 || p
[0] != 1)
1337 /* HEVCDecoderConfigurationRecord */
1339 *pi_profile
= p
[1] & 0x1F;
1344 if (pi_nal_length_size
)
1345 *pi_nal_length_size
= 1 + (p
[21]&0x03);
1351 * HEVCDecoderConfigurationRecord operations
1354 static void hevc_dcr_params_from_vps( const uint8_t * p_buffer
, size_t i_buffer
,
1355 struct hevc_dcr_values
*p_values
)
1361 struct hxxx_bsfw_ep3b_ctx_s bsctx
;
1362 hxxx_bsfw_ep3b_ctx_init( &bsctx
);
1363 bs_init_custom( &bs
, p_buffer
, i_buffer
, &hxxx_bsfw_ep3b_callbacks
, &bsctx
);
1365 /* first two bytes are the NAL header, 3rd and 4th are:
1366 vps_video_parameter_set_id(4)
1367 vps_reserved_3_2bis(2)
1368 vps_max_layers_minus1(6)
1369 vps_max_sub_layers_minus1(3)
1370 vps_temporal_id_nesting_flags
1372 bs_skip( &bs
, 16 + 4 + 2 + 6 );
1373 p_values
->i_numTemporalLayer
= bs_read( &bs
, 3 ) + 1;
1374 p_values
->b_temporalIdNested
= bs_read1( &bs
);
1376 /* 5th & 6th are reserved 0xffff */
1378 /* copy the first 12 bytes of profile tier */
1379 for( unsigned i
=0; i
<12; i
++ )
1380 p_values
->general_configuration
[i
] = bs_read( &bs
, 8 );
1383 #define HEVC_DCR_ADD_NALS(type, count, buffers, sizes) \
1384 for (uint8_t i = 0; i < count; i++) \
1388 *p++ = (type | (b_completeness ? 0x80 : 0)); \
1389 SetWBE( p, count ); p += 2; \
1391 SetWBE( p, sizes[i]); p += 2; \
1392 memcpy( p, buffers[i], sizes[i] ); p += sizes[i];\
1395 #define HEVC_DCR_ADD_SIZES(count, sizes) \
1399 for(uint8_t i=0; i<count; i++)\
1400 i_total_size += 2 + sizes[i];\
1403 /* Generate HEVCDecoderConfiguration iso/iec 14496-15 3rd edition */
1404 uint8_t * hevc_create_dcr( const struct hevc_dcr_params
*p_params
,
1405 uint8_t i_nal_length_size
,
1406 bool b_completeness
, size_t *pi_size
)
1410 if( i_nal_length_size
!= 1 && i_nal_length_size
!= 2 && i_nal_length_size
!= 4 )
1413 struct hevc_dcr_values values
=
1415 .general_configuration
= {0},
1416 .i_numTemporalLayer
= 0,
1418 .i_bit_depth_luma_minus8
= 0,
1419 .i_bit_depth_chroma_minus8
= 0,
1420 .b_temporalIdNested
= false,
1423 if( p_params
->p_values
!= NULL
)
1425 values
= *p_params
->p_values
;
1429 if( p_params
->i_vps_count
== 0 || p_params
->i_sps_count
== 0 )
1430 return NULL
; /* required to extract info */
1432 hevc_dcr_params_from_vps( p_params
->p_vps
[0], p_params
->rgi_vps
[0], &values
);
1434 hevc_sequence_parameter_set_t
*p_sps
=
1435 hevc_decode_sps( p_params
->p_sps
[0], p_params
->rgi_sps
[0], true );
1438 values
.i_chroma_idc
= p_sps
->chroma_format_idc
;
1439 values
.i_bit_depth_chroma_minus8
= p_sps
->bit_depth_chroma_minus8
;
1440 values
.i_bit_depth_luma_minus8
= p_sps
->bit_depth_luma_minus8
;
1441 hevc_rbsp_release_sps( p_sps
);
1445 size_t i_total_size
= 1+12+2+4+2+2;
1446 HEVC_DCR_ADD_SIZES(p_params
->i_vps_count
, p_params
->rgi_vps
);
1447 HEVC_DCR_ADD_SIZES(p_params
->i_sps_count
, p_params
->rgi_sps
);
1448 HEVC_DCR_ADD_SIZES(p_params
->i_pps_count
, p_params
->rgi_pps
);
1449 HEVC_DCR_ADD_SIZES(p_params
->i_seipref_count
, p_params
->rgi_seipref
);
1450 HEVC_DCR_ADD_SIZES(p_params
->i_seisuff_count
, p_params
->rgi_seisuff
);
1452 uint8_t *p_data
= malloc( i_total_size
);
1453 if( p_data
== NULL
)
1456 *pi_size
= i_total_size
;
1457 uint8_t *p
= p_data
;
1461 memcpy( p
, values
.general_configuration
, 12 ); p
+= 12;
1462 /* Don't set min spatial segmentation */
1463 SetWBE( p
, 0xF000 ); p
+= 2;
1464 /* Don't set parallelism type since segmentation isn't set */
1466 *p
++ = (0xFC | (values
.i_chroma_idc
& 0x03));
1467 *p
++ = (0xF8 | (values
.i_bit_depth_luma_minus8
& 0x07));
1468 *p
++ = (0xF8 | (values
.i_bit_depth_chroma_minus8
& 0x07));
1470 /* Don't set framerate */
1471 SetWBE( p
, 0x0000); p
+= 2;
1472 /* Force NAL size of 4 bytes that replace the startcode */
1473 *p
++ = ( ((values
.i_numTemporalLayer
& 0x07) << 3) |
1474 (values
.b_temporalIdNested
<< 2) |
1475 (i_nal_length_size
- 1) );
1476 /* total number of arrays */
1477 *p
++ = !!p_params
->i_vps_count
+ !!p_params
->i_sps_count
+
1478 !!p_params
->i_pps_count
+ !!p_params
->i_seipref_count
+
1479 !!p_params
->i_seisuff_count
;
1481 /* Write NAL arrays */
1482 HEVC_DCR_ADD_NALS(HEVC_NAL_VPS
, p_params
->i_vps_count
,
1483 p_params
->p_vps
, p_params
->rgi_vps
);
1484 HEVC_DCR_ADD_NALS(HEVC_NAL_SPS
, p_params
->i_sps_count
,
1485 p_params
->p_sps
, p_params
->rgi_sps
);
1486 HEVC_DCR_ADD_NALS(HEVC_NAL_PPS
, p_params
->i_pps_count
,
1487 p_params
->p_pps
, p_params
->rgi_pps
);
1488 HEVC_DCR_ADD_NALS(HEVC_NAL_PREF_SEI
, p_params
->i_seipref_count
,
1489 p_params
->p_seipref
, p_params
->rgi_seipref
);
1490 HEVC_DCR_ADD_NALS(HEVC_NAL_SUFF_SEI
, p_params
->i_seisuff_count
,
1491 p_params
->p_seisuff
, p_params
->rgi_seisuff
);
1496 #undef HEVC_DCR_ADD_NALS
1497 #undef HEVC_DCR_ADD_SIZES
1501 * 8.3.1 Decoding process for POC
1503 int hevc_compute_picture_order_count( const hevc_sequence_parameter_set_t
*p_sps
,
1504 const hevc_slice_segment_header_t
*p_slice
,
1505 hevc_poc_ctx_t
*p_ctx
)
1513 bool NoRaslOutputFlag
;
1514 bool IsIRAP
= ( p_slice
->nal_type
>= HEVC_NAL_BLA_W_LP
&&
1515 p_slice
->nal_type
<= HEVC_NAL_IRAP_VCL23
);
1519 /* if( IRAP ) NoRaslOutputFlag = first || IDR || BLA || after(EOSNAL) */
1520 NoRaslOutputFlag
=(p_ctx
->first_picture
||
1521 p_slice
->nal_type
== HEVC_NAL_IDR_N_LP
||
1522 p_slice
->nal_type
== HEVC_NAL_IDR_W_RADL
||
1523 p_slice
->nal_type
== HEVC_NAL_BLA_W_LP
||
1524 p_slice
->nal_type
== HEVC_NAL_BLA_W_RADL
||
1525 p_slice
->nal_type
== HEVC_NAL_BLA_N_LP
||
1526 p_ctx
->HandleCraAsBlaFlag
);
1530 NoRaslOutputFlag
= false;
1533 #ifdef HEVC_POC_DEBUG
1534 printf("slice lsb=%"PRIu32
" irap=%d norasl=%d tid=%d msb=%d lsb=%d",
1535 p_slice
->pic_order_cnt_lsb
,
1536 IsIRAP
, NoRaslOutputFlag
, p_slice
->temporal_id_plus1
,
1537 p_ctx
->prevTid0PicOrderCnt
.msb
, p_ctx
->prevTid0PicOrderCnt
.lsb
);
1540 if( p_slice
->nal_type
== HEVC_NAL_IDR_N_LP
||
1541 p_slice
->nal_type
== HEVC_NAL_IDR_W_RADL
)
1543 prevPicOrderCnt
.msb
= 0;
1544 prevPicOrderCnt
.lsb
= 0;
1546 /* Not an IRAP with NoRaslOutputFlag == 1 */
1547 else if( !IsIRAP
|| !NoRaslOutputFlag
)
1549 prevPicOrderCnt
.msb
= p_ctx
->prevTid0PicOrderCnt
.msb
;
1550 prevPicOrderCnt
.lsb
= p_ctx
->prevTid0PicOrderCnt
.lsb
;
1553 if( IsIRAP
&& NoRaslOutputFlag
)
1559 const unsigned maxPocLSB
= 1U << (p_sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4);
1560 pocMSB
= prevPicOrderCnt
.msb
;
1561 int64_t orderDiff
= (int64_t)p_slice
->pic_order_cnt_lsb
- prevPicOrderCnt
.lsb
;
1562 if( orderDiff
< 0 && -orderDiff
>= maxPocLSB
/ 2 )
1563 pocMSB
+= maxPocLSB
;
1564 else if( orderDiff
> maxPocLSB
/ 2 )
1565 pocMSB
-= maxPocLSB
;
1568 /* Set prevTid0Pic for next pic */
1569 if( p_slice
->temporal_id_plus1
== 1 &&
1570 !( ( p_slice
->nal_type
<= HEVC_NAL_RSV_VCL_N14
&& p_slice
->nal_type
% 2 == 0 /* SLNR */ ) ||
1571 ( p_slice
->nal_type
>= HEVC_NAL_RADL_N
&& p_slice
->nal_type
<= HEVC_NAL_RASL_R
) /* RADL or RASL */ ) )
1573 p_ctx
->prevTid0PicOrderCnt
.msb
= pocMSB
;
1574 p_ctx
->prevTid0PicOrderCnt
.lsb
= p_slice
->pic_order_cnt_lsb
;
1577 p_ctx
->first_picture
= false;
1579 #ifdef HEVC_POC_DEBUG
1580 printf(" POC=%"PRIu32
"\n", pocMSB
+ p_slice
->pic_order_cnt_lsb
);
1583 return pocMSB
+ p_slice
->pic_order_cnt_lsb
;
1586 struct hevc_sei_pic_timing_t
1588 nal_u4_t pic_struct
;
1589 nal_u2_t source_scan_type
;
1592 void hevc_release_sei_pic_timing( hevc_sei_pic_timing_t
*p_timing
)
1597 hevc_sei_pic_timing_t
* hevc_decode_sei_pic_timing( bs_t
*p_bs
,
1598 const hevc_sequence_parameter_set_t
*p_sps
)
1600 hevc_sei_pic_timing_t
*p_timing
= malloc(sizeof(*p_timing
));
1603 if( p_sps
->vui
.frame_field_info_present_flag
)
1605 p_timing
->pic_struct
= bs_read( p_bs
, 4 );
1606 p_timing
->source_scan_type
= bs_read( p_bs
, 2 );
1610 p_timing
->pic_struct
= 0;
1611 p_timing
->source_scan_type
= 1;
1617 bool hevc_frame_is_progressive( const hevc_sequence_parameter_set_t
*p_sps
,
1618 const hevc_sei_pic_timing_t
*p_timing
)
1620 if( p_sps
->vui_parameters_present_flag
&&
1621 p_sps
->vui
.field_seq_flag
)
1624 const hevc_inner_profile_tier_level_t
*p_profile
= &p_sps
->profile_tier_level
.general
;
1625 /* 1 & 0, 0 & 1, global */
1626 if( p_profile
->progressive_source_flag
!= p_profile
->interlaced_source_flag
)
1628 return p_profile
->progressive_source_flag
> p_profile
->interlaced_source_flag
;
1630 /* 1 & 1, defined in SEI */
1631 else if( p_profile
->progressive_source_flag
)
1633 if( p_timing
&& p_sps
->vui
.frame_field_info_present_flag
)
1635 if( p_timing
->source_scan_type
< 2 )
1636 return p_timing
->source_scan_type
!= 0;
1639 /* else 0 & 0, unspec */
1644 uint8_t hevc_get_num_clock_ts( const hevc_sequence_parameter_set_t
*p_sps
,
1645 const hevc_sei_pic_timing_t
*p_timing
)
1647 if( p_sps
->vui
.frame_field_info_present_flag
&& p_timing
&& p_timing
->pic_struct
< 13 )
1649 /* !WARN modified with units_field_based_flag (D.3.25) for values 0, 7 and 8 */
1650 const uint8_t rgi_numclock
[13] = { 2, 1, 1, 2, 2, 3, 3, 4, 6, 1, 1, 1, 1 };
1651 return rgi_numclock
[p_timing
->pic_struct
];
1654 if( p_sps
->vui_parameters_present_flag
)
1656 if( p_sps
->vui
.field_seq_flag
)
1657 return 1; /* D.3.27 */
1659 else if( p_sps
->profile_tier_level
.general
.interlaced_source_flag
&&
1660 !p_sps
->profile_tier_level
.general
.progressive_source_flag
)