DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs 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/filters/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 edash_packager {
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 } // namespace
55 
56 H265Pps::H265Pps() {}
57 H265Pps::~H265Pps() {}
58 
59 H265Sps::H265Sps() {}
60 H265Sps::~H265Sps() {}
61 
62 int H265Sps::GetPicSizeInCtbsY() const {
63  int min_cb_log2_size_y = log2_min_luma_coding_block_size_minus3 + 3;
64  int ctb_log2_size_y =
65  min_cb_log2_size_y + log2_diff_max_min_luma_coding_block_size;
66  int ctb_size_y = 1 << ctb_log2_size_y;
67 
68  // Round-up division.
69  int pic_width_in_ctbs_y = (pic_width_in_luma_samples - 1) / ctb_size_y + 1;
70  int pic_height_in_ctbs_y = (pic_height_in_luma_samples - 1) / ctb_size_y + 1;
71  return pic_width_in_ctbs_y * pic_height_in_ctbs_y;
72 }
73 
74 int H265Sps::GetChromaArrayType() const {
75  if (!separate_colour_plane_flag)
76  return chroma_format_idc;
77  else
78  return 0;
79 }
80 
81 H265ReferencePictureListModifications::H265ReferencePictureListModifications() {
82 }
83 H265ReferencePictureListModifications::
84  ~H265ReferencePictureListModifications() {}
85 
86 H265SliceHeader::H265SliceHeader() {}
87 H265SliceHeader::~H265SliceHeader() {}
88 
89 H265Parser::H265Parser() {}
90 H265Parser::~H265Parser() {
91  STLDeleteValues(&active_spses_);
92  STLDeleteValues(&active_ppses_);
93 }
94 
95 H265Parser::Result H265Parser::ParseSliceHeader(const Nalu& nalu,
96  H265SliceHeader* slice_header) {
97  DCHECK(nalu.is_video_slice());
98  *slice_header = H265SliceHeader();
99 
100  // Parses whole element.
101  H26xBitReader reader;
102  reader.Initialize(nalu.data() + nalu.header_size(), nalu.payload_size());
103  H26xBitReader* br = &reader;
104 
105  TRUE_OR_RETURN(br->ReadBool(&slice_header->first_slice_segment_in_pic_flag));
106  if (nalu.type() >= Nalu::H265_BLA_W_LP &&
107  nalu.type() <= Nalu::H265_RSV_IRAP_VCL23) {
108  TRUE_OR_RETURN(br->ReadBool(&slice_header->no_output_of_prior_pics_flag));
109  }
110 
111  TRUE_OR_RETURN(br->ReadUE(&slice_header->pic_parameter_set_id));
112  const H265Pps* pps = GetPps(slice_header->pic_parameter_set_id);
113  TRUE_OR_RETURN(pps);
114 
115  const H265Sps* sps = GetSps(pps->seq_parameter_set_id);
116  TRUE_OR_RETURN(sps);
117 
118  if (!slice_header->first_slice_segment_in_pic_flag) {
119  if (pps->dependent_slice_segments_enabled_flag) {
120  TRUE_OR_RETURN(br->ReadBool(&slice_header->dependent_slice_segment_flag));
121  }
122  const int bit_length = ceil(log2(sps->GetPicSizeInCtbsY()));
123  TRUE_OR_RETURN(br->ReadBits(bit_length, &slice_header->segment_address));
124  }
125 
126  if (!slice_header->dependent_slice_segment_flag) {
127  TRUE_OR_RETURN(br->SkipBits(pps->num_extra_slice_header_bits));
128  TRUE_OR_RETURN(br->ReadUE(&slice_header->slice_type));
129  if (pps->output_flag_present_flag) {
130  TRUE_OR_RETURN(br->ReadBool(&slice_header->pic_output_flag));
131  }
132  if (sps->separate_colour_plane_flag) {
133  TRUE_OR_RETURN(br->ReadBits(2, &slice_header->colour_plane_id));
134  }
135 
136  if (nalu.type() != Nalu::H265_IDR_W_RADL &&
137  nalu.type() != Nalu::H265_IDR_N_LP) {
138  TRUE_OR_RETURN(br->ReadBits(sps->log2_max_pic_order_cnt_lsb_minus4 + 4,
139  &slice_header->slice_pic_order_cnt_lsb));
140 
141  TRUE_OR_RETURN(
142  br->ReadBool(&slice_header->short_term_ref_pic_set_sps_flag));
143  if (!slice_header->short_term_ref_pic_set_sps_flag) {
144  OK_OR_RETURN(ParseReferencePictureSet(
145  sps->num_short_term_ref_pic_sets, sps->num_short_term_ref_pic_sets,
146  sps->st_ref_pic_sets, br, &slice_header->st_ref_pic_set));
147  } else if (sps->num_short_term_ref_pic_sets > 1) {
148  TRUE_OR_RETURN(
149  br->ReadBits(ceil(log2(sps->num_short_term_ref_pic_sets)),
150  &slice_header->short_term_ref_pic_set_idx));
151  }
152 
153  if (sps->long_term_ref_pic_present_flag) {
154  if (sps->num_long_term_ref_pics > 0) {
155  TRUE_OR_RETURN(br->ReadUE(&slice_header->num_long_term_sps));
156  }
157  TRUE_OR_RETURN(br->ReadUE(&slice_header->num_long_term_pics));
158 
159  const int pic_count =
160  slice_header->num_long_term_sps + slice_header->num_long_term_pics;
161  slice_header->long_term_pics_info.resize(pic_count);
162  for (int i = 0; i < pic_count; i++) {
163  if (i < slice_header->num_long_term_sps) {
164  int lt_idx_sps = 0;
165  if (sps->num_long_term_ref_pics > 1) {
166  TRUE_OR_RETURN(br->ReadBits(
167  ceil(log2(sps->num_long_term_ref_pics)), &lt_idx_sps));
168  }
169  if (sps->used_by_curr_pic_lt_flag[lt_idx_sps])
170  slice_header->used_by_curr_pic_lt++;
171  } else {
172  TRUE_OR_RETURN(br->SkipBits(sps->log2_max_pic_order_cnt_lsb_minus4 +
173  4)); // poc_lsb_lt
174  bool used_by_curr_pic_lt_flag;
175  TRUE_OR_RETURN(br->ReadBool(&used_by_curr_pic_lt_flag));
176  if (used_by_curr_pic_lt_flag)
177  slice_header->used_by_curr_pic_lt++;
178  }
179  TRUE_OR_RETURN(br->ReadBool(&slice_header->long_term_pics_info[i]
180  .delta_poc_msb_present_flag));
181  if (slice_header->long_term_pics_info[i].delta_poc_msb_present_flag) {
182  TRUE_OR_RETURN(br->ReadUE(
183  &slice_header->long_term_pics_info[i].delta_poc_msb_cycle_lt));
184  }
185  }
186  }
187 
188  if (sps->temporal_mvp_enabled_flag) {
189  TRUE_OR_RETURN(
190  br->ReadBool(&slice_header->slice_temporal_mvp_enabled_flag));
191  }
192  }
193 
194  if (nalu.nuh_layer_id() != 0) {
195  NOTIMPLEMENTED() << "Multi-layer streams are not supported.";
196  return kUnsupportedStream;
197  }
198 
199  if (sps->sample_adaptive_offset_enabled_flag) {
200  TRUE_OR_RETURN(br->ReadBool(&slice_header->slice_sao_luma_flag));
201  if (sps->GetChromaArrayType() != 0) {
202  TRUE_OR_RETURN(br->ReadBool(&slice_header->slice_sao_chroma_flag));
203  }
204  }
205 
206  slice_header->num_ref_idx_l0_active_minus1 =
207  pps->num_ref_idx_l0_default_active_minus1;
208  slice_header->num_ref_idx_l1_active_minus1 =
209  pps->num_ref_idx_l1_default_active_minus1;
210  if (slice_header->slice_type == kPSlice ||
211  slice_header->slice_type == kBSlice) {
212  TRUE_OR_RETURN(
213  br->ReadBool(&slice_header->num_ref_idx_active_override_flag));
214  if (slice_header->num_ref_idx_active_override_flag) {
215  TRUE_OR_RETURN(br->ReadUE(&slice_header->num_ref_idx_l0_active_minus1));
216  if (slice_header->slice_type == kBSlice) {
217  TRUE_OR_RETURN(
218  br->ReadUE(&slice_header->num_ref_idx_l1_active_minus1));
219  }
220  }
221 
222  const int num_pic_total_curr = GetNumPicTotalCurr(*slice_header, *sps);
223  if (pps->lists_modification_present_flag && num_pic_total_curr > 1) {
224  OK_OR_RETURN(SkipReferencePictureListModification(
225  *slice_header, *pps, num_pic_total_curr, br));
226  }
227 
228  if (slice_header->slice_type == kBSlice) {
229  TRUE_OR_RETURN(br->ReadBool(&slice_header->mvd_l1_zero_flag));
230  }
231  if (pps->cabac_init_present_flag) {
232  TRUE_OR_RETURN(br->ReadBool(&slice_header->cabac_init_flag));
233  }
234  if (slice_header->slice_temporal_mvp_enabled_flag) {
235  if (slice_header->slice_type == kBSlice) {
236  TRUE_OR_RETURN(br->ReadBool(&slice_header->collocated_from_l0));
237  }
238  bool l0_greater_than_0 = slice_header->num_ref_idx_l0_active_minus1 > 0;
239  bool l1_greater_than_0 = slice_header->num_ref_idx_l1_active_minus1 > 0;
240  if (slice_header->collocated_from_l0 ? l0_greater_than_0
241  : l1_greater_than_0) {
242  TRUE_OR_RETURN(br->ReadUE(&slice_header->collocated_ref_idx));
243  }
244  }
245 
246  if ((pps->weighted_pred_flag && slice_header->slice_type == kPSlice) ||
247  (pps->weighted_bipred_flag && slice_header->slice_type == kBSlice)) {
248  OK_OR_RETURN(SkipPredictionWeightTable(
249  slice_header->slice_type == kBSlice, *sps, *slice_header, br));
250  }
251  TRUE_OR_RETURN(br->ReadUE(&slice_header->five_minus_max_num_merge_cand));
252  }
253 
254  TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_qp_delta));
255  if (pps->slice_chroma_qp_offsets_present_flag) {
256  TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_cb_qp_offset));
257  TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_cr_qp_offset));
258  }
259 
260  if (pps->chroma_qp_offset_list_enabled_flag) {
261  TRUE_OR_RETURN(
262  br->ReadBool(&slice_header->cu_chroma_qp_offset_enabled_flag));
263  }
264  if (pps->deblocking_filter_override_enabled_flag) {
265  TRUE_OR_RETURN(
266  br->ReadBool(&slice_header->deblocking_filter_override_flag));
267  }
268  if (slice_header->deblocking_filter_override_flag) {
269  TRUE_OR_RETURN(
270  br->ReadBool(&slice_header->slice_deblocking_filter_disabled_flag));
271  if (!slice_header->slice_deblocking_filter_disabled_flag) {
272  TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_beta_offset_div2));
273  TRUE_OR_RETURN(br->ReadSE(&slice_header->slice_tc_offset_div2));
274  }
275  }
276  if (pps->loop_filter_across_slices_enabled_flag &&
277  (slice_header->slice_sao_luma_flag ||
278  slice_header->slice_sao_chroma_flag ||
279  !slice_header->slice_deblocking_filter_disabled_flag)) {
280  TRUE_OR_RETURN(br->ReadBool(
281  &slice_header->slice_loop_filter_across_slices_enabled_flag));
282  }
283  }
284 
285  if (pps->tiles_enabled_flag || pps->entropy_coding_sync_enabled_flag) {
286  TRUE_OR_RETURN(br->ReadUE(&slice_header->num_entry_point_offsets));
287  if (slice_header->num_entry_point_offsets > 0) {
288  TRUE_OR_RETURN(br->ReadUE(&slice_header->offset_len_minus1));
289  slice_header->entry_point_offset_minus1.resize(
290  slice_header->num_entry_point_offsets);
291  for (int i = 0; i < slice_header->num_entry_point_offsets; i++) {
292  TRUE_OR_RETURN(
293  br->ReadBits(slice_header->offset_len_minus1 + 1,
294  &slice_header->entry_point_offset_minus1[i]));
295  }
296  }
297  }
298 
299  if (pps->slice_segment_header_extension_present_flag) {
300  int extension_length;
301  TRUE_OR_RETURN(br->ReadUE(&extension_length));
302  TRUE_OR_RETURN(br->SkipBits(extension_length * 8));
303  }
304 
305  size_t epb = br->NumEmulationPreventionBytesRead();
306  slice_header->header_bit_size =
307  (nalu.payload_size() - epb) * 8 - br->NumBitsLeft();
308 
309  return kOk;
310 }
311 
312 H265Parser::Result H265Parser::ParsePps(const Nalu& nalu, int* pps_id) {
313  DCHECK_EQ(Nalu::H265_PPS, nalu.type());
314 
315  // Reads most of the element, not reading the extension data.
316  H26xBitReader reader;
317  reader.Initialize(nalu.data() + nalu.header_size(), nalu.payload_size());
318  H26xBitReader* br = &reader;
319 
320  *pps_id = -1;
321  scoped_ptr<H265Pps> pps(new H265Pps);
322 
323  TRUE_OR_RETURN(br->ReadUE(&pps->pic_parameter_set_id));
324  TRUE_OR_RETURN(br->ReadUE(&pps->seq_parameter_set_id));
325 
326  TRUE_OR_RETURN(br->ReadBool(&pps->dependent_slice_segments_enabled_flag));
327  TRUE_OR_RETURN(br->ReadBool(&pps->output_flag_present_flag));
328  TRUE_OR_RETURN(br->ReadBits(3, &pps->num_extra_slice_header_bits));
329  TRUE_OR_RETURN(br->ReadBool(&pps->sign_data_hiding_enabled_flag));
330  TRUE_OR_RETURN(br->ReadBool(&pps->cabac_init_present_flag));
331 
332  TRUE_OR_RETURN(br->ReadUE(&pps->num_ref_idx_l0_default_active_minus1));
333  TRUE_OR_RETURN(br->ReadUE(&pps->num_ref_idx_l1_default_active_minus1));
334  TRUE_OR_RETURN(br->ReadSE(&pps->init_qp_minus26));
335  TRUE_OR_RETURN(br->ReadBool(&pps->constrained_intra_pred_flag));
336  TRUE_OR_RETURN(br->ReadBool(&pps->transform_skip_enabled_flag));
337 
338  TRUE_OR_RETURN(br->ReadBool(&pps->cu_qp_delta_enabled_flag));
339  if (pps->cu_qp_delta_enabled_flag)
340  TRUE_OR_RETURN(br->ReadUE(&pps->diff_cu_qp_delta_depth));
341  TRUE_OR_RETURN(br->ReadSE(&pps->cb_qp_offset));
342  TRUE_OR_RETURN(br->ReadSE(&pps->cr_qp_offset));
343 
344  TRUE_OR_RETURN(br->ReadBool(&pps->slice_chroma_qp_offsets_present_flag));
345  TRUE_OR_RETURN(br->ReadBool(&pps->weighted_pred_flag));
346  TRUE_OR_RETURN(br->ReadBool(&pps->weighted_bipred_flag));
347  TRUE_OR_RETURN(br->ReadBool(&pps->transquant_bypass_enabled_flag));
348  TRUE_OR_RETURN(br->ReadBool(&pps->tiles_enabled_flag));
349  TRUE_OR_RETURN(br->ReadBool(&pps->entropy_coding_sync_enabled_flag));
350 
351  if (pps->tiles_enabled_flag) {
352  TRUE_OR_RETURN(br->ReadUE(&pps->num_tile_columns_minus1));
353  TRUE_OR_RETURN(br->ReadUE(&pps->num_tile_rows_minus1));
354  TRUE_OR_RETURN(br->ReadBool(&pps->uniform_spacing_flag));
355  if (!pps->uniform_spacing_flag) {
356  pps->column_width_minus1.resize(pps->num_tile_columns_minus1);
357  for (int i = 0; i < pps->num_tile_columns_minus1; i++) {
358  TRUE_OR_RETURN(br->ReadUE(&pps->column_width_minus1[i]));
359  }
360  pps->row_height_minus1.resize(pps->num_tile_rows_minus1);
361  for (int i = 0; i < pps->num_tile_rows_minus1; i++) {
362  TRUE_OR_RETURN(br->ReadUE(&pps->row_height_minus1[i]));
363  }
364  }
365  TRUE_OR_RETURN(br->ReadBool(&pps->loop_filter_across_tiles_enabled_flag));
366  }
367 
368  TRUE_OR_RETURN(br->ReadBool(&pps->loop_filter_across_slices_enabled_flag));
369  TRUE_OR_RETURN(br->ReadBool(&pps->deblocking_filter_control_present_flag));
370  if (pps->deblocking_filter_control_present_flag) {
371  TRUE_OR_RETURN(br->ReadBool(&pps->deblocking_filter_override_enabled_flag));
372  TRUE_OR_RETURN(br->ReadBool(&pps->deblocking_filter_disabled_flag));
373  if (!pps->deblocking_filter_disabled_flag) {
374  TRUE_OR_RETURN(br->ReadSE(&pps->beta_offset_div2));
375  TRUE_OR_RETURN(br->ReadSE(&pps->tc_offset_div2));
376  }
377  }
378 
379  TRUE_OR_RETURN(br->ReadBool(&pps->scaling_list_data_present_flag));
380  if (pps->scaling_list_data_present_flag) {
381  OK_OR_RETURN(SkipScalingListData(br));
382  }
383 
384  TRUE_OR_RETURN(br->ReadBool(&pps->lists_modification_present_flag));
385  TRUE_OR_RETURN(br->ReadUE(&pps->log2_parallel_merge_level_minus2));
386 
387  TRUE_OR_RETURN(
388  br->ReadBool(&pps->slice_segment_header_extension_present_flag));
389 
390  bool pps_extension_present_flag;
391  bool pps_range_extension_flag = false;
392  TRUE_OR_RETURN(br->ReadBool(&pps_extension_present_flag));
393  if (pps_extension_present_flag) {
394  TRUE_OR_RETURN(br->ReadBool(&pps_range_extension_flag));
395  // pps_multilayer_extension_flag, pps_3d_extension_flag, pps_extension_5bits
396  TRUE_OR_RETURN(br->SkipBits(1 + 1 + 5));
397  }
398 
399  if (pps_range_extension_flag) {
400  if (pps->transform_skip_enabled_flag) {
401  // log2_max_transform_skip_block_size_minus2
402  int ignored;
403  TRUE_OR_RETURN(br->ReadUE(&ignored));
404  }
405 
406  TRUE_OR_RETURN(br->SkipBits(1)); // cross_component_prediction_enabled_flag
407  TRUE_OR_RETURN(br->ReadBool(&pps->chroma_qp_offset_list_enabled_flag));
408  // Incomplete
409  }
410 
411  // Ignore remaining extension data.
412 
413  // This will replace any existing PPS instance.
414  *pps_id = pps->pic_parameter_set_id;
415  delete active_ppses_[*pps_id];
416  active_ppses_[*pps_id] = pps.release();
417 
418  return kOk;
419 }
420 
421 H265Parser::Result H265Parser::ParseSps(const Nalu& nalu, int* sps_id) {
422  DCHECK_EQ(Nalu::H265_SPS, nalu.type());
423 
424  // Reads most of the element, not reading the extension data.
425  H26xBitReader reader;
426  reader.Initialize(nalu.data() + nalu.header_size(), nalu.payload_size());
427  H26xBitReader* br = &reader;
428 
429  *sps_id = -1;
430 
431  scoped_ptr<H265Sps> sps(new H265Sps);
432 
433  TRUE_OR_RETURN(br->ReadBits(4, &sps->video_parameter_set_id));
434  TRUE_OR_RETURN(br->ReadBits(3, &sps->max_sub_layers_minus1));
435  TRUE_OR_RETURN(br->ReadBool(&sps->temporal_id_nesting_flag));
436 
437  OK_OR_RETURN(SkipProfileTierLevel(true, sps->max_sub_layers_minus1, br));
438 
439  TRUE_OR_RETURN(br->ReadUE(&sps->seq_parameter_set_id));
440  TRUE_OR_RETURN(br->ReadUE(&sps->chroma_format_idc));
441  if (sps->chroma_format_idc == 3) {
442  TRUE_OR_RETURN(br->ReadBool(&sps->separate_colour_plane_flag));
443  }
444  TRUE_OR_RETURN(br->ReadUE(&sps->pic_width_in_luma_samples));
445  TRUE_OR_RETURN(br->ReadUE(&sps->pic_height_in_luma_samples));
446 
447  TRUE_OR_RETURN(br->ReadBool(&sps->conformance_window_flag));
448  if (sps->conformance_window_flag) {
449  TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_left_offset));
450  TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_right_offset));
451  TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_top_offset));
452  TRUE_OR_RETURN(br->ReadUE(&sps->conf_win_bottom_offset));
453  }
454 
455  TRUE_OR_RETURN(br->ReadUE(&sps->bit_depth_luma_minus8));
456  TRUE_OR_RETURN(br->ReadUE(&sps->bit_depth_chroma_minus8));
457  TRUE_OR_RETURN(br->ReadUE(&sps->log2_max_pic_order_cnt_lsb_minus4));
458 
459  TRUE_OR_RETURN(br->ReadBool(&sps->sub_layer_ordering_info_present_flag));
460  int start = sps->sub_layer_ordering_info_present_flag
461  ? 0
462  : sps->max_sub_layers_minus1;
463  for (int i = start; i <= sps->max_sub_layers_minus1; i++) {
464  TRUE_OR_RETURN(br->ReadUE(&sps->max_dec_pic_buffering_minus1[i]));
465  TRUE_OR_RETURN(br->ReadUE(&sps->max_num_reorder_pics[i]));
466  TRUE_OR_RETURN(br->ReadUE(&sps->max_latency_increase_plus1[i]));
467  }
468 
469  TRUE_OR_RETURN(br->ReadUE(&sps->log2_min_luma_coding_block_size_minus3));
470  TRUE_OR_RETURN(br->ReadUE(&sps->log2_diff_max_min_luma_coding_block_size));
471  TRUE_OR_RETURN(br->ReadUE(&sps->log2_min_luma_transform_block_size_minus2));
472  TRUE_OR_RETURN(br->ReadUE(&sps->log2_diff_max_min_luma_transform_block_size));
473  TRUE_OR_RETURN(br->ReadUE(&sps->max_transform_hierarchy_depth_inter));
474  TRUE_OR_RETURN(br->ReadUE(&sps->max_transform_hierarchy_depth_intra));
475 
476  TRUE_OR_RETURN(br->ReadBool(&sps->scaling_list_enabled_flag));
477  if (sps->scaling_list_enabled_flag) {
478  TRUE_OR_RETURN(br->ReadBool(&sps->scaling_list_data_present_flag));
479  if (sps->scaling_list_data_present_flag) {
480  OK_OR_RETURN(SkipScalingListData(br));
481  }
482  }
483 
484  TRUE_OR_RETURN(br->ReadBool(&sps->amp_enabled_flag));
485  TRUE_OR_RETURN(br->ReadBool(&sps->sample_adaptive_offset_enabled_flag));
486  TRUE_OR_RETURN(br->ReadBool(&sps->pcm_enabled_flag));
487  if (sps->pcm_enabled_flag) {
488  TRUE_OR_RETURN(br->ReadBits(4, &sps->pcm_sample_bit_depth_luma_minus1));
489  TRUE_OR_RETURN(br->ReadBits(4, &sps->pcm_sample_bit_depth_chroma_minus1));
490  TRUE_OR_RETURN(
491  br->ReadUE(&sps->log2_min_pcm_luma_coding_block_size_minus3));
492  TRUE_OR_RETURN(
493  br->ReadUE(&sps->log2_diff_max_min_pcm_luma_coding_block_size));
494  TRUE_OR_RETURN(br->ReadBool(&sps->pcm_loop_filter_disabled_flag));
495  }
496 
497  TRUE_OR_RETURN(br->ReadUE(&sps->num_short_term_ref_pic_sets));
498  sps->st_ref_pic_sets.resize(sps->num_short_term_ref_pic_sets);
499  for (int i = 0; i < sps->num_short_term_ref_pic_sets; i++) {
500  OK_OR_RETURN(ParseReferencePictureSet(sps->num_short_term_ref_pic_sets, i,
501  sps->st_ref_pic_sets, br,
502  &sps->st_ref_pic_sets[i]));
503  }
504 
505  TRUE_OR_RETURN(br->ReadBool(&sps->long_term_ref_pic_present_flag));
506  if (sps->long_term_ref_pic_present_flag) {
507  TRUE_OR_RETURN(br->ReadUE(&sps->num_long_term_ref_pics));
508  sps->lt_ref_pic_poc_lsb.resize(sps->num_long_term_ref_pics);
509  sps->used_by_curr_pic_lt_flag.resize(sps->num_long_term_ref_pics);
510  for (int i = 0; i < sps->num_long_term_ref_pics; i++) {
511  TRUE_OR_RETURN(br->ReadBits(sps->log2_max_pic_order_cnt_lsb_minus4 + 4,
512  &sps->lt_ref_pic_poc_lsb[i]));
513  bool temp;
514  TRUE_OR_RETURN(br->ReadBool(&temp));
515  sps->used_by_curr_pic_lt_flag[i] = temp;
516  }
517  }
518 
519  TRUE_OR_RETURN(br->ReadBool(&sps->temporal_mvp_enabled_flag));
520  TRUE_OR_RETURN(br->ReadBool(&sps->strong_intra_smoothing_enabled_flag));
521 
522  // Ignore remaining extension data.
523 
524  // This will replace any existing SPS instance.
525  *sps_id = sps->seq_parameter_set_id;
526  delete active_spses_[*sps_id];
527  active_spses_[*sps_id] = sps.release();
528 
529  return kOk;
530 }
531 
532 const H265Pps* H265Parser::GetPps(int pps_id) {
533  return active_ppses_[pps_id];
534 }
535 
536 const H265Sps* H265Parser::GetSps(int sps_id) {
537  return active_spses_[sps_id];
538 }
539 
540 H265Parser::Result H265Parser::ParseReferencePictureSet(
541  int num_short_term_ref_pic_sets,
542  int st_rps_idx,
543  const std::vector<H265ReferencePictureSet>& ref_pic_sets,
544  H26xBitReader* br,
545  H265ReferencePictureSet* out_ref_pic_set) {
546  // Parses and processess a short-term reference picture set. This needs to
547  // be done since the size of this element may be dependent on previous
548  // reference picture sets.
549 
550  bool inter_ref_pic_set_prediction = false;
551  if (st_rps_idx != 0) {
552  TRUE_OR_RETURN(br->ReadBool(&inter_ref_pic_set_prediction));
553  }
554 
555  if (inter_ref_pic_set_prediction) {
556  int delta_idx = 1;
557  if (st_rps_idx == num_short_term_ref_pic_sets) {
558  TRUE_OR_RETURN(br->ReadUE(&delta_idx));
559  delta_idx++;
560  TRUE_OR_RETURN(delta_idx <= st_rps_idx);
561  }
562 
563  int ref_rps_idx = st_rps_idx - delta_idx;
564  DCHECK_LE(0, ref_rps_idx);
565  DCHECK_LT(ref_rps_idx, st_rps_idx);
566 
567  bool delta_rps_sign;
568  int abs_delta_rps_minus1;
569  TRUE_OR_RETURN(br->ReadBool(&delta_rps_sign));
570  TRUE_OR_RETURN(br->ReadUE(&abs_delta_rps_minus1));
571  int delta_rps =
572  delta_rps_sign ? -(abs_delta_rps_minus1 + 1) : abs_delta_rps_minus1 + 1;
573 
574  int ref_num_delta_pocs = ref_pic_sets[ref_rps_idx].num_delta_pocs;
575  std::vector<bool> used_by_curr_pic(ref_num_delta_pocs + 1);
576  std::vector<bool> use_delta(ref_num_delta_pocs + 1);
577  for (int j = 0; j <= ref_num_delta_pocs; j++) {
578  bool temp;
579  TRUE_OR_RETURN(br->ReadBool(&temp));
580  used_by_curr_pic[j] = temp;
581 
582  if (!used_by_curr_pic[j]) {
583  TRUE_OR_RETURN(br->ReadBool(&temp));
584  use_delta[j] = temp;
585  } else {
586  use_delta[j] = true;
587  }
588  }
589 
590  int ref_num_positive_pics = ref_pic_sets[ref_rps_idx].num_positive_pics;
591  int ref_num_negative_pics = ref_pic_sets[ref_rps_idx].num_negative_pics;
592  int i;
593 
594  // Update list 0.
595  {
596  i = 0;
597  for (int j = ref_num_positive_pics - 1; j >= 0; j--) {
598  int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s1[j] + delta_rps;
599  if (d_poc < 0 && use_delta[ref_num_negative_pics + j]) {
600  out_ref_pic_set->delta_poc_s0[i] = d_poc;
601  out_ref_pic_set->used_by_curr_pic_s0[i] =
602  used_by_curr_pic[ref_num_negative_pics + j];
603  i++;
604  }
605  }
606  if (delta_rps < 0 && use_delta[ref_num_delta_pocs]) {
607  out_ref_pic_set->delta_poc_s0[i] = delta_rps;
608  out_ref_pic_set->used_by_curr_pic_s0[i] =
609  used_by_curr_pic[ref_num_delta_pocs];
610  i++;
611  }
612  for (int j = 0; j < ref_num_negative_pics; j++) {
613  int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s0[j] + delta_rps;
614  if (d_poc < 0 && use_delta[j]) {
615  out_ref_pic_set->delta_poc_s0[i] = d_poc;
616  out_ref_pic_set->used_by_curr_pic_s0[i] = used_by_curr_pic[j];
617  i++;
618  }
619  }
620  out_ref_pic_set->num_negative_pics = i;
621  }
622 
623  // Update list 1.
624  {
625  i = 0;
626  for (int j = ref_num_negative_pics - 1; j >= 0; j--) {
627  int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s0[j] + delta_rps;
628  if (d_poc > 0 && use_delta[j]) {
629  out_ref_pic_set->delta_poc_s1[i] = d_poc;
630  out_ref_pic_set->used_by_curr_pic_s1[i] = used_by_curr_pic[j];
631  i++;
632  }
633  }
634  if (delta_rps > 0 && use_delta[ref_num_delta_pocs]) {
635  out_ref_pic_set->delta_poc_s1[i] = delta_rps;
636  out_ref_pic_set->used_by_curr_pic_s1[i] =
637  used_by_curr_pic[ref_num_delta_pocs];
638  i++;
639  }
640  for (int j = 0; j < ref_num_positive_pics; j++) {
641  int d_poc = ref_pic_sets[ref_rps_idx].delta_poc_s1[j] + delta_rps;
642  if (d_poc > 0 && use_delta[ref_num_negative_pics + j]) {
643  out_ref_pic_set->delta_poc_s1[i] = d_poc;
644  out_ref_pic_set->used_by_curr_pic_s1[i] =
645  used_by_curr_pic[ref_num_negative_pics + j];
646  i++;
647  }
648  }
649  out_ref_pic_set->num_positive_pics = i;
650  }
651  } else {
652  TRUE_OR_RETURN(br->ReadUE(&out_ref_pic_set->num_negative_pics));
653  TRUE_OR_RETURN(br->ReadUE(&out_ref_pic_set->num_positive_pics));
654 
655  int prev_poc = 0;
656  for (int i = 0; i < out_ref_pic_set->num_negative_pics; i++) {
657  int delta_poc_s0_minus1;
658  TRUE_OR_RETURN(br->ReadUE(&delta_poc_s0_minus1));
659  out_ref_pic_set->delta_poc_s0[i] = prev_poc - (delta_poc_s0_minus1 + 1);
660  prev_poc = out_ref_pic_set->delta_poc_s0[i];
661 
662  TRUE_OR_RETURN(br->ReadBool(&out_ref_pic_set->used_by_curr_pic_s0[i]));
663  }
664 
665  prev_poc = 0;
666  for (int i = 0; i < out_ref_pic_set->num_positive_pics; i++) {
667  int delta_poc_s1_minus1;
668  TRUE_OR_RETURN(br->ReadUE(&delta_poc_s1_minus1));
669  out_ref_pic_set->delta_poc_s1[i] = prev_poc + delta_poc_s1_minus1 + 1;
670  prev_poc = out_ref_pic_set->delta_poc_s1[i];
671 
672  TRUE_OR_RETURN(br->ReadBool(&out_ref_pic_set->used_by_curr_pic_s1[i]));
673  }
674  }
675 
676  out_ref_pic_set->num_delta_pocs =
677  out_ref_pic_set->num_positive_pics + out_ref_pic_set->num_negative_pics;
678  return kOk;
679 }
680 
681 H265Parser::Result H265Parser::SkipReferencePictureListModification(
682  const H265SliceHeader& slice_header,
683  const H265Pps& pps,
684  int num_pic_total_curr,
685  H26xBitReader* br) {
686  // Reads whole element but ignores it all.
687 
688  bool ref_pic_list_modification_flag_l0;
689  TRUE_OR_RETURN(br->ReadBool(&ref_pic_list_modification_flag_l0));
690  if (ref_pic_list_modification_flag_l0) {
691  for (int i = 0; i <= pps.num_ref_idx_l0_default_active_minus1; i++) {
692  TRUE_OR_RETURN(br->SkipBits(ceil(log2(num_pic_total_curr))));
693  }
694  }
695 
696  if (slice_header.slice_type == kBSlice) {
697  bool ref_pic_list_modification_flag_l1;
698  TRUE_OR_RETURN(br->ReadBool(&ref_pic_list_modification_flag_l1));
699  if (ref_pic_list_modification_flag_l1) {
700  for (int i = 0; i <= pps.num_ref_idx_l1_default_active_minus1; i++) {
701  TRUE_OR_RETURN(br->SkipBits(ceil(log2(num_pic_total_curr))));
702  }
703  }
704  }
705 
706  return kOk;
707 }
708 
709 H265Parser::Result H265Parser::SkipPredictionWeightTablePart(
710  int num_ref_idx_minus1,
711  int chroma_array_type,
712  H26xBitReader* br) {
713  // Reads whole element, ignores it.
714  int ignored;
715  std::vector<bool> luma_weight_flag(num_ref_idx_minus1 + 1);
716  std::vector<bool> chroma_weight_flag(num_ref_idx_minus1 + 1);
717 
718  for (int i = 0; i <= num_ref_idx_minus1; i++) {
719  bool temp;
720  TRUE_OR_RETURN(br->ReadBool(&temp));
721  luma_weight_flag[i] = temp;
722  }
723  if (chroma_array_type != 0) {
724  for (int i = 0; i <= num_ref_idx_minus1; i++) {
725  bool temp;
726  TRUE_OR_RETURN(br->ReadBool(&temp));
727  chroma_weight_flag[i] = temp;
728  }
729  }
730  for (int i = 0; i <= num_ref_idx_minus1; i++) {
731  if (luma_weight_flag[i]) {
732  TRUE_OR_RETURN(br->ReadSE(&ignored)); // delta_luma_weight_l#
733  TRUE_OR_RETURN(br->ReadSE(&ignored)); // luma_offset_l#
734  }
735  if (chroma_weight_flag[i]) {
736  for (int j = 0; j < 2; j++) {
737  TRUE_OR_RETURN(br->ReadSE(&ignored)); // delta_chroma_weight_l#
738  TRUE_OR_RETURN(br->ReadSE(&ignored)); // delta_chroma_offset_l#
739  }
740  }
741  }
742 
743  return kOk;
744 }
745 
746 H265Parser::Result H265Parser::SkipPredictionWeightTable(
747  bool is_b_slice,
748  const H265Sps& sps,
749  const H265SliceHeader& slice_header,
750  H26xBitReader* br) {
751  // Reads whole element, ignores it.
752  int ignored;
753  int chroma_array_type = sps.GetChromaArrayType();
754 
755  TRUE_OR_RETURN(br->ReadUE(&ignored)); // luma_log2_weight_denom
756  if (chroma_array_type != 0) {
757  TRUE_OR_RETURN(br->ReadSE(&ignored)); // delta_chroma_log2_weight_denom
758  }
759  OK_OR_RETURN(SkipPredictionWeightTablePart(
760  slice_header.num_ref_idx_l0_active_minus1, chroma_array_type, br));
761  if (is_b_slice) {
762  OK_OR_RETURN(SkipPredictionWeightTablePart(
763  slice_header.num_ref_idx_l1_active_minus1, chroma_array_type, br));
764  }
765 
766  return kOk;
767 }
768 
769 H265Parser::Result H265Parser::SkipProfileTierLevel(
770  bool profile_present,
771  int max_num_sub_layers_minus1,
772  H26xBitReader* br) {
773  // Reads whole element, ignores it.
774 
775  if (profile_present) {
776  // general_profile_space, general_tier_flag, general_profile_idc
777  // general_profile_compativility_flag
778  // general_progressive_source_flag
779  // general_interlaced_source_flag
780  // general_non_packed_constraint_flag
781  // general_frame_only_constraint_flag
782  // 44-bits of other flags
783  TRUE_OR_RETURN(br->SkipBits(2 + 1 + 5 + 32 + 4 + 44));
784  }
785 
786  TRUE_OR_RETURN(br->SkipBits(8)); // general_level_idc
787 
788  std::vector<bool> sub_layer_profile_present(max_num_sub_layers_minus1);
789  std::vector<bool> sub_layer_level_present(max_num_sub_layers_minus1);
790  for (int i = 0; i < max_num_sub_layers_minus1; i++) {
791  bool profile, level;
792  TRUE_OR_RETURN(br->ReadBool(&profile));
793  TRUE_OR_RETURN(br->ReadBool(&level));
794  sub_layer_profile_present[i] = profile;
795  sub_layer_level_present[i] = level;
796  }
797 
798  if (max_num_sub_layers_minus1 > 0) {
799  for (int i = max_num_sub_layers_minus1; i < 8; i++)
800  TRUE_OR_RETURN(br->SkipBits(2)); // reserved_zero_2bits
801  }
802 
803  for (int i = 0; i < max_num_sub_layers_minus1; i++) {
804  if (sub_layer_profile_present[i]) {
805  // sub_layer_profile_space, sub_layer_tier_flag, sub_layer_profile_idc
806  // sub_layer_profile_compatibility
807  // sub_layer_reserved_zero_43bits
808  // sub_layer_reserved_zero_bit
809  TRUE_OR_RETURN(br->SkipBits(2 + 1 + 5 + 32 + 4 + 43 + 1));
810  }
811  if (sub_layer_level_present[i]) {
812  TRUE_OR_RETURN(br->SkipBits(8));
813  }
814  }
815 
816  return kOk;
817 }
818 
819 H265Parser::Result H265Parser::SkipScalingListData(H26xBitReader* br) {
820  // Reads whole element, ignores it.
821  int ignored;
822  for (int size_id = 0; size_id < 4; size_id++) {
823  for (int matrix_id = 0; matrix_id < 6;
824  matrix_id += ((size_id == 3) ? 3 : 1)) {
825  bool scaling_list_pred_mode;
826  TRUE_OR_RETURN(br->ReadBool(&scaling_list_pred_mode));
827  if (!scaling_list_pred_mode) {
828  // scaling_list_pred_matrix_id_delta
829  TRUE_OR_RETURN(br->ReadUE(&ignored));
830  } else {
831  int coefNum = std::min(64, (1 << (4 + (size_id << 1))));
832  if (size_id > 1) {
833  TRUE_OR_RETURN(br->ReadSE(&ignored)); // scaling_list_dc_coef_minus8
834  }
835 
836  for (int i = 0; i < coefNum; i++) {
837  TRUE_OR_RETURN(br->ReadSE(&ignored)); // scaling_list_delta_coef
838  }
839  }
840  }
841  }
842 
843  return kOk;
844 }
845 
846 } // namespace media
847 } // namespace edash_packager
Result ParseSliceHeader(const Nalu &nalu, H265SliceHeader *slice_header)
Definition: h265_parser.cc:95
Result ParsePps(const Nalu &nalu, int *pps_id)
Definition: h265_parser.cc:312
Result ParseSps(const Nalu &nalu, int *sps_id)
Definition: h265_parser.cc:421
const H265Pps * GetPps(int pps_id)
Definition: h265_parser.cc:532
const H265Sps * GetSps(int sps_id)
Definition: h265_parser.cc:536