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