/src/vlc/modules/packetizer/hevc_nal.c
Line | Count | Source (jump to first uncovered line) |
1 | | /***************************************************************************** |
2 | | * Copyright © 2010-2014 VideoLAN |
3 | | * |
4 | | * Authors: Thomas Guillem <thomas.guillem@gmail.com> |
5 | | * |
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. |
10 | | * |
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. |
15 | | * |
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 |
23 | | |
24 | | #include <stdbit.h> |
25 | | |
26 | | #include "hevc_nal.h" |
27 | | #include "h26x_nal_common.h" |
28 | | #include "hxxx_nal.h" |
29 | | #include "hxxx_ep3b.h" |
30 | | |
31 | | #include <vlc_common.h> |
32 | | #include <vlc_bits.h> |
33 | | |
34 | | #include <limits.h> |
35 | | |
36 | | //#define HEVC_POC_DEBUG |
37 | | |
38 | | typedef struct |
39 | | { |
40 | | nal_u2_t profile_space; |
41 | | nal_u1_t tier_flag; |
42 | | nal_u5_t profile_idc; |
43 | | uint32_t profile_compatibility_flag; /* nal_u1_t * 32 */ |
44 | | nal_u1_t progressive_source_flag; |
45 | | nal_u1_t interlaced_source_flag; |
46 | | nal_u1_t non_packed_constraint_flag; |
47 | | nal_u1_t frame_only_constraint_flag; |
48 | | struct |
49 | | { |
50 | | nal_u1_t max_12bit_constraint_flag; |
51 | | nal_u1_t max_10bit_constraint_flag; |
52 | | nal_u1_t max_8bit_constraint_flag; |
53 | | nal_u1_t max_422chroma_constraint_flag; |
54 | | nal_u1_t max_420chroma_constraint_flag; |
55 | | nal_u1_t max_monochrome_constraint_flag; |
56 | | nal_u1_t intra_constraint_flag; |
57 | | nal_u1_t one_picture_only_constraint_flag; |
58 | | nal_u1_t lower_bit_rate_constraint_flag; |
59 | | nal_u1_t max_14bit_constraint_flag; |
60 | | } idc4to7; |
61 | | struct |
62 | | { |
63 | | nal_u1_t inbld_flag; |
64 | | } idc1to5; |
65 | | } hevc_inner_profile_tier_level_t; |
66 | | |
67 | | #define HEVC_MAX_SUBLAYERS 8 |
68 | | typedef struct |
69 | | { |
70 | | hevc_inner_profile_tier_level_t general; |
71 | | nal_u8_t general_level_idc; |
72 | | uint8_t sublayer_profile_present_flag; /* nal_u1_t * 8 */ |
73 | | uint8_t sublayer_level_present_flag; /* nal_u1_t * 8 */ |
74 | | hevc_inner_profile_tier_level_t sub_layer[HEVC_MAX_SUBLAYERS]; |
75 | | nal_u8_t sub_layer_level_idc[HEVC_MAX_SUBLAYERS]; |
76 | | } hevc_profile_tier_level_t; |
77 | | |
78 | 0 | #define HEVC_MAX_SHORT_TERM_REF_PIC_SET 65 |
79 | 0 | #define HEVC_MAX_LONG_TERM_REF_PIC_SET 33 |
80 | | |
81 | | typedef struct |
82 | | { |
83 | | unsigned num_delta_pocs; |
84 | | } hevc_short_term_ref_pic_set_t; |
85 | | |
86 | | typedef struct |
87 | | { |
88 | | nal_u1_t aspect_ratio_info_present_flag; |
89 | | h26x_aspect_ratio_t ar; |
90 | | nal_u1_t overscan_info_present_flag; |
91 | | nal_u1_t overscan_appropriate_flag; |
92 | | |
93 | | nal_u1_t video_signal_type_present_flag; |
94 | | struct |
95 | | { |
96 | | nal_u3_t video_format; |
97 | | nal_u1_t colour_description_present_flag; |
98 | | h26x_colour_description_t colour; |
99 | | } vs; |
100 | | |
101 | | nal_u1_t chroma_loc_info_present_flag; |
102 | | struct |
103 | | { |
104 | | nal_ue_t sample_loc_type_top_field; |
105 | | nal_ue_t sample_loc_type_bottom_field; |
106 | | } chroma; |
107 | | |
108 | | nal_u1_t neutral_chroma_indication_flag; |
109 | | nal_u1_t field_seq_flag; |
110 | | nal_u1_t frame_field_info_present_flag; |
111 | | |
112 | | nal_u1_t default_display_window_flag; |
113 | | struct |
114 | | { |
115 | | nal_ue_t win_left_offset; |
116 | | nal_ue_t win_right_offset; |
117 | | nal_ue_t win_top_offset; |
118 | | nal_ue_t win_bottom_offset; |
119 | | } def_disp; |
120 | | |
121 | | nal_u1_t vui_timing_info_present_flag; |
122 | | struct |
123 | | { |
124 | | uint32_t vui_num_units_in_tick; |
125 | | uint32_t vui_time_scale; |
126 | | /* incomplete */ |
127 | | } timing; |
128 | | |
129 | | /* incomplete */ |
130 | | } hevc_vui_parameters_t; |
131 | | |
132 | | struct hevc_video_parameter_set_t |
133 | | { |
134 | | nal_u4_t vps_video_parameter_set_id; |
135 | | nal_u1_t vps_base_layer_internal_flag; |
136 | | nal_u1_t vps_base_layer_available_flag; |
137 | | nal_u6_t vps_max_layers_minus1; |
138 | | nal_u3_t vps_max_sub_layers_minus1; |
139 | | nal_u1_t vps_temporal_id_nesting_flag; |
140 | | |
141 | | hevc_profile_tier_level_t profile_tier_level; |
142 | | |
143 | | nal_u1_t vps_sub_layer_ordering_info_present_flag; |
144 | | struct |
145 | | { |
146 | | nal_ue_t dec_pic_buffering_minus1; |
147 | | nal_ue_t num_reorder_pics; |
148 | | nal_ue_t max_latency_increase_plus1; |
149 | | } vps_max[1 + HEVC_MAX_SUBLAYERS]; |
150 | | |
151 | | nal_u6_t vps_max_layer_id; |
152 | | nal_ue_t vps_num_layer_set_minus1; |
153 | | // layer_id_included_flag; read but discarded |
154 | | |
155 | | nal_u1_t vps_timing_info_present_flag; |
156 | | uint32_t vps_num_units_in_tick; |
157 | | uint32_t vps_time_scale; |
158 | | |
159 | | /* incomplete */ |
160 | | }; |
161 | | |
162 | | struct hevc_sequence_parameter_set_t |
163 | | { |
164 | | nal_u4_t sps_video_parameter_set_id; |
165 | | nal_u3_t sps_max_sub_layers_minus1; |
166 | | nal_u1_t sps_temporal_id_nesting_flag; |
167 | | |
168 | | hevc_profile_tier_level_t profile_tier_level; |
169 | | |
170 | | nal_ue_t sps_seq_parameter_set_id; |
171 | | nal_ue_t chroma_format_idc; |
172 | | nal_u1_t separate_colour_plane_flag; |
173 | | |
174 | | nal_ue_t pic_width_in_luma_samples; |
175 | | nal_ue_t pic_height_in_luma_samples; |
176 | | |
177 | | nal_u1_t conformance_window_flag; |
178 | | h26x_conf_window_t conf_win; |
179 | | |
180 | | nal_ue_t bit_depth_luma_minus8; |
181 | | nal_ue_t bit_depth_chroma_minus8; |
182 | | nal_ue_t log2_max_pic_order_cnt_lsb_minus4; |
183 | | |
184 | | nal_u1_t sps_sub_layer_ordering_info_present_flag; |
185 | | struct |
186 | | { |
187 | | nal_ue_t dec_pic_buffering_minus1; |
188 | | nal_ue_t num_reorder_pics; |
189 | | nal_ue_t latency_increase_plus1; |
190 | | } sps_max[1 + HEVC_MAX_SUBLAYERS]; |
191 | | |
192 | | nal_ue_t log2_min_luma_coding_block_size_minus3; |
193 | | nal_ue_t log2_diff_max_min_luma_coding_block_size; |
194 | | nal_ue_t log2_min_luma_transform_block_size_minus2; |
195 | | nal_ue_t log2_diff_max_min_luma_transform_block_size; |
196 | | |
197 | | /* incomplete */ |
198 | | nal_ue_t max_transform_hierarchy_depth_inter; |
199 | | nal_ue_t max_transform_hierarchy_depth_intra; |
200 | | nal_u1_t scaling_list_enabled; |
201 | | nal_u1_t sps_scaling_list_data_present_flag; |
202 | | // scaling_list_data; read but discarded |
203 | | |
204 | | nal_u1_t amp_enabled_flag; |
205 | | nal_u1_t sample_adaptive_offset_enabled_flag; |
206 | | |
207 | | nal_u1_t pcm_enabled_flag; |
208 | | nal_u4_t pcm_sample_bit_depth_luma_minus1; |
209 | | nal_u4_t pcm_sample_bit_depth_chroma_minus1; |
210 | | nal_ue_t log2_min_pcm_luma_coding_block_size_minus3; |
211 | | nal_ue_t log2_diff_max_min_pcm_luma_coding_block_size; |
212 | | nal_u1_t pcm_loop_filter_disabled_flag; |
213 | | |
214 | | nal_ue_t num_short_term_ref_pic_sets; |
215 | | // st_ref_pic_set |
216 | | |
217 | | nal_u1_t long_term_ref_pics_present_flag; |
218 | | nal_ue_t num_long_term_ref_pics_sps; |
219 | | // |
220 | | |
221 | | nal_u1_t sps_temporal_mvp_enabled_flag; |
222 | | nal_u1_t strong_intra_smoothing_enabled_flag; |
223 | | |
224 | | nal_u1_t vui_parameters_present_flag; |
225 | | hevc_vui_parameters_t vui; |
226 | | /* incomplete */ |
227 | | }; |
228 | | |
229 | | struct hevc_picture_parameter_set_t |
230 | | { |
231 | | nal_ue_t pps_pic_parameter_set_id; |
232 | | nal_ue_t pps_seq_parameter_set_id; |
233 | | nal_u1_t dependent_slice_segments_enabled_flag; |
234 | | nal_u1_t output_flag_present_flag; |
235 | | nal_u3_t num_extra_slice_header_bits; |
236 | | nal_u1_t sign_data_hiding_enabled_flag; |
237 | | nal_u1_t cabac_init_present_flag; |
238 | | nal_ue_t num_ref_idx_l0_default_active_minus1; |
239 | | nal_ue_t num_ref_idx_l1_default_active_minus1; |
240 | | nal_se_t init_qp_minus26; |
241 | | nal_u1_t constrained_intra_pred_flag; |
242 | | nal_u1_t transform_skip_enabled_flag; |
243 | | |
244 | | nal_u1_t cu_qp_delta_enabled_flag; |
245 | | nal_ue_t diff_cu_qp_delta_depth; |
246 | | |
247 | | nal_se_t pps_cb_qp_offset; |
248 | | nal_se_t pps_cr_qp_offset; |
249 | | nal_u1_t pic_slice_level_chroma_qp_offsets_present_flag; |
250 | | nal_u1_t weighted_pred_flag; |
251 | | nal_u1_t weighted_bipred_flag; |
252 | | nal_u1_t transquant_bypass_enable_flag; |
253 | | |
254 | | nal_u1_t tiles_enabled_flag; |
255 | | nal_u1_t entropy_coding_sync_enabled_flag; |
256 | | nal_ue_t num_tile_columns_minus1; |
257 | | nal_ue_t num_tile_rows_minus1; |
258 | | nal_u1_t uniform_spacing_flag; |
259 | | // nal_ue_t *p_column_width_minus1; read but discarded |
260 | | // nal_ue_t *p_row_height_minus1; read but discarded |
261 | | nal_u1_t loop_filter_across_tiles_enabled_flag; |
262 | | |
263 | | nal_u1_t pps_loop_filter_across_slices_enabled_flag; |
264 | | |
265 | | nal_u1_t deblocking_filter_control_present_flag; |
266 | | nal_u1_t deblocking_filter_override_enabled_flag; |
267 | | nal_u1_t pps_deblocking_filter_disabled_flag; |
268 | | nal_se_t pps_beta_offset_div2; |
269 | | nal_se_t pps_tc_offset_div2; |
270 | | |
271 | | nal_u1_t scaling_list_data_present_flag; |
272 | | // scaling_list_data; read but discarded |
273 | | |
274 | | nal_u1_t lists_modification_present_flag; |
275 | | nal_ue_t log2_parallel_merge_level_minus2; |
276 | | nal_u1_t slice_header_extension_present_flag; |
277 | | |
278 | | nal_u1_t pps_extension_present_flag; |
279 | | nal_u1_t pps_range_extension_flag; |
280 | | nal_u1_t pps_multilayer_extension_flag; |
281 | | nal_u1_t pps_3d_extension_flag; |
282 | | nal_u5_t pps_extension_5bits; |
283 | | /* incomplete */ |
284 | | |
285 | | }; |
286 | | |
287 | | struct hevc_slice_segment_header_t |
288 | | { |
289 | | nal_u6_t nal_type; |
290 | | nal_u6_t nuh_layer_id; |
291 | | nal_u3_t temporal_id_plus1; |
292 | | nal_u1_t first_slice_segment_in_pic_flag; |
293 | | nal_u1_t no_output_of_prior_pics_flag; |
294 | | nal_ue_t slice_pic_parameter_set_id; |
295 | | nal_u1_t dependent_slice_segment_flag; |
296 | | // slice_segment_address; read but discarded |
297 | | nal_ue_t slice_type; |
298 | | nal_u1_t pic_output_flag; |
299 | | |
300 | | uint32_t pic_order_cnt_lsb; |
301 | | /* incomplete */ |
302 | | |
303 | | }; |
304 | | |
305 | | /* Computes size and does check the whole struct integrity */ |
306 | | static size_t get_hvcC_to_AnnexB_NAL_size( const uint8_t *p_buf, size_t i_buf ) |
307 | 0 | { |
308 | 0 | size_t i_total = 0; |
309 | |
|
310 | 0 | if( i_buf < HEVC_MIN_HVCC_SIZE ) |
311 | 0 | return 0; |
312 | | |
313 | 0 | const uint8_t i_nal_length_size = (p_buf[21] & 0x03) + 1; |
314 | 0 | if(i_nal_length_size == 3) |
315 | 0 | return 0; |
316 | | |
317 | 0 | const uint8_t i_num_array = p_buf[22]; |
318 | 0 | p_buf += 23; i_buf -= 23; |
319 | |
|
320 | 0 | for( uint8_t i = 0; i < i_num_array; i++ ) |
321 | 0 | { |
322 | 0 | if(i_buf < 3) |
323 | 0 | return 0; |
324 | | |
325 | 0 | const uint16_t i_num_nalu = p_buf[1] << 8 | p_buf[2]; |
326 | 0 | p_buf += 3; i_buf -= 3; |
327 | |
|
328 | 0 | for( uint16_t j = 0; j < i_num_nalu; j++ ) |
329 | 0 | { |
330 | 0 | if(i_buf < 2) |
331 | 0 | return 0; |
332 | | |
333 | 0 | const uint16_t i_nalu_length = p_buf[0] << 8 | p_buf[1]; |
334 | 0 | if(i_buf < (size_t)i_nalu_length + 2) |
335 | 0 | return 0; |
336 | | |
337 | 0 | i_total += i_nalu_length + i_nal_length_size; |
338 | 0 | p_buf += i_nalu_length + 2; |
339 | 0 | i_buf -= i_nalu_length + 2; |
340 | 0 | } |
341 | 0 | } |
342 | | |
343 | 0 | return i_total; |
344 | 0 | } |
345 | | |
346 | | uint8_t * hevc_hvcC_to_AnnexB_NAL( const uint8_t *p_buf, size_t i_buf, |
347 | | size_t *pi_result, uint8_t *pi_nal_length_size ) |
348 | 0 | { |
349 | 0 | *pi_result = get_hvcC_to_AnnexB_NAL_size( p_buf, i_buf ); /* Does all checks */ |
350 | 0 | if( *pi_result == 0 ) |
351 | 0 | return NULL; |
352 | | |
353 | 0 | if( pi_nal_length_size ) |
354 | 0 | *pi_nal_length_size = hevc_getNALLengthSize( p_buf ); |
355 | |
|
356 | 0 | uint8_t *p_ret; |
357 | 0 | uint8_t *p_out_buf = p_ret = malloc( *pi_result ); |
358 | 0 | if( !p_out_buf ) |
359 | 0 | { |
360 | 0 | *pi_result = 0; |
361 | 0 | return NULL; |
362 | 0 | } |
363 | | |
364 | 0 | const uint8_t i_num_array = p_buf[22]; |
365 | 0 | p_buf += 23; |
366 | |
|
367 | 0 | for( uint8_t i = 0; i < i_num_array; i++ ) |
368 | 0 | { |
369 | 0 | const uint16_t i_num_nalu = p_buf[1] << 8 | p_buf[2]; |
370 | 0 | p_buf += 3; |
371 | |
|
372 | 0 | for( uint16_t j = 0; j < i_num_nalu; j++ ) |
373 | 0 | { |
374 | 0 | const uint16_t i_nalu_length = p_buf[0] << 8 | p_buf[1]; |
375 | |
|
376 | 0 | memcpy( p_out_buf, annexb_startcode4, 4 ); |
377 | 0 | memcpy( &p_out_buf[4], &p_buf[2], i_nalu_length ); |
378 | |
|
379 | 0 | p_out_buf += 4 + i_nalu_length; |
380 | 0 | p_buf += 2 + i_nalu_length; |
381 | 0 | } |
382 | 0 | } |
383 | |
|
384 | 0 | return p_ret; |
385 | 0 | } |
386 | | |
387 | | static bool hevc_parse_scaling_list_rbsp( bs_t *p_bs ) |
388 | 0 | { |
389 | 0 | for( int i=0; i<4; i++ ) |
390 | 0 | { |
391 | 0 | for( int j=0; j<6; j += (i == 3) ? 3 : 1 ) |
392 | 0 | { |
393 | 0 | if( bs_read1( p_bs ) == 0 ) |
394 | 0 | bs_read_ue( p_bs ); |
395 | 0 | else |
396 | 0 | { |
397 | 0 | unsigned nextCoef = 8; |
398 | 0 | unsigned coefNum = __MIN( 64, (1 << (4 + (i << 1)))); |
399 | 0 | if( i > 1 ) |
400 | 0 | { |
401 | 0 | nextCoef = bs_read_se( p_bs ) + 8; |
402 | 0 | } |
403 | 0 | for( unsigned k=0; k<coefNum; k++ ) |
404 | 0 | { |
405 | 0 | nextCoef = ( nextCoef + bs_read_se( p_bs ) + 256 ) % 256; |
406 | 0 | } |
407 | 0 | } |
408 | 0 | } |
409 | 0 | } |
410 | |
|
411 | 0 | return true; |
412 | 0 | } |
413 | | |
414 | | static bool hevc_parse_vui_parameters_rbsp( bs_t *p_bs, hevc_vui_parameters_t *p_vui, |
415 | | bool b_broken ) |
416 | 0 | { |
417 | 0 | p_vui->aspect_ratio_info_present_flag = bs_read1( p_bs ); |
418 | 0 | if( p_vui->aspect_ratio_info_present_flag ) |
419 | 0 | { |
420 | 0 | p_vui->ar.aspect_ratio_idc = bs_read( p_bs, 8 ); |
421 | 0 | if( p_vui->ar.aspect_ratio_idc == 0xFF ) //HEVC_SAR__IDC_EXTENDED_SAR ) |
422 | 0 | { |
423 | 0 | p_vui->ar.sar_width = bs_read( p_bs, 16 ); |
424 | 0 | p_vui->ar.sar_height = bs_read( p_bs, 16 ); |
425 | 0 | } |
426 | 0 | } |
427 | |
|
428 | 0 | p_vui->overscan_info_present_flag = bs_read1( p_bs ); |
429 | 0 | if( p_vui->overscan_info_present_flag ) |
430 | 0 | p_vui->overscan_appropriate_flag = bs_read1( p_bs ); |
431 | |
|
432 | 0 | p_vui->video_signal_type_present_flag = bs_read1( p_bs ); |
433 | 0 | if( p_vui->video_signal_type_present_flag ) |
434 | 0 | { |
435 | 0 | p_vui->vs.video_format = bs_read( p_bs, 3 ); |
436 | 0 | p_vui->vs.colour.full_range_flag = bs_read1( p_bs ); |
437 | 0 | p_vui->vs.colour_description_present_flag = bs_read1( p_bs ); |
438 | 0 | if( p_vui->vs.colour_description_present_flag ) |
439 | 0 | { |
440 | 0 | p_vui->vs.colour.colour_primaries = bs_read( p_bs, 8 ); |
441 | 0 | p_vui->vs.colour.transfer_characteristics = bs_read( p_bs, 8 ); |
442 | 0 | p_vui->vs.colour.matrix_coeffs = bs_read( p_bs, 8 ); |
443 | 0 | } |
444 | 0 | else |
445 | 0 | { |
446 | 0 | p_vui->vs.colour.colour_primaries = ISO_23001_8_CP_UNSPECIFIED; |
447 | 0 | p_vui->vs.colour.transfer_characteristics = ISO_23001_8_TC_UNSPECIFIED; |
448 | 0 | p_vui->vs.colour.matrix_coeffs = ISO_23001_8_MC_UNSPECIFIED; |
449 | 0 | } |
450 | 0 | } |
451 | |
|
452 | 0 | p_vui->chroma_loc_info_present_flag = bs_read1( p_bs ); |
453 | 0 | if( p_vui->chroma_loc_info_present_flag ) |
454 | 0 | { |
455 | 0 | p_vui->chroma.sample_loc_type_top_field = bs_read_ue( p_bs ); |
456 | 0 | p_vui->chroma.sample_loc_type_bottom_field = bs_read_ue( p_bs ); |
457 | 0 | } |
458 | |
|
459 | 0 | p_vui->neutral_chroma_indication_flag = bs_read1( p_bs ); |
460 | 0 | p_vui->field_seq_flag = bs_read1( p_bs ); |
461 | 0 | p_vui->frame_field_info_present_flag = bs_read1( p_bs ); |
462 | |
|
463 | 0 | p_vui->default_display_window_flag = !b_broken && bs_read1( p_bs ); |
464 | 0 | if( p_vui->default_display_window_flag ) |
465 | 0 | { |
466 | 0 | p_vui->def_disp.win_left_offset = bs_read_ue( p_bs ); |
467 | 0 | p_vui->def_disp.win_right_offset = bs_read_ue( p_bs ); |
468 | 0 | p_vui->def_disp.win_top_offset = bs_read_ue( p_bs ); |
469 | 0 | p_vui->def_disp.win_bottom_offset = bs_read_ue( p_bs ); |
470 | 0 | } |
471 | |
|
472 | 0 | p_vui->vui_timing_info_present_flag = bs_read1( p_bs ); |
473 | 0 | if( p_vui->vui_timing_info_present_flag ) |
474 | 0 | { |
475 | 0 | p_vui->timing.vui_num_units_in_tick = bs_read( p_bs, 32 ); |
476 | 0 | p_vui->timing.vui_time_scale = bs_read( p_bs, 32 ); |
477 | 0 | } |
478 | | /* incomplete */ |
479 | |
|
480 | 0 | return !bs_error( p_bs ); |
481 | 0 | } |
482 | | |
483 | | /* Shortcut for retrieving vps/sps/pps id */ |
484 | | bool hevc_get_xps_id(const uint8_t *p_buf, size_t i_buf, uint8_t *pi_id) |
485 | 0 | { |
486 | 0 | if(i_buf < 3) |
487 | 0 | return false; |
488 | | /* No need to lookup convert from emulation for that data */ |
489 | 0 | uint8_t i_nal_type = hevc_getNALType(p_buf); |
490 | 0 | bs_t bs; |
491 | 0 | bs_init(&bs, &p_buf[2], i_buf - 2); |
492 | 0 | if(i_nal_type == HEVC_NAL_PPS) |
493 | 0 | { |
494 | 0 | *pi_id = bs_read_ue( &bs ); |
495 | 0 | if(*pi_id > HEVC_PPS_ID_MAX) |
496 | 0 | return false; |
497 | 0 | } |
498 | 0 | else |
499 | 0 | { |
500 | 0 | *pi_id = bs_read( &bs, 4 ); |
501 | 0 | if(i_nal_type == HEVC_NAL_SPS) |
502 | 0 | { |
503 | 0 | if(*pi_id > HEVC_SPS_ID_MAX) |
504 | 0 | return false; |
505 | 0 | } |
506 | 0 | else if(*pi_id > HEVC_VPS_ID_MAX) |
507 | 0 | return false; |
508 | 0 | } |
509 | 0 | return true; |
510 | 0 | } |
511 | | |
512 | | static bool hevc_parse_inner_profile_tier_level_rbsp( bs_t *p_bs, |
513 | | hevc_inner_profile_tier_level_t *p_in ) |
514 | 0 | { |
515 | 0 | p_in->profile_space = bs_read( p_bs, 2 ); |
516 | 0 | p_in->tier_flag = bs_read1( p_bs ); |
517 | 0 | p_in->profile_idc = bs_read( p_bs, 5 ); |
518 | 0 | p_in->profile_compatibility_flag = bs_read( p_bs, 32 ); |
519 | 0 | p_in->progressive_source_flag = bs_read1( p_bs ); |
520 | 0 | p_in->interlaced_source_flag = bs_read1( p_bs ); |
521 | 0 | p_in->non_packed_constraint_flag = bs_read1( p_bs ); |
522 | 0 | p_in->frame_only_constraint_flag = bs_read1( p_bs ); |
523 | |
|
524 | 0 | if( ( p_in->profile_idc >= 4 && p_in->profile_idc <= 10 ) || |
525 | 0 | ( p_in->profile_compatibility_flag & 0x0F700000 ) ) |
526 | 0 | { |
527 | 0 | p_in->idc4to7.max_12bit_constraint_flag = bs_read1( p_bs ); |
528 | 0 | p_in->idc4to7.max_10bit_constraint_flag = bs_read1( p_bs ); |
529 | 0 | p_in->idc4to7.max_8bit_constraint_flag = bs_read1( p_bs ); |
530 | 0 | p_in->idc4to7.max_422chroma_constraint_flag = bs_read1( p_bs ); |
531 | 0 | p_in->idc4to7.max_420chroma_constraint_flag = bs_read1( p_bs ); |
532 | 0 | p_in->idc4to7.max_monochrome_constraint_flag = bs_read1( p_bs ); |
533 | 0 | p_in->idc4to7.intra_constraint_flag = bs_read1( p_bs ); |
534 | 0 | p_in->idc4to7.one_picture_only_constraint_flag = bs_read1( p_bs ); |
535 | 0 | p_in->idc4to7.lower_bit_rate_constraint_flag = bs_read1( p_bs ); |
536 | 0 | if( p_in->profile_idc == 5 || |
537 | 0 | p_in->profile_idc == 9 || |
538 | 0 | p_in->profile_idc == 10 || |
539 | 0 | (p_in->profile_compatibility_flag & 0x08600000) ) |
540 | 0 | { |
541 | 0 | p_in->idc4to7.max_14bit_constraint_flag = bs_read1( p_bs ); |
542 | 0 | bs_skip( p_bs, 33 ); |
543 | 0 | } |
544 | 0 | else bs_skip( p_bs, 34 ); |
545 | 0 | } |
546 | 0 | else if( p_in->profile_idc == 2 || |
547 | 0 | (p_in->profile_compatibility_flag & 0x20000000) ) |
548 | 0 | { |
549 | 0 | bs_skip( p_bs, 7 ); |
550 | 0 | p_in->idc4to7.one_picture_only_constraint_flag = bs_read1( p_bs ); |
551 | 0 | bs_skip( p_bs, 35 ); |
552 | 0 | } |
553 | 0 | else |
554 | 0 | { |
555 | 0 | bs_read( p_bs, 43 ); |
556 | 0 | } |
557 | |
|
558 | 0 | if( ( p_in->profile_idc >= 1 && p_in->profile_idc <= 5 ) || |
559 | 0 | p_in->profile_idc == 9 || |
560 | 0 | ( p_in->profile_compatibility_flag & 0x7C400000 ) ) |
561 | 0 | p_in->idc1to5.inbld_flag = bs_read1( p_bs ); |
562 | 0 | else |
563 | 0 | bs_skip( p_bs, 1 ); |
564 | |
|
565 | 0 | return !bs_error( p_bs ); |
566 | 0 | } |
567 | | |
568 | | static bool hevc_parse_profile_tier_level_rbsp( bs_t *p_bs, bool profile_present, |
569 | | uint8_t max_num_sub_layers_minus1, |
570 | | hevc_profile_tier_level_t *p_ptl ) |
571 | 0 | { |
572 | 0 | if( profile_present && !hevc_parse_inner_profile_tier_level_rbsp( p_bs, &p_ptl->general ) ) |
573 | 0 | return false; |
574 | | |
575 | 0 | if( bs_error( p_bs ) ) |
576 | 0 | return false; |
577 | | |
578 | 0 | p_ptl->general_level_idc = bs_read( p_bs, 8 ); |
579 | |
|
580 | 0 | if( max_num_sub_layers_minus1 > 0 ) |
581 | 0 | { |
582 | 0 | if( bs_eof( p_bs ) ) |
583 | 0 | return false; |
584 | | |
585 | 0 | for( uint8_t i=0; i< 8; i++ ) |
586 | 0 | { |
587 | 0 | if( i < max_num_sub_layers_minus1 ) |
588 | 0 | { |
589 | 0 | if( bs_read1( p_bs ) ) |
590 | 0 | p_ptl->sublayer_profile_present_flag |= (0x80 >> i); |
591 | 0 | if( bs_read1( p_bs ) ) |
592 | 0 | p_ptl->sublayer_level_present_flag |= (0x80 >> i); |
593 | 0 | } |
594 | 0 | else |
595 | 0 | bs_read( p_bs, 2 ); |
596 | 0 | } |
597 | |
|
598 | 0 | for( uint8_t i=0; i < max_num_sub_layers_minus1; i++ ) |
599 | 0 | { |
600 | 0 | if( ( p_ptl->sublayer_profile_present_flag & (0x80 >> i) ) && |
601 | 0 | ! hevc_parse_inner_profile_tier_level_rbsp( p_bs, &p_ptl->sub_layer[i] ) ) |
602 | 0 | return false; |
603 | | |
604 | 0 | if( p_ptl->sublayer_profile_present_flag & (0x80 >> i) ) |
605 | 0 | { |
606 | 0 | if( bs_eof( p_bs ) ) |
607 | 0 | return false; |
608 | 0 | p_ptl->sub_layer_level_idc[i] = bs_read( p_bs, 8 ); |
609 | 0 | } |
610 | 0 | } |
611 | 0 | } |
612 | | |
613 | 0 | return !bs_error( p_bs ); |
614 | 0 | } |
615 | | |
616 | | static bool hevc_parse_video_parameter_set_rbsp( bs_t *p_bs, |
617 | | hevc_video_parameter_set_t *p_vps ) |
618 | 0 | { |
619 | 0 | if( bs_eof( p_bs ) ) |
620 | 0 | return false; |
621 | | |
622 | 0 | p_vps->vps_video_parameter_set_id = bs_read( p_bs, 4 ); |
623 | 0 | p_vps->vps_base_layer_internal_flag = bs_read1( p_bs ); |
624 | 0 | p_vps->vps_base_layer_available_flag = bs_read1( p_bs ); |
625 | 0 | p_vps->vps_max_layers_minus1 = bs_read( p_bs, 6 ); |
626 | 0 | p_vps->vps_max_sub_layers_minus1 = bs_read( p_bs, 3 ); |
627 | 0 | p_vps->vps_temporal_id_nesting_flag = bs_read1( p_bs ); |
628 | 0 | bs_skip( p_bs, 16 ); |
629 | |
|
630 | 0 | if( !hevc_parse_profile_tier_level_rbsp( p_bs, true, p_vps->vps_max_sub_layers_minus1, |
631 | 0 | &p_vps->profile_tier_level ) ) |
632 | 0 | return false; |
633 | | |
634 | 0 | p_vps->vps_sub_layer_ordering_info_present_flag = bs_read1( p_bs ); |
635 | 0 | for( unsigned i= (p_vps->vps_sub_layer_ordering_info_present_flag ? |
636 | 0 | 0 : p_vps->vps_max_sub_layers_minus1); |
637 | 0 | i<= p_vps->vps_max_sub_layers_minus1; i++ ) |
638 | 0 | { |
639 | 0 | p_vps->vps_max[i].dec_pic_buffering_minus1 = bs_read_ue( p_bs ); |
640 | 0 | p_vps->vps_max[i].num_reorder_pics = bs_read_ue( p_bs ); |
641 | 0 | p_vps->vps_max[i].max_latency_increase_plus1 = bs_read_ue( p_bs ); |
642 | 0 | } |
643 | |
|
644 | 0 | if( bs_error( p_bs ) ) |
645 | 0 | return false; |
646 | | |
647 | 0 | p_vps->vps_max_layer_id = bs_read( p_bs, 6 ); |
648 | 0 | p_vps->vps_num_layer_set_minus1 = bs_read_ue( p_bs ); |
649 | | // layer_id_included_flag; read but discarded |
650 | 0 | bs_skip( p_bs, p_vps->vps_num_layer_set_minus1 * (p_vps->vps_max_layer_id + 1) ); |
651 | |
|
652 | 0 | p_vps->vps_timing_info_present_flag = bs_read1( p_bs ); |
653 | 0 | if( p_vps->vps_timing_info_present_flag ) |
654 | 0 | { |
655 | 0 | p_vps->vps_num_units_in_tick = bs_read( p_bs, 32 ); |
656 | 0 | p_vps->vps_time_scale = bs_read( p_bs, 32 ); |
657 | 0 | } |
658 | | /* parsing incomplete */ |
659 | |
|
660 | 0 | return !bs_error( p_bs ); |
661 | 0 | } |
662 | | |
663 | | void hevc_rbsp_release_vps( hevc_video_parameter_set_t *p_vps ) |
664 | 0 | { |
665 | 0 | free( p_vps ); |
666 | 0 | } |
667 | | |
668 | | #define IMPL_hevc_generic_decode( name, hevctype, decode, release ) \ |
669 | | hevctype * name( const uint8_t *p_buf, size_t i_buf, bool b_escaped ) \ |
670 | 0 | { \ |
671 | 0 | hevctype *p_hevctype = calloc(1, sizeof(hevctype)); \ |
672 | 0 | if(likely(p_hevctype)) \ |
673 | 0 | { \ |
674 | 0 | bs_t bs; \ |
675 | 0 | struct hxxx_bsfw_ep3b_ctx_s bsctx; \ |
676 | 0 | if( b_escaped ) \ |
677 | 0 | { \ |
678 | 0 | hxxx_bsfw_ep3b_ctx_init( &bsctx ); \ |
679 | 0 | bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx );\ |
680 | 0 | } \ |
681 | 0 | else bs_init( &bs, p_buf, i_buf ); \ |
682 | 0 | bs_skip( &bs, 7 ); /* nal_unit_header */ \ |
683 | 0 | uint8_t i_nuh_layer_id = bs_read( &bs, 6 ); \ |
684 | 0 | bs_skip( &bs, 3 ); /* !nal_unit_header */ \ |
685 | 0 | if( i_nuh_layer_id > 62 || !decode( &bs, p_hevctype ) ) \ |
686 | 0 | { \ |
687 | 0 | release( p_hevctype ); \ |
688 | 0 | p_hevctype = NULL; \ |
689 | 0 | } \ |
690 | 0 | } \ |
691 | 0 | return p_hevctype; \ |
692 | 0 | } Unexecuted instantiation: hevc_decode_vps Unexecuted instantiation: hevc_decode_sps Unexecuted instantiation: hevc_decode_pps |
693 | | |
694 | | IMPL_hevc_generic_decode( hevc_decode_vps, hevc_video_parameter_set_t, |
695 | | hevc_parse_video_parameter_set_rbsp, hevc_rbsp_release_vps ) |
696 | | |
697 | | static bool hevc_parse_st_ref_pic_set( bs_t *p_bs, unsigned stRpsIdx, |
698 | | unsigned num_short_term_ref_pic_sets, |
699 | | hevc_short_term_ref_pic_set_t *p_sets ) |
700 | 0 | { |
701 | 0 | if( stRpsIdx && bs_read1( p_bs ) ) /* Interref pic set prediction flag */ |
702 | 0 | { |
703 | 0 | nal_ue_t delta_idx_minus_1 = 0; |
704 | 0 | if( stRpsIdx == num_short_term_ref_pic_sets ) |
705 | 0 | { |
706 | 0 | delta_idx_minus_1 = bs_read_ue( p_bs ); |
707 | 0 | if( delta_idx_minus_1 >= stRpsIdx ) |
708 | 0 | return false; |
709 | 0 | } |
710 | 0 | if(delta_idx_minus_1 == stRpsIdx) |
711 | 0 | return false; |
712 | | |
713 | 0 | nal_u1_t delta_rps_sign = bs_read1( p_bs ); |
714 | 0 | nal_ue_t abs_delta_rps_minus1 = bs_read_ue( p_bs ); |
715 | 0 | unsigned RefRpsIdx = stRpsIdx - delta_idx_minus_1 - 1; |
716 | 0 | int deltaRps = ( 1 - ( delta_rps_sign << 1 ) ) * ( abs_delta_rps_minus1 + 1 ); |
717 | 0 | VLC_UNUSED(deltaRps); |
718 | |
|
719 | 0 | unsigned numDeltaPocs = p_sets[RefRpsIdx].num_delta_pocs; |
720 | 0 | p_sets[stRpsIdx].num_delta_pocs = 0; |
721 | 0 | for( unsigned j=0; j<= numDeltaPocs; j++ ) |
722 | 0 | { |
723 | 0 | if( ! bs_read1( p_bs ) ) /* used_by_curr_pic_flag */ |
724 | 0 | { |
725 | 0 | if( bs_read1( p_bs ) ) /* use_delta_flag */ |
726 | 0 | p_sets[stRpsIdx].num_delta_pocs++; |
727 | 0 | } |
728 | 0 | else |
729 | 0 | p_sets[stRpsIdx].num_delta_pocs++; |
730 | 0 | } |
731 | 0 | } |
732 | 0 | else |
733 | 0 | { |
734 | 0 | nal_ue_t num_negative_pics = bs_read_ue( p_bs ); |
735 | 0 | nal_ue_t num_positive_pics = bs_read_ue( p_bs ); |
736 | 0 | if( bs_error( p_bs ) ) |
737 | 0 | return false; |
738 | 0 | for(unsigned int i=0; i<num_negative_pics; i++) |
739 | 0 | { |
740 | 0 | (void) bs_read_ue( p_bs ); /* delta_poc_s0_minus1 */ |
741 | 0 | (void) bs_read1( p_bs ); /* used_by_current_pic_s0_flag */ |
742 | 0 | } |
743 | 0 | for(unsigned int i=0; i<num_positive_pics; i++) |
744 | 0 | { |
745 | 0 | (void) bs_read_ue( p_bs ); /* delta_poc_s1_minus1 */ |
746 | 0 | (void) bs_read1( p_bs ); /* used_by_current_pic_s1_flag */ |
747 | 0 | } |
748 | 0 | p_sets[stRpsIdx].num_delta_pocs = num_positive_pics + num_negative_pics; |
749 | 0 | } |
750 | | |
751 | 0 | return !bs_error( p_bs ); |
752 | 0 | } |
753 | | |
754 | | static bool hevc_parse_sequence_parameter_set_rbsp( bs_t *p_bs, |
755 | | hevc_sequence_parameter_set_t *p_sps ) |
756 | 0 | { |
757 | 0 | p_sps->sps_video_parameter_set_id = bs_read( p_bs, 4 ); |
758 | 0 | p_sps->sps_max_sub_layers_minus1 = bs_read( p_bs, 3 ); |
759 | 0 | p_sps->sps_temporal_id_nesting_flag = bs_read1( p_bs ); |
760 | 0 | if( !hevc_parse_profile_tier_level_rbsp( p_bs, true, p_sps->sps_max_sub_layers_minus1, |
761 | 0 | &p_sps->profile_tier_level ) ) |
762 | 0 | return false; |
763 | | |
764 | 0 | if( bs_error( p_bs ) ) |
765 | 0 | return false; |
766 | | |
767 | 0 | p_sps->sps_seq_parameter_set_id = bs_read_ue( p_bs ); |
768 | 0 | if( p_sps->sps_seq_parameter_set_id > HEVC_SPS_ID_MAX ) |
769 | 0 | return false; |
770 | | |
771 | 0 | p_sps->chroma_format_idc = bs_read_ue( p_bs ); |
772 | 0 | if( p_sps->chroma_format_idc == 3 ) |
773 | 0 | p_sps->separate_colour_plane_flag = bs_read1( p_bs ); |
774 | 0 | p_sps->pic_width_in_luma_samples = bs_read_ue( p_bs ); |
775 | 0 | p_sps->pic_height_in_luma_samples = bs_read_ue( p_bs ); |
776 | 0 | if( !p_sps->pic_width_in_luma_samples || !p_sps->pic_height_in_luma_samples ) |
777 | 0 | return false; |
778 | | |
779 | 0 | p_sps->conformance_window_flag = bs_read1( p_bs ); |
780 | 0 | if( p_sps->conformance_window_flag ) |
781 | 0 | { |
782 | 0 | p_sps->conf_win.left_offset = bs_read_ue( p_bs ); |
783 | 0 | p_sps->conf_win.right_offset = bs_read_ue( p_bs ); |
784 | 0 | p_sps->conf_win.top_offset = bs_read_ue( p_bs ); |
785 | 0 | p_sps->conf_win.bottom_offset = bs_read_ue( p_bs ); |
786 | 0 | } |
787 | |
|
788 | 0 | p_sps->bit_depth_luma_minus8 = bs_read_ue( p_bs ); |
789 | 0 | p_sps->bit_depth_chroma_minus8 = bs_read_ue( p_bs ); |
790 | 0 | p_sps->log2_max_pic_order_cnt_lsb_minus4 = bs_read_ue( p_bs ); |
791 | |
|
792 | 0 | p_sps->sps_sub_layer_ordering_info_present_flag = bs_read1( p_bs ); |
793 | 0 | for( uint8_t i=(p_sps->sps_sub_layer_ordering_info_present_flag ? 0 : p_sps->sps_max_sub_layers_minus1); |
794 | 0 | i <= p_sps->sps_max_sub_layers_minus1; i++ ) |
795 | 0 | { |
796 | 0 | p_sps->sps_max[i].dec_pic_buffering_minus1 = bs_read_ue( p_bs ); |
797 | 0 | p_sps->sps_max[i].num_reorder_pics = bs_read_ue( p_bs ); |
798 | 0 | p_sps->sps_max[i].latency_increase_plus1 = bs_read_ue( p_bs ); |
799 | 0 | } |
800 | |
|
801 | 0 | if( bs_eof( p_bs ) ) |
802 | 0 | return false; |
803 | | |
804 | 0 | p_sps->log2_min_luma_coding_block_size_minus3 = bs_read_ue( p_bs ); |
805 | 0 | p_sps->log2_diff_max_min_luma_coding_block_size = bs_read_ue( p_bs ); |
806 | 0 | p_sps->log2_min_luma_transform_block_size_minus2 = bs_read_ue( p_bs ); |
807 | 0 | p_sps->log2_diff_max_min_luma_transform_block_size = bs_read_ue( p_bs ); |
808 | | |
809 | | /* parsing incomplete */ |
810 | |
|
811 | 0 | p_sps->max_transform_hierarchy_depth_inter = bs_read_ue( p_bs ); |
812 | 0 | p_sps->max_transform_hierarchy_depth_intra = bs_read_ue( p_bs ); |
813 | 0 | p_sps->scaling_list_enabled = bs_read1( p_bs ); |
814 | 0 | if( p_sps->scaling_list_enabled ) |
815 | 0 | { |
816 | 0 | p_sps->sps_scaling_list_data_present_flag = bs_read1( p_bs ); |
817 | 0 | if( p_sps->sps_scaling_list_data_present_flag && |
818 | 0 | ! hevc_parse_scaling_list_rbsp( p_bs ) ) |
819 | 0 | { |
820 | 0 | return false; |
821 | 0 | } |
822 | 0 | } |
823 | | |
824 | 0 | if( bs_error( p_bs ) ) |
825 | 0 | return false; |
826 | | |
827 | 0 | p_sps->amp_enabled_flag = bs_read1( p_bs ); |
828 | 0 | p_sps->sample_adaptive_offset_enabled_flag = bs_read1( p_bs ); |
829 | |
|
830 | 0 | p_sps->pcm_enabled_flag = bs_read1( p_bs ); |
831 | 0 | if( p_sps->pcm_enabled_flag ) |
832 | 0 | { |
833 | 0 | p_sps->pcm_sample_bit_depth_luma_minus1 = bs_read( p_bs, 4 ); |
834 | 0 | p_sps->pcm_sample_bit_depth_chroma_minus1 = bs_read( p_bs, 4 ); |
835 | 0 | p_sps->log2_min_pcm_luma_coding_block_size_minus3 = bs_read_ue( p_bs ); |
836 | 0 | p_sps->log2_diff_max_min_pcm_luma_coding_block_size = bs_read_ue( p_bs ); |
837 | 0 | p_sps->pcm_loop_filter_disabled_flag = bs_read1( p_bs ); |
838 | 0 | } |
839 | |
|
840 | 0 | p_sps->num_short_term_ref_pic_sets = bs_read_ue( p_bs ); |
841 | 0 | if( p_sps->num_short_term_ref_pic_sets > HEVC_MAX_SHORT_TERM_REF_PIC_SET ) |
842 | 0 | return false; |
843 | | |
844 | 0 | hevc_short_term_ref_pic_set_t sets[HEVC_MAX_SHORT_TERM_REF_PIC_SET]; |
845 | 0 | memset(&sets, 0, sizeof(hevc_short_term_ref_pic_set_t) * HEVC_MAX_SHORT_TERM_REF_PIC_SET); |
846 | 0 | for( unsigned int i=0; i<p_sps->num_short_term_ref_pic_sets; i++ ) |
847 | 0 | { |
848 | 0 | if( !hevc_parse_st_ref_pic_set( p_bs, i, p_sps->num_short_term_ref_pic_sets, sets ) ) |
849 | 0 | return false; |
850 | 0 | } |
851 | | |
852 | 0 | p_sps->long_term_ref_pics_present_flag = bs_read1( p_bs ); |
853 | 0 | if( p_sps->long_term_ref_pics_present_flag ) |
854 | 0 | { |
855 | 0 | p_sps->num_long_term_ref_pics_sps = bs_read_ue( p_bs ); |
856 | 0 | if( p_sps->num_long_term_ref_pics_sps > HEVC_MAX_LONG_TERM_REF_PIC_SET ) |
857 | 0 | return false; |
858 | 0 | for( unsigned int i=0; i< p_sps->num_long_term_ref_pics_sps; i++ ) |
859 | 0 | { |
860 | | /* lt_ref_pic_poc_lsb_sps */ |
861 | 0 | bs_skip( p_bs, p_sps->log2_max_pic_order_cnt_lsb_minus4 + 4 ); |
862 | | /* used_by_curr_pic_lt_sps_flag */ |
863 | 0 | bs_skip( p_bs, 1 ); |
864 | 0 | } |
865 | 0 | } |
866 | | |
867 | 0 | p_sps->sps_temporal_mvp_enabled_flag = bs_read1( p_bs ); |
868 | 0 | p_sps->strong_intra_smoothing_enabled_flag = bs_read1( p_bs ); |
869 | |
|
870 | 0 | if( bs_error( p_bs ) ) |
871 | 0 | return false; |
872 | | |
873 | 0 | p_sps->vui_parameters_present_flag = bs_read1( p_bs ); |
874 | 0 | if( p_sps->vui_parameters_present_flag ) |
875 | 0 | { |
876 | 0 | bs_t rollbackpoint = *p_bs; |
877 | 0 | if( !hevc_parse_vui_parameters_rbsp( p_bs, &p_sps->vui, false ) && |
878 | 0 | p_sps->vui.default_display_window_flag && |
879 | 0 | !bs_error( p_bs ) ) |
880 | 0 | { |
881 | | /* Broken MKV SPS vui bitstreams with missing display_window bits. |
882 | | * Forced to accept it since some decided to accept it... |
883 | | * see https://trac.ffmpeg.org/ticket/6644 |
884 | | * Might break decoders since cropping & clock rate have totally |
885 | | * funky values when decoded properly */ |
886 | 0 | *p_bs = rollbackpoint; |
887 | 0 | memset( &p_sps->vui, 0, sizeof(p_sps->vui) ); |
888 | 0 | if( !hevc_parse_vui_parameters_rbsp( p_bs, &p_sps->vui, true ) ) |
889 | 0 | return false; |
890 | 0 | } |
891 | 0 | } |
892 | | |
893 | | /* incomplete */ |
894 | | |
895 | 0 | return !bs_error( p_bs ); |
896 | 0 | } |
897 | | |
898 | | void hevc_rbsp_release_sps( hevc_sequence_parameter_set_t *p_sps ) |
899 | 0 | { |
900 | 0 | free( p_sps ); |
901 | 0 | } |
902 | | |
903 | | IMPL_hevc_generic_decode( hevc_decode_sps, hevc_sequence_parameter_set_t, |
904 | | hevc_parse_sequence_parameter_set_rbsp, hevc_rbsp_release_sps ) |
905 | | |
906 | | static bool hevc_parse_pic_parameter_set_rbsp( bs_t *p_bs, |
907 | | hevc_picture_parameter_set_t *p_pps ) |
908 | 0 | { |
909 | 0 | if( bs_eof( p_bs ) ) |
910 | 0 | return false; |
911 | 0 | p_pps->pps_pic_parameter_set_id = bs_read_ue( p_bs ); |
912 | 0 | if( p_pps->pps_pic_parameter_set_id > HEVC_PPS_ID_MAX ) |
913 | 0 | return false; |
914 | 0 | p_pps->pps_seq_parameter_set_id = bs_read_ue( p_bs ); |
915 | 0 | if( p_pps->pps_seq_parameter_set_id > HEVC_SPS_ID_MAX ) |
916 | 0 | return false; |
917 | 0 | p_pps->dependent_slice_segments_enabled_flag = bs_read1( p_bs ); |
918 | 0 | p_pps->output_flag_present_flag = bs_read1( p_bs ); |
919 | 0 | p_pps->num_extra_slice_header_bits = bs_read( p_bs, 3 ); |
920 | 0 | p_pps->sign_data_hiding_enabled_flag = bs_read1( p_bs ); |
921 | 0 | p_pps->cabac_init_present_flag = bs_read1( p_bs ); |
922 | |
|
923 | 0 | p_pps->num_ref_idx_l0_default_active_minus1 = bs_read_ue( p_bs ); |
924 | 0 | p_pps->num_ref_idx_l1_default_active_minus1 = bs_read_ue( p_bs ); |
925 | |
|
926 | 0 | p_pps->init_qp_minus26 = bs_read_se( p_bs ); |
927 | 0 | p_pps->constrained_intra_pred_flag = bs_read1( p_bs ); |
928 | 0 | p_pps->transform_skip_enabled_flag = bs_read1( p_bs ); |
929 | 0 | p_pps->cu_qp_delta_enabled_flag = bs_read1( p_bs ); |
930 | 0 | if( p_pps->cu_qp_delta_enabled_flag ) |
931 | 0 | p_pps->diff_cu_qp_delta_depth = bs_read_ue( p_bs ); |
932 | |
|
933 | 0 | if( bs_error( p_bs ) ) |
934 | 0 | return false; |
935 | | |
936 | 0 | p_pps->pps_cb_qp_offset = bs_read_se( p_bs ); |
937 | 0 | p_pps->pps_cr_qp_offset = bs_read_se( p_bs ); |
938 | 0 | p_pps->pic_slice_level_chroma_qp_offsets_present_flag = bs_read1( p_bs ); |
939 | 0 | p_pps->weighted_pred_flag = bs_read1( p_bs ); |
940 | 0 | p_pps->weighted_bipred_flag = bs_read1( p_bs ); |
941 | 0 | p_pps->transquant_bypass_enable_flag = bs_read1( p_bs ); |
942 | 0 | p_pps->tiles_enabled_flag = bs_read1( p_bs ); |
943 | 0 | p_pps->entropy_coding_sync_enabled_flag = bs_read1( p_bs ); |
944 | |
|
945 | 0 | if( p_pps->tiles_enabled_flag ) |
946 | 0 | { |
947 | 0 | p_pps->num_tile_columns_minus1 = bs_read_ue( p_bs ); /* TODO: validate max col/row values */ |
948 | 0 | p_pps->num_tile_rows_minus1 = bs_read_ue( p_bs ); /* against sps PicWidthInCtbsY */ |
949 | 0 | p_pps->uniform_spacing_flag = bs_read1( p_bs ); |
950 | 0 | if( !p_pps->uniform_spacing_flag ) |
951 | 0 | { |
952 | 0 | for( unsigned i=0; i< p_pps->num_tile_columns_minus1; i++ ) |
953 | 0 | (void) bs_read_ue( p_bs ); |
954 | 0 | for( unsigned i=0; i< p_pps->num_tile_rows_minus1; i++ ) |
955 | 0 | (void) bs_read_ue( p_bs ); |
956 | 0 | } |
957 | 0 | p_pps->loop_filter_across_tiles_enabled_flag = bs_read1( p_bs ); |
958 | 0 | if( bs_error( p_bs ) ) |
959 | 0 | return false; |
960 | 0 | } |
961 | | |
962 | 0 | p_pps->pps_loop_filter_across_slices_enabled_flag = bs_read1( p_bs ); |
963 | 0 | p_pps->deblocking_filter_control_present_flag = bs_read1( p_bs ); |
964 | 0 | if( p_pps->deblocking_filter_control_present_flag ) |
965 | 0 | { |
966 | 0 | p_pps->deblocking_filter_override_enabled_flag = bs_read1( p_bs ); |
967 | 0 | p_pps->pps_deblocking_filter_disabled_flag = bs_read1( p_bs ); |
968 | 0 | if( !p_pps->pps_deblocking_filter_disabled_flag ) |
969 | 0 | { |
970 | 0 | p_pps->pps_beta_offset_div2 = bs_read_se( p_bs ); |
971 | 0 | p_pps->pps_tc_offset_div2 = bs_read_se( p_bs ); |
972 | 0 | } |
973 | 0 | } |
974 | |
|
975 | 0 | p_pps->scaling_list_data_present_flag = bs_read1( p_bs ); |
976 | 0 | if( p_pps->scaling_list_data_present_flag && !hevc_parse_scaling_list_rbsp( p_bs ) ) |
977 | 0 | return false; |
978 | | |
979 | 0 | p_pps->lists_modification_present_flag = bs_read1( p_bs ); |
980 | 0 | p_pps->log2_parallel_merge_level_minus2 = bs_read_ue( p_bs ); |
981 | 0 | p_pps->slice_header_extension_present_flag = bs_read1( p_bs ); |
982 | |
|
983 | 0 | p_pps->pps_extension_present_flag = bs_read1( p_bs ); |
984 | 0 | if( p_pps->pps_extension_present_flag ) |
985 | 0 | { |
986 | 0 | p_pps->pps_range_extension_flag = bs_read1( p_bs ); |
987 | 0 | p_pps->pps_multilayer_extension_flag = bs_read1( p_bs ); |
988 | 0 | p_pps->pps_3d_extension_flag = bs_read1( p_bs ); |
989 | 0 | p_pps->pps_extension_5bits = bs_read( p_bs, 5 ); |
990 | 0 | } |
991 | |
|
992 | 0 | return !bs_error( p_bs ); |
993 | 0 | } |
994 | | |
995 | | void hevc_rbsp_release_pps( hevc_picture_parameter_set_t *p_pps ) |
996 | 0 | { |
997 | 0 | free( p_pps ); |
998 | 0 | } |
999 | | |
1000 | | IMPL_hevc_generic_decode( hevc_decode_pps, hevc_picture_parameter_set_t, |
1001 | | hevc_parse_pic_parameter_set_rbsp, hevc_rbsp_release_pps ) |
1002 | | |
1003 | | uint8_t hevc_get_sps_vps_id( const hevc_sequence_parameter_set_t *p_sps ) |
1004 | 0 | { |
1005 | 0 | return p_sps->sps_video_parameter_set_id; |
1006 | 0 | } |
1007 | | |
1008 | | uint8_t hevc_get_pps_sps_id( const hevc_picture_parameter_set_t *p_pps ) |
1009 | 0 | { |
1010 | 0 | return p_pps->pps_seq_parameter_set_id; |
1011 | 0 | } |
1012 | | |
1013 | | uint8_t hevc_get_slice_pps_id( const hevc_slice_segment_header_t *p_slice ) |
1014 | 0 | { |
1015 | 0 | return p_slice->slice_pic_parameter_set_id; |
1016 | 0 | } |
1017 | | |
1018 | | bool hevc_get_slice_no_output_of_prior_pics_flag( const hevc_slice_segment_header_t *p_slice ) |
1019 | 0 | { |
1020 | 0 | return p_slice->no_output_of_prior_pics_flag; |
1021 | 0 | } |
1022 | | |
1023 | | bool hevc_get_sps_profile_tier_level( const hevc_sequence_parameter_set_t *p_sps, |
1024 | | uint8_t *pi_profile, uint8_t *pi_level) |
1025 | 0 | { |
1026 | 0 | if(p_sps->profile_tier_level.general.profile_idc) |
1027 | 0 | { |
1028 | 0 | *pi_profile = p_sps->profile_tier_level.general.profile_idc; |
1029 | 0 | *pi_level = p_sps->profile_tier_level.general_level_idc; |
1030 | 0 | return true; |
1031 | 0 | } |
1032 | 0 | return false; |
1033 | 0 | } |
1034 | | |
1035 | | bool hevc_get_picture_size( const hevc_sequence_parameter_set_t *p_sps, |
1036 | | unsigned *p_ox, unsigned *p_oy, |
1037 | | unsigned *p_w, unsigned *p_h, |
1038 | | unsigned *p_vw, unsigned *p_vh ) |
1039 | 0 | { |
1040 | 0 | return h26x_get_picture_size( p_sps->chroma_format_idc, |
1041 | 0 | p_sps->pic_width_in_luma_samples, |
1042 | 0 | p_sps->pic_height_in_luma_samples, |
1043 | 0 | &p_sps->conf_win, |
1044 | 0 | p_ox, p_oy, p_w, p_h, p_vw, p_vh ); |
1045 | 0 | } |
1046 | | |
1047 | | void hevc_get_dpb_values( const hevc_sequence_parameter_set_t *p_sps, uint8_t *max_num_reorder_pics, |
1048 | | uint8_t *max_latency_pics, uint8_t *max_dec_pic_buffering ) |
1049 | 0 | { |
1050 | 0 | *max_num_reorder_pics = p_sps->sps_max[p_sps->sps_max_sub_layers_minus1/* HighestTid */].num_reorder_pics; |
1051 | 0 | *max_latency_pics = p_sps->sps_max[p_sps->sps_max_sub_layers_minus1/* HighestTid */].latency_increase_plus1; |
1052 | 0 | if(*max_latency_pics != 0) |
1053 | 0 | *max_latency_pics = *max_num_reorder_pics + *max_latency_pics - 1; |
1054 | 0 | *max_dec_pic_buffering = 1 + |
1055 | 0 | p_sps->sps_max[p_sps->sps_max_sub_layers_minus1/* HighestTid */].dec_pic_buffering_minus1; |
1056 | | /* |
1057 | | When sps_max_dec_pic_buffering_minus1[ TemporalId ] is equal to 0, the value of TwoVersionsOfCurrDecPicFlag |
1058 | | shall be equal to 0. |
1059 | | |
1060 | | pps_curr_pic_ref_enabled_flag && |
1061 | | ( sample_adaptive_offset_enabled_flag | | !pps_deblocking_filter_disabled_flag | | |
1062 | | deblocking_filter_override_enabled_flag ); |
1063 | | */ |
1064 | 0 | } |
1065 | | |
1066 | | static bool hevc_get_picture_CtbsYsize( const hevc_sequence_parameter_set_t *p_sps, unsigned *p_w, unsigned *p_h ) |
1067 | 0 | { |
1068 | 0 | const unsigned int MinCbLog2SizeY = p_sps->log2_min_luma_coding_block_size_minus3 + 3; |
1069 | 0 | const unsigned int CtbLog2SizeY = MinCbLog2SizeY + p_sps->log2_diff_max_min_luma_coding_block_size; |
1070 | 0 | if( CtbLog2SizeY > 31 ) |
1071 | 0 | return false; |
1072 | 0 | const unsigned int CtbSizeY = 1 << CtbLog2SizeY; |
1073 | 0 | *p_w = (p_sps->pic_width_in_luma_samples - 1) / CtbSizeY + 1; |
1074 | 0 | *p_h = (p_sps->pic_height_in_luma_samples - 1) / CtbSizeY + 1; |
1075 | 0 | return true; |
1076 | 0 | } |
1077 | | |
1078 | | bool hevc_get_frame_rate( const hevc_sequence_parameter_set_t *p_sps, |
1079 | | const hevc_video_parameter_set_t *p_vps, |
1080 | | unsigned *pi_num, unsigned *pi_den ) |
1081 | 0 | { |
1082 | 0 | if( p_sps->vui_parameters_present_flag && p_sps->vui.vui_timing_info_present_flag ) |
1083 | 0 | { |
1084 | 0 | *pi_den = p_sps->vui.timing.vui_num_units_in_tick; |
1085 | 0 | *pi_num = p_sps->vui.timing.vui_time_scale; |
1086 | 0 | return (*pi_den && *pi_num); |
1087 | 0 | } |
1088 | 0 | else if( p_vps && p_vps->vps_timing_info_present_flag ) |
1089 | 0 | { |
1090 | 0 | *pi_den = p_vps->vps_num_units_in_tick; |
1091 | 0 | *pi_num = p_vps->vps_time_scale; |
1092 | 0 | return (*pi_den && *pi_num); |
1093 | 0 | } |
1094 | 0 | return false; |
1095 | 0 | } |
1096 | | |
1097 | | bool hevc_get_aspect_ratio( const hevc_sequence_parameter_set_t *p_sps, |
1098 | | unsigned *num, unsigned *den ) |
1099 | 0 | { |
1100 | 0 | if( !p_sps->vui_parameters_present_flag ) |
1101 | 0 | return false; |
1102 | 0 | return h26x_get_aspect_ratio( &p_sps->vui.ar, num, den ); |
1103 | 0 | } |
1104 | | |
1105 | | bool hevc_get_chroma_luma( const hevc_sequence_parameter_set_t *p_sps, uint8_t *pi_chroma_format, |
1106 | | uint8_t *pi_depth_luma, uint8_t *pi_depth_chroma ) |
1107 | 0 | { |
1108 | 0 | *pi_chroma_format = p_sps->chroma_format_idc; |
1109 | 0 | *pi_depth_luma = p_sps->bit_depth_chroma_minus8 + 8; |
1110 | 0 | *pi_depth_chroma = p_sps->bit_depth_chroma_minus8 + 8; |
1111 | 0 | return true; |
1112 | 0 | } |
1113 | | |
1114 | | bool hevc_get_colorimetry( const hevc_sequence_parameter_set_t *p_sps, |
1115 | | video_color_primaries_t *p_primaries, |
1116 | | video_transfer_func_t *p_transfer, |
1117 | | video_color_space_t *p_colorspace, |
1118 | | video_color_range_t *p_full_range ) |
1119 | 0 | { |
1120 | 0 | if( !p_sps->vui_parameters_present_flag ) |
1121 | 0 | return false; |
1122 | 0 | return h26x_get_colorimetry( &p_sps->vui.vs.colour, |
1123 | 0 | p_primaries, p_transfer, p_colorspace, p_full_range ); |
1124 | 0 | } |
1125 | | |
1126 | | static bool hevc_parse_slice_segment_header_rbsp( bs_t *p_bs, |
1127 | | pf_get_matchedxps get_matchedxps, |
1128 | | void *priv, |
1129 | | hevc_slice_segment_header_t *p_sl ) |
1130 | 0 | { |
1131 | 0 | hevc_sequence_parameter_set_t *p_sps; |
1132 | 0 | hevc_picture_parameter_set_t *p_pps; |
1133 | 0 | hevc_video_parameter_set_t *p_vps; |
1134 | |
|
1135 | 0 | if( bs_eof( p_bs ) ) |
1136 | 0 | return false; |
1137 | | |
1138 | 0 | p_sl->first_slice_segment_in_pic_flag = bs_read1( p_bs ); |
1139 | 0 | if( p_sl->nal_type >= HEVC_NAL_BLA_W_LP && p_sl->nal_type <= HEVC_NAL_IRAP_VCL23 ) |
1140 | 0 | p_sl->no_output_of_prior_pics_flag = bs_read1( p_bs ); |
1141 | 0 | p_sl->slice_pic_parameter_set_id = bs_read_ue( p_bs ); |
1142 | 0 | if( p_sl->slice_pic_parameter_set_id > HEVC_PPS_ID_MAX ) |
1143 | 0 | return false; |
1144 | | |
1145 | 0 | if( bs_error( p_bs ) ) |
1146 | 0 | return false; |
1147 | | |
1148 | 0 | get_matchedxps( p_sl->slice_pic_parameter_set_id, priv, &p_pps, &p_sps, &p_vps ); |
1149 | 0 | if(!p_sps || !p_pps) |
1150 | 0 | return false; |
1151 | | |
1152 | 0 | if( !p_sl->first_slice_segment_in_pic_flag ) |
1153 | 0 | { |
1154 | 0 | if( p_pps->dependent_slice_segments_enabled_flag ) |
1155 | 0 | p_sl->dependent_slice_segment_flag = bs_read1( p_bs ); |
1156 | |
|
1157 | 0 | unsigned w, h; |
1158 | 0 | if( !hevc_get_picture_CtbsYsize( p_sps, &w, &h ) ) |
1159 | 0 | return false; |
1160 | | |
1161 | 0 | (void) bs_read( p_bs, stdc_bit_width( w * h - 1 ) ); /* slice_segment_address */ |
1162 | 0 | } |
1163 | | |
1164 | 0 | if( !p_sl->dependent_slice_segment_flag ) |
1165 | 0 | { |
1166 | 0 | unsigned i=0; |
1167 | 0 | if( p_pps->num_extra_slice_header_bits > i ) |
1168 | 0 | { |
1169 | 0 | i++; |
1170 | 0 | bs_skip( p_bs, 1 ); /* discardable_flag */ |
1171 | 0 | } |
1172 | |
|
1173 | 0 | if( p_pps->num_extra_slice_header_bits > i ) |
1174 | 0 | { |
1175 | 0 | i++; |
1176 | 0 | bs_skip( p_bs, 1 ); /* cross_layer_bla_flag */ |
1177 | 0 | } |
1178 | |
|
1179 | 0 | if( i < p_pps->num_extra_slice_header_bits ) |
1180 | 0 | bs_skip( p_bs, p_pps->num_extra_slice_header_bits - i ); |
1181 | |
|
1182 | 0 | p_sl->slice_type = bs_read_ue( p_bs ); |
1183 | 0 | if( p_sl->slice_type > HEVC_SLICE_TYPE_I ) |
1184 | 0 | return false; |
1185 | | |
1186 | 0 | if( p_pps->output_flag_present_flag ) |
1187 | 0 | p_sl->pic_output_flag = bs_read1( p_bs ); |
1188 | 0 | else |
1189 | 0 | p_sl->pic_output_flag = 1; |
1190 | 0 | } |
1191 | | |
1192 | 0 | if( p_sps->separate_colour_plane_flag ) |
1193 | 0 | bs_skip( p_bs, 2 ); /* colour_plane_id */ |
1194 | |
|
1195 | 0 | if( p_sl->nal_type != HEVC_NAL_IDR_W_RADL && p_sl->nal_type != HEVC_NAL_IDR_N_LP ) |
1196 | 0 | p_sl->pic_order_cnt_lsb = bs_read( p_bs, p_sps->log2_max_pic_order_cnt_lsb_minus4 + 4 ); |
1197 | 0 | else |
1198 | 0 | p_sl->pic_order_cnt_lsb = 0; |
1199 | |
|
1200 | 0 | return !bs_error( p_bs ); |
1201 | 0 | } |
1202 | | |
1203 | | void hevc_rbsp_release_slice_header( hevc_slice_segment_header_t *p_sh ) |
1204 | 0 | { |
1205 | 0 | free( p_sh ); |
1206 | 0 | } |
1207 | | |
1208 | | hevc_slice_segment_header_t * hevc_decode_slice_header( const uint8_t *p_buf, size_t i_buf, bool b_escaped, |
1209 | | pf_get_matchedxps get_matchedxps, void *priv ) |
1210 | 0 | { |
1211 | 0 | hevc_slice_segment_header_t *p_sh = calloc(1, sizeof(hevc_slice_segment_header_t)); |
1212 | 0 | if(likely(p_sh)) |
1213 | 0 | { |
1214 | 0 | bs_t bs; |
1215 | 0 | struct hxxx_bsfw_ep3b_ctx_s bsctx; |
1216 | 0 | if( b_escaped ) |
1217 | 0 | { |
1218 | 0 | hxxx_bsfw_ep3b_ctx_init( &bsctx ); |
1219 | 0 | bs_init_custom( &bs, p_buf, i_buf, &hxxx_bsfw_ep3b_callbacks, &bsctx ); |
1220 | 0 | } |
1221 | 0 | else bs_init( &bs, p_buf, i_buf ); |
1222 | 0 | bs_skip( &bs, 1 ); |
1223 | 0 | p_sh->nal_type = bs_read( &bs, 6 ); |
1224 | 0 | p_sh->nuh_layer_id = bs_read( &bs, 6 ); |
1225 | 0 | p_sh->temporal_id_plus1 = bs_read( &bs, 3 ); |
1226 | 0 | if( p_sh->nuh_layer_id > 62 || p_sh->temporal_id_plus1 == 0 || |
1227 | 0 | !hevc_parse_slice_segment_header_rbsp( &bs, get_matchedxps, priv, p_sh ) ) |
1228 | 0 | { |
1229 | 0 | hevc_rbsp_release_slice_header( p_sh ); |
1230 | 0 | p_sh = NULL; |
1231 | 0 | } |
1232 | 0 | } |
1233 | 0 | return p_sh; |
1234 | 0 | } |
1235 | | |
1236 | | bool hevc_get_slice_type( const hevc_slice_segment_header_t *p_sli, enum hevc_slice_type_e *pi_type ) |
1237 | 0 | { |
1238 | 0 | if( !p_sli->dependent_slice_segment_flag ) |
1239 | 0 | { |
1240 | 0 | *pi_type = p_sli->slice_type; |
1241 | 0 | return true; |
1242 | 0 | } |
1243 | 0 | return false; |
1244 | 0 | } |
1245 | | |
1246 | | bool hevc_get_slice_pic_output( const hevc_slice_segment_header_t *p_sli ) |
1247 | 0 | { |
1248 | 0 | return p_sli->pic_output_flag; |
1249 | 0 | } |
1250 | | |
1251 | | bool hevc_get_profile_level(const es_format_t *p_fmt, uint8_t *pi_profile, |
1252 | | uint8_t *pi_level, uint8_t *pi_nal_length_size) |
1253 | 0 | { |
1254 | 0 | const uint8_t *p = (const uint8_t*)p_fmt->p_extra; |
1255 | 0 | if(p_fmt->i_extra < 23 || p[0] != 1) |
1256 | 0 | return false; |
1257 | | |
1258 | | /* HEVCDecoderConfigurationRecord */ |
1259 | 0 | if(pi_profile) |
1260 | 0 | *pi_profile = p[1] & 0x1F; |
1261 | |
|
1262 | 0 | if(pi_level) |
1263 | 0 | *pi_level = p[12]; |
1264 | |
|
1265 | 0 | if (pi_nal_length_size) |
1266 | 0 | *pi_nal_length_size = 1 + (p[21]&0x03); |
1267 | |
|
1268 | 0 | return true; |
1269 | 0 | } |
1270 | | |
1271 | | static unsigned hevc_make_indication( const hevc_inner_profile_tier_level_t *p ) |
1272 | 0 | { |
1273 | 0 | uint8_t flags[] = |
1274 | 0 | { |
1275 | 0 | p->idc4to7.max_14bit_constraint_flag, |
1276 | 0 | p->idc4to7.max_12bit_constraint_flag, |
1277 | 0 | p->idc4to7.max_10bit_constraint_flag, |
1278 | 0 | p->idc4to7.max_8bit_constraint_flag, |
1279 | 0 | p->idc4to7.max_422chroma_constraint_flag, |
1280 | 0 | p->idc4to7.max_420chroma_constraint_flag, |
1281 | 0 | p->idc4to7.max_monochrome_constraint_flag, |
1282 | 0 | p->idc4to7.intra_constraint_flag, |
1283 | 0 | p->idc4to7.one_picture_only_constraint_flag, |
1284 | 0 | p->idc4to7.lower_bit_rate_constraint_flag, |
1285 | 0 | }; |
1286 | 0 | unsigned indication = 0; |
1287 | 0 | for( size_t i=0; i<ARRAY_SIZE(flags); i++ ) |
1288 | 0 | { |
1289 | 0 | if( flags[i] ) |
1290 | 0 | indication |= (1 << (ARRAY_SIZE(flags) - 1 - i)); |
1291 | 0 | } |
1292 | 0 | return indication; |
1293 | 0 | } |
1294 | | |
1295 | | enum vlc_hevc_profile_e hevc_get_vlc_profile( const hevc_sequence_parameter_set_t *p_sps ) |
1296 | 0 | { |
1297 | 0 | unsigned indication = 0; |
1298 | 0 | enum hevc_general_profile_idc_e profile = p_sps->profile_tier_level.general.profile_idc; |
1299 | 0 | switch( profile ) |
1300 | 0 | { |
1301 | 0 | case HEVC_PROFILE_IDC_REXT: |
1302 | 0 | indication = hevc_make_indication( &p_sps->profile_tier_level.general ) & 0x1FF; |
1303 | 0 | break; |
1304 | 0 | case HEVC_PROFILE_IDC_HIGH_THROUGHPUT: |
1305 | 0 | case HEVC_PROFILE_IDC_SCREEN_EXTENDED: |
1306 | 0 | indication = hevc_make_indication( &p_sps->profile_tier_level.general ); |
1307 | 0 | break; |
1308 | 0 | default: |
1309 | 0 | break; |
1310 | 0 | } |
1311 | | |
1312 | | /* all intras have insignifiant lowest bit */ |
1313 | 0 | if( p_sps->profile_tier_level.general.idc4to7.intra_constraint_flag ) |
1314 | 0 | indication &= ~1; |
1315 | |
|
1316 | 0 | return (indication << HEVC_INDICATION_SHIFT) | profile; |
1317 | 0 | } |
1318 | | |
1319 | | /* |
1320 | | * HEVCDecoderConfigurationRecord operations |
1321 | | */ |
1322 | | |
1323 | | static void hevc_dcr_params_from_vps( const uint8_t * p_buffer, size_t i_buffer, |
1324 | | struct hevc_dcr_values *p_values ) |
1325 | 0 | { |
1326 | 0 | if( i_buffer < 19 ) |
1327 | 0 | return; |
1328 | | |
1329 | 0 | bs_t bs; |
1330 | 0 | struct hxxx_bsfw_ep3b_ctx_s bsctx; |
1331 | 0 | hxxx_bsfw_ep3b_ctx_init( &bsctx ); |
1332 | 0 | bs_init_custom( &bs, p_buffer, i_buffer, &hxxx_bsfw_ep3b_callbacks, &bsctx ); |
1333 | | |
1334 | | /* first two bytes are the NAL header, 3rd and 4th are: |
1335 | | vps_video_parameter_set_id(4) |
1336 | | vps_reserved_3_2bis(2) |
1337 | | vps_max_layers_minus1(6) |
1338 | | vps_max_sub_layers_minus1(3) |
1339 | | vps_temporal_id_nesting_flags |
1340 | | */ |
1341 | 0 | bs_skip( &bs, 16 + 4 + 2 + 6 ); |
1342 | 0 | p_values->i_numTemporalLayer = bs_read( &bs, 3 ) + 1; |
1343 | 0 | p_values->b_temporalIdNested = bs_read1( &bs ); |
1344 | | |
1345 | | /* 5th & 6th are reserved 0xffff */ |
1346 | 0 | bs_skip( &bs, 16 ); |
1347 | | /* copy the first 12 bytes of profile tier */ |
1348 | 0 | for( unsigned i=0; i<12; i++ ) |
1349 | 0 | p_values->general_configuration[i] = bs_read( &bs, 8 ); |
1350 | 0 | } |
1351 | | |
1352 | 0 | #define HEVC_DCR_ADD_NALS(type, count, buffers, sizes) \ |
1353 | 0 | for (uint8_t i = 0; i < count; i++) \ |
1354 | 0 | { \ |
1355 | 0 | if( i ==0 ) \ |
1356 | 0 | { \ |
1357 | 0 | *p++ = (type | (b_completeness ? 0x80 : 0)); \ |
1358 | 0 | SetWBE( p, count ); p += 2; \ |
1359 | 0 | } \ |
1360 | 0 | SetWBE( p, sizes[i]); p += 2; \ |
1361 | 0 | memcpy( p, buffers[i], sizes[i] ); p += sizes[i];\ |
1362 | 0 | } |
1363 | | |
1364 | 0 | #define HEVC_DCR_ADD_SIZES(count, sizes) \ |
1365 | 0 | if(count > 0) \ |
1366 | 0 | {\ |
1367 | 0 | i_total_size += 3;\ |
1368 | 0 | for(uint8_t i=0; i<count; i++)\ |
1369 | 0 | i_total_size += 2 + sizes[i];\ |
1370 | 0 | } |
1371 | | |
1372 | | /* Generate HEVCDecoderConfiguration iso/iec 14496-15 3rd edition */ |
1373 | | uint8_t * hevc_create_dcr( const struct hevc_dcr_params *p_params, |
1374 | | uint8_t i_nal_length_size, |
1375 | | bool b_completeness, size_t *pi_size ) |
1376 | 0 | { |
1377 | 0 | *pi_size = 0; |
1378 | |
|
1379 | 0 | if( i_nal_length_size != 1 && i_nal_length_size != 2 && i_nal_length_size != 4 ) |
1380 | 0 | return NULL; |
1381 | | |
1382 | 0 | struct hevc_dcr_values values = |
1383 | 0 | { |
1384 | 0 | .general_configuration = {0}, |
1385 | 0 | .i_numTemporalLayer = 0, |
1386 | 0 | .i_chroma_idc = 1, |
1387 | 0 | .i_bit_depth_luma_minus8 = 0, |
1388 | 0 | .i_bit_depth_chroma_minus8 = 0, |
1389 | 0 | .b_temporalIdNested = false, |
1390 | 0 | }; |
1391 | |
|
1392 | 0 | if( p_params->p_values != NULL ) |
1393 | 0 | { |
1394 | 0 | values = *p_params->p_values; |
1395 | 0 | } |
1396 | 0 | else |
1397 | 0 | { |
1398 | 0 | if( p_params->i_vps_count == 0 || p_params->i_sps_count == 0 ) |
1399 | 0 | return NULL; /* required to extract info */ |
1400 | | |
1401 | 0 | hevc_dcr_params_from_vps( p_params->p_vps[0], p_params->rgi_vps[0], &values ); |
1402 | |
|
1403 | 0 | hevc_sequence_parameter_set_t *p_sps = |
1404 | 0 | hevc_decode_sps( p_params->p_sps[0], p_params->rgi_sps[0], true ); |
1405 | 0 | if( p_sps ) |
1406 | 0 | { |
1407 | 0 | values.i_chroma_idc = p_sps->chroma_format_idc; |
1408 | 0 | values.i_bit_depth_chroma_minus8 = p_sps->bit_depth_chroma_minus8; |
1409 | 0 | values.i_bit_depth_luma_minus8 = p_sps->bit_depth_luma_minus8; |
1410 | 0 | hevc_rbsp_release_sps( p_sps ); |
1411 | 0 | } |
1412 | 0 | } |
1413 | | |
1414 | 0 | size_t i_total_size = 1+12+2+4+2+2; |
1415 | 0 | HEVC_DCR_ADD_SIZES(p_params->i_vps_count, p_params->rgi_vps); |
1416 | 0 | HEVC_DCR_ADD_SIZES(p_params->i_sps_count, p_params->rgi_sps); |
1417 | 0 | HEVC_DCR_ADD_SIZES(p_params->i_pps_count, p_params->rgi_pps); |
1418 | 0 | HEVC_DCR_ADD_SIZES(p_params->i_seipref_count, p_params->rgi_seipref); |
1419 | 0 | HEVC_DCR_ADD_SIZES(p_params->i_seisuff_count, p_params->rgi_seisuff); |
1420 | |
|
1421 | 0 | uint8_t *p_data = malloc( i_total_size ); |
1422 | 0 | if( p_data == NULL ) |
1423 | 0 | return NULL; |
1424 | | |
1425 | 0 | *pi_size = i_total_size; |
1426 | 0 | uint8_t *p = p_data; |
1427 | | |
1428 | | /* version */ |
1429 | 0 | *p++ = 0x01; |
1430 | 0 | memcpy( p, values.general_configuration, 12 ); p += 12; |
1431 | | /* Don't set min spatial segmentation */ |
1432 | 0 | SetWBE( p, 0xF000 ); p += 2; |
1433 | | /* Don't set parallelism type since segmentation isn't set */ |
1434 | 0 | *p++ = 0xFC; |
1435 | 0 | *p++ = (0xFC | (values.i_chroma_idc & 0x03)); |
1436 | 0 | *p++ = (0xF8 | (values.i_bit_depth_luma_minus8 & 0x07)); |
1437 | 0 | *p++ = (0xF8 | (values.i_bit_depth_chroma_minus8 & 0x07)); |
1438 | | |
1439 | | /* Don't set framerate */ |
1440 | 0 | SetWBE( p, 0x0000); p += 2; |
1441 | | /* Force NAL size of 4 bytes that replace the startcode */ |
1442 | 0 | *p++ = ( ((values.i_numTemporalLayer & 0x07) << 3) | |
1443 | 0 | (values.b_temporalIdNested << 2) | |
1444 | 0 | (i_nal_length_size - 1) ); |
1445 | | /* total number of arrays */ |
1446 | 0 | *p++ = !!p_params->i_vps_count + !!p_params->i_sps_count + |
1447 | 0 | !!p_params->i_pps_count + !!p_params->i_seipref_count + |
1448 | 0 | !!p_params->i_seisuff_count; |
1449 | | |
1450 | | /* Write NAL arrays */ |
1451 | 0 | HEVC_DCR_ADD_NALS(HEVC_NAL_VPS, p_params->i_vps_count, |
1452 | 0 | p_params->p_vps, p_params->rgi_vps); |
1453 | 0 | HEVC_DCR_ADD_NALS(HEVC_NAL_SPS, p_params->i_sps_count, |
1454 | 0 | p_params->p_sps, p_params->rgi_sps); |
1455 | 0 | HEVC_DCR_ADD_NALS(HEVC_NAL_PPS, p_params->i_pps_count, |
1456 | 0 | p_params->p_pps, p_params->rgi_pps); |
1457 | 0 | HEVC_DCR_ADD_NALS(HEVC_NAL_PREF_SEI, p_params->i_seipref_count, |
1458 | 0 | p_params->p_seipref, p_params->rgi_seipref); |
1459 | 0 | HEVC_DCR_ADD_NALS(HEVC_NAL_SUFF_SEI, p_params->i_seisuff_count, |
1460 | 0 | p_params->p_seisuff, p_params->rgi_seisuff); |
1461 | |
|
1462 | 0 | return p_data; |
1463 | 0 | } |
1464 | | |
1465 | | #undef HEVC_DCR_ADD_NALS |
1466 | | #undef HEVC_DCR_ADD_SIZES |
1467 | | |
1468 | | bool hevc_NAL_IsIRAP( uint8_t i_nal_type ) |
1469 | 0 | { |
1470 | 0 | return ( i_nal_type >= HEVC_NAL_BLA_W_LP && |
1471 | 0 | i_nal_type <= HEVC_NAL_IRAP_VCL23 ); |
1472 | 0 | } |
1473 | | |
1474 | | bool hevc_get_IRAPNoRaslOutputFlag( uint8_t i_nal_type, |
1475 | | const hevc_poc_ctx_t *p_ctx ) |
1476 | 0 | { |
1477 | | /* if( IRAP ) NoRaslOutputFlag = first || IDR || BLA || after(EOSNAL) */ |
1478 | 0 | return ( p_ctx->first_picture || |
1479 | 0 | i_nal_type == HEVC_NAL_IDR_N_LP || |
1480 | 0 | i_nal_type == HEVC_NAL_IDR_W_RADL || |
1481 | 0 | i_nal_type == HEVC_NAL_BLA_W_LP || |
1482 | 0 | i_nal_type == HEVC_NAL_BLA_W_RADL || |
1483 | 0 | i_nal_type == HEVC_NAL_BLA_N_LP || |
1484 | 0 | p_ctx->HandleCraAsBlaFlag ); |
1485 | 0 | } |
1486 | | |
1487 | | /* |
1488 | | * 8.3.1 Decoding process for POC |
1489 | | */ |
1490 | | int hevc_compute_picture_order_count( const hevc_sequence_parameter_set_t *p_sps, |
1491 | | const hevc_slice_segment_header_t *p_slice, |
1492 | | hevc_poc_ctx_t *p_ctx ) |
1493 | 0 | { |
1494 | 0 | struct |
1495 | 0 | { |
1496 | 0 | int lsb; |
1497 | 0 | int msb; |
1498 | 0 | } prevPicOrderCnt; |
1499 | 0 | int pocMSB; |
1500 | 0 | const bool IsIRAP = hevc_NAL_IsIRAP( p_slice->nal_type ); |
1501 | 0 | const bool NoRaslOutputFlag = |
1502 | 0 | IsIRAP ? hevc_get_IRAPNoRaslOutputFlag( p_slice->nal_type, p_ctx ) : false; |
1503 | |
|
1504 | | #ifdef HEVC_POC_DEBUG |
1505 | | printf("slice lsb=%"PRIu32" irap=%d norasl=%d tid=%d msb=%d lsb=%d", |
1506 | | p_slice->pic_order_cnt_lsb, |
1507 | | IsIRAP, NoRaslOutputFlag, p_slice->temporal_id_plus1, |
1508 | | p_ctx->prevTid0PicOrderCnt.msb, p_ctx->prevTid0PicOrderCnt.lsb); |
1509 | | #endif |
1510 | |
|
1511 | 0 | if( p_slice->nal_type == HEVC_NAL_IDR_N_LP || |
1512 | 0 | p_slice->nal_type == HEVC_NAL_IDR_W_RADL ) |
1513 | 0 | { |
1514 | 0 | prevPicOrderCnt.msb = 0; |
1515 | 0 | prevPicOrderCnt.lsb = 0; |
1516 | 0 | } |
1517 | | /* Not an IRAP with NoRaslOutputFlag == 1 */ |
1518 | 0 | else if( !IsIRAP || !NoRaslOutputFlag ) |
1519 | 0 | { |
1520 | 0 | prevPicOrderCnt.msb = p_ctx->prevTid0PicOrderCnt.msb; |
1521 | 0 | prevPicOrderCnt.lsb = p_ctx->prevTid0PicOrderCnt.lsb; |
1522 | 0 | } |
1523 | |
|
1524 | 0 | if( IsIRAP && NoRaslOutputFlag ) |
1525 | 0 | { |
1526 | 0 | pocMSB = 0; |
1527 | 0 | } |
1528 | 0 | else |
1529 | 0 | { |
1530 | 0 | const unsigned maxPocLSB = 1U << (p_sps->log2_max_pic_order_cnt_lsb_minus4 + 4); |
1531 | 0 | pocMSB = prevPicOrderCnt.msb; |
1532 | 0 | int64_t orderDiff = (int64_t)p_slice->pic_order_cnt_lsb - prevPicOrderCnt.lsb; |
1533 | 0 | if( orderDiff < 0 && -orderDiff >= maxPocLSB / 2 ) |
1534 | 0 | pocMSB += maxPocLSB; |
1535 | 0 | else if( orderDiff > maxPocLSB / 2 ) |
1536 | 0 | pocMSB -= maxPocLSB; |
1537 | 0 | } |
1538 | | |
1539 | | /* Set prevTid0Pic for next pic */ |
1540 | 0 | if( p_slice->temporal_id_plus1 == 1 && |
1541 | 0 | !( ( p_slice->nal_type <= HEVC_NAL_RSV_VCL_N14 && p_slice->nal_type % 2 == 0 /* SLNR */ ) || |
1542 | 0 | ( p_slice->nal_type >= HEVC_NAL_RADL_N && p_slice->nal_type <= HEVC_NAL_RASL_R ) /* RADL or RASL */ ) ) |
1543 | 0 | { |
1544 | 0 | p_ctx->prevTid0PicOrderCnt.msb = pocMSB; |
1545 | 0 | p_ctx->prevTid0PicOrderCnt.lsb = p_slice->pic_order_cnt_lsb; |
1546 | 0 | } |
1547 | |
|
1548 | 0 | p_ctx->first_picture = false; |
1549 | |
|
1550 | | #ifdef HEVC_POC_DEBUG |
1551 | | printf(" POC=%"PRIu32"\n", pocMSB + p_slice->pic_order_cnt_lsb); |
1552 | | #endif |
1553 | |
|
1554 | 0 | return pocMSB + p_slice->pic_order_cnt_lsb; |
1555 | 0 | } |
1556 | | |
1557 | | struct hevc_sei_pic_timing_t |
1558 | | { |
1559 | | nal_u4_t pic_struct; |
1560 | | nal_u2_t source_scan_type; |
1561 | | }; |
1562 | | |
1563 | | void hevc_release_sei_pic_timing( hevc_sei_pic_timing_t *p_timing ) |
1564 | 0 | { |
1565 | 0 | free( p_timing ); |
1566 | 0 | } |
1567 | | |
1568 | | hevc_sei_pic_timing_t * hevc_decode_sei_pic_timing( bs_t *p_bs, |
1569 | | const hevc_sequence_parameter_set_t *p_sps ) |
1570 | 0 | { |
1571 | 0 | hevc_sei_pic_timing_t *p_timing = malloc(sizeof(*p_timing)); |
1572 | 0 | if( p_timing ) |
1573 | 0 | { |
1574 | 0 | if( p_sps->vui.frame_field_info_present_flag ) |
1575 | 0 | { |
1576 | 0 | p_timing->pic_struct = bs_read( p_bs, 4 ); |
1577 | 0 | p_timing->source_scan_type = bs_read( p_bs, 2 ); |
1578 | 0 | } |
1579 | 0 | else |
1580 | 0 | { |
1581 | 0 | p_timing->pic_struct = 0; |
1582 | 0 | p_timing->source_scan_type = 1; |
1583 | 0 | } |
1584 | 0 | } |
1585 | 0 | return p_timing; |
1586 | 0 | } |
1587 | | |
1588 | | bool hevc_decode_sei_recovery_point( bs_t *p_bs, hevc_sei_recovery_point_t *p_reco ) |
1589 | 0 | { |
1590 | 0 | p_reco->i_frames = bs_read_se( p_bs ); |
1591 | 0 | return true; |
1592 | 0 | } |
1593 | | |
1594 | | bool hevc_frame_is_progressive( const hevc_sequence_parameter_set_t *p_sps, |
1595 | | const hevc_sei_pic_timing_t *p_timing ) |
1596 | 0 | { |
1597 | 0 | if( p_sps->vui_parameters_present_flag && |
1598 | 0 | p_sps->vui.field_seq_flag ) |
1599 | 0 | return false; |
1600 | | |
1601 | 0 | const hevc_inner_profile_tier_level_t *p_profile = &p_sps->profile_tier_level.general; |
1602 | | /* 1 & 0, 0 & 1, global */ |
1603 | 0 | if( p_profile->progressive_source_flag != p_profile->interlaced_source_flag ) |
1604 | 0 | { |
1605 | 0 | return p_profile->progressive_source_flag > p_profile->interlaced_source_flag; |
1606 | 0 | } |
1607 | | /* 1 & 1, defined in SEI */ |
1608 | 0 | else if( p_profile->progressive_source_flag ) |
1609 | 0 | { |
1610 | 0 | if( p_timing && p_sps->vui.frame_field_info_present_flag ) |
1611 | 0 | { |
1612 | 0 | if( p_timing->source_scan_type < 2 ) |
1613 | 0 | return p_timing->source_scan_type != 0; |
1614 | 0 | } |
1615 | 0 | } |
1616 | | /* else 0 & 0, unspec */ |
1617 | | |
1618 | 0 | return true; |
1619 | 0 | } |
1620 | | |
1621 | | uint8_t hevc_get_num_clock_ts( const hevc_sequence_parameter_set_t *p_sps, |
1622 | | const hevc_sei_pic_timing_t *p_timing ) |
1623 | 0 | { |
1624 | 0 | if( p_sps->vui.frame_field_info_present_flag && p_timing && p_timing->pic_struct < 13 ) |
1625 | 0 | { |
1626 | | /* !WARN modified with units_field_based_flag (D.3.25) for values 0, 7 and 8 */ |
1627 | 0 | const uint8_t rgi_numclock[13] = { 2, 1, 1, 2, 2, 3, 3, 4, 6, 1, 1, 1, 1 }; |
1628 | 0 | return rgi_numclock[p_timing->pic_struct]; |
1629 | 0 | } |
1630 | | |
1631 | 0 | if( p_sps->vui_parameters_present_flag ) |
1632 | 0 | { |
1633 | 0 | if( p_sps->vui.field_seq_flag ) |
1634 | 0 | return 1; /* D.3.27 */ |
1635 | 0 | } |
1636 | 0 | else if( p_sps->profile_tier_level.general.interlaced_source_flag && |
1637 | 0 | !p_sps->profile_tier_level.general.progressive_source_flag ) |
1638 | 0 | { |
1639 | 0 | return 1; |
1640 | 0 | } |
1641 | | |
1642 | 0 | return 2; |
1643 | 0 | } |