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