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