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