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