packetizer: hevc: fix poc msb on IRAP
[vlc.git] / modules / packetizer / hevc_nal.c
blobd6bf59f1db8c82feebe487a9143a4e7c06f264ef
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 *****************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include "hevc_nal.h"
25 #include "hxxx_nal.h"
27 #include <vlc_common.h>
28 #include <vlc_bits.h>
30 #include <limits.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;
43 typedef struct
45 nal_u2_t profile_space;
46 nal_u1_t tier_flag;
47 nal_u5_t profile_idc;
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;
53 struct
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;
64 } idc4to7;
65 struct
67 nal_u1_t inbld_flag;
68 } idc1to5;
69 } hevc_inner_profile_tier_level_t;
71 #define HEVC_MAX_SUBLAYERS 8
72 typedef struct
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
85 typedef struct
87 unsigned num_delta_pocs;
88 } hevc_short_term_ref_pic_set_t;
90 typedef struct
92 nal_u1_t aspect_ratio_info_present_flag;
93 struct
95 nal_u8_t aspect_ratio_idc;
96 uint16_t sar_width;
97 uint16_t sar_height;
98 } ar;
99 nal_u1_t overscan_info_present_flag;
100 nal_u1_t overscan_appropriate_flag;
102 nal_u1_t video_signal_type_present_flag;
103 struct
105 nal_u3_t video_format;
106 nal_u1_t video_full_range_flag;
107 nal_u1_t colour_description_present_flag;
108 struct
110 nal_u8_t colour_primaries;
111 nal_u8_t transfer_characteristics;
112 nal_u8_t matrix_coeffs;
113 } colour;
114 } vs;
116 nal_u1_t chroma_loc_info_present_flag;
117 struct
119 nal_ue_t sample_loc_type_top_field;
120 nal_ue_t sample_loc_type_bottom_field;
121 } chroma;
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;
128 struct
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;
134 } def_disp;
136 nal_u1_t vui_timing_info_present_flag;
137 struct
139 uint32_t vui_num_units_in_tick;
140 uint32_t vui_time_scale;
141 /* incomplete */
142 } timing;
144 /* incomplete */
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;
159 struct
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;
174 /* incomplete */
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;
193 struct
195 nal_ue_t left_offset;
196 nal_ue_t right_offset;
197 nal_ue_t top_offset;
198 nal_ue_t bottom_offset;
199 } conf_win;
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;
206 struct
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;
218 /* incomplete */
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;
236 // st_ref_pic_set
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;
247 /* incomplete */
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;
304 /* incomplete */
308 struct hevc_slice_segment_header_t
310 nal_u6_t nal_type;
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
318 nal_ue_t slice_type;
319 nal_u1_t pic_output_flag;
321 uint32_t pic_order_cnt_lsb;
322 /* incomplete */
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 )
329 size_t i_total = 0;
331 if( i_buf < HEVC_MIN_HVCC_SIZE )
332 return 0;
334 const uint8_t i_nal_length_size = (p_buf[21] & 0x03) + 1;
335 if(i_nal_length_size == 3)
336 return 0;
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++ )
343 if(i_buf < 3)
344 return 0;
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++ )
351 if(i_buf < 2)
352 return 0;
354 const uint16_t i_nalu_length = p_buf[0] << 8 | p_buf[1];
355 if(i_buf < (size_t)i_nalu_length + 2)
356 return 0;
358 i_total += i_nalu_length + i_nal_length_size;
359 p_buf += i_nalu_length + 2;
360 i_buf -= i_nalu_length + 2;
364 return i_total;
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 )
372 return NULL;
374 if( pi_nal_length_size )
375 *pi_nal_length_size = hevc_getNALLengthSize( p_buf );
377 uint8_t *p_ret;
378 uint8_t *p_out_buf = p_ret = malloc( *pi_result );
379 if( !p_out_buf )
381 *pi_result = 0;
382 return NULL;
385 const uint8_t i_num_array = p_buf[22];
386 p_buf += 23;
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];
391 p_buf += 3;
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;
405 return p_ret;
408 static bool hevc_parse_scaling_list_rbsp( bs_t *p_bs )
410 if( bs_remain( p_bs ) < 16 )
411 return false;
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 )
418 bs_read_ue( p_bs );
419 else
421 unsigned nextCoef = 8;
422 unsigned coefNum = __MIN( 64, (1 << (4 + (i << 1))));
423 if( 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;
435 return true;
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 )
441 return false;
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 );
470 else
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 )
505 return false;
507 /* incomplete */
509 if( bs_remain( p_bs ) < 1 ) /* late fail */
510 return false;
512 return true;
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(i_buf < 3)
519 return false;
520 /* No need to lookup convert from emulation for that data */
521 uint8_t i_nal_type = hevc_getNALType(p_buf);
522 bs_t bs;
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)
528 return false;
530 else
532 *pi_id = bs_read( &bs, 4 );
533 if(i_nal_type == HEVC_NAL_SPS)
535 if(*pi_id > HEVC_SPS_ID_MAX)
536 return false;
538 else if(*pi_id > HEVC_VPS_ID_MAX)
539 return false;
541 return true;
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 )
548 return false;
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 );
573 else
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 );
582 else
583 (void) bs_read1( p_bs );
585 return true;
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 ) )
593 return false;
595 if( bs_remain( p_bs ) < 8)
596 return false;
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 )
603 return false;
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);
614 else
615 bs_read( p_bs, 2 );
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] ) )
622 return false;
624 if( p_ptl->sublayer_profile_present_flag & (0x80 >> i) )
626 if( bs_remain( p_bs ) < 8 )
627 return false;
628 p_ptl->sub_layer_level_idc[i] = bs_read( p_bs, 8 );
633 return true;
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 )
640 return false;
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 );
648 bs_skip( p_bs, 16 );
650 if( !hevc_parse_profile_tier_level_rbsp( p_bs, true, p_vps->vps_max_sub_layers_minus1,
651 &p_vps->profile_tier_level ) )
652 return false;
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 )
664 return false;
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 )
671 return false;
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 )
682 return false;
684 return true;
687 void hevc_rbsp_release_vps( hevc_video_parameter_set_t *p_vps )
689 free( 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)) \
698 bs_t bs; \
699 bs_init( &bs, p_buf, i_buf ); \
700 unsigned i_bitflow = 0; \
701 if( b_escaped ) \
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 ); \
713 p_hevctype = NULL; \
716 return 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 )
733 return false;
735 if(delta_idx_minus_1 == stRpsIdx)
736 return false;
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++;
753 else
754 p_sets[stRpsIdx].num_delta_pocs++;
757 else
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 )
762 return false;
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;
776 return true;
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 ) )
787 return false;
789 if( bs_remain( p_bs ) < 1 )
790 return false;
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 )
794 return false;
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 )
802 return false;
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 )
827 return false;
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 */
833 return false;
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 ) )
847 return false;
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 )
866 return false;
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 ) )
873 return false;
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 )
881 return false;
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 */
887 bs_skip( p_bs, 1 );
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 */
895 return false;
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 ) )
900 return false;
902 /* incomplete */
904 return true;
907 void hevc_rbsp_release_sps( hevc_sequence_parameter_set_t *p_sps )
909 free( 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 )
919 return false;
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 )
922 return false;
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 )
925 return false;
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 )
943 return false;
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 )
963 return false;
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 ) )
987 return false;
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 )
994 return false;
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 )
1003 return false;
1004 p_pps->pps_extension_5bits = bs_read( p_bs, 5 );
1007 return true;
1010 void hevc_rbsp_release_pps( hevc_picture_parameter_set_t *p_pps )
1012 free( 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;
1040 return true;
1042 return false;
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;
1055 return true;
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)
1067 n++;
1068 return n;
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 )
1076 return false;
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;
1080 return true;
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);
1099 return false;
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] =
1111 { 1, 1 },
1112 { 12, 11 },
1113 { 10, 11 },
1114 { 16, 11 },
1115 { 40, 33 },
1116 { 24, 11 },
1117 { 20, 11 },
1118 { 32, 11 },
1119 { 80, 33 },
1120 { 18, 11 },
1121 { 15, 11 },
1122 { 64, 33 },
1123 { 160, 99 },
1124 { 4, 3 },
1125 { 3, 2 },
1126 { 2, 1 },
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];
1133 return true;
1136 else
1138 *num = p_sps->vui.ar.sar_width;
1139 *den = p_sps->vui.ar.sar_height;
1140 return true;
1143 return false;
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 )
1153 return false;
1154 *p_primaries =
1155 hxxx_colour_primaries_to_vlc( p_sps->vui.vs.colour.colour_primaries );
1156 *p_transfer =
1157 hxxx_transfer_characteristics_to_vlc( p_sps->vui.vs.colour.transfer_characteristics );
1158 *p_colorspace =
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;
1161 return true;
1164 static bool hevc_parse_slice_segment_header_rbsp( bs_t *p_bs,
1165 pf_get_matchedxps get_matchedxps,
1166 void *priv,
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 )
1174 return false;
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 )
1181 return false;
1183 get_matchedxps( p_sl->slice_pic_parameter_set_id, priv, &p_pps, &p_sps, &p_vps );
1184 if(!p_sps || !p_pps)
1185 return false;
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 );
1192 unsigned w, h;
1193 if( !hevc_get_picture_CtbsYsize( p_sps, &w, &h ) )
1194 return false;
1196 (void) bs_read( p_bs, vlc_ceil_log2( w * h ) ); /* slice_segment_address */
1199 if( !p_sl->dependent_slice_segment_flag )
1201 unsigned i=0;
1202 if( p_pps->num_extra_slice_header_bits > i )
1204 i++;
1205 bs_skip( p_bs, 1 ); /* discardable_flag */
1208 if( p_pps->num_extra_slice_header_bits > i )
1210 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 )
1219 return false;
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 );
1230 else
1231 p_sl->pic_order_cnt_lsb = 0;
1233 if( bs_remain( p_bs ) < 1 )
1234 return false;
1236 return true;
1239 void hevc_rbsp_release_slice_header( hevc_slice_segment_header_t *p_sh )
1241 free( 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));
1248 if(likely(p_sh))
1250 bs_t bs;
1251 bs_init( &bs, p_buf, i_buf );
1252 unsigned i_bitflow = 0;
1253 if( b_escaped )
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;
1259 bs_skip( &bs, 1 );
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 );
1267 p_sh = NULL;
1270 return 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;
1278 return true;
1280 return false;
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)
1288 return false;
1290 /* HEVCDecoderConfigurationRecord */
1291 if(pi_profile)
1292 *pi_profile = p[1] & 0x1F;
1294 if(pi_level)
1295 *pi_level = p[12];
1297 if (pi_nal_length_size)
1298 *pi_nal_length_size = 1 + (p[21]&0x03);
1300 return true;
1304 * HEVCDecoderConfigurationRecord operations
1307 static void hevc_dcr_params_from_vps( const uint8_t * p_buffer, size_t i_buffer,
1308 struct hevc_dcr_values *p_values )
1310 if( i_buffer < 19 )
1311 return;
1313 bs_t bs;
1314 bs_init( &bs, p_buffer, i_buffer );
1315 unsigned i_bitflow = 0;
1316 bs.p_fwpriv = &i_bitflow;
1317 bs.pf_forward = hxxx_bsfw_ep3b_to_rbsp; /* Does the emulated 3bytes conversion to rbsp */
1319 /* first two bytes are the NAL header, 3rd and 4th are:
1320 vps_video_parameter_set_id(4)
1321 vps_reserved_3_2bis(2)
1322 vps_max_layers_minus1(6)
1323 vps_max_sub_layers_minus1(3)
1324 vps_temporal_id_nesting_flags
1326 bs_skip( &bs, 16 + 4 + 2 + 6 );
1327 p_values->i_numTemporalLayer = bs_read( &bs, 3 ) + 1;
1328 p_values->b_temporalIdNested = bs_read1( &bs );
1330 /* 5th & 6th are reserved 0xffff */
1331 bs_skip( &bs, 16 );
1332 /* copy the first 12 bytes of profile tier */
1333 for( unsigned i=0; i<12; i++ )
1334 p_values->general_configuration[i] = bs_read( &bs, 8 );
1337 #define HEVC_DCR_ADD_NALS(type, count, buffers, sizes) \
1338 for (uint8_t i = 0; i < count; i++) \
1340 if( i ==0 ) \
1342 *p++ = (type | (b_completeness ? 0x80 : 0)); \
1343 SetWBE( p, count ); p += 2; \
1345 SetWBE( p, sizes[i]); p += 2; \
1346 memcpy( p, buffers[i], sizes[i] ); p += sizes[i];\
1349 #define HEVC_DCR_ADD_SIZES(count, sizes) \
1350 if(count > 0) \
1352 i_total_size += 3;\
1353 for(uint8_t i=0; i<count; i++)\
1354 i_total_size += 2 + sizes[i];\
1357 /* Generate HEVCDecoderConfiguration iso/iec 14496-15 3rd edition */
1358 uint8_t * hevc_create_dcr( const struct hevc_dcr_params *p_params,
1359 uint8_t i_nal_length_size,
1360 bool b_completeness, size_t *pi_size )
1362 *pi_size = 0;
1364 if( i_nal_length_size != 1 && i_nal_length_size != 2 && i_nal_length_size != 4 )
1365 return NULL;
1367 struct hevc_dcr_values values =
1369 .general_configuration = {0},
1370 .i_numTemporalLayer = 0,
1371 .i_chroma_idc = 1,
1372 .i_bit_depth_luma_minus8 = 0,
1373 .i_bit_depth_chroma_minus8 = 0,
1374 .b_temporalIdNested = false,
1377 if( p_params->p_values != NULL )
1379 values = *p_params->p_values;
1381 else
1383 if( p_params->i_vps_count == 0 || p_params->i_sps_count == 0 )
1384 return NULL; /* required to extract info */
1386 hevc_dcr_params_from_vps( p_params->p_vps[0], p_params->rgi_vps[0], &values );
1388 hevc_sequence_parameter_set_t *p_sps =
1389 hevc_decode_sps( p_params->p_sps[0], p_params->rgi_sps[0], true );
1390 if( p_sps )
1392 values.i_chroma_idc = p_sps->chroma_format_idc;
1393 values.i_bit_depth_chroma_minus8 = p_sps->bit_depth_chroma_minus8;
1394 values.i_bit_depth_luma_minus8 = p_sps->bit_depth_luma_minus8;
1395 hevc_rbsp_release_sps( p_sps );
1399 size_t i_total_size = 1+12+2+4+2+2;
1400 HEVC_DCR_ADD_SIZES(p_params->i_vps_count, p_params->rgi_vps);
1401 HEVC_DCR_ADD_SIZES(p_params->i_sps_count, p_params->rgi_sps);
1402 HEVC_DCR_ADD_SIZES(p_params->i_pps_count, p_params->rgi_pps);
1403 HEVC_DCR_ADD_SIZES(p_params->i_sei_count, p_params->rgi_sei);
1405 uint8_t *p_data = malloc( i_total_size );
1406 if( p_data == NULL )
1407 return NULL;
1409 *pi_size = i_total_size;
1410 uint8_t *p = p_data;
1412 /* version */
1413 *p++ = 0x01;
1414 memcpy( p, values.general_configuration, 12 ); p += 12;
1415 /* Don't set min spatial segmentation */
1416 SetWBE( p, 0xF000 ); p += 2;
1417 /* Don't set parallelism type since segmentation isn't set */
1418 *p++ = 0xFC;
1419 *p++ = (0xFC | (values.i_chroma_idc & 0x03));
1420 *p++ = (0xF8 | (values.i_bit_depth_luma_minus8 & 0x07));
1421 *p++ = (0xF8 | (values.i_bit_depth_chroma_minus8 & 0x07));
1423 /* Don't set framerate */
1424 SetWBE( p, 0x0000); p += 2;
1425 /* Force NAL size of 4 bytes that replace the startcode */
1426 *p++ = ( ((values.i_numTemporalLayer & 0x07) << 3) |
1427 (values.b_temporalIdNested << 2) |
1428 (i_nal_length_size - 1) );
1429 /* total number of arrays */
1430 *p++ = !!p_params->i_vps_count + !!p_params->i_sps_count +
1431 !!p_params->i_pps_count + !!p_params->i_sei_count;
1433 /* Write NAL arrays */
1434 HEVC_DCR_ADD_NALS(HEVC_NAL_VPS, p_params->i_vps_count,
1435 p_params->p_vps, p_params->rgi_vps);
1436 HEVC_DCR_ADD_NALS(HEVC_NAL_SPS, p_params->i_sps_count,
1437 p_params->p_sps, p_params->rgi_sps);
1438 HEVC_DCR_ADD_NALS(HEVC_NAL_PPS, p_params->i_pps_count,
1439 p_params->p_pps, p_params->rgi_pps);
1440 HEVC_DCR_ADD_NALS(HEVC_NAL_PREF_SEI, p_params->i_sei_count,
1441 p_params->p_sei, p_params->rgi_sei);
1443 return p_data;
1446 #undef HEVC_DCR_ADD_NALS
1447 #undef HEVC_DCR_ADD_SIZES
1451 * 8.3.1 Decoding process for POC
1453 int hevc_compute_picture_order_count( const hevc_sequence_parameter_set_t *p_sps,
1454 const hevc_slice_segment_header_t *p_slice,
1455 hevc_poc_ctx_t *p_ctx )
1457 int pocMSB;
1458 bool NoRaslOutputFlag;
1459 bool IsIRAP = ( p_slice->nal_type >= HEVC_NAL_BLA_W_LP &&
1460 p_slice->nal_type <= HEVC_NAL_IRAP_VCL23 );
1462 if( IsIRAP )
1464 /* if( IRAP ) NoRaslOutputFlag = first || IDR || BLA || after(EOSNAL) */
1465 NoRaslOutputFlag =(p_ctx->first_picture ||
1466 p_slice->nal_type == HEVC_NAL_IDR_N_LP ||
1467 p_slice->nal_type == HEVC_NAL_IDR_W_RADL ||
1468 p_slice->nal_type == HEVC_NAL_BLA_W_LP ||
1469 p_slice->nal_type == HEVC_NAL_BLA_W_RADL ||
1470 p_slice->nal_type == HEVC_NAL_BLA_N_LP);
1472 else
1474 NoRaslOutputFlag = false;
1477 if( p_slice->nal_type == HEVC_NAL_IDR_N_LP ||
1478 p_slice->nal_type == HEVC_NAL_IDR_W_RADL )
1480 p_ctx->prevPicOrderCnt.msb = 0;
1481 p_ctx->prevPicOrderCnt.lsb = 0;
1484 /* Not an IRAP with NoRaslOutputFlag == 1 */
1485 if( !IsIRAP || !NoRaslOutputFlag )
1487 p_ctx->prevPicOrderCnt.msb = p_ctx->prevTid0PicOrderCnt.msb;
1488 p_ctx->prevPicOrderCnt.lsb = p_ctx->prevTid0PicOrderCnt.lsb;
1491 if( IsIRAP && NoRaslOutputFlag )
1493 pocMSB = 0;
1495 else
1497 const unsigned maxPocLSB = 1U << (p_sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
1498 pocMSB = p_ctx->prevPicOrderCnt.msb;
1499 int64_t orderDiff = p_slice->pic_order_cnt_lsb - p_ctx->prevPicOrderCnt.lsb;
1500 if( orderDiff < 0 && -orderDiff >= maxPocLSB / 2 )
1501 pocMSB += maxPocLSB;
1502 else if( orderDiff > maxPocLSB / 2 )
1503 pocMSB -= maxPocLSB;
1506 /* Set prevTid0Pic for next pic */
1507 if( p_slice->temporal_id == 0 &&
1508 !( ( p_slice->nal_type <= HEVC_NAL_RSV_VCL_N14 && p_slice->nal_type % 2 == 0 /* SLNR */ ) ||
1509 ( p_slice->nal_type >= HEVC_NAL_RADL_N && p_slice->nal_type <= HEVC_NAL_RASL_R ) /* RADL or RASL */ ) )
1511 p_ctx->prevTid0PicOrderCnt.msb = pocMSB;
1512 p_ctx->prevTid0PicOrderCnt.lsb = p_slice->pic_order_cnt_lsb;
1515 p_ctx->prevPicOrderCnt.msb = pocMSB;
1516 p_ctx->prevPicOrderCnt.lsb = p_slice->pic_order_cnt_lsb;
1518 return pocMSB + p_slice->pic_order_cnt_lsb;
1521 struct hevc_sei_pic_timing_t
1523 nal_u4_t pic_struct;
1524 nal_u2_t source_scan_type;
1527 void hevc_release_sei_pic_timing( hevc_sei_pic_timing_t *p_timing )
1529 free( p_timing );
1532 hevc_sei_pic_timing_t * hevc_decode_sei_pic_timing( bs_t *p_bs,
1533 const hevc_sequence_parameter_set_t *p_sps )
1535 hevc_sei_pic_timing_t *p_timing = malloc(sizeof(*p_timing));
1536 if( p_timing )
1538 if( p_sps->vui.frame_field_info_present_flag )
1540 p_timing->pic_struct = bs_read( p_bs, 4 );
1541 p_timing->source_scan_type = bs_read( p_bs, 2 );
1543 else
1545 p_timing->pic_struct = 0;
1546 p_timing->source_scan_type = 1;
1549 return p_timing;
1552 bool hevc_frame_is_progressive( const hevc_sequence_parameter_set_t *p_sps,
1553 const hevc_sei_pic_timing_t *p_timing )
1555 if( p_sps->vui_parameters_present_flag &&
1556 p_sps->vui.field_seq_flag )
1557 return false;
1559 if( p_sps->profile_tier_level.general.interlaced_source_flag &&
1560 !p_sps->profile_tier_level.general.progressive_source_flag )
1561 return false;
1563 if( p_timing && p_sps->vui.frame_field_info_present_flag )
1565 if( p_timing->source_scan_type < 2 )
1566 return p_timing->source_scan_type != 0;
1569 return true;
1572 uint8_t hevc_get_num_clock_ts( const hevc_sequence_parameter_set_t *p_sps,
1573 const hevc_sei_pic_timing_t *p_timing )
1575 if( p_sps->vui.frame_field_info_present_flag && p_timing && p_timing->pic_struct < 13 )
1577 /* !WARN modified with units_field_based_flag (D.3.25) for values 0, 7 and 8 */
1578 const uint8_t rgi_numclock[13] = { 2, 1, 1, 2, 2, 3, 3, 4, 6, 1, 1, 1, 1 };
1579 return rgi_numclock[p_timing->pic_struct];
1582 if( p_sps->vui_parameters_present_flag )
1584 if( p_sps->vui.field_seq_flag )
1585 return 1; /* D.3.27 */
1587 else if( p_sps->profile_tier_level.general.interlaced_source_flag &&
1588 !p_sps->profile_tier_level.general.progressive_source_flag )
1590 return 1;
1593 return 2;