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  const uint8_t* nalu_data; // from NAL header
169  off_t nalu_size; // from NAL header
170  off_t header_bit_size; // calculated
171 
172  int first_mb_in_slice;
173  int slice_type;
174  int pic_parameter_set_id;
175  int colour_plane_id;
176  int frame_num;
177  bool field_pic_flag;
178  bool bottom_field_flag;
179  int idr_pic_id;
180  int pic_order_cnt_lsb;
181  int delta_pic_order_cnt_bottom;
182  int delta_pic_order_cnt[2];
183  int redundant_pic_cnt;
184  bool direct_spatial_mv_pred_flag;
185 
186  bool num_ref_idx_active_override_flag;
187  int num_ref_idx_l0_active_minus1;
188  int num_ref_idx_l1_active_minus1;
189  bool ref_pic_list_modification_flag_l0;
190  bool ref_pic_list_modification_flag_l1;
191  H264ModificationOfPicNum ref_list_l0_modifications[kRefListModSize];
192  H264ModificationOfPicNum ref_list_l1_modifications[kRefListModSize];
193 
194  int luma_log2_weight_denom;
195  int chroma_log2_weight_denom;
196 
197  bool luma_weight_l0_flag;
198  bool chroma_weight_l0_flag;
199  H264WeightingFactors pred_weight_table_l0;
200 
201  bool luma_weight_l1_flag;
202  bool chroma_weight_l1_flag;
203  H264WeightingFactors pred_weight_table_l1;
204 
205  bool no_output_of_prior_pics_flag;
206  bool long_term_reference_flag;
207 
208  bool adaptive_ref_pic_marking_mode_flag;
209  H264DecRefPicMarking ref_pic_marking[kRefListSize];
210 
211  int cabac_init_idc;
212  int slice_qp_delta;
213  bool sp_for_switch_flag;
214  int slice_qs_delta;
215  int disable_deblocking_filter_idc;
216  int slice_alpha_c0_offset_div2;
217  int slice_beta_offset_div2;
218 };
219 
221  int recovery_frame_cnt;
222  bool exact_match_flag;
223  bool broken_link_flag;
224  int changing_slice_group_idc;
225 };
226 
228  H264SEIMessage();
229 
230  enum Type {
231  kSEIRecoveryPoint = 6,
232  };
233 
234  int type;
235  int payload_size;
236  union {
237  // Placeholder; in future more supported types will contribute to more
238  // union members here.
239  H264SEIRecoveryPoint recovery_point;
240  };
241 };
242 
243 // Class to parse an Annex-B H.264 stream,
244 // as specified in chapters 7 and Annex B of the H.264 spec.
245 class H264Parser {
246  public:
247  enum Result {
248  kOk,
249  kInvalidStream, // error in stream
250  kUnsupportedStream, // stream not supported by the parser
251  kEOStream, // end of stream
252  };
253 
254  H264Parser();
255  ~H264Parser();
256 
257  // NALU-specific parsing functions.
258 
259  // SPSes and PPSes are owned by the parser class and the memory for their
260  // structures is managed here, not by the caller, as they are reused
261  // across NALUs.
262  //
263  // Parse an SPS/PPS NALU and save their data in the parser, returning id
264  // of the parsed structure in |*pps_id|/|*sps_id|.
265  // To get a pointer to a given SPS/PPS structure, use GetSps()/GetPps(),
266  // passing the returned |*sps_id|/|*pps_id| as parameter.
267  Result ParseSps(const Nalu& nalu, int* sps_id);
268  Result ParsePps(const Nalu& nalu, int* pps_id);
269 
270  // Return a pointer to SPS/PPS with given |sps_id|/|pps_id| or NULL if not
271  // present.
272  const H264Sps* GetSps(int sps_id);
273  const H264Pps* GetPps(int pps_id);
274 
275  // Slice headers and SEI messages are not used across NALUs by the parser
276  // and can be discarded after current NALU, so the parser does not store
277  // them, nor does it manage their memory.
278  // The caller has to provide and manage it instead.
279 
280  // Parse a slice header, returning it in |*shdr|. |*nalu| must be set to
281  // the NALU returned from AdvanceToNextNALU() and corresponding to |*shdr|.
282  Result ParseSliceHeader(const Nalu& nalu, H264SliceHeader* shdr);
283 
284  // Parse a SEI message, returning it in |*sei_msg|, provided and managed
285  // by the caller.
286  Result ParseSEI(const Nalu& nalu, H264SEIMessage* sei_msg);
287 
288  private:
289  // Parse scaling lists (see spec).
290  Result ParseScalingList(H26xBitReader* br,
291  int size,
292  int* scaling_list,
293  bool* use_default);
294  Result ParseSpsScalingLists(H26xBitReader* br, H264Sps* sps);
295  Result ParsePpsScalingLists(H26xBitReader* br,
296  const H264Sps& sps,
297  H264Pps* pps);
298 
299  // Parse optional VUI parameters in SPS (see spec).
300  Result ParseVUIParameters(H26xBitReader* br, H264Sps* sps);
301  // Set |hrd_parameters_present| to true only if they are present.
302  Result ParseAndIgnoreHRDParameters(H26xBitReader* br,
303  bool* hrd_parameters_present);
304 
305  // Parse reference picture lists' modifications (see spec).
306  Result ParseRefPicListModifications(H26xBitReader* br, H264SliceHeader* shdr);
307  Result ParseRefPicListModification(H26xBitReader* br,
308  int num_ref_idx_active_minus1,
309  H264ModificationOfPicNum* ref_list_mods);
310 
311  // Parse prediction weight table (see spec).
312  Result ParsePredWeightTable(H26xBitReader* br,
313  const H264Sps& sps,
314  H264SliceHeader* shdr);
315 
316  // Parse weighting factors (see spec).
317  Result ParseWeightingFactors(H26xBitReader* br,
318  int num_ref_idx_active_minus1,
319  int chroma_array_type,
320  int luma_log2_weight_denom,
321  int chroma_log2_weight_denom,
322  H264WeightingFactors* w_facts);
323 
324  // Parse decoded reference picture marking information (see spec).
325  Result ParseDecRefPicMarking(H26xBitReader* br, H264SliceHeader* shdr);
326 
327  // PPSes and SPSes stored for future reference.
328  typedef std::map<int, std::unique_ptr<H264Sps>> SpsById;
329  typedef std::map<int, std::unique_ptr<H264Pps>> PpsById;
330  SpsById active_SPSes_;
331  PpsById active_PPSes_;
332 
333  DISALLOW_COPY_AND_ASSIGN(H264Parser);
334 };
335 
336 } // namespace media
337 } // namespace shaka
338 
339 #endif // MEDIA_CODECS_H264_PARSER_H_