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 typedef uint8_t nal_u1_t
;
33 typedef uint8_t nal_u2_t
;
34 typedef uint8_t nal_u3_t
;
35 typedef uint8_t nal_u4_t
;
36 typedef uint8_t nal_u5_t
;
37 typedef uint8_t nal_u6_t
;
38 typedef uint8_t nal_u7_t
;
39 typedef uint8_t nal_u8_t
;
40 typedef int32_t nal_se_t
;
41 typedef uint32_t nal_ue_t
;
45 nal_u2_t profile_space
;
48 uint32_t profile_compatibility_flag
; /* nal_u1_t * 32 */
49 nal_u1_t progressive_source_flag
;
50 nal_u1_t interlaced_source_flag
;
51 nal_u1_t non_packed_constraint_flag
;
52 nal_u1_t frame_only_constraint_flag
;
55 nal_u1_t max_12bit_constraint_flag
;
56 nal_u1_t max_10bit_constraint_flag
;
57 nal_u1_t max_8bit_constraint_flag
;
58 nal_u1_t max_422chroma_constraint_flag
;
59 nal_u1_t max_420chroma_constraint_flag
;
60 nal_u1_t max_monochrome_constraint_flag
;
61 nal_u1_t intra_constraint_flag
;
62 nal_u1_t one_picture_only_constraint_flag
;
63 nal_u1_t lower_bit_rate_constraint_flag
;
69 } hevc_inner_profile_tier_level_t
;
71 #define HEVC_MAX_SUBLAYERS 8
74 hevc_inner_profile_tier_level_t general
;
75 nal_u8_t general_level_idc
;
76 uint8_t sublayer_profile_present_flag
; /* nal_u1_t * 8 */
77 uint8_t sublayer_level_present_flag
; /* nal_u1_t * 8 */
78 hevc_inner_profile_tier_level_t sub_layer
[HEVC_MAX_SUBLAYERS
];
79 nal_u8_t sub_layer_level_idc
[HEVC_MAX_SUBLAYERS
];
80 } hevc_profile_tier_level_t
;
82 #define HEVC_MAX_SHORT_TERM_REF_PIC_SET 65
83 #define HEVC_MAX_LONG_TERM_REF_PIC_SET 33
87 unsigned num_delta_pocs
;
88 } hevc_short_term_ref_pic_set_t
;
92 nal_u1_t aspect_ratio_info_present_flag
;
95 nal_u8_t aspect_ratio_idc
;
99 nal_u1_t overscan_info_present_flag
;
100 nal_u1_t overscan_appropriate_flag
;
102 nal_u1_t video_signal_type_present_flag
;
105 nal_u3_t video_format
;
106 nal_u1_t video_full_range_flag
;
107 nal_u1_t colour_description_present_flag
;
110 nal_u8_t colour_primaries
;
111 nal_u8_t transfer_characteristics
;
112 nal_u8_t matrix_coeffs
;
116 nal_u1_t chroma_loc_info_present_flag
;
119 nal_ue_t sample_loc_type_top_field
;
120 nal_ue_t sample_loc_type_bottom_field
;
123 nal_u1_t neutral_chroma_indication_flag
;
124 nal_u1_t field_seq_flag
;
125 nal_u1_t frame_field_info_present_flag
;
127 nal_u1_t default_display_window_flag
;
130 nal_ue_t win_left_offset
;
131 nal_ue_t win_right_offset
;
132 nal_ue_t win_top_offset
;
133 nal_ue_t win_bottom_offset
;
136 nal_u1_t vui_timing_info_present_flag
;
139 uint32_t vui_num_units_in_tick
;
140 uint32_t vui_time_scale
;
145 } hevc_vui_parameters_t
;
147 struct hevc_video_parameter_set_t
149 nal_u4_t vps_video_parameter_set_id
;
150 nal_u1_t vps_base_layer_internal_flag
;
151 nal_u1_t vps_base_layer_available_flag
;
152 nal_u6_t vps_max_layers_minus1
;
153 nal_u3_t vps_max_sub_layers_minus1
;
154 nal_u1_t vps_temporal_id_nesting_flag
;
156 hevc_profile_tier_level_t profile_tier_level
;
158 nal_u1_t vps_sub_layer_ordering_info_present_flag
;
161 nal_ue_t dec_pic_buffering_minus1
;
162 nal_ue_t num_reorder_pics
;
163 nal_ue_t max_latency_increase_plus1
;
164 } vps_max
[1 + HEVC_MAX_SUBLAYERS
];
166 nal_u6_t vps_max_layer_id
;
167 nal_ue_t vps_num_layer_set_minus1
;
168 // layer_id_included_flag; read but discarded
170 nal_u1_t vps_timing_info_present_flag
;
171 uint32_t vps_num_units_in_tick
;
172 uint32_t vps_time_scale
;
177 struct hevc_sequence_parameter_set_t
179 nal_u4_t sps_video_parameter_set_id
;
180 nal_u3_t sps_max_sub_layers_minus1
;
181 nal_u1_t sps_temporal_id_nesting_flag
;
183 hevc_profile_tier_level_t profile_tier_level
;
185 nal_ue_t sps_seq_parameter_set_id
;
186 nal_ue_t chroma_format_idc
;
187 nal_u1_t separate_colour_plane_flag
;
189 nal_ue_t pic_width_in_luma_samples
;
190 nal_ue_t pic_height_in_luma_samples
;
192 nal_u1_t conformance_window_flag
;
195 nal_ue_t left_offset
;
196 nal_ue_t right_offset
;
198 nal_ue_t bottom_offset
;
201 nal_ue_t bit_depth_luma_minus8
;
202 nal_ue_t bit_depth_chroma_minus8
;
203 nal_ue_t log2_max_pic_order_cnt_lsb_minus4
;
205 nal_u1_t sps_sub_layer_ordering_info_present_flag
;
208 nal_ue_t dec_pic_buffering_minus1
;
209 nal_ue_t num_reorder_pics
;
210 nal_ue_t latency_increase_plus1
;
211 } sps_max
[1 + HEVC_MAX_SUBLAYERS
];
213 nal_ue_t log2_min_luma_coding_block_size_minus3
;
214 nal_ue_t log2_diff_max_min_luma_coding_block_size
;
215 nal_ue_t log2_min_luma_transform_block_size_minus2
;
216 nal_ue_t log2_diff_max_min_luma_transform_block_size
;
219 nal_ue_t max_transform_hierarchy_depth_inter
;
220 nal_ue_t max_transform_hierarchy_depth_intra
;
221 nal_u1_t scaling_list_enabled
;
222 nal_u1_t sps_scaling_list_data_present_flag
;
223 // scaling_list_data; read but discarded
225 nal_u1_t amp_enabled_flag
;
226 nal_u1_t sample_adaptive_offset_enabled_flag
;
228 nal_u1_t pcm_enabled_flag
;
229 nal_u4_t pcm_sample_bit_depth_luma_minus1
;
230 nal_u4_t pcm_sample_bit_depth_chroma_minus1
;
231 nal_ue_t log2_min_pcm_luma_coding_block_size_minus3
;
232 nal_ue_t log2_diff_max_min_pcm_luma_coding_block_size
;
233 nal_u1_t pcm_loop_filter_disabled_flag
;
235 nal_ue_t num_short_term_ref_pic_sets
;
238 nal_u1_t long_term_ref_pics_present_flag
;
239 nal_ue_t num_long_term_ref_pics_sps
;
242 nal_u1_t sps_temporal_mvp_enabled_flag
;
243 nal_u1_t strong_intra_smoothing_enabled_flag
;
245 nal_u1_t vui_parameters_present_flag
;
246 hevc_vui_parameters_t vui
;
250 struct hevc_picture_parameter_set_t
252 nal_ue_t pps_pic_parameter_set_id
;
253 nal_ue_t pps_seq_parameter_set_id
;
254 nal_u1_t dependent_slice_segments_enabled_flag
;
255 nal_u1_t output_flag_present_flag
;
256 nal_u3_t num_extra_slice_header_bits
;
257 nal_u1_t sign_data_hiding_enabled_flag
;
258 nal_u1_t cabac_init_present_flag
;
259 nal_ue_t num_ref_idx_l0_default_active_minus1
;
260 nal_ue_t num_ref_idx_l1_default_active_minus1
;
261 nal_se_t init_qp_minus26
;
262 nal_u1_t constrained_intra_pred_flag
;
263 nal_u1_t transform_skip_enabled_flag
;
265 nal_u1_t cu_qp_delta_enabled_flag
;
266 nal_ue_t diff_cu_qp_delta_depth
;
268 nal_se_t pps_cb_qp_offset
;
269 nal_se_t pps_cr_qp_offset
;
270 nal_u1_t pic_slice_level_chroma_qp_offsets_present_flag
;
271 nal_u1_t weighted_pred_flag
;
272 nal_u1_t weighted_bipred_flag
;
273 nal_u1_t transquant_bypass_enable_flag
;
275 nal_u1_t tiles_enabled_flag
;
276 nal_u1_t entropy_coding_sync_enabled_flag
;
277 nal_ue_t num_tile_columns_minus1
;
278 nal_ue_t num_tile_rows_minus1
;
279 nal_u1_t uniform_spacing_flag
;
280 // nal_ue_t *p_column_width_minus1; read but discarded
281 // nal_ue_t *p_row_height_minus1; read but discarded
282 nal_u1_t loop_filter_across_tiles_enabled_flag
;
284 nal_u1_t pps_loop_filter_across_slices_enabled_flag
;
286 nal_u1_t deblocking_filter_control_present_flag
;
287 nal_u1_t deblocking_filter_override_enabled_flag
;
288 nal_u1_t pps_deblocking_filter_disabled_flag
;
289 nal_se_t pps_beta_offset_div2
;
290 nal_se_t pps_tc_offset_div2
;
292 nal_u1_t scaling_list_data_present_flag
;
293 // scaling_list_data; read but discarded
295 nal_u1_t lists_modification_present_flag
;
296 nal_ue_t log2_parallel_merge_level_minus2
;
297 nal_u1_t slice_header_extension_present_flag
;
299 nal_u1_t pps_extension_present_flag
;
300 nal_u1_t pps_range_extension_flag
;
301 nal_u1_t pps_multilayer_extension_flag
;
302 nal_u1_t pps_3d_extension_flag
;
303 nal_u5_t pps_extension_5bits
;
308 struct hevc_slice_segment_header_t
311 nal_u6_t nuh_layer_id
;
312 nal_u3_t temporal_id
;
313 nal_u1_t first_slice_segment_in_pic_flag
;
314 nal_u1_t no_output_of_prior_pics_flag
;
315 nal_ue_t slice_pic_parameter_set_id
;
316 nal_u1_t dependent_slice_segment_flag
;
317 // slice_segment_address; read but discarded
319 nal_u1_t pic_output_flag
;
321 uint32_t pic_order_cnt_lsb
;
326 /* Computes size and does check the whole struct integrity */
327 static size_t get_hvcC_to_AnnexB_NAL_size( const uint8_t *p_buf
, size_t i_buf
)
331 if( i_buf
< HEVC_MIN_HVCC_SIZE
)
334 const uint8_t i_nal_length_size
= (p_buf
[21] & 0x03) + 1;
335 if(i_nal_length_size
== 3)
338 const uint8_t i_num_array
= p_buf
[22];
339 p_buf
+= 23; i_buf
-= 23;
341 for( uint8_t i
= 0; i
< i_num_array
; i
++ )
346 const uint16_t i_num_nalu
= p_buf
[1] << 8 | p_buf
[2];
347 p_buf
+= 3; i_buf
-= 3;
349 for( uint16_t j
= 0; j
< i_num_nalu
; j
++ )
354 const uint16_t i_nalu_length
= p_buf
[0] << 8 | p_buf
[1];
355 if(i_buf
< (size_t)i_nalu_length
+ 2)
358 i_total
+= i_nalu_length
+ i_nal_length_size
;
359 p_buf
+= i_nalu_length
+ 2;
360 i_buf
-= i_nalu_length
+ 2;
367 uint8_t * hevc_hvcC_to_AnnexB_NAL( const uint8_t *p_buf
, size_t i_buf
,
368 size_t *pi_result
, uint8_t *pi_nal_length_size
)
370 *pi_result
= get_hvcC_to_AnnexB_NAL_size( p_buf
, i_buf
); /* Does all checks */
371 if( *pi_result
== 0 )
374 if( pi_nal_length_size
)
375 *pi_nal_length_size
= hevc_getNALLengthSize( p_buf
);
378 uint8_t *p_out_buf
= p_ret
= malloc( *pi_result
);
385 const uint8_t i_num_array
= p_buf
[22];
388 for( uint8_t i
= 0; i
< i_num_array
; i
++ )
390 const uint16_t i_num_nalu
= p_buf
[1] << 8 | p_buf
[2];
393 for( uint16_t j
= 0; j
< i_num_nalu
; j
++ )
395 const uint16_t i_nalu_length
= p_buf
[0] << 8 | p_buf
[1];
397 memcpy( p_out_buf
, annexb_startcode4
, 4 );
398 memcpy( &p_out_buf
[4], &p_buf
[2], i_nalu_length
);
400 p_out_buf
+= 4 + i_nalu_length
;
401 p_buf
+= 2 + i_nalu_length
;
408 static bool hevc_parse_scaling_list_rbsp( bs_t
*p_bs
)
410 if( bs_remain( p_bs
) < 16 )
413 for( int i
=0; i
<4; i
++ )
415 for( int j
=0; j
<6; j
+= (i
== 3) ? 3 : 1 )
417 if( bs_read1( p_bs
) == 0 )
421 unsigned nextCoef
= 8;
422 unsigned coefNum
= __MIN( 64, (1 << (4 + (i
<< 1))));
425 nextCoef
= bs_read_se( p_bs
) + 8;
427 for( unsigned k
=0; k
<coefNum
; k
++ )
429 nextCoef
= ( nextCoef
+ bs_read_se( p_bs
) + 256 ) % 256;
438 static bool hevc_parse_vui_parameters_rbsp( bs_t
*p_bs
, hevc_vui_parameters_t
*p_vui
)
440 if( bs_remain( p_bs
) < 10 )
443 p_vui
->aspect_ratio_info_present_flag
= bs_read1( p_bs
);
444 if( p_vui
->aspect_ratio_info_present_flag
)
446 p_vui
->ar
.aspect_ratio_idc
= bs_read( p_bs
, 8 );
447 if( p_vui
->ar
.aspect_ratio_idc
== 0xFF ) //HEVC_SAR__IDC_EXTENDED_SAR )
449 p_vui
->ar
.sar_width
= bs_read( p_bs
, 16 );
450 p_vui
->ar
.sar_height
= bs_read( p_bs
, 16 );
454 p_vui
->overscan_info_present_flag
= bs_read1( p_bs
);
455 if( p_vui
->overscan_info_present_flag
)
456 p_vui
->overscan_appropriate_flag
= bs_read1( p_bs
);
458 p_vui
->video_signal_type_present_flag
= bs_read1( p_bs
);
459 if( p_vui
->video_signal_type_present_flag
)
461 p_vui
->vs
.video_format
= bs_read( p_bs
, 3 );
462 p_vui
->vs
.video_full_range_flag
= bs_read1( p_bs
);
463 p_vui
->vs
.colour_description_present_flag
= bs_read1( p_bs
);
464 if( p_vui
->vs
.colour_description_present_flag
)
466 p_vui
->vs
.colour
.colour_primaries
= bs_read( p_bs
, 8 );
467 p_vui
->vs
.colour
.transfer_characteristics
= bs_read( p_bs
, 8 );
468 p_vui
->vs
.colour
.matrix_coeffs
= bs_read( p_bs
, 8 );
472 p_vui
->vs
.colour
.colour_primaries
= HXXX_PRIMARIES_UNSPECIFIED
;
473 p_vui
->vs
.colour
.transfer_characteristics
= HXXX_TRANSFER_UNSPECIFIED
;
474 p_vui
->vs
.colour
.matrix_coeffs
= HXXX_MATRIX_UNSPECIFIED
;
478 p_vui
->chroma_loc_info_present_flag
= bs_read1( p_bs
);
479 if( p_vui
->chroma_loc_info_present_flag
)
481 p_vui
->chroma
.sample_loc_type_top_field
= bs_read_ue( p_bs
);
482 p_vui
->chroma
.sample_loc_type_bottom_field
= bs_read_ue( p_bs
);
485 p_vui
->neutral_chroma_indication_flag
= bs_read1( p_bs
);
486 p_vui
->field_seq_flag
= bs_read1( p_bs
);
487 p_vui
->frame_field_info_present_flag
= bs_read1( p_bs
);
489 p_vui
->default_display_window_flag
= bs_read1( p_bs
);
490 if( p_vui
->default_display_window_flag
)
492 p_vui
->def_disp
.win_left_offset
= bs_read_ue( p_bs
);
493 p_vui
->def_disp
.win_right_offset
= bs_read_ue( p_bs
);
494 p_vui
->def_disp
.win_top_offset
= bs_read_ue( p_bs
);
495 p_vui
->def_disp
.win_bottom_offset
= bs_read_ue( p_bs
);
498 p_vui
->vui_timing_info_present_flag
= bs_read1( p_bs
);
499 if( p_vui
->vui_timing_info_present_flag
)
501 p_vui
->timing
.vui_num_units_in_tick
= bs_read( p_bs
, 32 );
502 p_vui
->timing
.vui_time_scale
= bs_read( p_bs
, 32 );
504 if( bs_remain( p_bs
) < 3 )
509 if( bs_remain( p_bs
) < 1 ) /* late fail */
515 /* Shortcut for retrieving vps/sps/pps id */
516 bool hevc_get_xps_id(const uint8_t *p_buf
, size_t i_buf
, uint8_t *pi_id
)
518 if(unlikely(!hxxx_strip_AnnexB_startcode(&p_buf
, &i_buf
) || i_buf
< 3))
520 /* No need to lookup convert from emulation for that data */
521 uint8_t i_nal_type
= hevc_getNALType(p_buf
);
523 bs_init(&bs
, &p_buf
[2], i_buf
- 2);
524 if(i_nal_type
== HEVC_NAL_PPS
)
526 *pi_id
= bs_read_ue( &bs
);
527 if(*pi_id
> HEVC_PPS_ID_MAX
)
532 *pi_id
= bs_read( &bs
, 4 );
533 if(i_nal_type
== HEVC_NAL_SPS
)
535 if(*pi_id
> HEVC_SPS_ID_MAX
)
538 else if(*pi_id
> HEVC_VPS_ID_MAX
)
544 static bool hevc_parse_inner_profile_tier_level_rbsp( bs_t
*p_bs
,
545 hevc_inner_profile_tier_level_t
*p_in
)
547 if( bs_remain( p_bs
) < 88 )
550 p_in
->profile_space
= bs_read( p_bs
, 2 );
551 p_in
->tier_flag
= bs_read1( p_bs
);
552 p_in
->profile_idc
= bs_read( p_bs
, 5 );
553 p_in
->profile_compatibility_flag
= bs_read( p_bs
, 32 );
554 p_in
->progressive_source_flag
= bs_read1( p_bs
);
555 p_in
->interlaced_source_flag
= bs_read1( p_bs
);
556 p_in
->non_packed_constraint_flag
= bs_read1( p_bs
);
557 p_in
->frame_only_constraint_flag
= bs_read1( p_bs
);
559 if( ( p_in
->profile_idc
>= 4 && p_in
->profile_idc
<= 7 ) ||
560 ( p_in
->profile_compatibility_flag
& 0x0F000000 ) )
562 p_in
->idc4to7
.max_12bit_constraint_flag
= bs_read1( p_bs
);
563 p_in
->idc4to7
.max_10bit_constraint_flag
= bs_read1( p_bs
);
564 p_in
->idc4to7
.max_8bit_constraint_flag
= bs_read1( p_bs
);
565 p_in
->idc4to7
.max_422chroma_constraint_flag
= bs_read1( p_bs
);
566 p_in
->idc4to7
.max_420chroma_constraint_flag
= bs_read1( p_bs
);
567 p_in
->idc4to7
.max_monochrome_constraint_flag
= bs_read1( p_bs
);
568 p_in
->idc4to7
.intra_constraint_flag
= bs_read1( p_bs
);
569 p_in
->idc4to7
.one_picture_only_constraint_flag
= bs_read1( p_bs
);
570 p_in
->idc4to7
.lower_bit_rate_constraint_flag
= bs_read1( p_bs
);
571 (void) bs_read( p_bs
, 2 );
575 (void) bs_read( p_bs
, 11 );
577 (void) bs_read( p_bs
, 32 );
579 if( ( p_in
->profile_idc
>= 1 && p_in
->profile_idc
<= 5 ) ||
580 ( p_in
->profile_compatibility_flag
& 0x7C000000 ) )
581 p_in
->idc1to5
.inbld_flag
= bs_read1( p_bs
);
583 (void) bs_read1( p_bs
);
588 static bool hevc_parse_profile_tier_level_rbsp( bs_t
*p_bs
, bool profile_present
,
589 uint8_t max_num_sub_layers_minus1
,
590 hevc_profile_tier_level_t
*p_ptl
)
592 if( profile_present
&& !hevc_parse_inner_profile_tier_level_rbsp( p_bs
, &p_ptl
->general
) )
595 if( bs_remain( p_bs
) < 8)
598 p_ptl
->general_level_idc
= bs_read( p_bs
, 8 );
600 if( max_num_sub_layers_minus1
> 0 )
602 if( bs_remain( p_bs
) < 16 )
605 for( uint8_t i
=0; i
< 8; i
++ )
607 if( i
< max_num_sub_layers_minus1
)
609 if( bs_read1( p_bs
) )
610 p_ptl
->sublayer_profile_present_flag
|= (0x80 >> i
);
611 if( bs_read1( p_bs
) )
612 p_ptl
->sublayer_level_present_flag
|= (0x80 >> i
);
618 for( uint8_t i
=0; i
< max_num_sub_layers_minus1
; i
++ )
620 if( ( p_ptl
->sublayer_profile_present_flag
& (0x80 >> i
) ) &&
621 ! hevc_parse_inner_profile_tier_level_rbsp( p_bs
, &p_ptl
->sub_layer
[i
] ) )
624 if( p_ptl
->sublayer_profile_present_flag
& (0x80 >> i
) )
626 if( bs_remain( p_bs
) < 8 )
628 p_ptl
->sub_layer_level_idc
[i
] = bs_read( p_bs
, 8 );
636 static bool hevc_parse_video_parameter_set_rbsp( bs_t
*p_bs
,
637 hevc_video_parameter_set_t
*p_vps
)
639 if( bs_remain( p_bs
) < 134 )
642 p_vps
->vps_video_parameter_set_id
= bs_read( p_bs
, 4 );
643 p_vps
->vps_base_layer_internal_flag
= bs_read1( p_bs
);
644 p_vps
->vps_base_layer_available_flag
= bs_read1( p_bs
);
645 p_vps
->vps_max_layers_minus1
= bs_read( p_bs
, 6 );
646 p_vps
->vps_max_sub_layers_minus1
= bs_read( p_bs
, 3 );
647 p_vps
->vps_temporal_id_nesting_flag
= bs_read1( p_bs
);
650 if( !hevc_parse_profile_tier_level_rbsp( p_bs
, true, p_vps
->vps_max_sub_layers_minus1
,
651 &p_vps
->profile_tier_level
) )
654 p_vps
->vps_sub_layer_ordering_info_present_flag
= bs_read1( p_bs
);
655 for( unsigned i
= (p_vps
->vps_sub_layer_ordering_info_present_flag
?
656 0 : p_vps
->vps_max_sub_layers_minus1
);
657 i
<= p_vps
->vps_max_sub_layers_minus1
; i
++ )
659 p_vps
->vps_max
[i
].dec_pic_buffering_minus1
= bs_read_ue( p_bs
);
660 p_vps
->vps_max
[i
].num_reorder_pics
= bs_read_ue( p_bs
);
661 p_vps
->vps_max
[i
].max_latency_increase_plus1
= bs_read_ue( p_bs
);
663 if( bs_remain( p_bs
) < 10 )
666 p_vps
->vps_max_layer_id
= bs_read( p_bs
, 6 );
667 p_vps
->vps_num_layer_set_minus1
= bs_read_ue( p_bs
);
668 // layer_id_included_flag; read but discarded
669 bs_skip( p_bs
, p_vps
->vps_num_layer_set_minus1
* (p_vps
->vps_max_layer_id
+ 1) );
670 if( bs_remain( p_bs
) < 2 )
673 p_vps
->vps_timing_info_present_flag
= bs_read1( p_bs
);
674 if( p_vps
->vps_timing_info_present_flag
)
676 p_vps
->vps_num_units_in_tick
= bs_read( p_bs
, 32 );
677 p_vps
->vps_time_scale
= bs_read( p_bs
, 32 );
679 /* parsing incomplete */
681 if( bs_remain( p_bs
) < 1 )
687 void hevc_rbsp_release_vps( hevc_video_parameter_set_t
*p_vps
)
692 #define IMPL_hevc_generic_decode( name, hevctype, decode, release ) \
693 hevctype * name( const uint8_t *p_buf, size_t i_buf, bool b_escaped ) \
695 hevctype *p_hevctype = calloc(1, sizeof(hevctype)); \
696 if(likely(p_hevctype)) \
699 bs_init( &bs, p_buf, i_buf ); \
700 unsigned i_bitflow = 0; \
703 bs.p_fwpriv = &i_bitflow; \
704 bs.pf_forward = hxxx_bsfw_ep3b_to_rbsp; /* Does the emulated 3bytes conversion to rbsp */ \
706 else (void) i_bitflow;\
707 bs_skip( &bs, 7 ); /* nal_unit_header */ \
708 uint8_t i_nuh_layer_id = bs_read( &bs, 6 ); \
709 bs_skip( &bs, 3 ); /* !nal_unit_header */ \
710 if( i_nuh_layer_id > 62 || !decode( &bs, p_hevctype ) ) \
712 release( p_hevctype ); \
719 IMPL_hevc_generic_decode( hevc_decode_vps
, hevc_video_parameter_set_t
,
720 hevc_parse_video_parameter_set_rbsp
, hevc_rbsp_release_vps
)
722 static bool hevc_parse_st_ref_pic_set( bs_t
*p_bs
, unsigned stRpsIdx
,
723 unsigned num_short_term_ref_pic_sets
,
724 hevc_short_term_ref_pic_set_t
*p_sets
)
726 if( stRpsIdx
&& bs_read1( p_bs
) ) /* Interref pic set prediction flag */
728 nal_ue_t delta_idx_minus_1
= 0;
729 if( stRpsIdx
== num_short_term_ref_pic_sets
)
731 delta_idx_minus_1
= bs_read_ue( p_bs
);
732 if( delta_idx_minus_1
>= stRpsIdx
)
735 if(delta_idx_minus_1
== stRpsIdx
)
738 nal_u1_t delta_rps_sign
= bs_read1( p_bs
);
739 nal_ue_t abs_delta_rps_minus1
= bs_read_ue( p_bs
);
740 unsigned RefRpsIdx
= stRpsIdx
- delta_idx_minus_1
- 1;
741 int deltaRps
= ( 1 - ( delta_rps_sign
<< 1 ) ) * ( abs_delta_rps_minus1
+ 1 );
742 VLC_UNUSED(deltaRps
);
744 unsigned numDeltaPocs
= p_sets
[RefRpsIdx
].num_delta_pocs
;
745 p_sets
[stRpsIdx
].num_delta_pocs
= 0;
746 for( unsigned j
=0; j
<= numDeltaPocs
; j
++ )
748 if( ! bs_read1( p_bs
) ) /* used_by_curr_pic_flag */
750 if( bs_read1( p_bs
) ) /* use_delta_flag */
751 p_sets
[stRpsIdx
].num_delta_pocs
++;
754 p_sets
[stRpsIdx
].num_delta_pocs
++;
759 nal_ue_t num_negative_pics
= bs_read_ue( p_bs
);
760 nal_ue_t num_positive_pics
= bs_read_ue( p_bs
);
761 if( bs_remain( p_bs
) < ((int64_t)num_negative_pics
+ num_positive_pics
) * 2 )
763 for(unsigned int i
=0; i
<num_negative_pics
; i
++)
765 (void) bs_read_ue( p_bs
); /* delta_poc_s0_minus1 */
766 (void) bs_read1( p_bs
); /* used_by_current_pic_s0_flag */
768 for(unsigned int i
=0; i
<num_positive_pics
; i
++)
770 (void) bs_read_ue( p_bs
); /* delta_poc_s1_minus1 */
771 (void) bs_read1( p_bs
); /* used_by_current_pic_s1_flag */
773 p_sets
[stRpsIdx
].num_delta_pocs
= num_positive_pics
+ num_negative_pics
;
779 static bool hevc_parse_sequence_parameter_set_rbsp( bs_t
*p_bs
,
780 hevc_sequence_parameter_set_t
*p_sps
)
782 p_sps
->sps_video_parameter_set_id
= bs_read( p_bs
, 4 );
783 p_sps
->sps_max_sub_layers_minus1
= bs_read( p_bs
, 3 );
784 p_sps
->sps_temporal_id_nesting_flag
= bs_read1( p_bs
);
785 if( !hevc_parse_profile_tier_level_rbsp( p_bs
, true, p_sps
->sps_max_sub_layers_minus1
,
786 &p_sps
->profile_tier_level
) )
789 if( bs_remain( p_bs
) < 1 )
792 p_sps
->sps_seq_parameter_set_id
= bs_read_ue( p_bs
);
793 if( p_sps
->sps_seq_parameter_set_id
> HEVC_SPS_ID_MAX
)
796 p_sps
->chroma_format_idc
= bs_read_ue( p_bs
);
797 if( p_sps
->chroma_format_idc
== 3 )
798 p_sps
->separate_colour_plane_flag
= bs_read1( p_bs
);
799 p_sps
->pic_width_in_luma_samples
= bs_read_ue( p_bs
);
800 p_sps
->pic_height_in_luma_samples
= bs_read_ue( p_bs
);
801 if( !p_sps
->pic_width_in_luma_samples
|| !p_sps
->pic_height_in_luma_samples
)
804 p_sps
->conformance_window_flag
= bs_read1( p_bs
);
805 if( p_sps
->conformance_window_flag
)
807 p_sps
->conf_win
.left_offset
= bs_read_ue( p_bs
);
808 p_sps
->conf_win
.right_offset
= bs_read_ue( p_bs
);
809 p_sps
->conf_win
.top_offset
= bs_read_ue( p_bs
);
810 p_sps
->conf_win
.bottom_offset
= bs_read_ue( p_bs
);
813 p_sps
->bit_depth_luma_minus8
= bs_read_ue( p_bs
);
814 p_sps
->bit_depth_chroma_minus8
= bs_read_ue( p_bs
);
815 p_sps
->log2_max_pic_order_cnt_lsb_minus4
= bs_read_ue( p_bs
);
817 p_sps
->sps_sub_layer_ordering_info_present_flag
= bs_read1( p_bs
);
818 for( uint8_t i
=(p_sps
->sps_sub_layer_ordering_info_present_flag
? 0 : p_sps
->sps_max_sub_layers_minus1
);
819 i
<= p_sps
->sps_max_sub_layers_minus1
; i
++ )
821 p_sps
->sps_max
[i
].dec_pic_buffering_minus1
= bs_read_ue( p_bs
);
822 p_sps
->sps_max
[i
].num_reorder_pics
= bs_read_ue( p_bs
);
823 p_sps
->sps_max
[i
].latency_increase_plus1
= bs_read_ue( p_bs
);
826 if( bs_remain( p_bs
) < 4 )
829 p_sps
->log2_min_luma_coding_block_size_minus3
= bs_read_ue( p_bs
);
830 p_sps
->log2_diff_max_min_luma_coding_block_size
= bs_read_ue( p_bs
);
831 p_sps
->log2_min_luma_transform_block_size_minus2
= bs_read_ue( p_bs
);
832 if( bs_remain( p_bs
) < 1 ) /* last late fail check */
834 p_sps
->log2_diff_max_min_luma_transform_block_size
= bs_read_ue( p_bs
);
836 /* parsing incomplete */
838 p_sps
->max_transform_hierarchy_depth_inter
= bs_read_ue( p_bs
);
839 p_sps
->max_transform_hierarchy_depth_intra
= bs_read_ue( p_bs
);
840 p_sps
->scaling_list_enabled
= bs_read1( p_bs
);
841 if( p_sps
->scaling_list_enabled
)
843 p_sps
->sps_scaling_list_data_present_flag
= bs_read1( p_bs
);
844 if( p_sps
->sps_scaling_list_data_present_flag
&&
845 ! hevc_parse_scaling_list_rbsp( p_bs
) )
851 p_sps
->amp_enabled_flag
= bs_read1( p_bs
);
852 p_sps
->sample_adaptive_offset_enabled_flag
= bs_read1( p_bs
);
854 p_sps
->pcm_enabled_flag
= bs_read1( p_bs
);
855 if( p_sps
->pcm_enabled_flag
)
857 p_sps
->pcm_sample_bit_depth_luma_minus1
= bs_read( p_bs
, 4 );
858 p_sps
->pcm_sample_bit_depth_chroma_minus1
= bs_read( p_bs
, 4 );
859 p_sps
->log2_min_pcm_luma_coding_block_size_minus3
= bs_read_ue( p_bs
);
860 p_sps
->log2_diff_max_min_pcm_luma_coding_block_size
= bs_read_ue( p_bs
);
861 p_sps
->pcm_loop_filter_disabled_flag
= bs_read1( p_bs
);
864 p_sps
->num_short_term_ref_pic_sets
= bs_read_ue( p_bs
);
865 if( p_sps
->num_short_term_ref_pic_sets
> HEVC_MAX_SHORT_TERM_REF_PIC_SET
)
868 hevc_short_term_ref_pic_set_t sets
[HEVC_MAX_SHORT_TERM_REF_PIC_SET
];
869 memset(&sets
, 0, sizeof(hevc_short_term_ref_pic_set_t
) * HEVC_MAX_SHORT_TERM_REF_PIC_SET
);
870 for( unsigned int i
=0; i
<p_sps
->num_short_term_ref_pic_sets
; i
++ )
872 if( !hevc_parse_st_ref_pic_set( p_bs
, i
, p_sps
->num_short_term_ref_pic_sets
, sets
) )
876 p_sps
->long_term_ref_pics_present_flag
= bs_read1( p_bs
);
877 if( p_sps
->long_term_ref_pics_present_flag
)
879 p_sps
->num_long_term_ref_pics_sps
= bs_read_ue( p_bs
);
880 if( p_sps
->num_long_term_ref_pics_sps
> HEVC_MAX_LONG_TERM_REF_PIC_SET
)
882 for( unsigned int i
=0; i
< p_sps
->num_long_term_ref_pics_sps
; i
++ )
884 /* lt_ref_pic_poc_lsb_sps */
885 bs_skip( p_bs
, p_sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4 );
886 /* used_by_curr_pic_lt_sps_flag */
891 p_sps
->sps_temporal_mvp_enabled_flag
= bs_read1( p_bs
);
892 p_sps
->strong_intra_smoothing_enabled_flag
= bs_read1( p_bs
);
894 if( bs_remain( p_bs
) < 1 ) /* late fail */
897 p_sps
->vui_parameters_present_flag
= bs_read1( p_bs
);
898 if( p_sps
->vui_parameters_present_flag
&&
899 !hevc_parse_vui_parameters_rbsp( p_bs
, &p_sps
->vui
) )
907 void hevc_rbsp_release_sps( hevc_sequence_parameter_set_t
*p_sps
)
912 IMPL_hevc_generic_decode( hevc_decode_sps
, hevc_sequence_parameter_set_t
,
913 hevc_parse_sequence_parameter_set_rbsp
, hevc_rbsp_release_sps
)
915 static bool hevc_parse_pic_parameter_set_rbsp( bs_t
*p_bs
,
916 hevc_picture_parameter_set_t
*p_pps
)
918 if( bs_remain( p_bs
) < 1 )
920 p_pps
->pps_pic_parameter_set_id
= bs_read_ue( p_bs
);
921 if( p_pps
->pps_pic_parameter_set_id
> HEVC_PPS_ID_MAX
|| bs_remain( p_bs
) < 1 )
923 p_pps
->pps_seq_parameter_set_id
= bs_read_ue( p_bs
);
924 if( p_pps
->pps_seq_parameter_set_id
> HEVC_SPS_ID_MAX
)
926 p_pps
->dependent_slice_segments_enabled_flag
= bs_read1( p_bs
);
927 p_pps
->output_flag_present_flag
= bs_read1( p_bs
);
928 p_pps
->num_extra_slice_header_bits
= bs_read( p_bs
, 3 );
929 p_pps
->sign_data_hiding_enabled_flag
= bs_read1( p_bs
);
930 p_pps
->cabac_init_present_flag
= bs_read1( p_bs
);
932 p_pps
->num_ref_idx_l0_default_active_minus1
= bs_read_ue( p_bs
);
933 p_pps
->num_ref_idx_l1_default_active_minus1
= bs_read_ue( p_bs
);
935 p_pps
->init_qp_minus26
= bs_read_se( p_bs
);
936 p_pps
->constrained_intra_pred_flag
= bs_read1( p_bs
);
937 p_pps
->transform_skip_enabled_flag
= bs_read1( p_bs
);
938 p_pps
->cu_qp_delta_enabled_flag
= bs_read1( p_bs
);
939 if( p_pps
->cu_qp_delta_enabled_flag
)
940 p_pps
->diff_cu_qp_delta_depth
= bs_read_ue( p_bs
);
942 if( bs_remain( p_bs
) < 1 )
945 p_pps
->pps_cb_qp_offset
= bs_read_se( p_bs
);
946 p_pps
->pps_cr_qp_offset
= bs_read_se( p_bs
);
947 p_pps
->pic_slice_level_chroma_qp_offsets_present_flag
= bs_read1( p_bs
);
948 p_pps
->weighted_pred_flag
= bs_read1( p_bs
);
949 p_pps
->weighted_bipred_flag
= bs_read1( p_bs
);
950 p_pps
->transquant_bypass_enable_flag
= bs_read1( p_bs
);
951 p_pps
->tiles_enabled_flag
= bs_read1( p_bs
);
952 p_pps
->entropy_coding_sync_enabled_flag
= bs_read1( p_bs
);
954 if( p_pps
->tiles_enabled_flag
)
956 p_pps
->num_tile_columns_minus1
= bs_read_ue( p_bs
); /* TODO: validate max col/row values */
957 p_pps
->num_tile_rows_minus1
= bs_read_ue( p_bs
); /* against sps PicWidthInCtbsY */
958 p_pps
->uniform_spacing_flag
= bs_read1( p_bs
);
959 if( !p_pps
->uniform_spacing_flag
)
961 if( bs_remain( p_bs
) < (int64_t) p_pps
->num_tile_columns_minus1
+
962 p_pps
->num_tile_rows_minus1
+ 1 )
964 for( unsigned i
=0; i
< p_pps
->num_tile_columns_minus1
; i
++ )
965 (void) bs_read_ue( p_bs
);
966 for( unsigned i
=0; i
< p_pps
->num_tile_rows_minus1
; i
++ )
967 (void) bs_read_ue( p_bs
);
969 p_pps
->loop_filter_across_tiles_enabled_flag
= bs_read1( p_bs
);
972 p_pps
->pps_loop_filter_across_slices_enabled_flag
= bs_read1( p_bs
);
973 p_pps
->deblocking_filter_control_present_flag
= bs_read1( p_bs
);
974 if( p_pps
->deblocking_filter_control_present_flag
)
976 p_pps
->deblocking_filter_override_enabled_flag
= bs_read1( p_bs
);
977 p_pps
->pps_deblocking_filter_disabled_flag
= bs_read1( p_bs
);
978 if( !p_pps
->pps_deblocking_filter_disabled_flag
)
980 p_pps
->pps_beta_offset_div2
= bs_read_se( p_bs
);
981 p_pps
->pps_tc_offset_div2
= bs_read_se( p_bs
);
985 p_pps
->scaling_list_data_present_flag
= bs_read1( p_bs
);
986 if( p_pps
->scaling_list_data_present_flag
&& !hevc_parse_scaling_list_rbsp( p_bs
) )
989 p_pps
->lists_modification_present_flag
= bs_read1( p_bs
);
990 p_pps
->log2_parallel_merge_level_minus2
= bs_read_ue( p_bs
);
991 p_pps
->slice_header_extension_present_flag
= bs_read1( p_bs
);
993 if( bs_remain( p_bs
) < 1 )
996 p_pps
->pps_extension_present_flag
= bs_read1( p_bs
);
997 if( p_pps
->pps_extension_present_flag
)
999 p_pps
->pps_range_extension_flag
= bs_read1( p_bs
);
1000 p_pps
->pps_multilayer_extension_flag
= bs_read1( p_bs
);
1001 p_pps
->pps_3d_extension_flag
= bs_read1( p_bs
);
1002 if( bs_remain( p_bs
) < 5 )
1004 p_pps
->pps_extension_5bits
= bs_read( p_bs
, 5 );
1010 void hevc_rbsp_release_pps( hevc_picture_parameter_set_t
*p_pps
)
1015 IMPL_hevc_generic_decode( hevc_decode_pps
, hevc_picture_parameter_set_t
,
1016 hevc_parse_pic_parameter_set_rbsp
, hevc_rbsp_release_pps
)
1018 uint8_t hevc_get_sps_vps_id( const hevc_sequence_parameter_set_t
*p_sps
)
1020 return p_sps
->sps_video_parameter_set_id
;
1023 uint8_t hevc_get_pps_sps_id( const hevc_picture_parameter_set_t
*p_pps
)
1025 return p_pps
->pps_seq_parameter_set_id
;
1028 uint8_t hevc_get_slice_pps_id( const hevc_slice_segment_header_t
*p_slice
)
1030 return p_slice
->slice_pic_parameter_set_id
;
1033 bool hevc_get_sps_profile_tier_level( const hevc_sequence_parameter_set_t
*p_sps
,
1034 uint8_t *pi_profile
, uint8_t *pi_level
)
1036 if(p_sps
->profile_tier_level
.general
.profile_idc
)
1038 *pi_profile
= p_sps
->profile_tier_level
.general
.profile_idc
;
1039 *pi_level
= p_sps
->profile_tier_level
.general_level_idc
;
1045 bool hevc_get_picture_size( const hevc_sequence_parameter_set_t
*p_sps
,
1046 unsigned *p_w
, unsigned *p_h
, unsigned *p_vw
, unsigned *p_vh
)
1048 *p_w
= *p_vw
= p_sps
->pic_width_in_luma_samples
;
1049 *p_h
= *p_vh
= p_sps
->pic_height_in_luma_samples
;
1050 if( p_sps
->conformance_window_flag
)
1052 *p_vh
-= p_sps
->conf_win
.bottom_offset
+ p_sps
->conf_win
.top_offset
;
1053 *p_vh
-= p_sps
->conf_win
.left_offset
+ p_sps
->conf_win
.right_offset
;
1058 uint8_t hevc_get_max_num_reorder( const hevc_video_parameter_set_t
*p_vps
)
1060 return p_vps
->vps_max
[0/* base layer */].num_reorder_pics
;
1063 static inline uint8_t vlc_ceil_log2( uint32_t val
)
1065 uint8_t n
= 31 - clz(val
);
1066 if (((unsigned)1 << n
) != val
)
1071 static bool hevc_get_picture_CtbsYsize( const hevc_sequence_parameter_set_t
*p_sps
, unsigned *p_w
, unsigned *p_h
)
1073 const unsigned int MinCbLog2SizeY
= p_sps
->log2_min_luma_coding_block_size_minus3
+ 3;
1074 const unsigned int CtbLog2SizeY
= MinCbLog2SizeY
+ p_sps
->log2_diff_max_min_luma_coding_block_size
;
1075 if( CtbLog2SizeY
> 31 )
1077 const unsigned int CtbSizeY
= 1 << CtbLog2SizeY
;
1078 *p_w
= (p_sps
->pic_width_in_luma_samples
- 1) / CtbSizeY
+ 1;
1079 *p_h
= (p_sps
->pic_height_in_luma_samples
- 1) / CtbSizeY
+ 1;
1083 bool hevc_get_frame_rate( const hevc_sequence_parameter_set_t
*p_sps
,
1084 const hevc_video_parameter_set_t
*p_vps
,
1085 unsigned *pi_num
, unsigned *pi_den
)
1087 if( p_sps
->vui_parameters_present_flag
&& p_sps
->vui
.vui_timing_info_present_flag
)
1089 *pi_den
= p_sps
->vui
.timing
.vui_num_units_in_tick
;
1090 *pi_num
= p_sps
->vui
.timing
.vui_time_scale
;
1091 return (*pi_den
&& *pi_num
);
1093 else if( p_vps
&& p_vps
->vps_timing_info_present_flag
)
1095 *pi_den
= p_vps
->vps_num_units_in_tick
;
1096 *pi_num
= p_vps
->vps_time_scale
;
1097 return (*pi_den
&& *pi_num
);
1102 bool hevc_get_aspect_ratio( const hevc_sequence_parameter_set_t
*p_sps
,
1103 unsigned *num
, unsigned *den
)
1105 if( p_sps
->vui_parameters_present_flag
)
1107 if( p_sps
->vui
.ar
.aspect_ratio_idc
!= 255 )
1109 static const uint8_t ar_table
[16][2] =
1128 if( p_sps
->vui
.ar
.aspect_ratio_idc
> 0 &&
1129 p_sps
->vui
.ar
.aspect_ratio_idc
< 17 )
1131 *num
= ar_table
[p_sps
->vui
.ar
.aspect_ratio_idc
- 1][0];
1132 *den
= ar_table
[p_sps
->vui
.ar
.aspect_ratio_idc
- 1][1];
1138 *num
= p_sps
->vui
.ar
.sar_width
;
1139 *den
= p_sps
->vui
.ar
.sar_height
;
1146 bool hevc_get_colorimetry( const hevc_sequence_parameter_set_t
*p_sps
,
1147 video_color_primaries_t
*p_primaries
,
1148 video_transfer_func_t
*p_transfer
,
1149 video_color_space_t
*p_colorspace
,
1150 bool *p_full_range
)
1152 if( !p_sps
->vui_parameters_present_flag
)
1155 hxxx_colour_primaries_to_vlc( p_sps
->vui
.vs
.colour
.colour_primaries
);
1157 hxxx_transfer_characteristics_to_vlc( p_sps
->vui
.vs
.colour
.transfer_characteristics
);
1159 hxxx_matrix_coeffs_to_vlc( p_sps
->vui
.vs
.colour
.matrix_coeffs
);
1160 *p_full_range
= p_sps
->vui
.vs
.video_full_range_flag
;
1164 static bool hevc_parse_slice_segment_header_rbsp( bs_t
*p_bs
,
1165 pf_get_matchedxps get_matchedxps
,
1167 hevc_slice_segment_header_t
*p_sl
)
1169 hevc_sequence_parameter_set_t
*p_sps
;
1170 hevc_picture_parameter_set_t
*p_pps
;
1171 hevc_video_parameter_set_t
*p_vps
;
1173 if( bs_remain( p_bs
) < 3 )
1176 p_sl
->first_slice_segment_in_pic_flag
= bs_read1( p_bs
);
1177 if( p_sl
->nal_type
>= HEVC_NAL_BLA_W_LP
&& p_sl
->nal_type
<= HEVC_NAL_IRAP_VCL23
)
1178 p_sl
->no_output_of_prior_pics_flag
= bs_read1( p_bs
);
1179 p_sl
->slice_pic_parameter_set_id
= bs_read_ue( p_bs
);
1180 if( p_sl
->slice_pic_parameter_set_id
> HEVC_PPS_ID_MAX
|| bs_remain( p_bs
) < 1 )
1183 get_matchedxps( p_sl
->slice_pic_parameter_set_id
, priv
, &p_pps
, &p_sps
, &p_vps
);
1184 if(!p_sps
|| !p_pps
)
1187 if( !p_sl
->first_slice_segment_in_pic_flag
)
1189 if( p_pps
->dependent_slice_segments_enabled_flag
)
1190 p_sl
->dependent_slice_segment_flag
= bs_read1( p_bs
);
1193 if( !hevc_get_picture_CtbsYsize( p_sps
, &w
, &h
) )
1196 (void) bs_read( p_bs
, vlc_ceil_log2( w
* h
) ); /* slice_segment_address */
1199 if( !p_sl
->dependent_slice_segment_flag
)
1202 if( p_pps
->num_extra_slice_header_bits
> i
)
1205 bs_skip( p_bs
, 1 ); /* discardable_flag */
1208 if( p_pps
->num_extra_slice_header_bits
> i
)
1211 bs_skip( p_bs
, 1 ); /* cross_layer_bla_flag */
1214 if( i
< p_pps
->num_extra_slice_header_bits
)
1215 bs_skip( p_bs
, p_pps
->num_extra_slice_header_bits
- i
);
1217 p_sl
->slice_type
= bs_read_ue( p_bs
);
1218 if( p_sl
->slice_type
> HEVC_SLICE_TYPE_I
)
1221 if( p_pps
->output_flag_present_flag
)
1222 p_sl
->pic_output_flag
= bs_read1( p_bs
);
1225 if( p_sps
->separate_colour_plane_flag
)
1226 bs_skip( p_bs
, 2 ); /* colour_plane_id */
1228 if( p_sl
->nal_type
!= HEVC_NAL_IDR_W_RADL
&& p_sl
->nal_type
!= HEVC_NAL_IDR_N_LP
)
1229 p_sl
->pic_order_cnt_lsb
= bs_read( p_bs
, p_sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4 );
1231 p_sl
->pic_order_cnt_lsb
= 0;
1233 if( bs_remain( p_bs
) < 1 )
1239 void hevc_rbsp_release_slice_header( hevc_slice_segment_header_t
*p_sh
)
1244 hevc_slice_segment_header_t
* hevc_decode_slice_header( const uint8_t *p_buf
, size_t i_buf
, bool b_escaped
,
1245 pf_get_matchedxps get_matchedxps
, void *priv
)
1247 hevc_slice_segment_header_t
*p_sh
= calloc(1, sizeof(hevc_slice_segment_header_t
));
1251 bs_init( &bs
, p_buf
, i_buf
);
1252 unsigned i_bitflow
= 0;
1255 bs
.p_fwpriv
= &i_bitflow
;
1256 bs
.pf_forward
= hxxx_bsfw_ep3b_to_rbsp
; /* Does the emulated 3bytes conversion to rbsp */
1258 else (void) i_bitflow
;
1260 p_sh
->nal_type
= bs_read( &bs
, 6 );
1261 p_sh
->nuh_layer_id
= bs_read( &bs
, 6 );
1262 p_sh
->temporal_id
= bs_read( &bs
, 3 );
1263 if( p_sh
->nuh_layer_id
> 62 ||
1264 !hevc_parse_slice_segment_header_rbsp( &bs
, get_matchedxps
, priv
, p_sh
) )
1266 hevc_rbsp_release_slice_header( p_sh
);
1273 bool hevc_get_slice_type( const hevc_slice_segment_header_t
*p_sli
, enum hevc_slice_type_e
*pi_type
)
1275 if( !p_sli
->dependent_slice_segment_flag
)
1277 *pi_type
= p_sli
->slice_type
;
1283 bool hevc_get_profile_level(const es_format_t
*p_fmt
, uint8_t *pi_profile
,
1284 uint8_t *pi_level
, uint8_t *pi_nal_length_size
)
1286 const uint8_t *p
= (const uint8_t*)p_fmt
->p_extra
;
1287 if(p_fmt
->i_extra
< 23 || p
[0] != 1)
1290 /* HEVCDecoderConfigurationRecord */
1292 *pi_profile
= p
[1] & 0x1F;
1297 if (pi_nal_length_size
)
1298 *pi_nal_length_size
= 1 + (p
[21]&0x03);
1303 /* 8.3.1 Decoding process for POC */
1304 int hevc_compute_picture_order_count( const hevc_sequence_parameter_set_t
*p_sps
,
1305 const hevc_slice_segment_header_t
*p_slice
,
1306 hevc_poc_ctx_t
*p_ctx
)
1309 bool NoRaslOutputFlag
;
1310 bool IsIRAP
= ( p_slice
->nal_type
>= HEVC_NAL_BLA_W_LP
&&
1311 p_slice
->nal_type
<= HEVC_NAL_IRAP_VCL23
);
1315 /* if( IRAP ) NoRaslOutputFlag = first || IDR || BLA || after(EOSNAL) */
1316 NoRaslOutputFlag
=(p_ctx
->first_picture
||
1317 p_slice
->nal_type
== HEVC_NAL_IDR_N_LP
||
1318 p_slice
->nal_type
== HEVC_NAL_IDR_W_RADL
||
1319 p_slice
->nal_type
== HEVC_NAL_BLA_W_LP
||
1320 p_slice
->nal_type
== HEVC_NAL_BLA_W_RADL
||
1321 p_slice
->nal_type
== HEVC_NAL_BLA_N_LP
);
1325 NoRaslOutputFlag
= false;
1328 if( p_slice
->nal_type
== HEVC_NAL_IDR_N_LP
||
1329 p_slice
->nal_type
== HEVC_NAL_IDR_W_RADL
)
1331 p_ctx
->prevPicOrderCnt
.msb
= 0;
1332 p_ctx
->prevPicOrderCnt
.lsb
= 0;
1335 /* Not an IRAP with NoRaslOutputFlag == 1 */
1336 if( !IsIRAP
|| !NoRaslOutputFlag
)
1339 p_ctx
->prevPicOrderCnt
.msb
= p_ctx
->prevTid0PicOrderCnt
.msb
;
1340 p_ctx
->prevPicOrderCnt
.lsb
= p_ctx
->prevTid0PicOrderCnt
.lsb
;
1344 const unsigned maxPocLSB
= 1U << (p_sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4);
1345 pocMSB
= p_ctx
->prevPicOrderCnt
.msb
;
1346 int64_t orderDiff
= p_slice
->pic_order_cnt_lsb
- p_ctx
->prevPicOrderCnt
.lsb
;
1347 if( orderDiff
< 0 && -orderDiff
>= maxPocLSB
/ 2 )
1348 pocMSB
+= maxPocLSB
;
1349 else if( orderDiff
> maxPocLSB
/ 2 )
1350 pocMSB
-= maxPocLSB
;
1353 /* Set prevTid0Pic for next pic */
1354 if( p_slice
->temporal_id
== 0 &&
1355 !( ( p_slice
->nal_type
<= HEVC_NAL_RSV_VCL_N14
&& p_slice
->nal_type
% 2 == 0 /* SLNR */ ) ||
1356 ( p_slice
->nal_type
>= HEVC_NAL_RADL_N
&& p_slice
->nal_type
<= HEVC_NAL_RASL_R
) /* RADL or RASL */ ) )
1358 p_ctx
->prevTid0PicOrderCnt
.msb
= pocMSB
;
1359 p_ctx
->prevTid0PicOrderCnt
.lsb
= p_slice
->pic_order_cnt_lsb
;
1362 p_ctx
->prevPicOrderCnt
.msb
= pocMSB
;
1363 p_ctx
->prevPicOrderCnt
.lsb
= p_slice
->pic_order_cnt_lsb
;
1365 return pocMSB
+ p_slice
->pic_order_cnt_lsb
;
1368 struct hevc_sei_pic_timing_t
1370 nal_u4_t pic_struct
;
1371 nal_u2_t source_scan_type
;
1374 void hevc_release_sei_pic_timing( hevc_sei_pic_timing_t
*p_timing
)
1379 hevc_sei_pic_timing_t
* hevc_decode_sei_pic_timing( bs_t
*p_bs
,
1380 const hevc_sequence_parameter_set_t
*p_sps
)
1382 hevc_sei_pic_timing_t
*p_timing
= malloc(sizeof(*p_timing
));
1385 if( p_sps
->vui
.frame_field_info_present_flag
)
1387 p_timing
->pic_struct
= bs_read( p_bs
, 4 );
1388 p_timing
->source_scan_type
= bs_read( p_bs
, 2 );
1392 p_timing
->pic_struct
= 0;
1393 p_timing
->source_scan_type
= 1;
1399 bool hevc_frame_is_progressive( const hevc_sequence_parameter_set_t
*p_sps
,
1400 const hevc_sei_pic_timing_t
*p_timing
)
1402 if( p_sps
->vui_parameters_present_flag
&&
1403 p_sps
->vui
.field_seq_flag
)
1406 if( p_sps
->profile_tier_level
.general
.interlaced_source_flag
&&
1407 !p_sps
->profile_tier_level
.general
.progressive_source_flag
)
1410 if( p_timing
&& p_sps
->vui
.frame_field_info_present_flag
)
1412 if( p_timing
->source_scan_type
< 2 )
1413 return p_timing
->source_scan_type
!= 0;
1419 uint8_t hevc_get_num_clock_ts( const hevc_sequence_parameter_set_t
*p_sps
,
1420 const hevc_sei_pic_timing_t
*p_timing
)
1422 if( p_sps
->vui
.frame_field_info_present_flag
&& p_timing
&& p_timing
->pic_struct
< 13 )
1424 /* !WARN modified with units_field_based_flag (D.3.25) for values 0, 7 and 8 */
1425 const uint8_t rgi_numclock
[13] = { 2, 1, 1, 2, 2, 3, 3, 4, 6, 1, 1, 1, 1 };
1426 return rgi_numclock
[p_timing
->pic_struct
];
1429 if( p_sps
->vui_parameters_present_flag
)
1431 if( p_sps
->vui
.field_seq_flag
)
1432 return 1; /* D.3.27 */
1434 else if( p_sps
->profile_tier_level
.general
.interlaced_source_flag
&&
1435 !p_sps
->profile_tier_level
.general
.progressive_source_flag
)