Reland r246152 cros: Change how multi-profile is enabled.
[chromium-blink-merge.git] / media / filters / h264_parser.h
blob7f6eb3fff2769442fa813d28e6a23ccdee923541
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.
7 #ifndef MEDIA_FILTERS_H264_PARSER_H_
8 #define MEDIA_FILTERS_H264_PARSER_H_
10 #include <sys/types.h>
12 #include <map>
14 #include "base/basictypes.h"
15 #include "media/base/media_export.h"
16 #include "media/filters/h264_bit_reader.h"
18 namespace media {
20 // For explanations of each struct and its members, see H.264 specification
21 // at http://www.itu.int/rec/T-REC-H.264.
22 struct MEDIA_EXPORT H264NALU {
23 H264NALU();
25 enum Type {
26 kUnspecified = 0,
27 kNonIDRSlice = 1,
28 kIDRSlice = 5,
29 kSEIMessage = 6,
30 kSPS = 7,
31 kPPS = 8,
32 kEOSeq = 9,
33 kEOStream = 11,
34 kCodedSliceExtension = 20,
37 // After (without) start code; we don't own the underlying memory
38 // and a shallow copy should be made when copying this struct.
39 const uint8* data;
40 off_t size; // From after start code to start code of next NALU (or EOS).
42 int nal_ref_idc;
43 int nal_unit_type;
46 enum {
47 kH264ScalingList4x4Length = 16,
48 kH264ScalingList8x8Length = 64,
51 struct MEDIA_EXPORT H264SPS {
52 H264SPS();
54 int profile_idc;
55 int constraint_setx_flag;
56 int level_idc;
57 int seq_parameter_set_id;
59 int chroma_format_idc;
60 bool separate_colour_plane_flag;
61 int bit_depth_luma_minus8;
62 int bit_depth_chroma_minus8;
63 bool qpprime_y_zero_transform_bypass_flag;
65 bool seq_scaling_matrix_present_flag;
66 int scaling_list4x4[6][kH264ScalingList4x4Length];
67 int scaling_list8x8[6][kH264ScalingList8x8Length];
69 int log2_max_frame_num_minus4;
70 int pic_order_cnt_type;
71 int log2_max_pic_order_cnt_lsb_minus4;
72 bool delta_pic_order_always_zero_flag;
73 int offset_for_non_ref_pic;
74 int offset_for_top_to_bottom_field;
75 int num_ref_frames_in_pic_order_cnt_cycle;
76 int expected_delta_per_pic_order_cnt_cycle; // calculated
77 int offset_for_ref_frame[255];
78 int max_num_ref_frames;
79 bool gaps_in_frame_num_value_allowed_flag;
80 int pic_width_in_mbs_minus1;
81 int pic_height_in_map_units_minus1;
82 bool frame_mbs_only_flag;
83 bool mb_adaptive_frame_field_flag;
84 bool direct_8x8_inference_flag;
85 bool frame_cropping_flag;
86 int frame_crop_left_offset;
87 int frame_crop_right_offset;
88 int frame_crop_top_offset;
89 int frame_crop_bottom_offset;
90 bool vui_parameters_present_flag;
91 int chroma_array_type;
94 struct MEDIA_EXPORT H264PPS {
95 H264PPS();
97 int pic_parameter_set_id;
98 int seq_parameter_set_id;
99 bool entropy_coding_mode_flag;
100 bool bottom_field_pic_order_in_frame_present_flag;
101 int num_slice_groups_minus1;
102 // TODO(posciak): Slice groups not implemented, could be added at some point.
103 int num_ref_idx_l0_default_active_minus1;
104 int num_ref_idx_l1_default_active_minus1;
105 bool weighted_pred_flag;
106 int weighted_bipred_idc;
107 int pic_init_qp_minus26;
108 int pic_init_qs_minus26;
109 int chroma_qp_index_offset;
110 bool deblocking_filter_control_present_flag;
111 bool constrained_intra_pred_flag;
112 bool redundant_pic_cnt_present_flag;
113 bool transform_8x8_mode_flag;
115 bool pic_scaling_matrix_present_flag;
116 int scaling_list4x4[6][kH264ScalingList4x4Length];
117 int scaling_list8x8[6][kH264ScalingList8x8Length];
119 int second_chroma_qp_index_offset;
122 struct MEDIA_EXPORT H264ModificationOfPicNum {
123 int modification_of_pic_nums_idc;
124 union {
125 int abs_diff_pic_num_minus1;
126 int long_term_pic_num;
130 struct MEDIA_EXPORT H264WeightingFactors {
131 bool luma_weight_flag;
132 bool chroma_weight_flag;
133 int luma_weight[32];
134 int luma_offset[32];
135 int chroma_weight[32][2];
136 int chroma_offset[32][2];
139 struct MEDIA_EXPORT H264DecRefPicMarking {
140 int memory_mgmnt_control_operation;
141 int difference_of_pic_nums_minus1;
142 int long_term_pic_num;
143 int long_term_frame_idx;
144 int max_long_term_frame_idx_plus1;
147 struct MEDIA_EXPORT H264SliceHeader {
148 H264SliceHeader();
150 enum {
151 kRefListSize = 32,
152 kRefListModSize = kRefListSize
155 enum Type {
156 kPSlice = 0,
157 kBSlice = 1,
158 kISlice = 2,
159 kSPSlice = 3,
160 kSISlice = 4,
163 bool IsPSlice() const;
164 bool IsBSlice() const;
165 bool IsISlice() const;
166 bool IsSPSlice() const;
167 bool IsSISlice() const;
169 bool idr_pic_flag; // from NAL header
170 int nal_ref_idc; // from NAL header
171 const uint8* nalu_data; // from NAL header
172 off_t nalu_size; // from NAL header
173 off_t header_bit_size; // calculated
175 int first_mb_in_slice;
176 int slice_type;
177 int pic_parameter_set_id;
178 int colour_plane_id; // TODO(posciak): use this! http://crbug.com/139878
179 int frame_num;
180 bool field_pic_flag;
181 bool bottom_field_flag;
182 int idr_pic_id;
183 int pic_order_cnt_lsb;
184 int delta_pic_order_cnt_bottom;
185 int delta_pic_order_cnt[2];
186 int redundant_pic_cnt;
187 bool direct_spatial_mv_pred_flag;
189 bool num_ref_idx_active_override_flag;
190 int num_ref_idx_l0_active_minus1;
191 int num_ref_idx_l1_active_minus1;
192 bool ref_pic_list_modification_flag_l0;
193 bool ref_pic_list_modification_flag_l1;
194 H264ModificationOfPicNum ref_list_l0_modifications[kRefListModSize];
195 H264ModificationOfPicNum ref_list_l1_modifications[kRefListModSize];
197 int luma_log2_weight_denom;
198 int chroma_log2_weight_denom;
200 bool luma_weight_l0_flag;
201 bool chroma_weight_l0_flag;
202 H264WeightingFactors pred_weight_table_l0;
204 bool luma_weight_l1_flag;
205 bool chroma_weight_l1_flag;
206 H264WeightingFactors pred_weight_table_l1;
208 bool no_output_of_prior_pics_flag;
209 bool long_term_reference_flag;
211 bool adaptive_ref_pic_marking_mode_flag;
212 H264DecRefPicMarking ref_pic_marking[kRefListSize];
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;
223 struct H264SEIRecoveryPoint {
224 int recovery_frame_cnt;
225 bool exact_match_flag;
226 bool broken_link_flag;
227 int changing_slice_group_idc;
230 struct MEDIA_EXPORT H264SEIMessage {
231 H264SEIMessage();
233 enum Type {
234 kSEIRecoveryPoint = 6,
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;
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 MEDIA_EXPORT 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
257 H264Parser();
258 ~H264Parser();
260 void Reset();
261 // Set current stream pointer to |stream| of |stream_size| in bytes,
262 // |stream| owned by caller.
263 void SetStream(const uint8* stream, off_t stream_size);
265 // Read the stream to find the next NALU, identify it and return
266 // that information in |*nalu|. This advances the stream to the beginning
267 // of this NALU, but not past it, so subsequent calls to NALU-specific
268 // parsing functions (ParseSPS, etc.) will parse this NALU.
269 // If the caller wishes to skip the current NALU, it can call this function
270 // again, instead of any NALU-type specific parse functions below.
271 Result AdvanceToNextNALU(H264NALU* nalu);
273 // NALU-specific parsing functions.
274 // These should be called after AdvanceToNextNALU().
276 // SPSes and PPSes are owned by the parser class and the memory for their
277 // structures is managed here, not by the caller, as they are reused
278 // across NALUs.
280 // Parse an SPS/PPS NALU and save their data in the parser, returning id
281 // of the parsed structure in |*pps_id|/|*sps_id|.
282 // To get a pointer to a given SPS/PPS structure, use GetSPS()/GetPPS(),
283 // passing the returned |*sps_id|/|*pps_id| as parameter.
284 // TODO(posciak,fischman): consider replacing returning Result from Parse*()
285 // methods with a scoped_ptr and adding an AtEOS() function to check for EOS
286 // if Parse*() return NULL.
287 Result ParseSPS(int* sps_id);
288 Result ParsePPS(int* pps_id);
290 // Return a pointer to SPS/PPS with given |sps_id|/|pps_id| or NULL if not
291 // present.
292 const H264SPS* GetSPS(int sps_id);
293 const H264PPS* GetPPS(int pps_id);
295 // Slice headers and SEI messages are not used across NALUs by the parser
296 // and can be discarded after current NALU, so the parser does not store
297 // them, nor does it manage their memory.
298 // The caller has to provide and manage it instead.
300 // Parse a slice header, returning it in |*shdr|. |*nalu| must be set to
301 // the NALU returned from AdvanceToNextNALU() and corresponding to |*shdr|.
302 Result ParseSliceHeader(const H264NALU& nalu, H264SliceHeader* shdr);
304 // Parse a SEI message, returning it in |*sei_msg|, provided and managed
305 // by the caller.
306 Result ParseSEI(H264SEIMessage* sei_msg);
308 private:
309 // Exp-Golomb code parsing as specified in chapter 9.1 of the spec.
310 // Read one unsigned exp-Golomb code from the stream and return in |*val|.
311 Result ReadUE(int* val);
313 // Read one signed exp-Golomb code from the stream and return in |*val|.
314 Result ReadSE(int* val);
316 // Parse scaling lists (see spec).
317 Result ParseScalingList(int size, int* scaling_list, bool* use_default);
318 Result ParseSPSScalingLists(H264SPS* sps);
319 Result ParsePPSScalingLists(const H264SPS& sps, H264PPS* pps);
321 // Parse reference picture lists' modifications (see spec).
322 Result ParseRefPicListModifications(H264SliceHeader* shdr);
323 Result ParseRefPicListModification(int num_ref_idx_active_minus1,
324 H264ModificationOfPicNum* ref_list_mods);
326 // Parse prediction weight table (see spec).
327 Result ParsePredWeightTable(const H264SPS& sps, H264SliceHeader* shdr);
329 // Parse weighting factors (see spec).
330 Result ParseWeightingFactors(int num_ref_idx_active_minus1,
331 int chroma_array_type,
332 int luma_log2_weight_denom,
333 int chroma_log2_weight_denom,
334 H264WeightingFactors* w_facts);
336 // Parse decoded reference picture marking information (see spec).
337 Result ParseDecRefPicMarking(H264SliceHeader* shdr);
339 // Pointer to the current NALU in the stream.
340 const uint8* stream_;
342 // Bytes left in the stream after the current NALU.
343 off_t bytes_left_;
345 H264BitReader br_;
347 // PPSes and SPSes stored for future reference.
348 typedef std::map<int, H264SPS*> SPSById;
349 typedef std::map<int, H264PPS*> PPSById;
350 SPSById active_SPSes_;
351 PPSById active_PPSes_;
353 DISALLOW_COPY_AND_ASSIGN(H264Parser);
356 } // namespace media
358 #endif // MEDIA_FILTERS_H264_PARSER_H_