7 #include "packager/media/codecs/h265_parser.h"
12 #include "packager/base/logging.h"
13 #include "packager/base/stl_util.h"
14 #include "packager/media/base/macros.h"
15 #include "packager/media/codecs/nalu_reader.h"
17 #define TRUE_OR_RETURN(a) \
20 DVLOG(1) << "Failure while processing " << #a; \
21 return kInvalidStream; \
25 #define OK_OR_RETURN(a) \
27 Result status = (a); \
36 int GetNumPicTotalCurr(
const H265SliceHeader& slice_header,
38 int num_pic_total_curr = 0;
39 const H265ReferencePictureSet& ref_pic_set =
40 slice_header.short_term_ref_pic_set_sps_flag
41 ? sps.st_ref_pic_sets[slice_header.short_term_ref_pic_set_idx]
42 : slice_header.st_ref_pic_set;
44 for (
int i = 0; i < ref_pic_set.num_negative_pics; i++) {
45 if (ref_pic_set.used_by_curr_pic_s0[i])
48 for (
int i = 0; i < ref_pic_set.num_positive_pics; i++) {
49 if (ref_pic_set.used_by_curr_pic_s1[i])
53 return num_pic_total_curr + slice_header.used_by_curr_pic_lt;
56 void GetAspectRatioInfo(
const H265Sps& sps,
57 uint32_t* pixel_width,
58 uint32_t* pixel_height) {
61 int aspect_ratio_idc = sps.vui_parameters.aspect_ratio_idc;
64 switch (aspect_ratio_idc) {
65 case 1: *pixel_width = 1; *pixel_height = 1;
break;
66 case 2: *pixel_width = 12; *pixel_height = 11;
break;
67 case 3: *pixel_width = 10; *pixel_height = 11;
break;
68 case 4: *pixel_width = 16; *pixel_height = 11;
break;
69 case 5: *pixel_width = 40; *pixel_height = 33;
break;
70 case 6: *pixel_width = 24; *pixel_height = 11;
break;
71 case 7: *pixel_width = 20; *pixel_height = 11;
break;
72 case 8: *pixel_width = 32; *pixel_height = 11;
break;
73 case 9: *pixel_width = 80; *pixel_height = 33;
break;
74 case 10: *pixel_width = 18; *pixel_height = 11;
break;
75 case 11: *pixel_width = 15; *pixel_height = 11;
break;
76 case 12: *pixel_width = 64; *pixel_height = 33;
break;
77 case 13: *pixel_width = 160; *pixel_height = 99;
break;
78 case 14: *pixel_width = 4; *pixel_height = 3;
break;
79 case 15: *pixel_width = 3; *pixel_height = 2;
break;
80 case 16: *pixel_width = 2; *pixel_height = 1;
break;
83 *pixel_width = sps.vui_parameters.sar_width;
84 *pixel_height = sps.vui_parameters.sar_height;
89 LOG(WARNING) <<
"Unknown aspect_ratio_idc " << aspect_ratio_idc;
100 bool ExtractResolutionFromSps(
const H265Sps& sps,
101 uint32_t* coded_width,
102 uint32_t* coded_height,
103 uint32_t* pixel_width,
104 uint32_t* pixel_height) {
107 if (sps.conformance_window_flag) {
109 int sub_height_c = 0;
112 switch (sps.chroma_format_idc) {
130 LOG(ERROR) <<
"Unexpected chroma_format_idc " << sps.chroma_format_idc;
136 sub_width_c * (sps.conf_win_right_offset + sps.conf_win_left_offset);
138 sub_height_c * (sps.conf_win_bottom_offset + sps.conf_win_top_offset);
142 *coded_width = sps.pic_width_in_luma_samples - crop_x;
143 *coded_height = sps.pic_height_in_luma_samples - crop_y;
144 GetAspectRatioInfo(sps, pixel_width, pixel_height);
148 H265Pps::H265Pps() {}
149 H265Pps::~H265Pps() {}
151 H265Sps::H265Sps() {}
152 H265Sps::~H265Sps() {}
154 int H265Sps::GetPicSizeInCtbsY()
const {
155 int min_cb_log2_size_y = log2_min_luma_coding_block_size_minus3 + 3;
156 int ctb_log2_size_y =
157 min_cb_log2_size_y + log2_diff_max_min_luma_coding_block_size;
158 int ctb_size_y = 1 << ctb_log2_size_y;
161 int pic_width_in_ctbs_y = (pic_width_in_luma_samples - 1) / ctb_size_y + 1;
162 int pic_height_in_ctbs_y = (pic_height_in_luma_samples - 1) / ctb_size_y + 1;
163 return pic_width_in_ctbs_y * pic_height_in_ctbs_y;
166 int H265Sps::GetChromaArrayType()
const {
167 if (!separate_colour_plane_flag)
168 return chroma_format_idc;
173 H265ReferencePictureListModifications::H265ReferencePictureListModifications() {
175 H265ReferencePictureListModifications::
176 ~H265ReferencePictureListModifications() {}
178 H265SliceHeader::H265SliceHeader() {}
179 H265SliceHeader::~H265SliceHeader() {}
181 H265Parser::H265Parser() {}
182 H265Parser::~H265Parser() {
183 STLDeleteValues(&active_spses_);
184 STLDeleteValues(&active_ppses_);
189 DCHECK(nalu.is_video_slice());
197 TRUE_OR_RETURN(br->ReadBool(&slice_header->first_slice_segment_in_pic_flag));
198 if (nalu.
type() >= Nalu::H265_BLA_W_LP &&
199 nalu.
type() <= Nalu::H265_RSV_IRAP_VCL23) {
200 TRUE_OR_RETURN(br->ReadBool(&slice_header->no_output_of_prior_pics_flag));
203 TRUE_OR_RETURN(br->ReadUE(&slice_header->pic_parameter_set_id));
204 const H265Pps* pps =
GetPps(slice_header->pic_parameter_set_id);
210 if (!slice_header->first_slice_segment_in_pic_flag) {
211 if (pps->dependent_slice_segments_enabled_flag) {
212 TRUE_OR_RETURN(br->ReadBool(&slice_header->dependent_slice_segment_flag));
214 const int bit_length = ceil(log2(sps->GetPicSizeInCtbsY()));
215 TRUE_OR_RETURN(br->ReadBits(bit_length, &slice_header->segment_address));
218 if (!slice_header->dependent_slice_segment_flag) {
219 TRUE_OR_RETURN(br->SkipBits(pps->num_extra_slice_header_bits));
220 TRUE_OR_RETURN(br->ReadUE(&slice_header->slice_type));
221 if (pps->output_flag_present_flag) {
222 TRUE_OR_RETURN(br->ReadBool(&slice_header->pic_output_flag));
224 if (sps->separate_colour_plane_flag) {
225 TRUE_OR_RETURN(br->ReadBits(2, &slice_header->colour_plane_id));
228 if (nalu.
type() != Nalu::H265_IDR_W_RADL &&
229 nalu.
type() != Nalu::H265_IDR_N_LP) {
230 TRUE_OR_RETURN(br->ReadBits(sps->log2_max_pic_order_cnt_lsb_minus4 + 4,
231 &slice_header->slice_pic_order_cnt_lsb));
234 br->ReadBool(&slice_header->short_term_ref_pic_set_sps_flag));
235 if (!slice_header->short_term_ref_pic_set_sps_flag) {
236 OK_OR_RETURN(ParseReferencePictureSet(
237 sps->num_short_term_ref_pic_sets, sps->num_short_term_ref_pic_sets,
238 sps->st_ref_pic_sets, br, &slice_header->st_ref_pic_set));
239 }
else if (sps->num_short_term_ref_pic_sets > 1) {
241 br->ReadBits(ceil(log2(sps->num_short_term_ref_pic_sets)),
242 &slice_header->short_term_ref_pic_set_idx));
245 if (sps->long_term_ref_pic_present_flag) {
246 if (sps->num_long_term_ref_pics > 0) {
247 TRUE_OR_RETURN(br->ReadUE(&slice_header->num_long_term_sps));
249 TRUE_OR_RETURN(br->ReadUE(&slice_header->num_long_term_pics));
251 const int pic_count =
252 slice_header->num_long_term_sps + slice_header->num_long_term_pics;
253 slice_header->long_term_pics_info.resize(pic_count);
254 for (
int i = 0; i < pic_count; i++) {
255 if (i < slice_header->num_long_term_sps) {
257 if (sps->num_long_term_ref_pics > 1) {
258 TRUE_OR_RETURN(br->ReadBits(
259 ceil(log2(sps->num_long_term_ref_pics)), <_idx_sps));
261 if (sps->used_by_curr_pic_lt_flag[lt_idx_sps])
262 slice_header->used_by_curr_pic_lt++;
264 TRUE_OR_RETURN(br->SkipBits(sps->log2_max_pic_order_cnt_lsb_minus4 +
266 bool used_by_curr_pic_lt_flag;
267 TRUE_OR_RETURN(br->ReadBool(&used_by_curr_pic_lt_flag));
268 if (used_by_curr_pic_lt_flag)
269 slice_header->used_by_curr_pic_lt++;
271 TRUE_OR_RETURN(br->ReadBool(&slice_header->long_term_pics_info[i]
272 .delta_poc_msb_present_flag));
273 if (slice_header->long_term_pics_info[i].delta_poc_msb_present_flag) {
274 TRUE_OR_RETURN(br->ReadUE(
275 &slice_header->long_term_pics_info[i].delta_poc_msb_cycle_lt));
280 if (sps->temporal_mvp_enabled_flag) {
282 br->ReadBool(&slice_header->slice_temporal_mvp_enabled_flag));
286 if (nalu.nuh_layer_id() != 0) {
287 NOTIMPLEMENTED() <<
"Multi-layer streams are not supported.";
288 return kUnsupportedStream;
291 if (sps->sample_adaptive_offset_enabled_flag) {
292 TRUE_OR_RETURN(br->ReadBool(&slice_header->slice_sao_luma_flag));
293 if (sps->GetChromaArrayType() != 0) {
294 TRUE_OR_RETURN(br->ReadBool(&slice_header->slice_sao_chroma_flag));
298 slice_header->num_ref_idx_l0_active_minus1 =
299 pps->num_ref_idx_l0_default_active_minus1;
300 slice_header->num_ref_idx_l1_active_minus1 =
301 pps->num_ref_idx_l1_default_active_minus1;
302 if (slice_header->slice_type == kPSlice ||
303 slice_header->slice_type == kBSlice) {
305 br->ReadBool(&slice_header->num_ref_idx_active_override_flag));
306 if (slice_header->num_ref_idx_active_override_flag) {
307 TRUE_OR_RETURN(br->ReadUE(&slice_header->num_ref_idx_l0_active_minus1));
308 if (slice_header->slice_type == kBSlice) {
310 br->ReadUE(&slice_header->num_ref_idx_l1_active_minus1));
314 const int num_pic_total_curr = GetNumPicTotalCurr(*slice_header, *sps);
315 if (pps->lists_modification_present_flag && num_pic_total_curr > 1) {
316 OK_OR_RETURN(SkipReferencePictureListModification(
317 *slice_header, *pps, num_pic_total_curr, br));
320 if (slice_header->slice_type == kBSlice) {
321 TRUE_OR_RETURN(br->ReadBool(&slice_header->mvd_l1_zero_flag));
323 if (pps->cabac_init_present_flag) {
324 TRUE_OR_RETURN(br->ReadBool(&slice_header->cabac_init_flag));
326 if (slice_header->slice_temporal_mvp_enabled_flag) {
327 if (slice_header->slice_type == kBSlice) {
328 TRUE_OR_RETURN(br->ReadBool(&slice_header->collocated_from_l0));
330 bool l0_greater_than_0 = slice_header->num_ref_idx_l0_active_minus1 > 0;
331 bool l1_greater_than_0 = slice_header->num_ref_idx_l1_active_minus1 > 0;
332 if (slice_header->collocated_from_l0 ? l0_greater_than_0
333 : l1_greater_than_0) {
334 TRUE_OR_RETURN(br->ReadUE(&slice_header->collocated_ref_idx));
338 if ((pps->weighted_pred_flag && slice_header->slice_type == kPSlice) ||
339 (pps->weighted_bipred_flag && slice_header->slice_type == kBSlice)) {
340 OK_OR_RETURN(SkipPredictionWeightTable(
341 slice_header->slice_type == kBSlice, *sps, *slice_header, br));
343 TRUE_OR_RETURN(br->ReadUE(&slice_header->five_minus_max_num_merge_cand));
346 TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_qp_delta));
347 if (pps->slice_chroma_qp_offsets_present_flag) {
348 TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_cb_qp_offset));
349 TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_cr_qp_offset));
352 if (pps->chroma_qp_offset_list_enabled_flag) {
354 br->ReadBool(&slice_header->cu_chroma_qp_offset_enabled_flag));
356 if (pps->deblocking_filter_override_enabled_flag) {
358 br->ReadBool(&slice_header->deblocking_filter_override_flag));
360 if (slice_header->deblocking_filter_override_flag) {
362 br->ReadBool(&slice_header->slice_deblocking_filter_disabled_flag));
363 if (!slice_header->slice_deblocking_filter_disabled_flag) {
364 TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_beta_offset_div2));
365 TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_tc_offset_div2));
368 if (pps->loop_filter_across_slices_enabled_flag &&
369 (slice_header->slice_sao_luma_flag ||
370 slice_header->slice_sao_chroma_flag ||
371 !slice_header->slice_deblocking_filter_disabled_flag)) {
372 TRUE_OR_RETURN(br->ReadBool(
373 &slice_header->slice_loop_filter_across_slices_enabled_flag));
377 if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) {
378 TRUE_OR_RETURN(br->ReadUE(&slice_header->num_entry_point_offsets));
379 if (slice_header->num_entry_point_offsets > 0) {
380 TRUE_OR_RETURN(br->ReadUE(&slice_header->offset_len_minus1));
381 slice_header->entry_point_offset_minus1.resize(
382 slice_header->num_entry_point_offsets);
383 for (
int i = 0; i < slice_header->num_entry_point_offsets; i++) {
385 br->ReadBits(slice_header->offset_len_minus1 + 1,
386 &slice_header->entry_point_offset_minus1[i]));
391 if (pps->slice_segment_header_extension_present_flag) {
392 int extension_length;
393 TRUE_OR_RETURN(br->ReadUE(&extension_length));
394 TRUE_OR_RETURN(br->SkipBits(extension_length * 8));
397 size_t epb = br->NumEmulationPreventionBytesRead();
398 slice_header->header_bit_size =
405 DCHECK_EQ(Nalu::H265_PPS, nalu.
type());
413 scoped_ptr<H265Pps> pps(
new H265Pps);
415 TRUE_OR_RETURN(br->ReadUE(&pps->pic_parameter_set_id));
416 TRUE_OR_RETURN(br->ReadUE(&pps->seq_parameter_set_id));
418 TRUE_OR_RETURN(br->ReadBool(&pps->dependent_slice_segments_enabled_flag));
419 TRUE_OR_RETURN(br->ReadBool(&pps->output_flag_present_flag));
420 TRUE_OR_RETURN(br->ReadBits(3, &pps->num_extra_slice_header_bits));
421 TRUE_OR_RETURN(br->ReadBool(&pps->sign_data_hiding_enabled_flag));
422 TRUE_OR_RETURN(br->ReadBool(&pps->cabac_init_present_flag));
424 TRUE_OR_RETURN(br->ReadUE(&pps->num_ref_idx_l0_default_active_minus1));
425 TRUE_OR_RETURN(br->ReadUE(&pps->num_ref_idx_l1_default_active_minus1));
426 TRUE_OR_RETURN(br->ReadSE(&pps->init_qp_minus26));
427 TRUE_OR_RETURN(br->ReadBool(&pps->constrained_intra_pred_flag));
428 TRUE_OR_RETURN(br->ReadBool(&pps->transform_skip_enabled_flag));
430 TRUE_OR_RETURN(br->ReadBool(&pps->cu_qp_delta_enabled_flag));
431 if (pps->cu_qp_delta_enabled_flag)
432 TRUE_OR_RETURN(br->ReadUE(&pps->diff_cu_qp_delta_depth));
433 TRUE_OR_RETURN(br->ReadSE(&pps->cb_qp_offset));
434 TRUE_OR_RETURN(br->ReadSE(&pps->cr_qp_offset));
436 TRUE_OR_RETURN(br->ReadBool(&pps->slice_chroma_qp_offsets_present_flag));
437 TRUE_OR_RETURN(br->ReadBool(&pps->weighted_pred_flag));
438 TRUE_OR_RETURN(br->ReadBool(&pps->weighted_bipred_flag));
439 TRUE_OR_RETURN(br->ReadBool(&pps->transquant_bypass_enabled_flag));
440 TRUE_OR_RETURN(br->ReadBool(&pps->tiles_enabled_flag));
441 TRUE_OR_RETURN(br->ReadBool(&pps->entropy_coding_sync_enabled_flag));
443 if (pps->tiles_enabled_flag) {
444 TRUE_OR_RETURN(br->ReadUE(&pps->num_tile_columns_minus1));
445 TRUE_OR_RETURN(br->ReadUE(&pps->num_tile_rows_minus1));
446 TRUE_OR_RETURN(br->ReadBool(&pps->uniform_spacing_flag));
447 if (!pps->uniform_spacing_flag) {
448 pps->column_width_minus1.resize(pps->num_tile_columns_minus1);
449 for (
int i = 0; i < pps->num_tile_columns_minus1; i++) {
450 TRUE_OR_RETURN(br->ReadUE(&pps->column_width_minus1[i]));
452 pps->row_height_minus1.resize(pps->num_tile_rows_minus1);
453 for (
int i = 0; i < pps->num_tile_rows_minus1; i++) {
454 TRUE_OR_RETURN(br->ReadUE(&pps->row_height_minus1[i]));
457 TRUE_OR_RETURN(br->ReadBool(&pps->loop_filter_across_tiles_enabled_flag));
460 TRUE_OR_RETURN(br->ReadBool(&pps->loop_filter_across_slices_enabled_flag));
461 TRUE_OR_RETURN(br->ReadBool(&pps->deblocking_filter_control_present_flag));
462 if (pps->deblocking_filter_control_present_flag) {
463 TRUE_OR_RETURN(br->ReadBool(&pps->deblocking_filter_override_enabled_flag));
464 TRUE_OR_RETURN(br->ReadBool(&pps->deblocking_filter_disabled_flag));
465 if (!pps->deblocking_filter_disabled_flag) {
466 TRUE_OR_RETURN(br->ReadSE(&pps->beta_offset_div2));
467 TRUE_OR_RETURN(br->ReadSE(&pps->tc_offset_div2));
471 TRUE_OR_RETURN(br->ReadBool(&pps->scaling_list_data_present_flag));
472 if (pps->scaling_list_data_present_flag) {
473 OK_OR_RETURN(SkipScalingListData(br));
476 TRUE_OR_RETURN(br->ReadBool(&pps->lists_modification_present_flag));
477 TRUE_OR_RETURN(br->ReadUE(&pps->log2_parallel_merge_level_minus2));
480 br->ReadBool(&pps->slice_segment_header_extension_present_flag));
482 bool pps_extension_present_flag;
483 bool pps_range_extension_flag =
false;
484 TRUE_OR_RETURN(br->ReadBool(&pps_extension_present_flag));
485 if (pps_extension_present_flag) {
486 TRUE_OR_RETURN(br->ReadBool(&pps_range_extension_flag));
488 TRUE_OR_RETURN(br->SkipBits(1 + 1 + 5));
491 if (pps_range_extension_flag) {
492 if (pps->transform_skip_enabled_flag) {
495 TRUE_OR_RETURN(br->ReadUE(&ignored));
498 TRUE_OR_RETURN(br->SkipBits(1));
499 TRUE_OR_RETURN(br->ReadBool(&pps->chroma_qp_offset_list_enabled_flag));
506 *pps_id = pps->pic_parameter_set_id;
507 delete active_ppses_[*pps_id];
508 active_ppses_[*pps_id] = pps.release();
514 DCHECK_EQ(Nalu::H265_SPS, nalu.
type());
523 scoped_ptr<H265Sps> sps(
new H265Sps);
525 TRUE_OR_RETURN(br->ReadBits(4, &sps->video_parameter_set_id));
526 TRUE_OR_RETURN(br->ReadBits(3, &sps->max_sub_layers_minus1));
527 TRUE_OR_RETURN(br->ReadBool(&sps->temporal_id_nesting_flag));
529 OK_OR_RETURN(SkipProfileTierLevel(
true, sps->max_sub_layers_minus1, br));
531 TRUE_OR_RETURN(br->ReadUE(&sps->seq_parameter_set_id));
532 TRUE_OR_RETURN(br->ReadUE(&sps->chroma_format_idc));
533 if (sps->chroma_format_idc == 3) {
534 TRUE_OR_RETURN(br->ReadBool(&sps->separate_colour_plane_flag));
536 TRUE_OR_RETURN(br->ReadUE(&sps->pic_width_in_luma_samples));
537 TRUE_OR_RETURN(br->ReadUE(&sps->pic_height_in_luma_samples));
539 TRUE_OR_RETURN(br->ReadBool(&sps->conformance_window_flag));
540 if (sps->conformance_window_flag) {
541 TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_left_offset));
542 TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_right_offset));
543 TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_top_offset));
544 TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_bottom_offset));
547 TRUE_OR_RETURN(br->ReadUE(&sps->bit_depth_luma_minus8));
548 TRUE_OR_RETURN(br->ReadUE(&sps->bit_depth_chroma_minus8));
549 TRUE_OR_RETURN(br->ReadUE(&sps->log2_max_pic_order_cnt_lsb_minus4));
551 TRUE_OR_RETURN(br->ReadBool(&sps->sub_layer_ordering_info_present_flag));
552 int start = sps->sub_layer_ordering_info_present_flag
554 : sps->max_sub_layers_minus1;
555 for (
int i = start; i <= sps->max_sub_layers_minus1; i++) {
556 TRUE_OR_RETURN(br->ReadUE(&sps->max_dec_pic_buffering_minus1[i]));
557 TRUE_OR_RETURN(br->ReadUE(&sps->max_num_reorder_pics[i]));
558 TRUE_OR_RETURN(br->ReadUE(&sps->max_latency_increase_plus1[i]));
561 TRUE_OR_RETURN(br->ReadUE(&sps->log2_min_luma_coding_block_size_minus3));
562 TRUE_OR_RETURN(br->ReadUE(&sps->log2_diff_max_min_luma_coding_block_size));
563 TRUE_OR_RETURN(br->ReadUE(&sps->log2_min_luma_transform_block_size_minus2));
564 TRUE_OR_RETURN(br->ReadUE(&sps->log2_diff_max_min_luma_transform_block_size));
565 TRUE_OR_RETURN(br->ReadUE(&sps->max_transform_hierarchy_depth_inter));
566 TRUE_OR_RETURN(br->ReadUE(&sps->max_transform_hierarchy_depth_intra));
568 TRUE_OR_RETURN(br->ReadBool(&sps->scaling_list_enabled_flag));
569 if (sps->scaling_list_enabled_flag) {
570 TRUE_OR_RETURN(br->ReadBool(&sps->scaling_list_data_present_flag));
571 if (sps->scaling_list_data_present_flag) {
572 OK_OR_RETURN(SkipScalingListData(br));
576 TRUE_OR_RETURN(br->ReadBool(&sps->amp_enabled_flag));
577 TRUE_OR_RETURN(br->ReadBool(&sps->sample_adaptive_offset_enabled_flag));
578 TRUE_OR_RETURN(br->ReadBool(&sps->pcm_enabled_flag));
579 if (sps->pcm_enabled_flag) {
580 TRUE_OR_RETURN(br->ReadBits(4, &sps->pcm_sample_bit_depth_luma_minus1));
581 TRUE_OR_RETURN(br->ReadBits(4, &sps->pcm_sample_bit_depth_chroma_minus1));
583 br->ReadUE(&sps->log2_min_pcm_luma_coding_block_size_minus3));
585 br->ReadUE(&sps->log2_diff_max_min_pcm_luma_coding_block_size));
586 TRUE_OR_RETURN(br->ReadBool(&sps->pcm_loop_filter_disabled_flag));
589 TRUE_OR_RETURN(br->ReadUE(&sps->num_short_term_ref_pic_sets));
590 sps->st_ref_pic_sets.resize(sps->num_short_term_ref_pic_sets);
591 for (
int i = 0; i < sps->num_short_term_ref_pic_sets; i++) {
592 OK_OR_RETURN(ParseReferencePictureSet(sps->num_short_term_ref_pic_sets, i,
593 sps->st_ref_pic_sets, br,
594 &sps->st_ref_pic_sets[i]));
597 TRUE_OR_RETURN(br->ReadBool(&sps->long_term_ref_pic_present_flag));
598 if (sps->long_term_ref_pic_present_flag) {
599 TRUE_OR_RETURN(br->ReadUE(&sps->num_long_term_ref_pics));
600 sps->lt_ref_pic_poc_lsb.resize(sps->num_long_term_ref_pics);
601 sps->used_by_curr_pic_lt_flag.resize(sps->num_long_term_ref_pics);
602 for (
int i = 0; i < sps->num_long_term_ref_pics; i++) {
603 TRUE_OR_RETURN(br->ReadBits(sps->log2_max_pic_order_cnt_lsb_minus4 + 4,
604 &sps->lt_ref_pic_poc_lsb[i]));
606 TRUE_OR_RETURN(br->ReadBool(&temp));
607 sps->used_by_curr_pic_lt_flag[i] = temp;
611 TRUE_OR_RETURN(br->ReadBool(&sps->temporal_mvp_enabled_flag));
612 TRUE_OR_RETURN(br->ReadBool(&sps->strong_intra_smoothing_enabled_flag));
614 TRUE_OR_RETURN(br->ReadBool(&sps->vui_parameters_present));
615 if (sps->vui_parameters_present) {
616 OK_OR_RETURN(ParseVuiParameters(sps->max_sub_layers_minus1, br,
617 &sps->vui_parameters));
623 *sps_id = sps->seq_parameter_set_id;
624 delete active_spses_[*sps_id];
625 active_spses_[*sps_id] = sps.release();
631 return active_ppses_[pps_id];
635 return active_spses_[sps_id];
638 H265Parser::Result H265Parser::ParseVuiParameters(
int max_num_sub_layers_minus1,
644 TRUE_OR_RETURN(br->ReadBool(&vui->aspect_ratio_info_present_flag));
645 if (vui->aspect_ratio_info_present_flag) {
646 TRUE_OR_RETURN(br->ReadBits(8, &vui->aspect_ratio_idc));
647 if (vui->aspect_ratio_idc == H265VuiParameters::kExtendedSar) {
648 TRUE_OR_RETURN(br->ReadBits(16, &vui->sar_width));
649 TRUE_OR_RETURN(br->ReadBits(16, &vui->sar_height));
653 bool overscan_info_present_flag;
654 TRUE_OR_RETURN(br->ReadBool(&overscan_info_present_flag));
655 if (overscan_info_present_flag) {
656 TRUE_OR_RETURN(br->SkipBits(1));
659 bool video_signal_type_present_flag;
660 TRUE_OR_RETURN(br->ReadBool(&video_signal_type_present_flag));
661 if (video_signal_type_present_flag) {
662 TRUE_OR_RETURN(br->SkipBits(3));
663 TRUE_OR_RETURN(br->SkipBits(1));
665 bool colour_description_present_flag;
666 TRUE_OR_RETURN(br->ReadBool(&colour_description_present_flag));
667 if (colour_description_present_flag) {
669 TRUE_OR_RETURN(br->SkipBits(8 + 8 + 8));
673 bool chroma_loc_info_present_flag;
674 TRUE_OR_RETURN(br->ReadBool(&chroma_loc_info_present_flag));
675 if (chroma_loc_info_present_flag) {
677 TRUE_OR_RETURN(br->ReadUE(&ignored));
678 TRUE_OR_RETURN(br->ReadUE(&ignored));
683 TRUE_OR_RETURN(br->SkipBits(3));
685 bool default_display_window_flag;
686 TRUE_OR_RETURN(br->ReadBool(&default_display_window_flag));
687 if (default_display_window_flag) {
688 TRUE_OR_RETURN(br->ReadUE(&ignored));
689 TRUE_OR_RETURN(br->ReadUE(&ignored));
690 TRUE_OR_RETURN(br->ReadUE(&ignored));
691 TRUE_OR_RETURN(br->ReadUE(&ignored));
694 bool vui_timing_info_present_flag;
695 TRUE_OR_RETURN(br->ReadBool(&vui_timing_info_present_flag));
696 if (vui_timing_info_present_flag) {
698 TRUE_OR_RETURN(br->SkipBits(32 + 32));
700 bool vui_poc_proportional_to_timing_flag;
701 TRUE_OR_RETURN(br->ReadBool(&vui_poc_proportional_to_timing_flag));
702 if (vui_poc_proportional_to_timing_flag) {
704 TRUE_OR_RETURN(br->ReadUE(&ignored));
707 bool vui_hdr_parameters_present_flag;
708 TRUE_OR_RETURN(br->ReadBool(&vui_hdr_parameters_present_flag));
709 if (vui_hdr_parameters_present_flag) {
710 OK_OR_RETURN(SkipHrdParameters(max_num_sub_layers_minus1, br));
714 TRUE_OR_RETURN(br->ReadBool(&vui->bitstream_restriction_flag));
715 if (vui->bitstream_restriction_flag) {
718 TRUE_OR_RETURN(br->SkipBits(3));
720 TRUE_OR_RETURN(br->ReadUE(&vui->min_spatial_segmentation_idc));
721 TRUE_OR_RETURN(br->ReadUE(&ignored));
722 TRUE_OR_RETURN(br->ReadUE(&ignored));
723 TRUE_OR_RETURN(br->ReadUE(&ignored));
724 TRUE_OR_RETURN(br->ReadUE(&ignored));
730 H265Parser::Result H265Parser::ParseReferencePictureSet(
731 int num_short_term_ref_pic_sets,
733 const std::vector<H265ReferencePictureSet>& ref_pic_sets,
735 H265ReferencePictureSet* out_ref_pic_set) {
740 bool inter_ref_pic_set_prediction =
false;
741 if (st_rps_idx != 0) {
742 TRUE_OR_RETURN(br->ReadBool(&inter_ref_pic_set_prediction));
745 if (inter_ref_pic_set_prediction) {
747 if (st_rps_idx == num_short_term_ref_pic_sets) {
748 TRUE_OR_RETURN(br->ReadUE(&delta_idx));
750 TRUE_OR_RETURN(delta_idx <= st_rps_idx);
753 int ref_rps_idx = st_rps_idx - delta_idx;
754 DCHECK_LE(0, ref_rps_idx);
755 DCHECK_LT(ref_rps_idx, st_rps_idx);
758 int abs_delta_rps_minus1;
759 TRUE_OR_RETURN(br->ReadBool(&delta_rps_sign));
760 TRUE_OR_RETURN(br->ReadUE(&abs_delta_rps_minus1));
762 delta_rps_sign ? -(abs_delta_rps_minus1 + 1) : abs_delta_rps_minus1 + 1;
764 int ref_num_delta_pocs = ref_pic_sets[ref_rps_idx].num_delta_pocs;
765 std::vector<bool> used_by_curr_pic(ref_num_delta_pocs + 1);
766 std::vector<bool> use_delta(ref_num_delta_pocs + 1);
767 for (
int j = 0; j <= ref_num_delta_pocs; j++) {
769 TRUE_OR_RETURN(br->ReadBool(&temp));
770 used_by_curr_pic[j] = temp;
772 if (!used_by_curr_pic[j]) {
773 TRUE_OR_RETURN(br->ReadBool(&temp));
780 int ref_num_positive_pics = ref_pic_sets[ref_rps_idx].num_positive_pics;
781 int ref_num_negative_pics = ref_pic_sets[ref_rps_idx].num_negative_pics;
787 for (
int j = ref_num_positive_pics - 1; j >= 0; j--) {
788 int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s1[j] + delta_rps;
789 if (d_poc < 0 && use_delta[ref_num_negative_pics + j]) {
790 out_ref_pic_set->delta_poc_s0[i] = d_poc;
791 out_ref_pic_set->used_by_curr_pic_s0[i] =
792 used_by_curr_pic[ref_num_negative_pics + j];
796 if (delta_rps < 0 && use_delta[ref_num_delta_pocs]) {
797 out_ref_pic_set->delta_poc_s0[i] = delta_rps;
798 out_ref_pic_set->used_by_curr_pic_s0[i] =
799 used_by_curr_pic[ref_num_delta_pocs];
802 for (
int j = 0; j < ref_num_negative_pics; j++) {
803 int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s0[j] + delta_rps;
804 if (d_poc < 0 && use_delta[j]) {
805 out_ref_pic_set->delta_poc_s0[i] = d_poc;
806 out_ref_pic_set->used_by_curr_pic_s0[i] = used_by_curr_pic[j];
810 out_ref_pic_set->num_negative_pics = i;
816 for (
int j = ref_num_negative_pics - 1; j >= 0; j--) {
817 int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s0[j] + delta_rps;
818 if (d_poc > 0 && use_delta[j]) {
819 out_ref_pic_set->delta_poc_s1[i] = d_poc;
820 out_ref_pic_set->used_by_curr_pic_s1[i] = used_by_curr_pic[j];
824 if (delta_rps > 0 && use_delta[ref_num_delta_pocs]) {
825 out_ref_pic_set->delta_poc_s1[i] = delta_rps;
826 out_ref_pic_set->used_by_curr_pic_s1[i] =
827 used_by_curr_pic[ref_num_delta_pocs];
830 for (
int j = 0; j < ref_num_positive_pics; j++) {
831 int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s1[j] + delta_rps;
832 if (d_poc > 0 && use_delta[ref_num_negative_pics + j]) {
833 out_ref_pic_set->delta_poc_s1[i] = d_poc;
834 out_ref_pic_set->used_by_curr_pic_s1[i] =
835 used_by_curr_pic[ref_num_negative_pics + j];
839 out_ref_pic_set->num_positive_pics = i;
842 TRUE_OR_RETURN(br->ReadUE(&out_ref_pic_set->num_negative_pics));
843 TRUE_OR_RETURN(br->ReadUE(&out_ref_pic_set->num_positive_pics));
846 for (
int i = 0; i < out_ref_pic_set->num_negative_pics; i++) {
847 int delta_poc_s0_minus1;
848 TRUE_OR_RETURN(br->ReadUE(&delta_poc_s0_minus1));
849 out_ref_pic_set->delta_poc_s0[i] = prev_poc - (delta_poc_s0_minus1 + 1);
850 prev_poc = out_ref_pic_set->delta_poc_s0[i];
852 TRUE_OR_RETURN(br->ReadBool(&out_ref_pic_set->used_by_curr_pic_s0[i]));
856 for (
int i = 0; i < out_ref_pic_set->num_positive_pics; i++) {
857 int delta_poc_s1_minus1;
858 TRUE_OR_RETURN(br->ReadUE(&delta_poc_s1_minus1));
859 out_ref_pic_set->delta_poc_s1[i] = prev_poc + delta_poc_s1_minus1 + 1;
860 prev_poc = out_ref_pic_set->delta_poc_s1[i];
862 TRUE_OR_RETURN(br->ReadBool(&out_ref_pic_set->used_by_curr_pic_s1[i]));
866 out_ref_pic_set->num_delta_pocs =
867 out_ref_pic_set->num_positive_pics + out_ref_pic_set->num_negative_pics;
871 H265Parser::Result H265Parser::SkipReferencePictureListModification(
872 const H265SliceHeader& slice_header,
874 int num_pic_total_curr,
878 bool ref_pic_list_modification_flag_l0;
879 TRUE_OR_RETURN(br->ReadBool(&ref_pic_list_modification_flag_l0));
880 if (ref_pic_list_modification_flag_l0) {
881 for (
int i = 0; i <= pps.num_ref_idx_l0_default_active_minus1; i++) {
882 TRUE_OR_RETURN(br->SkipBits(ceil(log2(num_pic_total_curr))));
886 if (slice_header.slice_type == kBSlice) {
887 bool ref_pic_list_modification_flag_l1;
888 TRUE_OR_RETURN(br->ReadBool(&ref_pic_list_modification_flag_l1));
889 if (ref_pic_list_modification_flag_l1) {
890 for (
int i = 0; i <= pps.num_ref_idx_l1_default_active_minus1; i++) {
891 TRUE_OR_RETURN(br->SkipBits(ceil(log2(num_pic_total_curr))));
899 H265Parser::Result H265Parser::SkipPredictionWeightTablePart(
900 int num_ref_idx_minus1,
901 int chroma_array_type,
905 std::vector<bool> luma_weight_flag(num_ref_idx_minus1 + 1);
906 std::vector<bool> chroma_weight_flag(num_ref_idx_minus1 + 1);
908 for (
int i = 0; i <= num_ref_idx_minus1; i++) {
910 TRUE_OR_RETURN(br->ReadBool(&temp));
911 luma_weight_flag[i] = temp;
913 if (chroma_array_type != 0) {
914 for (
int i = 0; i <= num_ref_idx_minus1; i++) {
916 TRUE_OR_RETURN(br->ReadBool(&temp));
917 chroma_weight_flag[i] = temp;
920 for (
int i = 0; i <= num_ref_idx_minus1; i++) {
921 if (luma_weight_flag[i]) {
922 TRUE_OR_RETURN(br->ReadSE(&ignored));
923 TRUE_OR_RETURN(br->ReadSE(&ignored));
925 if (chroma_weight_flag[i]) {
926 for (
int j = 0; j < 2; j++) {
927 TRUE_OR_RETURN(br->ReadSE(&ignored));
928 TRUE_OR_RETURN(br->ReadSE(&ignored));
936 H265Parser::Result H265Parser::SkipPredictionWeightTable(
939 const H265SliceHeader& slice_header,
943 int chroma_array_type = sps.GetChromaArrayType();
945 TRUE_OR_RETURN(br->ReadUE(&ignored));
946 if (chroma_array_type != 0) {
947 TRUE_OR_RETURN(br->ReadSE(&ignored));
949 OK_OR_RETURN(SkipPredictionWeightTablePart(
950 slice_header.num_ref_idx_l0_active_minus1, chroma_array_type, br));
952 OK_OR_RETURN(SkipPredictionWeightTablePart(
953 slice_header.num_ref_idx_l1_active_minus1, chroma_array_type, br));
959 H265Parser::Result H265Parser::SkipProfileTierLevel(
960 bool profile_present,
961 int max_num_sub_layers_minus1,
965 if (profile_present) {
973 TRUE_OR_RETURN(br->SkipBits(2 + 1 + 5 + 32 + 4 + 44));
976 TRUE_OR_RETURN(br->SkipBits(8));
978 std::vector<bool> sub_layer_profile_present(max_num_sub_layers_minus1);
979 std::vector<bool> sub_layer_level_present(max_num_sub_layers_minus1);
980 for (
int i = 0; i < max_num_sub_layers_minus1; i++) {
982 TRUE_OR_RETURN(br->ReadBool(&profile));
983 TRUE_OR_RETURN(br->ReadBool(&level));
984 sub_layer_profile_present[i] = profile;
985 sub_layer_level_present[i] = level;
988 if (max_num_sub_layers_minus1 > 0) {
989 for (
int i = max_num_sub_layers_minus1; i < 8; i++)
990 TRUE_OR_RETURN(br->SkipBits(2));
993 for (
int i = 0; i < max_num_sub_layers_minus1; i++) {
994 if (sub_layer_profile_present[i]) {
999 TRUE_OR_RETURN(br->SkipBits(2 + 1 + 5 + 32 + 4 + 43 + 1));
1001 if (sub_layer_level_present[i]) {
1002 TRUE_OR_RETURN(br->SkipBits(8));
1009 H265Parser::Result H265Parser::SkipScalingListData(H26xBitReader* br) {
1012 for (
int size_id = 0; size_id < 4; size_id++) {
1013 for (
int matrix_id = 0; matrix_id < 6;
1014 matrix_id += ((size_id == 3) ? 3 : 1)) {
1015 bool scaling_list_pred_mode;
1016 TRUE_OR_RETURN(br->ReadBool(&scaling_list_pred_mode));
1017 if (!scaling_list_pred_mode) {
1019 TRUE_OR_RETURN(br->ReadUE(&ignored));
1021 int coefNum = std::min(64, (1 << (4 + (size_id << 1))));
1023 TRUE_OR_RETURN(br->ReadSE(&ignored));
1026 for (
int i = 0; i < coefNum; i++) {
1027 TRUE_OR_RETURN(br->ReadSE(&ignored));
1036 H265Parser::Result H265Parser::SkipHrdParameters(
int max_num_sub_layers_minus1,
1037 H26xBitReader* br) {
1039 const bool common_inf_present_flag =
true;
1042 bool nal_hdr_parameters_present_flag;
1043 bool vcl_hdr_parameters_present_flag;
1044 bool sub_pic_hdr_params_present_flag =
false;
1045 if (common_inf_present_flag) {
1046 TRUE_OR_RETURN(br->ReadBool(&nal_hdr_parameters_present_flag));
1047 TRUE_OR_RETURN(br->ReadBool(&vcl_hdr_parameters_present_flag));
1048 if (nal_hdr_parameters_present_flag || vcl_hdr_parameters_present_flag) {
1049 TRUE_OR_RETURN(br->ReadBool(&sub_pic_hdr_params_present_flag));
1050 if (sub_pic_hdr_params_present_flag) {
1054 TRUE_OR_RETURN(br->SkipBits(8 + 5 + 1 + 5));
1058 TRUE_OR_RETURN(br->SkipBits(4 + 4));
1059 if (sub_pic_hdr_params_present_flag)
1060 TRUE_OR_RETURN(br->SkipBits(4));
1064 TRUE_OR_RETURN(br->SkipBits(5 + 5 + 5));
1068 for (
int i = 0; i <= max_num_sub_layers_minus1; i++) {
1069 bool fixed_pic_rate_general_flag;
1070 bool fixed_pic_rate_within_cvs_flag =
true;
1071 bool low_delay_hdr_flag =
false;
1072 int cpb_cnt_minus1 = 0;
1073 TRUE_OR_RETURN(br->ReadBool(&fixed_pic_rate_general_flag));
1074 if (!fixed_pic_rate_general_flag)
1075 TRUE_OR_RETURN(br->ReadBool(&fixed_pic_rate_within_cvs_flag));
1076 if (fixed_pic_rate_within_cvs_flag)
1077 TRUE_OR_RETURN(br->ReadUE(&ignored));
1079 TRUE_OR_RETURN(br->ReadBool(&low_delay_hdr_flag));
1080 if (!low_delay_hdr_flag)
1081 TRUE_OR_RETURN(br->ReadUE(&cpb_cnt_minus1));
1083 if (nal_hdr_parameters_present_flag) {
1084 OK_OR_RETURN(SkipSubLayerHrdParameters(
1085 cpb_cnt_minus1, sub_pic_hdr_params_present_flag, br));
1087 if (vcl_hdr_parameters_present_flag) {
1088 OK_OR_RETURN(SkipSubLayerHrdParameters(
1089 cpb_cnt_minus1, sub_pic_hdr_params_present_flag, br));
1096 H265Parser::Result H265Parser::SkipSubLayerHrdParameters(
1098 bool sub_pic_hdr_params_present_flag,
1099 H26xBitReader* br) {
1101 for (
int i = 0; i <= cpb_cnt_minus1; i++) {
1102 TRUE_OR_RETURN(br->ReadUE(&ignored));
1103 TRUE_OR_RETURN(br->ReadUE(&ignored));
1104 if (sub_pic_hdr_params_present_flag) {
1105 TRUE_OR_RETURN(br->ReadUE(&ignored));
1106 TRUE_OR_RETURN(br->ReadUE(&ignored));
1109 TRUE_OR_RETURN(br->SkipBits(1));