7 #include <packager/media/codecs/h265_parser.h>
12 #include <absl/log/check.h>
13 #include <absl/log/log.h>
15 #include <packager/macros/compiler.h>
16 #include <packager/macros/logging.h>
17 #include <packager/media/codecs/nalu_reader.h>
19 #define TRUE_OR_RETURN(a) \
22 DVLOG(1) << "Failure while processing " << #a; \
23 return kInvalidStream; \
27 #define OK_OR_RETURN(a) \
29 Result status = (a); \
34 #define READ_LONG_OR_RETURN(out) \
36 int _top_half, _bottom_half; \
37 if (!br->ReadBits(16, &_top_half)) { \
39 << "Error in stream: unexpected EOS while trying to read " #out; \
40 return kInvalidStream; \
42 if (!br->ReadBits(16, &_bottom_half)) { \
44 << "Error in stream: unexpected EOS while trying to read " #out; \
45 return kInvalidStream; \
47 *(out) = ((long)_top_half) << 16 | _bottom_half; \
54 int GetNumPicTotalCurr(
const H265SliceHeader& slice_header,
56 int num_pic_total_curr = 0;
57 const H265ReferencePictureSet& ref_pic_set =
58 slice_header.short_term_ref_pic_set_sps_flag
59 ? sps.st_ref_pic_sets[slice_header.short_term_ref_pic_set_idx]
60 : slice_header.st_ref_pic_set;
62 for (
int i = 0; i < ref_pic_set.num_negative_pics; i++) {
63 if (ref_pic_set.used_by_curr_pic_s0[i])
66 for (
int i = 0; i < ref_pic_set.num_positive_pics; i++) {
67 if (ref_pic_set.used_by_curr_pic_s1[i])
71 return num_pic_total_curr + slice_header.used_by_curr_pic_lt;
74 void GetAspectRatioInfo(
const H265Sps& sps,
75 uint32_t* pixel_width,
76 uint32_t* pixel_height) {
79 int aspect_ratio_idc = sps.vui_parameters.aspect_ratio_idc;
82 switch (aspect_ratio_idc) {
83 case 1: *pixel_width = 1; *pixel_height = 1;
break;
84 case 2: *pixel_width = 12; *pixel_height = 11;
break;
85 case 3: *pixel_width = 10; *pixel_height = 11;
break;
86 case 4: *pixel_width = 16; *pixel_height = 11;
break;
87 case 5: *pixel_width = 40; *pixel_height = 33;
break;
88 case 6: *pixel_width = 24; *pixel_height = 11;
break;
89 case 7: *pixel_width = 20; *pixel_height = 11;
break;
90 case 8: *pixel_width = 32; *pixel_height = 11;
break;
91 case 9: *pixel_width = 80; *pixel_height = 33;
break;
92 case 10: *pixel_width = 18; *pixel_height = 11;
break;
93 case 11: *pixel_width = 15; *pixel_height = 11;
break;
94 case 12: *pixel_width = 64; *pixel_height = 33;
break;
95 case 13: *pixel_width = 160; *pixel_height = 99;
break;
96 case 14: *pixel_width = 4; *pixel_height = 3;
break;
97 case 15: *pixel_width = 3; *pixel_height = 2;
break;
98 case 16: *pixel_width = 2; *pixel_height = 1;
break;
101 *pixel_width = sps.vui_parameters.sar_width;
102 *pixel_height = sps.vui_parameters.sar_height;
107 LOG(WARNING) <<
"Unknown aspect_ratio_idc " << aspect_ratio_idc;
108 FALLTHROUGH_INTENDED;
118 bool ExtractResolutionFromSps(
const H265Sps& sps,
119 uint32_t* coded_width,
120 uint32_t* coded_height,
121 uint32_t* pixel_width,
122 uint32_t* pixel_height) {
125 if (sps.conformance_window_flag) {
127 int sub_height_c = 0;
130 switch (sps.chroma_format_idc) {
148 LOG(ERROR) <<
"Unexpected chroma_format_idc " << sps.chroma_format_idc;
154 sub_width_c * (sps.conf_win_right_offset + sps.conf_win_left_offset);
156 sub_height_c * (sps.conf_win_bottom_offset + sps.conf_win_top_offset);
160 *coded_width = sps.pic_width_in_luma_samples - crop_x;
161 *coded_height = sps.pic_height_in_luma_samples - crop_y;
162 GetAspectRatioInfo(sps, pixel_width, pixel_height);
166 H265Pps::H265Pps() {}
167 H265Pps::~H265Pps() {}
169 H265Sps::H265Sps() {}
170 H265Sps::~H265Sps() {}
172 int H265Sps::GetPicSizeInCtbsY()
const {
173 int min_cb_log2_size_y = log2_min_luma_coding_block_size_minus3 + 3;
174 int ctb_log2_size_y =
175 min_cb_log2_size_y + log2_diff_max_min_luma_coding_block_size;
176 int ctb_size_y = 1 << ctb_log2_size_y;
179 int pic_width_in_ctbs_y = (pic_width_in_luma_samples - 1) / ctb_size_y + 1;
180 int pic_height_in_ctbs_y = (pic_height_in_luma_samples - 1) / ctb_size_y + 1;
181 return pic_width_in_ctbs_y * pic_height_in_ctbs_y;
184 int H265Sps::GetChromaArrayType()
const {
185 if (!separate_colour_plane_flag)
186 return chroma_format_idc;
191 H265ReferencePictureListModifications::H265ReferencePictureListModifications() {
193 H265ReferencePictureListModifications::
194 ~H265ReferencePictureListModifications() {}
196 H265SliceHeader::H265SliceHeader() {}
197 H265SliceHeader::~H265SliceHeader() {}
199 H265Parser::H265Parser() {}
200 H265Parser::~H265Parser() {}
212 TRUE_OR_RETURN(br->ReadBool(&slice_header->first_slice_segment_in_pic_flag));
213 if (nalu.
type() >= Nalu::H265_BLA_W_LP &&
214 nalu.
type() <= Nalu::H265_RSV_IRAP_VCL23) {
215 TRUE_OR_RETURN(br->ReadBool(&slice_header->no_output_of_prior_pics_flag));
218 TRUE_OR_RETURN(br->ReadUE(&slice_header->pic_parameter_set_id));
219 const H265Pps* pps =
GetPps(slice_header->pic_parameter_set_id);
225 if (!slice_header->first_slice_segment_in_pic_flag) {
226 if (pps->dependent_slice_segments_enabled_flag) {
227 TRUE_OR_RETURN(br->ReadBool(&slice_header->dependent_slice_segment_flag));
229 const int bit_length = ceil(log2(sps->GetPicSizeInCtbsY()));
230 TRUE_OR_RETURN(br->ReadBits(bit_length, &slice_header->segment_address));
233 if (!slice_header->dependent_slice_segment_flag) {
234 TRUE_OR_RETURN(br->SkipBits(pps->num_extra_slice_header_bits));
235 TRUE_OR_RETURN(br->ReadUE(&slice_header->slice_type));
236 if (pps->output_flag_present_flag) {
237 TRUE_OR_RETURN(br->ReadBool(&slice_header->pic_output_flag));
239 if (sps->separate_colour_plane_flag) {
240 TRUE_OR_RETURN(br->ReadBits(2, &slice_header->colour_plane_id));
243 if (nalu.
type() != Nalu::H265_IDR_W_RADL &&
244 nalu.
type() != Nalu::H265_IDR_N_LP) {
245 TRUE_OR_RETURN(br->ReadBits(sps->log2_max_pic_order_cnt_lsb_minus4 + 4,
246 &slice_header->slice_pic_order_cnt_lsb));
249 br->ReadBool(&slice_header->short_term_ref_pic_set_sps_flag));
250 if (!slice_header->short_term_ref_pic_set_sps_flag) {
251 OK_OR_RETURN(ParseReferencePictureSet(
252 sps->num_short_term_ref_pic_sets, sps->num_short_term_ref_pic_sets,
253 sps->st_ref_pic_sets, br, &slice_header->st_ref_pic_set));
254 }
else if (sps->num_short_term_ref_pic_sets > 1) {
256 br->ReadBits(ceil(log2(sps->num_short_term_ref_pic_sets)),
257 &slice_header->short_term_ref_pic_set_idx));
258 TRUE_OR_RETURN(slice_header->short_term_ref_pic_set_idx <
259 sps->num_short_term_ref_pic_sets);
262 if (sps->long_term_ref_pic_present_flag) {
263 if (sps->num_long_term_ref_pics > 0) {
264 TRUE_OR_RETURN(br->ReadUE(&slice_header->num_long_term_sps));
266 TRUE_OR_RETURN(br->ReadUE(&slice_header->num_long_term_pics));
268 const int pic_count =
269 slice_header->num_long_term_sps + slice_header->num_long_term_pics;
270 slice_header->long_term_pics_info.resize(pic_count);
271 for (
int i = 0; i < pic_count; i++) {
272 if (i < slice_header->num_long_term_sps) {
274 if (sps->num_long_term_ref_pics > 1) {
275 TRUE_OR_RETURN(br->ReadBits(
276 ceil(log2(sps->num_long_term_ref_pics)), <_idx_sps));
278 if (sps->used_by_curr_pic_lt_flag[lt_idx_sps])
279 slice_header->used_by_curr_pic_lt++;
281 TRUE_OR_RETURN(br->SkipBits(sps->log2_max_pic_order_cnt_lsb_minus4 +
283 bool used_by_curr_pic_lt_flag;
284 TRUE_OR_RETURN(br->ReadBool(&used_by_curr_pic_lt_flag));
285 if (used_by_curr_pic_lt_flag)
286 slice_header->used_by_curr_pic_lt++;
288 TRUE_OR_RETURN(br->ReadBool(&slice_header->long_term_pics_info[i]
289 .delta_poc_msb_present_flag));
290 if (slice_header->long_term_pics_info[i].delta_poc_msb_present_flag) {
291 TRUE_OR_RETURN(br->ReadUE(
292 &slice_header->long_term_pics_info[i].delta_poc_msb_cycle_lt));
297 if (sps->temporal_mvp_enabled_flag) {
299 br->ReadBool(&slice_header->slice_temporal_mvp_enabled_flag));
303 if (nalu.nuh_layer_id() != 0) {
304 NOTIMPLEMENTED() <<
"Multi-layer streams are not supported.";
305 return kUnsupportedStream;
308 if (sps->sample_adaptive_offset_enabled_flag) {
309 TRUE_OR_RETURN(br->ReadBool(&slice_header->slice_sao_luma_flag));
310 if (sps->GetChromaArrayType() != 0) {
311 TRUE_OR_RETURN(br->ReadBool(&slice_header->slice_sao_chroma_flag));
315 slice_header->num_ref_idx_l0_active_minus1 =
316 pps->num_ref_idx_l0_default_active_minus1;
317 slice_header->num_ref_idx_l1_active_minus1 =
318 pps->num_ref_idx_l1_default_active_minus1;
319 if (slice_header->slice_type == kPSlice ||
320 slice_header->slice_type == kBSlice) {
322 br->ReadBool(&slice_header->num_ref_idx_active_override_flag));
323 if (slice_header->num_ref_idx_active_override_flag) {
324 TRUE_OR_RETURN(br->ReadUE(&slice_header->num_ref_idx_l0_active_minus1));
325 if (slice_header->slice_type == kBSlice) {
327 br->ReadUE(&slice_header->num_ref_idx_l1_active_minus1));
331 const int num_pic_total_curr = GetNumPicTotalCurr(*slice_header, *sps);
332 if (pps->lists_modification_present_flag && num_pic_total_curr > 1) {
333 OK_OR_RETURN(SkipReferencePictureListModification(
334 *slice_header, *pps, num_pic_total_curr, br));
337 if (slice_header->slice_type == kBSlice) {
338 TRUE_OR_RETURN(br->ReadBool(&slice_header->mvd_l1_zero_flag));
340 if (pps->cabac_init_present_flag) {
341 TRUE_OR_RETURN(br->ReadBool(&slice_header->cabac_init_flag));
343 if (slice_header->slice_temporal_mvp_enabled_flag) {
344 if (slice_header->slice_type == kBSlice) {
345 TRUE_OR_RETURN(br->ReadBool(&slice_header->collocated_from_l0));
347 bool l0_greater_than_0 = slice_header->num_ref_idx_l0_active_minus1 > 0;
348 bool l1_greater_than_0 = slice_header->num_ref_idx_l1_active_minus1 > 0;
349 if (slice_header->collocated_from_l0 ? l0_greater_than_0
350 : l1_greater_than_0) {
351 TRUE_OR_RETURN(br->ReadUE(&slice_header->collocated_ref_idx));
355 if ((pps->weighted_pred_flag && slice_header->slice_type == kPSlice) ||
356 (pps->weighted_bipred_flag && slice_header->slice_type == kBSlice)) {
357 OK_OR_RETURN(SkipPredictionWeightTable(
358 slice_header->slice_type == kBSlice, *sps, *slice_header, br));
360 TRUE_OR_RETURN(br->ReadUE(&slice_header->five_minus_max_num_merge_cand));
363 TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_qp_delta));
364 if (pps->slice_chroma_qp_offsets_present_flag) {
365 TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_cb_qp_offset));
366 TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_cr_qp_offset));
369 if (pps->chroma_qp_offset_list_enabled_flag) {
371 br->ReadBool(&slice_header->cu_chroma_qp_offset_enabled_flag));
373 if (pps->deblocking_filter_override_enabled_flag) {
375 br->ReadBool(&slice_header->deblocking_filter_override_flag));
377 if (slice_header->deblocking_filter_override_flag) {
379 br->ReadBool(&slice_header->slice_deblocking_filter_disabled_flag));
380 if (!slice_header->slice_deblocking_filter_disabled_flag) {
381 TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_beta_offset_div2));
382 TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_tc_offset_div2));
385 if (pps->loop_filter_across_slices_enabled_flag &&
386 (slice_header->slice_sao_luma_flag ||
387 slice_header->slice_sao_chroma_flag ||
388 !slice_header->slice_deblocking_filter_disabled_flag)) {
389 TRUE_OR_RETURN(br->ReadBool(
390 &slice_header->slice_loop_filter_across_slices_enabled_flag));
394 if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) {
395 TRUE_OR_RETURN(br->ReadUE(&slice_header->num_entry_point_offsets));
396 if (slice_header->num_entry_point_offsets > 0) {
397 TRUE_OR_RETURN(br->ReadUE(&slice_header->offset_len_minus1));
398 slice_header->entry_point_offset_minus1.resize(
399 slice_header->num_entry_point_offsets);
400 for (
int i = 0; i < slice_header->num_entry_point_offsets; i++) {
402 br->ReadBits(slice_header->offset_len_minus1 + 1,
403 &slice_header->entry_point_offset_minus1[i]));
408 if (pps->slice_segment_header_extension_present_flag) {
409 int extension_length;
410 TRUE_OR_RETURN(br->ReadUE(&extension_length));
411 TRUE_OR_RETURN(br->SkipBits(extension_length * 8));
414 OK_OR_RETURN(ByteAlignment(br));
416 slice_header->header_bit_size = nalu.
payload_size() * 8 - br->NumBitsLeft();
421 DCHECK_EQ(Nalu::H265_PPS, nalu.
type());
429 std::unique_ptr<H265Pps> pps(
new H265Pps);
431 TRUE_OR_RETURN(br->ReadUE(&pps->pic_parameter_set_id));
432 TRUE_OR_RETURN(br->ReadUE(&pps->seq_parameter_set_id));
434 TRUE_OR_RETURN(br->ReadBool(&pps->dependent_slice_segments_enabled_flag));
435 TRUE_OR_RETURN(br->ReadBool(&pps->output_flag_present_flag));
436 TRUE_OR_RETURN(br->ReadBits(3, &pps->num_extra_slice_header_bits));
437 TRUE_OR_RETURN(br->ReadBool(&pps->sign_data_hiding_enabled_flag));
438 TRUE_OR_RETURN(br->ReadBool(&pps->cabac_init_present_flag));
440 TRUE_OR_RETURN(br->ReadUE(&pps->num_ref_idx_l0_default_active_minus1));
441 TRUE_OR_RETURN(br->ReadUE(&pps->num_ref_idx_l1_default_active_minus1));
442 TRUE_OR_RETURN(br->ReadSE(&pps->init_qp_minus26));
443 TRUE_OR_RETURN(br->ReadBool(&pps->constrained_intra_pred_flag));
444 TRUE_OR_RETURN(br->ReadBool(&pps->transform_skip_enabled_flag));
446 TRUE_OR_RETURN(br->ReadBool(&pps->cu_qp_delta_enabled_flag));
447 if (pps->cu_qp_delta_enabled_flag)
448 TRUE_OR_RETURN(br->ReadUE(&pps->diff_cu_qp_delta_depth));
449 TRUE_OR_RETURN(br->ReadSE(&pps->cb_qp_offset));
450 TRUE_OR_RETURN(br->ReadSE(&pps->cr_qp_offset));
452 TRUE_OR_RETURN(br->ReadBool(&pps->slice_chroma_qp_offsets_present_flag));
453 TRUE_OR_RETURN(br->ReadBool(&pps->weighted_pred_flag));
454 TRUE_OR_RETURN(br->ReadBool(&pps->weighted_bipred_flag));
455 TRUE_OR_RETURN(br->ReadBool(&pps->transquant_bypass_enabled_flag));
456 TRUE_OR_RETURN(br->ReadBool(&pps->tiles_enabled_flag));
457 TRUE_OR_RETURN(br->ReadBool(&pps->entropy_coding_sync_enabled_flag));
459 if (pps->tiles_enabled_flag) {
460 TRUE_OR_RETURN(br->ReadUE(&pps->num_tile_columns_minus1));
461 TRUE_OR_RETURN(br->ReadUE(&pps->num_tile_rows_minus1));
462 TRUE_OR_RETURN(br->ReadBool(&pps->uniform_spacing_flag));
463 if (!pps->uniform_spacing_flag) {
464 pps->column_width_minus1.resize(pps->num_tile_columns_minus1);
465 for (
int i = 0; i < pps->num_tile_columns_minus1; i++) {
466 TRUE_OR_RETURN(br->ReadUE(&pps->column_width_minus1[i]));
468 pps->row_height_minus1.resize(pps->num_tile_rows_minus1);
469 for (
int i = 0; i < pps->num_tile_rows_minus1; i++) {
470 TRUE_OR_RETURN(br->ReadUE(&pps->row_height_minus1[i]));
473 TRUE_OR_RETURN(br->ReadBool(&pps->loop_filter_across_tiles_enabled_flag));
476 TRUE_OR_RETURN(br->ReadBool(&pps->loop_filter_across_slices_enabled_flag));
477 TRUE_OR_RETURN(br->ReadBool(&pps->deblocking_filter_control_present_flag));
478 if (pps->deblocking_filter_control_present_flag) {
479 TRUE_OR_RETURN(br->ReadBool(&pps->deblocking_filter_override_enabled_flag));
480 TRUE_OR_RETURN(br->ReadBool(&pps->deblocking_filter_disabled_flag));
481 if (!pps->deblocking_filter_disabled_flag) {
482 TRUE_OR_RETURN(br->ReadSE(&pps->beta_offset_div2));
483 TRUE_OR_RETURN(br->ReadSE(&pps->tc_offset_div2));
487 TRUE_OR_RETURN(br->ReadBool(&pps->scaling_list_data_present_flag));
488 if (pps->scaling_list_data_present_flag) {
489 OK_OR_RETURN(SkipScalingListData(br));
492 TRUE_OR_RETURN(br->ReadBool(&pps->lists_modification_present_flag));
493 TRUE_OR_RETURN(br->ReadUE(&pps->log2_parallel_merge_level_minus2));
496 br->ReadBool(&pps->slice_segment_header_extension_present_flag));
498 bool pps_extension_present_flag;
499 bool pps_range_extension_flag =
false;
500 TRUE_OR_RETURN(br->ReadBool(&pps_extension_present_flag));
501 if (pps_extension_present_flag) {
502 TRUE_OR_RETURN(br->ReadBool(&pps_range_extension_flag));
504 TRUE_OR_RETURN(br->SkipBits(1 + 1 + 5));
507 if (pps_range_extension_flag) {
508 if (pps->transform_skip_enabled_flag) {
511 TRUE_OR_RETURN(br->ReadUE(&ignored));
514 TRUE_OR_RETURN(br->SkipBits(1));
515 TRUE_OR_RETURN(br->ReadBool(&pps->chroma_qp_offset_list_enabled_flag));
522 *pps_id = pps->pic_parameter_set_id;
523 active_ppses_[*pps_id] = std::move(pps);
529 DCHECK_EQ(Nalu::H265_SPS, nalu.
type());
538 std::unique_ptr<H265Sps> sps(
new H265Sps);
540 TRUE_OR_RETURN(br->ReadBits(4, &sps->video_parameter_set_id));
541 TRUE_OR_RETURN(br->ReadBits(3, &sps->max_sub_layers_minus1));
542 TRUE_OR_RETURN(br->ReadBool(&sps->temporal_id_nesting_flag));
545 ReadProfileTierLevel(
true, sps->max_sub_layers_minus1, br, sps.get()));
547 TRUE_OR_RETURN(br->ReadUE(&sps->seq_parameter_set_id));
548 TRUE_OR_RETURN(br->ReadUE(&sps->chroma_format_idc));
549 if (sps->chroma_format_idc == 3) {
550 TRUE_OR_RETURN(br->ReadBool(&sps->separate_colour_plane_flag));
552 TRUE_OR_RETURN(br->ReadUE(&sps->pic_width_in_luma_samples));
553 TRUE_OR_RETURN(br->ReadUE(&sps->pic_height_in_luma_samples));
555 TRUE_OR_RETURN(br->ReadBool(&sps->conformance_window_flag));
556 if (sps->conformance_window_flag) {
557 TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_left_offset));
558 TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_right_offset));
559 TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_top_offset));
560 TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_bottom_offset));
563 TRUE_OR_RETURN(br->ReadUE(&sps->bit_depth_luma_minus8));
564 TRUE_OR_RETURN(br->ReadUE(&sps->bit_depth_chroma_minus8));
565 TRUE_OR_RETURN(br->ReadUE(&sps->log2_max_pic_order_cnt_lsb_minus4));
567 TRUE_OR_RETURN(br->ReadBool(&sps->sub_layer_ordering_info_present_flag));
568 int start = sps->sub_layer_ordering_info_present_flag
570 : sps->max_sub_layers_minus1;
571 for (
int i = start; i <= sps->max_sub_layers_minus1; i++) {
572 TRUE_OR_RETURN(br->ReadUE(&sps->max_dec_pic_buffering_minus1[i]));
573 TRUE_OR_RETURN(br->ReadUE(&sps->max_num_reorder_pics[i]));
574 TRUE_OR_RETURN(br->ReadUE(&sps->max_latency_increase_plus1[i]));
577 TRUE_OR_RETURN(br->ReadUE(&sps->log2_min_luma_coding_block_size_minus3));
578 TRUE_OR_RETURN(br->ReadUE(&sps->log2_diff_max_min_luma_coding_block_size));
579 TRUE_OR_RETURN(br->ReadUE(&sps->log2_min_luma_transform_block_size_minus2));
580 TRUE_OR_RETURN(br->ReadUE(&sps->log2_diff_max_min_luma_transform_block_size));
581 TRUE_OR_RETURN(br->ReadUE(&sps->max_transform_hierarchy_depth_inter));
582 TRUE_OR_RETURN(br->ReadUE(&sps->max_transform_hierarchy_depth_intra));
584 TRUE_OR_RETURN(br->ReadBool(&sps->scaling_list_enabled_flag));
585 if (sps->scaling_list_enabled_flag) {
586 TRUE_OR_RETURN(br->ReadBool(&sps->scaling_list_data_present_flag));
587 if (sps->scaling_list_data_present_flag) {
588 OK_OR_RETURN(SkipScalingListData(br));
592 TRUE_OR_RETURN(br->ReadBool(&sps->amp_enabled_flag));
593 TRUE_OR_RETURN(br->ReadBool(&sps->sample_adaptive_offset_enabled_flag));
594 TRUE_OR_RETURN(br->ReadBool(&sps->pcm_enabled_flag));
595 if (sps->pcm_enabled_flag) {
596 TRUE_OR_RETURN(br->ReadBits(4, &sps->pcm_sample_bit_depth_luma_minus1));
597 TRUE_OR_RETURN(br->ReadBits(4, &sps->pcm_sample_bit_depth_chroma_minus1));
599 br->ReadUE(&sps->log2_min_pcm_luma_coding_block_size_minus3));
601 br->ReadUE(&sps->log2_diff_max_min_pcm_luma_coding_block_size));
602 TRUE_OR_RETURN(br->ReadBool(&sps->pcm_loop_filter_disabled_flag));
605 TRUE_OR_RETURN(br->ReadUE(&sps->num_short_term_ref_pic_sets));
606 sps->st_ref_pic_sets.resize(sps->num_short_term_ref_pic_sets);
607 for (
int i = 0; i < sps->num_short_term_ref_pic_sets; i++) {
608 OK_OR_RETURN(ParseReferencePictureSet(sps->num_short_term_ref_pic_sets, i,
609 sps->st_ref_pic_sets, br,
610 &sps->st_ref_pic_sets[i]));
613 TRUE_OR_RETURN(br->ReadBool(&sps->long_term_ref_pic_present_flag));
614 if (sps->long_term_ref_pic_present_flag) {
615 TRUE_OR_RETURN(br->ReadUE(&sps->num_long_term_ref_pics));
616 sps->lt_ref_pic_poc_lsb.resize(sps->num_long_term_ref_pics);
617 sps->used_by_curr_pic_lt_flag.resize(sps->num_long_term_ref_pics);
618 for (
int i = 0; i < sps->num_long_term_ref_pics; i++) {
619 TRUE_OR_RETURN(br->ReadBits(sps->log2_max_pic_order_cnt_lsb_minus4 + 4,
620 &sps->lt_ref_pic_poc_lsb[i]));
622 TRUE_OR_RETURN(br->ReadBool(&temp));
623 sps->used_by_curr_pic_lt_flag[i] = temp;
627 TRUE_OR_RETURN(br->ReadBool(&sps->temporal_mvp_enabled_flag));
628 TRUE_OR_RETURN(br->ReadBool(&sps->strong_intra_smoothing_enabled_flag));
630 TRUE_OR_RETURN(br->ReadBool(&sps->vui_parameters_present));
631 if (sps->vui_parameters_present) {
632 OK_OR_RETURN(ParseVuiParameters(sps->max_sub_layers_minus1, br,
633 &sps->vui_parameters));
639 *sps_id = sps->seq_parameter_set_id;
640 active_spses_[*sps_id] = std::move(sps);
646 return active_ppses_[pps_id].get();
650 return active_spses_[sps_id].get();
653 H265Parser::Result H265Parser::ParseVuiParameters(
int max_num_sub_layers_minus1,
659 TRUE_OR_RETURN(br->ReadBool(&vui->aspect_ratio_info_present_flag));
660 if (vui->aspect_ratio_info_present_flag) {
661 TRUE_OR_RETURN(br->ReadBits(8, &vui->aspect_ratio_idc));
662 if (vui->aspect_ratio_idc == H265VuiParameters::kExtendedSar) {
663 TRUE_OR_RETURN(br->ReadBits(16, &vui->sar_width));
664 TRUE_OR_RETURN(br->ReadBits(16, &vui->sar_height));
668 bool overscan_info_present_flag;
669 TRUE_OR_RETURN(br->ReadBool(&overscan_info_present_flag));
670 if (overscan_info_present_flag) {
671 TRUE_OR_RETURN(br->SkipBits(1));
674 bool video_signal_type_present_flag;
675 TRUE_OR_RETURN(br->ReadBool(&video_signal_type_present_flag));
676 if (video_signal_type_present_flag) {
677 TRUE_OR_RETURN(br->SkipBits(3));
678 TRUE_OR_RETURN(br->SkipBits(1));
680 bool colour_description_present_flag;
681 TRUE_OR_RETURN(br->ReadBool(&colour_description_present_flag));
682 if (colour_description_present_flag) {
684 br->ReadBits(8, &vui->color_primaries));
685 TRUE_OR_RETURN(br->ReadBits(8, &vui->transfer_characteristics));
687 br->ReadBits(8, &vui->matrix_coefficients));
691 bool chroma_loc_info_present_flag;
692 TRUE_OR_RETURN(br->ReadBool(&chroma_loc_info_present_flag));
693 if (chroma_loc_info_present_flag) {
695 TRUE_OR_RETURN(br->ReadUE(&ignored));
696 TRUE_OR_RETURN(br->ReadUE(&ignored));
701 TRUE_OR_RETURN(br->SkipBits(3));
703 bool default_display_window_flag;
704 TRUE_OR_RETURN(br->ReadBool(&default_display_window_flag));
705 if (default_display_window_flag) {
706 TRUE_OR_RETURN(br->ReadUE(&ignored));
707 TRUE_OR_RETURN(br->ReadUE(&ignored));
708 TRUE_OR_RETURN(br->ReadUE(&ignored));
709 TRUE_OR_RETURN(br->ReadUE(&ignored));
712 TRUE_OR_RETURN(br->ReadBool(&vui->vui_timing_info_present_flag));
713 if (vui->vui_timing_info_present_flag) {
714 READ_LONG_OR_RETURN(&vui->vui_num_units_in_tick);
715 READ_LONG_OR_RETURN(&vui->vui_time_scale);
717 bool vui_poc_proportional_to_timing_flag;
718 TRUE_OR_RETURN(br->ReadBool(&vui_poc_proportional_to_timing_flag));
719 if (vui_poc_proportional_to_timing_flag) {
721 TRUE_OR_RETURN(br->ReadUE(&ignored));
724 bool vui_hdr_parameters_present_flag;
725 TRUE_OR_RETURN(br->ReadBool(&vui_hdr_parameters_present_flag));
726 if (vui_hdr_parameters_present_flag) {
727 OK_OR_RETURN(SkipHrdParameters(max_num_sub_layers_minus1, br));
731 TRUE_OR_RETURN(br->ReadBool(&vui->bitstream_restriction_flag));
732 if (vui->bitstream_restriction_flag) {
735 TRUE_OR_RETURN(br->SkipBits(3));
737 TRUE_OR_RETURN(br->ReadUE(&vui->min_spatial_segmentation_idc));
738 TRUE_OR_RETURN(br->ReadUE(&ignored));
739 TRUE_OR_RETURN(br->ReadUE(&ignored));
740 TRUE_OR_RETURN(br->ReadUE(&ignored));
741 TRUE_OR_RETURN(br->ReadUE(&ignored));
747 H265Parser::Result H265Parser::ParseReferencePictureSet(
748 int num_short_term_ref_pic_sets,
750 const std::vector<H265ReferencePictureSet>& ref_pic_sets,
752 H265ReferencePictureSet* out_ref_pic_set) {
757 bool inter_ref_pic_set_prediction =
false;
758 if (st_rps_idx != 0) {
759 TRUE_OR_RETURN(br->ReadBool(&inter_ref_pic_set_prediction));
762 if (inter_ref_pic_set_prediction) {
764 if (st_rps_idx == num_short_term_ref_pic_sets) {
765 TRUE_OR_RETURN(br->ReadUE(&delta_idx));
767 TRUE_OR_RETURN(delta_idx <= st_rps_idx);
770 int ref_rps_idx = st_rps_idx - delta_idx;
771 DCHECK_LE(0, ref_rps_idx);
772 DCHECK_LT(ref_rps_idx, st_rps_idx);
775 int abs_delta_rps_minus1;
776 TRUE_OR_RETURN(br->ReadBool(&delta_rps_sign));
777 TRUE_OR_RETURN(br->ReadUE(&abs_delta_rps_minus1));
779 delta_rps_sign ? -(abs_delta_rps_minus1 + 1) : abs_delta_rps_minus1 + 1;
781 int ref_num_delta_pocs = ref_pic_sets[ref_rps_idx].num_delta_pocs;
782 std::vector<bool> used_by_curr_pic(ref_num_delta_pocs + 1);
783 std::vector<bool> use_delta(ref_num_delta_pocs + 1);
784 for (
int j = 0; j <= ref_num_delta_pocs; j++) {
786 TRUE_OR_RETURN(br->ReadBool(&temp));
787 used_by_curr_pic[j] = temp;
789 if (!used_by_curr_pic[j]) {
790 TRUE_OR_RETURN(br->ReadBool(&temp));
797 int ref_num_positive_pics = ref_pic_sets[ref_rps_idx].num_positive_pics;
798 int ref_num_negative_pics = ref_pic_sets[ref_rps_idx].num_negative_pics;
804 for (
int j = ref_num_positive_pics - 1; j >= 0; j--) {
805 int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s1[j] + delta_rps;
806 if (d_poc < 0 && use_delta[ref_num_negative_pics + j]) {
807 out_ref_pic_set->delta_poc_s0[i] = d_poc;
808 out_ref_pic_set->used_by_curr_pic_s0[i] =
809 used_by_curr_pic[ref_num_negative_pics + j];
813 if (delta_rps < 0 && use_delta[ref_num_delta_pocs]) {
814 out_ref_pic_set->delta_poc_s0[i] = delta_rps;
815 out_ref_pic_set->used_by_curr_pic_s0[i] =
816 used_by_curr_pic[ref_num_delta_pocs];
819 for (
int j = 0; j < ref_num_negative_pics; j++) {
820 int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s0[j] + delta_rps;
821 if (d_poc < 0 && use_delta[j]) {
822 out_ref_pic_set->delta_poc_s0[i] = d_poc;
823 out_ref_pic_set->used_by_curr_pic_s0[i] = used_by_curr_pic[j];
827 out_ref_pic_set->num_negative_pics = i;
833 for (
int j = ref_num_negative_pics - 1; j >= 0; j--) {
834 int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s0[j] + delta_rps;
835 if (d_poc > 0 && use_delta[j]) {
836 out_ref_pic_set->delta_poc_s1[i] = d_poc;
837 out_ref_pic_set->used_by_curr_pic_s1[i] = used_by_curr_pic[j];
841 if (delta_rps > 0 && use_delta[ref_num_delta_pocs]) {
842 out_ref_pic_set->delta_poc_s1[i] = delta_rps;
843 out_ref_pic_set->used_by_curr_pic_s1[i] =
844 used_by_curr_pic[ref_num_delta_pocs];
847 for (
int j = 0; j < ref_num_positive_pics; j++) {
848 int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s1[j] + delta_rps;
849 if (d_poc > 0 && use_delta[ref_num_negative_pics + j]) {
850 out_ref_pic_set->delta_poc_s1[i] = d_poc;
851 out_ref_pic_set->used_by_curr_pic_s1[i] =
852 used_by_curr_pic[ref_num_negative_pics + j];
856 out_ref_pic_set->num_positive_pics = i;
859 TRUE_OR_RETURN(br->ReadUE(&out_ref_pic_set->num_negative_pics));
860 TRUE_OR_RETURN(out_ref_pic_set->num_negative_pics <= kMaxRefPicSetCount);
861 TRUE_OR_RETURN(br->ReadUE(&out_ref_pic_set->num_positive_pics));
862 TRUE_OR_RETURN(out_ref_pic_set->num_positive_pics <= kMaxRefPicSetCount);
865 for (
int i = 0; i < out_ref_pic_set->num_negative_pics; i++) {
866 int delta_poc_s0_minus1;
867 TRUE_OR_RETURN(br->ReadUE(&delta_poc_s0_minus1));
868 out_ref_pic_set->delta_poc_s0[i] = prev_poc - (delta_poc_s0_minus1 + 1);
869 prev_poc = out_ref_pic_set->delta_poc_s0[i];
871 TRUE_OR_RETURN(br->ReadBool(&out_ref_pic_set->used_by_curr_pic_s0[i]));
875 for (
int i = 0; i < out_ref_pic_set->num_positive_pics; i++) {
876 int delta_poc_s1_minus1;
877 TRUE_OR_RETURN(br->ReadUE(&delta_poc_s1_minus1));
878 out_ref_pic_set->delta_poc_s1[i] = prev_poc + delta_poc_s1_minus1 + 1;
879 prev_poc = out_ref_pic_set->delta_poc_s1[i];
881 TRUE_OR_RETURN(br->ReadBool(&out_ref_pic_set->used_by_curr_pic_s1[i]));
885 out_ref_pic_set->num_delta_pocs =
886 out_ref_pic_set->num_positive_pics + out_ref_pic_set->num_negative_pics;
890 H265Parser::Result H265Parser::SkipReferencePictureListModification(
891 const H265SliceHeader& slice_header,
893 int num_pic_total_curr,
897 bool ref_pic_list_modification_flag_l0;
898 TRUE_OR_RETURN(br->ReadBool(&ref_pic_list_modification_flag_l0));
899 if (ref_pic_list_modification_flag_l0) {
900 for (
int i = 0; i <= slice_header.num_ref_idx_l0_active_minus1; i++) {
901 TRUE_OR_RETURN(br->SkipBits(ceil(log2(num_pic_total_curr))));
905 if (slice_header.slice_type == kBSlice) {
906 bool ref_pic_list_modification_flag_l1;
907 TRUE_OR_RETURN(br->ReadBool(&ref_pic_list_modification_flag_l1));
908 if (ref_pic_list_modification_flag_l1) {
909 for (
int i = 0; i <= slice_header.num_ref_idx_l1_active_minus1; i++) {
910 TRUE_OR_RETURN(br->SkipBits(ceil(log2(num_pic_total_curr))));
918 H265Parser::Result H265Parser::SkipPredictionWeightTablePart(
919 int num_ref_idx_minus1,
920 int chroma_array_type,
924 std::vector<bool> luma_weight_flag(num_ref_idx_minus1 + 1);
925 std::vector<bool> chroma_weight_flag(num_ref_idx_minus1 + 1);
927 for (
int i = 0; i <= num_ref_idx_minus1; i++) {
929 TRUE_OR_RETURN(br->ReadBool(&temp));
930 luma_weight_flag[i] = temp;
932 if (chroma_array_type != 0) {
933 for (
int i = 0; i <= num_ref_idx_minus1; i++) {
935 TRUE_OR_RETURN(br->ReadBool(&temp));
936 chroma_weight_flag[i] = temp;
939 for (
int i = 0; i <= num_ref_idx_minus1; i++) {
940 if (luma_weight_flag[i]) {
941 TRUE_OR_RETURN(br->ReadSE(&ignored));
942 TRUE_OR_RETURN(br->ReadSE(&ignored));
944 if (chroma_weight_flag[i]) {
945 for (
int j = 0; j < 2; j++) {
946 TRUE_OR_RETURN(br->ReadSE(&ignored));
947 TRUE_OR_RETURN(br->ReadSE(&ignored));
955 H265Parser::Result H265Parser::SkipPredictionWeightTable(
958 const H265SliceHeader& slice_header,
962 int chroma_array_type = sps.GetChromaArrayType();
964 TRUE_OR_RETURN(br->ReadUE(&ignored));
965 if (chroma_array_type != 0) {
966 TRUE_OR_RETURN(br->ReadSE(&ignored));
968 OK_OR_RETURN(SkipPredictionWeightTablePart(
969 slice_header.num_ref_idx_l0_active_minus1, chroma_array_type, br));
971 OK_OR_RETURN(SkipPredictionWeightTablePart(
972 slice_header.num_ref_idx_l1_active_minus1, chroma_array_type, br));
978 H265Parser::Result H265Parser::ReadProfileTierLevel(
979 bool profile_present,
980 int max_num_sub_layers_minus1,
985 if (profile_present) {
994 for (
int i = 0; i < 11; i++)
995 TRUE_OR_RETURN(br->ReadBits(8, &sps->general_profile_tier_level_data[i]));
998 TRUE_OR_RETURN(br->ReadBits(8, &sps->general_profile_tier_level_data[11]));
1000 std::vector<bool> sub_layer_profile_present(max_num_sub_layers_minus1);
1001 std::vector<bool> sub_layer_level_present(max_num_sub_layers_minus1);
1002 for (
int i = 0; i < max_num_sub_layers_minus1; i++) {
1003 bool profile, level;
1004 TRUE_OR_RETURN(br->ReadBool(&profile));
1005 TRUE_OR_RETURN(br->ReadBool(&level));
1006 sub_layer_profile_present[i] = profile;
1007 sub_layer_level_present[i] = level;
1010 if (max_num_sub_layers_minus1 > 0) {
1011 for (
int i = max_num_sub_layers_minus1; i < 8; i++)
1012 TRUE_OR_RETURN(br->SkipBits(2));
1015 for (
int i = 0; i < max_num_sub_layers_minus1; i++) {
1016 if (sub_layer_profile_present[i]) {
1021 TRUE_OR_RETURN(br->SkipBits(2 + 1 + 5 + 32 + 4 + 43 + 1));
1023 if (sub_layer_level_present[i]) {
1024 TRUE_OR_RETURN(br->SkipBits(8));
1031 H265Parser::Result H265Parser::SkipScalingListData(H26xBitReader* br) {
1034 for (
int size_id = 0; size_id < 4; size_id++) {
1035 for (
int matrix_id = 0; matrix_id < 6;
1036 matrix_id += ((size_id == 3) ? 3 : 1)) {
1037 bool scaling_list_pred_mode;
1038 TRUE_OR_RETURN(br->ReadBool(&scaling_list_pred_mode));
1039 if (!scaling_list_pred_mode) {
1041 TRUE_OR_RETURN(br->ReadUE(&ignored));
1043 int coefNum = std::min(64, (1 << (4 + (size_id << 1))));
1045 TRUE_OR_RETURN(br->ReadSE(&ignored));
1048 for (
int i = 0; i < coefNum; i++) {
1049 TRUE_OR_RETURN(br->ReadSE(&ignored));
1058 H265Parser::Result H265Parser::SkipHrdParameters(
int max_num_sub_layers_minus1,
1059 H26xBitReader* br) {
1061 const bool common_inf_present_flag =
true;
1064 bool nal_hdr_parameters_present_flag;
1065 bool vcl_hdr_parameters_present_flag;
1066 bool sub_pic_hdr_params_present_flag =
false;
1067 if (common_inf_present_flag) {
1068 TRUE_OR_RETURN(br->ReadBool(&nal_hdr_parameters_present_flag));
1069 TRUE_OR_RETURN(br->ReadBool(&vcl_hdr_parameters_present_flag));
1070 if (nal_hdr_parameters_present_flag || vcl_hdr_parameters_present_flag) {
1071 TRUE_OR_RETURN(br->ReadBool(&sub_pic_hdr_params_present_flag));
1072 if (sub_pic_hdr_params_present_flag) {
1076 TRUE_OR_RETURN(br->SkipBits(8 + 5 + 1 + 5));
1080 TRUE_OR_RETURN(br->SkipBits(4 + 4));
1081 if (sub_pic_hdr_params_present_flag)
1082 TRUE_OR_RETURN(br->SkipBits(4));
1086 TRUE_OR_RETURN(br->SkipBits(5 + 5 + 5));
1090 for (
int i = 0; i <= max_num_sub_layers_minus1; i++) {
1091 bool fixed_pic_rate_general_flag;
1092 bool fixed_pic_rate_within_cvs_flag =
true;
1093 bool low_delay_hdr_flag =
false;
1094 int cpb_cnt_minus1 = 0;
1095 TRUE_OR_RETURN(br->ReadBool(&fixed_pic_rate_general_flag));
1096 if (!fixed_pic_rate_general_flag)
1097 TRUE_OR_RETURN(br->ReadBool(&fixed_pic_rate_within_cvs_flag));
1098 if (fixed_pic_rate_within_cvs_flag)
1099 TRUE_OR_RETURN(br->ReadUE(&ignored));
1101 TRUE_OR_RETURN(br->ReadBool(&low_delay_hdr_flag));
1102 if (!low_delay_hdr_flag)
1103 TRUE_OR_RETURN(br->ReadUE(&cpb_cnt_minus1));
1105 if (nal_hdr_parameters_present_flag) {
1106 OK_OR_RETURN(SkipSubLayerHrdParameters(
1107 cpb_cnt_minus1, sub_pic_hdr_params_present_flag, br));
1109 if (vcl_hdr_parameters_present_flag) {
1110 OK_OR_RETURN(SkipSubLayerHrdParameters(
1111 cpb_cnt_minus1, sub_pic_hdr_params_present_flag, br));
1118 H265Parser::Result H265Parser::SkipSubLayerHrdParameters(
1120 bool sub_pic_hdr_params_present_flag,
1121 H26xBitReader* br) {
1123 for (
int i = 0; i <= cpb_cnt_minus1; i++) {
1124 TRUE_OR_RETURN(br->ReadUE(&ignored));
1125 TRUE_OR_RETURN(br->ReadUE(&ignored));
1126 if (sub_pic_hdr_params_present_flag) {
1127 TRUE_OR_RETURN(br->ReadUE(&ignored));
1128 TRUE_OR_RETURN(br->ReadUE(&ignored));
1131 TRUE_OR_RETURN(br->SkipBits(1));
1137 H265Parser::Result H265Parser::ByteAlignment(H26xBitReader* br) {
1138 TRUE_OR_RETURN(br->SkipBits(1));
1139 TRUE_OR_RETURN(br->SkipBits(br->NumBitsLeft() % 8));
All the methods that are virtual are virtual for mocking.