Shaka Packager SDK
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 PACKAGER_MEDIA_CODECS_H264_PARSER_H_
8 #define PACKAGER_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  int profile_idc;
39  bool constraint_set0_flag;
40  bool constraint_set1_flag;
41  bool constraint_set2_flag;
42  bool constraint_set3_flag;
43  bool constraint_set4_flag;
44  bool constraint_set5_flag;
45  int level_idc;
46  int seq_parameter_set_id;
47 
48  int chroma_format_idc;
49  bool separate_colour_plane_flag;
50  int bit_depth_luma_minus8;
51  int bit_depth_chroma_minus8;
52  bool qpprime_y_zero_transform_bypass_flag;
53 
54  bool seq_scaling_matrix_present_flag;
55  int scaling_list4x4[6][kH264ScalingList4x4Length];
56  int scaling_list8x8[6][kH264ScalingList8x8Length];
57 
58  int log2_max_frame_num_minus4;
59  int pic_order_cnt_type;
60  int log2_max_pic_order_cnt_lsb_minus4;
61  bool delta_pic_order_always_zero_flag;
62  int offset_for_non_ref_pic;
63  int offset_for_top_to_bottom_field;
64  int num_ref_frames_in_pic_order_cnt_cycle;
65  int expected_delta_per_pic_order_cnt_cycle; // calculated
66  int offset_for_ref_frame[255];
67  int max_num_ref_frames;
68  bool gaps_in_frame_num_value_allowed_flag;
69  int pic_width_in_mbs_minus1;
70  int pic_height_in_map_units_minus1;
71  bool frame_mbs_only_flag;
72  bool mb_adaptive_frame_field_flag;
73  bool direct_8x8_inference_flag;
74  bool frame_cropping_flag;
75  int frame_crop_left_offset;
76  int frame_crop_right_offset;
77  int frame_crop_top_offset;
78  int frame_crop_bottom_offset;
79 
80  bool vui_parameters_present_flag;
81  int sar_width; // Set to 0 when not specified.
82  int sar_height; // Set to 0 when not specified.
83  int transfer_characteristics;
84 
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  int pic_parameter_set_id;
94  int seq_parameter_set_id;
95  bool entropy_coding_mode_flag;
96  bool bottom_field_pic_order_in_frame_present_flag;
97  int num_slice_groups_minus1;
98  int num_ref_idx_l0_default_active_minus1;
99  int num_ref_idx_l1_default_active_minus1;
100  bool weighted_pred_flag;
101  int weighted_bipred_idc;
102  int pic_init_qp_minus26;
103  int pic_init_qs_minus26;
104  int chroma_qp_index_offset;
105  bool deblocking_filter_control_present_flag;
106  bool constrained_intra_pred_flag;
107  bool redundant_pic_cnt_present_flag;
108  bool transform_8x8_mode_flag;
109 
110  bool pic_scaling_matrix_present_flag;
111  int scaling_list4x4[6][kH264ScalingList4x4Length];
112  int scaling_list8x8[6][kH264ScalingList8x8Length];
113 
114  int second_chroma_qp_index_offset;
115 };
116 
118  int modification_of_pic_nums_idc;
119  union {
120  int abs_diff_pic_num_minus1;
121  int long_term_pic_num;
122  };
123 };
124 
126  bool luma_weight_flag[32];
127  bool chroma_weight_flag[32];
128  int luma_weight[32];
129  int luma_offset[32];
130  int chroma_weight[32][2];
131  int chroma_offset[32][2];
132 };
133 
135  int memory_mgmnt_control_operation;
136  int difference_of_pic_nums_minus1;
137  int long_term_pic_num;
138  int long_term_frame_idx;
139  int max_long_term_frame_idx_plus1;
140 };
141 
143  enum {
144  kRefListSize = 32,
145  kRefListModSize = kRefListSize
146  };
147 
148  enum Type {
149  kPSlice = 0,
150  kBSlice = 1,
151  kISlice = 2,
152  kSPSlice = 3,
153  kSISlice = 4,
154  };
155 
156  bool IsPSlice() const;
157  bool IsBSlice() const;
158  bool IsISlice() const;
159  bool IsSPSlice() const;
160  bool IsSISlice() const;
161 
162  bool idr_pic_flag; // from NAL header
163  int nal_ref_idc; // from NAL header
164  // Points to the beginning of the nal unit.
165  const uint8_t* nalu_data;
166 
167  // Size of whole nalu unit.
168  size_t nalu_size;
169 
170  // This is the size of the slice header not including the nalu header byte.
171  // Sturcture: |NALU Header| Slice Header | Slice Data |
172  // Size: |<- 8bits ->|<- header_bit_size ->|<- Rest of nalu ->|
173  // Note that this is not a field in the H.264 spec.
174  size_t header_bit_size;
175 
176  int first_mb_in_slice;
177  int slice_type;
178  int pic_parameter_set_id;
179  int colour_plane_id;
180  int frame_num;
181  bool field_pic_flag;
182  bool bottom_field_flag;
183  int idr_pic_id;
184  int pic_order_cnt_lsb;
185  int delta_pic_order_cnt_bottom;
186  int delta_pic_order_cnt[2];
187  int redundant_pic_cnt;
188  bool direct_spatial_mv_pred_flag;
189 
190  bool num_ref_idx_active_override_flag;
191  int num_ref_idx_l0_active_minus1;
192  int num_ref_idx_l1_active_minus1;
193  bool ref_pic_list_modification_flag_l0;
194  bool ref_pic_list_modification_flag_l1;
195  H264ModificationOfPicNum ref_list_l0_modifications[kRefListModSize];
196  H264ModificationOfPicNum ref_list_l1_modifications[kRefListModSize];
197 
198  int luma_log2_weight_denom;
199  int chroma_log2_weight_denom;
200 
201  H264WeightingFactors pred_weight_table_l0;
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  enum Type {
228  kSEIRecoveryPoint = 6,
229  };
230 
231  int type;
232  int payload_size;
233  union {
234  // Placeholder; in future more supported types will contribute to more
235  // union members here.
236  H264SEIRecoveryPoint recovery_point;
237  };
238 };
239 
240 // Class to parse an Annex-B H.264 stream,
241 // as specified in chapters 7 and Annex B of the H.264 spec.
242 class H264Parser {
243  public:
244  enum Result {
245  kOk,
246  kInvalidStream, // error in stream
247  kUnsupportedStream, // stream not supported by the parser
248  kEOStream, // end of stream
249  };
250 
251  H264Parser();
252  ~H264Parser();
253 
254  // NALU-specific parsing functions.
255 
256  // SPSes and PPSes are owned by the parser class and the memory for their
257  // structures is managed here, not by the caller, as they are reused
258  // across NALUs.
259  //
260  // Parse an SPS/PPS NALU and save their data in the parser, returning id
261  // of the parsed structure in |*pps_id|/|*sps_id|.
262  // To get a pointer to a given SPS/PPS structure, use GetSps()/GetPps(),
263  // passing the returned |*sps_id|/|*pps_id| as parameter.
264  Result ParseSps(const Nalu& nalu, int* sps_id);
265  Result ParsePps(const Nalu& nalu, int* pps_id);
266 
267  // Return a pointer to SPS/PPS with given |sps_id|/|pps_id| or NULL if not
268  // present.
269  const H264Sps* GetSps(int sps_id);
270  const H264Pps* GetPps(int pps_id);
271 
272  // Slice headers and SEI messages are not used across NALUs by the parser
273  // and can be discarded after current NALU, so the parser does not store
274  // them, nor does it manage their memory.
275  // The caller has to provide and manage it instead.
276 
277  // Parse a slice header, returning it in |*shdr|. |*nalu| must be set to
278  // the NALU returned from AdvanceToNextNALU() and corresponding to |*shdr|.
279  Result ParseSliceHeader(const Nalu& nalu, H264SliceHeader* shdr);
280 
281  // Parse a SEI message, returning it in |*sei_msg|, provided and managed
282  // by the caller.
283  Result ParseSEI(const Nalu& nalu, H264SEIMessage* sei_msg);
284 
285  private:
286  // Parse scaling lists (see spec).
287  Result ParseScalingList(H26xBitReader* br,
288  int size,
289  int* scaling_list,
290  bool* use_default);
291  Result ParseSpsScalingLists(H26xBitReader* br, H264Sps* sps);
292  Result ParsePpsScalingLists(H26xBitReader* br,
293  const H264Sps& sps,
294  H264Pps* pps);
295 
296  // Parse optional VUI parameters in SPS (see spec).
297  Result ParseVUIParameters(H26xBitReader* br, H264Sps* sps);
298  // Set |hrd_parameters_present| to true only if they are present.
299  Result ParseAndIgnoreHRDParameters(H26xBitReader* br,
300  bool* hrd_parameters_present);
301 
302  // Parse reference picture lists' modifications (see spec).
303  Result ParseRefPicListModifications(H26xBitReader* br, H264SliceHeader* shdr);
304  Result ParseRefPicListModification(H26xBitReader* br,
305  int num_ref_idx_active_minus1,
306  H264ModificationOfPicNum* ref_list_mods);
307 
308  // Parse prediction weight table (see spec).
309  Result ParsePredWeightTable(H26xBitReader* br,
310  const H264Sps& sps,
311  H264SliceHeader* shdr);
312 
313  // Parse weighting factors (see spec).
314  Result ParseWeightingFactors(H26xBitReader* br,
315  int num_ref_idx_active_minus1,
316  int chroma_array_type,
317  int luma_log2_weight_denom,
318  int chroma_log2_weight_denom,
319  H264WeightingFactors* w_facts);
320 
321  // Parse decoded reference picture marking information (see spec).
322  Result ParseDecRefPicMarking(H26xBitReader* br, H264SliceHeader* shdr);
323 
324  // PPSes and SPSes stored for future reference.
325  typedef std::map<int, std::unique_ptr<H264Sps>> SpsById;
326  typedef std::map<int, std::unique_ptr<H264Pps>> PpsById;
327  SpsById active_SPSes_;
328  PpsById active_PPSes_;
329 
330  DISALLOW_COPY_AND_ASSIGN(H264Parser);
331 };
332 
333 } // namespace media
334 } // namespace shaka
335 
336 #endif // PACKAGER_MEDIA_CODECS_H264_PARSER_H_
All the methods that are virtual are virtual for mocking.