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