7 #include "packager/media/codecs/h265_parser.h"
12 #include "packager/base/logging.h"
13 #include "packager/media/base/macros.h"
14 #include "packager/media/codecs/nalu_reader.h"
16 #define TRUE_OR_RETURN(a) \
19 DVLOG(1) << "Failure while processing " << #a; \
20 return kInvalidStream; \
24 #define OK_OR_RETURN(a) \
26 Result status = (a); \
35 int GetNumPicTotalCurr(
const H265SliceHeader& slice_header,
37 int num_pic_total_curr = 0;
38 const H265ReferencePictureSet& ref_pic_set =
39 slice_header.short_term_ref_pic_set_sps_flag
40 ? sps.st_ref_pic_sets[slice_header.short_term_ref_pic_set_idx]
41 : slice_header.st_ref_pic_set;
43 for (
int i = 0; i < ref_pic_set.num_negative_pics; i++) {
44 if (ref_pic_set.used_by_curr_pic_s0[i])
47 for (
int i = 0; i < ref_pic_set.num_positive_pics; i++) {
48 if (ref_pic_set.used_by_curr_pic_s1[i])
52 return num_pic_total_curr + slice_header.used_by_curr_pic_lt;
55 void GetAspectRatioInfo(
const H265Sps& sps,
56 uint32_t* pixel_width,
57 uint32_t* pixel_height) {
60 int aspect_ratio_idc = sps.vui_parameters.aspect_ratio_idc;
63 switch (aspect_ratio_idc) {
64 case 1: *pixel_width = 1; *pixel_height = 1;
break;
65 case 2: *pixel_width = 12; *pixel_height = 11;
break;
66 case 3: *pixel_width = 10; *pixel_height = 11;
break;
67 case 4: *pixel_width = 16; *pixel_height = 11;
break;
68 case 5: *pixel_width = 40; *pixel_height = 33;
break;
69 case 6: *pixel_width = 24; *pixel_height = 11;
break;
70 case 7: *pixel_width = 20; *pixel_height = 11;
break;
71 case 8: *pixel_width = 32; *pixel_height = 11;
break;
72 case 9: *pixel_width = 80; *pixel_height = 33;
break;
73 case 10: *pixel_width = 18; *pixel_height = 11;
break;
74 case 11: *pixel_width = 15; *pixel_height = 11;
break;
75 case 12: *pixel_width = 64; *pixel_height = 33;
break;
76 case 13: *pixel_width = 160; *pixel_height = 99;
break;
77 case 14: *pixel_width = 4; *pixel_height = 3;
break;
78 case 15: *pixel_width = 3; *pixel_height = 2;
break;
79 case 16: *pixel_width = 2; *pixel_height = 1;
break;
82 *pixel_width = sps.vui_parameters.sar_width;
83 *pixel_height = sps.vui_parameters.sar_height;
88 LOG(WARNING) <<
"Unknown aspect_ratio_idc " << aspect_ratio_idc;
99 bool ExtractResolutionFromSps(
const H265Sps& sps,
100 uint32_t* coded_width,
101 uint32_t* coded_height,
102 uint32_t* pixel_width,
103 uint32_t* pixel_height) {
106 if (sps.conformance_window_flag) {
108 int sub_height_c = 0;
111 switch (sps.chroma_format_idc) {
129 LOG(ERROR) <<
"Unexpected chroma_format_idc " << sps.chroma_format_idc;
135 sub_width_c * (sps.conf_win_right_offset + sps.conf_win_left_offset);
137 sub_height_c * (sps.conf_win_bottom_offset + sps.conf_win_top_offset);
141 *coded_width = sps.pic_width_in_luma_samples - crop_x;
142 *coded_height = sps.pic_height_in_luma_samples - crop_y;
143 GetAspectRatioInfo(sps, pixel_width, pixel_height);
147 H265Pps::H265Pps() {}
148 H265Pps::~H265Pps() {}
150 H265Sps::H265Sps() {}
151 H265Sps::~H265Sps() {}
153 int H265Sps::GetPicSizeInCtbsY()
const {
154 int min_cb_log2_size_y = log2_min_luma_coding_block_size_minus3 + 3;
155 int ctb_log2_size_y =
156 min_cb_log2_size_y + log2_diff_max_min_luma_coding_block_size;
157 int ctb_size_y = 1 << ctb_log2_size_y;
160 int pic_width_in_ctbs_y = (pic_width_in_luma_samples - 1) / ctb_size_y + 1;
161 int pic_height_in_ctbs_y = (pic_height_in_luma_samples - 1) / ctb_size_y + 1;
162 return pic_width_in_ctbs_y * pic_height_in_ctbs_y;
165 int H265Sps::GetChromaArrayType()
const {
166 if (!separate_colour_plane_flag)
167 return chroma_format_idc;
172 H265ReferencePictureListModifications::H265ReferencePictureListModifications() {
174 H265ReferencePictureListModifications::
175 ~H265ReferencePictureListModifications() {}
177 H265SliceHeader::H265SliceHeader() {}
178 H265SliceHeader::~H265SliceHeader() {}
180 H265Parser::H265Parser() {}
181 H265Parser::~H265Parser() {}
185 DCHECK(nalu.is_video_slice());
193 TRUE_OR_RETURN(br->ReadBool(&slice_header->first_slice_segment_in_pic_flag));
194 if (nalu.
type() >= Nalu::H265_BLA_W_LP &&
195 nalu.
type() <= Nalu::H265_RSV_IRAP_VCL23) {
196 TRUE_OR_RETURN(br->ReadBool(&slice_header->no_output_of_prior_pics_flag));
199 TRUE_OR_RETURN(br->ReadUE(&slice_header->pic_parameter_set_id));
200 const H265Pps* pps =
GetPps(slice_header->pic_parameter_set_id);
206 if (!slice_header->first_slice_segment_in_pic_flag) {
207 if (pps->dependent_slice_segments_enabled_flag) {
208 TRUE_OR_RETURN(br->ReadBool(&slice_header->dependent_slice_segment_flag));
210 const int bit_length = ceil(log2(sps->GetPicSizeInCtbsY()));
211 TRUE_OR_RETURN(br->ReadBits(bit_length, &slice_header->segment_address));
214 if (!slice_header->dependent_slice_segment_flag) {
215 TRUE_OR_RETURN(br->SkipBits(pps->num_extra_slice_header_bits));
216 TRUE_OR_RETURN(br->ReadUE(&slice_header->slice_type));
217 if (pps->output_flag_present_flag) {
218 TRUE_OR_RETURN(br->ReadBool(&slice_header->pic_output_flag));
220 if (sps->separate_colour_plane_flag) {
221 TRUE_OR_RETURN(br->ReadBits(2, &slice_header->colour_plane_id));
224 if (nalu.
type() != Nalu::H265_IDR_W_RADL &&
225 nalu.
type() != Nalu::H265_IDR_N_LP) {
226 TRUE_OR_RETURN(br->ReadBits(sps->log2_max_pic_order_cnt_lsb_minus4 + 4,
227 &slice_header->slice_pic_order_cnt_lsb));
230 br->ReadBool(&slice_header->short_term_ref_pic_set_sps_flag));
231 if (!slice_header->short_term_ref_pic_set_sps_flag) {
232 OK_OR_RETURN(ParseReferencePictureSet(
233 sps->num_short_term_ref_pic_sets, sps->num_short_term_ref_pic_sets,
234 sps->st_ref_pic_sets, br, &slice_header->st_ref_pic_set));
235 }
else if (sps->num_short_term_ref_pic_sets > 1) {
237 br->ReadBits(ceil(log2(sps->num_short_term_ref_pic_sets)),
238 &slice_header->short_term_ref_pic_set_idx));
241 if (sps->long_term_ref_pic_present_flag) {
242 if (sps->num_long_term_ref_pics > 0) {
243 TRUE_OR_RETURN(br->ReadUE(&slice_header->num_long_term_sps));
245 TRUE_OR_RETURN(br->ReadUE(&slice_header->num_long_term_pics));
247 const int pic_count =
248 slice_header->num_long_term_sps + slice_header->num_long_term_pics;
249 slice_header->long_term_pics_info.resize(pic_count);
250 for (
int i = 0; i < pic_count; i++) {
251 if (i < slice_header->num_long_term_sps) {
253 if (sps->num_long_term_ref_pics > 1) {
254 TRUE_OR_RETURN(br->ReadBits(
255 ceil(log2(sps->num_long_term_ref_pics)), <_idx_sps));
257 if (sps->used_by_curr_pic_lt_flag[lt_idx_sps])
258 slice_header->used_by_curr_pic_lt++;
260 TRUE_OR_RETURN(br->SkipBits(sps->log2_max_pic_order_cnt_lsb_minus4 +
262 bool used_by_curr_pic_lt_flag;
263 TRUE_OR_RETURN(br->ReadBool(&used_by_curr_pic_lt_flag));
264 if (used_by_curr_pic_lt_flag)
265 slice_header->used_by_curr_pic_lt++;
267 TRUE_OR_RETURN(br->ReadBool(&slice_header->long_term_pics_info[i]
268 .delta_poc_msb_present_flag));
269 if (slice_header->long_term_pics_info[i].delta_poc_msb_present_flag) {
270 TRUE_OR_RETURN(br->ReadUE(
271 &slice_header->long_term_pics_info[i].delta_poc_msb_cycle_lt));
276 if (sps->temporal_mvp_enabled_flag) {
278 br->ReadBool(&slice_header->slice_temporal_mvp_enabled_flag));
282 if (nalu.nuh_layer_id() != 0) {
283 NOTIMPLEMENTED() <<
"Multi-layer streams are not supported.";
284 return kUnsupportedStream;
287 if (sps->sample_adaptive_offset_enabled_flag) {
288 TRUE_OR_RETURN(br->ReadBool(&slice_header->slice_sao_luma_flag));
289 if (sps->GetChromaArrayType() != 0) {
290 TRUE_OR_RETURN(br->ReadBool(&slice_header->slice_sao_chroma_flag));
294 slice_header->num_ref_idx_l0_active_minus1 =
295 pps->num_ref_idx_l0_default_active_minus1;
296 slice_header->num_ref_idx_l1_active_minus1 =
297 pps->num_ref_idx_l1_default_active_minus1;
298 if (slice_header->slice_type == kPSlice ||
299 slice_header->slice_type == kBSlice) {
301 br->ReadBool(&slice_header->num_ref_idx_active_override_flag));
302 if (slice_header->num_ref_idx_active_override_flag) {
303 TRUE_OR_RETURN(br->ReadUE(&slice_header->num_ref_idx_l0_active_minus1));
304 if (slice_header->slice_type == kBSlice) {
306 br->ReadUE(&slice_header->num_ref_idx_l1_active_minus1));
310 const int num_pic_total_curr = GetNumPicTotalCurr(*slice_header, *sps);
311 if (pps->lists_modification_present_flag && num_pic_total_curr > 1) {
312 OK_OR_RETURN(SkipReferencePictureListModification(
313 *slice_header, *pps, num_pic_total_curr, br));
316 if (slice_header->slice_type == kBSlice) {
317 TRUE_OR_RETURN(br->ReadBool(&slice_header->mvd_l1_zero_flag));
319 if (pps->cabac_init_present_flag) {
320 TRUE_OR_RETURN(br->ReadBool(&slice_header->cabac_init_flag));
322 if (slice_header->slice_temporal_mvp_enabled_flag) {
323 if (slice_header->slice_type == kBSlice) {
324 TRUE_OR_RETURN(br->ReadBool(&slice_header->collocated_from_l0));
326 bool l0_greater_than_0 = slice_header->num_ref_idx_l0_active_minus1 > 0;
327 bool l1_greater_than_0 = slice_header->num_ref_idx_l1_active_minus1 > 0;
328 if (slice_header->collocated_from_l0 ? l0_greater_than_0
329 : l1_greater_than_0) {
330 TRUE_OR_RETURN(br->ReadUE(&slice_header->collocated_ref_idx));
334 if ((pps->weighted_pred_flag && slice_header->slice_type == kPSlice) ||
335 (pps->weighted_bipred_flag && slice_header->slice_type == kBSlice)) {
336 OK_OR_RETURN(SkipPredictionWeightTable(
337 slice_header->slice_type == kBSlice, *sps, *slice_header, br));
339 TRUE_OR_RETURN(br->ReadUE(&slice_header->five_minus_max_num_merge_cand));
342 TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_qp_delta));
343 if (pps->slice_chroma_qp_offsets_present_flag) {
344 TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_cb_qp_offset));
345 TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_cr_qp_offset));
348 if (pps->chroma_qp_offset_list_enabled_flag) {
350 br->ReadBool(&slice_header->cu_chroma_qp_offset_enabled_flag));
352 if (pps->deblocking_filter_override_enabled_flag) {
354 br->ReadBool(&slice_header->deblocking_filter_override_flag));
356 if (slice_header->deblocking_filter_override_flag) {
358 br->ReadBool(&slice_header->slice_deblocking_filter_disabled_flag));
359 if (!slice_header->slice_deblocking_filter_disabled_flag) {
360 TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_beta_offset_div2));
361 TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_tc_offset_div2));
364 if (pps->loop_filter_across_slices_enabled_flag &&
365 (slice_header->slice_sao_luma_flag ||
366 slice_header->slice_sao_chroma_flag ||
367 !slice_header->slice_deblocking_filter_disabled_flag)) {
368 TRUE_OR_RETURN(br->ReadBool(
369 &slice_header->slice_loop_filter_across_slices_enabled_flag));
373 if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) {
374 TRUE_OR_RETURN(br->ReadUE(&slice_header->num_entry_point_offsets));
375 if (slice_header->num_entry_point_offsets > 0) {
376 TRUE_OR_RETURN(br->ReadUE(&slice_header->offset_len_minus1));
377 slice_header->entry_point_offset_minus1.resize(
378 slice_header->num_entry_point_offsets);
379 for (
int i = 0; i < slice_header->num_entry_point_offsets; i++) {
381 br->ReadBits(slice_header->offset_len_minus1 + 1,
382 &slice_header->entry_point_offset_minus1[i]));
387 if (pps->slice_segment_header_extension_present_flag) {
388 int extension_length;
389 TRUE_OR_RETURN(br->ReadUE(&extension_length));
390 TRUE_OR_RETURN(br->SkipBits(extension_length * 8));
393 slice_header->header_bit_size = nalu.
payload_size() * 8 - br->NumBitsLeft();
398 DCHECK_EQ(Nalu::H265_PPS, nalu.
type());
406 std::unique_ptr<H265Pps> pps(
new H265Pps);
408 TRUE_OR_RETURN(br->ReadUE(&pps->pic_parameter_set_id));
409 TRUE_OR_RETURN(br->ReadUE(&pps->seq_parameter_set_id));
411 TRUE_OR_RETURN(br->ReadBool(&pps->dependent_slice_segments_enabled_flag));
412 TRUE_OR_RETURN(br->ReadBool(&pps->output_flag_present_flag));
413 TRUE_OR_RETURN(br->ReadBits(3, &pps->num_extra_slice_header_bits));
414 TRUE_OR_RETURN(br->ReadBool(&pps->sign_data_hiding_enabled_flag));
415 TRUE_OR_RETURN(br->ReadBool(&pps->cabac_init_present_flag));
417 TRUE_OR_RETURN(br->ReadUE(&pps->num_ref_idx_l0_default_active_minus1));
418 TRUE_OR_RETURN(br->ReadUE(&pps->num_ref_idx_l1_default_active_minus1));
419 TRUE_OR_RETURN(br->ReadSE(&pps->init_qp_minus26));
420 TRUE_OR_RETURN(br->ReadBool(&pps->constrained_intra_pred_flag));
421 TRUE_OR_RETURN(br->ReadBool(&pps->transform_skip_enabled_flag));
423 TRUE_OR_RETURN(br->ReadBool(&pps->cu_qp_delta_enabled_flag));
424 if (pps->cu_qp_delta_enabled_flag)
425 TRUE_OR_RETURN(br->ReadUE(&pps->diff_cu_qp_delta_depth));
426 TRUE_OR_RETURN(br->ReadSE(&pps->cb_qp_offset));
427 TRUE_OR_RETURN(br->ReadSE(&pps->cr_qp_offset));
429 TRUE_OR_RETURN(br->ReadBool(&pps->slice_chroma_qp_offsets_present_flag));
430 TRUE_OR_RETURN(br->ReadBool(&pps->weighted_pred_flag));
431 TRUE_OR_RETURN(br->ReadBool(&pps->weighted_bipred_flag));
432 TRUE_OR_RETURN(br->ReadBool(&pps->transquant_bypass_enabled_flag));
433 TRUE_OR_RETURN(br->ReadBool(&pps->tiles_enabled_flag));
434 TRUE_OR_RETURN(br->ReadBool(&pps->entropy_coding_sync_enabled_flag));
436 if (pps->tiles_enabled_flag) {
437 TRUE_OR_RETURN(br->ReadUE(&pps->num_tile_columns_minus1));
438 TRUE_OR_RETURN(br->ReadUE(&pps->num_tile_rows_minus1));
439 TRUE_OR_RETURN(br->ReadBool(&pps->uniform_spacing_flag));
440 if (!pps->uniform_spacing_flag) {
441 pps->column_width_minus1.resize(pps->num_tile_columns_minus1);
442 for (
int i = 0; i < pps->num_tile_columns_minus1; i++) {
443 TRUE_OR_RETURN(br->ReadUE(&pps->column_width_minus1[i]));
445 pps->row_height_minus1.resize(pps->num_tile_rows_minus1);
446 for (
int i = 0; i < pps->num_tile_rows_minus1; i++) {
447 TRUE_OR_RETURN(br->ReadUE(&pps->row_height_minus1[i]));
450 TRUE_OR_RETURN(br->ReadBool(&pps->loop_filter_across_tiles_enabled_flag));
453 TRUE_OR_RETURN(br->ReadBool(&pps->loop_filter_across_slices_enabled_flag));
454 TRUE_OR_RETURN(br->ReadBool(&pps->deblocking_filter_control_present_flag));
455 if (pps->deblocking_filter_control_present_flag) {
456 TRUE_OR_RETURN(br->ReadBool(&pps->deblocking_filter_override_enabled_flag));
457 TRUE_OR_RETURN(br->ReadBool(&pps->deblocking_filter_disabled_flag));
458 if (!pps->deblocking_filter_disabled_flag) {
459 TRUE_OR_RETURN(br->ReadSE(&pps->beta_offset_div2));
460 TRUE_OR_RETURN(br->ReadSE(&pps->tc_offset_div2));
464 TRUE_OR_RETURN(br->ReadBool(&pps->scaling_list_data_present_flag));
465 if (pps->scaling_list_data_present_flag) {
466 OK_OR_RETURN(SkipScalingListData(br));
469 TRUE_OR_RETURN(br->ReadBool(&pps->lists_modification_present_flag));
470 TRUE_OR_RETURN(br->ReadUE(&pps->log2_parallel_merge_level_minus2));
473 br->ReadBool(&pps->slice_segment_header_extension_present_flag));
475 bool pps_extension_present_flag;
476 bool pps_range_extension_flag =
false;
477 TRUE_OR_RETURN(br->ReadBool(&pps_extension_present_flag));
478 if (pps_extension_present_flag) {
479 TRUE_OR_RETURN(br->ReadBool(&pps_range_extension_flag));
481 TRUE_OR_RETURN(br->SkipBits(1 + 1 + 5));
484 if (pps_range_extension_flag) {
485 if (pps->transform_skip_enabled_flag) {
488 TRUE_OR_RETURN(br->ReadUE(&ignored));
491 TRUE_OR_RETURN(br->SkipBits(1));
492 TRUE_OR_RETURN(br->ReadBool(&pps->chroma_qp_offset_list_enabled_flag));
499 *pps_id = pps->pic_parameter_set_id;
500 active_ppses_[*pps_id] = std::move(pps);
506 DCHECK_EQ(Nalu::H265_SPS, nalu.
type());
515 std::unique_ptr<H265Sps> sps(
new H265Sps);
517 TRUE_OR_RETURN(br->ReadBits(4, &sps->video_parameter_set_id));
518 TRUE_OR_RETURN(br->ReadBits(3, &sps->max_sub_layers_minus1));
519 TRUE_OR_RETURN(br->ReadBool(&sps->temporal_id_nesting_flag));
521 OK_OR_RETURN(SkipProfileTierLevel(
true, sps->max_sub_layers_minus1, br));
523 TRUE_OR_RETURN(br->ReadUE(&sps->seq_parameter_set_id));
524 TRUE_OR_RETURN(br->ReadUE(&sps->chroma_format_idc));
525 if (sps->chroma_format_idc == 3) {
526 TRUE_OR_RETURN(br->ReadBool(&sps->separate_colour_plane_flag));
528 TRUE_OR_RETURN(br->ReadUE(&sps->pic_width_in_luma_samples));
529 TRUE_OR_RETURN(br->ReadUE(&sps->pic_height_in_luma_samples));
531 TRUE_OR_RETURN(br->ReadBool(&sps->conformance_window_flag));
532 if (sps->conformance_window_flag) {
533 TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_left_offset));
534 TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_right_offset));
535 TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_top_offset));
536 TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_bottom_offset));
539 TRUE_OR_RETURN(br->ReadUE(&sps->bit_depth_luma_minus8));
540 TRUE_OR_RETURN(br->ReadUE(&sps->bit_depth_chroma_minus8));
541 TRUE_OR_RETURN(br->ReadUE(&sps->log2_max_pic_order_cnt_lsb_minus4));
543 TRUE_OR_RETURN(br->ReadBool(&sps->sub_layer_ordering_info_present_flag));
544 int start = sps->sub_layer_ordering_info_present_flag
546 : sps->max_sub_layers_minus1;
547 for (
int i = start; i <= sps->max_sub_layers_minus1; i++) {
548 TRUE_OR_RETURN(br->ReadUE(&sps->max_dec_pic_buffering_minus1[i]));
549 TRUE_OR_RETURN(br->ReadUE(&sps->max_num_reorder_pics[i]));
550 TRUE_OR_RETURN(br->ReadUE(&sps->max_latency_increase_plus1[i]));
553 TRUE_OR_RETURN(br->ReadUE(&sps->log2_min_luma_coding_block_size_minus3));
554 TRUE_OR_RETURN(br->ReadUE(&sps->log2_diff_max_min_luma_coding_block_size));
555 TRUE_OR_RETURN(br->ReadUE(&sps->log2_min_luma_transform_block_size_minus2));
556 TRUE_OR_RETURN(br->ReadUE(&sps->log2_diff_max_min_luma_transform_block_size));
557 TRUE_OR_RETURN(br->ReadUE(&sps->max_transform_hierarchy_depth_inter));
558 TRUE_OR_RETURN(br->ReadUE(&sps->max_transform_hierarchy_depth_intra));
560 TRUE_OR_RETURN(br->ReadBool(&sps->scaling_list_enabled_flag));
561 if (sps->scaling_list_enabled_flag) {
562 TRUE_OR_RETURN(br->ReadBool(&sps->scaling_list_data_present_flag));
563 if (sps->scaling_list_data_present_flag) {
564 OK_OR_RETURN(SkipScalingListData(br));
568 TRUE_OR_RETURN(br->ReadBool(&sps->amp_enabled_flag));
569 TRUE_OR_RETURN(br->ReadBool(&sps->sample_adaptive_offset_enabled_flag));
570 TRUE_OR_RETURN(br->ReadBool(&sps->pcm_enabled_flag));
571 if (sps->pcm_enabled_flag) {
572 TRUE_OR_RETURN(br->ReadBits(4, &sps->pcm_sample_bit_depth_luma_minus1));
573 TRUE_OR_RETURN(br->ReadBits(4, &sps->pcm_sample_bit_depth_chroma_minus1));
575 br->ReadUE(&sps->log2_min_pcm_luma_coding_block_size_minus3));
577 br->ReadUE(&sps->log2_diff_max_min_pcm_luma_coding_block_size));
578 TRUE_OR_RETURN(br->ReadBool(&sps->pcm_loop_filter_disabled_flag));
581 TRUE_OR_RETURN(br->ReadUE(&sps->num_short_term_ref_pic_sets));
582 sps->st_ref_pic_sets.resize(sps->num_short_term_ref_pic_sets);
583 for (
int i = 0; i < sps->num_short_term_ref_pic_sets; i++) {
584 OK_OR_RETURN(ParseReferencePictureSet(sps->num_short_term_ref_pic_sets, i,
585 sps->st_ref_pic_sets, br,
586 &sps->st_ref_pic_sets[i]));
589 TRUE_OR_RETURN(br->ReadBool(&sps->long_term_ref_pic_present_flag));
590 if (sps->long_term_ref_pic_present_flag) {
591 TRUE_OR_RETURN(br->ReadUE(&sps->num_long_term_ref_pics));
592 sps->lt_ref_pic_poc_lsb.resize(sps->num_long_term_ref_pics);
593 sps->used_by_curr_pic_lt_flag.resize(sps->num_long_term_ref_pics);
594 for (
int i = 0; i < sps->num_long_term_ref_pics; i++) {
595 TRUE_OR_RETURN(br->ReadBits(sps->log2_max_pic_order_cnt_lsb_minus4 + 4,
596 &sps->lt_ref_pic_poc_lsb[i]));
598 TRUE_OR_RETURN(br->ReadBool(&temp));
599 sps->used_by_curr_pic_lt_flag[i] = temp;
603 TRUE_OR_RETURN(br->ReadBool(&sps->temporal_mvp_enabled_flag));
604 TRUE_OR_RETURN(br->ReadBool(&sps->strong_intra_smoothing_enabled_flag));
606 TRUE_OR_RETURN(br->ReadBool(&sps->vui_parameters_present));
607 if (sps->vui_parameters_present) {
608 OK_OR_RETURN(ParseVuiParameters(sps->max_sub_layers_minus1, br,
609 &sps->vui_parameters));
615 *sps_id = sps->seq_parameter_set_id;
616 active_spses_[*sps_id] = std::move(sps);
622 return active_ppses_[pps_id].get();
626 return active_spses_[sps_id].get();
629 H265Parser::Result H265Parser::ParseVuiParameters(
int max_num_sub_layers_minus1,
635 TRUE_OR_RETURN(br->ReadBool(&vui->aspect_ratio_info_present_flag));
636 if (vui->aspect_ratio_info_present_flag) {
637 TRUE_OR_RETURN(br->ReadBits(8, &vui->aspect_ratio_idc));
638 if (vui->aspect_ratio_idc == H265VuiParameters::kExtendedSar) {
639 TRUE_OR_RETURN(br->ReadBits(16, &vui->sar_width));
640 TRUE_OR_RETURN(br->ReadBits(16, &vui->sar_height));
644 bool overscan_info_present_flag;
645 TRUE_OR_RETURN(br->ReadBool(&overscan_info_present_flag));
646 if (overscan_info_present_flag) {
647 TRUE_OR_RETURN(br->SkipBits(1));
650 bool video_signal_type_present_flag;
651 TRUE_OR_RETURN(br->ReadBool(&video_signal_type_present_flag));
652 if (video_signal_type_present_flag) {
653 TRUE_OR_RETURN(br->SkipBits(3));
654 TRUE_OR_RETURN(br->SkipBits(1));
656 bool colour_description_present_flag;
657 TRUE_OR_RETURN(br->ReadBool(&colour_description_present_flag));
658 if (colour_description_present_flag) {
660 TRUE_OR_RETURN(br->SkipBits(8 + 8 + 8));
664 bool chroma_loc_info_present_flag;
665 TRUE_OR_RETURN(br->ReadBool(&chroma_loc_info_present_flag));
666 if (chroma_loc_info_present_flag) {
668 TRUE_OR_RETURN(br->ReadUE(&ignored));
669 TRUE_OR_RETURN(br->ReadUE(&ignored));
674 TRUE_OR_RETURN(br->SkipBits(3));
676 bool default_display_window_flag;
677 TRUE_OR_RETURN(br->ReadBool(&default_display_window_flag));
678 if (default_display_window_flag) {
679 TRUE_OR_RETURN(br->ReadUE(&ignored));
680 TRUE_OR_RETURN(br->ReadUE(&ignored));
681 TRUE_OR_RETURN(br->ReadUE(&ignored));
682 TRUE_OR_RETURN(br->ReadUE(&ignored));
685 bool vui_timing_info_present_flag;
686 TRUE_OR_RETURN(br->ReadBool(&vui_timing_info_present_flag));
687 if (vui_timing_info_present_flag) {
689 TRUE_OR_RETURN(br->SkipBits(32 + 32));
691 bool vui_poc_proportional_to_timing_flag;
692 TRUE_OR_RETURN(br->ReadBool(&vui_poc_proportional_to_timing_flag));
693 if (vui_poc_proportional_to_timing_flag) {
695 TRUE_OR_RETURN(br->ReadUE(&ignored));
698 bool vui_hdr_parameters_present_flag;
699 TRUE_OR_RETURN(br->ReadBool(&vui_hdr_parameters_present_flag));
700 if (vui_hdr_parameters_present_flag) {
701 OK_OR_RETURN(SkipHrdParameters(max_num_sub_layers_minus1, br));
705 TRUE_OR_RETURN(br->ReadBool(&vui->bitstream_restriction_flag));
706 if (vui->bitstream_restriction_flag) {
709 TRUE_OR_RETURN(br->SkipBits(3));
711 TRUE_OR_RETURN(br->ReadUE(&vui->min_spatial_segmentation_idc));
712 TRUE_OR_RETURN(br->ReadUE(&ignored));
713 TRUE_OR_RETURN(br->ReadUE(&ignored));
714 TRUE_OR_RETURN(br->ReadUE(&ignored));
715 TRUE_OR_RETURN(br->ReadUE(&ignored));
721 H265Parser::Result H265Parser::ParseReferencePictureSet(
722 int num_short_term_ref_pic_sets,
724 const std::vector<H265ReferencePictureSet>& ref_pic_sets,
726 H265ReferencePictureSet* out_ref_pic_set) {
731 bool inter_ref_pic_set_prediction =
false;
732 if (st_rps_idx != 0) {
733 TRUE_OR_RETURN(br->ReadBool(&inter_ref_pic_set_prediction));
736 if (inter_ref_pic_set_prediction) {
738 if (st_rps_idx == num_short_term_ref_pic_sets) {
739 TRUE_OR_RETURN(br->ReadUE(&delta_idx));
741 TRUE_OR_RETURN(delta_idx <= st_rps_idx);
744 int ref_rps_idx = st_rps_idx - delta_idx;
745 DCHECK_LE(0, ref_rps_idx);
746 DCHECK_LT(ref_rps_idx, st_rps_idx);
749 int abs_delta_rps_minus1;
750 TRUE_OR_RETURN(br->ReadBool(&delta_rps_sign));
751 TRUE_OR_RETURN(br->ReadUE(&abs_delta_rps_minus1));
753 delta_rps_sign ? -(abs_delta_rps_minus1 + 1) : abs_delta_rps_minus1 + 1;
755 int ref_num_delta_pocs = ref_pic_sets[ref_rps_idx].num_delta_pocs;
756 std::vector<bool> used_by_curr_pic(ref_num_delta_pocs + 1);
757 std::vector<bool> use_delta(ref_num_delta_pocs + 1);
758 for (
int j = 0; j <= ref_num_delta_pocs; j++) {
760 TRUE_OR_RETURN(br->ReadBool(&temp));
761 used_by_curr_pic[j] = temp;
763 if (!used_by_curr_pic[j]) {
764 TRUE_OR_RETURN(br->ReadBool(&temp));
771 int ref_num_positive_pics = ref_pic_sets[ref_rps_idx].num_positive_pics;
772 int ref_num_negative_pics = ref_pic_sets[ref_rps_idx].num_negative_pics;
778 for (
int j = ref_num_positive_pics - 1; j >= 0; j--) {
779 int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s1[j] + delta_rps;
780 if (d_poc < 0 && use_delta[ref_num_negative_pics + j]) {
781 out_ref_pic_set->delta_poc_s0[i] = d_poc;
782 out_ref_pic_set->used_by_curr_pic_s0[i] =
783 used_by_curr_pic[ref_num_negative_pics + j];
787 if (delta_rps < 0 && use_delta[ref_num_delta_pocs]) {
788 out_ref_pic_set->delta_poc_s0[i] = delta_rps;
789 out_ref_pic_set->used_by_curr_pic_s0[i] =
790 used_by_curr_pic[ref_num_delta_pocs];
793 for (
int j = 0; j < ref_num_negative_pics; j++) {
794 int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s0[j] + delta_rps;
795 if (d_poc < 0 && use_delta[j]) {
796 out_ref_pic_set->delta_poc_s0[i] = d_poc;
797 out_ref_pic_set->used_by_curr_pic_s0[i] = used_by_curr_pic[j];
801 out_ref_pic_set->num_negative_pics = i;
807 for (
int j = ref_num_negative_pics - 1; j >= 0; j--) {
808 int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s0[j] + delta_rps;
809 if (d_poc > 0 && use_delta[j]) {
810 out_ref_pic_set->delta_poc_s1[i] = d_poc;
811 out_ref_pic_set->used_by_curr_pic_s1[i] = used_by_curr_pic[j];
815 if (delta_rps > 0 && use_delta[ref_num_delta_pocs]) {
816 out_ref_pic_set->delta_poc_s1[i] = delta_rps;
817 out_ref_pic_set->used_by_curr_pic_s1[i] =
818 used_by_curr_pic[ref_num_delta_pocs];
821 for (
int j = 0; j < ref_num_positive_pics; j++) {
822 int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s1[j] + delta_rps;
823 if (d_poc > 0 && use_delta[ref_num_negative_pics + j]) {
824 out_ref_pic_set->delta_poc_s1[i] = d_poc;
825 out_ref_pic_set->used_by_curr_pic_s1[i] =
826 used_by_curr_pic[ref_num_negative_pics + j];
830 out_ref_pic_set->num_positive_pics = i;
833 TRUE_OR_RETURN(br->ReadUE(&out_ref_pic_set->num_negative_pics));
834 TRUE_OR_RETURN(br->ReadUE(&out_ref_pic_set->num_positive_pics));
837 for (
int i = 0; i < out_ref_pic_set->num_negative_pics; i++) {
838 int delta_poc_s0_minus1;
839 TRUE_OR_RETURN(br->ReadUE(&delta_poc_s0_minus1));
840 out_ref_pic_set->delta_poc_s0[i] = prev_poc - (delta_poc_s0_minus1 + 1);
841 prev_poc = out_ref_pic_set->delta_poc_s0[i];
843 TRUE_OR_RETURN(br->ReadBool(&out_ref_pic_set->used_by_curr_pic_s0[i]));
847 for (
int i = 0; i < out_ref_pic_set->num_positive_pics; i++) {
848 int delta_poc_s1_minus1;
849 TRUE_OR_RETURN(br->ReadUE(&delta_poc_s1_minus1));
850 out_ref_pic_set->delta_poc_s1[i] = prev_poc + delta_poc_s1_minus1 + 1;
851 prev_poc = out_ref_pic_set->delta_poc_s1[i];
853 TRUE_OR_RETURN(br->ReadBool(&out_ref_pic_set->used_by_curr_pic_s1[i]));
857 out_ref_pic_set->num_delta_pocs =
858 out_ref_pic_set->num_positive_pics + out_ref_pic_set->num_negative_pics;
862 H265Parser::Result H265Parser::SkipReferencePictureListModification(
863 const H265SliceHeader& slice_header,
865 int num_pic_total_curr,
869 bool ref_pic_list_modification_flag_l0;
870 TRUE_OR_RETURN(br->ReadBool(&ref_pic_list_modification_flag_l0));
871 if (ref_pic_list_modification_flag_l0) {
872 for (
int i = 0; i <= pps.num_ref_idx_l0_default_active_minus1; i++) {
873 TRUE_OR_RETURN(br->SkipBits(ceil(log2(num_pic_total_curr))));
877 if (slice_header.slice_type == kBSlice) {
878 bool ref_pic_list_modification_flag_l1;
879 TRUE_OR_RETURN(br->ReadBool(&ref_pic_list_modification_flag_l1));
880 if (ref_pic_list_modification_flag_l1) {
881 for (
int i = 0; i <= pps.num_ref_idx_l1_default_active_minus1; i++) {
882 TRUE_OR_RETURN(br->SkipBits(ceil(log2(num_pic_total_curr))));
890 H265Parser::Result H265Parser::SkipPredictionWeightTablePart(
891 int num_ref_idx_minus1,
892 int chroma_array_type,
896 std::vector<bool> luma_weight_flag(num_ref_idx_minus1 + 1);
897 std::vector<bool> chroma_weight_flag(num_ref_idx_minus1 + 1);
899 for (
int i = 0; i <= num_ref_idx_minus1; i++) {
901 TRUE_OR_RETURN(br->ReadBool(&temp));
902 luma_weight_flag[i] = temp;
904 if (chroma_array_type != 0) {
905 for (
int i = 0; i <= num_ref_idx_minus1; i++) {
907 TRUE_OR_RETURN(br->ReadBool(&temp));
908 chroma_weight_flag[i] = temp;
911 for (
int i = 0; i <= num_ref_idx_minus1; i++) {
912 if (luma_weight_flag[i]) {
913 TRUE_OR_RETURN(br->ReadSE(&ignored));
914 TRUE_OR_RETURN(br->ReadSE(&ignored));
916 if (chroma_weight_flag[i]) {
917 for (
int j = 0; j < 2; j++) {
918 TRUE_OR_RETURN(br->ReadSE(&ignored));
919 TRUE_OR_RETURN(br->ReadSE(&ignored));
927 H265Parser::Result H265Parser::SkipPredictionWeightTable(
930 const H265SliceHeader& slice_header,
934 int chroma_array_type = sps.GetChromaArrayType();
936 TRUE_OR_RETURN(br->ReadUE(&ignored));
937 if (chroma_array_type != 0) {
938 TRUE_OR_RETURN(br->ReadSE(&ignored));
940 OK_OR_RETURN(SkipPredictionWeightTablePart(
941 slice_header.num_ref_idx_l0_active_minus1, chroma_array_type, br));
943 OK_OR_RETURN(SkipPredictionWeightTablePart(
944 slice_header.num_ref_idx_l1_active_minus1, chroma_array_type, br));
950 H265Parser::Result H265Parser::SkipProfileTierLevel(
951 bool profile_present,
952 int max_num_sub_layers_minus1,
956 if (profile_present) {
964 TRUE_OR_RETURN(br->SkipBits(2 + 1 + 5 + 32 + 4 + 44));
967 TRUE_OR_RETURN(br->SkipBits(8));
969 std::vector<bool> sub_layer_profile_present(max_num_sub_layers_minus1);
970 std::vector<bool> sub_layer_level_present(max_num_sub_layers_minus1);
971 for (
int i = 0; i < max_num_sub_layers_minus1; i++) {
973 TRUE_OR_RETURN(br->ReadBool(&profile));
974 TRUE_OR_RETURN(br->ReadBool(&level));
975 sub_layer_profile_present[i] = profile;
976 sub_layer_level_present[i] = level;
979 if (max_num_sub_layers_minus1 > 0) {
980 for (
int i = max_num_sub_layers_minus1; i < 8; i++)
981 TRUE_OR_RETURN(br->SkipBits(2));
984 for (
int i = 0; i < max_num_sub_layers_minus1; i++) {
985 if (sub_layer_profile_present[i]) {
990 TRUE_OR_RETURN(br->SkipBits(2 + 1 + 5 + 32 + 4 + 43 + 1));
992 if (sub_layer_level_present[i]) {
993 TRUE_OR_RETURN(br->SkipBits(8));
1000 H265Parser::Result H265Parser::SkipScalingListData(H26xBitReader* br) {
1003 for (
int size_id = 0; size_id < 4; size_id++) {
1004 for (
int matrix_id = 0; matrix_id < 6;
1005 matrix_id += ((size_id == 3) ? 3 : 1)) {
1006 bool scaling_list_pred_mode;
1007 TRUE_OR_RETURN(br->ReadBool(&scaling_list_pred_mode));
1008 if (!scaling_list_pred_mode) {
1010 TRUE_OR_RETURN(br->ReadUE(&ignored));
1012 int coefNum = std::min(64, (1 << (4 + (size_id << 1))));
1014 TRUE_OR_RETURN(br->ReadSE(&ignored));
1017 for (
int i = 0; i < coefNum; i++) {
1018 TRUE_OR_RETURN(br->ReadSE(&ignored));
1027 H265Parser::Result H265Parser::SkipHrdParameters(
int max_num_sub_layers_minus1,
1028 H26xBitReader* br) {
1030 const bool common_inf_present_flag =
true;
1033 bool nal_hdr_parameters_present_flag;
1034 bool vcl_hdr_parameters_present_flag;
1035 bool sub_pic_hdr_params_present_flag =
false;
1036 if (common_inf_present_flag) {
1037 TRUE_OR_RETURN(br->ReadBool(&nal_hdr_parameters_present_flag));
1038 TRUE_OR_RETURN(br->ReadBool(&vcl_hdr_parameters_present_flag));
1039 if (nal_hdr_parameters_present_flag || vcl_hdr_parameters_present_flag) {
1040 TRUE_OR_RETURN(br->ReadBool(&sub_pic_hdr_params_present_flag));
1041 if (sub_pic_hdr_params_present_flag) {
1045 TRUE_OR_RETURN(br->SkipBits(8 + 5 + 1 + 5));
1049 TRUE_OR_RETURN(br->SkipBits(4 + 4));
1050 if (sub_pic_hdr_params_present_flag)
1051 TRUE_OR_RETURN(br->SkipBits(4));
1055 TRUE_OR_RETURN(br->SkipBits(5 + 5 + 5));
1059 for (
int i = 0; i <= max_num_sub_layers_minus1; i++) {
1060 bool fixed_pic_rate_general_flag;
1061 bool fixed_pic_rate_within_cvs_flag =
true;
1062 bool low_delay_hdr_flag =
false;
1063 int cpb_cnt_minus1 = 0;
1064 TRUE_OR_RETURN(br->ReadBool(&fixed_pic_rate_general_flag));
1065 if (!fixed_pic_rate_general_flag)
1066 TRUE_OR_RETURN(br->ReadBool(&fixed_pic_rate_within_cvs_flag));
1067 if (fixed_pic_rate_within_cvs_flag)
1068 TRUE_OR_RETURN(br->ReadUE(&ignored));
1070 TRUE_OR_RETURN(br->ReadBool(&low_delay_hdr_flag));
1071 if (!low_delay_hdr_flag)
1072 TRUE_OR_RETURN(br->ReadUE(&cpb_cnt_minus1));
1074 if (nal_hdr_parameters_present_flag) {
1075 OK_OR_RETURN(SkipSubLayerHrdParameters(
1076 cpb_cnt_minus1, sub_pic_hdr_params_present_flag, br));
1078 if (vcl_hdr_parameters_present_flag) {
1079 OK_OR_RETURN(SkipSubLayerHrdParameters(
1080 cpb_cnt_minus1, sub_pic_hdr_params_present_flag, br));
1087 H265Parser::Result H265Parser::SkipSubLayerHrdParameters(
1089 bool sub_pic_hdr_params_present_flag,
1090 H26xBitReader* br) {
1092 for (
int i = 0; i <= cpb_cnt_minus1; i++) {
1093 TRUE_OR_RETURN(br->ReadUE(&ignored));
1094 TRUE_OR_RETURN(br->ReadUE(&ignored));
1095 if (sub_pic_hdr_params_present_flag) {
1096 TRUE_OR_RETURN(br->ReadUE(&ignored));
1097 TRUE_OR_RETURN(br->ReadUE(&ignored));
1100 TRUE_OR_RETURN(br->SkipBits(1));