Shaka Packager SDK
h264_parser.h
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // This file contains an implementation of an H264 Annex-B video stream parser.
6 
7 #ifndef PACKAGER_MEDIA_CODECS_H264_PARSER_H_
8 #define PACKAGER_MEDIA_CODECS_H264_PARSER_H_
9 
10 #include <cstdint>
11 #include <cstdlib>
12 #include <map>
13 #include <memory>
14 
15 #include <packager/macros/classes.h>
16 #include <packager/media/codecs/h26x_bit_reader.h>
17 #include <packager/media/codecs/nalu_reader.h>
18 
19 namespace shaka {
20 namespace media {
21 
22 // On success, |coded_width| and |coded_height| contains coded resolution after
23 // cropping; |pixel_width:pixel_height| contains pixel aspect ratio, 1:1 is
24 // assigned if it is not present in SPS.
25 struct H264Sps;
26 bool ExtractResolutionFromSps(const H264Sps& sps,
27  uint32_t* coded_width,
28  uint32_t* coded_height,
29  uint32_t* pixel_width,
30  uint32_t* pixel_height);
31 
32 enum {
33  kH264ScalingList4x4Length = 16,
34  kH264ScalingList8x8Length = 64,
35 };
36 
37 struct H264Sps {
38  int profile_idc;
39  bool constraint_set0_flag;
40  bool constraint_set1_flag;
41  bool constraint_set2_flag;
42  bool constraint_set3_flag;
43  bool constraint_set4_flag;
44  bool constraint_set5_flag;
45  int level_idc;
46  int seq_parameter_set_id;
47 
48  int chroma_format_idc;
49  bool separate_colour_plane_flag;
50  int bit_depth_luma_minus8;
51  int bit_depth_chroma_minus8;
52  bool qpprime_y_zero_transform_bypass_flag;
53 
54  bool seq_scaling_matrix_present_flag;
55  int scaling_list4x4[6][kH264ScalingList4x4Length];
56  int scaling_list8x8[6][kH264ScalingList8x8Length];
57 
58  int log2_max_frame_num_minus4;
59  int pic_order_cnt_type;
60  int log2_max_pic_order_cnt_lsb_minus4;
61  bool delta_pic_order_always_zero_flag;
62  int offset_for_non_ref_pic;
63  int offset_for_top_to_bottom_field;
64  int num_ref_frames_in_pic_order_cnt_cycle;
65  int expected_delta_per_pic_order_cnt_cycle; // calculated
66  int offset_for_ref_frame[255];
67  int max_num_ref_frames;
68  bool gaps_in_frame_num_value_allowed_flag;
69  int pic_width_in_mbs_minus1;
70  int pic_height_in_map_units_minus1;
71  bool frame_mbs_only_flag;
72  bool mb_adaptive_frame_field_flag;
73  bool direct_8x8_inference_flag;
74  bool frame_cropping_flag;
75  int frame_crop_left_offset;
76  int frame_crop_right_offset;
77  int frame_crop_top_offset;
78  int frame_crop_bottom_offset;
79 
80  bool vui_parameters_present_flag;
81  int sar_width; // Set to 0 when not specified.
82  int sar_height; // Set to 0 when not specified.
83  int transfer_characteristics;
84  int color_primaries;
85  int matrix_coefficients;
86 
87  bool timing_info_present_flag;
88  long num_units_in_tick;
89  long time_scale;
90  bool fixed_frame_rate_flag;
91 
92  bool bitstream_restriction_flag;
93  int max_num_reorder_frames;
94  int max_dec_frame_buffering;
95 
96  int chroma_array_type;
97 };
98 
99 struct H264Pps {
100  int pic_parameter_set_id;
101  int seq_parameter_set_id;
102  bool entropy_coding_mode_flag;
103  bool bottom_field_pic_order_in_frame_present_flag;
104  int num_slice_groups_minus1;
105  int num_ref_idx_l0_default_active_minus1;
106  int num_ref_idx_l1_default_active_minus1;
107  bool weighted_pred_flag;
108  int weighted_bipred_idc;
109  int pic_init_qp_minus26;
110  int pic_init_qs_minus26;
111  int chroma_qp_index_offset;
112  bool deblocking_filter_control_present_flag;
113  bool constrained_intra_pred_flag;
114  bool redundant_pic_cnt_present_flag;
115  bool transform_8x8_mode_flag;
116 
117  bool pic_scaling_matrix_present_flag;
118  int scaling_list4x4[6][kH264ScalingList4x4Length];
119  int scaling_list8x8[6][kH264ScalingList8x8Length];
120 
121  int second_chroma_qp_index_offset;
122 };
123 
125  int modification_of_pic_nums_idc;
126  union {
127  int abs_diff_pic_num_minus1;
128  int long_term_pic_num;
129  };
130 };
131 
133  bool luma_weight_flag[32];
134  bool chroma_weight_flag[32];
135  int luma_weight[32];
136  int luma_offset[32];
137  int chroma_weight[32][2];
138  int chroma_offset[32][2];
139 };
140 
142  int memory_mgmnt_control_operation;
143  int difference_of_pic_nums_minus1;
144  int long_term_pic_num;
145  int long_term_frame_idx;
146  int max_long_term_frame_idx_plus1;
147 };
148 
150  enum {
151  kRefListSize = 32,
152  kRefListModSize = kRefListSize
153  };
154 
155  enum Type {
156  kPSlice = 0,
157  kBSlice = 1,
158  kISlice = 2,
159  kSPSlice = 3,
160  kSISlice = 4,
161  };
162 
163  bool IsPSlice() const;
164  bool IsBSlice() const;
165  bool IsISlice() const;
166  bool IsSPSlice() const;
167  bool IsSISlice() const;
168 
169  bool idr_pic_flag; // from NAL header
170  int nal_ref_idc; // from NAL header
171  // Points to the beginning of the nal unit.
172  const uint8_t* nalu_data;
173 
174  // Size of whole nalu unit.
175  size_t nalu_size;
176 
177  // This is the size of the slice header not including the nalu header byte.
178  // Sturcture: |NALU Header| Slice Header | Slice Data |
179  // Size: |<- 8bits ->|<- header_bit_size ->|<- Rest of nalu ->|
180  // Note that this is not a field in the H.264 spec.
181  size_t header_bit_size;
182 
183  int first_mb_in_slice;
184  int slice_type;
185  int pic_parameter_set_id;
186  int colour_plane_id;
187  int frame_num;
188  bool field_pic_flag;
189  bool bottom_field_flag;
190  int idr_pic_id;
191  int pic_order_cnt_lsb;
192  int delta_pic_order_cnt_bottom;
193  int delta_pic_order_cnt[2];
194  int redundant_pic_cnt;
195  bool direct_spatial_mv_pred_flag;
196 
197  bool num_ref_idx_active_override_flag;
198  int num_ref_idx_l0_active_minus1;
199  int num_ref_idx_l1_active_minus1;
200  bool ref_pic_list_modification_flag_l0;
201  bool ref_pic_list_modification_flag_l1;
202  H264ModificationOfPicNum ref_list_l0_modifications[kRefListModSize];
203  H264ModificationOfPicNum ref_list_l1_modifications[kRefListModSize];
204 
205  int luma_log2_weight_denom;
206  int chroma_log2_weight_denom;
207 
208  H264WeightingFactors pred_weight_table_l0;
209  H264WeightingFactors pred_weight_table_l1;
210 
211  bool no_output_of_prior_pics_flag;
212  bool long_term_reference_flag;
213 
214  bool adaptive_ref_pic_marking_mode_flag;
215  H264DecRefPicMarking ref_pic_marking[kRefListSize];
216 
217  int cabac_init_idc;
218  int slice_qp_delta;
219  bool sp_for_switch_flag;
220  int slice_qs_delta;
221  int disable_deblocking_filter_idc;
222  int slice_alpha_c0_offset_div2;
223  int slice_beta_offset_div2;
224 };
225 
227  int recovery_frame_cnt;
228  bool exact_match_flag;
229  bool broken_link_flag;
230  int changing_slice_group_idc;
231 };
232 
234  enum Type {
235  kSEIRecoveryPoint = 6,
236  };
237 
238  int type;
239  int payload_size;
240  union {
241  // Placeholder; in future more supported types will contribute to more
242  // union members here.
243  H264SEIRecoveryPoint recovery_point;
244  };
245 };
246 
247 // Class to parse an Annex-B H.264 stream,
248 // as specified in chapters 7 and Annex B of the H.264 spec.
249 class H264Parser {
250  public:
251  enum Result {
252  kOk,
253  kInvalidStream, // error in stream
254  kUnsupportedStream, // stream not supported by the parser
255  kEOStream, // end of stream
256  };
257 
258  H264Parser();
259  ~H264Parser();
260 
261  // NALU-specific parsing functions.
262 
263  // SPSes and PPSes are owned by the parser class and the memory for their
264  // structures is managed here, not by the caller, as they are reused
265  // across NALUs.
266  //
267  // Parse an SPS/PPS NALU and save their data in the parser, returning id
268  // of the parsed structure in |*pps_id|/|*sps_id|.
269  // To get a pointer to a given SPS/PPS structure, use GetSps()/GetPps(),
270  // passing the returned |*sps_id|/|*pps_id| as parameter.
271  Result ParseSps(const Nalu& nalu, int* sps_id);
272  Result ParsePps(const Nalu& nalu, int* pps_id);
273 
274  // Return a pointer to SPS/PPS with given |sps_id|/|pps_id| or NULL if not
275  // present.
276  const H264Sps* GetSps(int sps_id);
277  const H264Pps* GetPps(int pps_id);
278 
279  // Slice headers and SEI messages are not used across NALUs by the parser
280  // and can be discarded after current NALU, so the parser does not store
281  // them, nor does it manage their memory.
282  // The caller has to provide and manage it instead.
283 
284  // Parse a slice header, returning it in |*shdr|. |*nalu| must be set to
285  // the NALU returned from AdvanceToNextNALU() and corresponding to |*shdr|.
286  Result ParseSliceHeader(const Nalu& nalu, H264SliceHeader* shdr);
287 
288  // Parse a SEI message, returning it in |*sei_msg|, provided and managed
289  // by the caller.
290  Result ParseSEI(const Nalu& nalu, H264SEIMessage* sei_msg);
291 
292  private:
293  // Parse scaling lists (see spec).
294  Result ParseScalingList(H26xBitReader* br,
295  int size,
296  int* scaling_list,
297  bool* use_default);
298  Result ParseSpsScalingLists(H26xBitReader* br, H264Sps* sps);
299  Result ParsePpsScalingLists(H26xBitReader* br,
300  const H264Sps& sps,
301  H264Pps* pps);
302 
303  // Parse optional VUI parameters in SPS (see spec).
304  Result ParseVUIParameters(H26xBitReader* br, H264Sps* sps);
305  // Set |hrd_parameters_present| to true only if they are present.
306  Result ParseAndIgnoreHRDParameters(H26xBitReader* br,
307  bool* hrd_parameters_present);
308 
309  // Parse reference picture lists' modifications (see spec).
310  Result ParseRefPicListModifications(H26xBitReader* br, H264SliceHeader* shdr);
311  Result ParseRefPicListModification(H26xBitReader* br,
312  int num_ref_idx_active_minus1,
313  H264ModificationOfPicNum* ref_list_mods);
314 
315  // Parse prediction weight table (see spec).
316  Result ParsePredWeightTable(H26xBitReader* br,
317  const H264Sps& sps,
318  H264SliceHeader* shdr);
319 
320  // Parse weighting factors (see spec).
321  Result ParseWeightingFactors(H26xBitReader* br,
322  int num_ref_idx_active_minus1,
323  int chroma_array_type,
324  int luma_log2_weight_denom,
325  int chroma_log2_weight_denom,
326  H264WeightingFactors* w_facts);
327 
328  // Parse decoded reference picture marking information (see spec).
329  Result ParseDecRefPicMarking(H26xBitReader* br, H264SliceHeader* shdr);
330 
331  // PPSes and SPSes stored for future reference.
332  typedef std::map<int, std::unique_ptr<H264Sps>> SpsById;
333  typedef std::map<int, std::unique_ptr<H264Pps>> PpsById;
334  SpsById active_SPSes_;
335  PpsById active_PPSes_;
336 
337  DISALLOW_COPY_AND_ASSIGN(H264Parser);
338 };
339 
340 } // namespace media
341 } // namespace shaka
342 
343 #endif // PACKAGER_MEDIA_CODECS_H264_PARSER_H_
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66