7 #include "packager/media/filters/h265_parser.h"
12 #include "packager/base/logging.h"
13 #include "packager/base/stl_util.h"
14 #include "packager/media/filters/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); \
31 namespace edash_packager {
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;
57 H265Pps::~H265Pps() {}
60 H265Sps::~H265Sps() {}
62 int H265Sps::GetPicSizeInCtbsY()
const {
63 int min_cb_log2_size_y = log2_min_luma_coding_block_size_minus3 + 3;
65 min_cb_log2_size_y + log2_diff_max_min_luma_coding_block_size;
66 int ctb_size_y = 1 << ctb_log2_size_y;
69 int pic_width_in_ctbs_y = (pic_width_in_luma_samples - 1) / ctb_size_y + 1;
70 int pic_height_in_ctbs_y = (pic_height_in_luma_samples - 1) / ctb_size_y + 1;
71 return pic_width_in_ctbs_y * pic_height_in_ctbs_y;
74 int H265Sps::GetChromaArrayType()
const {
75 if (!separate_colour_plane_flag)
76 return chroma_format_idc;
81 H265ReferencePictureListModifications::H265ReferencePictureListModifications() {
83 H265ReferencePictureListModifications::
84 ~H265ReferencePictureListModifications() {}
86 H265SliceHeader::H265SliceHeader() {}
87 H265SliceHeader::~H265SliceHeader() {}
89 H265Parser::H265Parser() {}
90 H265Parser::~H265Parser() {
91 STLDeleteValues(&active_spses_);
92 STLDeleteValues(&active_ppses_);
97 DCHECK(nalu.is_video_slice());
102 reader.Initialize(nalu.data() + nalu.header_size(), nalu.payload_size());
105 TRUE_OR_RETURN(br->ReadBool(&slice_header->first_slice_segment_in_pic_flag));
106 if (nalu.type() >= Nalu::H265_BLA_W_LP &&
107 nalu.type() <= Nalu::H265_RSV_IRAP_VCL23) {
108 TRUE_OR_RETURN(br->ReadBool(&slice_header->no_output_of_prior_pics_flag));
111 TRUE_OR_RETURN(br->ReadUE(&slice_header->pic_parameter_set_id));
112 const H265Pps* pps =
GetPps(slice_header->pic_parameter_set_id);
118 if (!slice_header->first_slice_segment_in_pic_flag) {
119 if (pps->dependent_slice_segments_enabled_flag) {
120 TRUE_OR_RETURN(br->ReadBool(&slice_header->dependent_slice_segment_flag));
122 const int bit_length = ceil(log2(sps->GetPicSizeInCtbsY()));
123 TRUE_OR_RETURN(br->ReadBits(bit_length, &slice_header->segment_address));
126 if (!slice_header->dependent_slice_segment_flag) {
127 TRUE_OR_RETURN(br->SkipBits(pps->num_extra_slice_header_bits));
128 TRUE_OR_RETURN(br->ReadUE(&slice_header->slice_type));
129 if (pps->output_flag_present_flag) {
130 TRUE_OR_RETURN(br->ReadBool(&slice_header->pic_output_flag));
132 if (sps->separate_colour_plane_flag) {
133 TRUE_OR_RETURN(br->ReadBits(2, &slice_header->colour_plane_id));
136 if (nalu.type() != Nalu::H265_IDR_W_RADL &&
137 nalu.type() != Nalu::H265_IDR_N_LP) {
138 TRUE_OR_RETURN(br->ReadBits(sps->log2_max_pic_order_cnt_lsb_minus4 + 4,
139 &slice_header->slice_pic_order_cnt_lsb));
142 br->ReadBool(&slice_header->short_term_ref_pic_set_sps_flag));
143 if (!slice_header->short_term_ref_pic_set_sps_flag) {
144 OK_OR_RETURN(ParseReferencePictureSet(
145 sps->num_short_term_ref_pic_sets, sps->num_short_term_ref_pic_sets,
146 sps->st_ref_pic_sets, br, &slice_header->st_ref_pic_set));
147 }
else if (sps->num_short_term_ref_pic_sets > 1) {
149 br->ReadBits(ceil(log2(sps->num_short_term_ref_pic_sets)),
150 &slice_header->short_term_ref_pic_set_idx));
153 if (sps->long_term_ref_pic_present_flag) {
154 if (sps->num_long_term_ref_pics > 0) {
155 TRUE_OR_RETURN(br->ReadUE(&slice_header->num_long_term_sps));
157 TRUE_OR_RETURN(br->ReadUE(&slice_header->num_long_term_pics));
159 const int pic_count =
160 slice_header->num_long_term_sps + slice_header->num_long_term_pics;
161 slice_header->long_term_pics_info.resize(pic_count);
162 for (
int i = 0; i < pic_count; i++) {
163 if (i < slice_header->num_long_term_sps) {
165 if (sps->num_long_term_ref_pics > 1) {
166 TRUE_OR_RETURN(br->ReadBits(
167 ceil(log2(sps->num_long_term_ref_pics)), <_idx_sps));
169 if (sps->used_by_curr_pic_lt_flag[lt_idx_sps])
170 slice_header->used_by_curr_pic_lt++;
172 TRUE_OR_RETURN(br->SkipBits(sps->log2_max_pic_order_cnt_lsb_minus4 +
174 bool used_by_curr_pic_lt_flag;
175 TRUE_OR_RETURN(br->ReadBool(&used_by_curr_pic_lt_flag));
176 if (used_by_curr_pic_lt_flag)
177 slice_header->used_by_curr_pic_lt++;
179 TRUE_OR_RETURN(br->ReadBool(&slice_header->long_term_pics_info[i]
180 .delta_poc_msb_present_flag));
181 if (slice_header->long_term_pics_info[i].delta_poc_msb_present_flag) {
182 TRUE_OR_RETURN(br->ReadUE(
183 &slice_header->long_term_pics_info[i].delta_poc_msb_cycle_lt));
188 if (sps->temporal_mvp_enabled_flag) {
190 br->ReadBool(&slice_header->slice_temporal_mvp_enabled_flag));
194 if (nalu.nuh_layer_id() != 0) {
195 NOTIMPLEMENTED() <<
"Multi-layer streams are not supported.";
196 return kUnsupportedStream;
199 if (sps->sample_adaptive_offset_enabled_flag) {
200 TRUE_OR_RETURN(br->ReadBool(&slice_header->slice_sao_luma_flag));
201 if (sps->GetChromaArrayType() != 0) {
202 TRUE_OR_RETURN(br->ReadBool(&slice_header->slice_sao_chroma_flag));
206 slice_header->num_ref_idx_l0_active_minus1 =
207 pps->num_ref_idx_l0_default_active_minus1;
208 slice_header->num_ref_idx_l1_active_minus1 =
209 pps->num_ref_idx_l1_default_active_minus1;
210 if (slice_header->slice_type == kPSlice ||
211 slice_header->slice_type == kBSlice) {
213 br->ReadBool(&slice_header->num_ref_idx_active_override_flag));
214 if (slice_header->num_ref_idx_active_override_flag) {
215 TRUE_OR_RETURN(br->ReadUE(&slice_header->num_ref_idx_l0_active_minus1));
216 if (slice_header->slice_type == kBSlice) {
218 br->ReadUE(&slice_header->num_ref_idx_l1_active_minus1));
222 const int num_pic_total_curr = GetNumPicTotalCurr(*slice_header, *sps);
223 if (pps->lists_modification_present_flag && num_pic_total_curr > 1) {
224 OK_OR_RETURN(SkipReferencePictureListModification(
225 *slice_header, *pps, num_pic_total_curr, br));
228 if (slice_header->slice_type == kBSlice) {
229 TRUE_OR_RETURN(br->ReadBool(&slice_header->mvd_l1_zero_flag));
231 if (pps->cabac_init_present_flag) {
232 TRUE_OR_RETURN(br->ReadBool(&slice_header->cabac_init_flag));
234 if (slice_header->slice_temporal_mvp_enabled_flag) {
235 if (slice_header->slice_type == kBSlice) {
236 TRUE_OR_RETURN(br->ReadBool(&slice_header->collocated_from_l0));
238 bool l0_greater_than_0 = slice_header->num_ref_idx_l0_active_minus1 > 0;
239 bool l1_greater_than_0 = slice_header->num_ref_idx_l1_active_minus1 > 0;
240 if (slice_header->collocated_from_l0 ? l0_greater_than_0
241 : l1_greater_than_0) {
242 TRUE_OR_RETURN(br->ReadUE(&slice_header->collocated_ref_idx));
246 if ((pps->weighted_pred_flag && slice_header->slice_type == kPSlice) ||
247 (pps->weighted_bipred_flag && slice_header->slice_type == kBSlice)) {
248 OK_OR_RETURN(SkipPredictionWeightTable(
249 slice_header->slice_type == kBSlice, *sps, *slice_header, br));
251 TRUE_OR_RETURN(br->ReadUE(&slice_header->five_minus_max_num_merge_cand));
254 TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_qp_delta));
255 if (pps->slice_chroma_qp_offsets_present_flag) {
256 TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_cb_qp_offset));
257 TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_cr_qp_offset));
260 if (pps->chroma_qp_offset_list_enabled_flag) {
262 br->ReadBool(&slice_header->cu_chroma_qp_offset_enabled_flag));
264 if (pps->deblocking_filter_override_enabled_flag) {
266 br->ReadBool(&slice_header->deblocking_filter_override_flag));
268 if (slice_header->deblocking_filter_override_flag) {
270 br->ReadBool(&slice_header->slice_deblocking_filter_disabled_flag));
271 if (!slice_header->slice_deblocking_filter_disabled_flag) {
272 TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_beta_offset_div2));
273 TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_tc_offset_div2));
276 if (pps->loop_filter_across_slices_enabled_flag &&
277 (slice_header->slice_sao_luma_flag ||
278 slice_header->slice_sao_chroma_flag ||
279 !slice_header->slice_deblocking_filter_disabled_flag)) {
280 TRUE_OR_RETURN(br->ReadBool(
281 &slice_header->slice_loop_filter_across_slices_enabled_flag));
285 if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) {
286 TRUE_OR_RETURN(br->ReadUE(&slice_header->num_entry_point_offsets));
287 if (slice_header->num_entry_point_offsets > 0) {
288 TRUE_OR_RETURN(br->ReadUE(&slice_header->offset_len_minus1));
289 slice_header->entry_point_offset_minus1.resize(
290 slice_header->num_entry_point_offsets);
291 for (
int i = 0; i < slice_header->num_entry_point_offsets; i++) {
293 br->ReadBits(slice_header->offset_len_minus1 + 1,
294 &slice_header->entry_point_offset_minus1[i]));
299 if (pps->slice_segment_header_extension_present_flag) {
300 int extension_length;
301 TRUE_OR_RETURN(br->ReadUE(&extension_length));
302 TRUE_OR_RETURN(br->SkipBits(extension_length * 8));
305 size_t epb = br->NumEmulationPreventionBytesRead();
306 slice_header->header_bit_size =
307 (nalu.payload_size() - epb) * 8 - br->NumBitsLeft();
313 DCHECK_EQ(Nalu::H265_PPS, nalu.type());
317 reader.Initialize(nalu.data() + nalu.header_size(), nalu.payload_size());
321 scoped_ptr<H265Pps> pps(
new H265Pps);
323 TRUE_OR_RETURN(br->ReadUE(&pps->pic_parameter_set_id));
324 TRUE_OR_RETURN(br->ReadUE(&pps->seq_parameter_set_id));
326 TRUE_OR_RETURN(br->ReadBool(&pps->dependent_slice_segments_enabled_flag));
327 TRUE_OR_RETURN(br->ReadBool(&pps->output_flag_present_flag));
328 TRUE_OR_RETURN(br->ReadBits(3, &pps->num_extra_slice_header_bits));
329 TRUE_OR_RETURN(br->ReadBool(&pps->sign_data_hiding_enabled_flag));
330 TRUE_OR_RETURN(br->ReadBool(&pps->cabac_init_present_flag));
332 TRUE_OR_RETURN(br->ReadUE(&pps->num_ref_idx_l0_default_active_minus1));
333 TRUE_OR_RETURN(br->ReadUE(&pps->num_ref_idx_l1_default_active_minus1));
334 TRUE_OR_RETURN(br->ReadSE(&pps->init_qp_minus26));
335 TRUE_OR_RETURN(br->ReadBool(&pps->constrained_intra_pred_flag));
336 TRUE_OR_RETURN(br->ReadBool(&pps->transform_skip_enabled_flag));
338 TRUE_OR_RETURN(br->ReadBool(&pps->cu_qp_delta_enabled_flag));
339 if (pps->cu_qp_delta_enabled_flag)
340 TRUE_OR_RETURN(br->ReadUE(&pps->diff_cu_qp_delta_depth));
341 TRUE_OR_RETURN(br->ReadSE(&pps->cb_qp_offset));
342 TRUE_OR_RETURN(br->ReadSE(&pps->cr_qp_offset));
344 TRUE_OR_RETURN(br->ReadBool(&pps->slice_chroma_qp_offsets_present_flag));
345 TRUE_OR_RETURN(br->ReadBool(&pps->weighted_pred_flag));
346 TRUE_OR_RETURN(br->ReadBool(&pps->weighted_bipred_flag));
347 TRUE_OR_RETURN(br->ReadBool(&pps->transquant_bypass_enabled_flag));
348 TRUE_OR_RETURN(br->ReadBool(&pps->tiles_enabled_flag));
349 TRUE_OR_RETURN(br->ReadBool(&pps->entropy_coding_sync_enabled_flag));
351 if (pps->tiles_enabled_flag) {
352 TRUE_OR_RETURN(br->ReadUE(&pps->num_tile_columns_minus1));
353 TRUE_OR_RETURN(br->ReadUE(&pps->num_tile_rows_minus1));
354 TRUE_OR_RETURN(br->ReadBool(&pps->uniform_spacing_flag));
355 if (!pps->uniform_spacing_flag) {
356 pps->column_width_minus1.resize(pps->num_tile_columns_minus1);
357 for (
int i = 0; i < pps->num_tile_columns_minus1; i++) {
358 TRUE_OR_RETURN(br->ReadUE(&pps->column_width_minus1[i]));
360 pps->row_height_minus1.resize(pps->num_tile_rows_minus1);
361 for (
int i = 0; i < pps->num_tile_rows_minus1; i++) {
362 TRUE_OR_RETURN(br->ReadUE(&pps->row_height_minus1[i]));
365 TRUE_OR_RETURN(br->ReadBool(&pps->loop_filter_across_tiles_enabled_flag));
368 TRUE_OR_RETURN(br->ReadBool(&pps->loop_filter_across_slices_enabled_flag));
369 TRUE_OR_RETURN(br->ReadBool(&pps->deblocking_filter_control_present_flag));
370 if (pps->deblocking_filter_control_present_flag) {
371 TRUE_OR_RETURN(br->ReadBool(&pps->deblocking_filter_override_enabled_flag));
372 TRUE_OR_RETURN(br->ReadBool(&pps->deblocking_filter_disabled_flag));
373 if (!pps->deblocking_filter_disabled_flag) {
374 TRUE_OR_RETURN(br->ReadSE(&pps->beta_offset_div2));
375 TRUE_OR_RETURN(br->ReadSE(&pps->tc_offset_div2));
379 TRUE_OR_RETURN(br->ReadBool(&pps->scaling_list_data_present_flag));
380 if (pps->scaling_list_data_present_flag) {
381 OK_OR_RETURN(SkipScalingListData(br));
384 TRUE_OR_RETURN(br->ReadBool(&pps->lists_modification_present_flag));
385 TRUE_OR_RETURN(br->ReadUE(&pps->log2_parallel_merge_level_minus2));
388 br->ReadBool(&pps->slice_segment_header_extension_present_flag));
390 bool pps_extension_present_flag;
391 bool pps_range_extension_flag =
false;
392 TRUE_OR_RETURN(br->ReadBool(&pps_extension_present_flag));
393 if (pps_extension_present_flag) {
394 TRUE_OR_RETURN(br->ReadBool(&pps_range_extension_flag));
396 TRUE_OR_RETURN(br->SkipBits(1 + 1 + 5));
399 if (pps_range_extension_flag) {
400 if (pps->transform_skip_enabled_flag) {
403 TRUE_OR_RETURN(br->ReadUE(&ignored));
406 TRUE_OR_RETURN(br->SkipBits(1));
407 TRUE_OR_RETURN(br->ReadBool(&pps->chroma_qp_offset_list_enabled_flag));
414 *pps_id = pps->pic_parameter_set_id;
415 delete active_ppses_[*pps_id];
416 active_ppses_[*pps_id] = pps.release();
422 DCHECK_EQ(Nalu::H265_SPS, nalu.type());
426 reader.Initialize(nalu.data() + nalu.header_size(), nalu.payload_size());
431 scoped_ptr<H265Sps> sps(
new H265Sps);
433 TRUE_OR_RETURN(br->ReadBits(4, &sps->video_parameter_set_id));
434 TRUE_OR_RETURN(br->ReadBits(3, &sps->max_sub_layers_minus1));
435 TRUE_OR_RETURN(br->ReadBool(&sps->temporal_id_nesting_flag));
437 OK_OR_RETURN(SkipProfileTierLevel(
true, sps->max_sub_layers_minus1, br));
439 TRUE_OR_RETURN(br->ReadUE(&sps->seq_parameter_set_id));
440 TRUE_OR_RETURN(br->ReadUE(&sps->chroma_format_idc));
441 if (sps->chroma_format_idc == 3) {
442 TRUE_OR_RETURN(br->ReadBool(&sps->separate_colour_plane_flag));
444 TRUE_OR_RETURN(br->ReadUE(&sps->pic_width_in_luma_samples));
445 TRUE_OR_RETURN(br->ReadUE(&sps->pic_height_in_luma_samples));
447 TRUE_OR_RETURN(br->ReadBool(&sps->conformance_window_flag));
448 if (sps->conformance_window_flag) {
449 TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_left_offset));
450 TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_right_offset));
451 TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_top_offset));
452 TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_bottom_offset));
455 TRUE_OR_RETURN(br->ReadUE(&sps->bit_depth_luma_minus8));
456 TRUE_OR_RETURN(br->ReadUE(&sps->bit_depth_chroma_minus8));
457 TRUE_OR_RETURN(br->ReadUE(&sps->log2_max_pic_order_cnt_lsb_minus4));
459 TRUE_OR_RETURN(br->ReadBool(&sps->sub_layer_ordering_info_present_flag));
460 int start = sps->sub_layer_ordering_info_present_flag
462 : sps->max_sub_layers_minus1;
463 for (
int i = start; i <= sps->max_sub_layers_minus1; i++) {
464 TRUE_OR_RETURN(br->ReadUE(&sps->max_dec_pic_buffering_minus1[i]));
465 TRUE_OR_RETURN(br->ReadUE(&sps->max_num_reorder_pics[i]));
466 TRUE_OR_RETURN(br->ReadUE(&sps->max_latency_increase_plus1[i]));
469 TRUE_OR_RETURN(br->ReadUE(&sps->log2_min_luma_coding_block_size_minus3));
470 TRUE_OR_RETURN(br->ReadUE(&sps->log2_diff_max_min_luma_coding_block_size));
471 TRUE_OR_RETURN(br->ReadUE(&sps->log2_min_luma_transform_block_size_minus2));
472 TRUE_OR_RETURN(br->ReadUE(&sps->log2_diff_max_min_luma_transform_block_size));
473 TRUE_OR_RETURN(br->ReadUE(&sps->max_transform_hierarchy_depth_inter));
474 TRUE_OR_RETURN(br->ReadUE(&sps->max_transform_hierarchy_depth_intra));
476 TRUE_OR_RETURN(br->ReadBool(&sps->scaling_list_enabled_flag));
477 if (sps->scaling_list_enabled_flag) {
478 TRUE_OR_RETURN(br->ReadBool(&sps->scaling_list_data_present_flag));
479 if (sps->scaling_list_data_present_flag) {
480 OK_OR_RETURN(SkipScalingListData(br));
484 TRUE_OR_RETURN(br->ReadBool(&sps->amp_enabled_flag));
485 TRUE_OR_RETURN(br->ReadBool(&sps->sample_adaptive_offset_enabled_flag));
486 TRUE_OR_RETURN(br->ReadBool(&sps->pcm_enabled_flag));
487 if (sps->pcm_enabled_flag) {
488 TRUE_OR_RETURN(br->ReadBits(4, &sps->pcm_sample_bit_depth_luma_minus1));
489 TRUE_OR_RETURN(br->ReadBits(4, &sps->pcm_sample_bit_depth_chroma_minus1));
491 br->ReadUE(&sps->log2_min_pcm_luma_coding_block_size_minus3));
493 br->ReadUE(&sps->log2_diff_max_min_pcm_luma_coding_block_size));
494 TRUE_OR_RETURN(br->ReadBool(&sps->pcm_loop_filter_disabled_flag));
497 TRUE_OR_RETURN(br->ReadUE(&sps->num_short_term_ref_pic_sets));
498 sps->st_ref_pic_sets.resize(sps->num_short_term_ref_pic_sets);
499 for (
int i = 0; i < sps->num_short_term_ref_pic_sets; i++) {
500 OK_OR_RETURN(ParseReferencePictureSet(sps->num_short_term_ref_pic_sets, i,
501 sps->st_ref_pic_sets, br,
502 &sps->st_ref_pic_sets[i]));
505 TRUE_OR_RETURN(br->ReadBool(&sps->long_term_ref_pic_present_flag));
506 if (sps->long_term_ref_pic_present_flag) {
507 TRUE_OR_RETURN(br->ReadUE(&sps->num_long_term_ref_pics));
508 sps->lt_ref_pic_poc_lsb.resize(sps->num_long_term_ref_pics);
509 sps->used_by_curr_pic_lt_flag.resize(sps->num_long_term_ref_pics);
510 for (
int i = 0; i < sps->num_long_term_ref_pics; i++) {
511 TRUE_OR_RETURN(br->ReadBits(sps->log2_max_pic_order_cnt_lsb_minus4 + 4,
512 &sps->lt_ref_pic_poc_lsb[i]));
514 TRUE_OR_RETURN(br->ReadBool(&temp));
515 sps->used_by_curr_pic_lt_flag[i] = temp;
519 TRUE_OR_RETURN(br->ReadBool(&sps->temporal_mvp_enabled_flag));
520 TRUE_OR_RETURN(br->ReadBool(&sps->strong_intra_smoothing_enabled_flag));
525 *sps_id = sps->seq_parameter_set_id;
526 delete active_spses_[*sps_id];
527 active_spses_[*sps_id] = sps.release();
533 return active_ppses_[pps_id];
537 return active_spses_[sps_id];
540 H265Parser::Result H265Parser::ParseReferencePictureSet(
541 int num_short_term_ref_pic_sets,
543 const std::vector<H265ReferencePictureSet>& ref_pic_sets,
550 bool inter_ref_pic_set_prediction =
false;
551 if (st_rps_idx != 0) {
552 TRUE_OR_RETURN(br->ReadBool(&inter_ref_pic_set_prediction));
555 if (inter_ref_pic_set_prediction) {
557 if (st_rps_idx == num_short_term_ref_pic_sets) {
558 TRUE_OR_RETURN(br->ReadUE(&delta_idx));
560 TRUE_OR_RETURN(delta_idx <= st_rps_idx);
563 int ref_rps_idx = st_rps_idx - delta_idx;
564 DCHECK_LE(0, ref_rps_idx);
565 DCHECK_LT(ref_rps_idx, st_rps_idx);
568 int abs_delta_rps_minus1;
569 TRUE_OR_RETURN(br->ReadBool(&delta_rps_sign));
570 TRUE_OR_RETURN(br->ReadUE(&abs_delta_rps_minus1));
572 delta_rps_sign ? -(abs_delta_rps_minus1 + 1) : abs_delta_rps_minus1 + 1;
574 int ref_num_delta_pocs = ref_pic_sets[ref_rps_idx].num_delta_pocs;
575 std::vector<bool> used_by_curr_pic(ref_num_delta_pocs + 1);
576 std::vector<bool> use_delta(ref_num_delta_pocs + 1);
577 for (
int j = 0; j <= ref_num_delta_pocs; j++) {
579 TRUE_OR_RETURN(br->ReadBool(&temp));
580 used_by_curr_pic[j] = temp;
582 if (!used_by_curr_pic[j]) {
583 TRUE_OR_RETURN(br->ReadBool(&temp));
590 int ref_num_positive_pics = ref_pic_sets[ref_rps_idx].num_positive_pics;
591 int ref_num_negative_pics = ref_pic_sets[ref_rps_idx].num_negative_pics;
597 for (
int j = ref_num_positive_pics - 1; j >= 0; j--) {
598 int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s1[j] + delta_rps;
599 if (d_poc < 0 && use_delta[ref_num_negative_pics + j]) {
600 out_ref_pic_set->delta_poc_s0[i] = d_poc;
601 out_ref_pic_set->used_by_curr_pic_s0[i] =
602 used_by_curr_pic[ref_num_negative_pics + j];
606 if (delta_rps < 0 && use_delta[ref_num_delta_pocs]) {
607 out_ref_pic_set->delta_poc_s0[i] = delta_rps;
608 out_ref_pic_set->used_by_curr_pic_s0[i] =
609 used_by_curr_pic[ref_num_delta_pocs];
612 for (
int j = 0; j < ref_num_negative_pics; j++) {
613 int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s0[j] + delta_rps;
614 if (d_poc < 0 && use_delta[j]) {
615 out_ref_pic_set->delta_poc_s0[i] = d_poc;
616 out_ref_pic_set->used_by_curr_pic_s0[i] = used_by_curr_pic[j];
620 out_ref_pic_set->num_negative_pics = i;
626 for (
int j = ref_num_negative_pics - 1; j >= 0; j--) {
627 int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s0[j] + delta_rps;
628 if (d_poc > 0 && use_delta[j]) {
629 out_ref_pic_set->delta_poc_s1[i] = d_poc;
630 out_ref_pic_set->used_by_curr_pic_s1[i] = used_by_curr_pic[j];
634 if (delta_rps > 0 && use_delta[ref_num_delta_pocs]) {
635 out_ref_pic_set->delta_poc_s1[i] = delta_rps;
636 out_ref_pic_set->used_by_curr_pic_s1[i] =
637 used_by_curr_pic[ref_num_delta_pocs];
640 for (
int j = 0; j < ref_num_positive_pics; j++) {
641 int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s1[j] + delta_rps;
642 if (d_poc > 0 && use_delta[ref_num_negative_pics + j]) {
643 out_ref_pic_set->delta_poc_s1[i] = d_poc;
644 out_ref_pic_set->used_by_curr_pic_s1[i] =
645 used_by_curr_pic[ref_num_negative_pics + j];
649 out_ref_pic_set->num_positive_pics = i;
652 TRUE_OR_RETURN(br->ReadUE(&out_ref_pic_set->num_negative_pics));
653 TRUE_OR_RETURN(br->ReadUE(&out_ref_pic_set->num_positive_pics));
656 for (
int i = 0; i < out_ref_pic_set->num_negative_pics; i++) {
657 int delta_poc_s0_minus1;
658 TRUE_OR_RETURN(br->ReadUE(&delta_poc_s0_minus1));
659 out_ref_pic_set->delta_poc_s0[i] = prev_poc - (delta_poc_s0_minus1 + 1);
660 prev_poc = out_ref_pic_set->delta_poc_s0[i];
662 TRUE_OR_RETURN(br->ReadBool(&out_ref_pic_set->used_by_curr_pic_s0[i]));
666 for (
int i = 0; i < out_ref_pic_set->num_positive_pics; i++) {
667 int delta_poc_s1_minus1;
668 TRUE_OR_RETURN(br->ReadUE(&delta_poc_s1_minus1));
669 out_ref_pic_set->delta_poc_s1[i] = prev_poc + delta_poc_s1_minus1 + 1;
670 prev_poc = out_ref_pic_set->delta_poc_s1[i];
672 TRUE_OR_RETURN(br->ReadBool(&out_ref_pic_set->used_by_curr_pic_s1[i]));
676 out_ref_pic_set->num_delta_pocs =
677 out_ref_pic_set->num_positive_pics + out_ref_pic_set->num_negative_pics;
681 H265Parser::Result H265Parser::SkipReferencePictureListModification(
682 const H265SliceHeader& slice_header,
684 int num_pic_total_curr,
688 bool ref_pic_list_modification_flag_l0;
689 TRUE_OR_RETURN(br->ReadBool(&ref_pic_list_modification_flag_l0));
690 if (ref_pic_list_modification_flag_l0) {
691 for (
int i = 0; i <= pps.num_ref_idx_l0_default_active_minus1; i++) {
692 TRUE_OR_RETURN(br->SkipBits(ceil(log2(num_pic_total_curr))));
696 if (slice_header.slice_type == kBSlice) {
697 bool ref_pic_list_modification_flag_l1;
698 TRUE_OR_RETURN(br->ReadBool(&ref_pic_list_modification_flag_l1));
699 if (ref_pic_list_modification_flag_l1) {
700 for (
int i = 0; i <= pps.num_ref_idx_l1_default_active_minus1; i++) {
701 TRUE_OR_RETURN(br->SkipBits(ceil(log2(num_pic_total_curr))));
709 H265Parser::Result H265Parser::SkipPredictionWeightTablePart(
710 int num_ref_idx_minus1,
711 int chroma_array_type,
715 std::vector<bool> luma_weight_flag(num_ref_idx_minus1 + 1);
716 std::vector<bool> chroma_weight_flag(num_ref_idx_minus1 + 1);
718 for (
int i = 0; i <= num_ref_idx_minus1; i++) {
720 TRUE_OR_RETURN(br->ReadBool(&temp));
721 luma_weight_flag[i] = temp;
723 if (chroma_array_type != 0) {
724 for (
int i = 0; i <= num_ref_idx_minus1; i++) {
726 TRUE_OR_RETURN(br->ReadBool(&temp));
727 chroma_weight_flag[i] = temp;
730 for (
int i = 0; i <= num_ref_idx_minus1; i++) {
731 if (luma_weight_flag[i]) {
732 TRUE_OR_RETURN(br->ReadSE(&ignored));
733 TRUE_OR_RETURN(br->ReadSE(&ignored));
735 if (chroma_weight_flag[i]) {
736 for (
int j = 0; j < 2; j++) {
737 TRUE_OR_RETURN(br->ReadSE(&ignored));
738 TRUE_OR_RETURN(br->ReadSE(&ignored));
746 H265Parser::Result H265Parser::SkipPredictionWeightTable(
749 const H265SliceHeader& slice_header,
753 int chroma_array_type = sps.GetChromaArrayType();
755 TRUE_OR_RETURN(br->ReadUE(&ignored));
756 if (chroma_array_type != 0) {
757 TRUE_OR_RETURN(br->ReadSE(&ignored));
759 OK_OR_RETURN(SkipPredictionWeightTablePart(
760 slice_header.num_ref_idx_l0_active_minus1, chroma_array_type, br));
762 OK_OR_RETURN(SkipPredictionWeightTablePart(
763 slice_header.num_ref_idx_l1_active_minus1, chroma_array_type, br));
769 H265Parser::Result H265Parser::SkipProfileTierLevel(
770 bool profile_present,
771 int max_num_sub_layers_minus1,
775 if (profile_present) {
783 TRUE_OR_RETURN(br->SkipBits(2 + 1 + 5 + 32 + 4 + 44));
786 TRUE_OR_RETURN(br->SkipBits(8));
788 std::vector<bool> sub_layer_profile_present(max_num_sub_layers_minus1);
789 std::vector<bool> sub_layer_level_present(max_num_sub_layers_minus1);
790 for (
int i = 0; i < max_num_sub_layers_minus1; i++) {
792 TRUE_OR_RETURN(br->ReadBool(&profile));
793 TRUE_OR_RETURN(br->ReadBool(&level));
794 sub_layer_profile_present[i] = profile;
795 sub_layer_level_present[i] = level;
798 if (max_num_sub_layers_minus1 > 0) {
799 for (
int i = max_num_sub_layers_minus1; i < 8; i++)
800 TRUE_OR_RETURN(br->SkipBits(2));
803 for (
int i = 0; i < max_num_sub_layers_minus1; i++) {
804 if (sub_layer_profile_present[i]) {
809 TRUE_OR_RETURN(br->SkipBits(2 + 1 + 5 + 32 + 4 + 43 + 1));
811 if (sub_layer_level_present[i]) {
812 TRUE_OR_RETURN(br->SkipBits(8));
819 H265Parser::Result H265Parser::SkipScalingListData(H26xBitReader* br) {
822 for (
int size_id = 0; size_id < 4; size_id++) {
823 for (
int matrix_id = 0; matrix_id < 6;
824 matrix_id += ((size_id == 3) ? 3 : 1)) {
825 bool scaling_list_pred_mode;
826 TRUE_OR_RETURN(br->ReadBool(&scaling_list_pred_mode));
827 if (!scaling_list_pred_mode) {
829 TRUE_OR_RETURN(br->ReadUE(&ignored));
831 int coefNum = std::min(64, (1 << (4 + (size_id << 1))));
833 TRUE_OR_RETURN(br->ReadSE(&ignored));
836 for (
int i = 0; i < coefNum; i++) {
837 TRUE_OR_RETURN(br->ReadSE(&ignored));