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