Shaka Packager SDK
Loading...
Searching...
No Matches
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 <cstddef>
11#include <cstdint>
12#include <map>
13#include <memory>
14#include <vector>
15
16#include <packager/macros/classes.h>
17#include <packager/media/codecs/h26x_bit_reader.h>
18
19namespace shaka {
20namespace media {
21
22class Nalu;
23
24enum H265SliceType { kBSlice = 0, kPSlice = 1, kISlice = 2 };
25
26const int kMaxRefPicSetCount = 16;
27
28// 12 bytes for general profile_tier_level() info:
29// 1 byte: general_profile_space, general_tier_flag, general_profile_idc
30// 4 bytes: general_profile_compatibility_flags
31// 6 bytes: general_constraint_indicator_flags
32// 1 byte: general_level_idc
33const int kGeneralProfileTierLevelBytes = 12;
34// The standard allows up to 64, but for MV-HEVC support for stereo video,
35// there is no need for more than 3.
36const int kMaxNumProfileTierLevels = 3;
37// The standard allows up to 64, but for MV-HEVC support for stereo video, only
38// 2 layers are needed for the two views.
39const int kMaxLayers = 2;
40// Only multiview scalability type is supported.
41const int kMaxScalabilityTypes = 1;
42// Only multiview scalability is supported; so num views equal num layers.
43const int kNumViews = kMaxLayers;
44// For MV-HEVC support for stereo video, only one extra rep_format() is needed.
45const int kMaxNumRepFromats = 1;
46// This is standard defined.
47const int kMaxLayerIdPlus1 = 64;
48// For stereo video support of MV-HEVC, no need for more than 3 layer sets.
49const int kMaxLayerSets = 3;
50// Only support the case where the output layer set is the same as the layer
51// set.
52const int kMaxOuputLayerSets = kMaxLayerSets;
53
54const int kInvalidId = -1;
55
56// On success, |coded_width| and |coded_height| contains coded resolution after
57// cropping; |pixel_width:pixel_height| contains pixel aspect ratio, 1:1 is
58// assigned if it is not present in SPS.
59struct H265Sps;
60bool ExtractResolutionFromSps(const H265Sps& sps,
61 uint32_t* coded_width,
62 uint32_t* coded_height,
63 uint32_t* pixel_width,
64 uint32_t* pixel_height);
65
67 int delta_poc_s0[kMaxRefPicSetCount];
68 int delta_poc_s1[kMaxRefPicSetCount];
69 bool used_by_curr_pic_s0[kMaxRefPicSetCount];
70 bool used_by_curr_pic_s1[kMaxRefPicSetCount];
71
72 int num_negative_pics;
73 int num_positive_pics;
74 int num_delta_pocs;
75};
76
78 enum { kExtendedSar = 255 };
79
80 bool aspect_ratio_info_present_flag = false;
81 int aspect_ratio_idc = 0;
82 int sar_width = 0;
83 int sar_height = 0;
84 int transfer_characteristics = 0;
85 int color_primaries = 0;
86 int matrix_coefficients = 0;
87
88 bool vui_timing_info_present_flag = false;
89 long vui_num_units_in_tick = 0;
90 long vui_time_scale = 0;
91
92 bool bitstream_restriction_flag = false;
93 int min_spatial_segmentation_idc = 0;
94
95 // Incomplete...
96};
97
98struct H265Pps {
99 H265Pps();
100 ~H265Pps();
101
102 // Many of the fields here are required when parsing so the default here may
103 // not be valid.
104
105 int pic_parameter_set_id = 0;
106 int seq_parameter_set_id = 0;
107
108 bool dependent_slice_segments_enabled_flag = false;
109 bool output_flag_present_flag = false;
110 int num_extra_slice_header_bits = 0;
111 bool sign_data_hiding_enabled_flag = false;
112 bool cabac_init_present_flag = false;
113
114 int num_ref_idx_l0_default_active_minus1 = 0;
115 int num_ref_idx_l1_default_active_minus1 = 0;
116 int init_qp_minus26 = 0;
117 bool constrained_intra_pred_flag = false;
118 bool transform_skip_enabled_flag = false;
119
120 bool cu_qp_delta_enabled_flag = 0;
121 int diff_cu_qp_delta_depth = 0;
122 int cb_qp_offset = 0;
123 int cr_qp_offset = 0;
124
125 bool slice_chroma_qp_offsets_present_flag = false;
126 bool weighted_pred_flag = false;
127 bool weighted_bipred_flag = false;
128 bool transquant_bypass_enabled_flag = false;
129 bool tiles_enabled_flag = false;
130 bool entropy_coding_sync_enabled_flag = false;
131
132 int num_tile_columns_minus1 = 0;
133 int num_tile_rows_minus1 = 0;
134 bool uniform_spacing_flag = true;
135 std::vector<int> column_width_minus1;
136 std::vector<int> row_height_minus1;
137 bool loop_filter_across_tiles_enabled_flag = true;
138
139 bool loop_filter_across_slices_enabled_flag = false;
140 bool deblocking_filter_control_present_flag = false;
141 bool deblocking_filter_override_enabled_flag = false;
142 bool deblocking_filter_disabled_flag = false;
143 int beta_offset_div2 = 0;
144 int tc_offset_div2 = 0;
145
146 bool scaling_list_data_present_flag = false;
147 // Ignored: scaling_list_data( )
148
149 bool lists_modification_present_flag = false;
150 int log2_parallel_merge_level_minus2 = 0;
151 bool slice_segment_header_extension_present_flag = false;
152
153 // Incomplete: pps_range_extension:
154 bool chroma_qp_offset_list_enabled_flag = false;
155
156 // Ignored: extensions...
157};
158
159struct H265Sps {
160 H265Sps();
161 ~H265Sps();
162
163 int GetPicSizeInCtbsY() const;
164 int GetChromaArrayType() const;
165
166 // Many of the fields here are required when parsing so the default here may
167 // not be valid.
168
169 int video_parameter_set_id = 0;
170 int max_sub_layers_minus1 = 0;
171 bool temporal_id_nesting_flag = false;
172
173 // general_profile_space (2), general_tier_flag (1), general_profile_idc (5),
174 // general_profile_compatibility_flags (32),
175 // general_constraint_indicator_flags (48), general_level_idc (8).
176 int general_profile_tier_level_data[12] = {};
177
178 int seq_parameter_set_id = 0;
179
180 int chroma_format_idc = 0;
181 bool separate_colour_plane_flag = false;
182 int pic_width_in_luma_samples = 0;
183 int pic_height_in_luma_samples = 0;
184
185 bool conformance_window_flag = false;
186 int conf_win_left_offset = 0;
187 int conf_win_right_offset = 0;
188 int conf_win_top_offset = 0;
189 int conf_win_bottom_offset = 0;
190
191 int bit_depth_luma_minus8 = 0;
192 int bit_depth_chroma_minus8 = 0;
193 int log2_max_pic_order_cnt_lsb_minus4 = 0;
194
195 bool sub_layer_ordering_info_present_flag = false;
196 int max_dec_pic_buffering_minus1[8];
197 int max_num_reorder_pics[8];
198 int max_latency_increase_plus1[8];
199
200 int log2_min_luma_coding_block_size_minus3 = 0;
201 int log2_diff_max_min_luma_coding_block_size = 0;
202 int log2_min_luma_transform_block_size_minus2 = 0;
203 int log2_diff_max_min_luma_transform_block_size = 0;
204 int max_transform_hierarchy_depth_inter = 0;
205 int max_transform_hierarchy_depth_intra = 0;
206
207 bool scaling_list_enabled_flag = false;
208 bool scaling_list_data_present_flag = false;
209 // Ignored: scaling_list_data()
210
211 bool amp_enabled_flag = false;
212 bool sample_adaptive_offset_enabled_flag = false;
213 bool pcm_enabled_flag = false;
214 int pcm_sample_bit_depth_luma_minus1 = 0;
215 int pcm_sample_bit_depth_chroma_minus1 = 0;
216 int log2_min_pcm_luma_coding_block_size_minus3 = 0;
217 int log2_diff_max_min_pcm_luma_coding_block_size = 0;
218 bool pcm_loop_filter_disabled_flag = false;
219
220 int num_short_term_ref_pic_sets = 0;
221 std::vector<H265ReferencePictureSet> st_ref_pic_sets;
222
223 bool long_term_ref_pic_present_flag = false;
224 int num_long_term_ref_pics = 0;
225 std::vector<int> lt_ref_pic_poc_lsb;
226 std::vector<bool> used_by_curr_pic_lt_flag;
227
228 bool temporal_mvp_enabled_flag = false;
229 bool strong_intra_smoothing_enabled_flag = false;
230
231 bool vui_parameters_present = false;
232 H265VuiParameters vui_parameters;
233
234 // Ignored: extensions...
235};
236
238 int pic_width_vps_in_luma_samples = 0;
239 int pic_height_vps_in_luma_samples = 0;
240
241 int chroma_format_vps_idc = 0;
242 bool separate_colour_plane_vps_flag = false;
243
244 int bit_depth_vps_luma_minus8 = 0;
245 int bit_depth_vps_chroma_minus8 = 0;
246
247 int conf_win_vps_left_offset = 0;
248 int conf_win_vps_right_offset = 0;
249 int conf_win_vps_top_offset = 0;
250 int conf_win_vps_bottom_offset = 0;
251};
252
253struct H265Vps {
254 H265Vps();
255 ~H265Vps();
256
257 enum {
258 kTexture = 0,
259 kMultiview = 1,
260 kSpatial = 2,
261 kAuxiliary = 3,
262 kNone = 16
263 };
264
265 // Many of the fields here are required when parsing so the default here may
266 // not be valid.
267
268 int scalability_type = kNone;
269
270 int vps_video_parameter_set_id = 0;
271 bool vps_base_layer_internal_flag = false;
272 bool vps_base_layer_available_flag = false;
273
274 int vps_max_layers_minus1 = 0;
275 int vps_max_sub_layers_minus1 = 0;
276 int sub_layers_vps_max_minus1[kMaxLayers];
277 int max_tid_il_ref_pics_plus1[kMaxLayers][kMaxLayers];
278
279 int layer_id_in_vps[kMaxLayerIdPlus1];
280 int view_id[kMaxLayerIdPlus1];
281
282 int num_views = 0;
283
284 int general_profile_tier_level_data[kMaxNumProfileTierLevels]
285 [kGeneralProfileTierLevelBytes];
286 int num_profile_tier_levels = 0;
287 int profile_tier_level_idx[kMaxOuputLayerSets][kMaxLayerIdPlus1];
288
289 H265RepFormat rep_format[kMaxNumRepFromats];
290 int num_rep_formats = 0;
291
292 int vps_max_layer_id = 0;
293 int vps_num_layer_sets_minus1 = 0;
294 int num_direct_ref_layers[kMaxLayerIdPlus1];
295 int id_direct_ref_layers[kMaxLayerIdPlus1][kMaxLayers];
296 bool default_ref_layers_active_flag = false;
297 bool max_one_active_ref_layer_flag = false;
298 bool poc_lsb_not_present_flag[kMaxLayers];
299};
300
304
305 bool ref_pic_list_modification_flag_l0 = false;
306 std::vector<int> list_entry_l0;
307
308 bool ref_pic_list_modification_flag_l1 = false;
309 std::vector<int> list_entry_l1;
310};
311
315
317 bool delta_poc_msb_present_flag;
318 int delta_poc_msb_cycle_lt;
319 };
320 // This is the value UsedByCurrPicLt for the current slice segment. This
321 // value is calulated from the LongTermPicsInfo during parsing.
322 int used_by_curr_pic_lt = 0;
323
324 // Many of the fields here are required when parsing so the default here may
325 // not be valid.
326
327 // This is the size of the slice header not including the nalu header byte.
328 // Sturcture: |NALU Header | Slice Header | Slice Data |
329 // Size: |<- 16bits ->|<- header_bit_size ->|<- Rest of nalu ->|
330 // Note that this is not a field in the H.265 spec.
331 size_t header_bit_size = 0;
332
333 bool first_slice_segment_in_pic_flag = false;
334 bool no_output_of_prior_pics_flag = false;
335 int pic_parameter_set_id = 0;
336
337 bool dependent_slice_segment_flag = false;
338 int segment_address = 0;
339 int slice_type = 0;
340 bool pic_output_flag = true;
341 int colour_plane_id = 0;
342 int slice_pic_order_cnt_lsb = 0;
343
344 bool short_term_ref_pic_set_sps_flag = false;
345 H265ReferencePictureSet st_ref_pic_set{};
346 int short_term_ref_pic_set_idx = 0;
347
348 int num_long_term_sps = 0;
349 int num_long_term_pics = 0;
350 std::vector<LongTermPicsInfo> long_term_pics_info;
351
352 bool slice_temporal_mvp_enabled_flag = false;
353 bool slice_sao_luma_flag = false;
354 bool slice_sao_chroma_flag = false;
355
356 bool num_ref_idx_active_override_flag = false;
357 int num_ref_idx_l0_active_minus1 = 0;
358 int num_ref_idx_l1_active_minus1 = 0;
359
360 H265ReferencePictureListModifications ref_pic_lists_modification;
361
362 bool mvd_l1_zero_flag = false;
363 bool cabac_init_flag = false;
364 bool collocated_from_l0 = true;
365 int collocated_ref_idx = 0;
366
367 int five_minus_max_num_merge_cand = 0;
368 int slice_qp_delta = 0;
369 int slice_cb_qp_offset = 0;
370 int slice_cr_qp_offset = 0;
371
372 bool cu_chroma_qp_offset_enabled_flag = false;
373 bool deblocking_filter_override_flag = false;
374 bool slice_deblocking_filter_disabled_flag = false;
375 int slice_beta_offset_div2 = 0;
376 int slice_tc_offset_div2 = 0;
377 bool slice_loop_filter_across_slices_enabled_flag = false;
378
379 int num_entry_point_offsets = 0;
380 int offset_len_minus1 = 0;
381 std::vector<int> entry_point_offset_minus1;
382
383 bool inter_layer_pred_enabled_flag = false;
384};
385
390 public:
391 enum Result {
392 kOk,
393 kInvalidStream, // error in stream
394 kUnsupportedStream, // stream not supported by the parser
395 kEOStream, // end of stream
396 };
397
398 H265Parser();
399 ~H265Parser();
400
404 Result ParseSliceHeader(const Nalu& nalu, H265SliceHeader* slice_header);
405
408 Result ParsePps(const Nalu& nalu, int* pps_id);
411 Result ParseSps(const Nalu& nalu, int* sps_id);
414 Result ParseVps(const Nalu& nalu, int* vps_id);
415
417 const H265Pps* GetPps(int pps_id);
419 const H265Sps* GetSps(int sps_id);
421 const H265Vps* GetVps(int vps_id);
422
423 private:
424 Result ParseVuiParameters(int max_num_sub_layers_minus1,
425 H26xBitReader* br,
426 H265VuiParameters* vui);
427
428 Result ParseReferencePictureSet(
429 int num_short_term_ref_pic_sets,
430 int st_rpx_idx,
431 const std::vector<H265ReferencePictureSet>& ref_pic_sets,
432 H26xBitReader* br,
433 H265ReferencePictureSet* st_ref_pic_set);
434
435 Result SkipReferencePictureListModification(
436 const H265SliceHeader& slice_header,
437 const H265Pps& pps,
438 int num_pic_total_curr,
439 H26xBitReader* br);
440
441 Result SkipPredictionWeightTablePart(int num_ref_idx_minus1,
442 int chroma_array_type,
443 H26xBitReader* br);
444
445 Result SkipPredictionWeightTable(bool is_b_slice,
446 const H265Sps& sps,
447 const H265SliceHeader& slice_header,
448 H26xBitReader* br);
449
450 Result ReadProfileTierLevel(bool profile_present,
451 int max_num_sub_layers_minus1,
452 H26xBitReader* br,
453 int* prev_data,
454 int* general_profile_tier_level_data);
455
456 Result SkipScalingListData(H26xBitReader* br);
457
458 Result SkipHrdParameters(bool common_inf_present_flag,
459 int max_num_sub_layers_minus1,
460 H26xBitReader* br);
461
462 Result SkipSubLayerHrdParameters(int cpb_cnt_minus1,
463 bool sub_pic_hdr_params_present_flag,
464 H26xBitReader* br);
465
466 Result ByteAlignment(H26xBitReader* br);
467
468 typedef std::map<int, std::unique_ptr<H265Vps>> VpsById;
469 typedef std::map<int, std::unique_ptr<H265Sps>> SpsById;
470 typedef std::map<int, std::unique_ptr<H265Pps>> PpsById;
471
472 VpsById active_vpses_;
473 SpsById active_spses_;
474 PpsById active_ppses_;
475
476 DISALLOW_COPY_AND_ASSIGN(H265Parser);
477};
478
479} // namespace media
480} // namespace shaka
481
482#endif // PACKAGER_MEDIA_CODECS_H265_PARSER_H_
Result ParseSps(const Nalu &nalu, int *sps_id)
Result ParsePps(const Nalu &nalu, int *pps_id)
const H265Vps * GetVps(int vps_id)
Result ParseVps(const Nalu &nalu, int *vps_id)
const H265Sps * GetSps(int sps_id)
Result ParseSliceHeader(const Nalu &nalu, H265SliceHeader *slice_header)
const H265Pps * GetPps(int pps_id)
All the methods that are virtual are virtual for mocking.