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