DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs 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/h264_bit_reader.h"
16 #include "packager/media/filters/nalu_reader.h"
17 
18 namespace edash_packager {
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  // Exp-Golomb code parsing as specified in chapter 9.1 of the spec.
289  // Read one unsigned exp-Golomb code from the stream and return in |*val|.
290  Result ReadUE(H264BitReader* br, int* val);
291 
292  // Read one signed exp-Golomb code from the stream and return in |*val|.
293  Result ReadSE(H264BitReader* br, int* val);
294 
295  // Parse scaling lists (see spec).
296  Result ParseScalingList(H264BitReader* br,
297  int size,
298  int* scaling_list,
299  bool* use_default);
300  Result ParseSPSScalingLists(H264BitReader* br, H264SPS* sps);
301  Result ParsePPSScalingLists(H264BitReader* br,
302  const H264SPS& sps,
303  H264PPS* pps);
304 
305  // Parse optional VUI parameters in SPS (see spec).
306  Result ParseVUIParameters(H264BitReader* br, H264SPS* sps);
307  // Set |hrd_parameters_present| to true only if they are present.
308  Result ParseAndIgnoreHRDParameters(H264BitReader* br,
309  bool* hrd_parameters_present);
310 
311  // Parse reference picture lists' modifications (see spec).
312  Result ParseRefPicListModifications(H264BitReader* br, H264SliceHeader* shdr);
313  Result ParseRefPicListModification(H264BitReader* br,
314  int num_ref_idx_active_minus1,
315  H264ModificationOfPicNum* ref_list_mods);
316 
317  // Parse prediction weight table (see spec).
318  Result ParsePredWeightTable(H264BitReader* br,
319  const H264SPS& sps,
320  H264SliceHeader* shdr);
321 
322  // Parse weighting factors (see spec).
323  Result ParseWeightingFactors(H264BitReader* br,
324  int num_ref_idx_active_minus1,
325  int chroma_array_type,
326  int luma_log2_weight_denom,
327  int chroma_log2_weight_denom,
328  H264WeightingFactors* w_facts);
329 
330  // Parse decoded reference picture marking information (see spec).
331  Result ParseDecRefPicMarking(H264BitReader* br, H264SliceHeader* shdr);
332 
333  // PPSes and SPSes stored for future reference.
334  typedef std::map<int, H264SPS*> SPSById;
335  typedef std::map<int, H264PPS*> PPSById;
336  SPSById active_SPSes_;
337  PPSById active_PPSes_;
338 
339  DISALLOW_COPY_AND_ASSIGN(H264Parser);
340 };
341 
342 } // namespace media
343 } // namespace edash_packager
344 
345 #endif // MEDIA_FILTERS_H264_PARSER_H_