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
)
525 *pi_id
= bs_read_ue( &bs
);
527 *pi_id
= bs_read( &bs
, 4 );
532 static bool hevc_parse_inner_profile_tier_level_rbsp( bs_t
*p_bs
,
533 hevc_inner_profile_tier_level_t
*p_in
)
535 if( bs_remain( p_bs
) < 88 )
538 p_in
->profile_space
= bs_read( p_bs
, 2 );
539 p_in
->tier_flag
= bs_read1( p_bs
);
540 p_in
->profile_idc
= bs_read( p_bs
, 5 );
541 p_in
->profile_compatibility_flag
= bs_read( p_bs
, 32 );
542 p_in
->progressive_source_flag
= bs_read1( p_bs
);
543 p_in
->interlaced_source_flag
= bs_read1( p_bs
);
544 p_in
->non_packed_constraint_flag
= bs_read1( p_bs
);
545 p_in
->frame_only_constraint_flag
= bs_read1( p_bs
);
547 if( ( p_in
->profile_idc
>= 4 && p_in
->profile_idc
<= 7 ) ||
548 ( p_in
->profile_compatibility_flag
& 0x0F000000 ) )
550 p_in
->idc4to7
.max_12bit_constraint_flag
= bs_read1( p_bs
);
551 p_in
->idc4to7
.max_10bit_constraint_flag
= bs_read1( p_bs
);
552 p_in
->idc4to7
.max_8bit_constraint_flag
= bs_read1( p_bs
);
553 p_in
->idc4to7
.max_422chroma_constraint_flag
= bs_read1( p_bs
);
554 p_in
->idc4to7
.max_420chroma_constraint_flag
= bs_read1( p_bs
);
555 p_in
->idc4to7
.max_monochrome_constraint_flag
= bs_read1( p_bs
);
556 p_in
->idc4to7
.intra_constraint_flag
= bs_read1( p_bs
);
557 p_in
->idc4to7
.one_picture_only_constraint_flag
= bs_read1( p_bs
);
558 p_in
->idc4to7
.lower_bit_rate_constraint_flag
= bs_read1( p_bs
);
559 (void) bs_read( p_bs
, 2 );
563 (void) bs_read( p_bs
, 11 );
565 (void) bs_read( p_bs
, 32 );
567 if( ( p_in
->profile_idc
>= 1 && p_in
->profile_idc
<= 5 ) ||
568 ( p_in
->profile_compatibility_flag
& 0x7C000000 ) )
569 p_in
->idc1to5
.inbld_flag
= bs_read1( p_bs
);
571 (void) bs_read1( p_bs
);
576 static bool hevc_parse_profile_tier_level_rbsp( bs_t
*p_bs
, bool profile_present
,
577 uint8_t max_num_sub_layers_minus1
,
578 hevc_profile_tier_level_t
*p_ptl
)
580 if( profile_present
&& !hevc_parse_inner_profile_tier_level_rbsp( p_bs
, &p_ptl
->general
) )
583 if( bs_remain( p_bs
) < 8)
586 p_ptl
->general_level_idc
= bs_read( p_bs
, 8 );
588 if( max_num_sub_layers_minus1
> 0 )
590 if( bs_remain( p_bs
) < 16 )
593 for( uint8_t i
=0; i
< 8; i
++ )
595 if( i
< max_num_sub_layers_minus1
)
597 if( bs_read1( p_bs
) )
598 p_ptl
->sublayer_profile_present_flag
|= (0x80 >> i
);
599 if( bs_read1( p_bs
) )
600 p_ptl
->sublayer_level_present_flag
|= (0x80 >> i
);
606 for( uint8_t i
=0; i
< max_num_sub_layers_minus1
; i
++ )
608 if( ( p_ptl
->sublayer_profile_present_flag
& (0x80 >> i
) ) &&
609 ! hevc_parse_inner_profile_tier_level_rbsp( p_bs
, &p_ptl
->sub_layer
[i
] ) )
612 if( p_ptl
->sublayer_profile_present_flag
& (0x80 >> i
) )
614 if( bs_remain( p_bs
) < 8 )
616 p_ptl
->sub_layer_level_idc
[i
] = bs_read( p_bs
, 8 );
624 static bool hevc_parse_video_parameter_set_rbsp( bs_t
*p_bs
,
625 hevc_video_parameter_set_t
*p_vps
)
627 if( bs_remain( p_bs
) < 134 )
630 p_vps
->vps_video_parameter_set_id
= bs_read( p_bs
, 4 );
631 p_vps
->vps_base_layer_internal_flag
= bs_read1( p_bs
);
632 p_vps
->vps_base_layer_available_flag
= bs_read1( p_bs
);
633 p_vps
->vps_max_layers_minus1
= bs_read( p_bs
, 6 );
634 p_vps
->vps_max_sub_layers_minus1
= bs_read( p_bs
, 3 );
635 p_vps
->vps_temporal_id_nesting_flag
= bs_read1( p_bs
);
638 if( !hevc_parse_profile_tier_level_rbsp( p_bs
, true, p_vps
->vps_max_sub_layers_minus1
,
639 &p_vps
->profile_tier_level
) )
642 p_vps
->vps_sub_layer_ordering_info_present_flag
= bs_read1( p_bs
);
643 for( unsigned i
= (p_vps
->vps_sub_layer_ordering_info_present_flag
?
644 0 : p_vps
->vps_max_sub_layers_minus1
);
645 i
<= p_vps
->vps_max_sub_layers_minus1
; i
++ )
647 (void) bs_read_ue( p_bs
); //nal_ue_t dec_pic_buffering_minus1;
648 (void) bs_read_ue( p_bs
); //nal_ue_t num_reorder_pics;
649 (void) bs_read_ue( p_bs
); //nal_ue_t max_latency_increase_plus1;
651 if( bs_remain( p_bs
) < 10 )
654 p_vps
->vps_max_layer_id
= bs_read( p_bs
, 6 );
655 p_vps
->vps_num_layer_set_minus1
= bs_read_ue( p_bs
);
656 // layer_id_included_flag; read but discarded
657 bs_skip( p_bs
, p_vps
->vps_num_layer_set_minus1
* (p_vps
->vps_max_layer_id
+ 1) );
658 if( bs_remain( p_bs
) < 2 )
661 p_vps
->vps_timing_info_present_flag
= bs_read1( p_bs
);
662 if( p_vps
->vps_timing_info_present_flag
)
664 p_vps
->vps_num_units_in_tick
= bs_read( p_bs
, 32 );
665 p_vps
->vps_time_scale
= bs_read( p_bs
, 32 );
667 /* parsing incomplete */
669 if( bs_remain( p_bs
) < 1 )
675 void hevc_rbsp_release_vps( hevc_video_parameter_set_t
*p_vps
)
680 #define IMPL_hevc_generic_decode( name, hevctype, decode, release ) \
681 hevctype * name( const uint8_t *p_buf, size_t i_buf, bool b_escaped ) \
683 hevctype *p_hevctype = calloc(1, sizeof(hevctype)); \
684 if(likely(p_hevctype)) \
687 bs_init( &bs, p_buf, i_buf ); \
688 unsigned i_bitflow = 0; \
691 bs.p_fwpriv = &i_bitflow; \
692 bs.pf_forward = hxxx_bsfw_ep3b_to_rbsp; /* Does the emulated 3bytes conversion to rbsp */ \
694 else (void) i_bitflow;\
695 bs_skip( &bs, 7 ); /* nal_unit_header */ \
696 uint8_t i_nuh_layer_id = bs_read( &bs, 6 ); \
697 bs_skip( &bs, 3 ); /* !nal_unit_header */ \
698 if( i_nuh_layer_id > 62 || !decode( &bs, p_hevctype ) ) \
700 release( p_hevctype ); \
707 IMPL_hevc_generic_decode( hevc_decode_vps
, hevc_video_parameter_set_t
,
708 hevc_parse_video_parameter_set_rbsp
, hevc_rbsp_release_vps
)
710 static bool hevc_parse_st_ref_pic_set( bs_t
*p_bs
, unsigned stRpsIdx
,
711 unsigned num_short_term_ref_pic_sets
,
712 hevc_short_term_ref_pic_set_t
*p_sets
)
714 if( stRpsIdx
&& bs_read1( p_bs
) ) /* Interref pic set prediction flag */
716 nal_ue_t delta_idx_minus_1
= 0;
717 if( stRpsIdx
== num_short_term_ref_pic_sets
)
719 delta_idx_minus_1
= bs_read_ue( p_bs
);
720 if( delta_idx_minus_1
>= stRpsIdx
)
723 if(delta_idx_minus_1
== stRpsIdx
)
726 nal_u1_t delta_rps_sign
= bs_read1( p_bs
);
727 nal_ue_t abs_delta_rps_minus1
= bs_read_ue( p_bs
);
728 unsigned RefRpsIdx
= stRpsIdx
- delta_idx_minus_1
- 1;
729 int deltaRps
= ( 1 - ( delta_rps_sign
<< 1 ) ) * ( abs_delta_rps_minus1
+ 1 );
730 VLC_UNUSED(deltaRps
);
732 unsigned numDeltaPocs
= p_sets
[RefRpsIdx
].num_delta_pocs
;
733 p_sets
[stRpsIdx
].num_delta_pocs
= 0;
734 for( unsigned j
=0; j
<= numDeltaPocs
; j
++ )
736 if( ! bs_read1( p_bs
) ) /* used_by_curr_pic_flag */
738 if( bs_read1( p_bs
) ) /* use_delta_flag */
739 p_sets
[stRpsIdx
].num_delta_pocs
++;
742 p_sets
[stRpsIdx
].num_delta_pocs
++;
747 nal_ue_t num_negative_pics
= bs_read_ue( p_bs
);
748 nal_ue_t num_positive_pics
= bs_read_ue( p_bs
);
749 if( bs_remain( p_bs
) < ((int64_t)num_negative_pics
+ num_positive_pics
) * 2 )
751 for(unsigned int i
=0; i
<num_negative_pics
; i
++)
753 (void) bs_read_ue( p_bs
); /* delta_poc_s0_minus1 */
754 (void) bs_read1( p_bs
); /* used_by_current_pic_s0_flag */
756 for(unsigned int i
=0; i
<num_positive_pics
; i
++)
758 (void) bs_read_ue( p_bs
); /* delta_poc_s1_minus1 */
759 (void) bs_read1( p_bs
); /* used_by_current_pic_s1_flag */
761 p_sets
[stRpsIdx
].num_delta_pocs
= num_positive_pics
+ num_negative_pics
;
767 static bool hevc_parse_sequence_parameter_set_rbsp( bs_t
*p_bs
,
768 hevc_sequence_parameter_set_t
*p_sps
)
770 p_sps
->sps_video_parameter_set_id
= bs_read( p_bs
, 4 );
771 p_sps
->sps_max_sub_layers_minus1
= bs_read( p_bs
, 3 );
772 p_sps
->sps_temporal_id_nesting_flag
= bs_read1( p_bs
);
773 if( !hevc_parse_profile_tier_level_rbsp( p_bs
, true, p_sps
->sps_max_sub_layers_minus1
,
774 &p_sps
->profile_tier_level
) )
777 if( bs_remain( p_bs
) < 1 )
780 p_sps
->sps_seq_parameter_set_id
= bs_read_ue( p_bs
);
781 if( p_sps
->sps_seq_parameter_set_id
> HEVC_SPS_ID_MAX
)
784 p_sps
->chroma_format_idc
= bs_read_ue( p_bs
);
785 if( p_sps
->chroma_format_idc
== 3 )
786 p_sps
->separate_colour_plane_flag
= bs_read1( p_bs
);
787 p_sps
->pic_width_in_luma_samples
= bs_read_ue( p_bs
);
788 p_sps
->pic_height_in_luma_samples
= bs_read_ue( p_bs
);
789 if( !p_sps
->pic_width_in_luma_samples
|| !p_sps
->pic_height_in_luma_samples
)
792 p_sps
->conformance_window_flag
= bs_read1( p_bs
);
793 if( p_sps
->conformance_window_flag
)
795 p_sps
->conf_win
.left_offset
= bs_read_ue( p_bs
);
796 p_sps
->conf_win
.right_offset
= bs_read_ue( p_bs
);
797 p_sps
->conf_win
.top_offset
= bs_read_ue( p_bs
);
798 p_sps
->conf_win
.bottom_offset
= bs_read_ue( p_bs
);
801 p_sps
->bit_depth_luma_minus8
= bs_read_ue( p_bs
);
802 p_sps
->bit_depth_chroma_minus8
= bs_read_ue( p_bs
);
803 p_sps
->log2_max_pic_order_cnt_lsb_minus4
= bs_read_ue( p_bs
);
805 p_sps
->sps_sub_layer_ordering_info_present_flag
= bs_read1( p_bs
);
806 for( uint8_t i
=(p_sps
->sps_sub_layer_ordering_info_present_flag
? 0 : p_sps
->sps_max_sub_layers_minus1
);
807 i
<= p_sps
->sps_max_sub_layers_minus1
; i
++ )
809 p_sps
->sps_max
[i
].dec_pic_buffering_minus1
= bs_read_ue( p_bs
);
810 p_sps
->sps_max
[i
].num_reorder_pics
= bs_read_ue( p_bs
);
811 p_sps
->sps_max
[i
].latency_increase_plus1
= bs_read_ue( p_bs
);
814 if( bs_remain( p_bs
) < 4 )
817 p_sps
->log2_min_luma_coding_block_size_minus3
= bs_read_ue( p_bs
);
818 p_sps
->log2_diff_max_min_luma_coding_block_size
= bs_read_ue( p_bs
);
819 p_sps
->log2_min_luma_transform_block_size_minus2
= bs_read_ue( p_bs
);
820 if( bs_remain( p_bs
) < 1 ) /* last late fail check */
822 p_sps
->log2_diff_max_min_luma_transform_block_size
= bs_read_ue( p_bs
);
824 /* parsing incomplete */
826 p_sps
->max_transform_hierarchy_depth_inter
= bs_read_ue( p_bs
);
827 p_sps
->max_transform_hierarchy_depth_intra
= bs_read_ue( p_bs
);
828 p_sps
->scaling_list_enabled
= bs_read1( p_bs
);
829 if( p_sps
->scaling_list_enabled
)
831 p_sps
->sps_scaling_list_data_present_flag
= bs_read1( p_bs
);
832 if( p_sps
->sps_scaling_list_data_present_flag
&&
833 ! hevc_parse_scaling_list_rbsp( p_bs
) )
839 p_sps
->amp_enabled_flag
= bs_read1( p_bs
);
840 p_sps
->sample_adaptive_offset_enabled_flag
= bs_read1( p_bs
);
842 p_sps
->pcm_enabled_flag
= bs_read1( p_bs
);
843 if( p_sps
->pcm_enabled_flag
)
845 p_sps
->pcm_sample_bit_depth_luma_minus1
= bs_read( p_bs
, 4 );
846 p_sps
->pcm_sample_bit_depth_chroma_minus1
= bs_read( p_bs
, 4 );
847 p_sps
->log2_min_pcm_luma_coding_block_size_minus3
= bs_read_ue( p_bs
);
848 p_sps
->log2_diff_max_min_pcm_luma_coding_block_size
= bs_read_ue( p_bs
);
849 p_sps
->pcm_loop_filter_disabled_flag
= bs_read1( p_bs
);
852 p_sps
->num_short_term_ref_pic_sets
= bs_read_ue( p_bs
);
853 if( p_sps
->num_short_term_ref_pic_sets
> HEVC_MAX_SHORT_TERM_REF_PIC_SET
)
856 hevc_short_term_ref_pic_set_t sets
[HEVC_MAX_SHORT_TERM_REF_PIC_SET
];
857 memset(&sets
, 0, sizeof(hevc_short_term_ref_pic_set_t
) * HEVC_MAX_SHORT_TERM_REF_PIC_SET
);
858 for( unsigned int i
=0; i
<p_sps
->num_short_term_ref_pic_sets
; i
++ )
860 if( !hevc_parse_st_ref_pic_set( p_bs
, i
, p_sps
->num_short_term_ref_pic_sets
, sets
) )
864 p_sps
->long_term_ref_pics_present_flag
= bs_read1( p_bs
);
865 if( p_sps
->long_term_ref_pics_present_flag
)
867 p_sps
->num_long_term_ref_pics_sps
= bs_read_ue( p_bs
);
868 if( p_sps
->num_long_term_ref_pics_sps
> HEVC_MAX_LONG_TERM_REF_PIC_SET
)
870 for( unsigned int i
=0; i
< p_sps
->num_long_term_ref_pics_sps
; i
++ )
872 /* lt_ref_pic_poc_lsb_sps */
873 bs_skip( p_bs
, p_sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4 );
874 /* used_by_curr_pic_lt_sps_flag */
879 p_sps
->sps_temporal_mvp_enabled_flag
= bs_read1( p_bs
);
880 p_sps
->strong_intra_smoothing_enabled_flag
= bs_read1( p_bs
);
882 if( bs_remain( p_bs
) < 1 ) /* late fail */
885 p_sps
->vui_parameters_present_flag
= bs_read1( p_bs
);
886 if( p_sps
->vui_parameters_present_flag
&&
887 !hevc_parse_vui_parameters_rbsp( p_bs
, &p_sps
->vui
) )
895 void hevc_rbsp_release_sps( hevc_sequence_parameter_set_t
*p_sps
)
900 IMPL_hevc_generic_decode( hevc_decode_sps
, hevc_sequence_parameter_set_t
,
901 hevc_parse_sequence_parameter_set_rbsp
, hevc_rbsp_release_sps
)
903 static bool hevc_parse_pic_parameter_set_rbsp( bs_t
*p_bs
,
904 hevc_picture_parameter_set_t
*p_pps
)
906 if( bs_remain( p_bs
) < 1 )
908 p_pps
->pps_pic_parameter_set_id
= bs_read_ue( p_bs
);
909 if( p_pps
->pps_pic_parameter_set_id
> HEVC_PPS_ID_MAX
|| bs_remain( p_bs
) < 1 )
911 p_pps
->pps_seq_parameter_set_id
= bs_read_ue( p_bs
);
912 if( p_pps
->pps_seq_parameter_set_id
> HEVC_SPS_ID_MAX
)
914 p_pps
->dependent_slice_segments_enabled_flag
= bs_read1( p_bs
);
915 p_pps
->output_flag_present_flag
= bs_read1( p_bs
);
916 p_pps
->num_extra_slice_header_bits
= bs_read( p_bs
, 3 );
917 p_pps
->sign_data_hiding_enabled_flag
= bs_read1( p_bs
);
918 p_pps
->cabac_init_present_flag
= bs_read1( p_bs
);
920 p_pps
->num_ref_idx_l0_default_active_minus1
= bs_read_ue( p_bs
);
921 p_pps
->num_ref_idx_l1_default_active_minus1
= bs_read_ue( p_bs
);
923 p_pps
->init_qp_minus26
= bs_read_se( p_bs
);
924 p_pps
->constrained_intra_pred_flag
= bs_read1( p_bs
);
925 p_pps
->transform_skip_enabled_flag
= bs_read1( p_bs
);
926 p_pps
->cu_qp_delta_enabled_flag
= bs_read1( p_bs
);
927 if( p_pps
->cu_qp_delta_enabled_flag
)
928 p_pps
->diff_cu_qp_delta_depth
= bs_read_ue( p_bs
);
930 if( bs_remain( p_bs
) < 1 )
933 p_pps
->pps_cb_qp_offset
= bs_read_se( p_bs
);
934 p_pps
->pps_cr_qp_offset
= bs_read_se( p_bs
);
935 p_pps
->pic_slice_level_chroma_qp_offsets_present_flag
= bs_read1( p_bs
);
936 p_pps
->weighted_pred_flag
= bs_read1( p_bs
);
937 p_pps
->weighted_bipred_flag
= bs_read1( p_bs
);
938 p_pps
->transquant_bypass_enable_flag
= bs_read1( p_bs
);
939 p_pps
->tiles_enabled_flag
= bs_read1( p_bs
);
940 p_pps
->entropy_coding_sync_enabled_flag
= bs_read1( p_bs
);
942 if( p_pps
->tiles_enabled_flag
)
944 p_pps
->num_tile_columns_minus1
= bs_read_ue( p_bs
); /* TODO: validate max col/row values */
945 p_pps
->num_tile_rows_minus1
= bs_read_ue( p_bs
); /* against sps PicWidthInCtbsY */
946 p_pps
->uniform_spacing_flag
= bs_read1( p_bs
);
947 if( !p_pps
->uniform_spacing_flag
)
949 if( bs_remain( p_bs
) < (int64_t) p_pps
->num_tile_columns_minus1
+
950 p_pps
->num_tile_rows_minus1
+ 1 )
952 for( unsigned i
=0; i
< p_pps
->num_tile_columns_minus1
; i
++ )
953 (void) bs_read_ue( p_bs
);
954 for( unsigned i
=0; i
< p_pps
->num_tile_rows_minus1
; i
++ )
955 (void) bs_read_ue( p_bs
);
957 p_pps
->loop_filter_across_tiles_enabled_flag
= bs_read1( p_bs
);
960 p_pps
->pps_loop_filter_across_slices_enabled_flag
= bs_read1( p_bs
);
961 p_pps
->deblocking_filter_control_present_flag
= bs_read1( p_bs
);
962 if( p_pps
->deblocking_filter_control_present_flag
)
964 p_pps
->deblocking_filter_override_enabled_flag
= bs_read1( p_bs
);
965 p_pps
->pps_deblocking_filter_disabled_flag
= bs_read1( p_bs
);
966 if( !p_pps
->pps_deblocking_filter_disabled_flag
)
968 p_pps
->pps_beta_offset_div2
= bs_read_se( p_bs
);
969 p_pps
->pps_tc_offset_div2
= bs_read_se( p_bs
);
973 p_pps
->scaling_list_data_present_flag
= bs_read1( p_bs
);
974 if( p_pps
->scaling_list_data_present_flag
&& !hevc_parse_scaling_list_rbsp( p_bs
) )
977 p_pps
->lists_modification_present_flag
= bs_read1( p_bs
);
978 p_pps
->log2_parallel_merge_level_minus2
= bs_read_ue( p_bs
);
979 p_pps
->slice_header_extension_present_flag
= bs_read1( p_bs
);
981 if( bs_remain( p_bs
) < 1 )
984 p_pps
->pps_extension_present_flag
= bs_read1( p_bs
);
985 if( p_pps
->pps_extension_present_flag
)
987 p_pps
->pps_range_extension_flag
= bs_read1( p_bs
);
988 p_pps
->pps_multilayer_extension_flag
= bs_read1( p_bs
);
989 p_pps
->pps_3d_extension_flag
= bs_read1( p_bs
);
990 if( bs_remain( p_bs
) < 5 )
992 p_pps
->pps_extension_5bits
= bs_read( p_bs
, 5 );
998 void hevc_rbsp_release_pps( hevc_picture_parameter_set_t
*p_pps
)
1003 IMPL_hevc_generic_decode( hevc_decode_pps
, hevc_picture_parameter_set_t
,
1004 hevc_parse_pic_parameter_set_rbsp
, hevc_rbsp_release_pps
)
1006 uint8_t hevc_get_sps_vps_id( const hevc_sequence_parameter_set_t
*p_sps
)
1008 return p_sps
->sps_video_parameter_set_id
;
1011 uint8_t hevc_get_pps_sps_id( const hevc_picture_parameter_set_t
*p_pps
)
1013 return p_pps
->pps_seq_parameter_set_id
;
1016 uint8_t hevc_get_slice_pps_id( const hevc_slice_segment_header_t
*p_slice
)
1018 return p_slice
->slice_pic_parameter_set_id
;
1021 bool hevc_get_sps_profile_tier_level( const hevc_sequence_parameter_set_t
*p_sps
,
1022 uint8_t *pi_profile
, uint8_t *pi_level
)
1024 if(p_sps
->profile_tier_level
.general
.profile_idc
)
1026 *pi_profile
= p_sps
->profile_tier_level
.general
.profile_idc
;
1027 *pi_level
= p_sps
->profile_tier_level
.general_level_idc
;
1033 bool hevc_get_picture_size( const hevc_sequence_parameter_set_t
*p_sps
,
1034 unsigned *p_w
, unsigned *p_h
, unsigned *p_vw
, unsigned *p_vh
)
1036 *p_w
= *p_vw
= p_sps
->pic_width_in_luma_samples
;
1037 *p_h
= *p_vh
= p_sps
->pic_height_in_luma_samples
;
1038 if( p_sps
->conformance_window_flag
)
1040 *p_vh
-= p_sps
->conf_win
.bottom_offset
+ p_sps
->conf_win
.top_offset
;
1041 *p_vh
-= p_sps
->conf_win
.left_offset
+ p_sps
->conf_win
.right_offset
;
1046 static inline uint8_t vlc_ceil_log2( uint32_t val
)
1048 uint8_t n
= 31 - clz(val
);
1049 if (((unsigned)1 << n
) != val
)
1054 static bool hevc_get_picture_CtbsYsize( const hevc_sequence_parameter_set_t
*p_sps
, unsigned *p_w
, unsigned *p_h
)
1056 const unsigned int MinCbLog2SizeY
= p_sps
->log2_min_luma_coding_block_size_minus3
+ 3;
1057 const unsigned int CtbLog2SizeY
= MinCbLog2SizeY
+ p_sps
->log2_diff_max_min_luma_coding_block_size
;
1058 if( CtbLog2SizeY
> 31 )
1060 const unsigned int CtbSizeY
= 1 << CtbLog2SizeY
;
1061 *p_w
= (p_sps
->pic_width_in_luma_samples
- 1) / CtbSizeY
+ 1;
1062 *p_h
= (p_sps
->pic_height_in_luma_samples
- 1) / CtbSizeY
+ 1;
1066 bool hevc_get_frame_rate( const hevc_sequence_parameter_set_t
*p_sps
,
1067 hevc_video_parameter_set_t
**pp_vps
,
1068 unsigned *pi_num
, unsigned *pi_den
)
1070 if( p_sps
->vui_parameters_present_flag
&& p_sps
->vui
.vui_timing_info_present_flag
)
1072 *pi_den
= p_sps
->vui
.timing
.vui_num_units_in_tick
;
1073 *pi_num
= p_sps
->vui
.timing
.vui_time_scale
;
1074 return (*pi_den
&& *pi_num
);
1076 else if( pp_vps
&& pp_vps
[p_sps
->sps_video_parameter_set_id
] &&
1077 pp_vps
[p_sps
->sps_video_parameter_set_id
]->vps_timing_info_present_flag
)
1079 *pi_den
= pp_vps
[p_sps
->sps_video_parameter_set_id
]->vps_num_units_in_tick
;
1080 *pi_num
= pp_vps
[p_sps
->sps_video_parameter_set_id
]->vps_time_scale
;
1081 return (*pi_den
&& *pi_num
);
1086 bool hevc_get_colorimetry( const hevc_sequence_parameter_set_t
*p_sps
,
1087 video_color_primaries_t
*p_primaries
,
1088 video_transfer_func_t
*p_transfer
,
1089 video_color_space_t
*p_colorspace
,
1090 bool *p_full_range
)
1092 if( !p_sps
->vui_parameters_present_flag
)
1095 hxxx_colour_primaries_to_vlc( p_sps
->vui
.vs
.colour
.colour_primaries
);
1097 hxxx_transfer_characteristics_to_vlc( p_sps
->vui
.vs
.colour
.transfer_characteristics
);
1099 hxxx_matrix_coeffs_to_vlc( p_sps
->vui
.vs
.colour
.matrix_coeffs
);
1100 *p_full_range
= p_sps
->vui
.vs
.video_full_range_flag
;
1104 static bool hevc_parse_slice_segment_header_rbsp( bs_t
*p_bs
,
1105 pf_get_matchedxps get_matchedxps
,
1107 hevc_slice_segment_header_t
*p_sl
)
1109 hevc_sequence_parameter_set_t
*p_sps
;
1110 hevc_picture_parameter_set_t
*p_pps
;
1111 hevc_video_parameter_set_t
*p_vps
;
1113 if( bs_remain( p_bs
) < 3 )
1116 p_sl
->first_slice_segment_in_pic_flag
= bs_read1( p_bs
);
1117 if( p_sl
->nal_type
>= HEVC_NAL_BLA_W_LP
&& p_sl
->nal_type
<= HEVC_NAL_IRAP_VCL23
)
1118 p_sl
->no_output_of_prior_pics_flag
= bs_read1( p_bs
);
1119 p_sl
->slice_pic_parameter_set_id
= bs_read_ue( p_bs
);
1120 if( p_sl
->slice_pic_parameter_set_id
> HEVC_PPS_ID_MAX
|| bs_remain( p_bs
) < 1 )
1123 get_matchedxps( p_sl
->slice_pic_parameter_set_id
, priv
, &p_pps
, &p_sps
, &p_vps
);
1124 if(!p_sps
|| !p_pps
)
1127 if( !p_sl
->first_slice_segment_in_pic_flag
)
1129 if( p_pps
->dependent_slice_segments_enabled_flag
)
1130 p_sl
->dependent_slice_segment_flag
= bs_read1( p_bs
);
1133 if( !hevc_get_picture_CtbsYsize( p_sps
, &w
, &h
) )
1136 (void) bs_read( p_bs
, vlc_ceil_log2( w
* h
) ); /* slice_segment_address */
1139 if( !p_sl
->dependent_slice_segment_flag
)
1142 if( p_pps
->num_extra_slice_header_bits
> i
)
1145 bs_skip( p_bs
, 1 ); /* discardable_flag */
1148 if( p_pps
->num_extra_slice_header_bits
> i
)
1151 bs_skip( p_bs
, 1 ); /* cross_layer_bla_flag */
1154 if( i
< p_pps
->num_extra_slice_header_bits
)
1155 bs_skip( p_bs
, p_pps
->num_extra_slice_header_bits
- i
);
1157 p_sl
->slice_type
= bs_read_ue( p_bs
);
1158 if( p_sl
->slice_type
> HEVC_SLICE_TYPE_I
)
1161 if( p_pps
->output_flag_present_flag
)
1162 p_sl
->pic_output_flag
= bs_read1( p_bs
);
1165 if( p_sps
->separate_colour_plane_flag
)
1166 bs_skip( p_bs
, 2 ); /* colour_plane_id */
1168 if( p_sl
->nal_type
!= HEVC_NAL_IDR_W_RADL
&& p_sl
->nal_type
!= HEVC_NAL_IDR_N_LP
)
1169 p_sl
->pic_order_cnt_lsb
= bs_read( p_bs
, p_sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4 );
1171 p_sl
->pic_order_cnt_lsb
= 0;
1173 if( bs_remain( p_bs
) < 1 )
1179 void hevc_rbsp_release_slice_header( hevc_slice_segment_header_t
*p_sh
)
1184 hevc_slice_segment_header_t
* hevc_decode_slice_header( const uint8_t *p_buf
, size_t i_buf
, bool b_escaped
,
1185 pf_get_matchedxps get_matchedxps
, void *priv
)
1187 hevc_slice_segment_header_t
*p_sh
= calloc(1, sizeof(hevc_slice_segment_header_t
));
1191 bs_init( &bs
, p_buf
, i_buf
);
1192 unsigned i_bitflow
= 0;
1195 bs
.p_fwpriv
= &i_bitflow
;
1196 bs
.pf_forward
= hxxx_bsfw_ep3b_to_rbsp
; /* Does the emulated 3bytes conversion to rbsp */
1198 else (void) i_bitflow
;
1200 p_sh
->nal_type
= bs_read( &bs
, 6 );
1201 p_sh
->nuh_layer_id
= bs_read( &bs
, 6 );
1202 p_sh
->temporal_id
= bs_read( &bs
, 3 );
1203 if( p_sh
->nuh_layer_id
> 62 ||
1204 !hevc_parse_slice_segment_header_rbsp( &bs
, get_matchedxps
, priv
, p_sh
) )
1206 hevc_rbsp_release_slice_header( p_sh
);
1213 bool hevc_get_slice_type( const hevc_slice_segment_header_t
*p_sli
, enum hevc_slice_type_e
*pi_type
)
1215 if( !p_sli
->dependent_slice_segment_flag
)
1217 *pi_type
= p_sli
->slice_type
;
1223 bool hevc_get_profile_level(const es_format_t
*p_fmt
, uint8_t *pi_profile
,
1224 uint8_t *pi_level
, uint8_t *pi_nal_length_size
)
1226 const uint8_t *p
= (const uint8_t*)p_fmt
->p_extra
;
1227 if(p_fmt
->i_extra
< 23 || p
[0] != 1)
1230 /* HEVCDecoderConfigurationRecord */
1232 *pi_profile
= p
[1] & 0x1F;
1237 if (pi_nal_length_size
)
1238 *pi_nal_length_size
= 1 + (p
[21]&0x03);
1243 /* 8.3.1 Decoding process for POC */
1244 int hevc_compute_picture_order_count( const hevc_sequence_parameter_set_t
*p_sps
,
1245 const hevc_slice_segment_header_t
*p_slice
,
1246 hevc_poc_ctx_t
*p_ctx
)
1249 bool NoRaslOutputFlag
;
1250 bool IsIRAP
= ( p_slice
->nal_type
>= HEVC_NAL_BLA_W_LP
&&
1251 p_slice
->nal_type
<= HEVC_NAL_IRAP_VCL23
);
1255 /* if( IRAP ) NoRaslOutputFlag = first || IDR || BLA || after(EOSNAL) */
1256 NoRaslOutputFlag
=(p_ctx
->first_picture
||
1257 p_slice
->nal_type
== HEVC_NAL_IDR_N_LP
||
1258 p_slice
->nal_type
== HEVC_NAL_IDR_W_RADL
||
1259 p_slice
->nal_type
== HEVC_NAL_BLA_W_LP
||
1260 p_slice
->nal_type
== HEVC_NAL_BLA_W_RADL
||
1261 p_slice
->nal_type
== HEVC_NAL_BLA_N_LP
);
1265 NoRaslOutputFlag
= false;
1268 if( p_slice
->nal_type
== HEVC_NAL_IDR_N_LP
||
1269 p_slice
->nal_type
== HEVC_NAL_IDR_W_RADL
)
1271 p_ctx
->prevPicOrderCnt
.msb
= 0;
1272 p_ctx
->prevPicOrderCnt
.lsb
= 0;
1275 /* Not an IRAP with NoRaslOutputFlag == 1 */
1276 if( !IsIRAP
|| !NoRaslOutputFlag
)
1279 p_ctx
->prevPicOrderCnt
.msb
= p_ctx
->prevTid0PicOrderCnt
.msb
;
1280 p_ctx
->prevPicOrderCnt
.lsb
= p_ctx
->prevTid0PicOrderCnt
.lsb
;
1284 const unsigned maxPocLSB
= 1U << (p_sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4);
1285 pocMSB
= p_ctx
->prevPicOrderCnt
.msb
;
1286 int64_t orderDiff
= p_slice
->pic_order_cnt_lsb
- p_ctx
->prevPicOrderCnt
.lsb
;
1287 if( orderDiff
< 0 && -orderDiff
>= maxPocLSB
/ 2 )
1288 pocMSB
+= maxPocLSB
;
1289 else if( orderDiff
> maxPocLSB
/ 2 )
1290 pocMSB
-= maxPocLSB
;
1293 /* Set prevTid0Pic for next pic */
1294 if( p_slice
->temporal_id
== 0 &&
1295 !( ( p_slice
->nal_type
<= HEVC_NAL_RSV_VCL_N14
&& p_slice
->nal_type
% 2 == 0 /* SLNR */ ) ||
1296 ( p_slice
->nal_type
>= HEVC_NAL_RADL_N
&& p_slice
->nal_type
<= HEVC_NAL_RASL_R
) /* RADL or RASL */ ) )
1298 p_ctx
->prevTid0PicOrderCnt
.msb
= pocMSB
;
1299 p_ctx
->prevTid0PicOrderCnt
.lsb
= p_slice
->pic_order_cnt_lsb
;
1302 p_ctx
->prevPicOrderCnt
.msb
= pocMSB
;
1303 p_ctx
->prevPicOrderCnt
.lsb
= p_slice
->pic_order_cnt_lsb
;
1305 return pocMSB
+ p_slice
->pic_order_cnt_lsb
;
1308 struct hevc_sei_pic_timing_t
1310 nal_u4_t pic_struct
;
1313 void hevc_release_sei_pic_timing( hevc_sei_pic_timing_t
*p_timing
)
1318 hevc_sei_pic_timing_t
* hevc_decode_sei_pic_timing( bs_t
*p_bs
,
1319 const hevc_sequence_parameter_set_t
*p_sps
)
1321 hevc_sei_pic_timing_t
*p_timing
= malloc(sizeof(*p_timing
));
1324 if( p_sps
->vui
.frame_field_info_present_flag
)
1325 p_timing
->pic_struct
= bs_read( p_bs
, 4 );
1327 p_timing
->pic_struct
= 0;
1332 uint8_t hevc_get_num_clock_ts( const hevc_sequence_parameter_set_t
*p_sps
,
1333 const hevc_sei_pic_timing_t
*p_timing
)
1335 if( p_sps
->vui
.frame_field_info_present_flag
&& p_timing
&& p_timing
->pic_struct
< 13 )
1337 /* !WARN modified with units_field_based_flag (D.3.25) for values 0, 7 and 8 */
1338 const uint8_t rgi_numclock
[13] = { 2, 1, 1, 2, 2, 3, 3, 4, 6, 1, 1, 1, 1 };
1339 return rgi_numclock
[p_timing
->pic_struct
];
1342 if( p_sps
->vui_parameters_present_flag
)
1344 if( p_sps
->vui
.field_seq_flag
)
1345 return 1; /* D.3.27 */
1347 else if( p_sps
->profile_tier_level
.general
.interlaced_source_flag
&&
1348 !p_sps
->profile_tier_level
.general
.progressive_source_flag
)