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