Shaka Packager SDK
Loading...
Searching...
No Matches
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 <cstdint>
11#include <cstdlib>
12#include <map>
13#include <memory>
14
15#include <packager/macros/classes.h>
16#include <packager/media/codecs/h26x_bit_reader.h>
17#include <packager/media/codecs/nalu_reader.h>
18
19namespace shaka {
20namespace 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.
25struct H264Sps;
26bool 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
32enum {
33 kH264ScalingList4x4Length = 16,
34 kH264ScalingList8x8Length = 64,
35};
36
37struct 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 int color_primaries;
85 int matrix_coefficients;
86
87 bool timing_info_present_flag;
88 long num_units_in_tick;
89 long time_scale;
90 bool fixed_frame_rate_flag;
91
92 bool bitstream_restriction_flag;
93 int max_num_reorder_frames;
94 int max_dec_frame_buffering;
95
96 int chroma_array_type;
97};
98
99struct H264Pps {
100 int pic_parameter_set_id;
101 int seq_parameter_set_id;
102 bool entropy_coding_mode_flag;
103 bool bottom_field_pic_order_in_frame_present_flag;
104 int num_slice_groups_minus1;
105 int num_ref_idx_l0_default_active_minus1;
106 int num_ref_idx_l1_default_active_minus1;
107 bool weighted_pred_flag;
108 int weighted_bipred_idc;
109 int pic_init_qp_minus26;
110 int pic_init_qs_minus26;
111 int chroma_qp_index_offset;
112 bool deblocking_filter_control_present_flag;
113 bool constrained_intra_pred_flag;
114 bool redundant_pic_cnt_present_flag;
115 bool transform_8x8_mode_flag;
116
117 bool pic_scaling_matrix_present_flag;
118 int scaling_list4x4[6][kH264ScalingList4x4Length];
119 int scaling_list8x8[6][kH264ScalingList8x8Length];
120
121 int second_chroma_qp_index_offset;
122};
123
125 int modification_of_pic_nums_idc;
126 union {
127 int abs_diff_pic_num_minus1;
128 int long_term_pic_num;
129 };
130};
131
133 bool luma_weight_flag[32];
134 bool chroma_weight_flag[32];
135 int luma_weight[32];
136 int luma_offset[32];
137 int chroma_weight[32][2];
138 int chroma_offset[32][2];
139};
140
142 int memory_mgmnt_control_operation;
143 int difference_of_pic_nums_minus1;
144 int long_term_pic_num;
145 int long_term_frame_idx;
146 int max_long_term_frame_idx_plus1;
147};
148
150 enum { kRefListSize = 32, kRefListModSize = kRefListSize };
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 enum Type {
232 kSEIRecoveryPoint = 6,
233 };
234
235 int type;
236 int payload_size;
237 union {
238 // Placeholder; in future more supported types will contribute to more
239 // union members here.
240 H264SEIRecoveryPoint recovery_point;
241 };
242};
243
244// Class to parse an Annex-B H.264 stream,
245// as specified in chapters 7 and Annex B of the H.264 spec.
247 public:
248 enum Result {
249 kOk,
250 kInvalidStream, // error in stream
251 kUnsupportedStream, // stream not supported by the parser
252 kEOStream, // end of stream
253 };
254
255 H264Parser();
256 ~H264Parser();
257
258 // NALU-specific parsing functions.
259
260 // SPSes and PPSes are owned by the parser class and the memory for their
261 // structures is managed here, not by the caller, as they are reused
262 // across NALUs.
263 //
264 // Parse an SPS/PPS NALU and save their data in the parser, returning id
265 // of the parsed structure in |*pps_id|/|*sps_id|.
266 // To get a pointer to a given SPS/PPS structure, use GetSps()/GetPps(),
267 // passing the returned |*sps_id|/|*pps_id| as parameter.
268 Result ParseSps(const Nalu& nalu, int* sps_id);
269 Result ParsePps(const Nalu& nalu, int* pps_id);
270
271 // Return a pointer to SPS/PPS with given |sps_id|/|pps_id| or NULL if not
272 // present.
273 const H264Sps* GetSps(int sps_id);
274 const H264Pps* GetPps(int pps_id);
275
276 // Slice headers and SEI messages are not used across NALUs by the parser
277 // and can be discarded after current NALU, so the parser does not store
278 // them, nor does it manage their memory.
279 // The caller has to provide and manage it instead.
280
281 // Parse a slice header, returning it in |*shdr|. |*nalu| must be set to
282 // the NALU returned from AdvanceToNextNALU() and corresponding to |*shdr|.
283 Result ParseSliceHeader(const Nalu& nalu, H264SliceHeader* shdr);
284
285 // Parse a SEI message, returning it in |*sei_msg|, provided and managed
286 // by the caller.
287 Result ParseSEI(const Nalu& nalu, H264SEIMessage* sei_msg);
288
289 private:
290 // Parse scaling lists (see spec).
291 Result ParseScalingList(H26xBitReader* br,
292 int size,
293 int* scaling_list,
294 bool* use_default);
295 Result ParseSpsScalingLists(H26xBitReader* br, H264Sps* sps);
296 Result ParsePpsScalingLists(H26xBitReader* br,
297 const H264Sps& sps,
298 H264Pps* pps);
299
300 // Parse optional VUI parameters in SPS (see spec).
301 Result ParseVUIParameters(H26xBitReader* br, H264Sps* sps);
302 // Set |hrd_parameters_present| to true only if they are present.
303 Result ParseAndIgnoreHRDParameters(H26xBitReader* br,
304 bool* hrd_parameters_present);
305
306 // Parse reference picture lists' modifications (see spec).
307 Result ParseRefPicListModifications(H26xBitReader* br, H264SliceHeader* shdr);
308 Result ParseRefPicListModification(H26xBitReader* br,
309 int num_ref_idx_active_minus1,
310 H264ModificationOfPicNum* ref_list_mods);
311
312 // Parse prediction weight table (see spec).
313 Result ParsePredWeightTable(H26xBitReader* br,
314 const H264Sps& sps,
315 H264SliceHeader* shdr);
316
317 // Parse weighting factors (see spec).
318 Result ParseWeightingFactors(H26xBitReader* br,
319 int num_ref_idx_active_minus1,
320 int chroma_array_type,
321 int luma_log2_weight_denom,
322 int chroma_log2_weight_denom,
323 H264WeightingFactors* w_facts);
324
325 // Parse decoded reference picture marking information (see spec).
326 Result ParseDecRefPicMarking(H26xBitReader* br, H264SliceHeader* shdr);
327
328 // PPSes and SPSes stored for future reference.
329 typedef std::map<int, std::unique_ptr<H264Sps>> SpsById;
330 typedef std::map<int, std::unique_ptr<H264Pps>> PpsById;
331 SpsById active_SPSes_;
332 PpsById active_PPSes_;
333
334 DISALLOW_COPY_AND_ASSIGN(H264Parser);
335};
336
337} // namespace media
338} // namespace shaka
339
340#endif // PACKAGER_MEDIA_CODECS_H264_PARSER_H_
All the methods that are virtual are virtual for mocking.