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