DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
h265_parser.h
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 #ifndef MEDIA_FILTERS_H265_PARSER_H_
8 #define MEDIA_FILTERS_H265_PARSER_H_
9 
10 #include <map>
11 #include <vector>
12 
13 #include "packager/base/memory/scoped_ptr.h"
14 #include "packager/media/filters/h26x_bit_reader.h"
15 
16 namespace shaka {
17 namespace media {
18 
19 class Nalu;
20 
21 enum H265SliceType { kBSlice = 0, kPSlice = 1, kISlice = 2 };
22 
23 const int kMaxRefPicSetCount = 16;
24 
25 // On success, |coded_width| and |coded_height| contains coded resolution after
26 // cropping; |pixel_width:pixel_height| contains pixel aspect ratio, 1:1 is
27 // assigned if it is not present in SPS.
28 struct H265Sps;
29 bool ExtractResolutionFromSps(const H265Sps& sps,
30  uint32_t* coded_width,
31  uint32_t* coded_height,
32  uint32_t* pixel_width,
33  uint32_t* pixel_height);
34 
36  int delta_poc_s0[kMaxRefPicSetCount];
37  int delta_poc_s1[kMaxRefPicSetCount];
38  bool used_by_curr_pic_s0[kMaxRefPicSetCount];
39  bool used_by_curr_pic_s1[kMaxRefPicSetCount];
40 
41  int num_negative_pics;
42  int num_positive_pics;
43  int num_delta_pocs;
44 };
45 
47  enum { kExtendedSar = 255 };
48 
49  bool aspect_ratio_info_present_flag = false;
50  int aspect_ratio_idc = 0;
51  int sar_width = 0;
52  int sar_height = 0;
53 
54  bool bitstream_restriction_flag = false;
55  int min_spatial_segmentation_idc = 0;
56 
57  // Incomplete...
58 };
59 
60 struct H265Pps {
61  H265Pps();
62  ~H265Pps();
63 
64  // Many of the fields here are required when parsing so the default here may
65  // not be valid.
66 
67  int pic_parameter_set_id = 0;
68  int seq_parameter_set_id = 0;
69 
70  bool dependent_slice_segments_enabled_flag = false;
71  bool output_flag_present_flag = false;
72  int num_extra_slice_header_bits = 0;
73  bool sign_data_hiding_enabled_flag = false;
74  bool cabac_init_present_flag = false;
75 
76  int num_ref_idx_l0_default_active_minus1 = 0;
77  int num_ref_idx_l1_default_active_minus1 = 0;
78  int init_qp_minus26 = 0;
79  bool constrained_intra_pred_flag = false;
80  bool transform_skip_enabled_flag = false;
81 
82  bool cu_qp_delta_enabled_flag = 0;
83  int diff_cu_qp_delta_depth = 0;
84  int cb_qp_offset = 0;
85  int cr_qp_offset = 0;
86 
87  bool slice_chroma_qp_offsets_present_flag = false;
88  bool weighted_pred_flag = false;
89  bool weighted_bipred_flag = false;
90  bool transquant_bypass_enabled_flag = false;
91  bool tiles_enabled_flag = false;
92  bool entropy_coding_sync_enabled_flag = false;
93 
94  int num_tile_columns_minus1 = 0;
95  int num_tile_rows_minus1 = 0;
96  bool uniform_spacing_flag = true;
97  std::vector<int> column_width_minus1;
98  std::vector<int> row_height_minus1;
99  bool loop_filter_across_tiles_enabled_flag = true;
100 
101  bool loop_filter_across_slices_enabled_flag = false;
102  bool deblocking_filter_control_present_flag = false;
103  bool deblocking_filter_override_enabled_flag = false;
104  bool deblocking_filter_disabled_flag = false;
105  int beta_offset_div2 = 0;
106  int tc_offset_div2 = 0;
107 
108  bool scaling_list_data_present_flag = false;
109  // Ignored: scaling_list_data( )
110 
111  bool lists_modification_present_flag = false;
112  int log2_parallel_merge_level_minus2 = 0;
113  bool slice_segment_header_extension_present_flag = false;
114 
115  // Incomplete: pps_range_extension:
116  bool chroma_qp_offset_list_enabled_flag = false;
117 
118  // Ignored: extensions...
119 };
120 
121 struct H265Sps {
122  H265Sps();
123  ~H265Sps();
124 
125  int GetPicSizeInCtbsY() const;
126  int GetChromaArrayType() const;
127 
128  // Many of the fields here are required when parsing so the default here may
129  // not be valid.
130 
131  int video_parameter_set_id = 0;
132  int max_sub_layers_minus1 = 0;
133  bool temporal_id_nesting_flag = false;
134  // Ignored: profile_tier_level(...)
135 
136  int seq_parameter_set_id = 0;
137 
138  int chroma_format_idc = 0;
139  bool separate_colour_plane_flag = false;
140  int pic_width_in_luma_samples = 0;
141  int pic_height_in_luma_samples = 0;
142 
143  bool conformance_window_flag = false;
144  int conf_win_left_offset = 0;
145  int conf_win_right_offset = 0;
146  int conf_win_top_offset = 0;
147  int conf_win_bottom_offset = 0;
148 
149  int bit_depth_luma_minus8 = 0;
150  int bit_depth_chroma_minus8 = 0;
151  int log2_max_pic_order_cnt_lsb_minus4 = 0;
152 
153  bool sub_layer_ordering_info_present_flag = false;
154  int max_dec_pic_buffering_minus1[8];
155  int max_num_reorder_pics[8];
156  int max_latency_increase_plus1[8];
157 
158  int log2_min_luma_coding_block_size_minus3 = 0;
159  int log2_diff_max_min_luma_coding_block_size = 0;
160  int log2_min_luma_transform_block_size_minus2 = 0;
161  int log2_diff_max_min_luma_transform_block_size = 0;
162  int max_transform_hierarchy_depth_inter = 0;
163  int max_transform_hierarchy_depth_intra = 0;
164 
165  bool scaling_list_enabled_flag = false;
166  bool scaling_list_data_present_flag = false;
167  // Ignored: scaling_list_data()
168 
169  bool amp_enabled_flag = false;
170  bool sample_adaptive_offset_enabled_flag = false;
171  bool pcm_enabled_flag = false;
172  int pcm_sample_bit_depth_luma_minus1 = 0;
173  int pcm_sample_bit_depth_chroma_minus1 = 0;
174  int log2_min_pcm_luma_coding_block_size_minus3 = 0;
175  int log2_diff_max_min_pcm_luma_coding_block_size = 0;
176  bool pcm_loop_filter_disabled_flag = false;
177 
178  int num_short_term_ref_pic_sets = 0;
179  std::vector<H265ReferencePictureSet> st_ref_pic_sets;
180 
181  bool long_term_ref_pic_present_flag = false;
182  int num_long_term_ref_pics = 0;
183  std::vector<int> lt_ref_pic_poc_lsb;
184  std::vector<bool> used_by_curr_pic_lt_flag;
185 
186  bool temporal_mvp_enabled_flag = false;
187  bool strong_intra_smoothing_enabled_flag = false;
188 
189  bool vui_parameters_present = false;
190  H265VuiParameters vui_parameters;
191 
192  // Ignored: extensions...
193 };
194 
198 
199  bool ref_pic_list_modification_flag_l0 = false;
200  std::vector<int> list_entry_l0;
201 
202  bool ref_pic_list_modification_flag_l1 = false;
203  std::vector<int> list_entry_l1;
204 };
205 
207  H265SliceHeader();
208  ~H265SliceHeader();
209 
211  bool delta_poc_msb_present_flag;
212  int delta_poc_msb_cycle_lt;
213  };
214  // This is the value UsedByCurrPicLt for the current slice segment. This
215  // value is calulated from the LongTermPicsInfo during parsing.
216  int used_by_curr_pic_lt = 0;
217 
218  // Many of the fields here are required when parsing so the default here may
219  // not be valid.
220 
221  int header_bit_size = 0;
222 
223  bool first_slice_segment_in_pic_flag = false;
224  bool no_output_of_prior_pics_flag = false;
225  int pic_parameter_set_id = 0;
226 
227  bool dependent_slice_segment_flag = false;
228  int segment_address = 0;
229  int slice_type = 0;
230  bool pic_output_flag = true;
231  int colour_plane_id = 0;
232  int slice_pic_order_cnt_lsb = 0;
233 
234  bool short_term_ref_pic_set_sps_flag = false;
235  H265ReferencePictureSet st_ref_pic_set;
236  int short_term_ref_pic_set_idx = 0;
237 
238  int num_long_term_sps = 0;
239  int num_long_term_pics = 0;
240  std::vector<LongTermPicsInfo> long_term_pics_info;
241 
242  bool slice_temporal_mvp_enabled_flag = false;
243  bool slice_sao_luma_flag = false;
244  bool slice_sao_chroma_flag = false;
245 
246  bool num_ref_idx_active_override_flag = false;
247  int num_ref_idx_l0_active_minus1 = 0;
248  int num_ref_idx_l1_active_minus1 = 0;
249 
250  H265ReferencePictureListModifications ref_pic_lists_modification;
251 
252  bool mvd_l1_zero_flag = false;
253  bool cabac_init_flag = false;
254  bool collocated_from_l0 = true;
255  int collocated_ref_idx = 0;
256 
257  int five_minus_max_num_merge_cand = 0;
258  int slice_qp_delta = 0;
259  int slice_cb_qp_offset = 0;
260  int slice_cr_qp_offset = 0;
261 
262  bool cu_chroma_qp_offset_enabled_flag = false;
263  bool deblocking_filter_override_flag = false;
264  bool slice_deblocking_filter_disabled_flag = false;
265  int slice_beta_offset_div2 = 0;
266  int slice_tc_offset_div2 = 0;
267  bool slice_loop_filter_across_slices_enabled_flag = false;
268 
269  int num_entry_point_offsets = 0;
270  int offset_len_minus1 = 0;
271  std::vector<int> entry_point_offset_minus1;
272 };
273 
277 class H265Parser {
278  public:
279  enum Result {
280  kOk,
281  kInvalidStream, // error in stream
282  kUnsupportedStream, // stream not supported by the parser
283  kEOStream, // end of stream
284  };
285 
286  H265Parser();
287  ~H265Parser();
288 
292  Result ParseSliceHeader(const Nalu& nalu, H265SliceHeader* slice_header);
293 
296  Result ParsePps(const Nalu& nalu, int* pps_id);
299  Result ParseSps(const Nalu& nalu, int* sps_id);
300 
302  const H265Pps* GetPps(int pps_id);
304  const H265Sps* GetSps(int sps_id);
305 
306  private:
307  Result ParseVuiParameters(int max_num_sub_layers_minus1,
308  H26xBitReader* br,
309  H265VuiParameters* vui);
310 
311  Result ParseReferencePictureSet(
312  int num_short_term_ref_pic_sets,
313  int st_rpx_idx,
314  const std::vector<H265ReferencePictureSet>& ref_pic_sets,
315  H26xBitReader* br,
316  H265ReferencePictureSet* st_ref_pic_set);
317 
318  Result SkipReferencePictureListModification(
319  const H265SliceHeader& slice_header,
320  const H265Pps& pps,
321  int num_pic_total_curr,
322  H26xBitReader* br);
323 
324  Result SkipPredictionWeightTablePart(int num_ref_idx_minus1,
325  int chroma_array_type,
326  H26xBitReader* br);
327 
328  Result SkipPredictionWeightTable(bool is_b_slice,
329  const H265Sps& sps,
330  const H265SliceHeader& slice_header,
331  H26xBitReader* br);
332 
333  Result SkipProfileTierLevel(bool profile_present,
334  int max_num_sub_layers_minus1,
335  H26xBitReader* br);
336 
337  Result SkipScalingListData(H26xBitReader* br);
338 
339  Result SkipHrdParameters(int max_num_sub_layers_minus1, H26xBitReader* br);
340 
341  Result SkipSubLayerHrdParameters(int cpb_cnt_minus1,
342  bool sub_pic_hdr_params_present_flag,
343  H26xBitReader* br);
344 
345  typedef std::map<int, H265Sps*> SpsById;
346  typedef std::map<int, H265Pps*> PpsById;
347 
348  SpsById active_spses_;
349  PpsById active_ppses_;
350 
351  DISALLOW_COPY_AND_ASSIGN(H265Parser);
352 };
353 
354 } // namespace media
355 } // namespace shaka
356 
357 #endif // MEDIA_FILTERS_H265_PARSER_H_
Result ParseSliceHeader(const Nalu &nalu, H265SliceHeader *slice_header)
Definition: h265_parser.cc:187
Result ParsePps(const Nalu &nalu, int *pps_id)
Definition: h265_parser.cc:404
const H265Sps * GetSps(int sps_id)
Definition: h265_parser.cc:634
const H265Pps * GetPps(int pps_id)
Definition: h265_parser.cc:630
Result ParseSps(const Nalu &nalu, int *sps_id)
Definition: h265_parser.cc:513