decoder: remove unused variable
[vlc.git] / modules / packetizer / hevc_nal.c
blob309c6d0e7cbf1e8a534d15fc2886567a3048f029
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"
26 #include "hxxx_ep3b.h"
27 #include "iso_color_tables.h"
29 #include <vlc_common.h>
30 #include <vlc_bits.h>
32 #include <limits.h>
34 //#define HEVC_POC_DEBUG
36 typedef uint8_t nal_u1_t;
37 typedef uint8_t nal_u2_t;
38 typedef uint8_t nal_u3_t;
39 typedef uint8_t nal_u4_t;
40 typedef uint8_t nal_u5_t;
41 typedef uint8_t nal_u6_t;
42 typedef uint8_t nal_u7_t;
43 typedef uint8_t nal_u8_t;
44 typedef int32_t nal_se_t;
45 typedef uint32_t nal_ue_t;
47 typedef struct
49 nal_u2_t profile_space;
50 nal_u1_t tier_flag;
51 nal_u5_t profile_idc;
52 uint32_t profile_compatibility_flag; /* nal_u1_t * 32 */
53 nal_u1_t progressive_source_flag;
54 nal_u1_t interlaced_source_flag;
55 nal_u1_t non_packed_constraint_flag;
56 nal_u1_t frame_only_constraint_flag;
57 struct
59 nal_u1_t max_12bit_constraint_flag;
60 nal_u1_t max_10bit_constraint_flag;
61 nal_u1_t max_8bit_constraint_flag;
62 nal_u1_t max_422chroma_constraint_flag;
63 nal_u1_t max_420chroma_constraint_flag;
64 nal_u1_t max_monochrome_constraint_flag;
65 nal_u1_t intra_constraint_flag;
66 nal_u1_t one_picture_only_constraint_flag;
67 nal_u1_t lower_bit_rate_constraint_flag;
68 nal_u1_t max_14bit_constraint_flag;
69 } idc4to7;
70 struct
72 nal_u1_t inbld_flag;
73 } idc1to5;
74 } hevc_inner_profile_tier_level_t;
76 #define HEVC_MAX_SUBLAYERS 8
77 typedef struct
79 hevc_inner_profile_tier_level_t general;
80 nal_u8_t general_level_idc;
81 uint8_t sublayer_profile_present_flag; /* nal_u1_t * 8 */
82 uint8_t sublayer_level_present_flag; /* nal_u1_t * 8 */
83 hevc_inner_profile_tier_level_t sub_layer[HEVC_MAX_SUBLAYERS];
84 nal_u8_t sub_layer_level_idc[HEVC_MAX_SUBLAYERS];
85 } hevc_profile_tier_level_t;
87 #define HEVC_MAX_SHORT_TERM_REF_PIC_SET 65
88 #define HEVC_MAX_LONG_TERM_REF_PIC_SET 33
90 typedef struct
92 unsigned num_delta_pocs;
93 } hevc_short_term_ref_pic_set_t;
95 typedef struct
97 nal_u1_t aspect_ratio_info_present_flag;
98 struct
100 nal_u8_t aspect_ratio_idc;
101 uint16_t sar_width;
102 uint16_t sar_height;
103 } ar;
104 nal_u1_t overscan_info_present_flag;
105 nal_u1_t overscan_appropriate_flag;
107 nal_u1_t video_signal_type_present_flag;
108 struct
110 nal_u3_t video_format;
111 nal_u1_t video_full_range_flag;
112 nal_u1_t colour_description_present_flag;
113 struct
115 nal_u8_t colour_primaries;
116 nal_u8_t transfer_characteristics;
117 nal_u8_t matrix_coeffs;
118 } colour;
119 } vs;
121 nal_u1_t chroma_loc_info_present_flag;
122 struct
124 nal_ue_t sample_loc_type_top_field;
125 nal_ue_t sample_loc_type_bottom_field;
126 } chroma;
128 nal_u1_t neutral_chroma_indication_flag;
129 nal_u1_t field_seq_flag;
130 nal_u1_t frame_field_info_present_flag;
132 nal_u1_t default_display_window_flag;
133 struct
135 nal_ue_t win_left_offset;
136 nal_ue_t win_right_offset;
137 nal_ue_t win_top_offset;
138 nal_ue_t win_bottom_offset;
139 } def_disp;
141 nal_u1_t vui_timing_info_present_flag;
142 struct
144 uint32_t vui_num_units_in_tick;
145 uint32_t vui_time_scale;
146 /* incomplete */
147 } timing;
149 /* incomplete */
150 } hevc_vui_parameters_t;
152 struct hevc_video_parameter_set_t
154 nal_u4_t vps_video_parameter_set_id;
155 nal_u1_t vps_base_layer_internal_flag;
156 nal_u1_t vps_base_layer_available_flag;
157 nal_u6_t vps_max_layers_minus1;
158 nal_u3_t vps_max_sub_layers_minus1;
159 nal_u1_t vps_temporal_id_nesting_flag;
161 hevc_profile_tier_level_t profile_tier_level;
163 nal_u1_t vps_sub_layer_ordering_info_present_flag;
164 struct
166 nal_ue_t dec_pic_buffering_minus1;
167 nal_ue_t num_reorder_pics;
168 nal_ue_t max_latency_increase_plus1;
169 } vps_max[1 + HEVC_MAX_SUBLAYERS];
171 nal_u6_t vps_max_layer_id;
172 nal_ue_t vps_num_layer_set_minus1;
173 // layer_id_included_flag; read but discarded
175 nal_u1_t vps_timing_info_present_flag;
176 uint32_t vps_num_units_in_tick;
177 uint32_t vps_time_scale;
179 /* incomplete */
182 struct hevc_sequence_parameter_set_t
184 nal_u4_t sps_video_parameter_set_id;
185 nal_u3_t sps_max_sub_layers_minus1;
186 nal_u1_t sps_temporal_id_nesting_flag;
188 hevc_profile_tier_level_t profile_tier_level;
190 nal_ue_t sps_seq_parameter_set_id;
191 nal_ue_t chroma_format_idc;
192 nal_u1_t separate_colour_plane_flag;
194 nal_ue_t pic_width_in_luma_samples;
195 nal_ue_t pic_height_in_luma_samples;
197 nal_u1_t conformance_window_flag;
198 struct
200 nal_ue_t left_offset;
201 nal_ue_t right_offset;
202 nal_ue_t top_offset;
203 nal_ue_t bottom_offset;
204 } conf_win;
206 nal_ue_t bit_depth_luma_minus8;
207 nal_ue_t bit_depth_chroma_minus8;
208 nal_ue_t log2_max_pic_order_cnt_lsb_minus4;
210 nal_u1_t sps_sub_layer_ordering_info_present_flag;
211 struct
213 nal_ue_t dec_pic_buffering_minus1;
214 nal_ue_t num_reorder_pics;
215 nal_ue_t latency_increase_plus1;
216 } sps_max[1 + HEVC_MAX_SUBLAYERS];
218 nal_ue_t log2_min_luma_coding_block_size_minus3;
219 nal_ue_t log2_diff_max_min_luma_coding_block_size;
220 nal_ue_t log2_min_luma_transform_block_size_minus2;
221 nal_ue_t log2_diff_max_min_luma_transform_block_size;
223 /* incomplete */
224 nal_ue_t max_transform_hierarchy_depth_inter;
225 nal_ue_t max_transform_hierarchy_depth_intra;
226 nal_u1_t scaling_list_enabled;
227 nal_u1_t sps_scaling_list_data_present_flag;
228 // scaling_list_data; read but discarded
230 nal_u1_t amp_enabled_flag;
231 nal_u1_t sample_adaptive_offset_enabled_flag;
233 nal_u1_t pcm_enabled_flag;
234 nal_u4_t pcm_sample_bit_depth_luma_minus1;
235 nal_u4_t pcm_sample_bit_depth_chroma_minus1;
236 nal_ue_t log2_min_pcm_luma_coding_block_size_minus3;
237 nal_ue_t log2_diff_max_min_pcm_luma_coding_block_size;
238 nal_u1_t pcm_loop_filter_disabled_flag;
240 nal_ue_t num_short_term_ref_pic_sets;
241 // st_ref_pic_set
243 nal_u1_t long_term_ref_pics_present_flag;
244 nal_ue_t num_long_term_ref_pics_sps;
247 nal_u1_t sps_temporal_mvp_enabled_flag;
248 nal_u1_t strong_intra_smoothing_enabled_flag;
250 nal_u1_t vui_parameters_present_flag;
251 hevc_vui_parameters_t vui;
252 /* incomplete */
255 struct hevc_picture_parameter_set_t
257 nal_ue_t pps_pic_parameter_set_id;
258 nal_ue_t pps_seq_parameter_set_id;
259 nal_u1_t dependent_slice_segments_enabled_flag;
260 nal_u1_t output_flag_present_flag;
261 nal_u3_t num_extra_slice_header_bits;
262 nal_u1_t sign_data_hiding_enabled_flag;
263 nal_u1_t cabac_init_present_flag;
264 nal_ue_t num_ref_idx_l0_default_active_minus1;
265 nal_ue_t num_ref_idx_l1_default_active_minus1;
266 nal_se_t init_qp_minus26;
267 nal_u1_t constrained_intra_pred_flag;
268 nal_u1_t transform_skip_enabled_flag;
270 nal_u1_t cu_qp_delta_enabled_flag;
271 nal_ue_t diff_cu_qp_delta_depth;
273 nal_se_t pps_cb_qp_offset;
274 nal_se_t pps_cr_qp_offset;
275 nal_u1_t pic_slice_level_chroma_qp_offsets_present_flag;
276 nal_u1_t weighted_pred_flag;
277 nal_u1_t weighted_bipred_flag;
278 nal_u1_t transquant_bypass_enable_flag;
280 nal_u1_t tiles_enabled_flag;
281 nal_u1_t entropy_coding_sync_enabled_flag;
282 nal_ue_t num_tile_columns_minus1;
283 nal_ue_t num_tile_rows_minus1;
284 nal_u1_t uniform_spacing_flag;
285 // nal_ue_t *p_column_width_minus1; read but discarded
286 // nal_ue_t *p_row_height_minus1; read but discarded
287 nal_u1_t loop_filter_across_tiles_enabled_flag;
289 nal_u1_t pps_loop_filter_across_slices_enabled_flag;
291 nal_u1_t deblocking_filter_control_present_flag;
292 nal_u1_t deblocking_filter_override_enabled_flag;
293 nal_u1_t pps_deblocking_filter_disabled_flag;
294 nal_se_t pps_beta_offset_div2;
295 nal_se_t pps_tc_offset_div2;
297 nal_u1_t scaling_list_data_present_flag;
298 // scaling_list_data; read but discarded
300 nal_u1_t lists_modification_present_flag;
301 nal_ue_t log2_parallel_merge_level_minus2;
302 nal_u1_t slice_header_extension_present_flag;
304 nal_u1_t pps_extension_present_flag;
305 nal_u1_t pps_range_extension_flag;
306 nal_u1_t pps_multilayer_extension_flag;
307 nal_u1_t pps_3d_extension_flag;
308 nal_u5_t pps_extension_5bits;
309 /* incomplete */
313 struct hevc_slice_segment_header_t
315 nal_u6_t nal_type;
316 nal_u6_t nuh_layer_id;
317 nal_u3_t temporal_id_plus1;
318 nal_u1_t first_slice_segment_in_pic_flag;
319 nal_u1_t no_output_of_prior_pics_flag;
320 nal_ue_t slice_pic_parameter_set_id;
321 nal_u1_t dependent_slice_segment_flag;
322 // slice_segment_address; read but discarded
323 nal_ue_t slice_type;
324 nal_u1_t pic_output_flag;
326 uint32_t pic_order_cnt_lsb;
327 /* incomplete */
331 /* Computes size and does check the whole struct integrity */
332 static size_t get_hvcC_to_AnnexB_NAL_size( const uint8_t *p_buf, size_t i_buf )
334 size_t i_total = 0;
336 if( i_buf < HEVC_MIN_HVCC_SIZE )
337 return 0;
339 const uint8_t i_nal_length_size = (p_buf[21] & 0x03) + 1;
340 if(i_nal_length_size == 3)
341 return 0;
343 const uint8_t i_num_array = p_buf[22];
344 p_buf += 23; i_buf -= 23;
346 for( uint8_t i = 0; i < i_num_array; i++ )
348 if(i_buf < 3)
349 return 0;
351 const uint16_t i_num_nalu = p_buf[1] << 8 | p_buf[2];
352 p_buf += 3; i_buf -= 3;
354 for( uint16_t j = 0; j < i_num_nalu; j++ )
356 if(i_buf < 2)
357 return 0;
359 const uint16_t i_nalu_length = p_buf[0] << 8 | p_buf[1];
360 if(i_buf < (size_t)i_nalu_length + 2)
361 return 0;
363 i_total += i_nalu_length + i_nal_length_size;
364 p_buf += i_nalu_length + 2;
365 i_buf -= i_nalu_length + 2;
369 return i_total;
372 uint8_t * hevc_hvcC_to_AnnexB_NAL( const uint8_t *p_buf, size_t i_buf,
373 size_t *pi_result, uint8_t *pi_nal_length_size )
375 *pi_result = get_hvcC_to_AnnexB_NAL_size( p_buf, i_buf ); /* Does all checks */
376 if( *pi_result == 0 )
377 return NULL;
379 if( pi_nal_length_size )
380 *pi_nal_length_size = hevc_getNALLengthSize( p_buf );
382 uint8_t *p_ret;
383 uint8_t *p_out_buf = p_ret = malloc( *pi_result );
384 if( !p_out_buf )
386 *pi_result = 0;
387 return NULL;
390 const uint8_t i_num_array = p_buf[22];
391 p_buf += 23;
393 for( uint8_t i = 0; i < i_num_array; i++ )
395 const uint16_t i_num_nalu = p_buf[1] << 8 | p_buf[2];
396 p_buf += 3;
398 for( uint16_t j = 0; j < i_num_nalu; j++ )
400 const uint16_t i_nalu_length = p_buf[0] << 8 | p_buf[1];
402 memcpy( p_out_buf, annexb_startcode4, 4 );
403 memcpy( &p_out_buf[4], &p_buf[2], i_nalu_length );
405 p_out_buf += 4 + i_nalu_length;
406 p_buf += 2 + i_nalu_length;
410 return p_ret;
413 static bool hevc_parse_scaling_list_rbsp( bs_t *p_bs )
415 if( bs_remain( p_bs ) < 16 )
416 return false;
418 for( int i=0; i<4; i++ )
420 for( int j=0; j<6; j += (i == 3) ? 3 : 1 )
422 if( bs_read1( p_bs ) == 0 )
423 bs_read_ue( p_bs );
424 else
426 unsigned nextCoef = 8;
427 unsigned coefNum = __MIN( 64, (1 << (4 + (i << 1))));
428 if( i > 1 )
430 nextCoef = bs_read_se( p_bs ) + 8;
432 for( unsigned k=0; k<coefNum; k++ )
434 nextCoef = ( nextCoef + bs_read_se( p_bs ) + 256 ) % 256;
440 return true;
443 static bool hevc_parse_vui_parameters_rbsp( bs_t *p_bs, hevc_vui_parameters_t *p_vui,
444 bool b_broken )
446 if( bs_remain( p_bs ) < 10 )
447 return false;
449 p_vui->aspect_ratio_info_present_flag = bs_read1( p_bs );
450 if( p_vui->aspect_ratio_info_present_flag )
452 p_vui->ar.aspect_ratio_idc = bs_read( p_bs, 8 );
453 if( p_vui->ar.aspect_ratio_idc == 0xFF ) //HEVC_SAR__IDC_EXTENDED_SAR )
455 p_vui->ar.sar_width = bs_read( p_bs, 16 );
456 p_vui->ar.sar_height = bs_read( p_bs, 16 );
460 p_vui->overscan_info_present_flag = bs_read1( p_bs );
461 if( p_vui->overscan_info_present_flag )
462 p_vui->overscan_appropriate_flag = bs_read1( p_bs );
464 p_vui->video_signal_type_present_flag = bs_read1( p_bs );
465 if( p_vui->video_signal_type_present_flag )
467 p_vui->vs.video_format = bs_read( p_bs, 3 );
468 p_vui->vs.video_full_range_flag = bs_read1( p_bs );
469 p_vui->vs.colour_description_present_flag = bs_read1( p_bs );
470 if( p_vui->vs.colour_description_present_flag )
472 p_vui->vs.colour.colour_primaries = bs_read( p_bs, 8 );
473 p_vui->vs.colour.transfer_characteristics = bs_read( p_bs, 8 );
474 p_vui->vs.colour.matrix_coeffs = bs_read( p_bs, 8 );
476 else
478 p_vui->vs.colour.colour_primaries = ISO_23001_8_CP_UNSPECIFIED;
479 p_vui->vs.colour.transfer_characteristics = ISO_23001_8_TC_UNSPECIFIED;
480 p_vui->vs.colour.matrix_coeffs = ISO_23001_8_MC_UNSPECIFIED;
484 p_vui->chroma_loc_info_present_flag = bs_read1( p_bs );
485 if( p_vui->chroma_loc_info_present_flag )
487 p_vui->chroma.sample_loc_type_top_field = bs_read_ue( p_bs );
488 p_vui->chroma.sample_loc_type_bottom_field = bs_read_ue( p_bs );
491 p_vui->neutral_chroma_indication_flag = bs_read1( p_bs );
492 p_vui->field_seq_flag = bs_read1( p_bs );
493 p_vui->frame_field_info_present_flag = bs_read1( p_bs );
495 p_vui->default_display_window_flag = !b_broken && bs_read1( p_bs );
496 if( p_vui->default_display_window_flag )
498 p_vui->def_disp.win_left_offset = bs_read_ue( p_bs );
499 p_vui->def_disp.win_right_offset = bs_read_ue( p_bs );
500 p_vui->def_disp.win_top_offset = bs_read_ue( p_bs );
501 p_vui->def_disp.win_bottom_offset = bs_read_ue( p_bs );
504 p_vui->vui_timing_info_present_flag = bs_read1( p_bs );
505 if( p_vui->vui_timing_info_present_flag )
507 p_vui->timing.vui_num_units_in_tick = bs_read( p_bs, 32 );
508 p_vui->timing.vui_time_scale = bs_read( p_bs, 32 );
510 if( bs_remain( p_bs ) < 3 )
511 return false;
513 /* incomplete */
515 if( bs_remain( p_bs ) < 1 ) /* late fail */
516 return false;
518 return true;
521 /* Shortcut for retrieving vps/sps/pps id */
522 bool hevc_get_xps_id(const uint8_t *p_buf, size_t i_buf, uint8_t *pi_id)
524 if(i_buf < 3)
525 return false;
526 /* No need to lookup convert from emulation for that data */
527 uint8_t i_nal_type = hevc_getNALType(p_buf);
528 bs_t bs;
529 bs_init(&bs, &p_buf[2], i_buf - 2);
530 if(i_nal_type == HEVC_NAL_PPS)
532 *pi_id = bs_read_ue( &bs );
533 if(*pi_id > HEVC_PPS_ID_MAX)
534 return false;
536 else
538 *pi_id = bs_read( &bs, 4 );
539 if(i_nal_type == HEVC_NAL_SPS)
541 if(*pi_id > HEVC_SPS_ID_MAX)
542 return false;
544 else if(*pi_id > HEVC_VPS_ID_MAX)
545 return false;
547 return true;
550 static bool hevc_parse_inner_profile_tier_level_rbsp( bs_t *p_bs,
551 hevc_inner_profile_tier_level_t *p_in )
553 if( bs_remain( p_bs ) < 88 )
554 return false;
556 p_in->profile_space = bs_read( p_bs, 2 );
557 p_in->tier_flag = bs_read1( p_bs );
558 p_in->profile_idc = bs_read( p_bs, 5 );
559 p_in->profile_compatibility_flag = bs_read( p_bs, 32 );
560 p_in->progressive_source_flag = bs_read1( p_bs );
561 p_in->interlaced_source_flag = bs_read1( p_bs );
562 p_in->non_packed_constraint_flag = bs_read1( p_bs );
563 p_in->frame_only_constraint_flag = bs_read1( p_bs );
565 if( ( p_in->profile_idc >= 4 && p_in->profile_idc <= 10 ) ||
566 ( p_in->profile_compatibility_flag & 0x0F700000 ) )
568 p_in->idc4to7.max_12bit_constraint_flag = bs_read1( p_bs );
569 p_in->idc4to7.max_10bit_constraint_flag = bs_read1( p_bs );
570 p_in->idc4to7.max_8bit_constraint_flag = bs_read1( p_bs );
571 p_in->idc4to7.max_422chroma_constraint_flag = bs_read1( p_bs );
572 p_in->idc4to7.max_420chroma_constraint_flag = bs_read1( p_bs );
573 p_in->idc4to7.max_monochrome_constraint_flag = bs_read1( p_bs );
574 p_in->idc4to7.intra_constraint_flag = bs_read1( p_bs );
575 p_in->idc4to7.one_picture_only_constraint_flag = bs_read1( p_bs );
576 p_in->idc4to7.lower_bit_rate_constraint_flag = bs_read1( p_bs );
577 if( p_in->profile_idc == 5 ||
578 p_in->profile_idc == 9 ||
579 p_in->profile_idc == 10 ||
580 (p_in->profile_compatibility_flag & 0x08600000) )
582 p_in->idc4to7.max_14bit_constraint_flag = bs_read1( p_bs );
583 bs_skip( p_bs, 33 );
585 else bs_skip( p_bs, 34 );
587 else if( p_in->profile_idc == 2 ||
588 (p_in->profile_compatibility_flag & 0x20000000) )
590 bs_skip( p_bs, 7 );
591 p_in->idc4to7.one_picture_only_constraint_flag = bs_read1( p_bs );
592 bs_skip( p_bs, 35 );
594 else
596 bs_read( p_bs, 43 );
599 if( ( p_in->profile_idc >= 1 && p_in->profile_idc <= 5 ) ||
600 p_in->profile_idc == 9 ||
601 ( p_in->profile_compatibility_flag & 0x7C400000 ) )
602 p_in->idc1to5.inbld_flag = bs_read1( p_bs );
603 else
604 bs_skip( p_bs, 1 );
606 return true;
609 static bool hevc_parse_profile_tier_level_rbsp( bs_t *p_bs, bool profile_present,
610 uint8_t max_num_sub_layers_minus1,
611 hevc_profile_tier_level_t *p_ptl )
613 if( profile_present && !hevc_parse_inner_profile_tier_level_rbsp( p_bs, &p_ptl->general ) )
614 return false;
616 if( bs_remain( p_bs ) < 8)
617 return false;
619 p_ptl->general_level_idc = bs_read( p_bs, 8 );
621 if( max_num_sub_layers_minus1 > 0 )
623 if( bs_remain( p_bs ) < 16 )
624 return false;
626 for( uint8_t i=0; i< 8; i++ )
628 if( i < max_num_sub_layers_minus1 )
630 if( bs_read1( p_bs ) )
631 p_ptl->sublayer_profile_present_flag |= (0x80 >> i);
632 if( bs_read1( p_bs ) )
633 p_ptl->sublayer_level_present_flag |= (0x80 >> i);
635 else
636 bs_read( p_bs, 2 );
639 for( uint8_t i=0; i < max_num_sub_layers_minus1; i++ )
641 if( ( p_ptl->sublayer_profile_present_flag & (0x80 >> i) ) &&
642 ! hevc_parse_inner_profile_tier_level_rbsp( p_bs, &p_ptl->sub_layer[i] ) )
643 return false;
645 if( p_ptl->sublayer_profile_present_flag & (0x80 >> i) )
647 if( bs_remain( p_bs ) < 8 )
648 return false;
649 p_ptl->sub_layer_level_idc[i] = bs_read( p_bs, 8 );
654 return true;
657 static bool hevc_parse_video_parameter_set_rbsp( bs_t *p_bs,
658 hevc_video_parameter_set_t *p_vps )
660 if( bs_remain( p_bs ) < 134 )
661 return false;
663 p_vps->vps_video_parameter_set_id = bs_read( p_bs, 4 );
664 p_vps->vps_base_layer_internal_flag = bs_read1( p_bs );
665 p_vps->vps_base_layer_available_flag = bs_read1( p_bs );
666 p_vps->vps_max_layers_minus1 = bs_read( p_bs, 6 );
667 p_vps->vps_max_sub_layers_minus1 = bs_read( p_bs, 3 );
668 p_vps->vps_temporal_id_nesting_flag = bs_read1( p_bs );
669 bs_skip( p_bs, 16 );
671 if( !hevc_parse_profile_tier_level_rbsp( p_bs, true, p_vps->vps_max_sub_layers_minus1,
672 &p_vps->profile_tier_level ) )
673 return false;
675 p_vps->vps_sub_layer_ordering_info_present_flag = bs_read1( p_bs );
676 for( unsigned i= (p_vps->vps_sub_layer_ordering_info_present_flag ?
677 0 : p_vps->vps_max_sub_layers_minus1);
678 i<= p_vps->vps_max_sub_layers_minus1; i++ )
680 p_vps->vps_max[i].dec_pic_buffering_minus1 = bs_read_ue( p_bs );
681 p_vps->vps_max[i].num_reorder_pics = bs_read_ue( p_bs );
682 p_vps->vps_max[i].max_latency_increase_plus1 = bs_read_ue( p_bs );
684 if( bs_remain( p_bs ) < 10 )
685 return false;
687 p_vps->vps_max_layer_id = bs_read( p_bs, 6 );
688 p_vps->vps_num_layer_set_minus1 = bs_read_ue( p_bs );
689 // layer_id_included_flag; read but discarded
690 bs_skip( p_bs, p_vps->vps_num_layer_set_minus1 * (p_vps->vps_max_layer_id + 1) );
691 if( bs_remain( p_bs ) < 2 )
692 return false;
694 p_vps->vps_timing_info_present_flag = bs_read1( p_bs );
695 if( p_vps->vps_timing_info_present_flag )
697 p_vps->vps_num_units_in_tick = bs_read( p_bs, 32 );
698 p_vps->vps_time_scale = bs_read( p_bs, 32 );
700 /* parsing incomplete */
702 if( bs_remain( p_bs ) < 1 )
703 return false;
705 return true;
708 void hevc_rbsp_release_vps( hevc_video_parameter_set_t *p_vps )
710 free( p_vps );
713 #define IMPL_hevc_generic_decode( name, hevctype, decode, release ) \
714 hevctype * name( const uint8_t *p_buf, size_t i_buf, bool b_escaped ) \
716 hevctype *p_hevctype = calloc(1, sizeof(hevctype)); \
717 if(likely(p_hevctype)) \
719 bs_t bs; \
720 struct hxxx_bsfw_ep3b_ctx_s bsctx; \
721 if( b_escaped ) \
723 hxxx_bsfw_ep3b_ctx_init( &bsctx ); \
724 bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );\
726 else bs_init( &bs, p_buf, i_buf ); \
727 bs_skip( &bs, 7 ); /* nal_unit_header */ \
728 uint8_t i_nuh_layer_id = bs_read( &bs, 6 ); \
729 bs_skip( &bs, 3 ); /* !nal_unit_header */ \
730 if( i_nuh_layer_id > 62 || !decode( &bs, p_hevctype ) ) \
732 release( p_hevctype ); \
733 p_hevctype = NULL; \
736 return p_hevctype; \
739 IMPL_hevc_generic_decode( hevc_decode_vps, hevc_video_parameter_set_t,
740 hevc_parse_video_parameter_set_rbsp, hevc_rbsp_release_vps )
742 static bool hevc_parse_st_ref_pic_set( bs_t *p_bs, unsigned stRpsIdx,
743 unsigned num_short_term_ref_pic_sets,
744 hevc_short_term_ref_pic_set_t *p_sets )
746 if( stRpsIdx && bs_read1( p_bs ) ) /* Interref pic set prediction flag */
748 nal_ue_t delta_idx_minus_1 = 0;
749 if( stRpsIdx == num_short_term_ref_pic_sets )
751 delta_idx_minus_1 = bs_read_ue( p_bs );
752 if( delta_idx_minus_1 >= stRpsIdx )
753 return false;
755 if(delta_idx_minus_1 == stRpsIdx)
756 return false;
758 nal_u1_t delta_rps_sign = bs_read1( p_bs );
759 nal_ue_t abs_delta_rps_minus1 = bs_read_ue( p_bs );
760 unsigned RefRpsIdx = stRpsIdx - delta_idx_minus_1 - 1;
761 int deltaRps = ( 1 - ( delta_rps_sign << 1 ) ) * ( abs_delta_rps_minus1 + 1 );
762 VLC_UNUSED(deltaRps);
764 unsigned numDeltaPocs = p_sets[RefRpsIdx].num_delta_pocs;
765 p_sets[stRpsIdx].num_delta_pocs = 0;
766 for( unsigned j=0; j<= numDeltaPocs; j++ )
768 if( ! bs_read1( p_bs ) ) /* used_by_curr_pic_flag */
770 if( bs_read1( p_bs ) ) /* use_delta_flag */
771 p_sets[stRpsIdx].num_delta_pocs++;
773 else
774 p_sets[stRpsIdx].num_delta_pocs++;
777 else
779 nal_ue_t num_negative_pics = bs_read_ue( p_bs );
780 nal_ue_t num_positive_pics = bs_read_ue( p_bs );
781 if( bs_remain( p_bs ) < (num_negative_pics + num_positive_pics) * 2 )
782 return false;
783 for(unsigned int i=0; i<num_negative_pics; i++)
785 (void) bs_read_ue( p_bs ); /* delta_poc_s0_minus1 */
786 (void) bs_read1( p_bs ); /* used_by_current_pic_s0_flag */
788 for(unsigned int i=0; i<num_positive_pics; i++)
790 (void) bs_read_ue( p_bs ); /* delta_poc_s1_minus1 */
791 (void) bs_read1( p_bs ); /* used_by_current_pic_s1_flag */
793 p_sets[stRpsIdx].num_delta_pocs = num_positive_pics + num_negative_pics;
796 return true;
799 static bool hevc_parse_sequence_parameter_set_rbsp( bs_t *p_bs,
800 hevc_sequence_parameter_set_t *p_sps )
802 p_sps->sps_video_parameter_set_id = bs_read( p_bs, 4 );
803 p_sps->sps_max_sub_layers_minus1 = bs_read( p_bs, 3 );
804 p_sps->sps_temporal_id_nesting_flag = bs_read1( p_bs );
805 if( !hevc_parse_profile_tier_level_rbsp( p_bs, true, p_sps->sps_max_sub_layers_minus1,
806 &p_sps->profile_tier_level ) )
807 return false;
809 if( bs_remain( p_bs ) < 1 )
810 return false;
812 p_sps->sps_seq_parameter_set_id = bs_read_ue( p_bs );
813 if( p_sps->sps_seq_parameter_set_id > HEVC_SPS_ID_MAX )
814 return false;
816 p_sps->chroma_format_idc = bs_read_ue( p_bs );
817 if( p_sps->chroma_format_idc == 3 )
818 p_sps->separate_colour_plane_flag = bs_read1( p_bs );
819 p_sps->pic_width_in_luma_samples = bs_read_ue( p_bs );
820 p_sps->pic_height_in_luma_samples = bs_read_ue( p_bs );
821 if( !p_sps->pic_width_in_luma_samples || !p_sps->pic_height_in_luma_samples )
822 return false;
824 p_sps->conformance_window_flag = bs_read1( p_bs );
825 if( p_sps->conformance_window_flag )
827 p_sps->conf_win.left_offset = bs_read_ue( p_bs );
828 p_sps->conf_win.right_offset = bs_read_ue( p_bs );
829 p_sps->conf_win.top_offset = bs_read_ue( p_bs );
830 p_sps->conf_win.bottom_offset = bs_read_ue( p_bs );
833 p_sps->bit_depth_luma_minus8 = bs_read_ue( p_bs );
834 p_sps->bit_depth_chroma_minus8 = bs_read_ue( p_bs );
835 p_sps->log2_max_pic_order_cnt_lsb_minus4 = bs_read_ue( p_bs );
837 p_sps->sps_sub_layer_ordering_info_present_flag = bs_read1( p_bs );
838 for( uint8_t i=(p_sps->sps_sub_layer_ordering_info_present_flag ? 0 : p_sps->sps_max_sub_layers_minus1);
839 i <= p_sps->sps_max_sub_layers_minus1; i++ )
841 p_sps->sps_max[i].dec_pic_buffering_minus1 = bs_read_ue( p_bs );
842 p_sps->sps_max[i].num_reorder_pics = bs_read_ue( p_bs );
843 p_sps->sps_max[i].latency_increase_plus1 = bs_read_ue( p_bs );
846 if( bs_remain( p_bs ) < 4 )
847 return false;
849 p_sps->log2_min_luma_coding_block_size_minus3 = bs_read_ue( p_bs );
850 p_sps->log2_diff_max_min_luma_coding_block_size = bs_read_ue( p_bs );
851 p_sps->log2_min_luma_transform_block_size_minus2 = bs_read_ue( p_bs );
852 if( bs_remain( p_bs ) < 1 ) /* last late fail check */
853 return false;
854 p_sps->log2_diff_max_min_luma_transform_block_size = bs_read_ue( p_bs );
856 /* parsing incomplete */
858 p_sps->max_transform_hierarchy_depth_inter = bs_read_ue( p_bs );
859 p_sps->max_transform_hierarchy_depth_intra = bs_read_ue( p_bs );
860 p_sps->scaling_list_enabled = bs_read1( p_bs );
861 if( p_sps->scaling_list_enabled )
863 p_sps->sps_scaling_list_data_present_flag = bs_read1( p_bs );
864 if( p_sps->sps_scaling_list_data_present_flag &&
865 ! hevc_parse_scaling_list_rbsp( p_bs ) )
867 return false;
871 p_sps->amp_enabled_flag = bs_read1( p_bs );
872 p_sps->sample_adaptive_offset_enabled_flag = bs_read1( p_bs );
874 p_sps->pcm_enabled_flag = bs_read1( p_bs );
875 if( p_sps->pcm_enabled_flag )
877 p_sps->pcm_sample_bit_depth_luma_minus1 = bs_read( p_bs, 4 );
878 p_sps->pcm_sample_bit_depth_chroma_minus1 = bs_read( p_bs, 4 );
879 p_sps->log2_min_pcm_luma_coding_block_size_minus3 = bs_read_ue( p_bs );
880 p_sps->log2_diff_max_min_pcm_luma_coding_block_size = bs_read_ue( p_bs );
881 p_sps->pcm_loop_filter_disabled_flag = bs_read1( p_bs );
884 p_sps->num_short_term_ref_pic_sets = bs_read_ue( p_bs );
885 if( p_sps->num_short_term_ref_pic_sets > HEVC_MAX_SHORT_TERM_REF_PIC_SET )
886 return false;
888 hevc_short_term_ref_pic_set_t sets[HEVC_MAX_SHORT_TERM_REF_PIC_SET];
889 memset(&sets, 0, sizeof(hevc_short_term_ref_pic_set_t) * HEVC_MAX_SHORT_TERM_REF_PIC_SET);
890 for( unsigned int i=0; i<p_sps->num_short_term_ref_pic_sets; i++ )
892 if( !hevc_parse_st_ref_pic_set( p_bs, i, p_sps->num_short_term_ref_pic_sets, sets ) )
893 return false;
896 p_sps->long_term_ref_pics_present_flag = bs_read1( p_bs );
897 if( p_sps->long_term_ref_pics_present_flag )
899 p_sps->num_long_term_ref_pics_sps = bs_read_ue( p_bs );
900 if( p_sps->num_long_term_ref_pics_sps > HEVC_MAX_LONG_TERM_REF_PIC_SET )
901 return false;
902 for( unsigned int i=0; i< p_sps->num_long_term_ref_pics_sps; i++ )
904 /* lt_ref_pic_poc_lsb_sps */
905 bs_skip( p_bs, p_sps->log2_max_pic_order_cnt_lsb_minus4 + 4 );
906 /* used_by_curr_pic_lt_sps_flag */
907 bs_skip( p_bs, 1 );
911 p_sps->sps_temporal_mvp_enabled_flag = bs_read1( p_bs );
912 p_sps->strong_intra_smoothing_enabled_flag = bs_read1( p_bs );
914 if( bs_remain( p_bs ) < 1 ) /* late fail */
915 return false;
917 p_sps->vui_parameters_present_flag = bs_read1( p_bs );
918 if( p_sps->vui_parameters_present_flag )
920 bs_t rollbackpoint = *p_bs;
921 if( !hevc_parse_vui_parameters_rbsp( p_bs, &p_sps->vui, false ) &&
922 p_sps->vui.default_display_window_flag &&
923 bs_remain( p_bs ) < 66 )
925 /* Broken MKV SPS vui bitstreams with missing display_window bits.
926 * Forced to accept it since some decided to accept it...
927 * see https://trac.ffmpeg.org/ticket/6644
928 * Might break decoders since cropping & clock rate have totally
929 * funky values when decoded properly */
930 *p_bs = rollbackpoint;
931 memset( &p_sps->vui, 0, sizeof(p_sps->vui) );
932 if( !hevc_parse_vui_parameters_rbsp( p_bs, &p_sps->vui, true ) )
933 return false;
937 /* incomplete */
939 return true;
942 void hevc_rbsp_release_sps( hevc_sequence_parameter_set_t *p_sps )
944 free( p_sps );
947 IMPL_hevc_generic_decode( hevc_decode_sps, hevc_sequence_parameter_set_t,
948 hevc_parse_sequence_parameter_set_rbsp, hevc_rbsp_release_sps )
950 static bool hevc_parse_pic_parameter_set_rbsp( bs_t *p_bs,
951 hevc_picture_parameter_set_t *p_pps )
953 if( bs_remain( p_bs ) < 1 )
954 return false;
955 p_pps->pps_pic_parameter_set_id = bs_read_ue( p_bs );
956 if( p_pps->pps_pic_parameter_set_id > HEVC_PPS_ID_MAX || bs_remain( p_bs ) < 1 )
957 return false;
958 p_pps->pps_seq_parameter_set_id = bs_read_ue( p_bs );
959 if( p_pps->pps_seq_parameter_set_id > HEVC_SPS_ID_MAX )
960 return false;
961 p_pps->dependent_slice_segments_enabled_flag = bs_read1( p_bs );
962 p_pps->output_flag_present_flag = bs_read1( p_bs );
963 p_pps->num_extra_slice_header_bits = bs_read( p_bs, 3 );
964 p_pps->sign_data_hiding_enabled_flag = bs_read1( p_bs );
965 p_pps->cabac_init_present_flag = bs_read1( p_bs );
967 p_pps->num_ref_idx_l0_default_active_minus1 = bs_read_ue( p_bs );
968 p_pps->num_ref_idx_l1_default_active_minus1 = bs_read_ue( p_bs );
970 p_pps->init_qp_minus26 = bs_read_se( p_bs );
971 p_pps->constrained_intra_pred_flag = bs_read1( p_bs );
972 p_pps->transform_skip_enabled_flag = bs_read1( p_bs );
973 p_pps->cu_qp_delta_enabled_flag = bs_read1( p_bs );
974 if( p_pps->cu_qp_delta_enabled_flag )
975 p_pps->diff_cu_qp_delta_depth = bs_read_ue( p_bs );
977 if( bs_remain( p_bs ) < 1 )
978 return false;
980 p_pps->pps_cb_qp_offset = bs_read_se( p_bs );
981 p_pps->pps_cr_qp_offset = bs_read_se( p_bs );
982 p_pps->pic_slice_level_chroma_qp_offsets_present_flag = bs_read1( p_bs );
983 p_pps->weighted_pred_flag = bs_read1( p_bs );
984 p_pps->weighted_bipred_flag = bs_read1( p_bs );
985 p_pps->transquant_bypass_enable_flag = bs_read1( p_bs );
986 p_pps->tiles_enabled_flag = bs_read1( p_bs );
987 p_pps->entropy_coding_sync_enabled_flag = bs_read1( p_bs );
989 if( p_pps->tiles_enabled_flag )
991 p_pps->num_tile_columns_minus1 = bs_read_ue( p_bs ); /* TODO: validate max col/row values */
992 p_pps->num_tile_rows_minus1 = bs_read_ue( p_bs ); /* against sps PicWidthInCtbsY */
993 p_pps->uniform_spacing_flag = bs_read1( p_bs );
994 if( !p_pps->uniform_spacing_flag )
996 if( bs_remain( p_bs ) < p_pps->num_tile_columns_minus1 +
997 p_pps->num_tile_rows_minus1 + 1 )
998 return false;
999 for( unsigned i=0; i< p_pps->num_tile_columns_minus1; i++ )
1000 (void) bs_read_ue( p_bs );
1001 for( unsigned i=0; i< p_pps->num_tile_rows_minus1; i++ )
1002 (void) bs_read_ue( p_bs );
1004 p_pps->loop_filter_across_tiles_enabled_flag = bs_read1( p_bs );
1007 p_pps->pps_loop_filter_across_slices_enabled_flag = bs_read1( p_bs );
1008 p_pps->deblocking_filter_control_present_flag = bs_read1( p_bs );
1009 if( p_pps->deblocking_filter_control_present_flag )
1011 p_pps->deblocking_filter_override_enabled_flag = bs_read1( p_bs );
1012 p_pps->pps_deblocking_filter_disabled_flag = bs_read1( p_bs );
1013 if( !p_pps->pps_deblocking_filter_disabled_flag )
1015 p_pps->pps_beta_offset_div2 = bs_read_se( p_bs );
1016 p_pps->pps_tc_offset_div2 = bs_read_se( p_bs );
1020 p_pps->scaling_list_data_present_flag = bs_read1( p_bs );
1021 if( p_pps->scaling_list_data_present_flag && !hevc_parse_scaling_list_rbsp( p_bs ) )
1022 return false;
1024 p_pps->lists_modification_present_flag = bs_read1( p_bs );
1025 p_pps->log2_parallel_merge_level_minus2 = bs_read_ue( p_bs );
1026 p_pps->slice_header_extension_present_flag = bs_read1( p_bs );
1028 if( bs_remain( p_bs ) < 1 )
1029 return false;
1031 p_pps->pps_extension_present_flag = bs_read1( p_bs );
1032 if( p_pps->pps_extension_present_flag )
1034 p_pps->pps_range_extension_flag = bs_read1( p_bs );
1035 p_pps->pps_multilayer_extension_flag = bs_read1( p_bs );
1036 p_pps->pps_3d_extension_flag = bs_read1( p_bs );
1037 if( bs_remain( p_bs ) < 5 )
1038 return false;
1039 p_pps->pps_extension_5bits = bs_read( p_bs, 5 );
1042 return true;
1045 void hevc_rbsp_release_pps( hevc_picture_parameter_set_t *p_pps )
1047 free( p_pps );
1050 IMPL_hevc_generic_decode( hevc_decode_pps, hevc_picture_parameter_set_t,
1051 hevc_parse_pic_parameter_set_rbsp, hevc_rbsp_release_pps )
1053 uint8_t hevc_get_sps_vps_id( const hevc_sequence_parameter_set_t *p_sps )
1055 return p_sps->sps_video_parameter_set_id;
1058 uint8_t hevc_get_pps_sps_id( const hevc_picture_parameter_set_t *p_pps )
1060 return p_pps->pps_seq_parameter_set_id;
1063 uint8_t hevc_get_slice_pps_id( const hevc_slice_segment_header_t *p_slice )
1065 return p_slice->slice_pic_parameter_set_id;
1068 bool hevc_get_sps_profile_tier_level( const hevc_sequence_parameter_set_t *p_sps,
1069 uint8_t *pi_profile, uint8_t *pi_level)
1071 if(p_sps->profile_tier_level.general.profile_idc)
1073 *pi_profile = p_sps->profile_tier_level.general.profile_idc;
1074 *pi_level = p_sps->profile_tier_level.general_level_idc;
1075 return true;
1077 return false;
1080 bool hevc_get_picture_size( const hevc_sequence_parameter_set_t *p_sps,
1081 unsigned *p_w, unsigned *p_h, unsigned *p_vw, unsigned *p_vh )
1083 *p_w = *p_vw = p_sps->pic_width_in_luma_samples;
1084 *p_h = *p_vh = p_sps->pic_height_in_luma_samples;
1085 if( p_sps->conformance_window_flag )
1087 unsigned sub_width_c, sub_height_c;
1089 if( p_sps->chroma_format_idc == 1 )
1091 sub_width_c = 2;
1092 sub_height_c = 2;
1094 else if( p_sps->chroma_format_idc == 2 )
1096 sub_width_c = 2;
1097 sub_height_c = 1;
1099 else
1101 sub_width_c = 1;
1102 sub_height_c = 1;
1105 *p_vh -= (p_sps->conf_win.bottom_offset + p_sps->conf_win.top_offset) * sub_height_c;
1106 *p_vw -= (p_sps->conf_win.left_offset + p_sps->conf_win.right_offset) * sub_width_c;
1108 return true;
1111 uint8_t hevc_get_max_num_reorder( const hevc_video_parameter_set_t *p_vps )
1113 return p_vps->vps_max[p_vps->vps_max_sub_layers_minus1/* HighestTid */].num_reorder_pics;
1116 static inline uint8_t vlc_ceil_log2( uint32_t val )
1118 uint8_t n = 31 - clz(val);
1119 if (((unsigned)1 << n) != val)
1120 n++;
1121 return n;
1124 static bool hevc_get_picture_CtbsYsize( const hevc_sequence_parameter_set_t *p_sps, unsigned *p_w, unsigned *p_h )
1126 const unsigned int MinCbLog2SizeY = p_sps->log2_min_luma_coding_block_size_minus3 + 3;
1127 const unsigned int CtbLog2SizeY = MinCbLog2SizeY + p_sps->log2_diff_max_min_luma_coding_block_size;
1128 if( CtbLog2SizeY > 31 )
1129 return false;
1130 const unsigned int CtbSizeY = 1 << CtbLog2SizeY;
1131 *p_w = (p_sps->pic_width_in_luma_samples - 1) / CtbSizeY + 1;
1132 *p_h = (p_sps->pic_height_in_luma_samples - 1) / CtbSizeY + 1;
1133 return true;
1136 bool hevc_get_frame_rate( const hevc_sequence_parameter_set_t *p_sps,
1137 const hevc_video_parameter_set_t *p_vps,
1138 unsigned *pi_num, unsigned *pi_den )
1140 if( p_sps->vui_parameters_present_flag && p_sps->vui.vui_timing_info_present_flag )
1142 *pi_den = p_sps->vui.timing.vui_num_units_in_tick;
1143 *pi_num = p_sps->vui.timing.vui_time_scale;
1144 return (*pi_den && *pi_num);
1146 else if( p_vps && p_vps->vps_timing_info_present_flag )
1148 *pi_den = p_vps->vps_num_units_in_tick;
1149 *pi_num = p_vps->vps_time_scale;
1150 return (*pi_den && *pi_num);
1152 return false;
1155 bool hevc_get_aspect_ratio( const hevc_sequence_parameter_set_t *p_sps,
1156 unsigned *num, unsigned *den )
1158 if( p_sps->vui_parameters_present_flag )
1160 if( p_sps->vui.ar.aspect_ratio_idc != 255 )
1162 static const uint8_t ar_table[16][2] =
1164 { 1, 1 },
1165 { 12, 11 },
1166 { 10, 11 },
1167 { 16, 11 },
1168 { 40, 33 },
1169 { 24, 11 },
1170 { 20, 11 },
1171 { 32, 11 },
1172 { 80, 33 },
1173 { 18, 11 },
1174 { 15, 11 },
1175 { 64, 33 },
1176 { 160, 99 },
1177 { 4, 3 },
1178 { 3, 2 },
1179 { 2, 1 },
1181 if( p_sps->vui.ar.aspect_ratio_idc > 0 &&
1182 p_sps->vui.ar.aspect_ratio_idc < 17 )
1184 *num = ar_table[p_sps->vui.ar.aspect_ratio_idc - 1][0];
1185 *den = ar_table[p_sps->vui.ar.aspect_ratio_idc - 1][1];
1186 return true;
1189 else
1191 *num = p_sps->vui.ar.sar_width;
1192 *den = p_sps->vui.ar.sar_height;
1193 return true;
1196 return false;
1199 bool hevc_get_chroma_luma( const hevc_sequence_parameter_set_t *p_sps, uint8_t *pi_chroma_format,
1200 uint8_t *pi_depth_luma, uint8_t *pi_depth_chroma )
1202 *pi_chroma_format = p_sps->chroma_format_idc;
1203 *pi_depth_luma = p_sps->bit_depth_chroma_minus8 + 8;
1204 *pi_depth_chroma = p_sps->bit_depth_chroma_minus8 + 8;
1205 return true;
1208 bool hevc_get_colorimetry( const hevc_sequence_parameter_set_t *p_sps,
1209 video_color_primaries_t *p_primaries,
1210 video_transfer_func_t *p_transfer,
1211 video_color_space_t *p_colorspace,
1212 video_color_range_t *p_full_range )
1214 if( !p_sps->vui_parameters_present_flag )
1215 return false;
1216 *p_primaries =
1217 iso_23001_8_cp_to_vlc_primaries( p_sps->vui.vs.colour.colour_primaries );
1218 *p_transfer =
1219 iso_23001_8_tc_to_vlc_xfer( p_sps->vui.vs.colour.transfer_characteristics );
1220 *p_colorspace =
1221 iso_23001_8_mc_to_vlc_coeffs( p_sps->vui.vs.colour.matrix_coeffs );
1222 *p_full_range = p_sps->vui.vs.video_full_range_flag ? COLOR_RANGE_FULL : COLOR_RANGE_LIMITED;
1223 return true;
1226 static bool hevc_parse_slice_segment_header_rbsp( bs_t *p_bs,
1227 pf_get_matchedxps get_matchedxps,
1228 void *priv,
1229 hevc_slice_segment_header_t *p_sl )
1231 hevc_sequence_parameter_set_t *p_sps;
1232 hevc_picture_parameter_set_t *p_pps;
1233 hevc_video_parameter_set_t *p_vps;
1235 if( bs_remain( p_bs ) < 3 )
1236 return false;
1238 p_sl->first_slice_segment_in_pic_flag = bs_read1( p_bs );
1239 if( p_sl->nal_type >= HEVC_NAL_BLA_W_LP && p_sl->nal_type <= HEVC_NAL_IRAP_VCL23 )
1240 p_sl->no_output_of_prior_pics_flag = bs_read1( p_bs );
1241 p_sl->slice_pic_parameter_set_id = bs_read_ue( p_bs );
1242 if( p_sl->slice_pic_parameter_set_id > HEVC_PPS_ID_MAX || bs_remain( p_bs ) < 1 )
1243 return false;
1245 get_matchedxps( p_sl->slice_pic_parameter_set_id, priv, &p_pps, &p_sps, &p_vps );
1246 if(!p_sps || !p_pps)
1247 return false;
1249 if( !p_sl->first_slice_segment_in_pic_flag )
1251 if( p_pps->dependent_slice_segments_enabled_flag )
1252 p_sl->dependent_slice_segment_flag = bs_read1( p_bs );
1254 unsigned w, h;
1255 if( !hevc_get_picture_CtbsYsize( p_sps, &w, &h ) )
1256 return false;
1258 (void) bs_read( p_bs, vlc_ceil_log2( w * h ) ); /* slice_segment_address */
1261 if( !p_sl->dependent_slice_segment_flag )
1263 unsigned i=0;
1264 if( p_pps->num_extra_slice_header_bits > i )
1266 i++;
1267 bs_skip( p_bs, 1 ); /* discardable_flag */
1270 if( p_pps->num_extra_slice_header_bits > i )
1272 i++;
1273 bs_skip( p_bs, 1 ); /* cross_layer_bla_flag */
1276 if( i < p_pps->num_extra_slice_header_bits )
1277 bs_skip( p_bs, p_pps->num_extra_slice_header_bits - i );
1279 p_sl->slice_type = bs_read_ue( p_bs );
1280 if( p_sl->slice_type > HEVC_SLICE_TYPE_I )
1281 return false;
1283 if( p_pps->output_flag_present_flag )
1284 p_sl->pic_output_flag = bs_read1( p_bs );
1285 else
1286 p_sl->pic_output_flag = 1;
1289 if( p_sps->separate_colour_plane_flag )
1290 bs_skip( p_bs, 2 ); /* colour_plane_id */
1292 if( p_sl->nal_type != HEVC_NAL_IDR_W_RADL && p_sl->nal_type != HEVC_NAL_IDR_N_LP )
1293 p_sl->pic_order_cnt_lsb = bs_read( p_bs, p_sps->log2_max_pic_order_cnt_lsb_minus4 + 4 );
1294 else
1295 p_sl->pic_order_cnt_lsb = 0;
1297 if( bs_remain( p_bs ) < 1 )
1298 return false;
1300 return true;
1303 void hevc_rbsp_release_slice_header( hevc_slice_segment_header_t *p_sh )
1305 free( p_sh );
1308 hevc_slice_segment_header_t * hevc_decode_slice_header( const uint8_t *p_buf, size_t i_buf, bool b_escaped,
1309 pf_get_matchedxps get_matchedxps, void *priv )
1311 hevc_slice_segment_header_t *p_sh = calloc(1, sizeof(hevc_slice_segment_header_t));
1312 if(likely(p_sh))
1314 bs_t bs;
1315 struct hxxx_bsfw_ep3b_ctx_s bsctx;
1316 if( b_escaped )
1318 hxxx_bsfw_ep3b_ctx_init( &bsctx );
1319 bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );
1321 else bs_init( &bs, p_buf, i_buf );
1322 bs_skip( &bs, 1 );
1323 p_sh->nal_type = bs_read( &bs, 6 );
1324 p_sh->nuh_layer_id = bs_read( &bs, 6 );
1325 p_sh->temporal_id_plus1 = bs_read( &bs, 3 );
1326 if( p_sh->nuh_layer_id > 62 || p_sh->temporal_id_plus1 == 0 ||
1327 !hevc_parse_slice_segment_header_rbsp( &bs, get_matchedxps, priv, p_sh ) )
1329 hevc_rbsp_release_slice_header( p_sh );
1330 p_sh = NULL;
1333 return p_sh;
1336 bool hevc_get_slice_type( const hevc_slice_segment_header_t *p_sli, enum hevc_slice_type_e *pi_type )
1338 if( !p_sli->dependent_slice_segment_flag )
1340 *pi_type = p_sli->slice_type;
1341 return true;
1343 return false;
1346 bool hevc_get_profile_level(const es_format_t *p_fmt, uint8_t *pi_profile,
1347 uint8_t *pi_level, uint8_t *pi_nal_length_size)
1349 const uint8_t *p = (const uint8_t*)p_fmt->p_extra;
1350 if(p_fmt->i_extra < 23 || p[0] != 1)
1351 return false;
1353 /* HEVCDecoderConfigurationRecord */
1354 if(pi_profile)
1355 *pi_profile = p[1] & 0x1F;
1357 if(pi_level)
1358 *pi_level = p[12];
1360 if (pi_nal_length_size)
1361 *pi_nal_length_size = 1 + (p[21]&0x03);
1363 return true;
1366 static unsigned hevc_make_indication( const hevc_inner_profile_tier_level_t *p )
1368 uint8_t flags[] =
1370 p->idc4to7.max_14bit_constraint_flag,
1371 p->idc4to7.max_12bit_constraint_flag,
1372 p->idc4to7.max_10bit_constraint_flag,
1373 p->idc4to7.max_8bit_constraint_flag,
1374 p->idc4to7.max_422chroma_constraint_flag,
1375 p->idc4to7.max_420chroma_constraint_flag,
1376 p->idc4to7.max_monochrome_constraint_flag,
1377 p->idc4to7.intra_constraint_flag,
1378 p->idc4to7.one_picture_only_constraint_flag,
1379 p->idc4to7.lower_bit_rate_constraint_flag,
1381 unsigned indication = 0;
1382 for( size_t i=0; i<ARRAY_SIZE(flags); i++ )
1384 if( flags[i] )
1385 indication |= (1 << (ARRAY_SIZE(flags) - 1 - i));
1387 return indication;
1390 enum vlc_hevc_profile_e hevc_get_vlc_profile( const hevc_sequence_parameter_set_t *p_sps )
1392 unsigned indication = 0;
1393 enum hevc_general_profile_idc_e profile = p_sps->profile_tier_level.general.profile_idc;
1394 switch( profile )
1396 case HEVC_PROFILE_IDC_REXT:
1397 indication = hevc_make_indication( &p_sps->profile_tier_level.general ) & 0x1FF;
1398 break;
1399 case HEVC_PROFILE_IDC_HIGH_THROUGHPUT:
1400 case HEVC_PROFILE_IDC_SCREEN_EXTENDED:
1401 indication = hevc_make_indication( &p_sps->profile_tier_level.general );
1402 break;
1403 default:
1404 break;
1407 /* all intras have insignifiant lowest bit */
1408 if( p_sps->profile_tier_level.general.idc4to7.intra_constraint_flag )
1409 indication &= ~1;
1411 return (indication << HEVC_INDICATION_SHIFT) | profile;
1415 * HEVCDecoderConfigurationRecord operations
1418 static void hevc_dcr_params_from_vps( const uint8_t * p_buffer, size_t i_buffer,
1419 struct hevc_dcr_values *p_values )
1421 if( i_buffer < 19 )
1422 return;
1424 bs_t bs;
1425 struct hxxx_bsfw_ep3b_ctx_s bsctx;
1426 hxxx_bsfw_ep3b_ctx_init( &bsctx );
1427 bs_init_custom( &bs, p_buffer, i_buffer, &hxxx_bsfw_ep3b_callbacks, &bsctx );
1429 /* first two bytes are the NAL header, 3rd and 4th are:
1430 vps_video_parameter_set_id(4)
1431 vps_reserved_3_2bis(2)
1432 vps_max_layers_minus1(6)
1433 vps_max_sub_layers_minus1(3)
1434 vps_temporal_id_nesting_flags
1436 bs_skip( &bs, 16 + 4 + 2 + 6 );
1437 p_values->i_numTemporalLayer = bs_read( &bs, 3 ) + 1;
1438 p_values->b_temporalIdNested = bs_read1( &bs );
1440 /* 5th & 6th are reserved 0xffff */
1441 bs_skip( &bs, 16 );
1442 /* copy the first 12 bytes of profile tier */
1443 for( unsigned i=0; i<12; i++ )
1444 p_values->general_configuration[i] = bs_read( &bs, 8 );
1447 #define HEVC_DCR_ADD_NALS(type, count, buffers, sizes) \
1448 for (uint8_t i = 0; i < count; i++) \
1450 if( i ==0 ) \
1452 *p++ = (type | (b_completeness ? 0x80 : 0)); \
1453 SetWBE( p, count ); p += 2; \
1455 SetWBE( p, sizes[i]); p += 2; \
1456 memcpy( p, buffers[i], sizes[i] ); p += sizes[i];\
1459 #define HEVC_DCR_ADD_SIZES(count, sizes) \
1460 if(count > 0) \
1462 i_total_size += 3;\
1463 for(uint8_t i=0; i<count; i++)\
1464 i_total_size += 2 + sizes[i];\
1467 /* Generate HEVCDecoderConfiguration iso/iec 14496-15 3rd edition */
1468 uint8_t * hevc_create_dcr( const struct hevc_dcr_params *p_params,
1469 uint8_t i_nal_length_size,
1470 bool b_completeness, size_t *pi_size )
1472 *pi_size = 0;
1474 if( i_nal_length_size != 1 && i_nal_length_size != 2 && i_nal_length_size != 4 )
1475 return NULL;
1477 struct hevc_dcr_values values =
1479 .general_configuration = {0},
1480 .i_numTemporalLayer = 0,
1481 .i_chroma_idc = 1,
1482 .i_bit_depth_luma_minus8 = 0,
1483 .i_bit_depth_chroma_minus8 = 0,
1484 .b_temporalIdNested = false,
1487 if( p_params->p_values != NULL )
1489 values = *p_params->p_values;
1491 else
1493 if( p_params->i_vps_count == 0 || p_params->i_sps_count == 0 )
1494 return NULL; /* required to extract info */
1496 hevc_dcr_params_from_vps( p_params->p_vps[0], p_params->rgi_vps[0], &values );
1498 hevc_sequence_parameter_set_t *p_sps =
1499 hevc_decode_sps( p_params->p_sps[0], p_params->rgi_sps[0], true );
1500 if( p_sps )
1502 values.i_chroma_idc = p_sps->chroma_format_idc;
1503 values.i_bit_depth_chroma_minus8 = p_sps->bit_depth_chroma_minus8;
1504 values.i_bit_depth_luma_minus8 = p_sps->bit_depth_luma_minus8;
1505 hevc_rbsp_release_sps( p_sps );
1509 size_t i_total_size = 1+12+2+4+2+2;
1510 HEVC_DCR_ADD_SIZES(p_params->i_vps_count, p_params->rgi_vps);
1511 HEVC_DCR_ADD_SIZES(p_params->i_sps_count, p_params->rgi_sps);
1512 HEVC_DCR_ADD_SIZES(p_params->i_pps_count, p_params->rgi_pps);
1513 HEVC_DCR_ADD_SIZES(p_params->i_seipref_count, p_params->rgi_seipref);
1514 HEVC_DCR_ADD_SIZES(p_params->i_seisuff_count, p_params->rgi_seisuff);
1516 uint8_t *p_data = malloc( i_total_size );
1517 if( p_data == NULL )
1518 return NULL;
1520 *pi_size = i_total_size;
1521 uint8_t *p = p_data;
1523 /* version */
1524 *p++ = 0x01;
1525 memcpy( p, values.general_configuration, 12 ); p += 12;
1526 /* Don't set min spatial segmentation */
1527 SetWBE( p, 0xF000 ); p += 2;
1528 /* Don't set parallelism type since segmentation isn't set */
1529 *p++ = 0xFC;
1530 *p++ = (0xFC | (values.i_chroma_idc & 0x03));
1531 *p++ = (0xF8 | (values.i_bit_depth_luma_minus8 & 0x07));
1532 *p++ = (0xF8 | (values.i_bit_depth_chroma_minus8 & 0x07));
1534 /* Don't set framerate */
1535 SetWBE( p, 0x0000); p += 2;
1536 /* Force NAL size of 4 bytes that replace the startcode */
1537 *p++ = ( ((values.i_numTemporalLayer & 0x07) << 3) |
1538 (values.b_temporalIdNested << 2) |
1539 (i_nal_length_size - 1) );
1540 /* total number of arrays */
1541 *p++ = !!p_params->i_vps_count + !!p_params->i_sps_count +
1542 !!p_params->i_pps_count + !!p_params->i_seipref_count +
1543 !!p_params->i_seisuff_count;
1545 /* Write NAL arrays */
1546 HEVC_DCR_ADD_NALS(HEVC_NAL_VPS, p_params->i_vps_count,
1547 p_params->p_vps, p_params->rgi_vps);
1548 HEVC_DCR_ADD_NALS(HEVC_NAL_SPS, p_params->i_sps_count,
1549 p_params->p_sps, p_params->rgi_sps);
1550 HEVC_DCR_ADD_NALS(HEVC_NAL_PPS, p_params->i_pps_count,
1551 p_params->p_pps, p_params->rgi_pps);
1552 HEVC_DCR_ADD_NALS(HEVC_NAL_PREF_SEI, p_params->i_seipref_count,
1553 p_params->p_seipref, p_params->rgi_seipref);
1554 HEVC_DCR_ADD_NALS(HEVC_NAL_SUFF_SEI, p_params->i_seisuff_count,
1555 p_params->p_seisuff, p_params->rgi_seisuff);
1557 return p_data;
1560 #undef HEVC_DCR_ADD_NALS
1561 #undef HEVC_DCR_ADD_SIZES
1565 * 8.3.1 Decoding process for POC
1567 int hevc_compute_picture_order_count( const hevc_sequence_parameter_set_t *p_sps,
1568 const hevc_slice_segment_header_t *p_slice,
1569 hevc_poc_ctx_t *p_ctx )
1571 struct
1573 int lsb;
1574 int msb;
1575 } prevPicOrderCnt;
1576 int pocMSB;
1577 bool NoRaslOutputFlag;
1578 bool IsIRAP = ( p_slice->nal_type >= HEVC_NAL_BLA_W_LP &&
1579 p_slice->nal_type <= HEVC_NAL_IRAP_VCL23 );
1581 if( IsIRAP )
1583 /* if( IRAP ) NoRaslOutputFlag = first || IDR || BLA || after(EOSNAL) */
1584 NoRaslOutputFlag =(p_ctx->first_picture ||
1585 p_slice->nal_type == HEVC_NAL_IDR_N_LP ||
1586 p_slice->nal_type == HEVC_NAL_IDR_W_RADL ||
1587 p_slice->nal_type == HEVC_NAL_BLA_W_LP ||
1588 p_slice->nal_type == HEVC_NAL_BLA_W_RADL ||
1589 p_slice->nal_type == HEVC_NAL_BLA_N_LP ||
1590 p_ctx->HandleCraAsBlaFlag );
1592 else
1594 NoRaslOutputFlag = false;
1597 #ifdef HEVC_POC_DEBUG
1598 printf("slice lsb=%"PRIu32" irap=%d norasl=%d tid=%d msb=%d lsb=%d",
1599 p_slice->pic_order_cnt_lsb,
1600 IsIRAP, NoRaslOutputFlag, p_slice->temporal_id_plus1,
1601 p_ctx->prevTid0PicOrderCnt.msb, p_ctx->prevTid0PicOrderCnt.lsb);
1602 #endif
1604 if( p_slice->nal_type == HEVC_NAL_IDR_N_LP ||
1605 p_slice->nal_type == HEVC_NAL_IDR_W_RADL )
1607 prevPicOrderCnt.msb = 0;
1608 prevPicOrderCnt.lsb = 0;
1610 /* Not an IRAP with NoRaslOutputFlag == 1 */
1611 else if( !IsIRAP || !NoRaslOutputFlag )
1613 prevPicOrderCnt.msb = p_ctx->prevTid0PicOrderCnt.msb;
1614 prevPicOrderCnt.lsb = p_ctx->prevTid0PicOrderCnt.lsb;
1617 if( IsIRAP && NoRaslOutputFlag )
1619 pocMSB = 0;
1621 else
1623 const unsigned maxPocLSB = 1U << (p_sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
1624 pocMSB = prevPicOrderCnt.msb;
1625 int64_t orderDiff = (int64_t)p_slice->pic_order_cnt_lsb - prevPicOrderCnt.lsb;
1626 if( orderDiff < 0 && -orderDiff >= maxPocLSB / 2 )
1627 pocMSB += maxPocLSB;
1628 else if( orderDiff > maxPocLSB / 2 )
1629 pocMSB -= maxPocLSB;
1632 /* Set prevTid0Pic for next pic */
1633 if( p_slice->temporal_id_plus1 == 1 &&
1634 !( ( p_slice->nal_type <= HEVC_NAL_RSV_VCL_N14 && p_slice->nal_type % 2 == 0 /* SLNR */ ) ||
1635 ( p_slice->nal_type >= HEVC_NAL_RADL_N && p_slice->nal_type <= HEVC_NAL_RASL_R ) /* RADL or RASL */ ) )
1637 p_ctx->prevTid0PicOrderCnt.msb = pocMSB;
1638 p_ctx->prevTid0PicOrderCnt.lsb = p_slice->pic_order_cnt_lsb;
1641 p_ctx->first_picture = false;
1643 #ifdef HEVC_POC_DEBUG
1644 printf(" POC=%"PRIu32"\n", pocMSB + p_slice->pic_order_cnt_lsb);
1645 #endif
1647 return pocMSB + p_slice->pic_order_cnt_lsb;
1650 struct hevc_sei_pic_timing_t
1652 nal_u4_t pic_struct;
1653 nal_u2_t source_scan_type;
1656 void hevc_release_sei_pic_timing( hevc_sei_pic_timing_t *p_timing )
1658 free( p_timing );
1661 hevc_sei_pic_timing_t * hevc_decode_sei_pic_timing( bs_t *p_bs,
1662 const hevc_sequence_parameter_set_t *p_sps )
1664 hevc_sei_pic_timing_t *p_timing = malloc(sizeof(*p_timing));
1665 if( p_timing )
1667 if( p_sps->vui.frame_field_info_present_flag )
1669 p_timing->pic_struct = bs_read( p_bs, 4 );
1670 p_timing->source_scan_type = bs_read( p_bs, 2 );
1672 else
1674 p_timing->pic_struct = 0;
1675 p_timing->source_scan_type = 1;
1678 return p_timing;
1681 bool hevc_frame_is_progressive( const hevc_sequence_parameter_set_t *p_sps,
1682 const hevc_sei_pic_timing_t *p_timing )
1684 if( p_sps->vui_parameters_present_flag &&
1685 p_sps->vui.field_seq_flag )
1686 return false;
1688 const hevc_inner_profile_tier_level_t *p_profile = &p_sps->profile_tier_level.general;
1689 /* 1 & 0, 0 & 1, global */
1690 if( p_profile->progressive_source_flag != p_profile->interlaced_source_flag )
1692 return p_profile->progressive_source_flag > p_profile->interlaced_source_flag;
1694 /* 1 & 1, defined in SEI */
1695 else if( p_profile->progressive_source_flag )
1697 if( p_timing && p_sps->vui.frame_field_info_present_flag )
1699 if( p_timing->source_scan_type < 2 )
1700 return p_timing->source_scan_type != 0;
1703 /* else 0 & 0, unspec */
1705 return true;
1708 uint8_t hevc_get_num_clock_ts( const hevc_sequence_parameter_set_t *p_sps,
1709 const hevc_sei_pic_timing_t *p_timing )
1711 if( p_sps->vui.frame_field_info_present_flag && p_timing && p_timing->pic_struct < 13 )
1713 /* !WARN modified with units_field_based_flag (D.3.25) for values 0, 7 and 8 */
1714 const uint8_t rgi_numclock[13] = { 2, 1, 1, 2, 2, 3, 3, 4, 6, 1, 1, 1, 1 };
1715 return rgi_numclock[p_timing->pic_struct];
1718 if( p_sps->vui_parameters_present_flag )
1720 if( p_sps->vui.field_seq_flag )
1721 return 1; /* D.3.27 */
1723 else if( p_sps->profile_tier_level.general.interlaced_source_flag &&
1724 !p_sps->profile_tier_level.general.progressive_source_flag )
1726 return 1;
1729 return 2;