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