Shaka Packager SDK
h265_parser.cc
1 // Copyright 2016 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/media/codecs/h265_parser.h"
8 
9 #include <math.h>
10 #include <algorithm>
11 
12 #include "packager/base/logging.h"
13 #include "packager/media/base/macros.h"
14 #include "packager/media/codecs/nalu_reader.h"
15 
16 #define TRUE_OR_RETURN(a) \
17  do { \
18  if (!(a)) { \
19  DVLOG(1) << "Failure while processing " << #a; \
20  return kInvalidStream; \
21  } \
22  } while (0)
23 
24 #define OK_OR_RETURN(a) \
25  do { \
26  Result status = (a); \
27  if (status != kOk) \
28  return status; \
29  } while (false)
30 
31 namespace shaka {
32 namespace media {
33 
34 namespace {
35 int GetNumPicTotalCurr(const H265SliceHeader& slice_header,
36  const H265Sps& sps) {
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;
42 
43  for (int i = 0; i < ref_pic_set.num_negative_pics; i++) {
44  if (ref_pic_set.used_by_curr_pic_s0[i])
45  num_pic_total_curr++;
46  }
47  for (int i = 0; i < ref_pic_set.num_positive_pics; i++) {
48  if (ref_pic_set.used_by_curr_pic_s1[i])
49  num_pic_total_curr++;
50  }
51 
52  return num_pic_total_curr + slice_header.used_by_curr_pic_lt;
53 }
54 
55 void GetAspectRatioInfo(const H265Sps& sps,
56  uint32_t* pixel_width,
57  uint32_t* pixel_height) {
58  // The default value is 0; so if this is not in the SPS, it will correctly
59  // assume unspecified.
60  int aspect_ratio_idc = sps.vui_parameters.aspect_ratio_idc;
61 
62  // Table E.1
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;
80 
81  case 255:
82  *pixel_width = sps.vui_parameters.sar_width;
83  *pixel_height = sps.vui_parameters.sar_height;
84  break;
85 
86  default:
87  // Section E.3.1 specifies that other values should be interpreted as 0.
88  LOG(WARNING) << "Unknown aspect_ratio_idc " << aspect_ratio_idc;
89  FALLTHROUGH_INTENDED;
90  case 0:
91  // Unlike the spec, assume 1:1 if not specified.
92  *pixel_width = 1;
93  *pixel_height = 1;
94  break;
95  }
96 }
97 } // namespace
98 
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) {
104  int crop_x = 0;
105  int crop_y = 0;
106  if (sps.conformance_window_flag) {
107  int sub_width_c = 0;
108  int sub_height_c = 0;
109 
110  // Table 6-1
111  switch (sps.chroma_format_idc) {
112  case 0: // Monochrome
113  sub_width_c = 1;
114  sub_height_c = 1;
115  break;
116  case 1: // 4:2:0
117  sub_width_c = 2;
118  sub_height_c = 2;
119  break;
120  case 2: // 4:2:2
121  sub_width_c = 2;
122  sub_height_c = 1;
123  break;
124  case 3: // 4:4:4
125  sub_width_c = 1;
126  sub_height_c = 1;
127  break;
128  default:
129  LOG(ERROR) << "Unexpected chroma_format_idc " << sps.chroma_format_idc;
130  return false;
131  }
132 
133  // Formula D-28, D-29
134  crop_x =
135  sub_width_c * (sps.conf_win_right_offset + sps.conf_win_left_offset);
136  crop_y =
137  sub_height_c * (sps.conf_win_bottom_offset + sps.conf_win_top_offset);
138  }
139 
140  // Formula D-28, D-29
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);
144  return true;
145 }
146 
147 H265Pps::H265Pps() {}
148 H265Pps::~H265Pps() {}
149 
150 H265Sps::H265Sps() {}
151 H265Sps::~H265Sps() {}
152 
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;
158 
159  // Round-up division.
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;
163 }
164 
165 int H265Sps::GetChromaArrayType() const {
166  if (!separate_colour_plane_flag)
167  return chroma_format_idc;
168  else
169  return 0;
170 }
171 
172 H265ReferencePictureListModifications::H265ReferencePictureListModifications() {
173 }
174 H265ReferencePictureListModifications::
175  ~H265ReferencePictureListModifications() {}
176 
177 H265SliceHeader::H265SliceHeader() {}
178 H265SliceHeader::~H265SliceHeader() {}
179 
180 H265Parser::H265Parser() {}
181 H265Parser::~H265Parser() {}
182 
183 H265Parser::Result H265Parser::ParseSliceHeader(const Nalu& nalu,
184  H265SliceHeader* slice_header) {
185  DCHECK(nalu.is_video_slice());
186  *slice_header = H265SliceHeader();
187 
188  // Parses whole element.
189  H26xBitReader reader;
190  reader.Initialize(nalu.data() + nalu.header_size(), nalu.payload_size());
191  H26xBitReader* br = &reader;
192 
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));
197  }
198 
199  TRUE_OR_RETURN(br->ReadUE(&slice_header->pic_parameter_set_id));
200  const H265Pps* pps = GetPps(slice_header->pic_parameter_set_id);
201  TRUE_OR_RETURN(pps);
202 
203  const H265Sps* sps = GetSps(pps->seq_parameter_set_id);
204  TRUE_OR_RETURN(sps);
205 
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));
209  }
210  const int bit_length = ceil(log2(sps->GetPicSizeInCtbsY()));
211  TRUE_OR_RETURN(br->ReadBits(bit_length, &slice_header->segment_address));
212  }
213 
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));
219  }
220  if (sps->separate_colour_plane_flag) {
221  TRUE_OR_RETURN(br->ReadBits(2, &slice_header->colour_plane_id));
222  }
223 
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));
228 
229  TRUE_OR_RETURN(
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) {
236  TRUE_OR_RETURN(
237  br->ReadBits(ceil(log2(sps->num_short_term_ref_pic_sets)),
238  &slice_header->short_term_ref_pic_set_idx));
239  TRUE_OR_RETURN(slice_header->short_term_ref_pic_set_idx <
240  sps->num_short_term_ref_pic_sets);
241  }
242 
243  if (sps->long_term_ref_pic_present_flag) {
244  if (sps->num_long_term_ref_pics > 0) {
245  TRUE_OR_RETURN(br->ReadUE(&slice_header->num_long_term_sps));
246  }
247  TRUE_OR_RETURN(br->ReadUE(&slice_header->num_long_term_pics));
248 
249  const int pic_count =
250  slice_header->num_long_term_sps + slice_header->num_long_term_pics;
251  slice_header->long_term_pics_info.resize(pic_count);
252  for (int i = 0; i < pic_count; i++) {
253  if (i < slice_header->num_long_term_sps) {
254  int lt_idx_sps = 0;
255  if (sps->num_long_term_ref_pics > 1) {
256  TRUE_OR_RETURN(br->ReadBits(
257  ceil(log2(sps->num_long_term_ref_pics)), &lt_idx_sps));
258  }
259  if (sps->used_by_curr_pic_lt_flag[lt_idx_sps])
260  slice_header->used_by_curr_pic_lt++;
261  } else {
262  TRUE_OR_RETURN(br->SkipBits(sps->log2_max_pic_order_cnt_lsb_minus4 +
263  4)); // poc_lsb_lt
264  bool used_by_curr_pic_lt_flag;
265  TRUE_OR_RETURN(br->ReadBool(&used_by_curr_pic_lt_flag));
266  if (used_by_curr_pic_lt_flag)
267  slice_header->used_by_curr_pic_lt++;
268  }
269  TRUE_OR_RETURN(br->ReadBool(&slice_header->long_term_pics_info[i]
270  .delta_poc_msb_present_flag));
271  if (slice_header->long_term_pics_info[i].delta_poc_msb_present_flag) {
272  TRUE_OR_RETURN(br->ReadUE(
273  &slice_header->long_term_pics_info[i].delta_poc_msb_cycle_lt));
274  }
275  }
276  }
277 
278  if (sps->temporal_mvp_enabled_flag) {
279  TRUE_OR_RETURN(
280  br->ReadBool(&slice_header->slice_temporal_mvp_enabled_flag));
281  }
282  }
283 
284  if (nalu.nuh_layer_id() != 0) {
285  NOTIMPLEMENTED() << "Multi-layer streams are not supported.";
286  return kUnsupportedStream;
287  }
288 
289  if (sps->sample_adaptive_offset_enabled_flag) {
290  TRUE_OR_RETURN(br->ReadBool(&slice_header->slice_sao_luma_flag));
291  if (sps->GetChromaArrayType() != 0) {
292  TRUE_OR_RETURN(br->ReadBool(&slice_header->slice_sao_chroma_flag));
293  }
294  }
295 
296  slice_header->num_ref_idx_l0_active_minus1 =
297  pps->num_ref_idx_l0_default_active_minus1;
298  slice_header->num_ref_idx_l1_active_minus1 =
299  pps->num_ref_idx_l1_default_active_minus1;
300  if (slice_header->slice_type == kPSlice ||
301  slice_header->slice_type == kBSlice) {
302  TRUE_OR_RETURN(
303  br->ReadBool(&slice_header->num_ref_idx_active_override_flag));
304  if (slice_header->num_ref_idx_active_override_flag) {
305  TRUE_OR_RETURN(br->ReadUE(&slice_header->num_ref_idx_l0_active_minus1));
306  if (slice_header->slice_type == kBSlice) {
307  TRUE_OR_RETURN(
308  br->ReadUE(&slice_header->num_ref_idx_l1_active_minus1));
309  }
310  }
311 
312  const int num_pic_total_curr = GetNumPicTotalCurr(*slice_header, *sps);
313  if (pps->lists_modification_present_flag && num_pic_total_curr > 1) {
314  OK_OR_RETURN(SkipReferencePictureListModification(
315  *slice_header, *pps, num_pic_total_curr, br));
316  }
317 
318  if (slice_header->slice_type == kBSlice) {
319  TRUE_OR_RETURN(br->ReadBool(&slice_header->mvd_l1_zero_flag));
320  }
321  if (pps->cabac_init_present_flag) {
322  TRUE_OR_RETURN(br->ReadBool(&slice_header->cabac_init_flag));
323  }
324  if (slice_header->slice_temporal_mvp_enabled_flag) {
325  if (slice_header->slice_type == kBSlice) {
326  TRUE_OR_RETURN(br->ReadBool(&slice_header->collocated_from_l0));
327  }
328  bool l0_greater_than_0 = slice_header->num_ref_idx_l0_active_minus1 > 0;
329  bool l1_greater_than_0 = slice_header->num_ref_idx_l1_active_minus1 > 0;
330  if (slice_header->collocated_from_l0 ? l0_greater_than_0
331  : l1_greater_than_0) {
332  TRUE_OR_RETURN(br->ReadUE(&slice_header->collocated_ref_idx));
333  }
334  }
335 
336  if ((pps->weighted_pred_flag && slice_header->slice_type == kPSlice) ||
337  (pps->weighted_bipred_flag && slice_header->slice_type == kBSlice)) {
338  OK_OR_RETURN(SkipPredictionWeightTable(
339  slice_header->slice_type == kBSlice, *sps, *slice_header, br));
340  }
341  TRUE_OR_RETURN(br->ReadUE(&slice_header->five_minus_max_num_merge_cand));
342  }
343 
344  TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_qp_delta));
345  if (pps->slice_chroma_qp_offsets_present_flag) {
346  TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_cb_qp_offset));
347  TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_cr_qp_offset));
348  }
349 
350  if (pps->chroma_qp_offset_list_enabled_flag) {
351  TRUE_OR_RETURN(
352  br->ReadBool(&slice_header->cu_chroma_qp_offset_enabled_flag));
353  }
354  if (pps->deblocking_filter_override_enabled_flag) {
355  TRUE_OR_RETURN(
356  br->ReadBool(&slice_header->deblocking_filter_override_flag));
357  }
358  if (slice_header->deblocking_filter_override_flag) {
359  TRUE_OR_RETURN(
360  br->ReadBool(&slice_header->slice_deblocking_filter_disabled_flag));
361  if (!slice_header->slice_deblocking_filter_disabled_flag) {
362  TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_beta_offset_div2));
363  TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_tc_offset_div2));
364  }
365  }
366  if (pps->loop_filter_across_slices_enabled_flag &&
367  (slice_header->slice_sao_luma_flag ||
368  slice_header->slice_sao_chroma_flag ||
369  !slice_header->slice_deblocking_filter_disabled_flag)) {
370  TRUE_OR_RETURN(br->ReadBool(
371  &slice_header->slice_loop_filter_across_slices_enabled_flag));
372  }
373  }
374 
375  if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) {
376  TRUE_OR_RETURN(br->ReadUE(&slice_header->num_entry_point_offsets));
377  if (slice_header->num_entry_point_offsets > 0) {
378  TRUE_OR_RETURN(br->ReadUE(&slice_header->offset_len_minus1));
379  slice_header->entry_point_offset_minus1.resize(
380  slice_header->num_entry_point_offsets);
381  for (int i = 0; i < slice_header->num_entry_point_offsets; i++) {
382  TRUE_OR_RETURN(
383  br->ReadBits(slice_header->offset_len_minus1 + 1,
384  &slice_header->entry_point_offset_minus1[i]));
385  }
386  }
387  }
388 
389  if (pps->slice_segment_header_extension_present_flag) {
390  int extension_length;
391  TRUE_OR_RETURN(br->ReadUE(&extension_length));
392  TRUE_OR_RETURN(br->SkipBits(extension_length * 8));
393  }
394 
395  OK_OR_RETURN(ByteAlignment(br));
396 
397  slice_header->header_bit_size = nalu.payload_size() * 8 - br->NumBitsLeft();
398  return kOk;
399 }
400 
401 H265Parser::Result H265Parser::ParsePps(const Nalu& nalu, int* pps_id) {
402  DCHECK_EQ(Nalu::H265_PPS, nalu.type());
403 
404  // Reads most of the element, not reading the extension data.
405  H26xBitReader reader;
406  reader.Initialize(nalu.data() + nalu.header_size(), nalu.payload_size());
407  H26xBitReader* br = &reader;
408 
409  *pps_id = -1;
410  std::unique_ptr<H265Pps> pps(new H265Pps);
411 
412  TRUE_OR_RETURN(br->ReadUE(&pps->pic_parameter_set_id));
413  TRUE_OR_RETURN(br->ReadUE(&pps->seq_parameter_set_id));
414 
415  TRUE_OR_RETURN(br->ReadBool(&pps->dependent_slice_segments_enabled_flag));
416  TRUE_OR_RETURN(br->ReadBool(&pps->output_flag_present_flag));
417  TRUE_OR_RETURN(br->ReadBits(3, &pps->num_extra_slice_header_bits));
418  TRUE_OR_RETURN(br->ReadBool(&pps->sign_data_hiding_enabled_flag));
419  TRUE_OR_RETURN(br->ReadBool(&pps->cabac_init_present_flag));
420 
421  TRUE_OR_RETURN(br->ReadUE(&pps->num_ref_idx_l0_default_active_minus1));
422  TRUE_OR_RETURN(br->ReadUE(&pps->num_ref_idx_l1_default_active_minus1));
423  TRUE_OR_RETURN(br->ReadSE(&pps->init_qp_minus26));
424  TRUE_OR_RETURN(br->ReadBool(&pps->constrained_intra_pred_flag));
425  TRUE_OR_RETURN(br->ReadBool(&pps->transform_skip_enabled_flag));
426 
427  TRUE_OR_RETURN(br->ReadBool(&pps->cu_qp_delta_enabled_flag));
428  if (pps->cu_qp_delta_enabled_flag)
429  TRUE_OR_RETURN(br->ReadUE(&pps->diff_cu_qp_delta_depth));
430  TRUE_OR_RETURN(br->ReadSE(&pps->cb_qp_offset));
431  TRUE_OR_RETURN(br->ReadSE(&pps->cr_qp_offset));
432 
433  TRUE_OR_RETURN(br->ReadBool(&pps->slice_chroma_qp_offsets_present_flag));
434  TRUE_OR_RETURN(br->ReadBool(&pps->weighted_pred_flag));
435  TRUE_OR_RETURN(br->ReadBool(&pps->weighted_bipred_flag));
436  TRUE_OR_RETURN(br->ReadBool(&pps->transquant_bypass_enabled_flag));
437  TRUE_OR_RETURN(br->ReadBool(&pps->tiles_enabled_flag));
438  TRUE_OR_RETURN(br->ReadBool(&pps->entropy_coding_sync_enabled_flag));
439 
440  if (pps->tiles_enabled_flag) {
441  TRUE_OR_RETURN(br->ReadUE(&pps->num_tile_columns_minus1));
442  TRUE_OR_RETURN(br->ReadUE(&pps->num_tile_rows_minus1));
443  TRUE_OR_RETURN(br->ReadBool(&pps->uniform_spacing_flag));
444  if (!pps->uniform_spacing_flag) {
445  pps->column_width_minus1.resize(pps->num_tile_columns_minus1);
446  for (int i = 0; i < pps->num_tile_columns_minus1; i++) {
447  TRUE_OR_RETURN(br->ReadUE(&pps->column_width_minus1[i]));
448  }
449  pps->row_height_minus1.resize(pps->num_tile_rows_minus1);
450  for (int i = 0; i < pps->num_tile_rows_minus1; i++) {
451  TRUE_OR_RETURN(br->ReadUE(&pps->row_height_minus1[i]));
452  }
453  }
454  TRUE_OR_RETURN(br->ReadBool(&pps->loop_filter_across_tiles_enabled_flag));
455  }
456 
457  TRUE_OR_RETURN(br->ReadBool(&pps->loop_filter_across_slices_enabled_flag));
458  TRUE_OR_RETURN(br->ReadBool(&pps->deblocking_filter_control_present_flag));
459  if (pps->deblocking_filter_control_present_flag) {
460  TRUE_OR_RETURN(br->ReadBool(&pps->deblocking_filter_override_enabled_flag));
461  TRUE_OR_RETURN(br->ReadBool(&pps->deblocking_filter_disabled_flag));
462  if (!pps->deblocking_filter_disabled_flag) {
463  TRUE_OR_RETURN(br->ReadSE(&pps->beta_offset_div2));
464  TRUE_OR_RETURN(br->ReadSE(&pps->tc_offset_div2));
465  }
466  }
467 
468  TRUE_OR_RETURN(br->ReadBool(&pps->scaling_list_data_present_flag));
469  if (pps->scaling_list_data_present_flag) {
470  OK_OR_RETURN(SkipScalingListData(br));
471  }
472 
473  TRUE_OR_RETURN(br->ReadBool(&pps->lists_modification_present_flag));
474  TRUE_OR_RETURN(br->ReadUE(&pps->log2_parallel_merge_level_minus2));
475 
476  TRUE_OR_RETURN(
477  br->ReadBool(&pps->slice_segment_header_extension_present_flag));
478 
479  bool pps_extension_present_flag;
480  bool pps_range_extension_flag = false;
481  TRUE_OR_RETURN(br->ReadBool(&pps_extension_present_flag));
482  if (pps_extension_present_flag) {
483  TRUE_OR_RETURN(br->ReadBool(&pps_range_extension_flag));
484  // pps_multilayer_extension_flag, pps_3d_extension_flag, pps_extension_5bits
485  TRUE_OR_RETURN(br->SkipBits(1 + 1 + 5));
486  }
487 
488  if (pps_range_extension_flag) {
489  if (pps->transform_skip_enabled_flag) {
490  // log2_max_transform_skip_block_size_minus2
491  int ignored;
492  TRUE_OR_RETURN(br->ReadUE(&ignored));
493  }
494 
495  TRUE_OR_RETURN(br->SkipBits(1)); // cross_component_prediction_enabled_flag
496  TRUE_OR_RETURN(br->ReadBool(&pps->chroma_qp_offset_list_enabled_flag));
497  // Incomplete
498  }
499 
500  // Ignore remaining extension data.
501 
502  // This will replace any existing PPS instance.
503  *pps_id = pps->pic_parameter_set_id;
504  active_ppses_[*pps_id] = std::move(pps);
505 
506  return kOk;
507 }
508 
509 H265Parser::Result H265Parser::ParseSps(const Nalu& nalu, int* sps_id) {
510  DCHECK_EQ(Nalu::H265_SPS, nalu.type());
511 
512  // Reads most of the element, not reading the extension data.
513  H26xBitReader reader;
514  reader.Initialize(nalu.data() + nalu.header_size(), nalu.payload_size());
515  H26xBitReader* br = &reader;
516 
517  *sps_id = -1;
518 
519  std::unique_ptr<H265Sps> sps(new H265Sps);
520 
521  TRUE_OR_RETURN(br->ReadBits(4, &sps->video_parameter_set_id));
522  TRUE_OR_RETURN(br->ReadBits(3, &sps->max_sub_layers_minus1));
523  TRUE_OR_RETURN(br->ReadBool(&sps->temporal_id_nesting_flag));
524 
525  OK_OR_RETURN(
526  ReadProfileTierLevel(true, sps->max_sub_layers_minus1, br, sps.get()));
527 
528  TRUE_OR_RETURN(br->ReadUE(&sps->seq_parameter_set_id));
529  TRUE_OR_RETURN(br->ReadUE(&sps->chroma_format_idc));
530  if (sps->chroma_format_idc == 3) {
531  TRUE_OR_RETURN(br->ReadBool(&sps->separate_colour_plane_flag));
532  }
533  TRUE_OR_RETURN(br->ReadUE(&sps->pic_width_in_luma_samples));
534  TRUE_OR_RETURN(br->ReadUE(&sps->pic_height_in_luma_samples));
535 
536  TRUE_OR_RETURN(br->ReadBool(&sps->conformance_window_flag));
537  if (sps->conformance_window_flag) {
538  TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_left_offset));
539  TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_right_offset));
540  TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_top_offset));
541  TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_bottom_offset));
542  }
543 
544  TRUE_OR_RETURN(br->ReadUE(&sps->bit_depth_luma_minus8));
545  TRUE_OR_RETURN(br->ReadUE(&sps->bit_depth_chroma_minus8));
546  TRUE_OR_RETURN(br->ReadUE(&sps->log2_max_pic_order_cnt_lsb_minus4));
547 
548  TRUE_OR_RETURN(br->ReadBool(&sps->sub_layer_ordering_info_present_flag));
549  int start = sps->sub_layer_ordering_info_present_flag
550  ? 0
551  : sps->max_sub_layers_minus1;
552  for (int i = start; i <= sps->max_sub_layers_minus1; i++) {
553  TRUE_OR_RETURN(br->ReadUE(&sps->max_dec_pic_buffering_minus1[i]));
554  TRUE_OR_RETURN(br->ReadUE(&sps->max_num_reorder_pics[i]));
555  TRUE_OR_RETURN(br->ReadUE(&sps->max_latency_increase_plus1[i]));
556  }
557 
558  TRUE_OR_RETURN(br->ReadUE(&sps->log2_min_luma_coding_block_size_minus3));
559  TRUE_OR_RETURN(br->ReadUE(&sps->log2_diff_max_min_luma_coding_block_size));
560  TRUE_OR_RETURN(br->ReadUE(&sps->log2_min_luma_transform_block_size_minus2));
561  TRUE_OR_RETURN(br->ReadUE(&sps->log2_diff_max_min_luma_transform_block_size));
562  TRUE_OR_RETURN(br->ReadUE(&sps->max_transform_hierarchy_depth_inter));
563  TRUE_OR_RETURN(br->ReadUE(&sps->max_transform_hierarchy_depth_intra));
564 
565  TRUE_OR_RETURN(br->ReadBool(&sps->scaling_list_enabled_flag));
566  if (sps->scaling_list_enabled_flag) {
567  TRUE_OR_RETURN(br->ReadBool(&sps->scaling_list_data_present_flag));
568  if (sps->scaling_list_data_present_flag) {
569  OK_OR_RETURN(SkipScalingListData(br));
570  }
571  }
572 
573  TRUE_OR_RETURN(br->ReadBool(&sps->amp_enabled_flag));
574  TRUE_OR_RETURN(br->ReadBool(&sps->sample_adaptive_offset_enabled_flag));
575  TRUE_OR_RETURN(br->ReadBool(&sps->pcm_enabled_flag));
576  if (sps->pcm_enabled_flag) {
577  TRUE_OR_RETURN(br->ReadBits(4, &sps->pcm_sample_bit_depth_luma_minus1));
578  TRUE_OR_RETURN(br->ReadBits(4, &sps->pcm_sample_bit_depth_chroma_minus1));
579  TRUE_OR_RETURN(
580  br->ReadUE(&sps->log2_min_pcm_luma_coding_block_size_minus3));
581  TRUE_OR_RETURN(
582  br->ReadUE(&sps->log2_diff_max_min_pcm_luma_coding_block_size));
583  TRUE_OR_RETURN(br->ReadBool(&sps->pcm_loop_filter_disabled_flag));
584  }
585 
586  TRUE_OR_RETURN(br->ReadUE(&sps->num_short_term_ref_pic_sets));
587  sps->st_ref_pic_sets.resize(sps->num_short_term_ref_pic_sets);
588  for (int i = 0; i < sps->num_short_term_ref_pic_sets; i++) {
589  OK_OR_RETURN(ParseReferencePictureSet(sps->num_short_term_ref_pic_sets, i,
590  sps->st_ref_pic_sets, br,
591  &sps->st_ref_pic_sets[i]));
592  }
593 
594  TRUE_OR_RETURN(br->ReadBool(&sps->long_term_ref_pic_present_flag));
595  if (sps->long_term_ref_pic_present_flag) {
596  TRUE_OR_RETURN(br->ReadUE(&sps->num_long_term_ref_pics));
597  sps->lt_ref_pic_poc_lsb.resize(sps->num_long_term_ref_pics);
598  sps->used_by_curr_pic_lt_flag.resize(sps->num_long_term_ref_pics);
599  for (int i = 0; i < sps->num_long_term_ref_pics; i++) {
600  TRUE_OR_RETURN(br->ReadBits(sps->log2_max_pic_order_cnt_lsb_minus4 + 4,
601  &sps->lt_ref_pic_poc_lsb[i]));
602  bool temp;
603  TRUE_OR_RETURN(br->ReadBool(&temp));
604  sps->used_by_curr_pic_lt_flag[i] = temp;
605  }
606  }
607 
608  TRUE_OR_RETURN(br->ReadBool(&sps->temporal_mvp_enabled_flag));
609  TRUE_OR_RETURN(br->ReadBool(&sps->strong_intra_smoothing_enabled_flag));
610 
611  TRUE_OR_RETURN(br->ReadBool(&sps->vui_parameters_present));
612  if (sps->vui_parameters_present) {
613  OK_OR_RETURN(ParseVuiParameters(sps->max_sub_layers_minus1, br,
614  &sps->vui_parameters));
615  }
616 
617  // Ignore remaining extension data.
618 
619  // This will replace any existing SPS instance.
620  *sps_id = sps->seq_parameter_set_id;
621  active_spses_[*sps_id] = std::move(sps);
622 
623  return kOk;
624 }
625 
626 const H265Pps* H265Parser::GetPps(int pps_id) {
627  return active_ppses_[pps_id].get();
628 }
629 
630 const H265Sps* H265Parser::GetSps(int sps_id) {
631  return active_spses_[sps_id].get();
632 }
633 
634 H265Parser::Result H265Parser::ParseVuiParameters(int max_num_sub_layers_minus1,
635  H26xBitReader* br,
636  H265VuiParameters* vui) {
637  // Reads whole element but ignores most of it.
638  int ignored;
639 
640  TRUE_OR_RETURN(br->ReadBool(&vui->aspect_ratio_info_present_flag));
641  if (vui->aspect_ratio_info_present_flag) {
642  TRUE_OR_RETURN(br->ReadBits(8, &vui->aspect_ratio_idc));
643  if (vui->aspect_ratio_idc == H265VuiParameters::kExtendedSar) {
644  TRUE_OR_RETURN(br->ReadBits(16, &vui->sar_width));
645  TRUE_OR_RETURN(br->ReadBits(16, &vui->sar_height));
646  }
647  }
648 
649  bool overscan_info_present_flag;
650  TRUE_OR_RETURN(br->ReadBool(&overscan_info_present_flag));
651  if (overscan_info_present_flag) {
652  TRUE_OR_RETURN(br->SkipBits(1)); // overscan_appropriate_flag
653  }
654 
655  bool video_signal_type_present_flag;
656  TRUE_OR_RETURN(br->ReadBool(&video_signal_type_present_flag));
657  if (video_signal_type_present_flag) {
658  TRUE_OR_RETURN(br->SkipBits(3)); // video_format
659  TRUE_OR_RETURN(br->SkipBits(1)); // video_full_range_flag
660 
661  bool colour_description_present_flag;
662  TRUE_OR_RETURN(br->ReadBool(&colour_description_present_flag));
663  if (colour_description_present_flag) {
664  TRUE_OR_RETURN(br->SkipBits(8)); // colour_primaries
665  TRUE_OR_RETURN(br->ReadBits(8, &vui->transfer_characteristics));
666  TRUE_OR_RETURN(br->SkipBits(8)); // matrix_coeffs
667  }
668  }
669 
670  bool chroma_loc_info_present_flag;
671  TRUE_OR_RETURN(br->ReadBool(&chroma_loc_info_present_flag));
672  if (chroma_loc_info_present_flag) {
673  // chroma_sample_log_type_top_field, chroma_sample_log_type_bottom_field
674  TRUE_OR_RETURN(br->ReadUE(&ignored));
675  TRUE_OR_RETURN(br->ReadUE(&ignored));
676  }
677 
678  // neutral_chroma_indication_flag, field_seq_flag,
679  // frame_field_info_present_flag.
680  TRUE_OR_RETURN(br->SkipBits(3));
681 
682  bool default_display_window_flag;
683  TRUE_OR_RETURN(br->ReadBool(&default_display_window_flag));
684  if (default_display_window_flag) {
685  TRUE_OR_RETURN(br->ReadUE(&ignored)); // def_disp_win_left_offset
686  TRUE_OR_RETURN(br->ReadUE(&ignored)); // def_disp_win_right_offset
687  TRUE_OR_RETURN(br->ReadUE(&ignored)); // def_disp_win_top_offset
688  TRUE_OR_RETURN(br->ReadUE(&ignored)); // def_disp_win_bottom_offset
689  }
690 
691  bool vui_timing_info_present_flag;
692  TRUE_OR_RETURN(br->ReadBool(&vui_timing_info_present_flag));
693  if (vui_timing_info_present_flag) {
694  // vui_num_units_in_tick, vui_time_scale
695  TRUE_OR_RETURN(br->SkipBits(32 + 32));
696 
697  bool vui_poc_proportional_to_timing_flag;
698  TRUE_OR_RETURN(br->ReadBool(&vui_poc_proportional_to_timing_flag));
699  if (vui_poc_proportional_to_timing_flag) {
700  // vui_num_ticks_poc_diff_one_minus1
701  TRUE_OR_RETURN(br->ReadUE(&ignored));
702  }
703 
704  bool vui_hdr_parameters_present_flag;
705  TRUE_OR_RETURN(br->ReadBool(&vui_hdr_parameters_present_flag));
706  if (vui_hdr_parameters_present_flag) {
707  OK_OR_RETURN(SkipHrdParameters(max_num_sub_layers_minus1, br));
708  }
709  }
710 
711  TRUE_OR_RETURN(br->ReadBool(&vui->bitstream_restriction_flag));
712  if (vui->bitstream_restriction_flag) {
713  // tiles_fixed_structure_flag, motion_vectors_over_pic_boundaries_flag,
714  // restricted_ref_pic_lists_flag.
715  TRUE_OR_RETURN(br->SkipBits(3));
716 
717  TRUE_OR_RETURN(br->ReadUE(&vui->min_spatial_segmentation_idc));
718  TRUE_OR_RETURN(br->ReadUE(&ignored)); // max_bytes_per_pic_denom
719  TRUE_OR_RETURN(br->ReadUE(&ignored)); // max_bits_per_min_cu_denum
720  TRUE_OR_RETURN(br->ReadUE(&ignored)); // log2_max_mv_length_horizontal
721  TRUE_OR_RETURN(br->ReadUE(&ignored)); // log2_max_mv_length_vertical
722  }
723 
724  return kOk;
725 }
726 
727 H265Parser::Result H265Parser::ParseReferencePictureSet(
728  int num_short_term_ref_pic_sets,
729  int st_rps_idx,
730  const std::vector<H265ReferencePictureSet>& ref_pic_sets,
731  H26xBitReader* br,
732  H265ReferencePictureSet* out_ref_pic_set) {
733  // Parses and processess a short-term reference picture set. This needs to
734  // be done since the size of this element may be dependent on previous
735  // reference picture sets.
736 
737  bool inter_ref_pic_set_prediction = false;
738  if (st_rps_idx != 0) {
739  TRUE_OR_RETURN(br->ReadBool(&inter_ref_pic_set_prediction));
740  }
741 
742  if (inter_ref_pic_set_prediction) {
743  int delta_idx = 1;
744  if (st_rps_idx == num_short_term_ref_pic_sets) {
745  TRUE_OR_RETURN(br->ReadUE(&delta_idx));
746  delta_idx++;
747  TRUE_OR_RETURN(delta_idx <= st_rps_idx);
748  }
749 
750  int ref_rps_idx = st_rps_idx - delta_idx;
751  DCHECK_LE(0, ref_rps_idx);
752  DCHECK_LT(ref_rps_idx, st_rps_idx);
753 
754  bool delta_rps_sign;
755  int abs_delta_rps_minus1;
756  TRUE_OR_RETURN(br->ReadBool(&delta_rps_sign));
757  TRUE_OR_RETURN(br->ReadUE(&abs_delta_rps_minus1));
758  int delta_rps =
759  delta_rps_sign ? -(abs_delta_rps_minus1 + 1) : abs_delta_rps_minus1 + 1;
760 
761  int ref_num_delta_pocs = ref_pic_sets[ref_rps_idx].num_delta_pocs;
762  std::vector<bool> used_by_curr_pic(ref_num_delta_pocs + 1);
763  std::vector<bool> use_delta(ref_num_delta_pocs + 1);
764  for (int j = 0; j <= ref_num_delta_pocs; j++) {
765  bool temp;
766  TRUE_OR_RETURN(br->ReadBool(&temp));
767  used_by_curr_pic[j] = temp;
768 
769  if (!used_by_curr_pic[j]) {
770  TRUE_OR_RETURN(br->ReadBool(&temp));
771  use_delta[j] = temp;
772  } else {
773  use_delta[j] = true;
774  }
775  }
776 
777  int ref_num_positive_pics = ref_pic_sets[ref_rps_idx].num_positive_pics;
778  int ref_num_negative_pics = ref_pic_sets[ref_rps_idx].num_negative_pics;
779  int i;
780 
781  // Update list 0.
782  {
783  i = 0;
784  for (int j = ref_num_positive_pics - 1; j >= 0; j--) {
785  int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s1[j] + delta_rps;
786  if (d_poc < 0 && use_delta[ref_num_negative_pics + j]) {
787  out_ref_pic_set->delta_poc_s0[i] = d_poc;
788  out_ref_pic_set->used_by_curr_pic_s0[i] =
789  used_by_curr_pic[ref_num_negative_pics + j];
790  i++;
791  }
792  }
793  if (delta_rps < 0 && use_delta[ref_num_delta_pocs]) {
794  out_ref_pic_set->delta_poc_s0[i] = delta_rps;
795  out_ref_pic_set->used_by_curr_pic_s0[i] =
796  used_by_curr_pic[ref_num_delta_pocs];
797  i++;
798  }
799  for (int j = 0; j < ref_num_negative_pics; j++) {
800  int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s0[j] + delta_rps;
801  if (d_poc < 0 && use_delta[j]) {
802  out_ref_pic_set->delta_poc_s0[i] = d_poc;
803  out_ref_pic_set->used_by_curr_pic_s0[i] = used_by_curr_pic[j];
804  i++;
805  }
806  }
807  out_ref_pic_set->num_negative_pics = i;
808  }
809 
810  // Update list 1.
811  {
812  i = 0;
813  for (int j = ref_num_negative_pics - 1; j >= 0; j--) {
814  int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s0[j] + delta_rps;
815  if (d_poc > 0 && use_delta[j]) {
816  out_ref_pic_set->delta_poc_s1[i] = d_poc;
817  out_ref_pic_set->used_by_curr_pic_s1[i] = used_by_curr_pic[j];
818  i++;
819  }
820  }
821  if (delta_rps > 0 && use_delta[ref_num_delta_pocs]) {
822  out_ref_pic_set->delta_poc_s1[i] = delta_rps;
823  out_ref_pic_set->used_by_curr_pic_s1[i] =
824  used_by_curr_pic[ref_num_delta_pocs];
825  i++;
826  }
827  for (int j = 0; j < ref_num_positive_pics; j++) {
828  int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s1[j] + delta_rps;
829  if (d_poc > 0 && use_delta[ref_num_negative_pics + j]) {
830  out_ref_pic_set->delta_poc_s1[i] = d_poc;
831  out_ref_pic_set->used_by_curr_pic_s1[i] =
832  used_by_curr_pic[ref_num_negative_pics + j];
833  i++;
834  }
835  }
836  out_ref_pic_set->num_positive_pics = i;
837  }
838  } else {
839  TRUE_OR_RETURN(br->ReadUE(&out_ref_pic_set->num_negative_pics));
840  TRUE_OR_RETURN(out_ref_pic_set->num_negative_pics <= kMaxRefPicSetCount);
841  TRUE_OR_RETURN(br->ReadUE(&out_ref_pic_set->num_positive_pics));
842  TRUE_OR_RETURN(out_ref_pic_set->num_positive_pics <= kMaxRefPicSetCount);
843 
844  int prev_poc = 0;
845  for (int i = 0; i < out_ref_pic_set->num_negative_pics; i++) {
846  int delta_poc_s0_minus1;
847  TRUE_OR_RETURN(br->ReadUE(&delta_poc_s0_minus1));
848  out_ref_pic_set->delta_poc_s0[i] = prev_poc - (delta_poc_s0_minus1 + 1);
849  prev_poc = out_ref_pic_set->delta_poc_s0[i];
850 
851  TRUE_OR_RETURN(br->ReadBool(&out_ref_pic_set->used_by_curr_pic_s0[i]));
852  }
853 
854  prev_poc = 0;
855  for (int i = 0; i < out_ref_pic_set->num_positive_pics; i++) {
856  int delta_poc_s1_minus1;
857  TRUE_OR_RETURN(br->ReadUE(&delta_poc_s1_minus1));
858  out_ref_pic_set->delta_poc_s1[i] = prev_poc + delta_poc_s1_minus1 + 1;
859  prev_poc = out_ref_pic_set->delta_poc_s1[i];
860 
861  TRUE_OR_RETURN(br->ReadBool(&out_ref_pic_set->used_by_curr_pic_s1[i]));
862  }
863  }
864 
865  out_ref_pic_set->num_delta_pocs =
866  out_ref_pic_set->num_positive_pics + out_ref_pic_set->num_negative_pics;
867  return kOk;
868 }
869 
870 H265Parser::Result H265Parser::SkipReferencePictureListModification(
871  const H265SliceHeader& slice_header,
872  const H265Pps& pps,
873  int num_pic_total_curr,
874  H26xBitReader* br) {
875  // Reads whole element but ignores it all.
876 
877  bool ref_pic_list_modification_flag_l0;
878  TRUE_OR_RETURN(br->ReadBool(&ref_pic_list_modification_flag_l0));
879  if (ref_pic_list_modification_flag_l0) {
880  for (int i = 0; i <= slice_header.num_ref_idx_l0_active_minus1; i++) {
881  TRUE_OR_RETURN(br->SkipBits(ceil(log2(num_pic_total_curr))));
882  }
883  }
884 
885  if (slice_header.slice_type == kBSlice) {
886  bool ref_pic_list_modification_flag_l1;
887  TRUE_OR_RETURN(br->ReadBool(&ref_pic_list_modification_flag_l1));
888  if (ref_pic_list_modification_flag_l1) {
889  for (int i = 0; i <= slice_header.num_ref_idx_l1_active_minus1; i++) {
890  TRUE_OR_RETURN(br->SkipBits(ceil(log2(num_pic_total_curr))));
891  }
892  }
893  }
894 
895  return kOk;
896 }
897 
898 H265Parser::Result H265Parser::SkipPredictionWeightTablePart(
899  int num_ref_idx_minus1,
900  int chroma_array_type,
901  H26xBitReader* br) {
902  // Reads whole element, ignores it.
903  int ignored;
904  std::vector<bool> luma_weight_flag(num_ref_idx_minus1 + 1);
905  std::vector<bool> chroma_weight_flag(num_ref_idx_minus1 + 1);
906 
907  for (int i = 0; i <= num_ref_idx_minus1; i++) {
908  bool temp;
909  TRUE_OR_RETURN(br->ReadBool(&temp));
910  luma_weight_flag[i] = temp;
911  }
912  if (chroma_array_type != 0) {
913  for (int i = 0; i <= num_ref_idx_minus1; i++) {
914  bool temp;
915  TRUE_OR_RETURN(br->ReadBool(&temp));
916  chroma_weight_flag[i] = temp;
917  }
918  }
919  for (int i = 0; i <= num_ref_idx_minus1; i++) {
920  if (luma_weight_flag[i]) {
921  TRUE_OR_RETURN(br->ReadSE(&ignored)); // delta_luma_weight_l#
922  TRUE_OR_RETURN(br->ReadSE(&ignored)); // luma_offset_l#
923  }
924  if (chroma_weight_flag[i]) {
925  for (int j = 0; j < 2; j++) {
926  TRUE_OR_RETURN(br->ReadSE(&ignored)); // delta_chroma_weight_l#
927  TRUE_OR_RETURN(br->ReadSE(&ignored)); // delta_chroma_offset_l#
928  }
929  }
930  }
931 
932  return kOk;
933 }
934 
935 H265Parser::Result H265Parser::SkipPredictionWeightTable(
936  bool is_b_slice,
937  const H265Sps& sps,
938  const H265SliceHeader& slice_header,
939  H26xBitReader* br) {
940  // Reads whole element, ignores it.
941  int ignored;
942  int chroma_array_type = sps.GetChromaArrayType();
943 
944  TRUE_OR_RETURN(br->ReadUE(&ignored)); // luma_log2_weight_denom
945  if (chroma_array_type != 0) {
946  TRUE_OR_RETURN(br->ReadSE(&ignored)); // delta_chroma_log2_weight_denom
947  }
948  OK_OR_RETURN(SkipPredictionWeightTablePart(
949  slice_header.num_ref_idx_l0_active_minus1, chroma_array_type, br));
950  if (is_b_slice) {
951  OK_OR_RETURN(SkipPredictionWeightTablePart(
952  slice_header.num_ref_idx_l1_active_minus1, chroma_array_type, br));
953  }
954 
955  return kOk;
956 }
957 
958 H265Parser::Result H265Parser::ReadProfileTierLevel(
959  bool profile_present,
960  int max_num_sub_layers_minus1,
961  H26xBitReader* br,
962  H265Sps* sps) {
963  // Reads whole element, ignores it.
964 
965  if (profile_present) {
966  // 11 bytes of general_profile_tier flags:
967  // general_profile_space, general_tier_flag, general_profile_idc
968  // general_profile_compativility_flag
969  // general_progressive_source_flag
970  // general_interlaced_source_flag
971  // general_non_packed_constraint_flag
972  // general_frame_only_constraint_flag
973  // 44-bits of other flags
974  for (int i = 0; i < 11; i++)
975  TRUE_OR_RETURN(br->ReadBits(8, &sps->general_profile_tier_level_data[i]));
976  }
977  // general_level_idc
978  TRUE_OR_RETURN(br->ReadBits(8, &sps->general_profile_tier_level_data[11]));
979 
980  std::vector<bool> sub_layer_profile_present(max_num_sub_layers_minus1);
981  std::vector<bool> sub_layer_level_present(max_num_sub_layers_minus1);
982  for (int i = 0; i < max_num_sub_layers_minus1; i++) {
983  bool profile, level;
984  TRUE_OR_RETURN(br->ReadBool(&profile));
985  TRUE_OR_RETURN(br->ReadBool(&level));
986  sub_layer_profile_present[i] = profile;
987  sub_layer_level_present[i] = level;
988  }
989 
990  if (max_num_sub_layers_minus1 > 0) {
991  for (int i = max_num_sub_layers_minus1; i < 8; i++)
992  TRUE_OR_RETURN(br->SkipBits(2)); // reserved_zero_2bits
993  }
994 
995  for (int i = 0; i < max_num_sub_layers_minus1; i++) {
996  if (sub_layer_profile_present[i]) {
997  // sub_layer_profile_space, sub_layer_tier_flag, sub_layer_profile_idc
998  // sub_layer_profile_compatibility
999  // sub_layer_reserved_zero_43bits
1000  // sub_layer_reserved_zero_bit
1001  TRUE_OR_RETURN(br->SkipBits(2 + 1 + 5 + 32 + 4 + 43 + 1));
1002  }
1003  if (sub_layer_level_present[i]) {
1004  TRUE_OR_RETURN(br->SkipBits(8));
1005  }
1006  }
1007 
1008  return kOk;
1009 }
1010 
1011 H265Parser::Result H265Parser::SkipScalingListData(H26xBitReader* br) {
1012  // Reads whole element, ignores it.
1013  int ignored;
1014  for (int size_id = 0; size_id < 4; size_id++) {
1015  for (int matrix_id = 0; matrix_id < 6;
1016  matrix_id += ((size_id == 3) ? 3 : 1)) {
1017  bool scaling_list_pred_mode;
1018  TRUE_OR_RETURN(br->ReadBool(&scaling_list_pred_mode));
1019  if (!scaling_list_pred_mode) {
1020  // scaling_list_pred_matrix_id_delta
1021  TRUE_OR_RETURN(br->ReadUE(&ignored));
1022  } else {
1023  int coefNum = std::min(64, (1 << (4 + (size_id << 1))));
1024  if (size_id > 1) {
1025  TRUE_OR_RETURN(br->ReadSE(&ignored)); // scaling_list_dc_coef_minus8
1026  }
1027 
1028  for (int i = 0; i < coefNum; i++) {
1029  TRUE_OR_RETURN(br->ReadSE(&ignored)); // scaling_list_delta_coef
1030  }
1031  }
1032  }
1033  }
1034 
1035  return kOk;
1036 }
1037 
1038 H265Parser::Result H265Parser::SkipHrdParameters(int max_num_sub_layers_minus1,
1039  H26xBitReader* br) {
1040  // common_inf_present_flag is always 1 when parsing vui_parameters.
1041  const bool common_inf_present_flag = true;
1042 
1043  int ignored;
1044  bool nal_hdr_parameters_present_flag;
1045  bool vcl_hdr_parameters_present_flag;
1046  bool sub_pic_hdr_params_present_flag = false;
1047  if (common_inf_present_flag) {
1048  TRUE_OR_RETURN(br->ReadBool(&nal_hdr_parameters_present_flag));
1049  TRUE_OR_RETURN(br->ReadBool(&vcl_hdr_parameters_present_flag));
1050  if (nal_hdr_parameters_present_flag || vcl_hdr_parameters_present_flag) {
1051  TRUE_OR_RETURN(br->ReadBool(&sub_pic_hdr_params_present_flag));
1052  if (sub_pic_hdr_params_present_flag) {
1053  // tick_divisor_minus2, du_cpb_removal_delay_increment_length_minus1,
1054  // sub_pic_cpb_params_in_pic_timing_sei_flag
1055  // dpb_output_delay_du_length_minus1
1056  TRUE_OR_RETURN(br->SkipBits(8 + 5 + 1 + 5));
1057  }
1058 
1059  // bit_rate_scale, cpb_size_scale
1060  TRUE_OR_RETURN(br->SkipBits(4 + 4));
1061  if (sub_pic_hdr_params_present_flag)
1062  TRUE_OR_RETURN(br->SkipBits(4)); // cpb_size_du_scale
1063 
1064  // initial_cpb_removal_delay_length_minus1,
1065  // au_cpb_removal_delay_length_minus1, dpb_output_delay_length_minus1
1066  TRUE_OR_RETURN(br->SkipBits(5 + 5 + 5));
1067  }
1068  }
1069 
1070  for (int i = 0; i <= max_num_sub_layers_minus1; i++) {
1071  bool fixed_pic_rate_general_flag;
1072  bool fixed_pic_rate_within_cvs_flag = true;
1073  bool low_delay_hdr_flag = false;
1074  int cpb_cnt_minus1 = 0;
1075  TRUE_OR_RETURN(br->ReadBool(&fixed_pic_rate_general_flag));
1076  if (!fixed_pic_rate_general_flag)
1077  TRUE_OR_RETURN(br->ReadBool(&fixed_pic_rate_within_cvs_flag));
1078  if (fixed_pic_rate_within_cvs_flag)
1079  TRUE_OR_RETURN(br->ReadUE(&ignored)); // elemental_duration_ic_tc_minus1
1080  else
1081  TRUE_OR_RETURN(br->ReadBool(&low_delay_hdr_flag));
1082  if (!low_delay_hdr_flag)
1083  TRUE_OR_RETURN(br->ReadUE(&cpb_cnt_minus1));
1084 
1085  if (nal_hdr_parameters_present_flag) {
1086  OK_OR_RETURN(SkipSubLayerHrdParameters(
1087  cpb_cnt_minus1, sub_pic_hdr_params_present_flag, br));
1088  }
1089  if (vcl_hdr_parameters_present_flag) {
1090  OK_OR_RETURN(SkipSubLayerHrdParameters(
1091  cpb_cnt_minus1, sub_pic_hdr_params_present_flag, br));
1092  }
1093  }
1094 
1095  return kOk;
1096 }
1097 
1098 H265Parser::Result H265Parser::SkipSubLayerHrdParameters(
1099  int cpb_cnt_minus1,
1100  bool sub_pic_hdr_params_present_flag,
1101  H26xBitReader* br) {
1102  int ignored;
1103  for (int i = 0; i <= cpb_cnt_minus1; i++) {
1104  TRUE_OR_RETURN(br->ReadUE(&ignored)); // bit_rate_value_minus1
1105  TRUE_OR_RETURN(br->ReadUE(&ignored)); // cpb_size_value_minus1
1106  if (sub_pic_hdr_params_present_flag) {
1107  TRUE_OR_RETURN(br->ReadUE(&ignored)); // cpb_size_du_value_minus1
1108  TRUE_OR_RETURN(br->ReadUE(&ignored)); // bit_rate_du_value_minus1
1109  }
1110 
1111  TRUE_OR_RETURN(br->SkipBits(1)); // cbr_flag
1112  }
1113 
1114  return kOk;
1115 }
1116 
1117 H265Parser::Result H265Parser::ByteAlignment(H26xBitReader* br) {
1118  TRUE_OR_RETURN(br->SkipBits(1));
1119  TRUE_OR_RETURN(br->SkipBits(br->NumBitsLeft() % 8));
1120  return kOk;
1121 }
1122 
1123 } // namespace media
1124 } // namespace shaka
Result ParseSps(const Nalu &nalu, int *sps_id)
Definition: h265_parser.cc:509
Result ParsePps(const Nalu &nalu, int *pps_id)
Definition: h265_parser.cc:401
const H265Sps * GetSps(int sps_id)
Definition: h265_parser.cc:630
Result ParseSliceHeader(const Nalu &nalu, H265SliceHeader *slice_header)
Definition: h265_parser.cc:183
const H265Pps * GetPps(int pps_id)
Definition: h265_parser.cc:626
const uint8_t * data() const
This is the pointer to the Nalu data, pointing to the header.
Definition: nalu_reader.h:97
bool is_video_slice() const
Slice data partition NALs are not considered as slice NALs.
Definition: nalu_reader.h:117
int type() const
Definition: nalu_reader.h:113
uint64_t header_size() const
The size of the header, e.g. 1 for H.264.
Definition: nalu_reader.h:100
uint64_t payload_size() const
Size of this Nalu minus header_size().
Definition: nalu_reader.h:102
All the methods that are virtual are virtual for mocking.